tvp514x.c 35 KB

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