From b8e28f2bc37fdb013677a1ea3456298636fd146f Mon Sep 17 00:00:00 2001 From: "Raziel K. Crowe" <84860158+CWDSYSTEMS@users.noreply.github.com> Date: Tue, 22 Mar 2022 19:02:56 +0500 Subject: [PATCH] These ain't insults, they are descriptions of that T3Q hoe. --- drivers/atm/firestream.c | 2 + drivers/media/i2c/Kconfig | 7 + drivers/media/i2c/Makefile | 1 + drivers/media/i2c/ad5398_vcm.c | 342 +++++++++++++++++++++++++++ drivers/media/i2c/ov5647.c | 2 +- drivers/media/mc/mc-entity.c | 46 +++- drivers/media/v4l2-core/v4l2-async.c | 31 +++ drivers/scsi/sd.c | 1 + drivers/scsi/sg.c | 1 + drivers/scsi/sr.c | 1 + drivers/scsi/st.c | 1 + 11 files changed, 431 insertions(+), 4 deletions(-) create mode 100644 drivers/media/i2c/ad5398_vcm.c diff --git a/drivers/atm/firestream.c b/drivers/atm/firestream.c index 3bc3c314a4..4f67404fe6 100644 --- a/drivers/atm/firestream.c +++ b/drivers/atm/firestream.c @@ -1676,6 +1676,8 @@ static int fs_init(struct fs_dev *dev) dev->hw_base = pci_resource_start(pci_dev, 0); dev->base = ioremap(dev->hw_base, 0x1000); + if (!dev->base) + return 1; reset_chip (dev); diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index 7638dad704..ef176eea2c 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -1451,6 +1451,13 @@ endmenu menu "Lens drivers" visible if MEDIA_CAMERA_SUPPORT +config VIDEO_AD5398 + tristate "AD5398 lens voice coil support" + depends on GPIOLIB && I2C && VIDEO_V4L2 + select MEDIA_CONTROLLER + help + This is a driver for the AD5398 camera lens voice coil. + config VIDEO_AD5820 tristate "AD5820 lens voice coil support" depends on GPIOLIB && I2C && VIDEO_V4L2 diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index daed83f591..8c26efffdc 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -21,6 +21,7 @@ obj-$(CONFIG_VIDEO_SAA717X) += saa717x.o obj-$(CONFIG_VIDEO_SAA7127) += saa7127.o obj-$(CONFIG_VIDEO_SAA7185) += saa7185.o obj-$(CONFIG_VIDEO_SAA6752HS) += saa6752hs.o +obj-$(CONFIG_VIDEO_AD5398) += ad5398_vcm.o obj-$(CONFIG_VIDEO_AD5820) += ad5820.o obj-$(CONFIG_VIDEO_AK7375) += ak7375.o obj-$(CONFIG_VIDEO_DW9714) += dw9714.o diff --git a/drivers/media/i2c/ad5398_vcm.c b/drivers/media/i2c/ad5398_vcm.c new file mode 100644 index 0000000000..102c3143f7 --- /dev/null +++ b/drivers/media/i2c/ad5398_vcm.c @@ -0,0 +1,342 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * AD5398 DAC driver for camera voice coil focus. + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd. + * + * Based on AD5820 DAC driver by Nokia and TI. + * + * This driver uses the regulator framework notification hooks on the + * assumption that the VCM and sensor share a regulator. This means the VCM + * position will be restored when either the sensor or VCM subdevices are opened + * or powered up. The client can therefore choose to ignore the VCM subdevice, + * and the lens position will be as previously requested. Without that, there + * is a hard requirement to have the VCM subdevice open in order for the VCM + * to be powered and at the requested position. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* Register definitions */ +#define AD5398_POWER_DOWN BIT(15) +#define AD5398_DAC_SHIFT 4 + +#define to_ad5398_device(sd) container_of(sd, struct ad5398_device, subdev) + +struct ad5398_device { + struct v4l2_subdev subdev; + struct ad5398_platform_data *platform_data; + struct regulator *vana; + struct notifier_block nb; + + struct v4l2_ctrl_handler ctrls; + u32 focus_absolute; + + bool standby; +}; + +static int ad5398_write(struct ad5398_device *coil, u16 data) +{ + struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev); + struct i2c_msg msg; + __be16 be_data; + int r; + + if (!client->adapter) + return -ENODEV; + + be_data = cpu_to_be16(data); + msg.addr = client->addr; + msg.flags = 0; + msg.len = 2; + msg.buf = (u8 *)&be_data; + + r = i2c_transfer(client->adapter, &msg, 1); + if (r < 0) { + dev_err(&client->dev, "write failed, error %d\n", r); + return r; + } + + return 0; +} + +/* + * Calculate status word and write it to the device based on current + * values of V4L2 controls. It is assumed that the stored V4L2 control + * values are properly limited and rounded. + */ +static int ad5398_update_hw(struct ad5398_device *coil) +{ + u16 status; + + status = coil->focus_absolute << AD5398_DAC_SHIFT; + + if (coil->standby) + status |= AD5398_POWER_DOWN; + + return ad5398_write(coil, status); +} + +/* + * Power handling + */ +static int ad5398_power_off(struct ad5398_device *coil) +{ + int ret = 0; + + coil->standby = true; + ret = ad5398_update_hw(coil); + + return ret; +} + +static int ad5398_power_on(struct ad5398_device *coil) +{ + int ret; + + /* Restore the hardware settings. */ + coil->standby = false; + ret = ad5398_update_hw(coil); + if (ret) + goto fail; + + return 0; + +fail: + coil->standby = true; + + return ret; +} + +/* + * V4L2 controls + */ +static int ad5398_set_ctrl(struct v4l2_ctrl *ctrl) +{ + struct ad5398_device *coil = + container_of(ctrl->handler, struct ad5398_device, ctrls); + + switch (ctrl->id) { + case V4L2_CID_FOCUS_ABSOLUTE: + coil->focus_absolute = ctrl->val; + return ad5398_update_hw(coil); + } + + return 0; +} + +static const struct v4l2_ctrl_ops ad5398_ctrl_ops = { + .s_ctrl = ad5398_set_ctrl, +}; + +static int ad5398_init_controls(struct ad5398_device *coil) +{ + v4l2_ctrl_handler_init(&coil->ctrls, 1); + + /* + * V4L2_CID_FOCUS_ABSOLUTE + * + * Minimum current is 0 mA, maximum is 120 mA. Thus, 1 code is + * equivalent to 120/1023 = 0.1173 mA. Nevertheless, we do not use [mA] + * for focus position, because it is meaningless for user. Meaningful + * would be to use focus distance or even its inverse, but since the + * driver doesn't have sufficient knowledge to do the conversion, we + * will just use abstract codes here. In any case, smaller value = focus + * position farther from camera. The default zero value means focus at + * infinity, and also least current consumption. + */ + v4l2_ctrl_new_std(&coil->ctrls, &ad5398_ctrl_ops, + V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0); + + if (coil->ctrls.error) + return coil->ctrls.error; + + coil->focus_absolute = 0; + + coil->subdev.ctrl_handler = &coil->ctrls; + + return 0; +} + +/* + * V4L2 subdev operations + */ +static int ad5398_registered(struct v4l2_subdev *subdev) +{ + struct ad5398_device *coil = to_ad5398_device(subdev); + + return ad5398_init_controls(coil); +} + +static int +ad5398_set_power(struct v4l2_subdev *subdev, int on) +{ + struct ad5398_device *coil = to_ad5398_device(subdev); + int ret; + + if (on) + ret = regulator_enable(coil->vana); + else + ret = regulator_disable(coil->vana); + + return ret; +} + +static int ad5398_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) +{ + struct ad5398_device *coil = to_ad5398_device(sd); + + return regulator_enable(coil->vana); +} + +static int ad5398_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) +{ + struct ad5398_device *coil = to_ad5398_device(sd); + + return regulator_disable(coil->vana); +} + +static const struct v4l2_subdev_core_ops ad5398_core_ops = { + .s_power = ad5398_set_power, +}; + +static const struct v4l2_subdev_ops ad5398_ops = { + .core = &ad5398_core_ops, +}; + +static const struct v4l2_subdev_internal_ops ad5398_internal_ops = { + .registered = ad5398_registered, + .open = ad5398_open, + .close = ad5398_close, +}; + +/* + * I2C driver + */ +static int __maybe_unused ad5398_suspend(struct device *dev) +{ + struct i2c_client *client = container_of(dev, struct i2c_client, dev); + struct v4l2_subdev *subdev = i2c_get_clientdata(client); + struct ad5398_device *coil = to_ad5398_device(subdev); + + return regulator_enable(coil->vana); +} + +static int __maybe_unused ad5398_resume(struct device *dev) +{ + struct i2c_client *client = container_of(dev, struct i2c_client, dev); + struct v4l2_subdev *subdev = i2c_get_clientdata(client); + struct ad5398_device *coil = to_ad5398_device(subdev); + + return regulator_disable(coil->vana); +} + +static int ad5398_regulator_notifier(struct notifier_block *nb, + unsigned long event, + void *ignored) +{ + struct ad5398_device *coil = container_of(nb, struct ad5398_device, nb); + + if (event == REGULATOR_EVENT_ENABLE) + ad5398_power_on(coil); + else if (event == REGULATOR_EVENT_PRE_DISABLE) + ad5398_power_off(coil); + + return NOTIFY_OK; +} + +static int ad5398_probe(struct i2c_client *client, + const struct i2c_device_id *devid) +{ + struct ad5398_device *coil; + int ret; + + coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL); + if (!coil) + return -ENOMEM; + + coil->vana = devm_regulator_get(&client->dev, "VANA"); + if (IS_ERR(coil->vana)) { + ret = PTR_ERR(coil->vana); + if (ret != -EPROBE_DEFER) + dev_err(&client->dev, "could not get regulator for vana\n"); + return ret; + } + + v4l2_i2c_subdev_init(&coil->subdev, client, &ad5398_ops); + coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; + coil->subdev.internal_ops = &ad5398_internal_ops; + coil->subdev.entity.function = MEDIA_ENT_F_LENS; + strscpy(coil->subdev.name, "ad5398 focus", sizeof(coil->subdev.name)); + + coil->nb.notifier_call = &ad5398_regulator_notifier; + ret = regulator_register_notifier(coil->vana, &coil->nb); + if (ret < 0) + return ret; + + ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL); + if (ret < 0) + goto cleanup2; + + ret = v4l2_async_register_subdev(&coil->subdev); + if (ret < 0) + goto cleanup; + + return ret; + +cleanup: + media_entity_cleanup(&coil->subdev.entity); +cleanup2: + regulator_unregister_notifier(coil->vana, &coil->nb); + return ret; +} + +static int ad5398_remove(struct i2c_client *client) +{ + struct v4l2_subdev *subdev = i2c_get_clientdata(client); + struct ad5398_device *coil = to_ad5398_device(subdev); + + v4l2_async_unregister_subdev(&coil->subdev); + v4l2_ctrl_handler_free(&coil->ctrls); + media_entity_cleanup(&coil->subdev.entity); + return 0; +} + +static const struct i2c_device_id ad5398_id_table[] = { + { "ad5398", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, ad5398_id_table); + +static const struct of_device_id ad5398_of_table[] = { + { .compatible = "adi,ad5398" }, + { } +}; +MODULE_DEVICE_TABLE(of, ad5398_of_table); + +static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume); + +static struct i2c_driver ad5398_i2c_driver = { + .driver = { + .name = "ad5398", + .pm = &ad5398_pm, + .of_match_table = ad5398_of_table, + }, + .probe = ad5398_probe, + .remove = ad5398_remove, + .id_table = ad5398_id_table, +}; + +module_i2c_driver(ad5398_i2c_driver); + +MODULE_AUTHOR("Dave Stevenson "); +MODULE_DESCRIPTION("AD5398 camera lens driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/i2c/ov5647.c b/drivers/media/i2c/ov5647.c index 491246786f..befdb53bda 100644 --- a/drivers/media/i2c/ov5647.c +++ b/drivers/media/i2c/ov5647.c @@ -1558,7 +1558,7 @@ static int ov5647_probe(struct i2c_client *client) if (ret < 0) goto power_off; - ret = v4l2_async_register_subdev(sd); + ret = v4l2_async_register_subdev_sensor(sd); if (ret < 0) goto power_off; diff --git a/drivers/media/mc/mc-entity.c b/drivers/media/mc/mc-entity.c index f40f419771..7bf2c73a38 100644 --- a/drivers/media/mc/mc-entity.c +++ b/drivers/media/mc/mc-entity.c @@ -60,6 +60,20 @@ static inline const char *intf_type(struct media_interface *intf) } }; +static inline const char *link_type_name(struct media_link *link) +{ + switch (link->flags & MEDIA_LNK_FL_LINK_TYPE) { + case MEDIA_LNK_FL_DATA_LINK: + return "data"; + case MEDIA_LNK_FL_INTERFACE_LINK: + return "interface"; + case MEDIA_LNK_FL_ANCILLARY_LINK: + return "ancillary"; + default: + return "unknown"; + } +} + __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum, int idx_max) { @@ -107,9 +121,7 @@ static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj) dev_dbg(gobj->mdev->dev, "%s id %u: %s link id %u ==> id %u\n", - event_name, media_id(gobj), - media_type(link->gobj0) == MEDIA_GRAPH_PAD ? - "data" : "interface", + event_name, media_id(gobj), link_type_name(link), media_id(link->gobj0), media_id(link->gobj1)); break; @@ -313,6 +325,12 @@ static void media_graph_walk_iter(struct media_graph *graph) link = list_entry(link_top(graph), typeof(*link), list); + /* If the link is not a data link, don't follow it */ + if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) { + link_top(graph) = link_top(graph)->next; + return; + } + /* The link is not enabled so we do not follow. */ if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { link_top(graph) = link_top(graph)->next; @@ -1032,3 +1050,25 @@ void media_remove_intf_links(struct media_interface *intf) mutex_unlock(&mdev->graph_mutex); } EXPORT_SYMBOL_GPL(media_remove_intf_links); + +struct media_link *media_create_ancillary_link(struct media_entity *primary, + struct media_entity *ancillary) +{ + struct media_link *link; + + link = media_add_link(&primary->links); + if (!link) + return ERR_PTR(-ENOMEM); + + link->gobj0 = &primary->graph_obj; + link->gobj1 = &ancillary->graph_obj; + link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED | + MEDIA_LNK_FL_ANCILLARY_LINK; + + /* Initialize graph object embedded in the new link */ + media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK, + &link->graph_obj); + + return link; +} +EXPORT_SYMBOL_GPL(media_create_ancillary_link); diff --git a/drivers/media/v4l2-core/v4l2-async.c b/drivers/media/v4l2-core/v4l2-async.c index cd9e78c637..84a12788d6 100644 --- a/drivers/media/v4l2-core/v4l2-async.c +++ b/drivers/media/v4l2-core/v4l2-async.c @@ -275,6 +275,24 @@ v4l2_async_notifier_try_complete(struct v4l2_async_notifier *notifier) static int v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier *notifier); +static int v4l2_async_create_ancillary_links(struct v4l2_async_notifier *n, + struct v4l2_subdev *sd) +{ + struct media_link *link = NULL; + +#if IS_ENABLED(CONFIG_MEDIA_CONTROLLER) + + if (sd->entity.function != MEDIA_ENT_F_LENS && + sd->entity.function != MEDIA_ENT_F_FLASH) + return 0; + + link = media_create_ancillary_link(&n->sd->entity, &sd->entity); + +#endif + + return IS_ERR(link) ? PTR_ERR(link) : 0; +} + static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier, struct v4l2_device *v4l2_dev, struct v4l2_subdev *sd, @@ -293,6 +311,19 @@ static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier, return ret; } + /* + * Depending of the function of the entities involved, we may want to + * create links between them (for example between a sensor and its lens + * or between a sensor's source pad and the connected device's sink + * pad). + */ + ret = v4l2_async_create_ancillary_links(notifier, sd); + if (ret) { + v4l2_async_notifier_call_unbind(notifier, sd, asd); + v4l2_device_unregister_subdev(sd); + return ret; + } + /* Remove from the waiting list */ list_del(&asd->list); sd->asd = asd; diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 78ead33697..564a21b5da 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c index 8f05248920..3c98f08dc2 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -31,6 +31,7 @@ static int sg_version_num = 30536; /* 2 digits for each component */ #include #include #include +#include #include #include #include diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c index 1203374828..973d6e079b 100644 --- a/drivers/scsi/sr.c +++ b/drivers/scsi/sr.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include diff --git a/drivers/scsi/st.c b/drivers/scsi/st.c index ae8636d378..9933722acf 100644 --- a/drivers/scsi/st.c +++ b/drivers/scsi/st.c @@ -32,6 +32,7 @@ static const char *verstr = "20160209"; #include #include #include +#include #include #include #include