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