tvp514x.c 34 KB

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