adv7170.c 12 KB

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