1 From 97ec6aeb265df0bfe7193f00c249b38873fb0fb7 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] media: i2c: Add ROHM BU64754 Camera Autofocus Actuator
6 Add support for the ROHM BU64754 Motor Driver for Camera Autofocus. A
7 V4L2 Subdevice is registered and provides a single
8 V4L2_CID_FOCUS_ABSOLUTE control.
10 Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
11 Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
13 drivers/media/i2c/Kconfig | 13 ++
14 drivers/media/i2c/Makefile | 1 +
15 drivers/media/i2c/bu64754.c | 315 ++++++++++++++++++++++++++++++++++++
16 3 files changed, 329 insertions(+)
17 create mode 100644 drivers/media/i2c/bu64754.c
19 --- a/drivers/media/i2c/Kconfig
20 +++ b/drivers/media/i2c/Kconfig
21 @@ -917,6 +917,19 @@ config VIDEO_AK7375
22 capability. This is designed for linear control of
23 voice coil motors, controlled via I2C serial interface.
26 + tristate "BU64754 Motor Driver for Camera Autofocus"
27 + depends on I2C && VIDEO_DEV
28 + select MEDIA_CONTROLLER
29 + select VIDEO_V4L2_SUBDEV_API
33 + This is a driver for the BU64754 Motor Driver for Camera
34 + Autofocus. The BU64754GWZ is an actuator driver IC which
35 + can be controlled the actuator position precisely using
36 + with internal Hall Sensor.
39 tristate "DW9714 lens voice coil support"
40 depends on I2C && VIDEO_DEV
41 --- a/drivers/media/i2c/Makefile
42 +++ b/drivers/media/i2c/Makefile
43 @@ -26,6 +26,7 @@ obj-$(CONFIG_VIDEO_ARDUCAM_PIVARIETY) +=
44 obj-$(CONFIG_VIDEO_BT819) += bt819.o
45 obj-$(CONFIG_VIDEO_BT856) += bt856.o
46 obj-$(CONFIG_VIDEO_BT866) += bt866.o
47 +obj-$(CONFIG_VIDEO_BU64754) += bu64754.o
48 obj-$(CONFIG_VIDEO_CCS) += ccs/
49 obj-$(CONFIG_VIDEO_CCS_PLL) += ccs-pll.o
50 obj-$(CONFIG_VIDEO_CS3308) += cs3308.o
52 +++ b/drivers/media/i2c/bu64754.c
54 +// SPDX-License-Identifier: GPL-2.0
56 + * The BU64754GWZ is an actuator driver IC which can control the
57 + * actuator position precisely using an internal Hall Sensor.
60 +#include <linux/delay.h>
61 +#include <linux/i2c.h>
62 +#include <linux/module.h>
63 +#include <linux/pm_runtime.h>
64 +#include <linux/regulator/consumer.h>
66 +#include <media/v4l2-cci.h>
67 +#include <media/v4l2-ctrls.h>
68 +#include <media/v4l2-device.h>
70 +#define BU64754_REG_ACTIVE CCI_REG16(0x07)
71 +#define BU64754_ACTIVE_MODE 0x8080
73 +#define BU64754_REG_SERVE CCI_REG16(0xd9)
74 +#define BU64754_SERVE_ON 0x0404
76 +#define BU64754_REG_POSITION CCI_REG16(0x45)
77 +#define BU64753_POSITION_MAX 1023 /* 0x3ff */
78 +#define BU64753_POSITION_STEPS 1
80 +#define BU64754_POWER_ON_DELAY 800 /* uS : t1, t3 */
85 + struct v4l2_ctrl_handler ctrls_vcm;
86 + struct v4l2_subdev sd;
90 + struct regulator *vdd;
91 + struct notifier_block notifier;
94 +static inline struct bu64754 *sd_to_bu64754(struct v4l2_subdev *subdev)
96 + return container_of(subdev, struct bu64754, sd);
99 +static int bu64754_set(struct bu64754 *bu64754, u16 position)
103 + position &= 0x3ff; /* BU64753_POSITION_MAX */
104 + ret = cci_write(bu64754->cci, BU64754_REG_POSITION, position, NULL);
106 + dev_err(bu64754->dev, "Set position failed ret=%d\n", ret);
113 +static int bu64754_active(struct bu64754 *bu64754)
118 + ret = cci_write(bu64754->cci, BU64754_REG_ACTIVE, BU64754_ACTIVE_MODE, NULL);
120 + dev_err(bu64754->dev, "Failed to set active mode ret = %d\n",
126 + ret = cci_write(bu64754->cci, BU64754_REG_SERVE, BU64754_SERVE_ON, NULL);
128 + dev_err(bu64754->dev, "Failed to enable serve ret = %d\n",
133 + return bu64754_set(bu64754, bu64754->current_val);
136 +static int bu64754_standby(struct bu64754 *bu64754)
140 + ret = cci_write(bu64754->cci, BU64754_REG_ACTIVE, 0, NULL);
142 + dev_err(bu64754->dev, "Failed to enter standby mode ret = %d\n",
148 +static int bu64754_regulator_event(struct notifier_block *nb,
149 + unsigned long action, void *data)
151 + struct bu64754 *bu64754 = container_of(nb, struct bu64754, notifier);
153 + if (action & REGULATOR_EVENT_ENABLE) {
155 + * Initialisation delay between VDD low->high and availability
158 + usleep_range(BU64754_POWER_ON_DELAY,
159 + BU64754_POWER_ON_DELAY + 100);
161 + bu64754_active(bu64754);
162 + } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
163 + bu64754_standby(bu64754);
169 +static int bu64754_set_ctrl(struct v4l2_ctrl *ctrl)
171 + struct bu64754 *bu64754 = container_of(ctrl->handler,
172 + struct bu64754, ctrls_vcm);
174 + if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
175 + bu64754->current_val = ctrl->val;
176 + return bu64754_set(bu64754, ctrl->val);
182 +static const struct v4l2_ctrl_ops bu64754_vcm_ctrl_ops = {
183 + .s_ctrl = bu64754_set_ctrl,
186 +static int bu64754_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
188 + return pm_runtime_resume_and_get(sd->dev);
191 +static int bu64754_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
193 + pm_runtime_put(sd->dev);
197 +static const struct v4l2_subdev_internal_ops bu64754_int_ops = {
198 + .open = bu64754_open,
199 + .close = bu64754_close,
202 +static const struct v4l2_subdev_ops bu64754_ops = { };
204 +static void bu64754_subdev_cleanup(struct bu64754 *bu64754)
206 + v4l2_async_unregister_subdev(&bu64754->sd);
207 + v4l2_ctrl_handler_free(&bu64754->ctrls_vcm);
208 + media_entity_cleanup(&bu64754->sd.entity);
211 +static int bu64754_init_controls(struct bu64754 *bu64754)
213 + struct v4l2_ctrl_handler *hdl = &bu64754->ctrls_vcm;
214 + const struct v4l2_ctrl_ops *ops = &bu64754_vcm_ctrl_ops;
216 + v4l2_ctrl_handler_init(hdl, 1);
218 + v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
219 + 0, BU64753_POSITION_MAX, BU64753_POSITION_STEPS,
222 + bu64754->current_val = 0;
224 + bu64754->sd.ctrl_handler = hdl;
226 + dev_err(bu64754->dev, "%s fail error: 0x%x\n",
227 + __func__, hdl->error);
234 +static int bu64754_probe(struct i2c_client *client)
236 + struct bu64754 *bu64754;
239 + bu64754 = devm_kzalloc(&client->dev, sizeof(*bu64754), GFP_KERNEL);
243 + bu64754->dev = &client->dev;
245 + bu64754->cci = devm_cci_regmap_init_i2c(client, 8);
246 + if (IS_ERR(bu64754->cci)) {
247 + dev_err(bu64754->dev, "Failed to initialize CCI\n");
248 + return PTR_ERR(bu64754->cci);
251 + bu64754->vdd = devm_regulator_get_optional(&client->dev, "vdd");
252 + if (IS_ERR(bu64754->vdd)) {
253 + if (PTR_ERR(bu64754->vdd) != -ENODEV)
254 + return PTR_ERR(bu64754->vdd);
256 + bu64754->vdd = NULL;
258 + bu64754->notifier.notifier_call = bu64754_regulator_event;
260 + ret = regulator_register_notifier(bu64754->vdd,
261 + &bu64754->notifier);
263 + dev_err(bu64754->dev,
264 + "could not register regulator notifier\n");
269 + v4l2_i2c_subdev_init(&bu64754->sd, client, &bu64754_ops);
270 + bu64754->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
271 + bu64754->sd.internal_ops = &bu64754_int_ops;
272 + bu64754->sd.entity.function = MEDIA_ENT_F_LENS;
274 + ret = bu64754_init_controls(bu64754);
278 + ret = media_entity_pads_init(&bu64754->sd.entity, 0, NULL);
282 + bu64754->sd.entity.function = MEDIA_ENT_F_LENS;
284 + ret = v4l2_async_register_subdev(&bu64754->sd);
289 + pm_runtime_set_active(&client->dev);
291 + pm_runtime_enable(&client->dev);
292 + pm_runtime_idle(&client->dev);
297 + v4l2_ctrl_handler_free(&bu64754->ctrls_vcm);
298 + media_entity_cleanup(&bu64754->sd.entity);
303 +static void bu64754_remove(struct i2c_client *client)
305 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
306 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
309 + regulator_unregister_notifier(bu64754->vdd,
310 + &bu64754->notifier);
312 + pm_runtime_disable(&client->dev);
314 + bu64754_subdev_cleanup(bu64754);
317 +static int __maybe_unused bu64754_vcm_suspend(struct device *dev)
319 + struct i2c_client *client = to_i2c_client(dev);
320 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
321 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
324 + return regulator_disable(bu64754->vdd);
326 + return bu64754_standby(bu64754);
329 +static int __maybe_unused bu64754_vcm_resume(struct device *dev)
331 + struct i2c_client *client = to_i2c_client(dev);
332 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
333 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
336 + return regulator_enable(bu64754->vdd);
338 + return bu64754_active(bu64754);
341 +static const struct of_device_id bu64754_of_table[] = {
342 + { .compatible = "rohm,bu64754", },
346 +MODULE_DEVICE_TABLE(of, bu64754_of_table);
348 +static const struct dev_pm_ops bu64754_pm_ops = {
349 + SET_SYSTEM_SLEEP_PM_OPS(bu64754_vcm_suspend, bu64754_vcm_resume)
350 + SET_RUNTIME_PM_OPS(bu64754_vcm_suspend, bu64754_vcm_resume, NULL)
353 +static struct i2c_driver bu64754_i2c_driver = {
356 + .pm = &bu64754_pm_ops,
357 + .of_match_table = bu64754_of_table,
359 + .probe_new = bu64754_probe,
360 + .remove = bu64754_remove,
363 +module_i2c_driver(bu64754_i2c_driver);
365 +MODULE_AUTHOR("Kieran Bingham");
366 +MODULE_DESCRIPTION("BU64754 VCM driver");
367 +MODULE_LICENSE("GPL");