Commit | Line | Data |
---|---|---|
59ea3c9f NT |
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 */ | |
baca986e | 65 | struct regulator_bulk_data regulators[2]; |
59ea3c9f NT |
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 | ||
baca986e LMB |
109 | ret = regulator_bulk_disable(ARRAY_SIZE(chip->regulators), |
110 | chip->regulators); | |
59ea3c9f NT |
111 | if (ret) { |
112 | dev_err(&chip->client->dev, | |
baca986e | 113 | "Failed to disable regulators: %d\n", ret); |
59ea3c9f NT |
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 | ||
baca986e LMB |
127 | ret = regulator_bulk_enable(ARRAY_SIZE(chip->regulators), |
128 | chip->regulators); | |
59ea3c9f NT |
129 | if (ret) { |
130 | dev_err(&chip->client->dev, | |
baca986e | 131 | "Failed to enable regulators: %d\n", ret); |
59ea3c9f NT |
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 | { | |
8853c95e | 266 | struct device_node *np = dev_of_node(&chip->client->dev), *child; |
59ea3c9f NT |
267 | int count, ret = 0, i = 0; |
268 | struct aw2013_led *led; | |
269 | ||
99a013c8 | 270 | count = of_get_available_child_count(np); |
59ea3c9f NT |
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 | ||
59ea3c9f NT |
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); | |
2c677562 MB |
307 | if (ret < 0) { |
308 | of_node_put(child); | |
59ea3c9f | 309 | return ret; |
2c677562 | 310 | } |
59ea3c9f NT |
311 | |
312 | i++; | |
313 | } | |
314 | ||
315 | if (!count) | |
316 | return -EINVAL; | |
317 | ||
318 | chip->num_leds = i; | |
319 | ||
320 | return 0; | |
321 | } | |
322 | ||
fb74e4fa GS |
323 | static void aw2013_chip_disable_action(void *data) |
324 | { | |
325 | aw2013_chip_disable(data); | |
326 | } | |
327 | ||
59ea3c9f NT |
328 | static const struct regmap_config aw2013_regmap_config = { |
329 | .reg_bits = 8, | |
330 | .val_bits = 8, | |
331 | .max_register = AW2013_REG_MAX, | |
332 | }; | |
333 | ||
334 | static int aw2013_probe(struct i2c_client *client) | |
335 | { | |
336 | struct aw2013 *chip; | |
337 | int ret; | |
338 | unsigned int chipid; | |
339 | ||
340 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); | |
341 | if (!chip) | |
342 | return -ENOMEM; | |
343 | ||
fb74e4fa GS |
344 | ret = devm_mutex_init(&client->dev, &chip->mutex); |
345 | if (ret) | |
346 | return ret; | |
347 | ||
59ea3c9f NT |
348 | mutex_lock(&chip->mutex); |
349 | ||
350 | chip->client = client; | |
351 | i2c_set_clientdata(client, chip); | |
352 | ||
353 | chip->regmap = devm_regmap_init_i2c(client, &aw2013_regmap_config); | |
354 | if (IS_ERR(chip->regmap)) { | |
355 | ret = PTR_ERR(chip->regmap); | |
356 | dev_err(&client->dev, "Failed to allocate register map: %d\n", | |
357 | ret); | |
358 | goto error; | |
359 | } | |
360 | ||
baca986e LMB |
361 | chip->regulators[0].supply = "vcc"; |
362 | chip->regulators[1].supply = "vio"; | |
363 | ret = devm_regulator_bulk_get(&client->dev, | |
364 | ARRAY_SIZE(chip->regulators), | |
365 | chip->regulators); | |
366 | if (ret < 0) { | |
59ea3c9f NT |
367 | if (ret != -EPROBE_DEFER) |
368 | dev_err(&client->dev, | |
baca986e | 369 | "Failed to request regulators: %d\n", ret); |
59ea3c9f NT |
370 | goto error; |
371 | } | |
372 | ||
baca986e LMB |
373 | ret = regulator_bulk_enable(ARRAY_SIZE(chip->regulators), |
374 | chip->regulators); | |
59ea3c9f NT |
375 | if (ret) { |
376 | dev_err(&client->dev, | |
baca986e | 377 | "Failed to enable regulators: %d\n", ret); |
59ea3c9f NT |
378 | goto error; |
379 | } | |
380 | ||
381 | ret = regmap_read(chip->regmap, AW2013_RSTR, &chipid); | |
382 | if (ret) { | |
383 | dev_err(&client->dev, "Failed to read chip ID: %d\n", | |
384 | ret); | |
385 | goto error_reg; | |
386 | } | |
387 | ||
388 | if (chipid != AW2013_RSTR_CHIP_ID) { | |
389 | dev_err(&client->dev, "Chip reported wrong ID: %x\n", | |
390 | chipid); | |
391 | ret = -ENODEV; | |
392 | goto error_reg; | |
393 | } | |
394 | ||
fb74e4fa GS |
395 | ret = devm_add_action(&client->dev, aw2013_chip_disable_action, chip); |
396 | if (ret) | |
397 | goto error_reg; | |
398 | ||
59ea3c9f NT |
399 | ret = aw2013_probe_dt(chip); |
400 | if (ret < 0) | |
401 | goto error_reg; | |
402 | ||
baca986e LMB |
403 | ret = regulator_bulk_disable(ARRAY_SIZE(chip->regulators), |
404 | chip->regulators); | |
59ea3c9f NT |
405 | if (ret) { |
406 | dev_err(&client->dev, | |
baca986e | 407 | "Failed to disable regulators: %d\n", ret); |
59ea3c9f NT |
408 | goto error; |
409 | } | |
410 | ||
411 | mutex_unlock(&chip->mutex); | |
412 | ||
413 | return 0; | |
414 | ||
415 | error_reg: | |
baca986e LMB |
416 | regulator_bulk_disable(ARRAY_SIZE(chip->regulators), |
417 | chip->regulators); | |
59ea3c9f NT |
418 | |
419 | error: | |
6969d0a2 | 420 | mutex_unlock(&chip->mutex); |
59ea3c9f NT |
421 | return ret; |
422 | } | |
423 | ||
59ea3c9f NT |
424 | static const struct of_device_id aw2013_match_table[] = { |
425 | { .compatible = "awinic,aw2013", }, | |
426 | { /* sentinel */ }, | |
427 | }; | |
428 | ||
429 | MODULE_DEVICE_TABLE(of, aw2013_match_table); | |
430 | ||
431 | static struct i2c_driver aw2013_driver = { | |
432 | .driver = { | |
433 | .name = "leds-aw2013", | |
3d590af8 | 434 | .of_match_table = aw2013_match_table, |
59ea3c9f | 435 | }, |
d9ff8a8e | 436 | .probe = aw2013_probe, |
59ea3c9f NT |
437 | }; |
438 | ||
439 | module_i2c_driver(aw2013_driver); | |
440 | ||
441 | MODULE_AUTHOR("Nikita Travkin <nikitos.tr@gmail.com>"); | |
442 | MODULE_DESCRIPTION("AW2013 LED driver"); | |
443 | MODULE_LICENSE("GPL v2"); |