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>
22 #include "leds-lp55xx-common.h"
24 /* External clock rate */
25 #define LP55XX_CLK_32K 32768
27 static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
29 return container_of(cdev, struct lp55xx_led, cdev);
32 static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
34 return cdev_to_lp55xx_led(dev_get_drvdata(dev));
37 static void lp55xx_reset_device(struct lp55xx_chip *chip)
39 struct lp55xx_device_config *cfg = chip->cfg;
40 u8 addr = cfg->reset.addr;
41 u8 val = cfg->reset.val;
43 /* no error checking here because no ACK from the device after reset */
44 lp55xx_write(chip, addr, val);
47 static int lp55xx_detect_device(struct lp55xx_chip *chip)
49 struct lp55xx_device_config *cfg = chip->cfg;
50 u8 addr = cfg->enable.addr;
51 u8 val = cfg->enable.val;
54 ret = lp55xx_write(chip, addr, val);
58 usleep_range(1000, 2000);
60 ret = lp55xx_read(chip, addr, &val);
64 if (val != cfg->enable.val)
70 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
72 struct lp55xx_device_config *cfg = chip->cfg;
74 if (!cfg->post_init_device)
77 return cfg->post_init_device(chip);
80 static ssize_t led_current_show(struct device *dev,
81 struct device_attribute *attr,
84 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
86 return scnprintf(buf, PAGE_SIZE, "%d\n", led->led_current);
89 static ssize_t led_current_store(struct device *dev,
90 struct device_attribute *attr,
91 const char *buf, size_t len)
93 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
94 struct lp55xx_chip *chip = led->chip;
97 if (kstrtoul(buf, 0, &curr))
100 if (curr > led->max_current)
103 if (!chip->cfg->set_led_current)
106 mutex_lock(&chip->lock);
107 chip->cfg->set_led_current(led, (u8)curr);
108 mutex_unlock(&chip->lock);
113 static ssize_t max_current_show(struct device *dev,
114 struct device_attribute *attr,
117 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
119 return scnprintf(buf, PAGE_SIZE, "%d\n", led->max_current);
122 static DEVICE_ATTR_RW(led_current);
123 static DEVICE_ATTR_RO(max_current);
125 static struct attribute *lp55xx_led_attrs[] = {
126 &dev_attr_led_current.attr,
127 &dev_attr_max_current.attr,
130 ATTRIBUTE_GROUPS(lp55xx_led);
132 static int lp55xx_set_brightness(struct led_classdev *cdev,
133 enum led_brightness brightness)
135 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
136 struct lp55xx_device_config *cfg = led->chip->cfg;
138 led->brightness = (u8)brightness;
139 return cfg->brightness_fn(led);
142 static int lp55xx_init_led(struct lp55xx_led *led,
143 struct lp55xx_chip *chip, int chan)
145 struct lp55xx_platform_data *pdata = chip->pdata;
146 struct lp55xx_device_config *cfg = chip->cfg;
147 struct device *dev = &chip->cl->dev;
150 int max_channel = cfg->max_channel;
152 if (chan >= max_channel) {
153 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
157 if (pdata->led_config[chan].led_current == 0)
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;
165 if (led->chan_nr >= max_channel) {
166 dev_err(dev, "Use channel numbers between 0 and %d\n",
171 led->cdev.brightness_set_blocking = lp55xx_set_brightness;
172 led->cdev.groups = lp55xx_led_groups;
174 if (pdata->led_config[chan].name) {
175 led->cdev.name = pdata->led_config[chan].name;
177 snprintf(name, sizeof(name), "%s:channel%d",
178 pdata->label ? : chip->cl->name, chan);
179 led->cdev.name = name;
182 ret = devm_led_classdev_register(dev, &led->cdev);
184 dev_err(dev, "led register err: %d\n", ret);
191 static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
193 struct lp55xx_chip *chip = context;
194 struct device *dev = &chip->cl->dev;
195 enum lp55xx_engine_index idx = chip->engine_idx;
198 dev_err(dev, "firmware request failed\n");
202 /* handling firmware data is chip dependent */
203 mutex_lock(&chip->lock);
205 chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
207 if (chip->cfg->firmware_cb)
208 chip->cfg->firmware_cb(chip);
210 mutex_unlock(&chip->lock);
212 /* firmware should be released for other channel use */
213 release_firmware(chip->fw);
217 static int lp55xx_request_firmware(struct lp55xx_chip *chip)
219 const char *name = chip->cl->name;
220 struct device *dev = &chip->cl->dev;
222 return request_firmware_nowait(THIS_MODULE, false, name, dev,
223 GFP_KERNEL, chip, lp55xx_firmware_loaded);
226 static ssize_t select_engine_show(struct device *dev,
227 struct device_attribute *attr,
230 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
231 struct lp55xx_chip *chip = led->chip;
233 return sprintf(buf, "%d\n", chip->engine_idx);
236 static ssize_t select_engine_store(struct device *dev,
237 struct device_attribute *attr,
238 const char *buf, size_t len)
240 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
241 struct lp55xx_chip *chip = led->chip;
245 if (kstrtoul(buf, 0, &val))
248 /* select the engine to be run */
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);
260 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
265 dev_err(dev, "request firmware err: %d\n", ret);
272 static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
274 if (chip->cfg->run_engine)
275 chip->cfg->run_engine(chip, start);
278 static ssize_t run_engine_store(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf, size_t len)
282 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
283 struct lp55xx_chip *chip = led->chip;
286 if (kstrtoul(buf, 0, &val))
289 /* run or stop the selected engine */
292 lp55xx_run_engine(chip, false);
296 mutex_lock(&chip->lock);
297 lp55xx_run_engine(chip, true);
298 mutex_unlock(&chip->lock);
303 static DEVICE_ATTR_RW(select_engine);
304 static DEVICE_ATTR_WO(run_engine);
306 static struct attribute *lp55xx_engine_attributes[] = {
307 &dev_attr_select_engine.attr,
308 &dev_attr_run_engine.attr,
312 static const struct attribute_group lp55xx_engine_attr_group = {
313 .attrs = lp55xx_engine_attributes,
316 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
318 return i2c_smbus_write_byte_data(chip->cl, reg, val);
320 EXPORT_SYMBOL_GPL(lp55xx_write);
322 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
326 ret = i2c_smbus_read_byte_data(chip->cl, reg);
333 EXPORT_SYMBOL_GPL(lp55xx_read);
335 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
340 ret = lp55xx_read(chip, reg, &tmp);
347 return lp55xx_write(chip, reg, tmp);
349 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
351 bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
356 clk = devm_clk_get(&chip->cl->dev, "32k_clk");
358 goto use_internal_clk;
360 err = clk_prepare_enable(clk);
362 goto use_internal_clk;
364 if (clk_get_rate(clk) != LP55XX_CLK_32K) {
365 clk_disable_unprepare(clk);
366 goto use_internal_clk;
369 dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K);
375 dev_info(&chip->cl->dev, "internal clock used\n");
378 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
380 int lp55xx_init_device(struct lp55xx_chip *chip)
382 struct lp55xx_platform_data *pdata;
383 struct lp55xx_device_config *cfg;
384 struct device *dev = &chip->cl->dev;
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. */
403 lp55xx_reset_device(chip);
406 * Exact value is not available. 10 - 20ms
407 * appears to be enough for reset.
409 usleep_range(10000, 20000);
411 ret = lp55xx_detect_device(chip);
413 dev_err(dev, "device detection err: %d\n", ret);
417 /* chip specific initialization */
418 ret = lp55xx_post_init_device(chip);
420 dev_err(dev, "post init device err: %d\n", ret);
427 lp55xx_deinit_device(chip);
431 EXPORT_SYMBOL_GPL(lp55xx_init_device);
433 void lp55xx_deinit_device(struct lp55xx_chip *chip)
435 struct lp55xx_platform_data *pdata = chip->pdata;
438 clk_disable_unprepare(chip->clk);
440 if (pdata->enable_gpiod)
441 gpiod_set_value(pdata->enable_gpiod, 0);
443 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
445 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
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;
455 if (!cfg->brightness_fn) {
456 dev_err(&chip->cl->dev, "empty brightness configuration\n");
460 for (i = 0; i < num_channels; i++) {
462 /* do not initialize channels that are not connected */
463 if (pdata->led_config[i].led_current == 0)
466 led_current = pdata->led_config[i].led_current;
468 ret = lp55xx_init_led(each, chip, i);
475 /* setting led current at each channel */
476 if (cfg->set_led_current)
477 cfg->set_led_current(each, led_current);
485 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
487 int lp55xx_register_sysfs(struct lp55xx_chip *chip)
489 struct device *dev = &chip->cl->dev;
490 struct lp55xx_device_config *cfg = chip->cfg;
493 if (!cfg->run_engine || !cfg->firmware_cb)
494 goto dev_specific_attrs;
496 ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
501 return cfg->dev_attr_group ?
502 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
504 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
506 void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
508 struct device *dev = &chip->cl->dev;
509 struct lp55xx_device_config *cfg = chip->cfg;
511 if (cfg->dev_attr_group)
512 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
514 sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
516 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
518 struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
519 struct device_node *np)
521 struct device_node *child;
522 struct lp55xx_platform_data *pdata;
523 struct lp55xx_led_config *cfg;
527 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
529 return ERR_PTR(-ENOMEM);
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);
537 cfg = devm_kcalloc(dev, num_channels, sizeof(*cfg), GFP_KERNEL);
539 return ERR_PTR(-ENOMEM);
541 pdata->led_config = &cfg[0];
542 pdata->num_channels = num_channels;
544 for_each_child_of_node(np, child) {
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);
556 of_property_read_string(np, "label", &pdata->label);
557 of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
559 pdata->enable_gpiod = devm_gpiod_get_optional(dev, "enable",
561 if (IS_ERR(pdata->enable_gpiod))
562 return ERR_CAST(pdata->enable_gpiod);
564 /* LP8501 specific */
565 of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
569 EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
571 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
572 MODULE_DESCRIPTION("LP55xx Common Driver");
573 MODULE_LICENSE("GPL");