lis3lv02d.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
  3. *
  4. * Copyright (C) 2007-2008 Yan Burman
  5. * Copyright (C) 2008 Eric Piel
  6. * Copyright (C) 2008-2009 Pavel Machek
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/dmi.h>
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/input-polldev.h>
  30. #include <linux/delay.h>
  31. #include <linux/wait.h>
  32. #include <linux/poll.h>
  33. #include <linux/freezer.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/miscdevice.h>
  36. #include <asm/atomic.h>
  37. #include "lis3lv02d.h"
  38. #define DRIVER_NAME "lis3lv02d"
  39. /* joystick device poll interval in milliseconds */
  40. #define MDPS_POLL_INTERVAL 50
  41. /*
  42. * The sensor can also generate interrupts (DRDY) but it's pretty pointless
  43. * because they are generated even if the data do not change. So it's better
  44. * to keep the interrupt for the free-fall event. The values are updated at
  45. * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
  46. * some low processor, we poll the sensor only at 20Hz... enough for the
  47. * joystick.
  48. */
  49. #define LIS3_PWRON_DELAY_WAI_12B (5000)
  50. #define LIS3_PWRON_DELAY_WAI_8B (3000)
  51. /*
  52. * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
  53. * LIS302D spec says: 18 mG / digit
  54. * LIS3_ACCURACY is used to increase accuracy of the intermediate
  55. * calculation results.
  56. */
  57. #define LIS3_ACCURACY 1024
  58. /* Sensitivity values for -2G +2G scale */
  59. #define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024)
  60. #define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY)
  61. #define LIS3_DEFAULT_FUZZ 3
  62. #define LIS3_DEFAULT_FLAT 3
  63. struct lis3lv02d lis3_dev = {
  64. .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
  65. };
  66. EXPORT_SYMBOL_GPL(lis3_dev);
  67. static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
  68. {
  69. s8 lo;
  70. if (lis3->read(lis3, reg, &lo) < 0)
  71. return 0;
  72. return lo;
  73. }
  74. static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
  75. {
  76. u8 lo, hi;
  77. lis3->read(lis3, reg - 1, &lo);
  78. lis3->read(lis3, reg, &hi);
  79. /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
  80. return (s16)((hi << 8) | lo);
  81. }
  82. /**
  83. * lis3lv02d_get_axis - For the given axis, give the value converted
  84. * @axis: 1,2,3 - can also be negative
  85. * @hw_values: raw values returned by the hardware
  86. *
  87. * Returns the converted value.
  88. */
  89. static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
  90. {
  91. if (axis > 0)
  92. return hw_values[axis - 1];
  93. else
  94. return -hw_values[-axis - 1];
  95. }
  96. /**
  97. * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
  98. * @lis3: pointer to the device struct
  99. * @x: where to store the X axis value
  100. * @y: where to store the Y axis value
  101. * @z: where to store the Z axis value
  102. *
  103. * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
  104. */
  105. static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
  106. {
  107. int position[3];
  108. int i;
  109. mutex_lock(&lis3->mutex);
  110. position[0] = lis3->read_data(lis3, OUTX);
  111. position[1] = lis3->read_data(lis3, OUTY);
  112. position[2] = lis3->read_data(lis3, OUTZ);
  113. mutex_unlock(&lis3->mutex);
  114. for (i = 0; i < 3; i++)
  115. position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
  116. *x = lis3lv02d_get_axis(lis3->ac.x, position);
  117. *y = lis3lv02d_get_axis(lis3->ac.y, position);
  118. *z = lis3lv02d_get_axis(lis3->ac.z, position);
  119. }
  120. /* conversion btw sampling rate and the register values */
  121. static int lis3_12_rates[4] = {40, 160, 640, 2560};
  122. static int lis3_8_rates[2] = {100, 400};
  123. /* ODR is Output Data Rate */
  124. static int lis3lv02d_get_odr(void)
  125. {
  126. u8 ctrl;
  127. int shift;
  128. lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
  129. ctrl &= lis3_dev.odr_mask;
  130. shift = ffs(lis3_dev.odr_mask) - 1;
  131. return lis3_dev.odrs[(ctrl >> shift)];
  132. }
  133. static int lis3lv02d_set_odr(int rate)
  134. {
  135. u8 ctrl;
  136. int i, len, shift;
  137. lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
  138. ctrl &= ~lis3_dev.odr_mask;
  139. len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
  140. shift = ffs(lis3_dev.odr_mask) - 1;
  141. for (i = 0; i < len; i++)
  142. if (lis3_dev.odrs[i] == rate) {
  143. lis3_dev.write(&lis3_dev, CTRL_REG1,
  144. ctrl | (i << shift));
  145. return 0;
  146. }
  147. return -EINVAL;
  148. }
  149. static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
  150. {
  151. u8 reg;
  152. s16 x, y, z;
  153. u8 selftest;
  154. int ret;
  155. mutex_lock(&lis3->mutex);
  156. if (lis3_dev.whoami == WAI_12B)
  157. selftest = CTRL1_ST;
  158. else
  159. selftest = CTRL1_STP;
  160. lis3->read(lis3, CTRL_REG1, &reg);
  161. lis3->write(lis3, CTRL_REG1, (reg | selftest));
  162. msleep(lis3->pwron_delay / lis3lv02d_get_odr());
  163. /* Read directly to avoid axis remap */
  164. x = lis3->read_data(lis3, OUTX);
  165. y = lis3->read_data(lis3, OUTY);
  166. z = lis3->read_data(lis3, OUTZ);
  167. /* back to normal settings */
  168. lis3->write(lis3, CTRL_REG1, reg);
  169. msleep(lis3->pwron_delay / lis3lv02d_get_odr());
  170. results[0] = x - lis3->read_data(lis3, OUTX);
  171. results[1] = y - lis3->read_data(lis3, OUTY);
  172. results[2] = z - lis3->read_data(lis3, OUTZ);
  173. ret = 0;
  174. if (lis3->pdata) {
  175. int i;
  176. for (i = 0; i < 3; i++) {
  177. /* Check against selftest acceptance limits */
  178. if ((results[i] < lis3->pdata->st_min_limits[i]) ||
  179. (results[i] > lis3->pdata->st_max_limits[i])) {
  180. ret = -EIO;
  181. goto fail;
  182. }
  183. }
  184. }
  185. /* test passed */
  186. fail:
  187. mutex_unlock(&lis3->mutex);
  188. return ret;
  189. }
  190. void lis3lv02d_poweroff(struct lis3lv02d *lis3)
  191. {
  192. /* disable X,Y,Z axis and power down */
  193. lis3->write(lis3, CTRL_REG1, 0x00);
  194. }
  195. EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
  196. void lis3lv02d_poweron(struct lis3lv02d *lis3)
  197. {
  198. u8 reg;
  199. lis3->init(lis3);
  200. /* LIS3 power on delay is quite long */
  201. msleep(lis3->pwron_delay / lis3lv02d_get_odr());
  202. /*
  203. * Common configuration
  204. * BDU: (12 bits sensors only) LSB and MSB values are not updated until
  205. * both have been read. So the value read will always be correct.
  206. */
  207. if (lis3->whoami == WAI_12B) {
  208. lis3->read(lis3, CTRL_REG2, &reg);
  209. reg |= CTRL2_BDU;
  210. lis3->write(lis3, CTRL_REG2, reg);
  211. }
  212. }
  213. EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
  214. static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
  215. {
  216. if (!test_bit(0, &lis3_dev.misc_opened))
  217. goto out;
  218. /*
  219. * Be careful: on some HP laptops the bios force DD when on battery and
  220. * the lid is closed. This leads to interrupts as soon as a little move
  221. * is done.
  222. */
  223. atomic_inc(&lis3_dev.count);
  224. wake_up_interruptible(&lis3_dev.misc_wait);
  225. kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
  226. out:
  227. if (lis3_dev.whoami == WAI_8B && lis3_dev.idev &&
  228. lis3_dev.idev->input->users)
  229. return IRQ_WAKE_THREAD;
  230. return IRQ_HANDLED;
  231. }
  232. static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
  233. {
  234. return IRQ_HANDLED;
  235. }
  236. static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
  237. {
  238. return IRQ_HANDLED;
  239. }
  240. static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
  241. {
  242. if (test_and_set_bit(0, &lis3_dev.misc_opened))
  243. return -EBUSY; /* already open */
  244. atomic_set(&lis3_dev.count, 0);
  245. return 0;
  246. }
  247. static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
  248. {
  249. fasync_helper(-1, file, 0, &lis3_dev.async_queue);
  250. clear_bit(0, &lis3_dev.misc_opened); /* release the device */
  251. return 0;
  252. }
  253. static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
  254. size_t count, loff_t *pos)
  255. {
  256. DECLARE_WAITQUEUE(wait, current);
  257. u32 data;
  258. unsigned char byte_data;
  259. ssize_t retval = 1;
  260. if (count < 1)
  261. return -EINVAL;
  262. add_wait_queue(&lis3_dev.misc_wait, &wait);
  263. while (true) {
  264. set_current_state(TASK_INTERRUPTIBLE);
  265. data = atomic_xchg(&lis3_dev.count, 0);
  266. if (data)
  267. break;
  268. if (file->f_flags & O_NONBLOCK) {
  269. retval = -EAGAIN;
  270. goto out;
  271. }
  272. if (signal_pending(current)) {
  273. retval = -ERESTARTSYS;
  274. goto out;
  275. }
  276. schedule();
  277. }
  278. if (data < 255)
  279. byte_data = data;
  280. else
  281. byte_data = 255;
  282. /* make sure we are not going into copy_to_user() with
  283. * TASK_INTERRUPTIBLE state */
  284. set_current_state(TASK_RUNNING);
  285. if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
  286. retval = -EFAULT;
  287. out:
  288. __set_current_state(TASK_RUNNING);
  289. remove_wait_queue(&lis3_dev.misc_wait, &wait);
  290. return retval;
  291. }
  292. static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
  293. {
  294. poll_wait(file, &lis3_dev.misc_wait, wait);
  295. if (atomic_read(&lis3_dev.count))
  296. return POLLIN | POLLRDNORM;
  297. return 0;
  298. }
  299. static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
  300. {
  301. return fasync_helper(fd, file, on, &lis3_dev.async_queue);
  302. }
  303. static const struct file_operations lis3lv02d_misc_fops = {
  304. .owner = THIS_MODULE,
  305. .llseek = no_llseek,
  306. .read = lis3lv02d_misc_read,
  307. .open = lis3lv02d_misc_open,
  308. .release = lis3lv02d_misc_release,
  309. .poll = lis3lv02d_misc_poll,
  310. .fasync = lis3lv02d_misc_fasync,
  311. };
  312. static struct miscdevice lis3lv02d_misc_device = {
  313. .minor = MISC_DYNAMIC_MINOR,
  314. .name = "freefall",
  315. .fops = &lis3lv02d_misc_fops,
  316. };
  317. static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
  318. {
  319. int x, y, z;
  320. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  321. input_report_abs(pidev->input, ABS_X, x);
  322. input_report_abs(pidev->input, ABS_Y, y);
  323. input_report_abs(pidev->input, ABS_Z, z);
  324. input_sync(pidev->input);
  325. }
  326. int lis3lv02d_joystick_enable(void)
  327. {
  328. struct input_dev *input_dev;
  329. int err;
  330. int max_val, fuzz, flat;
  331. if (lis3_dev.idev)
  332. return -EINVAL;
  333. lis3_dev.idev = input_allocate_polled_device();
  334. if (!lis3_dev.idev)
  335. return -ENOMEM;
  336. lis3_dev.idev->poll = lis3lv02d_joystick_poll;
  337. lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
  338. input_dev = lis3_dev.idev->input;
  339. input_dev->name = "ST LIS3LV02DL Accelerometer";
  340. input_dev->phys = DRIVER_NAME "/input0";
  341. input_dev->id.bustype = BUS_HOST;
  342. input_dev->id.vendor = 0;
  343. input_dev->dev.parent = &lis3_dev.pdev->dev;
  344. set_bit(EV_ABS, input_dev->evbit);
  345. max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
  346. fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
  347. flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
  348. input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
  349. input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
  350. input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
  351. err = input_register_polled_device(lis3_dev.idev);
  352. if (err) {
  353. input_free_polled_device(lis3_dev.idev);
  354. lis3_dev.idev = NULL;
  355. }
  356. return err;
  357. }
  358. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
  359. void lis3lv02d_joystick_disable(void)
  360. {
  361. if (lis3_dev.irq)
  362. free_irq(lis3_dev.irq, &lis3_dev);
  363. if (lis3_dev.pdata && lis3_dev.pdata->irq2)
  364. free_irq(lis3_dev.pdata->irq2, &lis3_dev);
  365. if (!lis3_dev.idev)
  366. return;
  367. if (lis3_dev.irq)
  368. misc_deregister(&lis3lv02d_misc_device);
  369. input_unregister_polled_device(lis3_dev.idev);
  370. input_free_polled_device(lis3_dev.idev);
  371. lis3_dev.idev = NULL;
  372. }
  373. EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
  374. /* Sysfs stuff */
  375. static ssize_t lis3lv02d_selftest_show(struct device *dev,
  376. struct device_attribute *attr, char *buf)
  377. {
  378. int result;
  379. s16 values[3];
  380. result = lis3lv02d_selftest(&lis3_dev, values);
  381. return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
  382. values[0], values[1], values[2]);
  383. }
  384. static ssize_t lis3lv02d_position_show(struct device *dev,
  385. struct device_attribute *attr, char *buf)
  386. {
  387. int x, y, z;
  388. lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
  389. return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
  390. }
  391. static ssize_t lis3lv02d_rate_show(struct device *dev,
  392. struct device_attribute *attr, char *buf)
  393. {
  394. return sprintf(buf, "%d\n", lis3lv02d_get_odr());
  395. }
  396. static ssize_t lis3lv02d_rate_set(struct device *dev,
  397. struct device_attribute *attr, const char *buf,
  398. size_t count)
  399. {
  400. unsigned long rate;
  401. if (strict_strtoul(buf, 0, &rate))
  402. return -EINVAL;
  403. if (lis3lv02d_set_odr(rate))
  404. return -EINVAL;
  405. return count;
  406. }
  407. static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
  408. static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
  409. static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
  410. lis3lv02d_rate_set);
  411. static struct attribute *lis3lv02d_attributes[] = {
  412. &dev_attr_selftest.attr,
  413. &dev_attr_position.attr,
  414. &dev_attr_rate.attr,
  415. NULL
  416. };
  417. static struct attribute_group lis3lv02d_attribute_group = {
  418. .attrs = lis3lv02d_attributes
  419. };
  420. static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
  421. {
  422. lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  423. if (IS_ERR(lis3->pdev))
  424. return PTR_ERR(lis3->pdev);
  425. return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  426. }
  427. int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
  428. {
  429. sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
  430. platform_device_unregister(lis3->pdev);
  431. return 0;
  432. }
  433. EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
  434. static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
  435. struct lis3lv02d_platform_data *p)
  436. {
  437. int err;
  438. int ctrl2 = p->hipass_ctrl;
  439. if (p->click_flags) {
  440. dev->write(dev, CLICK_CFG, p->click_flags);
  441. dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
  442. dev->write(dev, CLICK_LATENCY, p->click_latency);
  443. dev->write(dev, CLICK_WINDOW, p->click_window);
  444. dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
  445. dev->write(dev, CLICK_THSY_X,
  446. (p->click_thresh_x & 0xf) |
  447. (p->click_thresh_y << 4));
  448. }
  449. if (p->wakeup_flags) {
  450. dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
  451. dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
  452. /* default to 2.5ms for now */
  453. dev->write(dev, FF_WU_DURATION_1, 1);
  454. ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
  455. }
  456. if (p->wakeup_flags2) {
  457. dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
  458. dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
  459. /* default to 2.5ms for now */
  460. dev->write(dev, FF_WU_DURATION_2, 1);
  461. ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
  462. }
  463. /* Configure hipass filters */
  464. dev->write(dev, CTRL_REG2, ctrl2);
  465. if (p->irq2) {
  466. err = request_threaded_irq(p->irq2,
  467. NULL,
  468. lis302dl_interrupt_thread2_8b,
  469. IRQF_TRIGGER_RISING |
  470. IRQF_ONESHOT,
  471. DRIVER_NAME, &lis3_dev);
  472. if (err < 0)
  473. printk(KERN_ERR DRIVER_NAME
  474. "No second IRQ. Limited functionality\n");
  475. }
  476. }
  477. /*
  478. * Initialise the accelerometer and the various subsystems.
  479. * Should be rather independent of the bus system.
  480. */
  481. int lis3lv02d_init_device(struct lis3lv02d *dev)
  482. {
  483. int err;
  484. irq_handler_t thread_fn;
  485. dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
  486. switch (dev->whoami) {
  487. case WAI_12B:
  488. printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
  489. dev->read_data = lis3lv02d_read_12;
  490. dev->mdps_max_val = 2048;
  491. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
  492. dev->odrs = lis3_12_rates;
  493. dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
  494. dev->scale = LIS3_SENSITIVITY_12B;
  495. break;
  496. case WAI_8B:
  497. printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
  498. dev->read_data = lis3lv02d_read_8;
  499. dev->mdps_max_val = 128;
  500. dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
  501. dev->odrs = lis3_8_rates;
  502. dev->odr_mask = CTRL1_DR;
  503. dev->scale = LIS3_SENSITIVITY_8B;
  504. break;
  505. default:
  506. printk(KERN_ERR DRIVER_NAME
  507. ": unknown sensor type 0x%X\n", dev->whoami);
  508. return -EINVAL;
  509. }
  510. mutex_init(&dev->mutex);
  511. lis3lv02d_add_fs(dev);
  512. lis3lv02d_poweron(dev);
  513. if (lis3lv02d_joystick_enable())
  514. printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
  515. /* passing in platform specific data is purely optional and only
  516. * used by the SPI transport layer at the moment */
  517. if (dev->pdata) {
  518. struct lis3lv02d_platform_data *p = dev->pdata;
  519. if (dev->whoami == WAI_8B)
  520. lis3lv02d_8b_configure(dev, p);
  521. if (p->irq_cfg)
  522. dev->write(dev, CTRL_REG3, p->irq_cfg);
  523. }
  524. /* bail if we did not get an IRQ from the bus layer */
  525. if (!dev->irq) {
  526. printk(KERN_ERR DRIVER_NAME
  527. ": No IRQ. Disabling /dev/freefall\n");
  528. goto out;
  529. }
  530. /*
  531. * The sensor can generate interrupts for free-fall and direction
  532. * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
  533. * the things simple and _fast_ we activate it only for free-fall, so
  534. * no need to read register (very slow with ACPI). For the same reason,
  535. * we forbid shared interrupts.
  536. *
  537. * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
  538. * io-apic is not configurable (and generates a warning) but I keep it
  539. * in case of support for other hardware.
  540. */
  541. if (dev->whoami == WAI_8B)
  542. thread_fn = lis302dl_interrupt_thread1_8b;
  543. else
  544. thread_fn = NULL;
  545. err = request_threaded_irq(dev->irq, lis302dl_interrupt,
  546. thread_fn,
  547. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  548. DRIVER_NAME, &lis3_dev);
  549. if (err < 0) {
  550. printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
  551. goto out;
  552. }
  553. if (misc_register(&lis3lv02d_misc_device))
  554. printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
  555. out:
  556. return 0;
  557. }
  558. EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
  559. MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
  560. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  561. MODULE_LICENSE("GPL");