xref: /linux/drivers/leds/leds-aw2013.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Driver for Awinic AW2013 3-channel LED driver
3 
4 #include <linux/i2c.h>
5 #include <linux/leds.h>
6 #include <linux/module.h>
7 #include <linux/regulator/consumer.h>
8 #include <linux/mutex.h>
9 #include <linux/of.h>
10 #include <linux/regmap.h>
11 
12 #define AW2013_MAX_LEDS 3
13 
14 /* Reset and ID register */
15 #define AW2013_RSTR 0x00
16 #define AW2013_RSTR_RESET 0x55
17 #define AW2013_RSTR_CHIP_ID 0x33
18 
19 /* Global control register */
20 #define AW2013_GCR 0x01
21 #define AW2013_GCR_ENABLE BIT(0)
22 
23 /* LED channel enable register */
24 #define AW2013_LCTR 0x30
25 #define AW2013_LCTR_LE(x) BIT((x))
26 
27 /* LED channel control registers */
28 #define AW2013_LCFG(x) (0x31 + (x))
29 #define AW2013_LCFG_IMAX_MASK (BIT(0) | BIT(1)) // Should be 0-3
30 #define AW2013_LCFG_MD BIT(4)
31 #define AW2013_LCFG_FI BIT(5)
32 #define AW2013_LCFG_FO BIT(6)
33 
34 /* LED channel PWM registers */
35 #define AW2013_REG_PWM(x) (0x34 + (x))
36 
37 /* LED channel timing registers */
38 #define AW2013_LEDT0(x) (0x37 + (x) * 3)
39 #define AW2013_LEDT0_T1(x) ((x) << 4) // Should be 0-7
40 #define AW2013_LEDT0_T2(x) (x) // Should be 0-5
41 
42 #define AW2013_LEDT1(x) (0x38 + (x) * 3)
43 #define AW2013_LEDT1_T3(x) ((x) << 4) // Should be 0-7
44 #define AW2013_LEDT1_T4(x) (x) // Should be 0-7
45 
46 #define AW2013_LEDT2(x) (0x39 + (x) * 3)
47 #define AW2013_LEDT2_T0(x) ((x) << 4) // Should be 0-8
48 #define AW2013_LEDT2_REPEAT(x) (x) // Should be 0-15
49 
50 #define AW2013_REG_MAX 0x77
51 
52 #define AW2013_TIME_STEP 130 /* ms */
53 
54 struct aw2013;
55 
56 struct aw2013_led {
57 	struct aw2013 *chip;
58 	struct led_classdev cdev;
59 	u32 num;
60 	unsigned int imax;
61 };
62 
63 struct aw2013 {
64 	struct mutex mutex; /* held when writing to registers */
65 	struct regulator_bulk_data regulators[2];
66 	struct i2c_client *client;
67 	struct aw2013_led leds[AW2013_MAX_LEDS];
68 	struct regmap *regmap;
69 	int num_leds;
70 	bool enabled;
71 };
72 
aw2013_chip_init(struct aw2013 * chip)73 static int aw2013_chip_init(struct aw2013 *chip)
74 {
75 	int i, ret;
76 
77 	ret = regmap_write(chip->regmap, AW2013_GCR, AW2013_GCR_ENABLE);
78 	if (ret) {
79 		dev_err(&chip->client->dev, "Failed to enable the chip: %d\n",
80 			ret);
81 		return ret;
82 	}
83 
84 	for (i = 0; i < chip->num_leds; i++) {
85 		ret = regmap_update_bits(chip->regmap,
86 					 AW2013_LCFG(chip->leds[i].num),
87 					 AW2013_LCFG_IMAX_MASK,
88 					 chip->leds[i].imax);
89 		if (ret) {
90 			dev_err(&chip->client->dev,
91 				"Failed to set maximum current for led %d: %d\n",
92 				chip->leds[i].num, ret);
93 			return ret;
94 		}
95 	}
96 
97 	return ret;
98 }
99 
aw2013_chip_disable(struct aw2013 * chip)100 static void aw2013_chip_disable(struct aw2013 *chip)
101 {
102 	int ret;
103 
104 	if (!chip->enabled)
105 		return;
106 
107 	regmap_write(chip->regmap, AW2013_GCR, 0);
108 
109 	ret = regulator_bulk_disable(ARRAY_SIZE(chip->regulators),
110 				     chip->regulators);
111 	if (ret) {
112 		dev_err(&chip->client->dev,
113 			"Failed to disable regulators: %d\n", ret);
114 		return;
115 	}
116 
117 	chip->enabled = false;
118 }
119 
aw2013_chip_enable(struct aw2013 * chip)120 static int aw2013_chip_enable(struct aw2013 *chip)
121 {
122 	int ret;
123 
124 	if (chip->enabled)
125 		return 0;
126 
127 	ret = regulator_bulk_enable(ARRAY_SIZE(chip->regulators),
128 				    chip->regulators);
129 	if (ret) {
130 		dev_err(&chip->client->dev,
131 			"Failed to enable regulators: %d\n", ret);
132 		return ret;
133 	}
134 	chip->enabled = true;
135 
136 	ret = aw2013_chip_init(chip);
137 	if (ret)
138 		aw2013_chip_disable(chip);
139 
140 	return ret;
141 }
142 
aw2013_chip_in_use(struct aw2013 * chip)143 static bool aw2013_chip_in_use(struct aw2013 *chip)
144 {
145 	int i;
146 
147 	for (i = 0; i < chip->num_leds; i++)
148 		if (chip->leds[i].cdev.brightness)
149 			return true;
150 
151 	return false;
152 }
153 
aw2013_brightness_set(struct led_classdev * cdev,enum led_brightness brightness)154 static int aw2013_brightness_set(struct led_classdev *cdev,
155 				 enum led_brightness brightness)
156 {
157 	struct aw2013_led *led = container_of(cdev, struct aw2013_led, cdev);
158 	int ret, num;
159 
160 	mutex_lock(&led->chip->mutex);
161 
162 	if (aw2013_chip_in_use(led->chip)) {
163 		ret = aw2013_chip_enable(led->chip);
164 		if (ret)
165 			goto error;
166 	}
167 
168 	num = led->num;
169 
170 	ret = regmap_write(led->chip->regmap, AW2013_REG_PWM(num), brightness);
171 	if (ret)
172 		goto error;
173 
174 	if (brightness) {
175 		ret = regmap_update_bits(led->chip->regmap, AW2013_LCTR,
176 					 AW2013_LCTR_LE(num), 0xFF);
177 	} else {
178 		ret = regmap_update_bits(led->chip->regmap, AW2013_LCTR,
179 					 AW2013_LCTR_LE(num), 0);
180 		if (ret)
181 			goto error;
182 		ret = regmap_update_bits(led->chip->regmap, AW2013_LCFG(num),
183 					 AW2013_LCFG_MD, 0);
184 	}
185 	if (ret)
186 		goto error;
187 
188 	if (!aw2013_chip_in_use(led->chip))
189 		aw2013_chip_disable(led->chip);
190 
191 error:
192 	mutex_unlock(&led->chip->mutex);
193 
194 	return ret;
195 }
196 
aw2013_blink_set(struct led_classdev * cdev,unsigned long * delay_on,unsigned long * delay_off)197 static int aw2013_blink_set(struct led_classdev *cdev,
198 			    unsigned long *delay_on, unsigned long *delay_off)
199 {
200 	struct aw2013_led *led = container_of(cdev, struct aw2013_led, cdev);
201 	int ret, num = led->num;
202 	unsigned long off = 0, on = 0;
203 
204 	/* If no blink specified, default to 1 Hz. */
205 	if (!*delay_off && !*delay_on) {
206 		*delay_off = 500;
207 		*delay_on = 500;
208 	}
209 
210 	if (!led->cdev.brightness) {
211 		led->cdev.brightness = LED_FULL;
212 		ret = aw2013_brightness_set(&led->cdev, led->cdev.brightness);
213 		if (ret)
214 			return ret;
215 	}
216 
217 	/* Never on - just set to off */
218 	if (!*delay_on) {
219 		led->cdev.brightness = LED_OFF;
220 		return aw2013_brightness_set(&led->cdev, LED_OFF);
221 	}
222 
223 	mutex_lock(&led->chip->mutex);
224 
225 	/* Never off - brightness is already set, disable blinking */
226 	if (!*delay_off) {
227 		ret = regmap_update_bits(led->chip->regmap, AW2013_LCFG(num),
228 					 AW2013_LCFG_MD, 0);
229 		goto out;
230 	}
231 
232 	/* Convert into values the HW will understand. */
233 	off = min(5, ilog2((*delay_off - 1) / AW2013_TIME_STEP) + 1);
234 	on = min(7, ilog2((*delay_on - 1) / AW2013_TIME_STEP) + 1);
235 
236 	*delay_off = BIT(off) * AW2013_TIME_STEP;
237 	*delay_on = BIT(on) * AW2013_TIME_STEP;
238 
239 	/* Set timings */
240 	ret = regmap_write(led->chip->regmap,
241 			   AW2013_LEDT0(num), AW2013_LEDT0_T2(on));
242 	if (ret)
243 		goto out;
244 	ret = regmap_write(led->chip->regmap,
245 			   AW2013_LEDT1(num), AW2013_LEDT1_T4(off));
246 	if (ret)
247 		goto out;
248 
249 	/* Finally, enable the LED */
250 	ret = regmap_update_bits(led->chip->regmap, AW2013_LCFG(num),
251 				 AW2013_LCFG_MD, 0xFF);
252 	if (ret)
253 		goto out;
254 
255 	ret = regmap_update_bits(led->chip->regmap, AW2013_LCTR,
256 				 AW2013_LCTR_LE(num), 0xFF);
257 
258 out:
259 	mutex_unlock(&led->chip->mutex);
260 
261 	return ret;
262 }
263 
aw2013_probe_dt(struct aw2013 * chip)264 static int aw2013_probe_dt(struct aw2013 *chip)
265 {
266 	struct device_node *np = dev_of_node(&chip->client->dev);
267 	int count, ret = 0, i = 0;
268 	struct aw2013_led *led;
269 
270 	count = of_get_available_child_count(np);
271 	if (!count || count > AW2013_MAX_LEDS)
272 		return -EINVAL;
273 
274 	regmap_write(chip->regmap, AW2013_RSTR, AW2013_RSTR_RESET);
275 
276 	for_each_available_child_of_node_scoped(np, child) {
277 		struct led_init_data init_data = {};
278 		u32 source;
279 		u32 imax;
280 
281 		ret = of_property_read_u32(child, "reg", &source);
282 		if (ret != 0 || source >= AW2013_MAX_LEDS) {
283 			dev_err(&chip->client->dev,
284 				"Couldn't read LED address: %d\n", ret);
285 			count--;
286 			continue;
287 		}
288 
289 		led = &chip->leds[i];
290 		led->num = source;
291 		led->chip = chip;
292 		init_data.fwnode = of_fwnode_handle(child);
293 
294 		if (!of_property_read_u32(child, "led-max-microamp", &imax)) {
295 			led->imax = min_t(u32, imax / 5000, 3);
296 		} else {
297 			led->imax = 1; // 5mA
298 			dev_info(&chip->client->dev,
299 				 "DT property led-max-microamp is missing\n");
300 		}
301 
302 		led->cdev.brightness_set_blocking = aw2013_brightness_set;
303 		led->cdev.blink_set = aw2013_blink_set;
304 
305 		ret = devm_led_classdev_register_ext(&chip->client->dev,
306 						     &led->cdev, &init_data);
307 		if (ret < 0)
308 			return ret;
309 
310 		i++;
311 	}
312 
313 	if (!count)
314 		return -EINVAL;
315 
316 	chip->num_leds = i;
317 
318 	return 0;
319 }
320 
aw2013_chip_disable_action(void * data)321 static void aw2013_chip_disable_action(void *data)
322 {
323 	aw2013_chip_disable(data);
324 }
325 
326 static const struct regmap_config aw2013_regmap_config = {
327 	.reg_bits = 8,
328 	.val_bits = 8,
329 	.max_register = AW2013_REG_MAX,
330 };
331 
aw2013_probe(struct i2c_client * client)332 static int aw2013_probe(struct i2c_client *client)
333 {
334 	struct aw2013 *chip;
335 	int ret;
336 	unsigned int chipid;
337 
338 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
339 	if (!chip)
340 		return -ENOMEM;
341 
342 	ret = devm_mutex_init(&client->dev, &chip->mutex);
343 	if (ret)
344 		return ret;
345 
346 	mutex_lock(&chip->mutex);
347 
348 	chip->client = client;
349 	i2c_set_clientdata(client, chip);
350 
351 	chip->regmap = devm_regmap_init_i2c(client, &aw2013_regmap_config);
352 	if (IS_ERR(chip->regmap)) {
353 		ret = PTR_ERR(chip->regmap);
354 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
355 			ret);
356 		goto error;
357 	}
358 
359 	chip->regulators[0].supply = "vcc";
360 	chip->regulators[1].supply = "vio";
361 	ret = devm_regulator_bulk_get(&client->dev,
362 				      ARRAY_SIZE(chip->regulators),
363 				      chip->regulators);
364 	if (ret < 0) {
365 		if (ret != -EPROBE_DEFER)
366 			dev_err(&client->dev,
367 				"Failed to request regulators: %d\n", ret);
368 		goto error;
369 	}
370 
371 	ret = regulator_bulk_enable(ARRAY_SIZE(chip->regulators),
372 				    chip->regulators);
373 	if (ret) {
374 		dev_err(&client->dev,
375 			"Failed to enable regulators: %d\n", ret);
376 		goto error;
377 	}
378 
379 	ret = regmap_read(chip->regmap, AW2013_RSTR, &chipid);
380 	if (ret) {
381 		dev_err(&client->dev, "Failed to read chip ID: %d\n",
382 			ret);
383 		goto error_reg;
384 	}
385 
386 	if (chipid != AW2013_RSTR_CHIP_ID) {
387 		dev_err(&client->dev, "Chip reported wrong ID: %x\n",
388 			chipid);
389 		ret = -ENODEV;
390 		goto error_reg;
391 	}
392 
393 	ret = devm_add_action(&client->dev, aw2013_chip_disable_action, chip);
394 	if (ret)
395 		goto error_reg;
396 
397 	ret = aw2013_probe_dt(chip);
398 	if (ret < 0)
399 		goto error_reg;
400 
401 	ret = regulator_bulk_disable(ARRAY_SIZE(chip->regulators),
402 				     chip->regulators);
403 	if (ret) {
404 		dev_err(&client->dev,
405 			"Failed to disable regulators: %d\n", ret);
406 		goto error;
407 	}
408 
409 	mutex_unlock(&chip->mutex);
410 
411 	return 0;
412 
413 error_reg:
414 	regulator_bulk_disable(ARRAY_SIZE(chip->regulators),
415 			       chip->regulators);
416 
417 error:
418 	mutex_unlock(&chip->mutex);
419 	return ret;
420 }
421 
422 static const struct of_device_id aw2013_match_table[] = {
423 	{ .compatible = "awinic,aw2013", },
424 	{ /* sentinel */ },
425 };
426 
427 MODULE_DEVICE_TABLE(of, aw2013_match_table);
428 
429 static struct i2c_driver aw2013_driver = {
430 	.driver = {
431 		.name = "leds-aw2013",
432 		.of_match_table = aw2013_match_table,
433 	},
434 	.probe = aw2013_probe,
435 };
436 
437 module_i2c_driver(aw2013_driver);
438 
439 MODULE_AUTHOR("Nikita Travkin <nikitos.tr@gmail.com>");
440 MODULE_DESCRIPTION("AW2013 LED driver");
441 MODULE_LICENSE("GPL v2");
442