adv7170.c 9.0 KB

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