dw9714.c 7.2 KB

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