950-0397-media-i2c-Add-driver-for-AD5398-VCM-lens-driver.patch 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. From 099c31b8568b7e8c11e57ffced2da1cbccbfbcad Mon Sep 17 00:00:00 2001
  2. From: Dave Stevenson <[email protected]>
  3. Date: Wed, 29 Sep 2021 14:04:28 +0100
  4. Subject: [PATCH] media: i2c: Add driver for AD5398 VCM lens driver
  5. Adds a driver for the Analog Devices AD5398 10 bit
  6. I2C DAC which is commonly used for driving VCM lens
  7. mechanisms.
  8. Signed-off-by: Dave Stevenson <[email protected]>
  9. ---
  10. drivers/media/i2c/Kconfig | 7 +
  11. drivers/media/i2c/Makefile | 1 +
  12. drivers/media/i2c/ad5398.c | 341 +++++++++++++++++++++++++++++++++++++
  13. 3 files changed, 349 insertions(+)
  14. create mode 100644 drivers/media/i2c/ad5398.c
  15. --- a/drivers/media/i2c/Kconfig
  16. +++ b/drivers/media/i2c/Kconfig
  17. @@ -817,6 +817,13 @@ endif
  18. menu "Lens drivers"
  19. visible if MEDIA_CAMERA_SUPPORT
  20. +config VIDEO_AD5398
  21. + tristate "AD5398 lens voice coil support"
  22. + depends on GPIOLIB && I2C && VIDEO_DEV
  23. + select MEDIA_CONTROLLER
  24. + help
  25. + This is a driver for the AD5398 camera lens voice coil.
  26. +
  27. config VIDEO_AD5820
  28. tristate "AD5820 lens voice coil support"
  29. depends on GPIOLIB && I2C && VIDEO_DEV
  30. --- a/drivers/media/i2c/Makefile
  31. +++ b/drivers/media/i2c/Makefile
  32. @@ -3,6 +3,7 @@
  33. msp3400-objs := msp3400-driver.o msp3400-kthreads.o
  34. obj-$(CONFIG_SDR_MAX2175) += max2175.o
  35. +obj-$(CONFIG_VIDEO_AD5398) += ad5398.o
  36. obj-$(CONFIG_VIDEO_AD5820) += ad5820.o
  37. obj-$(CONFIG_VIDEO_AD9389B) += ad9389b.o
  38. obj-$(CONFIG_VIDEO_ADP1653) += adp1653.o
  39. --- /dev/null
  40. +++ b/drivers/media/i2c/ad5398.c
  41. @@ -0,0 +1,341 @@
  42. +// SPDX-License-Identifier: GPL-2.0-only
  43. +/*
  44. + * AD5398 DAC driver for camera voice coil focus.
  45. + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
  46. + *
  47. + * Based on AD5820 DAC driver by Nokia and TI.
  48. + *
  49. + * This driver uses the regulator framework notification hooks on the
  50. + * assumption that the VCM and sensor share a regulator. This means the VCM
  51. + * position will be restored when either the sensor or VCM subdevices are opened
  52. + * or powered up. The client can therefore choose to ignore the VCM subdevice,
  53. + * and the lens position will be as previously requested. Without that, there
  54. + * is a hard requirement to have the VCM subdevice open in order for the VCM
  55. + * to be powered and at the requested position.
  56. + */
  57. +
  58. +#include <linux/errno.h>
  59. +#include <linux/i2c.h>
  60. +#include <linux/kernel.h>
  61. +#include <linux/module.h>
  62. +#include <linux/regulator/consumer.h>
  63. +#include <linux/gpio/consumer.h>
  64. +
  65. +#include <media/v4l2-ctrls.h>
  66. +#include <media/v4l2-device.h>
  67. +#include <media/v4l2-subdev.h>
  68. +
  69. +/* Register definitions */
  70. +#define AD5398_POWER_DOWN BIT(15)
  71. +#define AD5398_DAC_SHIFT 4
  72. +
  73. +#define to_ad5398_device(sd) container_of(sd, struct ad5398_device, subdev)
  74. +
  75. +struct ad5398_device {
  76. + struct v4l2_subdev subdev;
  77. + struct ad5398_platform_data *platform_data;
  78. + struct regulator *vana;
  79. + struct notifier_block nb;
  80. +
  81. + struct v4l2_ctrl_handler ctrls;
  82. + u32 focus_absolute;
  83. +
  84. + bool standby;
  85. +};
  86. +
  87. +static int ad5398_write(struct ad5398_device *coil, u16 data)
  88. +{
  89. + struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
  90. + struct i2c_msg msg;
  91. + __be16 be_data;
  92. + int r;
  93. +
  94. + if (!client->adapter)
  95. + return -ENODEV;
  96. +
  97. + be_data = cpu_to_be16(data);
  98. + msg.addr = client->addr;
  99. + msg.flags = 0;
  100. + msg.len = 2;
  101. + msg.buf = (u8 *)&be_data;
  102. +
  103. + r = i2c_transfer(client->adapter, &msg, 1);
  104. + if (r < 0) {
  105. + dev_err(&client->dev, "write failed, error %d\n", r);
  106. + return r;
  107. + }
  108. +
  109. + return 0;
  110. +}
  111. +
  112. +/*
  113. + * Calculate status word and write it to the device based on current
  114. + * values of V4L2 controls. It is assumed that the stored V4L2 control
  115. + * values are properly limited and rounded.
  116. + */
  117. +static int ad5398_update_hw(struct ad5398_device *coil)
  118. +{
  119. + u16 status;
  120. +
  121. + status = coil->focus_absolute << AD5398_DAC_SHIFT;
  122. +
  123. + if (coil->standby)
  124. + status |= AD5398_POWER_DOWN;
  125. +
  126. + return ad5398_write(coil, status);
  127. +}
  128. +
  129. +/*
  130. + * Power handling
  131. + */
  132. +static int ad5398_power_off(struct ad5398_device *coil)
  133. +{
  134. + int ret = 0;
  135. +
  136. + coil->standby = true;
  137. + ret = ad5398_update_hw(coil);
  138. +
  139. + return ret;
  140. +}
  141. +
  142. +static int ad5398_power_on(struct ad5398_device *coil)
  143. +{
  144. + int ret;
  145. +
  146. + /* Restore the hardware settings. */
  147. + coil->standby = false;
  148. + ret = ad5398_update_hw(coil);
  149. + if (ret)
  150. + goto fail;
  151. +
  152. + return 0;
  153. +
  154. +fail:
  155. + coil->standby = true;
  156. +
  157. + return ret;
  158. +}
  159. +
  160. +/*
  161. + * V4L2 controls
  162. + */
  163. +static int ad5398_set_ctrl(struct v4l2_ctrl *ctrl)
  164. +{
  165. + struct ad5398_device *coil =
  166. + container_of(ctrl->handler, struct ad5398_device, ctrls);
  167. +
  168. + switch (ctrl->id) {
  169. + case V4L2_CID_FOCUS_ABSOLUTE:
  170. + coil->focus_absolute = ctrl->val;
  171. + return ad5398_update_hw(coil);
  172. + }
  173. +
  174. + return 0;
  175. +}
  176. +
  177. +static const struct v4l2_ctrl_ops ad5398_ctrl_ops = {
  178. + .s_ctrl = ad5398_set_ctrl,
  179. +};
  180. +
  181. +static int ad5398_init_controls(struct ad5398_device *coil)
  182. +{
  183. + v4l2_ctrl_handler_init(&coil->ctrls, 1);
  184. +
  185. + /*
  186. + * V4L2_CID_FOCUS_ABSOLUTE
  187. + *
  188. + * Minimum current is 0 mA, maximum is 120 mA. Thus, 1 code is
  189. + * equivalent to 120/1023 = 0.1173 mA. Nevertheless, we do not use [mA]
  190. + * for focus position, because it is meaningless for user. Meaningful
  191. + * would be to use focus distance or even its inverse, but since the
  192. + * driver doesn't have sufficient knowledge to do the conversion, we
  193. + * will just use abstract codes here. In any case, smaller value = focus
  194. + * position farther from camera. The default zero value means focus at
  195. + * infinity, and also least current consumption.
  196. + */
  197. + v4l2_ctrl_new_std(&coil->ctrls, &ad5398_ctrl_ops,
  198. + V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
  199. +
  200. + if (coil->ctrls.error)
  201. + return coil->ctrls.error;
  202. +
  203. + coil->focus_absolute = 0;
  204. +
  205. + coil->subdev.ctrl_handler = &coil->ctrls;
  206. +
  207. + return 0;
  208. +}
  209. +
  210. +/*
  211. + * V4L2 subdev operations
  212. + */
  213. +static int ad5398_registered(struct v4l2_subdev *subdev)
  214. +{
  215. + struct ad5398_device *coil = to_ad5398_device(subdev);
  216. +
  217. + return ad5398_init_controls(coil);
  218. +}
  219. +
  220. +static int
  221. +ad5398_set_power(struct v4l2_subdev *subdev, int on)
  222. +{
  223. + struct ad5398_device *coil = to_ad5398_device(subdev);
  224. + int ret;
  225. +
  226. + if (on)
  227. + ret = regulator_enable(coil->vana);
  228. + else
  229. + ret = regulator_disable(coil->vana);
  230. +
  231. + return ret;
  232. +}
  233. +
  234. +static int ad5398_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  235. +{
  236. + struct ad5398_device *coil = to_ad5398_device(sd);
  237. +
  238. + return regulator_enable(coil->vana);
  239. +}
  240. +
  241. +static int ad5398_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  242. +{
  243. + struct ad5398_device *coil = to_ad5398_device(sd);
  244. +
  245. + return regulator_disable(coil->vana);
  246. +}
  247. +
  248. +static const struct v4l2_subdev_core_ops ad5398_core_ops = {
  249. + .s_power = ad5398_set_power,
  250. +};
  251. +
  252. +static const struct v4l2_subdev_ops ad5398_ops = {
  253. + .core = &ad5398_core_ops,
  254. +};
  255. +
  256. +static const struct v4l2_subdev_internal_ops ad5398_internal_ops = {
  257. + .registered = ad5398_registered,
  258. + .open = ad5398_open,
  259. + .close = ad5398_close,
  260. +};
  261. +
  262. +/*
  263. + * I2C driver
  264. + */
  265. +static int __maybe_unused ad5398_suspend(struct device *dev)
  266. +{
  267. + struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  268. + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  269. + struct ad5398_device *coil = to_ad5398_device(subdev);
  270. +
  271. + return regulator_enable(coil->vana);
  272. +}
  273. +
  274. +static int __maybe_unused ad5398_resume(struct device *dev)
  275. +{
  276. + struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  277. + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  278. + struct ad5398_device *coil = to_ad5398_device(subdev);
  279. +
  280. + return regulator_disable(coil->vana);
  281. +}
  282. +
  283. +static int ad5398_regulator_notifier(struct notifier_block *nb,
  284. + unsigned long event,
  285. + void *ignored)
  286. +{
  287. + struct ad5398_device *coil = container_of(nb, struct ad5398_device, nb);
  288. +
  289. + if (event == REGULATOR_EVENT_ENABLE)
  290. + ad5398_power_on(coil);
  291. + else if (event == REGULATOR_EVENT_PRE_DISABLE)
  292. + ad5398_power_off(coil);
  293. +
  294. + return NOTIFY_OK;
  295. +}
  296. +
  297. +static int ad5398_probe(struct i2c_client *client,
  298. + const struct i2c_device_id *devid)
  299. +{
  300. + struct ad5398_device *coil;
  301. + int ret;
  302. +
  303. + coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
  304. + if (!coil)
  305. + return -ENOMEM;
  306. +
  307. + coil->vana = devm_regulator_get(&client->dev, "VANA");
  308. + if (IS_ERR(coil->vana)) {
  309. + ret = PTR_ERR(coil->vana);
  310. + if (ret != -EPROBE_DEFER)
  311. + dev_err(&client->dev, "could not get regulator for vana\n");
  312. + return ret;
  313. + }
  314. +
  315. + v4l2_i2c_subdev_init(&coil->subdev, client, &ad5398_ops);
  316. + coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  317. + coil->subdev.internal_ops = &ad5398_internal_ops;
  318. + coil->subdev.entity.function = MEDIA_ENT_F_LENS;
  319. + strscpy(coil->subdev.name, "ad5398 focus", sizeof(coil->subdev.name));
  320. +
  321. + coil->nb.notifier_call = &ad5398_regulator_notifier;
  322. + ret = regulator_register_notifier(coil->vana, &coil->nb);
  323. + if (ret < 0)
  324. + return ret;
  325. +
  326. + ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
  327. + if (ret < 0)
  328. + goto cleanup2;
  329. +
  330. + ret = v4l2_async_register_subdev(&coil->subdev);
  331. + if (ret < 0)
  332. + goto cleanup;
  333. +
  334. + return ret;
  335. +
  336. +cleanup:
  337. + media_entity_cleanup(&coil->subdev.entity);
  338. +cleanup2:
  339. + regulator_unregister_notifier(coil->vana, &coil->nb);
  340. + return ret;
  341. +}
  342. +
  343. +static void ad5398_remove(struct i2c_client *client)
  344. +{
  345. + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  346. + struct ad5398_device *coil = to_ad5398_device(subdev);
  347. +
  348. + v4l2_async_unregister_subdev(&coil->subdev);
  349. + v4l2_ctrl_handler_free(&coil->ctrls);
  350. + media_entity_cleanup(&coil->subdev.entity);
  351. +}
  352. +
  353. +static const struct i2c_device_id ad5398_id_table[] = {
  354. + { "ad5398", 0 },
  355. + { }
  356. +};
  357. +MODULE_DEVICE_TABLE(i2c, ad5398_id_table);
  358. +
  359. +static const struct of_device_id ad5398_of_table[] = {
  360. + { .compatible = "adi,ad5398" },
  361. + { }
  362. +};
  363. +MODULE_DEVICE_TABLE(of, ad5398_of_table);
  364. +
  365. +static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume);
  366. +
  367. +static struct i2c_driver ad5398_i2c_driver = {
  368. + .driver = {
  369. + .name = "ad5398",
  370. + .pm = &ad5398_pm,
  371. + .of_match_table = ad5398_of_table,
  372. + },
  373. + .probe = ad5398_probe,
  374. + .remove = ad5398_remove,
  375. + .id_table = ad5398_id_table,
  376. +};
  377. +
  378. +module_i2c_driver(ad5398_i2c_driver);
  379. +
  380. +MODULE_AUTHOR("Dave Stevenson <[email protected]>");
  381. +MODULE_DESCRIPTION("AD5398 camera lens driver");
  382. +MODULE_LICENSE("GPL");