adv7170.c 12 KB

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