adv7170.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/i2c-id.h>
  37. #include <linux/videodev2.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-chip-ident.h>
  40. #include <media/v4l2-i2c-drv.h>
  41. MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
  42. MODULE_AUTHOR("Maxim Yevtyushkin");
  43. MODULE_LICENSE("GPL");
  44. static int debug;
  45. module_param(debug, int, 0);
  46. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  47. /* ----------------------------------------------------------------------- */
  48. struct adv7170 {
  49. struct v4l2_subdev sd;
  50. unsigned char reg[128];
  51. v4l2_std_id norm;
  52. int input;
  53. };
  54. static inline struct adv7170 *to_adv7170(struct v4l2_subdev *sd)
  55. {
  56. return container_of(sd, struct adv7170, sd);
  57. }
  58. static char *inputs[] = { "pass_through", "play_back" };
  59. /* ----------------------------------------------------------------------- */
  60. static inline int adv7170_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  61. {
  62. struct i2c_client *client = v4l2_get_subdevdata(sd);
  63. struct adv7170 *encoder = to_adv7170(sd);
  64. encoder->reg[reg] = value;
  65. return i2c_smbus_write_byte_data(client, reg, value);
  66. }
  67. static inline int adv7170_read(struct v4l2_subdev *sd, u8 reg)
  68. {
  69. struct i2c_client *client = v4l2_get_subdevdata(sd);
  70. return i2c_smbus_read_byte_data(client, reg);
  71. }
  72. static int adv7170_write_block(struct v4l2_subdev *sd,
  73. const u8 *data, unsigned int len)
  74. {
  75. struct i2c_client *client = v4l2_get_subdevdata(sd);
  76. struct adv7170 *encoder = to_adv7170(sd);
  77. int ret = -1;
  78. u8 reg;
  79. /* the adv7170 has an autoincrement function, use it if
  80. * the adapter understands raw I2C */
  81. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  82. /* do raw I2C, not smbus compatible */
  83. u8 block_data[32];
  84. int block_len;
  85. while (len >= 2) {
  86. block_len = 0;
  87. block_data[block_len++] = reg = data[0];
  88. do {
  89. block_data[block_len++] =
  90. encoder->reg[reg++] = data[1];
  91. len -= 2;
  92. data += 2;
  93. } while (len >= 2 && data[0] == reg && block_len < 32);
  94. ret = i2c_master_send(client, block_data, block_len);
  95. if (ret < 0)
  96. break;
  97. }
  98. } else {
  99. /* do some slow I2C emulation kind of thing */
  100. while (len >= 2) {
  101. reg = *data++;
  102. ret = adv7170_write(sd, reg, *data++);
  103. if (ret < 0)
  104. break;
  105. len -= 2;
  106. }
  107. }
  108. return ret;
  109. }
  110. /* ----------------------------------------------------------------------- */
  111. #define TR0MODE 0x4c
  112. #define TR0RST 0x80
  113. #define TR1CAPT 0x00
  114. #define TR1PLAY 0x00
  115. static const unsigned char init_NTSC[] = {
  116. 0x00, 0x10, /* MR0 */
  117. 0x01, 0x20, /* MR1 */
  118. 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
  119. 0x03, 0x80, /* MR3 */
  120. 0x04, 0x30, /* MR4 */
  121. 0x05, 0x00, /* Reserved */
  122. 0x06, 0x00, /* Reserved */
  123. 0x07, TR0MODE, /* TM0 */
  124. 0x08, TR1CAPT, /* TM1 */
  125. 0x09, 0x16, /* Fsc0 */
  126. 0x0a, 0x7c, /* Fsc1 */
  127. 0x0b, 0xf0, /* Fsc2 */
  128. 0x0c, 0x21, /* Fsc3 */
  129. 0x0d, 0x00, /* Subcarrier Phase */
  130. 0x0e, 0x00, /* Closed Capt. Ext 0 */
  131. 0x0f, 0x00, /* Closed Capt. Ext 1 */
  132. 0x10, 0x00, /* Closed Capt. 0 */
  133. 0x11, 0x00, /* Closed Capt. 1 */
  134. 0x12, 0x00, /* Pedestal Ctl 0 */
  135. 0x13, 0x00, /* Pedestal Ctl 1 */
  136. 0x14, 0x00, /* Pedestal Ctl 2 */
  137. 0x15, 0x00, /* Pedestal Ctl 3 */
  138. 0x16, 0x00, /* CGMS_WSS_0 */
  139. 0x17, 0x00, /* CGMS_WSS_1 */
  140. 0x18, 0x00, /* CGMS_WSS_2 */
  141. 0x19, 0x00, /* Teletext Ctl */
  142. };
  143. static const unsigned char init_PAL[] = {
  144. 0x00, 0x71, /* MR0 */
  145. 0x01, 0x20, /* MR1 */
  146. 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
  147. 0x03, 0x80, /* MR3 */
  148. 0x04, 0x30, /* MR4 */
  149. 0x05, 0x00, /* Reserved */
  150. 0x06, 0x00, /* Reserved */
  151. 0x07, TR0MODE, /* TM0 */
  152. 0x08, TR1CAPT, /* TM1 */
  153. 0x09, 0xcb, /* Fsc0 */
  154. 0x0a, 0x8a, /* Fsc1 */
  155. 0x0b, 0x09, /* Fsc2 */
  156. 0x0c, 0x2a, /* Fsc3 */
  157. 0x0d, 0x00, /* Subcarrier Phase */
  158. 0x0e, 0x00, /* Closed Capt. Ext 0 */
  159. 0x0f, 0x00, /* Closed Capt. Ext 1 */
  160. 0x10, 0x00, /* Closed Capt. 0 */
  161. 0x11, 0x00, /* Closed Capt. 1 */
  162. 0x12, 0x00, /* Pedestal Ctl 0 */
  163. 0x13, 0x00, /* Pedestal Ctl 1 */
  164. 0x14, 0x00, /* Pedestal Ctl 2 */
  165. 0x15, 0x00, /* Pedestal Ctl 3 */
  166. 0x16, 0x00, /* CGMS_WSS_0 */
  167. 0x17, 0x00, /* CGMS_WSS_1 */
  168. 0x18, 0x00, /* CGMS_WSS_2 */
  169. 0x19, 0x00, /* Teletext Ctl */
  170. };
  171. static int adv7170_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  172. {
  173. struct adv7170 *encoder = to_adv7170(sd);
  174. v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
  175. if (std & V4L2_STD_NTSC) {
  176. adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
  177. if (encoder->input == 0)
  178. adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
  179. adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  180. adv7170_write(sd, 0x07, TR0MODE);
  181. } else if (std & V4L2_STD_PAL) {
  182. adv7170_write_block(sd, init_PAL, sizeof(init_PAL));
  183. if (encoder->input == 0)
  184. adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
  185. adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  186. adv7170_write(sd, 0x07, TR0MODE);
  187. } else {
  188. v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
  189. (unsigned long long)std);
  190. return -EINVAL;
  191. }
  192. v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
  193. encoder->norm = std;
  194. return 0;
  195. }
  196. static int adv7170_s_routing(struct v4l2_subdev *sd,
  197. u32 input, u32 output, u32 config)
  198. {
  199. struct adv7170 *encoder = to_adv7170(sd);
  200. /* RJ: input = 0: input is from decoder
  201. input = 1: input is from ZR36060
  202. input = 2: color bar */
  203. v4l2_dbg(1, debug, sd, "set input from %s\n",
  204. input == 0 ? "decoder" : "ZR36060");
  205. switch (input) {
  206. case 0:
  207. adv7170_write(sd, 0x01, 0x20);
  208. adv7170_write(sd, 0x08, TR1CAPT); /* TR1 */
  209. adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
  210. adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  211. adv7170_write(sd, 0x07, TR0MODE);
  212. /* udelay(10); */
  213. break;
  214. case 1:
  215. adv7170_write(sd, 0x01, 0x00);
  216. adv7170_write(sd, 0x08, TR1PLAY); /* TR1 */
  217. adv7170_write(sd, 0x02, 0x08);
  218. adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  219. adv7170_write(sd, 0x07, TR0MODE);
  220. /* udelay(10); */
  221. break;
  222. default:
  223. v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
  224. return -EINVAL;
  225. }
  226. v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
  227. encoder->input = input;
  228. return 0;
  229. }
  230. static int adv7170_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  231. {
  232. struct i2c_client *client = v4l2_get_subdevdata(sd);
  233. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7170, 0);
  234. }
  235. /* ----------------------------------------------------------------------- */
  236. static const struct v4l2_subdev_core_ops adv7170_core_ops = {
  237. .g_chip_ident = adv7170_g_chip_ident,
  238. };
  239. static const struct v4l2_subdev_video_ops adv7170_video_ops = {
  240. .s_std_output = adv7170_s_std_output,
  241. .s_routing = adv7170_s_routing,
  242. };
  243. static const struct v4l2_subdev_ops adv7170_ops = {
  244. .core = &adv7170_core_ops,
  245. .video = &adv7170_video_ops,
  246. };
  247. /* ----------------------------------------------------------------------- */
  248. static int adv7170_probe(struct i2c_client *client,
  249. const struct i2c_device_id *id)
  250. {
  251. struct adv7170 *encoder;
  252. struct v4l2_subdev *sd;
  253. int i;
  254. /* Check if the adapter supports the needed features */
  255. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  256. return -ENODEV;
  257. v4l_info(client, "chip found @ 0x%x (%s)\n",
  258. client->addr << 1, client->adapter->name);
  259. encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
  260. if (encoder == NULL)
  261. return -ENOMEM;
  262. sd = &encoder->sd;
  263. v4l2_i2c_subdev_init(sd, client, &adv7170_ops);
  264. encoder->norm = V4L2_STD_NTSC;
  265. encoder->input = 0;
  266. i = adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
  267. if (i >= 0) {
  268. i = adv7170_write(sd, 0x07, TR0MODE | TR0RST);
  269. i = adv7170_write(sd, 0x07, TR0MODE);
  270. i = adv7170_read(sd, 0x12);
  271. v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
  272. }
  273. if (i < 0)
  274. v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
  275. return 0;
  276. }
  277. static int adv7170_remove(struct i2c_client *client)
  278. {
  279. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  280. v4l2_device_unregister_subdev(sd);
  281. kfree(to_adv7170(sd));
  282. return 0;
  283. }
  284. /* ----------------------------------------------------------------------- */
  285. static const struct i2c_device_id adv7170_id[] = {
  286. { "adv7170", 0 },
  287. { "adv7171", 0 },
  288. { }
  289. };
  290. MODULE_DEVICE_TABLE(i2c, adv7170_id);
  291. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  292. .name = "adv7170",
  293. .probe = adv7170_probe,
  294. .remove = adv7170_remove,
  295. .id_table = adv7170_id,
  296. };