blob: 83e8e58d81cb8b20df2273889771d75ed8b24d12 [file] [log] [blame]
Daniel Mackdd08ac22018-06-27 20:52:37 +02001// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
Daniel Macka8dd18f2009-10-15 02:59:35 +02003
4#include <linux/kernel.h>
Daniel Macka8dd18f2009-10-15 02:59:35 +02005#include <linux/platform_device.h>
6#include <linux/leds.h>
Daniel Macka8dd18f2009-10-15 02:59:35 +02007#include <linux/delay.h>
8#include <linux/gpio.h>
Daniel Mack73f103c2018-06-27 20:52:35 +02009#include <linux/gpio/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040011#include <linux/module.h>
Daniel Mack8cd7d6d2018-06-27 20:52:36 +020012#include <linux/of.h>
13#include <uapi/linux/uleds.h>
Daniel Macka8dd18f2009-10-15 02:59:35 +020014
15struct lt3593_led_data {
Daniel Mack8cd7d6d2018-06-27 20:52:36 +020016 char name[LED_MAX_NAME_SIZE];
Daniel Macka8dd18f2009-10-15 02:59:35 +020017 struct led_classdev cdev;
Daniel Mack73f103c2018-06-27 20:52:35 +020018 struct gpio_desc *gpiod;
Daniel Macka8dd18f2009-10-15 02:59:35 +020019};
20
Andrew Lunna0011f12015-08-20 12:50:05 +020021static int lt3593_led_set(struct led_classdev *led_cdev,
22 enum led_brightness value)
Daniel Macka8dd18f2009-10-15 02:59:35 +020023{
Daniel Macka8dd18f2009-10-15 02:59:35 +020024 struct lt3593_led_data *led_dat =
Andrew Lunna0011f12015-08-20 12:50:05 +020025 container_of(led_cdev, struct lt3593_led_data, cdev);
26 int pulses;
Daniel Macka8dd18f2009-10-15 02:59:35 +020027
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
Andrew Lunna0011f12015-08-20 12:50:05 +020037 if (value == 0) {
Daniel Mack73f103c2018-06-27 20:52:35 +020038 gpiod_set_value_cansleep(led_dat->gpiod, 0);
Andrew Lunna0011f12015-08-20 12:50:05 +020039 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +020040 }
41
Andrew Lunna0011f12015-08-20 12:50:05 +020042 pulses = 32 - (value * 32) / 255;
Daniel Macka8dd18f2009-10-15 02:59:35 +020043
44 if (pulses == 0) {
Daniel Mack73f103c2018-06-27 20:52:35 +020045 gpiod_set_value_cansleep(led_dat->gpiod, 0);
Daniel Macka8dd18f2009-10-15 02:59:35 +020046 mdelay(1);
Daniel Mack73f103c2018-06-27 20:52:35 +020047 gpiod_set_value_cansleep(led_dat->gpiod, 1);
Andrew Lunna0011f12015-08-20 12:50:05 +020048 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +020049 }
50
Daniel Mack73f103c2018-06-27 20:52:35 +020051 gpiod_set_value_cansleep(led_dat->gpiod, 1);
Daniel Macka8dd18f2009-10-15 02:59:35 +020052
53 while (pulses--) {
Daniel Mack73f103c2018-06-27 20:52:35 +020054 gpiod_set_value_cansleep(led_dat->gpiod, 0);
Daniel Macka8dd18f2009-10-15 02:59:35 +020055 udelay(1);
Daniel Mack73f103c2018-06-27 20:52:35 +020056 gpiod_set_value_cansleep(led_dat->gpiod, 1);
Daniel Macka8dd18f2009-10-15 02:59:35 +020057 udelay(1);
58 }
Daniel Macka8dd18f2009-10-15 02:59:35 +020059
Andrew Lunna0011f12015-08-20 12:50:05 +020060 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +020061}
62
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050063static int lt3593_led_probe(struct platform_device *pdev)
Daniel Macka8dd18f2009-10-15 02:59:35 +020064{
Daniel Mackd8be2862018-06-27 20:52:34 +020065 struct device *dev = &pdev->dev;
66 struct lt3593_led_data *led_data;
Daniel Mack8cd7d6d2018-06-27 20:52:36 +020067 struct fwnode_handle *child;
68 int ret, state = LEDS_GPIO_DEFSTATE_OFF;
Daniel Mack8cd7d6d2018-06-27 20:52:36 +020069 const char *tmp;
Daniel Macka8dd18f2009-10-15 02:59:35 +020070
Daniel Mack8cd7d6d2018-06-27 20:52:36 +020071 if (!dev->of_node)
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
89 ret = fwnode_property_read_string(child, "label", &tmp);
90 if (ret < 0)
91 snprintf(led_data->name, sizeof(led_data->name),
92 "lt3593::");
93 else
94 snprintf(led_data->name, sizeof(led_data->name),
95 "lt3593:%s", tmp);
96
97 fwnode_property_read_string(child, "linux,default-trigger",
98 &led_data->cdev.default_trigger);
99
100 if (!fwnode_property_read_string(child, "default-state", &tmp)) {
YueHaibing1c310072019-03-23 11:26:59 +0000101 if (!strcmp(tmp, "on"))
Daniel Mack8cd7d6d2018-06-27 20:52:36 +0200102 state = LEDS_GPIO_DEFSTATE_ON;
Daniel Mack8cd7d6d2018-06-27 20:52:36 +0200103 }
104
105 led_data->cdev.name = led_data->name;
106 led_data->cdev.brightness_set_blocking = lt3593_led_set;
107 led_data->cdev.brightness = state ? LED_FULL : LED_OFF;
108
109 ret = devm_led_classdev_register(dev, &led_data->cdev);
110 if (ret < 0) {
111 fwnode_handle_put(child);
112 return ret;
113 }
114
115 led_data->cdev.dev->of_node = dev->of_node;
Daniel Mackd8be2862018-06-27 20:52:34 +0200116 platform_set_drvdata(pdev, led_data);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200117
Daniel Macka8dd18f2009-10-15 02:59:35 +0200118 return 0;
119}
120
Daniel Mack8cd7d6d2018-06-27 20:52:36 +0200121static const struct of_device_id of_lt3593_leds_match[] = {
122 { .compatible = "lltc,lt3593", },
123 {},
124};
125MODULE_DEVICE_TABLE(of, of_lt3593_leds_match);
Daniel Mack8cd7d6d2018-06-27 20:52:36 +0200126
Daniel Macka8dd18f2009-10-15 02:59:35 +0200127static struct platform_driver lt3593_led_driver = {
128 .probe = lt3593_led_probe,
Daniel Macka8dd18f2009-10-15 02:59:35 +0200129 .driver = {
130 .name = "leds-lt3593",
Daniel Mack8cd7d6d2018-06-27 20:52:36 +0200131 .of_match_table = of_match_ptr(of_lt3593_leds_match),
Daniel Macka8dd18f2009-10-15 02:59:35 +0200132 },
133};
134
Axel Lin892a8842012-01-10 15:09:24 -0800135module_platform_driver(lt3593_led_driver);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200136
Daniel Mackdd08ac22018-06-27 20:52:37 +0200137MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
Daniel Macka8dd18f2009-10-15 02:59:35 +0200138MODULE_DESCRIPTION("LED driver for LT3593 controllers");
Daniel Mackdd08ac22018-06-27 20:52:37 +0200139MODULE_LICENSE("GPL v2");
Axel Lin892a8842012-01-10 15:09:24 -0800140MODULE_ALIAS("platform:leds-lt3593");