saa7110.c 15 KB

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