blob: 3c8a5728920dde76e8dc87df0160a9a5db1d4711 [file] [log] [blame]
Christian Lampartere9348cd2009-03-21 23:05:13 +01001/*
2 * Atheros AR9170 driver
3 *
4 * LED handling
5 *
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, see
20 * http://www.gnu.org/licenses/.
21 *
22 * This file incorporates work covered by the following copyright and
23 * permission notice:
24 * Copyright (c) 2007-2008 Atheros Communications, Inc.
25 *
26 * Permission to use, copy, modify, and/or distribute this software for any
27 * purpose with or without fee is hereby granted, provided that the above
28 * copyright notice and this permission notice appear in all copies.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
31 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
32 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
33 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
35 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
36 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 */
38
39#include "ar9170.h"
40#include "cmd.h"
41
42int ar9170_set_leds_state(struct ar9170 *ar, u32 led_state)
43{
44 return ar9170_write_reg(ar, AR9170_GPIO_REG_DATA, led_state);
45}
46
47int ar9170_init_leds(struct ar9170 *ar)
48{
49 int err;
50
51 /* disable LEDs */
52 /* GPIO [0/1 mode: output, 2/3: input] */
53 err = ar9170_write_reg(ar, AR9170_GPIO_REG_PORT_TYPE, 3);
54 if (err)
55 goto out;
56
57 /* GPIO 0/1 value: off */
58 err = ar9170_set_leds_state(ar, 0);
59
60out:
61 return err;
62}
63
64#ifdef CONFIG_AR9170_LEDS
65static void ar9170_update_leds(struct work_struct *work)
66{
67 struct ar9170 *ar = container_of(work, struct ar9170, led_work.work);
68 int i, tmp, blink_delay = 1000;
69 u32 led_val = 0;
70 bool rerun = false;
71
72 if (unlikely(!IS_ACCEPTING_CMD(ar)))
73 return ;
74
75 mutex_lock(&ar->mutex);
76 for (i = 0; i < AR9170_NUM_LEDS; i++)
77 if (ar->leds[i].toggled) {
78 led_val |= 1 << i;
79
80 tmp = 70 + 200 / (ar->leds[i].toggled);
81 if (tmp < blink_delay)
82 blink_delay = tmp;
83
84 if (ar->leds[i].toggled > 1)
85 ar->leds[i].toggled = 0;
86
87 rerun = true;
88 }
89
90 ar9170_set_leds_state(ar, led_val);
91 mutex_unlock(&ar->mutex);
92
93 if (rerun)
94 queue_delayed_work(ar->hw->workqueue, &ar->led_work,
95 msecs_to_jiffies(blink_delay));
96}
97
98static void ar9170_led_brightness_set(struct led_classdev *led,
99 enum led_brightness brightness)
100{
101 struct ar9170_led *arl = container_of(led, struct ar9170_led, l);
102 struct ar9170 *ar = arl->ar;
103
Christian Lamparter2431fe92009-05-25 21:51:19 +0200104 if (arl->last_state != !!brightness) {
105 arl->toggled++;
106 arl->last_state = !!brightness;
107 }
Christian Lampartere9348cd2009-03-21 23:05:13 +0100108
Christian Lamparter2431fe92009-05-25 21:51:19 +0200109 if (likely(IS_ACCEPTING_CMD(ar) && arl->toggled))
Christian Lampartere9348cd2009-03-21 23:05:13 +0100110 queue_delayed_work(ar->hw->workqueue, &ar->led_work, HZ/10);
111}
112
113static int ar9170_register_led(struct ar9170 *ar, int i, char *name,
114 char *trigger)
115{
116 int err;
117
118 snprintf(ar->leds[i].name, sizeof(ar->leds[i].name),
119 "ar9170-%s::%s", wiphy_name(ar->hw->wiphy), name);
120
121 ar->leds[i].ar = ar;
122 ar->leds[i].l.name = ar->leds[i].name;
123 ar->leds[i].l.brightness_set = ar9170_led_brightness_set;
124 ar->leds[i].l.brightness = 0;
125 ar->leds[i].l.default_trigger = trigger;
126
127 err = led_classdev_register(wiphy_dev(ar->hw->wiphy),
128 &ar->leds[i].l);
129 if (err)
130 printk(KERN_ERR "%s: failed to register %s LED (%d).\n",
131 wiphy_name(ar->hw->wiphy), ar->leds[i].name, err);
132 else
133 ar->leds[i].registered = true;
134
135 return err;
136}
137
138void ar9170_unregister_leds(struct ar9170 *ar)
139{
140 int i;
141
142 cancel_delayed_work_sync(&ar->led_work);
143
144 for (i = 0; i < AR9170_NUM_LEDS; i++)
145 if (ar->leds[i].registered) {
146 led_classdev_unregister(&ar->leds[i].l);
147 ar->leds[i].registered = false;
148 }
149}
150
151int ar9170_register_leds(struct ar9170 *ar)
152{
153 int err;
154
155 INIT_DELAYED_WORK(&ar->led_work, ar9170_update_leds);
156
157 err = ar9170_register_led(ar, 0, "tx",
158 ieee80211_get_tx_led_name(ar->hw));
159 if (err)
160 goto fail;
161
162 err = ar9170_register_led(ar, 1, "assoc",
163 ieee80211_get_assoc_led_name(ar->hw));
164 if (err)
165 goto fail;
166
167 return 0;
168
169fail:
170 ar9170_unregister_leds(ar);
171 return err;
172}
173
174#endif /* CONFIG_AR9170_LEDS */