adv7175.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * adv7175 - adv7175a video encoder driver version 0.0.3
  3. *
  4. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  5. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  6. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  7. * - some corrections for Pinnacle Systems Inc. DC10plus card.
  8. *
  9. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  10. * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/errno.h>
  30. #include <linux/fs.h>
  31. #include <linux/kernel.h>
  32. #include <linux/major.h>
  33. #include <linux/slab.h>
  34. #include <linux/mm.h>
  35. #include <linux/pci.h>
  36. #include <linux/signal.h>
  37. #include <asm/io.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/page.h>
  40. #include <linux/sched.h>
  41. #include <asm/segment.h>
  42. #include <linux/types.h>
  43. #include <linux/videodev.h>
  44. #include <asm/uaccess.h>
  45. MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
  46. MODULE_AUTHOR("Dave Perks");
  47. MODULE_LICENSE("GPL");
  48. #include <linux/i2c.h>
  49. #include <linux/i2c-dev.h>
  50. #define I2C_NAME(s) (s)->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 adv7175 {
  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_ADV7175 0xd4
  72. #define I2C_ADV7176 0x54
  73. static char adv7175_name[] = "adv7175";
  74. static char adv7176_name[] = "adv7176";
  75. static char *inputs[] = { "pass_through", "play_back", "color_bar" };
  76. static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
  77. /* ----------------------------------------------------------------------- */
  78. static inline int
  79. adv7175_write (struct i2c_client *client,
  80. u8 reg,
  81. u8 value)
  82. {
  83. struct adv7175 *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. adv7175_read (struct i2c_client *client,
  89. u8 reg)
  90. {
  91. return i2c_smbus_read_byte_data(client, reg);
  92. }
  93. static int
  94. adv7175_write_block (struct i2c_client *client,
  95. const u8 *data,
  96. unsigned int len)
  97. {
  98. int ret = -1;
  99. u8 reg;
  100. /* the adv7175 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 adv7175 *encoder = i2c_get_clientdata(client);
  105. struct i2c_msg msg;
  106. u8 block_data[32];
  107. msg.addr = client->addr;
  108. msg.flags = 0;
  109. while (len >= 2) {
  110. msg.buf = (char *) block_data;
  111. msg.len = 0;
  112. block_data[msg.len++] = reg = data[0];
  113. do {
  114. block_data[msg.len++] =
  115. encoder->reg[reg++] = data[1];
  116. len -= 2;
  117. data += 2;
  118. } while (len >= 2 && data[0] == reg &&
  119. msg.len < 32);
  120. if ((ret = i2c_transfer(client->adapter,
  121. &msg, 1)) < 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 = adv7175_write(client, reg,
  129. *data++)) < 0)
  130. break;
  131. len -= 2;
  132. }
  133. }
  134. return ret;
  135. }
  136. static void
  137. set_subcarrier_freq (struct i2c_client *client,
  138. int pass_through)
  139. {
  140. /* for some reason pass_through NTSC needs
  141. * a different sub-carrier freq to remain stable. */
  142. if(pass_through)
  143. adv7175_write(client, 0x02, 0x00);
  144. else
  145. adv7175_write(client, 0x02, 0x55);
  146. adv7175_write(client, 0x03, 0x55);
  147. adv7175_write(client, 0x04, 0x55);
  148. adv7175_write(client, 0x05, 0x25);
  149. }
  150. #ifdef ENCODER_DUMP
  151. static void
  152. dump (struct i2c_client *client)
  153. {
  154. struct adv7175 *encoder = i2c_get_clientdata(client);
  155. int i, j;
  156. printk(KERN_INFO "%s: registry dump\n", I2C_NAME(client));
  157. for (i = 0; i < 182 / 8; i++) {
  158. printk("%s: 0x%02x -", I2C_NAME(client), i * 8);
  159. for (j = 0; j < 8; j++) {
  160. printk(" 0x%02x", encoder->reg[i * 8 + j]);
  161. }
  162. printk("\n");
  163. }
  164. }
  165. #endif
  166. /* ----------------------------------------------------------------------- */
  167. // Output filter: S-Video Composite
  168. #define MR050 0x11 //0x09
  169. #define MR060 0x14 //0x0c
  170. //---------------------------------------------------------------------------
  171. #define TR0MODE 0x46
  172. #define TR0RST 0x80
  173. #define TR1CAPT 0x80
  174. #define TR1PLAY 0x00
  175. static const unsigned char init_common[] = {
  176. 0x00, MR050, /* MR0, PAL enabled */
  177. 0x01, 0x00, /* MR1 */
  178. 0x02, 0x0c, /* subc. freq. */
  179. 0x03, 0x8c, /* subc. freq. */
  180. 0x04, 0x79, /* subc. freq. */
  181. 0x05, 0x26, /* subc. freq. */
  182. 0x06, 0x40, /* subc. phase */
  183. 0x07, TR0MODE, /* TR0, 16bit */
  184. 0x08, 0x21, /* */
  185. 0x09, 0x00, /* */
  186. 0x0a, 0x00, /* */
  187. 0x0b, 0x00, /* */
  188. 0x0c, TR1CAPT, /* TR1 */
  189. 0x0d, 0x4f, /* MR2 */
  190. 0x0e, 0x00, /* */
  191. 0x0f, 0x00, /* */
  192. 0x10, 0x00, /* */
  193. 0x11, 0x00, /* */
  194. };
  195. static const unsigned char init_pal[] = {
  196. 0x00, MR050, /* MR0, PAL enabled */
  197. 0x01, 0x00, /* MR1 */
  198. 0x02, 0x0c, /* subc. freq. */
  199. 0x03, 0x8c, /* subc. freq. */
  200. 0x04, 0x79, /* subc. freq. */
  201. 0x05, 0x26, /* subc. freq. */
  202. 0x06, 0x40, /* subc. phase */
  203. };
  204. static const unsigned char init_ntsc[] = {
  205. 0x00, MR060, /* MR0, NTSC enabled */
  206. 0x01, 0x00, /* MR1 */
  207. 0x02, 0x55, /* subc. freq. */
  208. 0x03, 0x55, /* subc. freq. */
  209. 0x04, 0x55, /* subc. freq. */
  210. 0x05, 0x25, /* subc. freq. */
  211. 0x06, 0x1a, /* subc. phase */
  212. };
  213. static int
  214. adv7175_command (struct i2c_client *client,
  215. unsigned int cmd,
  216. void *arg)
  217. {
  218. struct adv7175 *encoder = i2c_get_clientdata(client);
  219. switch (cmd) {
  220. case 0:
  221. /* This is just for testing!!! */
  222. adv7175_write_block(client, init_common,
  223. sizeof(init_common));
  224. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  225. adv7175_write(client, 0x07, TR0MODE);
  226. break;
  227. case ENCODER_GET_CAPABILITIES:
  228. {
  229. struct video_encoder_capability *cap = arg;
  230. cap->flags = VIDEO_ENCODER_PAL |
  231. VIDEO_ENCODER_NTSC |
  232. VIDEO_ENCODER_SECAM; /* well, hacky */
  233. cap->inputs = 2;
  234. cap->outputs = 1;
  235. }
  236. break;
  237. case ENCODER_SET_NORM:
  238. {
  239. int iarg = *(int *) arg;
  240. switch (iarg) {
  241. case VIDEO_MODE_NTSC:
  242. adv7175_write_block(client, init_ntsc,
  243. sizeof(init_ntsc));
  244. if (encoder->input == 0)
  245. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  246. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  247. adv7175_write(client, 0x07, TR0MODE);
  248. break;
  249. case VIDEO_MODE_PAL:
  250. adv7175_write_block(client, init_pal,
  251. sizeof(init_pal));
  252. if (encoder->input == 0)
  253. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  254. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  255. adv7175_write(client, 0x07, TR0MODE);
  256. break;
  257. case VIDEO_MODE_SECAM: // WARNING! ADV7176 does not support SECAM.
  258. /* This is an attempt to convert
  259. * SECAM->PAL (typically it does not work
  260. * due to genlock: when decoder is in SECAM
  261. * and encoder in in PAL the subcarrier can
  262. * not be syncronized with horizontal
  263. * quency) */
  264. adv7175_write_block(client, init_pal,
  265. sizeof(init_pal));
  266. if (encoder->input == 0)
  267. adv7175_write(client, 0x0d, 0x49); // Disable genlock
  268. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  269. adv7175_write(client, 0x07, TR0MODE);
  270. break;
  271. default:
  272. dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
  273. I2C_NAME(client), iarg);
  274. return -EINVAL;
  275. }
  276. dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
  277. norms[iarg]);
  278. encoder->norm = iarg;
  279. }
  280. break;
  281. case ENCODER_SET_INPUT:
  282. {
  283. int iarg = *(int *) arg;
  284. /* RJ: *iarg = 0: input is from SAA7110
  285. *iarg = 1: input is from ZR36060
  286. *iarg = 2: color bar */
  287. switch (iarg) {
  288. case 0:
  289. adv7175_write(client, 0x01, 0x00);
  290. if (encoder->norm == VIDEO_MODE_NTSC)
  291. set_subcarrier_freq(client, 1);
  292. adv7175_write(client, 0x0c, TR1CAPT); /* TR1 */
  293. if (encoder->norm == VIDEO_MODE_SECAM)
  294. adv7175_write(client, 0x0d, 0x49); // Disable genlock
  295. else
  296. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  297. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  298. adv7175_write(client, 0x07, TR0MODE);
  299. //udelay(10);
  300. break;
  301. case 1:
  302. adv7175_write(client, 0x01, 0x00);
  303. if (encoder->norm == VIDEO_MODE_NTSC)
  304. set_subcarrier_freq(client, 0);
  305. adv7175_write(client, 0x0c, TR1PLAY); /* TR1 */
  306. adv7175_write(client, 0x0d, 0x49);
  307. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  308. adv7175_write(client, 0x07, TR0MODE);
  309. //udelay(10);
  310. break;
  311. case 2:
  312. adv7175_write(client, 0x01, 0x80);
  313. if (encoder->norm == VIDEO_MODE_NTSC)
  314. set_subcarrier_freq(client, 0);
  315. adv7175_write(client, 0x0d, 0x49);
  316. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  317. adv7175_write(client, 0x07, TR0MODE);
  318. //udelay(10);
  319. break;
  320. default:
  321. dprintk(1, KERN_ERR "%s: illegal input: %d\n",
  322. I2C_NAME(client), iarg);
  323. return -EINVAL;
  324. }
  325. dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
  326. inputs[iarg]);
  327. encoder->input = iarg;
  328. }
  329. break;
  330. case ENCODER_SET_OUTPUT:
  331. {
  332. int *iarg = arg;
  333. /* not much choice of outputs */
  334. if (*iarg != 0) {
  335. return -EINVAL;
  336. }
  337. }
  338. break;
  339. case ENCODER_ENABLE_OUTPUT:
  340. {
  341. int *iarg = arg;
  342. encoder->enable = !!*iarg;
  343. }
  344. break;
  345. #ifdef ENCODER_DUMP
  346. case ENCODER_DUMP:
  347. {
  348. dump(client);
  349. }
  350. break;
  351. #endif
  352. default:
  353. return -EINVAL;
  354. }
  355. return 0;
  356. }
  357. /* ----------------------------------------------------------------------- */
  358. /*
  359. * Generic i2c probe
  360. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  361. */
  362. static unsigned short normal_i2c[] =
  363. { I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
  364. I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
  365. I2C_CLIENT_END
  366. };
  367. static unsigned short ignore = I2C_CLIENT_END;
  368. static struct i2c_client_address_data addr_data = {
  369. .normal_i2c = normal_i2c,
  370. .probe = &ignore,
  371. .ignore = &ignore,
  372. .force = &ignore,
  373. };
  374. static struct i2c_driver i2c_driver_adv7175;
  375. static int
  376. adv7175_detect_client (struct i2c_adapter *adapter,
  377. int address,
  378. int kind)
  379. {
  380. int i;
  381. struct i2c_client *client;
  382. struct adv7175 *encoder;
  383. char *dname;
  384. dprintk(1,
  385. KERN_INFO
  386. "adv7175.c: detecting adv7175 client on address 0x%x\n",
  387. address << 1);
  388. /* Check if the adapter supports the needed features */
  389. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  390. return 0;
  391. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  392. if (client == 0)
  393. return -ENOMEM;
  394. memset(client, 0, sizeof(struct i2c_client));
  395. client->addr = address;
  396. client->adapter = adapter;
  397. client->driver = &i2c_driver_adv7175;
  398. client->flags = I2C_CLIENT_ALLOW_USE;
  399. if ((client->addr == I2C_ADV7175 >> 1) ||
  400. (client->addr == (I2C_ADV7175 >> 1) + 1)) {
  401. dname = adv7175_name;
  402. } else if ((client->addr == I2C_ADV7176 >> 1) ||
  403. (client->addr == (I2C_ADV7176 >> 1) + 1)) {
  404. dname = adv7176_name;
  405. } else {
  406. /* We should never get here!!! */
  407. kfree(client);
  408. return 0;
  409. }
  410. strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
  411. encoder = kmalloc(sizeof(struct adv7175), GFP_KERNEL);
  412. if (encoder == NULL) {
  413. kfree(client);
  414. return -ENOMEM;
  415. }
  416. memset(encoder, 0, sizeof(struct adv7175));
  417. encoder->norm = VIDEO_MODE_PAL;
  418. encoder->input = 0;
  419. encoder->enable = 1;
  420. i2c_set_clientdata(client, encoder);
  421. i = i2c_attach_client(client);
  422. if (i) {
  423. kfree(client);
  424. kfree(encoder);
  425. return i;
  426. }
  427. i = adv7175_write_block(client, init_common, sizeof(init_common));
  428. if (i >= 0) {
  429. i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
  430. i = adv7175_write(client, 0x07, TR0MODE);
  431. i = adv7175_read(client, 0x12);
  432. dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%x\n",
  433. I2C_NAME(client), i & 1, client->addr << 1);
  434. }
  435. if (i < 0) {
  436. dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
  437. I2C_NAME(client), i);
  438. }
  439. return 0;
  440. }
  441. static int
  442. adv7175_attach_adapter (struct i2c_adapter *adapter)
  443. {
  444. dprintk(1,
  445. KERN_INFO
  446. "adv7175.c: starting probe for adapter %s (0x%x)\n",
  447. I2C_NAME(adapter), adapter->id);
  448. return i2c_probe(adapter, &addr_data, &adv7175_detect_client);
  449. }
  450. static int
  451. adv7175_detach_client (struct i2c_client *client)
  452. {
  453. struct adv7175 *encoder = i2c_get_clientdata(client);
  454. int err;
  455. err = i2c_detach_client(client);
  456. if (err) {
  457. return err;
  458. }
  459. kfree(encoder);
  460. kfree(client);
  461. return 0;
  462. }
  463. /* ----------------------------------------------------------------------- */
  464. static struct i2c_driver i2c_driver_adv7175 = {
  465. .owner = THIS_MODULE,
  466. .name = "adv7175", /* name */
  467. .id = I2C_DRIVERID_ADV7175,
  468. .flags = I2C_DF_NOTIFY,
  469. .attach_adapter = adv7175_attach_adapter,
  470. .detach_client = adv7175_detach_client,
  471. .command = adv7175_command,
  472. };
  473. static int __init
  474. adv7175_init (void)
  475. {
  476. return i2c_add_driver(&i2c_driver_adv7175);
  477. }
  478. static void __exit
  479. adv7175_exit (void)
  480. {
  481. i2c_del_driver(&i2c_driver_adv7175);
  482. }
  483. module_init(adv7175_init);
  484. module_exit(adv7175_exit);