diff --git a/Documentation/devicetree/bindings/media/i2c/onnn,lc898129.yaml b/Documentation/devicetree/bindings/media/i2c/onnn,lc898129.yaml new file mode 100644 index 0000000000..d2f0d719ac --- /dev/null +++ b/Documentation/devicetree/bindings/media/i2c/onnn,lc898129.yaml @@ -0,0 +1,54 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/i2c/onnn,lc898129.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: onsemi LC898129 camera lens autofocus and stabilisation controller + +maintainers: + - cbm + +description: + The onsemi LC898129 is a microcontroller that drives a camera module's + focus and optical image stabilisation actuators and holds the module's + calibration memory, controlled over I2C with 16-bit register addresses + and 32-bit values. It shares the module's supplies and reset with the + image sensor, which powers it while streaming (the sensor's lens-focus + property). It is the controller of the Google Pixel 9a's main camera + module. + +properties: + compatible: + const: onnn,lc898129 + + reg: + maxItems: 1 + + vio-supply: + description: Interface (1.8 V) supply. + + vm-supply: + description: Actuator driver supply (VM). + + vdda-supply: + description: Actuator driver analogue supply (VDDA). + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + lens@24 { + compatible = "onnn,lc898129"; + reg = <0x24>; + vio-supply = <&vddio_cam>; + }; + }; diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index 06e8614c55..edef8fa2b2 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -983,6 +983,17 @@ config VIDEO_SEM1215 To compile this driver as a module, choose M here: the module will be called sem1215. +config VIDEO_LC898129 + tristate "onsemi LC898129 lens focus support" + select V4L2_CCI_I2C + help + This is a driver for the focus part of the onsemi LC898129 + autofocus and image stabilisation controller, as in the Google + Pixel 9a's main camera. + + To compile this driver as a module, choose M here: the + module will be called lc898129. + endif menu "Flash devices" diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index de9058fc34..58200fd62b 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -67,6 +67,7 @@ obj-$(CONFIG_VIDEO_IMX471) += imx471.o obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o obj-$(CONFIG_VIDEO_KS0127) += ks0127.o +obj-$(CONFIG_VIDEO_LC898129) += lc898129.o obj-$(CONFIG_VIDEO_LM3560) += lm3560.o obj-$(CONFIG_VIDEO_LM3646) += lm3646.o obj-$(CONFIG_VIDEO_LT6911UXE) += lt6911uxe.o diff --git a/drivers/media/i2c/lc898129.c b/drivers/media/i2c/lc898129.c new file mode 100644 index 0000000000..50cce3a5f4 --- /dev/null +++ b/drivers/media/i2c/lc898129.c @@ -0,0 +1,320 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * onsemi LC898129 lens driver (focus only) + * + * The LC898129 is onsemi's combined autofocus and optical image + * stabilisation controller, a small microcontroller with the module's + * calibration memory behind it; the Google Pixel 9a's main camera module + * uses one. It has 16-bit register addresses and 32-bit big-endian values. + * It shares the module's supplies and reset with the image sensor, so it + * only answers while the sensor is powered: the sensor's driver takes this + * device's runtime PM while it streams (lens-focus), and opening the subdev + * doesn't power it. + * + * This driver uses the focus part only. After power-up the controller runs + * its focus servo on its own (0xf010 reads 7) with the lens at the far end + * of a 12-bit range (0xf01a reads 0x0fff). A move writes 0x0002 in the + * upper half of 0xf01a and the 12-bit target in the lower half; the + * controller's corrected Hall position (0x0548, upper half) follows the + * target's low 12 bits linearly over the whole stroke, and bits above them + * move the picture in other ways, so they are never set. Position 0 of the + * register is the near end, 4095 the far end; the V4L2 control counts the + * other way round, so that higher values focus closer. + * + * Registers used (all others, the calibration memory included, are never + * written): + * 0xf010 servo mode (read only: the controller is up once it answers) + * 0xf01a focus target, 0x0002xxxx + * 0x0548 corrected Hall position, read back with debug_position + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define LC898129_REG_SERVO CCI_REG32(0xf010) +#define LC898129_REG_FOCUS CCI_REG32(0xf01a) +#define LC898129_FOCUS_CMD 0x00020000 +#define LC898129_REG_HALL CCI_REG32(0x0548) + +#define LC898129_POS_MAX 4095 +#define LC898129_READY_TIMEOUT_US 200000 + +static bool debug_position; +module_param(debug_position, bool, 0644); +MODULE_PARM_DESC(debug_position, "Log the Hall position read back after each move"); + +static const char * const lc898129_supply_names[] = { + "vio", "vm", "vdda", +}; + +#define LC898129_NUM_SUPPLIES ARRAY_SIZE(lc898129_supply_names) + +struct lc898129 { + struct device *dev; + struct regmap *regmap; + struct v4l2_subdev sd; + struct v4l2_ctrl_handler ctrls; + struct v4l2_ctrl *focus; + struct regulator *supplies[LC898129_NUM_SUPPLIES]; + bool active; +}; + +static inline struct lc898129 *to_lc898129(struct v4l2_subdev *sd) +{ + return container_of(sd, struct lc898129, sd); +} + +static int lc898129_move(struct lc898129 *lens, u32 val) +{ + u32 target = LC898129_POS_MAX - val; + int ret; + + ret = cci_write(lens->regmap, LC898129_REG_FOCUS, + LC898129_FOCUS_CMD | target, NULL); + + if (!ret && debug_position) { + u64 hall = 0; + + usleep_range(10000, 11000); + cci_read(lens->regmap, LC898129_REG_HALL, &hall, NULL); + dev_info(lens->dev, "position %u (target %u), Hall %d\n", + val, target, (s16)(hall >> 16)); + } + + return ret; +} + +static int lc898129_set_ctrl(struct v4l2_ctrl *ctrl) +{ + struct lc898129 *lens = container_of(ctrl->handler, struct lc898129, ctrls); + int ret; + + if (ctrl->id != V4L2_CID_FOCUS_ABSOLUTE) + return -EINVAL; + + /* Only while powered; resume applies the value. */ + if (pm_runtime_get_if_in_use(lens->dev) <= 0) + return 0; + + ret = lc898129_move(lens, ctrl->val); + pm_runtime_put(lens->dev); + + return ret; +} + +static const struct v4l2_ctrl_ops lc898129_ctrl_ops = { + .s_ctrl = lc898129_set_ctrl, +}; + +static int lc898129_supplies_on(struct lc898129 *lens) +{ + int i, ret; + + for (i = 0; i < LC898129_NUM_SUPPLIES; i++) { + if (!lens->supplies[i]) + continue; + ret = regulator_enable(lens->supplies[i]); + if (ret) + goto err; + } + + return 0; + +err: + while (--i >= 0) + if (lens->supplies[i]) + regulator_disable(lens->supplies[i]); + return ret; +} + +static void lc898129_supplies_off(struct lc898129 *lens) +{ + int i; + + for (i = LC898129_NUM_SUPPLIES - 1; i >= 0; i--) + if (lens->supplies[i]) + regulator_disable(lens->supplies[i]); +} + +/* The controller boots after the sensor's reset: wait until it answers. */ +static int lc898129_wait_ready(struct lc898129 *lens) +{ + u64 servo = 0; + int ret, err; + + ret = read_poll_timeout(cci_read, err, !err, 5000, + LC898129_READY_TIMEOUT_US, false, + lens->regmap, LC898129_REG_SERVO, &servo, NULL); + if (ret) + dev_err(lens->dev, "doesn't answer: %d\n", err); + else + dev_dbg(lens->dev, "servo mode 0x%llx\n", servo); + + return ret ? err : 0; +} + +static int lc898129_power_on(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct lc898129 *lens = to_lc898129(sd); + int ret; + + if (lens->active) + return 0; + + ret = lc898129_supplies_on(lens); + if (ret) + return ret; + + ret = lc898129_wait_ready(lens); + if (!ret) + ret = lc898129_move(lens, lens->focus->val); + if (ret) { + lc898129_supplies_off(lens); + return ret; + } + + lens->active = true; + + return 0; +} + +static int lc898129_power_off(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct lc898129 *lens = to_lc898129(sd); + + if (!lens->active) + return 0; + + lc898129_supplies_off(lens); + lens->active = false; + + return 0; +} + +static const struct v4l2_subdev_ops lc898129_ops = { }; + +static int lc898129_init_controls(struct lc898129 *lens) +{ + struct v4l2_ctrl_handler *hdl = &lens->ctrls; + + v4l2_ctrl_handler_init(hdl, 1); + /* 0 is where the controller parks the lens itself: the far end */ + lens->focus = v4l2_ctrl_new_std(hdl, &lc898129_ctrl_ops, + V4L2_CID_FOCUS_ABSOLUTE, 0, + LC898129_POS_MAX, 1, 0); + if (hdl->error) { + int ret = hdl->error; + + v4l2_ctrl_handler_free(hdl); + return ret; + } + lens->sd.ctrl_handler = hdl; + + return 0; +} + +static int lc898129_probe(struct i2c_client *client) +{ + struct device *dev = &client->dev; + struct lc898129 *lens; + unsigned int i; + int ret; + + lens = devm_kzalloc(dev, sizeof(*lens), GFP_KERNEL); + if (!lens) + return -ENOMEM; + + lens->dev = dev; + lens->regmap = devm_cci_regmap_init_i2c(client, 16); + if (IS_ERR(lens->regmap)) + return PTR_ERR(lens->regmap); + + /* the sensor powers the module; these only keep the rails counted */ + for (i = 0; i < LC898129_NUM_SUPPLIES; i++) { + const char *name = lc898129_supply_names[i]; + + lens->supplies[i] = devm_regulator_get_optional(dev, name); + if (IS_ERR(lens->supplies[i])) { + ret = PTR_ERR(lens->supplies[i]); + if (ret == -ENODEV) { + lens->supplies[i] = NULL; + continue; + } + return dev_err_probe(dev, ret, "%s supply\n", name); + } + } + + v4l2_i2c_subdev_init(&lens->sd, client, &lc898129_ops); + lens->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; + lens->sd.entity.function = MEDIA_ENT_F_LENS; + + ret = lc898129_init_controls(lens); + if (ret) + return ret; + + ret = media_entity_pads_init(&lens->sd.entity, 0, NULL); + if (ret) + goto err_ctrls; + + pm_runtime_enable(dev); + + ret = v4l2_async_register_subdev(&lens->sd); + if (ret) + goto err_pm; + + return 0; + +err_pm: + pm_runtime_disable(dev); + media_entity_cleanup(&lens->sd.entity); +err_ctrls: + v4l2_ctrl_handler_free(&lens->ctrls); + return ret; +} + +static void lc898129_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd = i2c_get_clientdata(client); + struct lc898129 *lens = to_lc898129(sd); + + v4l2_async_unregister_subdev(sd); + v4l2_ctrl_handler_free(&lens->ctrls); + media_entity_cleanup(&sd->entity); + pm_runtime_disable(&client->dev); + if (!pm_runtime_status_suspended(&client->dev)) + lc898129_power_off(&client->dev); + pm_runtime_set_suspended(&client->dev); +} + +static DEFINE_RUNTIME_DEV_PM_OPS(lc898129_pm_ops, lc898129_power_off, + lc898129_power_on, NULL); + +static const struct of_device_id lc898129_of_match[] = { + { .compatible = "onnn,lc898129" }, + { } +}; +MODULE_DEVICE_TABLE(of, lc898129_of_match); + +static struct i2c_driver lc898129_driver = { + .probe = lc898129_probe, + .remove = lc898129_remove, + .driver = { + .name = "lc898129", + .of_match_table = lc898129_of_match, + .pm = pm_ptr(&lc898129_pm_ops), + }, +}; +module_i2c_driver(lc898129_driver); + +MODULE_DESCRIPTION("onsemi LC898129 lens driver"); +MODULE_LICENSE("GPL");