saa7110.c 15 KB

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