blob: 8e7074f0fee002138581614f71a618e7d70438f7 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +09002/*
Kim, Miloff45262a2013-02-18 21:10:14 -08003 * LP5521/LP5523/LP55231/LP5562 Common Driver
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +09004 *
5 * Copyright 2012 Texas Instruments
6 *
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 *
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +09009 * Derived from leds-lp5521.c, leds-lp5523.c
10 */
11
Kim, Milo53b41922013-03-20 17:37:00 -070012#include <linux/clk.h>
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +090013#include <linux/delay.h>
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +090014#include <linux/firmware.h>
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +090015#include <linux/i2c.h>
16#include <linux/leds.h>
17#include <linux/module.h>
18#include <linux/platform_data/leds-lp55xx.h>
Linus Walleij7542a04b2013-04-23 04:52:59 -070019#include <linux/slab.h>
Linus Walleijac219bf2020-06-27 00:40:11 +020020#include <linux/gpio/consumer.h>
Maarten Zanders54a7bef2023-04-21 09:53:05 +020021#include <dt-bindings/leds/leds-lp55xx.h>
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +090022
23#include "leds-lp55xx-common.h"
24
Kim, Milo53b41922013-03-20 17:37:00 -070025/* External clock rate */
26#define LP55XX_CLK_32K 32768
27
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +090028static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
29{
30 return container_of(cdev, struct lp55xx_led, cdev);
31}
32
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +090033static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
34{
35 return cdev_to_lp55xx_led(dev_get_drvdata(dev));
36}
37
Dan Murphy92a81562020-07-16 13:20:01 -050038static struct lp55xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev)
39{
40 return container_of(mc_cdev, struct lp55xx_led, mc_cdev);
41}
42
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090043static void lp55xx_reset_device(struct lp55xx_chip *chip)
44{
45 struct lp55xx_device_config *cfg = chip->cfg;
46 u8 addr = cfg->reset.addr;
47 u8 val = cfg->reset.val;
48
49 /* no error checking here because no ACK from the device after reset */
50 lp55xx_write(chip, addr, val);
51}
52
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +090053static int lp55xx_detect_device(struct lp55xx_chip *chip)
54{
55 struct lp55xx_device_config *cfg = chip->cfg;
56 u8 addr = cfg->enable.addr;
57 u8 val = cfg->enable.val;
58 int ret;
59
60 ret = lp55xx_write(chip, addr, val);
61 if (ret)
62 return ret;
63
64 usleep_range(1000, 2000);
65
66 ret = lp55xx_read(chip, addr, &val);
67 if (ret)
68 return ret;
69
70 if (val != cfg->enable.val)
71 return -ENODEV;
72
73 return 0;
74}
75
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +090076static int lp55xx_post_init_device(struct lp55xx_chip *chip)
77{
78 struct lp55xx_device_config *cfg = chip->cfg;
79
80 if (!cfg->post_init_device)
81 return 0;
82
83 return cfg->post_init_device(chip);
84}
85
Dan Murphy7e6f7f32020-07-13 10:45:29 -050086static ssize_t led_current_show(struct device *dev,
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +090087 struct device_attribute *attr,
88 char *buf)
89{
90 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
91
ye xingchen3f6fb1c2022-12-01 16:11:24 +080092 return sysfs_emit(buf, "%d\n", led->led_current);
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +090093}
94
Dan Murphy7e6f7f32020-07-13 10:45:29 -050095static ssize_t led_current_store(struct device *dev,
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +090096 struct device_attribute *attr,
97 const char *buf, size_t len)
98{
99 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
100 struct lp55xx_chip *chip = led->chip;
101 unsigned long curr;
102
103 if (kstrtoul(buf, 0, &curr))
104 return -EINVAL;
105
106 if (curr > led->max_current)
107 return -EINVAL;
108
109 if (!chip->cfg->set_led_current)
110 return len;
111
112 mutex_lock(&chip->lock);
113 chip->cfg->set_led_current(led, (u8)curr);
114 mutex_unlock(&chip->lock);
115
116 return len;
117}
118
Dan Murphy7e6f7f32020-07-13 10:45:29 -0500119static ssize_t max_current_show(struct device *dev,
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900120 struct device_attribute *attr,
121 char *buf)
122{
123 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
124
ye xingchen3f6fb1c2022-12-01 16:11:24 +0800125 return sysfs_emit(buf, "%d\n", led->max_current);
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900126}
127
Dan Murphy7e6f7f32020-07-13 10:45:29 -0500128static DEVICE_ATTR_RW(led_current);
129static DEVICE_ATTR_RO(max_current);
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900130
Johan Hovold44a12552014-06-25 10:08:56 -0700131static struct attribute *lp55xx_led_attrs[] = {
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900132 &dev_attr_led_current.attr,
133 &dev_attr_max_current.attr,
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900134 NULL,
135};
Johan Hovold44a12552014-06-25 10:08:56 -0700136ATTRIBUTE_GROUPS(lp55xx_led);
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900137
Dan Murphy92a81562020-07-16 13:20:01 -0500138static int lp55xx_set_mc_brightness(struct led_classdev *cdev,
139 enum led_brightness brightness)
140{
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;
144
145 led_mc_calc_color_components(&led->mc_cdev, brightness);
146 return cfg->multicolor_brightness_fn(led);
147
148}
149
Andrew Lunn95b2af62015-08-20 12:22:57 +0200150static int lp55xx_set_brightness(struct led_classdev *cdev,
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900151 enum led_brightness brightness)
152{
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900153 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
Andrew Lunn95b2af62015-08-20 12:22:57 +0200154 struct lp55xx_device_config *cfg = led->chip->cfg;
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900155
156 led->brightness = (u8)brightness;
Andrew Lunn95b2af62015-08-20 12:22:57 +0200157 return cfg->brightness_fn(led);
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900158}
159
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900160static int lp55xx_init_led(struct lp55xx_led *led,
161 struct lp55xx_chip *chip, int chan)
162{
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900163 struct lp55xx_platform_data *pdata = chip->pdata;
164 struct lp55xx_device_config *cfg = chip->cfg;
165 struct device *dev = &chip->cl->dev;
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900166 int max_channel = cfg->max_channel;
Dan Murphy92a81562020-07-16 13:20:01 -0500167 struct mc_subled *mc_led_info;
168 struct led_classdev *led_cdev;
169 char name[32];
Colin Ian Kingfb0f4052022-10-20 22:07:31 +0100170 int i;
Dan Murphy92a81562020-07-16 13:20:01 -0500171 int ret;
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900172
173 if (chan >= max_channel) {
174 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
175 return -EINVAL;
176 }
177
178 if (pdata->led_config[chan].led_current == 0)
179 return 0;
180
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900181 if (pdata->led_config[chan].name) {
182 led->cdev.name = pdata->led_config[chan].name;
183 } else {
184 snprintf(name, sizeof(name), "%s:channel%d",
185 pdata->label ? : chip->cl->name, chan);
186 led->cdev.name = name;
187 }
188
Dan Murphy92a81562020-07-16 13:20:01 -0500189 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);
193 if (!mc_led_info)
194 return -ENOMEM;
195
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];
Dan Murphy92a81562020-07-16 13:20:01 -0500205 }
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
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900229 if (ret) {
230 dev_err(dev, "led register err: %d\n", ret);
231 return ret;
232 }
233
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900234 return 0;
235}
236
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900237static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
238{
239 struct lp55xx_chip *chip = context;
240 struct device *dev = &chip->cl->dev;
Milo Kim93ad8a12013-11-20 22:14:34 -0800241 enum lp55xx_engine_index idx = chip->engine_idx;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900242
243 if (!fw) {
244 dev_err(dev, "firmware request failed\n");
Michal Kazior5ddb0862019-02-11 10:29:27 +0100245 return;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900246 }
247
248 /* handling firmware data is chip dependent */
249 mutex_lock(&chip->lock);
250
Milo Kim93ad8a12013-11-20 22:14:34 -0800251 chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900252 chip->fw = fw;
253 if (chip->cfg->firmware_cb)
254 chip->cfg->firmware_cb(chip);
255
256 mutex_unlock(&chip->lock);
257
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900258 /* firmware should be released for other channel use */
259 release_firmware(chip->fw);
Michal Kazior5ddb0862019-02-11 10:29:27 +0100260 chip->fw = NULL;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900261}
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
Milo Kimb6789322015-06-28 17:39:14 -0700268 return request_firmware_nowait(THIS_MODULE, false, name, dev,
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900269 GFP_KERNEL, chip, lp55xx_firmware_loaded);
270}
271
Dan Murphy7e6f7f32020-07-13 10:45:29 -0500272static ssize_t select_engine_show(struct device *dev,
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900273 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
Dan Murphy7e6f7f32020-07-13 10:45:29 -0500282static ssize_t select_engine_store(struct device *dev,
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900283 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
Dan Murphy7e6f7f32020-07-13 10:45:29 -0500324static ssize_t run_engine_store(struct device *dev,
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900325 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
Dan Murphy7e6f7f32020-07-13 10:45:29 -0500349static DEVICE_ATTR_RW(select_engine);
350static DEVICE_ATTR_WO(run_engine);
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900351
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900352static struct attribute *lp55xx_engine_attributes[] = {
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900353 &dev_attr_select_engine.attr,
354 &dev_attr_run_engine.attr,
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900355 NULL,
356};
357
358static const struct attribute_group lp55xx_engine_attr_group = {
359 .attrs = lp55xx_engine_attributes,
360};
361
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900362int 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
Kim, Milo53b41922013-03-20 17:37:00 -0700397bool 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
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900426int lp55xx_init_device(struct lp55xx_chip *chip)
427{
428 struct lp55xx_platform_data *pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900429 struct lp55xx_device_config *cfg;
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900430 struct device *dev = &chip->cl->dev;
431 int ret = 0;
432
433 WARN_ON(!chip);
434
435 pdata = chip->pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900436 cfg = chip->cfg;
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900437
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900438 if (!pdata || !cfg)
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900439 return -EINVAL;
440
Linus Walleijac219bf2020-06-27 00:40:11 +0200441 if (pdata->enable_gpiod) {
Merlijn Wajer9e87a8d2021-12-12 23:40:07 +0100442 gpiod_direction_output(pdata->enable_gpiod, 0);
443
Linus Walleijac219bf2020-06-27 00:40:11 +0200444 gpiod_set_consumer_name(pdata->enable_gpiod, "LP55xx enable");
Stefan Eichenberger7c977012023-09-18 16:32:38 +0200445 gpiod_set_value_cansleep(pdata->enable_gpiod, 0);
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900446 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
Stefan Eichenberger7c977012023-09-18 16:32:38 +0200447 gpiod_set_value_cansleep(pdata->enable_gpiod, 1);
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900448 usleep_range(1000, 2000); /* 500us abs min. */
449 }
450
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900451 lp55xx_reset_device(chip);
452
453 /*
454 * Exact value is not available. 10 - 20ms
455 * appears to be enough for reset.
456 */
457 usleep_range(10000, 20000);
458
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +0900459 ret = lp55xx_detect_device(chip);
460 if (ret) {
461 dev_err(dev, "device detection err: %d\n", ret);
462 goto err;
463 }
464
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900465 /* chip specific initialization */
466 ret = lp55xx_post_init_device(chip);
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900467 if (ret) {
468 dev_err(dev, "post init device err: %d\n", ret);
469 goto err_post_init;
470 }
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900471
472 return 0;
473
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900474err_post_init:
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900475 lp55xx_deinit_device(chip);
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900476err:
477 return ret;
478}
479EXPORT_SYMBOL_GPL(lp55xx_init_device);
480
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900481void lp55xx_deinit_device(struct lp55xx_chip *chip)
482{
483 struct lp55xx_platform_data *pdata = chip->pdata;
484
Kim, Milo53b41922013-03-20 17:37:00 -0700485 if (chip->clk)
486 clk_disable_unprepare(chip->clk);
487
Linus Walleijac219bf2020-06-27 00:40:11 +0200488 if (pdata->enable_gpiod)
489 gpiod_set_value(pdata->enable_gpiod, 0);
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900490}
491EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
492
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900493int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
494{
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;
499 u8 led_current;
500 int ret;
501 int i;
502
Andrew Lunn95b2af62015-08-20 12:22:57 +0200503 if (!cfg->brightness_fn) {
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900504 dev_err(&chip->cl->dev, "empty brightness configuration\n");
505 return -EINVAL;
506 }
507
508 for (i = 0; i < num_channels; i++) {
509
510 /* do not initialize channels that are not connected */
511 if (pdata->led_config[i].led_current == 0)
512 continue;
513
514 led_current = pdata->led_config[i].led_current;
515 each = led + i;
516 ret = lp55xx_init_led(each, chip, i);
517 if (ret)
518 goto err_init_led;
519
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900520 chip->num_leds++;
521 each->chip = chip;
522
523 /* setting led current at each channel */
524 if (cfg->set_led_current)
525 cfg->set_led_current(each, led_current);
526 }
527
528 return 0;
529
530err_init_led:
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900531 return ret;
532}
533EXPORT_SYMBOL_GPL(lp55xx_register_leds);
534
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900535int lp55xx_register_sysfs(struct lp55xx_chip *chip)
536{
537 struct device *dev = &chip->cl->dev;
Milo(Woogyom) Kim240085e2013-02-05 19:20:01 +0900538 struct lp55xx_device_config *cfg = chip->cfg;
539 int ret;
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900540
Milo(Woogyom) Kim240085e2013-02-05 19:20:01 +0900541 if (!cfg->run_engine || !cfg->firmware_cb)
542 goto dev_specific_attrs;
543
544 ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
545 if (ret)
546 return ret;
547
548dev_specific_attrs:
549 return cfg->dev_attr_group ?
550 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900551}
552EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
553
Milo(Woogyom) Kimba6fa842013-02-05 19:23:04 +0900554void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
555{
556 struct device *dev = &chip->cl->dev;
557 struct lp55xx_device_config *cfg = chip->cfg;
558
559 if (cfg->dev_attr_group)
560 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
561
562 sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
563}
564EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
565
Dan Murphy92a81562020-07-16 13:20:01 -0500566static int lp55xx_parse_common_child(struct device_node *np,
567 struct lp55xx_led_config *cfg,
568 int led_number, int *chan_nr)
569{
570 int ret;
571
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);
578
579 ret = of_property_read_u32(np, "reg", chan_nr);
580 if (ret)
581 return ret;
582
583 if (*chan_nr < 0 || *chan_nr > cfg->max_channel)
584 return -EINVAL;
585
586 return 0;
587}
588
589static int lp55xx_parse_multi_led_child(struct device_node *child,
590 struct lp55xx_led_config *cfg,
591 int child_number, int color_number)
592{
593 int chan_nr, color_id, ret;
594
595 ret = lp55xx_parse_common_child(child, cfg, child_number, &chan_nr);
596 if (ret)
597 return ret;
598
599 ret = of_property_read_u32(child, "color", &color_id);
600 if (ret)
601 return ret;
602
603 cfg[child_number].color_id[color_number] = color_id;
604 cfg[child_number].output_num[color_number] = chan_nr;
605
606 return 0;
607}
608
609static int lp55xx_parse_multi_led(struct device_node *np,
610 struct lp55xx_led_config *cfg,
611 int child_number)
612{
613 struct device_node *child;
614 int num_colors = 0, ret;
615
Marek Behún99a013c2020-09-18 00:32:56 +0200616 for_each_available_child_of_node(np, child) {
Dan Murphy92a81562020-07-16 13:20:01 -0500617 ret = lp55xx_parse_multi_led_child(child, cfg, child_number,
618 num_colors);
Marek Behún2c677562020-09-18 00:32:57 +0200619 if (ret) {
620 of_node_put(child);
Dan Murphy92a81562020-07-16 13:20:01 -0500621 return ret;
Marek Behún2c677562020-09-18 00:32:57 +0200622 }
Dan Murphy92a81562020-07-16 13:20:01 -0500623 num_colors++;
624 }
625
626 cfg[child_number].num_colors = num_colors;
627
628 return 0;
629}
630
631static int lp55xx_parse_logical_led(struct device_node *np,
632 struct lp55xx_led_config *cfg,
633 int child_number)
634{
635 int led_color, ret;
636 int chan_nr = 0;
637
638 cfg[child_number].default_trigger =
639 of_get_property(np, "linux,default-trigger", NULL);
640
641 ret = of_property_read_u32(np, "color", &led_color);
642 if (ret)
643 return ret;
644
Pavel Machek54212f52020-08-03 13:20:06 +0200645 if (led_color == LED_COLOR_ID_RGB)
Dan Murphy92a81562020-07-16 13:20:01 -0500646 return lp55xx_parse_multi_led(np, cfg, child_number);
647
648 ret = lp55xx_parse_common_child(np, cfg, child_number, &chan_nr);
649 if (ret < 0)
650 return ret;
651
652 cfg[child_number].chan_nr = chan_nr;
653
654 return ret;
655}
656
Milo Kimed1333522015-08-24 16:09:55 +0900657struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
Dan Murphy92a81562020-07-16 13:20:01 -0500658 struct device_node *np,
659 struct lp55xx_chip *chip)
Linus Walleij7542a04b2013-04-23 04:52:59 -0700660{
Kim, Milo2dac9122013-05-07 00:14:48 -0700661 struct device_node *child;
Linus Walleij7542a04b2013-04-23 04:52:59 -0700662 struct lp55xx_platform_data *pdata;
Kim, Milo2dac9122013-05-07 00:14:48 -0700663 struct lp55xx_led_config *cfg;
664 int num_channels;
665 int i = 0;
Dan Murphy92a81562020-07-16 13:20:01 -0500666 int ret;
Linus Walleij7542a04b2013-04-23 04:52:59 -0700667
668 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
669 if (!pdata)
Milo Kimed1333522015-08-24 16:09:55 +0900670 return ERR_PTR(-ENOMEM);
Linus Walleij7542a04b2013-04-23 04:52:59 -0700671
Marek Behún99a013c2020-09-18 00:32:56 +0200672 num_channels = of_get_available_child_count(np);
Kim, Milo2dac9122013-05-07 00:14:48 -0700673 if (num_channels == 0) {
674 dev_err(dev, "no LED channels\n");
Milo Kimed1333522015-08-24 16:09:55 +0900675 return ERR_PTR(-EINVAL);
Kim, Milo2dac9122013-05-07 00:14:48 -0700676 }
Linus Walleij7542a04b2013-04-23 04:52:59 -0700677
Kees Cooka86854d2018-06-12 14:07:58 -0700678 cfg = devm_kcalloc(dev, num_channels, sizeof(*cfg), GFP_KERNEL);
Kim, Milo2dac9122013-05-07 00:14:48 -0700679 if (!cfg)
Milo Kimed1333522015-08-24 16:09:55 +0900680 return ERR_PTR(-ENOMEM);
Linus Walleij7542a04b2013-04-23 04:52:59 -0700681
Kim, Milo2dac9122013-05-07 00:14:48 -0700682 pdata->led_config = &cfg[0];
683 pdata->num_channels = num_channels;
Dan Murphy92a81562020-07-16 13:20:01 -0500684 cfg->max_channel = chip->cfg->max_channel;
Kim, Milo2dac9122013-05-07 00:14:48 -0700685
Marek Behún99a013c2020-09-18 00:32:56 +0200686 for_each_available_child_of_node(np, child) {
Dan Murphy92a81562020-07-16 13:20:01 -0500687 ret = lp55xx_parse_logical_led(child, cfg, i);
Marek Behún2c677562020-09-18 00:32:57 +0200688 if (ret) {
689 of_node_put(child);
Dan Murphy92a81562020-07-16 13:20:01 -0500690 return ERR_PTR(-EINVAL);
Marek Behún2c677562020-09-18 00:32:57 +0200691 }
Kim, Milo2dac9122013-05-07 00:14:48 -0700692 i++;
Linus Walleij7542a04b2013-04-23 04:52:59 -0700693 }
Kim, Milo2dac9122013-05-07 00:14:48 -0700694
Maarten Zanders54a7bef2023-04-21 09:53:05 +0200695 if (of_property_read_u32(np, "ti,charge-pump-mode", &pdata->charge_pump_mode))
696 pdata->charge_pump_mode = LP55XX_CP_AUTO;
697
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);
701 }
702
Kim, Milo2dac9122013-05-07 00:14:48 -0700703 of_property_read_string(np, "label", &pdata->label);
704 of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
705
Linus Walleijac219bf2020-06-27 00:40:11 +0200706 pdata->enable_gpiod = devm_gpiod_get_optional(dev, "enable",
Merlijn Wajer9e87a8d2021-12-12 23:40:07 +0100707 GPIOD_ASIS);
Linus Walleijac219bf2020-06-27 00:40:11 +0200708 if (IS_ERR(pdata->enable_gpiod))
709 return ERR_CAST(pdata->enable_gpiod);
Sebastian Reichel30dae2f2013-10-22 11:02:56 -0700710
Kim, Milo33b3a562013-07-09 02:11:37 -0700711 /* LP8501 specific */
712 of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
713
Milo Kimed1333522015-08-24 16:09:55 +0900714 return pdata;
Linus Walleij7542a04b2013-04-23 04:52:59 -0700715}
716EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
717
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900718MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
719MODULE_DESCRIPTION("LP55xx Common Driver");
720MODULE_LICENSE("GPL");