summaryrefslogtreecommitdiff
path: root/board/lindar/led.c
blob: 8e804e63ebbcfd10d8ec4aab53e6d4fb9763f383 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* Copyright 2020 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.
 *
 * Power and battery LED control for Malefor
 */

#include "charge_state.h"
#include "common.h"
#include "cros_board_info.h"
#include "extpower.h"
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
#include "ktd20xx.h"
#include "led_common.h"
#include "led_onoff_states.h"
#include "lid_switch.h"
#include "stdbool.h"
#include "task.h"
#include "timer.h"

#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)

#define LED_OFF_LVL	1
#define LED_ON_LVL	0

const int led_charge_lvl_1 = 5;

const int led_charge_lvl_2 = 97;

struct led_descriptor led_bat_state_table[LED_NUM_STATES][LED_NUM_PHASES] = {
	[STATE_CHARGING_LVL_1]	     = {{EC_LED_COLOR_RED, LED_INDEFINITE} },
	[STATE_CHARGING_LVL_2]	     = {{EC_LED_COLOR_AMBER, LED_INDEFINITE} },
	[STATE_CHARGING_FULL_CHARGE] = {{EC_LED_COLOR_GREEN, LED_INDEFINITE} },
	[STATE_DISCHARGE_S0]	     = {{LED_OFF,            LED_INDEFINITE} },
	[STATE_DISCHARGE_S3]	     = {{LED_OFF,            LED_INDEFINITE} },
	[STATE_DISCHARGE_S5]         = {{LED_OFF,            LED_INDEFINITE} },
	[STATE_BATTERY_ERROR]        = {{EC_LED_COLOR_RED,   1 * LED_ONE_SEC},
					{LED_OFF,            1 * LED_ONE_SEC} },
	[STATE_FACTORY_TEST]         = {{EC_LED_COLOR_RED,   2 * LED_ONE_SEC},
					{EC_LED_COLOR_GREEN, 2 * LED_ONE_SEC} },
};

const struct led_descriptor
		led_pwr_state_table[PWR_LED_NUM_STATES][LED_NUM_PHASES] = {
	[PWR_LED_STATE_ON]            = {{EC_LED_COLOR_WHITE, LED_INDEFINITE} },
	[PWR_LED_STATE_SUSPEND_AC]    = {{EC_LED_COLOR_WHITE, 1 * LED_ONE_SEC},
					 {LED_OFF,         3 * LED_ONE_SEC} },
	[PWR_LED_STATE_SUSPEND_NO_AC] = {{EC_LED_COLOR_WHITE, 1 * LED_ONE_SEC},
					 {LED_OFF,         3 * LED_ONE_SEC} },
	[PWR_LED_STATE_OFF]           = {{LED_OFF,            LED_INDEFINITE} },
};

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

const int supported_led_ids_count = ARRAY_SIZE(supported_led_ids);

void led_set_color_power(enum ec_led_colors color)
{
	if (color == EC_LED_COLOR_WHITE)
		gpio_set_level(GPIO_LED_3_L, LED_ON_LVL);
	else
		/* LED_OFF and unsupported colors */
		gpio_set_level(GPIO_LED_3_L, LED_OFF_LVL);
}

void led_set_color_battery(enum ec_led_colors color)
{
	switch (color) {
	case EC_LED_COLOR_AMBER:
		gpio_set_level(GPIO_LED_1_L, LED_ON_LVL);
		gpio_set_level(GPIO_LED_2_L, LED_ON_LVL);
		break;
	case EC_LED_COLOR_RED:
		gpio_set_level(GPIO_LED_1_L, LED_OFF_LVL);
		gpio_set_level(GPIO_LED_2_L, LED_ON_LVL);
		break;
	case EC_LED_COLOR_GREEN:
		gpio_set_level(GPIO_LED_1_L, LED_ON_LVL);
		gpio_set_level(GPIO_LED_2_L, LED_OFF_LVL);
		break;
	default: /* LED_OFF and other unsupported colors */
		gpio_set_level(GPIO_LED_1_L, LED_OFF_LVL);
		gpio_set_level(GPIO_LED_2_L, LED_OFF_LVL);
		break;
	}
}

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] = 1;
		brightness_range[EC_LED_COLOR_AMBER] = 1;
		brightness_range[EC_LED_COLOR_GREEN] = 1;
	} else if (led_id == EC_LED_ID_POWER_LED) {
		brightness_range[EC_LED_COLOR_WHITE] = 1;
	}
}

int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
{
	if (led_id == EC_LED_ID_BATTERY_LED) {
		if (brightness[EC_LED_COLOR_RED] != 0)
			led_set_color_battery(EC_LED_COLOR_RED);
		else if (brightness[EC_LED_COLOR_AMBER] != 0)
			led_set_color_battery(EC_LED_COLOR_AMBER);
		else if (brightness[EC_LED_COLOR_GREEN] != 0)
			led_set_color_battery(EC_LED_COLOR_GREEN);
		else
			led_set_color_battery(LED_OFF);
	} else if (led_id == EC_LED_ID_POWER_LED) {
		if (brightness[EC_LED_COLOR_WHITE] != 0)
			led_set_color_power(EC_LED_COLOR_WHITE);
		else
			led_set_color_power(LED_OFF);
	}

	return EC_SUCCESS;
}

static const uint16_t ktd2061_i2c_addr = 0x68;
static void controller_write(uint8_t reg, uint8_t val)
{
	uint8_t buf[2];

	buf[0] = reg;
	buf[1] = val;

	i2c_xfer_unlocked(I2C_PORT_LIGHTBAR, ktd2061_i2c_addr,
			buf, 2, 0, 0,
			I2C_XFER_SINGLE);
}

enum lightbar_states {
	LB_STATE_OFF,
	LB_STATE_LID_CLOSE,
	LB_STATE_SLEEP_AC_ONLY,
	LB_STATE_SLEEP_AC_BAT_LOW,
	LB_STATE_SLEEP_AC_BAT_LV1,
	LB_STATE_SLEEP_AC_BAT_LV2,
	LB_STATE_SLEEP_AC_BAT_LV3,
	LB_STATE_SLEEP_AC_BAT_LV4,
	LB_STATE_SLEEP_BAT_LOW,
	LB_STATE_SLEEP_BAT_ONLY,
	LB_STATE_S0_AC_ONLY,
	LB_STATE_S0_BAT_LOW,
	LB_STATE_S0_BAT_LV1,
	LB_STATE_S0_BAT_LV2,
	LB_STATE_S0_BAT_LV3,
	LB_STATE_S0_BAT_LV4,
	LB_NUM_STATES
};

/*
 * All lightbar states should have one phase defined,
 * and an additional phase can be defined for blinking
 */
enum lightbar_phase {
	LIGHTBAR_PHASE_0 = 0,
	LIGHTBAR_PHASE_1 = 1,
	LIGHTBAR_NUM_PHASES
};

enum ec_lightbar_colors {
	BAR_RESET,
	BAR_OFF,
	BAR_COLOR_ORG_20_PERCENT,
	BAR_COLOR_GRN_40_PERCENT,
	BAR_COLOR_GRN_60_PERCENT,
	BAR_COLOR_GRN_80_PERCENT,
	BAR_COLOR_GRN_FULL,
	BAR_COLOR_ORG_FULL,
	LIGHTBAR_COLOR_TOTAL
};

struct lightbar_descriptor {
	enum ec_lightbar_colors color;
	uint8_t ticks;
};

#define BAR_INFINITE      UINT8_MAX
#define LIGHTBAR_ONE_SEC  (1000 / HOOK_TICK_INTERVAL_MS)
#define LIGHTBAR_COUNT_FOR_RESUME_FROM_SLEEP (3 * LIGHTBAR_ONE_SEC)
int lightbar_resume_tick;

const struct lightbar_descriptor
	lb_table[LB_NUM_STATES][LIGHTBAR_NUM_PHASES] = {
	[LB_STATE_OFF]              = {{BAR_OFF, BAR_INFINITE} },
	[LB_STATE_LID_CLOSE]        = {{BAR_OFF, BAR_INFINITE} },
	[LB_STATE_SLEEP_AC_ONLY]    = {{BAR_OFF, BAR_INFINITE} },
	[LB_STATE_SLEEP_AC_BAT_LOW] = {{BAR_COLOR_ORG_20_PERCENT,
				BAR_INFINITE} },
	[LB_STATE_SLEEP_AC_BAT_LV1] = {{BAR_COLOR_GRN_40_PERCENT,
				BAR_INFINITE} },
	[LB_STATE_SLEEP_AC_BAT_LV2] = {{BAR_COLOR_GRN_60_PERCENT,
				BAR_INFINITE} },
	[LB_STATE_SLEEP_AC_BAT_LV3] = {{BAR_COLOR_GRN_80_PERCENT,
				BAR_INFINITE} },
	[LB_STATE_SLEEP_AC_BAT_LV4] = {{BAR_COLOR_GRN_FULL, BAR_INFINITE} },
	[LB_STATE_SLEEP_BAT_LOW]    = {{BAR_OFF, 5 * LIGHTBAR_ONE_SEC},
				{BAR_COLOR_ORG_FULL, LIGHTBAR_ONE_SEC} },
	[LB_STATE_SLEEP_BAT_ONLY]   = {{BAR_OFF, BAR_INFINITE} },
	[LB_STATE_S0_AC_ONLY]       = {{BAR_OFF, BAR_INFINITE} },
	[LB_STATE_S0_BAT_LOW]       = {{BAR_COLOR_ORG_20_PERCENT,
				BAR_INFINITE} },
	[LB_STATE_S0_BAT_LV1]       = {{BAR_COLOR_GRN_40_PERCENT,
				BAR_INFINITE} },
	[LB_STATE_S0_BAT_LV2]       = {{BAR_COLOR_GRN_60_PERCENT,
				BAR_INFINITE} },
	[LB_STATE_S0_BAT_LV3]       = {{BAR_COLOR_GRN_80_PERCENT,
				BAR_INFINITE} },
	[LB_STATE_S0_BAT_LV4]       = {{BAR_COLOR_GRN_FULL, BAR_INFINITE} },
};

#define DISABLE_LIGHTBAR         0x00
#define ENABLE_LIGHTBAR          0x80
#define I_OFF                    0x00
#define GRN_I_ON                 0x1E
#define ORG_I_ON                 0x28
#define SEL_OFF                  0x00
#define SEL_1ST_LED              BIT(7)
#define SEL_2ND_LED              BIT(3)
#define SEL_BOTH                 (SEL_1ST_LED | SEL_2ND_LED)
#define SKU_ID_NONE              0x00
#define SKU_ID_INVALID           0x01
#define LB_SUPPORTED_SKUID_LOWER 458700
#define LB_SUPPORTED_SKUID_UPPER 458800

static bool lightbar_is_supported(void)
{
	static uint32_t skuid = SKU_ID_NONE;
	bool result;

	if (skuid == SKU_ID_NONE) {
		if (cbi_get_sku_id(&skuid)) {
			CPRINTS("Cannot get skuid for lightbar supported");
			skuid = SKU_ID_INVALID;
		}
	}

	/* Check sku_id to know if system support lightbar or not. */
	if (skuid >= LB_SUPPORTED_SKUID_LOWER &&
		skuid <= LB_SUPPORTED_SKUID_UPPER)
		result = true;
	else
		result = false;

	return result;
}

/*
 * lightbar_enter_s0ix_s3:
 * This flag is used to know if system ever enter S0ix/S3.
 * Lightbar V9 SPEC define lightbar resuming behavior, "S0ix/S3 -> S0",
 * but not include "G3/S5/S4 -> S0". "G3/S5/S4 -> S0" need to keep off.
 */
static bool lightbar_enter_s0ix_s3;

static bool lightbar_is_enabled(void)
{
	if (!lightbar_is_supported())
		return false;

	/*
	 * Lightbar's I2C is powered by PP3300_A, and its power will be turn
	 * when system enter S4/S5. It may get I2C error if EC keep polling
	 * lightbar. We should stop it when EC doesn't turn on PP330_A.
	 */
	if (!board_is_i2c_port_powered(I2C_PORT_LIGHTBAR))
		return false;

	return true;
}

/*
 * From "board_id = 3", HW change lightbar circuit, and it only support
 * two colors, orange (amber) and green. It connects KTD20xx's red-channel
 * green color led, and green-channel to orange color led.
 * Blue-channel is unused.
 *
 * lightbar_ctrl setting format is as below.
 * ID_DAT, STATUS_REG, CTRL_CFG
 * IRED_SET0, IGRN_SET0, IBLU_SET0, IRED_SET1, IGRN_SET1, IBLU_SET1
 * ISEL_A12, ISEL_A34, ISEL_B12, ISEL_B34, ISEL_C12, ISEL_C34
 */
const uint8_t lightbar_10_led_cfg[LIGHTBAR_COLOR_TOTAL][KTD20XX_TOTOAL_REG] = {
	[BAR_RESET] = {
		0x00, 0x00, DISABLE_LIGHTBAR,
		I_OFF, I_OFF, I_OFF, I_OFF, I_OFF, I_OFF,
		SEL_OFF, SEL_OFF, SEL_OFF, SEL_OFF, SEL_OFF, SEL_OFF
	},
	[BAR_OFF] = {
		0x00, 0x00, DISABLE_LIGHTBAR,
		I_OFF, I_OFF, I_OFF, I_OFF, I_OFF, I_OFF,
		SEL_OFF, SEL_OFF, SEL_OFF, SEL_OFF, SEL_OFF, SEL_OFF
	},
	[BAR_COLOR_ORG_20_PERCENT] = {
		0x00, 0x00, ENABLE_LIGHTBAR,
		I_OFF, ORG_I_ON, I_OFF, I_OFF, I_OFF, I_OFF,
		SEL_OFF, SEL_BOTH, SEL_OFF, SEL_OFF, SEL_OFF, SEL_OFF
	},
	[BAR_COLOR_GRN_40_PERCENT] = {
		0x00, 0x00, ENABLE_LIGHTBAR,
		GRN_I_ON, I_OFF, I_OFF, I_OFF, I_OFF, I_OFF,
		SEL_BOTH, SEL_BOTH, SEL_OFF, SEL_OFF, SEL_OFF, SEL_OFF
	},
	[BAR_COLOR_GRN_60_PERCENT] = {
		0x00, 0x00, ENABLE_LIGHTBAR,
		GRN_I_ON, I_OFF, I_OFF, I_OFF, I_OFF, I_OFF,
		SEL_BOTH, SEL_BOTH, SEL_OFF, SEL_BOTH, SEL_OFF, SEL_OFF
	},
	[BAR_COLOR_GRN_80_PERCENT] = {
		0x00, 0x00, ENABLE_LIGHTBAR,
		GRN_I_ON, I_OFF, I_OFF, I_OFF, I_OFF, I_OFF,
		SEL_BOTH, SEL_BOTH, SEL_BOTH, SEL_BOTH, SEL_OFF, SEL_OFF
	},
	[BAR_COLOR_GRN_FULL] = {
		0x00, 0x00, ENABLE_LIGHTBAR,
		GRN_I_ON, I_OFF, I_OFF, I_OFF, I_OFF, I_OFF,
		SEL_BOTH, SEL_BOTH, SEL_BOTH, SEL_BOTH, SEL_OFF, SEL_BOTH
	},
	[BAR_COLOR_ORG_FULL] = {
		0x00, 0x00, ENABLE_LIGHTBAR,
		I_OFF, ORG_I_ON, I_OFF, I_OFF, I_OFF, I_OFF,
		SEL_BOTH, SEL_BOTH, SEL_BOTH, SEL_BOTH, SEL_OFF, SEL_BOTH
	}
};

static void lightbar_set_color(enum ec_lightbar_colors color)
{
	enum ktd20xx_register i;
	const uint8_t (*lightbar_ctrl)[KTD20XX_TOTOAL_REG];

	lightbar_ctrl = lightbar_10_led_cfg;

	if (color >= LIGHTBAR_COLOR_TOTAL) {
		CPRINTS("Lightbar Error! Incorrect lightbard color %d", color);
		color = BAR_RESET;
	}

	i2c_lock(I2C_PORT_LIGHTBAR, 1);
	for (i = KTD20XX_IRED_SET0; i <= KTD20XX_ISEL_C34; i++)
		controller_write(i, lightbar_ctrl[color][i]);

	controller_write(KTD20XX_CTRL_CFG,
					lightbar_ctrl[color][KTD20XX_CTRL_CFG]);

	i2c_lock(I2C_PORT_LIGHTBAR, 0);
}

static void lightbar_init(void)
{
	if (!lightbar_is_enabled())
		return;

	/* Clear this flag if system doesn't enter S0ix/S3 */
	lightbar_enter_s0ix_s3 = false;
	lightbar_resume_tick = 0;

	lightbar_set_color(BAR_RESET);
}

DECLARE_HOOK(HOOK_CHIPSET_STARTUP, lightbar_init, HOOK_PRIO_DEFAULT);

static void lightbar_sleep_entry(void)
{
	if (!lightbar_is_enabled())
		return;

	/*
	 * Set this flag, then EC'll base on it to set resume tick after
	 * S0ix/S3 exit.
	 */
	lightbar_enter_s0ix_s3 = true;
	lightbar_resume_tick = 0;

	lightbar_set_color(BAR_RESET);
}

DECLARE_HOOK(HOOK_CHIPSET_SUSPEND, lightbar_sleep_entry, HOOK_PRIO_DEFAULT);

static void lightbar_sleep_exit(void)
{
	if (!lightbar_is_enabled())
		return;

	if (lightbar_enter_s0ix_s3)
		lightbar_resume_tick = LIGHTBAR_COUNT_FOR_RESUME_FROM_SLEEP;
	else
		lightbar_resume_tick = 0;
	lightbar_enter_s0ix_s3 = false;
}

DECLARE_HOOK(HOOK_CHIPSET_RESUME, lightbar_sleep_exit, HOOK_PRIO_DEFAULT);

#define LB_BAT_THRESHOLD_1	16
#define LB_BAT_THRESHOLD_2	40
#define LB_BAT_THRESHOLD_3	60
#define LB_BAT_THRESHOLD_4	80

static enum lightbar_states lightbar_get_state(void)
{
	enum lightbar_states new_state = LB_NUM_STATES;
	int cur_bat_percent;

	cur_bat_percent = charge_get_percent();

	if (!lid_is_open())
		return LB_STATE_LID_CLOSE;

	if (lightbar_resume_tick) {
		if ((battery_is_present() == BP_YES) &&
			charge_get_display_charge()) {
			if (cur_bat_percent < LB_BAT_THRESHOLD_1)
				new_state = LB_STATE_S0_BAT_LOW;
			else if (cur_bat_percent < LB_BAT_THRESHOLD_2)
				new_state = LB_STATE_S0_BAT_LV1;
			else if (cur_bat_percent < LB_BAT_THRESHOLD_3)
				new_state = LB_STATE_S0_BAT_LV2;
			else if (cur_bat_percent < LB_BAT_THRESHOLD_4)
				new_state = LB_STATE_S0_BAT_LV3;
			else
				new_state = LB_STATE_S0_BAT_LV4;
		} else
			new_state = LB_STATE_S0_AC_ONLY;
		return new_state;
	}

	if (!chipset_in_state(CHIPSET_STATE_ANY_SUSPEND))
		return LB_STATE_OFF;

	if (extpower_is_present()) {
		if ((battery_is_present() == BP_YES) &&
			charge_get_display_charge()) {
			if (cur_bat_percent < LB_BAT_THRESHOLD_1)
				new_state = LB_STATE_SLEEP_AC_BAT_LOW;
			else if (cur_bat_percent < LB_BAT_THRESHOLD_2)
				new_state = LB_STATE_SLEEP_AC_BAT_LV1;
			else if (cur_bat_percent < LB_BAT_THRESHOLD_3)
				new_state = LB_STATE_SLEEP_AC_BAT_LV2;
			else if (cur_bat_percent < LB_BAT_THRESHOLD_4)
				new_state = LB_STATE_SLEEP_AC_BAT_LV3;
			else
				new_state = LB_STATE_SLEEP_AC_BAT_LV4;
		} else
			new_state = LB_STATE_SLEEP_AC_ONLY;
	} else {
		if (cur_bat_percent < LB_BAT_THRESHOLD_1)
			new_state = LB_STATE_SLEEP_BAT_LOW;
		else
			new_state = LB_STATE_SLEEP_BAT_ONLY;
	}

	return new_state;
}

#define LIGHTBAR_DEBOUNCE_TICKS 1
static void lightbar_update(void)
{
	static uint8_t ticks, period;
	static enum lightbar_states lb_cur_state = LB_NUM_STATES;
	static int debounce_lightbar_state_update;
	enum lightbar_states desired_state;
	int phase;

	if (!lightbar_is_enabled())
		return;

	desired_state = lightbar_get_state();

	if (lightbar_resume_tick)
		lightbar_resume_tick--;

	if (desired_state != lb_cur_state &&
		desired_state < LB_NUM_STATES) {
		/* State is changing */
		lb_cur_state = desired_state;
		/* Reset ticks and period when state changes */
		ticks = 0;

		period = lb_table[lb_cur_state][LIGHTBAR_PHASE_0].ticks +
			lb_table[lb_cur_state][LIGHTBAR_PHASE_1].ticks;

		/*
		 * System will be waken up when AC status change in S0ix. Due to
		 * EC may be late to update chipset state and cause lightbar
		 * flash a while when system transfer to S0. We add to debounce
		 * for any lightbar status change.
		 * It can make sure lightbar state is ready to to update.
		 */
		debounce_lightbar_state_update = LIGHTBAR_DEBOUNCE_TICKS;
	}

	/* If this state is undefined, turn lightbar off */
	if (period == 0) {
		CPRINTS("Undefined lightbar behavior for lightbar state %d,"
			"turning off lightbar", lb_cur_state);
		lightbar_set_color(BAR_OFF);
		return;
	}

	if (debounce_lightbar_state_update != 0) {
		debounce_lightbar_state_update--;
		return;
	}

	/*
	 * Determine which phase of the state table to use. The phase is
	 * determined if it falls within first phase time duration.
	 */
	phase = ticks < lb_table[lb_cur_state][LIGHTBAR_PHASE_0].ticks ? 0 : 1;
	ticks = (ticks + 1) % period;

	/* Set the color for the given state and phase */
	lightbar_set_color(lb_table[lb_cur_state][phase].color);

}

DECLARE_HOOK(HOOK_TICK, lightbar_update, HOOK_PRIO_DEFAULT);