saa7111.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. #include <linux/i2c-dev.h>
  52. #define I2C_NAME(s) (s)->name
  53. #include <linux/video_decoder.h>
  54. static int debug = 0;
  55. module_param(debug, int, 0644);
  56. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  57. #define dprintk(num, format, args...) \
  58. do { \
  59. if (debug >= num) \
  60. printk(format, ##args); \
  61. } while (0)
  62. /* ----------------------------------------------------------------------- */
  63. struct saa7111 {
  64. unsigned char reg[32];
  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. struct i2c_msg msg;
  97. u8 block_data[32];
  98. msg.addr = client->addr;
  99. msg.flags = 0;
  100. while (len >= 2) {
  101. msg.buf = (char *) block_data;
  102. msg.len = 0;
  103. block_data[msg.len++] = reg = data[0];
  104. do {
  105. block_data[msg.len++] =
  106. decoder->reg[reg++] = data[1];
  107. len -= 2;
  108. data += 2;
  109. } while (len >= 2 && data[0] == reg &&
  110. msg.len < 32);
  111. if ((ret = i2c_transfer(client->adapter,
  112. &msg, 1)) < 0)
  113. break;
  114. }
  115. } else {
  116. /* do some slow I2C emulation kind of thing */
  117. while (len >= 2) {
  118. reg = *data++;
  119. if ((ret = saa7111_write(client, reg,
  120. *data++)) < 0)
  121. break;
  122. len -= 2;
  123. }
  124. }
  125. return ret;
  126. }
  127. static int
  128. saa7111_init_decoder (struct i2c_client *client,
  129. struct video_decoder_init *init)
  130. {
  131. return saa7111_write_block(client, init->data, init->len);
  132. }
  133. static inline int
  134. saa7111_read (struct i2c_client *client,
  135. u8 reg)
  136. {
  137. return i2c_smbus_read_byte_data(client, reg);
  138. }
  139. /* ----------------------------------------------------------------------- */
  140. static const unsigned char saa7111_i2c_init[] = {
  141. 0x00, 0x00, /* 00 - ID byte */
  142. 0x01, 0x00, /* 01 - reserved */
  143. /*front end */
  144. 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
  145. 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
  146. * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
  147. 0x04, 0x00, /* 04 - GAI1=256 */
  148. 0x05, 0x00, /* 05 - GAI2=256 */
  149. /* decoder */
  150. 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
  151. * pixels after end of last line */
  152. /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
  153. * after end of last line */
  154. 0x07, 0xe8, /* 07 - HSS seems to be needed to
  155. * work with NTSC, too */
  156. 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
  157. * VTRC=1, HPLL=0, VNOI=0 */
  158. 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
  159. * VBLB=0, UPTCV=0, APER=1 */
  160. 0x0a, 0x80, /* 0a - BRIG=128 */
  161. 0x0b, 0x47, /* 0b - CONT=1.109 */
  162. 0x0c, 0x40, /* 0c - SATN=1.0 */
  163. 0x0d, 0x00, /* 0d - HUE=0 */
  164. 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
  165. * FCTC=0, CHBW=1 */
  166. 0x0f, 0x00, /* 0f - reserved */
  167. 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
  168. 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
  169. * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
  170. 0x12, 0x00, /* 12 - output control 2 */
  171. 0x13, 0x00, /* 13 - output control 3 */
  172. 0x14, 0x00, /* 14 - reserved */
  173. 0x15, 0x00, /* 15 - VBI */
  174. 0x16, 0x00, /* 16 - VBI */
  175. 0x17, 0x00, /* 17 - VBI */
  176. };
  177. static int
  178. saa7111_command (struct i2c_client *client,
  179. unsigned int cmd,
  180. void *arg)
  181. {
  182. struct saa7111 *decoder = i2c_get_clientdata(client);
  183. switch (cmd) {
  184. case 0:
  185. case DECODER_INIT:
  186. {
  187. struct video_decoder_init *init = arg;
  188. if (NULL != init)
  189. return saa7111_init_decoder(client, init);
  190. else {
  191. struct video_decoder_init vdi;
  192. vdi.data = saa7111_i2c_init;
  193. vdi.len = sizeof(saa7111_i2c_init);
  194. return saa7111_init_decoder(client, &vdi);
  195. }
  196. }
  197. case DECODER_DUMP:
  198. {
  199. int i;
  200. for (i = 0; i < 32; i += 16) {
  201. int j;
  202. printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
  203. for (j = 0; j < 16; ++j) {
  204. printk(" %02x",
  205. saa7111_read(client, i + j));
  206. }
  207. printk("\n");
  208. }
  209. }
  210. break;
  211. case DECODER_GET_CAPABILITIES:
  212. {
  213. struct video_decoder_capability *cap = arg;
  214. cap->flags = VIDEO_DECODER_PAL |
  215. VIDEO_DECODER_NTSC |
  216. VIDEO_DECODER_SECAM |
  217. VIDEO_DECODER_AUTO |
  218. VIDEO_DECODER_CCIR;
  219. cap->inputs = 8;
  220. cap->outputs = 1;
  221. }
  222. break;
  223. case DECODER_GET_STATUS:
  224. {
  225. int *iarg = arg;
  226. int status;
  227. int res;
  228. status = saa7111_read(client, 0x1f);
  229. dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
  230. status);
  231. res = 0;
  232. if ((status & (1 << 6)) == 0) {
  233. res |= DECODER_STATUS_GOOD;
  234. }
  235. switch (decoder->norm) {
  236. case VIDEO_MODE_NTSC:
  237. res |= DECODER_STATUS_NTSC;
  238. break;
  239. case VIDEO_MODE_PAL:
  240. res |= DECODER_STATUS_PAL;
  241. break;
  242. case VIDEO_MODE_SECAM:
  243. res |= DECODER_STATUS_SECAM;
  244. break;
  245. default:
  246. case VIDEO_MODE_AUTO:
  247. if ((status & (1 << 5)) != 0) {
  248. res |= DECODER_STATUS_NTSC;
  249. } else {
  250. res |= DECODER_STATUS_PAL;
  251. }
  252. break;
  253. }
  254. if ((status & (1 << 0)) != 0) {
  255. res |= DECODER_STATUS_COLOR;
  256. }
  257. *iarg = res;
  258. }
  259. break;
  260. case DECODER_SET_GPIO:
  261. {
  262. int *iarg = arg;
  263. if (0 != *iarg) {
  264. saa7111_write(client, 0x11,
  265. (decoder->reg[0x11] | 0x80));
  266. } else {
  267. saa7111_write(client, 0x11,
  268. (decoder->reg[0x11] & 0x7f));
  269. }
  270. break;
  271. }
  272. case DECODER_SET_VBI_BYPASS:
  273. {
  274. int *iarg = arg;
  275. if (0 != *iarg) {
  276. saa7111_write(client, 0x13,
  277. (decoder->reg[0x13] & 0xf0) | 0x0a);
  278. } else {
  279. saa7111_write(client, 0x13,
  280. (decoder->reg[0x13] & 0xf0));
  281. }
  282. break;
  283. }
  284. case DECODER_SET_NORM:
  285. {
  286. int *iarg = arg;
  287. switch (*iarg) {
  288. case VIDEO_MODE_NTSC:
  289. saa7111_write(client, 0x08,
  290. (decoder->reg[0x08] & 0x3f) | 0x40);
  291. saa7111_write(client, 0x0e,
  292. (decoder->reg[0x0e] & 0x8f));
  293. break;
  294. case VIDEO_MODE_PAL:
  295. saa7111_write(client, 0x08,
  296. (decoder->reg[0x08] & 0x3f) | 0x00);
  297. saa7111_write(client, 0x0e,
  298. (decoder->reg[0x0e] & 0x8f));
  299. break;
  300. case VIDEO_MODE_SECAM:
  301. saa7111_write(client, 0x08,
  302. (decoder->reg[0x08] & 0x3f) | 0x00);
  303. saa7111_write(client, 0x0e,
  304. (decoder->reg[0x0e] & 0x8f) | 0x50);
  305. break;
  306. case VIDEO_MODE_AUTO:
  307. saa7111_write(client, 0x08,
  308. (decoder->reg[0x08] & 0x3f) | 0x80);
  309. saa7111_write(client, 0x0e,
  310. (decoder->reg[0x0e] & 0x8f));
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. decoder->norm = *iarg;
  316. }
  317. break;
  318. case DECODER_SET_INPUT:
  319. {
  320. int *iarg = arg;
  321. if (*iarg < 0 || *iarg > 7) {
  322. return -EINVAL;
  323. }
  324. if (decoder->input != *iarg) {
  325. decoder->input = *iarg;
  326. /* select mode */
  327. saa7111_write(client, 0x02,
  328. (decoder->
  329. reg[0x02] & 0xf8) | decoder->input);
  330. /* bypass chrominance trap for modes 4..7 */
  331. saa7111_write(client, 0x09,
  332. (decoder->
  333. reg[0x09] & 0x7f) | ((decoder->
  334. input >
  335. 3) ? 0x80 :
  336. 0));
  337. }
  338. }
  339. break;
  340. case DECODER_SET_OUTPUT:
  341. {
  342. int *iarg = arg;
  343. /* not much choice of outputs */
  344. if (*iarg != 0) {
  345. return -EINVAL;
  346. }
  347. }
  348. break;
  349. case DECODER_ENABLE_OUTPUT:
  350. {
  351. int *iarg = arg;
  352. int enable = (*iarg != 0);
  353. if (decoder->enable != enable) {
  354. decoder->enable = enable;
  355. /* RJ: If output should be disabled (for
  356. * playing videos), we also need a open PLL.
  357. * The input is set to 0 (where no input
  358. * source is connected), although this
  359. * is not necessary.
  360. *
  361. * If output should be enabled, we have to
  362. * reverse the above.
  363. */
  364. if (decoder->enable) {
  365. saa7111_write(client, 0x02,
  366. (decoder->
  367. reg[0x02] & 0xf8) |
  368. decoder->input);
  369. saa7111_write(client, 0x08,
  370. (decoder->reg[0x08] & 0xfb));
  371. saa7111_write(client, 0x11,
  372. (decoder->
  373. reg[0x11] & 0xf3) | 0x0c);
  374. } else {
  375. saa7111_write(client, 0x02,
  376. (decoder->reg[0x02] & 0xf8));
  377. saa7111_write(client, 0x08,
  378. (decoder->
  379. reg[0x08] & 0xfb) | 0x04);
  380. saa7111_write(client, 0x11,
  381. (decoder->reg[0x11] & 0xf3));
  382. }
  383. }
  384. }
  385. break;
  386. case DECODER_SET_PICTURE:
  387. {
  388. struct video_picture *pic = arg;
  389. if (decoder->bright != pic->brightness) {
  390. /* We want 0 to 255 we get 0-65535 */
  391. decoder->bright = pic->brightness;
  392. saa7111_write(client, 0x0a, decoder->bright >> 8);
  393. }
  394. if (decoder->contrast != pic->contrast) {
  395. /* We want 0 to 127 we get 0-65535 */
  396. decoder->contrast = pic->contrast;
  397. saa7111_write(client, 0x0b,
  398. decoder->contrast >> 9);
  399. }
  400. if (decoder->sat != pic->colour) {
  401. /* We want 0 to 127 we get 0-65535 */
  402. decoder->sat = pic->colour;
  403. saa7111_write(client, 0x0c, decoder->sat >> 9);
  404. }
  405. if (decoder->hue != pic->hue) {
  406. /* We want -128 to 127 we get 0-65535 */
  407. decoder->hue = pic->hue;
  408. saa7111_write(client, 0x0d,
  409. (decoder->hue - 32768) >> 8);
  410. }
  411. }
  412. break;
  413. default:
  414. return -EINVAL;
  415. }
  416. return 0;
  417. }
  418. /* ----------------------------------------------------------------------- */
  419. /*
  420. * Generic i2c probe
  421. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  422. */
  423. static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
  424. static unsigned short ignore = I2C_CLIENT_END;
  425. static struct i2c_client_address_data addr_data = {
  426. .normal_i2c = normal_i2c,
  427. .probe = &ignore,
  428. .ignore = &ignore,
  429. };
  430. static struct i2c_driver i2c_driver_saa7111;
  431. static int
  432. saa7111_detect_client (struct i2c_adapter *adapter,
  433. int address,
  434. int kind)
  435. {
  436. int i;
  437. struct i2c_client *client;
  438. struct saa7111 *decoder;
  439. struct video_decoder_init vdi;
  440. dprintk(1,
  441. KERN_INFO
  442. "saa7111.c: detecting saa7111 client on address 0x%x\n",
  443. address << 1);
  444. /* Check if the adapter supports the needed features */
  445. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  446. return 0;
  447. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  448. if (client == 0)
  449. return -ENOMEM;
  450. memset(client, 0, sizeof(struct i2c_client));
  451. client->addr = address;
  452. client->adapter = adapter;
  453. client->driver = &i2c_driver_saa7111;
  454. client->flags = I2C_CLIENT_ALLOW_USE;
  455. strlcpy(I2C_NAME(client), "saa7111", sizeof(I2C_NAME(client)));
  456. decoder = kmalloc(sizeof(struct saa7111), GFP_KERNEL);
  457. if (decoder == NULL) {
  458. kfree(client);
  459. return -ENOMEM;
  460. }
  461. memset(decoder, 0, sizeof(struct saa7111));
  462. decoder->norm = VIDEO_MODE_NTSC;
  463. decoder->input = 0;
  464. decoder->enable = 1;
  465. decoder->bright = 32768;
  466. decoder->contrast = 32768;
  467. decoder->hue = 32768;
  468. decoder->sat = 32768;
  469. i2c_set_clientdata(client, decoder);
  470. i = i2c_attach_client(client);
  471. if (i) {
  472. kfree(client);
  473. kfree(decoder);
  474. return i;
  475. }
  476. vdi.data = saa7111_i2c_init;
  477. vdi.len = sizeof(saa7111_i2c_init);
  478. i = saa7111_init_decoder(client, &vdi);
  479. if (i < 0) {
  480. dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
  481. I2C_NAME(client), i);
  482. } else {
  483. dprintk(1,
  484. KERN_INFO
  485. "%s_attach: chip version %x at address 0x%x\n",
  486. I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
  487. client->addr << 1);
  488. }
  489. return 0;
  490. }
  491. static int
  492. saa7111_attach_adapter (struct i2c_adapter *adapter)
  493. {
  494. dprintk(1,
  495. KERN_INFO
  496. "saa7111.c: starting probe for adapter %s (0x%x)\n",
  497. I2C_NAME(adapter), adapter->id);
  498. return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
  499. }
  500. static int
  501. saa7111_detach_client (struct i2c_client *client)
  502. {
  503. struct saa7111 *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_saa7111 = {
  515. .owner = THIS_MODULE,
  516. .name = "saa7111",
  517. .id = I2C_DRIVERID_SAA7111A,
  518. .flags = I2C_DF_NOTIFY,
  519. .attach_adapter = saa7111_attach_adapter,
  520. .detach_client = saa7111_detach_client,
  521. .command = saa7111_command,
  522. };
  523. static int __init
  524. saa7111_init (void)
  525. {
  526. return i2c_add_driver(&i2c_driver_saa7111);
  527. }
  528. static void __exit
  529. saa7111_exit (void)
  530. {
  531. i2c_del_driver(&i2c_driver_saa7111);
  532. }
  533. module_init(saa7111_init);
  534. module_exit(saa7111_exit);