saa7110.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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/io.h>
  34. #include <asm/uaccess.h>
  35. MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
  36. MODULE_AUTHOR("Pauline Middelink");
  37. MODULE_LICENSE("GPL");
  38. #include <linux/i2c.h>
  39. #define I2C_NAME(s) (s)->name
  40. #include <linux/videodev.h>
  41. #include <media/v4l2-common.h>
  42. #include <linux/video_decoder.h>
  43. static int debug = 0;
  44. module_param(debug, int, 0);
  45. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  46. #define dprintk(num, format, args...) \
  47. do { \
  48. if (debug >= num) \
  49. printk(format, ##args); \
  50. } while (0)
  51. #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
  52. #define SAA7110_MAX_OUTPUT 0 /* its a decoder only */
  53. #define I2C_SAA7110 0x9C /* or 0x9E */
  54. #define SAA7110_NR_REG 0x35
  55. struct saa7110 {
  56. u8 reg[SAA7110_NR_REG];
  57. int norm;
  58. int input;
  59. int enable;
  60. int bright;
  61. int contrast;
  62. int hue;
  63. int sat;
  64. wait_queue_head_t wq;
  65. };
  66. /* ----------------------------------------------------------------------- */
  67. /* I2C support functions */
  68. /* ----------------------------------------------------------------------- */
  69. static int
  70. saa7110_write (struct i2c_client *client,
  71. u8 reg,
  72. u8 value)
  73. {
  74. struct saa7110 *decoder = i2c_get_clientdata(client);
  75. decoder->reg[reg] = value;
  76. return i2c_smbus_write_byte_data(client, reg, value);
  77. }
  78. static int
  79. saa7110_write_block (struct i2c_client *client,
  80. const u8 *data,
  81. unsigned int len)
  82. {
  83. int ret = -1;
  84. u8 reg = *data; /* first register to write to */
  85. /* Sanity check */
  86. if (reg + (len - 1) > SAA7110_NR_REG)
  87. return ret;
  88. /* the saa7110 has an autoincrement function, use it if
  89. * the adapter understands raw I2C */
  90. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  91. struct saa7110 *decoder = i2c_get_clientdata(client);
  92. ret = i2c_master_send(client, data, len);
  93. /* Cache the written data */
  94. memcpy(decoder->reg + reg, data + 1, len - 1);
  95. } else {
  96. for (++data, --len; len; len--) {
  97. if ((ret = saa7110_write(client, reg++,
  98. *data++)) < 0)
  99. break;
  100. }
  101. }
  102. return ret;
  103. }
  104. static inline int
  105. saa7110_read (struct i2c_client *client)
  106. {
  107. return i2c_smbus_read_byte(client);
  108. }
  109. /* ----------------------------------------------------------------------- */
  110. /* SAA7110 functions */
  111. /* ----------------------------------------------------------------------- */
  112. #define FRESP_06H_COMPST 0x03 //0x13
  113. #define FRESP_06H_SVIDEO 0x83 //0xC0
  114. static int
  115. saa7110_selmux (struct i2c_client *client,
  116. int chan)
  117. {
  118. static const unsigned char modes[9][8] = {
  119. /* mode 0 */
  120. {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
  121. 0x44, 0x75, 0x16},
  122. /* mode 1 */
  123. {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
  124. 0x44, 0x75, 0x16},
  125. /* mode 2 */
  126. {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
  127. 0x60, 0xB5, 0x05},
  128. /* mode 3 */
  129. {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
  130. 0x60, 0xB5, 0x05},
  131. /* mode 4 */
  132. {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
  133. 0x60, 0xB5, 0x03},
  134. /* mode 5 */
  135. {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
  136. 0x60, 0xB5, 0x03},
  137. /* mode 6 */
  138. {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
  139. 0x44, 0x75, 0x12},
  140. /* mode 7 */
  141. {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
  142. 0x60, 0xB5, 0x14},
  143. /* mode 8 */
  144. {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
  145. 0x44, 0x75, 0x21}
  146. };
  147. struct saa7110 *decoder = i2c_get_clientdata(client);
  148. const unsigned char *ptr = modes[chan];
  149. saa7110_write(client, 0x06, ptr[0]); /* Luminance control */
  150. saa7110_write(client, 0x20, ptr[1]); /* Analog Control #1 */
  151. saa7110_write(client, 0x21, ptr[2]); /* Analog Control #2 */
  152. saa7110_write(client, 0x22, ptr[3]); /* Mixer Control #1 */
  153. saa7110_write(client, 0x2C, ptr[4]); /* Mixer Control #2 */
  154. saa7110_write(client, 0x30, ptr[5]); /* ADCs gain control */
  155. saa7110_write(client, 0x31, ptr[6]); /* Mixer Control #3 */
  156. saa7110_write(client, 0x21, ptr[7]); /* Analog Control #2 */
  157. decoder->input = chan;
  158. return 0;
  159. }
  160. static const unsigned char initseq[1 + SAA7110_NR_REG] = {
  161. 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
  162. /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
  163. /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
  164. /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
  166. /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
  167. /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
  168. };
  169. static int
  170. determine_norm (struct i2c_client *client)
  171. {
  172. DEFINE_WAIT(wait);
  173. struct saa7110 *decoder = i2c_get_clientdata(client);
  174. int status;
  175. /* mode changed, start automatic detection */
  176. saa7110_write_block(client, initseq, sizeof(initseq));
  177. saa7110_selmux(client, decoder->input);
  178. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  179. schedule_timeout(HZ/4);
  180. finish_wait(&decoder->wq, &wait);
  181. status = saa7110_read(client);
  182. if (status & 0x40) {
  183. dprintk(1, KERN_INFO "%s: status=0x%02x (no signal)\n",
  184. I2C_NAME(client), status);
  185. return decoder->norm; // no change
  186. }
  187. if ((status & 3) == 0) {
  188. saa7110_write(client, 0x06, 0x83);
  189. if (status & 0x20) {
  190. dprintk(1,
  191. KERN_INFO
  192. "%s: status=0x%02x (NTSC/no color)\n",
  193. I2C_NAME(client), status);
  194. //saa7110_write(client,0x2E,0x81);
  195. return VIDEO_MODE_NTSC;
  196. }
  197. dprintk(1, KERN_INFO "%s: status=0x%02x (PAL/no color)\n",
  198. I2C_NAME(client), status);
  199. //saa7110_write(client,0x2E,0x9A);
  200. return VIDEO_MODE_PAL;
  201. }
  202. //saa7110_write(client,0x06,0x03);
  203. if (status & 0x20) { /* 60Hz */
  204. dprintk(1, KERN_INFO "%s: status=0x%02x (NTSC)\n",
  205. I2C_NAME(client), status);
  206. saa7110_write(client, 0x0D, 0x86);
  207. saa7110_write(client, 0x0F, 0x50);
  208. saa7110_write(client, 0x11, 0x2C);
  209. //saa7110_write(client,0x2E,0x81);
  210. return VIDEO_MODE_NTSC;
  211. }
  212. /* 50Hz -> PAL/SECAM */
  213. saa7110_write(client, 0x0D, 0x86);
  214. saa7110_write(client, 0x0F, 0x10);
  215. saa7110_write(client, 0x11, 0x59);
  216. //saa7110_write(client,0x2E,0x9A);
  217. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  218. schedule_timeout(HZ/4);
  219. finish_wait(&decoder->wq, &wait);
  220. status = saa7110_read(client);
  221. if ((status & 0x03) == 0x01) {
  222. dprintk(1, KERN_INFO "%s: status=0x%02x (SECAM)\n",
  223. I2C_NAME(client), status);
  224. saa7110_write(client, 0x0D, 0x87);
  225. return VIDEO_MODE_SECAM;
  226. }
  227. dprintk(1, KERN_INFO "%s: status=0x%02x (PAL)\n", I2C_NAME(client),
  228. status);
  229. return VIDEO_MODE_PAL;
  230. }
  231. static int
  232. saa7110_command (struct i2c_client *client,
  233. unsigned int cmd,
  234. void *arg)
  235. {
  236. struct saa7110 *decoder = i2c_get_clientdata(client);
  237. int v;
  238. switch (cmd) {
  239. case 0:
  240. //saa7110_write_block(client, initseq, sizeof(initseq));
  241. break;
  242. case DECODER_GET_CAPABILITIES:
  243. {
  244. struct video_decoder_capability *dc = arg;
  245. dc->flags =
  246. VIDEO_DECODER_PAL | VIDEO_DECODER_NTSC |
  247. VIDEO_DECODER_SECAM | VIDEO_DECODER_AUTO;
  248. dc->inputs = SAA7110_MAX_INPUT;
  249. dc->outputs = SAA7110_MAX_OUTPUT;
  250. }
  251. break;
  252. case DECODER_GET_STATUS:
  253. {
  254. int status;
  255. int res = 0;
  256. status = saa7110_read(client);
  257. dprintk(1, KERN_INFO "%s: status=0x%02x norm=%d\n",
  258. I2C_NAME(client), status, decoder->norm);
  259. if (!(status & 0x40))
  260. res |= DECODER_STATUS_GOOD;
  261. if (status & 0x03)
  262. res |= DECODER_STATUS_COLOR;
  263. switch (decoder->norm) {
  264. case VIDEO_MODE_NTSC:
  265. res |= DECODER_STATUS_NTSC;
  266. break;
  267. case VIDEO_MODE_PAL:
  268. res |= DECODER_STATUS_PAL;
  269. break;
  270. case VIDEO_MODE_SECAM:
  271. res |= DECODER_STATUS_SECAM;
  272. break;
  273. }
  274. *(int *) arg = res;
  275. }
  276. break;
  277. case DECODER_SET_NORM:
  278. v = *(int *) arg;
  279. if (decoder->norm != v) {
  280. decoder->norm = v;
  281. //saa7110_write(client, 0x06, 0x03);
  282. switch (v) {
  283. case VIDEO_MODE_NTSC:
  284. saa7110_write(client, 0x0D, 0x86);
  285. saa7110_write(client, 0x0F, 0x50);
  286. saa7110_write(client, 0x11, 0x2C);
  287. //saa7110_write(client, 0x2E, 0x81);
  288. dprintk(1,
  289. KERN_INFO "%s: switched to NTSC\n",
  290. I2C_NAME(client));
  291. break;
  292. case VIDEO_MODE_PAL:
  293. saa7110_write(client, 0x0D, 0x86);
  294. saa7110_write(client, 0x0F, 0x10);
  295. saa7110_write(client, 0x11, 0x59);
  296. //saa7110_write(client, 0x2E, 0x9A);
  297. dprintk(1,
  298. KERN_INFO "%s: switched to PAL\n",
  299. I2C_NAME(client));
  300. break;
  301. case VIDEO_MODE_SECAM:
  302. saa7110_write(client, 0x0D, 0x87);
  303. saa7110_write(client, 0x0F, 0x10);
  304. saa7110_write(client, 0x11, 0x59);
  305. //saa7110_write(client, 0x2E, 0x9A);
  306. dprintk(1,
  307. KERN_INFO
  308. "%s: switched to SECAM\n",
  309. I2C_NAME(client));
  310. break;
  311. case VIDEO_MODE_AUTO:
  312. dprintk(1,
  313. KERN_INFO
  314. "%s: TV standard detection...\n",
  315. I2C_NAME(client));
  316. decoder->norm = determine_norm(client);
  317. *(int *) arg = decoder->norm;
  318. break;
  319. default:
  320. return -EPERM;
  321. }
  322. }
  323. break;
  324. case DECODER_SET_INPUT:
  325. v = *(int *) arg;
  326. if (v < 0 || v > SAA7110_MAX_INPUT) {
  327. dprintk(1,
  328. KERN_INFO "%s: input=%d not available\n",
  329. I2C_NAME(client), v);
  330. return -EINVAL;
  331. }
  332. if (decoder->input != v) {
  333. saa7110_selmux(client, v);
  334. dprintk(1, KERN_INFO "%s: switched to input=%d\n",
  335. I2C_NAME(client), v);
  336. }
  337. break;
  338. case DECODER_SET_OUTPUT:
  339. v = *(int *) arg;
  340. /* not much choice of outputs */
  341. if (v != 0)
  342. return -EINVAL;
  343. break;
  344. case DECODER_ENABLE_OUTPUT:
  345. v = *(int *) arg;
  346. if (decoder->enable != v) {
  347. decoder->enable = v;
  348. saa7110_write(client, 0x0E, v ? 0x18 : 0x80);
  349. dprintk(1, KERN_INFO "%s: YUV %s\n", I2C_NAME(client),
  350. v ? "on" : "off");
  351. }
  352. break;
  353. case DECODER_SET_PICTURE:
  354. {
  355. struct video_picture *pic = arg;
  356. if (decoder->bright != pic->brightness) {
  357. /* We want 0 to 255 we get 0-65535 */
  358. decoder->bright = pic->brightness;
  359. saa7110_write(client, 0x19, decoder->bright >> 8);
  360. }
  361. if (decoder->contrast != pic->contrast) {
  362. /* We want 0 to 127 we get 0-65535 */
  363. decoder->contrast = pic->contrast;
  364. saa7110_write(client, 0x13,
  365. decoder->contrast >> 9);
  366. }
  367. if (decoder->sat != pic->colour) {
  368. /* We want 0 to 127 we get 0-65535 */
  369. decoder->sat = pic->colour;
  370. saa7110_write(client, 0x12, decoder->sat >> 9);
  371. }
  372. if (decoder->hue != pic->hue) {
  373. /* We want -128 to 127 we get 0-65535 */
  374. decoder->hue = pic->hue;
  375. saa7110_write(client, 0x07,
  376. (decoder->hue >> 8) - 128);
  377. }
  378. }
  379. break;
  380. case DECODER_DUMP:
  381. for (v = 0; v < SAA7110_NR_REG; v += 16) {
  382. int j;
  383. dprintk(1, KERN_DEBUG "%s: %02x:", I2C_NAME(client),
  384. v);
  385. for (j = 0; j < 16 && v + j < SAA7110_NR_REG; j++)
  386. dprintk(1, " %02x", decoder->reg[v + j]);
  387. dprintk(1, "\n");
  388. }
  389. break;
  390. default:
  391. dprintk(1, KERN_INFO "unknown saa7110_command??(%d)\n",
  392. cmd);
  393. return -EINVAL;
  394. }
  395. return 0;
  396. }
  397. /* ----------------------------------------------------------------------- */
  398. /*
  399. * Generic i2c probe
  400. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  401. */
  402. static unsigned short normal_i2c[] = {
  403. I2C_SAA7110 >> 1,
  404. (I2C_SAA7110 >> 1) + 1,
  405. I2C_CLIENT_END
  406. };
  407. static unsigned short ignore = I2C_CLIENT_END;
  408. static struct i2c_client_address_data addr_data = {
  409. .normal_i2c = normal_i2c,
  410. .probe = &ignore,
  411. .ignore = &ignore,
  412. };
  413. static struct i2c_driver i2c_driver_saa7110;
  414. static int
  415. saa7110_detect_client (struct i2c_adapter *adapter,
  416. int address,
  417. int kind)
  418. {
  419. struct i2c_client *client;
  420. struct saa7110 *decoder;
  421. int rv;
  422. dprintk(1,
  423. KERN_INFO
  424. "saa7110.c: detecting saa7110 client on address 0x%x\n",
  425. address << 1);
  426. /* Check if the adapter supports the needed features */
  427. if (!i2c_check_functionality
  428. (adapter,
  429. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  430. return 0;
  431. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  432. if (client == 0)
  433. return -ENOMEM;
  434. client->addr = address;
  435. client->adapter = adapter;
  436. client->driver = &i2c_driver_saa7110;
  437. strlcpy(I2C_NAME(client), "saa7110", sizeof(I2C_NAME(client)));
  438. decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
  439. if (decoder == 0) {
  440. kfree(client);
  441. return -ENOMEM;
  442. }
  443. decoder->norm = VIDEO_MODE_PAL;
  444. decoder->input = 0;
  445. decoder->enable = 1;
  446. decoder->bright = 32768;
  447. decoder->contrast = 32768;
  448. decoder->hue = 32768;
  449. decoder->sat = 32768;
  450. init_waitqueue_head(&decoder->wq);
  451. i2c_set_clientdata(client, decoder);
  452. rv = i2c_attach_client(client);
  453. if (rv) {
  454. kfree(client);
  455. kfree(decoder);
  456. return rv;
  457. }
  458. rv = saa7110_write_block(client, initseq, sizeof(initseq));
  459. if (rv < 0)
  460. dprintk(1, KERN_ERR "%s_attach: init status %d\n",
  461. I2C_NAME(client), rv);
  462. else {
  463. int ver, status;
  464. saa7110_write(client, 0x21, 0x10);
  465. saa7110_write(client, 0x0e, 0x18);
  466. saa7110_write(client, 0x0D, 0x04);
  467. ver = saa7110_read(client);
  468. saa7110_write(client, 0x0D, 0x06);
  469. //mdelay(150);
  470. status = saa7110_read(client);
  471. dprintk(1,
  472. KERN_INFO
  473. "%s_attach: SAA7110A version %x at 0x%02x, status=0x%02x\n",
  474. I2C_NAME(client), ver, client->addr << 1, status);
  475. saa7110_write(client, 0x0D, 0x86);
  476. saa7110_write(client, 0x0F, 0x10);
  477. saa7110_write(client, 0x11, 0x59);
  478. //saa7110_write(client, 0x2E, 0x9A);
  479. }
  480. //saa7110_selmux(client,0);
  481. //determine_norm(client);
  482. /* setup and implicit mode 0 select has been performed */
  483. return 0;
  484. }
  485. static int
  486. saa7110_attach_adapter (struct i2c_adapter *adapter)
  487. {
  488. dprintk(1,
  489. KERN_INFO
  490. "saa7110.c: starting probe for adapter %s (0x%x)\n",
  491. I2C_NAME(adapter), adapter->id);
  492. return i2c_probe(adapter, &addr_data, &saa7110_detect_client);
  493. }
  494. static int
  495. saa7110_detach_client (struct i2c_client *client)
  496. {
  497. struct saa7110 *decoder = i2c_get_clientdata(client);
  498. int err;
  499. err = i2c_detach_client(client);
  500. if (err) {
  501. return err;
  502. }
  503. kfree(decoder);
  504. kfree(client);
  505. return 0;
  506. }
  507. /* ----------------------------------------------------------------------- */
  508. static struct i2c_driver i2c_driver_saa7110 = {
  509. .driver = {
  510. .name = "saa7110",
  511. },
  512. .id = I2C_DRIVERID_SAA7110,
  513. .attach_adapter = saa7110_attach_adapter,
  514. .detach_client = saa7110_detach_client,
  515. .command = saa7110_command,
  516. };
  517. static int __init
  518. saa7110_init (void)
  519. {
  520. return i2c_add_driver(&i2c_driver_saa7110);
  521. }
  522. static void __exit
  523. saa7110_exit (void)
  524. {
  525. i2c_del_driver(&i2c_driver_saa7110);
  526. }
  527. module_init(saa7110_init);
  528. module_exit(saa7110_exit);