2 * Apple Motion Sensor driver (I2C variant)
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
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
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.
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>
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 */
50 /* AMS commands - use with the AMS_COMMAND register */
63 static int ams_i2c_attach(struct i2c_adapter
*adapter
);
64 static int ams_i2c_detach(struct i2c_adapter
*adapter
);
66 static struct i2c_driver ams_i2c_driver
= {
71 .attach_adapter
= ams_i2c_attach
,
72 .detach_adapter
= ams_i2c_detach
,
75 static s32
ams_i2c_read(u8 reg
)
77 return i2c_smbus_read_byte_data(&ams_info
.i2c_client
, reg
);
80 static int ams_i2c_write(u8 reg
, u8 value
)
82 return i2c_smbus_write_byte_data(&ams_info
.i2c_client
, reg
, value
);
85 static int ams_i2c_cmd(enum ams_i2c_cmd cmd
)
90 ams_i2c_write(AMS_COMMAND
, cmd
);
94 result
= ams_i2c_read(AMS_COMMAND
);
95 if (result
== 0 || result
& 0x80)
98 schedule_timeout_uninterruptible(HZ
/ 20);
104 static void ams_i2c_set_irq(enum ams_irq reg
, char enable
)
106 if (reg
& AMS_IRQ_FREEFALL
) {
107 u8 val
= ams_i2c_read(AMS_CTRLX
);
112 ams_i2c_write(AMS_CTRLX
, val
);
115 if (reg
& AMS_IRQ_SHOCK
) {
116 u8 val
= ams_i2c_read(AMS_CTRLY
);
121 ams_i2c_write(AMS_CTRLY
, val
);
124 if (reg
& AMS_IRQ_GLOBAL
) {
125 u8 val
= ams_i2c_read(AMS_CTRLZ
);
130 ams_i2c_write(AMS_CTRLZ
, val
);
134 static void ams_i2c_clear_irq(enum ams_irq reg
)
136 if (reg
& AMS_IRQ_FREEFALL
)
137 ams_i2c_write(AMS_FREEFALL
, 0);
139 if (reg
& AMS_IRQ_SHOCK
)
140 ams_i2c_write(AMS_SHOCK
, 0);
143 static u8
ams_i2c_get_vendor(void)
145 return ams_i2c_read(AMS_VENDOR
);
148 static void ams_i2c_get_xyz(s8
*x
, s8
*y
, s8
*z
)
150 *x
= ams_i2c_read(AMS_DATAX
);
151 *y
= ams_i2c_read(AMS_DATAY
);
152 *z
= ams_i2c_read(AMS_DATAZ
);
155 static int ams_i2c_attach(struct i2c_adapter
*adapter
)
161 /* There can be only one */
162 if (unlikely(ams_info
.has_device
))
165 if (strncmp(adapter
->name
, "uni-n", 5))
168 bus
= simple_strtoul(adapter
->name
+ 6, NULL
, 10);
169 if (bus
!= ams_info
.i2c_bus
)
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");
177 if (ams_i2c_cmd(AMS_CMD_RESET
)) {
178 printk(KERN_INFO
"ams: Failed to reset the device\n");
182 if (ams_i2c_cmd(AMS_CMD_START
)) {
183 printk(KERN_INFO
"ams: Failed to start the device\n");
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);
192 ams_i2c_cmd(AMS_CMD_READMEM
);
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",
202 ams_i2c_cmd(AMS_CMD_VERSION
);
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",
212 /* Disable interrupts */
213 ams_i2c_set_irq(AMS_IRQ_ALL
, 0);
215 result
= ams_sensor_attach();
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);
227 /* Clear interrupts */
228 ams_i2c_clear_irq(AMS_IRQ_ALL
);
230 ams_info
.has_device
= 1;
232 /* Enable interrupts */
233 ams_i2c_set_irq(AMS_IRQ_ALL
, 1);
235 printk(KERN_INFO
"ams: Found I2C based motion sensor\n");
240 static int ams_i2c_detach(struct i2c_adapter
*adapter
)
242 if (ams_info
.has_device
) {
243 /* Disable interrupts */
244 ams_i2c_set_irq(AMS_IRQ_ALL
, 0);
246 /* Clear interrupts */
247 ams_i2c_clear_irq(AMS_IRQ_ALL
);
249 printk(KERN_INFO
"ams: Unloading\n");
251 ams_info
.has_device
= 0;
257 static void ams_i2c_exit(void)
259 i2c_del_driver(&ams_i2c_driver
);
262 int __init
ams_i2c_init(struct device_node
*np
)
268 mutex_lock(&ams_info
.lock
);
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
;
278 /* look for bus either using "reg" or by path */
279 prop
= of_get_property(ams_info
.of_node
, "reg", NULL
);
286 tmp_bus
= strstr(ams_info
.of_node
->full_name
, "/i2c-bus@");
288 ams_info
.i2c_bus
= *(tmp_bus
+ 9) - '0';
290 ams_info
.i2c_bus
= ((*prop
) >> 8) & 0x0f;
291 ams_info
.i2c_address
= ((*prop
) & 0xff) >> 1;
293 result
= i2c_add_driver(&ams_i2c_driver
);
296 mutex_unlock(&ams_info
.lock
);