tvp514x.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /*
  2. * drivers/media/i2c/tvp514x.c
  3. *
  4. * TI TVP5146/47 decoder driver
  5. *
  6. * Copyright (C) 2008 Texas Instruments Inc
  7. * Author: Vaibhav Hiremath <hvaibhav@ti.com>
  8. *
  9. * Contributors:
  10. * Sivaraj R <sivaraj@ti.com>
  11. * Brijesh R Jadav <brijesh.j@ti.com>
  12. * Hardik Shah <hardik.shah@ti.com>
  13. * Manjunath Hadli <mrh@ti.com>
  14. * Karicheri Muralidharan <m-karicheri2@ti.com>
  15. * Prabhakar Lad <prabhakar.lad@ti.com>
  16. *
  17. * This package is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. */
  31. #include <linux/i2c.h>
  32. #include <linux/slab.h>
  33. #include <linux/delay.h>
  34. #include <linux/videodev2.h>
  35. #include <linux/module.h>
  36. #include <linux/v4l2-mediabus.h>
  37. #include <media/v4l2-async.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-common.h>
  40. #include <media/v4l2-mediabus.h>
  41. #include <media/v4l2-of.h>
  42. #include <media/v4l2-ctrls.h>
  43. #include <media/tvp514x.h>
  44. #include <media/media-entity.h>
  45. #include "tvp514x_regs.h"
  46. /* Private macros for TVP */
  47. #define I2C_RETRY_COUNT (5)
  48. #define LOCK_RETRY_COUNT (5)
  49. #define LOCK_RETRY_DELAY (200)
  50. /* Debug functions */
  51. static bool debug;
  52. module_param(debug, bool, 0644);
  53. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  54. MODULE_AUTHOR("Texas Instruments");
  55. MODULE_DESCRIPTION("TVP514X linux decoder driver");
  56. MODULE_LICENSE("GPL");
  57. /* enum tvp514x_std - enum for supported standards */
  58. enum tvp514x_std {
  59. STD_NTSC_MJ = 0,
  60. STD_PAL_BDGHIN,
  61. STD_INVALID
  62. };
  63. /**
  64. * struct tvp514x_std_info - Structure to store standard informations
  65. * @width: Line width in pixels
  66. * @height:Number of active lines
  67. * @video_std: Value to write in REG_VIDEO_STD register
  68. * @standard: v4l2 standard structure information
  69. */
  70. struct tvp514x_std_info {
  71. unsigned long width;
  72. unsigned long height;
  73. u8 video_std;
  74. struct v4l2_standard standard;
  75. };
  76. static struct tvp514x_reg tvp514x_reg_list_default[0x40];
  77. static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
  78. /**
  79. * struct tvp514x_decoder - TVP5146/47 decoder object
  80. * @sd: Subdevice Slave handle
  81. * @tvp514x_regs: copy of hw's regs with preset values.
  82. * @pdata: Board specific
  83. * @ver: Chip version
  84. * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
  85. * @pix: Current pixel format
  86. * @num_fmts: Number of formats
  87. * @fmt_list: Format list
  88. * @current_std: Current standard
  89. * @num_stds: Number of standards
  90. * @std_list: Standards list
  91. * @input: Input routing at chip level
  92. * @output: Output routing at chip level
  93. */
  94. struct tvp514x_decoder {
  95. struct v4l2_subdev sd;
  96. struct v4l2_ctrl_handler hdl;
  97. struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
  98. const struct tvp514x_platform_data *pdata;
  99. int ver;
  100. int streaming;
  101. struct v4l2_pix_format pix;
  102. int num_fmts;
  103. const struct v4l2_fmtdesc *fmt_list;
  104. enum tvp514x_std current_std;
  105. int num_stds;
  106. const struct tvp514x_std_info *std_list;
  107. /* Input and Output Routing parameters */
  108. u32 input;
  109. u32 output;
  110. /* mc related members */
  111. struct media_pad pad;
  112. struct v4l2_mbus_framefmt format;
  113. struct tvp514x_reg *int_seq;
  114. };
  115. /* TVP514x default register values */
  116. static struct tvp514x_reg tvp514x_reg_list_default[] = {
  117. /* Composite selected */
  118. {TOK_WRITE, REG_INPUT_SEL, 0x05},
  119. {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
  120. /* Auto mode */
  121. {TOK_WRITE, REG_VIDEO_STD, 0x00},
  122. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  123. {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
  124. {TOK_WRITE, REG_COLOR_KILLER, 0x10},
  125. {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
  126. {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
  127. {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
  128. {TOK_WRITE, REG_BRIGHTNESS, 0x80},
  129. {TOK_WRITE, REG_CONTRAST, 0x80},
  130. {TOK_WRITE, REG_SATURATION, 0x80},
  131. {TOK_WRITE, REG_HUE, 0x00},
  132. {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
  133. {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
  134. /* Reserved */
  135. {TOK_SKIP, 0x0F, 0x00},
  136. {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
  137. {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
  138. {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
  139. /* Reserved */
  140. {TOK_SKIP, 0x13, 0x00},
  141. {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
  142. /* Reserved */
  143. {TOK_SKIP, 0x15, 0x00},
  144. /* NTSC timing */
  145. {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
  146. {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
  147. {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
  148. {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
  149. /* NTSC timing */
  150. {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
  151. {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
  152. {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
  153. {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
  154. /* NTSC timing */
  155. {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
  156. {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
  157. {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
  158. {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
  159. /* NTSC timing */
  160. {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
  161. {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
  162. {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
  163. {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
  164. /* Reserved */
  165. {TOK_SKIP, 0x26, 0x00},
  166. /* Reserved */
  167. {TOK_SKIP, 0x27, 0x00},
  168. {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
  169. /* Reserved */
  170. {TOK_SKIP, 0x29, 0x00},
  171. {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
  172. /* Reserved */
  173. {TOK_SKIP, 0x2B, 0x00},
  174. {TOK_SKIP, REG_SCART_DELAY, 0x00},
  175. {TOK_SKIP, REG_CTI_DELAY, 0x00},
  176. {TOK_SKIP, REG_CTI_CONTROL, 0x00},
  177. /* Reserved */
  178. {TOK_SKIP, 0x2F, 0x00},
  179. /* Reserved */
  180. {TOK_SKIP, 0x30, 0x00},
  181. /* Reserved */
  182. {TOK_SKIP, 0x31, 0x00},
  183. /* HS, VS active high */
  184. {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
  185. /* 10-bit BT.656 */
  186. {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
  187. /* Enable clk & data */
  188. {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
  189. /* Enable AVID & FLD */
  190. {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
  191. /* Enable VS & HS */
  192. {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
  193. {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
  194. {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
  195. /* Clear status */
  196. {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
  197. {TOK_TERM, 0, 0},
  198. };
  199. /**
  200. * List of image formats supported by TVP5146/47 decoder
  201. * Currently we are using 8 bit mode only, but can be
  202. * extended to 10/20 bit mode.
  203. */
  204. static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
  205. {
  206. .index = 0,
  207. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  208. .flags = 0,
  209. .description = "8-bit UYVY 4:2:2 Format",
  210. .pixelformat = V4L2_PIX_FMT_UYVY,
  211. },
  212. };
  213. /**
  214. * Supported standards -
  215. *
  216. * Currently supports two standards only, need to add support for rest of the
  217. * modes, like SECAM, etc...
  218. */
  219. static const struct tvp514x_std_info tvp514x_std_list[] = {
  220. /* Standard: STD_NTSC_MJ */
  221. [STD_NTSC_MJ] = {
  222. .width = NTSC_NUM_ACTIVE_PIXELS,
  223. .height = NTSC_NUM_ACTIVE_LINES,
  224. .video_std = VIDEO_STD_NTSC_MJ_BIT,
  225. .standard = {
  226. .index = 0,
  227. .id = V4L2_STD_NTSC,
  228. .name = "NTSC",
  229. .frameperiod = {1001, 30000},
  230. .framelines = 525
  231. },
  232. /* Standard: STD_PAL_BDGHIN */
  233. },
  234. [STD_PAL_BDGHIN] = {
  235. .width = PAL_NUM_ACTIVE_PIXELS,
  236. .height = PAL_NUM_ACTIVE_LINES,
  237. .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
  238. .standard = {
  239. .index = 1,
  240. .id = V4L2_STD_PAL,
  241. .name = "PAL",
  242. .frameperiod = {1, 25},
  243. .framelines = 625
  244. },
  245. },
  246. /* Standard: need to add for additional standard */
  247. };
  248. static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
  249. {
  250. return container_of(sd, struct tvp514x_decoder, sd);
  251. }
  252. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  253. {
  254. return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
  255. }
  256. /**
  257. * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
  258. * @sd: ptr to v4l2_subdev struct
  259. * @reg: TVP5146/47 register address
  260. *
  261. * Returns value read if successful, or non-zero (-1) otherwise.
  262. */
  263. static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
  264. {
  265. int err, retry = 0;
  266. struct i2c_client *client = v4l2_get_subdevdata(sd);
  267. read_again:
  268. err = i2c_smbus_read_byte_data(client, reg);
  269. if (err < 0) {
  270. if (retry <= I2C_RETRY_COUNT) {
  271. v4l2_warn(sd, "Read: retry ... %d\n", retry);
  272. retry++;
  273. msleep_interruptible(10);
  274. goto read_again;
  275. }
  276. }
  277. return err;
  278. }
  279. /**
  280. * dump_reg() - dump the register content of TVP5146/47.
  281. * @sd: ptr to v4l2_subdev struct
  282. * @reg: TVP5146/47 register address
  283. */
  284. static void dump_reg(struct v4l2_subdev *sd, u8 reg)
  285. {
  286. u32 val;
  287. val = tvp514x_read_reg(sd, reg);
  288. v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
  289. }
  290. /**
  291. * tvp514x_write_reg() - Write a value to a register in TVP5146/47
  292. * @sd: ptr to v4l2_subdev struct
  293. * @reg: TVP5146/47 register address
  294. * @val: value to be written to the register
  295. *
  296. * Write a value to a register in an TVP5146/47 decoder device.
  297. * Returns zero if successful, or non-zero otherwise.
  298. */
  299. static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
  300. {
  301. int err, retry = 0;
  302. struct i2c_client *client = v4l2_get_subdevdata(sd);
  303. write_again:
  304. err = i2c_smbus_write_byte_data(client, reg, val);
  305. if (err) {
  306. if (retry <= I2C_RETRY_COUNT) {
  307. v4l2_warn(sd, "Write: retry ... %d\n", retry);
  308. retry++;
  309. msleep_interruptible(10);
  310. goto write_again;
  311. }
  312. }
  313. return err;
  314. }
  315. /**
  316. * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
  317. * @sd: ptr to v4l2_subdev struct
  318. * @reglist: list of TVP5146/47 registers and values
  319. *
  320. * Initializes a list of TVP5146/47 registers:-
  321. * if token is TOK_TERM, then entire write operation terminates
  322. * if token is TOK_DELAY, then a delay of 'val' msec is introduced
  323. * if token is TOK_SKIP, then the register write is skipped
  324. * if token is TOK_WRITE, then the register write is performed
  325. * Returns zero if successful, or non-zero otherwise.
  326. */
  327. static int tvp514x_write_regs(struct v4l2_subdev *sd,
  328. const struct tvp514x_reg reglist[])
  329. {
  330. int err;
  331. const struct tvp514x_reg *next = reglist;
  332. for (; next->token != TOK_TERM; next++) {
  333. if (next->token == TOK_DELAY) {
  334. msleep(next->val);
  335. continue;
  336. }
  337. if (next->token == TOK_SKIP)
  338. continue;
  339. err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
  340. if (err) {
  341. v4l2_err(sd, "Write failed. Err[%d]\n", err);
  342. return err;
  343. }
  344. }
  345. return 0;
  346. }
  347. /**
  348. * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
  349. * @sd: ptr to v4l2_subdev struct
  350. *
  351. * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
  352. * standard detected.
  353. */
  354. static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
  355. {
  356. u8 std, std_status;
  357. std = tvp514x_read_reg(sd, REG_VIDEO_STD);
  358. if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
  359. /* use the standard status register */
  360. std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
  361. else
  362. /* use the standard register itself */
  363. std_status = std;
  364. switch (std_status & VIDEO_STD_MASK) {
  365. case VIDEO_STD_NTSC_MJ_BIT:
  366. return STD_NTSC_MJ;
  367. case VIDEO_STD_PAL_BDGHIN_BIT:
  368. return STD_PAL_BDGHIN;
  369. default:
  370. return STD_INVALID;
  371. }
  372. return STD_INVALID;
  373. }
  374. /* TVP5146/47 register dump function */
  375. static void tvp514x_reg_dump(struct v4l2_subdev *sd)
  376. {
  377. dump_reg(sd, REG_INPUT_SEL);
  378. dump_reg(sd, REG_AFE_GAIN_CTRL);
  379. dump_reg(sd, REG_VIDEO_STD);
  380. dump_reg(sd, REG_OPERATION_MODE);
  381. dump_reg(sd, REG_COLOR_KILLER);
  382. dump_reg(sd, REG_LUMA_CONTROL1);
  383. dump_reg(sd, REG_LUMA_CONTROL2);
  384. dump_reg(sd, REG_LUMA_CONTROL3);
  385. dump_reg(sd, REG_BRIGHTNESS);
  386. dump_reg(sd, REG_CONTRAST);
  387. dump_reg(sd, REG_SATURATION);
  388. dump_reg(sd, REG_HUE);
  389. dump_reg(sd, REG_CHROMA_CONTROL1);
  390. dump_reg(sd, REG_CHROMA_CONTROL2);
  391. dump_reg(sd, REG_COMP_PR_SATURATION);
  392. dump_reg(sd, REG_COMP_Y_CONTRAST);
  393. dump_reg(sd, REG_COMP_PB_SATURATION);
  394. dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
  395. dump_reg(sd, REG_AVID_START_PIXEL_LSB);
  396. dump_reg(sd, REG_AVID_START_PIXEL_MSB);
  397. dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
  398. dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
  399. dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
  400. dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
  401. dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
  402. dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
  403. dump_reg(sd, REG_VSYNC_START_LINE_LSB);
  404. dump_reg(sd, REG_VSYNC_START_LINE_MSB);
  405. dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
  406. dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
  407. dump_reg(sd, REG_VBLK_START_LINE_LSB);
  408. dump_reg(sd, REG_VBLK_START_LINE_MSB);
  409. dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
  410. dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
  411. dump_reg(sd, REG_SYNC_CONTROL);
  412. dump_reg(sd, REG_OUTPUT_FORMATTER1);
  413. dump_reg(sd, REG_OUTPUT_FORMATTER2);
  414. dump_reg(sd, REG_OUTPUT_FORMATTER3);
  415. dump_reg(sd, REG_OUTPUT_FORMATTER4);
  416. dump_reg(sd, REG_OUTPUT_FORMATTER5);
  417. dump_reg(sd, REG_OUTPUT_FORMATTER6);
  418. dump_reg(sd, REG_CLEAR_LOST_LOCK);
  419. }
  420. /**
  421. * tvp514x_configure() - Configure the TVP5146/47 registers
  422. * @sd: ptr to v4l2_subdev struct
  423. * @decoder: ptr to tvp514x_decoder structure
  424. *
  425. * Returns zero if successful, or non-zero otherwise.
  426. */
  427. static int tvp514x_configure(struct v4l2_subdev *sd,
  428. struct tvp514x_decoder *decoder)
  429. {
  430. int err;
  431. /* common register initialization */
  432. err =
  433. tvp514x_write_regs(sd, decoder->tvp514x_regs);
  434. if (err)
  435. return err;
  436. if (debug)
  437. tvp514x_reg_dump(sd);
  438. return 0;
  439. }
  440. /**
  441. * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
  442. * @sd: pointer to standard V4L2 sub-device structure
  443. * @decoder: pointer to tvp514x_decoder structure
  444. *
  445. * A device is considered to be detected if the chip ID (LSB and MSB)
  446. * registers match the expected values.
  447. * Any value of the rom version register is accepted.
  448. * Returns ENODEV error number if no device is detected, or zero
  449. * if a device is detected.
  450. */
  451. static int tvp514x_detect(struct v4l2_subdev *sd,
  452. struct tvp514x_decoder *decoder)
  453. {
  454. u8 chip_id_msb, chip_id_lsb, rom_ver;
  455. struct i2c_client *client = v4l2_get_subdevdata(sd);
  456. chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
  457. chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
  458. rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
  459. v4l2_dbg(1, debug, sd,
  460. "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
  461. chip_id_msb, chip_id_lsb, rom_ver);
  462. if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
  463. || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
  464. && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
  465. /* We didn't read the values we expected, so this must not be
  466. * an TVP5146/47.
  467. */
  468. v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
  469. chip_id_msb, chip_id_lsb);
  470. return -ENODEV;
  471. }
  472. decoder->ver = rom_ver;
  473. v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
  474. client->name, decoder->ver,
  475. client->addr << 1, client->adapter->name);
  476. return 0;
  477. }
  478. /**
  479. * tvp514x_querystd() - V4L2 decoder interface handler for querystd
  480. * @sd: pointer to standard V4L2 sub-device structure
  481. * @std_id: standard V4L2 std_id ioctl enum
  482. *
  483. * Returns the current standard detected by TVP5146/47. If no active input is
  484. * detected then *std_id is set to 0 and the function returns 0.
  485. */
  486. static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
  487. {
  488. struct tvp514x_decoder *decoder = to_decoder(sd);
  489. enum tvp514x_std current_std;
  490. enum tvp514x_input input_sel;
  491. u8 sync_lock_status, lock_mask;
  492. if (std_id == NULL)
  493. return -EINVAL;
  494. /* To query the standard the TVP514x must power on the ADCs. */
  495. if (!decoder->streaming) {
  496. tvp514x_s_stream(sd, 1);
  497. msleep(LOCK_RETRY_DELAY);
  498. }
  499. /* query the current standard */
  500. current_std = tvp514x_query_current_std(sd);
  501. if (current_std == STD_INVALID) {
  502. *std_id = V4L2_STD_UNKNOWN;
  503. return 0;
  504. }
  505. input_sel = decoder->input;
  506. switch (input_sel) {
  507. case INPUT_CVBS_VI1A:
  508. case INPUT_CVBS_VI1B:
  509. case INPUT_CVBS_VI1C:
  510. case INPUT_CVBS_VI2A:
  511. case INPUT_CVBS_VI2B:
  512. case INPUT_CVBS_VI2C:
  513. case INPUT_CVBS_VI3A:
  514. case INPUT_CVBS_VI3B:
  515. case INPUT_CVBS_VI3C:
  516. case INPUT_CVBS_VI4A:
  517. lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
  518. STATUS_HORZ_SYNC_LOCK_BIT |
  519. STATUS_VIRT_SYNC_LOCK_BIT;
  520. break;
  521. case INPUT_SVIDEO_VI2A_VI1A:
  522. case INPUT_SVIDEO_VI2B_VI1B:
  523. case INPUT_SVIDEO_VI2C_VI1C:
  524. case INPUT_SVIDEO_VI2A_VI3A:
  525. case INPUT_SVIDEO_VI2B_VI3B:
  526. case INPUT_SVIDEO_VI2C_VI3C:
  527. case INPUT_SVIDEO_VI4A_VI1A:
  528. case INPUT_SVIDEO_VI4A_VI1B:
  529. case INPUT_SVIDEO_VI4A_VI1C:
  530. case INPUT_SVIDEO_VI4A_VI3A:
  531. case INPUT_SVIDEO_VI4A_VI3B:
  532. case INPUT_SVIDEO_VI4A_VI3C:
  533. lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
  534. STATUS_VIRT_SYNC_LOCK_BIT;
  535. break;
  536. /*Need to add other interfaces*/
  537. default:
  538. return -EINVAL;
  539. }
  540. /* check whether signal is locked */
  541. sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
  542. if (lock_mask != (sync_lock_status & lock_mask)) {
  543. *std_id = V4L2_STD_UNKNOWN;
  544. return 0; /* No input detected */
  545. }
  546. *std_id &= decoder->std_list[current_std].standard.id;
  547. v4l2_dbg(1, debug, sd, "Current STD: %s\n",
  548. decoder->std_list[current_std].standard.name);
  549. return 0;
  550. }
  551. /**
  552. * tvp514x_s_std() - V4L2 decoder interface handler for s_std
  553. * @sd: pointer to standard V4L2 sub-device structure
  554. * @std_id: standard V4L2 v4l2_std_id ioctl enum
  555. *
  556. * If std_id is supported, sets the requested standard. Otherwise, returns
  557. * -EINVAL
  558. */
  559. static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
  560. {
  561. struct tvp514x_decoder *decoder = to_decoder(sd);
  562. int err, i;
  563. for (i = 0; i < decoder->num_stds; i++)
  564. if (std_id & decoder->std_list[i].standard.id)
  565. break;
  566. if ((i == decoder->num_stds) || (i == STD_INVALID))
  567. return -EINVAL;
  568. err = tvp514x_write_reg(sd, REG_VIDEO_STD,
  569. decoder->std_list[i].video_std);
  570. if (err)
  571. return err;
  572. decoder->current_std = i;
  573. decoder->tvp514x_regs[REG_VIDEO_STD].val =
  574. decoder->std_list[i].video_std;
  575. v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
  576. decoder->std_list[i].standard.name);
  577. return 0;
  578. }
  579. /**
  580. * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
  581. * @sd: pointer to standard V4L2 sub-device structure
  582. * @input: input selector for routing the signal
  583. * @output: output selector for routing the signal
  584. * @config: config value. Not used
  585. *
  586. * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
  587. * the input is not supported or there is no active signal present in the
  588. * selected input.
  589. */
  590. static int tvp514x_s_routing(struct v4l2_subdev *sd,
  591. u32 input, u32 output, u32 config)
  592. {
  593. struct tvp514x_decoder *decoder = to_decoder(sd);
  594. int err;
  595. enum tvp514x_input input_sel;
  596. enum tvp514x_output output_sel;
  597. if ((input >= INPUT_INVALID) ||
  598. (output >= OUTPUT_INVALID))
  599. /* Index out of bound */
  600. return -EINVAL;
  601. input_sel = input;
  602. output_sel = output;
  603. err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
  604. if (err)
  605. return err;
  606. output_sel |= tvp514x_read_reg(sd,
  607. REG_OUTPUT_FORMATTER1) & 0x7;
  608. err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
  609. output_sel);
  610. if (err)
  611. return err;
  612. decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
  613. decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
  614. decoder->input = input;
  615. decoder->output = output;
  616. v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
  617. return 0;
  618. }
  619. /**
  620. * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
  621. * @ctrl: pointer to v4l2_ctrl structure
  622. *
  623. * If the requested control is supported, sets the control's current
  624. * value in HW. Otherwise, returns -EINVAL if the control is not supported.
  625. */
  626. static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
  627. {
  628. struct v4l2_subdev *sd = to_sd(ctrl);
  629. struct tvp514x_decoder *decoder = to_decoder(sd);
  630. int err = -EINVAL, value;
  631. value = ctrl->val;
  632. switch (ctrl->id) {
  633. case V4L2_CID_BRIGHTNESS:
  634. err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
  635. if (!err)
  636. decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
  637. break;
  638. case V4L2_CID_CONTRAST:
  639. err = tvp514x_write_reg(sd, REG_CONTRAST, value);
  640. if (!err)
  641. decoder->tvp514x_regs[REG_CONTRAST].val = value;
  642. break;
  643. case V4L2_CID_SATURATION:
  644. err = tvp514x_write_reg(sd, REG_SATURATION, value);
  645. if (!err)
  646. decoder->tvp514x_regs[REG_SATURATION].val = value;
  647. break;
  648. case V4L2_CID_HUE:
  649. if (value == 180)
  650. value = 0x7F;
  651. else if (value == -180)
  652. value = 0x80;
  653. err = tvp514x_write_reg(sd, REG_HUE, value);
  654. if (!err)
  655. decoder->tvp514x_regs[REG_HUE].val = value;
  656. break;
  657. case V4L2_CID_AUTOGAIN:
  658. err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
  659. if (!err)
  660. decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
  661. break;
  662. }
  663. v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
  664. ctrl->id, ctrl->val);
  665. return err;
  666. }
  667. /**
  668. * tvp514x_enum_mbus_fmt() - V4L2 decoder interface handler for enum_mbus_fmt
  669. * @sd: pointer to standard V4L2 sub-device structure
  670. * @index: index of pixelcode to retrieve
  671. * @code: receives the pixelcode
  672. *
  673. * Enumerates supported mediabus formats
  674. */
  675. static int
  676. tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
  677. enum v4l2_mbus_pixelcode *code)
  678. {
  679. if (index)
  680. return -EINVAL;
  681. *code = V4L2_MBUS_FMT_YUYV10_2X10;
  682. return 0;
  683. }
  684. /**
  685. * tvp514x_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt
  686. * @sd: pointer to standard V4L2 sub-device structure
  687. * @f: pointer to the mediabus format structure
  688. *
  689. * Negotiates the image capture size and mediabus format.
  690. */
  691. static int
  692. tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
  693. {
  694. struct tvp514x_decoder *decoder = to_decoder(sd);
  695. enum tvp514x_std current_std;
  696. if (f == NULL)
  697. return -EINVAL;
  698. /* Calculate height and width based on current standard */
  699. current_std = decoder->current_std;
  700. f->code = V4L2_MBUS_FMT_YUYV8_2X8;
  701. f->width = decoder->std_list[current_std].width;
  702. f->height = decoder->std_list[current_std].height;
  703. f->field = V4L2_FIELD_INTERLACED;
  704. f->colorspace = V4L2_COLORSPACE_SMPTE170M;
  705. v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n",
  706. f->width, f->height);
  707. return 0;
  708. }
  709. /**
  710. * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
  711. * @sd: pointer to standard V4L2 sub-device structure
  712. * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
  713. *
  714. * Returns the decoder's video CAPTURE parameters.
  715. */
  716. static int
  717. tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
  718. {
  719. struct tvp514x_decoder *decoder = to_decoder(sd);
  720. struct v4l2_captureparm *cparm;
  721. enum tvp514x_std current_std;
  722. if (a == NULL)
  723. return -EINVAL;
  724. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  725. /* only capture is supported */
  726. return -EINVAL;
  727. /* get the current standard */
  728. current_std = decoder->current_std;
  729. cparm = &a->parm.capture;
  730. cparm->capability = V4L2_CAP_TIMEPERFRAME;
  731. cparm->timeperframe =
  732. decoder->std_list[current_std].standard.frameperiod;
  733. return 0;
  734. }
  735. /**
  736. * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
  737. * @sd: pointer to standard V4L2 sub-device structure
  738. * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
  739. *
  740. * Configures the decoder to use the input parameters, if possible. If
  741. * not possible, returns the appropriate error code.
  742. */
  743. static int
  744. tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
  745. {
  746. struct tvp514x_decoder *decoder = to_decoder(sd);
  747. struct v4l2_fract *timeperframe;
  748. enum tvp514x_std current_std;
  749. if (a == NULL)
  750. return -EINVAL;
  751. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  752. /* only capture is supported */
  753. return -EINVAL;
  754. timeperframe = &a->parm.capture.timeperframe;
  755. /* get the current standard */
  756. current_std = decoder->current_std;
  757. *timeperframe =
  758. decoder->std_list[current_std].standard.frameperiod;
  759. return 0;
  760. }
  761. /**
  762. * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
  763. * @sd: pointer to standard V4L2 sub-device structure
  764. * @enable: streaming enable or disable
  765. *
  766. * Sets streaming to enable or disable, if possible.
  767. */
  768. static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
  769. {
  770. int err = 0;
  771. struct tvp514x_decoder *decoder = to_decoder(sd);
  772. if (decoder->streaming == enable)
  773. return 0;
  774. switch (enable) {
  775. case 0:
  776. {
  777. /* Power Down Sequence */
  778. err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
  779. if (err) {
  780. v4l2_err(sd, "Unable to turn off decoder\n");
  781. return err;
  782. }
  783. decoder->streaming = enable;
  784. break;
  785. }
  786. case 1:
  787. {
  788. /* Power Up Sequence */
  789. err = tvp514x_write_regs(sd, decoder->int_seq);
  790. if (err) {
  791. v4l2_err(sd, "Unable to turn on decoder\n");
  792. return err;
  793. }
  794. /* Detect if not already detected */
  795. err = tvp514x_detect(sd, decoder);
  796. if (err) {
  797. v4l2_err(sd, "Unable to detect decoder\n");
  798. return err;
  799. }
  800. err = tvp514x_configure(sd, decoder);
  801. if (err) {
  802. v4l2_err(sd, "Unable to configure decoder\n");
  803. return err;
  804. }
  805. decoder->streaming = enable;
  806. break;
  807. }
  808. default:
  809. err = -ENODEV;
  810. break;
  811. }
  812. return err;
  813. }
  814. static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
  815. .s_ctrl = tvp514x_s_ctrl,
  816. };
  817. /**
  818. * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
  819. * @sd: pointer to standard V4L2 sub-device structure
  820. * @fh: file handle
  821. * @code: pointer to v4l2_subdev_mbus_code_enum structure
  822. *
  823. * Enumertaes mbus codes supported
  824. */
  825. static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd,
  826. struct v4l2_subdev_fh *fh,
  827. struct v4l2_subdev_mbus_code_enum *code)
  828. {
  829. u32 pad = code->pad;
  830. u32 index = code->index;
  831. memset(code, 0, sizeof(*code));
  832. code->index = index;
  833. code->pad = pad;
  834. if (index != 0)
  835. return -EINVAL;
  836. code->code = V4L2_MBUS_FMT_YUYV8_2X8;
  837. return 0;
  838. }
  839. /**
  840. * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
  841. * @sd: pointer to standard V4L2 sub-device structure
  842. * @fh: file handle
  843. * @format: pointer to v4l2_subdev_format structure
  844. *
  845. * Retrieves pad format which is active or tried based on requirement
  846. */
  847. static int tvp514x_get_pad_format(struct v4l2_subdev *sd,
  848. struct v4l2_subdev_fh *fh,
  849. struct v4l2_subdev_format *format)
  850. {
  851. struct tvp514x_decoder *decoder = to_decoder(sd);
  852. __u32 which = format->which;
  853. if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  854. format->format = decoder->format;
  855. return 0;
  856. }
  857. format->format.code = V4L2_MBUS_FMT_YUYV8_2X8;
  858. format->format.width = tvp514x_std_list[decoder->current_std].width;
  859. format->format.height = tvp514x_std_list[decoder->current_std].height;
  860. format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  861. format->format.field = V4L2_FIELD_INTERLACED;
  862. return 0;
  863. }
  864. /**
  865. * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
  866. * @sd: pointer to standard V4L2 sub-device structure
  867. * @fh: file handle
  868. * @format: pointer to v4l2_subdev_format structure
  869. *
  870. * Set pad format for the output pad
  871. */
  872. static int tvp514x_set_pad_format(struct v4l2_subdev *sd,
  873. struct v4l2_subdev_fh *fh,
  874. struct v4l2_subdev_format *fmt)
  875. {
  876. struct tvp514x_decoder *decoder = to_decoder(sd);
  877. if (fmt->format.field != V4L2_FIELD_INTERLACED ||
  878. fmt->format.code != V4L2_MBUS_FMT_YUYV8_2X8 ||
  879. fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M ||
  880. fmt->format.width != tvp514x_std_list[decoder->current_std].width ||
  881. fmt->format.height != tvp514x_std_list[decoder->current_std].height)
  882. return -EINVAL;
  883. decoder->format = fmt->format;
  884. return 0;
  885. }
  886. static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
  887. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  888. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  889. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  890. .g_ctrl = v4l2_subdev_g_ctrl,
  891. .s_ctrl = v4l2_subdev_s_ctrl,
  892. .queryctrl = v4l2_subdev_queryctrl,
  893. .querymenu = v4l2_subdev_querymenu,
  894. .s_std = tvp514x_s_std,
  895. };
  896. static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
  897. .s_routing = tvp514x_s_routing,
  898. .querystd = tvp514x_querystd,
  899. .enum_mbus_fmt = tvp514x_enum_mbus_fmt,
  900. .g_mbus_fmt = tvp514x_mbus_fmt,
  901. .try_mbus_fmt = tvp514x_mbus_fmt,
  902. .s_mbus_fmt = tvp514x_mbus_fmt,
  903. .g_parm = tvp514x_g_parm,
  904. .s_parm = tvp514x_s_parm,
  905. .s_stream = tvp514x_s_stream,
  906. };
  907. static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = {
  908. .enum_mbus_code = tvp514x_enum_mbus_code,
  909. .get_fmt = tvp514x_get_pad_format,
  910. .set_fmt = tvp514x_set_pad_format,
  911. };
  912. static const struct v4l2_subdev_ops tvp514x_ops = {
  913. .core = &tvp514x_core_ops,
  914. .video = &tvp514x_video_ops,
  915. .pad = &tvp514x_pad_ops,
  916. };
  917. static struct tvp514x_decoder tvp514x_dev = {
  918. .streaming = 0,
  919. .fmt_list = tvp514x_fmt_list,
  920. .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
  921. .pix = {
  922. /* Default to NTSC 8-bit YUV 422 */
  923. .width = NTSC_NUM_ACTIVE_PIXELS,
  924. .height = NTSC_NUM_ACTIVE_LINES,
  925. .pixelformat = V4L2_PIX_FMT_UYVY,
  926. .field = V4L2_FIELD_INTERLACED,
  927. .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
  928. .sizeimage = NTSC_NUM_ACTIVE_PIXELS * 2 *
  929. NTSC_NUM_ACTIVE_LINES,
  930. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  931. },
  932. .current_std = STD_NTSC_MJ,
  933. .std_list = tvp514x_std_list,
  934. .num_stds = ARRAY_SIZE(tvp514x_std_list),
  935. };
  936. static struct tvp514x_platform_data *
  937. tvp514x_get_pdata(struct i2c_client *client)
  938. {
  939. struct tvp514x_platform_data *pdata;
  940. struct v4l2_of_endpoint bus_cfg;
  941. struct device_node *endpoint;
  942. unsigned int flags;
  943. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  944. return client->dev.platform_data;
  945. endpoint = v4l2_of_get_next_endpoint(client->dev.of_node, NULL);
  946. if (!endpoint)
  947. return NULL;
  948. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  949. if (!pdata)
  950. goto done;
  951. v4l2_of_parse_endpoint(endpoint, &bus_cfg);
  952. flags = bus_cfg.bus.parallel.flags;
  953. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  954. pdata->hs_polarity = 1;
  955. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  956. pdata->vs_polarity = 1;
  957. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  958. pdata->clk_polarity = 1;
  959. done:
  960. of_node_put(endpoint);
  961. return pdata;
  962. }
  963. /**
  964. * tvp514x_probe() - decoder driver i2c probe handler
  965. * @client: i2c driver client device structure
  966. * @id: i2c driver id table
  967. *
  968. * Register decoder as an i2c client device and V4L2
  969. * device.
  970. */
  971. static int
  972. tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
  973. {
  974. struct tvp514x_platform_data *pdata = tvp514x_get_pdata(client);
  975. struct tvp514x_decoder *decoder;
  976. struct v4l2_subdev *sd;
  977. int ret;
  978. if (pdata == NULL) {
  979. dev_err(&client->dev, "No platform data\n");
  980. return -EINVAL;
  981. }
  982. /* Check if the adapter supports the needed features */
  983. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  984. return -EIO;
  985. decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
  986. if (!decoder)
  987. return -ENOMEM;
  988. /* Initialize the tvp514x_decoder with default configuration */
  989. *decoder = tvp514x_dev;
  990. /* Copy default register configuration */
  991. memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
  992. sizeof(tvp514x_reg_list_default));
  993. decoder->int_seq = (struct tvp514x_reg *)id->driver_data;
  994. /* Copy board specific information here */
  995. decoder->pdata = pdata;
  996. /**
  997. * Fetch platform specific data, and configure the
  998. * tvp514x_reg_list[] accordingly. Since this is one
  999. * time configuration, no need to preserve.
  1000. */
  1001. decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
  1002. (decoder->pdata->clk_polarity << 1);
  1003. decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
  1004. ((decoder->pdata->hs_polarity << 2) |
  1005. (decoder->pdata->vs_polarity << 3));
  1006. /* Set default standard to auto */
  1007. decoder->tvp514x_regs[REG_VIDEO_STD].val =
  1008. VIDEO_STD_AUTO_SWITCH_BIT;
  1009. /* Register with V4L2 layer as slave device */
  1010. sd = &decoder->sd;
  1011. v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
  1012. #if defined(CONFIG_MEDIA_CONTROLLER)
  1013. decoder->pad.flags = MEDIA_PAD_FL_SOURCE;
  1014. decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1015. decoder->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
  1016. ret = media_entity_init(&decoder->sd.entity, 1, &decoder->pad, 0);
  1017. if (ret < 0) {
  1018. v4l2_err(sd, "%s decoder driver failed to register !!\n",
  1019. sd->name);
  1020. return ret;
  1021. }
  1022. #endif
  1023. v4l2_ctrl_handler_init(&decoder->hdl, 5);
  1024. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  1025. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1026. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  1027. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  1028. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  1029. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1030. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  1031. V4L2_CID_HUE, -180, 180, 180, 0);
  1032. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  1033. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  1034. sd->ctrl_handler = &decoder->hdl;
  1035. if (decoder->hdl.error) {
  1036. ret = decoder->hdl.error;
  1037. goto done;
  1038. }
  1039. v4l2_ctrl_handler_setup(&decoder->hdl);
  1040. ret = v4l2_async_register_subdev(&decoder->sd);
  1041. if (!ret)
  1042. v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
  1043. done:
  1044. if (ret < 0) {
  1045. v4l2_ctrl_handler_free(&decoder->hdl);
  1046. #if defined(CONFIG_MEDIA_CONTROLLER)
  1047. media_entity_cleanup(&decoder->sd.entity);
  1048. #endif
  1049. }
  1050. return ret;
  1051. }
  1052. /**
  1053. * tvp514x_remove() - decoder driver i2c remove handler
  1054. * @client: i2c driver client device structure
  1055. *
  1056. * Unregister decoder as an i2c client device and V4L2
  1057. * device. Complement of tvp514x_probe().
  1058. */
  1059. static int tvp514x_remove(struct i2c_client *client)
  1060. {
  1061. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1062. struct tvp514x_decoder *decoder = to_decoder(sd);
  1063. v4l2_async_unregister_subdev(&decoder->sd);
  1064. v4l2_device_unregister_subdev(sd);
  1065. #if defined(CONFIG_MEDIA_CONTROLLER)
  1066. media_entity_cleanup(&decoder->sd.entity);
  1067. #endif
  1068. v4l2_ctrl_handler_free(&decoder->hdl);
  1069. return 0;
  1070. }
  1071. /* TVP5146 Init/Power on Sequence */
  1072. static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
  1073. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1074. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1075. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1076. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1077. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1078. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1079. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1080. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1081. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1082. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1083. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1084. {TOK_TERM, 0, 0},
  1085. };
  1086. /* TVP5147 Init/Power on Sequence */
  1087. static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
  1088. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1089. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1090. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1091. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1092. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1093. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1094. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1095. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1096. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
  1097. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1098. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
  1099. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
  1100. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1101. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1102. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1103. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1104. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1105. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1106. {TOK_TERM, 0, 0},
  1107. };
  1108. /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
  1109. static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
  1110. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1111. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1112. {TOK_TERM, 0, 0},
  1113. };
  1114. /**
  1115. * I2C Device Table -
  1116. *
  1117. * name - Name of the actual device/chip.
  1118. * driver_data - Driver data
  1119. */
  1120. static const struct i2c_device_id tvp514x_id[] = {
  1121. {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
  1122. {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
  1123. {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
  1124. {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
  1125. {},
  1126. };
  1127. MODULE_DEVICE_TABLE(i2c, tvp514x_id);
  1128. #if IS_ENABLED(CONFIG_OF)
  1129. static const struct of_device_id tvp514x_of_match[] = {
  1130. { .compatible = "ti,tvp5146", },
  1131. { .compatible = "ti,tvp5146m2", },
  1132. { .compatible = "ti,tvp5147", },
  1133. { .compatible = "ti,tvp5147m1", },
  1134. { /* sentinel */ },
  1135. };
  1136. MODULE_DEVICE_TABLE(of, tvp514x_of_match);
  1137. #endif
  1138. static struct i2c_driver tvp514x_driver = {
  1139. .driver = {
  1140. .of_match_table = of_match_ptr(tvp514x_of_match),
  1141. .owner = THIS_MODULE,
  1142. .name = TVP514X_MODULE_NAME,
  1143. },
  1144. .probe = tvp514x_probe,
  1145. .remove = tvp514x_remove,
  1146. .id_table = tvp514x_id,
  1147. };
  1148. module_i2c_driver(tvp514x_driver);