vpx3220.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * vpx3220a, vpx3216b & vpx3214c video decoder driver version 0.0.1
  3. *
  4. * Copyright (C) 2001 Laurent Pinchart <lpinchart@freegates.be>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/types.h>
  24. #include <linux/slab.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/i2c.h>
  27. #include <linux/videodev2.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-chip-ident.h>
  30. #include <media/v4l2-ctrls.h>
  31. MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
  32. MODULE_AUTHOR("Laurent Pinchart");
  33. MODULE_LICENSE("GPL");
  34. static int debug;
  35. module_param(debug, int, 0);
  36. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  37. #define VPX_TIMEOUT_COUNT 10
  38. /* ----------------------------------------------------------------------- */
  39. struct vpx3220 {
  40. struct v4l2_subdev sd;
  41. struct v4l2_ctrl_handler hdl;
  42. unsigned char reg[255];
  43. v4l2_std_id norm;
  44. int ident;
  45. int input;
  46. int enable;
  47. };
  48. static inline struct vpx3220 *to_vpx3220(struct v4l2_subdev *sd)
  49. {
  50. return container_of(sd, struct vpx3220, sd);
  51. }
  52. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  53. {
  54. return &container_of(ctrl->handler, struct vpx3220, hdl)->sd;
  55. }
  56. static char *inputs[] = { "internal", "composite", "svideo" };
  57. /* ----------------------------------------------------------------------- */
  58. static inline int vpx3220_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  59. {
  60. struct i2c_client *client = v4l2_get_subdevdata(sd);
  61. struct vpx3220 *decoder = i2c_get_clientdata(client);
  62. decoder->reg[reg] = value;
  63. return i2c_smbus_write_byte_data(client, reg, value);
  64. }
  65. static inline int vpx3220_read(struct v4l2_subdev *sd, u8 reg)
  66. {
  67. struct i2c_client *client = v4l2_get_subdevdata(sd);
  68. return i2c_smbus_read_byte_data(client, reg);
  69. }
  70. static int vpx3220_fp_status(struct v4l2_subdev *sd)
  71. {
  72. unsigned char status;
  73. unsigned int i;
  74. for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
  75. status = vpx3220_read(sd, 0x29);
  76. if (!(status & 4))
  77. return 0;
  78. udelay(10);
  79. if (need_resched())
  80. cond_resched();
  81. }
  82. return -1;
  83. }
  84. static int vpx3220_fp_write(struct v4l2_subdev *sd, u8 fpaddr, u16 data)
  85. {
  86. struct i2c_client *client = v4l2_get_subdevdata(sd);
  87. /* Write the 16-bit address to the FPWR register */
  88. if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
  89. v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
  90. return -1;
  91. }
  92. if (vpx3220_fp_status(sd) < 0)
  93. return -1;
  94. /* Write the 16-bit data to the FPDAT register */
  95. if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
  96. v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. static u16 vpx3220_fp_read(struct v4l2_subdev *sd, u16 fpaddr)
  102. {
  103. struct i2c_client *client = v4l2_get_subdevdata(sd);
  104. s16 data;
  105. /* Write the 16-bit address to the FPRD register */
  106. if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
  107. v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
  108. return -1;
  109. }
  110. if (vpx3220_fp_status(sd) < 0)
  111. return -1;
  112. /* Read the 16-bit data from the FPDAT register */
  113. data = i2c_smbus_read_word_data(client, 0x28);
  114. if (data == -1) {
  115. v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
  116. return -1;
  117. }
  118. return swab16(data);
  119. }
  120. static int vpx3220_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
  121. {
  122. u8 reg;
  123. int ret = -1;
  124. while (len >= 2) {
  125. reg = *data++;
  126. ret = vpx3220_write(sd, reg, *data++);
  127. if (ret < 0)
  128. break;
  129. len -= 2;
  130. }
  131. return ret;
  132. }
  133. static int vpx3220_write_fp_block(struct v4l2_subdev *sd,
  134. const u16 *data, unsigned int len)
  135. {
  136. u8 reg;
  137. int ret = 0;
  138. while (len > 1) {
  139. reg = *data++;
  140. ret |= vpx3220_fp_write(sd, reg, *data++);
  141. len -= 2;
  142. }
  143. return ret;
  144. }
  145. /* ---------------------------------------------------------------------- */
  146. static const unsigned short init_ntsc[] = {
  147. 0x1c, 0x00, /* NTSC tint angle */
  148. 0x88, 17, /* Window 1 vertical */
  149. 0x89, 240, /* Vertical lines in */
  150. 0x8a, 240, /* Vertical lines out */
  151. 0x8b, 000, /* Horizontal begin */
  152. 0x8c, 640, /* Horizontal length */
  153. 0x8d, 640, /* Number of pixels */
  154. 0x8f, 0xc00, /* Disable window 2 */
  155. 0xf0, 0x73, /* 13.5 MHz transport, Forced
  156. * mode, latch windows */
  157. 0xf2, 0x13, /* NTSC M, composite input */
  158. 0xe7, 0x1e1, /* Enable vertical standard
  159. * locking @ 240 lines */
  160. };
  161. static const unsigned short init_pal[] = {
  162. 0x88, 23, /* Window 1 vertical begin */
  163. 0x89, 288, /* Vertical lines in (16 lines
  164. * skipped by the VFE) */
  165. 0x8a, 288, /* Vertical lines out (16 lines
  166. * skipped by the VFE) */
  167. 0x8b, 16, /* Horizontal begin */
  168. 0x8c, 768, /* Horizontal length */
  169. 0x8d, 784, /* Number of pixels
  170. * Must be >= Horizontal begin + Horizontal length */
  171. 0x8f, 0xc00, /* Disable window 2 */
  172. 0xf0, 0x77, /* 13.5 MHz transport, Forced
  173. * mode, latch windows */
  174. 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
  175. 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
  176. };
  177. static const unsigned short init_secam[] = {
  178. 0x88, 23, /* Window 1 vertical begin */
  179. 0x89, 288, /* Vertical lines in (16 lines
  180. * skipped by the VFE) */
  181. 0x8a, 288, /* Vertical lines out (16 lines
  182. * skipped by the VFE) */
  183. 0x8b, 16, /* Horizontal begin */
  184. 0x8c, 768, /* Horizontal length */
  185. 0x8d, 784, /* Number of pixels
  186. * Must be >= Horizontal begin + Horizontal length */
  187. 0x8f, 0xc00, /* Disable window 2 */
  188. 0xf0, 0x77, /* 13.5 MHz transport, Forced
  189. * mode, latch windows */
  190. 0xf2, 0x3d5, /* SECAM, composite input */
  191. 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
  192. };
  193. static const unsigned char init_common[] = {
  194. 0xf2, 0x00, /* Disable all outputs */
  195. 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
  196. * (clamp off) */
  197. 0xd8, 0xa8, /* HREF/VREF active high, VREF
  198. * pulse = 2, Odd/Even flag */
  199. 0x20, 0x03, /* IF compensation 0dB/oct */
  200. 0xe0, 0xff, /* Open up all comparators */
  201. 0xe1, 0x00,
  202. 0xe2, 0x7f,
  203. 0xe3, 0x80,
  204. 0xe4, 0x7f,
  205. 0xe5, 0x80,
  206. 0xe6, 0x00, /* Brightness set to 0 */
  207. 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
  208. * 10 to 8 2-bit error diffusion */
  209. 0xe8, 0xf8, /* YUV422, CbCr binary offset,
  210. * ... (p.32) */
  211. 0xea, 0x18, /* LLC2 connected, output FIFO
  212. * reset with VACTintern */
  213. 0xf0, 0x8a, /* Half full level to 10, bus
  214. * shuffler [7:0, 23:16, 15:8] */
  215. 0xf1, 0x18, /* Single clock, sync mode, no
  216. * FE delay, no HLEN counter */
  217. 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
  218. * strength to 2 */
  219. 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
  220. * ALPHA strength to 4 */
  221. };
  222. static const unsigned short init_fp[] = {
  223. 0x59, 0,
  224. 0xa0, 2070, /* ACC reference */
  225. 0xa3, 0,
  226. 0xa4, 0,
  227. 0xa8, 30,
  228. 0xb2, 768,
  229. 0xbe, 27,
  230. 0x58, 0,
  231. 0x26, 0,
  232. 0x4b, 0x298, /* PLL gain */
  233. };
  234. static int vpx3220_init(struct v4l2_subdev *sd, u32 val)
  235. {
  236. struct vpx3220 *decoder = to_vpx3220(sd);
  237. vpx3220_write_block(sd, init_common, sizeof(init_common));
  238. vpx3220_write_fp_block(sd, init_fp, sizeof(init_fp) >> 1);
  239. if (decoder->norm & V4L2_STD_NTSC)
  240. vpx3220_write_fp_block(sd, init_ntsc, sizeof(init_ntsc) >> 1);
  241. else if (decoder->norm & V4L2_STD_PAL)
  242. vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
  243. else if (decoder->norm & V4L2_STD_SECAM)
  244. vpx3220_write_fp_block(sd, init_secam, sizeof(init_secam) >> 1);
  245. else
  246. vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
  247. return 0;
  248. }
  249. static int vpx3220_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
  250. {
  251. int res = V4L2_IN_ST_NO_SIGNAL, status;
  252. v4l2_std_id std = 0;
  253. status = vpx3220_fp_read(sd, 0x0f3);
  254. v4l2_dbg(1, debug, sd, "status: 0x%04x\n", status);
  255. if (status < 0)
  256. return status;
  257. if ((status & 0x20) == 0) {
  258. res = 0;
  259. switch (status & 0x18) {
  260. case 0x00:
  261. case 0x10:
  262. case 0x14:
  263. case 0x18:
  264. std = V4L2_STD_PAL;
  265. break;
  266. case 0x08:
  267. std = V4L2_STD_SECAM;
  268. break;
  269. case 0x04:
  270. case 0x0c:
  271. case 0x1c:
  272. std = V4L2_STD_NTSC;
  273. break;
  274. }
  275. }
  276. if (pstd)
  277. *pstd = std;
  278. if (pstatus)
  279. *pstatus = res;
  280. return 0;
  281. }
  282. static int vpx3220_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  283. {
  284. v4l2_dbg(1, debug, sd, "querystd\n");
  285. return vpx3220_status(sd, NULL, std);
  286. }
  287. static int vpx3220_g_input_status(struct v4l2_subdev *sd, u32 *status)
  288. {
  289. v4l2_dbg(1, debug, sd, "g_input_status\n");
  290. return vpx3220_status(sd, status, NULL);
  291. }
  292. static int vpx3220_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  293. {
  294. struct vpx3220 *decoder = to_vpx3220(sd);
  295. int temp_input;
  296. /* Here we back up the input selection because it gets
  297. overwritten when we fill the registers with the
  298. chosen video norm */
  299. temp_input = vpx3220_fp_read(sd, 0xf2);
  300. v4l2_dbg(1, debug, sd, "s_std %llx\n", (unsigned long long)std);
  301. if (std & V4L2_STD_NTSC) {
  302. vpx3220_write_fp_block(sd, init_ntsc, sizeof(init_ntsc) >> 1);
  303. v4l2_dbg(1, debug, sd, "norm switched to NTSC\n");
  304. } else if (std & V4L2_STD_PAL) {
  305. vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
  306. v4l2_dbg(1, debug, sd, "norm switched to PAL\n");
  307. } else if (std & V4L2_STD_SECAM) {
  308. vpx3220_write_fp_block(sd, init_secam, sizeof(init_secam) >> 1);
  309. v4l2_dbg(1, debug, sd, "norm switched to SECAM\n");
  310. } else {
  311. return -EINVAL;
  312. }
  313. decoder->norm = std;
  314. /* And here we set the backed up video input again */
  315. vpx3220_fp_write(sd, 0xf2, temp_input | 0x0010);
  316. udelay(10);
  317. return 0;
  318. }
  319. static int vpx3220_s_routing(struct v4l2_subdev *sd,
  320. u32 input, u32 output, u32 config)
  321. {
  322. int data;
  323. /* RJ: input = 0: ST8 (PCTV) input
  324. input = 1: COMPOSITE input
  325. input = 2: SVHS input */
  326. const int input_vals[3][2] = {
  327. {0x0c, 0},
  328. {0x0d, 0},
  329. {0x0e, 1}
  330. };
  331. if (input > 2)
  332. return -EINVAL;
  333. v4l2_dbg(1, debug, sd, "input switched to %s\n", inputs[input]);
  334. vpx3220_write(sd, 0x33, input_vals[input][0]);
  335. data = vpx3220_fp_read(sd, 0xf2) & ~(0x0020);
  336. if (data < 0)
  337. return data;
  338. /* 0x0010 is required to latch the setting */
  339. vpx3220_fp_write(sd, 0xf2,
  340. data | (input_vals[input][1] << 5) | 0x0010);
  341. udelay(10);
  342. return 0;
  343. }
  344. static int vpx3220_s_stream(struct v4l2_subdev *sd, int enable)
  345. {
  346. v4l2_dbg(1, debug, sd, "s_stream %s\n", enable ? "on" : "off");
  347. vpx3220_write(sd, 0xf2, (enable ? 0x1b : 0x00));
  348. return 0;
  349. }
  350. static int vpx3220_s_ctrl(struct v4l2_ctrl *ctrl)
  351. {
  352. struct v4l2_subdev *sd = to_sd(ctrl);
  353. switch (ctrl->id) {
  354. case V4L2_CID_BRIGHTNESS:
  355. vpx3220_write(sd, 0xe6, ctrl->val);
  356. return 0;
  357. case V4L2_CID_CONTRAST:
  358. /* Bit 7 and 8 is for noise shaping */
  359. vpx3220_write(sd, 0xe7, ctrl->val + 192);
  360. return 0;
  361. case V4L2_CID_SATURATION:
  362. vpx3220_fp_write(sd, 0xa0, ctrl->val);
  363. return 0;
  364. case V4L2_CID_HUE:
  365. vpx3220_fp_write(sd, 0x1c, ctrl->val);
  366. return 0;
  367. }
  368. return -EINVAL;
  369. }
  370. static int vpx3220_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  371. {
  372. struct vpx3220 *decoder = to_vpx3220(sd);
  373. struct i2c_client *client = v4l2_get_subdevdata(sd);
  374. return v4l2_chip_ident_i2c_client(client, chip, decoder->ident, 0);
  375. }
  376. /* ----------------------------------------------------------------------- */
  377. static const struct v4l2_ctrl_ops vpx3220_ctrl_ops = {
  378. .s_ctrl = vpx3220_s_ctrl,
  379. };
  380. static const struct v4l2_subdev_core_ops vpx3220_core_ops = {
  381. .g_chip_ident = vpx3220_g_chip_ident,
  382. .init = vpx3220_init,
  383. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  384. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  385. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  386. .g_ctrl = v4l2_subdev_g_ctrl,
  387. .s_ctrl = v4l2_subdev_s_ctrl,
  388. .queryctrl = v4l2_subdev_queryctrl,
  389. .querymenu = v4l2_subdev_querymenu,
  390. .s_std = vpx3220_s_std,
  391. };
  392. static const struct v4l2_subdev_video_ops vpx3220_video_ops = {
  393. .s_routing = vpx3220_s_routing,
  394. .s_stream = vpx3220_s_stream,
  395. .querystd = vpx3220_querystd,
  396. .g_input_status = vpx3220_g_input_status,
  397. };
  398. static const struct v4l2_subdev_ops vpx3220_ops = {
  399. .core = &vpx3220_core_ops,
  400. .video = &vpx3220_video_ops,
  401. };
  402. /* -----------------------------------------------------------------------
  403. * Client management code
  404. */
  405. static int vpx3220_probe(struct i2c_client *client,
  406. const struct i2c_device_id *id)
  407. {
  408. struct vpx3220 *decoder;
  409. struct v4l2_subdev *sd;
  410. const char *name = NULL;
  411. u8 ver;
  412. u16 pn;
  413. /* Check if the adapter supports the needed features */
  414. if (!i2c_check_functionality(client->adapter,
  415. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  416. return -ENODEV;
  417. decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
  418. if (decoder == NULL)
  419. return -ENOMEM;
  420. sd = &decoder->sd;
  421. v4l2_i2c_subdev_init(sd, client, &vpx3220_ops);
  422. decoder->norm = V4L2_STD_PAL;
  423. decoder->input = 0;
  424. decoder->enable = 1;
  425. v4l2_ctrl_handler_init(&decoder->hdl, 4);
  426. v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
  427. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  428. v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
  429. V4L2_CID_CONTRAST, 0, 63, 1, 32);
  430. v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
  431. V4L2_CID_SATURATION, 0, 4095, 1, 2048);
  432. v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
  433. V4L2_CID_HUE, -512, 511, 1, 0);
  434. sd->ctrl_handler = &decoder->hdl;
  435. if (decoder->hdl.error) {
  436. int err = decoder->hdl.error;
  437. v4l2_ctrl_handler_free(&decoder->hdl);
  438. kfree(decoder);
  439. return err;
  440. }
  441. v4l2_ctrl_handler_setup(&decoder->hdl);
  442. ver = i2c_smbus_read_byte_data(client, 0x00);
  443. pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
  444. i2c_smbus_read_byte_data(client, 0x01);
  445. decoder->ident = V4L2_IDENT_VPX3220A;
  446. if (ver == 0xec) {
  447. switch (pn) {
  448. case 0x4680:
  449. name = "vpx3220a";
  450. break;
  451. case 0x4260:
  452. name = "vpx3216b";
  453. decoder->ident = V4L2_IDENT_VPX3216B;
  454. break;
  455. case 0x4280:
  456. name = "vpx3214c";
  457. decoder->ident = V4L2_IDENT_VPX3214C;
  458. break;
  459. }
  460. }
  461. if (name)
  462. v4l2_info(sd, "%s found @ 0x%x (%s)\n", name,
  463. client->addr << 1, client->adapter->name);
  464. else
  465. v4l2_info(sd, "chip (%02x:%04x) found @ 0x%x (%s)\n",
  466. ver, pn, client->addr << 1, client->adapter->name);
  467. vpx3220_write_block(sd, init_common, sizeof(init_common));
  468. vpx3220_write_fp_block(sd, init_fp, sizeof(init_fp) >> 1);
  469. /* Default to PAL */
  470. vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
  471. return 0;
  472. }
  473. static int vpx3220_remove(struct i2c_client *client)
  474. {
  475. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  476. struct vpx3220 *decoder = to_vpx3220(sd);
  477. v4l2_device_unregister_subdev(sd);
  478. v4l2_ctrl_handler_free(&decoder->hdl);
  479. kfree(decoder);
  480. return 0;
  481. }
  482. static const struct i2c_device_id vpx3220_id[] = {
  483. { "vpx3220a", 0 },
  484. { "vpx3216b", 0 },
  485. { "vpx3214c", 0 },
  486. { }
  487. };
  488. MODULE_DEVICE_TABLE(i2c, vpx3220_id);
  489. static struct i2c_driver vpx3220_driver = {
  490. .driver = {
  491. .owner = THIS_MODULE,
  492. .name = "vpx3220",
  493. },
  494. .probe = vpx3220_probe,
  495. .remove = vpx3220_remove,
  496. .id_table = vpx3220_id,
  497. };
  498. module_i2c_driver(vpx3220_driver);