adv7170.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. int norm;
  50. int input;
  51. int enable;
  52. int bright;
  53. int contrast;
  54. int hue;
  55. int sat;
  56. };
  57. static char *inputs[] = { "pass_through", "play_back" };
  58. static char *norms[] = { "PAL", "NTSC" };
  59. /* ----------------------------------------------------------------------- */
  60. static inline int adv7170_write(struct i2c_client *client, u8 reg, u8 value)
  61. {
  62. struct adv7170 *encoder = i2c_get_clientdata(client);
  63. encoder->reg[reg] = value;
  64. return i2c_smbus_write_byte_data(client, reg, value);
  65. }
  66. static inline int adv7170_read(struct i2c_client *client, u8 reg)
  67. {
  68. return i2c_smbus_read_byte_data(client, reg);
  69. }
  70. static int adv7170_write_block(struct i2c_client *client,
  71. const u8 *data, unsigned int len)
  72. {
  73. int ret = -1;
  74. u8 reg;
  75. /* the adv7170 has an autoincrement function, use it if
  76. * the adapter understands raw I2C */
  77. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  78. /* do raw I2C, not smbus compatible */
  79. struct adv7170 *encoder = i2c_get_clientdata(client);
  80. u8 block_data[32];
  81. int block_len;
  82. while (len >= 2) {
  83. block_len = 0;
  84. block_data[block_len++] = reg = data[0];
  85. do {
  86. block_data[block_len++] =
  87. encoder->reg[reg++] = data[1];
  88. len -= 2;
  89. data += 2;
  90. } while (len >= 2 && data[0] == reg && block_len < 32);
  91. ret = i2c_master_send(client, block_data, block_len);
  92. if (ret < 0)
  93. break;
  94. }
  95. } else {
  96. /* do some slow I2C emulation kind of thing */
  97. while (len >= 2) {
  98. reg = *data++;
  99. ret = adv7170_write(client, reg, *data++);
  100. if (ret < 0)
  101. break;
  102. len -= 2;
  103. }
  104. }
  105. return ret;
  106. }
  107. /* ----------------------------------------------------------------------- */
  108. #define TR0MODE 0x4c
  109. #define TR0RST 0x80
  110. #define TR1CAPT 0x00
  111. #define TR1PLAY 0x00
  112. static const unsigned char init_NTSC[] = {
  113. 0x00, 0x10, // MR0
  114. 0x01, 0x20, // MR1
  115. 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
  116. 0x03, 0x80, // MR3
  117. 0x04, 0x30, // MR4
  118. 0x05, 0x00, // Reserved
  119. 0x06, 0x00, // Reserved
  120. 0x07, TR0MODE, // TM0
  121. 0x08, TR1CAPT, // TM1
  122. 0x09, 0x16, // Fsc0
  123. 0x0a, 0x7c, // Fsc1
  124. 0x0b, 0xf0, // Fsc2
  125. 0x0c, 0x21, // Fsc3
  126. 0x0d, 0x00, // Subcarrier Phase
  127. 0x0e, 0x00, // Closed Capt. Ext 0
  128. 0x0f, 0x00, // Closed Capt. Ext 1
  129. 0x10, 0x00, // Closed Capt. 0
  130. 0x11, 0x00, // Closed Capt. 1
  131. 0x12, 0x00, // Pedestal Ctl 0
  132. 0x13, 0x00, // Pedestal Ctl 1
  133. 0x14, 0x00, // Pedestal Ctl 2
  134. 0x15, 0x00, // Pedestal Ctl 3
  135. 0x16, 0x00, // CGMS_WSS_0
  136. 0x17, 0x00, // CGMS_WSS_1
  137. 0x18, 0x00, // CGMS_WSS_2
  138. 0x19, 0x00, // Teletext Ctl
  139. };
  140. static const unsigned char init_PAL[] = {
  141. 0x00, 0x71, // MR0
  142. 0x01, 0x20, // MR1
  143. 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
  144. 0x03, 0x80, // MR3
  145. 0x04, 0x30, // MR4
  146. 0x05, 0x00, // Reserved
  147. 0x06, 0x00, // Reserved
  148. 0x07, TR0MODE, // TM0
  149. 0x08, TR1CAPT, // TM1
  150. 0x09, 0xcb, // Fsc0
  151. 0x0a, 0x8a, // Fsc1
  152. 0x0b, 0x09, // Fsc2
  153. 0x0c, 0x2a, // Fsc3
  154. 0x0d, 0x00, // Subcarrier Phase
  155. 0x0e, 0x00, // Closed Capt. Ext 0
  156. 0x0f, 0x00, // Closed Capt. Ext 1
  157. 0x10, 0x00, // Closed Capt. 0
  158. 0x11, 0x00, // Closed Capt. 1
  159. 0x12, 0x00, // Pedestal Ctl 0
  160. 0x13, 0x00, // Pedestal Ctl 1
  161. 0x14, 0x00, // Pedestal Ctl 2
  162. 0x15, 0x00, // Pedestal Ctl 3
  163. 0x16, 0x00, // CGMS_WSS_0
  164. 0x17, 0x00, // CGMS_WSS_1
  165. 0x18, 0x00, // CGMS_WSS_2
  166. 0x19, 0x00, // Teletext Ctl
  167. };
  168. static int adv7170_command(struct i2c_client *client, unsigned cmd, void *arg)
  169. {
  170. struct adv7170 *encoder = i2c_get_clientdata(client);
  171. switch (cmd) {
  172. case 0:
  173. #if 0
  174. /* This is just for testing!!! */
  175. adv7170_write_block(client, init_common,
  176. sizeof(init_common));
  177. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  178. adv7170_write(client, 0x07, TR0MODE);
  179. #endif
  180. break;
  181. case ENCODER_GET_CAPABILITIES:
  182. {
  183. struct video_encoder_capability *cap = arg;
  184. cap->flags = VIDEO_ENCODER_PAL |
  185. VIDEO_ENCODER_NTSC;
  186. cap->inputs = 2;
  187. cap->outputs = 1;
  188. break;
  189. }
  190. case ENCODER_SET_NORM:
  191. {
  192. int iarg = *(int *) arg;
  193. v4l_dbg(1, debug, client, "set norm %d\n", iarg);
  194. switch (iarg) {
  195. case VIDEO_MODE_NTSC:
  196. adv7170_write_block(client, init_NTSC,
  197. sizeof(init_NTSC));
  198. if (encoder->input == 0)
  199. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  200. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  201. adv7170_write(client, 0x07, TR0MODE);
  202. break;
  203. case VIDEO_MODE_PAL:
  204. adv7170_write_block(client, init_PAL,
  205. sizeof(init_PAL));
  206. if (encoder->input == 0)
  207. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  208. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  209. adv7170_write(client, 0x07, TR0MODE);
  210. break;
  211. default:
  212. v4l_dbg(1, debug, client, "illegal norm: %d\n", iarg);
  213. return -EINVAL;
  214. }
  215. v4l_dbg(1, debug, client, "switched to %s\n", norms[iarg]);
  216. encoder->norm = iarg;
  217. break;
  218. }
  219. case ENCODER_SET_INPUT:
  220. {
  221. int iarg = *(int *) arg;
  222. /* RJ: *iarg = 0: input is from decoder
  223. *iarg = 1: input is from ZR36060
  224. *iarg = 2: color bar */
  225. v4l_dbg(1, debug, client, "set input from %s\n",
  226. iarg == 0 ? "decoder" : "ZR36060");
  227. switch (iarg) {
  228. case 0:
  229. adv7170_write(client, 0x01, 0x20);
  230. adv7170_write(client, 0x08, TR1CAPT); /* TR1 */
  231. adv7170_write(client, 0x02, 0x0e); // Enable genlock
  232. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  233. adv7170_write(client, 0x07, TR0MODE);
  234. /* udelay(10); */
  235. break;
  236. case 1:
  237. adv7170_write(client, 0x01, 0x00);
  238. adv7170_write(client, 0x08, TR1PLAY); /* TR1 */
  239. adv7170_write(client, 0x02, 0x08);
  240. adv7170_write(client, 0x07, TR0MODE | TR0RST);
  241. adv7170_write(client, 0x07, TR0MODE);
  242. /* udelay(10); */
  243. break;
  244. default:
  245. v4l_dbg(1, debug, client, "illegal input: %d\n", iarg);
  246. return -EINVAL;
  247. }
  248. v4l_dbg(1, debug, client, "switched to %s\n", inputs[iarg]);
  249. encoder->input = iarg;
  250. break;
  251. }
  252. case ENCODER_SET_OUTPUT:
  253. {
  254. int *iarg = arg;
  255. /* not much choice of outputs */
  256. if (*iarg != 0) {
  257. return -EINVAL;
  258. }
  259. break;
  260. }
  261. case ENCODER_ENABLE_OUTPUT:
  262. {
  263. int *iarg = arg;
  264. encoder->enable = !!*iarg;
  265. break;
  266. }
  267. default:
  268. return -EINVAL;
  269. }
  270. return 0;
  271. }
  272. /* ----------------------------------------------------------------------- */
  273. static unsigned short normal_i2c[] = {
  274. 0xd4 >> 1, 0xd6 >> 1, /* adv7170 IDs */
  275. 0x54 >> 1, 0x56 >> 1, /* adv7171 IDs */
  276. I2C_CLIENT_END
  277. };
  278. I2C_CLIENT_INSMOD;
  279. static int adv7170_probe(struct i2c_client *client,
  280. const struct i2c_device_id *id)
  281. {
  282. struct adv7170 *encoder;
  283. int i;
  284. /* Check if the adapter supports the needed features */
  285. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  286. return -ENODEV;
  287. v4l_info(client, "chip found @ 0x%x (%s)\n",
  288. client->addr << 1, client->adapter->name);
  289. encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
  290. if (encoder == NULL)
  291. return -ENOMEM;
  292. encoder->norm = VIDEO_MODE_NTSC;
  293. encoder->input = 0;
  294. encoder->enable = 1;
  295. i2c_set_clientdata(client, encoder);
  296. i = adv7170_write_block(client, init_NTSC, sizeof(init_NTSC));
  297. if (i >= 0) {
  298. i = adv7170_write(client, 0x07, TR0MODE | TR0RST);
  299. i = adv7170_write(client, 0x07, TR0MODE);
  300. i = adv7170_read(client, 0x12);
  301. v4l_dbg(1, debug, client, "revision %d\n", i & 1);
  302. }
  303. if (i < 0)
  304. v4l_dbg(1, debug, client, "init error 0x%x\n", i);
  305. return 0;
  306. }
  307. static int adv7170_remove(struct i2c_client *client)
  308. {
  309. kfree(i2c_get_clientdata(client));
  310. return 0;
  311. }
  312. /* ----------------------------------------------------------------------- */
  313. static const struct i2c_device_id adv7170_id[] = {
  314. { "adv7170", 0 },
  315. { "adv7171", 0 },
  316. { }
  317. };
  318. MODULE_DEVICE_TABLE(i2c, adv7170_id);
  319. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  320. .name = "adv7170",
  321. .driverid = I2C_DRIVERID_ADV7170,
  322. .command = adv7170_command,
  323. .probe = adv7170_probe,
  324. .remove = adv7170_remove,
  325. .id_table = adv7170_id,
  326. };