vpx3220.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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-ctrls.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. struct v4l2_subdev sd;
  40. struct v4l2_ctrl_handler hdl;
  41. unsigned char reg[255];
  42. v4l2_std_id norm;
  43. int input;
  44. int enable;
  45. };
  46. static inline struct vpx3220 *to_vpx3220(struct v4l2_subdev *sd)
  47. {
  48. return container_of(sd, struct vpx3220, sd);
  49. }
  50. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  51. {
  52. return &container_of(ctrl->handler, struct vpx3220, hdl)->sd;
  53. }
  54. static char *inputs[] = { "internal", "composite", "svideo" };
  55. /* ----------------------------------------------------------------------- */
  56. static inline int vpx3220_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  57. {
  58. struct i2c_client *client = v4l2_get_subdevdata(sd);
  59. struct vpx3220 *decoder = i2c_get_clientdata(client);
  60. decoder->reg[reg] = value;
  61. return i2c_smbus_write_byte_data(client, reg, value);
  62. }
  63. static inline int vpx3220_read(struct v4l2_subdev *sd, u8 reg)
  64. {
  65. struct i2c_client *client = v4l2_get_subdevdata(sd);
  66. return i2c_smbus_read_byte_data(client, reg);
  67. }
  68. static int vpx3220_fp_status(struct v4l2_subdev *sd)
  69. {
  70. unsigned char status;
  71. unsigned int i;
  72. for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
  73. status = vpx3220_read(sd, 0x29);
  74. if (!(status & 4))
  75. return 0;
  76. udelay(10);
  77. if (need_resched())
  78. cond_resched();
  79. }
  80. return -1;
  81. }
  82. static int vpx3220_fp_write(struct v4l2_subdev *sd, u8 fpaddr, u16 data)
  83. {
  84. struct i2c_client *client = v4l2_get_subdevdata(sd);
  85. /* Write the 16-bit address to the FPWR register */
  86. if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
  87. v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
  88. return -1;
  89. }
  90. if (vpx3220_fp_status(sd) < 0)
  91. return -1;
  92. /* Write the 16-bit data to the FPDAT register */
  93. if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
  94. v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. static u16 vpx3220_fp_read(struct v4l2_subdev *sd, u16 fpaddr)
  100. {
  101. struct i2c_client *client = v4l2_get_subdevdata(sd);
  102. s16 data;
  103. /* Write the 16-bit address to the FPRD register */
  104. if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
  105. v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
  106. return -1;
  107. }
  108. if (vpx3220_fp_status(sd) < 0)
  109. return -1;
  110. /* Read the 16-bit data from the FPDAT register */
  111. data = i2c_smbus_read_word_data(client, 0x28);
  112. if (data == -1) {
  113. v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
  114. return -1;
  115. }
  116. return swab16(data);
  117. }
  118. static int vpx3220_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
  119. {
  120. u8 reg;
  121. int ret = -1;
  122. while (len >= 2) {
  123. reg = *data++;
  124. ret = vpx3220_write(sd, reg, *data++);
  125. if (ret < 0)
  126. break;
  127. len -= 2;
  128. }
  129. return ret;
  130. }
  131. static int vpx3220_write_fp_block(struct v4l2_subdev *sd,
  132. const u16 *data, unsigned int len)
  133. {
  134. u8 reg;
  135. int ret = 0;
  136. while (len > 1) {
  137. reg = *data++;
  138. ret |= vpx3220_fp_write(sd, reg, *data++);
  139. len -= 2;
  140. }
  141. return ret;
  142. }
  143. /* ---------------------------------------------------------------------- */
  144. static const unsigned short init_ntsc[] = {
  145. 0x1c, 0x00, /* NTSC tint angle */
  146. 0x88, 17, /* Window 1 vertical */
  147. 0x89, 240, /* Vertical lines in */
  148. 0x8a, 240, /* Vertical lines out */
  149. 0x8b, 000, /* Horizontal begin */
  150. 0x8c, 640, /* Horizontal length */
  151. 0x8d, 640, /* Number of pixels */
  152. 0x8f, 0xc00, /* Disable window 2 */
  153. 0xf0, 0x73, /* 13.5 MHz transport, Forced
  154. * mode, latch windows */
  155. 0xf2, 0x13, /* NTSC M, composite input */
  156. 0xe7, 0x1e1, /* Enable vertical standard
  157. * locking @ 240 lines */
  158. };
  159. static const unsigned short init_pal[] = {
  160. 0x88, 23, /* Window 1 vertical begin */
  161. 0x89, 288, /* Vertical lines in (16 lines
  162. * skipped by the VFE) */
  163. 0x8a, 288, /* Vertical lines out (16 lines
  164. * skipped by the VFE) */
  165. 0x8b, 16, /* Horizontal begin */
  166. 0x8c, 768, /* Horizontal length */
  167. 0x8d, 784, /* Number of pixels
  168. * Must be >= Horizontal begin + Horizontal length */
  169. 0x8f, 0xc00, /* Disable window 2 */
  170. 0xf0, 0x77, /* 13.5 MHz transport, Forced
  171. * mode, latch windows */
  172. 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
  173. 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
  174. };
  175. static const unsigned short init_secam[] = {
  176. 0x88, 23, /* Window 1 vertical begin */
  177. 0x89, 288, /* Vertical lines in (16 lines
  178. * skipped by the VFE) */
  179. 0x8a, 288, /* Vertical lines out (16 lines
  180. * skipped by the VFE) */
  181. 0x8b, 16, /* Horizontal begin */
  182. 0x8c, 768, /* Horizontal length */
  183. 0x8d, 784, /* Number of pixels
  184. * Must be >= Horizontal begin + Horizontal length */
  185. 0x8f, 0xc00, /* Disable window 2 */
  186. 0xf0, 0x77, /* 13.5 MHz transport, Forced
  187. * mode, latch windows */
  188. 0xf2, 0x3d5, /* SECAM, composite input */
  189. 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
  190. };
  191. static const unsigned char init_common[] = {
  192. 0xf2, 0x00, /* Disable all outputs */
  193. 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
  194. * (clamp off) */
  195. 0xd8, 0xa8, /* HREF/VREF active high, VREF
  196. * pulse = 2, Odd/Even flag */
  197. 0x20, 0x03, /* IF compensation 0dB/oct */
  198. 0xe0, 0xff, /* Open up all comparators */
  199. 0xe1, 0x00,
  200. 0xe2, 0x7f,
  201. 0xe3, 0x80,
  202. 0xe4, 0x7f,
  203. 0xe5, 0x80,
  204. 0xe6, 0x00, /* Brightness set to 0 */
  205. 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
  206. * 10 to 8 2-bit error diffusion */
  207. 0xe8, 0xf8, /* YUV422, CbCr binary offset,
  208. * ... (p.32) */
  209. 0xea, 0x18, /* LLC2 connected, output FIFO
  210. * reset with VACTintern */
  211. 0xf0, 0x8a, /* Half full level to 10, bus
  212. * shuffler [7:0, 23:16, 15:8] */
  213. 0xf1, 0x18, /* Single clock, sync mode, no
  214. * FE delay, no HLEN counter */
  215. 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
  216. * strength to 2 */
  217. 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
  218. * ALPHA strength to 4 */
  219. };
  220. static const unsigned short init_fp[] = {
  221. 0x59, 0,
  222. 0xa0, 2070, /* ACC reference */
  223. 0xa3, 0,
  224. 0xa4, 0,
  225. 0xa8, 30,
  226. 0xb2, 768,
  227. 0xbe, 27,
  228. 0x58, 0,
  229. 0x26, 0,
  230. 0x4b, 0x298, /* PLL gain */
  231. };
  232. static int vpx3220_init(struct v4l2_subdev *sd, u32 val)
  233. {
  234. struct vpx3220 *decoder = to_vpx3220(sd);
  235. vpx3220_write_block(sd, init_common, sizeof(init_common));
  236. vpx3220_write_fp_block(sd, init_fp, sizeof(init_fp) >> 1);
  237. if (decoder->norm & V4L2_STD_NTSC)
  238. vpx3220_write_fp_block(sd, init_ntsc, sizeof(init_ntsc) >> 1);
  239. else if (decoder->norm & V4L2_STD_PAL)
  240. vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
  241. else if (decoder->norm & V4L2_STD_SECAM)
  242. vpx3220_write_fp_block(sd, init_secam, sizeof(init_secam) >> 1);
  243. else
  244. vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
  245. return 0;
  246. }
  247. static int vpx3220_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
  248. {
  249. int res = V4L2_IN_ST_NO_SIGNAL, status;
  250. v4l2_std_id std = pstd ? *pstd : V4L2_STD_ALL;
  251. status = vpx3220_fp_read(sd, 0x0f3);
  252. v4l2_dbg(1, debug, sd, "status: 0x%04x\n", status);
  253. if (status < 0)
  254. return status;
  255. if ((status & 0x20) == 0) {
  256. res = 0;
  257. switch (status & 0x18) {
  258. case 0x00:
  259. case 0x10:
  260. case 0x14:
  261. case 0x18:
  262. std &= V4L2_STD_PAL;
  263. break;
  264. case 0x08:
  265. std &= V4L2_STD_SECAM;
  266. break;
  267. case 0x04:
  268. case 0x0c:
  269. case 0x1c:
  270. std &= V4L2_STD_NTSC;
  271. break;
  272. }
  273. } else {
  274. std = V4L2_STD_UNKNOWN;
  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. /* ----------------------------------------------------------------------- */
  371. static const struct v4l2_ctrl_ops vpx3220_ctrl_ops = {
  372. .s_ctrl = vpx3220_s_ctrl,
  373. };
  374. static const struct v4l2_subdev_core_ops vpx3220_core_ops = {
  375. .init = vpx3220_init,
  376. };
  377. static const struct v4l2_subdev_video_ops vpx3220_video_ops = {
  378. .s_std = vpx3220_s_std,
  379. .s_routing = vpx3220_s_routing,
  380. .s_stream = vpx3220_s_stream,
  381. .querystd = vpx3220_querystd,
  382. .g_input_status = vpx3220_g_input_status,
  383. };
  384. static const struct v4l2_subdev_ops vpx3220_ops = {
  385. .core = &vpx3220_core_ops,
  386. .video = &vpx3220_video_ops,
  387. };
  388. /* -----------------------------------------------------------------------
  389. * Client management code
  390. */
  391. static int vpx3220_probe(struct i2c_client *client,
  392. const struct i2c_device_id *id)
  393. {
  394. struct vpx3220 *decoder;
  395. struct v4l2_subdev *sd;
  396. const char *name = NULL;
  397. u8 ver;
  398. u16 pn;
  399. /* Check if the adapter supports the needed features */
  400. if (!i2c_check_functionality(client->adapter,
  401. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  402. return -ENODEV;
  403. decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
  404. if (decoder == NULL)
  405. return -ENOMEM;
  406. sd = &decoder->sd;
  407. v4l2_i2c_subdev_init(sd, client, &vpx3220_ops);
  408. decoder->norm = V4L2_STD_PAL;
  409. decoder->input = 0;
  410. decoder->enable = 1;
  411. v4l2_ctrl_handler_init(&decoder->hdl, 4);
  412. v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
  413. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  414. v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
  415. V4L2_CID_CONTRAST, 0, 63, 1, 32);
  416. v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
  417. V4L2_CID_SATURATION, 0, 4095, 1, 2048);
  418. v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
  419. V4L2_CID_HUE, -512, 511, 1, 0);
  420. sd->ctrl_handler = &decoder->hdl;
  421. if (decoder->hdl.error) {
  422. int err = decoder->hdl.error;
  423. v4l2_ctrl_handler_free(&decoder->hdl);
  424. return err;
  425. }
  426. v4l2_ctrl_handler_setup(&decoder->hdl);
  427. ver = i2c_smbus_read_byte_data(client, 0x00);
  428. pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
  429. i2c_smbus_read_byte_data(client, 0x01);
  430. if (ver == 0xec) {
  431. switch (pn) {
  432. case 0x4680:
  433. name = "vpx3220a";
  434. break;
  435. case 0x4260:
  436. name = "vpx3216b";
  437. break;
  438. case 0x4280:
  439. name = "vpx3214c";
  440. break;
  441. }
  442. }
  443. if (name)
  444. v4l2_info(sd, "%s found @ 0x%x (%s)\n", name,
  445. client->addr << 1, client->adapter->name);
  446. else
  447. v4l2_info(sd, "chip (%02x:%04x) found @ 0x%x (%s)\n",
  448. ver, pn, client->addr << 1, client->adapter->name);
  449. vpx3220_write_block(sd, init_common, sizeof(init_common));
  450. vpx3220_write_fp_block(sd, init_fp, sizeof(init_fp) >> 1);
  451. /* Default to PAL */
  452. vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
  453. return 0;
  454. }
  455. static int vpx3220_remove(struct i2c_client *client)
  456. {
  457. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  458. struct vpx3220 *decoder = to_vpx3220(sd);
  459. v4l2_device_unregister_subdev(sd);
  460. v4l2_ctrl_handler_free(&decoder->hdl);
  461. return 0;
  462. }
  463. static const struct i2c_device_id vpx3220_id[] = {
  464. { "vpx3220a", 0 },
  465. { "vpx3216b", 0 },
  466. { "vpx3214c", 0 },
  467. { }
  468. };
  469. MODULE_DEVICE_TABLE(i2c, vpx3220_id);
  470. static struct i2c_driver vpx3220_driver = {
  471. .driver = {
  472. .name = "vpx3220",
  473. },
  474. .probe = vpx3220_probe,
  475. .remove = vpx3220_remove,
  476. .id_table = vpx3220_id,
  477. };
  478. module_i2c_driver(vpx3220_driver);