1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * LED Driver for Dialog DA9052 PMICs.
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
7 * Author: David Dajun Chen <dchen@diasemi.com>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/leds.h>
14 #include <linux/slab.h>
16 #include <linux/mfd/da9052/reg.h>
17 #include <linux/mfd/da9052/da9052.h>
18 #include <linux/mfd/da9052/pdata.h>
20 #define DA9052_OPENDRAIN_OUTPUT 2
21 #define DA9052_SET_HIGH_LVL_OUTPUT (1 << 3)
22 #define DA9052_MASK_UPPER_NIBBLE 0xF0
23 #define DA9052_MASK_LOWER_NIBBLE 0x0F
24 #define DA9052_NIBBLE_SHIFT 4
25 #define DA9052_MAX_BRIGHTNESS 0x5f
28 struct led_classdev cdev;
29 struct da9052 *da9052;
30 unsigned char led_index;
34 static unsigned char led_reg[] = {
35 DA9052_LED_CONT_4_REG,
36 DA9052_LED_CONT_5_REG,
39 static int da9052_set_led_brightness(struct da9052_led *led,
40 enum led_brightness brightness)
45 val = (brightness & 0x7f) | DA9052_LED_CONT_DIM;
47 error = da9052_reg_write(led->da9052, led_reg[led->led_index], val);
49 dev_err(led->da9052->dev, "Failed to set led brightness, %d\n",
54 static int da9052_led_set(struct led_classdev *led_cdev,
55 enum led_brightness value)
57 struct da9052_led *led =
58 container_of(led_cdev, struct da9052_led, cdev);
60 return da9052_set_led_brightness(led, value);
63 static int da9052_configure_leds(struct da9052 *da9052)
66 unsigned char register_value = DA9052_OPENDRAIN_OUTPUT
67 | DA9052_SET_HIGH_LVL_OUTPUT;
69 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
70 DA9052_MASK_LOWER_NIBBLE,
74 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
79 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
80 DA9052_MASK_UPPER_NIBBLE,
81 register_value << DA9052_NIBBLE_SHIFT);
83 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
89 static int da9052_led_probe(struct platform_device *pdev)
91 struct da9052_pdata *pdata;
92 struct da9052 *da9052;
93 struct led_platform_data *pled;
94 struct da9052_led *led = NULL;
98 da9052 = dev_get_drvdata(pdev->dev.parent);
99 pdata = dev_get_platdata(da9052->dev);
101 dev_err(&pdev->dev, "No platform data\n");
107 dev_err(&pdev->dev, "No platform data for LED\n");
111 led = devm_kcalloc(&pdev->dev,
112 pled->num_leds, sizeof(struct da9052_led),
119 for (i = 0; i < pled->num_leds; i++) {
120 led[i].cdev.name = pled->leds[i].name;
121 led[i].cdev.brightness_set_blocking = da9052_led_set;
122 led[i].cdev.brightness = LED_OFF;
123 led[i].cdev.max_brightness = DA9052_MAX_BRIGHTNESS;
124 led[i].led_index = pled->leds[i].flags;
125 led[i].da9052 = dev_get_drvdata(pdev->dev.parent);
127 error = led_classdev_register(pdev->dev.parent, &led[i].cdev);
129 dev_err(&pdev->dev, "Failed to register led %d\n",
134 error = da9052_set_led_brightness(&led[i],
135 led[i].cdev.brightness);
137 dev_err(&pdev->dev, "Unable to init led %d\n",
142 error = da9052_configure_leds(led->da9052);
144 dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error);
148 platform_set_drvdata(pdev, led);
153 for (i = i - 1; i >= 0; i--)
154 led_classdev_unregister(&led[i].cdev);
159 static int da9052_led_remove(struct platform_device *pdev)
161 struct da9052_led *led = platform_get_drvdata(pdev);
162 struct da9052_pdata *pdata;
163 struct da9052 *da9052;
164 struct led_platform_data *pled;
167 da9052 = dev_get_drvdata(pdev->dev.parent);
168 pdata = dev_get_platdata(da9052->dev);
171 for (i = 0; i < pled->num_leds; i++) {
172 da9052_set_led_brightness(&led[i], LED_OFF);
173 led_classdev_unregister(&led[i].cdev);
179 static struct platform_driver da9052_led_driver = {
181 .name = "da9052-leds",
183 .probe = da9052_led_probe,
184 .remove = da9052_led_remove,
187 module_platform_driver(da9052_led_driver);
189 MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>");
190 MODULE_DESCRIPTION("LED driver for Dialog DA9052 PMIC");
191 MODULE_LICENSE("GPL");