Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * LED driver for Richtek RT8515 flash/torch white LEDs
0004  * found on some Samsung mobile phones.
0005  *
0006  * This is a 1.5A Boost dual channel driver produced around 2011.
0007  *
0008  * The component lacks a datasheet, but in the schematic picture
0009  * from the LG P970 service manual you can see the connections
0010  * from the RT8515 to the LED, with two resistors connected
0011  * from the pins "RFS" and "RTS" to ground.
0012  *
0013  * On the LG P970:
0014  * RFS (resistance flash setting?) is 20 kOhm
0015  * RTS (resistance torch setting?) is 39 kOhm
0016  *
0017  * Some sleuthing finds us the RT9387A which we have a datasheet for:
0018  * https://static5.arrow.com/pdfs/2014/7/27/8/21/12/794/rtt_/manual/94download_ds.jspprt9387a.jspprt9387a.pdf
0019  * This apparently works the same way so in theory this driver
0020  * should cover RT9387A as well. This has not been tested, please
0021  * update the compatibles if you add RT9387A support.
0022  *
0023  * Linus Walleij <linus.walleij@linaro.org>
0024  */
0025 #include <linux/delay.h>
0026 #include <linux/err.h>
0027 #include <linux/gpio/consumer.h>
0028 #include <linux/led-class-flash.h>
0029 #include <linux/mod_devicetable.h>
0030 #include <linux/module.h>
0031 #include <linux/platform_device.h>
0032 #include <linux/property.h>
0033 #include <linux/regulator/consumer.h>
0034 
0035 #include <media/v4l2-flash-led-class.h>
0036 
0037 /* We can provide 15-700 mA out to the LED */
0038 #define RT8515_MIN_IOUT_MA  15
0039 #define RT8515_MAX_IOUT_MA  700
0040 /* The maximum intensity is 1-16 for flash and 1-100 for torch */
0041 #define RT8515_FLASH_MAX    16
0042 #define RT8515_TORCH_MAX    100
0043 
0044 #define RT8515_TIMEOUT_US   250000U
0045 #define RT8515_MAX_TIMEOUT_US   300000U
0046 
0047 struct rt8515 {
0048     struct led_classdev_flash fled;
0049     struct device *dev;
0050     struct v4l2_flash *v4l2_flash;
0051     struct mutex lock;
0052     struct regulator *reg;
0053     struct gpio_desc *enable_torch;
0054     struct gpio_desc *enable_flash;
0055     struct timer_list powerdown_timer;
0056     u32 max_timeout; /* Flash max timeout */
0057     int flash_max_intensity;
0058     int torch_max_intensity;
0059 };
0060 
0061 static struct rt8515 *to_rt8515(struct led_classdev_flash *fled)
0062 {
0063     return container_of(fled, struct rt8515, fled);
0064 }
0065 
0066 static void rt8515_gpio_led_off(struct rt8515 *rt)
0067 {
0068     gpiod_set_value(rt->enable_flash, 0);
0069     gpiod_set_value(rt->enable_torch, 0);
0070 }
0071 
0072 static void rt8515_gpio_brightness_commit(struct gpio_desc *gpiod,
0073                       int brightness)
0074 {
0075     int i;
0076 
0077     /*
0078      * Toggling a GPIO line with a small delay increases the
0079      * brightness one step at a time.
0080      */
0081     for (i = 0; i < brightness; i++) {
0082         gpiod_set_value(gpiod, 0);
0083         udelay(1);
0084         gpiod_set_value(gpiod, 1);
0085         udelay(1);
0086     }
0087 }
0088 
0089 /* This is setting the torch light level */
0090 static int rt8515_led_brightness_set(struct led_classdev *led,
0091                      enum led_brightness brightness)
0092 {
0093     struct led_classdev_flash *fled = lcdev_to_flcdev(led);
0094     struct rt8515 *rt = to_rt8515(fled);
0095 
0096     mutex_lock(&rt->lock);
0097 
0098     if (brightness == LED_OFF) {
0099         /* Off */
0100         rt8515_gpio_led_off(rt);
0101     } else if (brightness < RT8515_TORCH_MAX) {
0102         /* Step it up to movie mode brightness using the flash pin */
0103         rt8515_gpio_brightness_commit(rt->enable_torch, brightness);
0104     } else {
0105         /* Max torch brightness requested */
0106         gpiod_set_value(rt->enable_torch, 1);
0107     }
0108 
0109     mutex_unlock(&rt->lock);
0110 
0111     return 0;
0112 }
0113 
0114 static int rt8515_led_flash_strobe_set(struct led_classdev_flash *fled,
0115                        bool state)
0116 {
0117     struct rt8515 *rt = to_rt8515(fled);
0118     struct led_flash_setting *timeout = &fled->timeout;
0119     int brightness = rt->flash_max_intensity;
0120 
0121     mutex_lock(&rt->lock);
0122 
0123     if (state) {
0124         /* Enable LED flash mode and set brightness */
0125         rt8515_gpio_brightness_commit(rt->enable_flash, brightness);
0126         /* Set timeout */
0127         mod_timer(&rt->powerdown_timer,
0128               jiffies + usecs_to_jiffies(timeout->val));
0129     } else {
0130         del_timer_sync(&rt->powerdown_timer);
0131         /* Turn the LED off */
0132         rt8515_gpio_led_off(rt);
0133     }
0134 
0135     fled->led_cdev.brightness = LED_OFF;
0136     /* After this the torch LED will be disabled */
0137 
0138     mutex_unlock(&rt->lock);
0139 
0140     return 0;
0141 }
0142 
0143 static int rt8515_led_flash_strobe_get(struct led_classdev_flash *fled,
0144                        bool *state)
0145 {
0146     struct rt8515 *rt = to_rt8515(fled);
0147 
0148     *state = timer_pending(&rt->powerdown_timer);
0149 
0150     return 0;
0151 }
0152 
0153 static int rt8515_led_flash_timeout_set(struct led_classdev_flash *fled,
0154                     u32 timeout)
0155 {
0156     /* The timeout is stored in the led-class-flash core */
0157     return 0;
0158 }
0159 
0160 static const struct led_flash_ops rt8515_flash_ops = {
0161     .strobe_set = rt8515_led_flash_strobe_set,
0162     .strobe_get = rt8515_led_flash_strobe_get,
0163     .timeout_set = rt8515_led_flash_timeout_set,
0164 };
0165 
0166 static void rt8515_powerdown_timer(struct timer_list *t)
0167 {
0168     struct rt8515 *rt = from_timer(rt, t, powerdown_timer);
0169 
0170     /* Turn the LED off */
0171     rt8515_gpio_led_off(rt);
0172 }
0173 
0174 static void rt8515_init_flash_timeout(struct rt8515 *rt)
0175 {
0176     struct led_classdev_flash *fled = &rt->fled;
0177     struct led_flash_setting *s;
0178 
0179     /* Init flash timeout setting */
0180     s = &fled->timeout;
0181     s->min = 1;
0182     s->max = rt->max_timeout;
0183     s->step = 1;
0184     /*
0185      * Set default timeout to RT8515_TIMEOUT_US except if
0186      * max_timeout from DT is lower.
0187      */
0188     s->val = min(rt->max_timeout, RT8515_TIMEOUT_US);
0189 }
0190 
0191 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
0192 /* Configure the V2L2 flash subdevice */
0193 static void rt8515_init_v4l2_flash_config(struct rt8515 *rt,
0194                       struct v4l2_flash_config *v4l2_sd_cfg)
0195 {
0196     struct led_classdev *led = &rt->fled.led_cdev;
0197     struct led_flash_setting *s;
0198 
0199     strscpy(v4l2_sd_cfg->dev_name, led->dev->kobj.name,
0200         sizeof(v4l2_sd_cfg->dev_name));
0201 
0202     /*
0203      * Init flash intensity setting: this is a linear scale
0204      * capped from the device tree max intensity setting
0205      * 1..flash_max_intensity
0206      */
0207     s = &v4l2_sd_cfg->intensity;
0208     s->min = 1;
0209     s->max = rt->flash_max_intensity;
0210     s->step = 1;
0211     s->val = s->max;
0212 }
0213 
0214 static void rt8515_v4l2_flash_release(struct rt8515 *rt)
0215 {
0216     v4l2_flash_release(rt->v4l2_flash);
0217 }
0218 
0219 #else
0220 static void rt8515_init_v4l2_flash_config(struct rt8515 *rt,
0221                       struct v4l2_flash_config *v4l2_sd_cfg)
0222 {
0223 }
0224 
0225 static void rt8515_v4l2_flash_release(struct rt8515 *rt)
0226 {
0227 }
0228 #endif
0229 
0230 static void rt8515_determine_max_intensity(struct rt8515 *rt,
0231                        struct fwnode_handle *led,
0232                        const char *resistance,
0233                        const char *max_ua_prop, int hw_max,
0234                        int *max_intensity_setting)
0235 {
0236     u32 res = 0; /* Can't be 0 so 0 is undefined */
0237     u32 ua;
0238     u32 max_ma;
0239     int max_intensity;
0240     int ret;
0241 
0242     fwnode_property_read_u32(rt->dev->fwnode, resistance, &res);
0243     ret = fwnode_property_read_u32(led, max_ua_prop, &ua);
0244 
0245     /* Missing info in DT, OK go with hardware maxima */
0246     if (ret || res == 0) {
0247         dev_err(rt->dev,
0248             "either %s or %s missing from DT, using HW max\n",
0249             resistance, max_ua_prop);
0250         max_ma = RT8515_MAX_IOUT_MA;
0251         max_intensity = hw_max;
0252         goto out_assign_max;
0253     }
0254 
0255     /*
0256      * Formula from the datasheet, this is the maximum current
0257      * defined by the hardware.
0258      */
0259     max_ma = (5500 * 1000) / res;
0260     /*
0261      * Calculate max intensity (linear scaling)
0262      * Formula is ((ua / 1000) / max_ma) * 100, then simplified
0263      */
0264     max_intensity = (ua / 10) / max_ma;
0265 
0266     dev_info(rt->dev,
0267          "current restricted from %u to %u mA, max intensity %d/100\n",
0268          max_ma, (ua / 1000), max_intensity);
0269 
0270 out_assign_max:
0271     dev_info(rt->dev, "max intensity %d/%d = %d mA\n",
0272          max_intensity, hw_max, max_ma);
0273     *max_intensity_setting = max_intensity;
0274 }
0275 
0276 static int rt8515_probe(struct platform_device *pdev)
0277 {
0278     struct device *dev = &pdev->dev;
0279     struct fwnode_handle *child;
0280     struct rt8515 *rt;
0281     struct led_classdev *led;
0282     struct led_classdev_flash *fled;
0283     struct led_init_data init_data = {};
0284     struct v4l2_flash_config v4l2_sd_cfg = {};
0285     int ret;
0286 
0287     rt = devm_kzalloc(dev, sizeof(*rt), GFP_KERNEL);
0288     if (!rt)
0289         return -ENOMEM;
0290 
0291     rt->dev = dev;
0292     fled = &rt->fled;
0293     led = &fled->led_cdev;
0294 
0295     /* ENF - Enable Flash line */
0296     rt->enable_flash = devm_gpiod_get(dev, "enf", GPIOD_OUT_LOW);
0297     if (IS_ERR(rt->enable_flash))
0298         return dev_err_probe(dev, PTR_ERR(rt->enable_flash),
0299                      "cannot get ENF (enable flash) GPIO\n");
0300 
0301     /* ENT - Enable Torch line */
0302     rt->enable_torch = devm_gpiod_get(dev, "ent", GPIOD_OUT_LOW);
0303     if (IS_ERR(rt->enable_torch))
0304         return dev_err_probe(dev, PTR_ERR(rt->enable_torch),
0305                      "cannot get ENT (enable torch) GPIO\n");
0306 
0307     child = fwnode_get_next_available_child_node(dev->fwnode, NULL);
0308     if (!child) {
0309         dev_err(dev,
0310             "No fwnode child node found for connected LED.\n");
0311         return -EINVAL;
0312     }
0313     init_data.fwnode = child;
0314 
0315     rt8515_determine_max_intensity(rt, child, "richtek,rfs-ohms",
0316                        "flash-max-microamp",
0317                        RT8515_FLASH_MAX,
0318                        &rt->flash_max_intensity);
0319     rt8515_determine_max_intensity(rt, child, "richtek,rts-ohms",
0320                        "led-max-microamp",
0321                        RT8515_TORCH_MAX,
0322                        &rt->torch_max_intensity);
0323 
0324     ret = fwnode_property_read_u32(child, "flash-max-timeout-us",
0325                        &rt->max_timeout);
0326     if (ret) {
0327         rt->max_timeout = RT8515_MAX_TIMEOUT_US;
0328         dev_warn(dev,
0329              "flash-max-timeout-us property missing\n");
0330     }
0331     timer_setup(&rt->powerdown_timer, rt8515_powerdown_timer, 0);
0332     rt8515_init_flash_timeout(rt);
0333 
0334     fled->ops = &rt8515_flash_ops;
0335 
0336     led->max_brightness = rt->torch_max_intensity;
0337     led->brightness_set_blocking = rt8515_led_brightness_set;
0338     led->flags |= LED_CORE_SUSPENDRESUME | LED_DEV_CAP_FLASH;
0339 
0340     mutex_init(&rt->lock);
0341 
0342     platform_set_drvdata(pdev, rt);
0343 
0344     ret = devm_led_classdev_flash_register_ext(dev, fled, &init_data);
0345     if (ret) {
0346         fwnode_handle_put(child);
0347         mutex_destroy(&rt->lock);
0348         dev_err(dev, "can't register LED %s\n", led->name);
0349         return ret;
0350     }
0351 
0352     rt8515_init_v4l2_flash_config(rt, &v4l2_sd_cfg);
0353 
0354     /* Create a V4L2 Flash device if V4L2 flash is enabled */
0355     rt->v4l2_flash = v4l2_flash_init(dev, child, fled, NULL, &v4l2_sd_cfg);
0356     if (IS_ERR(rt->v4l2_flash)) {
0357         ret = PTR_ERR(rt->v4l2_flash);
0358         dev_err(dev, "failed to register V4L2 flash device (%d)\n",
0359             ret);
0360         /*
0361          * Continue without the V4L2 flash
0362          * (we still have the classdev)
0363          */
0364     }
0365 
0366     fwnode_handle_put(child);
0367     return 0;
0368 }
0369 
0370 static int rt8515_remove(struct platform_device *pdev)
0371 {
0372     struct rt8515 *rt = platform_get_drvdata(pdev);
0373 
0374     rt8515_v4l2_flash_release(rt);
0375     del_timer_sync(&rt->powerdown_timer);
0376     mutex_destroy(&rt->lock);
0377 
0378     return 0;
0379 }
0380 
0381 static const struct of_device_id rt8515_match[] = {
0382     { .compatible = "richtek,rt8515", },
0383     { /* sentinel */ }
0384 };
0385 MODULE_DEVICE_TABLE(of, rt8515_match);
0386 
0387 static struct platform_driver rt8515_driver = {
0388     .driver = {
0389         .name  = "rt8515",
0390         .of_match_table = rt8515_match,
0391     },
0392     .probe  = rt8515_probe,
0393     .remove = rt8515_remove,
0394 };
0395 module_platform_driver(rt8515_driver);
0396 
0397 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
0398 MODULE_DESCRIPTION("Richtek RT8515 LED driver");
0399 MODULE_LICENSE("GPL");