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
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_CRIT_NS 100000 /* 100 us, now it doesn't look good */
29 #define KTD253_T_OFF_MS 3
31 struct ktd253_backlight {
33 struct backlight_device *bl;
34 struct gpio_desc *gpiod;
38 static void ktd253_backlight_set_max_ratio(struct ktd253_backlight *ktd253)
40 gpiod_set_value_cansleep(ktd253->gpiod, 1);
41 ndelay(KTD253_T_HIGH_NS);
42 /* We always fall back to this when we power on */
45 static int ktd253_backlight_stepdown(struct ktd253_backlight *ktd253)
48 * These GPIO operations absolutely can NOT sleep so no _cansleep
49 * suffixes, and no using GPIO expanders on slow buses for this!
51 * The maximum number of cycles of the loop is 32 so the time taken
52 * should nominally be:
53 * (T_LOW_NS + T_HIGH_NS + loop_time) * 32
55 * Architectures do not always support ndelay() and we will get a few us
56 * instead. If we get to a critical time limit an interrupt has likely
57 * occured in the low part of the loop and we need to restart from the
58 * top so we have the backlight in a known state.
63 gpiod_set_value(ktd253->gpiod, 0);
64 ndelay(KTD253_T_LOW_NS);
65 gpiod_set_value(ktd253->gpiod, 1);
66 ns = ktime_get_ns() - ns;
67 if (ns >= KTD253_T_OFF_CRIT_NS) {
68 dev_err(ktd253->dev, "PCM on backlight took too long (%llu ns)\n", ns);
71 ndelay(KTD253_T_HIGH_NS);
75 static int ktd253_backlight_update_status(struct backlight_device *bl)
77 struct ktd253_backlight *ktd253 = bl_get_data(bl);
78 int brightness = backlight_get_brightness(bl);
80 u16 current_ratio = ktd253->ratio;
83 dev_dbg(ktd253->dev, "new brightness/ratio: %d/32\n", brightness);
85 target_ratio = brightness;
87 if (target_ratio == current_ratio)
88 /* This is already right */
91 if (target_ratio == 0) {
92 gpiod_set_value_cansleep(ktd253->gpiod, 0);
94 * We need to keep the GPIO low for at least this long
95 * to actually switch the KTD253 off.
97 msleep(KTD253_T_OFF_MS);
102 if (current_ratio == 0) {
103 ktd253_backlight_set_max_ratio(ktd253);
104 current_ratio = KTD253_MAX_RATIO;
107 while (current_ratio != target_ratio) {
109 * These GPIO operations absolutely can NOT sleep so no
110 * _cansleep suffixes, and no using GPIO expanders on
111 * slow buses for this!
113 ret = ktd253_backlight_stepdown(ktd253);
114 if (ret == -EAGAIN) {
116 * Something disturbed the backlight setting code when
117 * running so we need to bring the PWM back to a known
118 * state. This shouldn't happen too much.
120 gpiod_set_value_cansleep(ktd253->gpiod, 0);
121 msleep(KTD253_T_OFF_MS);
122 ktd253_backlight_set_max_ratio(ktd253);
123 current_ratio = KTD253_MAX_RATIO;
124 } else if (current_ratio == KTD253_MIN_RATIO) {
125 /* After 1/32 we loop back to 32/32 */
126 current_ratio = KTD253_MAX_RATIO;
131 ktd253->ratio = current_ratio;
133 dev_dbg(ktd253->dev, "new ratio set to %d/32\n", target_ratio);
138 static const struct backlight_ops ktd253_backlight_ops = {
139 .options = BL_CORE_SUSPENDRESUME,
140 .update_status = ktd253_backlight_update_status,
143 static int ktd253_backlight_probe(struct platform_device *pdev)
145 struct device *dev = &pdev->dev;
146 struct backlight_device *bl;
147 struct ktd253_backlight *ktd253;
152 ktd253 = devm_kzalloc(dev, sizeof(*ktd253), GFP_KERNEL);
157 ret = device_property_read_u32(dev, "max-brightness", &max_brightness);
159 max_brightness = KTD253_MAX_RATIO;
160 if (max_brightness > KTD253_MAX_RATIO) {
161 /* Clamp brightness to hardware max */
162 dev_err(dev, "illegal max brightness specified\n");
163 max_brightness = KTD253_MAX_RATIO;
166 ret = device_property_read_u32(dev, "default-brightness", &brightness);
168 brightness = KTD253_DEFAULT_RATIO;
169 if (brightness > max_brightness) {
170 /* Clamp default brightness to max brightness */
171 dev_err(dev, "default brightness exceeds max brightness\n");
172 brightness = max_brightness;
175 ktd253->gpiod = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
176 if (IS_ERR(ktd253->gpiod)) {
177 ret = PTR_ERR(ktd253->gpiod);
178 if (ret != -EPROBE_DEFER)
179 dev_err(dev, "gpio line missing or invalid.\n");
182 gpiod_set_consumer_name(ktd253->gpiod, dev_name(dev));
183 /* Bring backlight to a known off state */
184 msleep(KTD253_T_OFF_MS);
186 bl = devm_backlight_device_register(dev, dev_name(dev), dev, ktd253,
187 &ktd253_backlight_ops, NULL);
189 dev_err(dev, "failed to register backlight\n");
192 bl->props.max_brightness = max_brightness;
193 /* When we just enable the GPIO line we set max brightness */
195 bl->props.brightness = brightness;
196 bl->props.power = FB_BLANK_UNBLANK;
198 bl->props.brightness = 0;
199 bl->props.power = FB_BLANK_POWERDOWN;
203 platform_set_drvdata(pdev, bl);
204 backlight_update_status(bl);
209 static const struct of_device_id ktd253_backlight_of_match[] = {
210 { .compatible = "kinetic,ktd253" },
211 { .compatible = "kinetic,ktd259" },
214 MODULE_DEVICE_TABLE(of, ktd253_backlight_of_match);
216 static struct platform_driver ktd253_backlight_driver = {
218 .name = "ktd253-backlight",
219 .of_match_table = ktd253_backlight_of_match,
221 .probe = ktd253_backlight_probe,
223 module_platform_driver(ktd253_backlight_driver);
226 MODULE_DESCRIPTION("Kinetic KTD253 Backlight Driver");
227 MODULE_LICENSE("GPL");
228 MODULE_ALIAS("platform:ktd253-backlight");