leds-ns2.c 9.4 KB
Newer Older
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
/*
 * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
 *
 * Copyright (C) 2010 LaCie
 *
 * Author: Simon Guinot <sguinot@lacie.com>
 *
 * Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/leds.h>
30
#include <linux/module.h>
31
#include <linux/platform_data/leds-kirkwood-ns2.h>
S
Sachin Kamat 已提交
32
#include <linux/of.h>
33
#include <linux/of_gpio.h>
34 35

/*
36 37 38 39
 * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED
 * modes are available: off, on and SATA activity blinking. The LED modes are
 * controlled through two GPIOs (command and slow): each combination of values
 * for the command/slow GPIOs corresponds to a LED mode.
40 41 42 43 44 45 46 47
 */

struct ns2_led_data {
	struct led_classdev	cdev;
	unsigned		cmd;
	unsigned		slow;
	unsigned char		sata; /* True when SATA mode active. */
	rwlock_t		rw_lock; /* Lock GPIOs. */
48 49
	int			num_modes;
	struct ns2_led_modval	*modval;
50 51 52 53 54 55 56 57 58 59
};

static int ns2_led_get_mode(struct ns2_led_data *led_dat,
			    enum ns2_led_modes *mode)
{
	int i;
	int ret = -EINVAL;
	int cmd_level;
	int slow_level;

S
Simon Guinot 已提交
60
	read_lock_irq(&led_dat->rw_lock);
61 62 63 64

	cmd_level = gpio_get_value(led_dat->cmd);
	slow_level = gpio_get_value(led_dat->slow);

65 66 67 68
	for (i = 0; i < led_dat->num_modes; i++) {
		if (cmd_level == led_dat->modval[i].cmd_level &&
		    slow_level == led_dat->modval[i].slow_level) {
			*mode = led_dat->modval[i].mode;
69 70 71 72 73
			ret = 0;
			break;
		}
	}

S
Simon Guinot 已提交
74
	read_unlock_irq(&led_dat->rw_lock);
75 76 77 78 79 80 81 82

	return ret;
}

static void ns2_led_set_mode(struct ns2_led_data *led_dat,
			     enum ns2_led_modes mode)
{
	int i;
S
Simon Guinot 已提交
83
	unsigned long flags;
84

S
Simon Guinot 已提交
85
	write_lock_irqsave(&led_dat->rw_lock, flags);
86

87 88
	for (i = 0; i < led_dat->num_modes; i++) {
		if (mode == led_dat->modval[i].mode) {
89
			gpio_set_value(led_dat->cmd,
90
				       led_dat->modval[i].cmd_level);
91
			gpio_set_value(led_dat->slow,
92
				       led_dat->modval[i].slow_level);
93 94 95
		}
	}

S
Simon Guinot 已提交
96
	write_unlock_irqrestore(&led_dat->rw_lock, flags);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

static void ns2_led_set(struct led_classdev *led_cdev,
			enum led_brightness value)
{
	struct ns2_led_data *led_dat =
		container_of(led_cdev, struct ns2_led_data, cdev);
	enum ns2_led_modes mode;

	if (value == LED_OFF)
		mode = NS_V2_LED_OFF;
	else if (led_dat->sata)
		mode = NS_V2_LED_SATA;
	else
		mode = NS_V2_LED_ON;

	ns2_led_set_mode(led_dat, mode);
}

static ssize_t ns2_led_sata_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buff, size_t count)
{
120 121 122
	struct led_classdev *led_cdev = dev_get_drvdata(dev);
	struct ns2_led_data *led_dat =
		container_of(led_cdev, struct ns2_led_data, cdev);
123 124 125 126
	int ret;
	unsigned long enable;
	enum ns2_led_modes mode;

127
	ret = kstrtoul(buff, 10, &enable);
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
	if (ret < 0)
		return ret;

	enable = !!enable;

	if (led_dat->sata == enable)
		return count;

	ret = ns2_led_get_mode(led_dat, &mode);
	if (ret < 0)
		return ret;

	if (enable && mode == NS_V2_LED_ON)
		ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
	if (!enable && mode == NS_V2_LED_SATA)
		ns2_led_set_mode(led_dat, NS_V2_LED_ON);

	led_dat->sata = enable;

	return count;
}

static ssize_t ns2_led_sata_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
153 154 155
	struct led_classdev *led_cdev = dev_get_drvdata(dev);
	struct ns2_led_data *led_dat =
		container_of(led_cdev, struct ns2_led_data, cdev);
156 157 158 159 160 161

	return sprintf(buf, "%d\n", led_dat->sata);
}

static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);

162 163 164 165 166 167
static struct attribute *ns2_led_attrs[] = {
	&dev_attr_sata.attr,
	NULL
};
ATTRIBUTE_GROUPS(ns2_led);

B
Bill Pemberton 已提交
168
static int
169 170 171 172 173 174
create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
	       const struct ns2_led *template)
{
	int ret;
	enum ns2_led_modes mode;

175
	ret = devm_gpio_request_one(&pdev->dev, template->cmd,
176 177
			gpio_get_value(template->cmd) ?
			GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
J
Jingoo Han 已提交
178
			template->name);
179 180 181
	if (ret) {
		dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
			template->name);
J
Jingoo Han 已提交
182
		return ret;
183 184
	}

185
	ret = devm_gpio_request_one(&pdev->dev, template->slow,
186 187
			gpio_get_value(template->slow) ?
			GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
J
Jingoo Han 已提交
188
			template->name);
189 190 191
	if (ret) {
		dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
			template->name);
192
		return ret;
193 194 195 196 197 198 199 200 201
	}

	rwlock_init(&led_dat->rw_lock);

	led_dat->cdev.name = template->name;
	led_dat->cdev.default_trigger = template->default_trigger;
	led_dat->cdev.blink_set = NULL;
	led_dat->cdev.brightness_set = ns2_led_set;
	led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
202
	led_dat->cdev.groups = ns2_led_groups;
203 204
	led_dat->cmd = template->cmd;
	led_dat->slow = template->slow;
205 206
	led_dat->modval = template->modval;
	led_dat->num_modes = template->num_modes;
207 208 209

	ret = ns2_led_get_mode(led_dat, &mode);
	if (ret < 0)
210
		return ret;
211 212 213 214 215 216 217 218

	/* Set LED initial state. */
	led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
	led_dat->cdev.brightness =
		(mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;

	ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
	if (ret < 0)
219
		return ret;
220 221 222 223

	return 0;
}

224
static void delete_ns2_led(struct ns2_led_data *led_dat)
225 226 227 228
{
	led_classdev_unregister(&led_dat->cdev);
}

229 230 231 232
#ifdef CONFIG_OF_GPIO
/*
 * Translate OpenFirmware node properties into platform_data.
 */
233
static int
234 235 236 237
ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
{
	struct device_node *np = dev->of_node;
	struct device_node *child;
238
	struct ns2_led *led, *leds;
239 240 241 242 243 244 245 246 247 248 249
	int num_leds = 0;

	num_leds = of_get_child_count(np);
	if (!num_leds)
		return -ENODEV;

	leds = devm_kzalloc(dev, num_leds * sizeof(struct ns2_led),
			    GFP_KERNEL);
	if (!leds)
		return -ENOMEM;

250
	led = leds;
251 252
	for_each_child_of_node(np, child) {
		const char *string;
253 254
		int ret, i, num_modes;
		struct ns2_led_modval *modval;
255 256 257 258

		ret = of_get_named_gpio(child, "cmd-gpio", 0);
		if (ret < 0)
			return ret;
259
		led->cmd = ret;
260 261 262
		ret = of_get_named_gpio(child, "slow-gpio", 0);
		if (ret < 0)
			return ret;
263
		led->slow = ret;
264
		ret = of_property_read_string(child, "label", &string);
265
		led->name = (ret == 0) ? string : child->name;
266 267 268
		ret = of_property_read_string(child, "linux,default-trigger",
					      &string);
		if (ret == 0)
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
			led->default_trigger = string;

		ret = of_property_count_u32_elems(child, "modes-map");
		if (ret < 0 || ret % 3) {
			dev_err(dev,
				"Missing or malformed modes-map property\n");
			return -EINVAL;
		}

		num_modes = ret / 3;
		modval = devm_kzalloc(dev,
				      num_modes * sizeof(struct ns2_led_modval),
				      GFP_KERNEL);
		if (!modval)
			return -ENOMEM;

		for (i = 0; i < num_modes; i++) {
			of_property_read_u32_index(child,
						"modes-map", 3 * i,
						(u32 *) &modval[i].mode);
			of_property_read_u32_index(child,
						"modes-map", 3 * i + 1,
						(u32 *) &modval[i].cmd_level);
			of_property_read_u32_index(child,
						"modes-map", 3 * i + 2,
						(u32 *) &modval[i].slow_level);
		}

		led->num_modes = num_modes;
		led->modval = modval;
299

300
		led++;
301 302 303 304 305 306 307 308 309 310 311 312 313 314
	}

	pdata->leds = leds;
	pdata->num_leds = num_leds;

	return 0;
}

static const struct of_device_id of_ns2_leds_match[] = {
	{ .compatible = "lacie,ns2-leds", },
	{},
};
#endif /* CONFIG_OF_GPIO */

315 316 317 318 319 320 321 322 323 324 325
struct ns2_led_priv {
	int num_leds;
	struct ns2_led_data leds_data[];
};

static inline int sizeof_ns2_led_priv(int num_leds)
{
	return sizeof(struct ns2_led_priv) +
		      (sizeof(struct ns2_led_data) * num_leds);
}

B
Bill Pemberton 已提交
326
static int ns2_led_probe(struct platform_device *pdev)
327
{
J
Jingoo Han 已提交
328
	struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
329
	struct ns2_led_priv *priv;
330 331 332
	int i;
	int ret;

333 334 335 336 337 338 339 340 341 342 343 344 345
#ifdef CONFIG_OF_GPIO
	if (!pdata) {
		pdata = devm_kzalloc(&pdev->dev,
				     sizeof(struct ns2_led_platform_data),
				     GFP_KERNEL);
		if (!pdata)
			return -ENOMEM;

		ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
		if (ret)
			return ret;
	}
#else
346 347
	if (!pdata)
		return -EINVAL;
348
#endif /* CONFIG_OF_GPIO */
349

350 351 352
	priv = devm_kzalloc(&pdev->dev,
			    sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
	if (!priv)
353
		return -ENOMEM;
354
	priv->num_leds = pdata->num_leds;
355

356 357 358
	for (i = 0; i < priv->num_leds; i++) {
		ret = create_ns2_led(pdev, &priv->leds_data[i],
				     &pdata->leds[i]);
359 360
		if (ret < 0) {
			for (i = i - 1; i >= 0; i--)
361
				delete_ns2_led(&priv->leds_data[i]);
362 363
			return ret;
		}
364 365
	}

366
	platform_set_drvdata(pdev, priv);
367 368 369 370

	return 0;
}

B
Bill Pemberton 已提交
371
static int ns2_led_remove(struct platform_device *pdev)
372 373
{
	int i;
374
	struct ns2_led_priv *priv;
375

376
	priv = platform_get_drvdata(pdev);
377

378 379
	for (i = 0; i < priv->num_leds; i++)
		delete_ns2_led(&priv->leds_data[i]);
380 381 382 383 384 385

	return 0;
}

static struct platform_driver ns2_led_driver = {
	.probe		= ns2_led_probe,
B
Bill Pemberton 已提交
386
	.remove		= ns2_led_remove,
387
	.driver		= {
388 389
		.name		= "leds-ns2",
		.of_match_table	= of_match_ptr(of_ns2_leds_match),
390 391 392
	},
};

393
module_platform_driver(ns2_led_driver);
394 395 396 397

MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
MODULE_DESCRIPTION("Network Space v2 LED driver");
MODULE_LICENSE("GPL");
398
MODULE_ALIAS("platform:leds-ns2");