dw9807-vcm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Intel Corporation
  3. #include <linux/acpi.h>
  4. #include <linux/delay.h>
  5. #include <linux/i2c.h>
  6. #include <linux/iopoll.h>
  7. #include <linux/module.h>
  8. #include <linux/pm_runtime.h>
  9. #include <media/v4l2-ctrls.h>
  10. #include <media/v4l2-device.h>
  11. #define DW9807_MAX_FOCUS_POS 1023
  12. /*
  13. * This sets the minimum granularity for the focus positions.
  14. * A value of 1 gives maximum accuracy for a desired focus position.
  15. */
  16. #define DW9807_FOCUS_STEPS 1
  17. /*
  18. * This acts as the minimum granularity of lens movement.
  19. * Keep this value power of 2, so the control steps can be
  20. * uniformly adjusted for gradual lens movement, with desired
  21. * number of control steps.
  22. */
  23. #define DW9807_CTRL_STEPS 16
  24. #define DW9807_CTRL_DELAY_US 1000
  25. #define DW9807_CTL_ADDR 0x02
  26. /*
  27. * DW9807 separates two registers to control the VCM position.
  28. * One for MSB value, another is LSB value.
  29. */
  30. #define DW9807_MSB_ADDR 0x03
  31. #define DW9807_LSB_ADDR 0x04
  32. #define DW9807_STATUS_ADDR 0x05
  33. #define DW9807_MODE_ADDR 0x06
  34. #define DW9807_RESONANCE_ADDR 0x07
  35. #define MAX_RETRY 10
  36. struct dw9807_device {
  37. struct v4l2_ctrl_handler ctrls_vcm;
  38. struct v4l2_subdev sd;
  39. u16 current_val;
  40. };
  41. static inline struct dw9807_device *sd_to_dw9807_vcm(
  42. struct v4l2_subdev *subdev)
  43. {
  44. return container_of(subdev, struct dw9807_device, sd);
  45. }
  46. static int dw9807_i2c_check(struct i2c_client *client)
  47. {
  48. const char status_addr = DW9807_STATUS_ADDR;
  49. char status_result;
  50. int ret;
  51. ret = i2c_master_send(client, &status_addr, sizeof(status_addr));
  52. if (ret < 0) {
  53. dev_err(&client->dev, "I2C write STATUS address fail ret = %d\n",
  54. ret);
  55. return ret;
  56. }
  57. ret = i2c_master_recv(client, &status_result, sizeof(status_result));
  58. if (ret < 0) {
  59. dev_err(&client->dev, "I2C read STATUS value fail ret = %d\n",
  60. ret);
  61. return ret;
  62. }
  63. return status_result;
  64. }
  65. static int dw9807_set_dac(struct i2c_client *client, u16 data)
  66. {
  67. const char tx_data[3] = {
  68. DW9807_MSB_ADDR, ((data >> 8) & 0x03), (data & 0xff)
  69. };
  70. int val, ret;
  71. /*
  72. * According to the datasheet, need to check the bus status before we
  73. * write VCM position. This ensure that we really write the value
  74. * into the register
  75. */
  76. ret = readx_poll_timeout(dw9807_i2c_check, client, val, val <= 0,
  77. DW9807_CTRL_DELAY_US, MAX_RETRY * DW9807_CTRL_DELAY_US);
  78. if (ret || val < 0) {
  79. if (ret) {
  80. dev_warn(&client->dev,
  81. "Cannot do the write operation because VCM is busy\n");
  82. }
  83. return ret ? -EBUSY : val;
  84. }
  85. /* Write VCM position to registers */
  86. ret = i2c_master_send(client, tx_data, sizeof(tx_data));
  87. if (ret < 0) {
  88. dev_err(&client->dev,
  89. "I2C write MSB fail ret=%d\n", ret);
  90. return ret;
  91. }
  92. return 0;
  93. }
  94. static int dw9807_set_ctrl(struct v4l2_ctrl *ctrl)
  95. {
  96. struct dw9807_device *dev_vcm = container_of(ctrl->handler,
  97. struct dw9807_device, ctrls_vcm);
  98. if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
  99. struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
  100. dev_vcm->current_val = ctrl->val;
  101. return dw9807_set_dac(client, ctrl->val);
  102. }
  103. return -EINVAL;
  104. }
  105. static const struct v4l2_ctrl_ops dw9807_vcm_ctrl_ops = {
  106. .s_ctrl = dw9807_set_ctrl,
  107. };
  108. static int dw9807_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  109. {
  110. int rval;
  111. rval = pm_runtime_get_sync(sd->dev);
  112. if (rval < 0) {
  113. pm_runtime_put_noidle(sd->dev);
  114. return rval;
  115. }
  116. return 0;
  117. }
  118. static int dw9807_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  119. {
  120. pm_runtime_put(sd->dev);
  121. return 0;
  122. }
  123. static const struct v4l2_subdev_internal_ops dw9807_int_ops = {
  124. .open = dw9807_open,
  125. .close = dw9807_close,
  126. };
  127. static const struct v4l2_subdev_ops dw9807_ops = { };
  128. static void dw9807_subdev_cleanup(struct dw9807_device *dw9807_dev)
  129. {
  130. v4l2_async_unregister_subdev(&dw9807_dev->sd);
  131. v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
  132. media_entity_cleanup(&dw9807_dev->sd.entity);
  133. }
  134. static int dw9807_init_controls(struct dw9807_device *dev_vcm)
  135. {
  136. struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
  137. const struct v4l2_ctrl_ops *ops = &dw9807_vcm_ctrl_ops;
  138. struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
  139. v4l2_ctrl_handler_init(hdl, 1);
  140. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
  141. 0, DW9807_MAX_FOCUS_POS, DW9807_FOCUS_STEPS, 0);
  142. dev_vcm->sd.ctrl_handler = hdl;
  143. if (hdl->error) {
  144. dev_err(&client->dev, "%s fail error: 0x%x\n",
  145. __func__, hdl->error);
  146. return hdl->error;
  147. }
  148. return 0;
  149. }
  150. static int dw9807_probe(struct i2c_client *client)
  151. {
  152. struct dw9807_device *dw9807_dev;
  153. int rval;
  154. dw9807_dev = devm_kzalloc(&client->dev, sizeof(*dw9807_dev),
  155. GFP_KERNEL);
  156. if (dw9807_dev == NULL)
  157. return -ENOMEM;
  158. v4l2_i2c_subdev_init(&dw9807_dev->sd, client, &dw9807_ops);
  159. dw9807_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  160. dw9807_dev->sd.internal_ops = &dw9807_int_ops;
  161. rval = dw9807_init_controls(dw9807_dev);
  162. if (rval)
  163. goto err_cleanup;
  164. rval = media_entity_pads_init(&dw9807_dev->sd.entity, 0, NULL);
  165. if (rval < 0)
  166. goto err_cleanup;
  167. dw9807_dev->sd.entity.function = MEDIA_ENT_F_LENS;
  168. rval = v4l2_async_register_subdev(&dw9807_dev->sd);
  169. if (rval < 0)
  170. goto err_cleanup;
  171. pm_runtime_set_active(&client->dev);
  172. pm_runtime_enable(&client->dev);
  173. pm_runtime_idle(&client->dev);
  174. return 0;
  175. err_cleanup:
  176. v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
  177. media_entity_cleanup(&dw9807_dev->sd.entity);
  178. return rval;
  179. }
  180. static int dw9807_remove(struct i2c_client *client)
  181. {
  182. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  183. struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
  184. pm_runtime_disable(&client->dev);
  185. pm_runtime_set_suspended(&client->dev);
  186. dw9807_subdev_cleanup(dw9807_dev);
  187. return 0;
  188. }
  189. /*
  190. * This function sets the vcm position, so it consumes least current
  191. * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
  192. * to make the movements smoothly.
  193. */
  194. static int __maybe_unused dw9807_vcm_suspend(struct device *dev)
  195. {
  196. struct i2c_client *client = to_i2c_client(dev);
  197. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  198. struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
  199. const char tx_data[2] = { DW9807_CTL_ADDR, 0x01 };
  200. int ret, val;
  201. for (val = dw9807_dev->current_val & ~(DW9807_CTRL_STEPS - 1);
  202. val >= 0; val -= DW9807_CTRL_STEPS) {
  203. ret = dw9807_set_dac(client, val);
  204. if (ret)
  205. dev_err_once(dev, "%s I2C failure: %d", __func__, ret);
  206. usleep_range(DW9807_CTRL_DELAY_US, DW9807_CTRL_DELAY_US + 10);
  207. }
  208. /* Power down */
  209. ret = i2c_master_send(client, tx_data, sizeof(tx_data));
  210. if (ret < 0) {
  211. dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. /*
  217. * This function sets the vcm position to the value set by the user
  218. * through v4l2_ctrl_ops s_ctrl handler
  219. * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
  220. * to make the movements smoothly.
  221. */
  222. static int __maybe_unused dw9807_vcm_resume(struct device *dev)
  223. {
  224. struct i2c_client *client = to_i2c_client(dev);
  225. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  226. struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
  227. const char tx_data[2] = { DW9807_CTL_ADDR, 0x00 };
  228. int ret, val;
  229. /* Power on */
  230. ret = i2c_master_send(client, tx_data, sizeof(tx_data));
  231. if (ret < 0) {
  232. dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
  233. return ret;
  234. }
  235. for (val = dw9807_dev->current_val % DW9807_CTRL_STEPS;
  236. val < dw9807_dev->current_val + DW9807_CTRL_STEPS - 1;
  237. val += DW9807_CTRL_STEPS) {
  238. ret = dw9807_set_dac(client, val);
  239. if (ret)
  240. dev_err_ratelimited(dev, "%s I2C failure: %d",
  241. __func__, ret);
  242. usleep_range(DW9807_CTRL_DELAY_US, DW9807_CTRL_DELAY_US + 10);
  243. }
  244. return 0;
  245. }
  246. static const struct of_device_id dw9807_of_table[] = {
  247. { .compatible = "dongwoon,dw9807-vcm" },
  248. { /* sentinel */ }
  249. };
  250. MODULE_DEVICE_TABLE(of, dw9807_of_table);
  251. static const struct dev_pm_ops dw9807_pm_ops = {
  252. SET_SYSTEM_SLEEP_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume)
  253. SET_RUNTIME_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume, NULL)
  254. };
  255. static struct i2c_driver dw9807_i2c_driver = {
  256. .driver = {
  257. .name = "dw9807",
  258. .pm = &dw9807_pm_ops,
  259. .of_match_table = dw9807_of_table,
  260. },
  261. .probe_new = dw9807_probe,
  262. .remove = dw9807_remove,
  263. };
  264. module_i2c_driver(dw9807_i2c_driver);
  265. MODULE_AUTHOR("Chiang, Alan <alanx.chiang@intel.com>");
  266. MODULE_DESCRIPTION("DW9807 VCM driver");
  267. MODULE_LICENSE("GPL v2");