blob: 957760536a4cf46d7c387087e3fa6ee6134a78ef [file] [log] [blame]
Stelian Popdcb69dd2006-12-12 18:18:30 +01001/*
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;
Stelian Pop63232dc2007-05-08 17:22:02 +020088 int count = 3;
Stelian Popdcb69dd2006-12-12 18:18:30 +010089
90 ams_i2c_write(AMS_COMMAND, cmd);
Stelian Pop63232dc2007-05-08 17:22:02 +020091 msleep(5);
Stelian Popdcb69dd2006-12-12 18:18:30 +010092
Stelian Pop63232dc2007-05-08 17:22:02 +020093 while (count--) {
Stelian Popdcb69dd2006-12-12 18:18:30 +010094 result = ams_i2c_read(AMS_COMMAND);
95 if (result == 0 || result & 0x80)
96 return 0;
97
Stelian Pop63232dc2007-05-08 17:22:02 +020098 schedule_timeout_uninterruptible(HZ / 20);
Stelian Popdcb69dd2006-12-12 18:18:30 +010099 }
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;
Stephen Rothwella7edd0e2007-04-03 10:52:17 +1000266 const u32 *prop;
Stelian Popdcb69dd2006-12-12 18:18:30 +0100267
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 */
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000279 prop = of_get_property(ams_info.of_node, "reg", NULL);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100280 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}