1 From 31bcdc66bb6bb5ae9b700693786a5ab46231484c 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
21 @@ -21,7 +21,7 @@ obj-$(CONFIG_VIDEO_SAA717X) += saa717x.o
22 obj-$(CONFIG_VIDEO_SAA7127) += saa7127.o
23 obj-$(CONFIG_VIDEO_SAA7185) += saa7185.o
24 obj-$(CONFIG_VIDEO_SAA6752HS) += saa6752hs.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_AK7375) += ak7375.o
29 obj-$(CONFIG_VIDEO_DW9714) += dw9714.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 int 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);
345 -static const struct i2c_device_id ad5398_id_table[] = {
349 -MODULE_DEVICE_TABLE(i2c, ad5398_id_table);
351 -static const struct of_device_id ad5398_of_table[] = {
352 - { .compatible = "adi,ad5398" },
355 -MODULE_DEVICE_TABLE(of, ad5398_of_table);
357 -static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume);
359 -static struct i2c_driver ad5398_i2c_driver = {
363 - .of_match_table = ad5398_of_table,
365 - .probe = ad5398_probe,
366 - .remove = ad5398_remove,
367 - .id_table = ad5398_id_table,
370 -module_i2c_driver(ad5398_i2c_driver);
372 -MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
373 -MODULE_DESCRIPTION("AD5398 camera lens driver");
374 -MODULE_LICENSE("GPL");
376 +++ b/drivers/media/i2c/ad5398_vcm.c
378 +// SPDX-License-Identifier: GPL-2.0-only
380 + * AD5398 DAC driver for camera voice coil focus.
381 + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
383 + * Based on AD5820 DAC driver by Nokia and TI.
385 + * This driver uses the regulator framework notification hooks on the
386 + * assumption that the VCM and sensor share a regulator. This means the VCM
387 + * position will be restored when either the sensor or VCM subdevices are opened
388 + * or powered up. The client can therefore choose to ignore the VCM subdevice,
389 + * and the lens position will be as previously requested. Without that, there
390 + * is a hard requirement to have the VCM subdevice open in order for the VCM
391 + * to be powered and at the requested position.
394 +#include <linux/errno.h>
395 +#include <linux/i2c.h>
396 +#include <linux/kernel.h>
397 +#include <linux/module.h>
398 +#include <linux/regulator/consumer.h>
399 +#include <linux/gpio/consumer.h>
401 +#include <media/v4l2-ctrls.h>
402 +#include <media/v4l2-device.h>
403 +#include <media/v4l2-subdev.h>
405 +/* Register definitions */
406 +#define AD5398_POWER_DOWN BIT(15)
407 +#define AD5398_DAC_SHIFT 4
409 +#define to_ad5398_device(sd) container_of(sd, struct ad5398_device, subdev)
411 +struct ad5398_device {
412 + struct v4l2_subdev subdev;
413 + struct ad5398_platform_data *platform_data;
414 + struct regulator *vana;
415 + struct notifier_block nb;
417 + struct v4l2_ctrl_handler ctrls;
418 + u32 focus_absolute;
423 +static int ad5398_write(struct ad5398_device *coil, u16 data)
425 + struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
426 + struct i2c_msg msg;
430 + if (!client->adapter)
433 + be_data = cpu_to_be16(data);
434 + msg.addr = client->addr;
437 + msg.buf = (u8 *)&be_data;
439 + r = i2c_transfer(client->adapter, &msg, 1);
441 + dev_err(&client->dev, "write failed, error %d\n", r);
449 + * Calculate status word and write it to the device based on current
450 + * values of V4L2 controls. It is assumed that the stored V4L2 control
451 + * values are properly limited and rounded.
453 +static int ad5398_update_hw(struct ad5398_device *coil)
457 + status = coil->focus_absolute << AD5398_DAC_SHIFT;
460 + status |= AD5398_POWER_DOWN;
462 + return ad5398_write(coil, status);
468 +static int ad5398_power_off(struct ad5398_device *coil)
472 + coil->standby = true;
473 + ret = ad5398_update_hw(coil);
478 +static int ad5398_power_on(struct ad5398_device *coil)
482 + /* Restore the hardware settings. */
483 + coil->standby = false;
484 + ret = ad5398_update_hw(coil);
491 + coil->standby = true;
499 +static int ad5398_set_ctrl(struct v4l2_ctrl *ctrl)
501 + struct ad5398_device *coil =
502 + container_of(ctrl->handler, struct ad5398_device, ctrls);
504 + switch (ctrl->id) {
505 + case V4L2_CID_FOCUS_ABSOLUTE:
506 + coil->focus_absolute = ctrl->val;
507 + return ad5398_update_hw(coil);
513 +static const struct v4l2_ctrl_ops ad5398_ctrl_ops = {
514 + .s_ctrl = ad5398_set_ctrl,
517 +static int ad5398_init_controls(struct ad5398_device *coil)
519 + v4l2_ctrl_handler_init(&coil->ctrls, 1);
522 + * V4L2_CID_FOCUS_ABSOLUTE
524 + * Minimum current is 0 mA, maximum is 120 mA. Thus, 1 code is
525 + * equivalent to 120/1023 = 0.1173 mA. Nevertheless, we do not use [mA]
526 + * for focus position, because it is meaningless for user. Meaningful
527 + * would be to use focus distance or even its inverse, but since the
528 + * driver doesn't have sufficient knowledge to do the conversion, we
529 + * will just use abstract codes here. In any case, smaller value = focus
530 + * position farther from camera. The default zero value means focus at
531 + * infinity, and also least current consumption.
533 + v4l2_ctrl_new_std(&coil->ctrls, &ad5398_ctrl_ops,
534 + V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
536 + if (coil->ctrls.error)
537 + return coil->ctrls.error;
539 + coil->focus_absolute = 0;
541 + coil->subdev.ctrl_handler = &coil->ctrls;
547 + * V4L2 subdev operations
549 +static int ad5398_registered(struct v4l2_subdev *subdev)
551 + struct ad5398_device *coil = to_ad5398_device(subdev);
553 + return ad5398_init_controls(coil);
557 +ad5398_set_power(struct v4l2_subdev *subdev, int on)
559 + struct ad5398_device *coil = to_ad5398_device(subdev);
563 + ret = regulator_enable(coil->vana);
565 + ret = regulator_disable(coil->vana);
570 +static int ad5398_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
572 + struct ad5398_device *coil = to_ad5398_device(sd);
574 + return regulator_enable(coil->vana);
577 +static int ad5398_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
579 + struct ad5398_device *coil = to_ad5398_device(sd);
581 + return regulator_disable(coil->vana);
584 +static const struct v4l2_subdev_core_ops ad5398_core_ops = {
585 + .s_power = ad5398_set_power,
588 +static const struct v4l2_subdev_ops ad5398_ops = {
589 + .core = &ad5398_core_ops,
592 +static const struct v4l2_subdev_internal_ops ad5398_internal_ops = {
593 + .registered = ad5398_registered,
594 + .open = ad5398_open,
595 + .close = ad5398_close,
601 +static int __maybe_unused ad5398_suspend(struct device *dev)
603 + struct i2c_client *client = container_of(dev, struct i2c_client, dev);
604 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
605 + struct ad5398_device *coil = to_ad5398_device(subdev);
607 + return regulator_enable(coil->vana);
610 +static int __maybe_unused ad5398_resume(struct device *dev)
612 + struct i2c_client *client = container_of(dev, struct i2c_client, dev);
613 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
614 + struct ad5398_device *coil = to_ad5398_device(subdev);
616 + return regulator_disable(coil->vana);
619 +static int ad5398_regulator_notifier(struct notifier_block *nb,
620 + unsigned long event,
623 + struct ad5398_device *coil = container_of(nb, struct ad5398_device, nb);
625 + if (event == REGULATOR_EVENT_ENABLE)
626 + ad5398_power_on(coil);
627 + else if (event == REGULATOR_EVENT_PRE_DISABLE)
628 + ad5398_power_off(coil);
633 +static int ad5398_probe(struct i2c_client *client,
634 + const struct i2c_device_id *devid)
636 + struct ad5398_device *coil;
639 + coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
643 + coil->vana = devm_regulator_get(&client->dev, "VANA");
644 + if (IS_ERR(coil->vana)) {
645 + ret = PTR_ERR(coil->vana);
646 + if (ret != -EPROBE_DEFER)
647 + dev_err(&client->dev, "could not get regulator for vana\n");
651 + v4l2_i2c_subdev_init(&coil->subdev, client, &ad5398_ops);
652 + coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
653 + coil->subdev.internal_ops = &ad5398_internal_ops;
654 + coil->subdev.entity.function = MEDIA_ENT_F_LENS;
655 + strscpy(coil->subdev.name, "ad5398 focus", sizeof(coil->subdev.name));
657 + coil->nb.notifier_call = &ad5398_regulator_notifier;
658 + ret = regulator_register_notifier(coil->vana, &coil->nb);
662 + ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
666 + ret = v4l2_async_register_subdev(&coil->subdev);
673 + media_entity_cleanup(&coil->subdev.entity);
675 + regulator_unregister_notifier(coil->vana, &coil->nb);
679 +static int ad5398_remove(struct i2c_client *client)
681 + struct v4l2_subdev *subdev = i2c_get_clientdata(client);
682 + struct ad5398_device *coil = to_ad5398_device(subdev);
684 + v4l2_async_unregister_subdev(&coil->subdev);
685 + v4l2_ctrl_handler_free(&coil->ctrls);
686 + media_entity_cleanup(&coil->subdev.entity);
690 +static const struct i2c_device_id ad5398_id_table[] = {
694 +MODULE_DEVICE_TABLE(i2c, ad5398_id_table);
696 +static const struct of_device_id ad5398_of_table[] = {
697 + { .compatible = "adi,ad5398" },
700 +MODULE_DEVICE_TABLE(of, ad5398_of_table);
702 +static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume);
704 +static struct i2c_driver ad5398_i2c_driver = {
708 + .of_match_table = ad5398_of_table,
710 + .probe = ad5398_probe,
711 + .remove = ad5398_remove,
712 + .id_table = ad5398_id_table,
715 +module_i2c_driver(ad5398_i2c_driver);
717 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
718 +MODULE_DESCRIPTION("AD5398 camera lens driver");
719 +MODULE_LICENSE("GPL");