tvp514x.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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 <linux/of.h>
  38. #include <linux/of_graph.h>
  39. #include <media/v4l2-async.h>
  40. #include <media/v4l2-device.h>
  41. #include <media/v4l2-common.h>
  42. #include <media/v4l2-mediabus.h>
  43. #include <media/v4l2-of.h>
  44. #include <media/v4l2-ctrls.h>
  45. #include <media/tvp514x.h>
  46. #include <media/media-entity.h>
  47. #include "tvp514x_regs.h"
  48. /* Private macros for TVP */
  49. #define I2C_RETRY_COUNT (5)
  50. #define LOCK_RETRY_COUNT (5)
  51. #define LOCK_RETRY_DELAY (200)
  52. /* Debug functions */
  53. static bool debug;
  54. module_param(debug, bool, 0644);
  55. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  56. MODULE_AUTHOR("Texas Instruments");
  57. MODULE_DESCRIPTION("TVP514X linux decoder driver");
  58. MODULE_LICENSE("GPL");
  59. /* enum tvp514x_std - enum for supported standards */
  60. enum tvp514x_std {
  61. STD_NTSC_MJ = 0,
  62. STD_PAL_BDGHIN,
  63. STD_INVALID
  64. };
  65. /**
  66. * struct tvp514x_std_info - Structure to store standard informations
  67. * @width: Line width in pixels
  68. * @height:Number of active lines
  69. * @video_std: Value to write in REG_VIDEO_STD register
  70. * @standard: v4l2 standard structure information
  71. */
  72. struct tvp514x_std_info {
  73. unsigned long width;
  74. unsigned long height;
  75. u8 video_std;
  76. struct v4l2_standard standard;
  77. };
  78. static struct tvp514x_reg tvp514x_reg_list_default[0x40];
  79. static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
  80. /**
  81. * struct tvp514x_decoder - TVP5146/47 decoder object
  82. * @sd: Subdevice Slave handle
  83. * @tvp514x_regs: copy of hw's regs with preset values.
  84. * @pdata: Board specific
  85. * @ver: Chip version
  86. * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
  87. * @pix: Current pixel format
  88. * @num_fmts: Number of formats
  89. * @fmt_list: Format list
  90. * @current_std: Current standard
  91. * @num_stds: Number of standards
  92. * @std_list: Standards list
  93. * @input: Input routing at chip level
  94. * @output: Output routing at chip level
  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_parm() - V4L2 decoder interface handler for g_parm
  671. * @sd: pointer to standard V4L2 sub-device structure
  672. * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
  673. *
  674. * Returns the decoder's video CAPTURE parameters.
  675. */
  676. static int
  677. tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
  678. {
  679. struct tvp514x_decoder *decoder = to_decoder(sd);
  680. struct v4l2_captureparm *cparm;
  681. enum tvp514x_std current_std;
  682. if (a == NULL)
  683. return -EINVAL;
  684. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  685. /* only capture is supported */
  686. return -EINVAL;
  687. /* get the current standard */
  688. current_std = decoder->current_std;
  689. cparm = &a->parm.capture;
  690. cparm->capability = V4L2_CAP_TIMEPERFRAME;
  691. cparm->timeperframe =
  692. decoder->std_list[current_std].standard.frameperiod;
  693. return 0;
  694. }
  695. /**
  696. * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
  697. * @sd: pointer to standard V4L2 sub-device structure
  698. * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
  699. *
  700. * Configures the decoder to use the input parameters, if possible. If
  701. * not possible, returns the appropriate error code.
  702. */
  703. static int
  704. tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
  705. {
  706. struct tvp514x_decoder *decoder = to_decoder(sd);
  707. struct v4l2_fract *timeperframe;
  708. enum tvp514x_std current_std;
  709. if (a == NULL)
  710. return -EINVAL;
  711. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  712. /* only capture is supported */
  713. return -EINVAL;
  714. timeperframe = &a->parm.capture.timeperframe;
  715. /* get the current standard */
  716. current_std = decoder->current_std;
  717. *timeperframe =
  718. decoder->std_list[current_std].standard.frameperiod;
  719. return 0;
  720. }
  721. /**
  722. * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
  723. * @sd: pointer to standard V4L2 sub-device structure
  724. * @enable: streaming enable or disable
  725. *
  726. * Sets streaming to enable or disable, if possible.
  727. */
  728. static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
  729. {
  730. int err = 0;
  731. struct tvp514x_decoder *decoder = to_decoder(sd);
  732. if (decoder->streaming == enable)
  733. return 0;
  734. switch (enable) {
  735. case 0:
  736. {
  737. /* Power Down Sequence */
  738. err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
  739. if (err) {
  740. v4l2_err(sd, "Unable to turn off decoder\n");
  741. return err;
  742. }
  743. decoder->streaming = enable;
  744. break;
  745. }
  746. case 1:
  747. {
  748. /* Power Up Sequence */
  749. err = tvp514x_write_regs(sd, decoder->int_seq);
  750. if (err) {
  751. v4l2_err(sd, "Unable to turn on decoder\n");
  752. return err;
  753. }
  754. /* Detect if not already detected */
  755. err = tvp514x_detect(sd, decoder);
  756. if (err) {
  757. v4l2_err(sd, "Unable to detect decoder\n");
  758. return err;
  759. }
  760. err = tvp514x_configure(sd, decoder);
  761. if (err) {
  762. v4l2_err(sd, "Unable to configure decoder\n");
  763. return err;
  764. }
  765. decoder->streaming = enable;
  766. break;
  767. }
  768. default:
  769. err = -ENODEV;
  770. break;
  771. }
  772. return err;
  773. }
  774. static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
  775. .s_ctrl = tvp514x_s_ctrl,
  776. };
  777. /**
  778. * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
  779. * @sd: pointer to standard V4L2 sub-device structure
  780. * @cfg: pad configuration
  781. * @code: pointer to v4l2_subdev_mbus_code_enum structure
  782. *
  783. * Enumertaes mbus codes supported
  784. */
  785. static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd,
  786. struct v4l2_subdev_pad_config *cfg,
  787. struct v4l2_subdev_mbus_code_enum *code)
  788. {
  789. u32 pad = code->pad;
  790. u32 index = code->index;
  791. memset(code, 0, sizeof(*code));
  792. code->index = index;
  793. code->pad = pad;
  794. if (index != 0)
  795. return -EINVAL;
  796. code->code = MEDIA_BUS_FMT_YUYV8_2X8;
  797. return 0;
  798. }
  799. /**
  800. * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
  801. * @sd: pointer to standard V4L2 sub-device structure
  802. * @cfg: pad configuration
  803. * @format: pointer to v4l2_subdev_format structure
  804. *
  805. * Retrieves pad format which is active or tried based on requirement
  806. */
  807. static int tvp514x_get_pad_format(struct v4l2_subdev *sd,
  808. struct v4l2_subdev_pad_config *cfg,
  809. struct v4l2_subdev_format *format)
  810. {
  811. struct tvp514x_decoder *decoder = to_decoder(sd);
  812. __u32 which = format->which;
  813. if (format->pad)
  814. return -EINVAL;
  815. if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  816. format->format = decoder->format;
  817. return 0;
  818. }
  819. format->format.code = MEDIA_BUS_FMT_YUYV8_2X8;
  820. format->format.width = tvp514x_std_list[decoder->current_std].width;
  821. format->format.height = tvp514x_std_list[decoder->current_std].height;
  822. format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  823. format->format.field = V4L2_FIELD_INTERLACED;
  824. return 0;
  825. }
  826. /**
  827. * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
  828. * @sd: pointer to standard V4L2 sub-device structure
  829. * @cfg: pad configuration
  830. * @format: pointer to v4l2_subdev_format structure
  831. *
  832. * Set pad format for the output pad
  833. */
  834. static int tvp514x_set_pad_format(struct v4l2_subdev *sd,
  835. struct v4l2_subdev_pad_config *cfg,
  836. struct v4l2_subdev_format *fmt)
  837. {
  838. struct tvp514x_decoder *decoder = to_decoder(sd);
  839. if (fmt->format.field != V4L2_FIELD_INTERLACED ||
  840. fmt->format.code != MEDIA_BUS_FMT_YUYV8_2X8 ||
  841. fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M ||
  842. fmt->format.width != tvp514x_std_list[decoder->current_std].width ||
  843. fmt->format.height != tvp514x_std_list[decoder->current_std].height)
  844. return -EINVAL;
  845. decoder->format = fmt->format;
  846. return 0;
  847. }
  848. static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
  849. .s_std = tvp514x_s_std,
  850. .s_routing = tvp514x_s_routing,
  851. .querystd = tvp514x_querystd,
  852. .g_parm = tvp514x_g_parm,
  853. .s_parm = tvp514x_s_parm,
  854. .s_stream = tvp514x_s_stream,
  855. };
  856. static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = {
  857. .enum_mbus_code = tvp514x_enum_mbus_code,
  858. .get_fmt = tvp514x_get_pad_format,
  859. .set_fmt = tvp514x_set_pad_format,
  860. };
  861. static const struct v4l2_subdev_ops tvp514x_ops = {
  862. .video = &tvp514x_video_ops,
  863. .pad = &tvp514x_pad_ops,
  864. };
  865. static struct tvp514x_decoder tvp514x_dev = {
  866. .streaming = 0,
  867. .fmt_list = tvp514x_fmt_list,
  868. .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
  869. .pix = {
  870. /* Default to NTSC 8-bit YUV 422 */
  871. .width = NTSC_NUM_ACTIVE_PIXELS,
  872. .height = NTSC_NUM_ACTIVE_LINES,
  873. .pixelformat = V4L2_PIX_FMT_UYVY,
  874. .field = V4L2_FIELD_INTERLACED,
  875. .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
  876. .sizeimage = NTSC_NUM_ACTIVE_PIXELS * 2 *
  877. NTSC_NUM_ACTIVE_LINES,
  878. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  879. },
  880. .current_std = STD_NTSC_MJ,
  881. .std_list = tvp514x_std_list,
  882. .num_stds = ARRAY_SIZE(tvp514x_std_list),
  883. };
  884. static struct tvp514x_platform_data *
  885. tvp514x_get_pdata(struct i2c_client *client)
  886. {
  887. struct tvp514x_platform_data *pdata;
  888. struct v4l2_of_endpoint bus_cfg;
  889. struct device_node *endpoint;
  890. unsigned int flags;
  891. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  892. return client->dev.platform_data;
  893. endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  894. if (!endpoint)
  895. return NULL;
  896. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  897. if (!pdata)
  898. goto done;
  899. v4l2_of_parse_endpoint(endpoint, &bus_cfg);
  900. flags = bus_cfg.bus.parallel.flags;
  901. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  902. pdata->hs_polarity = 1;
  903. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  904. pdata->vs_polarity = 1;
  905. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  906. pdata->clk_polarity = 1;
  907. done:
  908. of_node_put(endpoint);
  909. return pdata;
  910. }
  911. /**
  912. * tvp514x_probe() - decoder driver i2c probe handler
  913. * @client: i2c driver client device structure
  914. * @id: i2c driver id table
  915. *
  916. * Register decoder as an i2c client device and V4L2
  917. * device.
  918. */
  919. static int
  920. tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
  921. {
  922. struct tvp514x_platform_data *pdata = tvp514x_get_pdata(client);
  923. struct tvp514x_decoder *decoder;
  924. struct v4l2_subdev *sd;
  925. int ret;
  926. if (pdata == NULL) {
  927. dev_err(&client->dev, "No platform data\n");
  928. return -EINVAL;
  929. }
  930. /* Check if the adapter supports the needed features */
  931. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  932. return -EIO;
  933. decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
  934. if (!decoder)
  935. return -ENOMEM;
  936. /* Initialize the tvp514x_decoder with default configuration */
  937. *decoder = tvp514x_dev;
  938. /* Copy default register configuration */
  939. memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
  940. sizeof(tvp514x_reg_list_default));
  941. decoder->int_seq = (struct tvp514x_reg *)id->driver_data;
  942. /* Copy board specific information here */
  943. decoder->pdata = pdata;
  944. /**
  945. * Fetch platform specific data, and configure the
  946. * tvp514x_reg_list[] accordingly. Since this is one
  947. * time configuration, no need to preserve.
  948. */
  949. decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
  950. (decoder->pdata->clk_polarity << 1);
  951. decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
  952. ((decoder->pdata->hs_polarity << 2) |
  953. (decoder->pdata->vs_polarity << 3));
  954. /* Set default standard to auto */
  955. decoder->tvp514x_regs[REG_VIDEO_STD].val =
  956. VIDEO_STD_AUTO_SWITCH_BIT;
  957. /* Register with V4L2 layer as slave device */
  958. sd = &decoder->sd;
  959. v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
  960. #if defined(CONFIG_MEDIA_CONTROLLER)
  961. decoder->pad.flags = MEDIA_PAD_FL_SOURCE;
  962. decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  963. decoder->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
  964. ret = media_entity_init(&decoder->sd.entity, 1, &decoder->pad, 0);
  965. if (ret < 0) {
  966. v4l2_err(sd, "%s decoder driver failed to register !!\n",
  967. sd->name);
  968. return ret;
  969. }
  970. #endif
  971. v4l2_ctrl_handler_init(&decoder->hdl, 5);
  972. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  973. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  974. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  975. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  976. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  977. V4L2_CID_SATURATION, 0, 255, 1, 128);
  978. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  979. V4L2_CID_HUE, -180, 180, 180, 0);
  980. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  981. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  982. sd->ctrl_handler = &decoder->hdl;
  983. if (decoder->hdl.error) {
  984. ret = decoder->hdl.error;
  985. goto done;
  986. }
  987. v4l2_ctrl_handler_setup(&decoder->hdl);
  988. ret = v4l2_async_register_subdev(&decoder->sd);
  989. if (!ret)
  990. v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
  991. done:
  992. if (ret < 0) {
  993. v4l2_ctrl_handler_free(&decoder->hdl);
  994. #if defined(CONFIG_MEDIA_CONTROLLER)
  995. media_entity_cleanup(&decoder->sd.entity);
  996. #endif
  997. }
  998. return ret;
  999. }
  1000. /**
  1001. * tvp514x_remove() - decoder driver i2c remove handler
  1002. * @client: i2c driver client device structure
  1003. *
  1004. * Unregister decoder as an i2c client device and V4L2
  1005. * device. Complement of tvp514x_probe().
  1006. */
  1007. static int tvp514x_remove(struct i2c_client *client)
  1008. {
  1009. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1010. struct tvp514x_decoder *decoder = to_decoder(sd);
  1011. v4l2_async_unregister_subdev(&decoder->sd);
  1012. #if defined(CONFIG_MEDIA_CONTROLLER)
  1013. media_entity_cleanup(&decoder->sd.entity);
  1014. #endif
  1015. v4l2_ctrl_handler_free(&decoder->hdl);
  1016. return 0;
  1017. }
  1018. /* TVP5146 Init/Power on Sequence */
  1019. static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
  1020. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1021. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1022. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1023. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1024. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1025. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1026. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1027. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1028. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1029. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1030. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1031. {TOK_TERM, 0, 0},
  1032. };
  1033. /* TVP5147 Init/Power on Sequence */
  1034. static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
  1035. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1036. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1037. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1038. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1039. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1040. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1041. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1042. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1043. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
  1044. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1045. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
  1046. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
  1047. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1048. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1049. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1050. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1051. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1052. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1053. {TOK_TERM, 0, 0},
  1054. };
  1055. /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
  1056. static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
  1057. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1058. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1059. {TOK_TERM, 0, 0},
  1060. };
  1061. /**
  1062. * I2C Device Table -
  1063. *
  1064. * name - Name of the actual device/chip.
  1065. * driver_data - Driver data
  1066. */
  1067. static const struct i2c_device_id tvp514x_id[] = {
  1068. {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
  1069. {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
  1070. {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
  1071. {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
  1072. {},
  1073. };
  1074. MODULE_DEVICE_TABLE(i2c, tvp514x_id);
  1075. #if IS_ENABLED(CONFIG_OF)
  1076. static const struct of_device_id tvp514x_of_match[] = {
  1077. { .compatible = "ti,tvp5146", },
  1078. { .compatible = "ti,tvp5146m2", },
  1079. { .compatible = "ti,tvp5147", },
  1080. { .compatible = "ti,tvp5147m1", },
  1081. { /* sentinel */ },
  1082. };
  1083. MODULE_DEVICE_TABLE(of, tvp514x_of_match);
  1084. #endif
  1085. static struct i2c_driver tvp514x_driver = {
  1086. .driver = {
  1087. .of_match_table = of_match_ptr(tvp514x_of_match),
  1088. .owner = THIS_MODULE,
  1089. .name = TVP514X_MODULE_NAME,
  1090. },
  1091. .probe = tvp514x_probe,
  1092. .remove = tvp514x_remove,
  1093. .id_table = tvp514x_id,
  1094. };
  1095. module_i2c_driver(tvp514x_driver);