tvp514x.c 33 KB

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