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