1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LED driver for Marvell 88PM860x
4 *
5 * Copyright (C) 2009 Marvell International Ltd.
6 * Haojian Zhuang <haojian.zhuang@marvell.com>
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/i2c.h>
13 #include <linux/leds.h>
14 #include <linux/slab.h>
15 #include <linux/mfd/88pm860x.h>
16 #include <linux/module.h>
17
18 #define LED_PWM_MASK (0x1F)
19 #define LED_CURRENT_MASK (0x07 << 5)
20
21 #define LED_BLINK_MASK (0x7F)
22
23 #define LED_ON_CONTINUOUS (0x0F << 3)
24
25 #define LED1_BLINK_EN (1 << 1)
26 #define LED2_BLINK_EN (1 << 2)
27
28 struct pm860x_led {
29 struct led_classdev cdev;
30 struct i2c_client *i2c;
31 struct pm860x_chip *chip;
32 struct mutex lock;
33 char name[MFD_NAME_SIZE];
34
35 int port;
36 int iset;
37 unsigned char brightness;
38 unsigned char current_brightness;
39
40 int reg_control;
41 int reg_blink;
42 int blink_mask;
43 };
44
led_power_set(struct pm860x_chip * chip,int port,int on)45 static int led_power_set(struct pm860x_chip *chip, int port, int on)
46 {
47 int ret = -EINVAL;
48
49 switch (port) {
50 case 0:
51 case 1:
52 case 2:
53 ret = on ? pm8606_osc_enable(chip, RGB1_ENABLE) :
54 pm8606_osc_disable(chip, RGB1_ENABLE);
55 break;
56 case 3:
57 case 4:
58 case 5:
59 ret = on ? pm8606_osc_enable(chip, RGB2_ENABLE) :
60 pm8606_osc_disable(chip, RGB2_ENABLE);
61 break;
62 }
63 return ret;
64 }
65
pm860x_led_set(struct led_classdev * cdev,enum led_brightness value)66 static int pm860x_led_set(struct led_classdev *cdev,
67 enum led_brightness value)
68 {
69 struct pm860x_led *led = container_of(cdev, struct pm860x_led, cdev);
70 struct pm860x_chip *chip;
71 unsigned char buf[3];
72 int ret;
73
74 chip = led->chip;
75 mutex_lock(&led->lock);
76 led->brightness = value >> 3;
77
78 if ((led->current_brightness == 0) && led->brightness) {
79 led_power_set(chip, led->port, 1);
80 if (led->iset) {
81 pm860x_set_bits(led->i2c, led->reg_control,
82 LED_CURRENT_MASK, led->iset);
83 }
84 pm860x_set_bits(led->i2c, led->reg_blink,
85 LED_BLINK_MASK, LED_ON_CONTINUOUS);
86 pm860x_set_bits(led->i2c, PM8606_WLED3B, led->blink_mask,
87 led->blink_mask);
88 }
89 pm860x_set_bits(led->i2c, led->reg_control, LED_PWM_MASK,
90 led->brightness);
91
92 if (led->brightness == 0) {
93 pm860x_bulk_read(led->i2c, led->reg_control, 3, buf);
94 ret = buf[0] & LED_PWM_MASK;
95 ret |= buf[1] & LED_PWM_MASK;
96 ret |= buf[2] & LED_PWM_MASK;
97 if (ret == 0) {
98 /* unset current since no led is lighting */
99 pm860x_set_bits(led->i2c, led->reg_control,
100 LED_CURRENT_MASK, 0);
101 pm860x_set_bits(led->i2c, PM8606_WLED3B,
102 led->blink_mask, 0);
103 led_power_set(chip, led->port, 0);
104 }
105 }
106 led->current_brightness = led->brightness;
107 dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
108 led->reg_control, led->brightness);
109 mutex_unlock(&led->lock);
110
111 return 0;
112 }
113
114 #ifdef CONFIG_OF
pm860x_led_dt_init(struct platform_device * pdev,struct pm860x_led * data)115 static int pm860x_led_dt_init(struct platform_device *pdev,
116 struct pm860x_led *data)
117 {
118 struct device_node *nproot;
119 int iset = 0;
120
121 if (!dev_of_node(pdev->dev.parent))
122 return -ENODEV;
123 nproot = of_get_child_by_name(dev_of_node(pdev->dev.parent), "leds");
124 if (!nproot) {
125 dev_err(&pdev->dev, "failed to find leds node\n");
126 return -ENODEV;
127 }
128 for_each_available_child_of_node_scoped(nproot, np) {
129 if (of_node_name_eq(np, data->name)) {
130 of_property_read_u32(np, "marvell,88pm860x-iset",
131 &iset);
132 data->iset = PM8606_LED_CURRENT(iset);
133 break;
134 }
135 }
136 of_node_put(nproot);
137 return 0;
138 }
139 #else
140 #define pm860x_led_dt_init(x, y) (-1)
141 #endif
142
pm860x_led_probe(struct platform_device * pdev)143 static int pm860x_led_probe(struct platform_device *pdev)
144 {
145 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
146 struct pm860x_led_pdata *pdata = dev_get_platdata(&pdev->dev);
147 struct pm860x_led *data;
148 struct resource *res;
149 int ret = 0;
150
151 data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_led), GFP_KERNEL);
152 if (data == NULL)
153 return -ENOMEM;
154 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "control");
155 if (!res) {
156 dev_err(&pdev->dev, "No REG resource for control\n");
157 return -ENXIO;
158 }
159 data->reg_control = res->start;
160 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "blink");
161 if (!res) {
162 dev_err(&pdev->dev, "No REG resource for blink\n");
163 return -ENXIO;
164 }
165 data->reg_blink = res->start;
166 memset(data->name, 0, MFD_NAME_SIZE);
167 switch (pdev->id) {
168 case 0:
169 data->blink_mask = LED1_BLINK_EN;
170 sprintf(data->name, "led0-red");
171 break;
172 case 1:
173 data->blink_mask = LED1_BLINK_EN;
174 sprintf(data->name, "led0-green");
175 break;
176 case 2:
177 data->blink_mask = LED1_BLINK_EN;
178 sprintf(data->name, "led0-blue");
179 break;
180 case 3:
181 data->blink_mask = LED2_BLINK_EN;
182 sprintf(data->name, "led1-red");
183 break;
184 case 4:
185 data->blink_mask = LED2_BLINK_EN;
186 sprintf(data->name, "led1-green");
187 break;
188 case 5:
189 data->blink_mask = LED2_BLINK_EN;
190 sprintf(data->name, "led1-blue");
191 break;
192 }
193 data->chip = chip;
194 data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
195 data->port = pdev->id;
196 if (pm860x_led_dt_init(pdev, data))
197 if (pdata)
198 data->iset = pdata->iset;
199
200 data->current_brightness = 0;
201 data->cdev.name = data->name;
202 data->cdev.brightness_set_blocking = pm860x_led_set;
203 mutex_init(&data->lock);
204
205 ret = led_classdev_register(chip->dev, &data->cdev);
206 if (ret < 0) {
207 dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
208 return ret;
209 }
210 pm860x_led_set(&data->cdev, 0);
211
212 platform_set_drvdata(pdev, data);
213
214 return 0;
215 }
216
pm860x_led_remove(struct platform_device * pdev)217 static void pm860x_led_remove(struct platform_device *pdev)
218 {
219 struct pm860x_led *data = platform_get_drvdata(pdev);
220
221 led_classdev_unregister(&data->cdev);
222 }
223
224 static struct platform_driver pm860x_led_driver = {
225 .driver = {
226 .name = "88pm860x-led",
227 },
228 .probe = pm860x_led_probe,
229 .remove_new = pm860x_led_remove,
230 };
231
232 module_platform_driver(pm860x_led_driver);
233
234 MODULE_DESCRIPTION("LED driver for Marvell PM860x");
235 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
236 MODULE_LICENSE("GPL");
237 MODULE_ALIAS("platform:88pm860x-led");
238