aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ar71xx/files/drivers/leds/leds-nu801.c
blob: 11e8927785f5360c347c7ccdadca095477a60dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * LED driver for NU801
 *
 * Kevin Paul Herbert
 * Copyright (c) 2012, Meraki, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/leds-nu801.h>

#include <linux/gpio.h>
#include <linux/of_gpio.h>

#define MAX_NAME_LENGTH 24
#define NUM_COLORS 3

static const char * const led_nu801_colors[] = { "blue", "green", "red" };

struct led_nu801_led_data {
	struct led_classdev cdev;
	struct led_nu801_data *controller;
	enum led_brightness level;
	char name[MAX_NAME_LENGTH];
};

struct led_nu801_data {
	unsigned cki;
	unsigned sdi;
	int lei;
	struct delayed_work work;
	struct led_nu801_led_data *led_chain;
	int num_leds;
	const char *device_name;
	const char *name;
	u32 ndelay;
	atomic_t pending;
};

static void led_nu801_work(struct work_struct *work)
{
	struct led_nu801_data	*controller =
		container_of(work, struct led_nu801_data, work.work);
	struct led_nu801_led_data *led;
	u16 bit;
	u16 brightness;
	int index;

	for (index = 0; index < controller->num_leds; index++) {
		led = &controller->led_chain[index];
		brightness = led->level << 8; /* To do: gamma correction */
		for (bit = 0x8000; bit; bit = bit >> 1) {
			gpio_set_value(controller->sdi,
				       (brightness & bit) != 0);
			gpio_set_value(controller->cki, 1);
			if (unlikely(((index == (controller->num_leds - 1)) &&
				      (bit == 1) &&
				      (controller->lei < 0)))) {
				udelay(600);
			} else {
				ndelay(controller->ndelay);
			}
			gpio_set_value(controller->cki, 0);
			ndelay(controller->ndelay);
		}
	}
	if (controller->lei >= 0) {
		gpio_set_value(controller->lei, 1);
		ndelay(controller->ndelay);
		gpio_set_value(controller->lei, 0);
	}
	atomic_set(&controller->pending, 1);
}

static void led_nu801_set(struct led_classdev *led_cdev,
			  enum led_brightness value)
{
	struct led_nu801_led_data *led_dat =
		container_of(led_cdev, struct led_nu801_led_data, cdev);
	struct led_nu801_data *controller = led_dat->controller;

	if (led_dat->level != value) {
		led_dat->level = value;
		if (atomic_dec_and_test(&controller->pending))
			schedule_delayed_work(&led_dat->controller->work,
					      (HZ/1000) + 1);
	}
}

static int led_nu801_create(struct led_nu801_data *controller,
				    struct device *parent,
				    int index,
				    enum led_brightness brightness,
#ifdef CONFIG_LEDS_TRIGGERS
				    const char *default_trigger,
#endif
				    const char *color)
{
	struct led_nu801_led_data *led = &controller->led_chain[index];
	int ret;

	scnprintf(led->name, sizeof(led->name), "%s:%s:%s%d",
		  controller->device_name, color, controller->name,
		  (controller->num_leds - (index + 1)) / NUM_COLORS);
	led->cdev.name = led->name;
	led->cdev.brightness_set = led_nu801_set;
#ifdef CONFIG_LEDS_TRIGGERS
	led->cdev.default_trigger = default_trigger;
#endif
	led->level = brightness;
	led->controller = controller;
	ret = led_classdev_register(parent, &led->cdev);
	if (ret < 0)
		goto err;

	return 0;

err:
	kfree(led);
	return ret;
}

static int
led_nu801_create_chain(const struct led_nu801_template *template,
			struct led_nu801_data *controller,
			struct device *parent)
{
	int ret;
	int index;

	controller->cki = template->cki;
	controller->sdi = template->sdi;
	controller->lei = template->lei;
	controller->num_leds = template->num_leds * 3;
	controller->device_name = template->device_name;
	controller->name = template->name;
	controller->ndelay = template->ndelay;
	atomic_set(&controller->pending, 1);

	controller->led_chain = kzalloc(sizeof(struct led_nu801_led_data) *
					controller->num_leds, GFP_KERNEL);

	if (!controller->led_chain)
		return -ENOMEM;

	ret = gpio_request(controller->cki, template->name);
	if (ret < 0)
		goto err_free_chain;

	ret = gpio_request(controller->sdi, template->name);
	if (ret < 0)
		goto err_ret_cki;

	if (controller->lei >= 0) {
		ret = gpio_request(controller->lei, template->name);
		if (ret < 0)
			goto err_ret_sdi;
		ret = gpio_direction_output(controller->lei, 0);
		if (ret < 0)
			goto err_ret_lei;
	}

	ret = gpio_direction_output(controller->cki, 0);
	if (ret < 0)
		goto err_ret_lei;

	ret = gpio_direction_output(controller->sdi, 0);
	if (ret < 0)
		goto err_ret_lei;

	for (index = 0; index < controller->num_leds; index++) {
		ret = led_nu801_create(controller, parent, index,
			template->init_brightness
			[index % NUM_COLORS],
#ifdef CONFIG_LEDS_TRIGGERS
			template->default_trigger,
#endif
			template->led_colors[index % NUM_COLORS] ?
			template->led_colors[index % NUM_COLORS] :
			led_nu801_colors[index % NUM_COLORS]);
		if (ret < 0)
			goto err_ret_sdi;
	}

	INIT_DELAYED_WORK(&controller->work, led_nu801_work);
	schedule_delayed_work(&controller->work, 0);

	return 0;

err_ret_lei:
	if (controller->lei >= 0)
		gpio_free(controller->lei);
err_ret_sdi:
	gpio_free(controller->sdi);
err_ret_cki:
	gpio_free(controller->cki);
err_free_chain:
	kfree(controller->led_chain);

	return ret;
}

static void led_nu801_delete_chain(struct led_nu801_data *controller)
{
	struct led_nu801_led_data *led_chain;
	struct led_nu801_led_data *led;
	int index;
	int num_leds;

	led_chain = controller->led_chain;
	controller->led_chain = 0;
	num_leds = controller->num_leds;
	controller->num_leds = 0;
	cancel_delayed_work_sync(&controller->work);

	for (index = 0; index < num_leds; index++) {
		led = &led_chain[index];
		led_classdev_unregister(&led->cdev);
	}

	gpio_free(controller->cki);
	gpio_free(controller->sdi);
	if (controller->lei >= 0)
		gpio_free(controller->lei);

	kfree(led_chain);
}

static struct led_nu801_data *
leds_nu801_create_of(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node, *child;
	struct led_nu801_data *controllers;
	int count = 0, ret;
	int i = 0;

	for_each_child_of_node(np, child)
		count++;
	if (!count)
		return NULL;

	controllers = kzalloc(sizeof(struct led_nu801_data) * count,
			      GFP_KERNEL);
	if (!controllers)
		return NULL;

	for_each_child_of_node(np, child) {
		const char *state;
		struct led_nu801_template template = {};
		struct device_node *colors;
		int jj;

		template.cki = of_get_named_gpio_flags(child, "cki", 0, NULL);
		template.sdi = of_get_named_gpio_flags(child, "sdi", 0, NULL);
		if (of_find_property(child, "lei", NULL)) {
			template.lei = of_get_named_gpio_flags(child, "lei",
							       0, NULL);
		} else {
			template.lei = -1;
		}
		of_property_read_u32(child, "ndelay", &template.ndelay);
		of_property_read_u32(child, "num_leds", &template.num_leds);
		template.name = of_get_property(child, "label", NULL) ? :
			child->name;
		template.default_trigger = of_get_property(child,
			"default-trigger", NULL);

		jj = 0;
		for_each_child_of_node(child, colors) {
			template.led_colors[jj] = of_get_property(colors,
				 "label", NULL);
			state = of_get_property(colors, "state", NULL);
			if (!strncmp(state, "off", 3))
				template.init_brightness[jj] = LED_OFF;
			else if (!strncmp(state, "half", 4))
				template.init_brightness[jj] = LED_HALF;
			else if (!strncmp(state, "full", 4))
				template.init_brightness[jj] = LED_FULL;
			jj++;
		}

		ret = led_nu801_create_chain(&template,
					     &controllers[i],
					     &pdev->dev);
		if (ret < 0)
			goto err;
		i++;
	}

	return controllers;

err:
	for (i = i - 1; i >= 0; i--)
		led_nu801_delete_chain(&controllers[i]);
	kfree(controllers);
	return NULL;
}

static int led_nu801_probe(struct platform_device *pdev)
{
	struct led_nu801_platform_data *pdata = pdev->dev.platform_data;
	struct led_nu801_data *controllers;
	int i, ret = 0;

	if (!(pdata && pdata->num_controllers)) {
		controllers = leds_nu801_create_of(pdev);
		if (!controllers)
			return -ENODEV;
	}

	controllers = kzalloc(sizeof(struct led_nu801_data) *
			      pdata->num_controllers, GFP_KERNEL);
	if (!controllers)
		return -ENOMEM;

	for (i = 0; i < pdata->num_controllers; i++) {
		ret = led_nu801_create_chain(&pdata->template[i],
					      &controllers[i],
					      &pdev->dev);
		if (ret < 0)
			goto err;
	}

	platform_set_drvdata(pdev, controllers);

	return 0;

err:
	for (i = i - 1; i >= 0; i--)
		led_nu801_delete_chain(&controllers[i]);

	kfree(controllers);

	return ret;
}

static int led_nu801_remove(struct platform_device *pdev)
{
	int i;
	struct led_nu801_platform_data *pdata = pdev->dev.platform_data;
	struct led_nu801_data *controllers;

	controllers = platform_get_drvdata(pdev);

	for (i = 0; i < pdata->num_controllers; i++)
		led_nu801_delete_chain(&controllers[i]);

	kfree(controllers);

	return 0;
}

static const struct of_device_id of_numen_leds_match[] = {
	{ .compatible = "numen,leds-nu801", },
	{},
};
MODULE_DEVICE_TABLE(of, of_pwm_leds_match);

static struct platform_driver led_nu801_driver = {
	.probe		= led_nu801_probe,
	.remove		= led_nu801_remove,
	.driver		= {
		.name	= "leds-nu801",
		.owner	= THIS_MODULE,
		.of_match_table = of_numen_leds_match,
	},
};

static int __init led_nu801_init(void)
{
	return platform_driver_register(&led_nu801_driver);
}

static void __exit led_nu801_exit(void)
{
	platform_driver_unregister(&led_nu801_driver);
}

module_init(led_nu801_init);
module_exit(led_nu801_exit);

MODULE_AUTHOR("Kevin Paul Herbert <kph@meraki.net>");
MODULE_DESCRIPTION("NU801 LED driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:leds-nu801");