Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

Linux/drivers/leds/leds-da9052.c

  1 /*
  2  * LED Driver for Dialog DA9052 PMICs.
  3  *
  4  * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5  *
  6  * Author: David Dajun Chen <dchen@diasemi.com>
  7  *
  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.
 12  *
 13  */
 14 
 15 #include <linux/module.h>
 16 #include <linux/kernel.h>
 17 #include <linux/platform_device.h>
 18 #include <linux/leds.h>
 19 #include <linux/workqueue.h>
 20 #include <linux/slab.h>
 21 
 22 #include <linux/mfd/da9052/reg.h>
 23 #include <linux/mfd/da9052/da9052.h>
 24 #include <linux/mfd/da9052/pdata.h>
 25 
 26 #define DA9052_OPENDRAIN_OUTPUT 2
 27 #define DA9052_SET_HIGH_LVL_OUTPUT      (1 << 3)
 28 #define DA9052_MASK_UPPER_NIBBLE        0xF0
 29 #define DA9052_MASK_LOWER_NIBBLE        0x0F
 30 #define DA9052_NIBBLE_SHIFT             4
 31 #define DA9052_MAX_BRIGHTNESS           0x5f
 32 
 33 struct da9052_led {
 34         struct led_classdev cdev;
 35         struct work_struct work;
 36         struct da9052 *da9052;
 37         unsigned char led_index;
 38         unsigned char id;
 39         int brightness;
 40 };
 41 
 42 static unsigned char led_reg[] = {
 43         DA9052_LED_CONT_4_REG,
 44         DA9052_LED_CONT_5_REG,
 45 };
 46 
 47 static int da9052_set_led_brightness(struct da9052_led *led)
 48 {
 49         u8 val;
 50         int error;
 51 
 52         val = (led->brightness & 0x7f) | DA9052_LED_CONT_DIM;
 53 
 54         error = da9052_reg_write(led->da9052, led_reg[led->led_index], val);
 55         if (error < 0)
 56                 dev_err(led->da9052->dev, "Failed to set led brightness, %d\n",
 57                         error);
 58         return error;
 59 }
 60 
 61 static void da9052_led_work(struct work_struct *work)
 62 {
 63         struct da9052_led *led = container_of(work, struct da9052_led, work);
 64 
 65         da9052_set_led_brightness(led);
 66 }
 67 
 68 static void da9052_led_set(struct led_classdev *led_cdev,
 69                            enum led_brightness value)
 70 {
 71         struct da9052_led *led;
 72 
 73         led = container_of(led_cdev, struct da9052_led, cdev);
 74         led->brightness = value;
 75         schedule_work(&led->work);
 76 }
 77 
 78 static int da9052_configure_leds(struct da9052 *da9052)
 79 {
 80         int error;
 81         unsigned char register_value = DA9052_OPENDRAIN_OUTPUT
 82                                        | DA9052_SET_HIGH_LVL_OUTPUT;
 83 
 84         error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
 85                                   DA9052_MASK_LOWER_NIBBLE,
 86                                   register_value);
 87 
 88         if (error < 0) {
 89                 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
 90                         error);
 91                 return error;
 92         }
 93 
 94         error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
 95                                   DA9052_MASK_UPPER_NIBBLE,
 96                                   register_value << DA9052_NIBBLE_SHIFT);
 97         if (error < 0)
 98                 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
 99                         error);
100 
101         return error;
102 }
103 
104 static int da9052_led_probe(struct platform_device *pdev)
105 {
106         struct da9052_pdata *pdata;
107         struct da9052 *da9052;
108         struct led_platform_data *pled;
109         struct da9052_led *led = NULL;
110         int error = -ENODEV;
111         int i;
112 
113         da9052 = dev_get_drvdata(pdev->dev.parent);
114         pdata = dev_get_platdata(da9052->dev);
115         if (pdata == NULL) {
116                 dev_err(&pdev->dev, "No platform data\n");
117                 goto err;
118         }
119 
120         pled = pdata->pled;
121         if (pled == NULL) {
122                 dev_err(&pdev->dev, "No platform data for LED\n");
123                 goto err;
124         }
125 
126         led = devm_kzalloc(&pdev->dev,
127                            sizeof(struct da9052_led) * pled->num_leds,
128                            GFP_KERNEL);
129         if (led == NULL) {
130                 dev_err(&pdev->dev, "Failed to alloc memory\n");
131                 error = -ENOMEM;
132                 goto err;
133         }
134 
135         for (i = 0; i < pled->num_leds; i++) {
136                 led[i].cdev.name = pled->leds[i].name;
137                 led[i].cdev.brightness_set = da9052_led_set;
138                 led[i].cdev.brightness = LED_OFF;
139                 led[i].cdev.max_brightness = DA9052_MAX_BRIGHTNESS;
140                 led[i].brightness = LED_OFF;
141                 led[i].led_index = pled->leds[i].flags;
142                 led[i].da9052 = dev_get_drvdata(pdev->dev.parent);
143                 INIT_WORK(&led[i].work, da9052_led_work);
144 
145                 error = led_classdev_register(pdev->dev.parent, &led[i].cdev);
146                 if (error) {
147                         dev_err(&pdev->dev, "Failed to register led %d\n",
148                                 led[i].led_index);
149                         goto err_register;
150                 }
151 
152                 error = da9052_set_led_brightness(&led[i]);
153                 if (error) {
154                         dev_err(&pdev->dev, "Unable to init led %d\n",
155                                 led[i].led_index);
156                         continue;
157                 }
158         }
159         error = da9052_configure_leds(led->da9052);
160         if (error) {
161                 dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error);
162                 goto err_register;
163         }
164 
165         platform_set_drvdata(pdev, led);
166 
167         return 0;
168 
169 err_register:
170         for (i = i - 1; i >= 0; i--) {
171                 led_classdev_unregister(&led[i].cdev);
172                 cancel_work_sync(&led[i].work);
173         }
174 err:
175         return error;
176 }
177 
178 static int da9052_led_remove(struct platform_device *pdev)
179 {
180         struct da9052_led *led = platform_get_drvdata(pdev);
181         struct da9052_pdata *pdata;
182         struct da9052 *da9052;
183         struct led_platform_data *pled;
184         int i;
185 
186         da9052 = dev_get_drvdata(pdev->dev.parent);
187         pdata = dev_get_platdata(da9052->dev);
188         pled = pdata->pled;
189 
190         for (i = 0; i < pled->num_leds; i++) {
191                 led[i].brightness = 0;
192                 da9052_set_led_brightness(&led[i]);
193                 led_classdev_unregister(&led[i].cdev);
194                 cancel_work_sync(&led[i].work);
195         }
196 
197         return 0;
198 }
199 
200 static struct platform_driver da9052_led_driver = {
201         .driver         = {
202                 .name   = "da9052-leds",
203                 .owner  = THIS_MODULE,
204         },
205         .probe          = da9052_led_probe,
206         .remove         = da9052_led_remove,
207 };
208 
209 module_platform_driver(da9052_led_driver);
210 
211 MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>");
212 MODULE_DESCRIPTION("LED driver for Dialog DA9052 PMIC");
213 MODULE_LICENSE("GPL");
214 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us