0001
0002
0003
0004 #include <linux/acpi.h>
0005 #include <linux/delay.h>
0006 #include <linux/i2c.h>
0007 #include <linux/module.h>
0008 #include <linux/pm_runtime.h>
0009 #include <media/v4l2-ctrls.h>
0010 #include <media/v4l2-device.h>
0011
0012 #define AK7375_MAX_FOCUS_POS 4095
0013
0014
0015
0016
0017 #define AK7375_FOCUS_STEPS 1
0018
0019
0020
0021
0022
0023
0024 #define AK7375_CTRL_STEPS 64
0025 #define AK7375_CTRL_DELAY_US 1000
0026
0027 #define AK7375_REG_POSITION 0x0
0028 #define AK7375_REG_CONT 0x2
0029 #define AK7375_MODE_ACTIVE 0x0
0030 #define AK7375_MODE_STANDBY 0x40
0031
0032
0033 struct ak7375_device {
0034 struct v4l2_ctrl_handler ctrls_vcm;
0035 struct v4l2_subdev sd;
0036 struct v4l2_ctrl *focus;
0037
0038 bool active;
0039 };
0040
0041 static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
0042 {
0043 return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
0044 }
0045
0046 static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
0047 {
0048 return container_of(subdev, struct ak7375_device, sd);
0049 }
0050
0051 static int ak7375_i2c_write(struct ak7375_device *ak7375,
0052 u8 addr, u16 data, u8 size)
0053 {
0054 struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
0055 u8 buf[3];
0056 int ret;
0057
0058 if (size != 1 && size != 2)
0059 return -EINVAL;
0060 buf[0] = addr;
0061 buf[size] = data & 0xff;
0062 if (size == 2)
0063 buf[1] = (data >> 8) & 0xff;
0064 ret = i2c_master_send(client, (const char *)buf, size + 1);
0065 if (ret < 0)
0066 return ret;
0067 if (ret != size + 1)
0068 return -EIO;
0069
0070 return 0;
0071 }
0072
0073 static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
0074 {
0075 struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
0076
0077 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
0078 return ak7375_i2c_write(dev_vcm, AK7375_REG_POSITION,
0079 ctrl->val << 4, 2);
0080
0081 return -EINVAL;
0082 }
0083
0084 static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
0085 .s_ctrl = ak7375_set_ctrl,
0086 };
0087
0088 static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
0089 {
0090 return pm_runtime_resume_and_get(sd->dev);
0091 }
0092
0093 static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
0094 {
0095 pm_runtime_put(sd->dev);
0096
0097 return 0;
0098 }
0099
0100 static const struct v4l2_subdev_internal_ops ak7375_int_ops = {
0101 .open = ak7375_open,
0102 .close = ak7375_close,
0103 };
0104
0105 static const struct v4l2_subdev_ops ak7375_ops = { };
0106
0107 static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
0108 {
0109 v4l2_async_unregister_subdev(&ak7375_dev->sd);
0110 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
0111 media_entity_cleanup(&ak7375_dev->sd.entity);
0112 }
0113
0114 static int ak7375_init_controls(struct ak7375_device *dev_vcm)
0115 {
0116 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
0117 const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
0118
0119 v4l2_ctrl_handler_init(hdl, 1);
0120
0121 dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
0122 0, AK7375_MAX_FOCUS_POS, AK7375_FOCUS_STEPS, 0);
0123
0124 if (hdl->error)
0125 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
0126 __func__, hdl->error);
0127 dev_vcm->sd.ctrl_handler = hdl;
0128
0129 return hdl->error;
0130 }
0131
0132 static int ak7375_probe(struct i2c_client *client)
0133 {
0134 struct ak7375_device *ak7375_dev;
0135 int ret;
0136
0137 ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
0138 GFP_KERNEL);
0139 if (!ak7375_dev)
0140 return -ENOMEM;
0141
0142 v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
0143 ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
0144 ak7375_dev->sd.internal_ops = &ak7375_int_ops;
0145 ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS;
0146
0147 ret = ak7375_init_controls(ak7375_dev);
0148 if (ret)
0149 goto err_cleanup;
0150
0151 ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
0152 if (ret < 0)
0153 goto err_cleanup;
0154
0155 ret = v4l2_async_register_subdev(&ak7375_dev->sd);
0156 if (ret < 0)
0157 goto err_cleanup;
0158
0159 pm_runtime_set_active(&client->dev);
0160 pm_runtime_enable(&client->dev);
0161 pm_runtime_idle(&client->dev);
0162
0163 return 0;
0164
0165 err_cleanup:
0166 v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
0167 media_entity_cleanup(&ak7375_dev->sd.entity);
0168
0169 return ret;
0170 }
0171
0172 static int ak7375_remove(struct i2c_client *client)
0173 {
0174 struct v4l2_subdev *sd = i2c_get_clientdata(client);
0175 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
0176
0177 ak7375_subdev_cleanup(ak7375_dev);
0178 pm_runtime_disable(&client->dev);
0179 pm_runtime_set_suspended(&client->dev);
0180
0181 return 0;
0182 }
0183
0184
0185
0186
0187
0188
0189 static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
0190 {
0191 struct v4l2_subdev *sd = dev_get_drvdata(dev);
0192 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
0193 int ret, val;
0194
0195 if (!ak7375_dev->active)
0196 return 0;
0197
0198 for (val = ak7375_dev->focus->val & ~(AK7375_CTRL_STEPS - 1);
0199 val >= 0; val -= AK7375_CTRL_STEPS) {
0200 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
0201 val << 4, 2);
0202 if (ret)
0203 dev_err_once(dev, "%s I2C failure: %d\n",
0204 __func__, ret);
0205 usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
0206 }
0207
0208 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
0209 AK7375_MODE_STANDBY, 1);
0210 if (ret)
0211 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
0212
0213 ak7375_dev->active = false;
0214
0215 return 0;
0216 }
0217
0218
0219
0220
0221
0222
0223
0224 static int __maybe_unused ak7375_vcm_resume(struct device *dev)
0225 {
0226 struct v4l2_subdev *sd = dev_get_drvdata(dev);
0227 struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
0228 int ret, val;
0229
0230 if (ak7375_dev->active)
0231 return 0;
0232
0233 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
0234 AK7375_MODE_ACTIVE, 1);
0235 if (ret) {
0236 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
0237 return ret;
0238 }
0239
0240 for (val = ak7375_dev->focus->val % AK7375_CTRL_STEPS;
0241 val <= ak7375_dev->focus->val;
0242 val += AK7375_CTRL_STEPS) {
0243 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_POSITION,
0244 val << 4, 2);
0245 if (ret)
0246 dev_err_ratelimited(dev, "%s I2C failure: %d\n",
0247 __func__, ret);
0248 usleep_range(AK7375_CTRL_DELAY_US, AK7375_CTRL_DELAY_US + 10);
0249 }
0250
0251 ak7375_dev->active = true;
0252
0253 return 0;
0254 }
0255
0256 static const struct of_device_id ak7375_of_table[] = {
0257 { .compatible = "asahi-kasei,ak7375" },
0258 { }
0259 };
0260 MODULE_DEVICE_TABLE(of, ak7375_of_table);
0261
0262 static const struct dev_pm_ops ak7375_pm_ops = {
0263 SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume)
0264 SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL)
0265 };
0266
0267 static struct i2c_driver ak7375_i2c_driver = {
0268 .driver = {
0269 .name = "ak7375",
0270 .pm = &ak7375_pm_ops,
0271 .of_match_table = ak7375_of_table,
0272 },
0273 .probe_new = ak7375_probe,
0274 .remove = ak7375_remove,
0275 };
0276 module_i2c_driver(ak7375_i2c_driver);
0277
0278 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
0279 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
0280 MODULE_DESCRIPTION("AK7375 VCM driver");
0281 MODULE_LICENSE("GPL v2");