adv7175.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/videodev2.h>
  33. #include <media/v4l2-device.h>
  34. MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
  35. MODULE_AUTHOR("Dave Perks");
  36. MODULE_LICENSE("GPL");
  37. #define I2C_ADV7175 0xd4
  38. #define I2C_ADV7176 0x54
  39. static int debug;
  40. module_param(debug, int, 0);
  41. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  42. /* ----------------------------------------------------------------------- */
  43. struct adv7175 {
  44. struct v4l2_subdev sd;
  45. v4l2_std_id norm;
  46. int input;
  47. };
  48. static inline struct adv7175 *to_adv7175(struct v4l2_subdev *sd)
  49. {
  50. return container_of(sd, struct adv7175, sd);
  51. }
  52. static char *inputs[] = { "pass_through", "play_back", "color_bar" };
  53. static u32 adv7175_codes[] = {
  54. MEDIA_BUS_FMT_UYVY8_2X8,
  55. MEDIA_BUS_FMT_UYVY8_1X16,
  56. };
  57. /* ----------------------------------------------------------------------- */
  58. static inline int adv7175_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  59. {
  60. struct i2c_client *client = v4l2_get_subdevdata(sd);
  61. return i2c_smbus_write_byte_data(client, reg, value);
  62. }
  63. static inline int adv7175_read(struct v4l2_subdev *sd, u8 reg)
  64. {
  65. struct i2c_client *client = v4l2_get_subdevdata(sd);
  66. return i2c_smbus_read_byte_data(client, reg);
  67. }
  68. static int adv7175_write_block(struct v4l2_subdev *sd,
  69. const u8 *data, unsigned int len)
  70. {
  71. struct i2c_client *client = v4l2_get_subdevdata(sd);
  72. int ret = -1;
  73. u8 reg;
  74. /* the adv7175 has an autoincrement function, use it if
  75. * the adapter understands raw I2C */
  76. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  77. /* do raw I2C, not smbus compatible */
  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++] = data[1];
  85. reg++;
  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 = adv7175_write(sd, reg, *data++);
  98. if (ret < 0)
  99. break;
  100. len -= 2;
  101. }
  102. }
  103. return ret;
  104. }
  105. static void set_subcarrier_freq(struct v4l2_subdev *sd, int pass_through)
  106. {
  107. /* for some reason pass_through NTSC needs
  108. * a different sub-carrier freq to remain stable. */
  109. if (pass_through)
  110. adv7175_write(sd, 0x02, 0x00);
  111. else
  112. adv7175_write(sd, 0x02, 0x55);
  113. adv7175_write(sd, 0x03, 0x55);
  114. adv7175_write(sd, 0x04, 0x55);
  115. adv7175_write(sd, 0x05, 0x25);
  116. }
  117. /* ----------------------------------------------------------------------- */
  118. /* Output filter: S-Video Composite */
  119. #define MR050 0x11 /* 0x09 */
  120. #define MR060 0x14 /* 0x0c */
  121. /* ----------------------------------------------------------------------- */
  122. #define TR0MODE 0x46
  123. #define TR0RST 0x80
  124. #define TR1CAPT 0x80
  125. #define TR1PLAY 0x00
  126. static const unsigned char init_common[] = {
  127. 0x00, MR050, /* MR0, PAL enabled */
  128. 0x01, 0x00, /* MR1 */
  129. 0x02, 0x0c, /* subc. freq. */
  130. 0x03, 0x8c, /* subc. freq. */
  131. 0x04, 0x79, /* subc. freq. */
  132. 0x05, 0x26, /* subc. freq. */
  133. 0x06, 0x40, /* subc. phase */
  134. 0x07, TR0MODE, /* TR0, 16bit */
  135. 0x08, 0x21, /* */
  136. 0x09, 0x00, /* */
  137. 0x0a, 0x00, /* */
  138. 0x0b, 0x00, /* */
  139. 0x0c, TR1CAPT, /* TR1 */
  140. 0x0d, 0x4f, /* MR2 */
  141. 0x0e, 0x00, /* */
  142. 0x0f, 0x00, /* */
  143. 0x10, 0x00, /* */
  144. 0x11, 0x00, /* */
  145. };
  146. static const unsigned char init_pal[] = {
  147. 0x00, MR050, /* MR0, PAL enabled */
  148. 0x01, 0x00, /* MR1 */
  149. 0x02, 0x0c, /* subc. freq. */
  150. 0x03, 0x8c, /* subc. freq. */
  151. 0x04, 0x79, /* subc. freq. */
  152. 0x05, 0x26, /* subc. freq. */
  153. 0x06, 0x40, /* subc. phase */
  154. };
  155. static const unsigned char init_ntsc[] = {
  156. 0x00, MR060, /* MR0, NTSC enabled */
  157. 0x01, 0x00, /* MR1 */
  158. 0x02, 0x55, /* subc. freq. */
  159. 0x03, 0x55, /* subc. freq. */
  160. 0x04, 0x55, /* subc. freq. */
  161. 0x05, 0x25, /* subc. freq. */
  162. 0x06, 0x1a, /* subc. phase */
  163. };
  164. static int adv7175_init(struct v4l2_subdev *sd, u32 val)
  165. {
  166. /* This is just for testing!!! */
  167. adv7175_write_block(sd, init_common, sizeof(init_common));
  168. adv7175_write(sd, 0x07, TR0MODE | TR0RST);
  169. adv7175_write(sd, 0x07, TR0MODE);
  170. return 0;
  171. }
  172. static int adv7175_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  173. {
  174. struct adv7175 *encoder = to_adv7175(sd);
  175. if (std & V4L2_STD_NTSC) {
  176. adv7175_write_block(sd, init_ntsc, sizeof(init_ntsc));
  177. if (encoder->input == 0)
  178. adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
  179. adv7175_write(sd, 0x07, TR0MODE | TR0RST);
  180. adv7175_write(sd, 0x07, TR0MODE);
  181. } else if (std & V4L2_STD_PAL) {
  182. adv7175_write_block(sd, init_pal, sizeof(init_pal));
  183. if (encoder->input == 0)
  184. adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
  185. adv7175_write(sd, 0x07, TR0MODE | TR0RST);
  186. adv7175_write(sd, 0x07, TR0MODE);
  187. } else if (std & V4L2_STD_SECAM) {
  188. /* This is an attempt to convert
  189. * SECAM->PAL (typically it does not work
  190. * due to genlock: when decoder is in SECAM
  191. * and encoder in in PAL the subcarrier can
  192. * not be syncronized with horizontal
  193. * quency) */
  194. adv7175_write_block(sd, init_pal, sizeof(init_pal));
  195. if (encoder->input == 0)
  196. adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */
  197. adv7175_write(sd, 0x07, TR0MODE | TR0RST);
  198. adv7175_write(sd, 0x07, TR0MODE);
  199. } else {
  200. v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
  201. (unsigned long long)std);
  202. return -EINVAL;
  203. }
  204. v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
  205. encoder->norm = std;
  206. return 0;
  207. }
  208. static int adv7175_s_routing(struct v4l2_subdev *sd,
  209. u32 input, u32 output, u32 config)
  210. {
  211. struct adv7175 *encoder = to_adv7175(sd);
  212. /* RJ: input = 0: input is from decoder
  213. input = 1: input is from ZR36060
  214. input = 2: color bar */
  215. switch (input) {
  216. case 0:
  217. adv7175_write(sd, 0x01, 0x00);
  218. if (encoder->norm & V4L2_STD_NTSC)
  219. set_subcarrier_freq(sd, 1);
  220. adv7175_write(sd, 0x0c, TR1CAPT); /* TR1 */
  221. if (encoder->norm & V4L2_STD_SECAM)
  222. adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */
  223. else
  224. adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
  225. adv7175_write(sd, 0x07, TR0MODE | TR0RST);
  226. adv7175_write(sd, 0x07, TR0MODE);
  227. /*udelay(10);*/
  228. break;
  229. case 1:
  230. adv7175_write(sd, 0x01, 0x00);
  231. if (encoder->norm & V4L2_STD_NTSC)
  232. set_subcarrier_freq(sd, 0);
  233. adv7175_write(sd, 0x0c, TR1PLAY); /* TR1 */
  234. adv7175_write(sd, 0x0d, 0x49);
  235. adv7175_write(sd, 0x07, TR0MODE | TR0RST);
  236. adv7175_write(sd, 0x07, TR0MODE);
  237. /* udelay(10); */
  238. break;
  239. case 2:
  240. adv7175_write(sd, 0x01, 0x80);
  241. if (encoder->norm & V4L2_STD_NTSC)
  242. set_subcarrier_freq(sd, 0);
  243. adv7175_write(sd, 0x0d, 0x49);
  244. adv7175_write(sd, 0x07, TR0MODE | TR0RST);
  245. adv7175_write(sd, 0x07, TR0MODE);
  246. /* udelay(10); */
  247. break;
  248. default:
  249. v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
  250. return -EINVAL;
  251. }
  252. v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
  253. encoder->input = input;
  254. return 0;
  255. }
  256. static int adv7175_enum_mbus_code(struct v4l2_subdev *sd,
  257. struct v4l2_subdev_pad_config *cfg,
  258. struct v4l2_subdev_mbus_code_enum *code)
  259. {
  260. if (code->pad || code->index >= ARRAY_SIZE(adv7175_codes))
  261. return -EINVAL;
  262. code->code = adv7175_codes[code->index];
  263. return 0;
  264. }
  265. static int adv7175_get_fmt(struct v4l2_subdev *sd,
  266. struct v4l2_subdev_pad_config *cfg,
  267. struct v4l2_subdev_format *format)
  268. {
  269. struct v4l2_mbus_framefmt *mf = &format->format;
  270. u8 val = adv7175_read(sd, 0x7);
  271. if (format->pad)
  272. return -EINVAL;
  273. if ((val & 0x40) == (1 << 6))
  274. mf->code = MEDIA_BUS_FMT_UYVY8_1X16;
  275. else
  276. mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
  277. mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
  278. mf->width = 0;
  279. mf->height = 0;
  280. mf->field = V4L2_FIELD_ANY;
  281. return 0;
  282. }
  283. static int adv7175_set_fmt(struct v4l2_subdev *sd,
  284. struct v4l2_subdev_pad_config *cfg,
  285. struct v4l2_subdev_format *format)
  286. {
  287. struct v4l2_mbus_framefmt *mf = &format->format;
  288. u8 val = adv7175_read(sd, 0x7);
  289. int ret = 0;
  290. if (format->pad)
  291. return -EINVAL;
  292. switch (mf->code) {
  293. case MEDIA_BUS_FMT_UYVY8_2X8:
  294. val &= ~0x40;
  295. break;
  296. case MEDIA_BUS_FMT_UYVY8_1X16:
  297. val |= 0x40;
  298. break;
  299. default:
  300. v4l2_dbg(1, debug, sd,
  301. "illegal v4l2_mbus_framefmt code: %d\n", mf->code);
  302. return -EINVAL;
  303. }
  304. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  305. ret = adv7175_write(sd, 0x7, val);
  306. return ret;
  307. }
  308. static int adv7175_s_power(struct v4l2_subdev *sd, int on)
  309. {
  310. if (on)
  311. adv7175_write(sd, 0x01, 0x00);
  312. else
  313. adv7175_write(sd, 0x01, 0x78);
  314. return 0;
  315. }
  316. /* ----------------------------------------------------------------------- */
  317. static const struct v4l2_subdev_core_ops adv7175_core_ops = {
  318. .init = adv7175_init,
  319. .s_power = adv7175_s_power,
  320. };
  321. static const struct v4l2_subdev_video_ops adv7175_video_ops = {
  322. .s_std_output = adv7175_s_std_output,
  323. .s_routing = adv7175_s_routing,
  324. };
  325. static const struct v4l2_subdev_pad_ops adv7175_pad_ops = {
  326. .enum_mbus_code = adv7175_enum_mbus_code,
  327. .get_fmt = adv7175_get_fmt,
  328. .set_fmt = adv7175_set_fmt,
  329. };
  330. static const struct v4l2_subdev_ops adv7175_ops = {
  331. .core = &adv7175_core_ops,
  332. .video = &adv7175_video_ops,
  333. .pad = &adv7175_pad_ops,
  334. };
  335. /* ----------------------------------------------------------------------- */
  336. static int adv7175_probe(struct i2c_client *client,
  337. const struct i2c_device_id *id)
  338. {
  339. int i;
  340. struct adv7175 *encoder;
  341. struct v4l2_subdev *sd;
  342. /* Check if the adapter supports the needed features */
  343. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  344. return -ENODEV;
  345. v4l_info(client, "chip found @ 0x%x (%s)\n",
  346. client->addr << 1, client->adapter->name);
  347. encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
  348. if (encoder == NULL)
  349. return -ENOMEM;
  350. sd = &encoder->sd;
  351. v4l2_i2c_subdev_init(sd, client, &adv7175_ops);
  352. encoder->norm = V4L2_STD_NTSC;
  353. encoder->input = 0;
  354. i = adv7175_write_block(sd, init_common, sizeof(init_common));
  355. if (i >= 0) {
  356. i = adv7175_write(sd, 0x07, TR0MODE | TR0RST);
  357. i = adv7175_write(sd, 0x07, TR0MODE);
  358. i = adv7175_read(sd, 0x12);
  359. v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
  360. }
  361. if (i < 0)
  362. v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
  363. return 0;
  364. }
  365. static int adv7175_remove(struct i2c_client *client)
  366. {
  367. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  368. v4l2_device_unregister_subdev(sd);
  369. return 0;
  370. }
  371. /* ----------------------------------------------------------------------- */
  372. static const struct i2c_device_id adv7175_id[] = {
  373. { "adv7175", 0 },
  374. { "adv7176", 0 },
  375. { }
  376. };
  377. MODULE_DEVICE_TABLE(i2c, adv7175_id);
  378. static struct i2c_driver adv7175_driver = {
  379. .driver = {
  380. .name = "adv7175",
  381. },
  382. .probe = adv7175_probe,
  383. .remove = adv7175_remove,
  384. .id_table = adv7175_id,
  385. };
  386. module_i2c_driver(adv7175_driver);