summaryrefslogtreecommitdiffstats
path: root/drivers/platform/chrome/cros_kbd_led_backlight.c
blob: 78097c8a4966c608e881f6079798d694fe23fd73 (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
// SPDX-License-Identifier: GPL-2.0+
// Keyboard backlight LED driver for ChromeOS
//
// Copyright (C) 2012 Google, Inc.

#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/mfd/core.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>

struct keyboard_led {
	struct led_classdev cdev;
	struct cros_ec_device *ec;
};

/**
 * struct keyboard_led_drvdata - keyboard LED driver data.
 * @init:			Init function.
 * @brightness_get:		Get LED brightness level.
 * @brightness_set:		Set LED brightness level.  Must not sleep.
 * @brightness_set_blocking:	Set LED brightness level.  It can block the
 *				caller for the time required for accessing a
 *				LED device register
 * @max_brightness:		Maximum brightness.
 *
 * See struct led_classdev in include/linux/leds.h for more details.
 */
struct keyboard_led_drvdata {
	int (*init)(struct platform_device *pdev);

	enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);

	void (*brightness_set)(struct led_classdev *led_cdev,
			       enum led_brightness brightness);
	int (*brightness_set_blocking)(struct led_classdev *led_cdev,
				       enum led_brightness brightness);

	enum led_brightness max_brightness;
};

#define KEYBOARD_BACKLIGHT_MAX 100

#ifdef CONFIG_ACPI

/* Keyboard LED ACPI Device must be defined in firmware */
#define ACPI_KEYBOARD_BACKLIGHT_DEVICE	"\\_SB.KBLT"
#define ACPI_KEYBOARD_BACKLIGHT_READ	ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBQC"
#define ACPI_KEYBOARD_BACKLIGHT_WRITE	ACPI_KEYBOARD_BACKLIGHT_DEVICE ".KBCM"

static void keyboard_led_set_brightness_acpi(struct led_classdev *cdev,
					     enum led_brightness brightness)
{
	union acpi_object param;
	struct acpi_object_list input;
	acpi_status status;

	param.type = ACPI_TYPE_INTEGER;
	param.integer.value = brightness;
	input.count = 1;
	input.pointer = &param;

	status = acpi_evaluate_object(NULL, ACPI_KEYBOARD_BACKLIGHT_WRITE,
				      &input, NULL);
	if (ACPI_FAILURE(status))
		dev_err(cdev->dev, "Error setting keyboard LED value: %d\n",
			status);
}

static enum led_brightness
keyboard_led_get_brightness_acpi(struct led_classdev *cdev)
{
	unsigned long long brightness;
	acpi_status status;

	status = acpi_evaluate_integer(NULL, ACPI_KEYBOARD_BACKLIGHT_READ,
				       NULL, &brightness);
	if (ACPI_FAILURE(status)) {
		dev_err(cdev->dev, "Error getting keyboard LED value: %d\n",
			status);
		return -EIO;
	}

	return brightness;
}

static int keyboard_led_init_acpi(struct platform_device *pdev)
{
	acpi_handle handle;
	acpi_status status;

	/* Look for the keyboard LED ACPI Device */
	status = acpi_get_handle(ACPI_ROOT_OBJECT,
				 ACPI_KEYBOARD_BACKLIGHT_DEVICE,
				 &handle);
	if (ACPI_FAILURE(status)) {
		dev_err(&pdev->dev, "Unable to find ACPI device %s: %d\n",
			ACPI_KEYBOARD_BACKLIGHT_DEVICE, status);
		return -ENXIO;
	}

	return 0;
}

static const struct keyboard_led_drvdata keyboard_led_drvdata_acpi = {
	.init = keyboard_led_init_acpi,
	.brightness_set = keyboard_led_set_brightness_acpi,
	.brightness_get = keyboard_led_get_brightness_acpi,
	.max_brightness = KEYBOARD_BACKLIGHT_MAX,
};

#endif /* CONFIG_ACPI */

#if IS_ENABLED(CONFIG_CROS_EC)

static int
keyboard_led_set_brightness_ec_pwm(struct led_classdev *cdev,
				   enum led_brightness brightness)
{
	struct {
		struct cros_ec_command msg;
		struct ec_params_pwm_set_keyboard_backlight params;
	} __packed buf;
	struct ec_params_pwm_set_keyboard_backlight *params = &buf.params;
	struct cros_ec_command *msg = &buf.msg;
	struct keyboard_led *keyboard_led = container_of(cdev, struct keyboard_led, cdev);

	memset(&buf, 0, sizeof(buf));

	msg->command = EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT;
	msg->outsize = sizeof(*params);

	params->percent = brightness;

	return cros_ec_cmd_xfer_status(keyboard_led->ec, msg);
}

static enum led_brightness
keyboard_led_get_brightness_ec_pwm(struct led_classdev *cdev)
{
	struct {
		struct cros_ec_command msg;
		struct ec_response_pwm_get_keyboard_backlight resp;
	} __packed buf;
	struct ec_response_pwm_get_keyboard_backlight *resp = &buf.resp;
	struct cros_ec_command *msg = &buf.msg;
	struct keyboard_led *keyboard_led = container_of(cdev, struct keyboard_led, cdev);
	int ret;

	memset(&buf, 0, sizeof(buf));

	msg->command = EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT;
	msg->insize = sizeof(*resp);

	ret = cros_ec_cmd_xfer_status(keyboard_led->ec, msg);
	if (ret < 0)
		return ret;

	return resp->percent;
}

static int keyboard_led_init_ec_pwm(struct platform_device *pdev)
{
	struct keyboard_led *keyboard_led = platform_get_drvdata(pdev);

	keyboard_led->ec = dev_get_drvdata(pdev->dev.parent);
	if (!keyboard_led->ec) {
		dev_err(&pdev->dev, "no parent EC device\n");
		return -EINVAL;
	}

	return 0;
}

static const __maybe_unused struct keyboard_led_drvdata keyboard_led_drvdata_ec_pwm = {
	.init = keyboard_led_init_ec_pwm,
	.brightness_set_blocking = keyboard_led_set_brightness_ec_pwm,
	.brightness_get = keyboard_led_get_brightness_ec_pwm,
	.max_brightness = KEYBOARD_BACKLIGHT_MAX,
};

#else /* IS_ENABLED(CONFIG_CROS_EC) */

static const __maybe_unused struct keyboard_led_drvdata keyboard_led_drvdata_ec_pwm = {};

#endif /* IS_ENABLED(CONFIG_CROS_EC) */

#if IS_ENABLED(CONFIG_MFD_CROS_EC_DEV)
static int keyboard_led_init_ec_pwm_mfd(struct platform_device *pdev)
{
	struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
	struct cros_ec_device *cros_ec = ec_dev->ec_dev;
	struct keyboard_led *keyboard_led = platform_get_drvdata(pdev);

	keyboard_led->ec = cros_ec;

	return 0;
}

static const struct keyboard_led_drvdata keyboard_led_drvdata_ec_pwm_mfd = {
	.init = keyboard_led_init_ec_pwm_mfd,
	.brightness_set_blocking = keyboard_led_set_brightness_ec_pwm,
	.brightness_get = keyboard_led_get_brightness_ec_pwm,
	.max_brightness = KEYBOARD_BACKLIGHT_MAX,
};

#else /* IS_ENABLED(CONFIG_MFD_CROS_EC_DEV) */

static const struct keyboard_led_drvdata keyboard_led_drvdata_ec_pwm_mfd = {};

#endif /* IS_ENABLED(CONFIG_MFD_CROS_EC_DEV) */

static int keyboard_led_is_mfd_device(struct platform_device *pdev)
{
	return IS_ENABLED(CONFIG_MFD_CROS_EC_DEV) && mfd_get_cell(pdev);
}

static int keyboard_led_probe(struct platform_device *pdev)
{
	const struct keyboard_led_drvdata *drvdata;
	struct keyboard_led *keyboard_led;
	int error;

	if (keyboard_led_is_mfd_device(pdev))
		drvdata = &keyboard_led_drvdata_ec_pwm_mfd;
	else
		drvdata = device_get_match_data(&pdev->dev);
	if (!drvdata)
		return -EINVAL;

	keyboard_led = devm_kzalloc(&pdev->dev, sizeof(*keyboard_led), GFP_KERNEL);
	if (!keyboard_led)
		return -ENOMEM;
	platform_set_drvdata(pdev, keyboard_led);

	if (drvdata->init) {
		error = drvdata->init(pdev);
		if (error)
			return error;
	}

	keyboard_led->cdev.name = "chromeos::kbd_backlight";
	keyboard_led->cdev.flags |= LED_CORE_SUSPENDRESUME | LED_REJECT_NAME_CONFLICT;
	keyboard_led->cdev.max_brightness = drvdata->max_brightness;
	keyboard_led->cdev.brightness_set = drvdata->brightness_set;
	keyboard_led->cdev.brightness_set_blocking = drvdata->brightness_set_blocking;
	keyboard_led->cdev.brightness_get = drvdata->brightness_get;

	error = devm_led_classdev_register(&pdev->dev, &keyboard_led->cdev);
	if (error == -EEXIST) /* Already bound via other mechanism */
		return -ENODEV;
	if (error)
		return error;

	return 0;
}

#ifdef CONFIG_ACPI
static const struct acpi_device_id keyboard_led_acpi_match[] = {
	{ "GOOG0002", (kernel_ulong_t)&keyboard_led_drvdata_acpi },
	{ }
};
MODULE_DEVICE_TABLE(acpi, keyboard_led_acpi_match);
#endif

#ifdef CONFIG_OF
static const struct of_device_id keyboard_led_of_match[] = {
	{
		.compatible = "google,cros-kbd-led-backlight",
		.data = &keyboard_led_drvdata_ec_pwm,
	},
	{}
};
MODULE_DEVICE_TABLE(of, keyboard_led_of_match);
#endif

static const struct platform_device_id keyboard_led_id[] = {
	{ "cros-keyboard-leds", 0 },
	{}
};
MODULE_DEVICE_TABLE(platform, keyboard_led_id);

static struct platform_driver keyboard_led_driver = {
	.driver		= {
		.name	= "cros-keyboard-leds",
		.acpi_match_table = ACPI_PTR(keyboard_led_acpi_match),
		.of_match_table = of_match_ptr(keyboard_led_of_match),
	},
	.probe		= keyboard_led_probe,
	.id_table	= keyboard_led_id,
};
module_platform_driver(keyboard_led_driver);

MODULE_AUTHOR("Simon Que <sque@chromium.org>");
MODULE_DESCRIPTION("ChromeOS Keyboard backlight LED Driver");
MODULE_LICENSE("GPL");