Lines Matching +full:pwm +full:- +full:number

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simple PWM based backlight control, board code has to setup
4 * 1) pin configuration so PWM waveforms can output
17 #include <linux/pwm.h>
23 struct pwm_device *pwm; member
45 if (pb->enabled) in pwm_backlight_power_on()
48 if (pb->power_supply) { in pwm_backlight_power_on()
49 err = regulator_enable(pb->power_supply); in pwm_backlight_power_on()
51 dev_err(pb->dev, "failed to enable power supply\n"); in pwm_backlight_power_on()
54 if (pb->post_pwm_on_delay) in pwm_backlight_power_on()
55 msleep(pb->post_pwm_on_delay); in pwm_backlight_power_on()
57 gpiod_set_value_cansleep(pb->enable_gpio, 1); in pwm_backlight_power_on()
59 pb->enabled = true; in pwm_backlight_power_on()
64 if (!pb->enabled) in pwm_backlight_power_off()
67 gpiod_set_value_cansleep(pb->enable_gpio, 0); in pwm_backlight_power_off()
69 if (pb->pwm_off_delay) in pwm_backlight_power_off()
70 msleep(pb->pwm_off_delay); in pwm_backlight_power_off()
72 if (pb->power_supply) in pwm_backlight_power_off()
73 regulator_disable(pb->power_supply); in pwm_backlight_power_off()
74 pb->enabled = false; in pwm_backlight_power_off()
79 unsigned int lth = pb->lth_brightness; in compute_duty_cycle()
82 if (pb->levels) in compute_duty_cycle()
83 duty_cycle = pb->levels[brightness]; in compute_duty_cycle()
87 duty_cycle *= state->period - lth; in compute_duty_cycle()
88 do_div(duty_cycle, pb->scale); in compute_duty_cycle()
99 if (pb->notify) in pwm_backlight_update_status()
100 brightness = pb->notify(pb->dev, brightness); in pwm_backlight_update_status()
103 pwm_get_state(pb->pwm, &state); in pwm_backlight_update_status()
106 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_update_status()
112 pwm_get_state(pb->pwm, &state); in pwm_backlight_update_status()
115 * We cannot assume a disabled PWM to drive its output to the in pwm_backlight_update_status()
117 * we assume that this isn't relevant and we can disable the PWM in pwm_backlight_update_status()
119 * a regulator keep the PWM on be sure to get a constant in pwm_backlight_update_status()
122 state.enabled = !pb->power_supply && !pb->enable_gpio; in pwm_backlight_update_status()
123 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_update_status()
126 if (pb->notify_after) in pwm_backlight_update_status()
127 pb->notify_after(pb->dev, brightness); in pwm_backlight_update_status()
137 return !pb->check_fb || pb->check_fb(pb->dev, info); in pwm_backlight_check_fb()
150 * CIE lightness to PWM conversion.
158 * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human
159 * perceives the screen to be, and is a number between 0 and 100.
169 * @lightness is given as a number between 0 and 1, expressed in cie1931()
170 * as a fixed-point number in scale in cie1931()
172 * expressed as a fixed-point number, so the above formulas in cie1931()
181 retval += 1ULL << (2*PWM_LUMINANCE_SHIFT - 1); in cie1931()
189 * Create a default correction table for PWM values to create linear brightness
205 data->max_brightness = in pwm_backlight_brightness_default()
208 data->levels = devm_kcalloc(dev, data->max_brightness, in pwm_backlight_brightness_default()
209 sizeof(*data->levels), GFP_KERNEL); in pwm_backlight_brightness_default()
210 if (!data->levels) in pwm_backlight_brightness_default()
211 return -ENOMEM; in pwm_backlight_brightness_default()
214 for (i = 0; i < data->max_brightness; i++) { in pwm_backlight_brightness_default()
216 data->max_brightness) * period; in pwm_backlight_brightness_default()
219 return -EINVAL; in pwm_backlight_brightness_default()
220 data->levels[i] = (unsigned int)retval; in pwm_backlight_brightness_default()
223 data->dft_brightness = data->max_brightness / 2; in pwm_backlight_brightness_default()
224 data->max_brightness--; in pwm_backlight_brightness_default()
232 struct device_node *node = dev->of_node; in pwm_backlight_parse_dt()
242 return -ENODEV; in pwm_backlight_parse_dt()
250 of_property_read_u32(node, "post-pwm-on-delay-ms", in pwm_backlight_parse_dt()
251 &data->post_pwm_on_delay); in pwm_backlight_parse_dt()
252 of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay); in pwm_backlight_parse_dt()
255 * Determine the number of brightness levels, if this property is not in pwm_backlight_parse_dt()
258 prop = of_find_property(node, "brightness-levels", &length); in pwm_backlight_parse_dt()
266 data->levels = devm_kcalloc(dev, num_levels, in pwm_backlight_parse_dt()
267 sizeof(*data->levels), GFP_KERNEL); in pwm_backlight_parse_dt()
268 if (!data->levels) in pwm_backlight_parse_dt()
269 return -ENOMEM; in pwm_backlight_parse_dt()
271 ret = of_property_read_u32_array(node, "brightness-levels", in pwm_backlight_parse_dt()
272 data->levels, in pwm_backlight_parse_dt()
277 ret = of_property_read_u32(node, "default-brightness-level", in pwm_backlight_parse_dt()
282 data->dft_brightness = value; in pwm_backlight_parse_dt()
287 * and creates a new pre-computed table. in pwm_backlight_parse_dt()
289 of_property_read_u32(node, "num-interpolated-steps", in pwm_backlight_parse_dt()
294 * brightness-levels table, otherwise we can't interpolate in pwm_backlight_parse_dt()
306 return -EINVAL; in pwm_backlight_parse_dt()
310 * Recalculate the number of brightness levels, now in pwm_backlight_parse_dt()
311 * taking in consideration the number of interpolated in pwm_backlight_parse_dt()
314 num_levels = (num_input_levels - 1) * num_steps + 1; in pwm_backlight_parse_dt()
315 dev_dbg(dev, "new number of brightness levels: %d\n", in pwm_backlight_parse_dt()
325 return -ENOMEM; in pwm_backlight_parse_dt()
331 for (i = 0; i < num_input_levels - 1; i++) { in pwm_backlight_parse_dt()
334 y1 = data->levels[i]; in pwm_backlight_parse_dt()
335 y2 = data->levels[i + 1]; in pwm_backlight_parse_dt()
336 dy = (s64)y2 - y1; in pwm_backlight_parse_dt()
340 div_s64(dy * (x - x1), dx); in pwm_backlight_parse_dt()
351 devm_kfree(dev, data->levels); in pwm_backlight_parse_dt()
352 data->levels = table; in pwm_backlight_parse_dt()
355 data->max_brightness = num_levels - 1; in pwm_backlight_parse_dt()
362 { .compatible = "pwm-backlight" },
371 return -ENODEV; in pwm_backlight_parse_dt()
379 return -ENODEV; in pwm_backlight_brightness_default()
385 unsigned int nlevels = data->max_brightness + 1; in pwm_backlight_is_linear()
386 unsigned int min_val = data->levels[0]; in pwm_backlight_is_linear()
387 unsigned int max_val = data->levels[nlevels - 1]; in pwm_backlight_is_linear()
390 * as (max_val - min_val) == nlevels the error at max_val is less in pwm_backlight_is_linear()
393 unsigned int slope = (128 * (max_val - min_val)) / nlevels; in pwm_backlight_is_linear()
394 unsigned int margin = (max_val - min_val) / 20; /* 5% */ in pwm_backlight_is_linear()
399 unsigned int delta = abs(linear_value - data->levels[i]); in pwm_backlight_is_linear()
410 struct device_node *node = pb->dev->of_node; in pwm_backlight_initial_power_state()
417 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0) in pwm_backlight_initial_power_state()
420 if (pb->power_supply && !regulator_is_enabled(pb->power_supply)) in pwm_backlight_initial_power_state()
423 if (!pwm_is_enabled(pb->pwm)) in pwm_backlight_initial_power_state()
430 gpiod_direction_output(pb->enable_gpio, active); in pwm_backlight_initial_power_state()
433 * Do not change pb->enabled here! pb->enabled essentially in pwm_backlight_initial_power_state()
439 if (!node || !node->phandle) in pwm_backlight_initial_power_state()
453 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev); in pwm_backlight_probe()
463 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata); in pwm_backlight_probe()
465 dev_err(&pdev->dev, "failed to find platform data\n"); in pwm_backlight_probe()
472 if (data->init) { in pwm_backlight_probe()
473 ret = data->init(&pdev->dev); in pwm_backlight_probe()
478 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); in pwm_backlight_probe()
480 ret = -ENOMEM; in pwm_backlight_probe()
484 pb->notify = data->notify; in pwm_backlight_probe()
485 pb->notify_after = data->notify_after; in pwm_backlight_probe()
486 pb->check_fb = data->check_fb; in pwm_backlight_probe()
487 pb->exit = data->exit; in pwm_backlight_probe()
488 pb->dev = &pdev->dev; in pwm_backlight_probe()
489 pb->enabled = false; in pwm_backlight_probe()
490 pb->post_pwm_on_delay = data->post_pwm_on_delay; in pwm_backlight_probe()
491 pb->pwm_off_delay = data->pwm_off_delay; in pwm_backlight_probe()
493 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", in pwm_backlight_probe()
495 if (IS_ERR(pb->enable_gpio)) { in pwm_backlight_probe()
496 ret = PTR_ERR(pb->enable_gpio); in pwm_backlight_probe()
500 pb->power_supply = devm_regulator_get_optional(&pdev->dev, "power"); in pwm_backlight_probe()
501 if (IS_ERR(pb->power_supply)) { in pwm_backlight_probe()
502 ret = PTR_ERR(pb->power_supply); in pwm_backlight_probe()
503 if (ret == -ENODEV) in pwm_backlight_probe()
504 pb->power_supply = NULL; in pwm_backlight_probe()
509 pb->pwm = devm_pwm_get(&pdev->dev, NULL); in pwm_backlight_probe()
510 if (IS_ERR(pb->pwm)) { in pwm_backlight_probe()
511 ret = PTR_ERR(pb->pwm); in pwm_backlight_probe()
512 if (ret != -EPROBE_DEFER) in pwm_backlight_probe()
513 dev_err(&pdev->dev, "unable to request PWM\n"); in pwm_backlight_probe()
517 dev_dbg(&pdev->dev, "got pwm for backlight\n"); in pwm_backlight_probe()
519 /* Sync up PWM state. */ in pwm_backlight_probe()
520 pwm_init_state(pb->pwm, &state); in pwm_backlight_probe()
524 * period, parsed from the DT, in the PWM device. For the non-DT case, in pwm_backlight_probe()
526 * via the PWM lookup table. in pwm_backlight_probe()
528 if (!state.period && (data->pwm_period_ns > 0)) in pwm_backlight_probe()
529 state.period = data->pwm_period_ns; in pwm_backlight_probe()
531 ret = pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_probe()
533 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", in pwm_backlight_probe()
540 if (data->levels) { in pwm_backlight_probe()
541 pb->levels = data->levels; in pwm_backlight_probe()
545 * data->levels is filled. For the non-DT case, data->levels in pwm_backlight_probe()
548 for (i = 0; i <= data->max_brightness; i++) in pwm_backlight_probe()
549 if (data->levels[i] > pb->scale) in pwm_backlight_probe()
550 pb->scale = data->levels[i]; in pwm_backlight_probe()
556 } else if (!data->max_brightness) { in pwm_backlight_probe()
561 * specified. For the non-DT case, max_brightness is usually in pwm_backlight_probe()
565 /* Get the PWM period (in nanoseconds) */ in pwm_backlight_probe()
566 pwm_get_state(pb->pwm, &state); in pwm_backlight_probe()
568 ret = pwm_backlight_brightness_default(&pdev->dev, data, in pwm_backlight_probe()
571 dev_err(&pdev->dev, in pwm_backlight_probe()
576 for (i = 0; i <= data->max_brightness; i++) { in pwm_backlight_probe()
577 if (data->levels[i] > pb->scale) in pwm_backlight_probe()
578 pb->scale = data->levels[i]; in pwm_backlight_probe()
580 pb->levels = data->levels; in pwm_backlight_probe()
586 * That only happens for the non-DT case, where platform data in pwm_backlight_probe()
589 pb->scale = data->max_brightness; in pwm_backlight_probe()
592 pb->lth_brightness = data->lth_brightness * (div_u64(state.period, in pwm_backlight_probe()
593 pb->scale)); in pwm_backlight_probe()
596 props.max_brightness = data->max_brightness; in pwm_backlight_probe()
597 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, in pwm_backlight_probe()
600 dev_err(&pdev->dev, "failed to register backlight\n"); in pwm_backlight_probe()
605 if (data->dft_brightness > data->max_brightness) { in pwm_backlight_probe()
606 dev_warn(&pdev->dev, in pwm_backlight_probe()
608 data->dft_brightness, data->max_brightness); in pwm_backlight_probe()
609 data->dft_brightness = data->max_brightness; in pwm_backlight_probe()
612 bl->props.brightness = data->dft_brightness; in pwm_backlight_probe()
613 bl->props.power = pwm_backlight_initial_power_state(pb); in pwm_backlight_probe()
620 if (data->exit) in pwm_backlight_probe()
621 data->exit(&pdev->dev); in pwm_backlight_probe()
633 pwm_get_state(pb->pwm, &state); in pwm_backlight_remove()
636 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_remove()
638 if (pb->exit) in pwm_backlight_remove()
639 pb->exit(&pdev->dev); in pwm_backlight_remove()
649 pwm_get_state(pb->pwm, &state); in pwm_backlight_shutdown()
652 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_shutdown()
662 if (pb->notify) in pwm_backlight_suspend()
663 pb->notify(pb->dev, 0); in pwm_backlight_suspend()
668 * Note that disabling the PWM doesn't guarantee that the output stays in pwm_backlight_suspend()
669 * at its inactive state. However without the PWM disabled, the PWM in pwm_backlight_suspend()
673 pwm_get_state(pb->pwm, &state); in pwm_backlight_suspend()
676 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_suspend()
678 if (pb->notify_after) in pwm_backlight_suspend()
679 pb->notify_after(pb->dev, 0); in pwm_backlight_suspend()
705 .name = "pwm-backlight",
716 MODULE_DESCRIPTION("PWM based Backlight Driver");
718 MODULE_ALIAS("platform:pwm-backlight");