Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * drivers/media/i2c/adp1653.c
0004  *
0005  * Copyright (C) 2008--2011 Nokia Corporation
0006  *
0007  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
0008  *
0009  * Contributors:
0010  *  Sakari Ailus <sakari.ailus@iki.fi>
0011  *  Tuukka Toivonen <tuukkat76@gmail.com>
0012  *  Pavel Machek <pavel@ucw.cz>
0013  *
0014  * TODO:
0015  * - fault interrupt handling
0016  * - hardware strobe
0017  * - power doesn't need to be ON if all lights are off
0018  */
0019 
0020 #include <linux/delay.h>
0021 #include <linux/module.h>
0022 #include <linux/i2c.h>
0023 #include <linux/slab.h>
0024 #include <linux/of.h>
0025 #include <linux/gpio/consumer.h>
0026 #include <media/i2c/adp1653.h>
0027 #include <media/v4l2-device.h>
0028 
0029 #define TIMEOUT_MAX     820000
0030 #define TIMEOUT_STEP        54600
0031 #define TIMEOUT_MIN     (TIMEOUT_MAX - ADP1653_REG_CONFIG_TMR_SET_MAX \
0032                  * TIMEOUT_STEP)
0033 #define TIMEOUT_US_TO_CODE(t)   ((TIMEOUT_MAX + (TIMEOUT_STEP / 2) - (t)) \
0034                  / TIMEOUT_STEP)
0035 #define TIMEOUT_CODE_TO_US(c)   (TIMEOUT_MAX - (c) * TIMEOUT_STEP)
0036 
0037 /* Write values into ADP1653 registers. */
0038 static int adp1653_update_hw(struct adp1653_flash *flash)
0039 {
0040     struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
0041     u8 out_sel;
0042     u8 config = 0;
0043     int rval;
0044 
0045     out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
0046         flash->indicator_intensity->val)
0047         << ADP1653_REG_OUT_SEL_ILED_SHIFT;
0048 
0049     switch (flash->led_mode->val) {
0050     case V4L2_FLASH_LED_MODE_NONE:
0051         break;
0052     case V4L2_FLASH_LED_MODE_FLASH:
0053         /* Flash mode, light on with strobe, duration from timer */
0054         config = ADP1653_REG_CONFIG_TMR_CFG;
0055         config |= TIMEOUT_US_TO_CODE(flash->flash_timeout->val)
0056               << ADP1653_REG_CONFIG_TMR_SET_SHIFT;
0057         break;
0058     case V4L2_FLASH_LED_MODE_TORCH:
0059         /* Torch mode, light immediately on, duration indefinite */
0060         out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
0061             flash->torch_intensity->val)
0062             << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
0063         break;
0064     }
0065 
0066     rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
0067     if (rval < 0)
0068         return rval;
0069 
0070     rval = i2c_smbus_write_byte_data(client, ADP1653_REG_CONFIG, config);
0071     if (rval < 0)
0072         return rval;
0073 
0074     return 0;
0075 }
0076 
0077 static int adp1653_get_fault(struct adp1653_flash *flash)
0078 {
0079     struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
0080     int fault;
0081     int rval;
0082 
0083     fault = i2c_smbus_read_byte_data(client, ADP1653_REG_FAULT);
0084     if (fault < 0)
0085         return fault;
0086 
0087     flash->fault |= fault;
0088 
0089     if (!flash->fault)
0090         return 0;
0091 
0092     /* Clear faults. */
0093     rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
0094     if (rval < 0)
0095         return rval;
0096 
0097     flash->led_mode->val = V4L2_FLASH_LED_MODE_NONE;
0098 
0099     rval = adp1653_update_hw(flash);
0100     if (rval)
0101         return rval;
0102 
0103     return flash->fault;
0104 }
0105 
0106 static int adp1653_strobe(struct adp1653_flash *flash, int enable)
0107 {
0108     struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
0109     u8 out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
0110         flash->indicator_intensity->val)
0111         << ADP1653_REG_OUT_SEL_ILED_SHIFT;
0112     int rval;
0113 
0114     if (flash->led_mode->val != V4L2_FLASH_LED_MODE_FLASH)
0115         return -EBUSY;
0116 
0117     if (!enable)
0118         return i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL,
0119                          out_sel);
0120 
0121     out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
0122         flash->flash_intensity->val)
0123         << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
0124     rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
0125     if (rval)
0126         return rval;
0127 
0128     /* Software strobe using i2c */
0129     rval = i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE,
0130         ADP1653_REG_SW_STROBE_SW_STROBE);
0131     if (rval)
0132         return rval;
0133     return i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE, 0);
0134 }
0135 
0136 /* --------------------------------------------------------------------------
0137  * V4L2 controls
0138  */
0139 
0140 static int adp1653_get_ctrl(struct v4l2_ctrl *ctrl)
0141 {
0142     struct adp1653_flash *flash =
0143         container_of(ctrl->handler, struct adp1653_flash, ctrls);
0144     int rval;
0145 
0146     rval = adp1653_get_fault(flash);
0147     if (rval)
0148         return rval;
0149 
0150     ctrl->cur.val = 0;
0151 
0152     if (flash->fault & ADP1653_REG_FAULT_FLT_SCP)
0153         ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
0154     if (flash->fault & ADP1653_REG_FAULT_FLT_OT)
0155         ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
0156     if (flash->fault & ADP1653_REG_FAULT_FLT_TMR)
0157         ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
0158     if (flash->fault & ADP1653_REG_FAULT_FLT_OV)
0159         ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
0160 
0161     flash->fault = 0;
0162 
0163     return 0;
0164 }
0165 
0166 static int adp1653_set_ctrl(struct v4l2_ctrl *ctrl)
0167 {
0168     struct adp1653_flash *flash =
0169         container_of(ctrl->handler, struct adp1653_flash, ctrls);
0170     int rval;
0171 
0172     rval = adp1653_get_fault(flash);
0173     if (rval)
0174         return rval;
0175     if ((rval & (ADP1653_REG_FAULT_FLT_SCP |
0176              ADP1653_REG_FAULT_FLT_OT |
0177              ADP1653_REG_FAULT_FLT_OV)) &&
0178         (ctrl->id == V4L2_CID_FLASH_STROBE ||
0179          ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
0180          ctrl->id == V4L2_CID_FLASH_LED_MODE))
0181         return -EBUSY;
0182 
0183     switch (ctrl->id) {
0184     case V4L2_CID_FLASH_STROBE:
0185         return adp1653_strobe(flash, 1);
0186     case V4L2_CID_FLASH_STROBE_STOP:
0187         return adp1653_strobe(flash, 0);
0188     }
0189 
0190     return adp1653_update_hw(flash);
0191 }
0192 
0193 static const struct v4l2_ctrl_ops adp1653_ctrl_ops = {
0194     .g_volatile_ctrl = adp1653_get_ctrl,
0195     .s_ctrl = adp1653_set_ctrl,
0196 };
0197 
0198 static int adp1653_init_controls(struct adp1653_flash *flash)
0199 {
0200     struct v4l2_ctrl *fault;
0201 
0202     v4l2_ctrl_handler_init(&flash->ctrls, 9);
0203 
0204     flash->led_mode =
0205         v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
0206                        V4L2_CID_FLASH_LED_MODE,
0207                        V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
0208     v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
0209                    V4L2_CID_FLASH_STROBE_SOURCE,
0210                    V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
0211     v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
0212               V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
0213     v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
0214               V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
0215     flash->flash_timeout =
0216         v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
0217                   V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
0218                   flash->platform_data->max_flash_timeout,
0219                   TIMEOUT_STEP,
0220                   flash->platform_data->max_flash_timeout);
0221     flash->flash_intensity =
0222         v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
0223                   V4L2_CID_FLASH_INTENSITY,
0224                   ADP1653_FLASH_INTENSITY_MIN,
0225                   flash->platform_data->max_flash_intensity,
0226                   1, flash->platform_data->max_flash_intensity);
0227     flash->torch_intensity =
0228         v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
0229                   V4L2_CID_FLASH_TORCH_INTENSITY,
0230                   ADP1653_TORCH_INTENSITY_MIN,
0231                   flash->platform_data->max_torch_intensity,
0232                   ADP1653_FLASH_INTENSITY_STEP,
0233                   flash->platform_data->max_torch_intensity);
0234     flash->indicator_intensity =
0235         v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
0236                   V4L2_CID_FLASH_INDICATOR_INTENSITY,
0237                   ADP1653_INDICATOR_INTENSITY_MIN,
0238                   flash->platform_data->max_indicator_intensity,
0239                   ADP1653_INDICATOR_INTENSITY_STEP,
0240                   ADP1653_INDICATOR_INTENSITY_MIN);
0241     fault = v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
0242                   V4L2_CID_FLASH_FAULT, 0,
0243                   V4L2_FLASH_FAULT_OVER_VOLTAGE
0244                   | V4L2_FLASH_FAULT_OVER_TEMPERATURE
0245                   | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
0246 
0247     if (flash->ctrls.error)
0248         return flash->ctrls.error;
0249 
0250     fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
0251 
0252     flash->subdev.ctrl_handler = &flash->ctrls;
0253     return 0;
0254 }
0255 
0256 /* --------------------------------------------------------------------------
0257  * V4L2 subdev operations
0258  */
0259 
0260 static int
0261 adp1653_init_device(struct adp1653_flash *flash)
0262 {
0263     struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
0264     int rval;
0265 
0266     /* Clear FAULT register by writing zero to OUT_SEL */
0267     rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
0268     if (rval < 0) {
0269         dev_err(&client->dev, "failed writing fault register\n");
0270         return -EIO;
0271     }
0272 
0273     mutex_lock(flash->ctrls.lock);
0274     /* Reset faults before reading new ones. */
0275     flash->fault = 0;
0276     rval = adp1653_get_fault(flash);
0277     mutex_unlock(flash->ctrls.lock);
0278     if (rval > 0) {
0279         dev_err(&client->dev, "faults detected: 0x%1.1x\n", rval);
0280         return -EIO;
0281     }
0282 
0283     mutex_lock(flash->ctrls.lock);
0284     rval = adp1653_update_hw(flash);
0285     mutex_unlock(flash->ctrls.lock);
0286     if (rval) {
0287         dev_err(&client->dev,
0288             "adp1653_update_hw failed at %s\n", __func__);
0289         return -EIO;
0290     }
0291 
0292     return 0;
0293 }
0294 
0295 static int
0296 __adp1653_set_power(struct adp1653_flash *flash, int on)
0297 {
0298     int ret;
0299 
0300     if (flash->platform_data->power) {
0301         ret = flash->platform_data->power(&flash->subdev, on);
0302         if (ret < 0)
0303             return ret;
0304     } else {
0305         gpiod_set_value(flash->platform_data->enable_gpio, on);
0306         if (on)
0307             /* Some delay is apparently required. */
0308             udelay(20);
0309     }
0310 
0311     if (!on)
0312         return 0;
0313 
0314     ret = adp1653_init_device(flash);
0315     if (ret >= 0)
0316         return ret;
0317 
0318     if (flash->platform_data->power)
0319         flash->platform_data->power(&flash->subdev, 0);
0320     else
0321         gpiod_set_value(flash->platform_data->enable_gpio, 0);
0322 
0323     return ret;
0324 }
0325 
0326 static int
0327 adp1653_set_power(struct v4l2_subdev *subdev, int on)
0328 {
0329     struct adp1653_flash *flash = to_adp1653_flash(subdev);
0330     int ret = 0;
0331 
0332     mutex_lock(&flash->power_lock);
0333 
0334     /* If the power count is modified from 0 to != 0 or from != 0 to 0,
0335      * update the power state.
0336      */
0337     if (flash->power_count == !on) {
0338         ret = __adp1653_set_power(flash, !!on);
0339         if (ret < 0)
0340             goto done;
0341     }
0342 
0343     /* Update the power count. */
0344     flash->power_count += on ? 1 : -1;
0345     WARN_ON(flash->power_count < 0);
0346 
0347 done:
0348     mutex_unlock(&flash->power_lock);
0349     return ret;
0350 }
0351 
0352 static int adp1653_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
0353 {
0354     return adp1653_set_power(sd, 1);
0355 }
0356 
0357 static int adp1653_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
0358 {
0359     return adp1653_set_power(sd, 0);
0360 }
0361 
0362 static const struct v4l2_subdev_core_ops adp1653_core_ops = {
0363     .s_power = adp1653_set_power,
0364 };
0365 
0366 static const struct v4l2_subdev_ops adp1653_ops = {
0367     .core = &adp1653_core_ops,
0368 };
0369 
0370 static const struct v4l2_subdev_internal_ops adp1653_internal_ops = {
0371     .open = adp1653_open,
0372     .close = adp1653_close,
0373 };
0374 
0375 /* --------------------------------------------------------------------------
0376  * I2C driver
0377  */
0378 #ifdef CONFIG_PM
0379 
0380 static int adp1653_suspend(struct device *dev)
0381 {
0382     struct v4l2_subdev *subdev = dev_get_drvdata(dev);
0383     struct adp1653_flash *flash = to_adp1653_flash(subdev);
0384 
0385     if (!flash->power_count)
0386         return 0;
0387 
0388     return __adp1653_set_power(flash, 0);
0389 }
0390 
0391 static int adp1653_resume(struct device *dev)
0392 {
0393     struct v4l2_subdev *subdev = dev_get_drvdata(dev);
0394     struct adp1653_flash *flash = to_adp1653_flash(subdev);
0395 
0396     if (!flash->power_count)
0397         return 0;
0398 
0399     return __adp1653_set_power(flash, 1);
0400 }
0401 
0402 #else
0403 
0404 #define adp1653_suspend NULL
0405 #define adp1653_resume  NULL
0406 
0407 #endif /* CONFIG_PM */
0408 
0409 static int adp1653_of_init(struct i2c_client *client,
0410                struct adp1653_flash *flash,
0411                struct device_node *node)
0412 {
0413     struct adp1653_platform_data *pd;
0414     struct device_node *child;
0415 
0416     pd = devm_kzalloc(&client->dev, sizeof(*pd), GFP_KERNEL);
0417     if (!pd)
0418         return -ENOMEM;
0419     flash->platform_data = pd;
0420 
0421     child = of_get_child_by_name(node, "flash");
0422     if (!child)
0423         return -EINVAL;
0424 
0425     if (of_property_read_u32(child, "flash-timeout-us",
0426                  &pd->max_flash_timeout))
0427         goto err;
0428 
0429     if (of_property_read_u32(child, "flash-max-microamp",
0430                  &pd->max_flash_intensity))
0431         goto err;
0432 
0433     pd->max_flash_intensity /= 1000;
0434 
0435     if (of_property_read_u32(child, "led-max-microamp",
0436                  &pd->max_torch_intensity))
0437         goto err;
0438 
0439     pd->max_torch_intensity /= 1000;
0440     of_node_put(child);
0441 
0442     child = of_get_child_by_name(node, "indicator");
0443     if (!child)
0444         return -EINVAL;
0445 
0446     if (of_property_read_u32(child, "led-max-microamp",
0447                  &pd->max_indicator_intensity))
0448         goto err;
0449 
0450     of_node_put(child);
0451 
0452     pd->enable_gpio = devm_gpiod_get(&client->dev, "enable", GPIOD_OUT_LOW);
0453     if (IS_ERR(pd->enable_gpio)) {
0454         dev_err(&client->dev, "Error getting GPIO\n");
0455         return PTR_ERR(pd->enable_gpio);
0456     }
0457 
0458     return 0;
0459 err:
0460     dev_err(&client->dev, "Required property not found\n");
0461     of_node_put(child);
0462     return -EINVAL;
0463 }
0464 
0465 
0466 static int adp1653_probe(struct i2c_client *client,
0467              const struct i2c_device_id *devid)
0468 {
0469     struct adp1653_flash *flash;
0470     int ret;
0471 
0472     flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
0473     if (flash == NULL)
0474         return -ENOMEM;
0475 
0476     if (client->dev.of_node) {
0477         ret = adp1653_of_init(client, flash, client->dev.of_node);
0478         if (ret)
0479             return ret;
0480     } else {
0481         if (!client->dev.platform_data) {
0482             dev_err(&client->dev,
0483                 "Neither DT not platform data provided\n");
0484             return -EINVAL;
0485         }
0486         flash->platform_data = client->dev.platform_data;
0487     }
0488 
0489     mutex_init(&flash->power_lock);
0490 
0491     v4l2_i2c_subdev_init(&flash->subdev, client, &adp1653_ops);
0492     flash->subdev.internal_ops = &adp1653_internal_ops;
0493     flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
0494 
0495     ret = adp1653_init_controls(flash);
0496     if (ret)
0497         goto free_and_quit;
0498 
0499     ret = media_entity_pads_init(&flash->subdev.entity, 0, NULL);
0500     if (ret < 0)
0501         goto free_and_quit;
0502 
0503     flash->subdev.entity.function = MEDIA_ENT_F_FLASH;
0504 
0505     return 0;
0506 
0507 free_and_quit:
0508     dev_err(&client->dev, "adp1653: failed to register device\n");
0509     v4l2_ctrl_handler_free(&flash->ctrls);
0510     return ret;
0511 }
0512 
0513 static int adp1653_remove(struct i2c_client *client)
0514 {
0515     struct v4l2_subdev *subdev = i2c_get_clientdata(client);
0516     struct adp1653_flash *flash = to_adp1653_flash(subdev);
0517 
0518     v4l2_device_unregister_subdev(&flash->subdev);
0519     v4l2_ctrl_handler_free(&flash->ctrls);
0520     media_entity_cleanup(&flash->subdev.entity);
0521 
0522     return 0;
0523 }
0524 
0525 static const struct i2c_device_id adp1653_id_table[] = {
0526     { ADP1653_NAME, 0 },
0527     { }
0528 };
0529 MODULE_DEVICE_TABLE(i2c, adp1653_id_table);
0530 
0531 static const struct dev_pm_ops adp1653_pm_ops = {
0532     .suspend    = adp1653_suspend,
0533     .resume     = adp1653_resume,
0534 };
0535 
0536 static struct i2c_driver adp1653_i2c_driver = {
0537     .driver     = {
0538         .name   = ADP1653_NAME,
0539         .pm = &adp1653_pm_ops,
0540     },
0541     .probe      = adp1653_probe,
0542     .remove     = adp1653_remove,
0543     .id_table   = adp1653_id_table,
0544 };
0545 
0546 module_i2c_driver(adp1653_i2c_driver);
0547 
0548 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
0549 MODULE_DESCRIPTION("Analog Devices ADP1653 LED flash driver");
0550 MODULE_LICENSE("GPL");