adv7170.c 12 KB

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