ml86v7667.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * OKI Semiconductor ML86V7667 video decoder driver
  3. *
  4. * Author: Vladimir Barinov <source@cogentembedded.com>
  5. * Copyright (C) 2013 Cogent Embedded, Inc.
  6. * Copyright (C) 2013 Renesas Solutions Corp.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/videodev2.h>
  18. #include <media/v4l2-subdev.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-ioctl.h>
  21. #include <media/v4l2-ctrls.h>
  22. #define DRV_NAME "ml86v7667"
  23. /* Subaddresses */
  24. #define MRA_REG 0x00 /* Mode Register A */
  25. #define MRC_REG 0x02 /* Mode Register C */
  26. #define LUMC_REG 0x0C /* Luminance Control */
  27. #define CLC_REG 0x10 /* Contrast level control */
  28. #define SSEPL_REG 0x11 /* Sync separation level */
  29. #define CHRCA_REG 0x12 /* Chrominance Control A */
  30. #define ACCC_REG 0x14 /* ACC Loop filter & Chrominance control */
  31. #define ACCRC_REG 0x15 /* ACC Reference level control */
  32. #define HUE_REG 0x16 /* Hue control */
  33. #define ADC2_REG 0x1F /* ADC Register 2 */
  34. #define PLLR1_REG 0x20 /* PLL Register 1 */
  35. #define STATUS_REG 0x2C /* STATUS Register */
  36. /* Mode Register A register bits */
  37. #define MRA_OUTPUT_MODE_MASK (3 << 6)
  38. #define MRA_ITUR_BT601 (1 << 6)
  39. #define MRA_ITUR_BT656 (0 << 6)
  40. #define MRA_INPUT_MODE_MASK (7 << 3)
  41. #define MRA_PAL_BT601 (4 << 3)
  42. #define MRA_NTSC_BT601 (0 << 3)
  43. #define MRA_REGISTER_MODE (1 << 0)
  44. /* Mode Register C register bits */
  45. #define MRC_AUTOSELECT (1 << 7)
  46. /* Luminance Control register bits */
  47. #define LUMC_ONOFF_SHIFT 7
  48. #define LUMC_ONOFF_MASK (1 << 7)
  49. /* Contrast level control register bits */
  50. #define CLC_CONTRAST_ONOFF (1 << 7)
  51. #define CLC_CONTRAST_MASK 0x0F
  52. /* Sync separation level register bits */
  53. #define SSEPL_LUMINANCE_ONOFF (1 << 7)
  54. #define SSEPL_LUMINANCE_MASK 0x7F
  55. /* Chrominance Control A register bits */
  56. #define CHRCA_MODE_SHIFT 6
  57. #define CHRCA_MODE_MASK (1 << 6)
  58. /* ACC Loop filter & Chrominance control register bits */
  59. #define ACCC_CHROMA_CR_SHIFT 3
  60. #define ACCC_CHROMA_CR_MASK (7 << 3)
  61. #define ACCC_CHROMA_CB_SHIFT 0
  62. #define ACCC_CHROMA_CB_MASK (7 << 0)
  63. /* ACC Reference level control register bits */
  64. #define ACCRC_CHROMA_MASK 0xfc
  65. #define ACCRC_CHROMA_SHIFT 2
  66. /* ADC Register 2 register bits */
  67. #define ADC2_CLAMP_VOLTAGE_MASK (7 << 1)
  68. #define ADC2_CLAMP_VOLTAGE(n) ((n & 7) << 1)
  69. /* PLL Register 1 register bits */
  70. #define PLLR1_FIXED_CLOCK (1 << 7)
  71. /* STATUS Register register bits */
  72. #define STATUS_HLOCK_DETECT (1 << 3)
  73. #define STATUS_NTSCPAL (1 << 2)
  74. struct ml86v7667_priv {
  75. struct v4l2_subdev sd;
  76. struct v4l2_ctrl_handler hdl;
  77. v4l2_std_id std;
  78. };
  79. static inline struct ml86v7667_priv *to_ml86v7667(struct v4l2_subdev *subdev)
  80. {
  81. return container_of(subdev, struct ml86v7667_priv, sd);
  82. }
  83. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  84. {
  85. return &container_of(ctrl->handler, struct ml86v7667_priv, hdl)->sd;
  86. }
  87. static int ml86v7667_mask_set(struct i2c_client *client, const u8 reg,
  88. const u8 mask, const u8 data)
  89. {
  90. int val = i2c_smbus_read_byte_data(client, reg);
  91. if (val < 0)
  92. return val;
  93. val = (val & ~mask) | (data & mask);
  94. return i2c_smbus_write_byte_data(client, reg, val);
  95. }
  96. static int ml86v7667_s_ctrl(struct v4l2_ctrl *ctrl)
  97. {
  98. struct v4l2_subdev *sd = to_sd(ctrl);
  99. struct i2c_client *client = v4l2_get_subdevdata(sd);
  100. int ret = -EINVAL;
  101. switch (ctrl->id) {
  102. case V4L2_CID_BRIGHTNESS:
  103. ret = ml86v7667_mask_set(client, SSEPL_REG,
  104. SSEPL_LUMINANCE_MASK, ctrl->val);
  105. break;
  106. case V4L2_CID_CONTRAST:
  107. ret = ml86v7667_mask_set(client, CLC_REG,
  108. CLC_CONTRAST_MASK, ctrl->val);
  109. break;
  110. case V4L2_CID_CHROMA_GAIN:
  111. ret = ml86v7667_mask_set(client, ACCRC_REG, ACCRC_CHROMA_MASK,
  112. ctrl->val << ACCRC_CHROMA_SHIFT);
  113. break;
  114. case V4L2_CID_HUE:
  115. ret = ml86v7667_mask_set(client, HUE_REG, ~0, ctrl->val);
  116. break;
  117. case V4L2_CID_RED_BALANCE:
  118. ret = ml86v7667_mask_set(client, ACCC_REG,
  119. ACCC_CHROMA_CR_MASK,
  120. ctrl->val << ACCC_CHROMA_CR_SHIFT);
  121. break;
  122. case V4L2_CID_BLUE_BALANCE:
  123. ret = ml86v7667_mask_set(client, ACCC_REG,
  124. ACCC_CHROMA_CB_MASK,
  125. ctrl->val << ACCC_CHROMA_CB_SHIFT);
  126. break;
  127. case V4L2_CID_SHARPNESS:
  128. ret = ml86v7667_mask_set(client, LUMC_REG,
  129. LUMC_ONOFF_MASK,
  130. ctrl->val << LUMC_ONOFF_SHIFT);
  131. break;
  132. case V4L2_CID_COLOR_KILLER:
  133. ret = ml86v7667_mask_set(client, CHRCA_REG,
  134. CHRCA_MODE_MASK,
  135. ctrl->val << CHRCA_MODE_SHIFT);
  136. break;
  137. }
  138. return ret;
  139. }
  140. static int ml86v7667_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  141. {
  142. struct i2c_client *client = v4l2_get_subdevdata(sd);
  143. int status;
  144. status = i2c_smbus_read_byte_data(client, STATUS_REG);
  145. if (status < 0)
  146. return status;
  147. if (status & STATUS_HLOCK_DETECT)
  148. *std &= status & STATUS_NTSCPAL ? V4L2_STD_625_50 : V4L2_STD_525_60;
  149. else
  150. *std = V4L2_STD_UNKNOWN;
  151. return 0;
  152. }
  153. static int ml86v7667_g_input_status(struct v4l2_subdev *sd, u32 *status)
  154. {
  155. struct i2c_client *client = v4l2_get_subdevdata(sd);
  156. int status_reg;
  157. status_reg = i2c_smbus_read_byte_data(client, STATUS_REG);
  158. if (status_reg < 0)
  159. return status_reg;
  160. *status = status_reg & STATUS_HLOCK_DETECT ? 0 : V4L2_IN_ST_NO_SIGNAL;
  161. return 0;
  162. }
  163. static int ml86v7667_enum_mbus_code(struct v4l2_subdev *sd,
  164. struct v4l2_subdev_pad_config *cfg,
  165. struct v4l2_subdev_mbus_code_enum *code)
  166. {
  167. if (code->pad || code->index > 0)
  168. return -EINVAL;
  169. code->code = MEDIA_BUS_FMT_YUYV8_2X8;
  170. return 0;
  171. }
  172. static int ml86v7667_fill_fmt(struct v4l2_subdev *sd,
  173. struct v4l2_subdev_pad_config *cfg,
  174. struct v4l2_subdev_format *format)
  175. {
  176. struct ml86v7667_priv *priv = to_ml86v7667(sd);
  177. struct v4l2_mbus_framefmt *fmt = &format->format;
  178. if (format->pad)
  179. return -EINVAL;
  180. fmt->code = MEDIA_BUS_FMT_YUYV8_2X8;
  181. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  182. /* The top field is always transferred first by the chip */
  183. fmt->field = V4L2_FIELD_INTERLACED_TB;
  184. fmt->width = 720;
  185. fmt->height = priv->std & V4L2_STD_525_60 ? 480 : 576;
  186. return 0;
  187. }
  188. static int ml86v7667_g_mbus_config(struct v4l2_subdev *sd,
  189. struct v4l2_mbus_config *cfg)
  190. {
  191. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
  192. V4L2_MBUS_DATA_ACTIVE_HIGH;
  193. cfg->type = V4L2_MBUS_BT656;
  194. return 0;
  195. }
  196. static int ml86v7667_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
  197. {
  198. struct ml86v7667_priv *priv = to_ml86v7667(sd);
  199. *std = priv->std;
  200. return 0;
  201. }
  202. static int ml86v7667_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  203. {
  204. struct ml86v7667_priv *priv = to_ml86v7667(sd);
  205. struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
  206. int ret;
  207. u8 mode;
  208. /* PAL/NTSC ITU-R BT.601 input mode */
  209. mode = std & V4L2_STD_525_60 ? MRA_NTSC_BT601 : MRA_PAL_BT601;
  210. ret = ml86v7667_mask_set(client, MRA_REG, MRA_INPUT_MODE_MASK, mode);
  211. if (ret < 0)
  212. return ret;
  213. priv->std = std;
  214. return 0;
  215. }
  216. #ifdef CONFIG_VIDEO_ADV_DEBUG
  217. static int ml86v7667_g_register(struct v4l2_subdev *sd,
  218. struct v4l2_dbg_register *reg)
  219. {
  220. struct i2c_client *client = v4l2_get_subdevdata(sd);
  221. int ret;
  222. ret = i2c_smbus_read_byte_data(client, (u8)reg->reg);
  223. if (ret < 0)
  224. return ret;
  225. reg->val = ret;
  226. reg->size = sizeof(u8);
  227. return 0;
  228. }
  229. static int ml86v7667_s_register(struct v4l2_subdev *sd,
  230. const struct v4l2_dbg_register *reg)
  231. {
  232. struct i2c_client *client = v4l2_get_subdevdata(sd);
  233. return i2c_smbus_write_byte_data(client, (u8)reg->reg, (u8)reg->val);
  234. }
  235. #endif
  236. static const struct v4l2_ctrl_ops ml86v7667_ctrl_ops = {
  237. .s_ctrl = ml86v7667_s_ctrl,
  238. };
  239. static struct v4l2_subdev_video_ops ml86v7667_subdev_video_ops = {
  240. .g_std = ml86v7667_g_std,
  241. .s_std = ml86v7667_s_std,
  242. .querystd = ml86v7667_querystd,
  243. .g_input_status = ml86v7667_g_input_status,
  244. .g_mbus_config = ml86v7667_g_mbus_config,
  245. };
  246. static const struct v4l2_subdev_pad_ops ml86v7667_subdev_pad_ops = {
  247. .enum_mbus_code = ml86v7667_enum_mbus_code,
  248. .get_fmt = ml86v7667_fill_fmt,
  249. .set_fmt = ml86v7667_fill_fmt,
  250. };
  251. static struct v4l2_subdev_core_ops ml86v7667_subdev_core_ops = {
  252. #ifdef CONFIG_VIDEO_ADV_DEBUG
  253. .g_register = ml86v7667_g_register,
  254. .s_register = ml86v7667_s_register,
  255. #endif
  256. };
  257. static struct v4l2_subdev_ops ml86v7667_subdev_ops = {
  258. .core = &ml86v7667_subdev_core_ops,
  259. .video = &ml86v7667_subdev_video_ops,
  260. .pad = &ml86v7667_subdev_pad_ops,
  261. };
  262. static int ml86v7667_init(struct ml86v7667_priv *priv)
  263. {
  264. struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
  265. int val;
  266. int ret;
  267. /* BT.656-4 output mode, register mode */
  268. ret = ml86v7667_mask_set(client, MRA_REG,
  269. MRA_OUTPUT_MODE_MASK | MRA_REGISTER_MODE,
  270. MRA_ITUR_BT656 | MRA_REGISTER_MODE);
  271. /* PLL circuit fixed clock, 32MHz */
  272. ret |= ml86v7667_mask_set(client, PLLR1_REG, PLLR1_FIXED_CLOCK,
  273. PLLR1_FIXED_CLOCK);
  274. /* ADC2 clamping voltage maximum */
  275. ret |= ml86v7667_mask_set(client, ADC2_REG, ADC2_CLAMP_VOLTAGE_MASK,
  276. ADC2_CLAMP_VOLTAGE(7));
  277. /* enable luminance function */
  278. ret |= ml86v7667_mask_set(client, SSEPL_REG, SSEPL_LUMINANCE_ONOFF,
  279. SSEPL_LUMINANCE_ONOFF);
  280. /* enable contrast function */
  281. ret |= ml86v7667_mask_set(client, CLC_REG, CLC_CONTRAST_ONOFF, 0);
  282. /*
  283. * PAL/NTSC autodetection is enabled after reset,
  284. * set the autodetected std in manual std mode and
  285. * disable autodetection
  286. */
  287. val = i2c_smbus_read_byte_data(client, STATUS_REG);
  288. if (val < 0)
  289. return val;
  290. priv->std = val & STATUS_NTSCPAL ? V4L2_STD_625_50 : V4L2_STD_525_60;
  291. ret |= ml86v7667_mask_set(client, MRC_REG, MRC_AUTOSELECT, 0);
  292. val = priv->std & V4L2_STD_525_60 ? MRA_NTSC_BT601 : MRA_PAL_BT601;
  293. ret |= ml86v7667_mask_set(client, MRA_REG, MRA_INPUT_MODE_MASK, val);
  294. return ret;
  295. }
  296. static int ml86v7667_probe(struct i2c_client *client,
  297. const struct i2c_device_id *did)
  298. {
  299. struct ml86v7667_priv *priv;
  300. int ret;
  301. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  302. return -EIO;
  303. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  304. if (!priv)
  305. return -ENOMEM;
  306. v4l2_i2c_subdev_init(&priv->sd, client, &ml86v7667_subdev_ops);
  307. v4l2_ctrl_handler_init(&priv->hdl, 8);
  308. v4l2_ctrl_new_std(&priv->hdl, &ml86v7667_ctrl_ops,
  309. V4L2_CID_BRIGHTNESS, -64, 63, 1, 0);
  310. v4l2_ctrl_new_std(&priv->hdl, &ml86v7667_ctrl_ops,
  311. V4L2_CID_CONTRAST, -8, 7, 1, 0);
  312. v4l2_ctrl_new_std(&priv->hdl, &ml86v7667_ctrl_ops,
  313. V4L2_CID_CHROMA_GAIN, -32, 31, 1, 0);
  314. v4l2_ctrl_new_std(&priv->hdl, &ml86v7667_ctrl_ops,
  315. V4L2_CID_HUE, -128, 127, 1, 0);
  316. v4l2_ctrl_new_std(&priv->hdl, &ml86v7667_ctrl_ops,
  317. V4L2_CID_RED_BALANCE, -4, 3, 1, 0);
  318. v4l2_ctrl_new_std(&priv->hdl, &ml86v7667_ctrl_ops,
  319. V4L2_CID_BLUE_BALANCE, -4, 3, 1, 0);
  320. v4l2_ctrl_new_std(&priv->hdl, &ml86v7667_ctrl_ops,
  321. V4L2_CID_SHARPNESS, 0, 1, 1, 0);
  322. v4l2_ctrl_new_std(&priv->hdl, &ml86v7667_ctrl_ops,
  323. V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
  324. priv->sd.ctrl_handler = &priv->hdl;
  325. ret = priv->hdl.error;
  326. if (ret)
  327. goto cleanup;
  328. v4l2_ctrl_handler_setup(&priv->hdl);
  329. ret = ml86v7667_init(priv);
  330. if (ret)
  331. goto cleanup;
  332. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  333. client->addr, client->adapter->name);
  334. return 0;
  335. cleanup:
  336. v4l2_ctrl_handler_free(&priv->hdl);
  337. v4l2_device_unregister_subdev(&priv->sd);
  338. v4l_err(client, "failed to probe @ 0x%02x (%s)\n",
  339. client->addr, client->adapter->name);
  340. return ret;
  341. }
  342. static int ml86v7667_remove(struct i2c_client *client)
  343. {
  344. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  345. struct ml86v7667_priv *priv = to_ml86v7667(sd);
  346. v4l2_ctrl_handler_free(&priv->hdl);
  347. v4l2_device_unregister_subdev(&priv->sd);
  348. return 0;
  349. }
  350. static const struct i2c_device_id ml86v7667_id[] = {
  351. {DRV_NAME, 0},
  352. {},
  353. };
  354. MODULE_DEVICE_TABLE(i2c, ml86v7667_id);
  355. static struct i2c_driver ml86v7667_i2c_driver = {
  356. .driver = {
  357. .name = DRV_NAME,
  358. },
  359. .probe = ml86v7667_probe,
  360. .remove = ml86v7667_remove,
  361. .id_table = ml86v7667_id,
  362. };
  363. module_i2c_driver(ml86v7667_i2c_driver);
  364. MODULE_DESCRIPTION("OKI Semiconductor ML86V7667 video decoder driver");
  365. MODULE_AUTHOR("Vladimir Barinov");
  366. MODULE_LICENSE("GPL");