Blame view

drivers/macintosh/ams/ams-core.c 5.95 KB
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
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
  /*
   * Apple Motion Sensor driver
   *
   * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
   * 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.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   */
  
  #include <linux/module.h>
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/init.h>
7587cb2f1   Stephen Rothwell   hwmon: Use linux/...
26
  #include <linux/of_platform.h>
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
27
  #include <asm/pmac_pfunc.h>
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
28
29
30
31
32
  
  #include "ams.h"
  
  /* There is only one motion sensor per machine */
  struct ams ams_info;
90ab5ee94   Rusty Russell   module_param: mak...
33
  static bool verbose;
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
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
  module_param(verbose, bool, 0644);
  MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");
  
  /* Call with ams_info.lock held! */
  void ams_sensors(s8 *x, s8 *y, s8 *z)
  {
  	u32 orient = ams_info.vflag? ams_info.orient1 : ams_info.orient2;
  
  	if (orient & 0x80)
  		/* X and Y swapped */
  		ams_info.get_xyz(y, x, z);
  	else
  		ams_info.get_xyz(x, y, z);
  
  	if (orient & 0x04)
  		*z = ~(*z);
  	if (orient & 0x02)
  		*y = ~(*y);
  	if (orient & 0x01)
  		*x = ~(*x);
  }
  
  static ssize_t ams_show_current(struct device *dev,
  	struct device_attribute *attr, char *buf)
  {
  	s8 x, y, z;
  
  	mutex_lock(&ams_info.lock);
  	ams_sensors(&x, &y, &z);
  	mutex_unlock(&ams_info.lock);
  
  	return snprintf(buf, PAGE_SIZE, "%d %d %d
  ", x, y, z);
  }
  
  static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);
  
  static void ams_handle_irq(void *data)
  {
  	enum ams_irq irq = *((enum ams_irq *)data);
  
  	spin_lock(&ams_info.irq_lock);
  
  	ams_info.worker_irqs |= irq;
  	schedule_work(&ams_info.worker);
  
  	spin_unlock(&ams_info.irq_lock);
  }
  
  static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;
  static struct pmf_irq_client ams_freefall_client = {
  	.owner = THIS_MODULE,
  	.handler = ams_handle_irq,
  	.data = &ams_freefall_irq_data,
  };
  
  static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;
  static struct pmf_irq_client ams_shock_client = {
  	.owner = THIS_MODULE,
  	.handler = ams_handle_irq,
  	.data = &ams_shock_irq_data,
  };
  
  /* Once hard disk parking is implemented in the kernel, this function can
   * trigger it.
   */
  static void ams_worker(struct work_struct *work)
  {
dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
102
103
  	unsigned long flags;
  	u8 irqs_to_clear;
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
104

dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
105
  	mutex_lock(&ams_info.lock);
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
106

dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
107
108
  	spin_lock_irqsave(&ams_info.irq_lock, flags);
  	irqs_to_clear = ams_info.worker_irqs;
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
109

dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
110
111
112
113
  	if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
  		if (verbose)
  			printk(KERN_INFO "ams: freefall detected!
  ");
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
114

dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
115
116
  		ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;
  	}
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
117

dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
118
119
120
121
  	if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
  		if (verbose)
  			printk(KERN_INFO "ams: shock detected!
  ");
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
122

dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
123
124
  		ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
  	}
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
125

dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
126
  	spin_unlock_irqrestore(&ams_info.irq_lock, flags);
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
127

dbee35626   Dmitry Torokhov   hwmon: (ams) Simp...
128
  	ams_info.clear_irq(irqs_to_clear);
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
129
130
131
132
133
134
135
136
  
  	mutex_unlock(&ams_info.lock);
  }
  
  /* Call with ams_info.lock held! */
  int ams_sensor_attach(void)
  {
  	int result;
a7edd0e67   Stephen Rothwell   [POWERPC] get_pro...
137
  	const u32 *prop;
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
138
139
  
  	/* Get orientation */
40cd3a456   Stephen Rothwell   [POWERPC] Rename ...
140
  	prop = of_get_property(ams_info.of_node, "orientation", NULL);
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
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
  	if (!prop)
  		return -ENODEV;
  	ams_info.orient1 = *prop;
  	ams_info.orient2 = *(prop + 1);
  
  	/* Register freefall interrupt handler */
  	result = pmf_register_irq_client(ams_info.of_node,
  			"accel-int-1",
  			&ams_freefall_client);
  	if (result < 0)
  		return -ENODEV;
  
  	/* Reset saved irqs */
  	ams_info.worker_irqs = 0;
  
  	/* Register shock interrupt handler */
  	result = pmf_register_irq_client(ams_info.of_node,
  			"accel-int-2",
  			&ams_shock_client);
  	if (result < 0)
  		goto release_freefall;
  
  	/* Create device */
  	ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
  	if (!ams_info.of_dev) {
  		result = -ENODEV;
  		goto release_shock;
  	}
  
  	/* Create attributes */
  	result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
  	if (result)
  		goto release_of;
  
  	ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
  
  	/* Init input device */
  	result = ams_input_init();
  	if (result)
  		goto release_device_file;
  
  	return result;
  release_device_file:
  	device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
  release_of:
  	of_device_unregister(ams_info.of_dev);
  release_shock:
  	pmf_unregister_irq_client(&ams_shock_client);
  release_freefall:
  	pmf_unregister_irq_client(&ams_freefall_client);
  	return result;
  }
  
  int __init ams_init(void)
  {
  	struct device_node *np;
  
  	spin_lock_init(&ams_info.irq_lock);
  	mutex_init(&ams_info.lock);
  	INIT_WORK(&ams_info.worker, ams_worker);
  
  #ifdef CONFIG_SENSORS_AMS_I2C
  	np = of_find_node_by_name(NULL, "accelerometer");
55b61fec2   Stephen Rothwell   [POWERPC] Rename ...
204
  	if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
205
206
207
208
209
210
  		/* Found I2C motion sensor */
  		return ams_i2c_init(np);
  #endif
  
  #ifdef CONFIG_SENSORS_AMS_PMU
  	np = of_find_node_by_name(NULL, "sms");
55b61fec2   Stephen Rothwell   [POWERPC] Rename ...
211
  	if (np && of_device_is_compatible(np, "sms"))
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
212
213
214
  		/* Found PMU motion sensor */
  		return ams_pmu_init(np);
  #endif
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
215
216
  	return -ENODEV;
  }
98ceb75c7   Jean Delvare   macintosh/hwmon/a...
217
  void ams_sensor_detach(void)
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
218
  {
ee4cd32ee   Dmitry Torokhov   hwmon: (ams) Fix ...
219
220
  	/* Remove input device */
  	ams_input_exit();
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
221

ee4cd32ee   Dmitry Torokhov   hwmon: (ams) Fix ...
222
223
  	/* Remove attributes */
  	device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
224

ee4cd32ee   Dmitry Torokhov   hwmon: (ams) Fix ...
225
226
227
228
229
  	/* Flush interrupt worker
  	 *
  	 * We do this after ams_info.exit(), because an interrupt might
  	 * have arrived before disabling them.
  	 */
d9c612eb6   Tejun Heo   macintosh/ams: do...
230
  	flush_work_sync(&ams_info.worker);
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
231

ee4cd32ee   Dmitry Torokhov   hwmon: (ams) Fix ...
232
233
  	/* Remove device */
  	of_device_unregister(ams_info.of_dev);
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
234

ee4cd32ee   Dmitry Torokhov   hwmon: (ams) Fix ...
235
236
237
  	/* Remove handler */
  	pmf_unregister_irq_client(&ams_shock_client);
  	pmf_unregister_irq_client(&ams_freefall_client);
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
238
  }
98ceb75c7   Jean Delvare   macintosh/hwmon/a...
239
240
241
242
243
  static void __exit ams_exit(void)
  {
  	/* Shut down implementation */
  	ams_info.exit();
  }
dcb69dd01   Stelian Pop   hwmon: New AMS ha...
244
245
246
247
248
249
  MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
  MODULE_DESCRIPTION("Apple Motion Sensor driver");
  MODULE_LICENSE("GPL");
  
  module_init(ams_init);
  module_exit(ams_exit);