aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStelian Pop <stelian@popies.net>2006-12-12 12:18:30 -0500
committerJean Delvare <khali@arrakis.delvare>2006-12-12 12:18:30 -0500
commitdcb69dd010c182507a8db8940618f6413f65e0af (patch)
treeb0a2d0795866d7d787af82be6041cf3cabb6c640
parent61db011d403388b2dd342489d7110cf13cb99025 (diff)
hwmon: New AMS hardware monitoring driver
This driver adds support for the Apple Motion Sensor (AMS) as found in 2005 revisions of Apple PowerBooks and iBooks. It implements both the PMU and I2C variants. The I2C driver and mouse emulation is based on code by Stelian Pop, while the PMU driver has been developped by Michael Hanselmann. HD parking support will be added later. Various people contributed fixes to this driver, including Aristeu Sergio Rozanski Filho and Jean Delvare. Signed-off-by: Stelian Pop <stelian@popies.net> Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Acked-by: Robert Love <rml@novell.com> Signed-off-by: Jean Delvare <khali@linux-fr.org>
-rw-r--r--drivers/hwmon/Kconfig25
-rw-r--r--drivers/hwmon/Makefile1
-rw-r--r--drivers/hwmon/ams/Makefile8
-rw-r--r--drivers/hwmon/ams/ams-core.c265
-rw-r--r--drivers/hwmon/ams/ams-i2c.c299
-rw-r--r--drivers/hwmon/ams/ams-input.c160
-rw-r--r--drivers/hwmon/ams/ams-pmu.c207
-rw-r--r--drivers/hwmon/ams/ams.h72
8 files changed, 1037 insertions, 0 deletions
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 23b7eac563b3..891ef6d0b1bf 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -106,6 +106,31 @@ config SENSORS_K8TEMP
106 This driver can also be built as a module. If so, the module 106 This driver can also be built as a module. If so, the module
107 will be called k8temp. 107 will be called k8temp.
108 108
109config SENSORS_AMS
110 tristate "Apple Motion Sensor driver"
111 depends on HWMON && PPC_PMAC && !PPC64 && INPUT && ((ADB_PMU && I2C = y) || (ADB_PMU && !I2C) || I2C) && EXPERIMENTAL
112 help
113 Support for the motion sensor included in PowerBooks. Includes
114 implementations for PMU and I2C.
115
116 This driver can also be built as a module. If so, the module
117 will be called ams.
118
119config SENSORS_AMS_PMU
120 bool "PMU variant"
121 depends on SENSORS_AMS && ADB_PMU
122 default y
123 help
124 PMU variant of motion sensor, found in late 2005 PowerBooks.
125
126config SENSORS_AMS_I2C
127 bool "I2C variant"
128 depends on SENSORS_AMS && I2C
129 default y
130 help
131 I2C variant of motion sensor, found in early 2005 PowerBooks and
132 iBooks.
133
109config SENSORS_ASB100 134config SENSORS_ASB100
110 tristate "Asus ASB100 Bach" 135 tristate "Asus ASB100 Bach"
111 depends on HWMON && I2C && EXPERIMENTAL 136 depends on HWMON && I2C && EXPERIMENTAL
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index 8b0a8b54a366..31661124271e 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -19,6 +19,7 @@ obj-$(CONFIG_SENSORS_ADM1025) += adm1025.o
19obj-$(CONFIG_SENSORS_ADM1026) += adm1026.o 19obj-$(CONFIG_SENSORS_ADM1026) += adm1026.o
20obj-$(CONFIG_SENSORS_ADM1031) += adm1031.o 20obj-$(CONFIG_SENSORS_ADM1031) += adm1031.o
21obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o 21obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o
22obj-$(CONFIG_SENSORS_AMS) += ams/
22obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o 23obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o
23obj-$(CONFIG_SENSORS_DS1621) += ds1621.o 24obj-$(CONFIG_SENSORS_DS1621) += ds1621.o
24obj-$(CONFIG_SENSORS_F71805F) += f71805f.o 25obj-$(CONFIG_SENSORS_F71805F) += f71805f.o
diff --git a/drivers/hwmon/ams/Makefile b/drivers/hwmon/ams/Makefile
new file mode 100644
index 000000000000..41c95b2089dc
--- /dev/null
+++ b/drivers/hwmon/ams/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for Apple Motion Sensor driver
3#
4
5ams-y := ams-core.o ams-input.o
6ams-$(CONFIG_SENSORS_AMS_PMU) += ams-pmu.o
7ams-$(CONFIG_SENSORS_AMS_I2C) += ams-i2c.o
8obj-$(CONFIG_SENSORS_AMS) += ams.o
diff --git a/drivers/hwmon/ams/ams-core.c b/drivers/hwmon/ams/ams-core.c
new file mode 100644
index 000000000000..f1f0f5d0442c
--- /dev/null
+++ b/drivers/hwmon/ams/ams-core.c
@@ -0,0 +1,265 @@
1/*
2 * Apple Motion Sensor driver
3 *
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <asm/pmac_pfunc.h>
28#include <asm/of_platform.h>
29
30#include "ams.h"
31
32/* There is only one motion sensor per machine */
33struct ams ams_info;
34
35static unsigned int verbose;
36module_param(verbose, bool, 0644);
37MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");
38
39/* Call with ams_info.lock held! */
40void ams_sensors(s8 *x, s8 *y, s8 *z)
41{
42 u32 orient = ams_info.vflag? ams_info.orient1 : ams_info.orient2;
43
44 if (orient & 0x80)
45 /* X and Y swapped */
46 ams_info.get_xyz(y, x, z);
47 else
48 ams_info.get_xyz(x, y, z);
49
50 if (orient & 0x04)
51 *z = ~(*z);
52 if (orient & 0x02)
53 *y = ~(*y);
54 if (orient & 0x01)
55 *x = ~(*x);
56}
57
58static ssize_t ams_show_current(struct device *dev,
59 struct device_attribute *attr, char *buf)
60{
61 s8 x, y, z;
62
63 mutex_lock(&ams_info.lock);
64 ams_sensors(&x, &y, &z);
65 mutex_unlock(&ams_info.lock);
66
67 return snprintf(buf, PAGE_SIZE, "%d %d %d\n", x, y, z);
68}
69
70static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);
71
72static void ams_handle_irq(void *data)
73{
74 enum ams_irq irq = *((enum ams_irq *)data);
75
76 spin_lock(&ams_info.irq_lock);
77
78 ams_info.worker_irqs |= irq;
79 schedule_work(&ams_info.worker);
80
81 spin_unlock(&ams_info.irq_lock);
82}
83
84static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;
85static struct pmf_irq_client ams_freefall_client = {
86 .owner = THIS_MODULE,
87 .handler = ams_handle_irq,
88 .data = &ams_freefall_irq_data,
89};
90
91static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;
92static struct pmf_irq_client ams_shock_client = {
93 .owner = THIS_MODULE,
94 .handler = ams_handle_irq,
95 .data = &ams_shock_irq_data,
96};
97
98/* Once hard disk parking is implemented in the kernel, this function can
99 * trigger it.
100 */
101static void ams_worker(struct work_struct *work)
102{
103 mutex_lock(&ams_info.lock);
104
105 if (ams_info.has_device) {
106 unsigned long flags;
107
108 spin_lock_irqsave(&ams_info.irq_lock, flags);
109
110 if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
111 if (verbose)
112 printk(KERN_INFO "ams: freefall detected!\n");
113
114 ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;
115
116 /* we must call this with interrupts enabled */
117 spin_unlock_irqrestore(&ams_info.irq_lock, flags);
118 ams_info.clear_irq(AMS_IRQ_FREEFALL);
119 spin_lock_irqsave(&ams_info.irq_lock, flags);
120 }
121
122 if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
123 if (verbose)
124 printk(KERN_INFO "ams: shock detected!\n");
125
126 ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
127
128 /* we must call this with interrupts enabled */
129 spin_unlock_irqrestore(&ams_info.irq_lock, flags);
130 ams_info.clear_irq(AMS_IRQ_SHOCK);
131 spin_lock_irqsave(&ams_info.irq_lock, flags);
132 }
133
134 spin_unlock_irqrestore(&ams_info.irq_lock, flags);
135 }
136
137 mutex_unlock(&ams_info.lock);
138}
139
140/* Call with ams_info.lock held! */
141int ams_sensor_attach(void)
142{
143 int result;
144 u32 *prop;
145
146 /* Get orientation */
147 prop = (u32*)get_property(ams_info.of_node, "orientation", NULL);
148 if (!prop)
149 return -ENODEV;
150 ams_info.orient1 = *prop;
151 ams_info.orient2 = *(prop + 1);
152
153 /* Register freefall interrupt handler */
154 result = pmf_register_irq_client(ams_info.of_node,
155 "accel-int-1",
156 &ams_freefall_client);
157 if (result < 0)
158 return -ENODEV;
159
160 /* Reset saved irqs */
161 ams_info.worker_irqs = 0;
162
163 /* Register shock interrupt handler */
164 result = pmf_register_irq_client(ams_info.of_node,
165 "accel-int-2",
166 &ams_shock_client);
167 if (result < 0)
168 goto release_freefall;
169
170 /* Create device */
171 ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
172 if (!ams_info.of_dev) {
173 result = -ENODEV;
174 goto release_shock;
175 }
176
177 /* Create attributes */
178 result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
179 if (result)
180 goto release_of;
181
182 ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
183
184 /* Init input device */
185 result = ams_input_init();
186 if (result)
187 goto release_device_file;
188
189 return result;
190release_device_file:
191 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
192release_of:
193 of_device_unregister(ams_info.of_dev);
194release_shock:
195 pmf_unregister_irq_client(&ams_shock_client);
196release_freefall:
197 pmf_unregister_irq_client(&ams_freefall_client);
198 return result;
199}
200
201int __init ams_init(void)
202{
203 struct device_node *np;
204
205 spin_lock_init(&ams_info.irq_lock);
206 mutex_init(&ams_info.lock);
207 INIT_WORK(&ams_info.worker, ams_worker);
208
209#ifdef CONFIG_SENSORS_AMS_I2C
210 np = of_find_node_by_name(NULL, "accelerometer");
211 if (np && device_is_compatible(np, "AAPL,accelerometer_1"))
212 /* Found I2C motion sensor */
213 return ams_i2c_init(np);
214#endif
215
216#ifdef CONFIG_SENSORS_AMS_PMU
217 np = of_find_node_by_name(NULL, "sms");
218 if (np && device_is_compatible(np, "sms"))
219 /* Found PMU motion sensor */
220 return ams_pmu_init(np);
221#endif
222
223 printk(KERN_ERR "ams: No motion sensor found.\n");
224
225 return -ENODEV;
226}
227
228void ams_exit(void)
229{
230 mutex_lock(&ams_info.lock);
231
232 if (ams_info.has_device) {
233 /* Remove input device */
234 ams_input_exit();
235
236 /* Shut down implementation */
237 ams_info.exit();
238
239 /* Flush interrupt worker
240 *
241 * We do this after ams_info.exit(), because an interrupt might
242 * have arrived before disabling them.
243 */
244 flush_scheduled_work();
245
246 /* Remove attributes */
247 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
248
249 /* Remove device */
250 of_device_unregister(ams_info.of_dev);
251
252 /* Remove handler */
253 pmf_unregister_irq_client(&ams_shock_client);
254 pmf_unregister_irq_client(&ams_freefall_client);
255 }
256
257 mutex_unlock(&ams_info.lock);
258}
259
260MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
261MODULE_DESCRIPTION("Apple Motion Sensor driver");
262MODULE_LICENSE("GPL");
263
264module_init(ams_init);
265module_exit(ams_exit);
diff --git a/drivers/hwmon/ams/ams-i2c.c b/drivers/hwmon/ams/ams-i2c.c
new file mode 100644
index 000000000000..0d24bdfea53e
--- /dev/null
+++ b/drivers/hwmon/ams/ams-i2c.c
@@ -0,0 +1,299 @@
1/*
2 * Apple Motion Sensor driver (I2C variant)
3 *
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
6 *
7 * Clean room implementation based on the reverse engineered Mac OS X driver by
8 * Johannes Berg <johannes@sipsolutions.net>, documentation available at
9 * http://johannes.sipsolutions.net/PowerBook/Apple_Motion_Sensor_Specification
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22
23#include "ams.h"
24
25/* AMS registers */
26#define AMS_COMMAND 0x00 /* command register */
27#define AMS_STATUS 0x01 /* status register */
28#define AMS_CTRL1 0x02 /* read control 1 (number of values) */
29#define AMS_CTRL2 0x03 /* read control 2 (offset?) */
30#define AMS_CTRL3 0x04 /* read control 3 (size of each value?) */
31#define AMS_DATA1 0x05 /* read data 1 */
32#define AMS_DATA2 0x06 /* read data 2 */
33#define AMS_DATA3 0x07 /* read data 3 */
34#define AMS_DATA4 0x08 /* read data 4 */
35#define AMS_DATAX 0x20 /* data X */
36#define AMS_DATAY 0x21 /* data Y */
37#define AMS_DATAZ 0x22 /* data Z */
38#define AMS_FREEFALL 0x24 /* freefall int control */
39#define AMS_SHOCK 0x25 /* shock int control */
40#define AMS_SENSLOW 0x26 /* sensitivity low limit */
41#define AMS_SENSHIGH 0x27 /* sensitivity high limit */
42#define AMS_CTRLX 0x28 /* control X */
43#define AMS_CTRLY 0x29 /* control Y */
44#define AMS_CTRLZ 0x2A /* control Z */
45#define AMS_UNKNOWN1 0x2B /* unknown 1 */
46#define AMS_UNKNOWN2 0x2C /* unknown 2 */
47#define AMS_UNKNOWN3 0x2D /* unknown 3 */
48#define AMS_VENDOR 0x2E /* vendor */
49
50/* AMS commands - use with the AMS_COMMAND register */
51enum ams_i2c_cmd {
52 AMS_CMD_NOOP = 0,
53 AMS_CMD_VERSION,
54 AMS_CMD_READMEM,
55 AMS_CMD_WRITEMEM,
56 AMS_CMD_ERASEMEM,
57 AMS_CMD_READEE,
58 AMS_CMD_WRITEEE,
59 AMS_CMD_RESET,
60 AMS_CMD_START,
61};
62
63static int ams_i2c_attach(struct i2c_adapter *adapter);
64static int ams_i2c_detach(struct i2c_adapter *adapter);
65
66static struct i2c_driver ams_i2c_driver = {
67 .driver = {
68 .name = "ams",
69 .owner = THIS_MODULE,
70 },
71 .attach_adapter = ams_i2c_attach,
72 .detach_adapter = ams_i2c_detach,
73};
74
75static s32 ams_i2c_read(u8 reg)
76{
77 return i2c_smbus_read_byte_data(&ams_info.i2c_client, reg);
78}
79
80static int ams_i2c_write(u8 reg, u8 value)
81{
82 return i2c_smbus_write_byte_data(&ams_info.i2c_client, reg, value);
83}
84
85static int ams_i2c_cmd(enum ams_i2c_cmd cmd)
86{
87 s32 result;
88 int remaining = HZ / 20;
89
90 ams_i2c_write(AMS_COMMAND, cmd);
91 mdelay(5);
92
93 while (remaining) {
94 result = ams_i2c_read(AMS_COMMAND);
95 if (result == 0 || result & 0x80)
96 return 0;
97
98 remaining = schedule_timeout(remaining);
99 }
100
101 return -1;
102}
103
104static void ams_i2c_set_irq(enum ams_irq reg, char enable)
105{
106 if (reg & AMS_IRQ_FREEFALL) {
107 u8 val = ams_i2c_read(AMS_CTRLX);
108 if (enable)
109 val |= 0x80;
110 else
111 val &= ~0x80;
112 ams_i2c_write(AMS_CTRLX, val);
113 }
114
115 if (reg & AMS_IRQ_SHOCK) {
116 u8 val = ams_i2c_read(AMS_CTRLY);
117 if (enable)
118 val |= 0x80;
119 else
120 val &= ~0x80;
121 ams_i2c_write(AMS_CTRLY, val);
122 }
123
124 if (reg & AMS_IRQ_GLOBAL) {
125 u8 val = ams_i2c_read(AMS_CTRLZ);
126 if (enable)
127 val |= 0x80;
128 else
129 val &= ~0x80;
130 ams_i2c_write(AMS_CTRLZ, val);
131 }
132}
133
134static void ams_i2c_clear_irq(enum ams_irq reg)
135{
136 if (reg & AMS_IRQ_FREEFALL)
137 ams_i2c_write(AMS_FREEFALL, 0);
138
139 if (reg & AMS_IRQ_SHOCK)
140 ams_i2c_write(AMS_SHOCK, 0);
141}
142
143static u8 ams_i2c_get_vendor(void)
144{
145 return ams_i2c_read(AMS_VENDOR);
146}
147
148static void ams_i2c_get_xyz(s8 *x, s8 *y, s8 *z)
149{
150 *x = ams_i2c_read(AMS_DATAX);
151 *y = ams_i2c_read(AMS_DATAY);
152 *z = ams_i2c_read(AMS_DATAZ);
153}
154
155static int ams_i2c_attach(struct i2c_adapter *adapter)
156{
157 unsigned long bus;
158 int vmaj, vmin;
159 int result;
160
161 /* There can be only one */
162 if (unlikely(ams_info.has_device))
163 return -ENODEV;
164
165 if (strncmp(adapter->name, "uni-n", 5))
166 return -ENODEV;
167
168 bus = simple_strtoul(adapter->name + 6, NULL, 10);
169 if (bus != ams_info.i2c_bus)
170 return -ENODEV;
171
172 ams_info.i2c_client.addr = ams_info.i2c_address;
173 ams_info.i2c_client.adapter = adapter;
174 ams_info.i2c_client.driver = &ams_i2c_driver;
175 strcpy(ams_info.i2c_client.name, "Apple Motion Sensor");
176
177 if (ams_i2c_cmd(AMS_CMD_RESET)) {
178 printk(KERN_INFO "ams: Failed to reset the device\n");
179 return -ENODEV;
180 }
181
182 if (ams_i2c_cmd(AMS_CMD_START)) {
183 printk(KERN_INFO "ams: Failed to start the device\n");
184 return -ENODEV;
185 }
186
187 /* get version/vendor information */
188 ams_i2c_write(AMS_CTRL1, 0x02);
189 ams_i2c_write(AMS_CTRL2, 0x85);
190 ams_i2c_write(AMS_CTRL3, 0x01);
191
192 ams_i2c_cmd(AMS_CMD_READMEM);
193
194 vmaj = ams_i2c_read(AMS_DATA1);
195 vmin = ams_i2c_read(AMS_DATA2);
196 if (vmaj != 1 || vmin != 52) {
197 printk(KERN_INFO "ams: Incorrect device version (%d.%d)\n",
198 vmaj, vmin);
199 return -ENODEV;
200 }
201
202 ams_i2c_cmd(AMS_CMD_VERSION);
203
204 vmaj = ams_i2c_read(AMS_DATA1);
205 vmin = ams_i2c_read(AMS_DATA2);
206 if (vmaj != 0 || vmin != 1) {
207 printk(KERN_INFO "ams: Incorrect firmware version (%d.%d)\n",
208 vmaj, vmin);
209 return -ENODEV;
210 }
211
212 /* Disable interrupts */
213 ams_i2c_set_irq(AMS_IRQ_ALL, 0);
214
215 result = ams_sensor_attach();
216 if (result < 0)
217 return result;
218
219 /* Set default values */
220 ams_i2c_write(AMS_SENSLOW, 0x15);
221 ams_i2c_write(AMS_SENSHIGH, 0x60);
222 ams_i2c_write(AMS_CTRLX, 0x08);
223 ams_i2c_write(AMS_CTRLY, 0x0F);
224 ams_i2c_write(AMS_CTRLZ, 0x4F);
225 ams_i2c_write(AMS_UNKNOWN1, 0x14);
226
227 /* Clear interrupts */
228 ams_i2c_clear_irq(AMS_IRQ_ALL);
229
230 ams_info.has_device = 1;
231
232 /* Enable interrupts */
233 ams_i2c_set_irq(AMS_IRQ_ALL, 1);
234
235 printk(KERN_INFO "ams: Found I2C based motion sensor\n");
236
237 return 0;
238}
239
240static int ams_i2c_detach(struct i2c_adapter *adapter)
241{
242 if (ams_info.has_device) {
243 /* Disable interrupts */
244 ams_i2c_set_irq(AMS_IRQ_ALL, 0);
245
246 /* Clear interrupts */
247 ams_i2c_clear_irq(AMS_IRQ_ALL);
248
249 printk(KERN_INFO "ams: Unloading\n");
250
251 ams_info.has_device = 0;
252 }
253
254 return 0;
255}
256
257static void ams_i2c_exit(void)
258{
259 i2c_del_driver(&ams_i2c_driver);
260}
261
262int __init ams_i2c_init(struct device_node *np)
263{
264 char *tmp_bus;
265 int result;
266 u32 *prop;
267
268 mutex_lock(&ams_info.lock);
269
270 /* Set implementation stuff */
271 ams_info.of_node = np;
272 ams_info.exit = ams_i2c_exit;
273 ams_info.get_vendor = ams_i2c_get_vendor;
274 ams_info.get_xyz = ams_i2c_get_xyz;
275 ams_info.clear_irq = ams_i2c_clear_irq;
276 ams_info.bustype = BUS_I2C;
277
278 /* look for bus either using "reg" or by path */
279 prop = (u32*)get_property(ams_info.of_node, "reg", NULL);
280 if (!prop) {
281 result = -ENODEV;
282
283 goto exit;
284 }
285
286 tmp_bus = strstr(ams_info.of_node->full_name, "/i2c-bus@");
287 if (tmp_bus)
288 ams_info.i2c_bus = *(tmp_bus + 9) - '0';
289 else
290 ams_info.i2c_bus = ((*prop) >> 8) & 0x0f;
291 ams_info.i2c_address = ((*prop) & 0xff) >> 1;
292
293 result = i2c_add_driver(&ams_i2c_driver);
294
295exit:
296 mutex_unlock(&ams_info.lock);
297
298 return result;
299}
diff --git a/drivers/hwmon/ams/ams-input.c b/drivers/hwmon/ams/ams-input.c
new file mode 100644
index 000000000000..f126aa485134
--- /dev/null
+++ b/drivers/hwmon/ams/ams-input.c
@@ -0,0 +1,160 @@
1/*
2 * Apple Motion Sensor driver (joystick emulation)
3 *
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/module.h>
14
15#include <linux/types.h>
16#include <linux/errno.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19
20#include "ams.h"
21
22static unsigned int joystick;
23module_param(joystick, bool, 0644);
24MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
25
26static unsigned int invert;
27module_param(invert, bool, 0644);
28MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
29
30static int ams_input_kthread(void *data)
31{
32 s8 x, y, z;
33
34 while (!kthread_should_stop()) {
35 mutex_lock(&ams_info.lock);
36
37 ams_sensors(&x, &y, &z);
38
39 x -= ams_info.xcalib;
40 y -= ams_info.ycalib;
41 z -= ams_info.zcalib;
42
43 input_report_abs(ams_info.idev, ABS_X, invert ? -x : x);
44 input_report_abs(ams_info.idev, ABS_Y, invert ? -y : y);
45 input_report_abs(ams_info.idev, ABS_Z, z);
46
47 input_sync(ams_info.idev);
48
49 mutex_unlock(&ams_info.lock);
50
51 msleep(25);
52 }
53
54 return 0;
55}
56
57static int ams_input_open(struct input_dev *dev)
58{
59 ams_info.kthread = kthread_run(ams_input_kthread, NULL, "kams");
60 return IS_ERR(ams_info.kthread) ? PTR_ERR(ams_info.kthread) : 0;
61}
62
63static void ams_input_close(struct input_dev *dev)
64{
65 kthread_stop(ams_info.kthread);
66}
67
68/* Call with ams_info.lock held! */
69static void ams_input_enable(void)
70{
71 s8 x, y, z;
72
73 if (ams_info.idev)
74 return;
75
76 ams_sensors(&x, &y, &z);
77 ams_info.xcalib = x;
78 ams_info.ycalib = y;
79 ams_info.zcalib = z;
80
81 ams_info.idev = input_allocate_device();
82 if (!ams_info.idev)
83 return;
84
85 ams_info.idev->name = "Apple Motion Sensor";
86 ams_info.idev->id.bustype = ams_info.bustype;
87 ams_info.idev->id.vendor = 0;
88 ams_info.idev->open = ams_input_open;
89 ams_info.idev->close = ams_input_close;
90 ams_info.idev->cdev.dev = &ams_info.of_dev->dev;
91
92 input_set_abs_params(ams_info.idev, ABS_X, -50, 50, 3, 0);
93 input_set_abs_params(ams_info.idev, ABS_Y, -50, 50, 3, 0);
94 input_set_abs_params(ams_info.idev, ABS_Z, -50, 50, 3, 0);
95
96 set_bit(EV_ABS, ams_info.idev->evbit);
97 set_bit(EV_KEY, ams_info.idev->evbit);
98 set_bit(BTN_TOUCH, ams_info.idev->keybit);
99
100 if (input_register_device(ams_info.idev)) {
101 input_free_device(ams_info.idev);
102 ams_info.idev = NULL;
103 return;
104 }
105}
106
107/* Call with ams_info.lock held! */
108static void ams_input_disable(void)
109{
110 if (ams_info.idev) {
111 input_unregister_device(ams_info.idev);
112 ams_info.idev = NULL;
113 }
114}
115
116static ssize_t ams_input_show_joystick(struct device *dev,
117 struct device_attribute *attr, char *buf)
118{
119 return sprintf(buf, "%d\n", joystick);
120}
121
122static ssize_t ams_input_store_joystick(struct device *dev,
123 struct device_attribute *attr, const char *buf, size_t count)
124{
125 if (sscanf(buf, "%d\n", &joystick) != 1)
126 return -EINVAL;
127
128 mutex_lock(&ams_info.lock);
129
130 if (joystick)
131 ams_input_enable();
132 else
133 ams_input_disable();
134
135 mutex_unlock(&ams_info.lock);
136
137 return count;
138}
139
140static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
141 ams_input_show_joystick, ams_input_store_joystick);
142
143/* Call with ams_info.lock held! */
144int ams_input_init(void)
145{
146 int result;
147
148 result = device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
149
150 if (!result && joystick)
151 ams_input_enable();
152 return result;
153}
154
155/* Call with ams_info.lock held! */
156void ams_input_exit()
157{
158 ams_input_disable();
159 device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
160}
diff --git a/drivers/hwmon/ams/ams-pmu.c b/drivers/hwmon/ams/ams-pmu.c
new file mode 100644
index 000000000000..4636ae031a53
--- /dev/null
+++ b/drivers/hwmon/ams/ams-pmu.c
@@ -0,0 +1,207 @@
1/*
2 * Apple Motion Sensor driver (PMU variant)
3 *
4 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/adb.h>
17#include <linux/pmu.h>
18
19#include "ams.h"
20
21/* Attitude */
22#define AMS_X 0x00
23#define AMS_Y 0x01
24#define AMS_Z 0x02
25
26/* Not exactly known, maybe chip vendor */
27#define AMS_VENDOR 0x03
28
29/* Freefall registers */
30#define AMS_FF_CLEAR 0x04
31#define AMS_FF_ENABLE 0x05
32#define AMS_FF_LOW_LIMIT 0x06
33#define AMS_FF_DEBOUNCE 0x07
34
35/* Shock registers */
36#define AMS_SHOCK_CLEAR 0x08
37#define AMS_SHOCK_ENABLE 0x09
38#define AMS_SHOCK_HIGH_LIMIT 0x0a
39#define AMS_SHOCK_DEBOUNCE 0x0b
40
41/* Global interrupt and power control register */
42#define AMS_CONTROL 0x0c
43
44static u8 ams_pmu_cmd;
45
46static void ams_pmu_req_complete(struct adb_request *req)
47{
48 complete((struct completion *)req->arg);
49}
50
51/* Only call this function from task context */
52static void ams_pmu_set_register(u8 reg, u8 value)
53{
54 static struct adb_request req;
55 DECLARE_COMPLETION(req_complete);
56
57 req.arg = &req_complete;
58 if (pmu_request(&req, ams_pmu_req_complete, 4, ams_pmu_cmd, 0x00, reg, value))
59 return;
60
61 wait_for_completion(&req_complete);
62}
63
64/* Only call this function from task context */
65static u8 ams_pmu_get_register(u8 reg)
66{
67 static struct adb_request req;
68 DECLARE_COMPLETION(req_complete);
69
70 req.arg = &req_complete;
71 if (pmu_request(&req, ams_pmu_req_complete, 3, ams_pmu_cmd, 0x01, reg))
72 return 0;
73
74 wait_for_completion(&req_complete);
75
76 if (req.reply_len > 0)
77 return req.reply[0];
78 else
79 return 0;
80}
81
82/* Enables or disables the specified interrupts */
83static void ams_pmu_set_irq(enum ams_irq reg, char enable)
84{
85 if (reg & AMS_IRQ_FREEFALL) {
86 u8 val = ams_pmu_get_register(AMS_FF_ENABLE);
87 if (enable)
88 val |= 0x80;
89 else
90 val &= ~0x80;
91 ams_pmu_set_register(AMS_FF_ENABLE, val);
92 }
93
94 if (reg & AMS_IRQ_SHOCK) {
95 u8 val = ams_pmu_get_register(AMS_SHOCK_ENABLE);
96 if (enable)
97 val |= 0x80;
98 else
99 val &= ~0x80;
100 ams_pmu_set_register(AMS_SHOCK_ENABLE, val);
101 }
102
103 if (reg & AMS_IRQ_GLOBAL) {
104 u8 val = ams_pmu_get_register(AMS_CONTROL);
105 if (enable)
106 val |= 0x80;
107 else
108 val &= ~0x80;
109 ams_pmu_set_register(AMS_CONTROL, val);
110 }
111}
112
113static void ams_pmu_clear_irq(enum ams_irq reg)
114{
115 if (reg & AMS_IRQ_FREEFALL)
116 ams_pmu_set_register(AMS_FF_CLEAR, 0x00);
117
118 if (reg & AMS_IRQ_SHOCK)
119 ams_pmu_set_register(AMS_SHOCK_CLEAR, 0x00);
120}
121
122static u8 ams_pmu_get_vendor(void)
123{
124 return ams_pmu_get_register(AMS_VENDOR);
125}
126
127static void ams_pmu_get_xyz(s8 *x, s8 *y, s8 *z)
128{
129 *x = ams_pmu_get_register(AMS_X);
130 *y = ams_pmu_get_register(AMS_Y);
131 *z = ams_pmu_get_register(AMS_Z);
132}
133
134static void ams_pmu_exit(void)
135{
136 /* Disable interrupts */
137 ams_pmu_set_irq(AMS_IRQ_ALL, 0);
138
139 /* Clear interrupts */
140 ams_pmu_clear_irq(AMS_IRQ_ALL);
141
142 ams_info.has_device = 0;
143
144 printk(KERN_INFO "ams: Unloading\n");
145}
146
147int __init ams_pmu_init(struct device_node *np)
148{
149 u32 *prop;
150 int result;
151
152 mutex_lock(&ams_info.lock);
153
154 /* Set implementation stuff */
155 ams_info.of_node = np;
156 ams_info.exit = ams_pmu_exit;
157 ams_info.get_vendor = ams_pmu_get_vendor;
158 ams_info.get_xyz = ams_pmu_get_xyz;
159 ams_info.clear_irq = ams_pmu_clear_irq;
160 ams_info.bustype = BUS_HOST;
161
162 /* Get PMU command, should be 0x4e, but we can never know */
163 prop = (u32*)get_property(ams_info.of_node, "reg", NULL);
164 if (!prop) {
165 result = -ENODEV;
166 goto exit;
167 }
168 ams_pmu_cmd = ((*prop) >> 8) & 0xff;
169
170 /* Disable interrupts */
171 ams_pmu_set_irq(AMS_IRQ_ALL, 0);
172
173 /* Clear interrupts */
174 ams_pmu_clear_irq(AMS_IRQ_ALL);
175
176 result = ams_sensor_attach();
177 if (result < 0)
178 goto exit;
179
180 /* Set default values */
181 ams_pmu_set_register(AMS_FF_LOW_LIMIT, 0x15);
182 ams_pmu_set_register(AMS_FF_ENABLE, 0x08);
183 ams_pmu_set_register(AMS_FF_DEBOUNCE, 0x14);
184
185 ams_pmu_set_register(AMS_SHOCK_HIGH_LIMIT, 0x60);
186 ams_pmu_set_register(AMS_SHOCK_ENABLE, 0x0f);
187 ams_pmu_set_register(AMS_SHOCK_DEBOUNCE, 0x14);
188
189 ams_pmu_set_register(AMS_CONTROL, 0x4f);
190
191 /* Clear interrupts */
192 ams_pmu_clear_irq(AMS_IRQ_ALL);
193
194 ams_info.has_device = 1;
195
196 /* Enable interrupts */
197 ams_pmu_set_irq(AMS_IRQ_ALL, 1);
198
199 printk(KERN_INFO "ams: Found PMU based motion sensor\n");
200
201 result = 0;
202
203exit:
204 mutex_unlock(&ams_info.lock);
205
206 return result;
207}
diff --git a/drivers/hwmon/ams/ams.h b/drivers/hwmon/ams/ams.h
new file mode 100644
index 000000000000..240730e6bcde
--- /dev/null
+++ b/drivers/hwmon/ams/ams.h
@@ -0,0 +1,72 @@
1#include <linux/i2c.h>
2#include <linux/input.h>
3#include <linux/kthread.h>
4#include <linux/mutex.h>
5#include <linux/spinlock.h>
6#include <linux/types.h>
7#include <asm/of_device.h>
8
9enum ams_irq {
10 AMS_IRQ_FREEFALL = 0x01,
11 AMS_IRQ_SHOCK = 0x02,
12 AMS_IRQ_GLOBAL = 0x04,
13 AMS_IRQ_ALL =
14 AMS_IRQ_FREEFALL |
15 AMS_IRQ_SHOCK |
16 AMS_IRQ_GLOBAL,
17};
18
19struct ams {
20 /* Locks */
21 spinlock_t irq_lock;
22 struct mutex lock;
23
24 /* General properties */
25 struct device_node *of_node;
26 struct of_device *of_dev;
27 char has_device;
28 char vflag;
29 u32 orient1;
30 u32 orient2;
31
32 /* Interrupt worker */
33 struct work_struct worker;
34 u8 worker_irqs;
35
36 /* Implementation
37 *
38 * Only call these functions with the main lock held.
39 */
40 void (*exit)(void);
41
42 void (*get_xyz)(s8 *x, s8 *y, s8 *z);
43 u8 (*get_vendor)(void);
44
45 void (*clear_irq)(enum ams_irq reg);
46
47#ifdef CONFIG_SENSORS_AMS_I2C
48 /* I2C properties */
49 int i2c_bus;
50 int i2c_address;
51 struct i2c_client i2c_client;
52#endif
53
54 /* Joystick emulation */
55 struct task_struct *kthread;
56 struct input_dev *idev;
57 __u16 bustype;
58
59 /* calibrated null values */
60 int xcalib, ycalib, zcalib;
61};
62
63extern struct ams ams_info;
64
65extern void ams_sensors(s8 *x, s8 *y, s8 *z);
66extern int ams_sensor_attach(void);
67
68extern int ams_pmu_init(struct device_node *np);
69extern int ams_i2c_init(struct device_node *np);
70
71extern int ams_input_init(void);
72extern void ams_input_exit(void);