adv7175.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <linux/types.h>
  42. #include <linux/videodev.h>
  43. #include <asm/uaccess.h>
  44. MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
  45. MODULE_AUTHOR("Dave Perks");
  46. MODULE_LICENSE("GPL");
  47. #include <linux/i2c.h>
  48. #define I2C_NAME(s) (s)->name
  49. #include <linux/video_encoder.h>
  50. static int debug = 0;
  51. module_param(debug, int, 0);
  52. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  53. #define dprintk(num, format, args...) \
  54. do { \
  55. if (debug >= num) \
  56. printk(format, ##args); \
  57. } while (0)
  58. /* ----------------------------------------------------------------------- */
  59. struct adv7175 {
  60. unsigned char reg[128];
  61. int norm;
  62. int input;
  63. int enable;
  64. int bright;
  65. int contrast;
  66. int hue;
  67. int sat;
  68. };
  69. #define I2C_ADV7175 0xd4
  70. #define I2C_ADV7176 0x54
  71. static char adv7175_name[] = "adv7175";
  72. static char adv7176_name[] = "adv7176";
  73. static char *inputs[] = { "pass_through", "play_back", "color_bar" };
  74. static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
  75. /* ----------------------------------------------------------------------- */
  76. static inline int
  77. adv7175_write (struct i2c_client *client,
  78. u8 reg,
  79. u8 value)
  80. {
  81. struct adv7175 *encoder = i2c_get_clientdata(client);
  82. encoder->reg[reg] = value;
  83. return i2c_smbus_write_byte_data(client, reg, value);
  84. }
  85. static inline int
  86. adv7175_read (struct i2c_client *client,
  87. u8 reg)
  88. {
  89. return i2c_smbus_read_byte_data(client, reg);
  90. }
  91. static int
  92. adv7175_write_block (struct i2c_client *client,
  93. const u8 *data,
  94. unsigned int len)
  95. {
  96. int ret = -1;
  97. u8 reg;
  98. /* the adv7175 has an autoincrement function, use it if
  99. * the adapter understands raw I2C */
  100. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  101. /* do raw I2C, not smbus compatible */
  102. struct adv7175 *encoder = i2c_get_clientdata(client);
  103. struct i2c_msg msg;
  104. u8 block_data[32];
  105. msg.addr = client->addr;
  106. msg.flags = 0;
  107. while (len >= 2) {
  108. msg.buf = (char *) block_data;
  109. msg.len = 0;
  110. block_data[msg.len++] = reg = data[0];
  111. do {
  112. block_data[msg.len++] =
  113. encoder->reg[reg++] = data[1];
  114. len -= 2;
  115. data += 2;
  116. } while (len >= 2 && data[0] == reg &&
  117. msg.len < 32);
  118. if ((ret = i2c_transfer(client->adapter,
  119. &msg, 1)) < 0)
  120. break;
  121. }
  122. } else {
  123. /* do some slow I2C emulation kind of thing */
  124. while (len >= 2) {
  125. reg = *data++;
  126. if ((ret = adv7175_write(client, reg,
  127. *data++)) < 0)
  128. break;
  129. len -= 2;
  130. }
  131. }
  132. return ret;
  133. }
  134. static void
  135. set_subcarrier_freq (struct i2c_client *client,
  136. int pass_through)
  137. {
  138. /* for some reason pass_through NTSC needs
  139. * a different sub-carrier freq to remain stable. */
  140. if(pass_through)
  141. adv7175_write(client, 0x02, 0x00);
  142. else
  143. adv7175_write(client, 0x02, 0x55);
  144. adv7175_write(client, 0x03, 0x55);
  145. adv7175_write(client, 0x04, 0x55);
  146. adv7175_write(client, 0x05, 0x25);
  147. }
  148. #ifdef ENCODER_DUMP
  149. static void
  150. dump (struct i2c_client *client)
  151. {
  152. struct adv7175 *encoder = i2c_get_clientdata(client);
  153. int i, j;
  154. printk(KERN_INFO "%s: registry dump\n", I2C_NAME(client));
  155. for (i = 0; i < 182 / 8; i++) {
  156. printk("%s: 0x%02x -", I2C_NAME(client), i * 8);
  157. for (j = 0; j < 8; j++) {
  158. printk(" 0x%02x", encoder->reg[i * 8 + j]);
  159. }
  160. printk("\n");
  161. }
  162. }
  163. #endif
  164. /* ----------------------------------------------------------------------- */
  165. // Output filter: S-Video Composite
  166. #define MR050 0x11 //0x09
  167. #define MR060 0x14 //0x0c
  168. //---------------------------------------------------------------------------
  169. #define TR0MODE 0x46
  170. #define TR0RST 0x80
  171. #define TR1CAPT 0x80
  172. #define TR1PLAY 0x00
  173. static const unsigned char init_common[] = {
  174. 0x00, MR050, /* MR0, PAL enabled */
  175. 0x01, 0x00, /* MR1 */
  176. 0x02, 0x0c, /* subc. freq. */
  177. 0x03, 0x8c, /* subc. freq. */
  178. 0x04, 0x79, /* subc. freq. */
  179. 0x05, 0x26, /* subc. freq. */
  180. 0x06, 0x40, /* subc. phase */
  181. 0x07, TR0MODE, /* TR0, 16bit */
  182. 0x08, 0x21, /* */
  183. 0x09, 0x00, /* */
  184. 0x0a, 0x00, /* */
  185. 0x0b, 0x00, /* */
  186. 0x0c, TR1CAPT, /* TR1 */
  187. 0x0d, 0x4f, /* MR2 */
  188. 0x0e, 0x00, /* */
  189. 0x0f, 0x00, /* */
  190. 0x10, 0x00, /* */
  191. 0x11, 0x00, /* */
  192. };
  193. static const unsigned char init_pal[] = {
  194. 0x00, MR050, /* MR0, PAL enabled */
  195. 0x01, 0x00, /* MR1 */
  196. 0x02, 0x0c, /* subc. freq. */
  197. 0x03, 0x8c, /* subc. freq. */
  198. 0x04, 0x79, /* subc. freq. */
  199. 0x05, 0x26, /* subc. freq. */
  200. 0x06, 0x40, /* subc. phase */
  201. };
  202. static const unsigned char init_ntsc[] = {
  203. 0x00, MR060, /* MR0, NTSC enabled */
  204. 0x01, 0x00, /* MR1 */
  205. 0x02, 0x55, /* subc. freq. */
  206. 0x03, 0x55, /* subc. freq. */
  207. 0x04, 0x55, /* subc. freq. */
  208. 0x05, 0x25, /* subc. freq. */
  209. 0x06, 0x1a, /* subc. phase */
  210. };
  211. static int
  212. adv7175_command (struct i2c_client *client,
  213. unsigned int cmd,
  214. void *arg)
  215. {
  216. struct adv7175 *encoder = i2c_get_clientdata(client);
  217. switch (cmd) {
  218. case 0:
  219. /* This is just for testing!!! */
  220. adv7175_write_block(client, init_common,
  221. sizeof(init_common));
  222. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  223. adv7175_write(client, 0x07, TR0MODE);
  224. break;
  225. case ENCODER_GET_CAPABILITIES:
  226. {
  227. struct video_encoder_capability *cap = arg;
  228. cap->flags = VIDEO_ENCODER_PAL |
  229. VIDEO_ENCODER_NTSC |
  230. VIDEO_ENCODER_SECAM; /* well, hacky */
  231. cap->inputs = 2;
  232. cap->outputs = 1;
  233. }
  234. break;
  235. case ENCODER_SET_NORM:
  236. {
  237. int iarg = *(int *) arg;
  238. switch (iarg) {
  239. case VIDEO_MODE_NTSC:
  240. adv7175_write_block(client, init_ntsc,
  241. sizeof(init_ntsc));
  242. if (encoder->input == 0)
  243. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  244. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  245. adv7175_write(client, 0x07, TR0MODE);
  246. break;
  247. case VIDEO_MODE_PAL:
  248. adv7175_write_block(client, init_pal,
  249. sizeof(init_pal));
  250. if (encoder->input == 0)
  251. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  252. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  253. adv7175_write(client, 0x07, TR0MODE);
  254. break;
  255. case VIDEO_MODE_SECAM: // WARNING! ADV7176 does not support SECAM.
  256. /* This is an attempt to convert
  257. * SECAM->PAL (typically it does not work
  258. * due to genlock: when decoder is in SECAM
  259. * and encoder in in PAL the subcarrier can
  260. * not be syncronized with horizontal
  261. * quency) */
  262. adv7175_write_block(client, init_pal,
  263. sizeof(init_pal));
  264. if (encoder->input == 0)
  265. adv7175_write(client, 0x0d, 0x49); // Disable genlock
  266. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  267. adv7175_write(client, 0x07, TR0MODE);
  268. break;
  269. default:
  270. dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
  271. I2C_NAME(client), iarg);
  272. return -EINVAL;
  273. }
  274. dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
  275. norms[iarg]);
  276. encoder->norm = iarg;
  277. }
  278. break;
  279. case ENCODER_SET_INPUT:
  280. {
  281. int iarg = *(int *) arg;
  282. /* RJ: *iarg = 0: input is from SAA7110
  283. *iarg = 1: input is from ZR36060
  284. *iarg = 2: color bar */
  285. switch (iarg) {
  286. case 0:
  287. adv7175_write(client, 0x01, 0x00);
  288. if (encoder->norm == VIDEO_MODE_NTSC)
  289. set_subcarrier_freq(client, 1);
  290. adv7175_write(client, 0x0c, TR1CAPT); /* TR1 */
  291. if (encoder->norm == VIDEO_MODE_SECAM)
  292. adv7175_write(client, 0x0d, 0x49); // Disable genlock
  293. else
  294. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  295. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  296. adv7175_write(client, 0x07, TR0MODE);
  297. //udelay(10);
  298. break;
  299. case 1:
  300. adv7175_write(client, 0x01, 0x00);
  301. if (encoder->norm == VIDEO_MODE_NTSC)
  302. set_subcarrier_freq(client, 0);
  303. adv7175_write(client, 0x0c, TR1PLAY); /* TR1 */
  304. adv7175_write(client, 0x0d, 0x49);
  305. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  306. adv7175_write(client, 0x07, TR0MODE);
  307. //udelay(10);
  308. break;
  309. case 2:
  310. adv7175_write(client, 0x01, 0x80);
  311. if (encoder->norm == VIDEO_MODE_NTSC)
  312. set_subcarrier_freq(client, 0);
  313. adv7175_write(client, 0x0d, 0x49);
  314. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  315. adv7175_write(client, 0x07, TR0MODE);
  316. //udelay(10);
  317. break;
  318. default:
  319. dprintk(1, KERN_ERR "%s: illegal input: %d\n",
  320. I2C_NAME(client), iarg);
  321. return -EINVAL;
  322. }
  323. dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
  324. inputs[iarg]);
  325. encoder->input = iarg;
  326. }
  327. break;
  328. case ENCODER_SET_OUTPUT:
  329. {
  330. int *iarg = arg;
  331. /* not much choice of outputs */
  332. if (*iarg != 0) {
  333. return -EINVAL;
  334. }
  335. }
  336. break;
  337. case ENCODER_ENABLE_OUTPUT:
  338. {
  339. int *iarg = arg;
  340. encoder->enable = !!*iarg;
  341. }
  342. break;
  343. #ifdef ENCODER_DUMP
  344. case ENCODER_DUMP:
  345. {
  346. dump(client);
  347. }
  348. break;
  349. #endif
  350. default:
  351. return -EINVAL;
  352. }
  353. return 0;
  354. }
  355. /* ----------------------------------------------------------------------- */
  356. /*
  357. * Generic i2c probe
  358. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  359. */
  360. static unsigned short normal_i2c[] =
  361. { I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
  362. I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
  363. I2C_CLIENT_END
  364. };
  365. static unsigned short ignore = I2C_CLIENT_END;
  366. static struct i2c_client_address_data addr_data = {
  367. .normal_i2c = normal_i2c,
  368. .probe = &ignore,
  369. .ignore = &ignore,
  370. };
  371. static struct i2c_driver i2c_driver_adv7175;
  372. static int
  373. adv7175_detect_client (struct i2c_adapter *adapter,
  374. int address,
  375. int kind)
  376. {
  377. int i;
  378. struct i2c_client *client;
  379. struct adv7175 *encoder;
  380. char *dname;
  381. dprintk(1,
  382. KERN_INFO
  383. "adv7175.c: detecting adv7175 client on address 0x%x\n",
  384. address << 1);
  385. /* Check if the adapter supports the needed features */
  386. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  387. return 0;
  388. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  389. if (client == 0)
  390. return -ENOMEM;
  391. client->addr = address;
  392. client->adapter = adapter;
  393. client->driver = &i2c_driver_adv7175;
  394. if ((client->addr == I2C_ADV7175 >> 1) ||
  395. (client->addr == (I2C_ADV7175 >> 1) + 1)) {
  396. dname = adv7175_name;
  397. } else if ((client->addr == I2C_ADV7176 >> 1) ||
  398. (client->addr == (I2C_ADV7176 >> 1) + 1)) {
  399. dname = adv7176_name;
  400. } else {
  401. /* We should never get here!!! */
  402. kfree(client);
  403. return 0;
  404. }
  405. strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
  406. encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL);
  407. if (encoder == NULL) {
  408. kfree(client);
  409. return -ENOMEM;
  410. }
  411. encoder->norm = VIDEO_MODE_PAL;
  412. encoder->input = 0;
  413. encoder->enable = 1;
  414. i2c_set_clientdata(client, encoder);
  415. i = i2c_attach_client(client);
  416. if (i) {
  417. kfree(client);
  418. kfree(encoder);
  419. return i;
  420. }
  421. i = adv7175_write_block(client, init_common, sizeof(init_common));
  422. if (i >= 0) {
  423. i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
  424. i = adv7175_write(client, 0x07, TR0MODE);
  425. i = adv7175_read(client, 0x12);
  426. dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%x\n",
  427. I2C_NAME(client), i & 1, client->addr << 1);
  428. }
  429. if (i < 0) {
  430. dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
  431. I2C_NAME(client), i);
  432. }
  433. return 0;
  434. }
  435. static int
  436. adv7175_attach_adapter (struct i2c_adapter *adapter)
  437. {
  438. dprintk(1,
  439. KERN_INFO
  440. "adv7175.c: starting probe for adapter %s (0x%x)\n",
  441. I2C_NAME(adapter), adapter->id);
  442. return i2c_probe(adapter, &addr_data, &adv7175_detect_client);
  443. }
  444. static int
  445. adv7175_detach_client (struct i2c_client *client)
  446. {
  447. struct adv7175 *encoder = i2c_get_clientdata(client);
  448. int err;
  449. err = i2c_detach_client(client);
  450. if (err) {
  451. return err;
  452. }
  453. kfree(encoder);
  454. kfree(client);
  455. return 0;
  456. }
  457. /* ----------------------------------------------------------------------- */
  458. static struct i2c_driver i2c_driver_adv7175 = {
  459. .driver = {
  460. .name = "adv7175", /* name */
  461. },
  462. .id = I2C_DRIVERID_ADV7175,
  463. .attach_adapter = adv7175_attach_adapter,
  464. .detach_client = adv7175_detach_client,
  465. .command = adv7175_command,
  466. };
  467. static int __init
  468. adv7175_init (void)
  469. {
  470. return i2c_add_driver(&i2c_driver_adv7175);
  471. }
  472. static void __exit
  473. adv7175_exit (void)
  474. {
  475. i2c_del_driver(&i2c_driver_adv7175);
  476. }
  477. module_init(adv7175_init);
  478. module_exit(adv7175_exit);