summaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/ams/ams-pmu.c
blob: 1b01c215bfe7471c672557dd41303bee1979d46c (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
/*
 * Apple Motion Sensor driver (PMU variant)
 *
 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/adb.h>
#include <linux/pmu.h>

#include "ams.h"

/* Attitude */
#define AMS_X			0x00
#define AMS_Y			0x01
#define AMS_Z			0x02

/* Not exactly known, maybe chip vendor */
#define AMS_VENDOR		0x03

/* Freefall registers */
#define AMS_FF_CLEAR		0x04
#define AMS_FF_ENABLE		0x05
#define AMS_FF_LOW_LIMIT	0x06
#define AMS_FF_DEBOUNCE		0x07

/* Shock registers */
#define AMS_SHOCK_CLEAR		0x08
#define AMS_SHOCK_ENABLE	0x09
#define AMS_SHOCK_HIGH_LIMIT	0x0a
#define AMS_SHOCK_DEBOUNCE	0x0b

/* Global interrupt and power control register */
#define AMS_CONTROL		0x0c

static u8 ams_pmu_cmd;

static void ams_pmu_req_complete(struct adb_request *req)
{
	complete((struct completion *)req->arg);
}

/* Only call this function from task context */
static void ams_pmu_set_register(u8 reg, u8 value)
{
	static struct adb_request req;
	DECLARE_COMPLETION(req_complete);

	req.arg = &req_complete;
	if (pmu_request(&req, ams_pmu_req_complete, 4, ams_pmu_cmd, 0x00, reg, value))
		return;

	wait_for_completion(&req_complete);
}

/* Only call this function from task context */
static u8 ams_pmu_get_register(u8 reg)
{
	static struct adb_request req;
	DECLARE_COMPLETION(req_complete);

	req.arg = &req_complete;
	if (pmu_request(&req, ams_pmu_req_complete, 3, ams_pmu_cmd, 0x01, reg))
		return 0;

	wait_for_completion(&req_complete);

	if (req.reply_len > 0)
		return req.reply[0];
	else
		return 0;
}

/* Enables or disables the specified interrupts */
static void ams_pmu_set_irq(enum ams_irq reg, char enable)
{
	if (reg & AMS_IRQ_FREEFALL) {
		u8 val = ams_pmu_get_register(AMS_FF_ENABLE);
		if (enable)
			val |= 0x80;
		else
			val &= ~0x80;
		ams_pmu_set_register(AMS_FF_ENABLE, val);
	}

	if (reg & AMS_IRQ_SHOCK) {
		u8 val = ams_pmu_get_register(AMS_SHOCK_ENABLE);
		if (enable)
			val |= 0x80;
		else
			val &= ~0x80;
		ams_pmu_set_register(AMS_SHOCK_ENABLE, val);
	}

	if (reg & AMS_IRQ_GLOBAL) {
		u8 val = ams_pmu_get_register(AMS_CONTROL);
		if (enable)
			val |= 0x80;
		else
			val &= ~0x80;
		ams_pmu_set_register(AMS_CONTROL, val);
	}
}

static void ams_pmu_clear_irq(enum ams_irq reg)
{
	if (reg & AMS_IRQ_FREEFALL)
		ams_pmu_set_register(AMS_FF_CLEAR, 0x00);

	if (reg & AMS_IRQ_SHOCK)
		ams_pmu_set_register(AMS_SHOCK_CLEAR, 0x00);
}

static u8 ams_pmu_get_vendor(void)
{
	return ams_pmu_get_register(AMS_VENDOR);
}

static void ams_pmu_get_xyz(s8 *x, s8 *y, s8 *z)
{
	*x = ams_pmu_get_register(AMS_X);
	*y = ams_pmu_get_register(AMS_Y);
	*z = ams_pmu_get_register(AMS_Z);
}

static void ams_pmu_exit(void)
{
	/* Disable interrupts */
	ams_pmu_set_irq(AMS_IRQ_ALL, 0);

	/* Clear interrupts */
	ams_pmu_clear_irq(AMS_IRQ_ALL);

	ams_info.has_device = 0;

	printk(KERN_INFO "ams: Unloading\n");
}

int __init ams_pmu_init(struct device_node *np)
{
	const u32 *prop;
	int result;

	mutex_lock(&ams_info.lock);

	/* Set implementation stuff */
	ams_info.of_node = np;
	ams_info.exit = ams_pmu_exit;
	ams_info.get_vendor = ams_pmu_get_vendor;
	ams_info.get_xyz = ams_pmu_get_xyz;
	ams_info.clear_irq = ams_pmu_clear_irq;
	ams_info.bustype = BUS_HOST;

	/* Get PMU command, should be 0x4e, but we can never know */
	prop = get_property(ams_info.of_node, "reg", NULL);
	if (!prop) {
		result = -ENODEV;
		goto exit;
	}
	ams_pmu_cmd = ((*prop) >> 8) & 0xff;

	/* Disable interrupts */
	ams_pmu_set_irq(AMS_IRQ_ALL, 0);

	/* Clear interrupts */
	ams_pmu_clear_irq(AMS_IRQ_ALL);

	result = ams_sensor_attach();
	if (result < 0)
		goto exit;

	/* Set default values */
	ams_pmu_set_register(AMS_FF_LOW_LIMIT, 0x15);
	ams_pmu_set_register(AMS_FF_ENABLE, 0x08);
	ams_pmu_set_register(AMS_FF_DEBOUNCE, 0x14);

	ams_pmu_set_register(AMS_SHOCK_HIGH_LIMIT, 0x60);
	ams_pmu_set_register(AMS_SHOCK_ENABLE, 0x0f);
	ams_pmu_set_register(AMS_SHOCK_DEBOUNCE, 0x14);

	ams_pmu_set_register(AMS_CONTROL, 0x4f);

	/* Clear interrupts */
	ams_pmu_clear_irq(AMS_IRQ_ALL);

	ams_info.has_device = 1;

	/* Enable interrupts */
	ams_pmu_set_irq(AMS_IRQ_ALL, 1);

	printk(KERN_INFO "ams: Found PMU based motion sensor\n");

	result = 0;

exit:
	mutex_unlock(&ams_info.lock);

	return result;
}
OpenPOWER on IntegriCloud