1 // SPDX-License-Identifier: GPL-2.0-only
4 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
6 * LED driver for the DAC124S085 SPI DAC
9 #include <linux/leds.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
15 struct dac124s085_led {
16 struct led_classdev ldev;
17 struct spi_device *spi;
19 char name[sizeof("dac124s085-3")];
25 struct dac124s085_led leds[4];
28 #define REG_WRITE (0 << 12)
29 #define REG_WRITE_UPDATE (1 << 12)
30 #define ALL_WRITE_UPDATE (2 << 12)
31 #define POWER_DOWN_OUTPUT (3 << 12)
33 static int dac124s085_set_brightness(struct led_classdev *ldev,
34 enum led_brightness brightness)
36 struct dac124s085_led *led = container_of(ldev, struct dac124s085_led,
41 mutex_lock(&led->mutex);
42 word = cpu_to_le16(((led->id) << 14) | REG_WRITE_UPDATE |
43 (brightness & 0xfff));
44 ret = spi_write(led->spi, (const u8 *)&word, sizeof(word));
45 mutex_unlock(&led->mutex);
50 static int dac124s085_probe(struct spi_device *spi)
52 struct dac124s085 *dac;
53 struct dac124s085_led *led;
56 dac = devm_kzalloc(&spi->dev, sizeof(*dac), GFP_KERNEL);
60 spi->bits_per_word = 16;
62 for (i = 0; i < ARRAY_SIZE(dac->leds); i++) {
66 snprintf(led->name, sizeof(led->name), "dac124s085-%d", i);
67 mutex_init(&led->mutex);
68 led->ldev.name = led->name;
69 led->ldev.brightness = LED_OFF;
70 led->ldev.max_brightness = 0xfff;
71 led->ldev.brightness_set_blocking = dac124s085_set_brightness;
72 ret = led_classdev_register(&spi->dev, &led->ldev);
77 spi_set_drvdata(spi, dac);
83 led_classdev_unregister(&dac->leds[i].ldev);
88 static int dac124s085_remove(struct spi_device *spi)
90 struct dac124s085 *dac = spi_get_drvdata(spi);
93 for (i = 0; i < ARRAY_SIZE(dac->leds); i++)
94 led_classdev_unregister(&dac->leds[i].ldev);
99 static struct spi_driver dac124s085_driver = {
100 .probe = dac124s085_probe,
101 .remove = dac124s085_remove,
103 .name = "dac124s085",
107 module_spi_driver(dac124s085_driver);
109 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
110 MODULE_DESCRIPTION("DAC124S085 LED driver");
111 MODULE_LICENSE("GPL v2");
112 MODULE_ALIAS("spi:dac124s085");