Daniel Mack | dd08ac2 | 2018-06-27 20:52:37 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org> |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 3 | |
| 4 | #include <linux/kernel.h> |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 5 | #include <linux/platform_device.h> |
| 6 | #include <linux/leds.h> |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 7 | #include <linux/delay.h> |
| 8 | #include <linux/gpio.h> |
Daniel Mack | 73f103c | 2018-06-27 20:52:35 +0200 | [diff] [blame] | 9 | #include <linux/gpio/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Paul Gortmaker | 54f4ded | 2011-07-03 13:56:03 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 12 | #include <linux/of.h> |
| 13 | #include <uapi/linux/uleds.h> |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 14 | |
| 15 | struct lt3593_led_data { |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 16 | char name[LED_MAX_NAME_SIZE]; |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 17 | struct led_classdev cdev; |
Daniel Mack | 73f103c | 2018-06-27 20:52:35 +0200 | [diff] [blame] | 18 | struct gpio_desc *gpiod; |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 19 | }; |
| 20 | |
Andrew Lunn | a0011f1 | 2015-08-20 12:50:05 +0200 | [diff] [blame] | 21 | static int lt3593_led_set(struct led_classdev *led_cdev, |
| 22 | enum led_brightness value) |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 23 | { |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 24 | struct lt3593_led_data *led_dat = |
Andrew Lunn | a0011f1 | 2015-08-20 12:50:05 +0200 | [diff] [blame] | 25 | container_of(led_cdev, struct lt3593_led_data, cdev); |
| 26 | int pulses; |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 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 | |
Andrew Lunn | a0011f1 | 2015-08-20 12:50:05 +0200 | [diff] [blame] | 37 | if (value == 0) { |
Daniel Mack | 73f103c | 2018-06-27 20:52:35 +0200 | [diff] [blame] | 38 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
Andrew Lunn | a0011f1 | 2015-08-20 12:50:05 +0200 | [diff] [blame] | 39 | return 0; |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Andrew Lunn | a0011f1 | 2015-08-20 12:50:05 +0200 | [diff] [blame] | 42 | pulses = 32 - (value * 32) / 255; |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 43 | |
| 44 | if (pulses == 0) { |
Daniel Mack | 73f103c | 2018-06-27 20:52:35 +0200 | [diff] [blame] | 45 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 46 | mdelay(1); |
Daniel Mack | 73f103c | 2018-06-27 20:52:35 +0200 | [diff] [blame] | 47 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
Andrew Lunn | a0011f1 | 2015-08-20 12:50:05 +0200 | [diff] [blame] | 48 | return 0; |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Daniel Mack | 73f103c | 2018-06-27 20:52:35 +0200 | [diff] [blame] | 51 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 52 | |
| 53 | while (pulses--) { |
Daniel Mack | 73f103c | 2018-06-27 20:52:35 +0200 | [diff] [blame] | 54 | gpiod_set_value_cansleep(led_dat->gpiod, 0); |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 55 | udelay(1); |
Daniel Mack | 73f103c | 2018-06-27 20:52:35 +0200 | [diff] [blame] | 56 | gpiod_set_value_cansleep(led_dat->gpiod, 1); |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 57 | udelay(1); |
| 58 | } |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 59 | |
Andrew Lunn | a0011f1 | 2015-08-20 12:50:05 +0200 | [diff] [blame] | 60 | return 0; |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 63 | static int lt3593_led_probe(struct platform_device *pdev) |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 64 | { |
Daniel Mack | d8be286 | 2018-06-27 20:52:34 +0200 | [diff] [blame] | 65 | struct device *dev = &pdev->dev; |
| 66 | struct lt3593_led_data *led_data; |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 67 | struct fwnode_handle *child; |
| 68 | int ret, state = LEDS_GPIO_DEFSTATE_OFF; |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 69 | const char *tmp; |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 70 | |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 71 | 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)) { |
YueHaibing | 1c31007 | 2019-03-23 11:26:59 +0000 | [diff] [blame] | 101 | if (!strcmp(tmp, "on")) |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 102 | state = LEDS_GPIO_DEFSTATE_ON; |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 103 | } |
| 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 Mack | d8be286 | 2018-06-27 20:52:34 +0200 | [diff] [blame] | 116 | platform_set_drvdata(pdev, led_data); |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 117 | |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 121 | static const struct of_device_id of_lt3593_leds_match[] = { |
| 122 | { .compatible = "lltc,lt3593", }, |
| 123 | {}, |
| 124 | }; |
| 125 | MODULE_DEVICE_TABLE(of, of_lt3593_leds_match); |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 126 | |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 127 | static struct platform_driver lt3593_led_driver = { |
| 128 | .probe = lt3593_led_probe, |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 129 | .driver = { |
| 130 | .name = "leds-lt3593", |
Daniel Mack | 8cd7d6d | 2018-06-27 20:52:36 +0200 | [diff] [blame] | 131 | .of_match_table = of_match_ptr(of_lt3593_leds_match), |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 132 | }, |
| 133 | }; |
| 134 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 135 | module_platform_driver(lt3593_led_driver); |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 136 | |
Daniel Mack | dd08ac2 | 2018-06-27 20:52:37 +0200 | [diff] [blame] | 137 | MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>"); |
Daniel Mack | a8dd18f | 2009-10-15 02:59:35 +0200 | [diff] [blame] | 138 | MODULE_DESCRIPTION("LED driver for LT3593 controllers"); |
Daniel Mack | dd08ac2 | 2018-06-27 20:52:37 +0200 | [diff] [blame] | 139 | MODULE_LICENSE("GPL v2"); |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 140 | MODULE_ALIAS("platform:leds-lt3593"); |