|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2023, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2023-02-25 GuEe-GUI the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rthw.h> |
| 12 | +#include <rtthread.h> |
| 13 | +#include <rtdevice.h> |
| 14 | + |
| 15 | +#define DBG_TAG "backlight.gpio" |
| 16 | +#define DBG_LVL DBG_INFO |
| 17 | +#include <rtdbg.h> |
| 18 | + |
| 19 | +struct gpio_backlight |
| 20 | +{ |
| 21 | + struct rt_backlight_device parent; |
| 22 | + |
| 23 | + rt_base_t pin; |
| 24 | + rt_uint8_t active_val; |
| 25 | +}; |
| 26 | + |
| 27 | +#define raw_to_gpio_backlight(raw) rt_container_of(raw, struct gpio_backlight, parent) |
| 28 | + |
| 29 | +static rt_err_t gpio_backlight_update_status(struct rt_backlight_device *bl) |
| 30 | +{ |
| 31 | + rt_uint8_t brightness; |
| 32 | + struct gpio_backlight *gbl = raw_to_gpio_backlight(bl); |
| 33 | + |
| 34 | + rt_pin_mode(gbl->pin, PIN_MODE_OUTPUT); |
| 35 | + |
| 36 | + brightness = rt_backlight_power_brightness(bl); |
| 37 | + |
| 38 | + if (!gbl->active_val) |
| 39 | + { |
| 40 | + brightness = !brightness; |
| 41 | + } |
| 42 | + |
| 43 | + rt_pin_write(gbl->pin, brightness); |
| 44 | + |
| 45 | + return RT_EOK; |
| 46 | +} |
| 47 | + |
| 48 | +static struct rt_backlight_ops gpio_backlight_ops = |
| 49 | +{ |
| 50 | + .update_status = gpio_backlight_update_status, |
| 51 | +}; |
| 52 | + |
| 53 | +static rt_err_t gpio_backlight_probe(struct rt_platform_device *pdev) |
| 54 | +{ |
| 55 | + rt_err_t err; |
| 56 | + rt_bool_t def_value; |
| 57 | + struct rt_device *dev = &pdev->parent; |
| 58 | + struct gpio_backlight *gbl = rt_calloc(1, sizeof(*gbl)); |
| 59 | + |
| 60 | + if (!gbl) |
| 61 | + { |
| 62 | + return -RT_ENOMEM; |
| 63 | + } |
| 64 | + |
| 65 | + def_value = rt_dm_dev_prop_read_bool(dev, "default-on"); |
| 66 | + |
| 67 | + gbl->pin = rt_pin_get_named_pin(dev, RT_NULL, 0, RT_NULL, &gbl->active_val); |
| 68 | + |
| 69 | + if (gbl->pin < 0) |
| 70 | + { |
| 71 | + err = gbl->pin; |
| 72 | + |
| 73 | + goto _fail; |
| 74 | + } |
| 75 | + |
| 76 | + /* Set the initial power state */ |
| 77 | + if (!dev->ofw_node || !rt_dm_dev_prop_read_bool(dev, "phandle")) |
| 78 | + { |
| 79 | + gbl->parent.props.power = def_value ? |
| 80 | + RT_BACKLIGHT_POWER_UNBLANK : RT_BACKLIGHT_POWER_POWERDOWN; |
| 81 | + } |
| 82 | + else if (rt_pin_read(gbl->pin) != gbl->active_val) |
| 83 | + { |
| 84 | + gbl->parent.props.power = RT_BACKLIGHT_POWER_POWERDOWN; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + gbl->parent.props.power = RT_BACKLIGHT_POWER_UNBLANK; |
| 89 | + } |
| 90 | + |
| 91 | + gbl->parent.props.max_brightness = 1; |
| 92 | + gbl->parent.ops = &gpio_backlight_ops; |
| 93 | + |
| 94 | + if ((err = rt_backlight_register(&gbl->parent))) |
| 95 | + { |
| 96 | + goto _fail; |
| 97 | + } |
| 98 | + |
| 99 | + rt_pin_mode(gbl->pin, PIN_MODE_OUTPUT); |
| 100 | + rt_backlight_set_brightness(&gbl->parent, 1); |
| 101 | + |
| 102 | + return RT_EOK; |
| 103 | + |
| 104 | +_fail: |
| 105 | + rt_free(gbl); |
| 106 | + |
| 107 | + return err; |
| 108 | +} |
| 109 | + |
| 110 | +static rt_err_t gpio_backlight_remove(struct rt_platform_device *pdev) |
| 111 | +{ |
| 112 | + struct gpio_backlight *gbl = pdev->parent.user_data; |
| 113 | + |
| 114 | + rt_backlight_unregister(&gbl->parent); |
| 115 | + |
| 116 | + rt_free(gbl); |
| 117 | + |
| 118 | + return RT_EOK; |
| 119 | +} |
| 120 | + |
| 121 | +static const struct rt_ofw_node_id gpio_backlight_ofw_ids[] = |
| 122 | +{ |
| 123 | + { .compatible = "gpio-backlight" }, |
| 124 | + { /* sentinel */ } |
| 125 | +}; |
| 126 | + |
| 127 | +static struct rt_platform_driver gpio_backlight_driver = |
| 128 | +{ |
| 129 | + .name = "gpio-backlight", |
| 130 | + .ids = gpio_backlight_ofw_ids, |
| 131 | + |
| 132 | + .probe = gpio_backlight_probe, |
| 133 | + .remove = gpio_backlight_remove, |
| 134 | +}; |
| 135 | +RT_PLATFORM_DRIVER_EXPORT(gpio_backlight_driver); |
0 commit comments