tvp514x.c 35 KB

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