adv7170.c 12 KB

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