saa7111.c 14 KB

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