saa7110.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.     saa7110 - Philips SAA7110(A) video decoder driver
  3.     Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl>
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.     This program is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.     GNU General Public License for more details.
  12.     You should have received a copy of the GNU General Public License
  13.     along with this program; if not, write to the Free Software
  14.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/types.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <asm/io.h>
  22. #include <asm/uaccess.h>
  23. #include <linux/i2c-old.h>
  24. #include <linux/videodev.h>
  25. #include "linux/video_decoder.h"
  26. #define DEBUG(x...) /* remove when no long debugging */
  27. #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
  28. #define SAA7110_MAX_OUTPUT 0 /* its a decoder only */
  29. #define I2C_SAA7110 0x9C /* or 0x9E */
  30. #define I2C_DELAY 10 /* 10 us or 100khz */
  31. struct saa7110 {
  32. struct i2c_bus *bus;
  33. int addr;
  34. unsigned char reg[36];
  35. int norm;
  36. int input;
  37. int enable;
  38. int bright;
  39. int contrast;
  40. int hue;
  41. int sat;
  42. };
  43. /* ----------------------------------------------------------------------- */
  44. /* I2C support functions    */
  45. /* ----------------------------------------------------------------------- */
  46. static
  47. int saa7110_write(struct saa7110 *decoder, unsigned char subaddr, unsigned char data)
  48. {
  49. int ack;
  50. LOCK_I2C_BUS(decoder->bus);
  51. i2c_start(decoder->bus);
  52. i2c_sendbyte(decoder->bus, decoder->addr, I2C_DELAY);
  53. i2c_sendbyte(decoder->bus, subaddr, I2C_DELAY);
  54. ack = i2c_sendbyte(decoder->bus, data, I2C_DELAY);
  55. i2c_stop(decoder->bus);
  56. decoder->reg[subaddr] = data;
  57. UNLOCK_I2C_BUS(decoder->bus);
  58. return ack;
  59. }
  60. static
  61. int saa7110_write_block(struct saa7110* decoder, unsigned const char *data, unsigned int len)
  62. {
  63. unsigned subaddr = *data;
  64. LOCK_I2C_BUS(decoder->bus);
  65.         i2c_start(decoder->bus);
  66.         i2c_sendbyte(decoder->bus,decoder->addr,I2C_DELAY);
  67. while (len-- > 0) {
  68.                 if (i2c_sendbyte(decoder->bus,*data,0)) {
  69.                         i2c_stop(decoder->bus);
  70.                         UNLOCK_I2C_BUS(decoder->bus);
  71.                         return -EAGAIN;
  72.                 }
  73. decoder->reg[subaddr++] = *data++;
  74.         }
  75. i2c_stop(decoder->bus);
  76. UNLOCK_I2C_BUS(decoder->bus);
  77. return 0;
  78. }
  79. static
  80. int saa7110_read(struct saa7110* decoder)
  81. {
  82. int data;
  83. LOCK_I2C_BUS(decoder->bus);
  84. i2c_start(decoder->bus);
  85. i2c_sendbyte(decoder->bus, decoder->addr, I2C_DELAY);
  86. i2c_start(decoder->bus);
  87. i2c_sendbyte(decoder->bus, decoder->addr | 1, I2C_DELAY);
  88. data = i2c_readbyte(decoder->bus, 1);
  89. i2c_stop(decoder->bus);
  90. UNLOCK_I2C_BUS(decoder->bus);
  91. return data;
  92. }
  93. /* ----------------------------------------------------------------------- */
  94. /* SAA7110 functions    */
  95. /* ----------------------------------------------------------------------- */
  96. static
  97. int saa7110_selmux(struct i2c_device *device, int chan)
  98. {
  99. static const unsigned char modes[9][8] = {
  100. /* mode 0 */ { 0x00, 0xD9, 0x17, 0x40, 0x03, 0x44, 0x75, 0x16 },
  101. /* mode 1 */ { 0x00, 0xD8, 0x17, 0x40, 0x03, 0x44, 0x75, 0x16 },
  102. /* mode 2 */ { 0x00, 0xBA, 0x07, 0x91, 0x03, 0x60, 0xB5, 0x05 },
  103. /* mode 3 */ { 0x00, 0xB8, 0x07, 0x91, 0x03, 0x60, 0xB5, 0x05 },
  104. /* mode 4 */ { 0x00, 0x7C, 0x07, 0xD2, 0x83, 0x60, 0xB5, 0x03 },
  105. /* mode 5 */ { 0x00, 0x78, 0x07, 0xD2, 0x83, 0x60, 0xB5, 0x03 },
  106. /* mode 6 */ { 0x80, 0x59, 0x17, 0x42, 0xA3, 0x44, 0x75, 0x12 },
  107. /* mode 7 */ { 0x80, 0x9A, 0x17, 0xB1, 0x13, 0x60, 0xB5, 0x14 },
  108. /* mode 8 */ { 0x80, 0x3C, 0x27, 0xC1, 0x23, 0x44, 0x75, 0x21 } };
  109. struct saa7110* decoder = device->data;
  110. const unsigned char* ptr = modes[chan];
  111. saa7110_write(decoder,0x06,ptr[0]); /* Luminance control */
  112. saa7110_write(decoder,0x20,ptr[1]); /* Analog Control #1 */
  113. saa7110_write(decoder,0x21,ptr[2]); /* Analog Control #2 */
  114. saa7110_write(decoder,0x22,ptr[3]); /* Mixer Control #1 */
  115. saa7110_write(decoder,0x2C,ptr[4]); /* Mixer Control #2 */
  116. saa7110_write(decoder,0x30,ptr[5]); /* ADCs gain control */
  117. saa7110_write(decoder,0x31,ptr[6]); /* Mixer Control #3 */
  118. saa7110_write(decoder,0x21,ptr[7]); /* Analog Control #2 */
  119. return 0;
  120. }
  121. static
  122. int determine_norm(struct i2c_device* dev)
  123. {
  124. struct saa7110* decoder = dev->data;
  125. int status;
  126. /* mode changed, start automatic detection */
  127. status = saa7110_read(decoder);
  128. if ((status & 3) == 0) {
  129. saa7110_write(decoder,0x06,0x80);
  130. if (status & 0x20) {
  131. DEBUG(printk(KERN_INFO "%s: norm=bw60n",dev->name));
  132. saa7110_write(decoder,0x2E,0x81);
  133. return VIDEO_MODE_NTSC;
  134. }
  135. DEBUG(printk(KERN_INFO "%s: norm=bw50n",dev->name));
  136. saa7110_write(decoder,0x2E,0x9A);
  137. return VIDEO_MODE_PAL;
  138. }
  139. saa7110_write(decoder,0x06,0x00);
  140. if (status & 0x20) { /* 60Hz */
  141. DEBUG(printk(KERN_INFO "%s: norm=ntscn",dev->name));
  142. saa7110_write(decoder,0x0D,0x06);
  143. saa7110_write(decoder,0x11,0x2C);
  144. saa7110_write(decoder,0x2E,0x81);
  145. return VIDEO_MODE_NTSC;
  146. }
  147. /* 50Hz -> PAL/SECAM */
  148. saa7110_write(decoder,0x0D,0x06);
  149. saa7110_write(decoder,0x11,0x59);
  150. saa7110_write(decoder,0x2E,0x9A);
  151. mdelay(150); /* pause 150 ms */
  152. status = saa7110_read(decoder);
  153. if ((status & 0x03) == 0x01) {
  154. DEBUG(printk(KERN_INFO "%s: norm=secamn",dev->name));
  155. saa7110_write(decoder,0x0D,0x07);
  156. return VIDEO_MODE_SECAM;
  157. }
  158. DEBUG(printk(KERN_INFO "%s: norm=paln",dev->name));
  159. return VIDEO_MODE_PAL;
  160. }
  161. static
  162. int saa7110_attach(struct i2c_device *device)
  163. {
  164. static const unsigned char initseq[] = {
  165.      0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF0, 0x00, 0x00,
  166. 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x06, 0x18, 0x90,
  167. 0x00, 0x2C, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
  168. 0xF0, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  169. 0xD9, 0x17, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
  170. 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x81, 0x03,
  171. 0x40, 0x75, 0x01, 0x8C, 0x03};
  172. struct saa7110* decoder;
  173. int rv;
  174. device->data = decoder = kmalloc(sizeof(struct saa7110), GFP_KERNEL);
  175. if (device->data == 0)
  176. return -ENOMEM;
  177. MOD_INC_USE_COUNT;
  178. /* clear our private data */
  179. memset(decoder, 0, sizeof(struct saa7110));
  180. strcpy(device->name, "saa7110");
  181. decoder->bus = device->bus;
  182. decoder->addr = device->addr;
  183. decoder->norm = VIDEO_MODE_PAL;
  184. decoder->input = 0;
  185. decoder->enable = 1;
  186. decoder->bright = 32768;
  187. decoder->contrast = 32768;
  188. decoder->hue = 32768;
  189. decoder->sat = 32768;
  190. rv = saa7110_write_block(decoder, initseq, sizeof(initseq));
  191. if (rv < 0)
  192. printk(KERN_ERR "%s_attach: init status %dn", device->name, rv);
  193. else {
  194. saa7110_write(decoder,0x21,0x16);
  195. saa7110_write(decoder,0x0D,0x04);
  196. DEBUG(printk(KERN_INFO "%s_attach: chip version %xn", device->name, saa7110_read(decoder)));
  197. saa7110_write(decoder,0x0D,0x06);
  198. }
  199. /* setup and implicit mode 0 select has been performed */
  200. return 0;
  201. }
  202. static
  203. int saa7110_detach(struct i2c_device *device)
  204. {
  205. struct saa7110* decoder = device->data;
  206. DEBUG(printk(KERN_INFO "%s_detachn",device->name));
  207. /* stop further output */
  208. saa7110_write(decoder,0x0E,0x00);
  209. kfree(device->data);
  210. MOD_DEC_USE_COUNT;
  211. return 0;
  212. }
  213. static
  214. int saa7110_command(struct i2c_device *device, unsigned int cmd, void *arg)
  215. {
  216. struct saa7110* decoder = device->data;
  217. int v;
  218. switch (cmd) {
  219.  case DECODER_GET_CAPABILITIES:
  220. {
  221. struct video_decoder_capability *dc = arg;
  222. dc->flags = VIDEO_DECODER_PAL
  223.   | VIDEO_DECODER_NTSC
  224.   | VIDEO_DECODER_SECAM
  225.   | VIDEO_DECODER_AUTO
  226.   | VIDEO_DECODER_CCIR;
  227. dc->inputs = SAA7110_MAX_INPUT;
  228. dc->outputs = SAA7110_MAX_OUTPUT;
  229. }
  230. break;
  231.  case DECODER_GET_STATUS:
  232. {
  233. struct saa7110* decoder = device->data;
  234. int status;
  235. int res = 0;
  236. status = i2c_read(device->bus,device->addr|1);
  237. if (status & 0x40)
  238. res |= DECODER_STATUS_GOOD;
  239. if (status & 0x03)
  240. res |= DECODER_STATUS_COLOR;
  241. switch (decoder->norm) {
  242.  case VIDEO_MODE_NTSC:
  243. res |= DECODER_STATUS_NTSC;
  244. break;
  245.  case VIDEO_MODE_PAL:
  246. res |= DECODER_STATUS_PAL;
  247. break;
  248.  case VIDEO_MODE_SECAM:
  249. res |= DECODER_STATUS_SECAM;
  250. break;
  251. }
  252. *(int*)arg = res;
  253. }
  254. break;
  255.  case DECODER_SET_NORM:
  256. v = *(int*)arg;
  257. if (decoder->norm != v) {
  258. decoder->norm = v;
  259. saa7110_write(decoder, 0x06, 0x00);
  260. switch (v) {
  261.  case VIDEO_MODE_NTSC:
  262. saa7110_write(decoder, 0x0D, 0x06);
  263. saa7110_write(decoder, 0x11, 0x2C);
  264. saa7110_write(decoder, 0x30, 0x81);
  265. saa7110_write(decoder, 0x2A, 0xDF);
  266. break;
  267.  case VIDEO_MODE_PAL:
  268. saa7110_write(decoder, 0x0D, 0x06);
  269. saa7110_write(decoder, 0x11, 0x59);
  270. saa7110_write(decoder, 0x2E, 0x9A);
  271. break;
  272.  case VIDEO_MODE_SECAM:
  273. saa7110_write(decoder, 0x0D, 0x07);
  274. saa7110_write(decoder, 0x11, 0x59);
  275. saa7110_write(decoder, 0x2E, 0x9A);
  276. break;
  277.  case VIDEO_MODE_AUTO:
  278. *(int*)arg = determine_norm(device);
  279. break;
  280.  default:
  281. return -EPERM;
  282. }
  283. }
  284. break;
  285.  case DECODER_SET_INPUT:
  286. v = *(int*)arg;
  287. if (v<0 || v>SAA7110_MAX_INPUT)
  288. return -EINVAL;
  289. if (decoder->input != v) {
  290. decoder->input = v;
  291. saa7110_selmux(device, v);
  292. }
  293. break;
  294.  case DECODER_SET_OUTPUT:
  295. v = *(int*)arg;
  296. /* not much choice of outputs */
  297. if (v != 0)
  298. return -EINVAL;
  299. break;
  300.  case DECODER_ENABLE_OUTPUT:
  301. v = *(int*)arg;
  302. if (decoder->enable != v) {
  303. decoder->enable = v;
  304. saa7110_write(decoder,0x0E, v ? 0x18 : 0x00);
  305. }
  306. break;
  307.  case DECODER_SET_PICTURE:
  308. {
  309. struct video_picture *pic = arg;
  310. if (decoder->bright != pic->brightness) {
  311. /* We want 0 to 255 we get 0-65535 */
  312. decoder->bright = pic->brightness;
  313. saa7110_write(decoder, 0x19, decoder->bright >> 8);
  314. }
  315. if (decoder->contrast != pic->contrast) {
  316. /* We want 0 to 127 we get 0-65535 */
  317. decoder->contrast = pic->contrast;
  318. saa7110_write(decoder, 0x13, decoder->contrast >> 9);
  319. }
  320. if (decoder->sat != pic->colour) {
  321. /* We want 0 to 127 we get 0-65535 */
  322. decoder->sat = pic->colour;
  323. saa7110_write(decoder, 0x12, decoder->sat >> 9);
  324. }
  325. if (decoder->hue != pic->hue) {
  326. /* We want -128 to 127 we get 0-65535 */
  327. decoder->hue = pic->hue;
  328. saa7110_write(decoder, 0x07, (decoder->hue>>8)-128);
  329. }
  330. }
  331. break;
  332.  case DECODER_DUMP:
  333. for (v=0; v<34; v+=16) {
  334. int j;
  335. DEBUG(printk(KERN_INFO "%s: %03xn",device->name,v));
  336. for (j=0; j<16; j++) {
  337. DEBUG(printk(KERN_INFO " %02x",decoder->reg[v+j]));
  338. }
  339. DEBUG(printk(KERN_INFO "n"));
  340. }
  341. break;
  342.  default:
  343. DEBUG(printk(KERN_INFO "unknown saa7110_command??(%d)n",cmd));
  344. return -EINVAL;
  345. }
  346. return 0;
  347. }
  348. /* ----------------------------------------------------------------------- */
  349. static struct i2c_driver i2c_driver_saa7110 =
  350. {
  351. "saa7110", /* name */
  352. I2C_DRIVERID_VIDEODECODER, /* in i2c.h */
  353. I2C_SAA7110, I2C_SAA7110+1, /* Addr range */
  354. saa7110_attach,
  355. saa7110_detach,
  356. saa7110_command
  357. };
  358. EXPORT_NO_SYMBOLS;
  359. static int saa7110_init(void)
  360. {
  361. return i2c_register_driver(&i2c_driver_saa7110);
  362. }
  363. static void saa7110_exit(void)
  364. {
  365. i2c_unregister_driver(&i2c_driver_saa7110);
  366. }
  367. module_init(saa7110_init);
  368. module_exit(saa7110_exit);
  369. MODULE_LICENSE("GPL");