leds: lp55xx: Convert LED class registration to devm_*
[linux-block.git] / drivers / leds / leds-lp55xx-common.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LP5521/LP5523/LP55231/LP5562 Common Driver
4  *
5  * Copyright 2012 Texas Instruments
6  *
7  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8  *
9  * Derived from leds-lp5521.c, leds-lp5523.c
10  */
11
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/firmware.h>
15 #include <linux/i2c.h>
16 #include <linux/leds.h>
17 #include <linux/module.h>
18 #include <linux/platform_data/leds-lp55xx.h>
19 #include <linux/slab.h>
20 #include <linux/gpio/consumer.h>
21
22 #include "leds-lp55xx-common.h"
23
24 /* External clock rate */
25 #define LP55XX_CLK_32K                  32768
26
27 static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
28 {
29         return container_of(cdev, struct lp55xx_led, cdev);
30 }
31
32 static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
33 {
34         return cdev_to_lp55xx_led(dev_get_drvdata(dev));
35 }
36
37 static void lp55xx_reset_device(struct lp55xx_chip *chip)
38 {
39         struct lp55xx_device_config *cfg = chip->cfg;
40         u8 addr = cfg->reset.addr;
41         u8 val  = cfg->reset.val;
42
43         /* no error checking here because no ACK from the device after reset */
44         lp55xx_write(chip, addr, val);
45 }
46
47 static int lp55xx_detect_device(struct lp55xx_chip *chip)
48 {
49         struct lp55xx_device_config *cfg = chip->cfg;
50         u8 addr = cfg->enable.addr;
51         u8 val  = cfg->enable.val;
52         int ret;
53
54         ret = lp55xx_write(chip, addr, val);
55         if (ret)
56                 return ret;
57
58         usleep_range(1000, 2000);
59
60         ret = lp55xx_read(chip, addr, &val);
61         if (ret)
62                 return ret;
63
64         if (val != cfg->enable.val)
65                 return -ENODEV;
66
67         return 0;
68 }
69
70 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
71 {
72         struct lp55xx_device_config *cfg = chip->cfg;
73
74         if (!cfg->post_init_device)
75                 return 0;
76
77         return cfg->post_init_device(chip);
78 }
79
80 static ssize_t led_current_show(struct device *dev,
81                             struct device_attribute *attr,
82                             char *buf)
83 {
84         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
85
86         return scnprintf(buf, PAGE_SIZE, "%d\n", led->led_current);
87 }
88
89 static ssize_t led_current_store(struct device *dev,
90                              struct device_attribute *attr,
91                              const char *buf, size_t len)
92 {
93         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
94         struct lp55xx_chip *chip = led->chip;
95         unsigned long curr;
96
97         if (kstrtoul(buf, 0, &curr))
98                 return -EINVAL;
99
100         if (curr > led->max_current)
101                 return -EINVAL;
102
103         if (!chip->cfg->set_led_current)
104                 return len;
105
106         mutex_lock(&chip->lock);
107         chip->cfg->set_led_current(led, (u8)curr);
108         mutex_unlock(&chip->lock);
109
110         return len;
111 }
112
113 static ssize_t max_current_show(struct device *dev,
114                             struct device_attribute *attr,
115                             char *buf)
116 {
117         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
118
119         return scnprintf(buf, PAGE_SIZE, "%d\n", led->max_current);
120 }
121
122 static DEVICE_ATTR_RW(led_current);
123 static DEVICE_ATTR_RO(max_current);
124
125 static struct attribute *lp55xx_led_attrs[] = {
126         &dev_attr_led_current.attr,
127         &dev_attr_max_current.attr,
128         NULL,
129 };
130 ATTRIBUTE_GROUPS(lp55xx_led);
131
132 static int lp55xx_set_brightness(struct led_classdev *cdev,
133                              enum led_brightness brightness)
134 {
135         struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
136         struct lp55xx_device_config *cfg = led->chip->cfg;
137
138         led->brightness = (u8)brightness;
139         return cfg->brightness_fn(led);
140 }
141
142 static int lp55xx_init_led(struct lp55xx_led *led,
143                         struct lp55xx_chip *chip, int chan)
144 {
145         struct lp55xx_platform_data *pdata = chip->pdata;
146         struct lp55xx_device_config *cfg = chip->cfg;
147         struct device *dev = &chip->cl->dev;
148         char name[32];
149         int ret;
150         int max_channel = cfg->max_channel;
151
152         if (chan >= max_channel) {
153                 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
154                 return -EINVAL;
155         }
156
157         if (pdata->led_config[chan].led_current == 0)
158                 return 0;
159
160         led->led_current = pdata->led_config[chan].led_current;
161         led->max_current = pdata->led_config[chan].max_current;
162         led->chan_nr = pdata->led_config[chan].chan_nr;
163         led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
164
165         if (led->chan_nr >= max_channel) {
166                 dev_err(dev, "Use channel numbers between 0 and %d\n",
167                         max_channel - 1);
168                 return -EINVAL;
169         }
170
171         led->cdev.brightness_set_blocking = lp55xx_set_brightness;
172         led->cdev.groups = lp55xx_led_groups;
173
174         if (pdata->led_config[chan].name) {
175                 led->cdev.name = pdata->led_config[chan].name;
176         } else {
177                 snprintf(name, sizeof(name), "%s:channel%d",
178                         pdata->label ? : chip->cl->name, chan);
179                 led->cdev.name = name;
180         }
181
182         ret = devm_led_classdev_register(dev, &led->cdev);
183         if (ret) {
184                 dev_err(dev, "led register err: %d\n", ret);
185                 return ret;
186         }
187
188         return 0;
189 }
190
191 static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
192 {
193         struct lp55xx_chip *chip = context;
194         struct device *dev = &chip->cl->dev;
195         enum lp55xx_engine_index idx = chip->engine_idx;
196
197         if (!fw) {
198                 dev_err(dev, "firmware request failed\n");
199                 return;
200         }
201
202         /* handling firmware data is chip dependent */
203         mutex_lock(&chip->lock);
204
205         chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
206         chip->fw = fw;
207         if (chip->cfg->firmware_cb)
208                 chip->cfg->firmware_cb(chip);
209
210         mutex_unlock(&chip->lock);
211
212         /* firmware should be released for other channel use */
213         release_firmware(chip->fw);
214         chip->fw = NULL;
215 }
216
217 static int lp55xx_request_firmware(struct lp55xx_chip *chip)
218 {
219         const char *name = chip->cl->name;
220         struct device *dev = &chip->cl->dev;
221
222         return request_firmware_nowait(THIS_MODULE, false, name, dev,
223                                 GFP_KERNEL, chip, lp55xx_firmware_loaded);
224 }
225
226 static ssize_t select_engine_show(struct device *dev,
227                             struct device_attribute *attr,
228                             char *buf)
229 {
230         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
231         struct lp55xx_chip *chip = led->chip;
232
233         return sprintf(buf, "%d\n", chip->engine_idx);
234 }
235
236 static ssize_t select_engine_store(struct device *dev,
237                              struct device_attribute *attr,
238                              const char *buf, size_t len)
239 {
240         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
241         struct lp55xx_chip *chip = led->chip;
242         unsigned long val;
243         int ret;
244
245         if (kstrtoul(buf, 0, &val))
246                 return -EINVAL;
247
248         /* select the engine to be run */
249
250         switch (val) {
251         case LP55XX_ENGINE_1:
252         case LP55XX_ENGINE_2:
253         case LP55XX_ENGINE_3:
254                 mutex_lock(&chip->lock);
255                 chip->engine_idx = val;
256                 ret = lp55xx_request_firmware(chip);
257                 mutex_unlock(&chip->lock);
258                 break;
259         default:
260                 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
261                 return -EINVAL;
262         }
263
264         if (ret) {
265                 dev_err(dev, "request firmware err: %d\n", ret);
266                 return ret;
267         }
268
269         return len;
270 }
271
272 static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
273 {
274         if (chip->cfg->run_engine)
275                 chip->cfg->run_engine(chip, start);
276 }
277
278 static ssize_t run_engine_store(struct device *dev,
279                              struct device_attribute *attr,
280                              const char *buf, size_t len)
281 {
282         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
283         struct lp55xx_chip *chip = led->chip;
284         unsigned long val;
285
286         if (kstrtoul(buf, 0, &val))
287                 return -EINVAL;
288
289         /* run or stop the selected engine */
290
291         if (val <= 0) {
292                 lp55xx_run_engine(chip, false);
293                 return len;
294         }
295
296         mutex_lock(&chip->lock);
297         lp55xx_run_engine(chip, true);
298         mutex_unlock(&chip->lock);
299
300         return len;
301 }
302
303 static DEVICE_ATTR_RW(select_engine);
304 static DEVICE_ATTR_WO(run_engine);
305
306 static struct attribute *lp55xx_engine_attributes[] = {
307         &dev_attr_select_engine.attr,
308         &dev_attr_run_engine.attr,
309         NULL,
310 };
311
312 static const struct attribute_group lp55xx_engine_attr_group = {
313         .attrs = lp55xx_engine_attributes,
314 };
315
316 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
317 {
318         return i2c_smbus_write_byte_data(chip->cl, reg, val);
319 }
320 EXPORT_SYMBOL_GPL(lp55xx_write);
321
322 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
323 {
324         s32 ret;
325
326         ret = i2c_smbus_read_byte_data(chip->cl, reg);
327         if (ret < 0)
328                 return ret;
329
330         *val = ret;
331         return 0;
332 }
333 EXPORT_SYMBOL_GPL(lp55xx_read);
334
335 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
336 {
337         int ret;
338         u8 tmp;
339
340         ret = lp55xx_read(chip, reg, &tmp);
341         if (ret)
342                 return ret;
343
344         tmp &= ~mask;
345         tmp |= val & mask;
346
347         return lp55xx_write(chip, reg, tmp);
348 }
349 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
350
351 bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
352 {
353         struct clk *clk;
354         int err;
355
356         clk = devm_clk_get(&chip->cl->dev, "32k_clk");
357         if (IS_ERR(clk))
358                 goto use_internal_clk;
359
360         err = clk_prepare_enable(clk);
361         if (err)
362                 goto use_internal_clk;
363
364         if (clk_get_rate(clk) != LP55XX_CLK_32K) {
365                 clk_disable_unprepare(clk);
366                 goto use_internal_clk;
367         }
368
369         dev_info(&chip->cl->dev, "%dHz external clock used\n",  LP55XX_CLK_32K);
370
371         chip->clk = clk;
372         return true;
373
374 use_internal_clk:
375         dev_info(&chip->cl->dev, "internal clock used\n");
376         return false;
377 }
378 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
379
380 int lp55xx_init_device(struct lp55xx_chip *chip)
381 {
382         struct lp55xx_platform_data *pdata;
383         struct lp55xx_device_config *cfg;
384         struct device *dev = &chip->cl->dev;
385         int ret = 0;
386
387         WARN_ON(!chip);
388
389         pdata = chip->pdata;
390         cfg = chip->cfg;
391
392         if (!pdata || !cfg)
393                 return -EINVAL;
394
395         if (pdata->enable_gpiod) {
396                 gpiod_set_consumer_name(pdata->enable_gpiod, "LP55xx enable");
397                 gpiod_set_value(pdata->enable_gpiod, 0);
398                 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
399                 gpiod_set_value(pdata->enable_gpiod, 1);
400                 usleep_range(1000, 2000); /* 500us abs min. */
401         }
402
403         lp55xx_reset_device(chip);
404
405         /*
406          * Exact value is not available. 10 - 20ms
407          * appears to be enough for reset.
408          */
409         usleep_range(10000, 20000);
410
411         ret = lp55xx_detect_device(chip);
412         if (ret) {
413                 dev_err(dev, "device detection err: %d\n", ret);
414                 goto err;
415         }
416
417         /* chip specific initialization */
418         ret = lp55xx_post_init_device(chip);
419         if (ret) {
420                 dev_err(dev, "post init device err: %d\n", ret);
421                 goto err_post_init;
422         }
423
424         return 0;
425
426 err_post_init:
427         lp55xx_deinit_device(chip);
428 err:
429         return ret;
430 }
431 EXPORT_SYMBOL_GPL(lp55xx_init_device);
432
433 void lp55xx_deinit_device(struct lp55xx_chip *chip)
434 {
435         struct lp55xx_platform_data *pdata = chip->pdata;
436
437         if (chip->clk)
438                 clk_disable_unprepare(chip->clk);
439
440         if (pdata->enable_gpiod)
441                 gpiod_set_value(pdata->enable_gpiod, 0);
442 }
443 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
444
445 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
446 {
447         struct lp55xx_platform_data *pdata = chip->pdata;
448         struct lp55xx_device_config *cfg = chip->cfg;
449         int num_channels = pdata->num_channels;
450         struct lp55xx_led *each;
451         u8 led_current;
452         int ret;
453         int i;
454
455         if (!cfg->brightness_fn) {
456                 dev_err(&chip->cl->dev, "empty brightness configuration\n");
457                 return -EINVAL;
458         }
459
460         for (i = 0; i < num_channels; i++) {
461
462                 /* do not initialize channels that are not connected */
463                 if (pdata->led_config[i].led_current == 0)
464                         continue;
465
466                 led_current = pdata->led_config[i].led_current;
467                 each = led + i;
468                 ret = lp55xx_init_led(each, chip, i);
469                 if (ret)
470                         goto err_init_led;
471
472                 chip->num_leds++;
473                 each->chip = chip;
474
475                 /* setting led current at each channel */
476                 if (cfg->set_led_current)
477                         cfg->set_led_current(each, led_current);
478         }
479
480         return 0;
481
482 err_init_led:
483         return ret;
484 }
485 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
486
487 int lp55xx_register_sysfs(struct lp55xx_chip *chip)
488 {
489         struct device *dev = &chip->cl->dev;
490         struct lp55xx_device_config *cfg = chip->cfg;
491         int ret;
492
493         if (!cfg->run_engine || !cfg->firmware_cb)
494                 goto dev_specific_attrs;
495
496         ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
497         if (ret)
498                 return ret;
499
500 dev_specific_attrs:
501         return cfg->dev_attr_group ?
502                 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
503 }
504 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
505
506 void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
507 {
508         struct device *dev = &chip->cl->dev;
509         struct lp55xx_device_config *cfg = chip->cfg;
510
511         if (cfg->dev_attr_group)
512                 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
513
514         sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
515 }
516 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
517
518 struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
519                                                       struct device_node *np)
520 {
521         struct device_node *child;
522         struct lp55xx_platform_data *pdata;
523         struct lp55xx_led_config *cfg;
524         int num_channels;
525         int i = 0;
526
527         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
528         if (!pdata)
529                 return ERR_PTR(-ENOMEM);
530
531         num_channels = of_get_child_count(np);
532         if (num_channels == 0) {
533                 dev_err(dev, "no LED channels\n");
534                 return ERR_PTR(-EINVAL);
535         }
536
537         cfg = devm_kcalloc(dev, num_channels, sizeof(*cfg), GFP_KERNEL);
538         if (!cfg)
539                 return ERR_PTR(-ENOMEM);
540
541         pdata->led_config = &cfg[0];
542         pdata->num_channels = num_channels;
543
544         for_each_child_of_node(np, child) {
545                 cfg[i].chan_nr = i;
546
547                 of_property_read_string(child, "chan-name", &cfg[i].name);
548                 of_property_read_u8(child, "led-cur", &cfg[i].led_current);
549                 of_property_read_u8(child, "max-cur", &cfg[i].max_current);
550                 cfg[i].default_trigger =
551                         of_get_property(child, "linux,default-trigger", NULL);
552
553                 i++;
554         }
555
556         of_property_read_string(np, "label", &pdata->label);
557         of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
558
559         pdata->enable_gpiod = devm_gpiod_get_optional(dev, "enable",
560                                                       GPIOD_ASIS);
561         if (IS_ERR(pdata->enable_gpiod))
562                 return ERR_CAST(pdata->enable_gpiod);
563
564         /* LP8501 specific */
565         of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
566
567         return pdata;
568 }
569 EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
570
571 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
572 MODULE_DESCRIPTION("LP55xx Common Driver");
573 MODULE_LICENSE("GPL");