vpx3220.c 14 KB

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