aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/mpu3050/mpuirq.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/misc/mpu3050/mpuirq.c')
-rw-r--r--drivers/misc/mpu3050/mpuirq.c319
1 files changed, 319 insertions, 0 deletions
diff --git a/drivers/misc/mpu3050/mpuirq.c b/drivers/misc/mpu3050/mpuirq.c
new file mode 100644
index 00000000000..ce1ad409cbf
--- /dev/null
+++ b/drivers/misc/mpu3050/mpuirq.c
@@ -0,0 +1,319 @@
1/*
2 $License:
3 Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 $
18 */
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/stat.h>
25#include <linux/irq.h>
26#include <linux/signal.h>
27#include <linux/miscdevice.h>
28#include <linux/i2c.h>
29#include <linux/i2c-dev.h>
30#include <linux/workqueue.h>
31#include <linux/poll.h>
32
33#include <linux/errno.h>
34#include <linux/fs.h>
35#include <linux/mm.h>
36#include <linux/sched.h>
37#include <linux/wait.h>
38#include <linux/uaccess.h>
39#include <linux/io.h>
40
41#include "mpu.h"
42#include "mpuirq.h"
43#include "mldl_cfg.h"
44#include "mpu-i2c.h"
45
46#define MPUIRQ_NAME "mpuirq"
47
48/* function which gets accel data and sends it to MPU */
49
50DECLARE_WAIT_QUEUE_HEAD(mpuirq_wait);
51
52struct mpuirq_dev_data {
53 struct work_struct work;
54 struct i2c_client *mpu_client;
55 struct miscdevice *dev;
56 int irq;
57 int pid;
58 int accel_divider;
59 int data_ready;
60 int timeout;
61};
62
63static struct mpuirq_dev_data mpuirq_dev_data;
64static struct mpuirq_data mpuirq_data;
65static char *interface = MPUIRQ_NAME;
66
67static void mpu_accel_data_work_fcn(struct work_struct *work);
68
69static int mpuirq_open(struct inode *inode, struct file *file)
70{
71 dev_dbg(mpuirq_dev_data.dev->this_device,
72 "%s current->pid %d\n", __func__, current->pid);
73 mpuirq_dev_data.pid = current->pid;
74 file->private_data = &mpuirq_dev_data;
75 return 0;
76}
77
78/* close function - called when the "file" /dev/mpuirq is closed in userspace */
79static int mpuirq_release(struct inode *inode, struct file *file)
80{
81 dev_dbg(mpuirq_dev_data.dev->this_device, "mpuirq_release\n");
82 return 0;
83}
84
85/* read function called when from /dev/mpuirq is read */
86static ssize_t mpuirq_read(struct file *file,
87 char *buf, size_t count, loff_t *ppos)
88{
89 int len, err;
90 struct mpuirq_dev_data *p_mpuirq_dev_data = file->private_data;
91
92 if (!mpuirq_dev_data.data_ready &&
93 mpuirq_dev_data.timeout &&
94 (!(file->f_flags & O_NONBLOCK))) {
95 wait_event_interruptible_timeout(mpuirq_wait,
96 mpuirq_dev_data.
97 data_ready,
98 mpuirq_dev_data.timeout);
99 }
100
101 if (mpuirq_dev_data.data_ready && NULL != buf
102 && count >= sizeof(mpuirq_data)) {
103 err = copy_to_user(buf, &mpuirq_data, sizeof(mpuirq_data));
104 mpuirq_data.data_type = 0;
105 } else {
106 return 0;
107 }
108 if (err != 0) {
109 dev_err(p_mpuirq_dev_data->dev->this_device,
110 "Copy to user returned %d\n", err);
111 return -EFAULT;
112 }
113 mpuirq_dev_data.data_ready = 0;
114 len = sizeof(mpuirq_data);
115 return len;
116}
117
118unsigned int mpuirq_poll(struct file *file, struct poll_table_struct *poll)
119{
120 int mask = 0;
121
122 poll_wait(file, &mpuirq_wait, poll);
123 if (mpuirq_dev_data.data_ready)
124 mask |= POLLIN | POLLRDNORM;
125 return mask;
126}
127
128/* ioctl - I/O control */
129static long mpuirq_ioctl(struct file *file,
130 unsigned int cmd, unsigned long arg)
131{
132 int retval = 0;
133 int data;
134
135 switch (cmd) {
136 case MPUIRQ_SET_TIMEOUT:
137 mpuirq_dev_data.timeout = arg;
138 break;
139
140 case MPUIRQ_GET_INTERRUPT_CNT:
141 data = mpuirq_data.interruptcount - 1;
142 if (mpuirq_data.interruptcount > 1)
143 mpuirq_data.interruptcount = 1;
144
145 if (copy_to_user((int *) arg, &data, sizeof(int)))
146 return -EFAULT;
147 break;
148 case MPUIRQ_GET_IRQ_TIME:
149 if (copy_to_user((int *) arg, &mpuirq_data.irqtime,
150 sizeof(mpuirq_data.irqtime)))
151 return -EFAULT;
152 mpuirq_data.irqtime = 0;
153 break;
154 case MPUIRQ_SET_FREQUENCY_DIVIDER:
155 mpuirq_dev_data.accel_divider = arg;
156 break;
157 default:
158 retval = -EINVAL;
159 }
160 return retval;
161}
162
163static void mpu_accel_data_work_fcn(struct work_struct *work)
164{
165 struct mpuirq_dev_data *mpuirq_dev_data =
166 (struct mpuirq_dev_data *) work;
167 struct mldl_cfg *mldl_cfg =
168 (struct mldl_cfg *)
169 i2c_get_clientdata(mpuirq_dev_data->mpu_client);
170 struct i2c_adapter *accel_adapter;
171 unsigned char wbuff[16];
172 unsigned char rbuff[16];
173 int ii;
174
175 accel_adapter = i2c_get_adapter(mldl_cfg->pdata->accel.adapt_num);
176 mldl_cfg->accel->read(accel_adapter,
177 mldl_cfg->accel,
178 &mldl_cfg->pdata->accel, rbuff);
179
180
181 /* @todo add other data formats here as well */
182 if (EXT_SLAVE_BIG_ENDIAN == mldl_cfg->accel->endian) {
183 for (ii = 0; ii < 3; ii++) {
184 wbuff[2 * ii + 1] = rbuff[2 * ii + 1];
185 wbuff[2 * ii + 2] = rbuff[2 * ii + 0];
186 }
187 } else {
188 memcpy(wbuff + 1, rbuff, mldl_cfg->accel->len);
189 }
190
191 wbuff[7] = 0;
192 wbuff[8] = 1; /*set semaphore */
193
194 mpu_memory_write(mpuirq_dev_data->mpu_client->adapter,
195 mldl_cfg->addr, 0x0108, 8, wbuff);
196}
197
198static irqreturn_t mpuirq_handler(int irq, void *dev_id)
199{
200 static int mycount;
201 struct timeval irqtime;
202 mycount++;
203
204 mpuirq_data.interruptcount++;
205
206 /* wake up (unblock) for reading data from userspace */
207 /* and ignore first interrupt generated in module init */
208 mpuirq_dev_data.data_ready = 1;
209
210 do_gettimeofday(&irqtime);
211 mpuirq_data.irqtime = (((long long) irqtime.tv_sec) << 32);
212 mpuirq_data.irqtime += irqtime.tv_usec;
213
214 if ((mpuirq_dev_data.accel_divider >= 0) &&
215 (0 == (mycount % (mpuirq_dev_data.accel_divider + 1)))) {
216 schedule_work((struct work_struct
217 *) (&mpuirq_dev_data));
218 }
219
220 wake_up_interruptible(&mpuirq_wait);
221
222 return IRQ_HANDLED;
223
224}
225
226/* define which file operations are supported */
227const struct file_operations mpuirq_fops = {
228 .owner = THIS_MODULE,
229 .read = mpuirq_read,
230 .poll = mpuirq_poll,
231
232#if HAVE_COMPAT_IOCTL
233 .compat_ioctl = mpuirq_ioctl,
234#endif
235#if HAVE_UNLOCKED_IOCTL
236 .unlocked_ioctl = mpuirq_ioctl,
237#endif
238 .open = mpuirq_open,
239 .release = mpuirq_release,
240};
241
242static struct miscdevice mpuirq_device = {
243 .minor = MISC_DYNAMIC_MINOR,
244 .name = MPUIRQ_NAME,
245 .fops = &mpuirq_fops,
246};
247
248int mpuirq_init(struct i2c_client *mpu_client)
249{
250
251 int res;
252 struct mldl_cfg *mldl_cfg =
253 (struct mldl_cfg *) i2c_get_clientdata(mpu_client);
254
255 /* work_struct initialization */
256 INIT_WORK((struct work_struct *) &mpuirq_dev_data,
257 mpu_accel_data_work_fcn);
258 mpuirq_dev_data.mpu_client = mpu_client;
259
260 dev_info(&mpu_client->adapter->dev,
261 "Module Param interface = %s\n", interface);
262
263 mpuirq_dev_data.irq = mpu_client->irq;
264 mpuirq_dev_data.pid = 0;
265 mpuirq_dev_data.accel_divider = -1;
266 mpuirq_dev_data.data_ready = 0;
267 mpuirq_dev_data.timeout = 0;
268 mpuirq_dev_data.dev = &mpuirq_device;
269
270 if (mpuirq_dev_data.irq) {
271 unsigned long flags;
272 if (BIT_ACTL_LOW ==
273 ((mldl_cfg->pdata->int_config) & BIT_ACTL))
274 flags = IRQF_TRIGGER_FALLING;
275 else
276 flags = IRQF_TRIGGER_RISING;
277
278 res =
279 request_irq(mpuirq_dev_data.irq, mpuirq_handler, flags,
280 interface, &mpuirq_dev_data.irq);
281 if (res) {
282 dev_err(&mpu_client->adapter->dev,
283 "myirqtest: cannot register IRQ %d\n",
284 mpuirq_dev_data.irq);
285 } else {
286 res = misc_register(&mpuirq_device);
287 if (res < 0) {
288 dev_err(&mpu_client->adapter->dev,
289 "misc_register returned %d\n",
290 res);
291 free_irq(mpuirq_dev_data.irq,
292 &mpuirq_dev_data.irq);
293 }
294 }
295
296 } else {
297 res = 0;
298 }
299
300 return res;
301}
302
303void mpuirq_exit(void)
304{
305 /* Free the IRQ first before flushing the work */
306 if (mpuirq_dev_data.irq > 0)
307 free_irq(mpuirq_dev_data.irq, &mpuirq_dev_data.irq);
308
309 flush_scheduled_work();
310
311 dev_info(mpuirq_device.this_device, "Unregistering %s\n",
312 MPUIRQ_NAME);
313 misc_deregister(&mpuirq_device);
314
315 return;
316}
317
318module_param(interface, charp, S_IRUGO | S_IWUSR);
319MODULE_PARM_DESC(interface, "The Interface name");