1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2018 Intel Corporation 3 4 #include <linux/acpi.h> 5 #include <linux/delay.h> 6 #include <linux/i2c.h> 7 #include <linux/module.h> 8 #include <linux/pm_runtime.h> 9 #include <linux/regulator/consumer.h> 10 #include <media/v4l2-ctrls.h> 11 #include <media/v4l2-device.h> 12 13 struct ak73xx_chipdef { 14 u8 reg_position; 15 u8 reg_cont; 16 u8 shift_pos; 17 u8 mode_active; 18 u8 mode_standby; 19 bool has_standby; /* Some chips may not have standby mode */ 20 u16 focus_pos_max; 21 /* 22 * This sets the minimum granularity for the focus positions. 23 * A value of 1 gives maximum accuracy for a desired focus position 24 */ 25 u16 focus_steps; 26 /* 27 * This acts as the minimum granularity of lens movement. 28 * Keep this value power of 2, so the control steps can be 29 * uniformly adjusted for gradual lens movement, with desired 30 * number of control steps. 31 */ 32 u16 ctrl_steps; 33 u16 ctrl_delay_us; 34 /* 35 * The vcm may take time (tDELAY) to power on and start taking 36 * I2C messages. 37 */ 38 u16 power_delay_us; 39 }; 40 41 static const struct ak73xx_chipdef ak7345_cdef = { 42 .reg_position = 0x0, 43 .reg_cont = 0x2, 44 .shift_pos = 7, /* 9 bits position values, need to << 7 */ 45 .mode_active = 0x0, 46 .has_standby = false, 47 .focus_pos_max = 511, 48 .focus_steps = 1, 49 .ctrl_steps = 16, 50 .ctrl_delay_us = 1000, 51 .power_delay_us = 20000, 52 }; 53 54 static const struct ak73xx_chipdef ak7375_cdef = { 55 .reg_position = 0x0, 56 .reg_cont = 0x2, 57 .shift_pos = 4, /* 12 bits position values, need to << 4 */ 58 .mode_active = 0x0, 59 .mode_standby = 0x40, 60 .has_standby = true, 61 .focus_pos_max = 4095, 62 .focus_steps = 1, 63 .ctrl_steps = 64, 64 .ctrl_delay_us = 1000, 65 .power_delay_us = 10000, 66 }; 67 68 static const char * const ak7375_supply_names[] = { 69 "vdd", 70 "vio", 71 }; 72 73 /* ak7375 device structure */ 74 struct ak7375_device { 75 const struct ak73xx_chipdef *cdef; 76 struct v4l2_ctrl_handler ctrls_vcm; 77 struct v4l2_subdev sd; 78 struct v4l2_ctrl *focus; 79 struct regulator_bulk_data supplies[ARRAY_SIZE(ak7375_supply_names)]; 80 81 /* active or standby mode */ 82 bool active; 83 }; 84 85 static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl) 86 { 87 return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm); 88 } 89 90 static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev) 91 { 92 return container_of(subdev, struct ak7375_device, sd); 93 } 94 95 static int ak7375_i2c_write(struct ak7375_device *ak7375, 96 u8 addr, u16 data, u8 size) 97 { 98 struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd); 99 u8 buf[3]; 100 int ret; 101 102 if (size != 1 && size != 2) 103 return -EINVAL; 104 buf[0] = addr; 105 buf[size] = data & 0xff; 106 if (size == 2) 107 buf[1] = (data >> 8) & 0xff; 108 ret = i2c_master_send(client, (const char *)buf, size + 1); 109 if (ret < 0) 110 return ret; 111 if (ret != size + 1) 112 return -EIO; 113 114 return 0; 115 } 116 117 static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl) 118 { 119 struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl); 120 const struct ak73xx_chipdef *cdef = dev_vcm->cdef; 121 122 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) 123 return ak7375_i2c_write(dev_vcm, cdef->reg_position, 124 ctrl->val << cdef->shift_pos, 2); 125 126 return -EINVAL; 127 } 128 129 static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = { 130 .s_ctrl = ak7375_set_ctrl, 131 }; 132 133 static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 134 { 135 return pm_runtime_resume_and_get(sd->dev); 136 } 137 138 static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 139 { 140 pm_runtime_put(sd->dev); 141 142 return 0; 143 } 144 145 static const struct v4l2_subdev_internal_ops ak7375_int_ops = { 146 .open = ak7375_open, 147 .close = ak7375_close, 148 }; 149 150 static const struct v4l2_subdev_ops ak7375_ops = { }; 151 152 static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev) 153 { 154 v4l2_async_unregister_subdev(&ak7375_dev->sd); 155 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm); 156 media_entity_cleanup(&ak7375_dev->sd.entity); 157 } 158 159 static int ak7375_init_controls(struct ak7375_device *dev_vcm) 160 { 161 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm; 162 const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops; 163 const struct ak73xx_chipdef *cdef = dev_vcm->cdef; 164 165 v4l2_ctrl_handler_init(hdl, 1); 166 167 dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE, 168 0, cdef->focus_pos_max, cdef->focus_steps, 0); 169 170 if (hdl->error) 171 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n", 172 __func__, hdl->error); 173 dev_vcm->sd.ctrl_handler = hdl; 174 175 return hdl->error; 176 } 177 178 static int ak7375_probe(struct i2c_client *client) 179 { 180 struct ak7375_device *ak7375_dev; 181 int ret; 182 unsigned int i; 183 184 ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev), 185 GFP_KERNEL); 186 if (!ak7375_dev) 187 return -ENOMEM; 188 189 ak7375_dev->cdef = device_get_match_data(&client->dev); 190 191 for (i = 0; i < ARRAY_SIZE(ak7375_supply_names); i++) 192 ak7375_dev->supplies[i].supply = ak7375_supply_names[i]; 193 194 ret = devm_regulator_bulk_get(&client->dev, 195 ARRAY_SIZE(ak7375_supply_names), 196 ak7375_dev->supplies); 197 if (ret) { 198 dev_err_probe(&client->dev, ret, "Failed to get regulators\n"); 199 return ret; 200 } 201 202 v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops); 203 ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 204 ak7375_dev->sd.internal_ops = &ak7375_int_ops; 205 ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS; 206 207 ret = ak7375_init_controls(ak7375_dev); 208 if (ret) 209 goto err_cleanup; 210 211 ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL); 212 if (ret < 0) 213 goto err_cleanup; 214 215 ret = v4l2_async_register_subdev(&ak7375_dev->sd); 216 if (ret < 0) 217 goto err_cleanup; 218 219 pm_runtime_set_active(&client->dev); 220 pm_runtime_enable(&client->dev); 221 pm_runtime_idle(&client->dev); 222 223 return 0; 224 225 err_cleanup: 226 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm); 227 media_entity_cleanup(&ak7375_dev->sd.entity); 228 229 return ret; 230 } 231 232 static void ak7375_remove(struct i2c_client *client) 233 { 234 struct v4l2_subdev *sd = i2c_get_clientdata(client); 235 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd); 236 237 ak7375_subdev_cleanup(ak7375_dev); 238 pm_runtime_disable(&client->dev); 239 pm_runtime_set_suspended(&client->dev); 240 } 241 242 /* 243 * This function sets the vcm position, so it consumes least current 244 * The lens position is gradually moved in units of ctrl_steps, 245 * to make the movements smoothly. 246 */ 247 static int __maybe_unused ak7375_vcm_suspend(struct device *dev) 248 { 249 struct v4l2_subdev *sd = dev_get_drvdata(dev); 250 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd); 251 const struct ak73xx_chipdef *cdef = ak7375_dev->cdef; 252 int ret, val; 253 254 if (!ak7375_dev->active) 255 return 0; 256 257 for (val = ak7375_dev->focus->val & ~(cdef->ctrl_steps - 1); 258 val >= 0; val -= cdef->ctrl_steps) { 259 ret = ak7375_i2c_write(ak7375_dev, cdef->reg_position, 260 val << cdef->shift_pos, 2); 261 if (ret) 262 dev_err_once(dev, "%s I2C failure: %d\n", 263 __func__, ret); 264 usleep_range(cdef->ctrl_delay_us, cdef->ctrl_delay_us + 10); 265 } 266 267 if (cdef->has_standby) { 268 ret = ak7375_i2c_write(ak7375_dev, cdef->reg_cont, 269 cdef->mode_standby, 1); 270 if (ret) 271 dev_err(dev, "%s I2C failure: %d\n", __func__, ret); 272 } 273 274 ret = regulator_bulk_disable(ARRAY_SIZE(ak7375_supply_names), 275 ak7375_dev->supplies); 276 if (ret) 277 return ret; 278 279 ak7375_dev->active = false; 280 281 return 0; 282 } 283 284 /* 285 * This function sets the vcm position to the value set by the user 286 * through v4l2_ctrl_ops s_ctrl handler 287 * The lens position is gradually moved in units of ctrl_steps, 288 * to make the movements smoothly. 289 */ 290 static int __maybe_unused ak7375_vcm_resume(struct device *dev) 291 { 292 struct v4l2_subdev *sd = dev_get_drvdata(dev); 293 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd); 294 const struct ak73xx_chipdef *cdef = ak7375_dev->cdef; 295 int ret, val; 296 297 if (ak7375_dev->active) 298 return 0; 299 300 ret = regulator_bulk_enable(ARRAY_SIZE(ak7375_supply_names), 301 ak7375_dev->supplies); 302 if (ret) 303 return ret; 304 305 /* Wait for vcm to become ready */ 306 usleep_range(cdef->power_delay_us, cdef->power_delay_us + 500); 307 308 ret = ak7375_i2c_write(ak7375_dev, cdef->reg_cont, 309 cdef->mode_active, 1); 310 if (ret) { 311 dev_err(dev, "%s I2C failure: %d\n", __func__, ret); 312 return ret; 313 } 314 315 for (val = ak7375_dev->focus->val % cdef->ctrl_steps; 316 val <= ak7375_dev->focus->val; 317 val += cdef->ctrl_steps) { 318 ret = ak7375_i2c_write(ak7375_dev, cdef->reg_position, 319 val << cdef->shift_pos, 2); 320 if (ret) 321 dev_err_ratelimited(dev, "%s I2C failure: %d\n", 322 __func__, ret); 323 usleep_range(cdef->ctrl_delay_us, cdef->ctrl_delay_us + 10); 324 } 325 326 ak7375_dev->active = true; 327 328 return 0; 329 } 330 331 static const struct of_device_id ak7375_of_table[] = { 332 { .compatible = "asahi-kasei,ak7345", .data = &ak7345_cdef, }, 333 { .compatible = "asahi-kasei,ak7375", .data = &ak7375_cdef, }, 334 { /* sentinel */ } 335 }; 336 MODULE_DEVICE_TABLE(of, ak7375_of_table); 337 338 static const struct dev_pm_ops ak7375_pm_ops = { 339 SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume) 340 SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL) 341 }; 342 343 static struct i2c_driver ak7375_i2c_driver = { 344 .driver = { 345 .name = "ak7375", 346 .pm = &ak7375_pm_ops, 347 .of_match_table = ak7375_of_table, 348 }, 349 .probe = ak7375_probe, 350 .remove = ak7375_remove, 351 }; 352 module_i2c_driver(ak7375_i2c_driver); 353 354 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>"); 355 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>"); 356 MODULE_DESCRIPTION("AK7375 VCM driver"); 357 MODULE_LICENSE("GPL v2"); 358