leds: is31fl319x: Use non-wildcard names for vars, structs and defines
[platform/kernel/linux-starfive.git] / drivers / leds / leds-is31fl319x.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2015-16 Golden Delicious Computers
4  *
5  * Author: Nikolaus Schaller <hns@goldelico.com>
6  *
7  * LED driver for the IS31FL319{0,1,3,6,9} to drive 1, 3, 6 or 9 light
8  * effect LEDs.
9  */
10
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
21
22 /* register numbers */
23 #define IS31FL319X_SHUTDOWN             0x00
24 #define IS31FL3196_CTRL1                0x01
25 #define IS31FL3196_CTRL2                0x02
26 #define IS31FL3196_CONFIG1              0x03
27 #define IS31FL3196_CONFIG2              0x04
28 #define IS31FL3196_RAMP_MODE            0x05
29 #define IS31FL3196_BREATH_MARK          0x06
30 #define IS31FL3196_PWM(channel)         (0x07 + channel)
31 #define IS31FL3196_DATA_UPDATE          0x10
32 #define IS31FL3196_T0(channel)          (0x11 + channel)
33 #define IS31FL3196_T123_1               0x1a
34 #define IS31FL3196_T123_2               0x1b
35 #define IS31FL3196_T123_3               0x1c
36 #define IS31FL3196_T4(channel)          (0x1d + channel)
37 #define IS31FL3196_TIME_UPDATE          0x26
38 #define IS31FL3196_RESET                0xff
39
40 #define IS31FL3196_REG_CNT              (IS31FL3196_RESET + 1)
41
42 #define IS31FL319X_MAX_LEDS             9
43
44 /* CS (Current Setting) in CONFIG2 register */
45 #define IS31FL3196_CONFIG2_CS_SHIFT     4
46 #define IS31FL3196_CONFIG2_CS_MASK      GENMASK(2, 0)
47 #define IS31FL3196_CONFIG2_CS_STEP_REF  12
48
49 #define IS31FL3196_CURRENT_uA_MIN       5000
50 #define IS31FL3196_CURRENT_uA_MAX       40000
51 #define IS31FL3196_CURRENT_uA_STEP      5000
52 #define IS31FL3196_CURRENT_uA_DEFAULT   20000
53
54 /* Audio gain in CONFIG2 register */
55 #define IS31FL3196_AUDIO_GAIN_DB_MAX    ((u32)21)
56 #define IS31FL3196_AUDIO_GAIN_DB_STEP   3
57
58 /*
59  * regmap is used as a cache of chip's register space,
60  * to avoid reading back brightness values from chip,
61  * which is known to hang.
62  */
63 struct is31fl319x_chip {
64         const struct is31fl319x_chipdef *cdef;
65         struct i2c_client               *client;
66         struct gpio_desc                *shutdown_gpio;
67         struct regmap                   *regmap;
68         struct mutex                    lock;
69         u32                             audio_gain_db;
70
71         struct is31fl319x_led {
72                 struct is31fl319x_chip  *chip;
73                 struct led_classdev     cdev;
74                 u32                     max_microamp;
75                 bool                    configured;
76         } leds[IS31FL319X_MAX_LEDS];
77 };
78
79 struct is31fl319x_chipdef {
80         int num_leds;
81 };
82
83 static const struct is31fl319x_chipdef is31fl3190_cdef = {
84         .num_leds = 1,
85 };
86
87 static const struct is31fl319x_chipdef is31fl3193_cdef = {
88         .num_leds = 3,
89 };
90
91 static const struct is31fl319x_chipdef is31fl3196_cdef = {
92         .num_leds = 6,
93 };
94
95 static const struct is31fl319x_chipdef is31fl3199_cdef = {
96         .num_leds = 9,
97 };
98
99 static const struct of_device_id of_is31fl319x_match[] = {
100         { .compatible = "issi,is31fl3190", .data = &is31fl3190_cdef, },
101         { .compatible = "issi,is31fl3191", .data = &is31fl3190_cdef, },
102         { .compatible = "issi,is31fl3193", .data = &is31fl3193_cdef, },
103         { .compatible = "issi,is31fl3196", .data = &is31fl3196_cdef, },
104         { .compatible = "issi,is31fl3199", .data = &is31fl3199_cdef, },
105         { .compatible = "si-en,sn3190",    .data = &is31fl3190_cdef, },
106         { .compatible = "si-en,sn3191",    .data = &is31fl3190_cdef, },
107         { .compatible = "si-en,sn3193",    .data = &is31fl3193_cdef, },
108         { .compatible = "si-en,sn3196",    .data = &is31fl3196_cdef, },
109         { .compatible = "si-en,sn3199",    .data = &is31fl3199_cdef, },
110         { }
111 };
112 MODULE_DEVICE_TABLE(of, of_is31fl319x_match);
113
114 static int is31fl3196_brightness_set(struct led_classdev *cdev,
115                                      enum led_brightness brightness)
116 {
117         struct is31fl319x_led *led = container_of(cdev, struct is31fl319x_led,
118                                                   cdev);
119         struct is31fl319x_chip *is31 = led->chip;
120         int chan = led - is31->leds;
121         int ret;
122         int i;
123         u8 ctrl1 = 0, ctrl2 = 0;
124
125         dev_dbg(&is31->client->dev, "%s %d: %d\n", __func__, chan, brightness);
126
127         mutex_lock(&is31->lock);
128
129         /* update PWM register */
130         ret = regmap_write(is31->regmap, IS31FL3196_PWM(chan), brightness);
131         if (ret < 0)
132                 goto out;
133
134         /* read current brightness of all PWM channels */
135         for (i = 0; i < is31->cdef->num_leds; i++) {
136                 unsigned int pwm_value;
137                 bool on;
138
139                 /*
140                  * since neither cdev nor the chip can provide
141                  * the current setting, we read from the regmap cache
142                  */
143
144                 ret = regmap_read(is31->regmap, IS31FL3196_PWM(i), &pwm_value);
145                 dev_dbg(&is31->client->dev, "%s read %d: ret=%d: %d\n",
146                         __func__, i, ret, pwm_value);
147                 on = ret >= 0 && pwm_value > LED_OFF;
148
149                 if (i < 3)
150                         ctrl1 |= on << i;       /* 0..2 => bit 0..2 */
151                 else if (i < 6)
152                         ctrl1 |= on << (i + 1); /* 3..5 => bit 4..6 */
153                 else
154                         ctrl2 |= on << (i - 6); /* 6..8 => bit 0..2 */
155         }
156
157         if (ctrl1 > 0 || ctrl2 > 0) {
158                 dev_dbg(&is31->client->dev, "power up %02x %02x\n",
159                         ctrl1, ctrl2);
160                 regmap_write(is31->regmap, IS31FL3196_CTRL1, ctrl1);
161                 regmap_write(is31->regmap, IS31FL3196_CTRL2, ctrl2);
162                 /* update PWMs */
163                 regmap_write(is31->regmap, IS31FL3196_DATA_UPDATE, 0x00);
164                 /* enable chip from shut down */
165                 ret = regmap_write(is31->regmap, IS31FL319X_SHUTDOWN, 0x01);
166         } else {
167                 dev_dbg(&is31->client->dev, "power down\n");
168                 /* shut down (no need to clear CTRL1/2) */
169                 ret = regmap_write(is31->regmap, IS31FL319X_SHUTDOWN, 0x00);
170         }
171
172 out:
173         mutex_unlock(&is31->lock);
174
175         return ret;
176 }
177
178 static int is31fl319x_parse_child_dt(const struct device *dev,
179                                      const struct device_node *child,
180                                      struct is31fl319x_led *led)
181 {
182         struct led_classdev *cdev = &led->cdev;
183         int ret;
184
185         if (of_property_read_string(child, "label", &cdev->name))
186                 cdev->name = child->name;
187
188         ret = of_property_read_string(child, "linux,default-trigger",
189                                       &cdev->default_trigger);
190         if (ret < 0 && ret != -EINVAL) /* is optional */
191                 return ret;
192
193         led->max_microamp = IS31FL3196_CURRENT_uA_DEFAULT;
194         ret = of_property_read_u32(child, "led-max-microamp",
195                                    &led->max_microamp);
196         if (!ret) {
197                 if (led->max_microamp < IS31FL3196_CURRENT_uA_MIN)
198                         return -EINVAL; /* not supported */
199                 led->max_microamp = min(led->max_microamp,
200                                           IS31FL3196_CURRENT_uA_MAX);
201         }
202
203         return 0;
204 }
205
206 static int is31fl319x_parse_dt(struct device *dev,
207                                struct is31fl319x_chip *is31)
208 {
209         struct device_node *np = dev_of_node(dev), *child;
210         int count;
211         int ret;
212
213         if (!np)
214                 return -ENODEV;
215
216         is31->shutdown_gpio = devm_gpiod_get_optional(dev,
217                                                 "shutdown",
218                                                 GPIOD_OUT_HIGH);
219         if (IS_ERR(is31->shutdown_gpio)) {
220                 ret = PTR_ERR(is31->shutdown_gpio);
221                 dev_err(dev, "Failed to get shutdown gpio: %d\n", ret);
222                 return ret;
223         }
224
225         is31->cdef = device_get_match_data(dev);
226
227         count = of_get_available_child_count(np);
228
229         dev_dbg(dev, "probing with %d leds defined in DT\n", count);
230
231         if (!count || count > is31->cdef->num_leds) {
232                 dev_err(dev, "Number of leds defined must be between 1 and %u\n",
233                         is31->cdef->num_leds);
234                 return -ENODEV;
235         }
236
237         for_each_available_child_of_node(np, child) {
238                 struct is31fl319x_led *led;
239                 u32 reg;
240
241                 ret = of_property_read_u32(child, "reg", &reg);
242                 if (ret) {
243                         dev_err(dev, "Failed to read led 'reg' property\n");
244                         goto put_child_node;
245                 }
246
247                 if (reg < 1 || reg > is31->cdef->num_leds) {
248                         dev_err(dev, "invalid led reg %u\n", reg);
249                         ret = -EINVAL;
250                         goto put_child_node;
251                 }
252
253                 led = &is31->leds[reg - 1];
254
255                 if (led->configured) {
256                         dev_err(dev, "led %u is already configured\n", reg);
257                         ret = -EINVAL;
258                         goto put_child_node;
259                 }
260
261                 ret = is31fl319x_parse_child_dt(dev, child, led);
262                 if (ret) {
263                         dev_err(dev, "led %u DT parsing failed\n", reg);
264                         goto put_child_node;
265                 }
266
267                 led->configured = true;
268         }
269
270         is31->audio_gain_db = 0;
271         ret = of_property_read_u32(np, "audio-gain-db", &is31->audio_gain_db);
272         if (!ret)
273                 is31->audio_gain_db = min(is31->audio_gain_db,
274                                           IS31FL3196_AUDIO_GAIN_DB_MAX);
275
276         return 0;
277
278 put_child_node:
279         of_node_put(child);
280         return ret;
281 }
282
283 static bool is31fl319x_readable_reg(struct device *dev, unsigned int reg)
284 { /* we have no readable registers */
285         return false;
286 }
287
288 static bool is31fl3196_volatile_reg(struct device *dev, unsigned int reg)
289 { /* volatile registers are not cached */
290         switch (reg) {
291         case IS31FL3196_DATA_UPDATE:
292         case IS31FL3196_TIME_UPDATE:
293         case IS31FL3196_RESET:
294                 return true; /* always write-through */
295         default:
296                 return false;
297         }
298 }
299
300 static const struct reg_default is31fl3196_reg_defaults[] = {
301         { IS31FL3196_CONFIG1, 0x00 },
302         { IS31FL3196_CONFIG2, 0x00 },
303         { IS31FL3196_PWM(0), 0x00 },
304         { IS31FL3196_PWM(1), 0x00 },
305         { IS31FL3196_PWM(2), 0x00 },
306         { IS31FL3196_PWM(3), 0x00 },
307         { IS31FL3196_PWM(4), 0x00 },
308         { IS31FL3196_PWM(5), 0x00 },
309         { IS31FL3196_PWM(6), 0x00 },
310         { IS31FL3196_PWM(7), 0x00 },
311         { IS31FL3196_PWM(8), 0x00 },
312 };
313
314 static struct regmap_config is31fl3196_regmap_config = {
315         .reg_bits = 8,
316         .val_bits = 8,
317         .max_register = IS31FL3196_REG_CNT,
318         .cache_type = REGCACHE_FLAT,
319         .readable_reg = is31fl319x_readable_reg,
320         .volatile_reg = is31fl3196_volatile_reg,
321         .reg_defaults = is31fl3196_reg_defaults,
322         .num_reg_defaults = ARRAY_SIZE(is31fl3196_reg_defaults),
323 };
324
325 static inline int is31fl3196_microamp_to_cs(struct device *dev, u32 microamp)
326 { /* round down to nearest supported value (range check done by caller) */
327         u32 step = microamp / IS31FL3196_CURRENT_uA_STEP;
328
329         return ((IS31FL3196_CONFIG2_CS_STEP_REF - step) &
330                 IS31FL3196_CONFIG2_CS_MASK) <<
331                 IS31FL3196_CONFIG2_CS_SHIFT; /* CS encoding */
332 }
333
334 static inline int is31fl3196_db_to_gain(u32 dezibel)
335 { /* round down to nearest supported value (range check done by caller) */
336         return dezibel / IS31FL3196_AUDIO_GAIN_DB_STEP;
337 }
338
339 static int is31fl319x_probe(struct i2c_client *client,
340                             const struct i2c_device_id *id)
341 {
342         struct is31fl319x_chip *is31;
343         struct device *dev = &client->dev;
344         int err;
345         int i = 0;
346         u32 aggregated_led_microamp = IS31FL3196_CURRENT_uA_MAX;
347
348         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
349                 return -EIO;
350
351         is31 = devm_kzalloc(&client->dev, sizeof(*is31), GFP_KERNEL);
352         if (!is31)
353                 return -ENOMEM;
354
355         mutex_init(&is31->lock);
356
357         err = is31fl319x_parse_dt(&client->dev, is31);
358         if (err)
359                 goto free_mutex;
360
361         if (is31->shutdown_gpio) {
362                 gpiod_direction_output(is31->shutdown_gpio, 0);
363                 mdelay(5);
364                 gpiod_direction_output(is31->shutdown_gpio, 1);
365         }
366
367         is31->client = client;
368         is31->regmap = devm_regmap_init_i2c(client, &is31fl3196_regmap_config);
369         if (IS_ERR(is31->regmap)) {
370                 dev_err(&client->dev, "failed to allocate register map\n");
371                 err = PTR_ERR(is31->regmap);
372                 goto free_mutex;
373         }
374
375         i2c_set_clientdata(client, is31);
376
377         /* check for write-reply from chip (we can't read any registers) */
378         err = regmap_write(is31->regmap, IS31FL3196_RESET, 0x00);
379         if (err < 0) {
380                 dev_err(&client->dev, "no response from chip write: err = %d\n",
381                         err);
382                 err = -EIO; /* does not answer */
383                 goto free_mutex;
384         }
385
386         /*
387          * Kernel conventions require per-LED led-max-microamp property.
388          * But the chip does not allow to limit individual LEDs.
389          * So we take minimum from all subnodes for safety of hardware.
390          */
391         for (i = 0; i < is31->cdef->num_leds; i++)
392                 if (is31->leds[i].configured &&
393                     is31->leds[i].max_microamp < aggregated_led_microamp)
394                         aggregated_led_microamp = is31->leds[i].max_microamp;
395
396         regmap_write(is31->regmap, IS31FL3196_CONFIG2,
397                      is31fl3196_microamp_to_cs(dev, aggregated_led_microamp) |
398                      is31fl3196_db_to_gain(is31->audio_gain_db));
399
400         for (i = 0; i < is31->cdef->num_leds; i++) {
401                 struct is31fl319x_led *led = &is31->leds[i];
402
403                 if (!led->configured)
404                         continue;
405
406                 led->chip = is31;
407                 led->cdev.brightness_set_blocking = is31fl3196_brightness_set;
408
409                 err = devm_led_classdev_register(&client->dev, &led->cdev);
410                 if (err < 0)
411                         goto free_mutex;
412         }
413
414         return 0;
415
416 free_mutex:
417         mutex_destroy(&is31->lock);
418         return err;
419 }
420
421 static int is31fl319x_remove(struct i2c_client *client)
422 {
423         struct is31fl319x_chip *is31 = i2c_get_clientdata(client);
424
425         mutex_destroy(&is31->lock);
426         return 0;
427 }
428
429 /*
430  * i2c-core (and modalias) requires that id_table be properly filled,
431  * even though it is not used for DeviceTree based instantiation.
432  */
433 static const struct i2c_device_id is31fl319x_id[] = {
434         { "is31fl3190" },
435         { "is31fl3191" },
436         { "is31fl3193" },
437         { "is31fl3196" },
438         { "is31fl3199" },
439         { "sn3190" },
440         { "sn3191" },
441         { "sn3193" },
442         { "sn3196" },
443         { "sn3199" },
444         {},
445 };
446 MODULE_DEVICE_TABLE(i2c, is31fl319x_id);
447
448 static struct i2c_driver is31fl319x_driver = {
449         .driver   = {
450                 .name           = "leds-is31fl319x",
451                 .of_match_table = of_match_ptr(of_is31fl319x_match),
452         },
453         .probe    = is31fl319x_probe,
454         .remove   = is31fl319x_remove,
455         .id_table = is31fl319x_id,
456 };
457
458 module_i2c_driver(is31fl319x_driver);
459
460 MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
461 MODULE_AUTHOR("Andrey Utkin <andrey_utkin@fastmail.com>");
462 MODULE_DESCRIPTION("IS31FL319X LED driver");
463 MODULE_LICENSE("GPL v2");