]>
Commit | Line | Data |
---|---|---|
dd08ac2e DM |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | // Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org> | |
a8dd18fe DM |
3 | |
4 | #include <linux/kernel.h> | |
a8dd18fe DM |
5 | #include <linux/platform_device.h> |
6 | #include <linux/leds.h> | |
a8dd18fe | 7 | #include <linux/delay.h> |
73f103c9 | 8 | #include <linux/gpio/consumer.h> |
5a0e3ad6 | 9 | #include <linux/slab.h> |
54f4dedb | 10 | #include <linux/module.h> |
8cd7d6da | 11 | #include <linux/of.h> |
68e19207 JA |
12 | |
13 | #define LED_LT3593_NAME "lt3593" | |
a8dd18fe DM |
14 | |
15 | struct lt3593_led_data { | |
16 | struct led_classdev cdev; | |
73f103c9 | 17 | struct gpio_desc *gpiod; |
a8dd18fe DM |
18 | }; |
19 | ||
a0011f1b AL |
20 | static int lt3593_led_set(struct led_classdev *led_cdev, |
21 | enum led_brightness value) | |
a8dd18fe | 22 | { |
a8dd18fe | 23 | struct lt3593_led_data *led_dat = |
a0011f1b AL |
24 | container_of(led_cdev, struct lt3593_led_data, cdev); |
25 | int pulses; | |
a8dd18fe DM |
26 | |
27 | /* | |
28 | * The LT3593 resets its internal current level register to the maximum | |
29 | * level on the first falling edge on the control pin. Each following | |
30 | * falling edge decreases the current level by 625uA. Up to 32 pulses | |
31 | * can be sent, so the maximum power reduction is 20mA. | |
32 | * After a timeout of 128us, the value is taken from the register and | |
33 | * applied is to the output driver. | |
34 | */ | |
35 | ||
a0011f1b | 36 | if (value == 0) { |
73f103c9 | 37 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
a0011f1b | 38 | return 0; |
a8dd18fe DM |
39 | } |
40 | ||
a0011f1b | 41 | pulses = 32 - (value * 32) / 255; |
a8dd18fe DM |
42 | |
43 | if (pulses == 0) { | |
73f103c9 | 44 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
a8dd18fe | 45 | mdelay(1); |
73f103c9 | 46 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
a0011f1b | 47 | return 0; |
a8dd18fe DM |
48 | } |
49 | ||
73f103c9 | 50 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
a8dd18fe DM |
51 | |
52 | while (pulses--) { | |
73f103c9 | 53 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
a8dd18fe | 54 | udelay(1); |
73f103c9 | 55 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
a8dd18fe DM |
56 | udelay(1); |
57 | } | |
a8dd18fe | 58 | |
a0011f1b | 59 | return 0; |
a8dd18fe DM |
60 | } |
61 | ||
98ea1ea2 | 62 | static int lt3593_led_probe(struct platform_device *pdev) |
a8dd18fe | 63 | { |
d8be2867 DM |
64 | struct device *dev = &pdev->dev; |
65 | struct lt3593_led_data *led_data; | |
8cd7d6da DM |
66 | struct fwnode_handle *child; |
67 | int ret, state = LEDS_GPIO_DEFSTATE_OFF; | |
68e19207 | 68 | struct led_init_data init_data = {}; |
8cd7d6da | 69 | const char *tmp; |
a8dd18fe | 70 | |
8853c95e | 71 | if (!dev_of_node(dev)) |
8cd7d6da DM |
72 | return -ENODEV; |
73 | ||
74 | led_data = devm_kzalloc(dev, sizeof(*led_data), GFP_KERNEL); | |
75 | if (!led_data) | |
76 | return -ENOMEM; | |
77 | ||
78 | if (device_get_child_node_count(dev) != 1) { | |
79 | dev_err(dev, "Device must have exactly one LED sub-node."); | |
80 | return -EINVAL; | |
81 | } | |
82 | ||
83 | led_data->gpiod = devm_gpiod_get(dev, "lltc,ctrl", 0); | |
84 | if (IS_ERR(led_data->gpiod)) | |
85 | return PTR_ERR(led_data->gpiod); | |
86 | ||
87 | child = device_get_next_child_node(dev, NULL); | |
88 | ||
8cd7d6da | 89 | if (!fwnode_property_read_string(child, "default-state", &tmp)) { |
1c310074 | 90 | if (!strcmp(tmp, "on")) |
8cd7d6da | 91 | state = LEDS_GPIO_DEFSTATE_ON; |
8cd7d6da DM |
92 | } |
93 | ||
8cd7d6da DM |
94 | led_data->cdev.brightness_set_blocking = lt3593_led_set; |
95 | led_data->cdev.brightness = state ? LED_FULL : LED_OFF; | |
96 | ||
68e19207 JA |
97 | init_data.fwnode = child; |
98 | init_data.devicename = LED_LT3593_NAME; | |
99 | init_data.default_label = ":"; | |
100 | ||
101 | ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data); | |
8cd7d6da DM |
102 | if (ret < 0) { |
103 | fwnode_handle_put(child); | |
104 | return ret; | |
105 | } | |
106 | ||
d8be2867 | 107 | platform_set_drvdata(pdev, led_data); |
a8dd18fe | 108 | |
a8dd18fe DM |
109 | return 0; |
110 | } | |
111 | ||
8cd7d6da DM |
112 | static const struct of_device_id of_lt3593_leds_match[] = { |
113 | { .compatible = "lltc,lt3593", }, | |
114 | {}, | |
115 | }; | |
116 | MODULE_DEVICE_TABLE(of, of_lt3593_leds_match); | |
8cd7d6da | 117 | |
a8dd18fe DM |
118 | static struct platform_driver lt3593_led_driver = { |
119 | .probe = lt3593_led_probe, | |
a8dd18fe DM |
120 | .driver = { |
121 | .name = "leds-lt3593", | |
8cd7d6da | 122 | .of_match_table = of_match_ptr(of_lt3593_leds_match), |
a8dd18fe DM |
123 | }, |
124 | }; | |
125 | ||
892a8843 | 126 | module_platform_driver(lt3593_led_driver); |
a8dd18fe | 127 | |
dd08ac2e | 128 | MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>"); |
a8dd18fe | 129 | MODULE_DESCRIPTION("LED driver for LT3593 controllers"); |
dd08ac2e | 130 | MODULE_LICENSE("GPL v2"); |
892a8843 | 131 | MODULE_ALIAS("platform:leds-lt3593"); |