summaryrefslogtreecommitdiff
path: root/common/keyboard_backlight.c
blob: 82312e07765056df9d37f863b8e706126d61a399 (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
/* Copyright 2018 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.
 */

#include "console.h"
#include "ec_commands.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "keyboard_backlight.h"
#include "lid_switch.h"
#include "timer.h"
#include "util.h"

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

static struct kblight_conf kblight;
static int current_percent;

void __attribute__((weak)) board_kblight_init(void)
{ }

static int kblight_init(void)
{
	if (!kblight.drv || !kblight.drv->init)
		return EC_ERROR_UNIMPLEMENTED;
	return kblight.drv->init();
}

static void kblight_set_deferred(void)
{
	if (!kblight.drv || !kblight.drv->set)
		return;
	kblight.drv->set(current_percent);
}
DECLARE_DEFERRED(kblight_set_deferred);

/*
 * APIs
 */
int kblight_set(int percent)
{
	if (percent < 0 || 100 < percent)
		return EC_ERROR_INVAL;
	current_percent = percent;
	/* Need to defer i2c in case it's called from an interrupt handler. */
	hook_call_deferred(&kblight_set_deferred_data, 0);
	return EC_SUCCESS;
}

int kblight_get(void)
{
	return current_percent;
}

int kblight_enable(int enable)
{
#ifdef GPIO_EN_KEYBOARD_BACKLIGHT
	gpio_set_level(GPIO_EN_KEYBOARD_BACKLIGHT, enable);
#endif
	if (!kblight.drv || !kblight.drv->enable)
		return -1;
	return kblight.drv->enable(enable);
}

int kblight_register(const struct kblight_drv *drv)
{
	kblight.drv = drv;
	CPRINTS("kblight registered");
	return EC_SUCCESS;
}

/*
 * Hooks
 */
static void keyboard_backlight_init(void)
{
	/* Uses PWM by default. Can be customized by board_kblight_init */
#ifdef CONFIG_PWM_KBLIGHT
	kblight_register(&kblight_pwm);
#endif
	board_kblight_init();
	if (kblight_init())
		CPRINTS("kblight init failed");
	/* Don't leave kblight enable state undetermined */
	kblight_enable(0);
}
DECLARE_HOOK(HOOK_INIT, keyboard_backlight_init, HOOK_PRIO_DEFAULT);

static void kblight_suspend(void)
{
	kblight_enable(0);
}
DECLARE_HOOK(HOOK_CHIPSET_SUSPEND, kblight_suspend, HOOK_PRIO_DEFAULT);

static void kblight_resume(void)
{
	if (lid_is_open() && current_percent) {
		kblight_enable(1);
		kblight_set(current_percent);
	}
}
DECLARE_HOOK(HOOK_CHIPSET_RESUME, kblight_resume, HOOK_PRIO_DEFAULT);

static void kblight_lid_change(void)
{
	kblight_enable(lid_is_open() && current_percent);
}
DECLARE_HOOK(HOOK_LID_CHANGE, kblight_lid_change, HOOK_PRIO_DEFAULT);

/*
 * Console and host commands
 */
static int cc_kblight(int argc, char **argv)
{
	if (argc >= 2) {
		char *e;
		int i = strtoi(argv[1], &e, 0);
		if (*e)
			return EC_ERROR_PARAM1;
		if (kblight_set(i))
			return EC_ERROR_PARAM1;
		if (kblight_enable(i > 0))
			return EC_ERROR_PARAM1;
	}
	ccprintf("Keyboard backlight: %d%%\n", kblight_get());
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(kblight, cc_kblight,
			"percent",
			"Get/set keyboard backlight");

enum ec_status hc_get_keyboard_backlight(struct host_cmd_handler_args *args)
{
	struct ec_response_pwm_get_keyboard_backlight *r = args->response;

	r->percent = kblight_get();
	r->enabled = 1;			/* Deprecated */
	args->response_size = sizeof(*r);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT,
		     hc_get_keyboard_backlight,
		     EC_VER_MASK(0));

enum ec_status hc_set_keyboard_backlight(struct host_cmd_handler_args *args)
{
	const struct ec_params_pwm_set_keyboard_backlight *p = args->params;

	if (kblight_set(p->percent))
		return EC_RES_ERROR;
	if (kblight_enable(p->percent > 0))
		return EC_RES_ERROR;
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT,
		     hc_set_keyboard_backlight,
		     EC_VER_MASK(0));