saa7111.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 <linux/types.h>
  40. #include <linux/i2c.h>
  41. #include <asm/io.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/page.h>
  44. #include <asm/uaccess.h>
  45. #include <linux/videodev.h>
  46. #include <linux/video_decoder.h>
  47. MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
  48. MODULE_AUTHOR("Dave Perks");
  49. MODULE_LICENSE("GPL");
  50. #define I2C_NAME(s) (s)->name
  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. };
  67. #define I2C_SAA7111 0x48
  68. /* ----------------------------------------------------------------------- */
  69. static inline int
  70. saa7111_write (struct i2c_client *client,
  71. u8 reg,
  72. u8 value)
  73. {
  74. struct saa7111 *decoder = i2c_get_clientdata(client);
  75. decoder->reg[reg] = value;
  76. return i2c_smbus_write_byte_data(client, reg, value);
  77. }
  78. static inline void
  79. saa7111_write_if_changed(struct i2c_client *client, u8 reg, u8 value)
  80. {
  81. struct saa7111 *decoder = i2c_get_clientdata(client);
  82. if (decoder->reg[reg] != value) {
  83. decoder->reg[reg] = value;
  84. i2c_smbus_write_byte_data(client, reg, value);
  85. }
  86. }
  87. static int
  88. saa7111_write_block (struct i2c_client *client,
  89. const u8 *data,
  90. unsigned int len)
  91. {
  92. int ret = -1;
  93. u8 reg;
  94. /* the saa7111 has an autoincrement function, use it if
  95. * the adapter understands raw I2C */
  96. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  97. /* do raw I2C, not smbus compatible */
  98. struct saa7111 *decoder = i2c_get_clientdata(client);
  99. u8 block_data[32];
  100. int block_len;
  101. while (len >= 2) {
  102. block_len = 0;
  103. block_data[block_len++] = reg = data[0];
  104. do {
  105. block_data[block_len++] =
  106. decoder->reg[reg++] = data[1];
  107. len -= 2;
  108. data += 2;
  109. } while (len >= 2 && data[0] == reg &&
  110. block_len < 32);
  111. if ((ret = i2c_master_send(client, block_data,
  112. block_len)) < 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. break;
  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 < SAA7111_NR_REG; i += 16) {
  202. int j;
  203. printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
  204. for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++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. /* We want 0 to 255 we get 0-65535 */
  391. saa7111_write_if_changed(client, 0x0a, pic->brightness >> 8);
  392. /* We want 0 to 127 we get 0-65535 */
  393. saa7111_write(client, 0x0b, pic->contrast >> 9);
  394. /* We want 0 to 127 we get 0-65535 */
  395. saa7111_write(client, 0x0c, pic->colour >> 9);
  396. /* We want -128 to 127 we get 0-65535 */
  397. saa7111_write(client, 0x0d, (pic->hue - 32768) >> 8);
  398. }
  399. break;
  400. default:
  401. return -EINVAL;
  402. }
  403. return 0;
  404. }
  405. /* ----------------------------------------------------------------------- */
  406. /*
  407. * Generic i2c probe
  408. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  409. */
  410. static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
  411. static unsigned short ignore = I2C_CLIENT_END;
  412. static struct i2c_client_address_data addr_data = {
  413. .normal_i2c = normal_i2c,
  414. .probe = &ignore,
  415. .ignore = &ignore,
  416. };
  417. static struct i2c_driver i2c_driver_saa7111;
  418. static int
  419. saa7111_detect_client (struct i2c_adapter *adapter,
  420. int address,
  421. int kind)
  422. {
  423. int i;
  424. struct i2c_client *client;
  425. struct saa7111 *decoder;
  426. struct video_decoder_init vdi;
  427. dprintk(1,
  428. KERN_INFO
  429. "saa7111.c: detecting saa7111 client on address 0x%x\n",
  430. address << 1);
  431. /* Check if the adapter supports the needed features */
  432. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  433. return 0;
  434. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  435. if (client == 0)
  436. return -ENOMEM;
  437. client->addr = address;
  438. client->adapter = adapter;
  439. client->driver = &i2c_driver_saa7111;
  440. strlcpy(I2C_NAME(client), "saa7111", sizeof(I2C_NAME(client)));
  441. decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
  442. if (decoder == NULL) {
  443. kfree(client);
  444. return -ENOMEM;
  445. }
  446. decoder->norm = VIDEO_MODE_NTSC;
  447. decoder->input = 0;
  448. decoder->enable = 1;
  449. i2c_set_clientdata(client, decoder);
  450. i = i2c_attach_client(client);
  451. if (i) {
  452. kfree(client);
  453. kfree(decoder);
  454. return i;
  455. }
  456. vdi.data = saa7111_i2c_init;
  457. vdi.len = sizeof(saa7111_i2c_init);
  458. i = saa7111_init_decoder(client, &vdi);
  459. if (i < 0) {
  460. dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
  461. I2C_NAME(client), i);
  462. } else {
  463. dprintk(1,
  464. KERN_INFO
  465. "%s_attach: chip version %x at address 0x%x\n",
  466. I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
  467. client->addr << 1);
  468. }
  469. return 0;
  470. }
  471. static int
  472. saa7111_attach_adapter (struct i2c_adapter *adapter)
  473. {
  474. dprintk(1,
  475. KERN_INFO
  476. "saa7111.c: starting probe for adapter %s (0x%x)\n",
  477. I2C_NAME(adapter), adapter->id);
  478. return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
  479. }
  480. static int
  481. saa7111_detach_client (struct i2c_client *client)
  482. {
  483. struct saa7111 *decoder = i2c_get_clientdata(client);
  484. int err;
  485. err = i2c_detach_client(client);
  486. if (err) {
  487. return err;
  488. }
  489. kfree(decoder);
  490. kfree(client);
  491. return 0;
  492. }
  493. /* ----------------------------------------------------------------------- */
  494. static struct i2c_driver i2c_driver_saa7111 = {
  495. .driver = {
  496. .name = "saa7111",
  497. },
  498. .id = I2C_DRIVERID_SAA7111A,
  499. .attach_adapter = saa7111_attach_adapter,
  500. .detach_client = saa7111_detach_client,
  501. .command = saa7111_command,
  502. };
  503. static int __init
  504. saa7111_init (void)
  505. {
  506. return i2c_add_driver(&i2c_driver_saa7111);
  507. }
  508. static void __exit
  509. saa7111_exit (void)
  510. {
  511. i2c_del_driver(&i2c_driver_saa7111);
  512. }
  513. module_init(saa7111_init);
  514. module_exit(saa7111_exit);