ak7375.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/module.h>
  7. #include <linux/pm_runtime.h>
  8. #include <media/v4l2-ctrls.h>
  9. #include <media/v4l2-device.h>
  10. #define AK7375_MAX_FOCUS_POS 4095
  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 AK7375_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 AK7375_CTRL_STEPS 64
  23. #define AK7375_CTRL_DELAY_US 1000
  24. #define AK7375_REG_POSITION 0x0
  25. #define AK7375_REG_CONT 0x2
  26. #define AK7375_MODE_ACTIVE 0x0
  27. #define AK7375_MODE_STANDBY 0x40
  28. /* ak7375 device structure */
  29. struct ak7375_device {
  30. struct v4l2_ctrl_handler ctrls_vcm;
  31. struct v4l2_subdev sd;
  32. struct v4l2_ctrl *focus;
  33. /* active or standby mode */
  34. bool active;
  35. };
  36. static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
  37. {
  38. return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
  39. }
  40. static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
  41. {
  42. return container_of(subdev, struct ak7375_device, sd);
  43. }
  44. static int ak7375_i2c_write(struct ak7375_device *ak7375,
  45. u8 addr, u16 data, u8 size)
  46. {
  47. struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
  48. u8 buf[3];
  49. int ret;
  50. if (size != 1 && size != 2)
  51. return -EINVAL;
  52. buf[0] = addr;
  53. buf[size] = data & 0xff;
  54. if (size == 2)
  55. buf[1] = (data >> 8) & 0xff;
  56. ret = i2c_master_send(client, (const char *)buf, size + 1);
  57. if (ret < 0)
  58. return ret;
  59. if (ret != size + 1)
  60. return -EIO;
  61. return 0;
  62. }
  63. static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
  64. {
  65. struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
  66. if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
  67. return ak7375_i2c_write(dev_vcm, AK7375_REG_POSITION,
  68. ctrl->val << 4, 2);
  69. return -EINVAL;
  70. }
  71. static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
  72. .s_ctrl = ak7375_set_ctrl,
  73. };
  74. static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  75. {
  76. return pm_runtime_resume_and_get(sd->dev);
  77. }
  78. static int ak7375_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 ak7375_int_ops = {
  84. .open = ak7375_open,
  85. .close = ak7375_close,
  86. };
  87. static const struct v4l2_subdev_ops ak7375_ops = { };
  88. static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
  89. {
  90. v4l2_async_unregister_subdev(&ak7375_dev->sd);
  91. v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
  92. media_entity_cleanup(&ak7375_dev->sd.entity);
  93. }
  94. static int ak7375_init_controls(struct ak7375_device *dev_vcm)
  95. {
  96. struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
  97. const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
  98. v4l2_ctrl_handler_init(hdl, 1);
  99. dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
  100. 0, AK7375_MAX_FOCUS_POS, AK7375_FOCUS_STEPS, 0);
  101. if (hdl->error)
  102. dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
  103. __func__, hdl->error);
  104. dev_vcm->sd.ctrl_handler = hdl;
  105. return hdl->error;
  106. }
  107. static int ak7375_probe(struct i2c_client *client)
  108. {
  109. struct ak7375_device *ak7375_dev;
  110. int ret;
  111. ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
  112. GFP_KERNEL);
  113. if (!ak7375_dev)
  114. return -ENOMEM;
  115. v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
  116. ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  117. ak7375_dev->sd.internal_ops = &ak7375_int_ops;
  118. ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS;
  119. ret = ak7375_init_controls(ak7375_dev);
  120. if (ret)
  121. goto err_cleanup;
  122. ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
  123. if (ret < 0)
  124. goto err_cleanup;
  125. ret = v4l2_async_register_subdev(&ak7375_dev->sd);
  126. if (ret < 0)
  127. goto err_cleanup;
  128. pm_runtime_set_active(&client->dev);
  129. pm_runtime_enable(&client->dev);
  130. pm_runtime_idle(&client->dev);
  131. return 0;
  132. err_cleanup:
  133. v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
  134. media_entity_cleanup(&ak7375_dev->sd.entity);
  135. return ret;
  136. }
  137. static void ak7375_remove(struct i2c_client *client)
  138. {
  139. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  140. struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
  141. ak7375_subdev_cleanup(ak7375_dev);
  142. pm_runtime_disable(&client->dev);
  143. pm_runtime_set_suspended(&client->dev);
  144. }
  145. /*
  146. * This function sets the vcm position, so it consumes least current
  147. * The lens position is gradually moved in units of AK7375_CTRL_STEPS,
  148. * to make the movements smoothly.
  149. */
  150. static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
  151. {
  152. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  153. struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
  154. int ret, val;
  155. if (!ak7375_dev->active)
  156. return 0;
  157. for (val = ak7375_dev->focus->val & ~(AK7375_CTRL_STEPS - 1);
  158. val >= 0; val -= AK7375_CTRL_STEPS) {
  159. ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
  160. val << 4, 2);
  161. if (ret)
  162. dev_err_once(dev, "%s I2C failure: %d\n",
  163. __func__, ret);
  164. usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
  165. }
  166. ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
  167. AK7375_MODE_STANDBY, 1);
  168. if (ret)
  169. dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
  170. ak7375_dev->active = false;
  171. return 0;
  172. }
  173. /*
  174. * This function sets the vcm position to the value set by the user
  175. * through v4l2_ctrl_ops s_ctrl handler
  176. * The lens position is gradually moved in units of AK7375_CTRL_STEPS,
  177. * to make the movements smoothly.
  178. */
  179. static int __maybe_unused ak7375_vcm_resume(struct device *dev)
  180. {
  181. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  182. struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
  183. int ret, val;
  184. if (ak7375_dev->active)
  185. return 0;
  186. ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
  187. AK7375_MODE_ACTIVE, 1);
  188. if (ret) {
  189. dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
  190. return ret;
  191. }
  192. for (val = ak7375_dev->focus->val % AK7375_CTRL_STEPS;
  193. val <= ak7375_dev->focus->val;
  194. val += AK7375_CTRL_STEPS) {
  195. ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
  196. val << 4, 2);
  197. if (ret)
  198. dev_err_ratelimited(dev, "%s I2C failure: %d\n",
  199. __func__, ret);
  200. usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
  201. }
  202. ak7375_dev->active = true;
  203. return 0;
  204. }
  205. static const struct of_device_id ak7375_of_table[] = {
  206. { .compatible = "asahi-kasei,ak7375" },
  207. { /* sentinel */ }
  208. };
  209. MODULE_DEVICE_TABLE(of, ak7375_of_table);
  210. static const struct dev_pm_ops ak7375_pm_ops = {
  211. SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume)
  212. SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL)
  213. };
  214. static struct i2c_driver ak7375_i2c_driver = {
  215. .driver = {
  216. .name = "ak7375",
  217. .pm = &ak7375_pm_ops,
  218. .of_match_table = ak7375_of_table,
  219. },
  220. .probe_new = ak7375_probe,
  221. .remove = ak7375_remove,
  222. };
  223. module_i2c_driver(ak7375_i2c_driver);
  224. MODULE_AUTHOR("Tianshu Qiu <[email protected]>");
  225. MODULE_AUTHOR("Bingbu Cao <[email protected]>");
  226. MODULE_DESCRIPTION("AK7375 VCM driver");
  227. MODULE_LICENSE("GPL v2");