saa7110.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * saa7110 - Philips SAA7110(A) video decoder driver
  3. *
  4. * Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl>
  5. *
  6. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  7. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  8. * - some corrections for Pinnacle Systems Inc. DC10plus card.
  9. *
  10. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  11. * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/delay.h>
  31. #include <linux/slab.h>
  32. #include <linux/wait.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/i2c.h>
  35. #include <linux/videodev.h>
  36. #include <linux/video_decoder.h>
  37. #include <media/v4l2-common.h>
  38. #include <media/v4l2-i2c-drv-legacy.h>
  39. MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
  40. MODULE_AUTHOR("Pauline Middelink");
  41. MODULE_LICENSE("GPL");
  42. static int debug;
  43. module_param(debug, int, 0);
  44. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  45. #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
  46. #define SAA7110_MAX_OUTPUT 1 /* 1 YUV */
  47. #define SAA7110_NR_REG 0x35
  48. struct saa7110 {
  49. u8 reg[SAA7110_NR_REG];
  50. int norm;
  51. int input;
  52. int enable;
  53. int bright;
  54. int contrast;
  55. int hue;
  56. int sat;
  57. wait_queue_head_t wq;
  58. };
  59. /* ----------------------------------------------------------------------- */
  60. /* I2C support functions */
  61. /* ----------------------------------------------------------------------- */
  62. static int saa7110_write(struct i2c_client *client, u8 reg, u8 value)
  63. {
  64. struct saa7110 *decoder = i2c_get_clientdata(client);
  65. decoder->reg[reg] = value;
  66. return i2c_smbus_write_byte_data(client, reg, value);
  67. }
  68. static int saa7110_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
  69. {
  70. int ret = -1;
  71. u8 reg = *data; /* first register to write to */
  72. /* Sanity check */
  73. if (reg + (len - 1) > SAA7110_NR_REG)
  74. return ret;
  75. /* the saa7110 has an autoincrement function, use it if
  76. * the adapter understands raw I2C */
  77. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  78. struct saa7110 *decoder = i2c_get_clientdata(client);
  79. ret = i2c_master_send(client, data, len);
  80. /* Cache the written data */
  81. memcpy(decoder->reg + reg, data + 1, len - 1);
  82. } else {
  83. for (++data, --len; len; len--) {
  84. ret = saa7110_write(client, reg++, *data++);
  85. if (ret < 0)
  86. break;
  87. }
  88. }
  89. return ret;
  90. }
  91. static inline int saa7110_read(struct i2c_client *client)
  92. {
  93. return i2c_smbus_read_byte(client);
  94. }
  95. /* ----------------------------------------------------------------------- */
  96. /* SAA7110 functions */
  97. /* ----------------------------------------------------------------------- */
  98. #define FRESP_06H_COMPST 0x03 //0x13
  99. #define FRESP_06H_SVIDEO 0x83 //0xC0
  100. static int saa7110_selmux(struct i2c_client *client, int chan)
  101. {
  102. static const unsigned char modes[9][8] = {
  103. /* mode 0 */
  104. {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
  105. 0x44, 0x75, 0x16},
  106. /* mode 1 */
  107. {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
  108. 0x44, 0x75, 0x16},
  109. /* mode 2 */
  110. {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
  111. 0x60, 0xB5, 0x05},
  112. /* mode 3 */
  113. {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
  114. 0x60, 0xB5, 0x05},
  115. /* mode 4 */
  116. {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
  117. 0x60, 0xB5, 0x03},
  118. /* mode 5 */
  119. {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
  120. 0x60, 0xB5, 0x03},
  121. /* mode 6 */
  122. {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
  123. 0x44, 0x75, 0x12},
  124. /* mode 7 */
  125. {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
  126. 0x60, 0xB5, 0x14},
  127. /* mode 8 */
  128. {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
  129. 0x44, 0x75, 0x21}
  130. };
  131. struct saa7110 *decoder = i2c_get_clientdata(client);
  132. const unsigned char *ptr = modes[chan];
  133. saa7110_write(client, 0x06, ptr[0]); /* Luminance control */
  134. saa7110_write(client, 0x20, ptr[1]); /* Analog Control #1 */
  135. saa7110_write(client, 0x21, ptr[2]); /* Analog Control #2 */
  136. saa7110_write(client, 0x22, ptr[3]); /* Mixer Control #1 */
  137. saa7110_write(client, 0x2C, ptr[4]); /* Mixer Control #2 */
  138. saa7110_write(client, 0x30, ptr[5]); /* ADCs gain control */
  139. saa7110_write(client, 0x31, ptr[6]); /* Mixer Control #3 */
  140. saa7110_write(client, 0x21, ptr[7]); /* Analog Control #2 */
  141. decoder->input = chan;
  142. return 0;
  143. }
  144. static const unsigned char initseq[1 + SAA7110_NR_REG] = {
  145. 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
  146. /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
  147. /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
  148. /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149. /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
  150. /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
  151. /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
  152. };
  153. static int determine_norm(struct i2c_client *client)
  154. {
  155. DEFINE_WAIT(wait);
  156. struct saa7110 *decoder = i2c_get_clientdata(client);
  157. int status;
  158. /* mode changed, start automatic detection */
  159. saa7110_write_block(client, initseq, sizeof(initseq));
  160. saa7110_selmux(client, decoder->input);
  161. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  162. schedule_timeout(msecs_to_jiffies(250));
  163. finish_wait(&decoder->wq, &wait);
  164. status = saa7110_read(client);
  165. if (status & 0x40) {
  166. v4l_dbg(1, debug, client, "status=0x%02x (no signal)\n", status);
  167. return decoder->norm; // no change
  168. }
  169. if ((status & 3) == 0) {
  170. saa7110_write(client, 0x06, 0x83);
  171. if (status & 0x20) {
  172. v4l_dbg(1, debug, client, "status=0x%02x (NTSC/no color)\n", status);
  173. //saa7110_write(client,0x2E,0x81);
  174. return VIDEO_MODE_NTSC;
  175. }
  176. v4l_dbg(1, debug, client, "status=0x%02x (PAL/no color)\n", status);
  177. //saa7110_write(client,0x2E,0x9A);
  178. return VIDEO_MODE_PAL;
  179. }
  180. //saa7110_write(client,0x06,0x03);
  181. if (status & 0x20) { /* 60Hz */
  182. v4l_dbg(1, debug, client, "status=0x%02x (NTSC)\n", status);
  183. saa7110_write(client, 0x0D, 0x86);
  184. saa7110_write(client, 0x0F, 0x50);
  185. saa7110_write(client, 0x11, 0x2C);
  186. //saa7110_write(client,0x2E,0x81);
  187. return VIDEO_MODE_NTSC;
  188. }
  189. /* 50Hz -> PAL/SECAM */
  190. saa7110_write(client, 0x0D, 0x86);
  191. saa7110_write(client, 0x0F, 0x10);
  192. saa7110_write(client, 0x11, 0x59);
  193. //saa7110_write(client,0x2E,0x9A);
  194. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  195. schedule_timeout(msecs_to_jiffies(250));
  196. finish_wait(&decoder->wq, &wait);
  197. status = saa7110_read(client);
  198. if ((status & 0x03) == 0x01) {
  199. v4l_dbg(1, debug, client, "status=0x%02x (SECAM)\n", status);
  200. saa7110_write(client, 0x0D, 0x87);
  201. return VIDEO_MODE_SECAM;
  202. }
  203. v4l_dbg(1, debug, client, "status=0x%02x (PAL)\n", status);
  204. return VIDEO_MODE_PAL;
  205. }
  206. static int
  207. saa7110_command (struct i2c_client *client,
  208. unsigned int cmd,
  209. void *arg)
  210. {
  211. struct saa7110 *decoder = i2c_get_clientdata(client);
  212. int v;
  213. switch (cmd) {
  214. case 0:
  215. //saa7110_write_block(client, initseq, sizeof(initseq));
  216. break;
  217. case DECODER_GET_CAPABILITIES:
  218. {
  219. struct video_decoder_capability *dc = arg;
  220. dc->flags =
  221. VIDEO_DECODER_PAL | VIDEO_DECODER_NTSC |
  222. VIDEO_DECODER_SECAM | VIDEO_DECODER_AUTO;
  223. dc->inputs = SAA7110_MAX_INPUT;
  224. dc->outputs = SAA7110_MAX_OUTPUT;
  225. break;
  226. }
  227. case DECODER_GET_STATUS:
  228. {
  229. int status;
  230. int res = 0;
  231. status = saa7110_read(client);
  232. v4l_dbg(1, debug, client, "status=0x%02x norm=%d\n",
  233. status, decoder->norm);
  234. if (!(status & 0x40))
  235. res |= DECODER_STATUS_GOOD;
  236. if (status & 0x03)
  237. res |= DECODER_STATUS_COLOR;
  238. switch (decoder->norm) {
  239. case VIDEO_MODE_NTSC:
  240. res |= DECODER_STATUS_NTSC;
  241. break;
  242. case VIDEO_MODE_PAL:
  243. res |= DECODER_STATUS_PAL;
  244. break;
  245. case VIDEO_MODE_SECAM:
  246. res |= DECODER_STATUS_SECAM;
  247. break;
  248. }
  249. *(int *) arg = res;
  250. break;
  251. }
  252. case DECODER_SET_NORM:
  253. v = *(int *) arg;
  254. if (decoder->norm != v) {
  255. decoder->norm = v;
  256. //saa7110_write(client, 0x06, 0x03);
  257. switch (v) {
  258. case VIDEO_MODE_NTSC:
  259. saa7110_write(client, 0x0D, 0x86);
  260. saa7110_write(client, 0x0F, 0x50);
  261. saa7110_write(client, 0x11, 0x2C);
  262. //saa7110_write(client, 0x2E, 0x81);
  263. v4l_dbg(1, debug, client, "switched to NTSC\n");
  264. break;
  265. case VIDEO_MODE_PAL:
  266. saa7110_write(client, 0x0D, 0x86);
  267. saa7110_write(client, 0x0F, 0x10);
  268. saa7110_write(client, 0x11, 0x59);
  269. //saa7110_write(client, 0x2E, 0x9A);
  270. v4l_dbg(1, debug, client, "switched to PAL\n");
  271. break;
  272. case VIDEO_MODE_SECAM:
  273. saa7110_write(client, 0x0D, 0x87);
  274. saa7110_write(client, 0x0F, 0x10);
  275. saa7110_write(client, 0x11, 0x59);
  276. //saa7110_write(client, 0x2E, 0x9A);
  277. v4l_dbg(1, debug, client, "switched to SECAM\n");
  278. break;
  279. case VIDEO_MODE_AUTO:
  280. v4l_dbg(1, debug, client, "switched to AUTO\n");
  281. decoder->norm = determine_norm(client);
  282. *(int *) arg = decoder->norm;
  283. break;
  284. default:
  285. return -EPERM;
  286. }
  287. }
  288. break;
  289. case DECODER_SET_INPUT:
  290. v = *(int *) arg;
  291. if (v < 0 || v >= SAA7110_MAX_INPUT) {
  292. v4l_dbg(1, debug, client, "input=%d not available\n", v);
  293. return -EINVAL;
  294. }
  295. if (decoder->input != v) {
  296. saa7110_selmux(client, v);
  297. v4l_dbg(1, debug, client, "switched to input=%d\n", v);
  298. }
  299. break;
  300. case DECODER_SET_OUTPUT:
  301. v = *(int *) arg;
  302. /* not much choice of outputs */
  303. if (v != 0)
  304. return -EINVAL;
  305. break;
  306. case DECODER_ENABLE_OUTPUT:
  307. v = *(int *) arg;
  308. if (decoder->enable != v) {
  309. decoder->enable = v;
  310. saa7110_write(client, 0x0E, v ? 0x18 : 0x80);
  311. v4l_dbg(1, debug, client, "YUV %s\n", v ? "on" : "off");
  312. }
  313. break;
  314. case DECODER_SET_PICTURE:
  315. {
  316. struct video_picture *pic = arg;
  317. if (decoder->bright != pic->brightness) {
  318. /* We want 0 to 255 we get 0-65535 */
  319. decoder->bright = pic->brightness;
  320. saa7110_write(client, 0x19, decoder->bright >> 8);
  321. }
  322. if (decoder->contrast != pic->contrast) {
  323. /* We want 0 to 127 we get 0-65535 */
  324. decoder->contrast = pic->contrast;
  325. saa7110_write(client, 0x13,
  326. decoder->contrast >> 9);
  327. }
  328. if (decoder->sat != pic->colour) {
  329. /* We want 0 to 127 we get 0-65535 */
  330. decoder->sat = pic->colour;
  331. saa7110_write(client, 0x12, decoder->sat >> 9);
  332. }
  333. if (decoder->hue != pic->hue) {
  334. /* We want -128 to 127 we get 0-65535 */
  335. decoder->hue = pic->hue;
  336. saa7110_write(client, 0x07,
  337. (decoder->hue >> 8) - 128);
  338. }
  339. break;
  340. }
  341. case DECODER_DUMP:
  342. if (!debug)
  343. break;
  344. for (v = 0; v < SAA7110_NR_REG; v += 16) {
  345. int j;
  346. v4l_dbg(1, debug, client, "%02x:", v);
  347. for (j = 0; j < 16 && v + j < SAA7110_NR_REG; j++)
  348. printk(KERN_CONT " %02x", decoder->reg[v + j]);
  349. printk(KERN_CONT "\n");
  350. }
  351. break;
  352. default:
  353. v4l_dbg(1, debug, client, "unknown command %08x\n", cmd);
  354. return -EINVAL;
  355. }
  356. return 0;
  357. }
  358. /* ----------------------------------------------------------------------- */
  359. /*
  360. * Generic i2c probe
  361. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  362. */
  363. static unsigned short normal_i2c[] = { 0x9c >> 1, 0x9e >> 1, I2C_CLIENT_END };
  364. I2C_CLIENT_INSMOD;
  365. static int saa7110_probe(struct i2c_client *client,
  366. const struct i2c_device_id *id)
  367. {
  368. struct saa7110 *decoder;
  369. int rv;
  370. /* Check if the adapter supports the needed features */
  371. if (!i2c_check_functionality(client->adapter,
  372. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  373. return -ENODEV;
  374. v4l_info(client, "chip found @ 0x%x (%s)\n",
  375. client->addr << 1, client->adapter->name);
  376. decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
  377. if (!decoder)
  378. return -ENOMEM;
  379. decoder->norm = VIDEO_MODE_PAL;
  380. decoder->input = 0;
  381. decoder->enable = 1;
  382. decoder->bright = 32768;
  383. decoder->contrast = 32768;
  384. decoder->hue = 32768;
  385. decoder->sat = 32768;
  386. init_waitqueue_head(&decoder->wq);
  387. i2c_set_clientdata(client, decoder);
  388. rv = saa7110_write_block(client, initseq, sizeof(initseq));
  389. if (rv < 0) {
  390. v4l_dbg(1, debug, client, "init status %d\n", rv);
  391. } else {
  392. int ver, status;
  393. saa7110_write(client, 0x21, 0x10);
  394. saa7110_write(client, 0x0e, 0x18);
  395. saa7110_write(client, 0x0D, 0x04);
  396. ver = saa7110_read(client);
  397. saa7110_write(client, 0x0D, 0x06);
  398. //mdelay(150);
  399. status = saa7110_read(client);
  400. v4l_dbg(1, debug, client, "version %x, status=0x%02x\n",
  401. ver, status);
  402. saa7110_write(client, 0x0D, 0x86);
  403. saa7110_write(client, 0x0F, 0x10);
  404. saa7110_write(client, 0x11, 0x59);
  405. //saa7110_write(client, 0x2E, 0x9A);
  406. }
  407. //saa7110_selmux(client,0);
  408. //determine_norm(client);
  409. /* setup and implicit mode 0 select has been performed */
  410. return 0;
  411. }
  412. static int saa7110_remove(struct i2c_client *client)
  413. {
  414. kfree(i2c_get_clientdata(client));
  415. return 0;
  416. }
  417. /* ----------------------------------------------------------------------- */
  418. static const struct i2c_device_id saa7110_id[] = {
  419. { "saa7110", 0 },
  420. { }
  421. };
  422. MODULE_DEVICE_TABLE(i2c, saa7110_id);
  423. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  424. .name = "saa7110",
  425. .driverid = I2C_DRIVERID_SAA7110,
  426. .command = saa7110_command,
  427. .probe = saa7110_probe,
  428. .remove = saa7110_remove,
  429. .id_table = saa7110_id,
  430. };