1 From 75bcf465c32541eb20eb5c75c16e073830c5b1d1 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Thu, 17 Mar 2022 15:13:10 +0000
4 Subject: [PATCH] media: i2c: Rename ad5398 to ad5398_vcm
6 There's already a regulator module called ad5398 that exposes
7 this device through the regulator API. That is meaningless in
8 the terms that it uses and how it maps to V4L2, so a new driver
9 was added. However the module name collision wasn't noted, so
12 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
14 drivers/media/i2c/Makefile | 2 +-
15 drivers/media/i2c/{ad5398.c => ad5398_vcm.c} | 0
16 2 files changed, 1 insertion(+), 1 deletion(-)
17 rename drivers/media/i2c/{ad5398.c => ad5398_vcm.c} (100%)
19 --- a/drivers/media/i2c/Makefile
20 +++ b/drivers/media/i2c/Makefile
22 msp3400-objs := msp3400-driver.o msp3400-kthreads.o
24 obj-$(CONFIG_SDR_MAX2175) += max2175.o
25 -obj-$(CONFIG_VIDEO_AD5398) += ad5398.o
26 +obj-$(CONFIG_VIDEO_AD5398) += ad5398_vcm.o
27 obj-$(CONFIG_VIDEO_AD5820) += ad5820.o
28 obj-$(CONFIG_VIDEO_AD9389B) += ad9389b.o
29 obj-$(CONFIG_VIDEO_ADP1653) += adp1653.o
30 --- a/drivers/media/i2c/ad5398.c
33 -// SPDX-License-Identifier: GPL-2.0-only
35 - * AD5398 DAC driver for camera voice coil focus.
36 - * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
38 - * Based on AD5820 DAC driver by Nokia and TI.
40 - * This driver uses the regulator framework notification hooks on the
41 - * assumption that the VCM and sensor share a regulator. This means the VCM
42 - * position will be restored when either the sensor or VCM subdevices are opened
43 - * or powered up. The client can therefore choose to ignore the VCM subdevice,
44 - * and the lens position will be as previously requested. Without that, there
45 - * is a hard requirement to have the VCM subdevice open in order for the VCM
46 - * to be powered and at the requested position.
49 -#include <linux/errno.h>
50 -#include <linux/i2c.h>
51 -#include <linux/kernel.h>
52 -#include <linux/module.h>
53 -#include <linux/regulator/consumer.h>
54 -#include <linux/gpio/consumer.h>
56 -#include <media/v4l2-ctrls.h>
57 -#include <media/v4l2-device.h>
58 -#include <media/v4l2-subdev.h>
60 -/* Register definitions */
61 -#define AD5398_POWER_DOWN BIT(15)
62 -#define AD5398_DAC_SHIFT 4
64 -#define to_ad5398_device(sd) container_of(sd, struct ad5398_device, subdev)
66 -struct ad5398_device {
67 - struct v4l2_subdev subdev;
68 - struct ad5398_platform_data *platform_data;
69 - struct regulator *vana;
70 - struct notifier_block nb;
72 - struct v4l2_ctrl_handler ctrls;
78 -static int ad5398_write(struct ad5398_device *coil, u16 data)
80 - struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
85 - if (!client->adapter)
88 - be_data = cpu_to_be16(data);
89 - msg.addr = client->addr;
92 - msg.buf = (u8 *)&be_data;
94 - r = i2c_transfer(client->adapter, &msg, 1);
96 - dev_err(&client->dev, "write failed, error %d\n", r);
104 - * Calculate status word and write it to the device based on current
105 - * values of V4L2 controls. It is assumed that the stored V4L2 control
106 - * values are properly limited and rounded.
108 -static int ad5398_update_hw(struct ad5398_device *coil)
112 - status = coil->focus_absolute << AD5398_DAC_SHIFT;
115 - status |= AD5398_POWER_DOWN;
117 - return ad5398_write(coil, status);
123 -static int ad5398_power_off(struct ad5398_device *coil)
127 - coil->standby = true;
128 - ret = ad5398_update_hw(coil);
133 -static int ad5398_power_on(struct ad5398_device *coil)
137 - /* Restore the hardware settings. */
138 - coil->standby = false;
139 - ret = ad5398_update_hw(coil);
146 - coil->standby = true;
154 -static int ad5398_set_ctrl(struct v4l2_ctrl *ctrl)
156 - struct ad5398_device *coil =
157 - container_of(ctrl->handler, struct ad5398_device, ctrls);
159 - switch (ctrl->id) {
160 - case V4L2_CID_FOCUS_ABSOLUTE:
161 - coil->focus_absolute = ctrl->val;
162 - return ad5398_update_hw(coil);
168 -static const struct v4l2_ctrl_ops ad5398_ctrl_ops = {
169 - .s_ctrl = ad5398_set_ctrl,
172 -static int ad5398_init_controls(struct ad5398_device *coil)
174 - v4l2_ctrl_handler_init(&coil->ctrls, 1);
177 - * V4L2_CID_FOCUS_ABSOLUTE
179 - * Minimum current is 0 mA, maximum is 120 mA. Thus, 1 code is
180 - * equivalent to 120/1023 = 0.1173 mA. Nevertheless, we do not use [mA]
181 - * for focus position, because it is meaningless for user. Meaningful
182 - * would be to use focus distance or even its inverse, but since the
183 - * driver doesn't have sufficient knowledge to do the conversion, we
184 - * will just use abstract codes here. In any case, smaller value = focus
185 - * position farther from camera. The default zero value means focus at
186 - * infinity, and also least current consumption.
188 - v4l2_ctrl_new_std(&coil->ctrls, &ad5398_ctrl_ops,
189 - V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
191 - if (coil->ctrls.error)
192 - return coil->ctrls.error;
194 - coil->focus_absolute = 0;
196 - coil->subdev.ctrl_handler = &coil->ctrls;
202 - * V4L2 subdev operations
204 -static int ad5398_registered(struct v4l2_subdev *subdev)
206 - struct ad5398_device *coil = to_ad5398_device(subdev);
208 - return ad5398_init_controls(coil);
212 -ad5398_set_power(struct v4l2_subdev *subdev, int on)
214 - struct ad5398_device *coil = to_ad5398_device(subdev);
218 - ret = regulator_enable(coil->vana);
220 - ret = regulator_disable(coil->vana);
225 -static int ad5398_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
227 - struct ad5398_device *coil = to_ad5398_device(sd);
229 - return regulator_enable(coil->vana);
232 -static int ad5398_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
234 - struct ad5398_device *coil = to_ad5398_device(sd);
236 - return regulator_disable(coil->vana);
239 -static const struct v4l2_subdev_core_ops ad5398_core_ops = {
240 - .s_power = ad5398_set_power,
243 -static const struct v4l2_subdev_ops ad5398_ops = {
244 - .core = &ad5398_core_ops,
247 -static const struct v4l2_subdev_internal_ops ad5398_internal_ops = {
248 - .registered = ad5398_registered,
249 - .open = ad5398_open,
250 - .close = ad5398_close,
256 -static int __maybe_unused ad5398_suspend(struct device *dev)
258 - struct i2c_client *client = container_of(dev, struct i2c_client, dev);
259 - struct v4l2_subdev *subdev = i2c_get_clientdata(client);
260 - struct ad5398_device *coil = to_ad5398_device(subdev);
262 - return regulator_enable(coil->vana);
265 -static int __maybe_unused ad5398_resume(struct device *dev)
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);
271 - return regulator_disable(coil->vana);
274 -static int ad5398_regulator_notifier(struct notifier_block *nb,
275 - unsigned long event,
278 - struct ad5398_device *coil = container_of(nb, struct ad5398_device, nb);
280 - if (event == REGULATOR_EVENT_ENABLE)
281 - ad5398_power_on(coil);
282 - else if (event == REGULATOR_EVENT_PRE_DISABLE)
283 - ad5398_power_off(coil);
288 -static int ad5398_probe(struct i2c_client *client,
289 - const struct i2c_device_id *devid)
291 - struct ad5398_device *coil;
294 - coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
298 - coil->vana = devm_regulator_get(&client->dev, "VANA");
299 - if (IS_ERR(coil->vana)) {
300 - ret = PTR_ERR(coil->vana);
301 - if (ret != -EPROBE_DEFER)
302 - dev_err(&client->dev, "could not get regulator for vana\n");
306 - v4l2_i2c_subdev_init(&coil->subdev, client, &ad5398_ops);
307 - coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
308 - coil->subdev.internal_ops = &ad5398_internal_ops;
309 - coil->subdev.entity.function = MEDIA_ENT_F_LENS;
310 - strscpy(coil->subdev.name, "ad5398 focus", sizeof(coil->subdev.name));
312 - coil->nb.notifier_call = &ad5398_regulator_notifier;
313 - ret = regulator_register_notifier(coil->vana, &coil->nb);
317 - ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
321 - ret = v4l2_async_register_subdev(&coil->subdev);
328 - media_entity_cleanup(&coil->subdev.entity);
330 - regulator_unregister_notifier(coil->vana, &coil->nb);
334 -static void ad5398_remove(struct i2c_client *client)
336 - struct v4l2_subdev *subdev = i2c_get_clientdata(client);
337 - struct ad5398_device *coil = to_ad5398_device(subdev);
339 - v4l2_async_unregister_subdev(&coil->subdev);
340 - v4l2_ctrl_handler_free(&coil->ctrls);
341 - media_entity_cleanup(&coil->subdev.entity);
344 -static const struct i2c_device_id ad5398_id_table[] = {
348 -MODULE_DEVICE_TABLE(i2c, ad5398_id_table);
350 -static const struct of_device_id ad5398_of_table[] = {
351 - { .compatible = "adi,ad5398" },
354 -MODULE_DEVICE_TABLE(of, ad5398_of_table);
356 -static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume);
358 -static struct i2c_driver ad5398_i2c_driver = {
362 - .of_match_table = ad5398_of_table,
364 - .probe = ad5398_probe,
365 - .remove = ad5398_remove,
366 - .id_table = ad5398_id_table,
369 -module_i2c_driver(ad5398_i2c_driver);
371 -MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
372 -MODULE_DESCRIPTION("AD5398 camera lens driver");
373 -MODULE_LICENSE("GPL");
375 +++ b/drivers/media/i2c/ad5398_vcm.c
377 +// SPDX-License-Identifier: GPL-2.0-only
379 + * AD5398 DAC driver for camera voice coil focus.
380 + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
382 + * Based on AD5820 DAC driver by Nokia and TI.
384 + * This driver uses the regulator framework notification hooks on the
385 + * assumption that the VCM and sensor share a regulator. This means the VCM
386 + * position will be restored when either the sensor or VCM subdevices are opened
387 + * or powered up. The client can therefore choose to ignore the VCM subdevice,
388 + * and the lens position will be as previously requested. Without that, there
389 + * is a hard requirement to have the VCM subdevice open in order for the VCM
390 + * to be powered and at the requested position.
393 +#include <linux/errno.h>
394 +#include <linux/i2c.h>
395 +#include <linux/kernel.h>
396 +#include <linux/module.h>
397 +#include <linux/regulator/consumer.h>
398 +#include <linux/gpio/consumer.h>
400 +#include <media/v4l2-ctrls.h>
401 +#include <media/v4l2-device.h>
402 +#include <media/v4l2-subdev.h>
404 +/* Register definitions */
405 +#define AD5398_POWER_DOWN BIT(15)
406 +#define AD5398_DAC_SHIFT 4
408 +#define to_ad5398_device(sd) container_of(sd, struct ad5398_device, subdev)
410 +struct ad5398_device {
411 + struct v4l2_subdev subdev;
412 + struct ad5398_platform_data *platform_data;
413 + struct regulator *vana;
414 + struct notifier_block nb;
416 + struct v4l2_ctrl_handler ctrls;
417 + u32 focus_absolute;
422 +static int ad5398_write(struct ad5398_device *coil, u16 data)
424 + struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
425 + struct i2c_msg msg;
429 + if (!client->adapter)
432 + be_data = cpu_to_be16(data);
433 + msg.addr = client->addr;
436 + msg.buf = (u8 *)&be_data;
438 + r = i2c_transfer(client->adapter, &msg, 1);
440 + dev_err(&client->dev, "write failed, error %d\n", r);
448 + * Calculate status word and write it to the device based on current
449 + * values of V4L2 controls. It is assumed that the stored V4L2 control
450 + * values are properly limited and rounded.
452 +static int ad5398_update_hw(struct ad5398_device *coil)
456 + status = coil->focus_absolute << AD5398_DAC_SHIFT;
459 + status |= AD5398_POWER_DOWN;
461 + return ad5398_write(coil, status);
467 +static int ad5398_power_off(struct ad5398_device *coil)
471 + coil->standby = true;
472 + ret = ad5398_update_hw(coil);
477 +static int ad5398_power_on(struct ad5398_device *coil)
481 + /* Restore the hardware settings. */
482 + coil->standby = false;
483 + ret = ad5398_update_hw(coil);
490 + coil->standby = true;
498 +static int ad5398_set_ctrl(struct v4l2_ctrl *ctrl)
500 + struct ad5398_device *coil =
501 + container_of(ctrl->handler, struct ad5398_device, ctrls);
503 + switch (ctrl->id) {
504 + case V4L2_CID_FOCUS_ABSOLUTE:
505 + coil->focus_absolute = ctrl->val;
506 + return ad5398_update_hw(coil);
512 +static const struct v4l2_ctrl_ops ad5398_ctrl_ops = {
513 + .s_ctrl = ad5398_set_ctrl,
516 +static int ad5398_init_controls(struct ad5398_device *coil)
518 + v4l2_ctrl_handler_init(&coil->ctrls, 1);
521 + * V4L2_CID_FOCUS_ABSOLUTE
523 + * Minimum current is 0 mA, maximum is 120 mA. Thus, 1 code is
524 + * equivalent to 120/1023 = 0.1173 mA. Nevertheless, we do not use [mA]
525 + * for focus position, because it is meaningless for user. Meaningful
526 + * would be to use focus distance or even its inverse, but since the
527 + * driver doesn't have sufficient knowledge to do the conversion, we
528 + * will just use abstract codes here. In any case, smaller value = focus
529 + * position farther from camera. The default zero value means focus at
530 + * infinity, and also least current consumption.
532 + v4l2_ctrl_new_std(&coil->ctrls, &ad5398_ctrl_ops,
533 + V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
535 + if (coil->ctrls.error)
536 + return coil->ctrls.error;
538 + coil->focus_absolute = 0;
540 + coil->subdev.ctrl_handler = &coil->ctrls;
546 + * V4L2 subdev operations
548 +static int ad5398_registered(struct v4l2_subdev *subdev)
550 + struct ad5398_device *coil = to_ad5398_device(subdev);
552 + return ad5398_init_controls(coil);
556 +ad5398_set_power(struct v4l2_subdev *subdev, int on)
558 + struct ad5398_device *coil = to_ad5398_device(subdev);
562 + ret = regulator_enable(coil->vana);
564 + ret = regulator_disable(coil->vana);
569 +static int ad5398_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
571 + struct ad5398_device *coil = to_ad5398_device(sd);
573 + return regulator_enable(coil->vana);
576 +static int ad5398_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
578 + struct ad5398_device *coil = to_ad5398_device(sd);
580 + return regulator_disable(coil->vana);
583 +static const struct v4l2_subdev_core_ops ad5398_core_ops = {
584 + .s_power = ad5398_set_power,
587 +static const struct v4l2_subdev_ops ad5398_ops = {
588 + .core = &ad5398_core_ops,
591 +static const struct v4l2_subdev_internal_ops ad5398_internal_ops = {
592 + .registered = ad5398_registered,
593 + .open = ad5398_open,
594 + .close = ad5398_close,
600 +static int __maybe_unused ad5398_suspend(struct device *dev)
602 + struct i2c_client *client = container_of(dev, struct i2c_client, dev);
603 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
604 + struct ad5398_device *coil = to_ad5398_device(subdev);
606 + return regulator_enable(coil->vana);
609 +static int __maybe_unused ad5398_resume(struct device *dev)
611 + struct i2c_client *client = container_of(dev, struct i2c_client, dev);
612 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
613 + struct ad5398_device *coil = to_ad5398_device(subdev);
615 + return regulator_disable(coil->vana);
618 +static int ad5398_regulator_notifier(struct notifier_block *nb,
619 + unsigned long event,
622 + struct ad5398_device *coil = container_of(nb, struct ad5398_device, nb);
624 + if (event == REGULATOR_EVENT_ENABLE)
625 + ad5398_power_on(coil);
626 + else if (event == REGULATOR_EVENT_PRE_DISABLE)
627 + ad5398_power_off(coil);
632 +static int ad5398_probe(struct i2c_client *client,
633 + const struct i2c_device_id *devid)
635 + struct ad5398_device *coil;
638 + coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
642 + coil->vana = devm_regulator_get(&client->dev, "VANA");
643 + if (IS_ERR(coil->vana)) {
644 + ret = PTR_ERR(coil->vana);
645 + if (ret != -EPROBE_DEFER)
646 + dev_err(&client->dev, "could not get regulator for vana\n");
650 + v4l2_i2c_subdev_init(&coil->subdev, client, &ad5398_ops);
651 + coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
652 + coil->subdev.internal_ops = &ad5398_internal_ops;
653 + coil->subdev.entity.function = MEDIA_ENT_F_LENS;
654 + strscpy(coil->subdev.name, "ad5398 focus", sizeof(coil->subdev.name));
656 + coil->nb.notifier_call = &ad5398_regulator_notifier;
657 + ret = regulator_register_notifier(coil->vana, &coil->nb);
661 + ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
665 + ret = v4l2_async_register_subdev(&coil->subdev);
672 + media_entity_cleanup(&coil->subdev.entity);
674 + regulator_unregister_notifier(coil->vana, &coil->nb);
678 +static void ad5398_remove(struct i2c_client *client)
680 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
681 + struct ad5398_device *coil = to_ad5398_device(subdev);
683 + v4l2_async_unregister_subdev(&coil->subdev);
684 + v4l2_ctrl_handler_free(&coil->ctrls);
685 + media_entity_cleanup(&coil->subdev.entity);
688 +static const struct i2c_device_id ad5398_id_table[] = {
692 +MODULE_DEVICE_TABLE(i2c, ad5398_id_table);
694 +static const struct of_device_id ad5398_of_table[] = {
695 + { .compatible = "adi,ad5398" },
698 +MODULE_DEVICE_TABLE(of, ad5398_of_table);
700 +static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume);
702 +static struct i2c_driver ad5398_i2c_driver = {
706 + .of_match_table = ad5398_of_table,
708 + .probe = ad5398_probe,
709 + .remove = ad5398_remove,
710 + .id_table = ad5398_id_table,
713 +module_i2c_driver(ad5398_i2c_driver);
715 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
716 +MODULE_DESCRIPTION("AD5398 camera lens driver");
717 +MODULE_LICENSE("GPL");