1 // SPDX-License-Identifier: GPL-2.0-only
3 * LP5521/LP5523/LP55231/LP5562 Common Driver
5 * Copyright 2012 Texas Instruments
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
9 * Derived from leds-lp5521.c, leds-lp5523.c
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 #include <dt-bindings/leds/leds-lp55xx.h>
23 #include "leds-lp55xx-common.h"
25 /* External clock rate */
26 #define LP55XX_CLK_32K 32768
28 static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
30 return container_of(cdev, struct lp55xx_led, cdev);
33 static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
35 return cdev_to_lp55xx_led(dev_get_drvdata(dev));
38 static struct lp55xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev)
40 return container_of(mc_cdev, struct lp55xx_led, mc_cdev);
43 static void lp55xx_reset_device(struct lp55xx_chip *chip)
45 struct lp55xx_device_config *cfg = chip->cfg;
46 u8 addr = cfg->reset.addr;
47 u8 val = cfg->reset.val;
49 /* no error checking here because no ACK from the device after reset */
50 lp55xx_write(chip, addr, val);
53 static int lp55xx_detect_device(struct lp55xx_chip *chip)
55 struct lp55xx_device_config *cfg = chip->cfg;
56 u8 addr = cfg->enable.addr;
57 u8 val = cfg->enable.val;
60 ret = lp55xx_write(chip, addr, val);
64 usleep_range(1000, 2000);
66 ret = lp55xx_read(chip, addr, &val);
70 if (val != cfg->enable.val)
76 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
78 struct lp55xx_device_config *cfg = chip->cfg;
80 if (!cfg->post_init_device)
83 return cfg->post_init_device(chip);
86 static ssize_t led_current_show(struct device *dev,
87 struct device_attribute *attr,
90 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
92 return sysfs_emit(buf, "%d\n", led->led_current);
95 static ssize_t led_current_store(struct device *dev,
96 struct device_attribute *attr,
97 const char *buf, size_t len)
99 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
100 struct lp55xx_chip *chip = led->chip;
103 if (kstrtoul(buf, 0, &curr))
106 if (curr > led->max_current)
109 if (!chip->cfg->set_led_current)
112 mutex_lock(&chip->lock);
113 chip->cfg->set_led_current(led, (u8)curr);
114 mutex_unlock(&chip->lock);
119 static ssize_t max_current_show(struct device *dev,
120 struct device_attribute *attr,
123 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
125 return sysfs_emit(buf, "%d\n", led->max_current);
128 static DEVICE_ATTR_RW(led_current);
129 static DEVICE_ATTR_RO(max_current);
131 static struct attribute *lp55xx_led_attrs[] = {
132 &dev_attr_led_current.attr,
133 &dev_attr_max_current.attr,
136 ATTRIBUTE_GROUPS(lp55xx_led);
138 static int lp55xx_set_mc_brightness(struct led_classdev *cdev,
139 enum led_brightness brightness)
141 struct led_classdev_mc *mc_dev = lcdev_to_mccdev(cdev);
142 struct lp55xx_led *led = mcled_cdev_to_led(mc_dev);
143 struct lp55xx_device_config *cfg = led->chip->cfg;
145 led_mc_calc_color_components(&led->mc_cdev, brightness);
146 return cfg->multicolor_brightness_fn(led);
150 static int lp55xx_set_brightness(struct led_classdev *cdev,
151 enum led_brightness brightness)
153 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
154 struct lp55xx_device_config *cfg = led->chip->cfg;
156 led->brightness = (u8)brightness;
157 return cfg->brightness_fn(led);
160 static int lp55xx_init_led(struct lp55xx_led *led,
161 struct lp55xx_chip *chip, int chan)
163 struct lp55xx_platform_data *pdata = chip->pdata;
164 struct lp55xx_device_config *cfg = chip->cfg;
165 struct device *dev = &chip->cl->dev;
166 int max_channel = cfg->max_channel;
167 struct mc_subled *mc_led_info;
168 struct led_classdev *led_cdev;
173 if (chan >= max_channel) {
174 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
178 if (pdata->led_config[chan].led_current == 0)
181 if (pdata->led_config[chan].name) {
182 led->cdev.name = pdata->led_config[chan].name;
184 snprintf(name, sizeof(name), "%s:channel%d",
185 pdata->label ? : chip->cl->name, chan);
186 led->cdev.name = name;
189 if (pdata->led_config[chan].num_colors > 1) {
190 mc_led_info = devm_kcalloc(dev,
191 pdata->led_config[chan].num_colors,
192 sizeof(*mc_led_info), GFP_KERNEL);
196 led_cdev = &led->mc_cdev.led_cdev;
197 led_cdev->name = led->cdev.name;
198 led_cdev->brightness_set_blocking = lp55xx_set_mc_brightness;
199 led->mc_cdev.num_colors = pdata->led_config[chan].num_colors;
200 for (i = 0; i < led->mc_cdev.num_colors; i++) {
201 mc_led_info[i].color_index =
202 pdata->led_config[chan].color_id[i];
203 mc_led_info[i].channel =
204 pdata->led_config[chan].output_num[i];
207 led->mc_cdev.subled_info = mc_led_info;
209 led->cdev.brightness_set_blocking = lp55xx_set_brightness;
212 led->cdev.groups = lp55xx_led_groups;
213 led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
214 led->led_current = pdata->led_config[chan].led_current;
215 led->max_current = pdata->led_config[chan].max_current;
216 led->chan_nr = pdata->led_config[chan].chan_nr;
218 if (led->chan_nr >= max_channel) {
219 dev_err(dev, "Use channel numbers between 0 and %d\n",
224 if (pdata->led_config[chan].num_colors > 1)
225 ret = devm_led_classdev_multicolor_register(dev, &led->mc_cdev);
227 ret = devm_led_classdev_register(dev, &led->cdev);
230 dev_err(dev, "led register err: %d\n", ret);
237 static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
239 struct lp55xx_chip *chip = context;
240 struct device *dev = &chip->cl->dev;
241 enum lp55xx_engine_index idx = chip->engine_idx;
244 dev_err(dev, "firmware request failed\n");
248 /* handling firmware data is chip dependent */
249 mutex_lock(&chip->lock);
251 chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
253 if (chip->cfg->firmware_cb)
254 chip->cfg->firmware_cb(chip);
256 mutex_unlock(&chip->lock);
258 /* firmware should be released for other channel use */
259 release_firmware(chip->fw);
263 static int lp55xx_request_firmware(struct lp55xx_chip *chip)
265 const char *name = chip->cl->name;
266 struct device *dev = &chip->cl->dev;
268 return request_firmware_nowait(THIS_MODULE, false, name, dev,
269 GFP_KERNEL, chip, lp55xx_firmware_loaded);
272 static ssize_t select_engine_show(struct device *dev,
273 struct device_attribute *attr,
276 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
277 struct lp55xx_chip *chip = led->chip;
279 return sprintf(buf, "%d\n", chip->engine_idx);
282 static ssize_t select_engine_store(struct device *dev,
283 struct device_attribute *attr,
284 const char *buf, size_t len)
286 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
287 struct lp55xx_chip *chip = led->chip;
291 if (kstrtoul(buf, 0, &val))
294 /* select the engine to be run */
297 case LP55XX_ENGINE_1:
298 case LP55XX_ENGINE_2:
299 case LP55XX_ENGINE_3:
300 mutex_lock(&chip->lock);
301 chip->engine_idx = val;
302 ret = lp55xx_request_firmware(chip);
303 mutex_unlock(&chip->lock);
306 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
311 dev_err(dev, "request firmware err: %d\n", ret);
318 static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
320 if (chip->cfg->run_engine)
321 chip->cfg->run_engine(chip, start);
324 static ssize_t run_engine_store(struct device *dev,
325 struct device_attribute *attr,
326 const char *buf, size_t len)
328 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
329 struct lp55xx_chip *chip = led->chip;
332 if (kstrtoul(buf, 0, &val))
335 /* run or stop the selected engine */
338 lp55xx_run_engine(chip, false);
342 mutex_lock(&chip->lock);
343 lp55xx_run_engine(chip, true);
344 mutex_unlock(&chip->lock);
349 static DEVICE_ATTR_RW(select_engine);
350 static DEVICE_ATTR_WO(run_engine);
352 static struct attribute *lp55xx_engine_attributes[] = {
353 &dev_attr_select_engine.attr,
354 &dev_attr_run_engine.attr,
358 static const struct attribute_group lp55xx_engine_attr_group = {
359 .attrs = lp55xx_engine_attributes,
362 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
364 return i2c_smbus_write_byte_data(chip->cl, reg, val);
366 EXPORT_SYMBOL_GPL(lp55xx_write);
368 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
372 ret = i2c_smbus_read_byte_data(chip->cl, reg);
379 EXPORT_SYMBOL_GPL(lp55xx_read);
381 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
386 ret = lp55xx_read(chip, reg, &tmp);
393 return lp55xx_write(chip, reg, tmp);
395 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
397 bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
402 clk = devm_clk_get(&chip->cl->dev, "32k_clk");
404 goto use_internal_clk;
406 err = clk_prepare_enable(clk);
408 goto use_internal_clk;
410 if (clk_get_rate(clk) != LP55XX_CLK_32K) {
411 clk_disable_unprepare(clk);
412 goto use_internal_clk;
415 dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K);
421 dev_info(&chip->cl->dev, "internal clock used\n");
424 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
426 int lp55xx_init_device(struct lp55xx_chip *chip)
428 struct lp55xx_platform_data *pdata;
429 struct lp55xx_device_config *cfg;
430 struct device *dev = &chip->cl->dev;
441 if (pdata->enable_gpiod) {
442 gpiod_direction_output(pdata->enable_gpiod, 0);
444 gpiod_set_consumer_name(pdata->enable_gpiod, "LP55xx enable");
445 gpiod_set_value(pdata->enable_gpiod, 0);
446 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
447 gpiod_set_value(pdata->enable_gpiod, 1);
448 usleep_range(1000, 2000); /* 500us abs min. */
451 lp55xx_reset_device(chip);
454 * Exact value is not available. 10 - 20ms
455 * appears to be enough for reset.
457 usleep_range(10000, 20000);
459 ret = lp55xx_detect_device(chip);
461 dev_err(dev, "device detection err: %d\n", ret);
465 /* chip specific initialization */
466 ret = lp55xx_post_init_device(chip);
468 dev_err(dev, "post init device err: %d\n", ret);
475 lp55xx_deinit_device(chip);
479 EXPORT_SYMBOL_GPL(lp55xx_init_device);
481 void lp55xx_deinit_device(struct lp55xx_chip *chip)
483 struct lp55xx_platform_data *pdata = chip->pdata;
486 clk_disable_unprepare(chip->clk);
488 if (pdata->enable_gpiod)
489 gpiod_set_value(pdata->enable_gpiod, 0);
491 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
493 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
495 struct lp55xx_platform_data *pdata = chip->pdata;
496 struct lp55xx_device_config *cfg = chip->cfg;
497 int num_channels = pdata->num_channels;
498 struct lp55xx_led *each;
503 if (!cfg->brightness_fn) {
504 dev_err(&chip->cl->dev, "empty brightness configuration\n");
508 for (i = 0; i < num_channels; i++) {
510 /* do not initialize channels that are not connected */
511 if (pdata->led_config[i].led_current == 0)
514 led_current = pdata->led_config[i].led_current;
516 ret = lp55xx_init_led(each, chip, i);
523 /* setting led current at each channel */
524 if (cfg->set_led_current)
525 cfg->set_led_current(each, led_current);
533 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
535 int lp55xx_register_sysfs(struct lp55xx_chip *chip)
537 struct device *dev = &chip->cl->dev;
538 struct lp55xx_device_config *cfg = chip->cfg;
541 if (!cfg->run_engine || !cfg->firmware_cb)
542 goto dev_specific_attrs;
544 ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
549 return cfg->dev_attr_group ?
550 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
552 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
554 void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
556 struct device *dev = &chip->cl->dev;
557 struct lp55xx_device_config *cfg = chip->cfg;
559 if (cfg->dev_attr_group)
560 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
562 sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
564 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
566 static int lp55xx_parse_common_child(struct device_node *np,
567 struct lp55xx_led_config *cfg,
568 int led_number, int *chan_nr)
572 of_property_read_string(np, "chan-name",
573 &cfg[led_number].name);
574 of_property_read_u8(np, "led-cur",
575 &cfg[led_number].led_current);
576 of_property_read_u8(np, "max-cur",
577 &cfg[led_number].max_current);
579 ret = of_property_read_u32(np, "reg", chan_nr);
583 if (*chan_nr < 0 || *chan_nr > cfg->max_channel)
589 static int lp55xx_parse_multi_led_child(struct device_node *child,
590 struct lp55xx_led_config *cfg,
591 int child_number, int color_number)
593 int chan_nr, color_id, ret;
595 ret = lp55xx_parse_common_child(child, cfg, child_number, &chan_nr);
599 ret = of_property_read_u32(child, "color", &color_id);
603 cfg[child_number].color_id[color_number] = color_id;
604 cfg[child_number].output_num[color_number] = chan_nr;
609 static int lp55xx_parse_multi_led(struct device_node *np,
610 struct lp55xx_led_config *cfg,
613 struct device_node *child;
614 int num_colors = 0, ret;
616 for_each_available_child_of_node(np, child) {
617 ret = lp55xx_parse_multi_led_child(child, cfg, child_number,
626 cfg[child_number].num_colors = num_colors;
631 static int lp55xx_parse_logical_led(struct device_node *np,
632 struct lp55xx_led_config *cfg,
638 cfg[child_number].default_trigger =
639 of_get_property(np, "linux,default-trigger", NULL);
641 ret = of_property_read_u32(np, "color", &led_color);
645 if (led_color == LED_COLOR_ID_RGB)
646 return lp55xx_parse_multi_led(np, cfg, child_number);
648 ret = lp55xx_parse_common_child(np, cfg, child_number, &chan_nr);
652 cfg[child_number].chan_nr = chan_nr;
657 struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
658 struct device_node *np,
659 struct lp55xx_chip *chip)
661 struct device_node *child;
662 struct lp55xx_platform_data *pdata;
663 struct lp55xx_led_config *cfg;
668 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
670 return ERR_PTR(-ENOMEM);
672 num_channels = of_get_available_child_count(np);
673 if (num_channels == 0) {
674 dev_err(dev, "no LED channels\n");
675 return ERR_PTR(-EINVAL);
678 cfg = devm_kcalloc(dev, num_channels, sizeof(*cfg), GFP_KERNEL);
680 return ERR_PTR(-ENOMEM);
682 pdata->led_config = &cfg[0];
683 pdata->num_channels = num_channels;
684 cfg->max_channel = chip->cfg->max_channel;
686 for_each_available_child_of_node(np, child) {
687 ret = lp55xx_parse_logical_led(child, cfg, i);
690 return ERR_PTR(-EINVAL);
695 if (of_property_read_u32(np, "ti,charge-pump-mode", &pdata->charge_pump_mode))
696 pdata->charge_pump_mode = LP55XX_CP_AUTO;
698 if (pdata->charge_pump_mode > LP55XX_CP_AUTO) {
699 dev_err(dev, "invalid charge pump mode %d\n", pdata->charge_pump_mode);
700 return ERR_PTR(-EINVAL);
703 of_property_read_string(np, "label", &pdata->label);
704 of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
706 pdata->enable_gpiod = devm_gpiod_get_optional(dev, "enable",
708 if (IS_ERR(pdata->enable_gpiod))
709 return ERR_CAST(pdata->enable_gpiod);
711 /* LP8501 specific */
712 of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
716 EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
718 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
719 MODULE_DESCRIPTION("LP55xx Common Driver");
720 MODULE_LICENSE("GPL");