blob: 44ced02b49f921d36bf3da9c843279d2ac44abe3 [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>
Sebastian Reichel30dae2f2013-10-22 11:02:56 -070020#include <linux/gpio.h>
21#include <linux/of_gpio.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
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090038static void lp55xx_reset_device(struct lp55xx_chip *chip)
39{
40 struct lp55xx_device_config *cfg = chip->cfg;
41 u8 addr = cfg->reset.addr;
42 u8 val = cfg->reset.val;
43
44 /* no error checking here because no ACK from the device after reset */
45 lp55xx_write(chip, addr, val);
46}
47
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +090048static int lp55xx_detect_device(struct lp55xx_chip *chip)
49{
50 struct lp55xx_device_config *cfg = chip->cfg;
51 u8 addr = cfg->enable.addr;
52 u8 val = cfg->enable.val;
53 int ret;
54
55 ret = lp55xx_write(chip, addr, val);
56 if (ret)
57 return ret;
58
59 usleep_range(1000, 2000);
60
61 ret = lp55xx_read(chip, addr, &val);
62 if (ret)
63 return ret;
64
65 if (val != cfg->enable.val)
66 return -ENODEV;
67
68 return 0;
69}
70
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +090071static int lp55xx_post_init_device(struct lp55xx_chip *chip)
72{
73 struct lp55xx_device_config *cfg = chip->cfg;
74
75 if (!cfg->post_init_device)
76 return 0;
77
78 return cfg->post_init_device(chip);
79}
80
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +090081static ssize_t lp55xx_show_current(struct device *dev,
82 struct device_attribute *attr,
83 char *buf)
84{
85 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
86
Kim, Milo24d32122013-03-14 17:19:36 -070087 return scnprintf(buf, PAGE_SIZE, "%d\n", led->led_current);
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +090088}
89
90static ssize_t lp55xx_store_current(struct device *dev,
91 struct device_attribute *attr,
92 const char *buf, size_t len)
93{
94 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
95 struct lp55xx_chip *chip = led->chip;
96 unsigned long curr;
97
98 if (kstrtoul(buf, 0, &curr))
99 return -EINVAL;
100
101 if (curr > led->max_current)
102 return -EINVAL;
103
104 if (!chip->cfg->set_led_current)
105 return len;
106
107 mutex_lock(&chip->lock);
108 chip->cfg->set_led_current(led, (u8)curr);
109 mutex_unlock(&chip->lock);
110
111 return len;
112}
113
114static ssize_t lp55xx_show_max_current(struct device *dev,
115 struct device_attribute *attr,
116 char *buf)
117{
118 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
119
Kim, Milo24d32122013-03-14 17:19:36 -0700120 return scnprintf(buf, PAGE_SIZE, "%d\n", led->max_current);
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900121}
122
123static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
124 lp55xx_store_current);
125static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
126
Johan Hovold44a12552014-06-25 10:08:56 -0700127static struct attribute *lp55xx_led_attrs[] = {
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900128 &dev_attr_led_current.attr,
129 &dev_attr_max_current.attr,
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900130 NULL,
131};
Johan Hovold44a12552014-06-25 10:08:56 -0700132ATTRIBUTE_GROUPS(lp55xx_led);
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900133
Andrew Lunn95b2af62015-08-20 12:22:57 +0200134static int lp55xx_set_brightness(struct led_classdev *cdev,
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900135 enum led_brightness brightness)
136{
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900137 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
Andrew Lunn95b2af62015-08-20 12:22:57 +0200138 struct lp55xx_device_config *cfg = led->chip->cfg;
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900139
140 led->brightness = (u8)brightness;
Andrew Lunn95b2af62015-08-20 12:22:57 +0200141 return cfg->brightness_fn(led);
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900142}
143
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900144static int lp55xx_init_led(struct lp55xx_led *led,
145 struct lp55xx_chip *chip, int chan)
146{
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900147 struct lp55xx_platform_data *pdata = chip->pdata;
148 struct lp55xx_device_config *cfg = chip->cfg;
149 struct device *dev = &chip->cl->dev;
150 char name[32];
151 int ret;
152 int max_channel = cfg->max_channel;
153
154 if (chan >= max_channel) {
155 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
156 return -EINVAL;
157 }
158
159 if (pdata->led_config[chan].led_current == 0)
160 return 0;
161
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;
Linus Walleijf65f0a12013-09-15 03:50:17 -0700165 led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900166
167 if (led->chan_nr >= max_channel) {
168 dev_err(dev, "Use channel numbers between 0 and %d\n",
169 max_channel - 1);
170 return -EINVAL;
171 }
172
Andrew Lunn95b2af62015-08-20 12:22:57 +0200173 led->cdev.brightness_set_blocking = lp55xx_set_brightness;
Johan Hovold44a12552014-06-25 10:08:56 -0700174 led->cdev.groups = lp55xx_led_groups;
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900175
176 if (pdata->led_config[chan].name) {
177 led->cdev.name = pdata->led_config[chan].name;
178 } else {
179 snprintf(name, sizeof(name), "%s:channel%d",
180 pdata->label ? : chip->cl->name, chan);
181 led->cdev.name = name;
182 }
183
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900184 ret = led_classdev_register(dev, &led->cdev);
185 if (ret) {
186 dev_err(dev, "led register err: %d\n", ret);
187 return ret;
188 }
189
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900190 return 0;
191}
192
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900193static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
194{
195 struct lp55xx_chip *chip = context;
196 struct device *dev = &chip->cl->dev;
Milo Kim93ad8a12013-11-20 22:14:34 -0800197 enum lp55xx_engine_index idx = chip->engine_idx;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900198
199 if (!fw) {
200 dev_err(dev, "firmware request failed\n");
Michal Kazior5ddb0862019-02-11 10:29:27 +0100201 return;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900202 }
203
204 /* handling firmware data is chip dependent */
205 mutex_lock(&chip->lock);
206
Milo Kim93ad8a12013-11-20 22:14:34 -0800207 chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900208 chip->fw = fw;
209 if (chip->cfg->firmware_cb)
210 chip->cfg->firmware_cb(chip);
211
212 mutex_unlock(&chip->lock);
213
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900214 /* firmware should be released for other channel use */
215 release_firmware(chip->fw);
Michal Kazior5ddb0862019-02-11 10:29:27 +0100216 chip->fw = NULL;
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900217}
218
219static int lp55xx_request_firmware(struct lp55xx_chip *chip)
220{
221 const char *name = chip->cl->name;
222 struct device *dev = &chip->cl->dev;
223
Milo Kimb6789322015-06-28 17:39:14 -0700224 return request_firmware_nowait(THIS_MODULE, false, name, dev,
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900225 GFP_KERNEL, chip, lp55xx_firmware_loaded);
226}
227
228static ssize_t lp55xx_show_engine_select(struct device *dev,
229 struct device_attribute *attr,
230 char *buf)
231{
232 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
233 struct lp55xx_chip *chip = led->chip;
234
235 return sprintf(buf, "%d\n", chip->engine_idx);
236}
237
238static ssize_t lp55xx_store_engine_select(struct device *dev,
239 struct device_attribute *attr,
240 const char *buf, size_t len)
241{
242 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
243 struct lp55xx_chip *chip = led->chip;
244 unsigned long val;
245 int ret;
246
247 if (kstrtoul(buf, 0, &val))
248 return -EINVAL;
249
250 /* select the engine to be run */
251
252 switch (val) {
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);
260 break;
261 default:
262 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
263 return -EINVAL;
264 }
265
266 if (ret) {
267 dev_err(dev, "request firmware err: %d\n", ret);
268 return ret;
269 }
270
271 return len;
272}
273
274static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
275{
276 if (chip->cfg->run_engine)
277 chip->cfg->run_engine(chip, start);
278}
279
280static ssize_t lp55xx_store_engine_run(struct device *dev,
281 struct device_attribute *attr,
282 const char *buf, size_t len)
283{
284 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
285 struct lp55xx_chip *chip = led->chip;
286 unsigned long val;
287
288 if (kstrtoul(buf, 0, &val))
289 return -EINVAL;
290
291 /* run or stop the selected engine */
292
293 if (val <= 0) {
294 lp55xx_run_engine(chip, false);
295 return len;
296 }
297
298 mutex_lock(&chip->lock);
299 lp55xx_run_engine(chip, true);
300 mutex_unlock(&chip->lock);
301
302 return len;
303}
304
305static DEVICE_ATTR(select_engine, S_IRUGO | S_IWUSR,
306 lp55xx_show_engine_select, lp55xx_store_engine_select);
307static DEVICE_ATTR(run_engine, S_IWUSR, NULL, lp55xx_store_engine_run);
308
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900309static struct attribute *lp55xx_engine_attributes[] = {
Milo(Woogyom) Kim10c06d12013-02-05 19:17:20 +0900310 &dev_attr_select_engine.attr,
311 &dev_attr_run_engine.attr,
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900312 NULL,
313};
314
315static const struct attribute_group lp55xx_engine_attr_group = {
316 .attrs = lp55xx_engine_attributes,
317};
318
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900319int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
320{
321 return i2c_smbus_write_byte_data(chip->cl, reg, val);
322}
323EXPORT_SYMBOL_GPL(lp55xx_write);
324
325int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
326{
327 s32 ret;
328
329 ret = i2c_smbus_read_byte_data(chip->cl, reg);
330 if (ret < 0)
331 return ret;
332
333 *val = ret;
334 return 0;
335}
336EXPORT_SYMBOL_GPL(lp55xx_read);
337
338int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
339{
340 int ret;
341 u8 tmp;
342
343 ret = lp55xx_read(chip, reg, &tmp);
344 if (ret)
345 return ret;
346
347 tmp &= ~mask;
348 tmp |= val & mask;
349
350 return lp55xx_write(chip, reg, tmp);
351}
352EXPORT_SYMBOL_GPL(lp55xx_update_bits);
353
Kim, Milo53b41922013-03-20 17:37:00 -0700354bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
355{
356 struct clk *clk;
357 int err;
358
359 clk = devm_clk_get(&chip->cl->dev, "32k_clk");
360 if (IS_ERR(clk))
361 goto use_internal_clk;
362
363 err = clk_prepare_enable(clk);
364 if (err)
365 goto use_internal_clk;
366
367 if (clk_get_rate(clk) != LP55XX_CLK_32K) {
368 clk_disable_unprepare(clk);
369 goto use_internal_clk;
370 }
371
372 dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K);
373
374 chip->clk = clk;
375 return true;
376
377use_internal_clk:
378 dev_info(&chip->cl->dev, "internal clock used\n");
379 return false;
380}
381EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
382
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900383int lp55xx_init_device(struct lp55xx_chip *chip)
384{
385 struct lp55xx_platform_data *pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900386 struct lp55xx_device_config *cfg;
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900387 struct device *dev = &chip->cl->dev;
388 int ret = 0;
389
390 WARN_ON(!chip);
391
392 pdata = chip->pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900393 cfg = chip->cfg;
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900394
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900395 if (!pdata || !cfg)
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900396 return -EINVAL;
397
Sebastian Reichel30dae2f2013-10-22 11:02:56 -0700398 if (gpio_is_valid(pdata->enable_gpio)) {
399 ret = devm_gpio_request_one(dev, pdata->enable_gpio,
400 GPIOF_DIR_OUT, "lp5523_enable");
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900401 if (ret < 0) {
Sebastian Reichel30dae2f2013-10-22 11:02:56 -0700402 dev_err(dev, "could not acquire enable gpio (err=%d)\n",
403 ret);
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900404 goto err;
405 }
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900406
Sebastian Reichel30dae2f2013-10-22 11:02:56 -0700407 gpio_set_value(pdata->enable_gpio, 0);
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900408 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
Sebastian Reichel30dae2f2013-10-22 11:02:56 -0700409 gpio_set_value(pdata->enable_gpio, 1);
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900410 usleep_range(1000, 2000); /* 500us abs min. */
411 }
412
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900413 lp55xx_reset_device(chip);
414
415 /*
416 * Exact value is not available. 10 - 20ms
417 * appears to be enough for reset.
418 */
419 usleep_range(10000, 20000);
420
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +0900421 ret = lp55xx_detect_device(chip);
422 if (ret) {
423 dev_err(dev, "device detection err: %d\n", ret);
424 goto err;
425 }
426
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900427 /* chip specific initialization */
428 ret = lp55xx_post_init_device(chip);
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900429 if (ret) {
430 dev_err(dev, "post init device err: %d\n", ret);
431 goto err_post_init;
432 }
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900433
434 return 0;
435
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900436err_post_init:
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900437 lp55xx_deinit_device(chip);
Milo(Woogyom) Kima85908d2013-02-05 18:07:20 +0900438err:
439 return ret;
440}
441EXPORT_SYMBOL_GPL(lp55xx_init_device);
442
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900443void lp55xx_deinit_device(struct lp55xx_chip *chip)
444{
445 struct lp55xx_platform_data *pdata = chip->pdata;
446
Kim, Milo53b41922013-03-20 17:37:00 -0700447 if (chip->clk)
448 clk_disable_unprepare(chip->clk);
449
Sebastian Reichel30dae2f2013-10-22 11:02:56 -0700450 if (gpio_is_valid(pdata->enable_gpio))
451 gpio_set_value(pdata->enable_gpio, 0);
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900452}
453EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
454
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900455int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
456{
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;
461 u8 led_current;
462 int ret;
463 int i;
464
Andrew Lunn95b2af62015-08-20 12:22:57 +0200465 if (!cfg->brightness_fn) {
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900466 dev_err(&chip->cl->dev, "empty brightness configuration\n");
467 return -EINVAL;
468 }
469
470 for (i = 0; i < num_channels; i++) {
471
472 /* do not initialize channels that are not connected */
473 if (pdata->led_config[i].led_current == 0)
474 continue;
475
476 led_current = pdata->led_config[i].led_current;
477 each = led + i;
478 ret = lp55xx_init_led(each, chip, i);
479 if (ret)
480 goto err_init_led;
481
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900482 chip->num_leds++;
483 each->chip = chip;
484
485 /* setting led current at each channel */
486 if (cfg->set_led_current)
487 cfg->set_led_current(each, led_current);
488 }
489
490 return 0;
491
492err_init_led:
Milo(Woogyom) Kimd9067d22013-02-05 19:12:47 +0900493 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900494 return ret;
495}
496EXPORT_SYMBOL_GPL(lp55xx_register_leds);
497
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900498void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
499{
500 int i;
501 struct lp55xx_led *each;
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900502
503 for (i = 0; i < chip->num_leds; i++) {
504 each = led + i;
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900505 led_classdev_unregister(&each->cdev);
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900506 }
507}
508EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
509
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900510int lp55xx_register_sysfs(struct lp55xx_chip *chip)
511{
512 struct device *dev = &chip->cl->dev;
Milo(Woogyom) Kim240085e2013-02-05 19:20:01 +0900513 struct lp55xx_device_config *cfg = chip->cfg;
514 int ret;
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900515
Milo(Woogyom) Kim240085e2013-02-05 19:20:01 +0900516 if (!cfg->run_engine || !cfg->firmware_cb)
517 goto dev_specific_attrs;
518
519 ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
520 if (ret)
521 return ret;
522
523dev_specific_attrs:
524 return cfg->dev_attr_group ?
525 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
Milo(Woogyom) Kimb3b6f812013-02-05 19:15:27 +0900526}
527EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
528
Milo(Woogyom) Kimba6fa842013-02-05 19:23:04 +0900529void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
530{
531 struct device *dev = &chip->cl->dev;
532 struct lp55xx_device_config *cfg = chip->cfg;
533
534 if (cfg->dev_attr_group)
535 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
536
537 sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
538}
539EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
540
Milo Kimed1333522015-08-24 16:09:55 +0900541struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
542 struct device_node *np)
Linus Walleij7542a04b2013-04-23 04:52:59 -0700543{
Kim, Milo2dac9122013-05-07 00:14:48 -0700544 struct device_node *child;
Linus Walleij7542a04b2013-04-23 04:52:59 -0700545 struct lp55xx_platform_data *pdata;
Kim, Milo2dac9122013-05-07 00:14:48 -0700546 struct lp55xx_led_config *cfg;
547 int num_channels;
548 int i = 0;
Linus Walleij7542a04b2013-04-23 04:52:59 -0700549
550 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
551 if (!pdata)
Milo Kimed1333522015-08-24 16:09:55 +0900552 return ERR_PTR(-ENOMEM);
Linus Walleij7542a04b2013-04-23 04:52:59 -0700553
Kim, Milo2dac9122013-05-07 00:14:48 -0700554 num_channels = of_get_child_count(np);
555 if (num_channels == 0) {
556 dev_err(dev, "no LED channels\n");
Milo Kimed1333522015-08-24 16:09:55 +0900557 return ERR_PTR(-EINVAL);
Kim, Milo2dac9122013-05-07 00:14:48 -0700558 }
Linus Walleij7542a04b2013-04-23 04:52:59 -0700559
Kees Cooka86854d2018-06-12 14:07:58 -0700560 cfg = devm_kcalloc(dev, num_channels, sizeof(*cfg), GFP_KERNEL);
Kim, Milo2dac9122013-05-07 00:14:48 -0700561 if (!cfg)
Milo Kimed1333522015-08-24 16:09:55 +0900562 return ERR_PTR(-ENOMEM);
Linus Walleij7542a04b2013-04-23 04:52:59 -0700563
Kim, Milo2dac9122013-05-07 00:14:48 -0700564 pdata->led_config = &cfg[0];
565 pdata->num_channels = num_channels;
566
567 for_each_child_of_node(np, child) {
568 cfg[i].chan_nr = i;
569
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);
Linus Walleijf65f0a12013-09-15 03:50:17 -0700573 cfg[i].default_trigger =
574 of_get_property(child, "linux,default-trigger", NULL);
Kim, Milo2dac9122013-05-07 00:14:48 -0700575
576 i++;
Linus Walleij7542a04b2013-04-23 04:52:59 -0700577 }
Kim, Milo2dac9122013-05-07 00:14:48 -0700578
579 of_property_read_string(np, "label", &pdata->label);
580 of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
581
Sebastian Reichel30dae2f2013-10-22 11:02:56 -0700582 pdata->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0);
583
Kim, Milo33b3a562013-07-09 02:11:37 -0700584 /* LP8501 specific */
585 of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
586
Milo Kimed1333522015-08-24 16:09:55 +0900587 return pdata;
Linus Walleij7542a04b2013-04-23 04:52:59 -0700588}
589EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
590
Milo(Woogyom) Kimc93d08f2013-02-05 18:01:23 +0900591MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
592MODULE_DESCRIPTION("LP55xx Common Driver");
593MODULE_LICENSE("GPL");