tvp514x.c 35 KB

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