adv7170.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. #include <linux/i2c-dev.h>
  53. #define I2C_NAME(x) (x)->name
  54. #include <linux/video_encoder.h>
  55. static int debug = 0;
  56. module_param(debug, int, 0);
  57. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  58. #define dprintk(num, format, args...) \
  59. do { \
  60. if (debug >= num) \
  61. printk(format, ##args); \
  62. } while (0)
  63. /* ----------------------------------------------------------------------- */
  64. struct adv7170 {
  65. unsigned char reg[128];
  66. int norm;
  67. int input;
  68. int enable;
  69. int bright;
  70. int contrast;
  71. int hue;
  72. int sat;
  73. };
  74. #define I2C_ADV7170 0xd4
  75. #define I2C_ADV7171 0x54
  76. static char adv7170_name[] = "adv7170";
  77. static char adv7171_name[] = "adv7171";
  78. static char *inputs[] = { "pass_through", "play_back" };
  79. static char *norms[] = { "PAL", "NTSC" };
  80. /* ----------------------------------------------------------------------- */
  81. static inline int
  82. adv7170_write (struct i2c_client *client,
  83. u8 reg,
  84. u8 value)
  85. {
  86. struct adv7170 *encoder = i2c_get_clientdata(client);
  87. encoder->reg[reg] = value;
  88. return i2c_smbus_write_byte_data(client, reg, value);
  89. }
  90. static inline int
  91. adv7170_read (struct i2c_client *client,
  92. u8 reg)
  93. {
  94. return i2c_smbus_read_byte_data(client, reg);
  95. }
  96. static int
  97. adv7170_write_block (struct i2c_client *client,
  98. const u8 *data,
  99. unsigned int len)
  100. {
  101. int ret = -1;
  102. u8 reg;
  103. /* the adv7170 has an autoincrement function, use it if
  104. * the adapter understands raw I2C */
  105. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  106. /* do raw I2C, not smbus compatible */
  107. struct adv7170 *encoder = i2c_get_clientdata(client);
  108. u8 block_data[32];
  109. int block_len;
  110. while (len >= 2) {
  111. block_len = 0;
  112. block_data[block_len++] = reg = data[0];
  113. do {
  114. block_data[block_len++] =
  115. encoder->reg[reg++] = data[1];
  116. len -= 2;
  117. data += 2;
  118. } while (len >= 2 && data[0] == reg &&
  119. block_len < 32);
  120. if ((ret = i2c_master_send(client, block_data,
  121. block_len)) < 0)
  122. break;
  123. }
  124. } else {
  125. /* do some slow I2C emulation kind of thing */
  126. while (len >= 2) {
  127. reg = *data++;
  128. if ((ret = adv7170_write(client, reg,
  129. *data++)) < 0)
  130. break;
  131. len -= 2;
  132. }
  133. }
  134. return ret;
  135. }
  136. /* ----------------------------------------------------------------------- */
  137. // Output filter: S-Video Composite
  138. #define MR050 0x11 //0x09
  139. #define MR060 0x14 //0x0c
  140. //---------------------------------------------------------------------------
  141. #define TR0MODE 0x4c
  142. #define TR0RST 0x80
  143. #define TR1CAPT 0x00
  144. #define TR1PLAY 0x00
  145. static const unsigned char init_NTSC[] = {
  146. 0x00, 0x10, // MR0
  147. 0x01, 0x20, // MR1
  148. 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
  149. 0x03, 0x80, // MR3
  150. 0x04, 0x30, // MR4
  151. 0x05, 0x00, // Reserved
  152. 0x06, 0x00, // Reserved
  153. 0x07, TR0MODE, // TM0
  154. 0x08, TR1CAPT, // TM1
  155. 0x09, 0x16, // Fsc0
  156. 0x0a, 0x7c, // Fsc1
  157. 0x0b, 0xf0, // Fsc2
  158. 0x0c, 0x21, // Fsc3
  159. 0x0d, 0x00, // Subcarrier Phase
  160. 0x0e, 0x00, // Closed Capt. Ext 0
  161. 0x0f, 0x00, // Closed Capt. Ext 1
  162. 0x10, 0x00, // Closed Capt. 0
  163. 0x11, 0x00, // Closed Capt. 1
  164. 0x12, 0x00, // Pedestal Ctl 0
  165. 0x13, 0x00, // Pedestal Ctl 1
  166. 0x14, 0x00, // Pedestal Ctl 2
  167. 0x15, 0x00, // Pedestal Ctl 3
  168. 0x16, 0x00, // CGMS_WSS_0
  169. 0x17, 0x00, // CGMS_WSS_1
  170. 0x18, 0x00, // CGMS_WSS_2
  171. 0x19, 0x00, // Teletext Ctl
  172. };
  173. static const unsigned char init_PAL[] = {
  174. 0x00, 0x71, // MR0
  175. 0x01, 0x20, // MR1
  176. 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
  177. 0x03, 0x80, // MR3
  178. 0x04, 0x30, // MR4
  179. 0x05, 0x00, // Reserved
  180. 0x06, 0x00, // Reserved
  181. 0x07, TR0MODE, // TM0
  182. 0x08, TR1CAPT, // TM1
  183. 0x09, 0xcb, // Fsc0
  184. 0x0a, 0x8a, // Fsc1
  185. 0x0b, 0x09, // Fsc2
  186. 0x0c, 0x2a, // Fsc3
  187. 0x0d, 0x00, // Subcarrier Phase
  188. 0x0e, 0x00, // Closed Capt. Ext 0
  189. 0x0f, 0x00, // Closed Capt. Ext 1
  190. 0x10, 0x00, // Closed Capt. 0
  191. 0x11, 0x00, // Closed Capt. 1
  192. 0x12, 0x00, // Pedestal Ctl 0
  193. 0x13, 0x00, // Pedestal Ctl 1
  194. 0x14, 0x00, // Pedestal Ctl 2
  195. 0x15, 0x00, // Pedestal Ctl 3
  196. 0x16, 0x00, // CGMS_WSS_0
  197. 0x17, 0x00, // CGMS_WSS_1
  198. 0x18, 0x00, // CGMS_WSS_2
  199. 0x19, 0x00, // Teletext Ctl
  200. };
  201. static int
  202. adv7170_command (struct i2c_client *client,
  203. unsigned int cmd,
  204. void * arg)
  205. {
  206. struct adv7170 *encoder = i2c_get_clientdata(client);
  207. switch (cmd) {
  208. case 0:
  209. #if 0
  210. /* This is just for testing!!! */
  211. adv7170_write_block(client, init_common,
  212. sizeof(init_common));
  213. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  214. adv7170_write(client, 0x07, TR0MODE);
  215. #endif
  216. break;
  217. case ENCODER_GET_CAPABILITIES:
  218. {
  219. struct video_encoder_capability *cap = arg;
  220. cap->flags = VIDEO_ENCODER_PAL |
  221. VIDEO_ENCODER_NTSC;
  222. cap->inputs = 2;
  223. cap->outputs = 1;
  224. }
  225. break;
  226. case ENCODER_SET_NORM:
  227. {
  228. int iarg = *(int *) arg;
  229. dprintk(1, KERN_DEBUG "%s_command: set norm %d",
  230. I2C_NAME(client), iarg);
  231. switch (iarg) {
  232. case VIDEO_MODE_NTSC:
  233. adv7170_write_block(client, init_NTSC,
  234. sizeof(init_NTSC));
  235. if (encoder->input == 0)
  236. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  237. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  238. adv7170_write(client, 0x07, TR0MODE);
  239. break;
  240. case VIDEO_MODE_PAL:
  241. adv7170_write_block(client, init_PAL,
  242. sizeof(init_PAL));
  243. if (encoder->input == 0)
  244. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  245. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  246. adv7170_write(client, 0x07, TR0MODE);
  247. break;
  248. default:
  249. dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
  250. I2C_NAME(client), iarg);
  251. return -EINVAL;
  252. }
  253. dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
  254. norms[iarg]);
  255. encoder->norm = iarg;
  256. }
  257. break;
  258. case ENCODER_SET_INPUT:
  259. {
  260. int iarg = *(int *) arg;
  261. /* RJ: *iarg = 0: input is from decoder
  262. *iarg = 1: input is from ZR36060
  263. *iarg = 2: color bar */
  264. dprintk(1, KERN_DEBUG "%s_command: set input from %s\n",
  265. I2C_NAME(client),
  266. iarg == 0 ? "decoder" : "ZR36060");
  267. switch (iarg) {
  268. case 0:
  269. adv7170_write(client, 0x01, 0x20);
  270. adv7170_write(client, 0x08, TR1CAPT); /* TR1 */
  271. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  272. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  273. adv7170_write(client, 0x07, TR0MODE);
  274. //udelay(10);
  275. break;
  276. case 1:
  277. adv7170_write(client, 0x01, 0x00);
  278. adv7170_write(client, 0x08, TR1PLAY); /* TR1 */
  279. adv7170_write(client, 0x02, 0x08);
  280. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  281. adv7170_write(client, 0x07, TR0MODE);
  282. //udelay(10);
  283. break;
  284. default:
  285. dprintk(1, KERN_ERR "%s: illegal input: %d\n",
  286. I2C_NAME(client), iarg);
  287. return -EINVAL;
  288. }
  289. dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
  290. inputs[iarg]);
  291. encoder->input = iarg;
  292. }
  293. break;
  294. case ENCODER_SET_OUTPUT:
  295. {
  296. int *iarg = arg;
  297. /* not much choice of outputs */
  298. if (*iarg != 0) {
  299. return -EINVAL;
  300. }
  301. }
  302. break;
  303. case ENCODER_ENABLE_OUTPUT:
  304. {
  305. int *iarg = arg;
  306. encoder->enable = !!*iarg;
  307. }
  308. break;
  309. default:
  310. return -EINVAL;
  311. }
  312. return 0;
  313. }
  314. /* ----------------------------------------------------------------------- */
  315. /*
  316. * Generic i2c probe
  317. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  318. */
  319. static unsigned short normal_i2c[] =
  320. { I2C_ADV7170 >> 1, (I2C_ADV7170 >> 1) + 1,
  321. I2C_ADV7171 >> 1, (I2C_ADV7171 >> 1) + 1,
  322. I2C_CLIENT_END
  323. };
  324. static unsigned short ignore = I2C_CLIENT_END;
  325. static struct i2c_client_address_data addr_data = {
  326. .normal_i2c = normal_i2c,
  327. .probe = &ignore,
  328. .ignore = &ignore,
  329. };
  330. static struct i2c_driver i2c_driver_adv7170;
  331. static int
  332. adv7170_detect_client (struct i2c_adapter *adapter,
  333. int address,
  334. int kind)
  335. {
  336. int i;
  337. struct i2c_client *client;
  338. struct adv7170 *encoder;
  339. char *dname;
  340. dprintk(1,
  341. KERN_INFO
  342. "adv7170.c: detecting adv7170 client on address 0x%x\n",
  343. address << 1);
  344. /* Check if the adapter supports the needed features */
  345. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  346. return 0;
  347. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  348. if (client == 0)
  349. return -ENOMEM;
  350. client->addr = address;
  351. client->adapter = adapter;
  352. client->driver = &i2c_driver_adv7170;
  353. if ((client->addr == I2C_ADV7170 >> 1) ||
  354. (client->addr == (I2C_ADV7170 >> 1) + 1)) {
  355. dname = adv7170_name;
  356. } else if ((client->addr == I2C_ADV7171 >> 1) ||
  357. (client->addr == (I2C_ADV7171 >> 1) + 1)) {
  358. dname = adv7171_name;
  359. } else {
  360. /* We should never get here!!! */
  361. kfree(client);
  362. return 0;
  363. }
  364. strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
  365. encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
  366. if (encoder == NULL) {
  367. kfree(client);
  368. return -ENOMEM;
  369. }
  370. encoder->norm = VIDEO_MODE_NTSC;
  371. encoder->input = 0;
  372. encoder->enable = 1;
  373. i2c_set_clientdata(client, encoder);
  374. i = i2c_attach_client(client);
  375. if (i) {
  376. kfree(client);
  377. kfree(encoder);
  378. return i;
  379. }
  380. i = adv7170_write_block(client, init_NTSC, sizeof(init_NTSC));
  381. if (i >= 0) {
  382. i = adv7170_write(client, 0x07, TR0MODE | TR0RST);
  383. i = adv7170_write(client, 0x07, TR0MODE);
  384. i = adv7170_read(client, 0x12);
  385. dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%02x\n",
  386. I2C_NAME(client), i & 1, client->addr << 1);
  387. }
  388. if (i < 0) {
  389. dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
  390. I2C_NAME(client), i);
  391. }
  392. return 0;
  393. }
  394. static int
  395. adv7170_attach_adapter (struct i2c_adapter *adapter)
  396. {
  397. dprintk(1,
  398. KERN_INFO
  399. "adv7170.c: starting probe for adapter %s (0x%x)\n",
  400. I2C_NAME(adapter), adapter->id);
  401. return i2c_probe(adapter, &addr_data, &adv7170_detect_client);
  402. }
  403. static int
  404. adv7170_detach_client (struct i2c_client *client)
  405. {
  406. struct adv7170 *encoder = i2c_get_clientdata(client);
  407. int err;
  408. err = i2c_detach_client(client);
  409. if (err) {
  410. return err;
  411. }
  412. kfree(encoder);
  413. kfree(client);
  414. return 0;
  415. }
  416. /* ----------------------------------------------------------------------- */
  417. static struct i2c_driver i2c_driver_adv7170 = {
  418. .driver = {
  419. .name = "adv7170", /* name */
  420. },
  421. .id = I2C_DRIVERID_ADV7170,
  422. .attach_adapter = adv7170_attach_adapter,
  423. .detach_client = adv7170_detach_client,
  424. .command = adv7170_command,
  425. };
  426. static int __init
  427. adv7170_init (void)
  428. {
  429. return i2c_add_driver(&i2c_driver_adv7170);
  430. }
  431. static void __exit
  432. adv7170_exit (void)
  433. {
  434. i2c_del_driver(&i2c_driver_adv7170);
  435. }
  436. module_init(adv7170_init);
  437. module_exit(adv7170_exit);