1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * LED Flash class driver for the AAT1290 4 * 1.5A Step-Up Current Regulator for Flash LEDs 5 * 6 * Copyright (C) 2015, Samsung Electronics Co., Ltd. 7 * Author: Jacek Anaszewski <j.anaszewski@samsung.com> 8 */ 9 10 #include <linux/cleanup.h> 11 #include <linux/delay.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/led-class-flash.h> 14 #include <linux/leds.h> 15 #include <linux/module.h> 16 #include <linux/mutex.h> 17 #include <linux/of.h> 18 #include <linux/pinctrl/consumer.h> 19 #include <linux/platform_device.h> 20 #include <linux/slab.h> 21 #include <media/v4l2-flash-led-class.h> 22 23 #define AAT1290_MOVIE_MODE_CURRENT_ADDR 17 24 #define AAT1290_MAX_MM_CURR_PERCENT_0 16 25 #define AAT1290_MAX_MM_CURR_PERCENT_100 1 26 27 #define AAT1290_FLASH_SAFETY_TIMER_ADDR 18 28 29 #define AAT1290_MOVIE_MODE_CONFIG_ADDR 19 30 #define AAT1290_MOVIE_MODE_OFF 1 31 #define AAT1290_MOVIE_MODE_ON 3 32 33 #define AAT1290_MM_CURRENT_RATIO_ADDR 20 34 #define AAT1290_MM_TO_FL_1_92 1 35 36 #define AAT1290_MM_TO_FL_RATIO 1000 / 1920 37 #define AAT1290_MAX_MM_CURRENT(fl_max) (fl_max * AAT1290_MM_TO_FL_RATIO) 38 39 #define AAT1290_LATCH_TIME_MIN_US 500 40 #define AAT1290_LATCH_TIME_MAX_US 1000 41 #define AAT1290_EN_SET_TICK_TIME_US 1 42 #define AAT1290_FLEN_OFF_DELAY_TIME_US 10 43 #define AAT1290_FLASH_TM_NUM_LEVELS 16 44 #define AAT1290_MM_CURRENT_SCALE_SIZE 15 45 46 #define AAT1290_NAME "aat1290" 47 48 49 struct aat1290_led_config_data { 50 /* maximum LED current in movie mode */ 51 u32 max_mm_current; 52 /* maximum LED current in flash mode */ 53 u32 max_flash_current; 54 /* maximum flash timeout */ 55 u32 max_flash_tm; 56 /* external strobe capability */ 57 bool has_external_strobe; 58 /* max LED brightness level */ 59 enum led_brightness max_brightness; 60 }; 61 62 struct aat1290_led { 63 /* platform device data */ 64 struct platform_device *pdev; 65 /* secures access to the device */ 66 struct mutex lock; 67 68 /* corresponding LED Flash class device */ 69 struct led_classdev_flash fled_cdev; 70 /* V4L2 Flash device */ 71 struct v4l2_flash *v4l2_flash; 72 73 /* FLEN pin */ 74 struct gpio_desc *gpio_fl_en; 75 /* EN|SET pin */ 76 struct gpio_desc *gpio_en_set; 77 /* movie mode current scale */ 78 int *mm_current_scale; 79 /* device mode */ 80 bool movie_mode; 81 }; 82 83 static struct aat1290_led *fled_cdev_to_led( 84 struct led_classdev_flash *fled_cdev) 85 { 86 return container_of(fled_cdev, struct aat1290_led, fled_cdev); 87 } 88 89 static struct led_classdev_flash *led_cdev_to_fled_cdev( 90 struct led_classdev *led_cdev) 91 { 92 return container_of(led_cdev, struct led_classdev_flash, led_cdev); 93 } 94 95 static void aat1290_as2cwire_write(struct aat1290_led *led, int addr, int value) 96 { 97 int i; 98 99 gpiod_direction_output(led->gpio_fl_en, 0); 100 gpiod_direction_output(led->gpio_en_set, 0); 101 102 udelay(AAT1290_FLEN_OFF_DELAY_TIME_US); 103 104 /* write address */ 105 for (i = 0; i < addr; ++i) { 106 udelay(AAT1290_EN_SET_TICK_TIME_US); 107 gpiod_direction_output(led->gpio_en_set, 0); 108 udelay(AAT1290_EN_SET_TICK_TIME_US); 109 gpiod_direction_output(led->gpio_en_set, 1); 110 } 111 112 usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US); 113 114 /* write data */ 115 for (i = 0; i < value; ++i) { 116 udelay(AAT1290_EN_SET_TICK_TIME_US); 117 gpiod_direction_output(led->gpio_en_set, 0); 118 udelay(AAT1290_EN_SET_TICK_TIME_US); 119 gpiod_direction_output(led->gpio_en_set, 1); 120 } 121 122 usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US); 123 } 124 125 static void aat1290_set_flash_safety_timer(struct aat1290_led *led, 126 unsigned int micro_sec) 127 { 128 struct led_classdev_flash *fled_cdev = &led->fled_cdev; 129 struct led_flash_setting *flash_tm = &fled_cdev->timeout; 130 int flash_tm_reg = AAT1290_FLASH_TM_NUM_LEVELS - 131 (micro_sec / flash_tm->step) + 1; 132 133 aat1290_as2cwire_write(led, AAT1290_FLASH_SAFETY_TIMER_ADDR, 134 flash_tm_reg); 135 } 136 137 /* LED subsystem callbacks */ 138 139 static int aat1290_led_brightness_set(struct led_classdev *led_cdev, 140 enum led_brightness brightness) 141 { 142 struct led_classdev_flash *fled_cdev = led_cdev_to_fled_cdev(led_cdev); 143 struct aat1290_led *led = fled_cdev_to_led(fled_cdev); 144 145 mutex_lock(&led->lock); 146 147 if (brightness == 0) { 148 gpiod_direction_output(led->gpio_fl_en, 0); 149 gpiod_direction_output(led->gpio_en_set, 0); 150 led->movie_mode = false; 151 } else { 152 if (!led->movie_mode) { 153 aat1290_as2cwire_write(led, 154 AAT1290_MM_CURRENT_RATIO_ADDR, 155 AAT1290_MM_TO_FL_1_92); 156 led->movie_mode = true; 157 } 158 159 aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CURRENT_ADDR, 160 AAT1290_MAX_MM_CURR_PERCENT_0 - brightness); 161 aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CONFIG_ADDR, 162 AAT1290_MOVIE_MODE_ON); 163 } 164 165 mutex_unlock(&led->lock); 166 167 return 0; 168 } 169 170 static int aat1290_led_flash_strobe_set(struct led_classdev_flash *fled_cdev, 171 bool state) 172 173 { 174 struct aat1290_led *led = fled_cdev_to_led(fled_cdev); 175 struct led_classdev *led_cdev = &fled_cdev->led_cdev; 176 struct led_flash_setting *timeout = &fled_cdev->timeout; 177 178 mutex_lock(&led->lock); 179 180 if (state) { 181 aat1290_set_flash_safety_timer(led, timeout->val); 182 gpiod_direction_output(led->gpio_fl_en, 1); 183 } else { 184 gpiod_direction_output(led->gpio_fl_en, 0); 185 gpiod_direction_output(led->gpio_en_set, 0); 186 } 187 188 /* 189 * To reenter movie mode after a flash event the part must be cycled 190 * off and back on to reset the movie mode and reprogrammed via the 191 * AS2Cwire. Therefore the brightness and movie_mode properties needs 192 * to be updated here to reflect the actual state. 193 */ 194 led_cdev->brightness = 0; 195 led->movie_mode = false; 196 197 mutex_unlock(&led->lock); 198 199 return 0; 200 } 201 202 static int aat1290_led_flash_timeout_set(struct led_classdev_flash *fled_cdev, 203 u32 timeout) 204 { 205 /* 206 * Don't do anything - flash timeout is cached in the led-class-flash 207 * core and will be applied in the strobe_set op, as writing the 208 * safety timer register spuriously turns the torch mode on. 209 */ 210 211 return 0; 212 } 213 214 static int aat1290_led_parse_dt(struct aat1290_led *led, 215 struct aat1290_led_config_data *cfg, 216 struct device_node **sub_node) 217 { 218 struct device *dev = &led->pdev->dev; 219 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) 220 struct pinctrl *pinctrl; 221 #endif 222 int ret = 0; 223 224 led->gpio_fl_en = devm_gpiod_get(dev, "flen", GPIOD_ASIS); 225 if (IS_ERR(led->gpio_fl_en)) { 226 ret = PTR_ERR(led->gpio_fl_en); 227 dev_err(dev, "Unable to claim gpio \"flen\".\n"); 228 return ret; 229 } 230 231 led->gpio_en_set = devm_gpiod_get(dev, "enset", GPIOD_ASIS); 232 if (IS_ERR(led->gpio_en_set)) { 233 ret = PTR_ERR(led->gpio_en_set); 234 dev_err(dev, "Unable to claim gpio \"enset\".\n"); 235 return ret; 236 } 237 238 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) 239 pinctrl = devm_pinctrl_get_select_default(&led->pdev->dev); 240 if (IS_ERR(pinctrl)) { 241 cfg->has_external_strobe = false; 242 dev_info(dev, 243 "No support for external strobe detected.\n"); 244 } else { 245 cfg->has_external_strobe = true; 246 } 247 #endif 248 249 struct device_node *child_node __free(device_node) = 250 of_get_next_available_child(dev_of_node(dev), NULL); 251 if (!child_node) { 252 dev_err(dev, "No DT child node found for connected LED.\n"); 253 return -EINVAL; 254 } 255 256 ret = of_property_read_u32(child_node, "led-max-microamp", 257 &cfg->max_mm_current); 258 /* 259 * led-max-microamp will default to 1/20 of flash-max-microamp 260 * in case it is missing. 261 */ 262 if (ret < 0) 263 dev_warn(dev, 264 "led-max-microamp DT property missing\n"); 265 266 ret = of_property_read_u32(child_node, "flash-max-microamp", 267 &cfg->max_flash_current); 268 if (ret < 0) { 269 dev_err(dev, 270 "flash-max-microamp DT property missing\n"); 271 return ret; 272 } 273 274 ret = of_property_read_u32(child_node, "flash-max-timeout-us", 275 &cfg->max_flash_tm); 276 if (ret < 0) { 277 dev_err(dev, 278 "flash-max-timeout-us DT property missing\n"); 279 return ret; 280 } 281 282 *sub_node = child_node; 283 284 return 0; 285 } 286 287 static void aat1290_led_validate_mm_current(struct aat1290_led *led, 288 struct aat1290_led_config_data *cfg) 289 { 290 int i, b = 0, e = AAT1290_MM_CURRENT_SCALE_SIZE; 291 292 while (e - b > 1) { 293 i = b + (e - b) / 2; 294 if (cfg->max_mm_current < led->mm_current_scale[i]) 295 e = i; 296 else 297 b = i; 298 } 299 300 cfg->max_mm_current = led->mm_current_scale[b]; 301 cfg->max_brightness = b + 1; 302 } 303 304 static int init_mm_current_scale(struct aat1290_led *led, 305 struct aat1290_led_config_data *cfg) 306 { 307 static const int max_mm_current_percent[] = { 308 20, 22, 25, 28, 32, 36, 40, 45, 50, 56, 309 63, 71, 79, 89, 100 310 }; 311 int i, max_mm_current = 312 AAT1290_MAX_MM_CURRENT(cfg->max_flash_current); 313 314 led->mm_current_scale = devm_kzalloc(&led->pdev->dev, 315 sizeof(max_mm_current_percent), 316 GFP_KERNEL); 317 if (!led->mm_current_scale) 318 return -ENOMEM; 319 320 for (i = 0; i < AAT1290_MM_CURRENT_SCALE_SIZE; ++i) 321 led->mm_current_scale[i] = max_mm_current * 322 max_mm_current_percent[i] / 100; 323 324 return 0; 325 } 326 327 static int aat1290_led_get_configuration(struct aat1290_led *led, 328 struct aat1290_led_config_data *cfg, 329 struct device_node **sub_node) 330 { 331 int ret; 332 333 ret = aat1290_led_parse_dt(led, cfg, sub_node); 334 if (ret < 0) 335 return ret; 336 /* 337 * Init non-linear movie mode current scale basing 338 * on the max flash current from led configuration. 339 */ 340 ret = init_mm_current_scale(led, cfg); 341 if (ret < 0) 342 return ret; 343 344 aat1290_led_validate_mm_current(led, cfg); 345 346 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) 347 #else 348 devm_kfree(&led->pdev->dev, led->mm_current_scale); 349 #endif 350 351 return 0; 352 } 353 354 static void aat1290_init_flash_timeout(struct aat1290_led *led, 355 struct aat1290_led_config_data *cfg) 356 { 357 struct led_classdev_flash *fled_cdev = &led->fled_cdev; 358 struct led_flash_setting *setting; 359 360 /* Init flash timeout setting */ 361 setting = &fled_cdev->timeout; 362 setting->min = cfg->max_flash_tm / AAT1290_FLASH_TM_NUM_LEVELS; 363 setting->max = cfg->max_flash_tm; 364 setting->step = setting->min; 365 setting->val = setting->max; 366 } 367 368 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) 369 static enum led_brightness aat1290_intensity_to_brightness( 370 struct v4l2_flash *v4l2_flash, 371 s32 intensity) 372 { 373 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 374 struct aat1290_led *led = fled_cdev_to_led(fled_cdev); 375 int i; 376 377 for (i = AAT1290_MM_CURRENT_SCALE_SIZE - 1; i >= 0; --i) 378 if (intensity >= led->mm_current_scale[i]) 379 return i + 1; 380 381 return 1; 382 } 383 384 static s32 aat1290_brightness_to_intensity(struct v4l2_flash *v4l2_flash, 385 enum led_brightness brightness) 386 { 387 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 388 struct aat1290_led *led = fled_cdev_to_led(fled_cdev); 389 390 return led->mm_current_scale[brightness - 1]; 391 } 392 393 static int aat1290_led_external_strobe_set(struct v4l2_flash *v4l2_flash, 394 bool enable) 395 { 396 struct aat1290_led *led = fled_cdev_to_led(v4l2_flash->fled_cdev); 397 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 398 struct led_classdev *led_cdev = &fled_cdev->led_cdev; 399 struct pinctrl *pinctrl; 400 401 gpiod_direction_output(led->gpio_fl_en, 0); 402 gpiod_direction_output(led->gpio_en_set, 0); 403 404 led->movie_mode = false; 405 led_cdev->brightness = 0; 406 407 pinctrl = devm_pinctrl_get_select(&led->pdev->dev, 408 enable ? "isp" : "host"); 409 if (IS_ERR(pinctrl)) { 410 dev_warn(&led->pdev->dev, "Unable to switch strobe source.\n"); 411 return PTR_ERR(pinctrl); 412 } 413 414 return 0; 415 } 416 417 static void aat1290_init_v4l2_flash_config(struct aat1290_led *led, 418 struct aat1290_led_config_data *led_cfg, 419 struct v4l2_flash_config *v4l2_sd_cfg) 420 { 421 struct led_classdev *led_cdev = &led->fled_cdev.led_cdev; 422 struct led_flash_setting *s; 423 424 strscpy(v4l2_sd_cfg->dev_name, led_cdev->dev->kobj.name, 425 sizeof(v4l2_sd_cfg->dev_name)); 426 427 s = &v4l2_sd_cfg->intensity; 428 s->min = led->mm_current_scale[0]; 429 s->max = led_cfg->max_mm_current; 430 s->step = 1; 431 s->val = s->max; 432 433 v4l2_sd_cfg->has_external_strobe = led_cfg->has_external_strobe; 434 } 435 436 static const struct v4l2_flash_ops v4l2_flash_ops = { 437 .external_strobe_set = aat1290_led_external_strobe_set, 438 .intensity_to_led_brightness = aat1290_intensity_to_brightness, 439 .led_brightness_to_intensity = aat1290_brightness_to_intensity, 440 }; 441 #else 442 static inline void aat1290_init_v4l2_flash_config(struct aat1290_led *led, 443 struct aat1290_led_config_data *led_cfg, 444 struct v4l2_flash_config *v4l2_sd_cfg) 445 { 446 } 447 static const struct v4l2_flash_ops v4l2_flash_ops; 448 #endif 449 450 static const struct led_flash_ops flash_ops = { 451 .strobe_set = aat1290_led_flash_strobe_set, 452 .timeout_set = aat1290_led_flash_timeout_set, 453 }; 454 455 static int aat1290_led_probe(struct platform_device *pdev) 456 { 457 struct device *dev = &pdev->dev; 458 struct device_node *sub_node = NULL; 459 struct aat1290_led *led; 460 struct led_classdev *led_cdev; 461 struct led_classdev_flash *fled_cdev; 462 struct led_init_data init_data = {}; 463 struct aat1290_led_config_data led_cfg = {}; 464 struct v4l2_flash_config v4l2_sd_cfg = {}; 465 int ret; 466 467 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); 468 if (!led) 469 return -ENOMEM; 470 471 led->pdev = pdev; 472 platform_set_drvdata(pdev, led); 473 474 fled_cdev = &led->fled_cdev; 475 fled_cdev->ops = &flash_ops; 476 led_cdev = &fled_cdev->led_cdev; 477 478 ret = aat1290_led_get_configuration(led, &led_cfg, &sub_node); 479 if (ret < 0) 480 return ret; 481 482 mutex_init(&led->lock); 483 484 /* Initialize LED Flash class device */ 485 led_cdev->brightness_set_blocking = aat1290_led_brightness_set; 486 led_cdev->max_brightness = led_cfg.max_brightness; 487 led_cdev->flags |= LED_DEV_CAP_FLASH; 488 489 aat1290_init_flash_timeout(led, &led_cfg); 490 491 init_data.fwnode = of_fwnode_handle(sub_node); 492 init_data.devicename = AAT1290_NAME; 493 494 /* Register LED Flash class device */ 495 ret = led_classdev_flash_register_ext(&pdev->dev, fled_cdev, 496 &init_data); 497 if (ret < 0) 498 goto err_flash_register; 499 500 aat1290_init_v4l2_flash_config(led, &led_cfg, &v4l2_sd_cfg); 501 502 /* Create V4L2 Flash subdev. */ 503 led->v4l2_flash = v4l2_flash_init(dev, of_fwnode_handle(sub_node), 504 fled_cdev, &v4l2_flash_ops, 505 &v4l2_sd_cfg); 506 if (IS_ERR(led->v4l2_flash)) { 507 ret = PTR_ERR(led->v4l2_flash); 508 goto error_v4l2_flash_init; 509 } 510 511 return 0; 512 513 error_v4l2_flash_init: 514 led_classdev_flash_unregister(fled_cdev); 515 err_flash_register: 516 mutex_destroy(&led->lock); 517 518 return ret; 519 } 520 521 static void aat1290_led_remove(struct platform_device *pdev) 522 { 523 struct aat1290_led *led = platform_get_drvdata(pdev); 524 525 v4l2_flash_release(led->v4l2_flash); 526 led_classdev_flash_unregister(&led->fled_cdev); 527 528 mutex_destroy(&led->lock); 529 } 530 531 static const struct of_device_id aat1290_led_dt_match[] = { 532 { .compatible = "skyworks,aat1290" }, 533 {}, 534 }; 535 MODULE_DEVICE_TABLE(of, aat1290_led_dt_match); 536 537 static struct platform_driver aat1290_led_driver = { 538 .probe = aat1290_led_probe, 539 .remove_new = aat1290_led_remove, 540 .driver = { 541 .name = "aat1290", 542 .of_match_table = aat1290_led_dt_match, 543 }, 544 }; 545 546 module_platform_driver(aat1290_led_driver); 547 548 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>"); 549 MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs"); 550 MODULE_LICENSE("GPL v2"); 551