tvp514x.c 36 KB

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