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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* Keyboard power button LED state machine.
*
* This sets up TIM2 to drive the power button LED so that the duty cycle
* can range from 0-100%. When the lid is closed or turned off, then the
* PWM is disabled and the GPIO is reconfigured to minimize leakage voltage.
*
* In suspend mode, duty cycle transitions progressively slower from 0%
* to 100%, and progressively faster from 100% back down to 0%. This
* results in a breathing effect. It takes about 2sec for a full cycle.
*/
#include "console.h"
#include "gpio.h"
#include "power_led.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "util.h"
#define LED_STATE_TIMEOUT_MIN (15 * MSEC) /* Minimum of 15ms per step */
#define LED_HOLD_TIME (330 * MSEC) /* Hold for 330ms at min/max */
#define LED_STEP_PERCENT 4 /* Incremental value of each step */
static enum powerled_state led_state = POWERLED_STATE_ON;
static int power_led_percent = 100;
static int using_pwm;
void powerled_set_state(enum powerled_state new_state)
{
led_state = new_state;
/* Wake up the task */
task_wake(TASK_ID_POWERLED);
}
static void power_led_set_duty(int percent)
{
ASSERT((percent >= 0) && (percent <= 100));
power_led_percent = percent;
/*
* Set the duty cycle. CCRx = percent * ARR / 100. Since we set
* ARR=100, this is just percent.
*/
#ifdef BOARD_pit
STM32_TIM_CCR3(2) = percent;
#else
STM32_TIM_CCR2(2) = percent;
#endif
}
static void power_led_use_pwm(void)
{
/* Configure power LED GPIO for TIM2/PWM alternate function */
#ifdef BOARD_pit
/* PA2 = TIM2_CH3 */
gpio_set_alternate_function(GPIO_A, (1 << 2), GPIO_ALT_TIM2);
#else
/* PB3 = TIM2_CH2 */
uint32_t val = STM32_GPIO_CRL(GPIO_B) & ~0x0000f000;
val |= 0x00009000; /* alt. function (TIM2/PWM) */
STM32_GPIO_CRL(GPIO_B) = val;
#endif
/* Enable TIM2 clock */
STM32_RCC_APB1ENR |= 0x1;
/* Disable counter during setup */
STM32_TIM_CR1(2) = 0x0000;
/*
* CPU_CLOCK / PSC determines how fast the counter operates.
* ARR determines the wave period, CCRn determines duty cycle.
* Thus, frequency = CPU_CLOCK / PSC / ARR.
*
* Assuming 16MHz clock, the following yields:
* 16MHz / 1600 / 100 = 100Hz.
*/
STM32_TIM_PSC(2) = CPU_CLOCK / 10000; /* pre-scaler */
STM32_TIM_ARR(2) = 100; /* auto-reload value */
power_led_set_duty(100);
#ifdef BOARD_PIT
/* CC3 configured as output, PWM mode 1, preload enable */
STM32_TIM_CCMR2(2) = (6 << 4) | (1 << 3);
/* CC3 output enable, active low */
STM32_TIM_CCER(2) = (1 << 8) | (1 << 9);
#else
/* CC2 configured as output, PWM mode 1, preload enable */
STM32_TIM_CCMR1(2) = (6 << 12) | (1 << 11);
/* CC2 output enable, active low */
STM32_TIM_CCER(2) = (1 << 4) | (1 << 5);
#endif
/* Generate update event to force loading of shadow registers */
STM32_TIM_EGR(2) |= 1;
/* Enable auto-reload preload, start counting */
STM32_TIM_CR1(2) |= (1 << 7) | (1 << 0);
using_pwm = 1;
}
static void power_led_manual_off(void)
{
/* disable counter */
STM32_TIM_CR1(2) &= ~0x1;
/* disable TIM2 clock */
STM32_RCC_APB1ENR &= ~0x1;
/*
* Reconfigure GPIO as a floating input. Alternatively we could
* configure it as an open-drain output and set it to high impedence,
* but reconfiguring as an input had better results in testing.
*/
gpio_set_flags(GPIO_LED_POWER_L, GPIO_INPUT);
gpio_set_level(GPIO_LED_POWER_L, 1);
using_pwm = 0;
}
/**
* Return the timeout period (in us) for the current step.
*/
static int power_led_step(void)
{
int state_timeout = 0;
static enum { DOWN = -1, UP = 1 } dir = UP;
if (0 == power_led_percent) {
dir = UP;
state_timeout = LED_HOLD_TIME;
} else if (100 == power_led_percent) {
dir = DOWN;
state_timeout = LED_HOLD_TIME;
} else {
/*
* Decreases timeout as duty cycle percentage approaches
* 0%, increase as it approaches 100%.
*/
state_timeout = LED_STATE_TIMEOUT_MIN +
LED_STATE_TIMEOUT_MIN * (power_led_percent / 33);
}
/*
* The next duty cycle will take effect after the timeout has
* elapsed for this duty cycle and the power LED task calls this
* function again.
*/
power_led_set_duty(power_led_percent);
power_led_percent += dir * LED_STEP_PERCENT;
return state_timeout;
}
void power_led_task(void)
{
while (1) {
int state_timeout = -1;
switch (led_state) {
case POWERLED_STATE_ON:
/*
* "ON" implies driving the LED using the PWM with a
* duty duty cycle of 100%. This produces a softer
* brightness than setting the GPIO to solid ON.
*/
if (!using_pwm)
power_led_use_pwm();
power_led_set_duty(100);
state_timeout = -1;
break;
case POWERLED_STATE_OFF:
/* Reconfigure GPIO to disable the LED */
if (using_pwm)
power_led_manual_off();
state_timeout = -1;
break;
case POWERLED_STATE_SUSPEND:
/* Drive using PWM with variable duty cycle */
if (!using_pwm)
power_led_use_pwm();
state_timeout = power_led_step();
break;
default:
break;
}
task_wait_event(state_timeout);
}
}
#ifdef CONFIG_CMD_POWERLED
static int command_powerled(int argc, char **argv)
{
enum powerled_state state;
if (argc != 2)
return EC_ERROR_INVAL;
if (!strcasecmp(argv[1], "off"))
state = POWERLED_STATE_OFF;
else if (!strcasecmp(argv[1], "on"))
state = POWERLED_STATE_ON;
else if (!strcasecmp(argv[1], "suspend"))
state = POWERLED_STATE_SUSPEND;
else
return EC_ERROR_INVAL;
powerled_set_state(state);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(powerled, command_powerled,
"[off | on | suspend]",
"Change power LED state",
NULL);
#endif
|