saa711x.c 15 KB

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