tvp514x.c 35 KB

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