blob: 098d9aae7259e1a7dc117c6143eb1be57a45b7fa [file] [log] [blame]
Guennadi Liakhovetskiac2dd0f2009-01-10 18:58:28 +00001/*
2 * Copyright 2008
3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4 *
5 * This file is subject to the terms and conditions of version 2 of
6 * the GNU General Public License. See the file COPYING in the main
7 * directory of this archive for more details.
8 *
9 * LED driver for the DAC124S085 SPI DAC
10 */
11
12#include <linux/gfp.h>
13#include <linux/leds.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/workqueue.h>
19#include <linux/spi/spi.h>
20
21struct dac124s085_led {
22 struct led_classdev ldev;
23 struct spi_device *spi;
24 int id;
25 int brightness;
26 char name[sizeof("dac124s085-3")];
27
28 struct mutex mutex;
29 struct work_struct work;
30 spinlock_t lock;
31};
32
33struct dac124s085 {
34 struct dac124s085_led leds[4];
35};
36
37#define REG_WRITE (0 << 12)
38#define REG_WRITE_UPDATE (1 << 12)
39#define ALL_WRITE_UPDATE (2 << 12)
40#define POWER_DOWN_OUTPUT (3 << 12)
41
42static void dac124s085_led_work(struct work_struct *work)
43{
44 struct dac124s085_led *led = container_of(work, struct dac124s085_led,
45 work);
46 u16 word;
47
48 mutex_lock(&led->mutex);
49 word = cpu_to_le16(((led->id) << 14) | REG_WRITE_UPDATE |
50 (led->brightness & 0xfff));
51 spi_write(led->spi, (const u8 *)&word, sizeof(word));
52 mutex_unlock(&led->mutex);
53}
54
55static void dac124s085_set_brightness(struct led_classdev *ldev,
56 enum led_brightness brightness)
57{
58 struct dac124s085_led *led = container_of(ldev, struct dac124s085_led,
59 ldev);
60
61 spin_lock(&led->lock);
62 led->brightness = brightness;
63 schedule_work(&led->work);
64 spin_unlock(&led->lock);
65}
66
67static int dac124s085_probe(struct spi_device *spi)
68{
69 struct dac124s085 *dac;
70 struct dac124s085_led *led;
71 int i, ret;
72
73 dac = kzalloc(sizeof(*dac), GFP_KERNEL);
74 if (!dac)
75 return -ENOMEM;
76
77 spi->bits_per_word = 16;
78
79 for (i = 0; i < ARRAY_SIZE(dac->leds); i++) {
80 led = dac->leds + i;
81 led->id = i;
82 led->brightness = LED_OFF;
83 led->spi = spi;
84 snprintf(led->name, sizeof(led->name), "dac124s085-%d", i);
85 spin_lock_init(&led->lock);
86 INIT_WORK(&led->work, dac124s085_led_work);
87 mutex_init(&led->mutex);
88 led->ldev.name = led->name;
89 led->ldev.brightness = LED_OFF;
90 led->ldev.max_brightness = 0xfff;
91 led->ldev.brightness_set = dac124s085_set_brightness;
92 ret = led_classdev_register(&spi->dev, &led->ldev);
93 if (ret < 0)
94 goto eledcr;
95 }
96
97 spi_set_drvdata(spi, dac);
98
99 return 0;
100
101eledcr:
102 while (i--)
103 led_classdev_unregister(&dac->leds[i].ldev);
104
105 spi_set_drvdata(spi, NULL);
106 kfree(dac);
107 return ret;
108}
109
110static int dac124s085_remove(struct spi_device *spi)
111{
112 struct dac124s085 *dac = spi_get_drvdata(spi);
113 int i;
114
115 for (i = 0; i < ARRAY_SIZE(dac->leds); i++) {
116 led_classdev_unregister(&dac->leds[i].ldev);
117 cancel_work_sync(&dac->leds[i].work);
118 }
119
120 spi_set_drvdata(spi, NULL);
121 kfree(dac);
122
123 return 0;
124}
125
126static struct spi_driver dac124s085_driver = {
127 .probe = dac124s085_probe,
128 .remove = dac124s085_remove,
129 .driver = {
130 .name = "dac124s085",
131 .owner = THIS_MODULE,
132 },
133};
134
135static int __init dac124s085_leds_init(void)
136{
137 return spi_register_driver(&dac124s085_driver);
138}
139
140static void __exit dac124s085_leds_exit(void)
141{
142 spi_unregister_driver(&dac124s085_driver);
143}
144
145module_init(dac124s085_leds_init);
146module_exit(dac124s085_leds_exit);
147
148MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
149MODULE_DESCRIPTION("DAC124S085 LED driver");
150MODULE_LICENSE("GPL v2");