Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / leds / leds-aw2013.c
blob80d937454aeef3fc953dd3dd344f28a5b3a6a95e
1 // SPDX-License-Identifier: GPL-2.0+
2 // Driver for Awinic AW2013 3-channel LED driver
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>
12 #define AW2013_MAX_LEDS 3
14 /* Reset and ID register */
15 #define AW2013_RSTR 0x00
16 #define AW2013_RSTR_RESET 0x55
17 #define AW2013_RSTR_CHIP_ID 0x33
19 /* Global control register */
20 #define AW2013_GCR 0x01
21 #define AW2013_GCR_ENABLE BIT(0)
23 /* LED channel enable register */
24 #define AW2013_LCTR 0x30
25 #define AW2013_LCTR_LE(x) BIT((x))
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)
34 /* LED channel PWM registers */
35 #define AW2013_REG_PWM(x) (0x34 + (x))
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
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
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
50 #define AW2013_REG_MAX 0x77
52 #define AW2013_TIME_STEP 130 /* ms */
54 struct aw2013;
56 struct aw2013_led {
57 struct aw2013 *chip;
58 struct led_classdev cdev;
59 u32 num;
60 unsigned int imax;
63 struct aw2013 {
64 struct mutex mutex; /* held when writing to registers */
65 struct regulator *vcc_regulator;
66 struct i2c_client *client;
67 struct aw2013_led leds[AW2013_MAX_LEDS];
68 struct regmap *regmap;
69 int num_leds;
70 bool enabled;
73 static int aw2013_chip_init(struct aw2013 *chip)
75 int i, ret;
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;
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;
97 return ret;
100 static void aw2013_chip_disable(struct aw2013 *chip)
102 int ret;
104 if (!chip->enabled)
105 return;
107 regmap_write(chip->regmap, AW2013_GCR, 0);
109 ret = regulator_disable(chip->vcc_regulator);
110 if (ret) {
111 dev_err(&chip->client->dev,
112 "Failed to disable regulator: %d\n", ret);
113 return;
116 chip->enabled = false;
119 static int aw2013_chip_enable(struct aw2013 *chip)
121 int ret;
123 if (chip->enabled)
124 return 0;
126 ret = regulator_enable(chip->vcc_regulator);
127 if (ret) {
128 dev_err(&chip->client->dev,
129 "Failed to enable regulator: %d\n", ret);
130 return ret;
132 chip->enabled = true;
134 ret = aw2013_chip_init(chip);
135 if (ret)
136 aw2013_chip_disable(chip);
138 return ret;
141 static bool aw2013_chip_in_use(struct aw2013 *chip)
143 int i;
145 for (i = 0; i < chip->num_leds; i++)
146 if (chip->leds[i].cdev.brightness)
147 return true;
149 return false;
152 static int aw2013_brightness_set(struct led_classdev *cdev,
153 enum led_brightness brightness)
155 struct aw2013_led *led = container_of(cdev, struct aw2013_led, cdev);
156 int ret, num;
158 mutex_lock(&led->chip->mutex);
160 if (aw2013_chip_in_use(led->chip)) {
161 ret = aw2013_chip_enable(led->chip);
162 if (ret)
163 goto error;
166 num = led->num;
168 ret = regmap_write(led->chip->regmap, AW2013_REG_PWM(num), brightness);
169 if (ret)
170 goto error;
172 if (brightness) {
173 ret = regmap_update_bits(led->chip->regmap, AW2013_LCTR,
174 AW2013_LCTR_LE(num), 0xFF);
175 } else {
176 ret = regmap_update_bits(led->chip->regmap, AW2013_LCTR,
177 AW2013_LCTR_LE(num), 0);
178 if (ret)
179 goto error;
180 ret = regmap_update_bits(led->chip->regmap, AW2013_LCFG(num),
181 AW2013_LCFG_MD, 0);
183 if (ret)
184 goto error;
186 if (!aw2013_chip_in_use(led->chip))
187 aw2013_chip_disable(led->chip);
189 error:
190 mutex_unlock(&led->chip->mutex);
192 return ret;
195 static int aw2013_blink_set(struct led_classdev *cdev,
196 unsigned long *delay_on, unsigned long *delay_off)
198 struct aw2013_led *led = container_of(cdev, struct aw2013_led, cdev);
199 int ret, num = led->num;
200 unsigned long off = 0, on = 0;
202 /* If no blink specified, default to 1 Hz. */
203 if (!*delay_off && !*delay_on) {
204 *delay_off = 500;
205 *delay_on = 500;
208 if (!led->cdev.brightness) {
209 led->cdev.brightness = LED_FULL;
210 ret = aw2013_brightness_set(&led->cdev, led->cdev.brightness);
211 if (ret)
212 return ret;
215 /* Never on - just set to off */
216 if (!*delay_on) {
217 led->cdev.brightness = LED_OFF;
218 return aw2013_brightness_set(&led->cdev, LED_OFF);
221 mutex_lock(&led->chip->mutex);
223 /* Never off - brightness is already set, disable blinking */
224 if (!*delay_off) {
225 ret = regmap_update_bits(led->chip->regmap, AW2013_LCFG(num),
226 AW2013_LCFG_MD, 0);
227 goto out;
230 /* Convert into values the HW will understand. */
231 off = min(5, ilog2((*delay_off - 1) / AW2013_TIME_STEP) + 1);
232 on = min(7, ilog2((*delay_on - 1) / AW2013_TIME_STEP) + 1);
234 *delay_off = BIT(off) * AW2013_TIME_STEP;
235 *delay_on = BIT(on) * AW2013_TIME_STEP;
237 /* Set timings */
238 ret = regmap_write(led->chip->regmap,
239 AW2013_LEDT0(num), AW2013_LEDT0_T2(on));
240 if (ret)
241 goto out;
242 ret = regmap_write(led->chip->regmap,
243 AW2013_LEDT1(num), AW2013_LEDT1_T4(off));
244 if (ret)
245 goto out;
247 /* Finally, enable the LED */
248 ret = regmap_update_bits(led->chip->regmap, AW2013_LCFG(num),
249 AW2013_LCFG_MD, 0xFF);
250 if (ret)
251 goto out;
253 ret = regmap_update_bits(led->chip->regmap, AW2013_LCTR,
254 AW2013_LCTR_LE(num), 0xFF);
256 out:
257 mutex_unlock(&led->chip->mutex);
259 return ret;
262 static int aw2013_probe_dt(struct aw2013 *chip)
264 struct device_node *np = dev_of_node(&chip->client->dev), *child;
265 int count, ret = 0, i = 0;
266 struct aw2013_led *led;
268 count = of_get_available_child_count(np);
269 if (!count || count > AW2013_MAX_LEDS)
270 return -EINVAL;
272 regmap_write(chip->regmap, AW2013_RSTR, AW2013_RSTR_RESET);
274 for_each_available_child_of_node(np, child) {
275 struct led_init_data init_data = {};
276 u32 source;
277 u32 imax;
279 ret = of_property_read_u32(child, "reg", &source);
280 if (ret != 0 || source >= AW2013_MAX_LEDS) {
281 dev_err(&chip->client->dev,
282 "Couldn't read LED address: %d\n", ret);
283 count--;
284 continue;
287 led = &chip->leds[i];
288 led->num = source;
289 led->chip = chip;
290 init_data.fwnode = of_fwnode_handle(child);
292 if (!of_property_read_u32(child, "led-max-microamp", &imax)) {
293 led->imax = min_t(u32, imax / 5000, 3);
294 } else {
295 led->imax = 1; // 5mA
296 dev_info(&chip->client->dev,
297 "DT property led-max-microamp is missing\n");
300 led->cdev.brightness_set_blocking = aw2013_brightness_set;
301 led->cdev.blink_set = aw2013_blink_set;
303 ret = devm_led_classdev_register_ext(&chip->client->dev,
304 &led->cdev, &init_data);
305 if (ret < 0) {
306 of_node_put(child);
307 return ret;
310 i++;
313 if (!count)
314 return -EINVAL;
316 chip->num_leds = i;
318 return 0;
321 static const struct regmap_config aw2013_regmap_config = {
322 .reg_bits = 8,
323 .val_bits = 8,
324 .max_register = AW2013_REG_MAX,
327 static int aw2013_probe(struct i2c_client *client)
329 struct aw2013 *chip;
330 int ret;
331 unsigned int chipid;
333 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
334 if (!chip)
335 return -ENOMEM;
337 mutex_init(&chip->mutex);
338 mutex_lock(&chip->mutex);
340 chip->client = client;
341 i2c_set_clientdata(client, chip);
343 chip->regmap = devm_regmap_init_i2c(client, &aw2013_regmap_config);
344 if (IS_ERR(chip->regmap)) {
345 ret = PTR_ERR(chip->regmap);
346 dev_err(&client->dev, "Failed to allocate register map: %d\n",
347 ret);
348 goto error;
351 chip->vcc_regulator = devm_regulator_get(&client->dev, "vcc");
352 ret = PTR_ERR_OR_ZERO(chip->vcc_regulator);
353 if (ret) {
354 if (ret != -EPROBE_DEFER)
355 dev_err(&client->dev,
356 "Failed to request regulator: %d\n", ret);
357 goto error;
360 ret = regulator_enable(chip->vcc_regulator);
361 if (ret) {
362 dev_err(&client->dev,
363 "Failed to enable regulator: %d\n", ret);
364 goto error;
367 ret = regmap_read(chip->regmap, AW2013_RSTR, &chipid);
368 if (ret) {
369 dev_err(&client->dev, "Failed to read chip ID: %d\n",
370 ret);
371 goto error_reg;
374 if (chipid != AW2013_RSTR_CHIP_ID) {
375 dev_err(&client->dev, "Chip reported wrong ID: %x\n",
376 chipid);
377 ret = -ENODEV;
378 goto error_reg;
381 ret = aw2013_probe_dt(chip);
382 if (ret < 0)
383 goto error_reg;
385 ret = regulator_disable(chip->vcc_regulator);
386 if (ret) {
387 dev_err(&client->dev,
388 "Failed to disable regulator: %d\n", ret);
389 goto error;
392 mutex_unlock(&chip->mutex);
394 return 0;
396 error_reg:
397 regulator_disable(chip->vcc_regulator);
399 error:
400 mutex_destroy(&chip->mutex);
401 return ret;
404 static int aw2013_remove(struct i2c_client *client)
406 struct aw2013 *chip = i2c_get_clientdata(client);
408 aw2013_chip_disable(chip);
410 mutex_destroy(&chip->mutex);
412 return 0;
415 static const struct of_device_id aw2013_match_table[] = {
416 { .compatible = "awinic,aw2013", },
417 { /* sentinel */ },
420 MODULE_DEVICE_TABLE(of, aw2013_match_table);
422 static struct i2c_driver aw2013_driver = {
423 .driver = {
424 .name = "leds-aw2013",
425 .of_match_table = of_match_ptr(aw2013_match_table),
427 .probe_new = aw2013_probe,
428 .remove = aw2013_remove,
431 module_i2c_driver(aw2013_driver);
433 MODULE_AUTHOR("Nikita Travkin <nikitos.tr@gmail.com>");
434 MODULE_DESCRIPTION("AW2013 LED driver");
435 MODULE_LICENSE("GPL v2");