saa711x.c 14 KB

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