saa711x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * saa711x - Philips SAA711x video decoder driver version 0.0.1
  3. *
  4. * To do: Now, it handles only saa7113/7114. Should be improved to
  5. * handle all Philips saa711x devices.
  6. *
  7. * Based on saa7113 driver from Dave Perks <dperks@ibm.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/errno.h>
  27. #include <linux/fs.h>
  28. #include <linux/kernel.h>
  29. #include <linux/major.h>
  30. #include <linux/slab.h>
  31. #include <linux/mm.h>
  32. #include <linux/pci.h>
  33. #include <linux/signal.h>
  34. #include <asm/io.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/page.h>
  37. #include <linux/sched.h>
  38. #include <linux/types.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/videodev.h>
  41. MODULE_DESCRIPTION("Philips SAA711x video decoder driver");
  42. MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden");
  43. MODULE_LICENSE("GPL");
  44. #include <linux/i2c.h>
  45. #include <linux/i2c-dev.h>
  46. #define I2C_NAME(s) (s)->name
  47. #include <linux/video_decoder.h>
  48. static int debug = 0;
  49. module_param(debug, int, 0644);
  50. MODULE_PARM_DESC(debug, " Set the default Debug level. Default: 0 (Off) - (0-1)");
  51. #define dprintk(num, format, args...) \
  52. do { \
  53. if (debug >= num) \
  54. printk(format, ##args); \
  55. } while (0)
  56. /* ----------------------------------------------------------------------- */
  57. struct saa711x {
  58. unsigned char reg[32];
  59. int norm;
  60. int input;
  61. int enable;
  62. int bright;
  63. int contrast;
  64. int hue;
  65. int sat;
  66. };
  67. #define I2C_SAA7113 0x4A
  68. #define I2C_SAA7114 0x42
  69. /* ----------------------------------------------------------------------- */
  70. static inline int
  71. saa711x_write (struct i2c_client *client,
  72. u8 reg,
  73. u8 value)
  74. {
  75. struct saa711x *decoder = i2c_get_clientdata(client);
  76. decoder->reg[reg] = value;
  77. return i2c_smbus_write_byte_data(client, reg, value);
  78. }
  79. static int
  80. saa711x_write_block (struct i2c_client *client,
  81. const u8 *data,
  82. unsigned int len)
  83. {
  84. int ret = -1;
  85. u8 reg;
  86. /* the saa711x has an autoincrement function, use it if
  87. * the adapter understands raw I2C */
  88. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  89. /* do raw I2C, not smbus compatible */
  90. struct saa711x *decoder = i2c_get_clientdata(client);
  91. struct i2c_msg msg;
  92. u8 block_data[32];
  93. msg.addr = client->addr;
  94. msg.flags = 0;
  95. while (len >= 2) {
  96. msg.buf = (char *) block_data;
  97. msg.len = 0;
  98. block_data[msg.len++] = reg = data[0];
  99. do {
  100. block_data[msg.len++] =
  101. decoder->reg[reg++] = data[1];
  102. len -= 2;
  103. data += 2;
  104. } while (len >= 2 && data[0] == reg &&
  105. msg.len < 32);
  106. if ((ret = i2c_transfer(client->adapter,
  107. &msg, 1)) < 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 = saa711x_write(client, reg,
  115. *data++)) < 0)
  116. break;
  117. len -= 2;
  118. }
  119. }
  120. return ret;
  121. }
  122. static int
  123. saa711x_init_decoder (struct i2c_client *client,
  124. struct video_decoder_init *init)
  125. {
  126. return saa711x_write_block(client, init->data, init->len);
  127. }
  128. static inline int
  129. saa711x_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 saa711x_i2c_init[] = {
  136. 0x00, 0x00, /* PH711x_CHIP_VERSION 00 - ID byte */
  137. 0x01, 0x08, /* PH711x_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */
  138. 0x02, 0xc0, /* PH711x_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */
  139. 0x03, 0x23, /* PH711x_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */
  140. 0x04, 0x00, /* PH711x_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */
  141. 0x05, 0x00, /* PH711x_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */
  142. 0x06, 0xeb, /* PH711x_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */
  143. 0x07, 0xe0, /* PH711x_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */
  144. 0x08, 0x88, /* PH711x_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */
  145. 0x09, 0x00, /* PH711x_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */
  146. 0x0a, 0x80, /* PH711x_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */
  147. 0x0b, 0x47, /* PH711x_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */
  148. 0x0c, 0x40, /* PH711x_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */
  149. 0x0d, 0x00, /* PH711x_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */
  150. 0x0e, 0x01, /* PH711x_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */
  151. 0x0f, 0xaa, /* PH711x_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */
  152. 0x10, 0x00, /* PH711x_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */
  153. 0x11, 0x1C, /* PH711x_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */
  154. 0x12, 0x01, /* PH711x_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */
  155. 0x13, 0x00, /* PH711x_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */
  156. 0x14, 0x00, /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */
  157. 0x15, 0x00, /* PH711x_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */
  158. 0x16, 0x00, /* PH711x_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */
  159. 0x17, 0x00, /* PH711x_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */
  160. };
  161. static int
  162. saa711x_command (struct i2c_client *client,
  163. unsigned int cmd,
  164. void *arg)
  165. {
  166. struct saa711x *decoder = i2c_get_clientdata(client);
  167. switch (cmd) {
  168. case 0:
  169. case DECODER_INIT:
  170. {
  171. struct video_decoder_init *init = arg;
  172. if (NULL != init)
  173. return saa711x_init_decoder(client, init);
  174. else {
  175. struct video_decoder_init vdi;
  176. vdi.data = saa711x_i2c_init;
  177. vdi.len = sizeof(saa711x_i2c_init);
  178. return saa711x_init_decoder(client, &vdi);
  179. }
  180. }
  181. case DECODER_DUMP:
  182. {
  183. int i;
  184. for (i = 0; i < 32; i += 16) {
  185. int j;
  186. printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
  187. for (j = 0; j < 16; ++j) {
  188. printk(" %02x",
  189. saa711x_read(client, i + j));
  190. }
  191. printk("\n");
  192. }
  193. }
  194. break;
  195. case DECODER_GET_CAPABILITIES:
  196. {
  197. struct video_decoder_capability *cap = arg;
  198. cap->flags = VIDEO_DECODER_PAL |
  199. VIDEO_DECODER_NTSC |
  200. VIDEO_DECODER_SECAM |
  201. VIDEO_DECODER_AUTO |
  202. VIDEO_DECODER_CCIR;
  203. cap->inputs = 8;
  204. cap->outputs = 1;
  205. }
  206. break;
  207. case DECODER_GET_STATUS:
  208. {
  209. int *iarg = arg;
  210. int status;
  211. int res;
  212. status = saa711x_read(client, 0x1f);
  213. dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
  214. status);
  215. res = 0;
  216. if ((status & (1 << 6)) == 0) {
  217. res |= DECODER_STATUS_GOOD;
  218. }
  219. switch (decoder->norm) {
  220. case VIDEO_MODE_NTSC:
  221. res |= DECODER_STATUS_NTSC;
  222. break;
  223. case VIDEO_MODE_PAL:
  224. res |= DECODER_STATUS_PAL;
  225. break;
  226. case VIDEO_MODE_SECAM:
  227. res |= DECODER_STATUS_SECAM;
  228. break;
  229. default:
  230. case VIDEO_MODE_AUTO:
  231. if ((status & (1 << 5)) != 0) {
  232. res |= DECODER_STATUS_NTSC;
  233. } else {
  234. res |= DECODER_STATUS_PAL;
  235. }
  236. break;
  237. }
  238. if ((status & (1 << 0)) != 0) {
  239. res |= DECODER_STATUS_COLOR;
  240. }
  241. *iarg = res;
  242. }
  243. break;
  244. case DECODER_SET_GPIO:
  245. {
  246. int *iarg = arg;
  247. if (0 != *iarg) {
  248. saa711x_write(client, 0x11,
  249. (decoder->reg[0x11] | 0x80));
  250. } else {
  251. saa711x_write(client, 0x11,
  252. (decoder->reg[0x11] & 0x7f));
  253. }
  254. break;
  255. }
  256. case DECODER_SET_VBI_BYPASS:
  257. {
  258. int *iarg = arg;
  259. if (0 != *iarg) {
  260. saa711x_write(client, 0x13,
  261. (decoder->reg[0x13] & 0xf0) | 0x0a);
  262. } else {
  263. saa711x_write(client, 0x13,
  264. (decoder->reg[0x13] & 0xf0));
  265. }
  266. break;
  267. }
  268. case DECODER_SET_NORM:
  269. {
  270. int *iarg = arg;
  271. switch (*iarg) {
  272. case VIDEO_MODE_NTSC:
  273. saa711x_write(client, 0x08,
  274. (decoder->reg[0x08] & 0x3f) | 0x40);
  275. saa711x_write(client, 0x0e,
  276. (decoder->reg[0x0e] & 0x8f));
  277. break;
  278. case VIDEO_MODE_PAL:
  279. saa711x_write(client, 0x08,
  280. (decoder->reg[0x08] & 0x3f) | 0x00);
  281. saa711x_write(client, 0x0e,
  282. (decoder->reg[0x0e] & 0x8f));
  283. break;
  284. case VIDEO_MODE_SECAM:
  285. saa711x_write(client, 0x08,
  286. (decoder->reg[0x08] & 0x3f) | 0x00);
  287. saa711x_write(client, 0x0e,
  288. (decoder->reg[0x0e] & 0x8f) | 0x50);
  289. break;
  290. case VIDEO_MODE_AUTO:
  291. saa711x_write(client, 0x08,
  292. (decoder->reg[0x08] & 0x3f) | 0x80);
  293. saa711x_write(client, 0x0e,
  294. (decoder->reg[0x0e] & 0x8f));
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. decoder->norm = *iarg;
  300. }
  301. break;
  302. case DECODER_SET_INPUT:
  303. {
  304. int *iarg = arg;
  305. if (*iarg < 0 || *iarg > 9) {
  306. return -EINVAL;
  307. }
  308. if (decoder->input != *iarg) {
  309. decoder->input = *iarg;
  310. /* select mode */
  311. saa711x_write(client, 0x02,
  312. (decoder->reg[0x02] & 0xf0) | decoder->input);
  313. /* bypass chrominance trap for modes 4..7 */
  314. saa711x_write(client, 0x09,
  315. (decoder->reg[0x09] & 0x7f) | ((decoder->input > 3) ? 0x80 : 0));
  316. }
  317. }
  318. break;
  319. case DECODER_SET_OUTPUT:
  320. {
  321. int *iarg = arg;
  322. /* not much choice of outputs */
  323. if (*iarg != 0) {
  324. return -EINVAL;
  325. }
  326. }
  327. break;
  328. case DECODER_ENABLE_OUTPUT:
  329. {
  330. int *iarg = arg;
  331. int enable = (*iarg != 0);
  332. if (decoder->enable != enable) {
  333. decoder->enable = enable;
  334. /* RJ: If output should be disabled (for
  335. * playing videos), we also need a open PLL.
  336. * The input is set to 0 (where no input
  337. * source is connected), although this
  338. * is not necessary.
  339. *
  340. * If output should be enabled, we have to
  341. * reverse the above.
  342. */
  343. if (decoder->enable) {
  344. saa711x_write(client, 0x02,
  345. (decoder->
  346. reg[0x02] & 0xf8) |
  347. decoder->input);
  348. saa711x_write(client, 0x08,
  349. (decoder->reg[0x08] & 0xfb));
  350. saa711x_write(client, 0x11,
  351. (decoder->
  352. reg[0x11] & 0xf3) | 0x0c);
  353. } else {
  354. saa711x_write(client, 0x02,
  355. (decoder->reg[0x02] & 0xf8));
  356. saa711x_write(client, 0x08,
  357. (decoder->
  358. reg[0x08] & 0xfb) | 0x04);
  359. saa711x_write(client, 0x11,
  360. (decoder->reg[0x11] & 0xf3));
  361. }
  362. }
  363. }
  364. break;
  365. case DECODER_SET_PICTURE:
  366. {
  367. struct video_picture *pic = arg;
  368. if (decoder->bright != pic->brightness) {
  369. /* We want 0 to 255 we get 0-65535 */
  370. decoder->bright = pic->brightness;
  371. saa711x_write(client, 0x0a, decoder->bright >> 8);
  372. }
  373. if (decoder->contrast != pic->contrast) {
  374. /* We want 0 to 127 we get 0-65535 */
  375. decoder->contrast = pic->contrast;
  376. saa711x_write(client, 0x0b,
  377. decoder->contrast >> 9);
  378. }
  379. if (decoder->sat != pic->colour) {
  380. /* We want 0 to 127 we get 0-65535 */
  381. decoder->sat = pic->colour;
  382. saa711x_write(client, 0x0c, decoder->sat >> 9);
  383. }
  384. if (decoder->hue != pic->hue) {
  385. /* We want -128 to 127 we get 0-65535 */
  386. decoder->hue = pic->hue;
  387. saa711x_write(client, 0x0d,
  388. (decoder->hue - 32768) >> 8);
  389. }
  390. }
  391. break;
  392. default:
  393. return -EINVAL;
  394. }
  395. return 0;
  396. }
  397. /* ----------------------------------------------------------------------- */
  398. /*
  399. * Generic i2c probe
  400. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  401. */
  402. /* standard i2c insmod options */
  403. static unsigned short normal_i2c[] = {
  404. I2C_SAA7113>>1, /* saa7113 */
  405. I2C_SAA7114>>1, /* saa7114 */
  406. I2C_CLIENT_END
  407. };
  408. I2C_CLIENT_INSMOD;
  409. static struct i2c_driver i2c_driver_saa711x;
  410. static int
  411. saa711x_detect_client (struct i2c_adapter *adapter,
  412. int address,
  413. int kind)
  414. {
  415. int i;
  416. struct i2c_client *client;
  417. struct saa711x *decoder;
  418. struct video_decoder_init vdi;
  419. dprintk(1,
  420. KERN_INFO
  421. "saa711x.c: detecting saa711x client on address 0x%x\n",
  422. address << 1);
  423. /* Check if the adapter supports the needed features */
  424. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  425. return 0;
  426. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  427. if (client == 0)
  428. return -ENOMEM;
  429. client->addr = address;
  430. client->adapter = adapter;
  431. client->driver = &i2c_driver_saa711x;
  432. strlcpy(I2C_NAME(client), "saa711x", sizeof(I2C_NAME(client)));
  433. decoder = kzalloc(sizeof(struct saa711x), GFP_KERNEL);
  434. if (decoder == NULL) {
  435. kfree(client);
  436. return -ENOMEM;
  437. }
  438. decoder->norm = VIDEO_MODE_NTSC;
  439. decoder->input = 0;
  440. decoder->enable = 1;
  441. decoder->bright = 32768;
  442. decoder->contrast = 32768;
  443. decoder->hue = 32768;
  444. decoder->sat = 32768;
  445. i2c_set_clientdata(client, decoder);
  446. i = i2c_attach_client(client);
  447. if (i) {
  448. kfree(client);
  449. kfree(decoder);
  450. return i;
  451. }
  452. vdi.data = saa711x_i2c_init;
  453. vdi.len = sizeof(saa711x_i2c_init);
  454. i = saa711x_init_decoder(client, &vdi);
  455. if (i < 0) {
  456. dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
  457. I2C_NAME(client), i);
  458. } else {
  459. dprintk(1,
  460. KERN_INFO
  461. "%s_attach: chip version %x at address 0x%x\n",
  462. I2C_NAME(client), saa711x_read(client, 0x00) >> 4,
  463. client->addr << 1);
  464. }
  465. return 0;
  466. }
  467. static int
  468. saa711x_attach_adapter (struct i2c_adapter *adapter)
  469. {
  470. dprintk(1,
  471. KERN_INFO
  472. "saa711x.c: starting probe for adapter %s (0x%x)\n",
  473. I2C_NAME(adapter), adapter->id);
  474. return i2c_probe(adapter, &addr_data, &saa711x_detect_client);
  475. }
  476. static int
  477. saa711x_detach_client (struct i2c_client *client)
  478. {
  479. struct saa711x *decoder = i2c_get_clientdata(client);
  480. int err;
  481. err = i2c_detach_client(client);
  482. if (err) {
  483. return err;
  484. }
  485. kfree(decoder);
  486. kfree(client);
  487. return 0;
  488. }
  489. /* ----------------------------------------------------------------------- */
  490. static struct i2c_driver i2c_driver_saa711x = {
  491. .driver = {
  492. .name = "saa711x",
  493. },
  494. .id = I2C_DRIVERID_SAA711X,
  495. .attach_adapter = saa711x_attach_adapter,
  496. .detach_client = saa711x_detach_client,
  497. .command = saa711x_command,
  498. };
  499. static int __init
  500. saa711x_init (void)
  501. {
  502. return i2c_add_driver(&i2c_driver_saa711x);
  503. }
  504. static void __exit
  505. saa711x_exit (void)
  506. {
  507. i2c_del_driver(&i2c_driver_saa711x);
  508. }
  509. module_init(saa711x_init);
  510. module_exit(saa711x_exit);