Lines Matching +full:default +full:- +full:brightness
1 // SPDX-License-Identifier: GPL-2.0-only
33 int brightness);
35 int brightness);
43 if (pb->enabled) in pwm_backlight_power_on()
46 if (pb->power_supply) { in pwm_backlight_power_on()
47 err = regulator_enable(pb->power_supply); in pwm_backlight_power_on()
49 dev_err(pb->dev, "failed to enable power supply\n"); in pwm_backlight_power_on()
52 if (pb->post_pwm_on_delay) in pwm_backlight_power_on()
53 msleep(pb->post_pwm_on_delay); in pwm_backlight_power_on()
55 gpiod_set_value_cansleep(pb->enable_gpio, 1); in pwm_backlight_power_on()
57 pb->enabled = true; in pwm_backlight_power_on()
62 if (!pb->enabled) in pwm_backlight_power_off()
65 gpiod_set_value_cansleep(pb->enable_gpio, 0); in pwm_backlight_power_off()
67 if (pb->pwm_off_delay) in pwm_backlight_power_off()
68 msleep(pb->pwm_off_delay); in pwm_backlight_power_off()
70 if (pb->power_supply) in pwm_backlight_power_off()
71 regulator_disable(pb->power_supply); in pwm_backlight_power_off()
72 pb->enabled = false; in pwm_backlight_power_off()
75 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness, struct pwm_state *state) in compute_duty_cycle() argument
77 unsigned int lth = pb->lth_brightness; in compute_duty_cycle()
80 if (pb->levels) in compute_duty_cycle()
81 duty_cycle = pb->levels[brightness]; in compute_duty_cycle()
83 duty_cycle = brightness; in compute_duty_cycle()
85 duty_cycle *= state->period - lth; in compute_duty_cycle()
86 do_div(duty_cycle, pb->scale); in compute_duty_cycle()
94 int brightness = backlight_get_brightness(bl); in pwm_backlight_update_status() local
97 if (pb->notify) in pwm_backlight_update_status()
98 brightness = pb->notify(pb->dev, brightness); in pwm_backlight_update_status()
100 if (brightness > 0) { in pwm_backlight_update_status()
101 pwm_get_state(pb->pwm, &state); in pwm_backlight_update_status()
102 state.duty_cycle = compute_duty_cycle(pb, brightness, &state); in pwm_backlight_update_status()
104 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_update_status()
110 pwm_get_state(pb->pwm, &state); in pwm_backlight_update_status()
120 state.enabled = !pb->power_supply && !pb->enable_gpio; in pwm_backlight_update_status()
121 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_update_status()
124 if (pb->notify_after) in pwm_backlight_update_status()
125 pb->notify_after(pb->dev, brightness); in pwm_backlight_update_status()
159 * as a fixed-point number in scale in cie1931()
161 * expressed as a fixed-point number, so the above formulas in cie1931()
170 retval += 1ULL << (2*PWM_LUMINANCE_SHIFT - 1); in cie1931()
178 * Create a default correction table for PWM values to create linear brightness
194 data->max_brightness = in pwm_backlight_brightness_default()
197 data->levels = devm_kcalloc(dev, data->max_brightness, in pwm_backlight_brightness_default()
198 sizeof(*data->levels), GFP_KERNEL); in pwm_backlight_brightness_default()
199 if (!data->levels) in pwm_backlight_brightness_default()
200 return -ENOMEM; in pwm_backlight_brightness_default()
203 for (i = 0; i < data->max_brightness; i++) { in pwm_backlight_brightness_default()
205 data->max_brightness) * period; in pwm_backlight_brightness_default()
208 return -EINVAL; in pwm_backlight_brightness_default()
209 data->levels[i] = (unsigned int)retval; in pwm_backlight_brightness_default()
212 data->dft_brightness = data->max_brightness / 2; in pwm_backlight_brightness_default()
213 data->max_brightness--; in pwm_backlight_brightness_default()
221 struct device_node *node = dev->of_node; in pwm_backlight_parse_dt()
231 return -ENODEV; in pwm_backlight_parse_dt()
236 * These values are optional and set as 0 by default, the out values in pwm_backlight_parse_dt()
239 of_property_read_u32(node, "post-pwm-on-delay-ms", in pwm_backlight_parse_dt()
240 &data->post_pwm_on_delay); in pwm_backlight_parse_dt()
241 of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay); in pwm_backlight_parse_dt()
244 * Determine the number of brightness levels, if this property is not in pwm_backlight_parse_dt()
245 * set a default table of brightness levels will be used. in pwm_backlight_parse_dt()
247 prop = of_find_property(node, "brightness-levels", &length); in pwm_backlight_parse_dt()
253 /* read brightness levels from DT property */ in pwm_backlight_parse_dt()
255 data->levels = devm_kcalloc(dev, num_levels, in pwm_backlight_parse_dt()
256 sizeof(*data->levels), GFP_KERNEL); in pwm_backlight_parse_dt()
257 if (!data->levels) in pwm_backlight_parse_dt()
258 return -ENOMEM; in pwm_backlight_parse_dt()
260 ret = of_property_read_u32_array(node, "brightness-levels", in pwm_backlight_parse_dt()
261 data->levels, in pwm_backlight_parse_dt()
266 ret = of_property_read_u32(node, "default-brightness-level", in pwm_backlight_parse_dt()
271 data->dft_brightness = value; in pwm_backlight_parse_dt()
275 * interpolation between each of the values of brightness levels in pwm_backlight_parse_dt()
276 * and creates a new pre-computed table. in pwm_backlight_parse_dt()
278 of_property_read_u32(node, "num-interpolated-steps", in pwm_backlight_parse_dt()
283 * brightness-levels table, otherwise we can't interpolate in pwm_backlight_parse_dt()
295 return -EINVAL; in pwm_backlight_parse_dt()
299 * Recalculate the number of brightness levels, now in pwm_backlight_parse_dt()
303 num_levels = (num_input_levels - 1) * num_steps + 1; in pwm_backlight_parse_dt()
304 dev_dbg(dev, "new number of brightness levels: %d\n", in pwm_backlight_parse_dt()
308 * Create a new table of brightness levels with all the in pwm_backlight_parse_dt()
314 return -ENOMEM; in pwm_backlight_parse_dt()
320 for (i = 0; i < num_input_levels - 1; i++) { in pwm_backlight_parse_dt()
323 y1 = data->levels[i]; in pwm_backlight_parse_dt()
324 y2 = data->levels[i + 1]; in pwm_backlight_parse_dt()
325 dy = (s64)y2 - y1; in pwm_backlight_parse_dt()
329 div_s64(dy * (x - x1), dx); in pwm_backlight_parse_dt()
337 * brightness levels table and replace for the in pwm_backlight_parse_dt()
340 devm_kfree(dev, data->levels); in pwm_backlight_parse_dt()
341 data->levels = table; in pwm_backlight_parse_dt()
344 data->max_brightness = num_levels - 1; in pwm_backlight_parse_dt()
351 { .compatible = "pwm-backlight" },
360 return -ENODEV; in pwm_backlight_parse_dt()
368 return -ENODEV; in pwm_backlight_brightness_default()
374 unsigned int nlevels = data->max_brightness + 1; in pwm_backlight_is_linear()
375 unsigned int min_val = data->levels[0]; in pwm_backlight_is_linear()
376 unsigned int max_val = data->levels[nlevels - 1]; in pwm_backlight_is_linear()
379 * as (max_val - min_val) == nlevels the error at max_val is less in pwm_backlight_is_linear()
382 unsigned int slope = (128 * (max_val - min_val)) / nlevels; in pwm_backlight_is_linear()
383 unsigned int margin = (max_val - min_val) / 20; /* 5% */ in pwm_backlight_is_linear()
388 unsigned int delta = abs(linear_value - data->levels[i]); in pwm_backlight_is_linear()
399 struct device_node *node = pb->dev->of_node; in pwm_backlight_initial_power_state()
406 if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0) in pwm_backlight_initial_power_state()
409 if (pb->power_supply && !regulator_is_enabled(pb->power_supply)) in pwm_backlight_initial_power_state()
412 if (!pwm_is_enabled(pb->pwm)) in pwm_backlight_initial_power_state()
419 gpiod_direction_output(pb->enable_gpio, active); in pwm_backlight_initial_power_state()
422 * Do not change pb->enabled here! pb->enabled essentially in pwm_backlight_initial_power_state()
428 if (!node || !node->phandle) in pwm_backlight_initial_power_state()
442 struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev); in pwm_backlight_probe()
452 ret = pwm_backlight_parse_dt(&pdev->dev, &defdata); in pwm_backlight_probe()
454 return dev_err_probe(&pdev->dev, ret, in pwm_backlight_probe()
460 if (data->init) { in pwm_backlight_probe()
461 ret = data->init(&pdev->dev); in pwm_backlight_probe()
466 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); in pwm_backlight_probe()
468 ret = -ENOMEM; in pwm_backlight_probe()
472 pb->notify = data->notify; in pwm_backlight_probe()
473 pb->notify_after = data->notify_after; in pwm_backlight_probe()
474 pb->exit = data->exit; in pwm_backlight_probe()
475 pb->dev = &pdev->dev; in pwm_backlight_probe()
476 pb->enabled = false; in pwm_backlight_probe()
477 pb->post_pwm_on_delay = data->post_pwm_on_delay; in pwm_backlight_probe()
478 pb->pwm_off_delay = data->pwm_off_delay; in pwm_backlight_probe()
480 pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", in pwm_backlight_probe()
482 if (IS_ERR(pb->enable_gpio)) { in pwm_backlight_probe()
483 ret = dev_err_probe(&pdev->dev, PTR_ERR(pb->enable_gpio), in pwm_backlight_probe()
488 pb->power_supply = devm_regulator_get_optional(&pdev->dev, "power"); in pwm_backlight_probe()
489 if (IS_ERR(pb->power_supply)) { in pwm_backlight_probe()
490 ret = PTR_ERR(pb->power_supply); in pwm_backlight_probe()
491 if (ret == -ENODEV) { in pwm_backlight_probe()
492 pb->power_supply = NULL; in pwm_backlight_probe()
494 dev_err_probe(&pdev->dev, ret, in pwm_backlight_probe()
500 pb->pwm = devm_pwm_get(&pdev->dev, NULL); in pwm_backlight_probe()
501 if (IS_ERR(pb->pwm)) { in pwm_backlight_probe()
502 ret = dev_err_probe(&pdev->dev, PTR_ERR(pb->pwm), in pwm_backlight_probe()
507 dev_dbg(&pdev->dev, "got pwm for backlight\n"); in pwm_backlight_probe()
510 pwm_init_state(pb->pwm, &state); in pwm_backlight_probe()
514 * period, parsed from the DT, in the PWM device. For the non-DT case, in pwm_backlight_probe()
518 if (!state.period && (data->pwm_period_ns > 0)) in pwm_backlight_probe()
519 state.period = data->pwm_period_ns; in pwm_backlight_probe()
521 ret = pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_probe()
523 dev_err_probe(&pdev->dev, ret, in pwm_backlight_probe()
530 if (data->levels) { in pwm_backlight_probe()
531 pb->levels = data->levels; in pwm_backlight_probe()
534 * For the DT case, only when brightness levels is defined in pwm_backlight_probe()
535 * data->levels is filled. For the non-DT case, data->levels in pwm_backlight_probe()
538 for (i = 0; i <= data->max_brightness; i++) in pwm_backlight_probe()
539 if (data->levels[i] > pb->scale) in pwm_backlight_probe()
540 pb->scale = data->levels[i]; in pwm_backlight_probe()
546 } else if (!data->max_brightness) { in pwm_backlight_probe()
548 * If no brightness levels are provided and max_brightness is in pwm_backlight_probe()
549 * not set, use the default brightness table. For the DT case, in pwm_backlight_probe()
550 * max_brightness is set to 0 when brightness levels is not in pwm_backlight_probe()
551 * specified. For the non-DT case, max_brightness is usually in pwm_backlight_probe()
556 pwm_get_state(pb->pwm, &state); in pwm_backlight_probe()
558 ret = pwm_backlight_brightness_default(&pdev->dev, data, in pwm_backlight_probe()
561 dev_err_probe(&pdev->dev, ret, in pwm_backlight_probe()
562 "failed to setup default brightness table\n"); in pwm_backlight_probe()
566 for (i = 0; i <= data->max_brightness; i++) { in pwm_backlight_probe()
567 if (data->levels[i] > pb->scale) in pwm_backlight_probe()
568 pb->scale = data->levels[i]; in pwm_backlight_probe()
570 pb->levels = data->levels; in pwm_backlight_probe()
576 * That only happens for the non-DT case, where platform data in pwm_backlight_probe()
579 pb->scale = data->max_brightness; in pwm_backlight_probe()
582 pb->lth_brightness = data->lth_brightness * (div_u64(state.period, in pwm_backlight_probe()
583 pb->scale)); in pwm_backlight_probe()
586 props.max_brightness = data->max_brightness; in pwm_backlight_probe()
587 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, in pwm_backlight_probe()
590 ret = dev_err_probe(&pdev->dev, PTR_ERR(bl), in pwm_backlight_probe()
595 if (data->dft_brightness > data->max_brightness) { in pwm_backlight_probe()
596 dev_warn(&pdev->dev, in pwm_backlight_probe()
597 "invalid default brightness level: %u, using %u\n", in pwm_backlight_probe()
598 data->dft_brightness, data->max_brightness); in pwm_backlight_probe()
599 data->dft_brightness = data->max_brightness; in pwm_backlight_probe()
602 bl->props.brightness = data->dft_brightness; in pwm_backlight_probe()
603 bl->props.power = pwm_backlight_initial_power_state(pb); in pwm_backlight_probe()
610 if (data->exit) in pwm_backlight_probe()
611 data->exit(&pdev->dev); in pwm_backlight_probe()
623 pwm_get_state(pb->pwm, &state); in pwm_backlight_remove()
626 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_remove()
628 if (pb->exit) in pwm_backlight_remove()
629 pb->exit(&pdev->dev); in pwm_backlight_remove()
639 pwm_get_state(pb->pwm, &state); in pwm_backlight_shutdown()
642 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_shutdown()
652 if (pb->notify) in pwm_backlight_suspend()
653 pb->notify(pb->dev, 0); in pwm_backlight_suspend()
663 pwm_get_state(pb->pwm, &state); in pwm_backlight_suspend()
666 pwm_apply_might_sleep(pb->pwm, &state); in pwm_backlight_suspend()
668 if (pb->notify_after) in pwm_backlight_suspend()
669 pb->notify_after(pb->dev, 0); in pwm_backlight_suspend()
695 .name = "pwm-backlight",
708 MODULE_ALIAS("platform:pwm-backlight");