vpx3220.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 <asm/uaccess.h>
  25. #include <linux/i2c.h>
  26. #include <media/v4l2-common.h>
  27. #include <media/v4l2-i2c-drv-legacy.h>
  28. #include <linux/videodev.h>
  29. #include <linux/videodev2.h>
  30. #include <linux/video_decoder.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. unsigned char reg[255];
  41. v4l2_std_id norm;
  42. int input;
  43. int enable;
  44. int bright;
  45. int contrast;
  46. int hue;
  47. int sat;
  48. };
  49. static char *inputs[] = { "internal", "composite", "svideo" };
  50. /* ----------------------------------------------------------------------- */
  51. static inline int vpx3220_write(struct i2c_client *client, u8 reg, u8 value)
  52. {
  53. struct vpx3220 *decoder = i2c_get_clientdata(client);
  54. decoder->reg[reg] = value;
  55. return i2c_smbus_write_byte_data(client, reg, value);
  56. }
  57. static inline int vpx3220_read(struct i2c_client *client, u8 reg)
  58. {
  59. return i2c_smbus_read_byte_data(client, reg);
  60. }
  61. static int vpx3220_fp_status(struct i2c_client *client)
  62. {
  63. unsigned char status;
  64. unsigned int i;
  65. for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
  66. status = vpx3220_read(client, 0x29);
  67. if (!(status & 4))
  68. return 0;
  69. udelay(10);
  70. if (need_resched())
  71. cond_resched();
  72. }
  73. return -1;
  74. }
  75. static int vpx3220_fp_write(struct i2c_client *client, u8 fpaddr, u16 data)
  76. {
  77. /* Write the 16-bit address to the FPWR register */
  78. if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
  79. v4l_dbg(1, debug, client, "%s: failed\n", __func__);
  80. return -1;
  81. }
  82. if (vpx3220_fp_status(client) < 0)
  83. return -1;
  84. /* Write the 16-bit data to the FPDAT register */
  85. if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
  86. v4l_dbg(1, debug, client, "%s: failed\n", __func__);
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. static u16 vpx3220_fp_read(struct i2c_client *client, u16 fpaddr)
  92. {
  93. s16 data;
  94. /* Write the 16-bit address to the FPRD register */
  95. if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
  96. v4l_dbg(1, debug, client, "%s: failed\n", __func__);
  97. return -1;
  98. }
  99. if (vpx3220_fp_status(client) < 0)
  100. return -1;
  101. /* Read the 16-bit data from the FPDAT register */
  102. data = i2c_smbus_read_word_data(client, 0x28);
  103. if (data == -1) {
  104. v4l_dbg(1, debug, client, "%s: failed\n", __func__);
  105. return -1;
  106. }
  107. return swab16(data);
  108. }
  109. static int vpx3220_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
  110. {
  111. u8 reg;
  112. int ret = -1;
  113. while (len >= 2) {
  114. reg = *data++;
  115. ret = vpx3220_write(client, reg, *data++);
  116. if (ret < 0)
  117. break;
  118. len -= 2;
  119. }
  120. return ret;
  121. }
  122. static int vpx3220_write_fp_block(struct i2c_client *client,
  123. const u16 *data, unsigned int len)
  124. {
  125. u8 reg;
  126. int ret = 0;
  127. while (len > 1) {
  128. reg = *data++;
  129. ret |= vpx3220_fp_write(client, reg, *data++);
  130. len -= 2;
  131. }
  132. return ret;
  133. }
  134. /* ---------------------------------------------------------------------- */
  135. static const unsigned short init_ntsc[] = {
  136. 0x1c, 0x00, /* NTSC tint angle */
  137. 0x88, 17, /* Window 1 vertical */
  138. 0x89, 240, /* Vertical lines in */
  139. 0x8a, 240, /* Vertical lines out */
  140. 0x8b, 000, /* Horizontal begin */
  141. 0x8c, 640, /* Horizontal length */
  142. 0x8d, 640, /* Number of pixels */
  143. 0x8f, 0xc00, /* Disable window 2 */
  144. 0xf0, 0x73, /* 13.5 MHz transport, Forced
  145. * mode, latch windows */
  146. 0xf2, 0x13, /* NTSC M, composite input */
  147. 0xe7, 0x1e1, /* Enable vertical standard
  148. * locking @ 240 lines */
  149. };
  150. static const unsigned short init_pal[] = {
  151. 0x88, 23, /* Window 1 vertical begin */
  152. 0x89, 288, /* Vertical lines in (16 lines
  153. * skipped by the VFE) */
  154. 0x8a, 288, /* Vertical lines out (16 lines
  155. * skipped by the VFE) */
  156. 0x8b, 16, /* Horizontal begin */
  157. 0x8c, 768, /* Horizontal length */
  158. 0x8d, 784, /* Number of pixels
  159. * Must be >= Horizontal begin + Horizontal length */
  160. 0x8f, 0xc00, /* Disable window 2 */
  161. 0xf0, 0x77, /* 13.5 MHz transport, Forced
  162. * mode, latch windows */
  163. 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
  164. 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
  165. };
  166. static const unsigned short init_secam[] = {
  167. 0x88, 23, /* Window 1 vertical begin */
  168. 0x89, 288, /* Vertical lines in (16 lines
  169. * skipped by the VFE) */
  170. 0x8a, 288, /* Vertical lines out (16 lines
  171. * skipped by the VFE) */
  172. 0x8b, 16, /* Horizontal begin */
  173. 0x8c, 768, /* Horizontal length */
  174. 0x8d, 784, /* Number of pixels
  175. * Must be >= Horizontal begin + Horizontal length */
  176. 0x8f, 0xc00, /* Disable window 2 */
  177. 0xf0, 0x77, /* 13.5 MHz transport, Forced
  178. * mode, latch windows */
  179. 0xf2, 0x3d5, /* SECAM, composite input */
  180. 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
  181. };
  182. static const unsigned char init_common[] = {
  183. 0xf2, 0x00, /* Disable all outputs */
  184. 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
  185. * (clamp off) */
  186. 0xd8, 0xa8, /* HREF/VREF active high, VREF
  187. * pulse = 2, Odd/Even flag */
  188. 0x20, 0x03, /* IF compensation 0dB/oct */
  189. 0xe0, 0xff, /* Open up all comparators */
  190. 0xe1, 0x00,
  191. 0xe2, 0x7f,
  192. 0xe3, 0x80,
  193. 0xe4, 0x7f,
  194. 0xe5, 0x80,
  195. 0xe6, 0x00, /* Brightness set to 0 */
  196. 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
  197. * 10 to 8 2-bit error diffusion */
  198. 0xe8, 0xf8, /* YUV422, CbCr binary offset,
  199. * ... (p.32) */
  200. 0xea, 0x18, /* LLC2 connected, output FIFO
  201. * reset with VACTintern */
  202. 0xf0, 0x8a, /* Half full level to 10, bus
  203. * shuffler [7:0, 23:16, 15:8] */
  204. 0xf1, 0x18, /* Single clock, sync mode, no
  205. * FE delay, no HLEN counter */
  206. 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
  207. * strength to 2 */
  208. 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
  209. * ALPHA strength to 4 */
  210. };
  211. static const unsigned short init_fp[] = {
  212. 0x59, 0,
  213. 0xa0, 2070, /* ACC reference */
  214. 0xa3, 0,
  215. 0xa4, 0,
  216. 0xa8, 30,
  217. 0xb2, 768,
  218. 0xbe, 27,
  219. 0x58, 0,
  220. 0x26, 0,
  221. 0x4b, 0x298, /* PLL gain */
  222. };
  223. static int vpx3220_command(struct i2c_client *client, unsigned cmd, void *arg)
  224. {
  225. struct vpx3220 *decoder = i2c_get_clientdata(client);
  226. switch (cmd) {
  227. case VIDIOC_INT_INIT:
  228. {
  229. vpx3220_write_block(client, init_common,
  230. sizeof(init_common));
  231. vpx3220_write_fp_block(client, init_fp,
  232. sizeof(init_fp) >> 1);
  233. if (decoder->norm & V4L2_STD_NTSC) {
  234. vpx3220_write_fp_block(client, init_ntsc,
  235. sizeof(init_ntsc) >> 1);
  236. } else if (decoder->norm & V4L2_STD_PAL) {
  237. vpx3220_write_fp_block(client, init_pal,
  238. sizeof(init_pal) >> 1);
  239. } else if (decoder->norm & V4L2_STD_SECAM) {
  240. vpx3220_write_fp_block(client, init_secam,
  241. sizeof(init_secam) >> 1);
  242. } else {
  243. vpx3220_write_fp_block(client, init_pal,
  244. sizeof(init_pal) >> 1);
  245. }
  246. break;
  247. }
  248. case VIDIOC_QUERYSTD:
  249. case VIDIOC_INT_G_INPUT_STATUS:
  250. {
  251. int res = V4L2_IN_ST_NO_SIGNAL, status;
  252. v4l2_std_id std = 0;
  253. v4l_dbg(1, debug, client, "VIDIOC_QUERYSTD/VIDIOC_INT_G_INPUT_STATUS\n");
  254. status = vpx3220_fp_read(client, 0x0f3);
  255. v4l_dbg(1, debug, client, "status: 0x%04x\n", status);
  256. if (status < 0)
  257. return status;
  258. if ((status & 0x20) == 0) {
  259. res = 0;
  260. switch (status & 0x18) {
  261. case 0x00:
  262. case 0x10:
  263. case 0x14:
  264. case 0x18:
  265. std = V4L2_STD_PAL;
  266. break;
  267. case 0x08:
  268. std = V4L2_STD_SECAM;
  269. break;
  270. case 0x04:
  271. case 0x0c:
  272. case 0x1c:
  273. std = V4L2_STD_NTSC;
  274. break;
  275. }
  276. }
  277. if (cmd == VIDIOC_QUERYSTD)
  278. *(v4l2_std_id *)arg = std;
  279. else
  280. *(int *)arg = res;
  281. break;
  282. }
  283. case VIDIOC_S_STD:
  284. {
  285. v4l2_std_id *iarg = arg;
  286. int temp_input;
  287. /* Here we back up the input selection because it gets
  288. overwritten when we fill the registers with the
  289. choosen video norm */
  290. temp_input = vpx3220_fp_read(client, 0xf2);
  291. v4l_dbg(1, debug, client, "VIDIOC_S_STD %llx\n", *iarg);
  292. if (*iarg & V4L2_STD_NTSC) {
  293. vpx3220_write_fp_block(client, init_ntsc,
  294. sizeof(init_ntsc) >> 1);
  295. v4l_dbg(1, debug, client, "norm switched to NTSC\n");
  296. } else if (*iarg & V4L2_STD_PAL) {
  297. vpx3220_write_fp_block(client, init_pal,
  298. sizeof(init_pal) >> 1);
  299. v4l_dbg(1, debug, client, "norm switched to PAL\n");
  300. } else if (*iarg & V4L2_STD_SECAM) {
  301. vpx3220_write_fp_block(client, init_secam,
  302. sizeof(init_secam) >> 1);
  303. v4l_dbg(1, debug, client, "norm switched to SECAM\n");
  304. } else {
  305. return -EINVAL;
  306. }
  307. decoder->norm = *iarg;
  308. /* And here we set the backed up video input again */
  309. vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
  310. udelay(10);
  311. break;
  312. }
  313. case VIDIOC_INT_S_VIDEO_ROUTING:
  314. {
  315. struct v4l2_routing *route = arg;
  316. int data;
  317. /* RJ: *iarg = 0: ST8 (PCTV) input
  318. *iarg = 1: COMPOSITE input
  319. *iarg = 2: SVHS input */
  320. const int input[3][2] = {
  321. {0x0c, 0},
  322. {0x0d, 0},
  323. {0x0e, 1}
  324. };
  325. if (route->input < 0 || route->input > 2)
  326. return -EINVAL;
  327. v4l_dbg(1, debug, client, "input switched to %s\n", inputs[route->input]);
  328. vpx3220_write(client, 0x33, input[route->input][0]);
  329. data = vpx3220_fp_read(client, 0xf2) & ~(0x0020);
  330. if (data < 0)
  331. return data;
  332. /* 0x0010 is required to latch the setting */
  333. vpx3220_fp_write(client, 0xf2,
  334. data | (input[route->input][1] << 5) | 0x0010);
  335. udelay(10);
  336. break;
  337. }
  338. case VIDIOC_STREAMON:
  339. case VIDIOC_STREAMOFF:
  340. {
  341. int on = cmd == VIDIOC_STREAMON;
  342. v4l_dbg(1, debug, client, "VIDIOC_STREAM%s\n", on ? "ON" : "OFF");
  343. vpx3220_write(client, 0xf2, (on ? 0x1b : 0x00));
  344. break;
  345. }
  346. case VIDIOC_QUERYCTRL:
  347. {
  348. struct v4l2_queryctrl *qc = arg;
  349. switch (qc->id) {
  350. case V4L2_CID_BRIGHTNESS:
  351. v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
  352. break;
  353. case V4L2_CID_CONTRAST:
  354. v4l2_ctrl_query_fill(qc, 0, 63, 1, 32);
  355. break;
  356. case V4L2_CID_SATURATION:
  357. v4l2_ctrl_query_fill(qc, 0, 4095, 1, 2048);
  358. break;
  359. case V4L2_CID_HUE:
  360. v4l2_ctrl_query_fill(qc, -512, 511, 1, 0);
  361. break;
  362. default:
  363. return -EINVAL;
  364. }
  365. break;
  366. }
  367. case VIDIOC_G_CTRL:
  368. {
  369. struct v4l2_control *ctrl = arg;
  370. switch (ctrl->id) {
  371. case V4L2_CID_BRIGHTNESS:
  372. ctrl->value = decoder->bright;
  373. break;
  374. case V4L2_CID_CONTRAST:
  375. ctrl->value = decoder->contrast;
  376. break;
  377. case V4L2_CID_SATURATION:
  378. ctrl->value = decoder->sat;
  379. break;
  380. case V4L2_CID_HUE:
  381. ctrl->value = decoder->hue;
  382. break;
  383. default:
  384. return -EINVAL;
  385. }
  386. break;
  387. }
  388. case VIDIOC_S_CTRL:
  389. {
  390. struct v4l2_control *ctrl = arg;
  391. switch (ctrl->id) {
  392. case V4L2_CID_BRIGHTNESS:
  393. if (decoder->bright != ctrl->value) {
  394. decoder->bright = ctrl->value;
  395. vpx3220_write(client, 0xe6, decoder->bright);
  396. }
  397. break;
  398. case V4L2_CID_CONTRAST:
  399. if (decoder->contrast != ctrl->value) {
  400. /* Bit 7 and 8 is for noise shaping */
  401. decoder->contrast = ctrl->value;
  402. vpx3220_write(client, 0xe7,
  403. decoder->contrast + 192);
  404. }
  405. break;
  406. case V4L2_CID_SATURATION:
  407. if (decoder->sat != ctrl->value) {
  408. decoder->sat = ctrl->value;
  409. vpx3220_fp_write(client, 0xa0, decoder->sat);
  410. }
  411. break;
  412. case V4L2_CID_HUE:
  413. if (decoder->hue != ctrl->value) {
  414. decoder->hue = ctrl->value;
  415. vpx3220_fp_write(client, 0x1c, decoder->hue);
  416. }
  417. break;
  418. default:
  419. return -EINVAL;
  420. }
  421. break;
  422. }
  423. default:
  424. return -EINVAL;
  425. }
  426. return 0;
  427. }
  428. static int vpx3220_init_client(struct i2c_client *client)
  429. {
  430. vpx3220_write_block(client, init_common, sizeof(init_common));
  431. vpx3220_write_fp_block(client, init_fp, sizeof(init_fp) >> 1);
  432. /* Default to PAL */
  433. vpx3220_write_fp_block(client, init_pal, sizeof(init_pal) >> 1);
  434. return 0;
  435. }
  436. /* -----------------------------------------------------------------------
  437. * Client management code
  438. */
  439. static unsigned short normal_i2c[] = { 0x86 >> 1, 0x8e >> 1, I2C_CLIENT_END };
  440. I2C_CLIENT_INSMOD;
  441. static int vpx3220_probe(struct i2c_client *client,
  442. const struct i2c_device_id *id)
  443. {
  444. struct vpx3220 *decoder;
  445. const char *name = NULL;
  446. u8 ver;
  447. u16 pn;
  448. /* Check if the adapter supports the needed features */
  449. if (!i2c_check_functionality(client->adapter,
  450. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  451. return -ENODEV;
  452. decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
  453. if (decoder == NULL)
  454. return -ENOMEM;
  455. decoder->norm = V4L2_STD_PAL;
  456. decoder->input = 0;
  457. decoder->enable = 1;
  458. decoder->bright = 32768;
  459. decoder->contrast = 32768;
  460. decoder->hue = 32768;
  461. decoder->sat = 32768;
  462. i2c_set_clientdata(client, decoder);
  463. ver = i2c_smbus_read_byte_data(client, 0x00);
  464. pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
  465. i2c_smbus_read_byte_data(client, 0x01);
  466. if (ver == 0xec) {
  467. switch (pn) {
  468. case 0x4680:
  469. name = "vpx3220a";
  470. break;
  471. case 0x4260:
  472. name = "vpx3216b";
  473. break;
  474. case 0x4280:
  475. name = "vpx3214c";
  476. break;
  477. }
  478. }
  479. if (name)
  480. v4l_info(client, "%s found @ 0x%x (%s)\n", name,
  481. client->addr << 1, client->adapter->name);
  482. else
  483. v4l_info(client, "chip (%02x:%04x) found @ 0x%x (%s)\n",
  484. ver, pn, client->addr << 1, client->adapter->name);
  485. vpx3220_init_client(client);
  486. return 0;
  487. }
  488. static int vpx3220_remove(struct i2c_client *client)
  489. {
  490. kfree(i2c_get_clientdata(client));
  491. return 0;
  492. }
  493. static const struct i2c_device_id vpx3220_id[] = {
  494. { "vpx3220a", 0 },
  495. { "vpx3216b", 0 },
  496. { "vpx3214c", 0 },
  497. { }
  498. };
  499. MODULE_DEVICE_TABLE(i2c, vpx3220_id);
  500. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  501. .name = "vpx3220",
  502. .driverid = I2C_DRIVERID_VPX3220,
  503. .command = vpx3220_command,
  504. .probe = vpx3220_probe,
  505. .remove = vpx3220_remove,
  506. .id_table = vpx3220_id,
  507. };