blob: df3f58613f7bf4067284c2040d8b9fad8ae43f58 [file] [log] [blame]
Pavel Machek455fbdd2008-11-12 13:27:02 -08001/*
2 * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
3 *
4 * Copyright (C) 2007-2008 Yan Burman
5 * Copyright (C) 2008 Eric Piel
Pavel Machekef2cfc72009-02-18 14:48:23 -08006 * Copyright (C) 2008-2009 Pavel Machek
Pavel Machek455fbdd2008-11-12 13:27:02 -08007 *
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
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/dmi.h>
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/platform_device.h>
29#include <linux/interrupt.h>
30#include <linux/input.h>
31#include <linux/kthread.h>
32#include <linux/semaphore.h>
33#include <linux/delay.h>
34#include <linux/wait.h>
35#include <linux/poll.h>
36#include <linux/freezer.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080037#include <linux/uaccess.h>
Pavel Machekef2cfc72009-02-18 14:48:23 -080038#include <linux/miscdevice.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080039#include <asm/atomic.h>
40#include "lis3lv02d.h"
41
42#define DRIVER_NAME "lis3lv02d"
Pavel Machek455fbdd2008-11-12 13:27:02 -080043
44/* joystick device poll interval in milliseconds */
45#define MDPS_POLL_INTERVAL 50
46/*
47 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
48 * because their are generated even if the data do not change. So it's better
49 * to keep the interrupt for the free-fall event. The values are updated at
50 * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
51 * some low processor, we poll the sensor only at 20Hz... enough for the
52 * joystick.
53 */
54
Daniel Macka38da2e2009-03-31 15:24:32 -070055struct lis3lv02d lis3_dev = {
Pavel Machekbe84cfc2009-03-31 15:24:26 -070056 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
Pavel Machekef2cfc72009-02-18 14:48:23 -080057};
58
Pavel Machekbe84cfc2009-03-31 15:24:26 -070059EXPORT_SYMBOL_GPL(lis3_dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080060
Daniel Macka38da2e2009-03-31 15:24:32 -070061static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
62{
63 s8 lo;
64 if (lis3->read(lis3, reg, &lo) < 0)
65 return 0;
66
67 return lo;
68}
69
70static s16 lis3lv02d_read_16(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -070071{
72 u8 lo, hi;
73
Daniel Macka38da2e2009-03-31 15:24:32 -070074 lis3->read(lis3, reg - 1, &lo);
75 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -070076 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
77 return (s16)((hi << 8) | lo);
78}
79
Pavel Machek455fbdd2008-11-12 13:27:02 -080080/**
81 * lis3lv02d_get_axis - For the given axis, give the value converted
82 * @axis: 1,2,3 - can also be negative
83 * @hw_values: raw values returned by the hardware
84 *
85 * Returns the converted value.
86 */
87static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
88{
89 if (axis > 0)
90 return hw_values[axis - 1];
91 else
92 return -hw_values[-axis - 1];
93}
94
95/**
96 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -070097 * @lis3: pointer to the device struct
98 * @x: where to store the X axis value
99 * @y: where to store the Y axis value
100 * @z: where to store the Z axis value
Pavel Machek455fbdd2008-11-12 13:27:02 -0800101 *
102 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
103 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700104static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800105{
106 int position[3];
107
Eric Piela002ee82009-06-16 15:34:14 -0700108 position[0] = lis3->read_data(lis3, OUTX);
109 position[1] = lis3->read_data(lis3, OUTY);
110 position[2] = lis3->read_data(lis3, OUTZ);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800111
Eric Piela002ee82009-06-16 15:34:14 -0700112 *x = lis3lv02d_get_axis(lis3->ac.x, position);
113 *y = lis3lv02d_get_axis(lis3->ac.y, position);
114 *z = lis3lv02d_get_axis(lis3->ac.z, position);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800115}
116
Daniel Macka38da2e2009-03-31 15:24:32 -0700117void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800118{
Eric Piela002ee82009-06-16 15:34:14 -0700119 /* disable X,Y,Z axis and power down */
120 lis3->write(lis3, CTRL_REG1, 0x00);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800121}
Eric Pielcfce41a2009-01-09 16:41:01 -0800122EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800123
Daniel Macka38da2e2009-03-31 15:24:32 -0700124void lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800125{
Eric Piela002ee82009-06-16 15:34:14 -0700126 u8 reg;
127
128 lis3->init(lis3);
129
130 /*
131 * Common configuration
132 * BDU: LSB and MSB values are not updated until both have been read.
133 * So the value read will always be correct.
134 */
135 lis3->read(lis3, CTRL_REG2, &reg);
136 reg |= CTRL2_BDU;
137 lis3->write(lis3, CTRL_REG2, reg);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800138}
Eric Pielcfce41a2009-01-09 16:41:01 -0800139EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800140
Pavel Machek455fbdd2008-11-12 13:27:02 -0800141
Pavel Machekef2cfc72009-02-18 14:48:23 -0800142static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
143{
144 /*
145 * Be careful: on some HP laptops the bios force DD when on battery and
146 * the lid is closed. This leads to interrupts as soon as a little move
147 * is done.
148 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700149 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800150
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700151 wake_up_interruptible(&lis3_dev.misc_wait);
152 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800153 return IRQ_HANDLED;
154}
155
156static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
157{
158 int ret;
159
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700160 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800161 return -EBUSY; /* already open */
162
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700163 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800164
165 /*
166 * The sensor can generate interrupts for free-fall and direction
167 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
168 * the things simple and _fast_ we activate it only for free-fall, so
169 * no need to read register (very slow with ACPI). For the same reason,
170 * we forbid shared interrupts.
171 *
172 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
173 * io-apic is not configurable (and generates a warning) but I keep it
174 * in case of support for other hardware.
175 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700176 ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
177 DRIVER_NAME, &lis3_dev);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800178
179 if (ret) {
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700180 clear_bit(0, &lis3_dev.misc_opened);
181 printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", lis3_dev.irq);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800182 return -EBUSY;
183 }
Pavel Machekef2cfc72009-02-18 14:48:23 -0800184 return 0;
185}
186
187static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
188{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700189 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700190 free_irq(lis3_dev.irq, &lis3_dev);
191 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Pavel Machekef2cfc72009-02-18 14:48:23 -0800192 return 0;
193}
194
195static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
196 size_t count, loff_t *pos)
197{
198 DECLARE_WAITQUEUE(wait, current);
199 u32 data;
200 unsigned char byte_data;
201 ssize_t retval = 1;
202
203 if (count < 1)
204 return -EINVAL;
205
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700206 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800207 while (true) {
208 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700209 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800210 if (data)
211 break;
212
213 if (file->f_flags & O_NONBLOCK) {
214 retval = -EAGAIN;
215 goto out;
216 }
217
218 if (signal_pending(current)) {
219 retval = -ERESTARTSYS;
220 goto out;
221 }
222
223 schedule();
224 }
225
226 if (data < 255)
227 byte_data = data;
228 else
229 byte_data = 255;
230
231 /* make sure we are not going into copy_to_user() with
232 * TASK_INTERRUPTIBLE state */
233 set_current_state(TASK_RUNNING);
234 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
235 retval = -EFAULT;
236
237out:
238 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700239 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800240
241 return retval;
242}
243
244static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
245{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700246 poll_wait(file, &lis3_dev.misc_wait, wait);
247 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800248 return POLLIN | POLLRDNORM;
249 return 0;
250}
251
252static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
253{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700254 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800255}
256
257static const struct file_operations lis3lv02d_misc_fops = {
258 .owner = THIS_MODULE,
259 .llseek = no_llseek,
260 .read = lis3lv02d_misc_read,
261 .open = lis3lv02d_misc_open,
262 .release = lis3lv02d_misc_release,
263 .poll = lis3lv02d_misc_poll,
264 .fasync = lis3lv02d_misc_fasync,
265};
266
267static struct miscdevice lis3lv02d_misc_device = {
268 .minor = MISC_DYNAMIC_MINOR,
269 .name = "freefall",
270 .fops = &lis3lv02d_misc_fops,
271};
272
Pavel Machek455fbdd2008-11-12 13:27:02 -0800273/**
274 * lis3lv02d_joystick_kthread - Kthread polling function
275 * @data: unused - here to conform to threadfn prototype
276 */
277static int lis3lv02d_joystick_kthread(void *data)
278{
279 int x, y, z;
280
281 while (!kthread_should_stop()) {
Daniel Macka38da2e2009-03-31 15:24:32 -0700282 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700283 input_report_abs(lis3_dev.idev, ABS_X, x - lis3_dev.xcalib);
284 input_report_abs(lis3_dev.idev, ABS_Y, y - lis3_dev.ycalib);
285 input_report_abs(lis3_dev.idev, ABS_Z, z - lis3_dev.zcalib);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800286
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700287 input_sync(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800288
289 try_to_freeze();
290 msleep_interruptible(MDPS_POLL_INTERVAL);
291 }
292
293 return 0;
294}
295
296static int lis3lv02d_joystick_open(struct input_dev *input)
297{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700298 lis3_dev.kthread = kthread_run(lis3lv02d_joystick_kthread, NULL, "klis3lv02d");
299 if (IS_ERR(lis3_dev.kthread)) {
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700300 return PTR_ERR(lis3_dev.kthread);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800301 }
302
303 return 0;
304}
305
306static void lis3lv02d_joystick_close(struct input_dev *input)
307{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700308 kthread_stop(lis3_dev.kthread);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800309}
310
Pavel Machek455fbdd2008-11-12 13:27:02 -0800311static inline void lis3lv02d_calibrate_joystick(void)
312{
Daniel Macka38da2e2009-03-31 15:24:32 -0700313 lis3lv02d_get_xyz(&lis3_dev,
314 &lis3_dev.xcalib, &lis3_dev.ycalib, &lis3_dev.zcalib);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800315}
316
Eric Pielcfce41a2009-01-09 16:41:01 -0800317int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800318{
319 int err;
320
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700321 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800322 return -EINVAL;
323
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700324 lis3_dev.idev = input_allocate_device();
325 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800326 return -ENOMEM;
327
328 lis3lv02d_calibrate_joystick();
329
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700330 lis3_dev.idev->name = "ST LIS3LV02DL Accelerometer";
331 lis3_dev.idev->phys = DRIVER_NAME "/input0";
332 lis3_dev.idev->id.bustype = BUS_HOST;
333 lis3_dev.idev->id.vendor = 0;
334 lis3_dev.idev->dev.parent = &lis3_dev.pdev->dev;
335 lis3_dev.idev->open = lis3lv02d_joystick_open;
336 lis3_dev.idev->close = lis3lv02d_joystick_close;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800337
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700338 set_bit(EV_ABS, lis3_dev.idev->evbit);
339 input_set_abs_params(lis3_dev.idev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
340 input_set_abs_params(lis3_dev.idev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
341 input_set_abs_params(lis3_dev.idev, ABS_Z, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800342
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700343 err = input_register_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800344 if (err) {
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700345 input_free_device(lis3_dev.idev);
346 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800347 }
348
349 return err;
350}
Eric Pielcfce41a2009-01-09 16:41:01 -0800351EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800352
Eric Pielcfce41a2009-01-09 16:41:01 -0800353void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800354{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700355 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800356 return;
357
Eric Pielc2884242009-06-16 15:34:13 -0700358 if (lis3_dev.irq)
359 misc_deregister(&lis3lv02d_misc_device);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700360 input_unregister_device(lis3_dev.idev);
361 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800362}
Eric Pielcfce41a2009-01-09 16:41:01 -0800363EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800364
Pavel Machek455fbdd2008-11-12 13:27:02 -0800365/* Sysfs stuff */
366static ssize_t lis3lv02d_position_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
368{
369 int x, y, z;
370
Daniel Macka38da2e2009-03-31 15:24:32 -0700371 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800372 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
373}
374
375static ssize_t lis3lv02d_calibrate_show(struct device *dev,
376 struct device_attribute *attr, char *buf)
377{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700378 return sprintf(buf, "(%d,%d,%d)\n", lis3_dev.xcalib, lis3_dev.ycalib, lis3_dev.zcalib);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800379}
380
381static ssize_t lis3lv02d_calibrate_store(struct device *dev,
382 struct device_attribute *attr,
383 const char *buf, size_t count)
384{
Pavel Machek455fbdd2008-11-12 13:27:02 -0800385 lis3lv02d_calibrate_joystick();
Pavel Machek455fbdd2008-11-12 13:27:02 -0800386 return count;
387}
388
389/* conversion btw sampling rate and the register values */
390static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
391static ssize_t lis3lv02d_rate_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
393{
394 u8 ctrl;
395 int val;
396
Daniel Macka38da2e2009-03-31 15:24:32 -0700397 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800398 val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
399 return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
400}
401
402static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
403static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
404 lis3lv02d_calibrate_store);
405static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
406
407static struct attribute *lis3lv02d_attributes[] = {
408 &dev_attr_position.attr,
409 &dev_attr_calibrate.attr,
410 &dev_attr_rate.attr,
411 NULL
412};
413
414static struct attribute_group lis3lv02d_attribute_group = {
415 .attrs = lis3lv02d_attributes
416};
417
Eric Pielcfce41a2009-01-09 16:41:01 -0800418
Daniel Macka38da2e2009-03-31 15:24:32 -0700419static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800420{
Eric Piela002ee82009-06-16 15:34:14 -0700421 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
422 if (IS_ERR(lis3->pdev))
423 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800424
Eric Piela002ee82009-06-16 15:34:14 -0700425 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800426}
427
Eric Piela002ee82009-06-16 15:34:14 -0700428int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800429{
Eric Piela002ee82009-06-16 15:34:14 -0700430 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
431 platform_device_unregister(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800432 return 0;
433}
Eric Pielcfce41a2009-01-09 16:41:01 -0800434EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800435
Daniel Mackab337a62009-03-31 15:24:31 -0700436/*
437 * Initialise the accelerometer and the various subsystems.
438 * Should be rather independant of the bus system.
439 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700440int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700441{
Daniel Macka38da2e2009-03-31 15:24:32 -0700442 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
443
444 switch (dev->whoami) {
445 case LIS_DOUBLE_ID:
446 printk(KERN_INFO DRIVER_NAME ": 2-byte sensor found\n");
447 dev->read_data = lis3lv02d_read_16;
448 dev->mdps_max_val = 2048;
449 break;
450 case LIS_SINGLE_ID:
451 printk(KERN_INFO DRIVER_NAME ": 1-byte sensor found\n");
452 dev->read_data = lis3lv02d_read_8;
453 dev->mdps_max_val = 128;
454 break;
455 default:
456 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700457 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700458 return -EINVAL;
459 }
460
Daniel Macka38da2e2009-03-31 15:24:32 -0700461 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700462 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700463
464 if (lis3lv02d_joystick_enable())
465 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
466
Daniel Macka38da2e2009-03-31 15:24:32 -0700467 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700468 if (!dev->irq) {
469 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700470 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700471 goto out;
472 }
473
Daniel Mackab337a62009-03-31 15:24:31 -0700474 if (misc_register(&lis3lv02d_misc_device))
475 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
476out:
Daniel Mackab337a62009-03-31 15:24:31 -0700477 return 0;
478}
479EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
480
Pavel Machek455fbdd2008-11-12 13:27:02 -0800481MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800482MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800483MODULE_LICENSE("GPL");
484