1*136fe992SRajmohan Mani // SPDX-License-Identifier: GPL-2.0 2*136fe992SRajmohan 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> 8cc95d342SRajmohan Mani #include <media/v4l2-ctrls.h> 9cc95d342SRajmohan Mani #include <media/v4l2-device.h> 10cc95d342SRajmohan Mani 11cc95d342SRajmohan Mani #define DW9714_NAME "dw9714" 12cc95d342SRajmohan Mani #define DW9714_MAX_FOCUS_POS 1023 13cc95d342SRajmohan Mani /* 141a58fbf5SRajmohan Mani * This sets the minimum granularity for the focus positions. 151a58fbf5SRajmohan Mani * A value of 1 gives maximum accuracy for a desired focus position 161a58fbf5SRajmohan Mani */ 171a58fbf5SRajmohan Mani #define DW9714_FOCUS_STEPS 1 181a58fbf5SRajmohan Mani /* 19cc95d342SRajmohan Mani * This acts as the minimum granularity of lens movement. 20cc95d342SRajmohan Mani * Keep this value power of 2, so the control steps can be 21cc95d342SRajmohan Mani * uniformly adjusted for gradual lens movement, with desired 22cc95d342SRajmohan Mani * number of control steps. 23cc95d342SRajmohan Mani */ 24cc95d342SRajmohan Mani #define DW9714_CTRL_STEPS 16 25cc95d342SRajmohan Mani #define DW9714_CTRL_DELAY_US 1000 26cc95d342SRajmohan Mani /* 27cc95d342SRajmohan Mani * S[3:2] = 0x00, codes per step for "Linear Slope Control" 28cc95d342SRajmohan Mani * S[1:0] = 0x00, step period 29cc95d342SRajmohan Mani */ 30cc95d342SRajmohan Mani #define DW9714_DEFAULT_S 0x0 31cc95d342SRajmohan Mani #define DW9714_VAL(data, s) ((data) << 4 | (s)) 32cc95d342SRajmohan Mani 33cc95d342SRajmohan Mani /* dw9714 device structure */ 34cc95d342SRajmohan Mani struct dw9714_device { 35cc95d342SRajmohan Mani struct v4l2_ctrl_handler ctrls_vcm; 36cc95d342SRajmohan Mani struct v4l2_subdev sd; 37cc95d342SRajmohan Mani u16 current_val; 38cc95d342SRajmohan Mani }; 39cc95d342SRajmohan Mani 40cc95d342SRajmohan Mani static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl) 41cc95d342SRajmohan Mani { 42cc95d342SRajmohan Mani return container_of(ctrl->handler, struct dw9714_device, ctrls_vcm); 43cc95d342SRajmohan Mani } 44cc95d342SRajmohan Mani 45cc95d342SRajmohan Mani static inline struct dw9714_device *sd_to_dw9714_vcm(struct v4l2_subdev *subdev) 46cc95d342SRajmohan Mani { 47cc95d342SRajmohan Mani return container_of(subdev, struct dw9714_device, sd); 48cc95d342SRajmohan Mani } 49cc95d342SRajmohan Mani 50cc95d342SRajmohan Mani static int dw9714_i2c_write(struct i2c_client *client, u16 data) 51cc95d342SRajmohan Mani { 52cc95d342SRajmohan Mani int ret; 53a4a02b6eSMauro Carvalho Chehab __be16 val = cpu_to_be16(data); 54cc95d342SRajmohan Mani 55cc95d342SRajmohan Mani ret = i2c_master_send(client, (const char *)&val, sizeof(val)); 56cc95d342SRajmohan Mani if (ret != sizeof(val)) { 57cc95d342SRajmohan Mani dev_err(&client->dev, "I2C write fail\n"); 58cc95d342SRajmohan Mani return -EIO; 59cc95d342SRajmohan Mani } 60cc95d342SRajmohan Mani return 0; 61cc95d342SRajmohan Mani } 62cc95d342SRajmohan Mani 63cc95d342SRajmohan Mani static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val) 64cc95d342SRajmohan Mani { 65a69e9972SSakari Ailus struct i2c_client *client = v4l2_get_subdevdata(&dw9714_dev->sd); 66cc95d342SRajmohan Mani 67cc95d342SRajmohan Mani dw9714_dev->current_val = val; 68cc95d342SRajmohan Mani 69cc95d342SRajmohan Mani return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S)); 70cc95d342SRajmohan Mani } 71cc95d342SRajmohan Mani 72cc95d342SRajmohan Mani static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl) 73cc95d342SRajmohan Mani { 74cc95d342SRajmohan Mani struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl); 75cc95d342SRajmohan Mani 76cc95d342SRajmohan Mani if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) 77cc95d342SRajmohan Mani return dw9714_t_focus_vcm(dev_vcm, ctrl->val); 78cc95d342SRajmohan Mani 79cc95d342SRajmohan Mani return -EINVAL; 80cc95d342SRajmohan Mani } 81cc95d342SRajmohan Mani 82cc95d342SRajmohan Mani static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = { 83cc95d342SRajmohan Mani .s_ctrl = dw9714_set_ctrl, 84cc95d342SRajmohan Mani }; 85cc95d342SRajmohan Mani 86cc95d342SRajmohan Mani static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 87cc95d342SRajmohan Mani { 88cc95d342SRajmohan Mani int rval; 89cc95d342SRajmohan Mani 90a69e9972SSakari Ailus rval = pm_runtime_get_sync(sd->dev); 91cc95d342SRajmohan Mani if (rval < 0) { 92a69e9972SSakari Ailus pm_runtime_put_noidle(sd->dev); 93cc95d342SRajmohan Mani return rval; 94cc95d342SRajmohan Mani } 95cc95d342SRajmohan Mani 96cc95d342SRajmohan Mani return 0; 97cc95d342SRajmohan Mani } 98cc95d342SRajmohan Mani 99cc95d342SRajmohan Mani static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 100cc95d342SRajmohan Mani { 101a69e9972SSakari Ailus pm_runtime_put(sd->dev); 102cc95d342SRajmohan Mani 103cc95d342SRajmohan Mani return 0; 104cc95d342SRajmohan Mani } 105cc95d342SRajmohan Mani 106cc95d342SRajmohan Mani static const struct v4l2_subdev_internal_ops dw9714_int_ops = { 107cc95d342SRajmohan Mani .open = dw9714_open, 108cc95d342SRajmohan Mani .close = dw9714_close, 109cc95d342SRajmohan Mani }; 110cc95d342SRajmohan Mani 111cc95d342SRajmohan Mani static const struct v4l2_subdev_ops dw9714_ops = { }; 112cc95d342SRajmohan Mani 113cc95d342SRajmohan Mani static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev) 114cc95d342SRajmohan Mani { 115cc95d342SRajmohan Mani v4l2_async_unregister_subdev(&dw9714_dev->sd); 116cc95d342SRajmohan Mani v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm); 117cc95d342SRajmohan Mani media_entity_cleanup(&dw9714_dev->sd.entity); 118cc95d342SRajmohan Mani } 119cc95d342SRajmohan Mani 120cc95d342SRajmohan Mani static int dw9714_init_controls(struct dw9714_device *dev_vcm) 121cc95d342SRajmohan Mani { 122cc95d342SRajmohan Mani struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm; 123cc95d342SRajmohan Mani const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops; 124cc95d342SRajmohan Mani 125cc95d342SRajmohan Mani v4l2_ctrl_handler_init(hdl, 1); 126cc95d342SRajmohan Mani 127cc95d342SRajmohan Mani v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE, 1281a58fbf5SRajmohan Mani 0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, 0); 129cc95d342SRajmohan Mani 130cc95d342SRajmohan Mani if (hdl->error) 131a69e9972SSakari Ailus dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n", 132cc95d342SRajmohan Mani __func__, hdl->error); 133cc95d342SRajmohan Mani dev_vcm->sd.ctrl_handler = hdl; 134cc95d342SRajmohan Mani return hdl->error; 135cc95d342SRajmohan Mani } 136cc95d342SRajmohan Mani 137f758eb23SSakari Ailus static int dw9714_probe(struct i2c_client *client) 138cc95d342SRajmohan Mani { 139cc95d342SRajmohan Mani struct dw9714_device *dw9714_dev; 140cc95d342SRajmohan Mani int rval; 141cc95d342SRajmohan Mani 142cc95d342SRajmohan Mani dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev), 143cc95d342SRajmohan Mani GFP_KERNEL); 144cc95d342SRajmohan Mani if (dw9714_dev == NULL) 145cc95d342SRajmohan Mani return -ENOMEM; 146cc95d342SRajmohan Mani 147cc95d342SRajmohan Mani v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops); 148cc95d342SRajmohan Mani dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 149cc95d342SRajmohan Mani dw9714_dev->sd.internal_ops = &dw9714_int_ops; 150cc95d342SRajmohan Mani 151cc95d342SRajmohan Mani rval = dw9714_init_controls(dw9714_dev); 152cc95d342SRajmohan Mani if (rval) 153cc95d342SRajmohan Mani goto err_cleanup; 154cc95d342SRajmohan Mani 155cc95d342SRajmohan Mani rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL); 156cc95d342SRajmohan Mani if (rval < 0) 157cc95d342SRajmohan Mani goto err_cleanup; 158cc95d342SRajmohan Mani 159cc95d342SRajmohan Mani dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS; 160cc95d342SRajmohan Mani 161cc95d342SRajmohan Mani rval = v4l2_async_register_subdev(&dw9714_dev->sd); 162cc95d342SRajmohan Mani if (rval < 0) 163cc95d342SRajmohan Mani goto err_cleanup; 164cc95d342SRajmohan Mani 165cc95d342SRajmohan Mani pm_runtime_set_active(&client->dev); 166cc95d342SRajmohan Mani pm_runtime_enable(&client->dev); 167fa429334SSakari Ailus pm_runtime_idle(&client->dev); 168cc95d342SRajmohan Mani 169cc95d342SRajmohan Mani return 0; 170cc95d342SRajmohan Mani 171cc95d342SRajmohan Mani err_cleanup: 172cc95d342SRajmohan Mani dw9714_subdev_cleanup(dw9714_dev); 173cc95d342SRajmohan Mani dev_err(&client->dev, "Probe failed: %d\n", rval); 174cc95d342SRajmohan Mani return rval; 175cc95d342SRajmohan Mani } 176cc95d342SRajmohan Mani 177cc95d342SRajmohan Mani static int dw9714_remove(struct i2c_client *client) 178cc95d342SRajmohan Mani { 179cc95d342SRajmohan Mani struct v4l2_subdev *sd = i2c_get_clientdata(client); 180cc95d342SRajmohan Mani struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 181cc95d342SRajmohan Mani 182cc95d342SRajmohan Mani pm_runtime_disable(&client->dev); 183cc95d342SRajmohan Mani dw9714_subdev_cleanup(dw9714_dev); 184cc95d342SRajmohan Mani 185cc95d342SRajmohan Mani return 0; 186cc95d342SRajmohan Mani } 187cc95d342SRajmohan Mani 188cc95d342SRajmohan Mani /* 189cc95d342SRajmohan Mani * This function sets the vcm position, so it consumes least current 190cc95d342SRajmohan Mani * The lens position is gradually moved in units of DW9714_CTRL_STEPS, 191cc95d342SRajmohan Mani * to make the movements smoothly. 192cc95d342SRajmohan Mani */ 193cc95d342SRajmohan Mani static int __maybe_unused dw9714_vcm_suspend(struct device *dev) 194cc95d342SRajmohan Mani { 195cc95d342SRajmohan Mani struct i2c_client *client = to_i2c_client(dev); 196cc95d342SRajmohan Mani struct v4l2_subdev *sd = i2c_get_clientdata(client); 197cc95d342SRajmohan Mani struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 198cc95d342SRajmohan Mani int ret, val; 199cc95d342SRajmohan Mani 200cc95d342SRajmohan Mani for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1); 201cc95d342SRajmohan Mani val >= 0; val -= DW9714_CTRL_STEPS) { 202cc95d342SRajmohan Mani ret = dw9714_i2c_write(client, 203cc95d342SRajmohan Mani DW9714_VAL(val, DW9714_DEFAULT_S)); 204cc95d342SRajmohan Mani if (ret) 205cc95d342SRajmohan Mani dev_err_once(dev, "%s I2C failure: %d", __func__, ret); 206cc95d342SRajmohan Mani usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); 207cc95d342SRajmohan Mani } 208cc95d342SRajmohan Mani return 0; 209cc95d342SRajmohan Mani } 210cc95d342SRajmohan Mani 211cc95d342SRajmohan Mani /* 212cc95d342SRajmohan Mani * This function sets the vcm position to the value set by the user 213cc95d342SRajmohan Mani * through v4l2_ctrl_ops s_ctrl handler 214cc95d342SRajmohan Mani * The lens position is gradually moved in units of DW9714_CTRL_STEPS, 215cc95d342SRajmohan Mani * to make the movements smoothly. 216cc95d342SRajmohan Mani */ 217cc95d342SRajmohan Mani static int __maybe_unused dw9714_vcm_resume(struct device *dev) 218cc95d342SRajmohan Mani { 219cc95d342SRajmohan Mani struct i2c_client *client = to_i2c_client(dev); 220cc95d342SRajmohan Mani struct v4l2_subdev *sd = i2c_get_clientdata(client); 221cc95d342SRajmohan Mani struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 222cc95d342SRajmohan Mani int ret, val; 223cc95d342SRajmohan Mani 224cc95d342SRajmohan Mani for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS; 225cc95d342SRajmohan Mani val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1; 226cc95d342SRajmohan Mani val += DW9714_CTRL_STEPS) { 227cc95d342SRajmohan Mani ret = dw9714_i2c_write(client, 228cc95d342SRajmohan Mani DW9714_VAL(val, DW9714_DEFAULT_S)); 229cc95d342SRajmohan Mani if (ret) 230cc95d342SRajmohan Mani dev_err_ratelimited(dev, "%s I2C failure: %d", 231cc95d342SRajmohan Mani __func__, ret); 232cc95d342SRajmohan Mani usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); 233cc95d342SRajmohan Mani } 234cc95d342SRajmohan Mani 235cc95d342SRajmohan Mani return 0; 236cc95d342SRajmohan Mani } 237cc95d342SRajmohan Mani 238cc95d342SRajmohan Mani static const struct i2c_device_id dw9714_id_table[] = { 239cc95d342SRajmohan Mani { DW9714_NAME, 0 }, 240f758eb23SSakari Ailus { { 0 } } 241cc95d342SRajmohan Mani }; 242cc95d342SRajmohan Mani MODULE_DEVICE_TABLE(i2c, dw9714_id_table); 243cc95d342SRajmohan Mani 244c2bc8b06SSakari Ailus static const struct of_device_id dw9714_of_table[] = { 245c2bc8b06SSakari Ailus { .compatible = "dongwoon,dw9714" }, 246c2bc8b06SSakari Ailus { { 0 } } 247c2bc8b06SSakari Ailus }; 248c2bc8b06SSakari Ailus MODULE_DEVICE_TABLE(of, dw9714_of_table); 249c2bc8b06SSakari Ailus 250cc95d342SRajmohan Mani static const struct dev_pm_ops dw9714_pm_ops = { 251cc95d342SRajmohan Mani SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume) 252cc95d342SRajmohan Mani SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL) 253cc95d342SRajmohan Mani }; 254cc95d342SRajmohan Mani 255cc95d342SRajmohan Mani static struct i2c_driver dw9714_i2c_driver = { 256cc95d342SRajmohan Mani .driver = { 257cc95d342SRajmohan Mani .name = DW9714_NAME, 258cc95d342SRajmohan Mani .pm = &dw9714_pm_ops, 259c2bc8b06SSakari Ailus .of_match_table = dw9714_of_table, 260cc95d342SRajmohan Mani }, 261f758eb23SSakari Ailus .probe_new = dw9714_probe, 262cc95d342SRajmohan Mani .remove = dw9714_remove, 263cc95d342SRajmohan Mani .id_table = dw9714_id_table, 264cc95d342SRajmohan Mani }; 265cc95d342SRajmohan Mani 266cc95d342SRajmohan Mani module_i2c_driver(dw9714_i2c_driver); 267cc95d342SRajmohan Mani 268cc95d342SRajmohan Mani MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>"); 269cc95d342SRajmohan Mani MODULE_AUTHOR("Jian Xu Zheng <jian.xu.zheng@intel.com>"); 270cc95d342SRajmohan Mani MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>"); 271cc95d342SRajmohan Mani MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>"); 272cc95d342SRajmohan Mani MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>"); 273cc95d342SRajmohan Mani MODULE_DESCRIPTION("DW9714 VCM driver"); 274cc95d342SRajmohan Mani MODULE_LICENSE("GPL v2"); 275