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
35 struct led_classdev cdev;
36 struct work_struct work;
37 struct da9052 *da9052;
38 unsigned char led_index;
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)
53 val = (led->brightness & 0x7f) | DA9052_LED_CONT_DIM;
55 error = da9052_reg_write(led->da9052, led_reg[led->led_index], val);
57 dev_err(led->da9052->dev, "Failed to set led brightness, %d\n",
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)
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,
90 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
95 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
96 DA9052_MASK_UPPER_NIBBLE,
97 register_value << DA9052_NIBBLE_SHIFT);
99 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
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;
114 da9052 = dev_get_drvdata(pdev->dev.parent);
115 pdata = da9052->dev->platform_data;
117 dev_err(&pdev->dev, "No platform data\n");
123 dev_err(&pdev->dev, "No platform data for LED\n");
127 led = devm_kzalloc(&pdev->dev,
128 sizeof(struct da9052_led) * pled->num_leds,
131 dev_err(&pdev->dev, "Failed to alloc memory\n");
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);
148 dev_err(&pdev->dev, "Failed to register led %d\n",
153 error = da9052_set_led_brightness(&led[i]);
155 dev_err(&pdev->dev, "Unable to init led %d\n",
160 error = da9052_configure_leds(led->da9052);
162 dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error);
166 platform_set_drvdata(pdev, led);
171 for (i = i - 1; i >= 0; i--) {
172 led_classdev_unregister(&led[i].cdev);
173 cancel_work_sync(&led[i].work);
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;
187 da9052 = dev_get_drvdata(pdev->dev.parent);
188 pdata = da9052->dev->platform_data;
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);
201 static struct platform_driver da9052_led_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");