]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/leds/leds-lt3593.c
Merge tag 'nfsd-5.6' of git://linux-nfs.org/~bfields/linux
[mirror_ubuntu-jammy-kernel.git] / drivers / leds / leds-lt3593.c
CommitLineData
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
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 12#include <linux/of.h>
68e19207
JA
13
14#define LED_LT3593_NAME "lt3593"
a8dd18fe
DM
15
16struct lt3593_led_data {
17 struct led_classdev cdev;
73f103c9 18 struct gpio_desc *gpiod;
a8dd18fe
DM
19};
20
a0011f1b
AL
21static 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
98ea1ea2 63static int lt3593_led_probe(struct platform_device *pdev)
a8dd18fe 64{
d8be2867
DM
65 struct device *dev = &pdev->dev;
66 struct lt3593_led_data *led_data;
8cd7d6da
DM
67 struct fwnode_handle *child;
68 int ret, state = LEDS_GPIO_DEFSTATE_OFF;
68e19207 69 struct led_init_data init_data = {};
8cd7d6da 70 const char *tmp;
a8dd18fe 71
8cd7d6da
DM
72 if (!dev->of_node)
73 return -ENODEV;
74
75 led_data = devm_kzalloc(dev, sizeof(*led_data), GFP_KERNEL);
76 if (!led_data)
77 return -ENOMEM;
78
79 if (device_get_child_node_count(dev) != 1) {
80 dev_err(dev, "Device must have exactly one LED sub-node.");
81 return -EINVAL;
82 }
83
84 led_data->gpiod = devm_gpiod_get(dev, "lltc,ctrl", 0);
85 if (IS_ERR(led_data->gpiod))
86 return PTR_ERR(led_data->gpiod);
87
88 child = device_get_next_child_node(dev, NULL);
89
8cd7d6da
DM
90 fwnode_property_read_string(child, "linux,default-trigger",
91 &led_data->cdev.default_trigger);
92
93 if (!fwnode_property_read_string(child, "default-state", &tmp)) {
1c310074 94 if (!strcmp(tmp, "on"))
8cd7d6da 95 state = LEDS_GPIO_DEFSTATE_ON;
8cd7d6da
DM
96 }
97
8cd7d6da
DM
98 led_data->cdev.brightness_set_blocking = lt3593_led_set;
99 led_data->cdev.brightness = state ? LED_FULL : LED_OFF;
100
68e19207
JA
101 init_data.fwnode = child;
102 init_data.devicename = LED_LT3593_NAME;
103 init_data.default_label = ":";
104
105 ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
8cd7d6da
DM
106 if (ret < 0) {
107 fwnode_handle_put(child);
108 return ret;
109 }
110
111 led_data->cdev.dev->of_node = dev->of_node;
d8be2867 112 platform_set_drvdata(pdev, led_data);
a8dd18fe 113
a8dd18fe
DM
114 return 0;
115}
116
8cd7d6da
DM
117static const struct of_device_id of_lt3593_leds_match[] = {
118 { .compatible = "lltc,lt3593", },
119 {},
120};
121MODULE_DEVICE_TABLE(of, of_lt3593_leds_match);
8cd7d6da 122
a8dd18fe
DM
123static struct platform_driver lt3593_led_driver = {
124 .probe = lt3593_led_probe,
a8dd18fe
DM
125 .driver = {
126 .name = "leds-lt3593",
8cd7d6da 127 .of_match_table = of_match_ptr(of_lt3593_leds_match),
a8dd18fe
DM
128 },
129};
130
892a8843 131module_platform_driver(lt3593_led_driver);
a8dd18fe 132
dd08ac2e 133MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
a8dd18fe 134MODULE_DESCRIPTION("LED driver for LT3593 controllers");
dd08ac2e 135MODULE_LICENSE("GPL v2");
892a8843 136MODULE_ALIAS("platform:leds-lt3593");