saa7111.c 14 KB

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