1136fe992SRajmohan Mani // SPDX-License-Identifier: GPL-2.0 2136fe992SRajmohan Mani // Copyright (c) 2015--2017 Intel Corporation. 3cc95d342SRajmohan Mani 4cc95d342SRajmohan Mani #include <linux/delay.h> 5cc95d342SRajmohan Mani #include <linux/i2c.h> 6cc95d342SRajmohan Mani #include <linux/module.h> 7cc95d342SRajmohan Mani #include <linux/pm_runtime.h> 8932de2cdSAngus Ainslie #include <linux/regulator/consumer.h> 9cc95d342SRajmohan Mani #include <media/v4l2-ctrls.h> 10cc95d342SRajmohan Mani #include <media/v4l2-device.h> 1198442bd0SRicardo Ribalda #include <media/v4l2-event.h> 12cc95d342SRajmohan Mani 13cc95d342SRajmohan Mani #define DW9714_NAME "dw9714" 14cc95d342SRajmohan Mani #define DW9714_MAX_FOCUS_POS 1023 15cc95d342SRajmohan Mani /* 161a58fbf5SRajmohan Mani * This sets the minimum granularity for the focus positions. 171a58fbf5SRajmohan Mani * A value of 1 gives maximum accuracy for a desired focus position 181a58fbf5SRajmohan Mani */ 191a58fbf5SRajmohan Mani #define DW9714_FOCUS_STEPS 1 201a58fbf5SRajmohan Mani /* 21cc95d342SRajmohan Mani * This acts as the minimum granularity of lens movement. 22cc95d342SRajmohan Mani * Keep this value power of 2, so the control steps can be 23cc95d342SRajmohan Mani * uniformly adjusted for gradual lens movement, with desired 24cc95d342SRajmohan Mani * number of control steps. 25cc95d342SRajmohan Mani */ 26cc95d342SRajmohan Mani #define DW9714_CTRL_STEPS 16 27cc95d342SRajmohan Mani #define DW9714_CTRL_DELAY_US 1000 28cc95d342SRajmohan Mani /* 29cc95d342SRajmohan Mani * S[3:2] = 0x00, codes per step for "Linear Slope Control" 30cc95d342SRajmohan Mani * S[1:0] = 0x00, step period 31cc95d342SRajmohan Mani */ 32cc95d342SRajmohan Mani #define DW9714_DEFAULT_S 0x0 33cc95d342SRajmohan Mani #define DW9714_VAL(data, s) ((data) << 4 | (s)) 34cc95d342SRajmohan Mani 35cc95d342SRajmohan Mani /* dw9714 device structure */ 36cc95d342SRajmohan Mani struct dw9714_device { 37cc95d342SRajmohan Mani struct v4l2_ctrl_handler ctrls_vcm; 38cc95d342SRajmohan Mani struct v4l2_subdev sd; 39cc95d342SRajmohan Mani u16 current_val; 40932de2cdSAngus Ainslie struct regulator *vcc; 41cc95d342SRajmohan Mani }; 42cc95d342SRajmohan Mani 43cc95d342SRajmohan Mani static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl) 44cc95d342SRajmohan Mani { 45cc95d342SRajmohan Mani return container_of(ctrl->handler, struct dw9714_device, ctrls_vcm); 46cc95d342SRajmohan Mani } 47cc95d342SRajmohan Mani 48cc95d342SRajmohan Mani static inline struct dw9714_device *sd_to_dw9714_vcm(struct v4l2_subdev *subdev) 49cc95d342SRajmohan Mani { 50cc95d342SRajmohan Mani return container_of(subdev, struct dw9714_device, sd); 51cc95d342SRajmohan Mani } 52cc95d342SRajmohan Mani 53cc95d342SRajmohan Mani static int dw9714_i2c_write(struct i2c_client *client, u16 data) 54cc95d342SRajmohan Mani { 55cc95d342SRajmohan Mani int ret; 56a4a02b6eSMauro Carvalho Chehab __be16 val = cpu_to_be16(data); 57cc95d342SRajmohan Mani 58cc95d342SRajmohan Mani ret = i2c_master_send(client, (const char *)&val, sizeof(val)); 59cc95d342SRajmohan Mani if (ret != sizeof(val)) { 60cc95d342SRajmohan Mani dev_err(&client->dev, "I2C write fail\n"); 61cc95d342SRajmohan Mani return -EIO; 62cc95d342SRajmohan Mani } 63cc95d342SRajmohan Mani return 0; 64cc95d342SRajmohan Mani } 65cc95d342SRajmohan Mani 66cc95d342SRajmohan Mani static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val) 67cc95d342SRajmohan Mani { 68a69e9972SSakari Ailus struct i2c_client *client = v4l2_get_subdevdata(&dw9714_dev->sd); 69cc95d342SRajmohan Mani 70cc95d342SRajmohan Mani dw9714_dev->current_val = val; 71cc95d342SRajmohan Mani 72cc95d342SRajmohan Mani return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S)); 73cc95d342SRajmohan Mani } 74cc95d342SRajmohan Mani 75cc95d342SRajmohan Mani static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl) 76cc95d342SRajmohan Mani { 77cc95d342SRajmohan Mani struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl); 78cc95d342SRajmohan Mani 79cc95d342SRajmohan Mani if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) 80cc95d342SRajmohan Mani return dw9714_t_focus_vcm(dev_vcm, ctrl->val); 81cc95d342SRajmohan Mani 82cc95d342SRajmohan Mani return -EINVAL; 83cc95d342SRajmohan Mani } 84cc95d342SRajmohan Mani 85cc95d342SRajmohan Mani static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = { 86cc95d342SRajmohan Mani .s_ctrl = dw9714_set_ctrl, 87cc95d342SRajmohan Mani }; 88cc95d342SRajmohan Mani 89cc95d342SRajmohan Mani static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 90cc95d342SRajmohan Mani { 917917f279SMauro Carvalho Chehab return pm_runtime_resume_and_get(sd->dev); 92cc95d342SRajmohan Mani } 93cc95d342SRajmohan Mani 94cc95d342SRajmohan Mani static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 95cc95d342SRajmohan Mani { 96a69e9972SSakari Ailus pm_runtime_put(sd->dev); 97cc95d342SRajmohan Mani 98cc95d342SRajmohan Mani return 0; 99cc95d342SRajmohan Mani } 100cc95d342SRajmohan Mani 101cc95d342SRajmohan Mani static const struct v4l2_subdev_internal_ops dw9714_int_ops = { 102cc95d342SRajmohan Mani .open = dw9714_open, 103cc95d342SRajmohan Mani .close = dw9714_close, 104cc95d342SRajmohan Mani }; 105cc95d342SRajmohan Mani 10698442bd0SRicardo Ribalda static const struct v4l2_subdev_core_ops dw9714_core_ops = { 10798442bd0SRicardo Ribalda .log_status = v4l2_ctrl_subdev_log_status, 10898442bd0SRicardo Ribalda .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 10998442bd0SRicardo Ribalda .unsubscribe_event = v4l2_event_subdev_unsubscribe, 11098442bd0SRicardo Ribalda }; 11198442bd0SRicardo Ribalda 11298442bd0SRicardo Ribalda static const struct v4l2_subdev_ops dw9714_ops = { 11398442bd0SRicardo Ribalda .core = &dw9714_core_ops, 11498442bd0SRicardo Ribalda }; 115cc95d342SRajmohan Mani 116cc95d342SRajmohan Mani static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev) 117cc95d342SRajmohan Mani { 118cc95d342SRajmohan Mani v4l2_async_unregister_subdev(&dw9714_dev->sd); 119cc95d342SRajmohan Mani v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm); 120cc95d342SRajmohan Mani media_entity_cleanup(&dw9714_dev->sd.entity); 121cc95d342SRajmohan Mani } 122cc95d342SRajmohan Mani 123cc95d342SRajmohan Mani static int dw9714_init_controls(struct dw9714_device *dev_vcm) 124cc95d342SRajmohan Mani { 125cc95d342SRajmohan Mani struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm; 126cc95d342SRajmohan Mani const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops; 127cc95d342SRajmohan Mani 128cc95d342SRajmohan Mani v4l2_ctrl_handler_init(hdl, 1); 129cc95d342SRajmohan Mani 130cc95d342SRajmohan Mani v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE, 1311a58fbf5SRajmohan Mani 0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, 0); 132cc95d342SRajmohan Mani 133cc95d342SRajmohan Mani if (hdl->error) 134a69e9972SSakari Ailus dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n", 135cc95d342SRajmohan Mani __func__, hdl->error); 136cc95d342SRajmohan Mani dev_vcm->sd.ctrl_handler = hdl; 137cc95d342SRajmohan Mani return hdl->error; 138cc95d342SRajmohan Mani } 139cc95d342SRajmohan Mani 140f758eb23SSakari Ailus static int dw9714_probe(struct i2c_client *client) 141cc95d342SRajmohan Mani { 142cc95d342SRajmohan Mani struct dw9714_device *dw9714_dev; 143cc95d342SRajmohan Mani int rval; 144cc95d342SRajmohan Mani 145cc95d342SRajmohan Mani dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev), 146cc95d342SRajmohan Mani GFP_KERNEL); 147cc95d342SRajmohan Mani if (dw9714_dev == NULL) 148cc95d342SRajmohan Mani return -ENOMEM; 149cc95d342SRajmohan Mani 150932de2cdSAngus Ainslie dw9714_dev->vcc = devm_regulator_get(&client->dev, "vcc"); 151932de2cdSAngus Ainslie if (IS_ERR(dw9714_dev->vcc)) 152932de2cdSAngus Ainslie return PTR_ERR(dw9714_dev->vcc); 153932de2cdSAngus Ainslie 154932de2cdSAngus Ainslie rval = regulator_enable(dw9714_dev->vcc); 155932de2cdSAngus Ainslie if (rval < 0) { 156932de2cdSAngus Ainslie dev_err(&client->dev, "failed to enable vcc: %d\n", rval); 157932de2cdSAngus Ainslie return rval; 158932de2cdSAngus Ainslie } 159932de2cdSAngus Ainslie 160cc95d342SRajmohan Mani v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops); 16198442bd0SRicardo Ribalda dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | 16298442bd0SRicardo Ribalda V4L2_SUBDEV_FL_HAS_EVENTS; 163cc95d342SRajmohan Mani dw9714_dev->sd.internal_ops = &dw9714_int_ops; 164cc95d342SRajmohan Mani 165cc95d342SRajmohan Mani rval = dw9714_init_controls(dw9714_dev); 166cc95d342SRajmohan Mani if (rval) 167cc95d342SRajmohan Mani goto err_cleanup; 168cc95d342SRajmohan Mani 169cc95d342SRajmohan Mani rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL); 170cc95d342SRajmohan Mani if (rval < 0) 171cc95d342SRajmohan Mani goto err_cleanup; 172cc95d342SRajmohan Mani 173cc95d342SRajmohan Mani dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS; 174cc95d342SRajmohan Mani 175cc95d342SRajmohan Mani rval = v4l2_async_register_subdev(&dw9714_dev->sd); 176cc95d342SRajmohan Mani if (rval < 0) 177cc95d342SRajmohan Mani goto err_cleanup; 178cc95d342SRajmohan Mani 179cc95d342SRajmohan Mani pm_runtime_set_active(&client->dev); 180cc95d342SRajmohan Mani pm_runtime_enable(&client->dev); 181fa429334SSakari Ailus pm_runtime_idle(&client->dev); 182cc95d342SRajmohan Mani 183cc95d342SRajmohan Mani return 0; 184cc95d342SRajmohan Mani 185cc95d342SRajmohan Mani err_cleanup: 186*02276e18SZheyu Ma regulator_disable(dw9714_dev->vcc); 187f9a0b142SRajmohan Mani v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm); 188f9a0b142SRajmohan Mani media_entity_cleanup(&dw9714_dev->sd.entity); 1891c55ecabSSakari Ailus 190cc95d342SRajmohan Mani return rval; 191cc95d342SRajmohan Mani } 192cc95d342SRajmohan Mani 193cc95d342SRajmohan Mani static int dw9714_remove(struct i2c_client *client) 194cc95d342SRajmohan Mani { 195cc95d342SRajmohan Mani struct v4l2_subdev *sd = i2c_get_clientdata(client); 196cc95d342SRajmohan Mani struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 197932de2cdSAngus Ainslie int ret; 198cc95d342SRajmohan Mani 199cc95d342SRajmohan Mani pm_runtime_disable(&client->dev); 200932de2cdSAngus Ainslie if (!pm_runtime_status_suspended(&client->dev)) { 201932de2cdSAngus Ainslie ret = regulator_disable(dw9714_dev->vcc); 202932de2cdSAngus Ainslie if (ret) { 203932de2cdSAngus Ainslie dev_err(&client->dev, 204932de2cdSAngus Ainslie "Failed to disable vcc: %d\n", ret); 205932de2cdSAngus Ainslie return ret; 206932de2cdSAngus Ainslie } 207932de2cdSAngus Ainslie } 208932de2cdSAngus Ainslie pm_runtime_set_suspended(&client->dev); 209cc95d342SRajmohan Mani dw9714_subdev_cleanup(dw9714_dev); 210cc95d342SRajmohan Mani 211cc95d342SRajmohan Mani return 0; 212cc95d342SRajmohan Mani } 213cc95d342SRajmohan Mani 214cc95d342SRajmohan Mani /* 215cc95d342SRajmohan Mani * This function sets the vcm position, so it consumes least current 216cc95d342SRajmohan Mani * The lens position is gradually moved in units of DW9714_CTRL_STEPS, 217cc95d342SRajmohan Mani * to make the movements smoothly. 218cc95d342SRajmohan Mani */ 219cc95d342SRajmohan Mani static int __maybe_unused dw9714_vcm_suspend(struct device *dev) 220cc95d342SRajmohan Mani { 221cc95d342SRajmohan Mani struct i2c_client *client = to_i2c_client(dev); 222cc95d342SRajmohan Mani struct v4l2_subdev *sd = i2c_get_clientdata(client); 223cc95d342SRajmohan Mani struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 224cc95d342SRajmohan Mani int ret, val; 225cc95d342SRajmohan Mani 226932de2cdSAngus Ainslie if (pm_runtime_suspended(&client->dev)) 227932de2cdSAngus Ainslie return 0; 228932de2cdSAngus Ainslie 229cc95d342SRajmohan Mani for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1); 230cc95d342SRajmohan Mani val >= 0; val -= DW9714_CTRL_STEPS) { 231cc95d342SRajmohan Mani ret = dw9714_i2c_write(client, 232cc95d342SRajmohan Mani DW9714_VAL(val, DW9714_DEFAULT_S)); 233cc95d342SRajmohan Mani if (ret) 234cc95d342SRajmohan Mani dev_err_once(dev, "%s I2C failure: %d", __func__, ret); 235cc95d342SRajmohan Mani usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); 236cc95d342SRajmohan Mani } 237932de2cdSAngus Ainslie 238932de2cdSAngus Ainslie ret = regulator_disable(dw9714_dev->vcc); 239932de2cdSAngus Ainslie if (ret) 240932de2cdSAngus Ainslie dev_err(dev, "Failed to disable vcc: %d\n", ret); 241932de2cdSAngus Ainslie 242932de2cdSAngus Ainslie return ret; 243cc95d342SRajmohan Mani } 244cc95d342SRajmohan Mani 245cc95d342SRajmohan Mani /* 246cc95d342SRajmohan Mani * This function sets the vcm position to the value set by the user 247cc95d342SRajmohan Mani * through v4l2_ctrl_ops s_ctrl handler 248cc95d342SRajmohan Mani * The lens position is gradually moved in units of DW9714_CTRL_STEPS, 249cc95d342SRajmohan Mani * to make the movements smoothly. 250cc95d342SRajmohan Mani */ 251cc95d342SRajmohan Mani static int __maybe_unused dw9714_vcm_resume(struct device *dev) 252cc95d342SRajmohan Mani { 253cc95d342SRajmohan Mani struct i2c_client *client = to_i2c_client(dev); 254cc95d342SRajmohan Mani struct v4l2_subdev *sd = i2c_get_clientdata(client); 255cc95d342SRajmohan Mani struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 256cc95d342SRajmohan Mani int ret, val; 257cc95d342SRajmohan Mani 258932de2cdSAngus Ainslie if (pm_runtime_suspended(&client->dev)) 259932de2cdSAngus Ainslie return 0; 260932de2cdSAngus Ainslie 261932de2cdSAngus Ainslie ret = regulator_enable(dw9714_dev->vcc); 262932de2cdSAngus Ainslie if (ret) { 263932de2cdSAngus Ainslie dev_err(dev, "Failed to enable vcc: %d\n", ret); 264932de2cdSAngus Ainslie return ret; 265932de2cdSAngus Ainslie } 266932de2cdSAngus Ainslie usleep_range(1000, 2000); 267932de2cdSAngus Ainslie 268cc95d342SRajmohan Mani for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS; 269cc95d342SRajmohan Mani val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1; 270cc95d342SRajmohan Mani val += DW9714_CTRL_STEPS) { 271cc95d342SRajmohan Mani ret = dw9714_i2c_write(client, 272cc95d342SRajmohan Mani DW9714_VAL(val, DW9714_DEFAULT_S)); 273cc95d342SRajmohan Mani if (ret) 274cc95d342SRajmohan Mani dev_err_ratelimited(dev, "%s I2C failure: %d", 275cc95d342SRajmohan Mani __func__, ret); 276cc95d342SRajmohan Mani usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); 277cc95d342SRajmohan Mani } 278cc95d342SRajmohan Mani 279cc95d342SRajmohan Mani return 0; 280cc95d342SRajmohan Mani } 281cc95d342SRajmohan Mani 282cc95d342SRajmohan Mani static const struct i2c_device_id dw9714_id_table[] = { 283cc95d342SRajmohan Mani { DW9714_NAME, 0 }, 284f758eb23SSakari Ailus { { 0 } } 285cc95d342SRajmohan Mani }; 286cc95d342SRajmohan Mani MODULE_DEVICE_TABLE(i2c, dw9714_id_table); 287cc95d342SRajmohan Mani 288c2bc8b06SSakari Ailus static const struct of_device_id dw9714_of_table[] = { 289c2bc8b06SSakari Ailus { .compatible = "dongwoon,dw9714" }, 290c2bc8b06SSakari Ailus { { 0 } } 291c2bc8b06SSakari Ailus }; 292c2bc8b06SSakari Ailus MODULE_DEVICE_TABLE(of, dw9714_of_table); 293c2bc8b06SSakari Ailus 294cc95d342SRajmohan Mani static const struct dev_pm_ops dw9714_pm_ops = { 295cc95d342SRajmohan Mani SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume) 296cc95d342SRajmohan Mani SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL) 297cc95d342SRajmohan Mani }; 298cc95d342SRajmohan Mani 299cc95d342SRajmohan Mani static struct i2c_driver dw9714_i2c_driver = { 300cc95d342SRajmohan Mani .driver = { 301cc95d342SRajmohan Mani .name = DW9714_NAME, 302cc95d342SRajmohan Mani .pm = &dw9714_pm_ops, 303c2bc8b06SSakari Ailus .of_match_table = dw9714_of_table, 304cc95d342SRajmohan Mani }, 305f758eb23SSakari Ailus .probe_new = dw9714_probe, 306cc95d342SRajmohan Mani .remove = dw9714_remove, 307cc95d342SRajmohan Mani .id_table = dw9714_id_table, 308cc95d342SRajmohan Mani }; 309cc95d342SRajmohan Mani 310cc95d342SRajmohan Mani module_i2c_driver(dw9714_i2c_driver); 311cc95d342SRajmohan Mani 312cc95d342SRajmohan Mani MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>"); 3138f57763eSSakari Ailus MODULE_AUTHOR("Jian Xu Zheng"); 314cc95d342SRajmohan Mani MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>"); 315cc95d342SRajmohan Mani MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>"); 316cc95d342SRajmohan Mani MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>"); 317cc95d342SRajmohan Mani MODULE_DESCRIPTION("DW9714 VCM driver"); 318cc95d342SRajmohan Mani MODULE_LICENSE("GPL v2"); 319