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

Linux/Unix编程

开发平台:

Unix_Linux

  1. #define DEBUGLEVEL 0
  2. /* 
  3.     adv7175 - adv7175a video encoder driver version 0.0.3
  4.     Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  5.     Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  6.     Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  7.        - some corrections for Pinnacle Systems Inc. DC10plus card.
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 2 of the License, or
  11.     (at your option) any later version.
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/fs.h>
  25. #include <linux/kernel.h>
  26. #include <linux/major.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/pci.h>
  30. #include <linux/signal.h>
  31. #include <asm/io.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/page.h>
  34. #include <linux/sched.h>
  35. #include <asm/segment.h>
  36. #include <linux/types.h>
  37. #include <linux/wrapper.h>
  38. #include <linux/videodev.h>
  39. #include <linux/version.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/i2c-old.h>
  42. #include <linux/video_encoder.h>
  43. #if (DEBUGLEVEL > 0)
  44. #define DEBUG(x...)  x /* Debug driver */
  45. #else
  46. #define DEBUG(x...)
  47. #endif
  48. /* ----------------------------------------------------------------------- */
  49. struct adv7175 {
  50. struct i2c_bus *bus;
  51. int addr;
  52. unsigned char reg[128];
  53. int norm;
  54. int input;
  55. int enable;
  56. int bright;
  57. int contrast;
  58. int hue;
  59. int sat;
  60. };
  61. #define   I2C_ADV7175        0xd4
  62. #define   I2C_ADV7176        0x54
  63. static char adv7175_name[] = "adv7175";
  64. static char adv7176_name[] = "adv7176";
  65. static char unknown_name[] = "UNKNOWN";
  66. #if (DEBUGLEVEL > 0)
  67. static char *inputs[] = { "pass_through", "play_back", "color_bar" };
  68. static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
  69. #endif
  70. #define I2C_DELAY   10
  71. /* ----------------------------------------------------------------------- */
  72. static int adv7175_write(struct adv7175 *dev, unsigned char subaddr, unsigned char data)
  73. {
  74. int ack;
  75. LOCK_I2C_BUS(dev->bus);
  76. i2c_start(dev->bus);
  77. i2c_sendbyte(dev->bus, dev->addr, I2C_DELAY);
  78. i2c_sendbyte(dev->bus, subaddr, I2C_DELAY);
  79. ack = i2c_sendbyte(dev->bus, data, I2C_DELAY);
  80. dev->reg[subaddr] = data;
  81. i2c_stop(dev->bus);
  82. UNLOCK_I2C_BUS(dev->bus);
  83. return ack;
  84. }
  85. static unsigned char adv7175_read(struct adv7175 *dev, unsigned char subaddr)
  86. {
  87. unsigned char data;
  88. LOCK_I2C_BUS(dev->bus);
  89. i2c_start(dev->bus);
  90. i2c_sendbyte(dev->bus, dev->addr, I2C_DELAY);
  91. i2c_sendbyte(dev->bus, subaddr, I2C_DELAY);
  92. i2c_sendbyte(dev->bus, dev->addr + 1, I2C_DELAY);
  93. data = i2c_readbyte(dev->bus, 1);
  94. dev->reg[subaddr] = data;
  95. i2c_stop(dev->bus);
  96. UNLOCK_I2C_BUS(dev->bus);
  97. return data;
  98. }
  99. static int adv7175_write_block(struct adv7175 *dev,
  100.        unsigned const char *data, unsigned int len)
  101. {
  102. int ack = 0;
  103. unsigned subaddr;
  104. while (len > 1) {
  105. LOCK_I2C_BUS(dev->bus);
  106. i2c_start(dev->bus);
  107. i2c_sendbyte(dev->bus, dev->addr, I2C_DELAY);
  108. ack = i2c_sendbyte(dev->bus, (subaddr = *data++), I2C_DELAY);
  109. ack = i2c_sendbyte(dev->bus, (dev->reg[subaddr] = *data++), I2C_DELAY);
  110. len -= 2;
  111. while (len > 1 && *data == ++subaddr) {
  112. data++;
  113. ack = i2c_sendbyte(dev->bus, (dev->reg[subaddr] = *data++), I2C_DELAY);
  114. len -= 2;
  115. }
  116. i2c_stop(dev->bus);
  117. UNLOCK_I2C_BUS(dev->bus);
  118. }
  119. return ack;
  120. }
  121. /* ----------------------------------------------------------------------- */
  122. // Output filter:  S-Video  Composite
  123. #define MR050       0x11 //0x09
  124. #define MR060       0x14 //0x0c
  125. //---------------------------------------------------------------------------
  126. #define TR0MODE     0x46
  127. #define TR0RST     0x80
  128. #define TR1CAPT     0x80
  129. #define TR1PLAY     0x00
  130. static const unsigned char init_common[] = {
  131. 0x00, MR050, /* MR0, PAL enabled */
  132. 0x01, 0x00, /* MR1 */
  133. 0x02, 0x0c, /* subc. freq. */
  134. 0x03, 0x8c, /* subc. freq. */
  135. 0x04, 0x79, /* subc. freq. */
  136. 0x05, 0x26, /* subc. freq. */
  137. 0x06, 0x40, /* subc. phase */
  138. 0x07, TR0MODE, /* TR0, 16bit */
  139. 0x08, 0x21, /*  */
  140. 0x09, 0x00, /*  */
  141. 0x0a, 0x00, /*  */
  142. 0x0b, 0x00, /*  */
  143. 0x0c, TR1CAPT, /* TR1 */
  144. 0x0d, 0x4f, /* MR2 */
  145. 0x0e, 0x00, /*  */
  146. 0x0f, 0x00, /*  */
  147. 0x10, 0x00, /*  */
  148. 0x11, 0x00, /*  */
  149. 0x12, 0x00, /* MR3 */
  150. 0x24, 0x00, /*  */
  151. };
  152. static const unsigned char init_pal[] = {
  153. 0x00, MR050, /* MR0, PAL enabled */
  154. 0x01, 0x00, /* MR1 */
  155. 0x02, 0x0c, /* subc. freq. */
  156. 0x03, 0x8c, /* subc. freq. */
  157. 0x04, 0x79, /* subc. freq. */
  158. 0x05, 0x26, /* subc. freq. */
  159. 0x06, 0x40, /* subc. phase */
  160. };
  161. static const unsigned char init_ntsc[] = {
  162. 0x00, MR060, /* MR0, NTSC enabled */
  163. 0x01, 0x00, /* MR1 */
  164. 0x02, 0x55, /* subc. freq. */
  165. 0x03, 0x55, /* subc. freq. */
  166. 0x04, 0x55, /* subc. freq. */
  167. 0x05, 0x25, /* subc. freq. */
  168. 0x06, 0x1a, /* subc. phase */
  169. };
  170. static int adv7175_attach(struct i2c_device *device)
  171. {
  172. int i;
  173. struct adv7175 *encoder;
  174. char *dname;
  175. MOD_INC_USE_COUNT;
  176. device->data = encoder = kmalloc(sizeof(struct adv7175), GFP_KERNEL);
  177. if (encoder == NULL) {
  178. MOD_DEC_USE_COUNT;
  179. return -ENOMEM;
  180. }
  181. memset(encoder, 0, sizeof(struct adv7175));
  182. if ((device->addr == I2C_ADV7175) || (device->addr == (I2C_ADV7175 + 2))) {
  183. dname = adv7175_name;
  184. } else if ((device->addr == I2C_ADV7176) || (device->addr == (I2C_ADV7176 + 2))) {
  185. dname = adv7176_name;
  186. } else {
  187. // We should never get here!!!
  188. dname = unknown_name;
  189. }
  190. strcpy(device->name, dname);
  191. encoder->bus = device->bus;
  192. encoder->addr = device->addr;
  193. encoder->norm = VIDEO_MODE_PAL;
  194. encoder->input = 0;
  195. encoder->enable = 1;
  196. i = adv7175_write_block(encoder, init_common, sizeof(init_common));
  197. if (i >= 0) {
  198. i = adv7175_write(encoder, 0x07, TR0MODE | TR0RST);
  199. i = adv7175_write(encoder, 0x07, TR0MODE);
  200. i = adv7175_read(encoder, 0x12);
  201. printk(KERN_INFO "%s_attach: %s rev. %d at 0x%02xn",
  202.        device->name, dname, i & 1, device->addr);
  203. }
  204. if (i < 0) {
  205. printk(KERN_ERR "%s_attach: init error %dn", device->name,
  206.        i);
  207. }
  208. return 0;
  209. }
  210. static int adv7175_detach(struct i2c_device *device)
  211. {
  212. kfree(device->data);
  213. MOD_DEC_USE_COUNT;
  214. return 0;
  215. }
  216. static int adv7175_command(struct i2c_device *device, unsigned int cmd,
  217.    void *arg)
  218. {
  219. struct adv7175 *encoder = device->data;
  220. switch (cmd) {
  221. case ENCODER_GET_CAPABILITIES:
  222. {
  223. struct video_encoder_capability *cap = arg;
  224. cap->flags = VIDEO_ENCODER_PAL | VIDEO_ENCODER_NTSC
  225.     // | VIDEO_ENCODER_SECAM
  226.     // | VIDEO_ENCODER_CCIR
  227.     ;
  228. cap->inputs = 2;
  229. cap->outputs = 1;
  230. }
  231. break;
  232. case ENCODER_SET_NORM:
  233. {
  234. int iarg = *(int *) arg;
  235. if (encoder->norm != iarg) {
  236. switch (iarg) {
  237. case VIDEO_MODE_NTSC:
  238. adv7175_write_block(encoder,
  239.     init_ntsc,
  240.     sizeof
  241.     (init_ntsc));
  242. if (encoder->input == 0)
  243. adv7175_write(encoder, 0x0d, 0x4f); // Enable genlock
  244. adv7175_write(encoder, 0x07,
  245.       TR0MODE | TR0RST);
  246. adv7175_write(encoder, 0x07,
  247.       TR0MODE);
  248. break;
  249. case VIDEO_MODE_PAL:
  250. adv7175_write_block(encoder,
  251.     init_pal,
  252.     sizeof
  253.     (init_pal));
  254. if (encoder->input == 0)
  255. adv7175_write(encoder, 0x0d, 0x4f); // Enable genlock
  256. adv7175_write(encoder, 0x07,
  257.       TR0MODE | TR0RST);
  258. adv7175_write(encoder, 0x07,
  259.       TR0MODE);
  260. break;
  261. case VIDEO_MODE_SECAM: // WARNING! ADV7176 does not support SECAM.
  262. // This is an attempt to convert SECAM->PAL (typically
  263. // it does not work due to genlock: when decoder
  264. // is in SECAM and encoder in in PAL the subcarrier
  265. // can not be syncronized with horizontal frequency)
  266. adv7175_write_block(encoder,
  267.     init_pal,
  268.     sizeof
  269.     (init_pal));
  270. if (encoder->input == 0)
  271. adv7175_write(encoder, 0x0d, 0x49); // Disable genlock
  272. adv7175_write(encoder, 0x07,
  273.       TR0MODE | TR0RST);
  274. adv7175_write(encoder, 0x07,
  275.       TR0MODE);
  276. break;
  277. default:
  278. printk(KERN_ERR
  279.        "%s: illegal norm: %dn",
  280.        device->name, iarg);
  281. return -EINVAL;
  282. }
  283. DEBUG(printk
  284.       (KERN_INFO "%s: switched to %sn",
  285.        device->name, norms[iarg]));
  286. encoder->norm = iarg;
  287. }
  288. }
  289. break;
  290. case ENCODER_SET_INPUT:
  291. {
  292. int iarg = *(int *) arg;
  293. /* RJ: *iarg = 0: input is from SAA7110
  294.    *iarg = 1: input is from ZR36060
  295.    *iarg = 2: color bar */
  296. if (encoder->input != iarg) {
  297. switch (iarg) {
  298. case 0:
  299. adv7175_write(encoder, 0x01, 0x00);
  300. adv7175_write(encoder, 0x0c, TR1CAPT); /* TR1 */
  301. if (encoder->norm ==
  302.     VIDEO_MODE_SECAM)
  303. adv7175_write(encoder, 0x0d, 0x49); // Disable genlock
  304. else
  305. adv7175_write(encoder, 0x0d, 0x4f); // Enable genlock
  306. adv7175_write(encoder, 0x07,
  307.       TR0MODE | TR0RST);
  308. adv7175_write(encoder, 0x07,
  309.       TR0MODE);
  310. //udelay(10);
  311. break;
  312. case 1:
  313. adv7175_write(encoder, 0x01, 0x00);
  314. adv7175_write(encoder, 0x0c, TR1PLAY); /* TR1 */
  315. adv7175_write(encoder, 0x0d, 0x49);
  316. adv7175_write(encoder, 0x07,
  317.       TR0MODE | TR0RST);
  318. adv7175_write(encoder, 0x07,
  319.       TR0MODE);
  320. //udelay(10);
  321. break;
  322. case 2:
  323. adv7175_write(encoder, 0x01, 0x80);
  324. adv7175_write(encoder, 0x0d, 0x49);
  325. adv7175_write(encoder, 0x07,
  326.       TR0MODE | TR0RST);
  327. adv7175_write(encoder, 0x07,
  328.       TR0MODE);
  329. //udelay(10);
  330. break;
  331. default:
  332. printk(KERN_ERR
  333.        "%s: illegal input: %dn",
  334.        device->name, iarg);
  335. return -EINVAL;
  336. }
  337. DEBUG(printk
  338.       (KERN_INFO "%s: switched to %sn",
  339.        device->name, inputs[iarg]));
  340. encoder->input = iarg;
  341. }
  342. }
  343. break;
  344. case ENCODER_SET_OUTPUT:
  345. {
  346. int *iarg = arg;
  347. /* not much choice of outputs */
  348. if (*iarg != 0) {
  349. return -EINVAL;
  350. }
  351. }
  352. break;
  353. case ENCODER_ENABLE_OUTPUT:
  354. {
  355. int *iarg = arg;
  356. encoder->enable = !!*iarg;
  357. adv7175_write(encoder, 0x61,
  358.       (encoder->
  359.        reg[0x61] & 0xbf) | (encoder->
  360.     enable ? 0x00 :
  361.     0x40));
  362. }
  363. break;
  364. default:
  365. return -EINVAL;
  366. }
  367. return 0;
  368. }
  369. /* ----------------------------------------------------------------------- */
  370. static struct i2c_driver i2c_driver_adv7175 = {
  371. "adv7175", /* name */
  372. I2C_DRIVERID_VIDEOENCODER, /* ID */
  373. I2C_ADV7175, I2C_ADV7175 + 3,
  374. adv7175_attach,
  375. adv7175_detach,
  376. adv7175_command
  377. };
  378. static struct i2c_driver i2c_driver_adv7176 = {
  379. "adv7175", /* name */
  380. I2C_DRIVERID_VIDEOENCODER, /* ID */
  381. I2C_ADV7176, I2C_ADV7176 + 3,
  382. adv7175_attach,
  383. adv7175_detach,
  384. adv7175_command
  385. };
  386. EXPORT_NO_SYMBOLS;
  387. static int adv7175_init(void)
  388. {
  389. int res_adv7175 = 0, res_adv7176 = 0;
  390. res_adv7175 = i2c_register_driver(&i2c_driver_adv7175);
  391. res_adv7176 = i2c_register_driver(&i2c_driver_adv7176);
  392. return (res_adv7175 | res_adv7176); // Any idea how to make it better?
  393. }
  394. static void adv7175_exit(void)
  395. {
  396. i2c_unregister_driver(&i2c_driver_adv7176);
  397. i2c_unregister_driver(&i2c_driver_adv7175);
  398. }
  399. module_init(adv7175_init);
  400. module_exit(adv7175_exit);
  401. MODULE_LICENSE("GPL");