adv7170.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1
  3. *
  4. * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
  5. *
  6. * Based on adv7176 driver by:
  7. *
  8. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  9. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  10. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  11. * - some corrections for Pinnacle Systems Inc. DC10plus card.
  12. *
  13. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  14. * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/delay.h>
  33. #include <linux/errno.h>
  34. #include <linux/fs.h>
  35. #include <linux/kernel.h>
  36. #include <linux/major.h>
  37. #include <linux/slab.h>
  38. #include <linux/mm.h>
  39. #include <linux/pci.h>
  40. #include <linux/signal.h>
  41. #include <asm/io.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/page.h>
  44. #include <linux/sched.h>
  45. #include <linux/types.h>
  46. #include <linux/videodev.h>
  47. #include <asm/uaccess.h>
  48. MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
  49. MODULE_AUTHOR("Maxim Yevtyushkin");
  50. MODULE_LICENSE("GPL");
  51. #include <linux/i2c.h>
  52. #define I2C_NAME(x) (x)->name
  53. #include <linux/video_encoder.h>
  54. static int debug = 0;
  55. module_param(debug, int, 0);
  56. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  57. #define dprintk(num, format, args...) \
  58. do { \
  59. if (debug >= num) \
  60. printk(format, ##args); \
  61. } while (0)
  62. /* ----------------------------------------------------------------------- */
  63. struct adv7170 {
  64. unsigned char reg[128];
  65. int norm;
  66. int input;
  67. int enable;
  68. int bright;
  69. int contrast;
  70. int hue;
  71. int sat;
  72. };
  73. #define I2C_ADV7170 0xd4
  74. #define I2C_ADV7171 0x54
  75. static char adv7170_name[] = "adv7170";
  76. static char adv7171_name[] = "adv7171";
  77. static char *inputs[] = { "pass_through", "play_back" };
  78. static char *norms[] = { "PAL", "NTSC" };
  79. /* ----------------------------------------------------------------------- */
  80. static inline int
  81. adv7170_write (struct i2c_client *client,
  82. u8 reg,
  83. u8 value)
  84. {
  85. struct adv7170 *encoder = i2c_get_clientdata(client);
  86. encoder->reg[reg] = value;
  87. return i2c_smbus_write_byte_data(client, reg, value);
  88. }
  89. static inline int
  90. adv7170_read (struct i2c_client *client,
  91. u8 reg)
  92. {
  93. return i2c_smbus_read_byte_data(client, reg);
  94. }
  95. static int
  96. adv7170_write_block (struct i2c_client *client,
  97. const u8 *data,
  98. unsigned int len)
  99. {
  100. int ret = -1;
  101. u8 reg;
  102. /* the adv7170 has an autoincrement function, use it if
  103. * the adapter understands raw I2C */
  104. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  105. /* do raw I2C, not smbus compatible */
  106. struct adv7170 *encoder = i2c_get_clientdata(client);
  107. u8 block_data[32];
  108. int block_len;
  109. while (len >= 2) {
  110. block_len = 0;
  111. block_data[block_len++] = reg = data[0];
  112. do {
  113. block_data[block_len++] =
  114. encoder->reg[reg++] = data[1];
  115. len -= 2;
  116. data += 2;
  117. } while (len >= 2 && data[0] == reg &&
  118. block_len < 32);
  119. if ((ret = i2c_master_send(client, block_data,
  120. block_len)) < 0)
  121. break;
  122. }
  123. } else {
  124. /* do some slow I2C emulation kind of thing */
  125. while (len >= 2) {
  126. reg = *data++;
  127. if ((ret = adv7170_write(client, reg,
  128. *data++)) < 0)
  129. break;
  130. len -= 2;
  131. }
  132. }
  133. return ret;
  134. }
  135. /* ----------------------------------------------------------------------- */
  136. // Output filter: S-Video Composite
  137. #define MR050 0x11 //0x09
  138. #define MR060 0x14 //0x0c
  139. //---------------------------------------------------------------------------
  140. #define TR0MODE 0x4c
  141. #define TR0RST 0x80
  142. #define TR1CAPT 0x00
  143. #define TR1PLAY 0x00
  144. static const unsigned char init_NTSC[] = {
  145. 0x00, 0x10, // MR0
  146. 0x01, 0x20, // MR1
  147. 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
  148. 0x03, 0x80, // MR3
  149. 0x04, 0x30, // MR4
  150. 0x05, 0x00, // Reserved
  151. 0x06, 0x00, // Reserved
  152. 0x07, TR0MODE, // TM0
  153. 0x08, TR1CAPT, // TM1
  154. 0x09, 0x16, // Fsc0
  155. 0x0a, 0x7c, // Fsc1
  156. 0x0b, 0xf0, // Fsc2
  157. 0x0c, 0x21, // Fsc3
  158. 0x0d, 0x00, // Subcarrier Phase
  159. 0x0e, 0x00, // Closed Capt. Ext 0
  160. 0x0f, 0x00, // Closed Capt. Ext 1
  161. 0x10, 0x00, // Closed Capt. 0
  162. 0x11, 0x00, // Closed Capt. 1
  163. 0x12, 0x00, // Pedestal Ctl 0
  164. 0x13, 0x00, // Pedestal Ctl 1
  165. 0x14, 0x00, // Pedestal Ctl 2
  166. 0x15, 0x00, // Pedestal Ctl 3
  167. 0x16, 0x00, // CGMS_WSS_0
  168. 0x17, 0x00, // CGMS_WSS_1
  169. 0x18, 0x00, // CGMS_WSS_2
  170. 0x19, 0x00, // Teletext Ctl
  171. };
  172. static const unsigned char init_PAL[] = {
  173. 0x00, 0x71, // MR0
  174. 0x01, 0x20, // MR1
  175. 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
  176. 0x03, 0x80, // MR3
  177. 0x04, 0x30, // MR4
  178. 0x05, 0x00, // Reserved
  179. 0x06, 0x00, // Reserved
  180. 0x07, TR0MODE, // TM0
  181. 0x08, TR1CAPT, // TM1
  182. 0x09, 0xcb, // Fsc0
  183. 0x0a, 0x8a, // Fsc1
  184. 0x0b, 0x09, // Fsc2
  185. 0x0c, 0x2a, // Fsc3
  186. 0x0d, 0x00, // Subcarrier Phase
  187. 0x0e, 0x00, // Closed Capt. Ext 0
  188. 0x0f, 0x00, // Closed Capt. Ext 1
  189. 0x10, 0x00, // Closed Capt. 0
  190. 0x11, 0x00, // Closed Capt. 1
  191. 0x12, 0x00, // Pedestal Ctl 0
  192. 0x13, 0x00, // Pedestal Ctl 1
  193. 0x14, 0x00, // Pedestal Ctl 2
  194. 0x15, 0x00, // Pedestal Ctl 3
  195. 0x16, 0x00, // CGMS_WSS_0
  196. 0x17, 0x00, // CGMS_WSS_1
  197. 0x18, 0x00, // CGMS_WSS_2
  198. 0x19, 0x00, // Teletext Ctl
  199. };
  200. static int
  201. adv7170_command (struct i2c_client *client,
  202. unsigned int cmd,
  203. void * arg)
  204. {
  205. struct adv7170 *encoder = i2c_get_clientdata(client);
  206. switch (cmd) {
  207. case 0:
  208. #if 0
  209. /* This is just for testing!!! */
  210. adv7170_write_block(client, init_common,
  211. sizeof(init_common));
  212. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  213. adv7170_write(client, 0x07, TR0MODE);
  214. #endif
  215. break;
  216. case ENCODER_GET_CAPABILITIES:
  217. {
  218. struct video_encoder_capability *cap = arg;
  219. cap->flags = VIDEO_ENCODER_PAL |
  220. VIDEO_ENCODER_NTSC;
  221. cap->inputs = 2;
  222. cap->outputs = 1;
  223. }
  224. break;
  225. case ENCODER_SET_NORM:
  226. {
  227. int iarg = *(int *) arg;
  228. dprintk(1, KERN_DEBUG "%s_command: set norm %d",
  229. I2C_NAME(client), iarg);
  230. switch (iarg) {
  231. case VIDEO_MODE_NTSC:
  232. adv7170_write_block(client, init_NTSC,
  233. sizeof(init_NTSC));
  234. if (encoder->input == 0)
  235. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  236. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  237. adv7170_write(client, 0x07, TR0MODE);
  238. break;
  239. case VIDEO_MODE_PAL:
  240. adv7170_write_block(client, init_PAL,
  241. sizeof(init_PAL));
  242. if (encoder->input == 0)
  243. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  244. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  245. adv7170_write(client, 0x07, TR0MODE);
  246. break;
  247. default:
  248. dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
  249. I2C_NAME(client), iarg);
  250. return -EINVAL;
  251. }
  252. dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
  253. norms[iarg]);
  254. encoder->norm = iarg;
  255. }
  256. break;
  257. case ENCODER_SET_INPUT:
  258. {
  259. int iarg = *(int *) arg;
  260. /* RJ: *iarg = 0: input is from decoder
  261. *iarg = 1: input is from ZR36060
  262. *iarg = 2: color bar */
  263. dprintk(1, KERN_DEBUG "%s_command: set input from %s\n",
  264. I2C_NAME(client),
  265. iarg == 0 ? "decoder" : "ZR36060");
  266. switch (iarg) {
  267. case 0:
  268. adv7170_write(client, 0x01, 0x20);
  269. adv7170_write(client, 0x08, TR1CAPT); /* TR1 */
  270. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  271. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  272. adv7170_write(client, 0x07, TR0MODE);
  273. //udelay(10);
  274. break;
  275. case 1:
  276. adv7170_write(client, 0x01, 0x00);
  277. adv7170_write(client, 0x08, TR1PLAY); /* TR1 */
  278. adv7170_write(client, 0x02, 0x08);
  279. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  280. adv7170_write(client, 0x07, TR0MODE);
  281. //udelay(10);
  282. break;
  283. default:
  284. dprintk(1, KERN_ERR "%s: illegal input: %d\n",
  285. I2C_NAME(client), iarg);
  286. return -EINVAL;
  287. }
  288. dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
  289. inputs[iarg]);
  290. encoder->input = iarg;
  291. }
  292. break;
  293. case ENCODER_SET_OUTPUT:
  294. {
  295. int *iarg = arg;
  296. /* not much choice of outputs */
  297. if (*iarg != 0) {
  298. return -EINVAL;
  299. }
  300. }
  301. break;
  302. case ENCODER_ENABLE_OUTPUT:
  303. {
  304. int *iarg = arg;
  305. encoder->enable = !!*iarg;
  306. }
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. return 0;
  312. }
  313. /* ----------------------------------------------------------------------- */
  314. /*
  315. * Generic i2c probe
  316. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  317. */
  318. static unsigned short normal_i2c[] =
  319. { I2C_ADV7170 >> 1, (I2C_ADV7170 >> 1) + 1,
  320. I2C_ADV7171 >> 1, (I2C_ADV7171 >> 1) + 1,
  321. I2C_CLIENT_END
  322. };
  323. static unsigned short ignore = I2C_CLIENT_END;
  324. static struct i2c_client_address_data addr_data = {
  325. .normal_i2c = normal_i2c,
  326. .probe = &ignore,
  327. .ignore = &ignore,
  328. };
  329. static struct i2c_driver i2c_driver_adv7170;
  330. static int
  331. adv7170_detect_client (struct i2c_adapter *adapter,
  332. int address,
  333. int kind)
  334. {
  335. int i;
  336. struct i2c_client *client;
  337. struct adv7170 *encoder;
  338. char *dname;
  339. dprintk(1,
  340. KERN_INFO
  341. "adv7170.c: detecting adv7170 client on address 0x%x\n",
  342. address << 1);
  343. /* Check if the adapter supports the needed features */
  344. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  345. return 0;
  346. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  347. if (client == 0)
  348. return -ENOMEM;
  349. client->addr = address;
  350. client->adapter = adapter;
  351. client->driver = &i2c_driver_adv7170;
  352. if ((client->addr == I2C_ADV7170 >> 1) ||
  353. (client->addr == (I2C_ADV7170 >> 1) + 1)) {
  354. dname = adv7170_name;
  355. } else if ((client->addr == I2C_ADV7171 >> 1) ||
  356. (client->addr == (I2C_ADV7171 >> 1) + 1)) {
  357. dname = adv7171_name;
  358. } else {
  359. /* We should never get here!!! */
  360. kfree(client);
  361. return 0;
  362. }
  363. strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
  364. encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
  365. if (encoder == NULL) {
  366. kfree(client);
  367. return -ENOMEM;
  368. }
  369. encoder->norm = VIDEO_MODE_NTSC;
  370. encoder->input = 0;
  371. encoder->enable = 1;
  372. i2c_set_clientdata(client, encoder);
  373. i = i2c_attach_client(client);
  374. if (i) {
  375. kfree(client);
  376. kfree(encoder);
  377. return i;
  378. }
  379. i = adv7170_write_block(client, init_NTSC, sizeof(init_NTSC));
  380. if (i >= 0) {
  381. i = adv7170_write(client, 0x07, TR0MODE | TR0RST);
  382. i = adv7170_write(client, 0x07, TR0MODE);
  383. i = adv7170_read(client, 0x12);
  384. dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%02x\n",
  385. I2C_NAME(client), i & 1, client->addr << 1);
  386. }
  387. if (i < 0) {
  388. dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
  389. I2C_NAME(client), i);
  390. }
  391. return 0;
  392. }
  393. static int
  394. adv7170_attach_adapter (struct i2c_adapter *adapter)
  395. {
  396. dprintk(1,
  397. KERN_INFO
  398. "adv7170.c: starting probe for adapter %s (0x%x)\n",
  399. I2C_NAME(adapter), adapter->id);
  400. return i2c_probe(adapter, &addr_data, &adv7170_detect_client);
  401. }
  402. static int
  403. adv7170_detach_client (struct i2c_client *client)
  404. {
  405. struct adv7170 *encoder = i2c_get_clientdata(client);
  406. int err;
  407. err = i2c_detach_client(client);
  408. if (err) {
  409. return err;
  410. }
  411. kfree(encoder);
  412. kfree(client);
  413. return 0;
  414. }
  415. /* ----------------------------------------------------------------------- */
  416. static struct i2c_driver i2c_driver_adv7170 = {
  417. .driver = {
  418. .name = "adv7170", /* name */
  419. },
  420. .id = I2C_DRIVERID_ADV7170,
  421. .attach_adapter = adv7170_attach_adapter,
  422. .detach_client = adv7170_detach_client,
  423. .command = adv7170_command,
  424. };
  425. static int __init
  426. adv7170_init (void)
  427. {
  428. return i2c_add_driver(&i2c_driver_adv7170);
  429. }
  430. static void __exit
  431. adv7170_exit (void)
  432. {
  433. i2c_del_driver(&i2c_driver_adv7170);
  434. }
  435. module_init(adv7170_init);
  436. module_exit(adv7170_exit);