adv7175.c 11 KB

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