2 * LED ADM5120 Switch Port State Trigger
4 * Copyright (C) 2007 Bernhard Held <bernhard at bernhardheld.de>
5 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
7 * This file was based on: drivers/leds/ledtrig-timer.c
8 * Copyright 2005-2006 Openedhand Ltd.
9 * Author: Richard Purdie
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
22 #include <linux/gpio.h>
26 #define DRV_NAME "port_state"
27 #define DRV_DESC "LED ADM5120 Switch Port State Trigger"
34 #define PORT_STATE(n,v) {.name = (n), .value = (v)}
36 static struct port_state port_states
[] = {
37 PORT_STATE("off", LED_OFF
),
38 PORT_STATE("on", LED_FULL
),
39 PORT_STATE("flash", ADM5120_GPIO_FLASH
),
40 PORT_STATE("link", ADM5120_GPIO_LINK
),
41 PORT_STATE("speed", ADM5120_GPIO_SPEED
),
42 PORT_STATE("duplex", ADM5120_GPIO_DUPLEX
),
43 PORT_STATE("act", ADM5120_GPIO_ACT
),
44 PORT_STATE("coll", ADM5120_GPIO_COLL
),
45 PORT_STATE("link_act", ADM5120_GPIO_LINK_ACT
),
46 PORT_STATE("duplex_coll", ADM5120_GPIO_DUPLEX_COLL
),
47 PORT_STATE("10M_act", ADM5120_GPIO_10M_ACT
),
48 PORT_STATE("100M_act", ADM5120_GPIO_100M_ACT
),
51 static ssize_t
led_port_state_show(struct device
*dev
,
52 struct device_attribute
*attr
, char *buf
)
54 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
55 struct port_state
*state
= led_cdev
->trigger_data
;
60 for (i
= 0; i
< ARRAY_SIZE(port_states
); i
++) {
61 if (&port_states
[i
] == state
)
62 len
+= sprintf(buf
+len
, "[%s] ", port_states
[i
].name
);
64 len
+= sprintf(buf
+len
, "%s ", port_states
[i
].name
);
66 len
+= sprintf(buf
+len
, "\n");
71 static ssize_t
led_port_state_store(struct device
*dev
,
72 struct device_attribute
*attr
, const char *buf
, size_t size
)
74 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
78 for (i
= 0; i
< ARRAY_SIZE(port_states
); i
++) {
79 len
= strlen(port_states
[i
].name
);
80 if (strncmp(port_states
[i
].name
, buf
, len
) != 0)
83 if (buf
[len
] != '\0' && buf
[len
] != '\n')
86 led_cdev
->trigger_data
= &port_states
[i
];
87 led_set_brightness(led_cdev
, port_states
[i
].value
);
94 static DEVICE_ATTR(port_state
, 0644, led_port_state_show
,
95 led_port_state_store
);
97 static void adm5120_switch_trig_activate(struct led_classdev
*led_cdev
)
99 struct port_state
*state
= port_states
;
102 led_cdev
->trigger_data
= state
;
104 rc
= device_create_file(led_cdev
->dev
, &dev_attr_port_state
);
108 led_set_brightness(led_cdev
, state
->value
);
112 led_cdev
->trigger_data
= NULL
;
115 static void adm5120_switch_trig_deactivate(struct led_classdev
*led_cdev
)
117 struct port_state
*state
= led_cdev
->trigger_data
;
122 device_remove_file(led_cdev
->dev
, &dev_attr_port_state
);
126 static struct led_trigger adm5120_switch_led_trigger
= {
128 .activate
= adm5120_switch_trig_activate
,
129 .deactivate
= adm5120_switch_trig_deactivate
,
132 static int __init
adm5120_switch_trig_init(void)
134 led_trigger_register(&adm5120_switch_led_trigger
);
138 static void __exit
adm5120_switch_trig_exit(void)
140 led_trigger_unregister(&adm5120_switch_led_trigger
);
143 module_init(adm5120_switch_trig_init
);
144 module_exit(adm5120_switch_trig_exit
);
146 MODULE_AUTHOR("Bernhard Held <bernhard at bernhardheld.de>, "
147 "Gabor Juhos <juhosg@openwrt.org>");
148 MODULE_DESCRIPTION(DRV_DESC
);
149 MODULE_LICENSE("GPL v2");