1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org> 3 4 #include <linux/kernel.h> 5 #include <linux/platform_device.h> 6 #include <linux/leds.h> 7 #include <linux/delay.h> 8 #include <linux/gpio/consumer.h> 9 #include <linux/slab.h> 10 #include <linux/module.h> 11 #include <linux/property.h> 12 13 #define LED_LT3593_NAME "lt3593" 14 15 struct lt3593_led_data { 16 struct led_classdev cdev; 17 struct gpio_desc *gpiod; 18 }; 19 20 static int lt3593_led_set(struct led_classdev *led_cdev, 21 enum led_brightness value) 22 { 23 struct lt3593_led_data *led_dat = 24 container_of(led_cdev, struct lt3593_led_data, cdev); 25 int pulses; 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 36 if (value == 0) { 37 gpiod_set_value_cansleep(led_dat->gpiod, 0); 38 return 0; 39 } 40 41 pulses = 32 - (value * 32) / 255; 42 43 if (pulses == 0) { 44 gpiod_set_value_cansleep(led_dat->gpiod, 0); 45 mdelay(1); 46 gpiod_set_value_cansleep(led_dat->gpiod, 1); 47 return 0; 48 } 49 50 gpiod_set_value_cansleep(led_dat->gpiod, 1); 51 52 while (pulses--) { 53 gpiod_set_value_cansleep(led_dat->gpiod, 0); 54 udelay(1); 55 gpiod_set_value_cansleep(led_dat->gpiod, 1); 56 udelay(1); 57 } 58 59 return 0; 60 } 61 62 static int lt3593_led_probe(struct platform_device *pdev) 63 { 64 struct device *dev = &pdev->dev; 65 struct lt3593_led_data *led_data; 66 struct fwnode_handle *child; 67 int ret, state = LEDS_GPIO_DEFSTATE_OFF; 68 struct led_init_data init_data = {}; 69 const char *tmp; 70 71 led_data = devm_kzalloc(dev, sizeof(*led_data), GFP_KERNEL); 72 if (!led_data) 73 return -ENOMEM; 74 75 if (device_get_child_node_count(dev) != 1) { 76 dev_err(dev, "Device must have exactly one LED sub-node."); 77 return -EINVAL; 78 } 79 80 led_data->gpiod = devm_gpiod_get(dev, "lltc,ctrl", 0); 81 if (IS_ERR(led_data->gpiod)) 82 return PTR_ERR(led_data->gpiod); 83 84 child = device_get_next_child_node(dev, NULL); 85 86 if (!fwnode_property_read_string(child, "default-state", &tmp)) { 87 if (!strcmp(tmp, "on")) 88 state = LEDS_GPIO_DEFSTATE_ON; 89 } 90 91 led_data->cdev.brightness_set_blocking = lt3593_led_set; 92 led_data->cdev.brightness = state ? LED_FULL : LED_OFF; 93 94 init_data.fwnode = child; 95 init_data.devicename = LED_LT3593_NAME; 96 init_data.default_label = ":"; 97 98 ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data); 99 fwnode_handle_put(child); 100 if (ret < 0) 101 return ret; 102 103 platform_set_drvdata(pdev, led_data); 104 105 return 0; 106 } 107 108 static const struct of_device_id of_lt3593_leds_match[] = { 109 { .compatible = "lltc,lt3593", }, 110 {}, 111 }; 112 MODULE_DEVICE_TABLE(of, of_lt3593_leds_match); 113 114 static struct platform_driver lt3593_led_driver = { 115 .probe = lt3593_led_probe, 116 .driver = { 117 .name = "leds-lt3593", 118 .of_match_table = of_lt3593_leds_match, 119 }, 120 }; 121 122 module_platform_driver(lt3593_led_driver); 123 124 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>"); 125 MODULE_DESCRIPTION("LED driver for LT3593 controllers"); 126 MODULE_LICENSE("GPL v2"); 127 MODULE_ALIAS("platform:leds-lt3593"); 128