ad5820.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/media/i2c/ad5820.c
  4. *
  5. * AD5820 DAC driver for camera voice coil focus.
  6. *
  7. * Copyright (C) 2008 Nokia Corporation
  8. * Copyright (C) 2007 Texas Instruments
  9. * Copyright (C) 2016 Pavel Machek <[email protected]>
  10. *
  11. * Contact: Tuukka Toivonen <[email protected]>
  12. * Sakari Ailus <[email protected]>
  13. *
  14. * Based on af_d88.c by Texas Instruments.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/i2c.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-subdev.h>
  25. /* Register definitions */
  26. #define AD5820_POWER_DOWN (1 << 15)
  27. #define AD5820_DAC_SHIFT 4
  28. #define AD5820_RAMP_MODE_LINEAR (0 << 3)
  29. #define AD5820_RAMP_MODE_64_16 (1 << 3)
  30. #define CODE_TO_RAMP_US(s) ((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
  31. #define RAMP_US_TO_CODE(c) fls(((c) + ((c)>>1)) / 50)
  32. #define to_ad5820_device(sd) container_of(sd, struct ad5820_device, subdev)
  33. struct ad5820_device {
  34. struct v4l2_subdev subdev;
  35. struct ad5820_platform_data *platform_data;
  36. struct regulator *vana;
  37. struct v4l2_ctrl_handler ctrls;
  38. u32 focus_absolute;
  39. u32 focus_ramp_time;
  40. u32 focus_ramp_mode;
  41. struct gpio_desc *enable_gpio;
  42. struct mutex power_lock;
  43. int power_count;
  44. bool standby;
  45. };
  46. static int ad5820_write(struct ad5820_device *coil, u16 data)
  47. {
  48. struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
  49. struct i2c_msg msg;
  50. __be16 be_data;
  51. int r;
  52. if (!client->adapter)
  53. return -ENODEV;
  54. be_data = cpu_to_be16(data);
  55. msg.addr = client->addr;
  56. msg.flags = 0;
  57. msg.len = 2;
  58. msg.buf = (u8 *)&be_data;
  59. r = i2c_transfer(client->adapter, &msg, 1);
  60. if (r < 0) {
  61. dev_err(&client->dev, "write failed, error %d\n", r);
  62. return r;
  63. }
  64. return 0;
  65. }
  66. /*
  67. * Calculate status word and write it to the device based on current
  68. * values of V4L2 controls. It is assumed that the stored V4L2 control
  69. * values are properly limited and rounded.
  70. */
  71. static int ad5820_update_hw(struct ad5820_device *coil)
  72. {
  73. u16 status;
  74. status = RAMP_US_TO_CODE(coil->focus_ramp_time);
  75. status |= coil->focus_ramp_mode
  76. ? AD5820_RAMP_MODE_64_16 : AD5820_RAMP_MODE_LINEAR;
  77. status |= coil->focus_absolute << AD5820_DAC_SHIFT;
  78. if (coil->standby)
  79. status |= AD5820_POWER_DOWN;
  80. return ad5820_write(coil, status);
  81. }
  82. /*
  83. * Power handling
  84. */
  85. static int ad5820_power_off(struct ad5820_device *coil, bool standby)
  86. {
  87. int ret = 0, ret2;
  88. /*
  89. * Go to standby first as real power off my be denied by the hardware
  90. * (single power line control for both coil and sensor).
  91. */
  92. if (standby) {
  93. coil->standby = true;
  94. ret = ad5820_update_hw(coil);
  95. }
  96. gpiod_set_value_cansleep(coil->enable_gpio, 0);
  97. ret2 = regulator_disable(coil->vana);
  98. if (ret)
  99. return ret;
  100. return ret2;
  101. }
  102. static int ad5820_power_on(struct ad5820_device *coil, bool restore)
  103. {
  104. int ret;
  105. ret = regulator_enable(coil->vana);
  106. if (ret < 0)
  107. return ret;
  108. gpiod_set_value_cansleep(coil->enable_gpio, 1);
  109. if (restore) {
  110. /* Restore the hardware settings. */
  111. coil->standby = false;
  112. ret = ad5820_update_hw(coil);
  113. if (ret)
  114. goto fail;
  115. }
  116. return 0;
  117. fail:
  118. gpiod_set_value_cansleep(coil->enable_gpio, 0);
  119. coil->standby = true;
  120. regulator_disable(coil->vana);
  121. return ret;
  122. }
  123. /*
  124. * V4L2 controls
  125. */
  126. static int ad5820_set_ctrl(struct v4l2_ctrl *ctrl)
  127. {
  128. struct ad5820_device *coil =
  129. container_of(ctrl->handler, struct ad5820_device, ctrls);
  130. switch (ctrl->id) {
  131. case V4L2_CID_FOCUS_ABSOLUTE:
  132. coil->focus_absolute = ctrl->val;
  133. return ad5820_update_hw(coil);
  134. }
  135. return 0;
  136. }
  137. static const struct v4l2_ctrl_ops ad5820_ctrl_ops = {
  138. .s_ctrl = ad5820_set_ctrl,
  139. };
  140. static int ad5820_init_controls(struct ad5820_device *coil)
  141. {
  142. v4l2_ctrl_handler_init(&coil->ctrls, 1);
  143. /*
  144. * V4L2_CID_FOCUS_ABSOLUTE
  145. *
  146. * Minimum current is 0 mA, maximum is 100 mA. Thus, 1 code is
  147. * equivalent to 100/1023 = 0.0978 mA. Nevertheless, we do not use [mA]
  148. * for focus position, because it is meaningless for user. Meaningful
  149. * would be to use focus distance or even its inverse, but since the
  150. * driver doesn't have sufficiently knowledge to do the conversion, we
  151. * will just use abstract codes here. In any case, smaller value = focus
  152. * position farther from camera. The default zero value means focus at
  153. * infinity, and also least current consumption.
  154. */
  155. v4l2_ctrl_new_std(&coil->ctrls, &ad5820_ctrl_ops,
  156. V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
  157. if (coil->ctrls.error)
  158. return coil->ctrls.error;
  159. coil->focus_absolute = 0;
  160. coil->focus_ramp_time = 0;
  161. coil->focus_ramp_mode = 0;
  162. coil->subdev.ctrl_handler = &coil->ctrls;
  163. return 0;
  164. }
  165. /*
  166. * V4L2 subdev operations
  167. */
  168. static int ad5820_registered(struct v4l2_subdev *subdev)
  169. {
  170. struct ad5820_device *coil = to_ad5820_device(subdev);
  171. return ad5820_init_controls(coil);
  172. }
  173. static int
  174. ad5820_set_power(struct v4l2_subdev *subdev, int on)
  175. {
  176. struct ad5820_device *coil = to_ad5820_device(subdev);
  177. int ret = 0;
  178. mutex_lock(&coil->power_lock);
  179. /*
  180. * If the power count is modified from 0 to != 0 or from != 0 to 0,
  181. * update the power state.
  182. */
  183. if (coil->power_count == !on) {
  184. ret = on ? ad5820_power_on(coil, true) :
  185. ad5820_power_off(coil, true);
  186. if (ret < 0)
  187. goto done;
  188. }
  189. /* Update the power count. */
  190. coil->power_count += on ? 1 : -1;
  191. WARN_ON(coil->power_count < 0);
  192. done:
  193. mutex_unlock(&coil->power_lock);
  194. return ret;
  195. }
  196. static int ad5820_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  197. {
  198. return ad5820_set_power(sd, 1);
  199. }
  200. static int ad5820_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  201. {
  202. return ad5820_set_power(sd, 0);
  203. }
  204. static const struct v4l2_subdev_core_ops ad5820_core_ops = {
  205. .s_power = ad5820_set_power,
  206. };
  207. static const struct v4l2_subdev_ops ad5820_ops = {
  208. .core = &ad5820_core_ops,
  209. };
  210. static const struct v4l2_subdev_internal_ops ad5820_internal_ops = {
  211. .registered = ad5820_registered,
  212. .open = ad5820_open,
  213. .close = ad5820_close,
  214. };
  215. /*
  216. * I2C driver
  217. */
  218. static int __maybe_unused ad5820_suspend(struct device *dev)
  219. {
  220. struct v4l2_subdev *subdev = dev_get_drvdata(dev);
  221. struct ad5820_device *coil = to_ad5820_device(subdev);
  222. if (!coil->power_count)
  223. return 0;
  224. return ad5820_power_off(coil, false);
  225. }
  226. static int __maybe_unused ad5820_resume(struct device *dev)
  227. {
  228. struct v4l2_subdev *subdev = dev_get_drvdata(dev);
  229. struct ad5820_device *coil = to_ad5820_device(subdev);
  230. if (!coil->power_count)
  231. return 0;
  232. return ad5820_power_on(coil, true);
  233. }
  234. static int ad5820_probe(struct i2c_client *client,
  235. const struct i2c_device_id *devid)
  236. {
  237. struct ad5820_device *coil;
  238. int ret;
  239. coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
  240. if (!coil)
  241. return -ENOMEM;
  242. coil->vana = devm_regulator_get(&client->dev, "VANA");
  243. if (IS_ERR(coil->vana)) {
  244. ret = PTR_ERR(coil->vana);
  245. if (ret != -EPROBE_DEFER)
  246. dev_err(&client->dev, "could not get regulator for vana\n");
  247. return ret;
  248. }
  249. coil->enable_gpio = devm_gpiod_get_optional(&client->dev, "enable",
  250. GPIOD_OUT_LOW);
  251. if (IS_ERR(coil->enable_gpio)) {
  252. ret = PTR_ERR(coil->enable_gpio);
  253. if (ret != -EPROBE_DEFER)
  254. dev_err(&client->dev, "could not get enable gpio\n");
  255. return ret;
  256. }
  257. mutex_init(&coil->power_lock);
  258. v4l2_i2c_subdev_init(&coil->subdev, client, &ad5820_ops);
  259. coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  260. coil->subdev.internal_ops = &ad5820_internal_ops;
  261. coil->subdev.entity.function = MEDIA_ENT_F_LENS;
  262. strscpy(coil->subdev.name, "ad5820 focus", sizeof(coil->subdev.name));
  263. ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
  264. if (ret < 0)
  265. goto clean_mutex;
  266. ret = v4l2_async_register_subdev(&coil->subdev);
  267. if (ret < 0)
  268. goto clean_entity;
  269. return ret;
  270. clean_entity:
  271. media_entity_cleanup(&coil->subdev.entity);
  272. clean_mutex:
  273. mutex_destroy(&coil->power_lock);
  274. return ret;
  275. }
  276. static void ad5820_remove(struct i2c_client *client)
  277. {
  278. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  279. struct ad5820_device *coil = to_ad5820_device(subdev);
  280. v4l2_async_unregister_subdev(&coil->subdev);
  281. v4l2_ctrl_handler_free(&coil->ctrls);
  282. media_entity_cleanup(&coil->subdev.entity);
  283. mutex_destroy(&coil->power_lock);
  284. }
  285. static const struct i2c_device_id ad5820_id_table[] = {
  286. { "ad5820", 0 },
  287. { "ad5821", 0 },
  288. { }
  289. };
  290. MODULE_DEVICE_TABLE(i2c, ad5820_id_table);
  291. static const struct of_device_id ad5820_of_table[] = {
  292. { .compatible = "adi,ad5820" },
  293. { .compatible = "adi,ad5821" },
  294. { }
  295. };
  296. MODULE_DEVICE_TABLE(of, ad5820_of_table);
  297. static SIMPLE_DEV_PM_OPS(ad5820_pm, ad5820_suspend, ad5820_resume);
  298. static struct i2c_driver ad5820_i2c_driver = {
  299. .driver = {
  300. .name = "ad5820",
  301. .pm = &ad5820_pm,
  302. .of_match_table = ad5820_of_table,
  303. },
  304. .probe = ad5820_probe,
  305. .remove = ad5820_remove,
  306. .id_table = ad5820_id_table,
  307. };
  308. module_i2c_driver(ad5820_i2c_driver);
  309. MODULE_AUTHOR("Tuukka Toivonen");
  310. MODULE_DESCRIPTION("AD5820 camera lens driver");
  311. MODULE_LICENSE("GPL");