saa7110.c 15 KB

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