adv7175.c 13 KB

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