1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD5398 DAC driver for camera voice coil focus.
4 * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
6 * Based on AD5820 DAC driver by Nokia and TI.
8 * This driver uses the regulator framework notification hooks on the
9 * assumption that the VCM and sensor share a regulator. This means the VCM
10 * position will be restored when either the sensor or VCM subdevices are opened
11 * or powered up. The client can therefore choose to ignore the VCM subdevice,
12 * and the lens position will be as previously requested. Without that, there
13 * is a hard requirement to have the VCM subdevice open in order for the VCM
14 * to be powered and at the requested position.
17 #include <linux/errno.h>
18 #include <linux/i2c.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/gpio/consumer.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-subdev.h>
28 /* Register definitions */
29 #define AD5398_POWER_DOWN BIT(15)
30 #define AD5398_DAC_SHIFT 4
32 #define to_ad5398_device(sd) container_of(sd, struct ad5398_device, subdev)
34 struct ad5398_device {
35 struct v4l2_subdev subdev;
36 struct ad5398_platform_data *platform_data;
37 struct regulator *vana;
38 struct notifier_block nb;
40 struct v4l2_ctrl_handler ctrls;
46 static int ad5398_write(struct ad5398_device *coil, u16 data)
48 struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
56 be_data = cpu_to_be16(data);
57 msg.addr = client->addr;
60 msg.buf = (u8 *)&be_data;
62 r = i2c_transfer(client->adapter, &msg, 1);
64 dev_err(&client->dev, "write failed, error %d\n", r);
72 * Calculate status word and write it to the device based on current
73 * values of V4L2 controls. It is assumed that the stored V4L2 control
74 * values are properly limited and rounded.
76 static int ad5398_update_hw(struct ad5398_device *coil)
80 status = coil->focus_absolute << AD5398_DAC_SHIFT;
83 status |= AD5398_POWER_DOWN;
85 return ad5398_write(coil, status);
91 static int ad5398_power_off(struct ad5398_device *coil)
96 ret = ad5398_update_hw(coil);
101 static int ad5398_power_on(struct ad5398_device *coil)
105 /* Restore the hardware settings. */
106 coil->standby = false;
107 ret = ad5398_update_hw(coil);
114 coil->standby = true;
122 static int ad5398_set_ctrl(struct v4l2_ctrl *ctrl)
124 struct ad5398_device *coil =
125 container_of(ctrl->handler, struct ad5398_device, ctrls);
128 case V4L2_CID_FOCUS_ABSOLUTE:
129 coil->focus_absolute = ctrl->val;
130 return ad5398_update_hw(coil);
136 static const struct v4l2_ctrl_ops ad5398_ctrl_ops = {
137 .s_ctrl = ad5398_set_ctrl,
140 static int ad5398_init_controls(struct ad5398_device *coil)
142 v4l2_ctrl_handler_init(&coil->ctrls, 1);
145 * V4L2_CID_FOCUS_ABSOLUTE
147 * Minimum current is 0 mA, maximum is 120 mA. Thus, 1 code is
148 * equivalent to 120/1023 = 0.1173 mA. Nevertheless, we do not use [mA]
149 * for focus position, because it is meaningless for user. Meaningful
150 * would be to use focus distance or even its inverse, but since the
151 * driver doesn't have sufficient knowledge to do the conversion, we
152 * will just use abstract codes here. In any case, smaller value = focus
153 * position farther from camera. The default zero value means focus at
154 * infinity, and also least current consumption.
156 v4l2_ctrl_new_std(&coil->ctrls, &ad5398_ctrl_ops,
157 V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
159 if (coil->ctrls.error)
160 return coil->ctrls.error;
162 coil->focus_absolute = 0;
164 coil->subdev.ctrl_handler = &coil->ctrls;
170 * V4L2 subdev operations
172 static int ad5398_registered(struct v4l2_subdev *subdev)
174 struct ad5398_device *coil = to_ad5398_device(subdev);
176 return ad5398_init_controls(coil);
180 ad5398_set_power(struct v4l2_subdev *subdev, int on)
182 struct ad5398_device *coil = to_ad5398_device(subdev);
186 ret = regulator_enable(coil->vana);
188 ret = regulator_disable(coil->vana);
193 static int ad5398_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
195 struct ad5398_device *coil = to_ad5398_device(sd);
197 return regulator_enable(coil->vana);
200 static int ad5398_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
202 struct ad5398_device *coil = to_ad5398_device(sd);
204 return regulator_disable(coil->vana);
207 static const struct v4l2_subdev_core_ops ad5398_core_ops = {
208 .s_power = ad5398_set_power,
211 static const struct v4l2_subdev_ops ad5398_ops = {
212 .core = &ad5398_core_ops,
215 static const struct v4l2_subdev_internal_ops ad5398_internal_ops = {
216 .registered = ad5398_registered,
218 .close = ad5398_close,
224 static int __maybe_unused ad5398_suspend(struct device *dev)
226 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
227 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
228 struct ad5398_device *coil = to_ad5398_device(subdev);
230 return regulator_enable(coil->vana);
233 static int __maybe_unused ad5398_resume(struct device *dev)
235 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
236 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
237 struct ad5398_device *coil = to_ad5398_device(subdev);
239 return regulator_disable(coil->vana);
242 static int ad5398_regulator_notifier(struct notifier_block *nb,
246 struct ad5398_device *coil = container_of(nb, struct ad5398_device, nb);
248 if (event == REGULATOR_EVENT_ENABLE)
249 ad5398_power_on(coil);
250 else if (event == REGULATOR_EVENT_PRE_DISABLE)
251 ad5398_power_off(coil);
256 static int ad5398_probe(struct i2c_client *client,
257 const struct i2c_device_id *devid)
259 struct ad5398_device *coil;
262 coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
266 coil->vana = devm_regulator_get(&client->dev, "VANA");
267 if (IS_ERR(coil->vana)) {
268 ret = PTR_ERR(coil->vana);
269 if (ret != -EPROBE_DEFER)
270 dev_err(&client->dev, "could not get regulator for vana\n");
274 v4l2_i2c_subdev_init(&coil->subdev, client, &ad5398_ops);
275 coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
276 coil->subdev.internal_ops = &ad5398_internal_ops;
277 coil->subdev.entity.function = MEDIA_ENT_F_LENS;
278 strscpy(coil->subdev.name, "ad5398 focus", sizeof(coil->subdev.name));
280 coil->nb.notifier_call = &ad5398_regulator_notifier;
281 ret = regulator_register_notifier(coil->vana, &coil->nb);
285 ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
289 ret = v4l2_async_register_subdev(&coil->subdev);
296 media_entity_cleanup(&coil->subdev.entity);
298 regulator_unregister_notifier(coil->vana, &coil->nb);
302 static int ad5398_remove(struct i2c_client *client)
304 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
305 struct ad5398_device *coil = to_ad5398_device(subdev);
307 v4l2_async_unregister_subdev(&coil->subdev);
308 v4l2_ctrl_handler_free(&coil->ctrls);
309 media_entity_cleanup(&coil->subdev.entity);
313 static const struct i2c_device_id ad5398_id_table[] = {
317 MODULE_DEVICE_TABLE(i2c, ad5398_id_table);
319 static const struct of_device_id ad5398_of_table[] = {
320 { .compatible = "adi,ad5398" },
323 MODULE_DEVICE_TABLE(of, ad5398_of_table);
325 static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume);
327 static struct i2c_driver ad5398_i2c_driver = {
331 .of_match_table = ad5398_of_table,
333 .probe = ad5398_probe,
334 .remove = ad5398_remove,
335 .id_table = ad5398_id_table,
338 module_i2c_driver(ad5398_i2c_driver);
340 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
341 MODULE_DESCRIPTION("AD5398 camera lens driver");
342 MODULE_LICENSE("GPL");