tvp5150.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /*
  2. * tvp5150 - Texas Instruments TVP5150A/AM1 video decoder driver
  3. *
  4. * Copyright (c) 2005,2006 Mauro Carvalho Chehab (mchehab@infradead.org)
  5. * This code is placed under the terms of the GNU General Public License v2
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/slab.h>
  9. #include <linux/videodev2.h>
  10. #include <linux/delay.h>
  11. #include <linux/module.h>
  12. #include <media/v4l2-device.h>
  13. #include <media/tvp5150.h>
  14. #include <media/v4l2-ctrls.h>
  15. #include "tvp5150_reg.h"
  16. #define TVP5150_H_MAX 720U
  17. #define TVP5150_V_MAX_525_60 480U
  18. #define TVP5150_V_MAX_OTHERS 576U
  19. #define TVP5150_MAX_CROP_LEFT 511
  20. #define TVP5150_MAX_CROP_TOP 127
  21. #define TVP5150_CROP_SHIFT 2
  22. MODULE_DESCRIPTION("Texas Instruments TVP5150A video decoder driver");
  23. MODULE_AUTHOR("Mauro Carvalho Chehab");
  24. MODULE_LICENSE("GPL");
  25. static int debug;
  26. module_param(debug, int, 0644);
  27. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  28. struct tvp5150 {
  29. struct v4l2_subdev sd;
  30. struct v4l2_ctrl_handler hdl;
  31. struct v4l2_rect rect;
  32. v4l2_std_id norm; /* Current set standard */
  33. u32 input;
  34. u32 output;
  35. int enable;
  36. };
  37. static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
  38. {
  39. return container_of(sd, struct tvp5150, sd);
  40. }
  41. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  42. {
  43. return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
  44. }
  45. static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
  46. {
  47. struct i2c_client *c = v4l2_get_subdevdata(sd);
  48. int rc;
  49. rc = i2c_smbus_read_byte_data(c, addr);
  50. if (rc < 0) {
  51. v4l2_err(sd, "i2c i/o error: rc == %d\n", rc);
  52. return rc;
  53. }
  54. v4l2_dbg(2, debug, sd, "tvp5150: read 0x%02x = 0x%02x\n", addr, rc);
  55. return rc;
  56. }
  57. static inline void tvp5150_write(struct v4l2_subdev *sd, unsigned char addr,
  58. unsigned char value)
  59. {
  60. struct i2c_client *c = v4l2_get_subdevdata(sd);
  61. int rc;
  62. v4l2_dbg(2, debug, sd, "tvp5150: writing 0x%02x 0x%02x\n", addr, value);
  63. rc = i2c_smbus_write_byte_data(c, addr, value);
  64. if (rc < 0)
  65. v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d\n", rc);
  66. }
  67. static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
  68. const u8 end, int max_line)
  69. {
  70. int i = 0;
  71. while (init != (u8)(end + 1)) {
  72. if ((i % max_line) == 0) {
  73. if (i > 0)
  74. printk("\n");
  75. printk("tvp5150: %s reg 0x%02x = ", s, init);
  76. }
  77. printk("%02x ", tvp5150_read(sd, init));
  78. init++;
  79. i++;
  80. }
  81. printk("\n");
  82. }
  83. static int tvp5150_log_status(struct v4l2_subdev *sd)
  84. {
  85. printk("tvp5150: Video input source selection #1 = 0x%02x\n",
  86. tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
  87. printk("tvp5150: Analog channel controls = 0x%02x\n",
  88. tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
  89. printk("tvp5150: Operation mode controls = 0x%02x\n",
  90. tvp5150_read(sd, TVP5150_OP_MODE_CTL));
  91. printk("tvp5150: Miscellaneous controls = 0x%02x\n",
  92. tvp5150_read(sd, TVP5150_MISC_CTL));
  93. printk("tvp5150: Autoswitch mask= 0x%02x\n",
  94. tvp5150_read(sd, TVP5150_AUTOSW_MSK));
  95. printk("tvp5150: Color killer threshold control = 0x%02x\n",
  96. tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
  97. printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
  98. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
  99. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
  100. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
  101. printk("tvp5150: Brightness control = 0x%02x\n",
  102. tvp5150_read(sd, TVP5150_BRIGHT_CTL));
  103. printk("tvp5150: Color saturation control = 0x%02x\n",
  104. tvp5150_read(sd, TVP5150_SATURATION_CTL));
  105. printk("tvp5150: Hue control = 0x%02x\n",
  106. tvp5150_read(sd, TVP5150_HUE_CTL));
  107. printk("tvp5150: Contrast control = 0x%02x\n",
  108. tvp5150_read(sd, TVP5150_CONTRAST_CTL));
  109. printk("tvp5150: Outputs and data rates select = 0x%02x\n",
  110. tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
  111. printk("tvp5150: Configuration shared pins = 0x%02x\n",
  112. tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
  113. printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
  114. tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
  115. tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
  116. printk("tvp5150: Active video cropping stop = 0x%02x%02x\n",
  117. tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
  118. tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
  119. printk("tvp5150: Genlock/RTC = 0x%02x\n",
  120. tvp5150_read(sd, TVP5150_GENLOCK));
  121. printk("tvp5150: Horizontal sync start = 0x%02x\n",
  122. tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
  123. printk("tvp5150: Vertical blanking start = 0x%02x\n",
  124. tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
  125. printk("tvp5150: Vertical blanking stop = 0x%02x\n",
  126. tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
  127. printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
  128. tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
  129. tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
  130. printk("tvp5150: Interrupt reset register B = 0x%02x\n",
  131. tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
  132. printk("tvp5150: Interrupt enable register B = 0x%02x\n",
  133. tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
  134. printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
  135. tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
  136. printk("tvp5150: Video standard = 0x%02x\n",
  137. tvp5150_read(sd, TVP5150_VIDEO_STD));
  138. printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
  139. tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
  140. tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
  141. printk("tvp5150: Macrovision on counter = 0x%02x\n",
  142. tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
  143. printk("tvp5150: Macrovision off counter = 0x%02x\n",
  144. tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
  145. printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
  146. (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
  147. printk("tvp5150: Device ID = %02x%02x\n",
  148. tvp5150_read(sd, TVP5150_MSB_DEV_ID),
  149. tvp5150_read(sd, TVP5150_LSB_DEV_ID));
  150. printk("tvp5150: ROM version = (hex) %02x.%02x\n",
  151. tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
  152. tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
  153. printk("tvp5150: Vertical line count = 0x%02x%02x\n",
  154. tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
  155. tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
  156. printk("tvp5150: Interrupt status register B = 0x%02x\n",
  157. tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
  158. printk("tvp5150: Interrupt active register B = 0x%02x\n",
  159. tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
  160. printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
  161. tvp5150_read(sd, TVP5150_STATUS_REG_1),
  162. tvp5150_read(sd, TVP5150_STATUS_REG_2),
  163. tvp5150_read(sd, TVP5150_STATUS_REG_3),
  164. tvp5150_read(sd, TVP5150_STATUS_REG_4),
  165. tvp5150_read(sd, TVP5150_STATUS_REG_5));
  166. dump_reg_range(sd, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI,
  167. TVP5150_TELETEXT_FIL1_END, 8);
  168. dump_reg_range(sd, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI,
  169. TVP5150_TELETEXT_FIL2_END, 8);
  170. printk("tvp5150: Teletext filter enable = 0x%02x\n",
  171. tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
  172. printk("tvp5150: Interrupt status register A = 0x%02x\n",
  173. tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
  174. printk("tvp5150: Interrupt enable register A = 0x%02x\n",
  175. tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
  176. printk("tvp5150: Interrupt configuration = 0x%02x\n",
  177. tvp5150_read(sd, TVP5150_INT_CONF));
  178. printk("tvp5150: VDP status register = 0x%02x\n",
  179. tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
  180. printk("tvp5150: FIFO word count = 0x%02x\n",
  181. tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
  182. printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
  183. tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
  184. printk("tvp5150: FIFO reset = 0x%02x\n",
  185. tvp5150_read(sd, TVP5150_FIFO_RESET));
  186. printk("tvp5150: Line number interrupt = 0x%02x\n",
  187. tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
  188. printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
  189. tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
  190. tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
  191. printk("tvp5150: FIFO output control = 0x%02x\n",
  192. tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
  193. printk("tvp5150: Full field enable = 0x%02x\n",
  194. tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
  195. printk("tvp5150: Full field mode register = 0x%02x\n",
  196. tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
  197. dump_reg_range(sd, "CC data", TVP5150_CC_DATA_INI,
  198. TVP5150_CC_DATA_END, 8);
  199. dump_reg_range(sd, "WSS data", TVP5150_WSS_DATA_INI,
  200. TVP5150_WSS_DATA_END, 8);
  201. dump_reg_range(sd, "VPS data", TVP5150_VPS_DATA_INI,
  202. TVP5150_VPS_DATA_END, 8);
  203. dump_reg_range(sd, "VITC data", TVP5150_VITC_DATA_INI,
  204. TVP5150_VITC_DATA_END, 10);
  205. dump_reg_range(sd, "Line mode", TVP5150_LINE_MODE_INI,
  206. TVP5150_LINE_MODE_END, 8);
  207. return 0;
  208. }
  209. /****************************************************************************
  210. Basic functions
  211. ****************************************************************************/
  212. static inline void tvp5150_selmux(struct v4l2_subdev *sd)
  213. {
  214. int opmode = 0;
  215. struct tvp5150 *decoder = to_tvp5150(sd);
  216. int input = 0;
  217. int val;
  218. if ((decoder->output & TVP5150_BLACK_SCREEN) || !decoder->enable)
  219. input = 8;
  220. switch (decoder->input) {
  221. case TVP5150_COMPOSITE1:
  222. input |= 2;
  223. /* fall through */
  224. case TVP5150_COMPOSITE0:
  225. break;
  226. case TVP5150_SVIDEO:
  227. default:
  228. input |= 1;
  229. break;
  230. }
  231. v4l2_dbg(1, debug, sd, "Selecting video route: route input=%i, output=%i "
  232. "=> tvp5150 input=%i, opmode=%i\n",
  233. decoder->input, decoder->output,
  234. input, opmode);
  235. tvp5150_write(sd, TVP5150_OP_MODE_CTL, opmode);
  236. tvp5150_write(sd, TVP5150_VD_IN_SRC_SEL_1, input);
  237. /* Svideo should enable YCrCb output and disable GPCL output
  238. * For Composite and TV, it should be the reverse
  239. */
  240. val = tvp5150_read(sd, TVP5150_MISC_CTL);
  241. if (val < 0) {
  242. v4l2_err(sd, "%s: failed with error = %d\n", __func__, val);
  243. return;
  244. }
  245. if (decoder->input == TVP5150_SVIDEO)
  246. val = (val & ~0x40) | 0x10;
  247. else
  248. val = (val & ~0x10) | 0x40;
  249. tvp5150_write(sd, TVP5150_MISC_CTL, val);
  250. };
  251. struct i2c_reg_value {
  252. unsigned char reg;
  253. unsigned char value;
  254. };
  255. /* Default values as sugested at TVP5150AM1 datasheet */
  256. static const struct i2c_reg_value tvp5150_init_default[] = {
  257. { /* 0x00 */
  258. TVP5150_VD_IN_SRC_SEL_1,0x00
  259. },
  260. { /* 0x01 */
  261. TVP5150_ANAL_CHL_CTL,0x15
  262. },
  263. { /* 0x02 */
  264. TVP5150_OP_MODE_CTL,0x00
  265. },
  266. { /* 0x03 */
  267. TVP5150_MISC_CTL,0x01
  268. },
  269. { /* 0x06 */
  270. TVP5150_COLOR_KIL_THSH_CTL,0x10
  271. },
  272. { /* 0x07 */
  273. TVP5150_LUMA_PROC_CTL_1,0x60
  274. },
  275. { /* 0x08 */
  276. TVP5150_LUMA_PROC_CTL_2,0x00
  277. },
  278. { /* 0x09 */
  279. TVP5150_BRIGHT_CTL,0x80
  280. },
  281. { /* 0x0a */
  282. TVP5150_SATURATION_CTL,0x80
  283. },
  284. { /* 0x0b */
  285. TVP5150_HUE_CTL,0x00
  286. },
  287. { /* 0x0c */
  288. TVP5150_CONTRAST_CTL,0x80
  289. },
  290. { /* 0x0d */
  291. TVP5150_DATA_RATE_SEL,0x47
  292. },
  293. { /* 0x0e */
  294. TVP5150_LUMA_PROC_CTL_3,0x00
  295. },
  296. { /* 0x0f */
  297. TVP5150_CONF_SHARED_PIN,0x08
  298. },
  299. { /* 0x11 */
  300. TVP5150_ACT_VD_CROP_ST_MSB,0x00
  301. },
  302. { /* 0x12 */
  303. TVP5150_ACT_VD_CROP_ST_LSB,0x00
  304. },
  305. { /* 0x13 */
  306. TVP5150_ACT_VD_CROP_STP_MSB,0x00
  307. },
  308. { /* 0x14 */
  309. TVP5150_ACT_VD_CROP_STP_LSB,0x00
  310. },
  311. { /* 0x15 */
  312. TVP5150_GENLOCK,0x01
  313. },
  314. { /* 0x16 */
  315. TVP5150_HORIZ_SYNC_START,0x80
  316. },
  317. { /* 0x18 */
  318. TVP5150_VERT_BLANKING_START,0x00
  319. },
  320. { /* 0x19 */
  321. TVP5150_VERT_BLANKING_STOP,0x00
  322. },
  323. { /* 0x1a */
  324. TVP5150_CHROMA_PROC_CTL_1,0x0c
  325. },
  326. { /* 0x1b */
  327. TVP5150_CHROMA_PROC_CTL_2,0x14
  328. },
  329. { /* 0x1c */
  330. TVP5150_INT_RESET_REG_B,0x00
  331. },
  332. { /* 0x1d */
  333. TVP5150_INT_ENABLE_REG_B,0x00
  334. },
  335. { /* 0x1e */
  336. TVP5150_INTT_CONFIG_REG_B,0x00
  337. },
  338. { /* 0x28 */
  339. TVP5150_VIDEO_STD,0x00
  340. },
  341. { /* 0x2e */
  342. TVP5150_MACROVISION_ON_CTR,0x0f
  343. },
  344. { /* 0x2f */
  345. TVP5150_MACROVISION_OFF_CTR,0x01
  346. },
  347. { /* 0xbb */
  348. TVP5150_TELETEXT_FIL_ENA,0x00
  349. },
  350. { /* 0xc0 */
  351. TVP5150_INT_STATUS_REG_A,0x00
  352. },
  353. { /* 0xc1 */
  354. TVP5150_INT_ENABLE_REG_A,0x00
  355. },
  356. { /* 0xc2 */
  357. TVP5150_INT_CONF,0x04
  358. },
  359. { /* 0xc8 */
  360. TVP5150_FIFO_INT_THRESHOLD,0x80
  361. },
  362. { /* 0xc9 */
  363. TVP5150_FIFO_RESET,0x00
  364. },
  365. { /* 0xca */
  366. TVP5150_LINE_NUMBER_INT,0x00
  367. },
  368. { /* 0xcb */
  369. TVP5150_PIX_ALIGN_REG_LOW,0x4e
  370. },
  371. { /* 0xcc */
  372. TVP5150_PIX_ALIGN_REG_HIGH,0x00
  373. },
  374. { /* 0xcd */
  375. TVP5150_FIFO_OUT_CTRL,0x01
  376. },
  377. { /* 0xcf */
  378. TVP5150_FULL_FIELD_ENA,0x00
  379. },
  380. { /* 0xd0 */
  381. TVP5150_LINE_MODE_INI,0x00
  382. },
  383. { /* 0xfc */
  384. TVP5150_FULL_FIELD_MODE_REG,0x7f
  385. },
  386. { /* end of data */
  387. 0xff,0xff
  388. }
  389. };
  390. /* Default values as sugested at TVP5150AM1 datasheet */
  391. static const struct i2c_reg_value tvp5150_init_enable[] = {
  392. {
  393. TVP5150_CONF_SHARED_PIN, 2
  394. },{ /* Automatic offset and AGC enabled */
  395. TVP5150_ANAL_CHL_CTL, 0x15
  396. },{ /* Activate YCrCb output 0x9 or 0xd ? */
  397. TVP5150_MISC_CTL, 0x6f
  398. },{ /* Activates video std autodetection for all standards */
  399. TVP5150_AUTOSW_MSK, 0x0
  400. },{ /* Default format: 0x47. For 4:2:2: 0x40 */
  401. TVP5150_DATA_RATE_SEL, 0x47
  402. },{
  403. TVP5150_CHROMA_PROC_CTL_1, 0x0c
  404. },{
  405. TVP5150_CHROMA_PROC_CTL_2, 0x54
  406. },{ /* Non documented, but initialized on WinTV USB2 */
  407. 0x27, 0x20
  408. },{
  409. 0xff,0xff
  410. }
  411. };
  412. struct tvp5150_vbi_type {
  413. unsigned int vbi_type;
  414. unsigned int ini_line;
  415. unsigned int end_line;
  416. unsigned int by_field :1;
  417. };
  418. struct i2c_vbi_ram_value {
  419. u16 reg;
  420. struct tvp5150_vbi_type type;
  421. unsigned char values[16];
  422. };
  423. /* This struct have the values for each supported VBI Standard
  424. * by
  425. tvp5150_vbi_types should follow the same order as vbi_ram_default
  426. * value 0 means rom position 0x10, value 1 means rom position 0x30
  427. * and so on. There are 16 possible locations from 0 to 15.
  428. */
  429. static struct i2c_vbi_ram_value vbi_ram_default[] =
  430. {
  431. /* FIXME: Current api doesn't handle all VBI types, those not
  432. yet supported are placed under #if 0 */
  433. #if 0
  434. {0x010, /* Teletext, SECAM, WST System A */
  435. {V4L2_SLICED_TELETEXT_SECAM,6,23,1},
  436. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
  437. 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
  438. },
  439. #endif
  440. {0x030, /* Teletext, PAL, WST System B */
  441. {V4L2_SLICED_TELETEXT_B,6,22,1},
  442. { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
  443. 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
  444. },
  445. #if 0
  446. {0x050, /* Teletext, PAL, WST System C */
  447. {V4L2_SLICED_TELETEXT_PAL_C,6,22,1},
  448. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
  449. 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  450. },
  451. {0x070, /* Teletext, NTSC, WST System B */
  452. {V4L2_SLICED_TELETEXT_NTSC_B,10,21,1},
  453. { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
  454. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  455. },
  456. {0x090, /* Tetetext, NTSC NABTS System C */
  457. {V4L2_SLICED_TELETEXT_NTSC_C,10,21,1},
  458. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
  459. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
  460. },
  461. {0x0b0, /* Teletext, NTSC-J, NABTS System D */
  462. {V4L2_SLICED_TELETEXT_NTSC_D,10,21,1},
  463. { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
  464. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  465. },
  466. {0x0d0, /* Closed Caption, PAL/SECAM */
  467. {V4L2_SLICED_CAPTION_625,22,22,1},
  468. { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
  469. 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
  470. },
  471. #endif
  472. {0x0f0, /* Closed Caption, NTSC */
  473. {V4L2_SLICED_CAPTION_525,21,21,1},
  474. { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
  475. 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
  476. },
  477. {0x110, /* Wide Screen Signal, PAL/SECAM */
  478. {V4L2_SLICED_WSS_625,23,23,1},
  479. { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
  480. 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
  481. },
  482. #if 0
  483. {0x130, /* Wide Screen Signal, NTSC C */
  484. {V4L2_SLICED_WSS_525,20,20,1},
  485. { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
  486. 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
  487. },
  488. {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
  489. {V4l2_SLICED_VITC_625,6,22,0},
  490. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
  491. 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
  492. },
  493. {0x170, /* Vertical Interval Timecode (VITC), NTSC */
  494. {V4l2_SLICED_VITC_525,10,20,0},
  495. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
  496. 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
  497. },
  498. #endif
  499. {0x190, /* Video Program System (VPS), PAL */
  500. {V4L2_SLICED_VPS,16,16,0},
  501. { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
  502. 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
  503. },
  504. /* 0x1d0 User programmable */
  505. /* End of struct */
  506. { (u16)-1 }
  507. };
  508. static int tvp5150_write_inittab(struct v4l2_subdev *sd,
  509. const struct i2c_reg_value *regs)
  510. {
  511. while (regs->reg != 0xff) {
  512. tvp5150_write(sd, regs->reg, regs->value);
  513. regs++;
  514. }
  515. return 0;
  516. }
  517. static int tvp5150_vdp_init(struct v4l2_subdev *sd,
  518. const struct i2c_vbi_ram_value *regs)
  519. {
  520. unsigned int i;
  521. /* Disable Full Field */
  522. tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
  523. /* Before programming, Line mode should be at 0xff */
  524. for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
  525. tvp5150_write(sd, i, 0xff);
  526. /* Load Ram Table */
  527. while (regs->reg != (u16)-1) {
  528. tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
  529. tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
  530. for (i = 0; i < 16; i++)
  531. tvp5150_write(sd, TVP5150_VDP_CONF_RAM_DATA, regs->values[i]);
  532. regs++;
  533. }
  534. return 0;
  535. }
  536. /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
  537. static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
  538. struct v4l2_sliced_vbi_cap *cap)
  539. {
  540. const struct i2c_vbi_ram_value *regs = vbi_ram_default;
  541. int line;
  542. v4l2_dbg(1, debug, sd, "g_sliced_vbi_cap\n");
  543. memset(cap, 0, sizeof *cap);
  544. while (regs->reg != (u16)-1 ) {
  545. for (line=regs->type.ini_line;line<=regs->type.end_line;line++) {
  546. cap->service_lines[0][line] |= regs->type.vbi_type;
  547. }
  548. cap->service_set |= regs->type.vbi_type;
  549. regs++;
  550. }
  551. return 0;
  552. }
  553. /* Set vbi processing
  554. * type - one of tvp5150_vbi_types
  555. * line - line to gather data
  556. * fields: bit 0 field1, bit 1, field2
  557. * flags (default=0xf0) is a bitmask, were set means:
  558. * bit 7: enable filtering null bytes on CC
  559. * bit 6: send data also to FIFO
  560. * bit 5: don't allow data with errors on FIFO
  561. * bit 4: enable ECC when possible
  562. * pix_align = pix alignment:
  563. * LSB = field1
  564. * MSB = field2
  565. */
  566. static int tvp5150_set_vbi(struct v4l2_subdev *sd,
  567. const struct i2c_vbi_ram_value *regs,
  568. unsigned int type,u8 flags, int line,
  569. const int fields)
  570. {
  571. struct tvp5150 *decoder = to_tvp5150(sd);
  572. v4l2_std_id std = decoder->norm;
  573. u8 reg;
  574. int pos=0;
  575. if (std == V4L2_STD_ALL) {
  576. v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
  577. return 0;
  578. } else if (std & V4L2_STD_625_50) {
  579. /* Don't follow NTSC Line number convension */
  580. line += 3;
  581. }
  582. if (line<6||line>27)
  583. return 0;
  584. while (regs->reg != (u16)-1 ) {
  585. if ((type & regs->type.vbi_type) &&
  586. (line>=regs->type.ini_line) &&
  587. (line<=regs->type.end_line)) {
  588. type=regs->type.vbi_type;
  589. break;
  590. }
  591. regs++;
  592. pos++;
  593. }
  594. if (regs->reg == (u16)-1)
  595. return 0;
  596. type=pos | (flags & 0xf0);
  597. reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
  598. if (fields&1) {
  599. tvp5150_write(sd, reg, type);
  600. }
  601. if (fields&2) {
  602. tvp5150_write(sd, reg+1, type);
  603. }
  604. return type;
  605. }
  606. static int tvp5150_get_vbi(struct v4l2_subdev *sd,
  607. const struct i2c_vbi_ram_value *regs, int line)
  608. {
  609. struct tvp5150 *decoder = to_tvp5150(sd);
  610. v4l2_std_id std = decoder->norm;
  611. u8 reg;
  612. int pos, type = 0;
  613. int i, ret = 0;
  614. if (std == V4L2_STD_ALL) {
  615. v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
  616. return 0;
  617. } else if (std & V4L2_STD_625_50) {
  618. /* Don't follow NTSC Line number convension */
  619. line += 3;
  620. }
  621. if (line < 6 || line > 27)
  622. return 0;
  623. reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
  624. for (i = 0; i <= 1; i++) {
  625. ret = tvp5150_read(sd, reg + i);
  626. if (ret < 0) {
  627. v4l2_err(sd, "%s: failed with error = %d\n",
  628. __func__, ret);
  629. return 0;
  630. }
  631. pos = ret & 0x0f;
  632. if (pos < 0x0f)
  633. type |= regs[pos].type.vbi_type;
  634. }
  635. return type;
  636. }
  637. static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
  638. {
  639. struct tvp5150 *decoder = to_tvp5150(sd);
  640. int fmt = 0;
  641. decoder->norm = std;
  642. /* First tests should be against specific std */
  643. if (std == V4L2_STD_NTSC_443) {
  644. fmt = VIDEO_STD_NTSC_4_43_BIT;
  645. } else if (std == V4L2_STD_PAL_M) {
  646. fmt = VIDEO_STD_PAL_M_BIT;
  647. } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
  648. fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
  649. } else {
  650. /* Then, test against generic ones */
  651. if (std & V4L2_STD_NTSC)
  652. fmt = VIDEO_STD_NTSC_MJ_BIT;
  653. else if (std & V4L2_STD_PAL)
  654. fmt = VIDEO_STD_PAL_BDGHIN_BIT;
  655. else if (std & V4L2_STD_SECAM)
  656. fmt = VIDEO_STD_SECAM_BIT;
  657. }
  658. v4l2_dbg(1, debug, sd, "Set video std register to %d.\n", fmt);
  659. tvp5150_write(sd, TVP5150_VIDEO_STD, fmt);
  660. return 0;
  661. }
  662. static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  663. {
  664. struct tvp5150 *decoder = to_tvp5150(sd);
  665. if (decoder->norm == std)
  666. return 0;
  667. /* Change cropping height limits */
  668. if (std & V4L2_STD_525_60)
  669. decoder->rect.height = TVP5150_V_MAX_525_60;
  670. else
  671. decoder->rect.height = TVP5150_V_MAX_OTHERS;
  672. return tvp5150_set_std(sd, std);
  673. }
  674. static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
  675. {
  676. struct tvp5150 *decoder = to_tvp5150(sd);
  677. /* Initializes TVP5150 to its default values */
  678. tvp5150_write_inittab(sd, tvp5150_init_default);
  679. /* Initializes VDP registers */
  680. tvp5150_vdp_init(sd, vbi_ram_default);
  681. /* Selects decoder input */
  682. tvp5150_selmux(sd);
  683. /* Initializes TVP5150 to stream enabled values */
  684. tvp5150_write_inittab(sd, tvp5150_init_enable);
  685. /* Initialize image preferences */
  686. v4l2_ctrl_handler_setup(&decoder->hdl);
  687. tvp5150_set_std(sd, decoder->norm);
  688. return 0;
  689. };
  690. static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
  691. {
  692. struct v4l2_subdev *sd = to_sd(ctrl);
  693. switch (ctrl->id) {
  694. case V4L2_CID_BRIGHTNESS:
  695. tvp5150_write(sd, TVP5150_BRIGHT_CTL, ctrl->val);
  696. return 0;
  697. case V4L2_CID_CONTRAST:
  698. tvp5150_write(sd, TVP5150_CONTRAST_CTL, ctrl->val);
  699. return 0;
  700. case V4L2_CID_SATURATION:
  701. tvp5150_write(sd, TVP5150_SATURATION_CTL, ctrl->val);
  702. return 0;
  703. case V4L2_CID_HUE:
  704. tvp5150_write(sd, TVP5150_HUE_CTL, ctrl->val);
  705. return 0;
  706. }
  707. return -EINVAL;
  708. }
  709. static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
  710. {
  711. int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
  712. switch (val & 0x0F) {
  713. case 0x01:
  714. return V4L2_STD_NTSC;
  715. case 0x03:
  716. return V4L2_STD_PAL;
  717. case 0x05:
  718. return V4L2_STD_PAL_M;
  719. case 0x07:
  720. return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
  721. case 0x09:
  722. return V4L2_STD_NTSC_443;
  723. case 0xb:
  724. return V4L2_STD_SECAM;
  725. default:
  726. return V4L2_STD_UNKNOWN;
  727. }
  728. }
  729. static int tvp5150_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
  730. u32 *code)
  731. {
  732. if (index)
  733. return -EINVAL;
  734. *code = MEDIA_BUS_FMT_UYVY8_2X8;
  735. return 0;
  736. }
  737. static int tvp5150_mbus_fmt(struct v4l2_subdev *sd,
  738. struct v4l2_mbus_framefmt *f)
  739. {
  740. struct tvp5150 *decoder = to_tvp5150(sd);
  741. if (f == NULL)
  742. return -EINVAL;
  743. tvp5150_reset(sd, 0);
  744. f->width = decoder->rect.width;
  745. f->height = decoder->rect.height;
  746. f->code = MEDIA_BUS_FMT_UYVY8_2X8;
  747. f->field = V4L2_FIELD_SEQ_TB;
  748. f->colorspace = V4L2_COLORSPACE_SMPTE170M;
  749. v4l2_dbg(1, debug, sd, "width = %d, height = %d\n", f->width,
  750. f->height);
  751. return 0;
  752. }
  753. static int tvp5150_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
  754. {
  755. struct v4l2_rect rect = a->c;
  756. struct tvp5150 *decoder = to_tvp5150(sd);
  757. v4l2_std_id std;
  758. unsigned int hmax;
  759. v4l2_dbg(1, debug, sd, "%s left=%d, top=%d, width=%d, height=%d\n",
  760. __func__, rect.left, rect.top, rect.width, rect.height);
  761. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  762. return -EINVAL;
  763. /* tvp5150 has some special limits */
  764. rect.left = clamp(rect.left, 0, TVP5150_MAX_CROP_LEFT);
  765. rect.width = clamp_t(unsigned int, rect.width,
  766. TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect.left,
  767. TVP5150_H_MAX - rect.left);
  768. rect.top = clamp(rect.top, 0, TVP5150_MAX_CROP_TOP);
  769. /* Calculate height based on current standard */
  770. if (decoder->norm == V4L2_STD_ALL)
  771. std = tvp5150_read_std(sd);
  772. else
  773. std = decoder->norm;
  774. if (std & V4L2_STD_525_60)
  775. hmax = TVP5150_V_MAX_525_60;
  776. else
  777. hmax = TVP5150_V_MAX_OTHERS;
  778. rect.height = clamp_t(unsigned int, rect.height,
  779. hmax - TVP5150_MAX_CROP_TOP - rect.top,
  780. hmax - rect.top);
  781. tvp5150_write(sd, TVP5150_VERT_BLANKING_START, rect.top);
  782. tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP,
  783. rect.top + rect.height - hmax);
  784. tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_MSB,
  785. rect.left >> TVP5150_CROP_SHIFT);
  786. tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_LSB,
  787. rect.left | (1 << TVP5150_CROP_SHIFT));
  788. tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_MSB,
  789. (rect.left + rect.width - TVP5150_MAX_CROP_LEFT) >>
  790. TVP5150_CROP_SHIFT);
  791. tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_LSB,
  792. rect.left + rect.width - TVP5150_MAX_CROP_LEFT);
  793. decoder->rect = rect;
  794. return 0;
  795. }
  796. static int tvp5150_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  797. {
  798. struct tvp5150 *decoder = to_tvp5150(sd);
  799. a->c = decoder->rect;
  800. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  801. return 0;
  802. }
  803. static int tvp5150_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  804. {
  805. struct tvp5150 *decoder = to_tvp5150(sd);
  806. v4l2_std_id std;
  807. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  808. return -EINVAL;
  809. a->bounds.left = 0;
  810. a->bounds.top = 0;
  811. a->bounds.width = TVP5150_H_MAX;
  812. /* Calculate height based on current standard */
  813. if (decoder->norm == V4L2_STD_ALL)
  814. std = tvp5150_read_std(sd);
  815. else
  816. std = decoder->norm;
  817. if (std & V4L2_STD_525_60)
  818. a->bounds.height = TVP5150_V_MAX_525_60;
  819. else
  820. a->bounds.height = TVP5150_V_MAX_OTHERS;
  821. a->defrect = a->bounds;
  822. a->pixelaspect.numerator = 1;
  823. a->pixelaspect.denominator = 1;
  824. return 0;
  825. }
  826. /****************************************************************************
  827. I2C Command
  828. ****************************************************************************/
  829. static int tvp5150_s_routing(struct v4l2_subdev *sd,
  830. u32 input, u32 output, u32 config)
  831. {
  832. struct tvp5150 *decoder = to_tvp5150(sd);
  833. decoder->input = input;
  834. decoder->output = output;
  835. tvp5150_selmux(sd);
  836. return 0;
  837. }
  838. static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
  839. {
  840. /* this is for capturing 36 raw vbi lines
  841. if there's a way to cut off the beginning 2 vbi lines
  842. with the tvp5150 then the vbi line count could be lowered
  843. to 17 lines/field again, although I couldn't find a register
  844. which could do that cropping */
  845. if (fmt->sample_format == V4L2_PIX_FMT_GREY)
  846. tvp5150_write(sd, TVP5150_LUMA_PROC_CTL_1, 0x70);
  847. if (fmt->count[0] == 18 && fmt->count[1] == 18) {
  848. tvp5150_write(sd, TVP5150_VERT_BLANKING_START, 0x00);
  849. tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP, 0x01);
  850. }
  851. return 0;
  852. }
  853. static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  854. {
  855. int i;
  856. if (svbi->service_set != 0) {
  857. for (i = 0; i <= 23; i++) {
  858. svbi->service_lines[1][i] = 0;
  859. svbi->service_lines[0][i] =
  860. tvp5150_set_vbi(sd, vbi_ram_default,
  861. svbi->service_lines[0][i], 0xf0, i, 3);
  862. }
  863. /* Enables FIFO */
  864. tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 1);
  865. } else {
  866. /* Disables FIFO*/
  867. tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 0);
  868. /* Disable Full Field */
  869. tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
  870. /* Disable Line modes */
  871. for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
  872. tvp5150_write(sd, i, 0xff);
  873. }
  874. return 0;
  875. }
  876. static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  877. {
  878. int i, mask = 0;
  879. memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
  880. for (i = 0; i <= 23; i++) {
  881. svbi->service_lines[0][i] =
  882. tvp5150_get_vbi(sd, vbi_ram_default, i);
  883. mask |= svbi->service_lines[0][i];
  884. }
  885. svbi->service_set = mask;
  886. return 0;
  887. }
  888. #ifdef CONFIG_VIDEO_ADV_DEBUG
  889. static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  890. {
  891. int res;
  892. res = tvp5150_read(sd, reg->reg & 0xff);
  893. if (res < 0) {
  894. v4l2_err(sd, "%s: failed with error = %d\n", __func__, res);
  895. return res;
  896. }
  897. reg->val = res;
  898. reg->size = 1;
  899. return 0;
  900. }
  901. static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  902. {
  903. tvp5150_write(sd, reg->reg & 0xff, reg->val & 0xff);
  904. return 0;
  905. }
  906. #endif
  907. static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  908. {
  909. int status = tvp5150_read(sd, 0x88);
  910. vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
  911. return 0;
  912. }
  913. /* ----------------------------------------------------------------------- */
  914. static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
  915. .s_ctrl = tvp5150_s_ctrl,
  916. };
  917. static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
  918. .log_status = tvp5150_log_status,
  919. .reset = tvp5150_reset,
  920. #ifdef CONFIG_VIDEO_ADV_DEBUG
  921. .g_register = tvp5150_g_register,
  922. .s_register = tvp5150_s_register,
  923. #endif
  924. };
  925. static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
  926. .g_tuner = tvp5150_g_tuner,
  927. };
  928. static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
  929. .s_std = tvp5150_s_std,
  930. .s_routing = tvp5150_s_routing,
  931. .enum_mbus_fmt = tvp5150_enum_mbus_fmt,
  932. .s_mbus_fmt = tvp5150_mbus_fmt,
  933. .try_mbus_fmt = tvp5150_mbus_fmt,
  934. .g_mbus_fmt = tvp5150_mbus_fmt,
  935. .s_crop = tvp5150_s_crop,
  936. .g_crop = tvp5150_g_crop,
  937. .cropcap = tvp5150_cropcap,
  938. };
  939. static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
  940. .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
  941. .g_sliced_fmt = tvp5150_g_sliced_fmt,
  942. .s_sliced_fmt = tvp5150_s_sliced_fmt,
  943. .s_raw_fmt = tvp5150_s_raw_fmt,
  944. };
  945. static const struct v4l2_subdev_ops tvp5150_ops = {
  946. .core = &tvp5150_core_ops,
  947. .tuner = &tvp5150_tuner_ops,
  948. .video = &tvp5150_video_ops,
  949. .vbi = &tvp5150_vbi_ops,
  950. };
  951. /****************************************************************************
  952. I2C Client & Driver
  953. ****************************************************************************/
  954. static int tvp5150_probe(struct i2c_client *c,
  955. const struct i2c_device_id *id)
  956. {
  957. struct tvp5150 *core;
  958. struct v4l2_subdev *sd;
  959. int tvp5150_id[4];
  960. int i, res;
  961. /* Check if the adapter supports the needed features */
  962. if (!i2c_check_functionality(c->adapter,
  963. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  964. return -EIO;
  965. core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
  966. if (!core)
  967. return -ENOMEM;
  968. sd = &core->sd;
  969. v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
  970. /*
  971. * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
  972. * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
  973. */
  974. for (i = 0; i < 4; i++) {
  975. res = tvp5150_read(sd, TVP5150_MSB_DEV_ID + i);
  976. if (res < 0)
  977. return res;
  978. tvp5150_id[i] = res;
  979. }
  980. v4l_info(c, "chip found @ 0x%02x (%s)\n",
  981. c->addr << 1, c->adapter->name);
  982. if (tvp5150_id[2] == 4 && tvp5150_id[3] == 0) { /* Is TVP5150AM1 */
  983. v4l2_info(sd, "tvp%02x%02xam1 detected.\n",
  984. tvp5150_id[0], tvp5150_id[1]);
  985. /* ITU-T BT.656.4 timing */
  986. tvp5150_write(sd, TVP5150_REV_SELECT, 0);
  987. } else {
  988. /* Is TVP5150A */
  989. if (tvp5150_id[2] == 3 || tvp5150_id[3] == 0x21) {
  990. v4l2_info(sd, "tvp%02x%02xa detected.\n",
  991. tvp5150_id[0], tvp5150_id[1]);
  992. } else {
  993. v4l2_info(sd, "*** unknown tvp%02x%02x chip detected.\n",
  994. tvp5150_id[0], tvp5150_id[1]);
  995. v4l2_info(sd, "*** Rom ver is %d.%d\n",
  996. tvp5150_id[2], tvp5150_id[3]);
  997. }
  998. }
  999. core->norm = V4L2_STD_ALL; /* Default is autodetect */
  1000. core->input = TVP5150_COMPOSITE1;
  1001. core->enable = 1;
  1002. v4l2_ctrl_handler_init(&core->hdl, 4);
  1003. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1004. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1005. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1006. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  1007. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1008. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1009. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1010. V4L2_CID_HUE, -128, 127, 1, 0);
  1011. sd->ctrl_handler = &core->hdl;
  1012. if (core->hdl.error) {
  1013. res = core->hdl.error;
  1014. v4l2_ctrl_handler_free(&core->hdl);
  1015. return res;
  1016. }
  1017. v4l2_ctrl_handler_setup(&core->hdl);
  1018. /* Default is no cropping */
  1019. core->rect.top = 0;
  1020. if (tvp5150_read_std(sd) & V4L2_STD_525_60)
  1021. core->rect.height = TVP5150_V_MAX_525_60;
  1022. else
  1023. core->rect.height = TVP5150_V_MAX_OTHERS;
  1024. core->rect.left = 0;
  1025. core->rect.width = TVP5150_H_MAX;
  1026. if (debug > 1)
  1027. tvp5150_log_status(sd);
  1028. return 0;
  1029. }
  1030. static int tvp5150_remove(struct i2c_client *c)
  1031. {
  1032. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  1033. struct tvp5150 *decoder = to_tvp5150(sd);
  1034. v4l2_dbg(1, debug, sd,
  1035. "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
  1036. c->addr << 1);
  1037. v4l2_device_unregister_subdev(sd);
  1038. v4l2_ctrl_handler_free(&decoder->hdl);
  1039. return 0;
  1040. }
  1041. /* ----------------------------------------------------------------------- */
  1042. static const struct i2c_device_id tvp5150_id[] = {
  1043. { "tvp5150", 0 },
  1044. { }
  1045. };
  1046. MODULE_DEVICE_TABLE(i2c, tvp5150_id);
  1047. static struct i2c_driver tvp5150_driver = {
  1048. .driver = {
  1049. .owner = THIS_MODULE,
  1050. .name = "tvp5150",
  1051. },
  1052. .probe = tvp5150_probe,
  1053. .remove = tvp5150_remove,
  1054. .id_table = tvp5150_id,
  1055. };
  1056. module_i2c_driver(tvp5150_driver);