rcu: Maintain special bits at bottom of ->dynticks counter
[linux/fpc-iii.git] / drivers / leds / leds-regulator.c
blobacf77ca47558a48145ad0801b26959c3fd829329
1 /*
2 * leds-regulator.c - LED class driver for regulator driven LEDs.
4 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
6 * Inspired by leds-wm8350 driver.
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.
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/leds.h>
18 #include <linux/leds-regulator.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
22 #define to_regulator_led(led_cdev) \
23 container_of(led_cdev, struct regulator_led, cdev)
25 struct regulator_led {
26 struct led_classdev cdev;
27 int enabled;
28 struct mutex mutex;
30 struct regulator *vcc;
33 static inline int led_regulator_get_max_brightness(struct regulator *supply)
35 int ret;
36 int voltage = regulator_list_voltage(supply, 0);
38 if (voltage <= 0)
39 return 1;
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.
45 ret = regulator_set_voltage(supply, voltage, voltage);
46 if (ret < 0)
47 return 1;
49 return regulator_count_voltages(supply);
52 static int led_regulator_get_voltage(struct regulator *supply,
53 enum led_brightness brightness)
55 if (brightness == 0)
56 return -EINVAL;
58 return regulator_list_voltage(supply, brightness - 1);
62 static void regulator_led_enable(struct regulator_led *led)
64 int ret;
66 if (led->enabled)
67 return;
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;
75 led->enabled = 1;
78 static void regulator_led_disable(struct regulator_led *led)
80 int ret;
82 if (!led->enabled)
83 return;
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;
91 led->enabled = 0;
94 static int regulator_led_brightness_set(struct led_classdev *led_cdev,
95 enum led_brightness value)
97 struct regulator_led *led = to_regulator_led(led_cdev);
98 int voltage;
99 int ret = 0;
101 mutex_lock(&led->mutex);
103 if (value == LED_OFF) {
104 regulator_led_disable(led);
105 goto out;
108 if (led->cdev.max_brightness > 1) {
109 voltage = led_regulator_get_voltage(led->vcc, value);
110 dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n",
111 value, voltage);
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);
119 regulator_led_enable(led);
121 out:
122 mutex_unlock(&led->mutex);
123 return ret;
126 static int regulator_led_probe(struct platform_device *pdev)
128 struct led_regulator_platform_data *pdata =
129 dev_get_platdata(&pdev->dev);
130 struct regulator_led *led;
131 struct regulator *vcc;
132 int ret = 0;
134 if (pdata == NULL) {
135 dev_err(&pdev->dev, "no platform data\n");
136 return -ENODEV;
139 vcc = devm_regulator_get_exclusive(&pdev->dev, "vled");
140 if (IS_ERR(vcc)) {
141 dev_err(&pdev->dev, "Cannot get vcc for %s\n", pdata->name);
142 return PTR_ERR(vcc);
145 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
146 if (led == NULL)
147 return -ENOMEM;
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);
153 return -EINVAL;
156 led->cdev.brightness_set_blocking = regulator_led_brightness_set;
157 led->cdev.name = pdata->name;
158 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
159 led->vcc = vcc;
161 /* to handle correctly an already enabled regulator */
162 if (regulator_is_enabled(led->vcc))
163 led->enabled = 1;
165 mutex_init(&led->mutex);
167 platform_set_drvdata(pdev, led);
169 ret = led_classdev_register(&pdev->dev, &led->cdev);
170 if (ret < 0)
171 return ret;
173 /* to expose the default value to userspace */
174 led->cdev.brightness = pdata->brightness;
176 /* Set the default led status */
177 regulator_led_brightness_set(&led->cdev, led->cdev.brightness);
179 return 0;
182 static int regulator_led_remove(struct platform_device *pdev)
184 struct regulator_led *led = platform_get_drvdata(pdev);
186 led_classdev_unregister(&led->cdev);
187 regulator_led_disable(led);
188 return 0;
191 static struct platform_driver regulator_led_driver = {
192 .driver = {
193 .name = "leds-regulator",
195 .probe = regulator_led_probe,
196 .remove = regulator_led_remove,
199 module_platform_driver(regulator_led_driver);
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");