1 From c1708ed0e6b86066b0b510aec54ca38687014c73 Mon Sep 17 00:00:00 2001
2 From: Kieran Bingham <kieran.bingham@ideasonboard.com>
3 Date: Wed, 13 Sep 2023 17:53:54 +0100
4 Subject: [PATCH 0769/1085] media: i2c: Add ROHM BU64754 Camera Autofocus
7 Add support for the ROHM BU64754 Motor Driver for Camera Autofocus. A
8 V4L2 Subdevice is registered and provides a single
9 V4L2_CID_FOCUS_ABSOLUTE control.
11 Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
12 Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
14 drivers/media/i2c/Kconfig | 13 ++
15 drivers/media/i2c/Makefile | 1 +
16 drivers/media/i2c/bu64754.c | 315 ++++++++++++++++++++++++++++++++++++
17 3 files changed, 329 insertions(+)
18 create mode 100644 drivers/media/i2c/bu64754.c
20 --- a/drivers/media/i2c/Kconfig
21 +++ b/drivers/media/i2c/Kconfig
22 @@ -732,6 +732,19 @@ config VIDEO_AK7375
23 capability. This is designed for linear control of
24 voice coil motors, controlled via I2C serial interface.
27 + tristate "BU64754 Motor Driver for Camera Autofocus"
28 + depends on I2C && VIDEO_DEV
29 + select MEDIA_CONTROLLER
30 + select VIDEO_V4L2_SUBDEV_API
34 + This is a driver for the BU64754 Motor Driver for Camera
35 + Autofocus. The BU64754GWZ is an actuator driver IC which
36 + can be controlled the actuator position precisely using
37 + with internal Hall Sensor.
40 tristate "DW9714 lens voice coil support"
41 depends on I2C && VIDEO_DEV
42 --- a/drivers/media/i2c/Makefile
43 +++ b/drivers/media/i2c/Makefile
44 @@ -25,6 +25,7 @@ obj-$(CONFIG_VIDEO_ARDUCAM_PIVARIETY) +=
45 obj-$(CONFIG_VIDEO_BT819) += bt819.o
46 obj-$(CONFIG_VIDEO_BT856) += bt856.o
47 obj-$(CONFIG_VIDEO_BT866) += bt866.o
48 +obj-$(CONFIG_VIDEO_BU64754) += bu64754.o
49 obj-$(CONFIG_VIDEO_CCS) += ccs/
50 obj-$(CONFIG_VIDEO_CCS_PLL) += ccs-pll.o
51 obj-$(CONFIG_VIDEO_CS3308) += cs3308.o
53 +++ b/drivers/media/i2c/bu64754.c
55 +// SPDX-License-Identifier: GPL-2.0
57 + * The BU64754GWZ is an actuator driver IC which can control the
58 + * actuator position precisely using an internal Hall Sensor.
61 +#include <linux/delay.h>
62 +#include <linux/i2c.h>
63 +#include <linux/module.h>
64 +#include <linux/pm_runtime.h>
65 +#include <linux/regulator/consumer.h>
67 +#include <media/v4l2-cci.h>
68 +#include <media/v4l2-ctrls.h>
69 +#include <media/v4l2-device.h>
71 +#define BU64754_REG_ACTIVE CCI_REG16(0x07)
72 +#define BU64754_ACTIVE_MODE 0x8080
74 +#define BU64754_REG_SERVE CCI_REG16(0xd9)
75 +#define BU64754_SERVE_ON 0x0404
77 +#define BU64754_REG_POSITION CCI_REG16(0x45)
78 +#define BU64753_POSITION_MAX 1023 /* 0x3ff */
79 +#define BU64753_POSITION_STEPS 1
81 +#define BU64754_POWER_ON_DELAY 800 /* uS : t1, t3 */
86 + struct v4l2_ctrl_handler ctrls_vcm;
87 + struct v4l2_subdev sd;
91 + struct regulator *vdd;
92 + struct notifier_block notifier;
95 +static inline struct bu64754 *sd_to_bu64754(struct v4l2_subdev *subdev)
97 + return container_of(subdev, struct bu64754, sd);
100 +static int bu64754_set(struct bu64754 *bu64754, u16 position)
104 + position &= 0x3ff; /* BU64753_POSITION_MAX */
105 + ret = cci_write(bu64754->cci, BU64754_REG_POSITION, position, NULL);
107 + dev_err(bu64754->dev, "Set position failed ret=%d\n", ret);
114 +static int bu64754_active(struct bu64754 *bu64754)
119 + ret = cci_write(bu64754->cci, BU64754_REG_ACTIVE, BU64754_ACTIVE_MODE, NULL);
121 + dev_err(bu64754->dev, "Failed to set active mode ret = %d\n",
127 + ret = cci_write(bu64754->cci, BU64754_REG_SERVE, BU64754_SERVE_ON, NULL);
129 + dev_err(bu64754->dev, "Failed to enable serve ret = %d\n",
134 + return bu64754_set(bu64754, bu64754->current_val);
137 +static int bu64754_standby(struct bu64754 *bu64754)
141 + ret = cci_write(bu64754->cci, BU64754_REG_ACTIVE, 0, NULL);
143 + dev_err(bu64754->dev, "Failed to enter standby mode ret = %d\n",
149 +static int bu64754_regulator_event(struct notifier_block *nb,
150 + unsigned long action, void *data)
152 + struct bu64754 *bu64754 = container_of(nb, struct bu64754, notifier);
154 + if (action & REGULATOR_EVENT_ENABLE) {
156 + * Initialisation delay between VDD low->high and availability
159 + usleep_range(BU64754_POWER_ON_DELAY,
160 + BU64754_POWER_ON_DELAY + 100);
162 + bu64754_active(bu64754);
163 + } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
164 + bu64754_standby(bu64754);
170 +static int bu64754_set_ctrl(struct v4l2_ctrl *ctrl)
172 + struct bu64754 *bu64754 = container_of(ctrl->handler,
173 + struct bu64754, ctrls_vcm);
175 + if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
176 + bu64754->current_val = ctrl->val;
177 + return bu64754_set(bu64754, ctrl->val);
183 +static const struct v4l2_ctrl_ops bu64754_vcm_ctrl_ops = {
184 + .s_ctrl = bu64754_set_ctrl,
187 +static int bu64754_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
189 + return pm_runtime_resume_and_get(sd->dev);
192 +static int bu64754_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
194 + pm_runtime_put(sd->dev);
198 +static const struct v4l2_subdev_internal_ops bu64754_int_ops = {
199 + .open = bu64754_open,
200 + .close = bu64754_close,
203 +static const struct v4l2_subdev_ops bu64754_ops = { };
205 +static void bu64754_subdev_cleanup(struct bu64754 *bu64754)
207 + v4l2_async_unregister_subdev(&bu64754->sd);
208 + v4l2_ctrl_handler_free(&bu64754->ctrls_vcm);
209 + media_entity_cleanup(&bu64754->sd.entity);
212 +static int bu64754_init_controls(struct bu64754 *bu64754)
214 + struct v4l2_ctrl_handler *hdl = &bu64754->ctrls_vcm;
215 + const struct v4l2_ctrl_ops *ops = &bu64754_vcm_ctrl_ops;
217 + v4l2_ctrl_handler_init(hdl, 1);
219 + v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
220 + 0, BU64753_POSITION_MAX, BU64753_POSITION_STEPS,
223 + bu64754->current_val = 0;
225 + bu64754->sd.ctrl_handler = hdl;
227 + dev_err(bu64754->dev, "%s fail error: 0x%x\n",
228 + __func__, hdl->error);
235 +static int bu64754_probe(struct i2c_client *client)
237 + struct bu64754 *bu64754;
240 + bu64754 = devm_kzalloc(&client->dev, sizeof(*bu64754), GFP_KERNEL);
244 + bu64754->dev = &client->dev;
246 + bu64754->cci = devm_cci_regmap_init_i2c(client, 8);
247 + if (IS_ERR(bu64754->cci)) {
248 + dev_err(bu64754->dev, "Failed to initialize CCI\n");
249 + return PTR_ERR(bu64754->cci);
252 + bu64754->vdd = devm_regulator_get_optional(&client->dev, "vdd");
253 + if (IS_ERR(bu64754->vdd)) {
254 + if (PTR_ERR(bu64754->vdd) != -ENODEV)
255 + return PTR_ERR(bu64754->vdd);
257 + bu64754->vdd = NULL;
259 + bu64754->notifier.notifier_call = bu64754_regulator_event;
261 + ret = regulator_register_notifier(bu64754->vdd,
262 + &bu64754->notifier);
264 + dev_err(bu64754->dev,
265 + "could not register regulator notifier\n");
270 + v4l2_i2c_subdev_init(&bu64754->sd, client, &bu64754_ops);
271 + bu64754->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
272 + bu64754->sd.internal_ops = &bu64754_int_ops;
273 + bu64754->sd.entity.function = MEDIA_ENT_F_LENS;
275 + ret = bu64754_init_controls(bu64754);
279 + ret = media_entity_pads_init(&bu64754->sd.entity, 0, NULL);
283 + bu64754->sd.entity.function = MEDIA_ENT_F_LENS;
285 + ret = v4l2_async_register_subdev(&bu64754->sd);
290 + pm_runtime_set_active(&client->dev);
292 + pm_runtime_enable(&client->dev);
293 + pm_runtime_idle(&client->dev);
298 + v4l2_ctrl_handler_free(&bu64754->ctrls_vcm);
299 + media_entity_cleanup(&bu64754->sd.entity);
304 +static void bu64754_remove(struct i2c_client *client)
306 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
307 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
310 + regulator_unregister_notifier(bu64754->vdd,
311 + &bu64754->notifier);
313 + pm_runtime_disable(&client->dev);
315 + bu64754_subdev_cleanup(bu64754);
318 +static int __maybe_unused bu64754_vcm_suspend(struct device *dev)
320 + struct i2c_client *client = to_i2c_client(dev);
321 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
322 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
325 + return regulator_disable(bu64754->vdd);
327 + return bu64754_standby(bu64754);
330 +static int __maybe_unused bu64754_vcm_resume(struct device *dev)
332 + struct i2c_client *client = to_i2c_client(dev);
333 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
334 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
337 + return regulator_enable(bu64754->vdd);
339 + return bu64754_active(bu64754);
342 +static const struct of_device_id bu64754_of_table[] = {
343 + { .compatible = "rohm,bu64754", },
347 +MODULE_DEVICE_TABLE(of, bu64754_of_table);
349 +static const struct dev_pm_ops bu64754_pm_ops = {
350 + SET_SYSTEM_SLEEP_PM_OPS(bu64754_vcm_suspend, bu64754_vcm_resume)
351 + SET_RUNTIME_PM_OPS(bu64754_vcm_suspend, bu64754_vcm_resume, NULL)
354 +static struct i2c_driver bu64754_i2c_driver = {
357 + .pm = &bu64754_pm_ops,
358 + .of_match_table = bu64754_of_table,
360 + .probe = bu64754_probe,
361 + .remove = bu64754_remove,
364 +module_i2c_driver(bu64754_i2c_driver);
366 +MODULE_AUTHOR("Kieran Bingham");
367 +MODULE_DESCRIPTION("BU64754 VCM driver");
368 +MODULE_LICENSE("GPL");