1 From 099c31b8568b7e8c11e57ffced2da1cbccbfbcad Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Wed, 29 Sep 2021 14:04:28 +0100
4 Subject: [PATCH] media: i2c: Add driver for AD5398 VCM lens driver
6 Adds a driver for the Analog Devices AD5398 10 bit
7 I2C DAC which is commonly used for driving VCM lens
10 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
12 drivers/media/i2c/Kconfig | 7 +
13 drivers/media/i2c/Makefile | 1 +
14 drivers/media/i2c/ad5398.c | 341 +++++++++++++++++++++++++++++++++++++
15 3 files changed, 349 insertions(+)
16 create mode 100644 drivers/media/i2c/ad5398.c
18 --- a/drivers/media/i2c/Kconfig
19 +++ b/drivers/media/i2c/Kconfig
20 @@ -810,6 +810,13 @@ endmenu
22 visible if MEDIA_CAMERA_SUPPORT
25 + tristate "AD5398 lens voice coil support"
26 + depends on GPIOLIB && I2C && VIDEO_DEV
27 + select MEDIA_CONTROLLER
29 + This is a driver for the AD5398 camera lens voice coil.
32 tristate "AD5820 lens voice coil support"
33 depends on GPIOLIB && I2C && VIDEO_DEV
34 --- a/drivers/media/i2c/Makefile
35 +++ b/drivers/media/i2c/Makefile
37 msp3400-objs := msp3400-driver.o msp3400-kthreads.o
39 obj-$(CONFIG_SDR_MAX2175) += max2175.o
40 +obj-$(CONFIG_VIDEO_AD5398) += ad5398.o
41 obj-$(CONFIG_VIDEO_AD5820) += ad5820.o
42 obj-$(CONFIG_VIDEO_AD9389B) += ad9389b.o
43 obj-$(CONFIG_VIDEO_ADP1653) += adp1653.o
45 +++ b/drivers/media/i2c/ad5398.c
47 +// SPDX-License-Identifier: GPL-2.0-only
49 + * AD5398 DAC driver for camera voice coil focus.
50 + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
52 + * Based on AD5820 DAC driver by Nokia and TI.
54 + * This driver uses the regulator framework notification hooks on the
55 + * assumption that the VCM and sensor share a regulator. This means the VCM
56 + * position will be restored when either the sensor or VCM subdevices are opened
57 + * or powered up. The client can therefore choose to ignore the VCM subdevice,
58 + * and the lens position will be as previously requested. Without that, there
59 + * is a hard requirement to have the VCM subdevice open in order for the VCM
60 + * to be powered and at the requested position.
63 +#include <linux/errno.h>
64 +#include <linux/i2c.h>
65 +#include <linux/kernel.h>
66 +#include <linux/module.h>
67 +#include <linux/regulator/consumer.h>
68 +#include <linux/gpio/consumer.h>
70 +#include <media/v4l2-ctrls.h>
71 +#include <media/v4l2-device.h>
72 +#include <media/v4l2-subdev.h>
74 +/* Register definitions */
75 +#define AD5398_POWER_DOWN BIT(15)
76 +#define AD5398_DAC_SHIFT 4
78 +#define to_ad5398_device(sd) container_of(sd, struct ad5398_device, subdev)
80 +struct ad5398_device {
81 + struct v4l2_subdev subdev;
82 + struct ad5398_platform_data *platform_data;
83 + struct regulator *vana;
84 + struct notifier_block nb;
86 + struct v4l2_ctrl_handler ctrls;
92 +static int ad5398_write(struct ad5398_device *coil, u16 data)
94 + struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
99 + if (!client->adapter)
102 + be_data = cpu_to_be16(data);
103 + msg.addr = client->addr;
106 + msg.buf = (u8 *)&be_data;
108 + r = i2c_transfer(client->adapter, &msg, 1);
110 + dev_err(&client->dev, "write failed, error %d\n", r);
118 + * Calculate status word and write it to the device based on current
119 + * values of V4L2 controls. It is assumed that the stored V4L2 control
120 + * values are properly limited and rounded.
122 +static int ad5398_update_hw(struct ad5398_device *coil)
126 + status = coil->focus_absolute << AD5398_DAC_SHIFT;
129 + status |= AD5398_POWER_DOWN;
131 + return ad5398_write(coil, status);
137 +static int ad5398_power_off(struct ad5398_device *coil)
141 + coil->standby = true;
142 + ret = ad5398_update_hw(coil);
147 +static int ad5398_power_on(struct ad5398_device *coil)
151 + /* Restore the hardware settings. */
152 + coil->standby = false;
153 + ret = ad5398_update_hw(coil);
160 + coil->standby = true;
168 +static int ad5398_set_ctrl(struct v4l2_ctrl *ctrl)
170 + struct ad5398_device *coil =
171 + container_of(ctrl->handler, struct ad5398_device, ctrls);
173 + switch (ctrl->id) {
174 + case V4L2_CID_FOCUS_ABSOLUTE:
175 + coil->focus_absolute = ctrl->val;
176 + return ad5398_update_hw(coil);
182 +static const struct v4l2_ctrl_ops ad5398_ctrl_ops = {
183 + .s_ctrl = ad5398_set_ctrl,
186 +static int ad5398_init_controls(struct ad5398_device *coil)
188 + v4l2_ctrl_handler_init(&coil->ctrls, 1);
191 + * V4L2_CID_FOCUS_ABSOLUTE
193 + * Minimum current is 0 mA, maximum is 120 mA. Thus, 1 code is
194 + * equivalent to 120/1023 = 0.1173 mA. Nevertheless, we do not use [mA]
195 + * for focus position, because it is meaningless for user. Meaningful
196 + * would be to use focus distance or even its inverse, but since the
197 + * driver doesn't have sufficient knowledge to do the conversion, we
198 + * will just use abstract codes here. In any case, smaller value = focus
199 + * position farther from camera. The default zero value means focus at
200 + * infinity, and also least current consumption.
202 + v4l2_ctrl_new_std(&coil->ctrls, &ad5398_ctrl_ops,
203 + V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
205 + if (coil->ctrls.error)
206 + return coil->ctrls.error;
208 + coil->focus_absolute = 0;
210 + coil->subdev.ctrl_handler = &coil->ctrls;
216 + * V4L2 subdev operations
218 +static int ad5398_registered(struct v4l2_subdev *subdev)
220 + struct ad5398_device *coil = to_ad5398_device(subdev);
222 + return ad5398_init_controls(coil);
226 +ad5398_set_power(struct v4l2_subdev *subdev, int on)
228 + struct ad5398_device *coil = to_ad5398_device(subdev);
232 + ret = regulator_enable(coil->vana);
234 + ret = regulator_disable(coil->vana);
239 +static int ad5398_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
241 + struct ad5398_device *coil = to_ad5398_device(sd);
243 + return regulator_enable(coil->vana);
246 +static int ad5398_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
248 + struct ad5398_device *coil = to_ad5398_device(sd);
250 + return regulator_disable(coil->vana);
253 +static const struct v4l2_subdev_core_ops ad5398_core_ops = {
254 + .s_power = ad5398_set_power,
257 +static const struct v4l2_subdev_ops ad5398_ops = {
258 + .core = &ad5398_core_ops,
261 +static const struct v4l2_subdev_internal_ops ad5398_internal_ops = {
262 + .registered = ad5398_registered,
263 + .open = ad5398_open,
264 + .close = ad5398_close,
270 +static int __maybe_unused ad5398_suspend(struct device *dev)
272 + struct i2c_client *client = container_of(dev, struct i2c_client, dev);
273 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
274 + struct ad5398_device *coil = to_ad5398_device(subdev);
276 + return regulator_enable(coil->vana);
279 +static int __maybe_unused ad5398_resume(struct device *dev)
281 + struct i2c_client *client = container_of(dev, struct i2c_client, dev);
282 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
283 + struct ad5398_device *coil = to_ad5398_device(subdev);
285 + return regulator_disable(coil->vana);
288 +static int ad5398_regulator_notifier(struct notifier_block *nb,
289 + unsigned long event,
292 + struct ad5398_device *coil = container_of(nb, struct ad5398_device, nb);
294 + if (event == REGULATOR_EVENT_ENABLE)
295 + ad5398_power_on(coil);
296 + else if (event == REGULATOR_EVENT_PRE_DISABLE)
297 + ad5398_power_off(coil);
302 +static int ad5398_probe(struct i2c_client *client,
303 + const struct i2c_device_id *devid)
305 + struct ad5398_device *coil;
308 + coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
312 + coil->vana = devm_regulator_get(&client->dev, "VANA");
313 + if (IS_ERR(coil->vana)) {
314 + ret = PTR_ERR(coil->vana);
315 + if (ret != -EPROBE_DEFER)
316 + dev_err(&client->dev, "could not get regulator for vana\n");
320 + v4l2_i2c_subdev_init(&coil->subdev, client, &ad5398_ops);
321 + coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
322 + coil->subdev.internal_ops = &ad5398_internal_ops;
323 + coil->subdev.entity.function = MEDIA_ENT_F_LENS;
324 + strscpy(coil->subdev.name, "ad5398 focus", sizeof(coil->subdev.name));
326 + coil->nb.notifier_call = &ad5398_regulator_notifier;
327 + ret = regulator_register_notifier(coil->vana, &coil->nb);
331 + ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
335 + ret = v4l2_async_register_subdev(&coil->subdev);
342 + media_entity_cleanup(&coil->subdev.entity);
344 + regulator_unregister_notifier(coil->vana, &coil->nb);
348 +static void ad5398_remove(struct i2c_client *client)
350 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
351 + struct ad5398_device *coil = to_ad5398_device(subdev);
353 + v4l2_async_unregister_subdev(&coil->subdev);
354 + v4l2_ctrl_handler_free(&coil->ctrls);
355 + media_entity_cleanup(&coil->subdev.entity);
358 +static const struct i2c_device_id ad5398_id_table[] = {
362 +MODULE_DEVICE_TABLE(i2c, ad5398_id_table);
364 +static const struct of_device_id ad5398_of_table[] = {
365 + { .compatible = "adi,ad5398" },
368 +MODULE_DEVICE_TABLE(of, ad5398_of_table);
370 +static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume);
372 +static struct i2c_driver ad5398_i2c_driver = {
376 + .of_match_table = ad5398_of_table,
378 + .probe = ad5398_probe,
379 + .remove = ad5398_remove,
380 + .id_table = ad5398_id_table,
383 +module_i2c_driver(ad5398_i2c_driver);
385 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
386 +MODULE_DESCRIPTION("AD5398 camera lens driver");
387 +MODULE_LICENSE("GPL");