adv7170.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1
  3. *
  4. * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
  5. *
  6. * Based on adv7176 driver by:
  7. *
  8. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  9. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  10. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  11. * - some corrections for Pinnacle Systems Inc. DC10plus card.
  12. *
  13. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  14. * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/slab.h>
  33. #include <linux/ioctl.h>
  34. #include <asm/uaccess.h>
  35. #include <linux/i2c.h>
  36. #include <linux/videodev2.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-chip-ident.h>
  39. MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
  40. MODULE_AUTHOR("Maxim Yevtyushkin");
  41. MODULE_LICENSE("GPL");
  42. static int debug;
  43. module_param(debug, int, 0);
  44. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  45. /* ----------------------------------------------------------------------- */
  46. struct adv7170 {
  47. struct v4l2_subdev sd;
  48. unsigned char reg[128];
  49. v4l2_std_id norm;
  50. int input;
  51. };
  52. static inline struct adv7170 *to_adv7170(struct v4l2_subdev *sd)
  53. {
  54. return container_of(sd, struct adv7170, sd);
  55. }
  56. static char *inputs[] = { "pass_through", "play_back" };
  57. /* ----------------------------------------------------------------------- */
  58. static inline int adv7170_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  59. {
  60. struct i2c_client *client = v4l2_get_subdevdata(sd);
  61. struct adv7170 *encoder = to_adv7170(sd);
  62. encoder->reg[reg] = value;
  63. return i2c_smbus_write_byte_data(client, reg, value);
  64. }
  65. static inline int adv7170_read(struct v4l2_subdev *sd, u8 reg)
  66. {
  67. struct i2c_client *client = v4l2_get_subdevdata(sd);
  68. return i2c_smbus_read_byte_data(client, reg);
  69. }
  70. static int adv7170_write_block(struct v4l2_subdev *sd,
  71. const u8 *data, unsigned int len)
  72. {
  73. struct i2c_client *client = v4l2_get_subdevdata(sd);
  74. struct adv7170 *encoder = to_adv7170(sd);
  75. int ret = -1;
  76. u8 reg;
  77. /* the adv7170 has an autoincrement function, use it if
  78. * the adapter understands raw I2C */
  79. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  80. /* do raw I2C, not smbus compatible */
  81. u8 block_data[32];
  82. int block_len;
  83. while (len >= 2) {
  84. block_len = 0;
  85. block_data[block_len++] = reg = data[0];
  86. do {
  87. block_data[block_len++] =
  88. encoder->reg[reg++] = data[1];
  89. len -= 2;
  90. data += 2;
  91. } while (len >= 2 && data[0] == reg && block_len < 32);
  92. ret = i2c_master_send(client, block_data, block_len);
  93. if (ret < 0)
  94. break;
  95. }
  96. } else {
  97. /* do some slow I2C emulation kind of thing */
  98. while (len >= 2) {
  99. reg = *data++;
  100. ret = adv7170_write(sd, reg, *data++);
  101. if (ret < 0)
  102. break;
  103. len -= 2;
  104. }
  105. }
  106. return ret;
  107. }
  108. /* ----------------------------------------------------------------------- */
  109. #define TR0MODE 0x4c
  110. #define TR0RST 0x80
  111. #define TR1CAPT 0x00
  112. #define TR1PLAY 0x00
  113. static const unsigned char init_NTSC[] = {
  114. 0x00, 0x10, /* MR0 */
  115. 0x01, 0x20, /* MR1 */
  116. 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
  117. 0x03, 0x80, /* MR3 */
  118. 0x04, 0x30, /* MR4 */
  119. 0x05, 0x00, /* Reserved */
  120. 0x06, 0x00, /* Reserved */
  121. 0x07, TR0MODE, /* TM0 */
  122. 0x08, TR1CAPT, /* TM1 */
  123. 0x09, 0x16, /* Fsc0 */
  124. 0x0a, 0x7c, /* Fsc1 */
  125. 0x0b, 0xf0, /* Fsc2 */
  126. 0x0c, 0x21, /* Fsc3 */
  127. 0x0d, 0x00, /* Subcarrier Phase */
  128. 0x0e, 0x00, /* Closed Capt. Ext 0 */
  129. 0x0f, 0x00, /* Closed Capt. Ext 1 */
  130. 0x10, 0x00, /* Closed Capt. 0 */
  131. 0x11, 0x00, /* Closed Capt. 1 */
  132. 0x12, 0x00, /* Pedestal Ctl 0 */
  133. 0x13, 0x00, /* Pedestal Ctl 1 */
  134. 0x14, 0x00, /* Pedestal Ctl 2 */
  135. 0x15, 0x00, /* Pedestal Ctl 3 */
  136. 0x16, 0x00, /* CGMS_WSS_0 */
  137. 0x17, 0x00, /* CGMS_WSS_1 */
  138. 0x18, 0x00, /* CGMS_WSS_2 */
  139. 0x19, 0x00, /* Teletext Ctl */
  140. };
  141. static const unsigned char init_PAL[] = {
  142. 0x00, 0x71, /* MR0 */
  143. 0x01, 0x20, /* MR1 */
  144. 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
  145. 0x03, 0x80, /* MR3 */
  146. 0x04, 0x30, /* MR4 */
  147. 0x05, 0x00, /* Reserved */
  148. 0x06, 0x00, /* Reserved */
  149. 0x07, TR0MODE, /* TM0 */
  150. 0x08, TR1CAPT, /* TM1 */
  151. 0x09, 0xcb, /* Fsc0 */
  152. 0x0a, 0x8a, /* Fsc1 */
  153. 0x0b, 0x09, /* Fsc2 */
  154. 0x0c, 0x2a, /* Fsc3 */
  155. 0x0d, 0x00, /* Subcarrier Phase */
  156. 0x0e, 0x00, /* Closed Capt. Ext 0 */
  157. 0x0f, 0x00, /* Closed Capt. Ext 1 */
  158. 0x10, 0x00, /* Closed Capt. 0 */
  159. 0x11, 0x00, /* Closed Capt. 1 */
  160. 0x12, 0x00, /* Pedestal Ctl 0 */
  161. 0x13, 0x00, /* Pedestal Ctl 1 */
  162. 0x14, 0x00, /* Pedestal Ctl 2 */
  163. 0x15, 0x00, /* Pedestal Ctl 3 */
  164. 0x16, 0x00, /* CGMS_WSS_0 */
  165. 0x17, 0x00, /* CGMS_WSS_1 */
  166. 0x18, 0x00, /* CGMS_WSS_2 */
  167. 0x19, 0x00, /* Teletext Ctl */
  168. };
  169. static int adv7170_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  170. {
  171. struct adv7170 *encoder = to_adv7170(sd);
  172. v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
  173. if (std & V4L2_STD_NTSC) {
  174. adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
  175. if (encoder->input == 0)
  176. adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
  177. adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  178. adv7170_write(sd, 0x07, TR0MODE);
  179. } else if (std & V4L2_STD_PAL) {
  180. adv7170_write_block(sd, init_PAL, sizeof(init_PAL));
  181. if (encoder->input == 0)
  182. adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
  183. adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  184. adv7170_write(sd, 0x07, TR0MODE);
  185. } else {
  186. v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
  187. (unsigned long long)std);
  188. return -EINVAL;
  189. }
  190. v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
  191. encoder->norm = std;
  192. return 0;
  193. }
  194. static int adv7170_s_routing(struct v4l2_subdev *sd,
  195. u32 input, u32 output, u32 config)
  196. {
  197. struct adv7170 *encoder = to_adv7170(sd);
  198. /* RJ: input = 0: input is from decoder
  199. input = 1: input is from ZR36060
  200. input = 2: color bar */
  201. v4l2_dbg(1, debug, sd, "set input from %s\n",
  202. input == 0 ? "decoder" : "ZR36060");
  203. switch (input) {
  204. case 0:
  205. adv7170_write(sd, 0x01, 0x20);
  206. adv7170_write(sd, 0x08, TR1CAPT); /* TR1 */
  207. adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
  208. adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  209. adv7170_write(sd, 0x07, TR0MODE);
  210. /* udelay(10); */
  211. break;
  212. case 1:
  213. adv7170_write(sd, 0x01, 0x00);
  214. adv7170_write(sd, 0x08, TR1PLAY); /* TR1 */
  215. adv7170_write(sd, 0x02, 0x08);
  216. adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  217. adv7170_write(sd, 0x07, TR0MODE);
  218. /* udelay(10); */
  219. break;
  220. default:
  221. v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
  222. return -EINVAL;
  223. }
  224. v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
  225. encoder->input = input;
  226. return 0;
  227. }
  228. static int adv7170_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  229. {
  230. struct i2c_client *client = v4l2_get_subdevdata(sd);
  231. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7170, 0);
  232. }
  233. /* ----------------------------------------------------------------------- */
  234. static const struct v4l2_subdev_core_ops adv7170_core_ops = {
  235. .g_chip_ident = adv7170_g_chip_ident,
  236. };
  237. static const struct v4l2_subdev_video_ops adv7170_video_ops = {
  238. .s_std_output = adv7170_s_std_output,
  239. .s_routing = adv7170_s_routing,
  240. };
  241. static const struct v4l2_subdev_ops adv7170_ops = {
  242. .core = &adv7170_core_ops,
  243. .video = &adv7170_video_ops,
  244. };
  245. /* ----------------------------------------------------------------------- */
  246. static int adv7170_probe(struct i2c_client *client,
  247. const struct i2c_device_id *id)
  248. {
  249. struct adv7170 *encoder;
  250. struct v4l2_subdev *sd;
  251. int i;
  252. /* Check if the adapter supports the needed features */
  253. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  254. return -ENODEV;
  255. v4l_info(client, "chip found @ 0x%x (%s)\n",
  256. client->addr << 1, client->adapter->name);
  257. encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
  258. if (encoder == NULL)
  259. return -ENOMEM;
  260. sd = &encoder->sd;
  261. v4l2_i2c_subdev_init(sd, client, &adv7170_ops);
  262. encoder->norm = V4L2_STD_NTSC;
  263. encoder->input = 0;
  264. i = adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
  265. if (i >= 0) {
  266. i = adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  267. i = adv7170_write(sd, 0x07, TR0MODE);
  268. i = adv7170_read(sd, 0x12);
  269. v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
  270. }
  271. if (i < 0)
  272. v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
  273. return 0;
  274. }
  275. static int adv7170_remove(struct i2c_client *client)
  276. {
  277. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  278. v4l2_device_unregister_subdev(sd);
  279. kfree(to_adv7170(sd));
  280. return 0;
  281. }
  282. /* ----------------------------------------------------------------------- */
  283. static const struct i2c_device_id adv7170_id[] = {
  284. { "adv7170", 0 },
  285. { "adv7171", 0 },
  286. { }
  287. };
  288. MODULE_DEVICE_TABLE(i2c, adv7170_id);
  289. static struct i2c_driver adv7170_driver = {
  290. .driver = {
  291. .owner = THIS_MODULE,
  292. .name = "adv7170",
  293. },
  294. .probe = adv7170_probe,
  295. .remove = adv7170_remove,
  296. .id_table = adv7170_id,
  297. };
  298. static __init int init_adv7170(void)
  299. {
  300. return i2c_add_driver(&adv7170_driver);
  301. }
  302. static __exit void exit_adv7170(void)
  303. {
  304. i2c_del_driver(&adv7170_driver);
  305. }
  306. module_init(init_adv7170);
  307. module_exit(exit_adv7170);