dw9714.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2015--2017 Intel Corporation.
  3. #include <linux/delay.h>
  4. #include <linux/i2c.h>
  5. #include <linux/module.h>
  6. #include <linux/pm_runtime.h>
  7. #include <linux/regulator/consumer.h>
  8. #include <media/v4l2-ctrls.h>
  9. #include <media/v4l2-device.h>
  10. #include <media/v4l2-event.h>
  11. #define DW9714_NAME "dw9714"
  12. #define DW9714_MAX_FOCUS_POS 1023
  13. /*
  14. * This sets the minimum granularity for the focus positions.
  15. * A value of 1 gives maximum accuracy for a desired focus position
  16. */
  17. #define DW9714_FOCUS_STEPS 1
  18. /*
  19. * This acts as the minimum granularity of lens movement.
  20. * Keep this value power of 2, so the control steps can be
  21. * uniformly adjusted for gradual lens movement, with desired
  22. * number of control steps.
  23. */
  24. #define DW9714_CTRL_STEPS 16
  25. #define DW9714_CTRL_DELAY_US 1000
  26. /*
  27. * S[3:2] = 0x00, codes per step for "Linear Slope Control"
  28. * S[1:0] = 0x00, step period
  29. */
  30. #define DW9714_DEFAULT_S 0x0
  31. #define DW9714_VAL(data, s) ((data) << 4 | (s))
  32. /* dw9714 device structure */
  33. struct dw9714_device {
  34. struct v4l2_ctrl_handler ctrls_vcm;
  35. struct v4l2_subdev sd;
  36. u16 current_val;
  37. struct regulator *vcc;
  38. };
  39. static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl)
  40. {
  41. return container_of(ctrl->handler, struct dw9714_device, ctrls_vcm);
  42. }
  43. static inline struct dw9714_device *sd_to_dw9714_vcm(struct v4l2_subdev *subdev)
  44. {
  45. return container_of(subdev, struct dw9714_device, sd);
  46. }
  47. static int dw9714_i2c_write(struct i2c_client *client, u16 data)
  48. {
  49. int ret;
  50. __be16 val = cpu_to_be16(data);
  51. ret = i2c_master_send(client, (const char *)&val, sizeof(val));
  52. if (ret != sizeof(val)) {
  53. dev_err(&client->dev, "I2C write fail\n");
  54. return -EIO;
  55. }
  56. return 0;
  57. }
  58. static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val)
  59. {
  60. struct i2c_client *client = v4l2_get_subdevdata(&dw9714_dev->sd);
  61. dw9714_dev->current_val = val;
  62. return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));
  63. }
  64. static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl)
  65. {
  66. struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
  67. if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
  68. return dw9714_t_focus_vcm(dev_vcm, ctrl->val);
  69. return -EINVAL;
  70. }
  71. static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
  72. .s_ctrl = dw9714_set_ctrl,
  73. };
  74. static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  75. {
  76. return pm_runtime_resume_and_get(sd->dev);
  77. }
  78. static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  79. {
  80. pm_runtime_put(sd->dev);
  81. return 0;
  82. }
  83. static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
  84. .open = dw9714_open,
  85. .close = dw9714_close,
  86. };
  87. static const struct v4l2_subdev_core_ops dw9714_core_ops = {
  88. .log_status = v4l2_ctrl_subdev_log_status,
  89. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  90. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  91. };
  92. static const struct v4l2_subdev_ops dw9714_ops = {
  93. .core = &dw9714_core_ops,
  94. };
  95. static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev)
  96. {
  97. v4l2_async_unregister_subdev(&dw9714_dev->sd);
  98. v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
  99. media_entity_cleanup(&dw9714_dev->sd.entity);
  100. }
  101. static int dw9714_init_controls(struct dw9714_device *dev_vcm)
  102. {
  103. struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
  104. const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
  105. v4l2_ctrl_handler_init(hdl, 1);
  106. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
  107. 0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, 0);
  108. if (hdl->error)
  109. dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
  110. __func__, hdl->error);
  111. dev_vcm->sd.ctrl_handler = hdl;
  112. return hdl->error;
  113. }
  114. static int dw9714_probe(struct i2c_client *client)
  115. {
  116. struct dw9714_device *dw9714_dev;
  117. int rval;
  118. dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev),
  119. GFP_KERNEL);
  120. if (dw9714_dev == NULL)
  121. return -ENOMEM;
  122. dw9714_dev->vcc = devm_regulator_get(&client->dev, "vcc");
  123. if (IS_ERR(dw9714_dev->vcc))
  124. return PTR_ERR(dw9714_dev->vcc);
  125. rval = regulator_enable(dw9714_dev->vcc);
  126. if (rval < 0) {
  127. dev_err(&client->dev, "failed to enable vcc: %d\n", rval);
  128. return rval;
  129. }
  130. v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
  131. dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  132. V4L2_SUBDEV_FL_HAS_EVENTS;
  133. dw9714_dev->sd.internal_ops = &dw9714_int_ops;
  134. rval = dw9714_init_controls(dw9714_dev);
  135. if (rval)
  136. goto err_cleanup;
  137. rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);
  138. if (rval < 0)
  139. goto err_cleanup;
  140. dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
  141. rval = v4l2_async_register_subdev(&dw9714_dev->sd);
  142. if (rval < 0)
  143. goto err_cleanup;
  144. pm_runtime_set_active(&client->dev);
  145. pm_runtime_enable(&client->dev);
  146. pm_runtime_idle(&client->dev);
  147. return 0;
  148. err_cleanup:
  149. regulator_disable(dw9714_dev->vcc);
  150. v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
  151. media_entity_cleanup(&dw9714_dev->sd.entity);
  152. return rval;
  153. }
  154. static void dw9714_remove(struct i2c_client *client)
  155. {
  156. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  157. struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
  158. int ret;
  159. pm_runtime_disable(&client->dev);
  160. if (!pm_runtime_status_suspended(&client->dev)) {
  161. ret = regulator_disable(dw9714_dev->vcc);
  162. if (ret) {
  163. dev_err(&client->dev,
  164. "Failed to disable vcc: %d\n", ret);
  165. }
  166. }
  167. pm_runtime_set_suspended(&client->dev);
  168. dw9714_subdev_cleanup(dw9714_dev);
  169. }
  170. /*
  171. * This function sets the vcm position, so it consumes least current
  172. * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
  173. * to make the movements smoothly.
  174. */
  175. static int __maybe_unused dw9714_vcm_suspend(struct device *dev)
  176. {
  177. struct i2c_client *client = to_i2c_client(dev);
  178. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  179. struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
  180. int ret, val;
  181. if (pm_runtime_suspended(&client->dev))
  182. return 0;
  183. for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
  184. val >= 0; val -= DW9714_CTRL_STEPS) {
  185. ret = dw9714_i2c_write(client,
  186. DW9714_VAL(val, DW9714_DEFAULT_S));
  187. if (ret)
  188. dev_err_once(dev, "%s I2C failure: %d", __func__, ret);
  189. usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
  190. }
  191. ret = regulator_disable(dw9714_dev->vcc);
  192. if (ret)
  193. dev_err(dev, "Failed to disable vcc: %d\n", ret);
  194. return ret;
  195. }
  196. /*
  197. * This function sets the vcm position to the value set by the user
  198. * through v4l2_ctrl_ops s_ctrl handler
  199. * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
  200. * to make the movements smoothly.
  201. */
  202. static int __maybe_unused dw9714_vcm_resume(struct device *dev)
  203. {
  204. struct i2c_client *client = to_i2c_client(dev);
  205. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  206. struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
  207. int ret, val;
  208. if (pm_runtime_suspended(&client->dev))
  209. return 0;
  210. ret = regulator_enable(dw9714_dev->vcc);
  211. if (ret) {
  212. dev_err(dev, "Failed to enable vcc: %d\n", ret);
  213. return ret;
  214. }
  215. usleep_range(1000, 2000);
  216. for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
  217. val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
  218. val += DW9714_CTRL_STEPS) {
  219. ret = dw9714_i2c_write(client,
  220. DW9714_VAL(val, DW9714_DEFAULT_S));
  221. if (ret)
  222. dev_err_ratelimited(dev, "%s I2C failure: %d",
  223. __func__, ret);
  224. usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
  225. }
  226. return 0;
  227. }
  228. static const struct i2c_device_id dw9714_id_table[] = {
  229. { DW9714_NAME, 0 },
  230. { { 0 } }
  231. };
  232. MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
  233. static const struct of_device_id dw9714_of_table[] = {
  234. { .compatible = "dongwoon,dw9714" },
  235. { { 0 } }
  236. };
  237. MODULE_DEVICE_TABLE(of, dw9714_of_table);
  238. static const struct dev_pm_ops dw9714_pm_ops = {
  239. SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume)
  240. SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL)
  241. };
  242. static struct i2c_driver dw9714_i2c_driver = {
  243. .driver = {
  244. .name = DW9714_NAME,
  245. .pm = &dw9714_pm_ops,
  246. .of_match_table = dw9714_of_table,
  247. },
  248. .probe_new = dw9714_probe,
  249. .remove = dw9714_remove,
  250. .id_table = dw9714_id_table,
  251. };
  252. module_i2c_driver(dw9714_i2c_driver);
  253. MODULE_AUTHOR("Tianshu Qiu <[email protected]>");
  254. MODULE_AUTHOR("Jian Xu Zheng");
  255. MODULE_AUTHOR("Yuning Pu <[email protected]>");
  256. MODULE_AUTHOR("Jouni Ukkonen <[email protected]>");
  257. MODULE_AUTHOR("Tommi Franttila <[email protected]>");
  258. MODULE_DESCRIPTION("DW9714 VCM driver");
  259. MODULE_LICENSE("GPL v2");