tvp514x.c 43 KB

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