Linux Audio

Check our new training course

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