adv7175.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/types.h>
  28. #include <linux/ioctl.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/i2c.h>
  31. #include <linux/i2c-id.h>
  32. #include <linux/videodev.h>
  33. #include <linux/video_encoder.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-i2c-drv-legacy.h>
  36. MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
  37. MODULE_AUTHOR("Dave Perks");
  38. MODULE_LICENSE("GPL");
  39. static int debug;
  40. module_param(debug, int, 0);
  41. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  42. /* ----------------------------------------------------------------------- */
  43. struct adv7175 {
  44. int norm;
  45. int input;
  46. int enable;
  47. int bright;
  48. int contrast;
  49. int hue;
  50. int sat;
  51. };
  52. #define I2C_ADV7175 0xd4
  53. #define I2C_ADV7176 0x54
  54. static char *inputs[] = { "pass_through", "play_back", "color_bar" };
  55. static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
  56. /* ----------------------------------------------------------------------- */
  57. static inline int adv7175_write(struct i2c_client *client, u8 reg, u8 value)
  58. {
  59. return i2c_smbus_write_byte_data(client, reg, value);
  60. }
  61. static inline int adv7175_read(struct i2c_client *client, u8 reg)
  62. {
  63. return i2c_smbus_read_byte_data(client, reg);
  64. }
  65. static int adv7175_write_block(struct i2c_client *client,
  66. const u8 *data, unsigned int len)
  67. {
  68. int ret = -1;
  69. u8 reg;
  70. /* the adv7175 has an autoincrement function, use it if
  71. * the adapter understands raw I2C */
  72. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  73. /* do raw I2C, not smbus compatible */
  74. u8 block_data[32];
  75. int block_len;
  76. while (len >= 2) {
  77. block_len = 0;
  78. block_data[block_len++] = reg = data[0];
  79. do {
  80. block_data[block_len++] = data[1];
  81. reg++;
  82. len -= 2;
  83. data += 2;
  84. } while (len >= 2 && data[0] == reg && block_len < 32);
  85. ret = i2c_master_send(client, block_data, block_len);
  86. if (ret < 0)
  87. break;
  88. }
  89. } else {
  90. /* do some slow I2C emulation kind of thing */
  91. while (len >= 2) {
  92. reg = *data++;
  93. ret = adv7175_write(client, reg, *data++);
  94. if (ret < 0)
  95. break;
  96. len -= 2;
  97. }
  98. }
  99. return ret;
  100. }
  101. static void set_subcarrier_freq(struct i2c_client *client, int pass_through)
  102. {
  103. /* for some reason pass_through NTSC needs
  104. * a different sub-carrier freq to remain stable. */
  105. if (pass_through)
  106. adv7175_write(client, 0x02, 0x00);
  107. else
  108. adv7175_write(client, 0x02, 0x55);
  109. adv7175_write(client, 0x03, 0x55);
  110. adv7175_write(client, 0x04, 0x55);
  111. adv7175_write(client, 0x05, 0x25);
  112. }
  113. /* ----------------------------------------------------------------------- */
  114. /* Output filter: S-Video Composite */
  115. #define MR050 0x11 /* 0x09 */
  116. #define MR060 0x14 /* 0x0c */
  117. /* ----------------------------------------------------------------------- */
  118. #define TR0MODE 0x46
  119. #define TR0RST 0x80
  120. #define TR1CAPT 0x80
  121. #define TR1PLAY 0x00
  122. static const unsigned char init_common[] = {
  123. 0x00, MR050, /* MR0, PAL enabled */
  124. 0x01, 0x00, /* MR1 */
  125. 0x02, 0x0c, /* subc. freq. */
  126. 0x03, 0x8c, /* subc. freq. */
  127. 0x04, 0x79, /* subc. freq. */
  128. 0x05, 0x26, /* subc. freq. */
  129. 0x06, 0x40, /* subc. phase */
  130. 0x07, TR0MODE, /* TR0, 16bit */
  131. 0x08, 0x21, /* */
  132. 0x09, 0x00, /* */
  133. 0x0a, 0x00, /* */
  134. 0x0b, 0x00, /* */
  135. 0x0c, TR1CAPT, /* TR1 */
  136. 0x0d, 0x4f, /* MR2 */
  137. 0x0e, 0x00, /* */
  138. 0x0f, 0x00, /* */
  139. 0x10, 0x00, /* */
  140. 0x11, 0x00, /* */
  141. };
  142. static const unsigned char init_pal[] = {
  143. 0x00, MR050, /* MR0, PAL enabled */
  144. 0x01, 0x00, /* MR1 */
  145. 0x02, 0x0c, /* subc. freq. */
  146. 0x03, 0x8c, /* subc. freq. */
  147. 0x04, 0x79, /* subc. freq. */
  148. 0x05, 0x26, /* subc. freq. */
  149. 0x06, 0x40, /* subc. phase */
  150. };
  151. static const unsigned char init_ntsc[] = {
  152. 0x00, MR060, /* MR0, NTSC enabled */
  153. 0x01, 0x00, /* MR1 */
  154. 0x02, 0x55, /* subc. freq. */
  155. 0x03, 0x55, /* subc. freq. */
  156. 0x04, 0x55, /* subc. freq. */
  157. 0x05, 0x25, /* subc. freq. */
  158. 0x06, 0x1a, /* subc. phase */
  159. };
  160. static int adv7175_command(struct i2c_client *client, unsigned cmd, void *arg)
  161. {
  162. struct adv7175 *encoder = i2c_get_clientdata(client);
  163. switch (cmd) {
  164. case 0:
  165. /* This is just for testing!!! */
  166. adv7175_write_block(client, init_common,
  167. sizeof(init_common));
  168. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  169. adv7175_write(client, 0x07, TR0MODE);
  170. break;
  171. case ENCODER_GET_CAPABILITIES:
  172. {
  173. struct video_encoder_capability *cap = arg;
  174. cap->flags = VIDEO_ENCODER_PAL |
  175. VIDEO_ENCODER_NTSC |
  176. VIDEO_ENCODER_SECAM; /* well, hacky */
  177. cap->inputs = 2;
  178. cap->outputs = 1;
  179. break;
  180. }
  181. case ENCODER_SET_NORM:
  182. {
  183. int iarg = *(int *) arg;
  184. switch (iarg) {
  185. case VIDEO_MODE_NTSC:
  186. adv7175_write_block(client, init_ntsc,
  187. sizeof(init_ntsc));
  188. if (encoder->input == 0)
  189. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  190. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  191. adv7175_write(client, 0x07, TR0MODE);
  192. break;
  193. case VIDEO_MODE_PAL:
  194. adv7175_write_block(client, init_pal,
  195. sizeof(init_pal));
  196. if (encoder->input == 0)
  197. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  198. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  199. adv7175_write(client, 0x07, TR0MODE);
  200. break;
  201. case VIDEO_MODE_SECAM: // WARNING! ADV7176 does not support SECAM.
  202. /* This is an attempt to convert
  203. * SECAM->PAL (typically it does not work
  204. * due to genlock: when decoder is in SECAM
  205. * and encoder in in PAL the subcarrier can
  206. * not be syncronized with horizontal
  207. * quency) */
  208. adv7175_write_block(client, init_pal,
  209. sizeof(init_pal));
  210. if (encoder->input == 0)
  211. adv7175_write(client, 0x0d, 0x49); // Disable genlock
  212. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  213. adv7175_write(client, 0x07, TR0MODE);
  214. break;
  215. default:
  216. v4l_dbg(1, debug, client, "illegal norm: %d\n", iarg);
  217. return -EINVAL;
  218. }
  219. v4l_dbg(1, debug, client, "switched to %s\n", norms[iarg]);
  220. encoder->norm = iarg;
  221. break;
  222. }
  223. case ENCODER_SET_INPUT:
  224. {
  225. int iarg = *(int *) arg;
  226. /* RJ: *iarg = 0: input is from SAA7110
  227. *iarg = 1: input is from ZR36060
  228. *iarg = 2: color bar */
  229. switch (iarg) {
  230. case 0:
  231. adv7175_write(client, 0x01, 0x00);
  232. if (encoder->norm == VIDEO_MODE_NTSC)
  233. set_subcarrier_freq(client, 1);
  234. adv7175_write(client, 0x0c, TR1CAPT); /* TR1 */
  235. if (encoder->norm == VIDEO_MODE_SECAM)
  236. adv7175_write(client, 0x0d, 0x49); // Disable genlock
  237. else
  238. adv7175_write(client, 0x0d, 0x4f); // Enable genlock
  239. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  240. adv7175_write(client, 0x07, TR0MODE);
  241. //udelay(10);
  242. break;
  243. case 1:
  244. adv7175_write(client, 0x01, 0x00);
  245. if (encoder->norm == VIDEO_MODE_NTSC)
  246. set_subcarrier_freq(client, 0);
  247. adv7175_write(client, 0x0c, TR1PLAY); /* TR1 */
  248. adv7175_write(client, 0x0d, 0x49);
  249. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  250. adv7175_write(client, 0x07, TR0MODE);
  251. /* udelay(10); */
  252. break;
  253. case 2:
  254. adv7175_write(client, 0x01, 0x80);
  255. if (encoder->norm == VIDEO_MODE_NTSC)
  256. set_subcarrier_freq(client, 0);
  257. adv7175_write(client, 0x0d, 0x49);
  258. adv7175_write(client, 0x07, TR0MODE | TR0RST);
  259. adv7175_write(client, 0x07, TR0MODE);
  260. /* udelay(10); */
  261. break;
  262. default:
  263. v4l_dbg(1, debug, client, "illegal input: %d\n", iarg);
  264. return -EINVAL;
  265. }
  266. v4l_dbg(1, debug, client, "switched to %s\n", inputs[iarg]);
  267. encoder->input = iarg;
  268. break;
  269. }
  270. case ENCODER_SET_OUTPUT:
  271. {
  272. int *iarg = arg;
  273. /* not much choice of outputs */
  274. if (*iarg != 0)
  275. return -EINVAL;
  276. break;
  277. }
  278. case ENCODER_ENABLE_OUTPUT:
  279. {
  280. int *iarg = arg;
  281. encoder->enable = !!*iarg;
  282. break;
  283. }
  284. default:
  285. return -EINVAL;
  286. }
  287. return 0;
  288. }
  289. /* ----------------------------------------------------------------------- */
  290. /*
  291. * Generic i2c probe
  292. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  293. */
  294. static unsigned short normal_i2c[] = {
  295. I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
  296. I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
  297. I2C_CLIENT_END
  298. };
  299. I2C_CLIENT_INSMOD;
  300. static int adv7175_probe(struct i2c_client *client,
  301. const struct i2c_device_id *id)
  302. {
  303. int i;
  304. struct adv7175 *encoder;
  305. /* Check if the adapter supports the needed features */
  306. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  307. return -ENODEV;
  308. v4l_info(client, "chip found @ 0x%x (%s)\n",
  309. client->addr << 1, client->adapter->name);
  310. encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL);
  311. if (encoder == NULL)
  312. return -ENOMEM;
  313. encoder->norm = VIDEO_MODE_PAL;
  314. encoder->input = 0;
  315. encoder->enable = 1;
  316. i2c_set_clientdata(client, encoder);
  317. i = adv7175_write_block(client, init_common, sizeof(init_common));
  318. if (i >= 0) {
  319. i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
  320. i = adv7175_write(client, 0x07, TR0MODE);
  321. i = adv7175_read(client, 0x12);
  322. v4l_dbg(1, debug, client, "revision %d\n", i & 1);
  323. }
  324. if (i < 0)
  325. v4l_dbg(1, debug, client, "init error 0x%x\n", i);
  326. return 0;
  327. }
  328. static int adv7175_remove(struct i2c_client *client)
  329. {
  330. kfree(i2c_get_clientdata(client));
  331. return 0;
  332. }
  333. /* ----------------------------------------------------------------------- */
  334. static const struct i2c_device_id adv7175_id[] = {
  335. { "adv7175", 0 },
  336. { "adv7176", 0 },
  337. { }
  338. };
  339. MODULE_DEVICE_TABLE(i2c, adv7175_id);
  340. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  341. .name = "adv7175",
  342. .driverid = I2C_DRIVERID_ADV7175,
  343. .command = adv7175_command,
  344. .probe = adv7175_probe,
  345. .remove = adv7175_remove,
  346. .id_table = adv7175_id,
  347. };