ad5820.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. __be16 be_data;
  58. int r;
  59. if (!client->adapter)
  60. return -ENODEV;
  61. be_data = cpu_to_be16(data);
  62. msg.addr = client->addr;
  63. msg.flags = 0;
  64. msg.len = 2;
  65. msg.buf = (u8 *)&be_data;
  66. r = i2c_transfer(client->adapter, &msg, 1);
  67. if (r < 0) {
  68. dev_err(&client->dev, "write failed, error %d\n", r);
  69. return r;
  70. }
  71. return 0;
  72. }
  73. /*
  74. * Calculate status word and write it to the device based on current
  75. * values of V4L2 controls. It is assumed that the stored V4L2 control
  76. * values are properly limited and rounded.
  77. */
  78. static int ad5820_update_hw(struct ad5820_device *coil)
  79. {
  80. u16 status;
  81. status = RAMP_US_TO_CODE(coil->focus_ramp_time);
  82. status |= coil->focus_ramp_mode
  83. ? AD5820_RAMP_MODE_64_16 : AD5820_RAMP_MODE_LINEAR;
  84. status |= coil->focus_absolute << AD5820_DAC_SHIFT;
  85. if (coil->standby)
  86. status |= AD5820_POWER_DOWN;
  87. return ad5820_write(coil, status);
  88. }
  89. /*
  90. * Power handling
  91. */
  92. static int ad5820_power_off(struct ad5820_device *coil, bool standby)
  93. {
  94. int ret = 0, ret2;
  95. /*
  96. * Go to standby first as real power off my be denied by the hardware
  97. * (single power line control for both coil and sensor).
  98. */
  99. if (standby) {
  100. coil->standby = true;
  101. ret = ad5820_update_hw(coil);
  102. }
  103. ret2 = regulator_disable(coil->vana);
  104. if (ret)
  105. return ret;
  106. return ret2;
  107. }
  108. static int ad5820_power_on(struct ad5820_device *coil, bool restore)
  109. {
  110. int ret;
  111. ret = regulator_enable(coil->vana);
  112. if (ret < 0)
  113. return ret;
  114. if (restore) {
  115. /* Restore the hardware settings. */
  116. coil->standby = false;
  117. ret = ad5820_update_hw(coil);
  118. if (ret)
  119. goto fail;
  120. }
  121. return 0;
  122. fail:
  123. coil->standby = true;
  124. regulator_disable(coil->vana);
  125. return ret;
  126. }
  127. /*
  128. * V4L2 controls
  129. */
  130. static int ad5820_set_ctrl(struct v4l2_ctrl *ctrl)
  131. {
  132. struct ad5820_device *coil =
  133. container_of(ctrl->handler, struct ad5820_device, ctrls);
  134. switch (ctrl->id) {
  135. case V4L2_CID_FOCUS_ABSOLUTE:
  136. coil->focus_absolute = ctrl->val;
  137. return ad5820_update_hw(coil);
  138. }
  139. return 0;
  140. }
  141. static const struct v4l2_ctrl_ops ad5820_ctrl_ops = {
  142. .s_ctrl = ad5820_set_ctrl,
  143. };
  144. static int ad5820_init_controls(struct ad5820_device *coil)
  145. {
  146. v4l2_ctrl_handler_init(&coil->ctrls, 1);
  147. /*
  148. * V4L2_CID_FOCUS_ABSOLUTE
  149. *
  150. * Minimum current is 0 mA, maximum is 100 mA. Thus, 1 code is
  151. * equivalent to 100/1023 = 0.0978 mA. Nevertheless, we do not use [mA]
  152. * for focus position, because it is meaningless for user. Meaningful
  153. * would be to use focus distance or even its inverse, but since the
  154. * driver doesn't have sufficiently knowledge to do the conversion, we
  155. * will just use abstract codes here. In any case, smaller value = focus
  156. * position farther from camera. The default zero value means focus at
  157. * infinity, and also least current consumption.
  158. */
  159. v4l2_ctrl_new_std(&coil->ctrls, &ad5820_ctrl_ops,
  160. V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
  161. if (coil->ctrls.error)
  162. return coil->ctrls.error;
  163. coil->focus_absolute = 0;
  164. coil->focus_ramp_time = 0;
  165. coil->focus_ramp_mode = 0;
  166. coil->subdev.ctrl_handler = &coil->ctrls;
  167. return 0;
  168. }
  169. /*
  170. * V4L2 subdev operations
  171. */
  172. static int ad5820_registered(struct v4l2_subdev *subdev)
  173. {
  174. struct ad5820_device *coil = to_ad5820_device(subdev);
  175. return ad5820_init_controls(coil);
  176. }
  177. static int
  178. ad5820_set_power(struct v4l2_subdev *subdev, int on)
  179. {
  180. struct ad5820_device *coil = to_ad5820_device(subdev);
  181. int ret = 0;
  182. mutex_lock(&coil->power_lock);
  183. /*
  184. * If the power count is modified from 0 to != 0 or from != 0 to 0,
  185. * update the power state.
  186. */
  187. if (coil->power_count == !on) {
  188. ret = on ? ad5820_power_on(coil, true) :
  189. ad5820_power_off(coil, true);
  190. if (ret < 0)
  191. goto done;
  192. }
  193. /* Update the power count. */
  194. coil->power_count += on ? 1 : -1;
  195. WARN_ON(coil->power_count < 0);
  196. done:
  197. mutex_unlock(&coil->power_lock);
  198. return ret;
  199. }
  200. static int ad5820_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  201. {
  202. return ad5820_set_power(sd, 1);
  203. }
  204. static int ad5820_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  205. {
  206. return ad5820_set_power(sd, 0);
  207. }
  208. static const struct v4l2_subdev_core_ops ad5820_core_ops = {
  209. .s_power = ad5820_set_power,
  210. };
  211. static const struct v4l2_subdev_ops ad5820_ops = {
  212. .core = &ad5820_core_ops,
  213. };
  214. static const struct v4l2_subdev_internal_ops ad5820_internal_ops = {
  215. .registered = ad5820_registered,
  216. .open = ad5820_open,
  217. .close = ad5820_close,
  218. };
  219. /*
  220. * I2C driver
  221. */
  222. static int __maybe_unused ad5820_suspend(struct device *dev)
  223. {
  224. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  225. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  226. struct ad5820_device *coil = to_ad5820_device(subdev);
  227. if (!coil->power_count)
  228. return 0;
  229. return ad5820_power_off(coil, false);
  230. }
  231. static int __maybe_unused ad5820_resume(struct device *dev)
  232. {
  233. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  234. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  235. struct ad5820_device *coil = to_ad5820_device(subdev);
  236. if (!coil->power_count)
  237. return 0;
  238. return ad5820_power_on(coil, true);
  239. }
  240. static int ad5820_probe(struct i2c_client *client,
  241. const struct i2c_device_id *devid)
  242. {
  243. struct ad5820_device *coil;
  244. int ret;
  245. coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
  246. if (!coil)
  247. return -ENOMEM;
  248. coil->vana = devm_regulator_get(&client->dev, "VANA");
  249. if (IS_ERR(coil->vana)) {
  250. ret = PTR_ERR(coil->vana);
  251. if (ret != -EPROBE_DEFER)
  252. dev_err(&client->dev, "could not get regulator for vana\n");
  253. return ret;
  254. }
  255. mutex_init(&coil->power_lock);
  256. v4l2_i2c_subdev_init(&coil->subdev, client, &ad5820_ops);
  257. coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  258. coil->subdev.internal_ops = &ad5820_internal_ops;
  259. strcpy(coil->subdev.name, "ad5820 focus");
  260. ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
  261. if (ret < 0)
  262. goto cleanup2;
  263. ret = v4l2_async_register_subdev(&coil->subdev);
  264. if (ret < 0)
  265. goto cleanup;
  266. return ret;
  267. cleanup2:
  268. mutex_destroy(&coil->power_lock);
  269. cleanup:
  270. media_entity_cleanup(&coil->subdev.entity);
  271. return ret;
  272. }
  273. static int ad5820_remove(struct i2c_client *client)
  274. {
  275. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  276. struct ad5820_device *coil = to_ad5820_device(subdev);
  277. v4l2_async_unregister_subdev(&coil->subdev);
  278. v4l2_ctrl_handler_free(&coil->ctrls);
  279. media_entity_cleanup(&coil->subdev.entity);
  280. mutex_destroy(&coil->power_lock);
  281. return 0;
  282. }
  283. static const struct i2c_device_id ad5820_id_table[] = {
  284. { AD5820_NAME, 0 },
  285. { }
  286. };
  287. MODULE_DEVICE_TABLE(i2c, ad5820_id_table);
  288. static SIMPLE_DEV_PM_OPS(ad5820_pm, ad5820_suspend, ad5820_resume);
  289. static struct i2c_driver ad5820_i2c_driver = {
  290. .driver = {
  291. .name = AD5820_NAME,
  292. .pm = &ad5820_pm,
  293. },
  294. .probe = ad5820_probe,
  295. .remove = ad5820_remove,
  296. .id_table = ad5820_id_table,
  297. };
  298. module_i2c_driver(ad5820_i2c_driver);
  299. MODULE_AUTHOR("Tuukka Toivonen");
  300. MODULE_DESCRIPTION("AD5820 camera lens driver");
  301. MODULE_LICENSE("GPL");