ak7375.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <linux/regulator/consumer.h>
  9. #include <media/v4l2-ctrls.h>
  10. #include <media/v4l2-device.h>
  11. struct ak73xx_chipdef {
  12. u8 reg_position;
  13. u8 reg_cont;
  14. u8 shift_pos;
  15. u8 mode_active;
  16. u8 mode_standby;
  17. bool has_standby; /* Some chips may not have standby mode */
  18. u16 focus_pos_max;
  19. /*
  20. * This sets the minimum granularity for the focus positions.
  21. * A value of 1 gives maximum accuracy for a desired focus position
  22. */
  23. u16 focus_steps;
  24. /*
  25. * This acts as the minimum granularity of lens movement.
  26. * Keep this value power of 2, so the control steps can be
  27. * uniformly adjusted for gradual lens movement, with desired
  28. * number of control steps.
  29. */
  30. u16 ctrl_steps;
  31. u16 ctrl_delay_us;
  32. /*
  33. * The vcm may take time (tDELAY) to power on and start taking
  34. * I2C messages.
  35. */
  36. u16 power_delay_us;
  37. };
  38. static const struct ak73xx_chipdef ak7345_cdef = {
  39. .reg_position = 0x0,
  40. .reg_cont = 0x2,
  41. .shift_pos = 7, /* 9 bits position values, need to << 7 */
  42. .mode_active = 0x0,
  43. .has_standby = false,
  44. .focus_pos_max = 511,
  45. .focus_steps = 1,
  46. .ctrl_steps = 16,
  47. .ctrl_delay_us = 1000,
  48. .power_delay_us = 20000,
  49. };
  50. static const struct ak73xx_chipdef ak7375_cdef = {
  51. .reg_position = 0x0,
  52. .reg_cont = 0x2,
  53. .shift_pos = 4, /* 12 bits position values, need to << 4 */
  54. .mode_active = 0x0,
  55. .mode_standby = 0x40,
  56. .has_standby = true,
  57. .focus_pos_max = 4095,
  58. .focus_steps = 1,
  59. .ctrl_steps = 64,
  60. .ctrl_delay_us = 1000,
  61. .power_delay_us = 10000,
  62. };
  63. static const char * const ak7375_supply_names[] = {
  64. "vdd",
  65. "vio",
  66. };
  67. /* ak7375 device structure */
  68. struct ak7375_device {
  69. const struct ak73xx_chipdef *cdef;
  70. struct v4l2_ctrl_handler ctrls_vcm;
  71. struct v4l2_subdev sd;
  72. struct v4l2_ctrl *focus;
  73. struct regulator_bulk_data supplies[ARRAY_SIZE(ak7375_supply_names)];
  74. /* active or standby mode */
  75. bool active;
  76. };
  77. static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
  78. {
  79. return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
  80. }
  81. static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
  82. {
  83. return container_of(subdev, struct ak7375_device, sd);
  84. }
  85. static int ak7375_i2c_write(struct ak7375_device *ak7375,
  86. u8 addr, u16 data, u8 size)
  87. {
  88. struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
  89. u8 buf[3];
  90. int ret;
  91. if (size != 1 && size != 2)
  92. return -EINVAL;
  93. buf[0] = addr;
  94. buf[size] = data & 0xff;
  95. if (size == 2)
  96. buf[1] = (data >> 8) & 0xff;
  97. ret = i2c_master_send(client, (const char *)buf, size + 1);
  98. if (ret < 0)
  99. return ret;
  100. if (ret != size + 1)
  101. return -EIO;
  102. return 0;
  103. }
  104. static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
  105. {
  106. struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
  107. const struct ak73xx_chipdef *cdef = dev_vcm->cdef;
  108. if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
  109. return ak7375_i2c_write(dev_vcm, cdef->reg_position,
  110. ctrl->val << cdef->shift_pos, 2);
  111. return -EINVAL;
  112. }
  113. static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
  114. .s_ctrl = ak7375_set_ctrl,
  115. };
  116. static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  117. {
  118. return pm_runtime_resume_and_get(sd->dev);
  119. }
  120. static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  121. {
  122. pm_runtime_put(sd->dev);
  123. return 0;
  124. }
  125. static const struct v4l2_subdev_internal_ops ak7375_int_ops = {
  126. .open = ak7375_open,
  127. .close = ak7375_close,
  128. };
  129. static const struct v4l2_subdev_ops ak7375_ops = { };
  130. static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
  131. {
  132. v4l2_async_unregister_subdev(&ak7375_dev->sd);
  133. v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
  134. media_entity_cleanup(&ak7375_dev->sd.entity);
  135. }
  136. static int ak7375_init_controls(struct ak7375_device *dev_vcm)
  137. {
  138. struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
  139. const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
  140. const struct ak73xx_chipdef *cdef = dev_vcm->cdef;
  141. v4l2_ctrl_handler_init(hdl, 1);
  142. dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
  143. 0, cdef->focus_pos_max, cdef->focus_steps, 0);
  144. if (hdl->error)
  145. dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
  146. __func__, hdl->error);
  147. dev_vcm->sd.ctrl_handler = hdl;
  148. return hdl->error;
  149. }
  150. static int ak7375_probe(struct i2c_client *client)
  151. {
  152. struct ak7375_device *ak7375_dev;
  153. int ret;
  154. unsigned int i;
  155. ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
  156. GFP_KERNEL);
  157. if (!ak7375_dev)
  158. return -ENOMEM;
  159. ak7375_dev->cdef = device_get_match_data(&client->dev);
  160. for (i = 0; i < ARRAY_SIZE(ak7375_supply_names); i++)
  161. ak7375_dev->supplies[i].supply = ak7375_supply_names[i];
  162. ret = devm_regulator_bulk_get(&client->dev,
  163. ARRAY_SIZE(ak7375_supply_names),
  164. ak7375_dev->supplies);
  165. if (ret) {
  166. dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
  167. return ret;
  168. }
  169. v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
  170. ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  171. ak7375_dev->sd.internal_ops = &ak7375_int_ops;
  172. ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS;
  173. ret = ak7375_init_controls(ak7375_dev);
  174. if (ret)
  175. goto err_cleanup;
  176. ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
  177. if (ret < 0)
  178. goto err_cleanup;
  179. ret = v4l2_async_register_subdev(&ak7375_dev->sd);
  180. if (ret < 0)
  181. goto err_cleanup;
  182. pm_runtime_set_active(&client->dev);
  183. pm_runtime_enable(&client->dev);
  184. pm_runtime_idle(&client->dev);
  185. return 0;
  186. err_cleanup:
  187. v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
  188. media_entity_cleanup(&ak7375_dev->sd.entity);
  189. return ret;
  190. }
  191. static void ak7375_remove(struct i2c_client *client)
  192. {
  193. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  194. struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
  195. ak7375_subdev_cleanup(ak7375_dev);
  196. pm_runtime_disable(&client->dev);
  197. pm_runtime_set_suspended(&client->dev);
  198. }
  199. /*
  200. * This function sets the vcm position, so it consumes least current
  201. * The lens position is gradually moved in units of ctrl_steps,
  202. * to make the movements smoothly.
  203. */
  204. static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
  205. {
  206. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  207. struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
  208. const struct ak73xx_chipdef *cdef = ak7375_dev->cdef;
  209. int ret, val;
  210. if (!ak7375_dev->active)
  211. return 0;
  212. for (val = ak7375_dev->focus->val & ~(cdef->ctrl_steps - 1);
  213. val >= 0; val -= cdef->ctrl_steps) {
  214. ret = ak7375_i2c_write(ak7375_dev, cdef->reg_position,
  215. val << cdef->shift_pos, 2);
  216. if (ret)
  217. dev_err_once(dev, "%s I2C failure: %d\n",
  218. __func__, ret);
  219. usleep_range(cdef->ctrl_delay_us, cdef->ctrl_delay_us + 10);
  220. }
  221. if (cdef->has_standby) {
  222. ret = ak7375_i2c_write(ak7375_dev, cdef->reg_cont,
  223. cdef->mode_standby, 1);
  224. if (ret)
  225. dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
  226. }
  227. ret = regulator_bulk_disable(ARRAY_SIZE(ak7375_supply_names),
  228. ak7375_dev->supplies);
  229. if (ret)
  230. return ret;
  231. ak7375_dev->active = false;
  232. return 0;
  233. }
  234. /*
  235. * This function sets the vcm position to the value set by the user
  236. * through v4l2_ctrl_ops s_ctrl handler
  237. * The lens position is gradually moved in units of ctrl_steps,
  238. * to make the movements smoothly.
  239. */
  240. static int __maybe_unused ak7375_vcm_resume(struct device *dev)
  241. {
  242. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  243. struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
  244. const struct ak73xx_chipdef *cdef = ak7375_dev->cdef;
  245. int ret, val;
  246. if (ak7375_dev->active)
  247. return 0;
  248. ret = regulator_bulk_enable(ARRAY_SIZE(ak7375_supply_names),
  249. ak7375_dev->supplies);
  250. if (ret)
  251. return ret;
  252. /* Wait for vcm to become ready */
  253. usleep_range(cdef->power_delay_us, cdef->power_delay_us + 500);
  254. ret = ak7375_i2c_write(ak7375_dev, cdef->reg_cont,
  255. cdef->mode_active, 1);
  256. if (ret) {
  257. dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
  258. return ret;
  259. }
  260. for (val = ak7375_dev->focus->val % cdef->ctrl_steps;
  261. val <= ak7375_dev->focus->val;
  262. val += cdef->ctrl_steps) {
  263. ret = ak7375_i2c_write(ak7375_dev, cdef->reg_position,
  264. val << cdef->shift_pos, 2);
  265. if (ret)
  266. dev_err_ratelimited(dev, "%s I2C failure: %d\n",
  267. __func__, ret);
  268. usleep_range(cdef->ctrl_delay_us, cdef->ctrl_delay_us + 10);
  269. }
  270. ak7375_dev->active = true;
  271. return 0;
  272. }
  273. static const struct of_device_id ak7375_of_table[] = {
  274. { .compatible = "asahi-kasei,ak7345", .data = &ak7345_cdef, },
  275. { .compatible = "asahi-kasei,ak7375", .data = &ak7375_cdef, },
  276. { /* sentinel */ }
  277. };
  278. MODULE_DEVICE_TABLE(of, ak7375_of_table);
  279. static const struct dev_pm_ops ak7375_pm_ops = {
  280. SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume)
  281. SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL)
  282. };
  283. static struct i2c_driver ak7375_i2c_driver = {
  284. .driver = {
  285. .name = "ak7375",
  286. .pm = &ak7375_pm_ops,
  287. .of_match_table = ak7375_of_table,
  288. },
  289. .probe = ak7375_probe,
  290. .remove = ak7375_remove,
  291. };
  292. module_i2c_driver(ak7375_i2c_driver);
  293. MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
  294. MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
  295. MODULE_DESCRIPTION("AK7375 VCM driver");
  296. MODULE_LICENSE("GPL v2");