summaryrefslogtreecommitdiff
path: root/board/banshee/led.c
blob: 14713880858a57233d210dd1029f998d4728060b (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Banshee specific PWM LED settings:
 * there are 2 LEDs on each side of the board,
 * each one can be controlled separately. The LED colors are white or amber,
 * and the default behavior is tied to the charging process: both sides are
 * amber while charging the battery and white when the battery is charged.
 */

#include <stdint.h>

#include "common.h"
#include "charge_manager.h"
#include "charge_state.h"
#include "compile_time_macros.h"
#include "ec_commands.h"
#include "gpio.h"
#include "hooks.h"
#include "lid_switch.h"
#include "led_common.h"
#include "led_pwm.h"
#include "pwm.h"
#include "util.h"

#define LED_TICKS_PER_CYCLE 10
#define LED_ON_TICKS 5

#define BREATH_LIGHT_LENGTH 100
#define BREATH_HOLD_LENGTH 50
#define BREATH_OFF_LENGTH 200

const enum ec_led_id supported_led_ids[] = {
	EC_LED_ID_BATTERY_LED,
	EC_LED_ID_POWER_LED,
};

enum breath_status {
	BREATH_LIGHT_UP = 0,
	BREATH_LIGHT_DOWN,
	BREATH_HOLD,
	BREATH_OFF,
};

enum led_port { RIGHT_PORT = 1, LEFT_PORT };

const int supported_led_ids_count = ARRAY_SIZE(supported_led_ids);

struct pwm_led_color_map led_color_map[EC_LED_COLOR_COUNT] = {
	/* Red, Green, Blue */
	[EC_LED_COLOR_RED] = { 50, 0, 0 },
	[EC_LED_COLOR_GREEN] = { 0, 50, 0 },
	[EC_LED_COLOR_BLUE] = { 0, 0, 8 },
	[EC_LED_COLOR_YELLOW] = { 40, 50, 0 },
	[EC_LED_COLOR_WHITE] = { 4, 10, 5 },
	[EC_LED_COLOR_AMBER] = { 45, 5, 0 },
};

struct pwm_led_color_map pwr_led_color_map[EC_LED_COLOR_COUNT] = {
	/* White, Green, Red */
	[EC_LED_COLOR_WHITE] = { BREATH_LIGHT_LENGTH, 0, 0 },
};

/*
 * Three logical LEDs with red、 blue、green channels
 * and one logical LED with white channels.
 */
struct pwm_led pwm_leds[CONFIG_LED_PWM_COUNT] = {
	[PWM_LED0] = {
		.ch0 = PWM_CH_SIDE_LED_R,
		.ch1 = PWM_CH_SIDE_LED_G,
		.ch2 = PWM_CH_SIDE_LED_B,
		.enable = &pwm_enable,
		.set_duty = &pwm_set_duty,
	},
	[PWM_LED1] = {
		.ch0 = PWM_CH_POWER_LED_W,
		.ch1 = PWM_LED_NO_CHANNEL,
		.ch2 = PWM_LED_NO_CHANNEL,
		.enable = &pwm_enable,
		.set_duty = &pwm_set_duty,
	},
};

uint8_t breath_led_light_up;
uint8_t breath_led_light_down;
uint8_t breath_led_hold;
uint8_t breath_led_off;

int breath_pwm_enable;
int breath_led_status;
static void breath_led_pwm_deferred(void);
DECLARE_DEFERRED(breath_led_pwm_deferred);

/*
 *	Breath LED API
 *	Max duty (percentage) = BREATH_LIGHT_LENGTH (100%)
 *	Fade time (second) = 1000ms(In) / 1000ms(Out)
 *	Duration time (second) = BREATH_HOLD_LENGTH(500ms)
 *	Interval time (second) = BREATH_OFF_LENGTH(2000ms)
 */

static void breath_led_pwm_deferred(void)
{
	switch (breath_led_status) {
	case BREATH_LIGHT_UP:

		if (breath_led_light_up <= BREATH_LIGHT_LENGTH)
			pwm_set_duty(PWM_CH_POWER_LED_W, breath_led_light_up++);
		else {
			breath_led_light_up = 0;
			breath_led_light_down = BREATH_LIGHT_LENGTH;
			breath_led_status = BREATH_HOLD;
		}

		break;
	case BREATH_HOLD:

		if (breath_led_hold <= BREATH_HOLD_LENGTH)
			breath_led_hold++;
		else {
			breath_led_hold = 0;
			breath_led_status = BREATH_LIGHT_DOWN;
		}

		break;
	case BREATH_LIGHT_DOWN:

		if (breath_led_light_down != 0)
			pwm_set_duty(PWM_CH_POWER_LED_W,
				     breath_led_light_down--);
		else {
			breath_led_light_down = BREATH_LIGHT_LENGTH;
			breath_led_status = BREATH_OFF;
		}

		break;
	case BREATH_OFF:

		if (breath_led_off <= BREATH_OFF_LENGTH)
			breath_led_off++;
		else {
			breath_led_off = 0;
			breath_led_status = BREATH_LIGHT_UP;
		}

		break;
	}

	if (breath_pwm_enable)
		hook_call_deferred(&breath_led_pwm_deferred_data, 10 * MSEC);
}

void breath_led_run(uint8_t enable)
{
	if (enable && !breath_pwm_enable) {
		breath_pwm_enable = true;
		breath_led_status = BREATH_LIGHT_UP;
		hook_call_deferred(&breath_led_pwm_deferred_data, 10 * MSEC);
	} else if (!enable && breath_pwm_enable) {
		breath_pwm_enable = false;
		breath_led_light_up = 0;
		breath_led_light_down = 0;
		breath_led_hold = 0;
		breath_led_off = 0;
		breath_led_status = BREATH_OFF;
		hook_call_deferred(&breath_led_pwm_deferred_data, -1);
	}
}

void led_get_brightness_range(enum ec_led_id led_id, uint8_t *brightness_range)
{
	if (led_id == EC_LED_ID_BATTERY_LED) {
		brightness_range[EC_LED_COLOR_RED] = 100;
		brightness_range[EC_LED_COLOR_GREEN] = 100;
		brightness_range[EC_LED_COLOR_YELLOW] = 100;
		brightness_range[EC_LED_COLOR_AMBER] = 100;
		brightness_range[EC_LED_COLOR_BLUE] = 100;
		brightness_range[EC_LED_COLOR_WHITE] = 100;
	} else if (led_id == EC_LED_ID_POWER_LED)
		brightness_range[EC_LED_COLOR_WHITE] = 100;
}

void set_pwr_led_color(enum pwm_led_id id, int color)
{
	struct pwm_led duty = { 0 };
	const struct pwm_led *led = &pwm_leds[id];

	if ((id >= CONFIG_LED_PWM_COUNT) || (id < 0) ||
	    (color >= EC_LED_COLOR_COUNT) || (color < -1))
		return;

	if (color != -1) {
		duty.ch0 = pwr_led_color_map[color].ch0;
		duty.ch1 = pwr_led_color_map[color].ch1;
		duty.ch2 = pwr_led_color_map[color].ch2;
	}

	if (led->ch0 != (enum pwm_channel)PWM_LED_NO_CHANNEL)
		led->set_duty(led->ch0, duty.ch0);
	if (led->ch1 != (enum pwm_channel)PWM_LED_NO_CHANNEL)
		led->set_duty(led->ch1, duty.ch1);
	if (led->ch2 != (enum pwm_channel)PWM_LED_NO_CHANNEL)
		led->set_duty(led->ch2, duty.ch2);
}

int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
{
	enum pwm_led_id pwm_id;

	/* Convert ec_led_id to pwm_led_id. */
	if (led_id == EC_LED_ID_BATTERY_LED)
		pwm_id = PWM_LED0;
	else if (led_id == EC_LED_ID_POWER_LED)
		pwm_id = PWM_LED1;
	else
		return EC_ERROR_UNKNOWN;

	if (led_id == EC_LED_ID_POWER_LED) {
		if (brightness[EC_LED_COLOR_WHITE])
			set_pwr_led_color(pwm_id, EC_LED_COLOR_WHITE);
		else
			/* Otherwise, the "color" is "off". */
			set_pwr_led_color(pwm_id, -1);
	} else {
		if (brightness[EC_LED_COLOR_RED])
			set_pwm_led_color(pwm_id, EC_LED_COLOR_RED);
		else if (brightness[EC_LED_COLOR_GREEN])
			set_pwm_led_color(pwm_id, EC_LED_COLOR_GREEN);
		else if (brightness[EC_LED_COLOR_BLUE])
			set_pwm_led_color(pwm_id, EC_LED_COLOR_BLUE);
		else if (brightness[EC_LED_COLOR_YELLOW])
			set_pwm_led_color(pwm_id, EC_LED_COLOR_YELLOW);
		else if (brightness[EC_LED_COLOR_WHITE])
			set_pwm_led_color(pwm_id, EC_LED_COLOR_WHITE);
		else if (brightness[EC_LED_COLOR_AMBER])
			set_pwm_led_color(pwm_id, EC_LED_COLOR_AMBER);
		else
			/* Otherwise, the "color" is "off". */
			set_pwm_led_color(pwm_id, -1);
	}

	return EC_SUCCESS;
}

static int led_get_charge_percent(void)
{
	return DIV_ROUND_NEAREST(charge_get_display_charge(), 10);
}

static void select_active_port_led(int port)
{
	if ((charge_get_state() == PWR_STATE_DISCHARGE &&
	     led_get_charge_percent() < 10) ||
	    charge_get_state() == PWR_STATE_ERROR) {
		gpio_set_level(GPIO_LEFT_SIDE, 1);
		gpio_set_level(GPIO_RIGHT_SIDE, 1);
	} else if (port == RIGHT_PORT) {
		gpio_set_level(GPIO_LEFT_SIDE, 0);
		gpio_set_level(GPIO_RIGHT_SIDE, 1);
	} else if (port == LEFT_PORT) {
		gpio_set_level(GPIO_LEFT_SIDE, 1);
		gpio_set_level(GPIO_RIGHT_SIDE, 0);
	} else {
		gpio_set_level(GPIO_LEFT_SIDE, 0);
		gpio_set_level(GPIO_RIGHT_SIDE, 0);
	}
}

static int led_power_enable(void)
{
	if (gpio_get_level(GPIO_LEFT_SIDE) || gpio_get_level(GPIO_RIGHT_SIDE))
		return true;

	return false;
}

static void set_active_port_color(int color)
{
	int usbc_port = charge_manager_get_active_charge_port();
	int port = 0;

	if ((usbc_port == USBC_PORT_C0) || (usbc_port == USBC_PORT_C1))
		port = RIGHT_PORT;
	else if ((usbc_port == USBC_PORT_C2) || (usbc_port == USBC_PORT_C3))
		port = LEFT_PORT;

	if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED)) {
		select_active_port_led(port);
		set_pwm_led_color(PWM_LED0, led_power_enable() ? color : -1);
	}
}

static void led_set_battery(void)
{
	static int battery_ticks;

	battery_ticks++;

	switch (charge_get_state()) {
	case PWR_STATE_CHARGE:
		/* Always indicate when charging, even in suspend. */
		set_active_port_color(EC_LED_COLOR_AMBER);
		break;
	case PWR_STATE_DISCHARGE:
		if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED)) {
			if (led_get_charge_percent() < 10)
				set_active_port_color((battery_ticks & 0x2) ?
							      EC_LED_COLOR_RED :
							      -1);
			else
				set_active_port_color(-1);
		}
		break;
	case PWR_STATE_ERROR:
		set_active_port_color((battery_ticks & 0x2) ? EC_LED_COLOR_RED :
							      -1);
		break;
	case PWR_STATE_CHARGE_NEAR_FULL:
		set_active_port_color(EC_LED_COLOR_WHITE);
		break;
	case PWR_STATE_IDLE:
		set_active_port_color(EC_LED_COLOR_AMBER);
		break;
	case PWR_STATE_FORCED_IDLE:
		set_active_port_color(
			(battery_ticks & 0x4) ? EC_LED_COLOR_AMBER : -1);
		break;
	default:
		break;
	}
}

static void led_set_power(void)
{
	/* turn off led when lid is close*/
	if (!lid_is_open()) {
		set_pwr_led_color(PWM_LED1, -1);
		return;
	}

	if (chipset_in_state(CHIPSET_STATE_ANY_SUSPEND)) {
		breath_led_run(1);
		return;
	} else
		breath_led_run(0);

	if (chipset_in_state(CHIPSET_STATE_ON)) {
		set_pwr_led_color(PWM_LED1, EC_LED_COLOR_WHITE);
	} else
		set_pwr_led_color(PWM_LED1, -1);
}

/* Called by hook task every TICK */
static void led_tick(void)
{
	if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED))
		led_set_battery();
	if (led_auto_control_is_enabled(EC_LED_ID_POWER_LED))
		led_set_power();
}
DECLARE_HOOK(HOOK_TICK, led_tick, HOOK_PRIO_DEFAULT);