tvp514x.c 38 KB

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