1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Simple PWM based backlight control, board code has to setup 4 * 1) pin configuration so PWM waveforms can output 5 * 2) platform_data being correctly configured 6 */ 7 8 #include <linux/delay.h> 9 #include <linux/gpio/consumer.h> 10 #include <linux/gpio.h> 11 #include <linux/module.h> 12 #include <linux/kernel.h> 13 #include <linux/init.h> 14 #include <linux/platform_device.h> 15 #include <linux/fb.h> 16 #include <linux/backlight.h> 17 #include <linux/err.h> 18 #include <linux/pwm.h> 19 #include <linux/pwm_backlight.h> 20 #include <linux/regulator/consumer.h> 21 #include <linux/slab.h> 22 23 struct pwm_bl_data { 24 struct pwm_device *pwm; 25 struct device *dev; 26 unsigned int lth_brightness; 27 unsigned int *levels; 28 bool enabled; 29 struct regulator *power_supply; 30 struct gpio_desc *enable_gpio; 31 unsigned int scale; 32 bool legacy; 33 unsigned int post_pwm_on_delay; 34 unsigned int pwm_off_delay; 35 int (*notify)(struct device *, 36 int brightness); 37 void (*notify_after)(struct device *, 38 int brightness); 39 int (*check_fb)(struct device *, struct fb_info *); 40 void (*exit)(struct device *); 41 }; 42 43 static void pwm_backlight_power_on(struct pwm_bl_data *pb) 44 { 45 struct pwm_state state; 46 int err; 47 48 pwm_get_state(pb->pwm, &state); 49 if (pb->enabled) 50 return; 51 52 err = regulator_enable(pb->power_supply); 53 if (err < 0) 54 dev_err(pb->dev, "failed to enable power supply\n"); 55 56 state.enabled = true; 57 pwm_apply_state(pb->pwm, &state); 58 59 if (pb->post_pwm_on_delay) 60 msleep(pb->post_pwm_on_delay); 61 62 if (pb->enable_gpio) 63 gpiod_set_value_cansleep(pb->enable_gpio, 1); 64 65 pb->enabled = true; 66 } 67 68 static void pwm_backlight_power_off(struct pwm_bl_data *pb) 69 { 70 struct pwm_state state; 71 72 pwm_get_state(pb->pwm, &state); 73 if (!pb->enabled) 74 return; 75 76 if (pb->enable_gpio) 77 gpiod_set_value_cansleep(pb->enable_gpio, 0); 78 79 if (pb->pwm_off_delay) 80 msleep(pb->pwm_off_delay); 81 82 state.enabled = false; 83 state.duty_cycle = 0; 84 pwm_apply_state(pb->pwm, &state); 85 86 regulator_disable(pb->power_supply); 87 pb->enabled = false; 88 } 89 90 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness) 91 { 92 unsigned int lth = pb->lth_brightness; 93 struct pwm_state state; 94 u64 duty_cycle; 95 96 pwm_get_state(pb->pwm, &state); 97 98 if (pb->levels) 99 duty_cycle = pb->levels[brightness]; 100 else 101 duty_cycle = brightness; 102 103 duty_cycle *= state.period - lth; 104 do_div(duty_cycle, pb->scale); 105 106 return duty_cycle + lth; 107 } 108 109 static int pwm_backlight_update_status(struct backlight_device *bl) 110 { 111 struct pwm_bl_data *pb = bl_get_data(bl); 112 int brightness = bl->props.brightness; 113 struct pwm_state state; 114 115 if (bl->props.power != FB_BLANK_UNBLANK || 116 bl->props.fb_blank != FB_BLANK_UNBLANK || 117 bl->props.state & BL_CORE_FBBLANK) 118 brightness = 0; 119 120 if (pb->notify) 121 brightness = pb->notify(pb->dev, brightness); 122 123 if (brightness > 0) { 124 pwm_get_state(pb->pwm, &state); 125 state.duty_cycle = compute_duty_cycle(pb, brightness); 126 pwm_apply_state(pb->pwm, &state); 127 pwm_backlight_power_on(pb); 128 } else { 129 pwm_backlight_power_off(pb); 130 } 131 132 if (pb->notify_after) 133 pb->notify_after(pb->dev, brightness); 134 135 return 0; 136 } 137 138 static int pwm_backlight_check_fb(struct backlight_device *bl, 139 struct fb_info *info) 140 { 141 struct pwm_bl_data *pb = bl_get_data(bl); 142 143 return !pb->check_fb || pb->check_fb(pb->dev, info); 144 } 145 146 static const struct backlight_ops pwm_backlight_ops = { 147 .update_status = pwm_backlight_update_status, 148 .check_fb = pwm_backlight_check_fb, 149 }; 150 151 #ifdef CONFIG_OF 152 #define PWM_LUMINANCE_SHIFT 16 153 #define PWM_LUMINANCE_SCALE (1 << PWM_LUMINANCE_SHIFT) /* luminance scale */ 154 155 /* 156 * CIE lightness to PWM conversion. 157 * 158 * The CIE 1931 lightness formula is what actually describes how we perceive 159 * light: 160 * Y = (L* / 903.3) if L* ≤ 8 161 * Y = ((L* + 16) / 116)^3 if L* > 8 162 * 163 * Where Y is the luminance, the amount of light coming out of the screen, and 164 * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human 165 * perceives the screen to be, and is a number between 0 and 100. 166 * 167 * The following function does the fixed point maths needed to implement the 168 * above formula. 169 */ 170 static u64 cie1931(unsigned int lightness) 171 { 172 u64 retval; 173 174 /* 175 * @lightness is given as a number between 0 and 1, expressed 176 * as a fixed-point number in scale 177 * PWM_LUMINANCE_SCALE. Convert to a percentage, still 178 * expressed as a fixed-point number, so the above formulas 179 * can be applied. 180 */ 181 lightness *= 100; 182 if (lightness <= (8 * PWM_LUMINANCE_SCALE)) { 183 retval = DIV_ROUND_CLOSEST(lightness * 10, 9033); 184 } else { 185 retval = (lightness + (16 * PWM_LUMINANCE_SCALE)) / 116; 186 retval *= retval * retval; 187 retval += 1ULL << (2*PWM_LUMINANCE_SHIFT - 1); 188 retval >>= 2*PWM_LUMINANCE_SHIFT; 189 } 190 191 return retval; 192 } 193 194 /* 195 * Create a default correction table for PWM values to create linear brightness 196 * for LED based backlights using the CIE1931 algorithm. 197 */ 198 static 199 int pwm_backlight_brightness_default(struct device *dev, 200 struct platform_pwm_backlight_data *data, 201 unsigned int period) 202 { 203 unsigned int i; 204 u64 retval; 205 206 /* 207 * Once we have 4096 levels there's little point going much higher... 208 * neither interactive sliders nor animation benefits from having 209 * more values in the table. 210 */ 211 data->max_brightness = 212 min((int)DIV_ROUND_UP(period, fls(period)), 4096); 213 214 data->levels = devm_kcalloc(dev, data->max_brightness, 215 sizeof(*data->levels), GFP_KERNEL); 216 if (!data->levels) 217 return -ENOMEM; 218 219 /* Fill the table using the cie1931 algorithm */ 220 for (i = 0; i < data->max_brightness; i++) { 221 retval = cie1931((i * PWM_LUMINANCE_SCALE) / 222 data->max_brightness) * period; 223 retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE); 224 if (retval > UINT_MAX) 225 return -EINVAL; 226 data->levels[i] = (unsigned int)retval; 227 } 228 229 data->dft_brightness = data->max_brightness / 2; 230 data->max_brightness--; 231 232 return 0; 233 } 234 235 static int pwm_backlight_parse_dt(struct device *dev, 236 struct platform_pwm_backlight_data *data) 237 { 238 struct device_node *node = dev->of_node; 239 unsigned int num_levels = 0; 240 unsigned int levels_count; 241 unsigned int num_steps = 0; 242 struct property *prop; 243 unsigned int *table; 244 int length; 245 u32 value; 246 int ret; 247 248 if (!node) 249 return -ENODEV; 250 251 memset(data, 0, sizeof(*data)); 252 253 /* 254 * These values are optional and set as 0 by default, the out values 255 * are modified only if a valid u32 value can be decoded. 256 */ 257 of_property_read_u32(node, "post-pwm-on-delay-ms", 258 &data->post_pwm_on_delay); 259 of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay); 260 261 data->enable_gpio = -EINVAL; 262 263 /* 264 * Determine the number of brightness levels, if this property is not 265 * set a default table of brightness levels will be used. 266 */ 267 prop = of_find_property(node, "brightness-levels", &length); 268 if (!prop) 269 return 0; 270 271 data->max_brightness = length / sizeof(u32); 272 273 /* read brightness levels from DT property */ 274 if (data->max_brightness > 0) { 275 size_t size = sizeof(*data->levels) * data->max_brightness; 276 unsigned int i, j, n = 0; 277 278 data->levels = devm_kzalloc(dev, size, GFP_KERNEL); 279 if (!data->levels) 280 return -ENOMEM; 281 282 ret = of_property_read_u32_array(node, "brightness-levels", 283 data->levels, 284 data->max_brightness); 285 if (ret < 0) 286 return ret; 287 288 ret = of_property_read_u32(node, "default-brightness-level", 289 &value); 290 if (ret < 0) 291 return ret; 292 293 data->dft_brightness = value; 294 295 /* 296 * This property is optional, if is set enables linear 297 * interpolation between each of the values of brightness levels 298 * and creates a new pre-computed table. 299 */ 300 of_property_read_u32(node, "num-interpolated-steps", 301 &num_steps); 302 303 /* 304 * Make sure that there is at least two entries in the 305 * brightness-levels table, otherwise we can't interpolate 306 * between two points. 307 */ 308 if (num_steps) { 309 if (data->max_brightness < 2) { 310 dev_err(dev, "can't interpolate\n"); 311 return -EINVAL; 312 } 313 314 /* 315 * Recalculate the number of brightness levels, now 316 * taking in consideration the number of interpolated 317 * steps between two levels. 318 */ 319 for (i = 0; i < data->max_brightness - 1; i++) { 320 if ((data->levels[i + 1] - data->levels[i]) / 321 num_steps) 322 num_levels += num_steps; 323 else 324 num_levels++; 325 } 326 num_levels++; 327 dev_dbg(dev, "new number of brightness levels: %d\n", 328 num_levels); 329 330 /* 331 * Create a new table of brightness levels with all the 332 * interpolated steps. 333 */ 334 size = sizeof(*table) * num_levels; 335 table = devm_kzalloc(dev, size, GFP_KERNEL); 336 if (!table) 337 return -ENOMEM; 338 339 /* Fill the interpolated table. */ 340 levels_count = 0; 341 for (i = 0; i < data->max_brightness - 1; i++) { 342 value = data->levels[i]; 343 n = (data->levels[i + 1] - value) / num_steps; 344 if (n > 0) { 345 for (j = 0; j < num_steps; j++) { 346 table[levels_count] = value; 347 value += n; 348 levels_count++; 349 } 350 } else { 351 table[levels_count] = data->levels[i]; 352 levels_count++; 353 } 354 } 355 table[levels_count] = data->levels[i]; 356 357 /* 358 * As we use interpolation lets remove current 359 * brightness levels table and replace for the 360 * new interpolated table. 361 */ 362 devm_kfree(dev, data->levels); 363 data->levels = table; 364 365 /* 366 * Reassign max_brightness value to the new total number 367 * of brightness levels. 368 */ 369 data->max_brightness = num_levels; 370 } 371 372 data->max_brightness--; 373 } 374 375 return 0; 376 } 377 378 static const struct of_device_id pwm_backlight_of_match[] = { 379 { .compatible = "pwm-backlight" }, 380 { } 381 }; 382 383 MODULE_DEVICE_TABLE(of, pwm_backlight_of_match); 384 #else 385 static int pwm_backlight_parse_dt(struct device *dev, 386 struct platform_pwm_backlight_data *data) 387 { 388 return -ENODEV; 389 } 390 391 static 392 int pwm_backlight_brightness_default(struct device *dev, 393 struct platform_pwm_backlight_data *data, 394 unsigned int period) 395 { 396 return -ENODEV; 397 } 398 #endif 399 400 static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data) 401 { 402 unsigned int nlevels = data->max_brightness + 1; 403 unsigned int min_val = data->levels[0]; 404 unsigned int max_val = data->levels[nlevels - 1]; 405 /* 406 * Multiplying by 128 means that even in pathological cases such 407 * as (max_val - min_val) == nlevels the error at max_val is less 408 * than 1%. 409 */ 410 unsigned int slope = (128 * (max_val - min_val)) / nlevels; 411 unsigned int margin = (max_val - min_val) / 20; /* 5% */ 412 int i; 413 414 for (i = 1; i < nlevels; i++) { 415 unsigned int linear_value = min_val + ((i * slope) / 128); 416 unsigned int delta = abs(linear_value - data->levels[i]); 417 418 if (delta > margin) 419 return false; 420 } 421 422 return true; 423 } 424 425 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb) 426 { 427 struct device_node *node = pb->dev->of_node; 428 429 /* Not booted with device tree or no phandle link to the node */ 430 if (!node || !node->phandle) 431 return FB_BLANK_UNBLANK; 432 433 /* 434 * If the driver is probed from the device tree and there is a 435 * phandle link pointing to the backlight node, it is safe to 436 * assume that another driver will enable the backlight at the 437 * appropriate time. Therefore, if it is disabled, keep it so. 438 */ 439 440 /* if the enable GPIO is disabled, do not enable the backlight */ 441 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0) 442 return FB_BLANK_POWERDOWN; 443 444 /* The regulator is disabled, do not enable the backlight */ 445 if (!regulator_is_enabled(pb->power_supply)) 446 return FB_BLANK_POWERDOWN; 447 448 /* The PWM is disabled, keep it like this */ 449 if (!pwm_is_enabled(pb->pwm)) 450 return FB_BLANK_POWERDOWN; 451 452 return FB_BLANK_UNBLANK; 453 } 454 455 static int pwm_backlight_probe(struct platform_device *pdev) 456 { 457 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev); 458 struct platform_pwm_backlight_data defdata; 459 struct backlight_properties props; 460 struct backlight_device *bl; 461 struct device_node *node = pdev->dev.of_node; 462 struct pwm_bl_data *pb; 463 struct pwm_state state; 464 unsigned int i; 465 int ret; 466 467 if (!data) { 468 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata); 469 if (ret < 0) { 470 dev_err(&pdev->dev, "failed to find platform data\n"); 471 return ret; 472 } 473 474 data = &defdata; 475 } 476 477 if (data->init) { 478 ret = data->init(&pdev->dev); 479 if (ret < 0) 480 return ret; 481 } 482 483 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); 484 if (!pb) { 485 ret = -ENOMEM; 486 goto err_alloc; 487 } 488 489 pb->notify = data->notify; 490 pb->notify_after = data->notify_after; 491 pb->check_fb = data->check_fb; 492 pb->exit = data->exit; 493 pb->dev = &pdev->dev; 494 pb->enabled = false; 495 pb->post_pwm_on_delay = data->post_pwm_on_delay; 496 pb->pwm_off_delay = data->pwm_off_delay; 497 498 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 499 GPIOD_ASIS); 500 if (IS_ERR(pb->enable_gpio)) { 501 ret = PTR_ERR(pb->enable_gpio); 502 goto err_alloc; 503 } 504 505 /* 506 * Compatibility fallback for drivers still using the integer GPIO 507 * platform data. Must go away soon. 508 */ 509 if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) { 510 ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio, 511 GPIOF_OUT_INIT_HIGH, "enable"); 512 if (ret < 0) { 513 dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n", 514 data->enable_gpio, ret); 515 goto err_alloc; 516 } 517 518 pb->enable_gpio = gpio_to_desc(data->enable_gpio); 519 } 520 521 /* 522 * If the GPIO is not known to be already configured as output, that 523 * is, if gpiod_get_direction returns either 1 or -EINVAL, change the 524 * direction to output and set the GPIO as active. 525 * Do not force the GPIO to active when it was already output as it 526 * could cause backlight flickering or we would enable the backlight too 527 * early. Leave the decision of the initial backlight state for later. 528 */ 529 if (pb->enable_gpio && 530 gpiod_get_direction(pb->enable_gpio) != 0) 531 gpiod_direction_output(pb->enable_gpio, 1); 532 533 pb->power_supply = devm_regulator_get(&pdev->dev, "power"); 534 if (IS_ERR(pb->power_supply)) { 535 ret = PTR_ERR(pb->power_supply); 536 goto err_alloc; 537 } 538 539 pb->pwm = devm_pwm_get(&pdev->dev, NULL); 540 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) { 541 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n"); 542 pb->legacy = true; 543 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight"); 544 } 545 546 if (IS_ERR(pb->pwm)) { 547 ret = PTR_ERR(pb->pwm); 548 if (ret != -EPROBE_DEFER) 549 dev_err(&pdev->dev, "unable to request PWM\n"); 550 goto err_alloc; 551 } 552 553 dev_dbg(&pdev->dev, "got pwm for backlight\n"); 554 555 /* Sync up PWM state. */ 556 pwm_init_state(pb->pwm, &state); 557 558 /* 559 * The DT case will set the pwm_period_ns field to 0 and store the 560 * period, parsed from the DT, in the PWM device. For the non-DT case, 561 * set the period from platform data if it has not already been set 562 * via the PWM lookup table. 563 */ 564 if (!state.period && (data->pwm_period_ns > 0)) 565 state.period = data->pwm_period_ns; 566 567 ret = pwm_apply_state(pb->pwm, &state); 568 if (ret) { 569 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", 570 ret); 571 goto err_alloc; 572 } 573 574 memset(&props, 0, sizeof(struct backlight_properties)); 575 576 if (data->levels) { 577 pb->levels = data->levels; 578 579 /* 580 * For the DT case, only when brightness levels is defined 581 * data->levels is filled. For the non-DT case, data->levels 582 * can come from platform data, however is not usual. 583 */ 584 for (i = 0; i <= data->max_brightness; i++) 585 if (data->levels[i] > pb->scale) 586 pb->scale = data->levels[i]; 587 588 if (pwm_backlight_is_linear(data)) 589 props.scale = BACKLIGHT_SCALE_LINEAR; 590 else 591 props.scale = BACKLIGHT_SCALE_NON_LINEAR; 592 } else if (!data->max_brightness) { 593 /* 594 * If no brightness levels are provided and max_brightness is 595 * not set, use the default brightness table. For the DT case, 596 * max_brightness is set to 0 when brightness levels is not 597 * specified. For the non-DT case, max_brightness is usually 598 * set to some value. 599 */ 600 601 /* Get the PWM period (in nanoseconds) */ 602 pwm_get_state(pb->pwm, &state); 603 604 ret = pwm_backlight_brightness_default(&pdev->dev, data, 605 state.period); 606 if (ret < 0) { 607 dev_err(&pdev->dev, 608 "failed to setup default brightness table\n"); 609 goto err_alloc; 610 } 611 612 for (i = 0; i <= data->max_brightness; i++) { 613 if (data->levels[i] > pb->scale) 614 pb->scale = data->levels[i]; 615 616 pb->levels = data->levels; 617 } 618 619 props.scale = BACKLIGHT_SCALE_NON_LINEAR; 620 } else { 621 /* 622 * That only happens for the non-DT case, where platform data 623 * sets the max_brightness value. 624 */ 625 pb->scale = data->max_brightness; 626 } 627 628 pb->lth_brightness = data->lth_brightness * (state.period / pb->scale); 629 630 props.type = BACKLIGHT_RAW; 631 props.max_brightness = data->max_brightness; 632 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, 633 &pwm_backlight_ops, &props); 634 if (IS_ERR(bl)) { 635 dev_err(&pdev->dev, "failed to register backlight\n"); 636 ret = PTR_ERR(bl); 637 if (pb->legacy) 638 pwm_free(pb->pwm); 639 goto err_alloc; 640 } 641 642 if (data->dft_brightness > data->max_brightness) { 643 dev_warn(&pdev->dev, 644 "invalid default brightness level: %u, using %u\n", 645 data->dft_brightness, data->max_brightness); 646 data->dft_brightness = data->max_brightness; 647 } 648 649 bl->props.brightness = data->dft_brightness; 650 bl->props.power = pwm_backlight_initial_power_state(pb); 651 backlight_update_status(bl); 652 653 platform_set_drvdata(pdev, bl); 654 return 0; 655 656 err_alloc: 657 if (data->exit) 658 data->exit(&pdev->dev); 659 return ret; 660 } 661 662 static int pwm_backlight_remove(struct platform_device *pdev) 663 { 664 struct backlight_device *bl = platform_get_drvdata(pdev); 665 struct pwm_bl_data *pb = bl_get_data(bl); 666 667 backlight_device_unregister(bl); 668 pwm_backlight_power_off(pb); 669 670 if (pb->exit) 671 pb->exit(&pdev->dev); 672 if (pb->legacy) 673 pwm_free(pb->pwm); 674 675 return 0; 676 } 677 678 static void pwm_backlight_shutdown(struct platform_device *pdev) 679 { 680 struct backlight_device *bl = platform_get_drvdata(pdev); 681 struct pwm_bl_data *pb = bl_get_data(bl); 682 683 pwm_backlight_power_off(pb); 684 } 685 686 #ifdef CONFIG_PM_SLEEP 687 static int pwm_backlight_suspend(struct device *dev) 688 { 689 struct backlight_device *bl = dev_get_drvdata(dev); 690 struct pwm_bl_data *pb = bl_get_data(bl); 691 692 if (pb->notify) 693 pb->notify(pb->dev, 0); 694 695 pwm_backlight_power_off(pb); 696 697 if (pb->notify_after) 698 pb->notify_after(pb->dev, 0); 699 700 return 0; 701 } 702 703 static int pwm_backlight_resume(struct device *dev) 704 { 705 struct backlight_device *bl = dev_get_drvdata(dev); 706 707 backlight_update_status(bl); 708 709 return 0; 710 } 711 #endif 712 713 static const struct dev_pm_ops pwm_backlight_pm_ops = { 714 #ifdef CONFIG_PM_SLEEP 715 .suspend = pwm_backlight_suspend, 716 .resume = pwm_backlight_resume, 717 .poweroff = pwm_backlight_suspend, 718 .restore = pwm_backlight_resume, 719 #endif 720 }; 721 722 static struct platform_driver pwm_backlight_driver = { 723 .driver = { 724 .name = "pwm-backlight", 725 .pm = &pwm_backlight_pm_ops, 726 .of_match_table = of_match_ptr(pwm_backlight_of_match), 727 }, 728 .probe = pwm_backlight_probe, 729 .remove = pwm_backlight_remove, 730 .shutdown = pwm_backlight_shutdown, 731 }; 732 733 module_platform_driver(pwm_backlight_driver); 734 735 MODULE_DESCRIPTION("PWM based Backlight Driver"); 736 MODULE_LICENSE("GPL v2"); 737 MODULE_ALIAS("platform:pwm-backlight"); 738