saa7111.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/types.h>
  31. #include <linux/ioctl.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/i2c.h>
  34. #include <linux/i2c-id.h>
  35. #include <linux/videodev.h>
  36. #include <linux/video_decoder.h>
  37. #include <media/v4l2-common.h>
  38. #include <media/v4l2-i2c-drv-legacy.h>
  39. MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
  40. MODULE_AUTHOR("Dave Perks");
  41. MODULE_LICENSE("GPL");
  42. static int debug;
  43. module_param(debug, int, 0644);
  44. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  45. /* ----------------------------------------------------------------------- */
  46. #define SAA7111_NR_REG 0x18
  47. struct saa7111 {
  48. unsigned char reg[SAA7111_NR_REG];
  49. int norm;
  50. int input;
  51. int enable;
  52. };
  53. /* ----------------------------------------------------------------------- */
  54. static inline int saa7111_write(struct i2c_client *client, u8 reg, u8 value)
  55. {
  56. struct saa7111 *decoder = i2c_get_clientdata(client);
  57. decoder->reg[reg] = value;
  58. return i2c_smbus_write_byte_data(client, reg, value);
  59. }
  60. static inline void saa7111_write_if_changed(struct i2c_client *client, u8 reg, u8 value)
  61. {
  62. struct saa7111 *decoder = i2c_get_clientdata(client);
  63. if (decoder->reg[reg] != value) {
  64. decoder->reg[reg] = value;
  65. i2c_smbus_write_byte_data(client, reg, value);
  66. }
  67. }
  68. static int saa7111_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
  69. {
  70. int ret = -1;
  71. u8 reg;
  72. /* the saa7111 has an autoincrement function, use it if
  73. * the adapter understands raw I2C */
  74. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  75. /* do raw I2C, not smbus compatible */
  76. struct saa7111 *decoder = i2c_get_clientdata(client);
  77. u8 block_data[32];
  78. int block_len;
  79. while (len >= 2) {
  80. block_len = 0;
  81. block_data[block_len++] = reg = data[0];
  82. do {
  83. block_data[block_len++] =
  84. decoder->reg[reg++] = data[1];
  85. len -= 2;
  86. data += 2;
  87. } while (len >= 2 && data[0] == reg && block_len < 32);
  88. ret = i2c_master_send(client, block_data, block_len);
  89. if (ret < 0)
  90. break;
  91. }
  92. } else {
  93. /* do some slow I2C emulation kind of thing */
  94. while (len >= 2) {
  95. reg = *data++;
  96. ret = saa7111_write(client, reg, *data++);
  97. if (ret < 0)
  98. break;
  99. len -= 2;
  100. }
  101. }
  102. return ret;
  103. }
  104. static int saa7111_init_decoder(struct i2c_client *client,
  105. struct video_decoder_init *init)
  106. {
  107. return saa7111_write_block(client, init->data, init->len);
  108. }
  109. static inline int saa7111_read(struct i2c_client *client, u8 reg)
  110. {
  111. return i2c_smbus_read_byte_data(client, reg);
  112. }
  113. /* ----------------------------------------------------------------------- */
  114. static const unsigned char saa7111_i2c_init[] = {
  115. 0x00, 0x00, /* 00 - ID byte */
  116. 0x01, 0x00, /* 01 - reserved */
  117. /*front end */
  118. 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
  119. 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
  120. * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
  121. 0x04, 0x00, /* 04 - GAI1=256 */
  122. 0x05, 0x00, /* 05 - GAI2=256 */
  123. /* decoder */
  124. 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
  125. * pixels after end of last line */
  126. /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
  127. * after end of last line */
  128. 0x07, 0xe8, /* 07 - HSS seems to be needed to
  129. * work with NTSC, too */
  130. 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
  131. * VTRC=1, HPLL=0, VNOI=0 */
  132. 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
  133. * VBLB=0, UPTCV=0, APER=1 */
  134. 0x0a, 0x80, /* 0a - BRIG=128 */
  135. 0x0b, 0x47, /* 0b - CONT=1.109 */
  136. 0x0c, 0x40, /* 0c - SATN=1.0 */
  137. 0x0d, 0x00, /* 0d - HUE=0 */
  138. 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
  139. * FCTC=0, CHBW=1 */
  140. 0x0f, 0x00, /* 0f - reserved */
  141. 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
  142. 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
  143. * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
  144. 0x12, 0x00, /* 12 - output control 2 */
  145. 0x13, 0x00, /* 13 - output control 3 */
  146. 0x14, 0x00, /* 14 - reserved */
  147. 0x15, 0x00, /* 15 - VBI */
  148. 0x16, 0x00, /* 16 - VBI */
  149. 0x17, 0x00, /* 17 - VBI */
  150. };
  151. static int saa7111_command(struct i2c_client *client, unsigned cmd, void *arg)
  152. {
  153. struct saa7111 *decoder = i2c_get_clientdata(client);
  154. switch (cmd) {
  155. case 0:
  156. break;
  157. case DECODER_INIT:
  158. {
  159. struct video_decoder_init *init = arg;
  160. struct video_decoder_init vdi;
  161. if (NULL != init)
  162. return saa7111_init_decoder(client, init);
  163. vdi.data = saa7111_i2c_init;
  164. vdi.len = sizeof(saa7111_i2c_init);
  165. return saa7111_init_decoder(client, &vdi);
  166. }
  167. case DECODER_DUMP:
  168. {
  169. int i;
  170. for (i = 0; i < SAA7111_NR_REG; i += 16) {
  171. int j;
  172. v4l_info(client, "%03x", i);
  173. for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
  174. printk(KERN_CONT " %02x",
  175. saa7111_read(client, i + j));
  176. }
  177. printk(KERN_CONT "\n");
  178. }
  179. break;
  180. }
  181. case DECODER_GET_CAPABILITIES:
  182. {
  183. struct video_decoder_capability *cap = arg;
  184. cap->flags = VIDEO_DECODER_PAL |
  185. VIDEO_DECODER_NTSC |
  186. VIDEO_DECODER_SECAM |
  187. VIDEO_DECODER_AUTO |
  188. VIDEO_DECODER_CCIR;
  189. cap->inputs = 8;
  190. cap->outputs = 1;
  191. break;
  192. }
  193. case DECODER_GET_STATUS:
  194. {
  195. int *iarg = arg;
  196. int status;
  197. int res;
  198. status = saa7111_read(client, 0x1f);
  199. v4l_dbg(1, debug, client, "status: 0x%02x\n", status);
  200. res = 0;
  201. if ((status & (1 << 6)) == 0) {
  202. res |= DECODER_STATUS_GOOD;
  203. }
  204. switch (decoder->norm) {
  205. case VIDEO_MODE_NTSC:
  206. res |= DECODER_STATUS_NTSC;
  207. break;
  208. case VIDEO_MODE_PAL:
  209. res |= DECODER_STATUS_PAL;
  210. break;
  211. case VIDEO_MODE_SECAM:
  212. res |= DECODER_STATUS_SECAM;
  213. break;
  214. default:
  215. case VIDEO_MODE_AUTO:
  216. if ((status & (1 << 5)) != 0) {
  217. res |= DECODER_STATUS_NTSC;
  218. } else {
  219. res |= DECODER_STATUS_PAL;
  220. }
  221. break;
  222. }
  223. if ((status & (1 << 0)) != 0) {
  224. res |= DECODER_STATUS_COLOR;
  225. }
  226. *iarg = res;
  227. break;
  228. }
  229. case DECODER_SET_GPIO:
  230. {
  231. int *iarg = arg;
  232. if (0 != *iarg) {
  233. saa7111_write(client, 0x11,
  234. (decoder->reg[0x11] | 0x80));
  235. } else {
  236. saa7111_write(client, 0x11,
  237. (decoder->reg[0x11] & 0x7f));
  238. }
  239. break;
  240. }
  241. case DECODER_SET_VBI_BYPASS:
  242. {
  243. int *iarg = arg;
  244. if (0 != *iarg) {
  245. saa7111_write(client, 0x13,
  246. (decoder->reg[0x13] & 0xf0) | 0x0a);
  247. } else {
  248. saa7111_write(client, 0x13,
  249. (decoder->reg[0x13] & 0xf0));
  250. }
  251. break;
  252. }
  253. case DECODER_SET_NORM:
  254. {
  255. int *iarg = arg;
  256. switch (*iarg) {
  257. case VIDEO_MODE_NTSC:
  258. saa7111_write(client, 0x08,
  259. (decoder->reg[0x08] & 0x3f) | 0x40);
  260. saa7111_write(client, 0x0e,
  261. (decoder->reg[0x0e] & 0x8f));
  262. break;
  263. case VIDEO_MODE_PAL:
  264. saa7111_write(client, 0x08,
  265. (decoder->reg[0x08] & 0x3f) | 0x00);
  266. saa7111_write(client, 0x0e,
  267. (decoder->reg[0x0e] & 0x8f));
  268. break;
  269. case VIDEO_MODE_SECAM:
  270. saa7111_write(client, 0x08,
  271. (decoder->reg[0x08] & 0x3f) | 0x00);
  272. saa7111_write(client, 0x0e,
  273. (decoder->reg[0x0e] & 0x8f) | 0x50);
  274. break;
  275. case VIDEO_MODE_AUTO:
  276. saa7111_write(client, 0x08,
  277. (decoder->reg[0x08] & 0x3f) | 0x80);
  278. saa7111_write(client, 0x0e,
  279. (decoder->reg[0x0e] & 0x8f));
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. decoder->norm = *iarg;
  285. break;
  286. }
  287. case DECODER_SET_INPUT:
  288. {
  289. int *iarg = arg;
  290. if (*iarg < 0 || *iarg > 7) {
  291. return -EINVAL;
  292. }
  293. if (decoder->input != *iarg) {
  294. decoder->input = *iarg;
  295. /* select mode */
  296. saa7111_write(client, 0x02,
  297. (decoder->
  298. reg[0x02] & 0xf8) | decoder->input);
  299. /* bypass chrominance trap for modes 4..7 */
  300. saa7111_write(client, 0x09,
  301. (decoder->
  302. reg[0x09] & 0x7f) | ((decoder->
  303. input >
  304. 3) ? 0x80 :
  305. 0));
  306. }
  307. break;
  308. }
  309. case DECODER_SET_OUTPUT:
  310. {
  311. int *iarg = arg;
  312. /* not much choice of outputs */
  313. if (*iarg != 0) {
  314. return -EINVAL;
  315. }
  316. break;
  317. }
  318. case DECODER_ENABLE_OUTPUT:
  319. {
  320. int *iarg = arg;
  321. int enable = (*iarg != 0);
  322. if (decoder->enable != enable) {
  323. decoder->enable = enable;
  324. /* RJ: If output should be disabled (for
  325. * playing videos), we also need a open PLL.
  326. * The input is set to 0 (where no input
  327. * source is connected), although this
  328. * is not necessary.
  329. *
  330. * If output should be enabled, we have to
  331. * reverse the above.
  332. */
  333. if (decoder->enable) {
  334. saa7111_write(client, 0x02,
  335. (decoder->
  336. reg[0x02] & 0xf8) |
  337. decoder->input);
  338. saa7111_write(client, 0x08,
  339. (decoder->reg[0x08] & 0xfb));
  340. saa7111_write(client, 0x11,
  341. (decoder->
  342. reg[0x11] & 0xf3) | 0x0c);
  343. } else {
  344. saa7111_write(client, 0x02,
  345. (decoder->reg[0x02] & 0xf8));
  346. saa7111_write(client, 0x08,
  347. (decoder->
  348. reg[0x08] & 0xfb) | 0x04);
  349. saa7111_write(client, 0x11,
  350. (decoder->reg[0x11] & 0xf3));
  351. }
  352. }
  353. break;
  354. }
  355. case DECODER_SET_PICTURE:
  356. {
  357. struct video_picture *pic = arg;
  358. /* We want 0 to 255 we get 0-65535 */
  359. saa7111_write_if_changed(client, 0x0a, pic->brightness >> 8);
  360. /* We want 0 to 127 we get 0-65535 */
  361. saa7111_write(client, 0x0b, pic->contrast >> 9);
  362. /* We want 0 to 127 we get 0-65535 */
  363. saa7111_write(client, 0x0c, pic->colour >> 9);
  364. /* We want -128 to 127 we get 0-65535 */
  365. saa7111_write(client, 0x0d, (pic->hue - 32768) >> 8);
  366. break;
  367. }
  368. default:
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. /* ----------------------------------------------------------------------- */
  374. static unsigned short normal_i2c[] = { 0x48 >> 1, I2C_CLIENT_END };
  375. I2C_CLIENT_INSMOD;
  376. static int saa7111_probe(struct i2c_client *client,
  377. const struct i2c_device_id *id)
  378. {
  379. int i;
  380. struct saa7111 *decoder;
  381. struct video_decoder_init vdi;
  382. /* Check if the adapter supports the needed features */
  383. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  384. return -ENODEV;
  385. v4l_info(client, "chip found @ 0x%x (%s)\n",
  386. client->addr << 1, client->adapter->name);
  387. decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
  388. if (decoder == NULL) {
  389. kfree(client);
  390. return -ENOMEM;
  391. }
  392. decoder->norm = VIDEO_MODE_NTSC;
  393. decoder->input = 0;
  394. decoder->enable = 1;
  395. i2c_set_clientdata(client, decoder);
  396. vdi.data = saa7111_i2c_init;
  397. vdi.len = sizeof(saa7111_i2c_init);
  398. i = saa7111_init_decoder(client, &vdi);
  399. if (i < 0) {
  400. v4l_dbg(1, debug, client, "init status %d\n", i);
  401. } else {
  402. v4l_dbg(1, debug, client, "revision %x\n",
  403. saa7111_read(client, 0x00) >> 4);
  404. }
  405. return 0;
  406. }
  407. static int saa7111_remove(struct i2c_client *client)
  408. {
  409. kfree(i2c_get_clientdata(client));
  410. return 0;
  411. }
  412. /* ----------------------------------------------------------------------- */
  413. static const struct i2c_device_id saa7111_id[] = {
  414. { "saa7111_old", 0 }, /* "saa7111" maps to the saa7115 driver */
  415. { }
  416. };
  417. MODULE_DEVICE_TABLE(i2c, saa7111_id);
  418. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  419. .name = "saa7111",
  420. .driverid = I2C_DRIVERID_SAA7111A,
  421. .command = saa7111_command,
  422. .probe = saa7111_probe,
  423. .remove = saa7111_remove,
  424. .id_table = saa7111_id,
  425. };