tvp514x.c 38 KB

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