blob: 04060c862bf9557267e72401e0fc129f44e4f76d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Dajun Chen5ba73632012-05-29 15:07:26 -07002/*
3 * LED Driver for Dialog DA9052 PMICs.
4 *
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 *
7 * Author: David Dajun Chen <dchen@diasemi.com>
David Dajun Chen5ba73632012-05-29 15:07:26 -07008 */
9
10#include <linux/module.h>
11#include <linux/kernel.h>
David Dajun Chen5ba73632012-05-29 15:07:26 -070012#include <linux/platform_device.h>
13#include <linux/leds.h>
David Dajun Chen5ba73632012-05-29 15:07:26 -070014#include <linux/slab.h>
15
16#include <linux/mfd/da9052/reg.h>
17#include <linux/mfd/da9052/da9052.h>
18#include <linux/mfd/da9052/pdata.h>
19
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
26
27struct da9052_led {
28 struct led_classdev cdev;
David Dajun Chen5ba73632012-05-29 15:07:26 -070029 struct da9052 *da9052;
30 unsigned char led_index;
31 unsigned char id;
David Dajun Chen5ba73632012-05-29 15:07:26 -070032};
33
34static unsigned char led_reg[] = {
35 DA9052_LED_CONT_4_REG,
36 DA9052_LED_CONT_5_REG,
37};
38
Andrew Lunn0a1f01d2015-08-20 12:45:23 +020039static int da9052_set_led_brightness(struct da9052_led *led,
40 enum led_brightness brightness)
David Dajun Chen5ba73632012-05-29 15:07:26 -070041{
42 u8 val;
43 int error;
44
Andrew Lunn0a1f01d2015-08-20 12:45:23 +020045 val = (brightness & 0x7f) | DA9052_LED_CONT_DIM;
David Dajun Chen5ba73632012-05-29 15:07:26 -070046
47 error = da9052_reg_write(led->da9052, led_reg[led->led_index], val);
48 if (error < 0)
49 dev_err(led->da9052->dev, "Failed to set led brightness, %d\n",
50 error);
51 return error;
52}
53
Andrew Lunn0a1f01d2015-08-20 12:45:23 +020054static int da9052_led_set(struct led_classdev *led_cdev,
David Dajun Chen5ba73632012-05-29 15:07:26 -070055 enum led_brightness value)
56{
Andrew Lunn0a1f01d2015-08-20 12:45:23 +020057 struct da9052_led *led =
58 container_of(led_cdev, struct da9052_led, cdev);
David Dajun Chen5ba73632012-05-29 15:07:26 -070059
Andrew Lunn0a1f01d2015-08-20 12:45:23 +020060 return da9052_set_led_brightness(led, value);
David Dajun Chen5ba73632012-05-29 15:07:26 -070061}
62
63static int da9052_configure_leds(struct da9052 *da9052)
64{
65 int error;
66 unsigned char register_value = DA9052_OPENDRAIN_OUTPUT
67 | DA9052_SET_HIGH_LVL_OUTPUT;
68
69 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
70 DA9052_MASK_LOWER_NIBBLE,
71 register_value);
72
73 if (error < 0) {
74 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
75 error);
76 return error;
77 }
78
79 error = da9052_reg_update(da9052, DA9052_GPIO_14_15_REG,
80 DA9052_MASK_UPPER_NIBBLE,
81 register_value << DA9052_NIBBLE_SHIFT);
82 if (error < 0)
83 dev_err(da9052->dev, "Failed to write GPIO 14-15 reg, %d\n",
84 error);
85
86 return error;
87}
88
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050089static int da9052_led_probe(struct platform_device *pdev)
David Dajun Chen5ba73632012-05-29 15:07:26 -070090{
91 struct da9052_pdata *pdata;
92 struct da9052 *da9052;
93 struct led_platform_data *pled;
94 struct da9052_led *led = NULL;
95 int error = -ENODEV;
96 int i;
97
98 da9052 = dev_get_drvdata(pdev->dev.parent);
Jingoo Han87aae1e2013-07-30 01:07:35 -070099 pdata = dev_get_platdata(da9052->dev);
David Dajun Chen5ba73632012-05-29 15:07:26 -0700100 if (pdata == NULL) {
101 dev_err(&pdev->dev, "No platform data\n");
102 goto err;
103 }
104
105 pled = pdata->pled;
106 if (pled == NULL) {
107 dev_err(&pdev->dev, "No platform data for LED\n");
108 goto err;
109 }
110
Kees Cooka86854d2018-06-12 14:07:58 -0700111 led = devm_kcalloc(&pdev->dev,
112 pled->num_leds, sizeof(struct da9052_led),
David Dajun Chen5ba73632012-05-29 15:07:26 -0700113 GFP_KERNEL);
Xiubo Li0c9a03b2014-05-07 20:25:52 -0700114 if (!led) {
David Dajun Chen5ba73632012-05-29 15:07:26 -0700115 error = -ENOMEM;
116 goto err;
117 }
118
119 for (i = 0; i < pled->num_leds; i++) {
120 led[i].cdev.name = pled->leds[i].name;
Andrew Lunn0a1f01d2015-08-20 12:45:23 +0200121 led[i].cdev.brightness_set_blocking = da9052_led_set;
David Dajun Chen5ba73632012-05-29 15:07:26 -0700122 led[i].cdev.brightness = LED_OFF;
123 led[i].cdev.max_brightness = DA9052_MAX_BRIGHTNESS;
David Dajun Chen5ba73632012-05-29 15:07:26 -0700124 led[i].led_index = pled->leds[i].flags;
125 led[i].da9052 = dev_get_drvdata(pdev->dev.parent);
David Dajun Chen5ba73632012-05-29 15:07:26 -0700126
127 error = led_classdev_register(pdev->dev.parent, &led[i].cdev);
128 if (error) {
129 dev_err(&pdev->dev, "Failed to register led %d\n",
130 led[i].led_index);
131 goto err_register;
132 }
133
Andrew Lunn0a1f01d2015-08-20 12:45:23 +0200134 error = da9052_set_led_brightness(&led[i],
135 led[i].cdev.brightness);
David Dajun Chen5ba73632012-05-29 15:07:26 -0700136 if (error) {
137 dev_err(&pdev->dev, "Unable to init led %d\n",
138 led[i].led_index);
139 continue;
140 }
141 }
142 error = da9052_configure_leds(led->da9052);
143 if (error) {
144 dev_err(&pdev->dev, "Failed to configure GPIO LED%d\n", error);
145 goto err_register;
146 }
147
148 platform_set_drvdata(pdev, led);
149
150 return 0;
151
152err_register:
Andrew Lunn0a1f01d2015-08-20 12:45:23 +0200153 for (i = i - 1; i >= 0; i--)
David Dajun Chen5ba73632012-05-29 15:07:26 -0700154 led_classdev_unregister(&led[i].cdev);
David Dajun Chen5ba73632012-05-29 15:07:26 -0700155err:
156 return error;
157}
158
Bill Pemberton678e8a62012-11-19 13:26:00 -0500159static int da9052_led_remove(struct platform_device *pdev)
David Dajun Chen5ba73632012-05-29 15:07:26 -0700160{
161 struct da9052_led *led = platform_get_drvdata(pdev);
162 struct da9052_pdata *pdata;
163 struct da9052 *da9052;
164 struct led_platform_data *pled;
165 int i;
166
167 da9052 = dev_get_drvdata(pdev->dev.parent);
Jingoo Han87aae1e2013-07-30 01:07:35 -0700168 pdata = dev_get_platdata(da9052->dev);
David Dajun Chen5ba73632012-05-29 15:07:26 -0700169 pled = pdata->pled;
170
171 for (i = 0; i < pled->num_leds; i++) {
Andrew Lunn0a1f01d2015-08-20 12:45:23 +0200172 da9052_set_led_brightness(&led[i], LED_OFF);
David Dajun Chen5ba73632012-05-29 15:07:26 -0700173 led_classdev_unregister(&led[i].cdev);
David Dajun Chen5ba73632012-05-29 15:07:26 -0700174 }
175
176 return 0;
177}
178
179static struct platform_driver da9052_led_driver = {
180 .driver = {
181 .name = "da9052-leds",
David Dajun Chen5ba73632012-05-29 15:07:26 -0700182 },
183 .probe = da9052_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500184 .remove = da9052_led_remove,
David Dajun Chen5ba73632012-05-29 15:07:26 -0700185};
186
187module_platform_driver(da9052_led_driver);
188
189MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>");
190MODULE_DESCRIPTION("LED driver for Dialog DA9052 PMIC");
191MODULE_LICENSE("GPL");