1/*
2 *  hp_accel.c - Interface between LIS3LV02DL driver and HP ACPI BIOS
3 *
4 *  Copyright (C) 2007-2008 Yan Burman
5 *  Copyright (C) 2008 Eric Piel
6 *  Copyright (C) 2008-2009 Pavel Machek
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation; either version 2 of the License, or
11 *  (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *  GNU General Public License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/dmi.h>
28#include <linux/module.h>
29#include <linux/types.h>
30#include <linux/platform_device.h>
31#include <linux/interrupt.h>
32#include <linux/delay.h>
33#include <linux/wait.h>
34#include <linux/poll.h>
35#include <linux/freezer.h>
36#include <linux/uaccess.h>
37#include <linux/leds.h>
38#include <linux/atomic.h>
39#include <linux/acpi.h>
40#include <linux/i8042.h>
41#include <linux/serio.h>
42#include "../../misc/lis3lv02d/lis3lv02d.h"
43
44#define DRIVER_NAME     "hp_accel"
45#define ACPI_MDPS_CLASS "accelerometer"
46
47/* Delayed LEDs infrastructure ------------------------------------ */
48
49/* Special LED class that can defer work */
50struct delayed_led_classdev {
51	struct led_classdev led_classdev;
52	struct work_struct work;
53	enum led_brightness new_brightness;
54
55	unsigned int led;		/* For driver */
56	void (*set_brightness)(struct delayed_led_classdev *data, enum led_brightness value);
57};
58
59static inline void delayed_set_status_worker(struct work_struct *work)
60{
61	struct delayed_led_classdev *data =
62			container_of(work, struct delayed_led_classdev, work);
63
64	data->set_brightness(data, data->new_brightness);
65}
66
67static inline void delayed_sysfs_set(struct led_classdev *led_cdev,
68			      enum led_brightness brightness)
69{
70	struct delayed_led_classdev *data = container_of(led_cdev,
71			     struct delayed_led_classdev, led_classdev);
72	data->new_brightness = brightness;
73	schedule_work(&data->work);
74}
75
76/* HP-specific accelerometer driver ------------------------------------ */
77
78/* e0 25, e0 26, e0 27, e0 28 are scan codes that the accelerometer with acpi id
79 * HPQ6000 sends through the keyboard bus */
80#define ACCEL_1 0x25
81#define ACCEL_2 0x26
82#define ACCEL_3 0x27
83#define ACCEL_4 0x28
84
85/* For automatic insertion of the module */
86static const struct acpi_device_id lis3lv02d_device_ids[] = {
87	{"HPQ0004", 0}, /* HP Mobile Data Protection System PNP */
88	{"HPQ6000", 0}, /* HP Mobile Data Protection System PNP */
89	{"HPQ6007", 0}, /* HP Mobile Data Protection System PNP */
90	{"", 0},
91};
92MODULE_DEVICE_TABLE(acpi, lis3lv02d_device_ids);
93
94
95/**
96 * lis3lv02d_acpi_init - ACPI _INI method: initialize the device.
97 * @lis3: pointer to the device struct
98 *
99 * Returns 0 on success.
100 */
101static int lis3lv02d_acpi_init(struct lis3lv02d *lis3)
102{
103	struct acpi_device *dev = lis3->bus_priv;
104	if (acpi_evaluate_object(dev->handle, METHOD_NAME__INI,
105				 NULL, NULL) != AE_OK)
106		return -EINVAL;
107
108	return 0;
109}
110
111/**
112 * lis3lv02d_acpi_read - ACPI ALRD method: read a register
113 * @lis3: pointer to the device struct
114 * @reg:    the register to read
115 * @ret:    result of the operation
116 *
117 * Returns 0 on success.
118 */
119static int lis3lv02d_acpi_read(struct lis3lv02d *lis3, int reg, u8 *ret)
120{
121	struct acpi_device *dev = lis3->bus_priv;
122	union acpi_object arg0 = { ACPI_TYPE_INTEGER };
123	struct acpi_object_list args = { 1, &arg0 };
124	unsigned long long lret;
125	acpi_status status;
126
127	arg0.integer.value = reg;
128
129	status = acpi_evaluate_integer(dev->handle, "ALRD", &args, &lret);
130	*ret = lret;
131	return (status != AE_OK) ? -EINVAL : 0;
132}
133
134/**
135 * lis3lv02d_acpi_write - ACPI ALWR method: write to a register
136 * @lis3: pointer to the device struct
137 * @reg:    the register to write to
138 * @val:    the value to write
139 *
140 * Returns 0 on success.
141 */
142static int lis3lv02d_acpi_write(struct lis3lv02d *lis3, int reg, u8 val)
143{
144	struct acpi_device *dev = lis3->bus_priv;
145	unsigned long long ret; /* Not used when writting */
146	union acpi_object in_obj[2];
147	struct acpi_object_list args = { 2, in_obj };
148
149	in_obj[0].type          = ACPI_TYPE_INTEGER;
150	in_obj[0].integer.value = reg;
151	in_obj[1].type          = ACPI_TYPE_INTEGER;
152	in_obj[1].integer.value = val;
153
154	if (acpi_evaluate_integer(dev->handle, "ALWR", &args, &ret) != AE_OK)
155		return -EINVAL;
156
157	return 0;
158}
159
160static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
161{
162	lis3_dev.ac = *((union axis_conversion *)dmi->driver_data);
163	pr_info("hardware type %s found\n", dmi->ident);
164
165	return 1;
166}
167
168/* Represents, for each axis seen by userspace, the corresponding hw axis (+1).
169 * If the value is negative, the opposite of the hw value is used. */
170#define DEFINE_CONV(name, x, y, z)			      \
171	static union axis_conversion lis3lv02d_axis_##name = \
172		{ .as_array = { x, y, z } }
173DEFINE_CONV(normal, 1, 2, 3);
174DEFINE_CONV(y_inverted, 1, -2, 3);
175DEFINE_CONV(x_inverted, -1, 2, 3);
176DEFINE_CONV(z_inverted, 1, 2, -3);
177DEFINE_CONV(xy_swap, 2, 1, 3);
178DEFINE_CONV(xy_rotated_left, -2, 1, 3);
179DEFINE_CONV(xy_rotated_left_usd, -2, 1, -3);
180DEFINE_CONV(xy_swap_inverted, -2, -1, 3);
181DEFINE_CONV(xy_rotated_right, 2, -1, 3);
182DEFINE_CONV(xy_swap_yz_inverted, 2, -1, -3);
183
184#define AXIS_DMI_MATCH(_ident, _name, _axis) {		\
185	.ident = _ident,				\
186	.callback = lis3lv02d_dmi_matched,		\
187	.matches = {					\
188		DMI_MATCH(DMI_PRODUCT_NAME, _name)	\
189	},						\
190	.driver_data = &lis3lv02d_axis_##_axis		\
191}
192
193#define AXIS_DMI_MATCH2(_ident, _class1, _name1,	\
194				_class2, _name2,	\
195				_axis) {		\
196	.ident = _ident,				\
197	.callback = lis3lv02d_dmi_matched,		\
198	.matches = {					\
199		DMI_MATCH(DMI_##_class1, _name1),	\
200		DMI_MATCH(DMI_##_class2, _name2),	\
201	},						\
202	.driver_data = &lis3lv02d_axis_##_axis		\
203}
204static const struct dmi_system_id lis3lv02d_dmi_ids[] = {
205	/* product names are truncated to match all kinds of a same model */
206	AXIS_DMI_MATCH("NC64x0", "HP Compaq nc64", x_inverted),
207	AXIS_DMI_MATCH("NC84x0", "HP Compaq nc84", z_inverted),
208	AXIS_DMI_MATCH("NX9420", "HP Compaq nx9420", x_inverted),
209	AXIS_DMI_MATCH("NW9440", "HP Compaq nw9440", x_inverted),
210	AXIS_DMI_MATCH("NC2510", "HP Compaq 2510", y_inverted),
211	AXIS_DMI_MATCH("NC2710", "HP Compaq 2710", xy_swap),
212	AXIS_DMI_MATCH("NC8510", "HP Compaq 8510", xy_swap_inverted),
213	AXIS_DMI_MATCH("HP2133", "HP 2133", xy_rotated_left),
214	AXIS_DMI_MATCH("HP2140", "HP 2140", xy_swap_inverted),
215	AXIS_DMI_MATCH("NC653x", "HP Compaq 653", xy_rotated_left_usd),
216	AXIS_DMI_MATCH("NC6730b", "HP Compaq 6730b", xy_rotated_left_usd),
217	AXIS_DMI_MATCH("NC6730s", "HP Compaq 6730s", xy_swap),
218	AXIS_DMI_MATCH("NC651xx", "HP Compaq 651", xy_rotated_right),
219	AXIS_DMI_MATCH("NC6710x", "HP Compaq 6710", xy_swap_yz_inverted),
220	AXIS_DMI_MATCH("NC6715x", "HP Compaq 6715", y_inverted),
221	AXIS_DMI_MATCH("NC693xx", "HP EliteBook 693", xy_rotated_right),
222	AXIS_DMI_MATCH("NC693xx", "HP EliteBook 853", xy_swap),
223	AXIS_DMI_MATCH("NC854xx", "HP EliteBook 854", y_inverted),
224	AXIS_DMI_MATCH("NC273xx", "HP EliteBook 273", y_inverted),
225	/* Intel-based HP Pavilion dv5 */
226	AXIS_DMI_MATCH2("HPDV5_I",
227			PRODUCT_NAME, "HP Pavilion dv5",
228			BOARD_NAME, "3603",
229			x_inverted),
230	/* AMD-based HP Pavilion dv5 */
231	AXIS_DMI_MATCH2("HPDV5_A",
232			PRODUCT_NAME, "HP Pavilion dv5",
233			BOARD_NAME, "3600",
234			y_inverted),
235	AXIS_DMI_MATCH("DV7", "HP Pavilion dv7", x_inverted),
236	AXIS_DMI_MATCH("HP8710", "HP Compaq 8710", y_inverted),
237	AXIS_DMI_MATCH("HDX18", "HP HDX 18", x_inverted),
238	AXIS_DMI_MATCH("HPB432x", "HP ProBook 432", xy_rotated_left),
239	AXIS_DMI_MATCH("HPB442x", "HP ProBook 442", xy_rotated_left),
240	AXIS_DMI_MATCH("HPB452x", "HP ProBook 452", y_inverted),
241	AXIS_DMI_MATCH("HPB522x", "HP ProBook 522", xy_swap),
242	AXIS_DMI_MATCH("HPB532x", "HP ProBook 532", y_inverted),
243	AXIS_DMI_MATCH("HPB655x", "HP ProBook 655", xy_swap_inverted),
244	AXIS_DMI_MATCH("Mini510x", "HP Mini 510", xy_rotated_left_usd),
245	AXIS_DMI_MATCH("HPB63xx", "HP ProBook 63", xy_swap),
246	AXIS_DMI_MATCH("HPB64xx", "HP ProBook 64", xy_swap),
247	AXIS_DMI_MATCH("HPB64xx", "HP EliteBook 84", xy_swap),
248	AXIS_DMI_MATCH("HPB65xx", "HP ProBook 65", x_inverted),
249	AXIS_DMI_MATCH("HPZBook15", "HP ZBook 15", x_inverted),
250	{ NULL, }
251/* Laptop models without axis info (yet):
252 * "NC6910" "HP Compaq 6910"
253 * "NC2400" "HP Compaq nc2400"
254 * "NX74x0" "HP Compaq nx74"
255 * "NX6325" "HP Compaq nx6325"
256 * "NC4400" "HP Compaq nc4400"
257 */
258};
259
260static void hpled_set(struct delayed_led_classdev *led_cdev, enum led_brightness value)
261{
262	struct acpi_device *dev = lis3_dev.bus_priv;
263	unsigned long long ret; /* Not used when writing */
264	union acpi_object in_obj[1];
265	struct acpi_object_list args = { 1, in_obj };
266
267	in_obj[0].type          = ACPI_TYPE_INTEGER;
268	in_obj[0].integer.value = !!value;
269
270	acpi_evaluate_integer(dev->handle, "ALED", &args, &ret);
271}
272
273static struct delayed_led_classdev hpled_led = {
274	.led_classdev = {
275		.name			= "hp::hddprotect",
276		.default_trigger	= "none",
277		.brightness_set		= delayed_sysfs_set,
278		.flags                  = LED_CORE_SUSPENDRESUME,
279	},
280	.set_brightness = hpled_set,
281};
282
283static acpi_status
284lis3lv02d_get_resource(struct acpi_resource *resource, void *context)
285{
286	if (resource->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
287		struct acpi_resource_extended_irq *irq;
288		u32 *device_irq = context;
289
290		irq = &resource->data.extended_irq;
291		*device_irq = irq->interrupts[0];
292	}
293
294	return AE_OK;
295}
296
297static void lis3lv02d_enum_resources(struct acpi_device *device)
298{
299	acpi_status status;
300
301	status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
302					lis3lv02d_get_resource, &lis3_dev.irq);
303	if (ACPI_FAILURE(status))
304		printk(KERN_DEBUG DRIVER_NAME ": Error getting resources\n");
305}
306
307static bool hp_accel_i8042_filter(unsigned char data, unsigned char str,
308				  struct serio *port)
309{
310	static bool extended;
311
312	if (str & I8042_STR_AUXDATA)
313		return false;
314
315	if (data == 0xe0) {
316		extended = true;
317		return true;
318	} else if (unlikely(extended)) {
319		extended = false;
320
321		switch (data) {
322		case ACCEL_1:
323		case ACCEL_2:
324		case ACCEL_3:
325		case ACCEL_4:
326			return true;
327		default:
328			serio_interrupt(port, 0xe0, 0);
329			return false;
330		}
331	}
332
333	return false;
334}
335
336static int lis3lv02d_add(struct acpi_device *device)
337{
338	int ret;
339
340	if (!device)
341		return -EINVAL;
342
343	lis3_dev.bus_priv = device;
344	lis3_dev.init = lis3lv02d_acpi_init;
345	lis3_dev.read = lis3lv02d_acpi_read;
346	lis3_dev.write = lis3lv02d_acpi_write;
347	strcpy(acpi_device_name(device), DRIVER_NAME);
348	strcpy(acpi_device_class(device), ACPI_MDPS_CLASS);
349	device->driver_data = &lis3_dev;
350
351	/* obtain IRQ number of our device from ACPI */
352	lis3lv02d_enum_resources(device);
353
354	/* If possible use a "standard" axes order */
355	if (lis3_dev.ac.x && lis3_dev.ac.y && lis3_dev.ac.z) {
356		pr_info("Using custom axes %d,%d,%d\n",
357			lis3_dev.ac.x, lis3_dev.ac.y, lis3_dev.ac.z);
358	} else if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
359		pr_info("laptop model unknown, using default axes configuration\n");
360		lis3_dev.ac = lis3lv02d_axis_normal;
361	}
362
363	/* call the core layer do its init */
364	ret = lis3lv02d_init_device(&lis3_dev);
365	if (ret)
366		return ret;
367
368	/* filter to remove HPQ6000 accelerometer data
369	 * from keyboard bus stream */
370	if (strstr(dev_name(&device->dev), "HPQ6000"))
371		i8042_install_filter(hp_accel_i8042_filter);
372
373	INIT_WORK(&hpled_led.work, delayed_set_status_worker);
374	ret = led_classdev_register(NULL, &hpled_led.led_classdev);
375	if (ret) {
376		lis3lv02d_joystick_disable(&lis3_dev);
377		lis3lv02d_poweroff(&lis3_dev);
378		flush_work(&hpled_led.work);
379		return ret;
380	}
381
382	return ret;
383}
384
385static int lis3lv02d_remove(struct acpi_device *device)
386{
387	if (!device)
388		return -EINVAL;
389
390	i8042_remove_filter(hp_accel_i8042_filter);
391	lis3lv02d_joystick_disable(&lis3_dev);
392	lis3lv02d_poweroff(&lis3_dev);
393
394	led_classdev_unregister(&hpled_led.led_classdev);
395	flush_work(&hpled_led.work);
396
397	return lis3lv02d_remove_fs(&lis3_dev);
398}
399
400
401#ifdef CONFIG_PM_SLEEP
402static int lis3lv02d_suspend(struct device *dev)
403{
404	/* make sure the device is off when we suspend */
405	lis3lv02d_poweroff(&lis3_dev);
406	return 0;
407}
408
409static int lis3lv02d_resume(struct device *dev)
410{
411	lis3lv02d_poweron(&lis3_dev);
412	return 0;
413}
414
415static SIMPLE_DEV_PM_OPS(hp_accel_pm, lis3lv02d_suspend, lis3lv02d_resume);
416#define HP_ACCEL_PM (&hp_accel_pm)
417#else
418#define HP_ACCEL_PM NULL
419#endif
420
421/* For the HP MDPS aka 3D Driveguard */
422static struct acpi_driver lis3lv02d_driver = {
423	.name  = DRIVER_NAME,
424	.class = ACPI_MDPS_CLASS,
425	.ids   = lis3lv02d_device_ids,
426	.ops = {
427		.add     = lis3lv02d_add,
428		.remove  = lis3lv02d_remove,
429	},
430	.drv.pm = HP_ACCEL_PM,
431};
432module_acpi_driver(lis3lv02d_driver);
433
434MODULE_DESCRIPTION("Glue between LIS3LV02Dx and HP ACPI BIOS and support for disk protection LED.");
435MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
436MODULE_LICENSE("GPL");
437