saa7111.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * saa7111 - Philips SAA7111A video decoder driver version 0.0.3
  3. *
  4. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  5. *
  6. * Slight changes for video timing and attachment output by
  7. * Wolfgang Scherr <scherr@net4you.net>
  8. *
  9. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  10. * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
  11. *
  12. * Changes by Michael Hunold <michael@mihu.de>
  13. * - implemented DECODER_SET_GPIO, DECODER_INIT, DECODER_SET_VBI_BYPASS
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/errno.h>
  33. #include <linux/fs.h>
  34. #include <linux/kernel.h>
  35. #include <linux/major.h>
  36. #include <linux/slab.h>
  37. #include <linux/mm.h>
  38. #include <linux/pci.h>
  39. #include <linux/signal.h>
  40. #include <asm/io.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/page.h>
  43. #include <linux/sched.h>
  44. #include <linux/types.h>
  45. #include <linux/videodev.h>
  46. #include <asm/uaccess.h>
  47. MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
  48. MODULE_AUTHOR("Dave Perks");
  49. MODULE_LICENSE("GPL");
  50. #include <linux/i2c.h>
  51. #define I2C_NAME(s) (s)->name
  52. #include <linux/video_decoder.h>
  53. static int debug = 0;
  54. module_param(debug, int, 0644);
  55. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  56. #define dprintk(num, format, args...) \
  57. do { \
  58. if (debug >= num) \
  59. printk(format, ##args); \
  60. } while (0)
  61. /* ----------------------------------------------------------------------- */
  62. #define SAA7111_NR_REG 0x18
  63. struct saa7111 {
  64. unsigned char reg[SAA7111_NR_REG];
  65. int norm;
  66. int input;
  67. int enable;
  68. int bright;
  69. int contrast;
  70. int hue;
  71. int sat;
  72. };
  73. #define I2C_SAA7111 0x48
  74. /* ----------------------------------------------------------------------- */
  75. static inline int
  76. saa7111_write (struct i2c_client *client,
  77. u8 reg,
  78. u8 value)
  79. {
  80. struct saa7111 *decoder = i2c_get_clientdata(client);
  81. decoder->reg[reg] = value;
  82. return i2c_smbus_write_byte_data(client, reg, value);
  83. }
  84. static int
  85. saa7111_write_block (struct i2c_client *client,
  86. const u8 *data,
  87. unsigned int len)
  88. {
  89. int ret = -1;
  90. u8 reg;
  91. /* the saa7111 has an autoincrement function, use it if
  92. * the adapter understands raw I2C */
  93. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  94. /* do raw I2C, not smbus compatible */
  95. struct saa7111 *decoder = i2c_get_clientdata(client);
  96. u8 block_data[32];
  97. int block_len;
  98. while (len >= 2) {
  99. block_len = 0;
  100. block_data[block_len++] = reg = data[0];
  101. do {
  102. block_data[block_len++] =
  103. decoder->reg[reg++] = data[1];
  104. len -= 2;
  105. data += 2;
  106. } while (len >= 2 && data[0] == reg &&
  107. block_len < 32);
  108. if ((ret = i2c_master_send(client, block_data,
  109. block_len)) < 0)
  110. break;
  111. }
  112. } else {
  113. /* do some slow I2C emulation kind of thing */
  114. while (len >= 2) {
  115. reg = *data++;
  116. if ((ret = saa7111_write(client, reg,
  117. *data++)) < 0)
  118. break;
  119. len -= 2;
  120. }
  121. }
  122. return ret;
  123. }
  124. static int
  125. saa7111_init_decoder (struct i2c_client *client,
  126. struct video_decoder_init *init)
  127. {
  128. return saa7111_write_block(client, init->data, init->len);
  129. }
  130. static inline int
  131. saa7111_read (struct i2c_client *client,
  132. u8 reg)
  133. {
  134. return i2c_smbus_read_byte_data(client, reg);
  135. }
  136. /* ----------------------------------------------------------------------- */
  137. static const unsigned char saa7111_i2c_init[] = {
  138. 0x00, 0x00, /* 00 - ID byte */
  139. 0x01, 0x00, /* 01 - reserved */
  140. /*front end */
  141. 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
  142. 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
  143. * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
  144. 0x04, 0x00, /* 04 - GAI1=256 */
  145. 0x05, 0x00, /* 05 - GAI2=256 */
  146. /* decoder */
  147. 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
  148. * pixels after end of last line */
  149. /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
  150. * after end of last line */
  151. 0x07, 0xe8, /* 07 - HSS seems to be needed to
  152. * work with NTSC, too */
  153. 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
  154. * VTRC=1, HPLL=0, VNOI=0 */
  155. 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
  156. * VBLB=0, UPTCV=0, APER=1 */
  157. 0x0a, 0x80, /* 0a - BRIG=128 */
  158. 0x0b, 0x47, /* 0b - CONT=1.109 */
  159. 0x0c, 0x40, /* 0c - SATN=1.0 */
  160. 0x0d, 0x00, /* 0d - HUE=0 */
  161. 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
  162. * FCTC=0, CHBW=1 */
  163. 0x0f, 0x00, /* 0f - reserved */
  164. 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
  165. 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
  166. * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
  167. 0x12, 0x00, /* 12 - output control 2 */
  168. 0x13, 0x00, /* 13 - output control 3 */
  169. 0x14, 0x00, /* 14 - reserved */
  170. 0x15, 0x00, /* 15 - VBI */
  171. 0x16, 0x00, /* 16 - VBI */
  172. 0x17, 0x00, /* 17 - VBI */
  173. };
  174. static int
  175. saa7111_command (struct i2c_client *client,
  176. unsigned int cmd,
  177. void *arg)
  178. {
  179. struct saa7111 *decoder = i2c_get_clientdata(client);
  180. switch (cmd) {
  181. case 0:
  182. break;
  183. case DECODER_INIT:
  184. {
  185. struct video_decoder_init *init = arg;
  186. if (NULL != init)
  187. return saa7111_init_decoder(client, init);
  188. else {
  189. struct video_decoder_init vdi;
  190. vdi.data = saa7111_i2c_init;
  191. vdi.len = sizeof(saa7111_i2c_init);
  192. return saa7111_init_decoder(client, &vdi);
  193. }
  194. }
  195. case DECODER_DUMP:
  196. {
  197. int i;
  198. for (i = 0; i < SAA7111_NR_REG; i += 16) {
  199. int j;
  200. printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
  201. for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
  202. printk(" %02x",
  203. saa7111_read(client, i + j));
  204. }
  205. printk("\n");
  206. }
  207. }
  208. break;
  209. case DECODER_GET_CAPABILITIES:
  210. {
  211. struct video_decoder_capability *cap = arg;
  212. cap->flags = VIDEO_DECODER_PAL |
  213. VIDEO_DECODER_NTSC |
  214. VIDEO_DECODER_SECAM |
  215. VIDEO_DECODER_AUTO |
  216. VIDEO_DECODER_CCIR;
  217. cap->inputs = 8;
  218. cap->outputs = 1;
  219. }
  220. break;
  221. case DECODER_GET_STATUS:
  222. {
  223. int *iarg = arg;
  224. int status;
  225. int res;
  226. status = saa7111_read(client, 0x1f);
  227. dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
  228. status);
  229. res = 0;
  230. if ((status & (1 << 6)) == 0) {
  231. res |= DECODER_STATUS_GOOD;
  232. }
  233. switch (decoder->norm) {
  234. case VIDEO_MODE_NTSC:
  235. res |= DECODER_STATUS_NTSC;
  236. break;
  237. case VIDEO_MODE_PAL:
  238. res |= DECODER_STATUS_PAL;
  239. break;
  240. case VIDEO_MODE_SECAM:
  241. res |= DECODER_STATUS_SECAM;
  242. break;
  243. default:
  244. case VIDEO_MODE_AUTO:
  245. if ((status & (1 << 5)) != 0) {
  246. res |= DECODER_STATUS_NTSC;
  247. } else {
  248. res |= DECODER_STATUS_PAL;
  249. }
  250. break;
  251. }
  252. if ((status & (1 << 0)) != 0) {
  253. res |= DECODER_STATUS_COLOR;
  254. }
  255. *iarg = res;
  256. }
  257. break;
  258. case DECODER_SET_GPIO:
  259. {
  260. int *iarg = arg;
  261. if (0 != *iarg) {
  262. saa7111_write(client, 0x11,
  263. (decoder->reg[0x11] | 0x80));
  264. } else {
  265. saa7111_write(client, 0x11,
  266. (decoder->reg[0x11] & 0x7f));
  267. }
  268. break;
  269. }
  270. case DECODER_SET_VBI_BYPASS:
  271. {
  272. int *iarg = arg;
  273. if (0 != *iarg) {
  274. saa7111_write(client, 0x13,
  275. (decoder->reg[0x13] & 0xf0) | 0x0a);
  276. } else {
  277. saa7111_write(client, 0x13,
  278. (decoder->reg[0x13] & 0xf0));
  279. }
  280. break;
  281. }
  282. case DECODER_SET_NORM:
  283. {
  284. int *iarg = arg;
  285. switch (*iarg) {
  286. case VIDEO_MODE_NTSC:
  287. saa7111_write(client, 0x08,
  288. (decoder->reg[0x08] & 0x3f) | 0x40);
  289. saa7111_write(client, 0x0e,
  290. (decoder->reg[0x0e] & 0x8f));
  291. break;
  292. case VIDEO_MODE_PAL:
  293. saa7111_write(client, 0x08,
  294. (decoder->reg[0x08] & 0x3f) | 0x00);
  295. saa7111_write(client, 0x0e,
  296. (decoder->reg[0x0e] & 0x8f));
  297. break;
  298. case VIDEO_MODE_SECAM:
  299. saa7111_write(client, 0x08,
  300. (decoder->reg[0x08] & 0x3f) | 0x00);
  301. saa7111_write(client, 0x0e,
  302. (decoder->reg[0x0e] & 0x8f) | 0x50);
  303. break;
  304. case VIDEO_MODE_AUTO:
  305. saa7111_write(client, 0x08,
  306. (decoder->reg[0x08] & 0x3f) | 0x80);
  307. saa7111_write(client, 0x0e,
  308. (decoder->reg[0x0e] & 0x8f));
  309. break;
  310. default:
  311. return -EINVAL;
  312. }
  313. decoder->norm = *iarg;
  314. }
  315. break;
  316. case DECODER_SET_INPUT:
  317. {
  318. int *iarg = arg;
  319. if (*iarg < 0 || *iarg > 7) {
  320. return -EINVAL;
  321. }
  322. if (decoder->input != *iarg) {
  323. decoder->input = *iarg;
  324. /* select mode */
  325. saa7111_write(client, 0x02,
  326. (decoder->
  327. reg[0x02] & 0xf8) | decoder->input);
  328. /* bypass chrominance trap for modes 4..7 */
  329. saa7111_write(client, 0x09,
  330. (decoder->
  331. reg[0x09] & 0x7f) | ((decoder->
  332. input >
  333. 3) ? 0x80 :
  334. 0));
  335. }
  336. }
  337. break;
  338. case DECODER_SET_OUTPUT:
  339. {
  340. int *iarg = arg;
  341. /* not much choice of outputs */
  342. if (*iarg != 0) {
  343. return -EINVAL;
  344. }
  345. }
  346. break;
  347. case DECODER_ENABLE_OUTPUT:
  348. {
  349. int *iarg = arg;
  350. int enable = (*iarg != 0);
  351. if (decoder->enable != enable) {
  352. decoder->enable = enable;
  353. /* RJ: If output should be disabled (for
  354. * playing videos), we also need a open PLL.
  355. * The input is set to 0 (where no input
  356. * source is connected), although this
  357. * is not necessary.
  358. *
  359. * If output should be enabled, we have to
  360. * reverse the above.
  361. */
  362. if (decoder->enable) {
  363. saa7111_write(client, 0x02,
  364. (decoder->
  365. reg[0x02] & 0xf8) |
  366. decoder->input);
  367. saa7111_write(client, 0x08,
  368. (decoder->reg[0x08] & 0xfb));
  369. saa7111_write(client, 0x11,
  370. (decoder->
  371. reg[0x11] & 0xf3) | 0x0c);
  372. } else {
  373. saa7111_write(client, 0x02,
  374. (decoder->reg[0x02] & 0xf8));
  375. saa7111_write(client, 0x08,
  376. (decoder->
  377. reg[0x08] & 0xfb) | 0x04);
  378. saa7111_write(client, 0x11,
  379. (decoder->reg[0x11] & 0xf3));
  380. }
  381. }
  382. }
  383. break;
  384. case DECODER_SET_PICTURE:
  385. {
  386. struct video_picture *pic = arg;
  387. if (decoder->bright != pic->brightness) {
  388. /* We want 0 to 255 we get 0-65535 */
  389. decoder->bright = pic->brightness;
  390. saa7111_write(client, 0x0a, decoder->bright >> 8);
  391. }
  392. if (decoder->contrast != pic->contrast) {
  393. /* We want 0 to 127 we get 0-65535 */
  394. decoder->contrast = pic->contrast;
  395. saa7111_write(client, 0x0b,
  396. decoder->contrast >> 9);
  397. }
  398. if (decoder->sat != pic->colour) {
  399. /* We want 0 to 127 we get 0-65535 */
  400. decoder->sat = pic->colour;
  401. saa7111_write(client, 0x0c, decoder->sat >> 9);
  402. }
  403. if (decoder->hue != pic->hue) {
  404. /* We want -128 to 127 we get 0-65535 */
  405. decoder->hue = pic->hue;
  406. saa7111_write(client, 0x0d,
  407. (decoder->hue - 32768) >> 8);
  408. }
  409. }
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. /* ----------------------------------------------------------------------- */
  417. /*
  418. * Generic i2c probe
  419. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  420. */
  421. static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
  422. static unsigned short ignore = I2C_CLIENT_END;
  423. static struct i2c_client_address_data addr_data = {
  424. .normal_i2c = normal_i2c,
  425. .probe = &ignore,
  426. .ignore = &ignore,
  427. };
  428. static struct i2c_driver i2c_driver_saa7111;
  429. static int
  430. saa7111_detect_client (struct i2c_adapter *adapter,
  431. int address,
  432. int kind)
  433. {
  434. int i;
  435. struct i2c_client *client;
  436. struct saa7111 *decoder;
  437. struct video_decoder_init vdi;
  438. dprintk(1,
  439. KERN_INFO
  440. "saa7111.c: detecting saa7111 client on address 0x%x\n",
  441. address << 1);
  442. /* Check if the adapter supports the needed features */
  443. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  444. return 0;
  445. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  446. if (client == 0)
  447. return -ENOMEM;
  448. client->addr = address;
  449. client->adapter = adapter;
  450. client->driver = &i2c_driver_saa7111;
  451. strlcpy(I2C_NAME(client), "saa7111", sizeof(I2C_NAME(client)));
  452. decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
  453. if (decoder == NULL) {
  454. kfree(client);
  455. return -ENOMEM;
  456. }
  457. decoder->norm = VIDEO_MODE_NTSC;
  458. decoder->input = 0;
  459. decoder->enable = 1;
  460. decoder->bright = 32768;
  461. decoder->contrast = 32768;
  462. decoder->hue = 32768;
  463. decoder->sat = 32768;
  464. i2c_set_clientdata(client, decoder);
  465. i = i2c_attach_client(client);
  466. if (i) {
  467. kfree(client);
  468. kfree(decoder);
  469. return i;
  470. }
  471. vdi.data = saa7111_i2c_init;
  472. vdi.len = sizeof(saa7111_i2c_init);
  473. i = saa7111_init_decoder(client, &vdi);
  474. if (i < 0) {
  475. dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
  476. I2C_NAME(client), i);
  477. } else {
  478. dprintk(1,
  479. KERN_INFO
  480. "%s_attach: chip version %x at address 0x%x\n",
  481. I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
  482. client->addr << 1);
  483. }
  484. return 0;
  485. }
  486. static int
  487. saa7111_attach_adapter (struct i2c_adapter *adapter)
  488. {
  489. dprintk(1,
  490. KERN_INFO
  491. "saa7111.c: starting probe for adapter %s (0x%x)\n",
  492. I2C_NAME(adapter), adapter->id);
  493. return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
  494. }
  495. static int
  496. saa7111_detach_client (struct i2c_client *client)
  497. {
  498. struct saa7111 *decoder = i2c_get_clientdata(client);
  499. int err;
  500. err = i2c_detach_client(client);
  501. if (err) {
  502. return err;
  503. }
  504. kfree(decoder);
  505. kfree(client);
  506. return 0;
  507. }
  508. /* ----------------------------------------------------------------------- */
  509. static struct i2c_driver i2c_driver_saa7111 = {
  510. .driver = {
  511. .name = "saa7111",
  512. },
  513. .id = I2C_DRIVERID_SAA7111A,
  514. .attach_adapter = saa7111_attach_adapter,
  515. .detach_client = saa7111_detach_client,
  516. .command = saa7111_command,
  517. };
  518. static int __init
  519. saa7111_init (void)
  520. {
  521. return i2c_add_driver(&i2c_driver_saa7111);
  522. }
  523. static void __exit
  524. saa7111_exit (void)
  525. {
  526. i2c_del_driver(&i2c_driver_saa7111);
  527. }
  528. module_init(saa7111_init);
  529. module_exit(saa7111_exit);