PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / leds / leds-da9052.c
blob865d4faf874a627424611314eb454e8ca2ba9d6e
1 /*
2 * LED Driver for Dialog DA9052 PMICs.
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 * Author: David Dajun Chen <dchen@diasemi.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/leds.h>
20 #include <linux/workqueue.h>
21 #include <linux/slab.h>
23 #include <linux/mfd/da9052/reg.h>
24 #include <linux/mfd/da9052/da9052.h>
25 #include <linux/mfd/da9052/pdata.h>
27 #define DA9052_OPENDRAIN_OUTPUT 2
28 #define DA9052_SET_HIGH_LVL_OUTPUT (1 << 3)
29 #define DA9052_MASK_UPPER_NIBBLE 0xF0
30 #define DA9052_MASK_LOWER_NIBBLE 0x0F
31 #define DA9052_NIBBLE_SHIFT 4
32 #define DA9052_MAX_BRIGHTNESS 0x5f
34 struct da9052_led {
35 struct led_classdev cdev;
36 struct work_struct work;
37 struct da9052 *da9052;
38 unsigned char led_index;
39 unsigned char id;
40 int brightness;
43 static unsigned char led_reg[] = {
44 DA9052_LED_CONT_4_REG,
45 DA9052_LED_CONT_5_REG,
48 static int da9052_set_led_brightness(struct da9052_led *led)
50 u8 val;
51 int error;
53 val = (led->brightness & 0x7f) | DA9052_LED_CONT_DIM;
55 error = da9052_reg_write(led->da9052, led_reg[led->led_index], val);
56 if (error < 0)
57 dev_err(led->da9052->dev, "Failed to set led brightness, %d\n",
58 error);
59 return error;
62 static void da9052_led_work(struct work_struct *work)
64 struct da9052_led *led = container_of(work, struct da9052_led, work);
66 da9052_set_led_brightness(led);
69 static void da9052_led_set(struct led_classdev *led_cdev,
70 enum led_brightness value)
72 struct da9052_led *led;
74 led = container_of(led_cdev, struct da9052_led, cdev);
75 led->brightness = value;
76 schedule_work(&led->work);
79 static int da9052_configure_leds(struct da9052 *da9052)
81 int error;
82 unsigned char register_value = DA9052_OPENDRAIN_OUTPUT
83 | DA9052_SET_HIGH_LVL_OUTPUT;
85 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
86 DA9052_MASK_LOWER_NIBBLE,
87 register_value);
89 if (error < 0) {
90 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
91 error);
92 return error;
95 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
96 DA9052_MASK_UPPER_NIBBLE,
97 register_value << DA9052_NIBBLE_SHIFT);
98 if (error < 0)
99 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
100 error);
102 return error;
105 static int da9052_led_probe(struct platform_device *pdev)
107 struct da9052_pdata *pdata;
108 struct da9052 *da9052;
109 struct led_platform_data *pled;
110 struct da9052_led *led = NULL;
111 int error = -ENODEV;
112 int i;
114 da9052 = dev_get_drvdata(pdev->dev.parent);
115 pdata = dev_get_platdata(da9052->dev);
116 if (pdata == NULL) {
117 dev_err(&pdev->dev, "No platform data\n");
118 goto err;
121 pled = pdata->pled;
122 if (pled == NULL) {
123 dev_err(&pdev->dev, "No platform data for LED\n");
124 goto err;
127 led = devm_kzalloc(&pdev->dev,
128 sizeof(struct da9052_led) * pled->num_leds,
129 GFP_KERNEL);
130 if (led == NULL) {
131 dev_err(&pdev->dev, "Failed to alloc memory\n");
132 error = -ENOMEM;
133 goto err;
136 for (i = 0; i < pled->num_leds; i++) {
137 led[i].cdev.name = pled->leds[i].name;
138 led[i].cdev.brightness_set = da9052_led_set;
139 led[i].cdev.brightness = LED_OFF;
140 led[i].cdev.max_brightness = DA9052_MAX_BRIGHTNESS;
141 led[i].brightness = LED_OFF;
142 led[i].led_index = pled->leds[i].flags;
143 led[i].da9052 = dev_get_drvdata(pdev->dev.parent);
144 INIT_WORK(&led[i].work, da9052_led_work);
146 error = led_classdev_register(pdev->dev.parent, &led[i].cdev);
147 if (error) {
148 dev_err(&pdev->dev, "Failed to register led %d\n",
149 led[i].led_index);
150 goto err_register;
153 error = da9052_set_led_brightness(&led[i]);
154 if (error) {
155 dev_err(&pdev->dev, "Unable to init led %d\n",
156 led[i].led_index);
157 continue;
160 error = da9052_configure_leds(led->da9052);
161 if (error) {
162 dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error);
163 goto err_register;
166 platform_set_drvdata(pdev, led);
168 return 0;
170 err_register:
171 for (i = i - 1; i >= 0; i--) {
172 led_classdev_unregister(&led[i].cdev);
173 cancel_work_sync(&led[i].work);
175 err:
176 return error;
179 static int da9052_led_remove(struct platform_device *pdev)
181 struct da9052_led *led = platform_get_drvdata(pdev);
182 struct da9052_pdata *pdata;
183 struct da9052 *da9052;
184 struct led_platform_data *pled;
185 int i;
187 da9052 = dev_get_drvdata(pdev->dev.parent);
188 pdata = dev_get_platdata(da9052->dev);
189 pled = pdata->pled;
191 for (i = 0; i < pled->num_leds; i++) {
192 led[i].brightness = 0;
193 da9052_set_led_brightness(&led[i]);
194 led_classdev_unregister(&led[i].cdev);
195 cancel_work_sync(&led[i].work);
198 return 0;
201 static struct platform_driver da9052_led_driver = {
202 .driver = {
203 .name = "da9052-leds",
204 .owner = THIS_MODULE,
206 .probe = da9052_led_probe,
207 .remove = da9052_led_remove,
210 module_platform_driver(da9052_led_driver);
212 MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>");
213 MODULE_DESCRIPTION("LED driver for Dialog DA9052 PMIC");
214 MODULE_LICENSE("GPL");