Lines Matching +full:pwm +full:- +full:active +full:- +full:state

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
44 struct pwm_state state; in pwm_backlight_power_on() local
47 pwm_get_state(pb->pwm, &state); in pwm_backlight_power_on()
48 if (pb->enabled) in pwm_backlight_power_on()
51 err = regulator_enable(pb->power_supply); in pwm_backlight_power_on()
53 dev_err(pb->dev, "failed to enable power supply\n"); in pwm_backlight_power_on()
55 state.enabled = true; in pwm_backlight_power_on()
56 pwm_apply_state(pb->pwm, &state); in pwm_backlight_power_on()
58 if (pb->post_pwm_on_delay) in pwm_backlight_power_on()
59 msleep(pb->post_pwm_on_delay); in pwm_backlight_power_on()
61 if (pb->enable_gpio) in pwm_backlight_power_on()
62 gpiod_set_value_cansleep(pb->enable_gpio, 1); in pwm_backlight_power_on()
64 pb->enabled = true; in pwm_backlight_power_on()
69 struct pwm_state state; in pwm_backlight_power_off() local
71 pwm_get_state(pb->pwm, &state); in pwm_backlight_power_off()
72 if (!pb->enabled) in pwm_backlight_power_off()
75 if (pb->enable_gpio) in pwm_backlight_power_off()
76 gpiod_set_value_cansleep(pb->enable_gpio, 0); in pwm_backlight_power_off()
78 if (pb->pwm_off_delay) in pwm_backlight_power_off()
79 msleep(pb->pwm_off_delay); in pwm_backlight_power_off()
81 state.enabled = false; in pwm_backlight_power_off()
82 state.duty_cycle = 0; in pwm_backlight_power_off()
83 pwm_apply_state(pb->pwm, &state); in pwm_backlight_power_off()
85 regulator_disable(pb->power_supply); in pwm_backlight_power_off()
86 pb->enabled = false; in pwm_backlight_power_off()
91 unsigned int lth = pb->lth_brightness; in compute_duty_cycle()
92 struct pwm_state state; in compute_duty_cycle() local
95 pwm_get_state(pb->pwm, &state); in compute_duty_cycle()
97 if (pb->levels) in compute_duty_cycle()
98 duty_cycle = pb->levels[brightness]; in compute_duty_cycle()
102 duty_cycle *= state.period - lth; in compute_duty_cycle()
103 do_div(duty_cycle, pb->scale); in compute_duty_cycle()
112 struct pwm_state state; in pwm_backlight_update_status() local
114 if (pb->notify) in pwm_backlight_update_status()
115 brightness = pb->notify(pb->dev, brightness); in pwm_backlight_update_status()
118 pwm_get_state(pb->pwm, &state); in pwm_backlight_update_status()
119 state.duty_cycle = compute_duty_cycle(pb, brightness); in pwm_backlight_update_status()
120 pwm_apply_state(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.
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()
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()
314 num_levels = (num_input_levels - 1) * num_steps + 1; 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()
411 bool active = true; in pwm_backlight_initial_power_state() local
415 * or output) and off then the backlight is not currently active. 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()
418 active = false; in pwm_backlight_initial_power_state()
420 if (!regulator_is_enabled(pb->power_supply)) in pwm_backlight_initial_power_state()
421 active = false; in pwm_backlight_initial_power_state()
423 if (!pwm_is_enabled(pb->pwm)) in pwm_backlight_initial_power_state()
424 active = false; in pwm_backlight_initial_power_state()
427 * Synchronize the enable_gpio with the observed state of the in pwm_backlight_initial_power_state()
430 if (pb->enable_gpio) in pwm_backlight_initial_power_state()
431 gpiod_direction_output(pb->enable_gpio, active); in pwm_backlight_initial_power_state()
434 * Do not change pb->enabled here! pb->enabled essentially in pwm_backlight_initial_power_state()
440 if (!node || !node->phandle) in pwm_backlight_initial_power_state()
449 return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN; in pwm_backlight_initial_power_state()
454 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev); in pwm_backlight_probe()
458 struct device_node *node = pdev->dev.of_node; in pwm_backlight_probe()
460 struct pwm_state state; in pwm_backlight_probe() local
465 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata); in pwm_backlight_probe()
467 dev_err(&pdev->dev, "failed to find platform data\n"); in pwm_backlight_probe()
474 if (data->init) { in pwm_backlight_probe()
475 ret = data->init(&pdev->dev); in pwm_backlight_probe()
480 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); in pwm_backlight_probe()
482 ret = -ENOMEM; in pwm_backlight_probe()
486 pb->notify = data->notify; in pwm_backlight_probe()
487 pb->notify_after = data->notify_after; in pwm_backlight_probe()
488 pb->check_fb = data->check_fb; in pwm_backlight_probe()
489 pb->exit = data->exit; in pwm_backlight_probe()
490 pb->dev = &pdev->dev; in pwm_backlight_probe()
491 pb->enabled = false; in pwm_backlight_probe()
492 pb->post_pwm_on_delay = data->post_pwm_on_delay; in pwm_backlight_probe()
493 pb->pwm_off_delay = data->pwm_off_delay; in pwm_backlight_probe()
495 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", in pwm_backlight_probe()
497 if (IS_ERR(pb->enable_gpio)) { in pwm_backlight_probe()
498 ret = PTR_ERR(pb->enable_gpio); in pwm_backlight_probe()
502 pb->power_supply = devm_regulator_get(&pdev->dev, "power"); in pwm_backlight_probe()
503 if (IS_ERR(pb->power_supply)) { in pwm_backlight_probe()
504 ret = PTR_ERR(pb->power_supply); in pwm_backlight_probe()
508 pb->pwm = devm_pwm_get(&pdev->dev, NULL); in pwm_backlight_probe()
509 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) { in pwm_backlight_probe()
510 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n"); in pwm_backlight_probe()
511 pb->legacy = true; in pwm_backlight_probe()
512 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight"); in pwm_backlight_probe()
515 if (IS_ERR(pb->pwm)) { in pwm_backlight_probe()
516 ret = PTR_ERR(pb->pwm); in pwm_backlight_probe()
517 if (ret != -EPROBE_DEFER) in pwm_backlight_probe()
518 dev_err(&pdev->dev, "unable to request PWM\n"); in pwm_backlight_probe()
522 dev_dbg(&pdev->dev, "got pwm for backlight\n"); in pwm_backlight_probe()
524 /* Sync up PWM state. */ in pwm_backlight_probe()
525 pwm_init_state(pb->pwm, &state); in pwm_backlight_probe()
529 * period, parsed from the DT, in the PWM device. For the non-DT case, in pwm_backlight_probe()
531 * via the PWM lookup table. in pwm_backlight_probe()
533 if (!state.period && (data->pwm_period_ns > 0)) in pwm_backlight_probe()
534 state.period = data->pwm_period_ns; in pwm_backlight_probe()
536 ret = pwm_apply_state(pb->pwm, &state); in pwm_backlight_probe()
538 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", in pwm_backlight_probe()
545 if (data->levels) { in pwm_backlight_probe()
546 pb->levels = data->levels; in pwm_backlight_probe()
550 * data->levels is filled. For the non-DT case, data->levels in pwm_backlight_probe()
553 for (i = 0; i <= data->max_brightness; i++) in pwm_backlight_probe()
554 if (data->levels[i] > pb->scale) in pwm_backlight_probe()
555 pb->scale = data->levels[i]; in pwm_backlight_probe()
561 } else if (!data->max_brightness) { in pwm_backlight_probe()
566 * specified. For the non-DT case, max_brightness is usually in pwm_backlight_probe()
570 /* Get the PWM period (in nanoseconds) */ in pwm_backlight_probe()
571 pwm_get_state(pb->pwm, &state); in pwm_backlight_probe()
573 ret = pwm_backlight_brightness_default(&pdev->dev, data, in pwm_backlight_probe()
574 state.period); in pwm_backlight_probe()
576 dev_err(&pdev->dev, in pwm_backlight_probe()
581 for (i = 0; i <= data->max_brightness; i++) { in pwm_backlight_probe()
582 if (data->levels[i] > pb->scale) in pwm_backlight_probe()
583 pb->scale = data->levels[i]; in pwm_backlight_probe()
585 pb->levels = data->levels; in pwm_backlight_probe()
591 * That only happens for the non-DT case, where platform data in pwm_backlight_probe()
594 pb->scale = data->max_brightness; in pwm_backlight_probe()
597 pb->lth_brightness = data->lth_brightness * (div_u64(state.period, in pwm_backlight_probe()
598 pb->scale)); in pwm_backlight_probe()
601 props.max_brightness = data->max_brightness; in pwm_backlight_probe()
602 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, in pwm_backlight_probe()
605 dev_err(&pdev->dev, "failed to register backlight\n"); in pwm_backlight_probe()
607 if (pb->legacy) in pwm_backlight_probe()
608 pwm_free(pb->pwm); in pwm_backlight_probe()
612 if (data->dft_brightness > data->max_brightness) { in pwm_backlight_probe()
613 dev_warn(&pdev->dev, in pwm_backlight_probe()
615 data->dft_brightness, data->max_brightness); in pwm_backlight_probe()
616 data->dft_brightness = data->max_brightness; in pwm_backlight_probe()
619 bl->props.brightness = data->dft_brightness; in pwm_backlight_probe()
620 bl->props.power = pwm_backlight_initial_power_state(pb); in pwm_backlight_probe()
627 if (data->exit) in pwm_backlight_probe()
628 data->exit(&pdev->dev); in pwm_backlight_probe()
640 if (pb->exit) in pwm_backlight_remove()
641 pb->exit(&pdev->dev); in pwm_backlight_remove()
642 if (pb->legacy) in pwm_backlight_remove()
643 pwm_free(pb->pwm); in pwm_backlight_remove()
662 if (pb->notify) in pwm_backlight_suspend()
663 pb->notify(pb->dev, 0); in pwm_backlight_suspend()
667 if (pb->notify_after) in pwm_backlight_suspend()
668 pb->notify_after(pb->dev, 0); in pwm_backlight_suspend()
694 .name = "pwm-backlight",
705 MODULE_DESCRIPTION("PWM based Backlight Driver");
707 MODULE_ALIAS("platform:pwm-backlight");