]>
Commit | Line | Data |
---|---|---|
dd08ac2e DM |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | // Copyright (c) 2009,2018 Daniel Mack <[email protected]> | |
a8dd18fe DM |
3 | |
4 | #include <linux/kernel.h> | |
a8dd18fe DM |
5 | #include <linux/platform_device.h> |
6 | #include <linux/leds.h> | |
a8dd18fe DM |
7 | #include <linux/delay.h> |
8 | #include <linux/gpio.h> | |
73f103c9 | 9 | #include <linux/gpio/consumer.h> |
5a0e3ad6 | 10 | #include <linux/slab.h> |
54f4dedb | 11 | #include <linux/module.h> |
8cd7d6da DM |
12 | #include <linux/of.h> |
13 | #include <uapi/linux/uleds.h> | |
a8dd18fe DM |
14 | |
15 | struct lt3593_led_data { | |
8cd7d6da | 16 | char name[LED_MAX_NAME_SIZE]; |
a8dd18fe | 17 | struct led_classdev cdev; |
73f103c9 | 18 | struct gpio_desc *gpiod; |
a8dd18fe DM |
19 | }; |
20 | ||
a0011f1b AL |
21 | static int lt3593_led_set(struct led_classdev *led_cdev, |
22 | enum led_brightness value) | |
a8dd18fe | 23 | { |
a8dd18fe | 24 | struct lt3593_led_data *led_dat = |
a0011f1b AL |
25 | container_of(led_cdev, struct lt3593_led_data, cdev); |
26 | int pulses; | |
a8dd18fe DM |
27 | |
28 | /* | |
29 | * The LT3593 resets its internal current level register to the maximum | |
30 | * level on the first falling edge on the control pin. Each following | |
31 | * falling edge decreases the current level by 625uA. Up to 32 pulses | |
32 | * can be sent, so the maximum power reduction is 20mA. | |
33 | * After a timeout of 128us, the value is taken from the register and | |
34 | * applied is to the output driver. | |
35 | */ | |
36 | ||
a0011f1b | 37 | if (value == 0) { |
73f103c9 | 38 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
a0011f1b | 39 | return 0; |
a8dd18fe DM |
40 | } |
41 | ||
a0011f1b | 42 | pulses = 32 - (value * 32) / 255; |
a8dd18fe DM |
43 | |
44 | if (pulses == 0) { | |
73f103c9 | 45 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
a8dd18fe | 46 | mdelay(1); |
73f103c9 | 47 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
a0011f1b | 48 | return 0; |
a8dd18fe DM |
49 | } |
50 | ||
73f103c9 | 51 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
a8dd18fe DM |
52 | |
53 | while (pulses--) { | |
73f103c9 | 54 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
a8dd18fe | 55 | udelay(1); |
73f103c9 | 56 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
a8dd18fe DM |
57 | udelay(1); |
58 | } | |
a8dd18fe | 59 | |
a0011f1b | 60 | return 0; |
a8dd18fe DM |
61 | } |
62 | ||
d8be2867 | 63 | static struct lt3593_led_data *lt3593_led_probe_pdata(struct device *dev) |
a8dd18fe | 64 | { |
d8be2867 DM |
65 | struct gpio_led_platform_data *pdata = dev_get_platdata(dev); |
66 | const struct gpio_led *template = &pdata->leds[0]; | |
67 | struct lt3593_led_data *led_data; | |
a8dd18fe DM |
68 | int ret, state; |
69 | ||
d8be2867 DM |
70 | if (pdata->num_leds != 1) |
71 | return ERR_PTR(-EINVAL); | |
72 | ||
73 | led_data = devm_kzalloc(dev, sizeof(*led_data), GFP_KERNEL); | |
74 | if (!led_data) | |
75 | return ERR_PTR(-ENOMEM); | |
76 | ||
d8be2867 DM |
77 | led_data->cdev.name = template->name; |
78 | led_data->cdev.default_trigger = template->default_trigger; | |
d8be2867 | 79 | led_data->cdev.brightness_set_blocking = lt3593_led_set; |
a8dd18fe DM |
80 | |
81 | state = (template->default_state == LEDS_GPIO_DEFSTATE_ON); | |
d8be2867 | 82 | led_data->cdev.brightness = state ? LED_FULL : LED_OFF; |
a8dd18fe DM |
83 | |
84 | if (!template->retain_state_suspended) | |
d8be2867 | 85 | led_data->cdev.flags |= LED_CORE_SUSPENDRESUME; |
a8dd18fe | 86 | |
d8be2867 | 87 | ret = devm_gpio_request_one(dev, template->gpio, state ? |
84f6942c JH |
88 | GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW, |
89 | template->name); | |
a8dd18fe | 90 | if (ret < 0) |
d8be2867 | 91 | return ERR_PTR(ret); |
a8dd18fe | 92 | |
73f103c9 DM |
93 | led_data->gpiod = gpio_to_desc(template->gpio); |
94 | if (!led_data->gpiod) | |
95 | return ERR_PTR(-EPROBE_DEFER); | |
96 | ||
d8be2867 | 97 | ret = devm_led_classdev_register(dev, &led_data->cdev); |
a8dd18fe | 98 | if (ret < 0) |
d8be2867 | 99 | return ERR_PTR(ret); |
a8dd18fe | 100 | |
d8be2867 DM |
101 | dev_info(dev, "registered LT3593 LED '%s' at GPIO %d\n", |
102 | template->name, template->gpio); | |
a8dd18fe | 103 | |
d8be2867 | 104 | return led_data; |
a8dd18fe DM |
105 | } |
106 | ||
98ea1ea2 | 107 | static int lt3593_led_probe(struct platform_device *pdev) |
a8dd18fe | 108 | { |
d8be2867 DM |
109 | struct device *dev = &pdev->dev; |
110 | struct lt3593_led_data *led_data; | |
8cd7d6da DM |
111 | struct fwnode_handle *child; |
112 | int ret, state = LEDS_GPIO_DEFSTATE_OFF; | |
113 | enum gpiod_flags flags = GPIOD_OUT_LOW; | |
114 | const char *tmp; | |
a8dd18fe | 115 | |
d8be2867 DM |
116 | if (dev_get_platdata(dev)) { |
117 | led_data = lt3593_led_probe_pdata(dev); | |
118 | if (IS_ERR(led_data)) | |
119 | return PTR_ERR(led_data); | |
8cd7d6da DM |
120 | |
121 | goto out; | |
d8be2867 | 122 | } |
a8dd18fe | 123 | |
8cd7d6da DM |
124 | if (!dev->of_node) |
125 | return -ENODEV; | |
126 | ||
127 | led_data = devm_kzalloc(dev, sizeof(*led_data), GFP_KERNEL); | |
128 | if (!led_data) | |
129 | return -ENOMEM; | |
130 | ||
131 | if (device_get_child_node_count(dev) != 1) { | |
132 | dev_err(dev, "Device must have exactly one LED sub-node."); | |
133 | return -EINVAL; | |
134 | } | |
135 | ||
136 | led_data->gpiod = devm_gpiod_get(dev, "lltc,ctrl", 0); | |
137 | if (IS_ERR(led_data->gpiod)) | |
138 | return PTR_ERR(led_data->gpiod); | |
139 | ||
140 | child = device_get_next_child_node(dev, NULL); | |
141 | ||
142 | ret = fwnode_property_read_string(child, "label", &tmp); | |
143 | if (ret < 0) | |
144 | snprintf(led_data->name, sizeof(led_data->name), | |
145 | "lt3593::"); | |
146 | else | |
147 | snprintf(led_data->name, sizeof(led_data->name), | |
148 | "lt3593:%s", tmp); | |
149 | ||
150 | fwnode_property_read_string(child, "linux,default-trigger", | |
151 | &led_data->cdev.default_trigger); | |
152 | ||
153 | if (!fwnode_property_read_string(child, "default-state", &tmp)) { | |
154 | if (!strcmp(tmp, "keep")) { | |
155 | state = LEDS_GPIO_DEFSTATE_KEEP; | |
156 | flags = GPIOD_ASIS; | |
157 | } else if (!strcmp(tmp, "on")) { | |
158 | state = LEDS_GPIO_DEFSTATE_ON; | |
159 | flags = GPIOD_OUT_HIGH; | |
160 | } | |
161 | } | |
162 | ||
163 | led_data->cdev.name = led_data->name; | |
164 | led_data->cdev.brightness_set_blocking = lt3593_led_set; | |
165 | led_data->cdev.brightness = state ? LED_FULL : LED_OFF; | |
166 | ||
167 | ret = devm_led_classdev_register(dev, &led_data->cdev); | |
168 | if (ret < 0) { | |
169 | fwnode_handle_put(child); | |
170 | return ret; | |
171 | } | |
172 | ||
173 | led_data->cdev.dev->of_node = dev->of_node; | |
174 | ||
175 | out: | |
d8be2867 | 176 | platform_set_drvdata(pdev, led_data); |
a8dd18fe | 177 | |
a8dd18fe DM |
178 | return 0; |
179 | } | |
180 | ||
8cd7d6da DM |
181 | #ifdef CONFIG_OF |
182 | static const struct of_device_id of_lt3593_leds_match[] = { | |
183 | { .compatible = "lltc,lt3593", }, | |
184 | {}, | |
185 | }; | |
186 | MODULE_DEVICE_TABLE(of, of_lt3593_leds_match); | |
187 | #endif | |
188 | ||
a8dd18fe DM |
189 | static struct platform_driver lt3593_led_driver = { |
190 | .probe = lt3593_led_probe, | |
a8dd18fe DM |
191 | .driver = { |
192 | .name = "leds-lt3593", | |
8cd7d6da | 193 | .of_match_table = of_match_ptr(of_lt3593_leds_match), |
a8dd18fe DM |
194 | }, |
195 | }; | |
196 | ||
892a8843 | 197 | module_platform_driver(lt3593_led_driver); |
a8dd18fe | 198 | |
dd08ac2e | 199 | MODULE_AUTHOR("Daniel Mack <[email protected]>"); |
a8dd18fe | 200 | MODULE_DESCRIPTION("LED driver for LT3593 controllers"); |
dd08ac2e | 201 | MODULE_LICENSE("GPL v2"); |
892a8843 | 202 | MODULE_ALIAS("platform:leds-lt3593"); |