Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 2 | /* |
| 3 | * leds-regulator.c - LED class driver for regulator driven LEDs. |
| 4 | * |
| 5 | * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it> |
| 6 | * |
| 7 | * Inspired by leds-wm8350 driver. |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 13 | #include <linux/leds.h> |
| 14 | #include <linux/leds-regulator.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/regulator/consumer.h> |
| 17 | |
| 18 | #define to_regulator_led(led_cdev) \ |
| 19 | container_of(led_cdev, struct regulator_led, cdev) |
| 20 | |
| 21 | struct regulator_led { |
| 22 | struct led_classdev cdev; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 23 | int enabled; |
| 24 | struct mutex mutex; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 25 | |
| 26 | struct regulator *vcc; |
| 27 | }; |
| 28 | |
| 29 | static inline int led_regulator_get_max_brightness(struct regulator *supply) |
| 30 | { |
| 31 | int ret; |
| 32 | int voltage = regulator_list_voltage(supply, 0); |
| 33 | |
| 34 | if (voltage <= 0) |
| 35 | return 1; |
| 36 | |
| 37 | /* even if regulator can't change voltages, |
| 38 | * we still assume it can change status |
| 39 | * and the LED can be turned on and off. |
| 40 | */ |
| 41 | ret = regulator_set_voltage(supply, voltage, voltage); |
| 42 | if (ret < 0) |
| 43 | return 1; |
| 44 | |
| 45 | return regulator_count_voltages(supply); |
| 46 | } |
| 47 | |
| 48 | static int led_regulator_get_voltage(struct regulator *supply, |
| 49 | enum led_brightness brightness) |
| 50 | { |
| 51 | if (brightness == 0) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | return regulator_list_voltage(supply, brightness - 1); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static void regulator_led_enable(struct regulator_led *led) |
| 59 | { |
| 60 | int ret; |
| 61 | |
| 62 | if (led->enabled) |
| 63 | return; |
| 64 | |
| 65 | ret = regulator_enable(led->vcc); |
| 66 | if (ret != 0) { |
| 67 | dev_err(led->cdev.dev, "Failed to enable vcc: %d\n", ret); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | led->enabled = 1; |
| 72 | } |
| 73 | |
| 74 | static void regulator_led_disable(struct regulator_led *led) |
| 75 | { |
| 76 | int ret; |
| 77 | |
| 78 | if (!led->enabled) |
| 79 | return; |
| 80 | |
| 81 | ret = regulator_disable(led->vcc); |
| 82 | if (ret != 0) { |
| 83 | dev_err(led->cdev.dev, "Failed to disable vcc: %d\n", ret); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | led->enabled = 0; |
| 88 | } |
| 89 | |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 90 | static int regulator_led_brightness_set(struct led_classdev *led_cdev, |
| 91 | enum led_brightness value) |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 92 | { |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 93 | struct regulator_led *led = to_regulator_led(led_cdev); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 94 | int voltage; |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 95 | int ret = 0; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 96 | |
| 97 | mutex_lock(&led->mutex); |
| 98 | |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 99 | if (value == LED_OFF) { |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 100 | regulator_led_disable(led); |
| 101 | goto out; |
| 102 | } |
| 103 | |
| 104 | if (led->cdev.max_brightness > 1) { |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 105 | voltage = led_regulator_get_voltage(led->vcc, value); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 106 | dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n", |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 107 | value, voltage); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 108 | |
| 109 | ret = regulator_set_voltage(led->vcc, voltage, voltage); |
| 110 | if (ret != 0) |
| 111 | dev_err(led->cdev.dev, "Failed to set voltage %d: %d\n", |
| 112 | voltage, ret); |
| 113 | } |
| 114 | |
| 115 | regulator_led_enable(led); |
| 116 | |
| 117 | out: |
| 118 | mutex_unlock(&led->mutex); |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 119 | return ret; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 122 | static int regulator_led_probe(struct platform_device *pdev) |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 123 | { |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 124 | struct led_regulator_platform_data *pdata = |
| 125 | dev_get_platdata(&pdev->dev); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 126 | struct regulator_led *led; |
| 127 | struct regulator *vcc; |
| 128 | int ret = 0; |
| 129 | |
| 130 | if (pdata == NULL) { |
| 131 | dev_err(&pdev->dev, "no platform data\n"); |
| 132 | return -ENODEV; |
| 133 | } |
| 134 | |
Axel Lin | 29ce9fe | 2014-10-24 07:45:58 -0700 | [diff] [blame] | 135 | vcc = devm_regulator_get_exclusive(&pdev->dev, "vled"); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 136 | if (IS_ERR(vcc)) { |
| 137 | dev_err(&pdev->dev, "Cannot get vcc for %s\n", pdata->name); |
| 138 | return PTR_ERR(vcc); |
| 139 | } |
| 140 | |
Sachin Kamat | d415896 | 2012-07-03 19:47:54 +0800 | [diff] [blame] | 141 | led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL); |
Axel Lin | 29ce9fe | 2014-10-24 07:45:58 -0700 | [diff] [blame] | 142 | if (led == NULL) |
| 143 | return -ENOMEM; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 144 | |
| 145 | led->cdev.max_brightness = led_regulator_get_max_brightness(vcc); |
| 146 | if (pdata->brightness > led->cdev.max_brightness) { |
| 147 | dev_err(&pdev->dev, "Invalid default brightness %d\n", |
| 148 | pdata->brightness); |
Axel Lin | 29ce9fe | 2014-10-24 07:45:58 -0700 | [diff] [blame] | 149 | return -EINVAL; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 150 | } |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 151 | |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 152 | led->cdev.brightness_set_blocking = regulator_led_brightness_set; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 153 | led->cdev.name = pdata->name; |
| 154 | led->cdev.flags |= LED_CORE_SUSPENDRESUME; |
| 155 | led->vcc = vcc; |
| 156 | |
Antonio Ospite | 592ce31 | 2011-04-14 15:21:59 -0700 | [diff] [blame] | 157 | /* to handle correctly an already enabled regulator */ |
| 158 | if (regulator_is_enabled(led->vcc)) |
| 159 | led->enabled = 1; |
| 160 | |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 161 | mutex_init(&led->mutex); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 162 | |
| 163 | platform_set_drvdata(pdev, led); |
| 164 | |
| 165 | ret = led_classdev_register(&pdev->dev, &led->cdev); |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 166 | if (ret < 0) |
Axel Lin | 29ce9fe | 2014-10-24 07:45:58 -0700 | [diff] [blame] | 167 | return ret; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 168 | |
| 169 | /* to expose the default value to userspace */ |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 170 | led->cdev.brightness = pdata->brightness; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 171 | |
| 172 | /* Set the default led status */ |
Andrew Lunn | 77e8503 | 2015-08-20 12:55:39 +0200 | [diff] [blame] | 173 | regulator_led_brightness_set(&led->cdev, led->cdev.brightness); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 174 | |
| 175 | return 0; |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 178 | static int regulator_led_remove(struct platform_device *pdev) |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 179 | { |
| 180 | struct regulator_led *led = platform_get_drvdata(pdev); |
| 181 | |
| 182 | led_classdev_unregister(&led->cdev); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 183 | regulator_led_disable(led); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static struct platform_driver regulator_led_driver = { |
| 188 | .driver = { |
| 189 | .name = "leds-regulator", |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 190 | }, |
| 191 | .probe = regulator_led_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 192 | .remove = regulator_led_remove, |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 193 | }; |
| 194 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 195 | module_platform_driver(regulator_led_driver); |
Antonio Ospite | d4cc6a2 | 2009-12-07 15:08:13 +0100 | [diff] [blame] | 196 | |
| 197 | MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>"); |
| 198 | MODULE_DESCRIPTION("Regulator driven LED driver"); |
| 199 | MODULE_LICENSE("GPL"); |
| 200 | MODULE_ALIAS("platform:leds-regulator"); |