1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backlight driver for the Kinetic KTD253
4 * Based on code and know-how from the Samsung GT-S7710
5 * Gareth Phillips <gareth.phillips@samsung.com>
7 #include <linux/backlight.h>
8 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/limits.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/slab.h>
21 /* Current ratio is n/32 from 1/32 to 32/32 */
22 #define KTD253_MIN_RATIO 1
23 #define KTD253_MAX_RATIO 32
24 #define KTD253_DEFAULT_RATIO 13
26 #define KTD253_T_LOW_NS (200 + 10) /* Additional 10ns as safety factor */
27 #define KTD253_T_HIGH_NS (200 + 10) /* Additional 10ns as safety factor */
28 #define KTD253_T_OFF_MS 3
30 struct ktd253_backlight {
32 struct backlight_device *bl;
33 struct gpio_desc *gpiod;
37 static int ktd253_backlight_update_status(struct backlight_device *bl)
39 struct ktd253_backlight *ktd253 = bl_get_data(bl);
40 int brightness = backlight_get_brightness(bl);
42 u16 current_ratio = ktd253->ratio;
45 dev_dbg(ktd253->dev, "new brightness/ratio: %d/32\n", brightness);
47 target_ratio = brightness;
49 if (target_ratio == current_ratio)
50 /* This is already right */
53 if (target_ratio == 0) {
54 gpiod_set_value_cansleep(ktd253->gpiod, 0);
56 * We need to keep the GPIO low for at least this long
57 * to actually switch the KTD253 off.
59 msleep(KTD253_T_OFF_MS);
64 if (current_ratio == 0) {
65 gpiod_set_value_cansleep(ktd253->gpiod, 1);
66 ndelay(KTD253_T_HIGH_NS);
67 /* We always fall back to this when we power on */
68 current_ratio = KTD253_MAX_RATIO;
73 * The loop to set the correct current level is performed
74 * with interrupts disabled as it is timing critical.
75 * The maximum number of cycles of the loop is 32
76 * so the time taken will be (T_LOW_NS + T_HIGH_NS + loop_time) * 32,
78 local_irq_save(flags);
79 while (current_ratio != target_ratio) {
81 * These GPIO operations absolutely can NOT sleep so no
82 * _cansleep suffixes, and no using GPIO expanders on
83 * slow buses for this!
85 gpiod_set_value(ktd253->gpiod, 0);
86 ndelay(KTD253_T_LOW_NS);
87 gpiod_set_value(ktd253->gpiod, 1);
88 ndelay(KTD253_T_HIGH_NS);
89 /* After 1/32 we loop back to 32/32 */
90 if (current_ratio == KTD253_MIN_RATIO)
91 current_ratio = KTD253_MAX_RATIO;
95 local_irq_restore(flags);
96 ktd253->ratio = current_ratio;
98 dev_dbg(ktd253->dev, "new ratio set to %d/32\n", target_ratio);
103 static const struct backlight_ops ktd253_backlight_ops = {
104 .options = BL_CORE_SUSPENDRESUME,
105 .update_status = ktd253_backlight_update_status,
108 static int ktd253_backlight_probe(struct platform_device *pdev)
110 struct device *dev = &pdev->dev;
111 struct backlight_device *bl;
112 struct ktd253_backlight *ktd253;
117 ktd253 = devm_kzalloc(dev, sizeof(*ktd253), GFP_KERNEL);
122 ret = device_property_read_u32(dev, "max-brightness", &max_brightness);
124 max_brightness = KTD253_MAX_RATIO;
125 if (max_brightness > KTD253_MAX_RATIO) {
126 /* Clamp brightness to hardware max */
127 dev_err(dev, "illegal max brightness specified\n");
128 max_brightness = KTD253_MAX_RATIO;
131 ret = device_property_read_u32(dev, "default-brightness", &brightness);
133 brightness = KTD253_DEFAULT_RATIO;
134 if (brightness > max_brightness) {
135 /* Clamp default brightness to max brightness */
136 dev_err(dev, "default brightness exceeds max brightness\n");
137 brightness = max_brightness;
140 ktd253->gpiod = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
141 if (IS_ERR(ktd253->gpiod)) {
142 ret = PTR_ERR(ktd253->gpiod);
143 if (ret != -EPROBE_DEFER)
144 dev_err(dev, "gpio line missing or invalid.\n");
147 gpiod_set_consumer_name(ktd253->gpiod, dev_name(dev));
148 /* Bring backlight to a known off state */
149 msleep(KTD253_T_OFF_MS);
151 bl = devm_backlight_device_register(dev, dev_name(dev), dev, ktd253,
152 &ktd253_backlight_ops, NULL);
154 dev_err(dev, "failed to register backlight\n");
157 bl->props.max_brightness = max_brightness;
158 /* When we just enable the GPIO line we set max brightness */
160 bl->props.brightness = brightness;
161 bl->props.power = FB_BLANK_UNBLANK;
163 bl->props.brightness = 0;
164 bl->props.power = FB_BLANK_POWERDOWN;
168 platform_set_drvdata(pdev, bl);
169 backlight_update_status(bl);
174 static const struct of_device_id ktd253_backlight_of_match[] = {
175 { .compatible = "kinetic,ktd253" },
178 MODULE_DEVICE_TABLE(of, ktd253_backlight_of_match);
180 static struct platform_driver ktd253_backlight_driver = {
182 .name = "ktd253-backlight",
183 .of_match_table = ktd253_backlight_of_match,
185 .probe = ktd253_backlight_probe,
187 module_platform_driver(ktd253_backlight_driver);
189 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
190 MODULE_DESCRIPTION("Kinetic KTD253 Backlight Driver");
191 MODULE_LICENSE("GPL");
192 MODULE_ALIAS("platform:ktd253-backlight");