xref: /linux/drivers/leds/leds-aw2013.c (revision f4738f56d1dc62aaba69b33702a5ab098f1b8c63)
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 
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 
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 
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 
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 
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 
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 
264 static int aw2013_probe_dt(struct aw2013 *chip)
265 {
266 	struct device_node *np = dev_of_node(&chip->client->dev), *child;
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(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 			of_node_put(child);
309 			return ret;
310 		}
311 
312 		i++;
313 	}
314 
315 	if (!count)
316 		return -EINVAL;
317 
318 	chip->num_leds = i;
319 
320 	return 0;
321 }
322 
323 static const struct regmap_config aw2013_regmap_config = {
324 	.reg_bits = 8,
325 	.val_bits = 8,
326 	.max_register = AW2013_REG_MAX,
327 };
328 
329 static int aw2013_probe(struct i2c_client *client)
330 {
331 	struct aw2013 *chip;
332 	int ret;
333 	unsigned int chipid;
334 
335 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
336 	if (!chip)
337 		return -ENOMEM;
338 
339 	mutex_init(&chip->mutex);
340 	mutex_lock(&chip->mutex);
341 
342 	chip->client = client;
343 	i2c_set_clientdata(client, chip);
344 
345 	chip->regmap = devm_regmap_init_i2c(client, &aw2013_regmap_config);
346 	if (IS_ERR(chip->regmap)) {
347 		ret = PTR_ERR(chip->regmap);
348 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
349 			ret);
350 		goto error;
351 	}
352 
353 	chip->regulators[0].supply = "vcc";
354 	chip->regulators[1].supply = "vio";
355 	ret = devm_regulator_bulk_get(&client->dev,
356 				      ARRAY_SIZE(chip->regulators),
357 				      chip->regulators);
358 	if (ret < 0) {
359 		if (ret != -EPROBE_DEFER)
360 			dev_err(&client->dev,
361 				"Failed to request regulators: %d\n", ret);
362 		goto error;
363 	}
364 
365 	ret = regulator_bulk_enable(ARRAY_SIZE(chip->regulators),
366 				    chip->regulators);
367 	if (ret) {
368 		dev_err(&client->dev,
369 			"Failed to enable regulators: %d\n", ret);
370 		goto error;
371 	}
372 
373 	ret = regmap_read(chip->regmap, AW2013_RSTR, &chipid);
374 	if (ret) {
375 		dev_err(&client->dev, "Failed to read chip ID: %d\n",
376 			ret);
377 		goto error_reg;
378 	}
379 
380 	if (chipid != AW2013_RSTR_CHIP_ID) {
381 		dev_err(&client->dev, "Chip reported wrong ID: %x\n",
382 			chipid);
383 		ret = -ENODEV;
384 		goto error_reg;
385 	}
386 
387 	ret = aw2013_probe_dt(chip);
388 	if (ret < 0)
389 		goto error_reg;
390 
391 	ret = regulator_bulk_disable(ARRAY_SIZE(chip->regulators),
392 				     chip->regulators);
393 	if (ret) {
394 		dev_err(&client->dev,
395 			"Failed to disable regulators: %d\n", ret);
396 		goto error;
397 	}
398 
399 	mutex_unlock(&chip->mutex);
400 
401 	return 0;
402 
403 error_reg:
404 	regulator_bulk_disable(ARRAY_SIZE(chip->regulators),
405 			       chip->regulators);
406 
407 error:
408 	mutex_destroy(&chip->mutex);
409 	return ret;
410 }
411 
412 static void aw2013_remove(struct i2c_client *client)
413 {
414 	struct aw2013 *chip = i2c_get_clientdata(client);
415 
416 	aw2013_chip_disable(chip);
417 
418 	mutex_destroy(&chip->mutex);
419 }
420 
421 static const struct of_device_id aw2013_match_table[] = {
422 	{ .compatible = "awinic,aw2013", },
423 	{ /* sentinel */ },
424 };
425 
426 MODULE_DEVICE_TABLE(of, aw2013_match_table);
427 
428 static struct i2c_driver aw2013_driver = {
429 	.driver = {
430 		.name = "leds-aw2013",
431 		.of_match_table = aw2013_match_table,
432 	},
433 	.probe = aw2013_probe,
434 	.remove = aw2013_remove,
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