adv7175.c 10 KB

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