mpuirq.c 7.8 KB

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