From 7ee68b0bf39237ac6fa0f3137be865c4048e2e64 Mon Sep 17 00:00:00 2001
From: juhosg <juhosg@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Date: Mon, 8 Mar 2010 12:10:01 +0000
Subject: [PATCH] ar71xx: add LED driver for the RB750

git-svn-id: svn://svn.openwrt.org/openwrt/trunk@20051 3c298f89-4303-0410-b956-a3cf2f4a3e73
---
 target/linux/ar71xx/config-2.6.32             |   1 +
 target/linux/ar71xx/config-2.6.33             |   1 +
 .../files/arch/mips/ar71xx/mach-rb750.c       |  49 ++++++
 .../mips/include/asm/mach-ar71xx/mach-rb750.h |  12 ++
 .../ar71xx/files/drivers/leds/leds-rb750.c    | 140 ++++++++++++++++++
 .../patches-2.6.32/207-rb750-led-driver.patch |  23 +++
 .../patches-2.6.33/207-rb750-led-driver.patch |  23 +++
 7 files changed, 249 insertions(+)
 create mode 100644 target/linux/ar71xx/files/drivers/leds/leds-rb750.c
 create mode 100644 target/linux/ar71xx/patches-2.6.32/207-rb750-led-driver.patch
 create mode 100644 target/linux/ar71xx/patches-2.6.33/207-rb750-led-driver.patch

diff --git a/target/linux/ar71xx/config-2.6.32 b/target/linux/ar71xx/config-2.6.32
index fa3609885..b343d957c 100644
--- a/target/linux/ar71xx/config-2.6.32
+++ b/target/linux/ar71xx/config-2.6.32
@@ -133,6 +133,7 @@ CONFIG_INITRAMFS_ROOT_UID=0
 CONFIG_INITRAMFS_SOURCE="../../root"
 CONFIG_IRQ_CPU=y
 # CONFIG_LEDS_GPIO is not set
+# CONFIG_LEDS_RB750 is not set
 # CONFIG_LEDS_WNDR3700_USB is not set
 # CONFIG_M25PXX_USE_FAST_READ is not set
 # CONFIG_MACH_ALCHEMY is not set
diff --git a/target/linux/ar71xx/config-2.6.33 b/target/linux/ar71xx/config-2.6.33
index 40eb555df..5d06a9efb 100644
--- a/target/linux/ar71xx/config-2.6.33
+++ b/target/linux/ar71xx/config-2.6.33
@@ -174,6 +174,7 @@ CONFIG_INLINE_WRITE_UNLOCK_IRQ=y
 # CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE is not set
 CONFIG_IRQ_CPU=y
 # CONFIG_LEDS_GPIO is not set
+# CONFIG_LEDS_RB750 is not set
 # CONFIG_LEDS_WNDR3700_USB is not set
 CONFIG_LOONGSON_UART_BASE=y
 # CONFIG_M25PXX_USE_FAST_READ is not set
diff --git a/target/linux/ar71xx/files/arch/mips/ar71xx/mach-rb750.c b/target/linux/ar71xx/files/arch/mips/ar71xx/mach-rb750.c
index 06f48328e..1698db914 100644
--- a/target/linux/ar71xx/files/arch/mips/ar71xx/mach-rb750.c
+++ b/target/linux/ar71xx/files/arch/mips/ar71xx/mach-rb750.c
@@ -8,12 +8,54 @@
  *  by the Free Software Foundation.
  */
 
+#include <linux/platform_device.h>
 #include <asm/mach-ar71xx/ar71xx.h>
 #include <asm/mach-ar71xx/mach-rb750.h>
 
 #include "machtype.h"
 #include "dev-ap91-eth.h"
 
+static struct rb750_led_data rb750_leds[] = {
+	{
+		.name		= "rb750:green:act",
+		.mask		= RB750_LED_ACT,
+		.active_low	= 1,
+	}, {
+		.name		= "rb750:green:port1",
+		.mask		= RB750_LED_PORT5,
+		.active_low	= 1,
+	}, {
+		.name		= "rb750:green:port2",
+		.mask		= RB750_LED_PORT4,
+		.active_low	= 1,
+	}, {
+		.name		= "rb750:green:port3",
+		.mask		= RB750_LED_PORT3,
+		.active_low	= 1,
+	}, {
+		.name		= "rb750:green:port4",
+		.mask		= RB750_LED_PORT2,
+		.active_low	= 1,
+	}, {
+		.name		= "rb750:green:port5",
+		.mask		= RB750_LED_PORT1,
+		.active_low	= 1,
+	}
+};
+
+static struct rb750_led_platform_data rb750_leds_data = {
+	.num_leds	= ARRAY_SIZE(rb750_leds),
+	.leds		= rb750_leds,
+};
+
+static struct platform_device rb750_leds_device = {
+	.name	= "leds-rb750",
+	.id	= -1,
+	.dev	= {
+		.platform_data = &rb750_leds_data,
+	}
+};
+
 int rb750_latch_change(u32 mask_clr, u32 mask_set)
 {
 	static DEFINE_SPINLOCK(lock);
@@ -65,7 +107,14 @@ EXPORT_SYMBOL_GPL(rb750_latch_change);
 
 static void __init rb750_setup(void)
 {
+	ar71xx_gpio_function_disable(AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
+				     AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
+				     AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
+				     AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
+				     AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN);
+
 	ap91_eth_init(NULL);
+	platform_device_register(&rb750_leds_device);
 }
 
 MIPS_MACHINE(AR71XX_MACH_RB_750, "750i", "MikroTik RouterBOARD 750",
diff --git a/target/linux/ar71xx/files/arch/mips/include/asm/mach-ar71xx/mach-rb750.h b/target/linux/ar71xx/files/arch/mips/include/asm/mach-ar71xx/mach-rb750.h
index 8165da3e6..661ba4ec7 100644
--- a/target/linux/ar71xx/files/arch/mips/include/asm/mach-ar71xx/mach-rb750.h
+++ b/target/linux/ar71xx/files/arch/mips/include/asm/mach-ar71xx/mach-rb750.h
@@ -49,6 +49,18 @@
 #define RB750_LED_BITS	(RB750_LED_PORT1 | RB750_LED_PORT2 | RB750_LED_PORT3 | \
 			 RB750_LED_PORT4 | RB750_LED_PORT5 | RB750_LED_ACT)
 
+struct rb750_led_data {
+	char	*name;
+	char	*default_trigger;
+	u32	mask;
+	int	active_low;
+};
+
+struct rb750_led_platform_data {
+	int			num_leds;
+	struct rb750_led_data	*leds;
+};
+
 int rb750_latch_change(u32 mask_clr, u32 mask_set);
 
 #endif /* _MACH_RB750_H */
\ No newline at end of file
diff --git a/target/linux/ar71xx/files/drivers/leds/leds-rb750.c b/target/linux/ar71xx/files/drivers/leds/leds-rb750.c
new file mode 100644
index 000000000..2e6064763
--- /dev/null
+++ b/target/linux/ar71xx/files/drivers/leds/leds-rb750.c
@@ -0,0 +1,140 @@
+/*
+ * LED driver for the RouterBOARD 750
+ *
+ * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
+ *
+ * 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/kernel.h>
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/leds.h>
+
+#include <asm/mach-ar71xx/mach-rb750.h>
+
+#define DRV_NAME	"leds-rb750"
+
+struct rb750_led_dev {
+	struct led_classdev	cdev;
+	u32			mask;
+	int			active_low;
+};
+
+struct rb750_led_drvdata {
+	struct rb750_led_dev	*led_devs;
+	int			num_leds;
+};
+
+static inline struct rb750_led_dev *to_rbled(struct led_classdev *led_cdev)
+{
+	return (struct rb750_led_dev *)container_of(led_cdev,
+		struct rb750_led_dev, cdev);
+}
+
+static void rb750_led_brightness_set(struct led_classdev *led_cdev,
+				     enum led_brightness value)
+{
+	struct rb750_led_dev *rbled = to_rbled(led_cdev);
+	int level;
+
+	level = (value == LED_OFF) ? 0 : 1;
+	level ^= rbled->active_low;
+
+	if (level)
+		rb750_latch_change(0, rbled->mask);
+	else
+		rb750_latch_change(rbled->mask, 0);
+}
+
+static int __devinit rb750_led_probe(struct platform_device *pdev)
+{
+	struct rb750_led_platform_data *pdata;
+	struct rb750_led_drvdata *drvdata;
+	int ret = 0;
+	int i;
+
+	pdata = pdev->dev.platform_data;
+	if (!pdata)
+		return -EINVAL;
+
+	drvdata = kzalloc(sizeof(struct rb750_led_drvdata) +
+			  sizeof(struct rb750_led_dev) * pdata->num_leds,
+			  GFP_KERNEL);
+	if (!drvdata)
+		return -ENOMEM;
+
+	drvdata->num_leds = pdata->num_leds;
+	drvdata->led_devs = (struct rb750_led_dev *) &drvdata[1];
+
+	for (i = 0; i < drvdata->num_leds; i++) {
+		struct rb750_led_dev *rbled = &drvdata->led_devs[i];
+		struct rb750_led_data *led_data = &pdata->leds[i];
+
+		rbled->cdev.name = led_data->name;
+		rbled->cdev.default_trigger = led_data->default_trigger;
+		rbled->cdev.brightness_set = rb750_led_brightness_set;
+		rbled->cdev.brightness = LED_OFF;
+
+		rbled->mask = led_data->mask;
+		rbled->active_low = !!led_data->active_low;
+
+		ret = led_classdev_register(&pdev->dev, &rbled->cdev);
+		if (ret)
+			goto err;
+	}
+
+	platform_set_drvdata(pdev, drvdata);
+	return 0;
+
+ err:
+	for (i = i - 1; i >= 0; i--)
+		led_classdev_unregister(&drvdata->led_devs[i].cdev);
+
+	kfree(drvdata);
+	return ret;
+}
+
+static int __devexit rb750_led_remove(struct platform_device *pdev)
+{
+	struct rb750_led_drvdata *drvdata;
+	int i;
+
+	drvdata = platform_get_drvdata(pdev);
+	for (i = 0; i < drvdata->num_leds; i++)
+		led_classdev_unregister(&drvdata->led_devs[i].cdev);
+
+	kfree(drvdata);
+	return 0;
+}
+
+static struct platform_driver rb750_led_driver = {
+	.probe		= rb750_led_probe,
+	.remove		= __devexit_p(rb750_led_remove),
+	.driver		= {
+		.name	= DRV_NAME,
+		.owner	= THIS_MODULE,
+	},
+};
+
+MODULE_ALIAS("platform:leds-rb750");
+
+static int __init rb750_led_init(void)
+{
+	return platform_driver_register(&rb750_led_driver);
+}
+
+static void __exit rb750_led_exit(void)
+{
+	platform_driver_unregister(&rb750_led_driver);
+}
+
+module_init(rb750_led_init);
+module_exit(rb750_led_exit);
+
+MODULE_DESCRIPTION(DRV_NAME);
+MODULE_DESCRIPTION("LED driver for the RouterBOARD 750");
+MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
+MODULE_LICENSE("GPL v2");
diff --git a/target/linux/ar71xx/patches-2.6.32/207-rb750-led-driver.patch b/target/linux/ar71xx/patches-2.6.32/207-rb750-led-driver.patch
new file mode 100644
index 000000000..a668c2c18
--- /dev/null
+++ b/target/linux/ar71xx/patches-2.6.32/207-rb750-led-driver.patch
@@ -0,0 +1,23 @@
+--- a/drivers/leds/Kconfig
++++ b/drivers/leds/Kconfig
+@@ -243,6 +243,10 @@
+ 	  This option enables support for the USB LED found on the
+ 	  NETGEAR WNDR3700 board.
+ 
++config LEDS_RB750
++	tristate "LED driver for the Mikrotik RouterBOARD 750"
++	depends on LEDS_CLASS && AR71XX_MACH_RB750
++
+ comment "LED Triggers"
+ 
+ config LEDS_TRIGGERS
+--- a/drivers/leds/Makefile
++++ b/drivers/leds/Makefile
+@@ -30,6 +30,7 @@
+ obj-$(CONFIG_LEDS_WM8350)		+= leds-wm8350.o
+ obj-$(CONFIG_LEDS_PWM)			+= leds-pwm.o
+ obj-${CONFIG_LEDS_WNDR3700_USB}		+= leds-wndr3700-usb.o
++obj-${CONFIG_LEDS_RB750}		+= leds-rb750.o
+ 
+ # LED SPI Drivers
+ obj-$(CONFIG_LEDS_DAC124S085)		+= leds-dac124s085.o
diff --git a/target/linux/ar71xx/patches-2.6.33/207-rb750-led-driver.patch b/target/linux/ar71xx/patches-2.6.33/207-rb750-led-driver.patch
new file mode 100644
index 000000000..a668c2c18
--- /dev/null
+++ b/target/linux/ar71xx/patches-2.6.33/207-rb750-led-driver.patch
@@ -0,0 +1,23 @@
+--- a/drivers/leds/Kconfig
++++ b/drivers/leds/Kconfig
+@@ -243,6 +243,10 @@
+ 	  This option enables support for the USB LED found on the
+ 	  NETGEAR WNDR3700 board.
+ 
++config LEDS_RB750
++	tristate "LED driver for the Mikrotik RouterBOARD 750"
++	depends on LEDS_CLASS && AR71XX_MACH_RB750
++
+ comment "LED Triggers"
+ 
+ config LEDS_TRIGGERS
+--- a/drivers/leds/Makefile
++++ b/drivers/leds/Makefile
+@@ -30,6 +30,7 @@
+ obj-$(CONFIG_LEDS_WM8350)		+= leds-wm8350.o
+ obj-$(CONFIG_LEDS_PWM)			+= leds-pwm.o
+ obj-${CONFIG_LEDS_WNDR3700_USB}		+= leds-wndr3700-usb.o
++obj-${CONFIG_LEDS_RB750}		+= leds-rb750.o
+ 
+ # LED SPI Drivers
+ obj-$(CONFIG_LEDS_DAC124S085)		+= leds-dac124s085.o
-- 
2.20.1