Lines Matching +full:default +full:- +full:brightness
1 // SPDX-License-Identifier: GPL-2.0-only
35 int brightness);
37 int brightness);
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()
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()
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()
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()
89 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness) in compute_duty_cycle() argument
91 unsigned int lth = pb->lth_brightness; in compute_duty_cycle()
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()
100 duty_cycle = 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()
111 int brightness = backlight_get_brightness(bl); 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()
117 if (brightness > 0) { 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()
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()
243 return -ENODEV; in pwm_backlight_parse_dt()
248 * These values are optional and set as 0 by default, the out values in pwm_backlight_parse_dt()
251 of_property_read_u32(node, "post-pwm-on-delay-ms", in pwm_backlight_parse_dt()
252 &data->post_pwm_on_delay); in pwm_backlight_parse_dt()
253 of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay); in pwm_backlight_parse_dt()
256 * Determine the number of brightness levels, if this property is not in pwm_backlight_parse_dt()
257 * set a default table of brightness levels will be used. in pwm_backlight_parse_dt()
259 prop = of_find_property(node, "brightness-levels", &length); in pwm_backlight_parse_dt()
263 data->max_brightness = length / sizeof(u32); in pwm_backlight_parse_dt()
265 /* read brightness levels from DT property */ in pwm_backlight_parse_dt()
266 if (data->max_brightness > 0) { in pwm_backlight_parse_dt()
267 size_t size = sizeof(*data->levels) * data->max_brightness; in pwm_backlight_parse_dt()
270 data->levels = devm_kzalloc(dev, size, GFP_KERNEL); in pwm_backlight_parse_dt()
271 if (!data->levels) in pwm_backlight_parse_dt()
272 return -ENOMEM; in pwm_backlight_parse_dt()
274 ret = of_property_read_u32_array(node, "brightness-levels", in pwm_backlight_parse_dt()
275 data->levels, in pwm_backlight_parse_dt()
276 data->max_brightness); in pwm_backlight_parse_dt()
280 ret = of_property_read_u32(node, "default-brightness-level", in pwm_backlight_parse_dt()
285 data->dft_brightness = value; in pwm_backlight_parse_dt()
289 * interpolation between each of the values of brightness levels in pwm_backlight_parse_dt()
290 * and creates a new pre-computed table. in pwm_backlight_parse_dt()
292 of_property_read_u32(node, "num-interpolated-steps", in pwm_backlight_parse_dt()
297 * brightness-levels table, otherwise we can't interpolate in pwm_backlight_parse_dt()
301 if (data->max_brightness < 2) { in pwm_backlight_parse_dt()
303 return -EINVAL; in pwm_backlight_parse_dt()
307 * Recalculate the number of brightness levels, now in pwm_backlight_parse_dt()
311 for (i = 0; i < data->max_brightness - 1; i++) { in pwm_backlight_parse_dt()
312 if ((data->levels[i + 1] - data->levels[i]) / in pwm_backlight_parse_dt()
319 dev_dbg(dev, "new number of brightness levels: %d\n", in pwm_backlight_parse_dt()
323 * Create a new table of brightness levels with all the in pwm_backlight_parse_dt()
329 return -ENOMEM; in pwm_backlight_parse_dt()
333 for (i = 0; i < data->max_brightness - 1; i++) { in pwm_backlight_parse_dt()
334 value = data->levels[i]; in pwm_backlight_parse_dt()
335 n = (data->levels[i + 1] - value) / num_steps; in pwm_backlight_parse_dt()
343 table[levels_count] = data->levels[i]; in pwm_backlight_parse_dt()
347 table[levels_count] = data->levels[i]; in pwm_backlight_parse_dt()
351 * brightness levels table and replace for the in pwm_backlight_parse_dt()
354 devm_kfree(dev, data->levels); in pwm_backlight_parse_dt()
355 data->levels = table; in pwm_backlight_parse_dt()
359 * of brightness levels. in pwm_backlight_parse_dt()
361 data->max_brightness = num_levels; in pwm_backlight_parse_dt()
364 data->max_brightness--; in pwm_backlight_parse_dt()
371 { .compatible = "pwm-backlight" },
380 return -ENODEV; in pwm_backlight_parse_dt()
388 return -ENODEV; in pwm_backlight_brightness_default()
394 unsigned int nlevels = data->max_brightness + 1; in pwm_backlight_is_linear()
395 unsigned int min_val = data->levels[0]; in pwm_backlight_is_linear()
396 unsigned int max_val = data->levels[nlevels - 1]; in pwm_backlight_is_linear()
399 * as (max_val - min_val) == nlevels the error at max_val is less in pwm_backlight_is_linear()
402 unsigned int slope = (128 * (max_val - min_val)) / nlevels; in pwm_backlight_is_linear()
403 unsigned int margin = (max_val - min_val) / 20; /* 5% */ in pwm_backlight_is_linear()
408 unsigned int delta = abs(linear_value - data->levels[i]); in pwm_backlight_is_linear()
419 struct device_node *node = pb->dev->of_node; in pwm_backlight_initial_power_state()
422 if (!node || !node->phandle) in pwm_backlight_initial_power_state()
433 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0) in pwm_backlight_initial_power_state()
437 if (!regulator_is_enabled(pb->power_supply)) in pwm_backlight_initial_power_state()
441 if (!pwm_is_enabled(pb->pwm)) in pwm_backlight_initial_power_state()
449 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev); in pwm_backlight_probe()
453 struct device_node *node = pdev->dev.of_node; in pwm_backlight_probe()
460 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata); in pwm_backlight_probe()
462 dev_err(&pdev->dev, "failed to find platform data\n"); in pwm_backlight_probe()
469 if (data->init) { in pwm_backlight_probe()
470 ret = data->init(&pdev->dev); in pwm_backlight_probe()
475 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); in pwm_backlight_probe()
477 ret = -ENOMEM; in pwm_backlight_probe()
481 pb->notify = data->notify; in pwm_backlight_probe()
482 pb->notify_after = data->notify_after; in pwm_backlight_probe()
483 pb->check_fb = data->check_fb; in pwm_backlight_probe()
484 pb->exit = data->exit; in pwm_backlight_probe()
485 pb->dev = &pdev->dev; in pwm_backlight_probe()
486 pb->enabled = false; in pwm_backlight_probe()
487 pb->post_pwm_on_delay = data->post_pwm_on_delay; in pwm_backlight_probe()
488 pb->pwm_off_delay = data->pwm_off_delay; in pwm_backlight_probe()
490 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", in pwm_backlight_probe()
492 if (IS_ERR(pb->enable_gpio)) { in pwm_backlight_probe()
493 ret = PTR_ERR(pb->enable_gpio); in pwm_backlight_probe()
499 * is, if gpiod_get_direction returns either 1 or -EINVAL, change the in pwm_backlight_probe()
505 if (pb->enable_gpio && in pwm_backlight_probe()
506 gpiod_get_direction(pb->enable_gpio) != 0) in pwm_backlight_probe()
507 gpiod_direction_output(pb->enable_gpio, 1); in pwm_backlight_probe()
509 pb->power_supply = devm_regulator_get(&pdev->dev, "power"); in pwm_backlight_probe()
510 if (IS_ERR(pb->power_supply)) { in pwm_backlight_probe()
511 ret = PTR_ERR(pb->power_supply); in pwm_backlight_probe()
515 pb->pwm = devm_pwm_get(&pdev->dev, NULL); in pwm_backlight_probe()
516 if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) { in pwm_backlight_probe()
517 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n"); in pwm_backlight_probe()
518 pb->legacy = true; in pwm_backlight_probe()
519 pb->pwm = pwm_request(data->pwm_id, "pwm-backlight"); in pwm_backlight_probe()
522 if (IS_ERR(pb->pwm)) { in pwm_backlight_probe()
523 ret = PTR_ERR(pb->pwm); in pwm_backlight_probe()
524 if (ret != -EPROBE_DEFER) in pwm_backlight_probe()
525 dev_err(&pdev->dev, "unable to request PWM\n"); in pwm_backlight_probe()
529 dev_dbg(&pdev->dev, "got pwm for backlight\n"); in pwm_backlight_probe()
532 pwm_init_state(pb->pwm, &state); in pwm_backlight_probe()
536 * period, parsed from the DT, in the PWM device. For the non-DT case, in pwm_backlight_probe()
540 if (!state.period && (data->pwm_period_ns > 0)) in pwm_backlight_probe()
541 state.period = data->pwm_period_ns; in pwm_backlight_probe()
543 ret = pwm_apply_state(pb->pwm, &state); in pwm_backlight_probe()
545 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", in pwm_backlight_probe()
552 if (data->levels) { in pwm_backlight_probe()
553 pb->levels = data->levels; in pwm_backlight_probe()
556 * For the DT case, only when brightness levels is defined in pwm_backlight_probe()
557 * data->levels is filled. For the non-DT case, data->levels in pwm_backlight_probe()
560 for (i = 0; i <= data->max_brightness; i++) in pwm_backlight_probe()
561 if (data->levels[i] > pb->scale) in pwm_backlight_probe()
562 pb->scale = data->levels[i]; in pwm_backlight_probe()
568 } else if (!data->max_brightness) { in pwm_backlight_probe()
570 * If no brightness levels are provided and max_brightness is in pwm_backlight_probe()
571 * not set, use the default brightness table. For the DT case, in pwm_backlight_probe()
572 * max_brightness is set to 0 when brightness levels is not in pwm_backlight_probe()
573 * specified. For the non-DT case, max_brightness is usually in pwm_backlight_probe()
578 pwm_get_state(pb->pwm, &state); in pwm_backlight_probe()
580 ret = pwm_backlight_brightness_default(&pdev->dev, data, in pwm_backlight_probe()
583 dev_err(&pdev->dev, in pwm_backlight_probe()
584 "failed to setup default brightness table\n"); in pwm_backlight_probe()
588 for (i = 0; i <= data->max_brightness; i++) { in pwm_backlight_probe()
589 if (data->levels[i] > pb->scale) in pwm_backlight_probe()
590 pb->scale = data->levels[i]; in pwm_backlight_probe()
592 pb->levels = data->levels; in pwm_backlight_probe()
598 * That only happens for the non-DT case, where platform data in pwm_backlight_probe()
601 pb->scale = data->max_brightness; in pwm_backlight_probe()
604 pb->lth_brightness = data->lth_brightness * (div_u64(state.period, in pwm_backlight_probe()
605 pb->scale)); in pwm_backlight_probe()
608 props.max_brightness = data->max_brightness; in pwm_backlight_probe()
609 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, in pwm_backlight_probe()
612 dev_err(&pdev->dev, "failed to register backlight\n"); in pwm_backlight_probe()
614 if (pb->legacy) in pwm_backlight_probe()
615 pwm_free(pb->pwm); in pwm_backlight_probe()
619 if (data->dft_brightness > data->max_brightness) { in pwm_backlight_probe()
620 dev_warn(&pdev->dev, in pwm_backlight_probe()
621 "invalid default brightness level: %u, using %u\n", in pwm_backlight_probe()
622 data->dft_brightness, data->max_brightness); in pwm_backlight_probe()
623 data->dft_brightness = data->max_brightness; in pwm_backlight_probe()
626 bl->props.brightness = data->dft_brightness; in pwm_backlight_probe()
627 bl->props.power = pwm_backlight_initial_power_state(pb); in pwm_backlight_probe()
634 if (data->exit) in pwm_backlight_probe()
635 data->exit(&pdev->dev); in pwm_backlight_probe()
647 if (pb->exit) in pwm_backlight_remove()
648 pb->exit(&pdev->dev); in pwm_backlight_remove()
649 if (pb->legacy) in pwm_backlight_remove()
650 pwm_free(pb->pwm); in pwm_backlight_remove()
669 if (pb->notify) in pwm_backlight_suspend()
670 pb->notify(pb->dev, 0); in pwm_backlight_suspend()
674 if (pb->notify_after) in pwm_backlight_suspend()
675 pb->notify_after(pb->dev, 0); in pwm_backlight_suspend()
701 .name = "pwm-backlight",
714 MODULE_ALIAS("platform:pwm-backlight");