a50617ee56b30b8e33ef8340cd5153cd24d8a299
[openwrt/staging/blocktrron.git] /
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
5
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.
9
10 Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
11 Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
12 ---
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
18
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.
24
25 +config VIDEO_BU64754
26 + tristate "BU64754 Motor Driver for Camera Autofocus"
27 + depends on I2C && VIDEO_DEV
28 + select MEDIA_CONTROLLER
29 + select VIDEO_V4L2_SUBDEV_API
30 + select V4L2_ASYNC
31 + select V4L2_CCI_I2C
32 + help
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.
37 +
38 config VIDEO_DW9714
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
51 --- /dev/null
52 +++ b/drivers/media/i2c/bu64754.c
53 @@ -0,0 +1,315 @@
54 +// SPDX-License-Identifier: GPL-2.0
55 +/*
56 + * The BU64754GWZ is an actuator driver IC which can control the
57 + * actuator position precisely using an internal Hall Sensor.
58 + */
59 +
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>
65 +
66 +#include <media/v4l2-cci.h>
67 +#include <media/v4l2-ctrls.h>
68 +#include <media/v4l2-device.h>
69 +
70 +#define BU64754_REG_ACTIVE CCI_REG16(0x07)
71 +#define BU64754_ACTIVE_MODE 0x8080
72 +
73 +#define BU64754_REG_SERVE CCI_REG16(0xd9)
74 +#define BU64754_SERVE_ON 0x0404
75 +
76 +#define BU64754_REG_POSITION CCI_REG16(0x45)
77 +#define BU64753_POSITION_MAX 1023 /* 0x3ff */
78 +#define BU64753_POSITION_STEPS 1
79 +
80 +#define BU64754_POWER_ON_DELAY 800 /* uS : t1, t3 */
81 +
82 +struct bu64754 {
83 + struct device *dev;
84 +
85 + struct v4l2_ctrl_handler ctrls_vcm;
86 + struct v4l2_subdev sd;
87 + struct regmap *cci;
88 +
89 + u16 current_val;
90 + struct regulator *vdd;
91 + struct notifier_block notifier;
92 +};
93 +
94 +static inline struct bu64754 *sd_to_bu64754(struct v4l2_subdev *subdev)
95 +{
96 + return container_of(subdev, struct bu64754, sd);
97 +}
98 +
99 +static int bu64754_set(struct bu64754 *bu64754, u16 position)
100 +{
101 + int ret;
102 +
103 + position &= 0x3ff; /* BU64753_POSITION_MAX */
104 + ret = cci_write(bu64754->cci, BU64754_REG_POSITION, position, NULL);
105 + if (ret) {
106 + dev_err(bu64754->dev, "Set position failed ret=%d\n", ret);
107 + return ret;
108 + }
109 +
110 + return 0;
111 +}
112 +
113 +static int bu64754_active(struct bu64754 *bu64754)
114 +{
115 + int ret;
116 +
117 + /* Power on */
118 + ret = cci_write(bu64754->cci, BU64754_REG_ACTIVE, BU64754_ACTIVE_MODE, NULL);
119 + if (ret < 0) {
120 + dev_err(bu64754->dev, "Failed to set active mode ret = %d\n",
121 + ret);
122 + return ret;
123 + }
124 +
125 + /* Serve on */
126 + ret = cci_write(bu64754->cci, BU64754_REG_SERVE, BU64754_SERVE_ON, NULL);
127 + if (ret < 0) {
128 + dev_err(bu64754->dev, "Failed to enable serve ret = %d\n",
129 + ret);
130 + return ret;
131 + }
132 +
133 + return bu64754_set(bu64754, bu64754->current_val);
134 +}
135 +
136 +static int bu64754_standby(struct bu64754 *bu64754)
137 +{
138 + int ret;
139 +
140 + ret = cci_write(bu64754->cci, BU64754_REG_ACTIVE, 0, NULL);
141 + if (ret < 0)
142 + dev_err(bu64754->dev, "Failed to enter standby mode ret = %d\n",
143 + ret);
144 +
145 + return ret;
146 +}
147 +
148 +static int bu64754_regulator_event(struct notifier_block *nb,
149 + unsigned long action, void *data)
150 +{
151 + struct bu64754 *bu64754 = container_of(nb, struct bu64754, notifier);
152 +
153 + if (action & REGULATOR_EVENT_ENABLE) {
154 + /*
155 + * Initialisation delay between VDD low->high and availability
156 + * i2c operation.
157 + */
158 + usleep_range(BU64754_POWER_ON_DELAY,
159 + BU64754_POWER_ON_DELAY + 100);
160 +
161 + bu64754_active(bu64754);
162 + } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
163 + bu64754_standby(bu64754);
164 + }
165 +
166 + return 0;
167 +}
168 +
169 +static int bu64754_set_ctrl(struct v4l2_ctrl *ctrl)
170 +{
171 + struct bu64754 *bu64754 = container_of(ctrl->handler,
172 + struct bu64754, ctrls_vcm);
173 +
174 + if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
175 + bu64754->current_val = ctrl->val;
176 + return bu64754_set(bu64754, ctrl->val);
177 + }
178 +
179 + return -EINVAL;
180 +}
181 +
182 +static const struct v4l2_ctrl_ops bu64754_vcm_ctrl_ops = {
183 + .s_ctrl = bu64754_set_ctrl,
184 +};
185 +
186 +static int bu64754_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
187 +{
188 + return pm_runtime_resume_and_get(sd->dev);
189 +}
190 +
191 +static int bu64754_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
192 +{
193 + pm_runtime_put(sd->dev);
194 + return 0;
195 +}
196 +
197 +static const struct v4l2_subdev_internal_ops bu64754_int_ops = {
198 + .open = bu64754_open,
199 + .close = bu64754_close,
200 +};
201 +
202 +static const struct v4l2_subdev_ops bu64754_ops = { };
203 +
204 +static void bu64754_subdev_cleanup(struct bu64754 *bu64754)
205 +{
206 + v4l2_async_unregister_subdev(&bu64754->sd);
207 + v4l2_ctrl_handler_free(&bu64754->ctrls_vcm);
208 + media_entity_cleanup(&bu64754->sd.entity);
209 +}
210 +
211 +static int bu64754_init_controls(struct bu64754 *bu64754)
212 +{
213 + struct v4l2_ctrl_handler *hdl = &bu64754->ctrls_vcm;
214 + const struct v4l2_ctrl_ops *ops = &bu64754_vcm_ctrl_ops;
215 +
216 + v4l2_ctrl_handler_init(hdl, 1);
217 +
218 + v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
219 + 0, BU64753_POSITION_MAX, BU64753_POSITION_STEPS,
220 + 0);
221 +
222 + bu64754->current_val = 0;
223 +
224 + bu64754->sd.ctrl_handler = hdl;
225 + if (hdl->error) {
226 + dev_err(bu64754->dev, "%s fail error: 0x%x\n",
227 + __func__, hdl->error);
228 + return hdl->error;
229 + }
230 +
231 + return 0;
232 +}
233 +
234 +static int bu64754_probe(struct i2c_client *client)
235 +{
236 + struct bu64754 *bu64754;
237 + int ret;
238 +
239 + bu64754 = devm_kzalloc(&client->dev, sizeof(*bu64754), GFP_KERNEL);
240 + if (!bu64754)
241 + return -ENOMEM;
242 +
243 + bu64754->dev = &client->dev;
244 +
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);
249 + }
250 +
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);
255 +
256 + bu64754->vdd = NULL;
257 + } else {
258 + bu64754->notifier.notifier_call = bu64754_regulator_event;
259 +
260 + ret = regulator_register_notifier(bu64754->vdd,
261 + &bu64754->notifier);
262 + if (ret) {
263 + dev_err(bu64754->dev,
264 + "could not register regulator notifier\n");
265 + return ret;
266 + }
267 + }
268 +
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;
273 +
274 + ret = bu64754_init_controls(bu64754);
275 + if (ret)
276 + goto err_cleanup;
277 +
278 + ret = media_entity_pads_init(&bu64754->sd.entity, 0, NULL);
279 + if (ret < 0)
280 + goto err_cleanup;
281 +
282 + bu64754->sd.entity.function = MEDIA_ENT_F_LENS;
283 +
284 + ret = v4l2_async_register_subdev(&bu64754->sd);
285 + if (ret < 0)
286 + goto err_cleanup;
287 +
288 + if (!bu64754->vdd)
289 + pm_runtime_set_active(&client->dev);
290 +
291 + pm_runtime_enable(&client->dev);
292 + pm_runtime_idle(&client->dev);
293 +
294 + return 0;
295 +
296 +err_cleanup:
297 + v4l2_ctrl_handler_free(&bu64754->ctrls_vcm);
298 + media_entity_cleanup(&bu64754->sd.entity);
299 +
300 + return ret;
301 +}
302 +
303 +static void bu64754_remove(struct i2c_client *client)
304 +{
305 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
306 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
307 +
308 + if (bu64754->vdd)
309 + regulator_unregister_notifier(bu64754->vdd,
310 + &bu64754->notifier);
311 +
312 + pm_runtime_disable(&client->dev);
313 +
314 + bu64754_subdev_cleanup(bu64754);
315 +}
316 +
317 +static int __maybe_unused bu64754_vcm_suspend(struct device *dev)
318 +{
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);
322 +
323 + if (bu64754->vdd)
324 + return regulator_disable(bu64754->vdd);
325 +
326 + return bu64754_standby(bu64754);
327 +}
328 +
329 +static int __maybe_unused bu64754_vcm_resume(struct device *dev)
330 +{
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);
334 +
335 + if (bu64754->vdd)
336 + return regulator_enable(bu64754->vdd);
337 +
338 + return bu64754_active(bu64754);
339 +}
340 +
341 +static const struct of_device_id bu64754_of_table[] = {
342 + { .compatible = "rohm,bu64754", },
343 + { /* sentinel */ }
344 +};
345 +
346 +MODULE_DEVICE_TABLE(of, bu64754_of_table);
347 +
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)
351 +};
352 +
353 +static struct i2c_driver bu64754_i2c_driver = {
354 + .driver = {
355 + .name = "bu64754",
356 + .pm = &bu64754_pm_ops,
357 + .of_match_table = bu64754_of_table,
358 + },
359 + .probe_new = bu64754_probe,
360 + .remove = bu64754_remove,
361 +};
362 +
363 +module_i2c_driver(bu64754_i2c_driver);
364 +
365 +MODULE_AUTHOR("Kieran Bingham");
366 +MODULE_DESCRIPTION("BU64754 VCM driver");
367 +MODULE_LICENSE("GPL");
368 +