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