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
|
/*
* batt_leds.c - WonderMedia Battery LED Driver.
*
* Copyright (C) 2013 WonderMedia Technologies, Inc.
*
* 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.
*
* 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.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/leds.h>
#include <linux/rtc.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/gpio.h>
#include <mach/wmt_iomux.h>
#include <mach/wmt_env.h>
enum {
LED_GREEN = 0,
LED_RED,
};
struct batt_led_trigger {
char *name;
struct led_trigger *trigger;
};
#define RTC_NAME "rtc0"
static struct rtc_device *rtc_dev = NULL;
static bool battery_led_registered = false;
static struct batt_led_trigger batt_led_triggers[] = {
[LED_GREEN] = {
.name = "bat-green",
},
[LED_RED] = {
.name = "bat-red",
}
};
static struct gpio_led batt_leds[] = {
[LED_GREEN] = {
.name = "bat-green",
.default_trigger = "bat-green",
.retain_state_suspended = 1,
.active_low = 0,
},
[LED_RED] = {
.name = "bat-red",
.default_trigger = "bat-red",
.retain_state_suspended = 1,
.active_low = 0,
},
};
static struct gpio_led_platform_data batt_leds_data = {
.leds = batt_leds,
.num_leds = ARRAY_SIZE(batt_leds),
};
static struct platform_device batt_leds_dev = {
.name = "leds-gpio",
.id = -1,
.dev = {
.platform_data = &batt_leds_data,
},
};
static int parse_battery_led_param(void)
{
char env[] = "wmt.charger.led";
char s[64];
size_t l = sizeof(s);
int id, r, g;
if (wmt_getsyspara(env, s, &l))
return -EINVAL;
if (sscanf(s, "%d:%d:%d", &id, &r, &g) != 3)
return -EINVAL;
if (id != 1 || !gpio_is_valid(r) || !gpio_is_valid(g))
return -EINVAL;
batt_leds[LED_GREEN].gpio = g;
batt_leds[LED_RED].gpio = r;
pr_info("charger-led: charging (gpio%d), full (gpio%d)\n", r, g);
return 0;
}
int batt_leds_update(int status)
{
struct led_trigger *r = batt_led_triggers[LED_RED].trigger;
struct led_trigger *g = batt_led_triggers[LED_GREEN].trigger;
if (battery_led_registered == false)
return 0;
switch (status) {
case POWER_SUPPLY_STATUS_CHARGING:
led_trigger_event(r, LED_FULL);
led_trigger_event(g, LED_OFF);
gpio_direction_input(batt_leds[LED_GREEN].gpio);
break;
case POWER_SUPPLY_STATUS_FULL:
led_trigger_event(r, LED_OFF);
led_trigger_event(g, LED_FULL);
break;
case POWER_SUPPLY_STATUS_DISCHARGING:
default:
led_trigger_event(r, LED_OFF);
led_trigger_event(g, LED_OFF);
gpio_direction_input(batt_leds[LED_GREEN].gpio);
break;
}
return 0;
}
void batt_leds_suspend_prepare(void)
{
struct rtc_wkalrm tmp;
unsigned long time, now;
//unsigned long add = 30; /* seconds */
unsigned long add = 120; /* seconds */
if (battery_led_registered == false)
return;
if (!rtc_dev) {
rtc_dev = rtc_class_open(RTC_NAME);
if (IS_ERR_OR_NULL(rtc_dev)) {
rtc_dev = NULL;
pr_err("Cannot get RTC %s, %ld.\n", RTC_NAME, PTR_ERR(rtc_dev));
return;
}
}
tmp.enabled = 1;
rtc_read_time(rtc_dev, &tmp.time);
rtc_tm_to_time(&tmp.time, &now);
time = now + add;
rtc_time_to_tm(time, &tmp.time);
rtc_set_alarm(rtc_dev, &tmp);
}
void batt_leds_resume_complete(void)
{
if (rtc_dev) {
rtc_class_close(rtc_dev);
rtc_dev = NULL;
}
}
int batt_leds_setup(void)
{
int i;
if (parse_battery_led_param())
return -EINVAL;
platform_device_register(&batt_leds_dev);
for (i = 0; i < ARRAY_SIZE(batt_led_triggers); ++i)
led_trigger_register_simple(batt_led_triggers[i].name,
&batt_led_triggers[i].trigger);
battery_led_registered = true;
return 0;
}
void batt_leds_cleanup(void)
{
int i;
if (battery_led_registered == true) {
platform_device_unregister(&batt_leds_dev);
for (i = 0; i < ARRAY_SIZE(batt_led_triggers); ++i)
led_trigger_unregister_simple(batt_led_triggers[i].trigger);
battery_led_registered = false;
}
}
|