1 // SPDX-License-Identifier: GPL-2.0-only
3 * LP5521/LP5523/LP55231/LP5562 Common Driver
5 * Copyright 2012 Texas Instruments
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.h>
21 #include <linux/of_gpio.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 void lp55xx_reset_device(struct lp55xx_chip *chip)
40 struct lp55xx_device_config *cfg = chip->cfg;
41 u8 addr = cfg->reset.addr;
42 u8 val = cfg->reset.val;
44 /* no error checking here because no ACK from the device after reset */
45 lp55xx_write(chip, addr, val);
48 static int lp55xx_detect_device(struct lp55xx_chip *chip)
50 struct lp55xx_device_config *cfg = chip->cfg;
51 u8 addr = cfg->enable.addr;
52 u8 val = cfg->enable.val;
55 ret = lp55xx_write(chip, addr, val);
59 usleep_range(1000, 2000);
61 ret = lp55xx_read(chip, addr, &val);
65 if (val != cfg->enable.val)
71 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
73 struct lp55xx_device_config *cfg = chip->cfg;
75 if (!cfg->post_init_device)
78 return cfg->post_init_device(chip);
81 static ssize_t lp55xx_show_current(struct device *dev,
82 struct device_attribute *attr,
85 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
87 return scnprintf(buf, PAGE_SIZE, "%d\n", led->led_current);
90 static ssize_t lp55xx_store_current(struct device *dev,
91 struct device_attribute *attr,
92 const char *buf, size_t len)
94 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
95 struct lp55xx_chip *chip = led->chip;
98 if (kstrtoul(buf, 0, &curr))
101 if (curr > led->max_current)
104 if (!chip->cfg->set_led_current)
107 mutex_lock(&chip->lock);
108 chip->cfg->set_led_current(led, (u8)curr);
109 mutex_unlock(&chip->lock);
114 static ssize_t lp55xx_show_max_current(struct device *dev,
115 struct device_attribute *attr,
118 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
120 return scnprintf(buf, PAGE_SIZE, "%d\n", led->max_current);
123 static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
124 lp55xx_store_current);
125 static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
127 static struct attribute *lp55xx_led_attrs[] = {
128 &dev_attr_led_current.attr,
129 &dev_attr_max_current.attr,
132 ATTRIBUTE_GROUPS(lp55xx_led);
134 static int lp55xx_set_brightness(struct led_classdev *cdev,
135 enum led_brightness brightness)
137 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
138 struct lp55xx_device_config *cfg = led->chip->cfg;
140 led->brightness = (u8)brightness;
141 return cfg->brightness_fn(led);
144 static int lp55xx_init_led(struct lp55xx_led *led,
145 struct lp55xx_chip *chip, int chan)
147 struct lp55xx_platform_data *pdata = chip->pdata;
148 struct lp55xx_device_config *cfg = chip->cfg;
149 struct device *dev = &chip->cl->dev;
152 int max_channel = cfg->max_channel;
154 if (chan >= max_channel) {
155 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
159 if (pdata->led_config[chan].led_current == 0)
162 led->led_current = pdata->led_config[chan].led_current;
163 led->max_current = pdata->led_config[chan].max_current;
164 led->chan_nr = pdata->led_config[chan].chan_nr;
165 led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
167 if (led->chan_nr >= max_channel) {
168 dev_err(dev, "Use channel numbers between 0 and %d\n",
173 led->cdev.brightness_set_blocking = lp55xx_set_brightness;
174 led->cdev.groups = lp55xx_led_groups;
176 if (pdata->led_config[chan].name) {
177 led->cdev.name = pdata->led_config[chan].name;
179 snprintf(name, sizeof(name), "%s:channel%d",
180 pdata->label ? : chip->cl->name, chan);
181 led->cdev.name = name;
184 ret = led_classdev_register(dev, &led->cdev);
186 dev_err(dev, "led register err: %d\n", ret);
193 static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
195 struct lp55xx_chip *chip = context;
196 struct device *dev = &chip->cl->dev;
197 enum lp55xx_engine_index idx = chip->engine_idx;
200 dev_err(dev, "firmware request failed\n");
204 /* handling firmware data is chip dependent */
205 mutex_lock(&chip->lock);
207 chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
209 if (chip->cfg->firmware_cb)
210 chip->cfg->firmware_cb(chip);
212 mutex_unlock(&chip->lock);
214 /* firmware should be released for other channel use */
215 release_firmware(chip->fw);
219 static int lp55xx_request_firmware(struct lp55xx_chip *chip)
221 const char *name = chip->cl->name;
222 struct device *dev = &chip->cl->dev;
224 return request_firmware_nowait(THIS_MODULE, false, name, dev,
225 GFP_KERNEL, chip, lp55xx_firmware_loaded);
228 static ssize_t lp55xx_show_engine_select(struct device *dev,
229 struct device_attribute *attr,
232 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
233 struct lp55xx_chip *chip = led->chip;
235 return sprintf(buf, "%d\n", chip->engine_idx);
238 static ssize_t lp55xx_store_engine_select(struct device *dev,
239 struct device_attribute *attr,
240 const char *buf, size_t len)
242 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
243 struct lp55xx_chip *chip = led->chip;
247 if (kstrtoul(buf, 0, &val))
250 /* select the engine to be run */
253 case LP55XX_ENGINE_1:
254 case LP55XX_ENGINE_2:
255 case LP55XX_ENGINE_3:
256 mutex_lock(&chip->lock);
257 chip->engine_idx = val;
258 ret = lp55xx_request_firmware(chip);
259 mutex_unlock(&chip->lock);
262 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
267 dev_err(dev, "request firmware err: %d\n", ret);
274 static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
276 if (chip->cfg->run_engine)
277 chip->cfg->run_engine(chip, start);
280 static ssize_t lp55xx_store_engine_run(struct device *dev,
281 struct device_attribute *attr,
282 const char *buf, size_t len)
284 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
285 struct lp55xx_chip *chip = led->chip;
288 if (kstrtoul(buf, 0, &val))
291 /* run or stop the selected engine */
294 lp55xx_run_engine(chip, false);
298 mutex_lock(&chip->lock);
299 lp55xx_run_engine(chip, true);
300 mutex_unlock(&chip->lock);
305 static DEVICE_ATTR(select_engine, S_IRUGO | S_IWUSR,
306 lp55xx_show_engine_select, lp55xx_store_engine_select);
307 static DEVICE_ATTR(run_engine, S_IWUSR, NULL, lp55xx_store_engine_run);
309 static struct attribute *lp55xx_engine_attributes[] = {
310 &dev_attr_select_engine.attr,
311 &dev_attr_run_engine.attr,
315 static const struct attribute_group lp55xx_engine_attr_group = {
316 .attrs = lp55xx_engine_attributes,
319 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
321 return i2c_smbus_write_byte_data(chip->cl, reg, val);
323 EXPORT_SYMBOL_GPL(lp55xx_write);
325 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
329 ret = i2c_smbus_read_byte_data(chip->cl, reg);
336 EXPORT_SYMBOL_GPL(lp55xx_read);
338 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
343 ret = lp55xx_read(chip, reg, &tmp);
350 return lp55xx_write(chip, reg, tmp);
352 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
354 bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
359 clk = devm_clk_get(&chip->cl->dev, "32k_clk");
361 goto use_internal_clk;
363 err = clk_prepare_enable(clk);
365 goto use_internal_clk;
367 if (clk_get_rate(clk) != LP55XX_CLK_32K) {
368 clk_disable_unprepare(clk);
369 goto use_internal_clk;
372 dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K);
378 dev_info(&chip->cl->dev, "internal clock used\n");
381 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
383 int lp55xx_init_device(struct lp55xx_chip *chip)
385 struct lp55xx_platform_data *pdata;
386 struct lp55xx_device_config *cfg;
387 struct device *dev = &chip->cl->dev;
398 if (gpio_is_valid(pdata->enable_gpio)) {
399 ret = devm_gpio_request_one(dev, pdata->enable_gpio,
400 GPIOF_DIR_OUT, "lp5523_enable");
402 dev_err(dev, "could not acquire enable gpio (err=%d)\n",
407 gpio_set_value(pdata->enable_gpio, 0);
408 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
409 gpio_set_value(pdata->enable_gpio, 1);
410 usleep_range(1000, 2000); /* 500us abs min. */
413 lp55xx_reset_device(chip);
416 * Exact value is not available. 10 - 20ms
417 * appears to be enough for reset.
419 usleep_range(10000, 20000);
421 ret = lp55xx_detect_device(chip);
423 dev_err(dev, "device detection err: %d\n", ret);
427 /* chip specific initialization */
428 ret = lp55xx_post_init_device(chip);
430 dev_err(dev, "post init device err: %d\n", ret);
437 lp55xx_deinit_device(chip);
441 EXPORT_SYMBOL_GPL(lp55xx_init_device);
443 void lp55xx_deinit_device(struct lp55xx_chip *chip)
445 struct lp55xx_platform_data *pdata = chip->pdata;
448 clk_disable_unprepare(chip->clk);
450 if (gpio_is_valid(pdata->enable_gpio))
451 gpio_set_value(pdata->enable_gpio, 0);
453 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
455 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
457 struct lp55xx_platform_data *pdata = chip->pdata;
458 struct lp55xx_device_config *cfg = chip->cfg;
459 int num_channels = pdata->num_channels;
460 struct lp55xx_led *each;
465 if (!cfg->brightness_fn) {
466 dev_err(&chip->cl->dev, "empty brightness configuration\n");
470 for (i = 0; i < num_channels; i++) {
472 /* do not initialize channels that are not connected */
473 if (pdata->led_config[i].led_current == 0)
476 led_current = pdata->led_config[i].led_current;
478 ret = lp55xx_init_led(each, chip, i);
485 /* setting led current at each channel */
486 if (cfg->set_led_current)
487 cfg->set_led_current(each, led_current);
493 lp55xx_unregister_leds(led, chip);
496 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
498 void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
501 struct lp55xx_led *each;
503 for (i = 0; i < chip->num_leds; i++) {
505 led_classdev_unregister(&each->cdev);
508 EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
510 int lp55xx_register_sysfs(struct lp55xx_chip *chip)
512 struct device *dev = &chip->cl->dev;
513 struct lp55xx_device_config *cfg = chip->cfg;
516 if (!cfg->run_engine || !cfg->firmware_cb)
517 goto dev_specific_attrs;
519 ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
524 return cfg->dev_attr_group ?
525 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
527 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
529 void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
531 struct device *dev = &chip->cl->dev;
532 struct lp55xx_device_config *cfg = chip->cfg;
534 if (cfg->dev_attr_group)
535 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
537 sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
539 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
541 struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
542 struct device_node *np)
544 struct device_node *child;
545 struct lp55xx_platform_data *pdata;
546 struct lp55xx_led_config *cfg;
550 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
552 return ERR_PTR(-ENOMEM);
554 num_channels = of_get_child_count(np);
555 if (num_channels == 0) {
556 dev_err(dev, "no LED channels\n");
557 return ERR_PTR(-EINVAL);
560 cfg = devm_kcalloc(dev, num_channels, sizeof(*cfg), GFP_KERNEL);
562 return ERR_PTR(-ENOMEM);
564 pdata->led_config = &cfg[0];
565 pdata->num_channels = num_channels;
567 for_each_child_of_node(np, child) {
570 of_property_read_string(child, "chan-name", &cfg[i].name);
571 of_property_read_u8(child, "led-cur", &cfg[i].led_current);
572 of_property_read_u8(child, "max-cur", &cfg[i].max_current);
573 cfg[i].default_trigger =
574 of_get_property(child, "linux,default-trigger", NULL);
579 of_property_read_string(np, "label", &pdata->label);
580 of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
582 pdata->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0);
584 /* LP8501 specific */
585 of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
589 EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
592 MODULE_DESCRIPTION("LP55xx Common Driver");
593 MODULE_LICENSE("GPL");