saa711x.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * saa7111 - Philips SAA7113A 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 <asm/uaccess.h>
  47. #include <linux/videodev.h>
  48. MODULE_DESCRIPTION("Philips SAA7113 video decoder driver");
  49. MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden");
  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_PARM(debug, "i");
  57. MODULE_PARM_DESC(debug, " Set the default Debug level. Default: 0 (Off) - (0-1)");
  58. #define dprintk(num, format, args...) \
  59. do { \
  60. if (debug >= num) \
  61. printk(format , ##args); \
  62. } while (0)
  63. /* ----------------------------------------------------------------------- */
  64. struct saa7113 {
  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_SAA7113 0x4A
  75. /* ----------------------------------------------------------------------- */
  76. static inline int
  77. saa7113_write (struct i2c_client *client,
  78. u8 reg,
  79. u8 value)
  80. {
  81. struct saa7113 *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. saa7113_write_block (struct i2c_client *client,
  87. const u8 *data,
  88. unsigned int len)
  89. {
  90. int ret = -1;
  91. u8 reg;
  92. /* the saa7113 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 saa7113 *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 = saa7113_write(client, reg,
  121. *data++)) < 0)
  122. break;
  123. len -= 2;
  124. }
  125. }
  126. return ret;
  127. }
  128. static int
  129. saa7113_init_decoder (struct i2c_client *client,
  130. struct video_decoder_init *init)
  131. {
  132. return saa7113_write_block(client, init->data, init->len);
  133. }
  134. static inline int
  135. saa7113_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 saa7113_i2c_init[] = {
  142. 0x00, 0x00, /* PH7113_CHIP_VERSION 00 - ID byte */
  143. 0x01, 0x08, /* PH7113_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */
  144. 0x02, 0xc0, /* PH7113_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */
  145. 0x03, 0x23, /* PH7113_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */
  146. 0x04, 0x00, /* PH7113_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */
  147. 0x05, 0x00, /* PH7113_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */
  148. 0x06, 0xeb, /* PH7113_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */
  149. 0x07, 0xe0, /* PH7113_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */
  150. 0x08, 0x88, /* PH7113_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */
  151. 0x09, 0x00, /* PH7113_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */
  152. 0x0a, 0x80, /* PH7113_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */
  153. 0x0b, 0x47, /* PH7113_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */
  154. 0x0c, 0x40, /* PH7113_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */
  155. 0x0d, 0x00, /* PH7113_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */
  156. 0x0e, 0x01, /* PH7113_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */
  157. 0x0f, 0xaa, /* PH7113_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */
  158. 0x10, 0x00, /* PH7113_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */
  159. 0x11, 0x1C, /* PH7113_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */
  160. 0x12, 0x01, /* PH7113_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */
  161. 0x13, 0x00, /* PH7113_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */
  162. 0x14, 0x00, /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */
  163. 0x15, 0x00, /* PH7113_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */
  164. 0x16, 0x00, /* PH7113_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */
  165. 0x17, 0x00, /* PH7113_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */
  166. };
  167. static int
  168. saa7113_command (struct i2c_client *client,
  169. unsigned int cmd,
  170. void *arg)
  171. {
  172. struct saa7113 *decoder = i2c_get_clientdata(client);
  173. switch (cmd) {
  174. case 0:
  175. case DECODER_INIT:
  176. {
  177. struct video_decoder_init *init = arg;
  178. if (NULL != init)
  179. return saa7113_init_decoder(client, init);
  180. else {
  181. struct video_decoder_init vdi;
  182. vdi.data = saa7113_i2c_init;
  183. vdi.len = sizeof(saa7113_i2c_init);
  184. return saa7113_init_decoder(client, &vdi);
  185. }
  186. }
  187. case DECODER_DUMP:
  188. {
  189. int i;
  190. for (i = 0; i < 32; i += 16) {
  191. int j;
  192. printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
  193. for (j = 0; j < 16; ++j) {
  194. printk(" %02x",
  195. saa7113_read(client, i + j));
  196. }
  197. printk("\n");
  198. }
  199. }
  200. break;
  201. case DECODER_GET_CAPABILITIES:
  202. {
  203. struct video_decoder_capability *cap = arg;
  204. cap->flags = VIDEO_DECODER_PAL |
  205. VIDEO_DECODER_NTSC |
  206. VIDEO_DECODER_SECAM |
  207. VIDEO_DECODER_AUTO |
  208. VIDEO_DECODER_CCIR;
  209. cap->inputs = 8;
  210. cap->outputs = 1;
  211. }
  212. break;
  213. case DECODER_GET_STATUS:
  214. {
  215. int *iarg = arg;
  216. int status;
  217. int res;
  218. status = saa7113_read(client, 0x1f);
  219. dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
  220. status);
  221. res = 0;
  222. if ((status & (1 << 6)) == 0) {
  223. res |= DECODER_STATUS_GOOD;
  224. }
  225. switch (decoder->norm) {
  226. case VIDEO_MODE_NTSC:
  227. res |= DECODER_STATUS_NTSC;
  228. break;
  229. case VIDEO_MODE_PAL:
  230. res |= DECODER_STATUS_PAL;
  231. break;
  232. case VIDEO_MODE_SECAM:
  233. res |= DECODER_STATUS_SECAM;
  234. break;
  235. default:
  236. case VIDEO_MODE_AUTO:
  237. if ((status & (1 << 5)) != 0) {
  238. res |= DECODER_STATUS_NTSC;
  239. } else {
  240. res |= DECODER_STATUS_PAL;
  241. }
  242. break;
  243. }
  244. if ((status & (1 << 0)) != 0) {
  245. res |= DECODER_STATUS_COLOR;
  246. }
  247. *iarg = res;
  248. }
  249. break;
  250. case DECODER_SET_GPIO:
  251. {
  252. int *iarg = arg;
  253. if (0 != *iarg) {
  254. saa7113_write(client, 0x11,
  255. (decoder->reg[0x11] | 0x80));
  256. } else {
  257. saa7113_write(client, 0x11,
  258. (decoder->reg[0x11] & 0x7f));
  259. }
  260. break;
  261. }
  262. case DECODER_SET_VBI_BYPASS:
  263. {
  264. int *iarg = arg;
  265. if (0 != *iarg) {
  266. saa7113_write(client, 0x13,
  267. (decoder->reg[0x13] & 0xf0) | 0x0a);
  268. } else {
  269. saa7113_write(client, 0x13,
  270. (decoder->reg[0x13] & 0xf0));
  271. }
  272. break;
  273. }
  274. case DECODER_SET_NORM:
  275. {
  276. int *iarg = arg;
  277. switch (*iarg) {
  278. case VIDEO_MODE_NTSC:
  279. saa7113_write(client, 0x08,
  280. (decoder->reg[0x08] & 0x3f) | 0x40);
  281. saa7113_write(client, 0x0e,
  282. (decoder->reg[0x0e] & 0x8f));
  283. break;
  284. case VIDEO_MODE_PAL:
  285. saa7113_write(client, 0x08,
  286. (decoder->reg[0x08] & 0x3f) | 0x00);
  287. saa7113_write(client, 0x0e,
  288. (decoder->reg[0x0e] & 0x8f));
  289. break;
  290. case VIDEO_MODE_SECAM:
  291. saa7113_write(client, 0x08,
  292. (decoder->reg[0x0e] & 0x3f) | 0x00);
  293. saa7113_write(client, 0x0e,
  294. (decoder->reg[0x0e] & 0x8f) | 0x50);
  295. break;
  296. case VIDEO_MODE_AUTO:
  297. saa7113_write(client, 0x08,
  298. (decoder->reg[0x08] & 0x3f) | 0x80);
  299. saa7113_write(client, 0x0e,
  300. (decoder->reg[0x0e] & 0x8f));
  301. break;
  302. default:
  303. return -EINVAL;
  304. }
  305. decoder->norm = *iarg;
  306. }
  307. break;
  308. case DECODER_SET_INPUT:
  309. {
  310. int *iarg = arg;
  311. if (*iarg < 0 || *iarg > 9) {
  312. return -EINVAL;
  313. }
  314. if (decoder->input != *iarg) {
  315. decoder->input = *iarg;
  316. /* select mode */
  317. saa7113_write(client, 0x02,
  318. (decoder->reg[0x02] & 0xf0) | decoder->input);
  319. /* bypass chrominance trap for modes 4..7 */
  320. saa7113_write(client, 0x09,
  321. (decoder->reg[0x09] & 0x7f) | ((decoder->input > 3) ? 0x80 : 0));
  322. }
  323. }
  324. break;
  325. case DECODER_SET_OUTPUT:
  326. {
  327. int *iarg = arg;
  328. /* not much choice of outputs */
  329. if (*iarg != 0) {
  330. return -EINVAL;
  331. }
  332. }
  333. break;
  334. case DECODER_ENABLE_OUTPUT:
  335. {
  336. int *iarg = arg;
  337. int enable = (*iarg != 0);
  338. if (decoder->enable != enable) {
  339. decoder->enable = enable;
  340. /* RJ: If output should be disabled (for
  341. * playing videos), we also need a open PLL.
  342. * The input is set to 0 (where no input
  343. * source is connected), although this
  344. * is not necessary.
  345. *
  346. * If output should be enabled, we have to
  347. * reverse the above.
  348. */
  349. if (decoder->enable) {
  350. saa7113_write(client, 0x02,
  351. (decoder->
  352. reg[0x02] & 0xf8) |
  353. decoder->input);
  354. saa7113_write(client, 0x08,
  355. (decoder->reg[0x08] & 0xfb));
  356. saa7113_write(client, 0x11,
  357. (decoder->
  358. reg[0x11] & 0xf3) | 0x0c);
  359. } else {
  360. saa7113_write(client, 0x02,
  361. (decoder->reg[0x02] & 0xf8));
  362. saa7113_write(client, 0x08,
  363. (decoder->
  364. reg[0x08] & 0xfb) | 0x04);
  365. saa7113_write(client, 0x11,
  366. (decoder->reg[0x11] & 0xf3));
  367. }
  368. }
  369. }
  370. break;
  371. case DECODER_SET_PICTURE:
  372. {
  373. struct video_picture *pic = arg;
  374. if (decoder->bright != pic->brightness) {
  375. /* We want 0 to 255 we get 0-65535 */
  376. decoder->bright = pic->brightness;
  377. saa7113_write(client, 0x0a, decoder->bright >> 8);
  378. }
  379. if (decoder->contrast != pic->contrast) {
  380. /* We want 0 to 127 we get 0-65535 */
  381. decoder->contrast = pic->contrast;
  382. saa7113_write(client, 0x0b,
  383. decoder->contrast >> 9);
  384. }
  385. if (decoder->sat != pic->colour) {
  386. /* We want 0 to 127 we get 0-65535 */
  387. decoder->sat = pic->colour;
  388. saa7113_write(client, 0x0c, decoder->sat >> 9);
  389. }
  390. if (decoder->hue != pic->hue) {
  391. /* We want -128 to 127 we get 0-65535 */
  392. decoder->hue = pic->hue;
  393. saa7113_write(client, 0x0d,
  394. (decoder->hue - 32768) >> 8);
  395. }
  396. }
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. return 0;
  402. }
  403. /* ----------------------------------------------------------------------- */
  404. /*
  405. * Generic i2c probe
  406. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  407. */
  408. /* standard i2c insmod options */
  409. static unsigned short normal_i2c[] = {
  410. I2C_SAA7113>>1, /* saa7113 */
  411. I2C_CLIENT_END
  412. };
  413. I2C_CLIENT_INSMOD;
  414. static struct i2c_driver i2c_driver_saa7113;
  415. static int
  416. saa7113_detect_client (struct i2c_adapter *adapter,
  417. int address,
  418. int kind)
  419. {
  420. int i;
  421. struct i2c_client *client;
  422. struct saa7113 *decoder;
  423. struct video_decoder_init vdi;
  424. dprintk(1,
  425. KERN_INFO
  426. "saa7113.c: detecting saa7113 client on address 0x%x\n",
  427. address << 1);
  428. /* Check if the adapter supports the needed features */
  429. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  430. return 0;
  431. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  432. if (client == 0)
  433. return -ENOMEM;
  434. memset(client, 0, sizeof(struct i2c_client));
  435. client->addr = address;
  436. client->adapter = adapter;
  437. client->driver = &i2c_driver_saa7113;
  438. client->flags = I2C_CLIENT_ALLOW_USE;
  439. strlcpy(I2C_NAME(client), "saa7113", sizeof(I2C_NAME(client)));
  440. decoder = kmalloc(sizeof(struct saa7113), GFP_KERNEL);
  441. if (decoder == NULL) {
  442. kfree(client);
  443. return -ENOMEM;
  444. }
  445. memset(decoder, 0, sizeof(struct saa7113));
  446. decoder->norm = VIDEO_MODE_NTSC;
  447. decoder->input = 0;
  448. decoder->enable = 1;
  449. decoder->bright = 32768;
  450. decoder->contrast = 32768;
  451. decoder->hue = 32768;
  452. decoder->sat = 32768;
  453. i2c_set_clientdata(client, decoder);
  454. i = i2c_attach_client(client);
  455. if (i) {
  456. kfree(client);
  457. kfree(decoder);
  458. return i;
  459. }
  460. vdi.data = saa7113_i2c_init;
  461. vdi.len = sizeof(saa7113_i2c_init);
  462. i = saa7113_init_decoder(client, &vdi);
  463. if (i < 0) {
  464. dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
  465. I2C_NAME(client), i);
  466. } else {
  467. dprintk(1,
  468. KERN_INFO
  469. "%s_attach: chip version %x at address 0x%x\n",
  470. I2C_NAME(client), saa7113_read(client, 0x00) >> 4,
  471. client->addr << 1);
  472. }
  473. return 0;
  474. }
  475. static int
  476. saa7113_attach_adapter (struct i2c_adapter *adapter)
  477. {
  478. dprintk(1,
  479. KERN_INFO
  480. "saa7113.c: starting probe for adapter %s (0x%x)\n",
  481. I2C_NAME(adapter), adapter->id);
  482. return i2c_probe(adapter, &addr_data, &saa7113_detect_client);
  483. }
  484. static int
  485. saa7113_detach_client (struct i2c_client *client)
  486. {
  487. struct saa7113 *decoder = i2c_get_clientdata(client);
  488. int err;
  489. err = i2c_detach_client(client);
  490. if (err) {
  491. return err;
  492. }
  493. kfree(decoder);
  494. kfree(client);
  495. return 0;
  496. }
  497. /* ----------------------------------------------------------------------- */
  498. static struct i2c_driver i2c_driver_saa7113 = {
  499. .owner = THIS_MODULE,
  500. .name = "saa7113",
  501. .id = I2C_DRIVERID_SAA7113,
  502. .flags = I2C_DF_NOTIFY,
  503. .attach_adapter = saa7113_attach_adapter,
  504. .detach_client = saa7113_detach_client,
  505. .command = saa7113_command,
  506. };
  507. static int __init
  508. saa7113_init (void)
  509. {
  510. return i2c_add_driver(&i2c_driver_saa7113);
  511. }
  512. static void __exit
  513. saa7113_exit (void)
  514. {
  515. i2c_del_driver(&i2c_driver_saa7113);
  516. }
  517. module_init(saa7113_init);
  518. module_exit(saa7113_exit);