Skip to content

Commit 082a4d3

Browse files
Ansuellag-linaro
authored andcommitted
leds: leds-lp55xx: Generalize sysfs engine_load and engine_mode
Generalize sysfs engine_load and engine_mode since their implementation is the same acrosso some lp55xx based LED driver. Suggested-by: Lee Jones <[email protected]> Signed-off-by: Christian Marangi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent 43e91e5 commit 082a4d3

File tree

4 files changed

+117
-186
lines changed

4 files changed

+117
-186
lines changed

drivers/leds/leds-lp5521.c

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -201,82 +201,6 @@ static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
201201
return 0;
202202
}
203203

204-
static ssize_t show_engine_mode(struct device *dev,
205-
struct device_attribute *attr,
206-
char *buf, int nr)
207-
{
208-
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
209-
struct lp55xx_chip *chip = led->chip;
210-
enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
211-
212-
switch (mode) {
213-
case LP55XX_ENGINE_RUN:
214-
return sprintf(buf, "run\n");
215-
case LP55XX_ENGINE_LOAD:
216-
return sprintf(buf, "load\n");
217-
case LP55XX_ENGINE_DISABLED:
218-
default:
219-
return sprintf(buf, "disabled\n");
220-
}
221-
}
222-
show_mode(1)
223-
show_mode(2)
224-
show_mode(3)
225-
226-
static ssize_t store_engine_mode(struct device *dev,
227-
struct device_attribute *attr,
228-
const char *buf, size_t len, int nr)
229-
{
230-
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
231-
struct lp55xx_chip *chip = led->chip;
232-
struct lp55xx_engine *engine = &chip->engines[nr - 1];
233-
234-
mutex_lock(&chip->lock);
235-
236-
chip->engine_idx = nr;
237-
238-
if (!strncmp(buf, "run", 3)) {
239-
lp5521_run_engine(chip, true);
240-
engine->mode = LP55XX_ENGINE_RUN;
241-
} else if (!strncmp(buf, "load", 4)) {
242-
lp55xx_stop_engine(chip);
243-
lp55xx_load_engine(chip);
244-
engine->mode = LP55XX_ENGINE_LOAD;
245-
} else if (!strncmp(buf, "disabled", 8)) {
246-
lp55xx_stop_engine(chip);
247-
engine->mode = LP55XX_ENGINE_DISABLED;
248-
}
249-
250-
mutex_unlock(&chip->lock);
251-
252-
return len;
253-
}
254-
store_mode(1)
255-
store_mode(2)
256-
store_mode(3)
257-
258-
static ssize_t store_engine_load(struct device *dev,
259-
struct device_attribute *attr,
260-
const char *buf, size_t len, int nr)
261-
{
262-
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
263-
struct lp55xx_chip *chip = led->chip;
264-
int ret;
265-
266-
mutex_lock(&chip->lock);
267-
268-
chip->engine_idx = nr;
269-
lp55xx_load_engine(chip);
270-
ret = lp55xx_update_program_memory(chip, buf, len);
271-
272-
mutex_unlock(&chip->lock);
273-
274-
return ret;
275-
}
276-
store_load(1)
277-
store_load(2)
278-
store_load(3)
279-
280204
static ssize_t lp5521_selftest(struct device *dev,
281205
struct device_attribute *attr,
282206
char *buf)
@@ -293,12 +217,12 @@ static ssize_t lp5521_selftest(struct device *dev,
293217
}
294218

295219
/* device attributes */
296-
static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
297-
static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
298-
static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
299-
static LP55XX_DEV_ATTR_WO(engine1_load, store_engine1_load);
300-
static LP55XX_DEV_ATTR_WO(engine2_load, store_engine2_load);
301-
static LP55XX_DEV_ATTR_WO(engine3_load, store_engine3_load);
220+
LP55XX_DEV_ATTR_ENGINE_MODE(1);
221+
LP55XX_DEV_ATTR_ENGINE_MODE(2);
222+
LP55XX_DEV_ATTR_ENGINE_MODE(3);
223+
LP55XX_DEV_ATTR_ENGINE_LOAD(1);
224+
LP55XX_DEV_ATTR_ENGINE_LOAD(2);
225+
LP55XX_DEV_ATTR_ENGINE_LOAD(3);
302226
static LP55XX_DEV_ATTR_RO(selftest, lp5521_selftest);
303227

304228
static struct attribute *lp5521_attributes[] = {

drivers/leds/leds-lp5523.c

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -225,60 +225,6 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
225225
return ret;
226226
}
227227

228-
static ssize_t show_engine_mode(struct device *dev,
229-
struct device_attribute *attr,
230-
char *buf, int nr)
231-
{
232-
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
233-
struct lp55xx_chip *chip = led->chip;
234-
enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
235-
236-
switch (mode) {
237-
case LP55XX_ENGINE_RUN:
238-
return sprintf(buf, "run\n");
239-
case LP55XX_ENGINE_LOAD:
240-
return sprintf(buf, "load\n");
241-
case LP55XX_ENGINE_DISABLED:
242-
default:
243-
return sprintf(buf, "disabled\n");
244-
}
245-
}
246-
show_mode(1)
247-
show_mode(2)
248-
show_mode(3)
249-
250-
static ssize_t store_engine_mode(struct device *dev,
251-
struct device_attribute *attr,
252-
const char *buf, size_t len, int nr)
253-
{
254-
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
255-
struct lp55xx_chip *chip = led->chip;
256-
struct lp55xx_engine *engine = &chip->engines[nr - 1];
257-
258-
mutex_lock(&chip->lock);
259-
260-
chip->engine_idx = nr;
261-
262-
if (!strncmp(buf, "run", 3)) {
263-
lp5523_run_engine(chip, true);
264-
engine->mode = LP55XX_ENGINE_RUN;
265-
} else if (!strncmp(buf, "load", 4)) {
266-
lp55xx_stop_engine(chip);
267-
lp55xx_load_engine(chip);
268-
engine->mode = LP55XX_ENGINE_LOAD;
269-
} else if (!strncmp(buf, "disabled", 8)) {
270-
lp55xx_stop_engine(chip);
271-
engine->mode = LP55XX_ENGINE_DISABLED;
272-
}
273-
274-
mutex_unlock(&chip->lock);
275-
276-
return len;
277-
}
278-
store_mode(1)
279-
store_mode(2)
280-
store_mode(3)
281-
282228
static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
283229
{
284230
u16 tmp_mux = 0;
@@ -392,28 +338,6 @@ store_leds(1)
392338
store_leds(2)
393339
store_leds(3)
394340

395-
static ssize_t store_engine_load(struct device *dev,
396-
struct device_attribute *attr,
397-
const char *buf, size_t len, int nr)
398-
{
399-
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
400-
struct lp55xx_chip *chip = led->chip;
401-
int ret;
402-
403-
mutex_lock(&chip->lock);
404-
405-
chip->engine_idx = nr;
406-
lp55xx_load_engine(chip);
407-
ret = lp55xx_update_program_memory(chip, buf, len);
408-
409-
mutex_unlock(&chip->lock);
410-
411-
return ret;
412-
}
413-
store_load(1)
414-
store_load(2)
415-
store_load(3)
416-
417341
static ssize_t lp5523_selftest(struct device *dev,
418342
struct device_attribute *attr,
419343
char *buf)
@@ -635,15 +559,15 @@ static ssize_t store_master_fader_leds(struct device *dev,
635559
return ret;
636560
}
637561

638-
static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
639-
static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
640-
static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
562+
LP55XX_DEV_ATTR_ENGINE_MODE(1);
563+
LP55XX_DEV_ATTR_ENGINE_MODE(2);
564+
LP55XX_DEV_ATTR_ENGINE_MODE(3);
641565
static LP55XX_DEV_ATTR_RW(engine1_leds, show_engine1_leds, store_engine1_leds);
642566
static LP55XX_DEV_ATTR_RW(engine2_leds, show_engine2_leds, store_engine2_leds);
643567
static LP55XX_DEV_ATTR_RW(engine3_leds, show_engine3_leds, store_engine3_leds);
644-
static LP55XX_DEV_ATTR_WO(engine1_load, store_engine1_load);
645-
static LP55XX_DEV_ATTR_WO(engine2_load, store_engine2_load);
646-
static LP55XX_DEV_ATTR_WO(engine3_load, store_engine3_load);
568+
LP55XX_DEV_ATTR_ENGINE_LOAD(1);
569+
LP55XX_DEV_ATTR_ENGINE_LOAD(2);
570+
LP55XX_DEV_ATTR_ENGINE_LOAD(3);
647571
static LP55XX_DEV_ATTR_RO(selftest, lp5523_selftest);
648572
static LP55XX_DEV_ATTR_RW(master_fader1, show_master_fader1,
649573
store_master_fader1);

drivers/leds/leds-lp55xx-common.c

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,8 @@ static int lp55xx_request_firmware(struct lp55xx_chip *chip)
541541
}
542542

543543
static ssize_t select_engine_show(struct device *dev,
544-
struct device_attribute *attr,
545-
char *buf)
544+
struct device_attribute *attr,
545+
char *buf)
546546
{
547547
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
548548
struct lp55xx_chip *chip = led->chip;
@@ -551,8 +551,8 @@ static ssize_t select_engine_show(struct device *dev,
551551
}
552552

553553
static ssize_t select_engine_store(struct device *dev,
554-
struct device_attribute *attr,
555-
const char *buf, size_t len)
554+
struct device_attribute *attr,
555+
const char *buf, size_t len)
556556
{
557557
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
558558
struct lp55xx_chip *chip = led->chip;
@@ -593,8 +593,8 @@ static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
593593
}
594594

595595
static ssize_t run_engine_store(struct device *dev,
596-
struct device_attribute *attr,
597-
const char *buf, size_t len)
596+
struct device_attribute *attr,
597+
const char *buf, size_t len)
598598
{
599599
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
600600
struct lp55xx_chip *chip = led->chip;
@@ -620,6 +620,77 @@ static ssize_t run_engine_store(struct device *dev,
620620
static DEVICE_ATTR_RW(select_engine);
621621
static DEVICE_ATTR_WO(run_engine);
622622

623+
ssize_t lp55xx_show_engine_mode(struct device *dev,
624+
struct device_attribute *attr,
625+
char *buf, int nr)
626+
{
627+
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
628+
struct lp55xx_chip *chip = led->chip;
629+
enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
630+
631+
switch (mode) {
632+
case LP55XX_ENGINE_RUN:
633+
return sysfs_emit(buf, "run\n");
634+
case LP55XX_ENGINE_LOAD:
635+
return sysfs_emit(buf, "load\n");
636+
case LP55XX_ENGINE_DISABLED:
637+
default:
638+
return sysfs_emit(buf, "disabled\n");
639+
}
640+
}
641+
EXPORT_SYMBOL_GPL(lp55xx_show_engine_mode);
642+
643+
ssize_t lp55xx_store_engine_mode(struct device *dev,
644+
struct device_attribute *attr,
645+
const char *buf, size_t len, int nr)
646+
{
647+
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
648+
struct lp55xx_chip *chip = led->chip;
649+
const struct lp55xx_device_config *cfg = chip->cfg;
650+
struct lp55xx_engine *engine = &chip->engines[nr - 1];
651+
652+
mutex_lock(&chip->lock);
653+
654+
chip->engine_idx = nr;
655+
656+
if (!strncmp(buf, "run", 3)) {
657+
cfg->run_engine(chip, true);
658+
engine->mode = LP55XX_ENGINE_RUN;
659+
} else if (!strncmp(buf, "load", 4)) {
660+
lp55xx_stop_engine(chip);
661+
lp55xx_load_engine(chip);
662+
engine->mode = LP55XX_ENGINE_LOAD;
663+
} else if (!strncmp(buf, "disabled", 8)) {
664+
lp55xx_stop_engine(chip);
665+
engine->mode = LP55XX_ENGINE_DISABLED;
666+
}
667+
668+
mutex_unlock(&chip->lock);
669+
670+
return len;
671+
}
672+
EXPORT_SYMBOL_GPL(lp55xx_store_engine_mode);
673+
674+
ssize_t lp55xx_store_engine_load(struct device *dev,
675+
struct device_attribute *attr,
676+
const char *buf, size_t len, int nr)
677+
{
678+
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
679+
struct lp55xx_chip *chip = led->chip;
680+
int ret;
681+
682+
mutex_lock(&chip->lock);
683+
684+
chip->engine_idx = nr;
685+
lp55xx_load_engine(chip);
686+
ret = lp55xx_update_program_memory(chip, buf, len);
687+
688+
mutex_unlock(&chip->lock);
689+
690+
return ret;
691+
}
692+
EXPORT_SYMBOL_GPL(lp55xx_store_engine_load);
693+
623694
static struct attribute *lp55xx_engine_attributes[] = {
624695
&dev_attr_select_engine.attr,
625696
&dev_attr_run_engine.attr,

drivers/leds/leds-lp55xx-common.h

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ enum lp55xx_engine_mode {
3535
#define LP55XX_DEV_ATTR_WO(name, store) \
3636
DEVICE_ATTR(name, S_IWUSR, NULL, store)
3737

38-
#define show_mode(nr) \
38+
#define LP55XX_DEV_ATTR_ENGINE_MODE(nr) \
3939
static ssize_t show_engine##nr##_mode(struct device *dev, \
40-
struct device_attribute *attr, \
41-
char *buf) \
40+
struct device_attribute *attr, \
41+
char *buf) \
4242
{ \
43-
return show_engine_mode(dev, attr, buf, nr); \
44-
}
45-
46-
#define store_mode(nr) \
43+
return lp55xx_show_engine_mode(dev, attr, buf, nr); \
44+
} \
4745
static ssize_t store_engine##nr##_mode(struct device *dev, \
48-
struct device_attribute *attr, \
49-
const char *buf, size_t len) \
46+
struct device_attribute *attr, \
47+
const char *buf, size_t len) \
5048
{ \
51-
return store_engine_mode(dev, attr, buf, len, nr); \
52-
}
49+
return lp55xx_store_engine_mode(dev, attr, buf, len, nr); \
50+
} \
51+
static LP55XX_DEV_ATTR_RW(engine##nr##_mode, show_engine##nr##_mode, \
52+
store_engine##nr##_mode)
5353

5454
#define show_leds(nr) \
5555
static ssize_t show_engine##nr##_leds(struct device *dev, \
@@ -67,13 +67,14 @@ static ssize_t store_engine##nr##_leds(struct device *dev, \
6767
return store_engine_leds(dev, attr, buf, len, nr); \
6868
}
6969

70-
#define store_load(nr) \
70+
#define LP55XX_DEV_ATTR_ENGINE_LOAD(nr) \
7171
static ssize_t store_engine##nr##_load(struct device *dev, \
72-
struct device_attribute *attr, \
73-
const char *buf, size_t len) \
72+
struct device_attribute *attr, \
73+
const char *buf, size_t len) \
7474
{ \
75-
return store_engine_load(dev, attr, buf, len, nr); \
76-
}
75+
return lp55xx_store_engine_load(dev, attr, buf, len, nr); \
76+
} \
77+
static LP55XX_DEV_ATTR_WO(engine##nr##_load, store_engine##nr##_load)
7778

7879
struct lp55xx_led;
7980
struct lp55xx_chip;
@@ -227,4 +228,15 @@ extern void lp55xx_stop_engine(struct lp55xx_chip *chip);
227228
extern int lp55xx_probe(struct i2c_client *client);
228229
extern void lp55xx_remove(struct i2c_client *client);
229230

231+
/* common sysfs function */
232+
extern ssize_t lp55xx_show_engine_mode(struct device *dev,
233+
struct device_attribute *attr,
234+
char *buf, int nr);
235+
extern ssize_t lp55xx_store_engine_mode(struct device *dev,
236+
struct device_attribute *attr,
237+
const char *buf, size_t len, int nr);
238+
extern ssize_t lp55xx_store_engine_load(struct device *dev,
239+
struct device_attribute *attr,
240+
const char *buf, size_t len, int nr);
241+
230242
#endif /* _LEDS_LP55XX_COMMON_H */

0 commit comments

Comments
 (0)