ad5820.c 8.6 KB

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