1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * LED driver for Richtek RT8515 flash/torch white LEDs 4 * found on some Samsung mobile phones. 5 * 6 * This is a 1.5A Boost dual channel driver produced around 2011. 7 * 8 * The component lacks a datasheet, but in the schematic picture 9 * from the LG P970 service manual you can see the connections 10 * from the RT8515 to the LED, with two resistors connected 11 * from the pins "RFS" and "RTS" to ground. 12 * 13 * On the LG P970: 14 * RFS (resistance flash setting?) is 20 kOhm 15 * RTS (resistance torch setting?) is 39 kOhm 16 * 17 * Some sleuthing finds us the RT9387A which we have a datasheet for: 18 * https://static5.arrow.com/pdfs/2014/7/27/8/21/12/794/rtt_/manual/94download_ds.jspprt9387a.jspprt9387a.pdf 19 * This apparently works the same way so in theory this driver 20 * should cover RT9387A as well. This has not been tested, please 21 * update the compatibles if you add RT9387A support. 22 * 23 * Linus Walleij <linus.walleij@linaro.org> 24 */ 25 #include <linux/delay.h> 26 #include <linux/err.h> 27 #include <linux/gpio/consumer.h> 28 #include <linux/led-class-flash.h> 29 #include <linux/module.h> 30 #include <linux/platform_device.h> 31 #include <linux/property.h> 32 #include <linux/regulator/consumer.h> 33 34 #include <media/v4l2-flash-led-class.h> 35 36 /* We can provide 15-700 mA out to the LED */ 37 #define RT8515_MIN_IOUT_MA 15 38 #define RT8515_MAX_IOUT_MA 700 39 /* The maximum intensity is 1-16 for flash and 1-100 for torch */ 40 #define RT8515_FLASH_MAX 16 41 #define RT8515_TORCH_MAX 100 42 43 #define RT8515_TIMEOUT_US 250000U 44 #define RT8515_MAX_TIMEOUT_US 300000U 45 46 struct rt8515 { 47 struct led_classdev_flash fled; 48 struct device *dev; 49 struct v4l2_flash *v4l2_flash; 50 struct mutex lock; 51 struct regulator *reg; 52 struct gpio_desc *enable_torch; 53 struct gpio_desc *enable_flash; 54 struct timer_list powerdown_timer; 55 u32 max_timeout; /* Flash max timeout */ 56 int flash_max_intensity; 57 int torch_max_intensity; 58 }; 59 60 static struct rt8515 *to_rt8515(struct led_classdev_flash *fled) 61 { 62 return container_of(fled, struct rt8515, fled); 63 } 64 65 static void rt8515_gpio_led_off(struct rt8515 *rt) 66 { 67 gpiod_set_value(rt->enable_flash, 0); 68 gpiod_set_value(rt->enable_torch, 0); 69 } 70 71 static void rt8515_gpio_brightness_commit(struct gpio_desc *gpiod, 72 int brightness) 73 { 74 int i; 75 76 /* 77 * Toggling a GPIO line with a small delay increases the 78 * brightness one step at a time. 79 */ 80 for (i = 0; i < brightness; i++) { 81 gpiod_set_value(gpiod, 0); 82 udelay(1); 83 gpiod_set_value(gpiod, 1); 84 udelay(1); 85 } 86 } 87 88 /* This is setting the torch light level */ 89 static int rt8515_led_brightness_set(struct led_classdev *led, 90 enum led_brightness brightness) 91 { 92 struct led_classdev_flash *fled = lcdev_to_flcdev(led); 93 struct rt8515 *rt = to_rt8515(fled); 94 95 mutex_lock(&rt->lock); 96 97 if (brightness == LED_OFF) { 98 /* Off */ 99 rt8515_gpio_led_off(rt); 100 } else if (brightness < RT8515_TORCH_MAX) { 101 /* Step it up to movie mode brightness using the flash pin */ 102 rt8515_gpio_brightness_commit(rt->enable_torch, brightness); 103 } else { 104 /* Max torch brightness requested */ 105 gpiod_set_value(rt->enable_torch, 1); 106 } 107 108 mutex_unlock(&rt->lock); 109 110 return 0; 111 } 112 113 static int rt8515_led_flash_strobe_set(struct led_classdev_flash *fled, 114 bool state) 115 { 116 struct rt8515 *rt = to_rt8515(fled); 117 struct led_flash_setting *timeout = &fled->timeout; 118 int brightness = rt->flash_max_intensity; 119 120 mutex_lock(&rt->lock); 121 122 if (state) { 123 /* Enable LED flash mode and set brightness */ 124 rt8515_gpio_brightness_commit(rt->enable_flash, brightness); 125 /* Set timeout */ 126 mod_timer(&rt->powerdown_timer, 127 jiffies + usecs_to_jiffies(timeout->val)); 128 } else { 129 timer_delete_sync(&rt->powerdown_timer); 130 /* Turn the LED off */ 131 rt8515_gpio_led_off(rt); 132 } 133 134 fled->led_cdev.brightness = LED_OFF; 135 /* After this the torch LED will be disabled */ 136 137 mutex_unlock(&rt->lock); 138 139 return 0; 140 } 141 142 static int rt8515_led_flash_strobe_get(struct led_classdev_flash *fled, 143 bool *state) 144 { 145 struct rt8515 *rt = to_rt8515(fled); 146 147 *state = timer_pending(&rt->powerdown_timer); 148 149 return 0; 150 } 151 152 static int rt8515_led_flash_timeout_set(struct led_classdev_flash *fled, 153 u32 timeout) 154 { 155 /* The timeout is stored in the led-class-flash core */ 156 return 0; 157 } 158 159 static const struct led_flash_ops rt8515_flash_ops = { 160 .strobe_set = rt8515_led_flash_strobe_set, 161 .strobe_get = rt8515_led_flash_strobe_get, 162 .timeout_set = rt8515_led_flash_timeout_set, 163 }; 164 165 static void rt8515_powerdown_timer(struct timer_list *t) 166 { 167 struct rt8515 *rt = timer_container_of(rt, t, powerdown_timer); 168 169 /* Turn the LED off */ 170 rt8515_gpio_led_off(rt); 171 } 172 173 static void rt8515_init_flash_timeout(struct rt8515 *rt) 174 { 175 struct led_classdev_flash *fled = &rt->fled; 176 struct led_flash_setting *s; 177 178 /* Init flash timeout setting */ 179 s = &fled->timeout; 180 s->min = 1; 181 s->max = rt->max_timeout; 182 s->step = 1; 183 /* 184 * Set default timeout to RT8515_TIMEOUT_US except if 185 * max_timeout from DT is lower. 186 */ 187 s->val = min(rt->max_timeout, RT8515_TIMEOUT_US); 188 } 189 190 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) 191 /* Configure the V2L2 flash subdevice */ 192 static void rt8515_init_v4l2_flash_config(struct rt8515 *rt, 193 struct v4l2_flash_config *v4l2_sd_cfg) 194 { 195 struct led_classdev *led = &rt->fled.led_cdev; 196 struct led_flash_setting *s; 197 198 strscpy(v4l2_sd_cfg->dev_name, led->dev->kobj.name, 199 sizeof(v4l2_sd_cfg->dev_name)); 200 201 /* 202 * Init flash intensity setting: this is a linear scale 203 * capped from the device tree max intensity setting 204 * 1..flash_max_intensity 205 */ 206 s = &v4l2_sd_cfg->intensity; 207 s->min = 1; 208 s->max = rt->flash_max_intensity; 209 s->step = 1; 210 s->val = s->max; 211 } 212 213 static void rt8515_v4l2_flash_release(struct rt8515 *rt) 214 { 215 v4l2_flash_release(rt->v4l2_flash); 216 } 217 218 #else 219 static void rt8515_init_v4l2_flash_config(struct rt8515 *rt, 220 struct v4l2_flash_config *v4l2_sd_cfg) 221 { 222 } 223 224 static void rt8515_v4l2_flash_release(struct rt8515 *rt) 225 { 226 } 227 #endif 228 229 static void rt8515_determine_max_intensity(struct rt8515 *rt, 230 struct fwnode_handle *led, 231 const char *resistance, 232 const char *max_ua_prop, int hw_max, 233 int *max_intensity_setting) 234 { 235 u32 res = 0; /* Can't be 0 so 0 is undefined */ 236 u32 ua; 237 u32 max_ma; 238 int max_intensity; 239 int ret; 240 241 fwnode_property_read_u32(rt->dev->fwnode, resistance, &res); 242 ret = fwnode_property_read_u32(led, max_ua_prop, &ua); 243 244 /* Missing info in DT, OK go with hardware maxima */ 245 if (ret || res == 0) { 246 dev_err(rt->dev, 247 "either %s or %s missing from DT, using HW max\n", 248 resistance, max_ua_prop); 249 max_ma = RT8515_MAX_IOUT_MA; 250 max_intensity = hw_max; 251 goto out_assign_max; 252 } 253 254 /* 255 * Formula from the datasheet, this is the maximum current 256 * defined by the hardware. 257 */ 258 max_ma = (5500 * 1000) / res; 259 /* 260 * Calculate max intensity (linear scaling) 261 * Formula is ((ua / 1000) / max_ma) * 100, then simplified 262 */ 263 max_intensity = (ua / 10) / max_ma; 264 265 dev_info(rt->dev, 266 "current restricted from %u to %u mA, max intensity %d/100\n", 267 max_ma, (ua / 1000), max_intensity); 268 269 out_assign_max: 270 dev_info(rt->dev, "max intensity %d/%d = %d mA\n", 271 max_intensity, hw_max, max_ma); 272 *max_intensity_setting = max_intensity; 273 } 274 275 static int rt8515_probe(struct platform_device *pdev) 276 { 277 struct device *dev = &pdev->dev; 278 struct fwnode_handle *child; 279 struct rt8515 *rt; 280 struct led_classdev *led; 281 struct led_classdev_flash *fled; 282 struct led_init_data init_data = {}; 283 struct v4l2_flash_config v4l2_sd_cfg = {}; 284 int ret; 285 286 rt = devm_kzalloc(dev, sizeof(*rt), GFP_KERNEL); 287 if (!rt) 288 return -ENOMEM; 289 290 rt->dev = dev; 291 fled = &rt->fled; 292 led = &fled->led_cdev; 293 294 /* ENF - Enable Flash line */ 295 rt->enable_flash = devm_gpiod_get(dev, "enf", GPIOD_OUT_LOW); 296 if (IS_ERR(rt->enable_flash)) 297 return dev_err_probe(dev, PTR_ERR(rt->enable_flash), 298 "cannot get ENF (enable flash) GPIO\n"); 299 300 /* ENT - Enable Torch line */ 301 rt->enable_torch = devm_gpiod_get(dev, "ent", GPIOD_OUT_LOW); 302 if (IS_ERR(rt->enable_torch)) 303 return dev_err_probe(dev, PTR_ERR(rt->enable_torch), 304 "cannot get ENT (enable torch) GPIO\n"); 305 306 child = device_get_next_child_node(dev, NULL); 307 if (!child) { 308 dev_err(dev, 309 "No fwnode child node found for connected LED.\n"); 310 return -EINVAL; 311 } 312 init_data.fwnode = child; 313 314 rt8515_determine_max_intensity(rt, child, "richtek,rfs-ohms", 315 "flash-max-microamp", 316 RT8515_FLASH_MAX, 317 &rt->flash_max_intensity); 318 rt8515_determine_max_intensity(rt, child, "richtek,rts-ohms", 319 "led-max-microamp", 320 RT8515_TORCH_MAX, 321 &rt->torch_max_intensity); 322 323 ret = fwnode_property_read_u32(child, "flash-max-timeout-us", 324 &rt->max_timeout); 325 if (ret) { 326 rt->max_timeout = RT8515_MAX_TIMEOUT_US; 327 dev_warn(dev, 328 "flash-max-timeout-us property missing\n"); 329 } 330 timer_setup(&rt->powerdown_timer, rt8515_powerdown_timer, 0); 331 rt8515_init_flash_timeout(rt); 332 333 fled->ops = &rt8515_flash_ops; 334 335 led->max_brightness = rt->torch_max_intensity; 336 led->brightness_set_blocking = rt8515_led_brightness_set; 337 led->flags |= LED_CORE_SUSPENDRESUME | LED_DEV_CAP_FLASH; 338 339 mutex_init(&rt->lock); 340 341 platform_set_drvdata(pdev, rt); 342 343 ret = devm_led_classdev_flash_register_ext(dev, fled, &init_data); 344 if (ret) { 345 fwnode_handle_put(child); 346 mutex_destroy(&rt->lock); 347 dev_err(dev, "can't register LED %s\n", led->name); 348 return ret; 349 } 350 351 rt8515_init_v4l2_flash_config(rt, &v4l2_sd_cfg); 352 353 /* Create a V4L2 Flash device if V4L2 flash is enabled */ 354 rt->v4l2_flash = v4l2_flash_init(dev, child, fled, NULL, &v4l2_sd_cfg); 355 if (IS_ERR(rt->v4l2_flash)) { 356 ret = PTR_ERR(rt->v4l2_flash); 357 dev_err(dev, "failed to register V4L2 flash device (%d)\n", 358 ret); 359 /* 360 * Continue without the V4L2 flash 361 * (we still have the classdev) 362 */ 363 } 364 365 fwnode_handle_put(child); 366 return 0; 367 } 368 369 static void rt8515_remove(struct platform_device *pdev) 370 { 371 struct rt8515 *rt = platform_get_drvdata(pdev); 372 373 rt8515_v4l2_flash_release(rt); 374 timer_delete_sync(&rt->powerdown_timer); 375 mutex_destroy(&rt->lock); 376 } 377 378 static const struct of_device_id rt8515_match[] = { 379 { .compatible = "richtek,rt8515", }, 380 { /* sentinel */ } 381 }; 382 MODULE_DEVICE_TABLE(of, rt8515_match); 383 384 static struct platform_driver rt8515_driver = { 385 .driver = { 386 .name = "rt8515", 387 .of_match_table = rt8515_match, 388 }, 389 .probe = rt8515_probe, 390 .remove = rt8515_remove, 391 }; 392 module_platform_driver(rt8515_driver); 393 394 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 395 MODULE_DESCRIPTION("Richtek RT8515 LED driver"); 396 MODULE_LICENSE("GPL"); 397