Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  lis3lv02d.c - ST LIS3LV02DL accelerometer driver
0004  *
0005  *  Copyright (C) 2007-2008 Yan Burman
0006  *  Copyright (C) 2008 Eric Piel
0007  *  Copyright (C) 2008-2009 Pavel Machek
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/sched/signal.h>
0014 #include <linux/dmi.h>
0015 #include <linux/module.h>
0016 #include <linux/types.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/input.h>
0020 #include <linux/delay.h>
0021 #include <linux/wait.h>
0022 #include <linux/poll.h>
0023 #include <linux/slab.h>
0024 #include <linux/freezer.h>
0025 #include <linux/uaccess.h>
0026 #include <linux/miscdevice.h>
0027 #include <linux/pm_runtime.h>
0028 #include <linux/atomic.h>
0029 #include <linux/of_device.h>
0030 #include "lis3lv02d.h"
0031 
0032 #define DRIVER_NAME     "lis3lv02d"
0033 
0034 /* joystick device poll interval in milliseconds */
0035 #define MDPS_POLL_INTERVAL 50
0036 #define MDPS_POLL_MIN      0
0037 #define MDPS_POLL_MAX      2000
0038 
0039 #define LIS3_SYSFS_POWERDOWN_DELAY 5000 /* In milliseconds */
0040 
0041 #define SELFTEST_OK        0
0042 #define SELFTEST_FAIL          -1
0043 #define SELFTEST_IRQ           -2
0044 
0045 #define IRQ_LINE0          0
0046 #define IRQ_LINE1          1
0047 
0048 /*
0049  * The sensor can also generate interrupts (DRDY) but it's pretty pointless
0050  * because they are generated even if the data do not change. So it's better
0051  * to keep the interrupt for the free-fall event. The values are updated at
0052  * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
0053  * some low processor, we poll the sensor only at 20Hz... enough for the
0054  * joystick.
0055  */
0056 
0057 #define LIS3_PWRON_DELAY_WAI_12B    (5000)
0058 #define LIS3_PWRON_DELAY_WAI_8B     (3000)
0059 
0060 /*
0061  * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
0062  * LIS302D spec says: 18 mG / digit
0063  * LIS3_ACCURACY is used to increase accuracy of the intermediate
0064  * calculation results.
0065  */
0066 #define LIS3_ACCURACY           1024
0067 /* Sensitivity values for -2G +2G scale */
0068 #define LIS3_SENSITIVITY_12B        ((LIS3_ACCURACY * 1000) / 1024)
0069 #define LIS3_SENSITIVITY_8B     (18 * LIS3_ACCURACY)
0070 
0071 /*
0072  * LIS331DLH spec says 1LSBs corresponds 4G/4096 -> 1LSB is 1000/1024 mG.
0073  * Below macros defines sensitivity values for +/-2G. Dataout bits for
0074  * +/-2G range is 12 bits so 4 bits adjustment must be done to get 12bit
0075  * data from 16bit value. Currently this driver supports only 2G range.
0076  */
0077 #define LIS3DLH_SENSITIVITY_2G      ((LIS3_ACCURACY * 1000) / 1024)
0078 #define SHIFT_ADJ_2G            4
0079 
0080 #define LIS3_DEFAULT_FUZZ_12B       3
0081 #define LIS3_DEFAULT_FLAT_12B       3
0082 #define LIS3_DEFAULT_FUZZ_8B        1
0083 #define LIS3_DEFAULT_FLAT_8B        1
0084 
0085 struct lis3lv02d lis3_dev = {
0086     .misc_wait   = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
0087 };
0088 EXPORT_SYMBOL_GPL(lis3_dev);
0089 
0090 /* just like param_set_int() but does sanity-check so that it won't point
0091  * over the axis array size
0092  */
0093 static int param_set_axis(const char *val, const struct kernel_param *kp)
0094 {
0095     int ret = param_set_int(val, kp);
0096     if (!ret) {
0097         int val = *(int *)kp->arg;
0098         if (val < 0)
0099             val = -val;
0100         if (!val || val > 3)
0101             return -EINVAL;
0102     }
0103     return ret;
0104 }
0105 
0106 static const struct kernel_param_ops param_ops_axis = {
0107     .set = param_set_axis,
0108     .get = param_get_int,
0109 };
0110 
0111 #define param_check_axis(name, p) param_check_int(name, p)
0112 
0113 module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
0114 MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
0115 
0116 static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
0117 {
0118     s8 lo;
0119     if (lis3->read(lis3, reg, &lo) < 0)
0120         return 0;
0121 
0122     return lo;
0123 }
0124 
0125 static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
0126 {
0127     u8 lo, hi;
0128 
0129     lis3->read(lis3, reg - 1, &lo);
0130     lis3->read(lis3, reg, &hi);
0131     /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
0132     return (s16)((hi << 8) | lo);
0133 }
0134 
0135 /* 12bits for 2G range, 13 bits for 4G range and 14 bits for 8G range */
0136 static s16 lis331dlh_read_data(struct lis3lv02d *lis3, int reg)
0137 {
0138     u8 lo, hi;
0139     int v;
0140 
0141     lis3->read(lis3, reg - 1, &lo);
0142     lis3->read(lis3, reg, &hi);
0143     v = (int) ((hi << 8) | lo);
0144 
0145     return (s16) v >> lis3->shift_adj;
0146 }
0147 
0148 /**
0149  * lis3lv02d_get_axis - For the given axis, give the value converted
0150  * @axis:      1,2,3 - can also be negative
0151  * @hw_values: raw values returned by the hardware
0152  *
0153  * Returns the converted value.
0154  */
0155 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
0156 {
0157     if (axis > 0)
0158         return hw_values[axis - 1];
0159     else
0160         return -hw_values[-axis - 1];
0161 }
0162 
0163 /**
0164  * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
0165  * @lis3: pointer to the device struct
0166  * @x:    where to store the X axis value
0167  * @y:    where to store the Y axis value
0168  * @z:    where to store the Z axis value
0169  *
0170  * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
0171  */
0172 static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
0173 {
0174     int position[3];
0175     int i;
0176 
0177     if (lis3->blkread) {
0178         if (lis3->whoami == WAI_12B) {
0179             u16 data[3];
0180             lis3->blkread(lis3, OUTX_L, 6, (u8 *)data);
0181             for (i = 0; i < 3; i++)
0182                 position[i] = (s16)le16_to_cpu(data[i]);
0183         } else {
0184             u8 data[5];
0185             /* Data: x, dummy, y, dummy, z */
0186             lis3->blkread(lis3, OUTX, 5, data);
0187             for (i = 0; i < 3; i++)
0188                 position[i] = (s8)data[i * 2];
0189         }
0190     } else {
0191         position[0] = lis3->read_data(lis3, OUTX);
0192         position[1] = lis3->read_data(lis3, OUTY);
0193         position[2] = lis3->read_data(lis3, OUTZ);
0194     }
0195 
0196     for (i = 0; i < 3; i++)
0197         position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
0198 
0199     *x = lis3lv02d_get_axis(lis3->ac.x, position);
0200     *y = lis3lv02d_get_axis(lis3->ac.y, position);
0201     *z = lis3lv02d_get_axis(lis3->ac.z, position);
0202 }
0203 
0204 /* conversion btw sampling rate and the register values */
0205 static int lis3_12_rates[4] = {40, 160, 640, 2560};
0206 static int lis3_8_rates[2] = {100, 400};
0207 static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
0208 static int lis3_3dlh_rates[4] = {50, 100, 400, 1000};
0209 
0210 /* ODR is Output Data Rate */
0211 static int lis3lv02d_get_odr_index(struct lis3lv02d *lis3)
0212 {
0213     u8 ctrl;
0214     int shift;
0215 
0216     lis3->read(lis3, CTRL_REG1, &ctrl);
0217     ctrl &= lis3->odr_mask;
0218     shift = ffs(lis3->odr_mask) - 1;
0219     return (ctrl >> shift);
0220 }
0221 
0222 static int lis3lv02d_get_pwron_wait(struct lis3lv02d *lis3)
0223 {
0224     int odr_idx = lis3lv02d_get_odr_index(lis3);
0225     int div = lis3->odrs[odr_idx];
0226 
0227     if (div == 0) {
0228         if (odr_idx == 0) {
0229             /* Power-down mode, not sampling no need to sleep */
0230             return 0;
0231         }
0232 
0233         dev_err(&lis3->pdev->dev, "Error unknown odrs-index: %d\n", odr_idx);
0234         return -ENXIO;
0235     }
0236 
0237     /* LIS3 power on delay is quite long */
0238     msleep(lis3->pwron_delay / div);
0239     return 0;
0240 }
0241 
0242 static int lis3lv02d_set_odr(struct lis3lv02d *lis3, int rate)
0243 {
0244     u8 ctrl;
0245     int i, len, shift;
0246 
0247     if (!rate)
0248         return -EINVAL;
0249 
0250     lis3->read(lis3, CTRL_REG1, &ctrl);
0251     ctrl &= ~lis3->odr_mask;
0252     len = 1 << hweight_long(lis3->odr_mask); /* # of possible values */
0253     shift = ffs(lis3->odr_mask) - 1;
0254 
0255     for (i = 0; i < len; i++)
0256         if (lis3->odrs[i] == rate) {
0257             lis3->write(lis3, CTRL_REG1,
0258                     ctrl | (i << shift));
0259             return 0;
0260         }
0261     return -EINVAL;
0262 }
0263 
0264 static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
0265 {
0266     u8 ctlreg, reg;
0267     s16 x, y, z;
0268     u8 selftest;
0269     int ret;
0270     u8 ctrl_reg_data;
0271     unsigned char irq_cfg;
0272 
0273     mutex_lock(&lis3->mutex);
0274 
0275     irq_cfg = lis3->irq_cfg;
0276     if (lis3->whoami == WAI_8B) {
0277         lis3->data_ready_count[IRQ_LINE0] = 0;
0278         lis3->data_ready_count[IRQ_LINE1] = 0;
0279 
0280         /* Change interrupt cfg to data ready for selftest */
0281         atomic_inc(&lis3->wake_thread);
0282         lis3->irq_cfg = LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY;
0283         lis3->read(lis3, CTRL_REG3, &ctrl_reg_data);
0284         lis3->write(lis3, CTRL_REG3, (ctrl_reg_data &
0285                 ~(LIS3_IRQ1_MASK | LIS3_IRQ2_MASK)) |
0286                 (LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY));
0287     }
0288 
0289     if ((lis3->whoami == WAI_3DC) || (lis3->whoami == WAI_3DLH)) {
0290         ctlreg = CTRL_REG4;
0291         selftest = CTRL4_ST0;
0292     } else {
0293         ctlreg = CTRL_REG1;
0294         if (lis3->whoami == WAI_12B)
0295             selftest = CTRL1_ST;
0296         else
0297             selftest = CTRL1_STP;
0298     }
0299 
0300     lis3->read(lis3, ctlreg, &reg);
0301     lis3->write(lis3, ctlreg, (reg | selftest));
0302     ret = lis3lv02d_get_pwron_wait(lis3);
0303     if (ret)
0304         goto fail;
0305 
0306     /* Read directly to avoid axis remap */
0307     x = lis3->read_data(lis3, OUTX);
0308     y = lis3->read_data(lis3, OUTY);
0309     z = lis3->read_data(lis3, OUTZ);
0310 
0311     /* back to normal settings */
0312     lis3->write(lis3, ctlreg, reg);
0313     ret = lis3lv02d_get_pwron_wait(lis3);
0314     if (ret)
0315         goto fail;
0316 
0317     results[0] = x - lis3->read_data(lis3, OUTX);
0318     results[1] = y - lis3->read_data(lis3, OUTY);
0319     results[2] = z - lis3->read_data(lis3, OUTZ);
0320 
0321     ret = 0;
0322 
0323     if (lis3->whoami == WAI_8B) {
0324         /* Restore original interrupt configuration */
0325         atomic_dec(&lis3->wake_thread);
0326         lis3->write(lis3, CTRL_REG3, ctrl_reg_data);
0327         lis3->irq_cfg = irq_cfg;
0328 
0329         if ((irq_cfg & LIS3_IRQ1_MASK) &&
0330             lis3->data_ready_count[IRQ_LINE0] < 2) {
0331             ret = SELFTEST_IRQ;
0332             goto fail;
0333         }
0334 
0335         if ((irq_cfg & LIS3_IRQ2_MASK) &&
0336             lis3->data_ready_count[IRQ_LINE1] < 2) {
0337             ret = SELFTEST_IRQ;
0338             goto fail;
0339         }
0340     }
0341 
0342     if (lis3->pdata) {
0343         int i;
0344         for (i = 0; i < 3; i++) {
0345             /* Check against selftest acceptance limits */
0346             if ((results[i] < lis3->pdata->st_min_limits[i]) ||
0347                 (results[i] > lis3->pdata->st_max_limits[i])) {
0348                 ret = SELFTEST_FAIL;
0349                 goto fail;
0350             }
0351         }
0352     }
0353 
0354     /* test passed */
0355 fail:
0356     mutex_unlock(&lis3->mutex);
0357     return ret;
0358 }
0359 
0360 /*
0361  * Order of registers in the list affects to order of the restore process.
0362  * Perhaps it is a good idea to set interrupt enable register as a last one
0363  * after all other configurations
0364  */
0365 static u8 lis3_wai8_regs[] = { FF_WU_CFG_1, FF_WU_THS_1, FF_WU_DURATION_1,
0366                    FF_WU_CFG_2, FF_WU_THS_2, FF_WU_DURATION_2,
0367                    CLICK_CFG, CLICK_SRC, CLICK_THSY_X, CLICK_THSZ,
0368                    CLICK_TIMELIMIT, CLICK_LATENCY, CLICK_WINDOW,
0369                    CTRL_REG1, CTRL_REG2, CTRL_REG3};
0370 
0371 static u8 lis3_wai12_regs[] = {FF_WU_CFG, FF_WU_THS_L, FF_WU_THS_H,
0372                    FF_WU_DURATION, DD_CFG, DD_THSI_L, DD_THSI_H,
0373                    DD_THSE_L, DD_THSE_H,
0374                    CTRL_REG1, CTRL_REG3, CTRL_REG2};
0375 
0376 static inline void lis3_context_save(struct lis3lv02d *lis3)
0377 {
0378     int i;
0379     for (i = 0; i < lis3->regs_size; i++)
0380         lis3->read(lis3, lis3->regs[i], &lis3->reg_cache[i]);
0381     lis3->regs_stored = true;
0382 }
0383 
0384 static inline void lis3_context_restore(struct lis3lv02d *lis3)
0385 {
0386     int i;
0387     if (lis3->regs_stored)
0388         for (i = 0; i < lis3->regs_size; i++)
0389             lis3->write(lis3, lis3->regs[i], lis3->reg_cache[i]);
0390 }
0391 
0392 void lis3lv02d_poweroff(struct lis3lv02d *lis3)
0393 {
0394     if (lis3->reg_ctrl)
0395         lis3_context_save(lis3);
0396     /* disable X,Y,Z axis and power down */
0397     lis3->write(lis3, CTRL_REG1, 0x00);
0398     if (lis3->reg_ctrl)
0399         lis3->reg_ctrl(lis3, LIS3_REG_OFF);
0400 }
0401 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
0402 
0403 int lis3lv02d_poweron(struct lis3lv02d *lis3)
0404 {
0405     int err;
0406     u8 reg;
0407 
0408     lis3->init(lis3);
0409 
0410     /*
0411      * Common configuration
0412      * BDU: (12 bits sensors only) LSB and MSB values are not updated until
0413      *      both have been read. So the value read will always be correct.
0414      * Set BOOT bit to refresh factory tuning values.
0415      */
0416     if (lis3->pdata) {
0417         lis3->read(lis3, CTRL_REG2, &reg);
0418         if (lis3->whoami ==  WAI_12B)
0419             reg |= CTRL2_BDU | CTRL2_BOOT;
0420         else if (lis3->whoami ==  WAI_3DLH)
0421             reg |= CTRL2_BOOT_3DLH;
0422         else
0423             reg |= CTRL2_BOOT_8B;
0424         lis3->write(lis3, CTRL_REG2, reg);
0425 
0426         if (lis3->whoami ==  WAI_3DLH) {
0427             lis3->read(lis3, CTRL_REG4, &reg);
0428             reg |= CTRL4_BDU;
0429             lis3->write(lis3, CTRL_REG4, reg);
0430         }
0431     }
0432 
0433     err = lis3lv02d_get_pwron_wait(lis3);
0434     if (err)
0435         return err;
0436 
0437     if (lis3->reg_ctrl)
0438         lis3_context_restore(lis3);
0439 
0440     return 0;
0441 }
0442 EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
0443 
0444 
0445 static void lis3lv02d_joystick_poll(struct input_dev *input)
0446 {
0447     struct lis3lv02d *lis3 = input_get_drvdata(input);
0448     int x, y, z;
0449 
0450     mutex_lock(&lis3->mutex);
0451     lis3lv02d_get_xyz(lis3, &x, &y, &z);
0452     input_report_abs(input, ABS_X, x);
0453     input_report_abs(input, ABS_Y, y);
0454     input_report_abs(input, ABS_Z, z);
0455     input_sync(input);
0456     mutex_unlock(&lis3->mutex);
0457 }
0458 
0459 static int lis3lv02d_joystick_open(struct input_dev *input)
0460 {
0461     struct lis3lv02d *lis3 = input_get_drvdata(input);
0462 
0463     if (lis3->pm_dev)
0464         pm_runtime_get_sync(lis3->pm_dev);
0465 
0466     if (lis3->pdata && lis3->whoami == WAI_8B && lis3->idev)
0467         atomic_set(&lis3->wake_thread, 1);
0468     /*
0469      * Update coordinates for the case where poll interval is 0 and
0470      * the chip in running purely under interrupt control
0471      */
0472     lis3lv02d_joystick_poll(input);
0473 
0474     return 0;
0475 }
0476 
0477 static void lis3lv02d_joystick_close(struct input_dev *input)
0478 {
0479     struct lis3lv02d *lis3 = input_get_drvdata(input);
0480 
0481     atomic_set(&lis3->wake_thread, 0);
0482     if (lis3->pm_dev)
0483         pm_runtime_put(lis3->pm_dev);
0484 }
0485 
0486 static irqreturn_t lis302dl_interrupt(int irq, void *data)
0487 {
0488     struct lis3lv02d *lis3 = data;
0489 
0490     if (!test_bit(0, &lis3->misc_opened))
0491         goto out;
0492 
0493     /*
0494      * Be careful: on some HP laptops the bios force DD when on battery and
0495      * the lid is closed. This leads to interrupts as soon as a little move
0496      * is done.
0497      */
0498     atomic_inc(&lis3->count);
0499 
0500     wake_up_interruptible(&lis3->misc_wait);
0501     kill_fasync(&lis3->async_queue, SIGIO, POLL_IN);
0502 out:
0503     if (atomic_read(&lis3->wake_thread))
0504         return IRQ_WAKE_THREAD;
0505     return IRQ_HANDLED;
0506 }
0507 
0508 static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
0509 {
0510     struct input_dev *dev = lis3->idev;
0511     u8 click_src;
0512 
0513     mutex_lock(&lis3->mutex);
0514     lis3->read(lis3, CLICK_SRC, &click_src);
0515 
0516     if (click_src & CLICK_SINGLE_X) {
0517         input_report_key(dev, lis3->mapped_btns[0], 1);
0518         input_report_key(dev, lis3->mapped_btns[0], 0);
0519     }
0520 
0521     if (click_src & CLICK_SINGLE_Y) {
0522         input_report_key(dev, lis3->mapped_btns[1], 1);
0523         input_report_key(dev, lis3->mapped_btns[1], 0);
0524     }
0525 
0526     if (click_src & CLICK_SINGLE_Z) {
0527         input_report_key(dev, lis3->mapped_btns[2], 1);
0528         input_report_key(dev, lis3->mapped_btns[2], 0);
0529     }
0530     input_sync(dev);
0531     mutex_unlock(&lis3->mutex);
0532 }
0533 
0534 static inline void lis302dl_data_ready(struct lis3lv02d *lis3, int index)
0535 {
0536     int dummy;
0537 
0538     /* Dummy read to ack interrupt */
0539     lis3lv02d_get_xyz(lis3, &dummy, &dummy, &dummy);
0540     lis3->data_ready_count[index]++;
0541 }
0542 
0543 static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
0544 {
0545     struct lis3lv02d *lis3 = data;
0546     u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ1_MASK;
0547 
0548     if (irq_cfg == LIS3_IRQ1_CLICK)
0549         lis302dl_interrupt_handle_click(lis3);
0550     else if (unlikely(irq_cfg == LIS3_IRQ1_DATA_READY))
0551         lis302dl_data_ready(lis3, IRQ_LINE0);
0552     else
0553         lis3lv02d_joystick_poll(lis3->idev);
0554 
0555     return IRQ_HANDLED;
0556 }
0557 
0558 static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
0559 {
0560     struct lis3lv02d *lis3 = data;
0561     u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ2_MASK;
0562 
0563     if (irq_cfg == LIS3_IRQ2_CLICK)
0564         lis302dl_interrupt_handle_click(lis3);
0565     else if (unlikely(irq_cfg == LIS3_IRQ2_DATA_READY))
0566         lis302dl_data_ready(lis3, IRQ_LINE1);
0567     else
0568         lis3lv02d_joystick_poll(lis3->idev);
0569 
0570     return IRQ_HANDLED;
0571 }
0572 
0573 static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
0574 {
0575     struct lis3lv02d *lis3 = container_of(file->private_data,
0576                           struct lis3lv02d, miscdev);
0577 
0578     if (test_and_set_bit(0, &lis3->misc_opened))
0579         return -EBUSY; /* already open */
0580 
0581     if (lis3->pm_dev)
0582         pm_runtime_get_sync(lis3->pm_dev);
0583 
0584     atomic_set(&lis3->count, 0);
0585     return 0;
0586 }
0587 
0588 static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
0589 {
0590     struct lis3lv02d *lis3 = container_of(file->private_data,
0591                           struct lis3lv02d, miscdev);
0592 
0593     clear_bit(0, &lis3->misc_opened); /* release the device */
0594     if (lis3->pm_dev)
0595         pm_runtime_put(lis3->pm_dev);
0596     return 0;
0597 }
0598 
0599 static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
0600                 size_t count, loff_t *pos)
0601 {
0602     struct lis3lv02d *lis3 = container_of(file->private_data,
0603                           struct lis3lv02d, miscdev);
0604 
0605     DECLARE_WAITQUEUE(wait, current);
0606     u32 data;
0607     unsigned char byte_data;
0608     ssize_t retval = 1;
0609 
0610     if (count < 1)
0611         return -EINVAL;
0612 
0613     add_wait_queue(&lis3->misc_wait, &wait);
0614     while (true) {
0615         set_current_state(TASK_INTERRUPTIBLE);
0616         data = atomic_xchg(&lis3->count, 0);
0617         if (data)
0618             break;
0619 
0620         if (file->f_flags & O_NONBLOCK) {
0621             retval = -EAGAIN;
0622             goto out;
0623         }
0624 
0625         if (signal_pending(current)) {
0626             retval = -ERESTARTSYS;
0627             goto out;
0628         }
0629 
0630         schedule();
0631     }
0632 
0633     if (data < 255)
0634         byte_data = data;
0635     else
0636         byte_data = 255;
0637 
0638     /* make sure we are not going into copy_to_user() with
0639      * TASK_INTERRUPTIBLE state */
0640     set_current_state(TASK_RUNNING);
0641     if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
0642         retval = -EFAULT;
0643 
0644 out:
0645     __set_current_state(TASK_RUNNING);
0646     remove_wait_queue(&lis3->misc_wait, &wait);
0647 
0648     return retval;
0649 }
0650 
0651 static __poll_t lis3lv02d_misc_poll(struct file *file, poll_table *wait)
0652 {
0653     struct lis3lv02d *lis3 = container_of(file->private_data,
0654                           struct lis3lv02d, miscdev);
0655 
0656     poll_wait(file, &lis3->misc_wait, wait);
0657     if (atomic_read(&lis3->count))
0658         return EPOLLIN | EPOLLRDNORM;
0659     return 0;
0660 }
0661 
0662 static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
0663 {
0664     struct lis3lv02d *lis3 = container_of(file->private_data,
0665                           struct lis3lv02d, miscdev);
0666 
0667     return fasync_helper(fd, file, on, &lis3->async_queue);
0668 }
0669 
0670 static const struct file_operations lis3lv02d_misc_fops = {
0671     .owner   = THIS_MODULE,
0672     .llseek  = no_llseek,
0673     .read    = lis3lv02d_misc_read,
0674     .open    = lis3lv02d_misc_open,
0675     .release = lis3lv02d_misc_release,
0676     .poll    = lis3lv02d_misc_poll,
0677     .fasync  = lis3lv02d_misc_fasync,
0678 };
0679 
0680 int lis3lv02d_joystick_enable(struct lis3lv02d *lis3)
0681 {
0682     struct input_dev *input_dev;
0683     int err;
0684     int max_val, fuzz, flat;
0685     int btns[] = {BTN_X, BTN_Y, BTN_Z};
0686 
0687     if (lis3->idev)
0688         return -EINVAL;
0689 
0690     input_dev = input_allocate_device();
0691     if (!input_dev)
0692         return -ENOMEM;
0693 
0694     input_dev->name       = "ST LIS3LV02DL Accelerometer";
0695     input_dev->phys       = DRIVER_NAME "/input0";
0696     input_dev->id.bustype = BUS_HOST;
0697     input_dev->id.vendor  = 0;
0698     input_dev->dev.parent = &lis3->pdev->dev;
0699 
0700     input_dev->open = lis3lv02d_joystick_open;
0701     input_dev->close = lis3lv02d_joystick_close;
0702 
0703     max_val = (lis3->mdps_max_val * lis3->scale) / LIS3_ACCURACY;
0704     if (lis3->whoami == WAI_12B) {
0705         fuzz = LIS3_DEFAULT_FUZZ_12B;
0706         flat = LIS3_DEFAULT_FLAT_12B;
0707     } else {
0708         fuzz = LIS3_DEFAULT_FUZZ_8B;
0709         flat = LIS3_DEFAULT_FLAT_8B;
0710     }
0711     fuzz = (fuzz * lis3->scale) / LIS3_ACCURACY;
0712     flat = (flat * lis3->scale) / LIS3_ACCURACY;
0713 
0714     input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
0715     input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
0716     input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
0717 
0718     input_set_drvdata(input_dev, lis3);
0719     lis3->idev = input_dev;
0720 
0721     err = input_setup_polling(input_dev, lis3lv02d_joystick_poll);
0722     if (err)
0723         goto err_free_input;
0724 
0725     input_set_poll_interval(input_dev, MDPS_POLL_INTERVAL);
0726     input_set_min_poll_interval(input_dev, MDPS_POLL_MIN);
0727     input_set_max_poll_interval(input_dev, MDPS_POLL_MAX);
0728 
0729     lis3->mapped_btns[0] = lis3lv02d_get_axis(abs(lis3->ac.x), btns);
0730     lis3->mapped_btns[1] = lis3lv02d_get_axis(abs(lis3->ac.y), btns);
0731     lis3->mapped_btns[2] = lis3lv02d_get_axis(abs(lis3->ac.z), btns);
0732 
0733     err = input_register_device(lis3->idev);
0734     if (err)
0735         goto err_free_input;
0736 
0737     return 0;
0738 
0739 err_free_input:
0740     input_free_device(input_dev);
0741     lis3->idev = NULL;
0742     return err;
0743 
0744 }
0745 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
0746 
0747 void lis3lv02d_joystick_disable(struct lis3lv02d *lis3)
0748 {
0749     if (lis3->irq)
0750         free_irq(lis3->irq, lis3);
0751     if (lis3->pdata && lis3->pdata->irq2)
0752         free_irq(lis3->pdata->irq2, lis3);
0753 
0754     if (!lis3->idev)
0755         return;
0756 
0757     if (lis3->irq)
0758         misc_deregister(&lis3->miscdev);
0759     input_unregister_device(lis3->idev);
0760     lis3->idev = NULL;
0761 }
0762 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
0763 
0764 /* Sysfs stuff */
0765 static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
0766 {
0767     /*
0768      * SYSFS functions are fast visitors so put-call
0769      * immediately after the get-call. However, keep
0770      * chip running for a while and schedule delayed
0771      * suspend. This way periodic sysfs calls doesn't
0772      * suffer from relatively long power up time.
0773      */
0774 
0775     if (lis3->pm_dev) {
0776         pm_runtime_get_sync(lis3->pm_dev);
0777         pm_runtime_put_noidle(lis3->pm_dev);
0778         pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
0779     }
0780 }
0781 
0782 static ssize_t lis3lv02d_selftest_show(struct device *dev,
0783                 struct device_attribute *attr, char *buf)
0784 {
0785     struct lis3lv02d *lis3 = dev_get_drvdata(dev);
0786     s16 values[3];
0787 
0788     static const char ok[] = "OK";
0789     static const char fail[] = "FAIL";
0790     static const char irq[] = "FAIL_IRQ";
0791     const char *res;
0792 
0793     lis3lv02d_sysfs_poweron(lis3);
0794     switch (lis3lv02d_selftest(lis3, values)) {
0795     case SELFTEST_FAIL:
0796         res = fail;
0797         break;
0798     case SELFTEST_IRQ:
0799         res = irq;
0800         break;
0801     case SELFTEST_OK:
0802     default:
0803         res = ok;
0804         break;
0805     }
0806     return sprintf(buf, "%s %d %d %d\n", res,
0807         values[0], values[1], values[2]);
0808 }
0809 
0810 static ssize_t lis3lv02d_position_show(struct device *dev,
0811                 struct device_attribute *attr, char *buf)
0812 {
0813     struct lis3lv02d *lis3 = dev_get_drvdata(dev);
0814     int x, y, z;
0815 
0816     lis3lv02d_sysfs_poweron(lis3);
0817     mutex_lock(&lis3->mutex);
0818     lis3lv02d_get_xyz(lis3, &x, &y, &z);
0819     mutex_unlock(&lis3->mutex);
0820     return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
0821 }
0822 
0823 static ssize_t lis3lv02d_rate_show(struct device *dev,
0824             struct device_attribute *attr, char *buf)
0825 {
0826     struct lis3lv02d *lis3 = dev_get_drvdata(dev);
0827     int odr_idx;
0828 
0829     lis3lv02d_sysfs_poweron(lis3);
0830 
0831     odr_idx = lis3lv02d_get_odr_index(lis3);
0832     return sprintf(buf, "%d\n", lis3->odrs[odr_idx]);
0833 }
0834 
0835 static ssize_t lis3lv02d_rate_set(struct device *dev,
0836                 struct device_attribute *attr, const char *buf,
0837                 size_t count)
0838 {
0839     struct lis3lv02d *lis3 = dev_get_drvdata(dev);
0840     unsigned long rate;
0841     int ret;
0842 
0843     ret = kstrtoul(buf, 0, &rate);
0844     if (ret)
0845         return ret;
0846 
0847     lis3lv02d_sysfs_poweron(lis3);
0848     if (lis3lv02d_set_odr(lis3, rate))
0849         return -EINVAL;
0850 
0851     return count;
0852 }
0853 
0854 static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
0855 static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
0856 static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
0857                         lis3lv02d_rate_set);
0858 
0859 static struct attribute *lis3lv02d_attributes[] = {
0860     &dev_attr_selftest.attr,
0861     &dev_attr_position.attr,
0862     &dev_attr_rate.attr,
0863     NULL
0864 };
0865 
0866 static const struct attribute_group lis3lv02d_attribute_group = {
0867     .attrs = lis3lv02d_attributes
0868 };
0869 
0870 
0871 static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
0872 {
0873     lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
0874     if (IS_ERR(lis3->pdev))
0875         return PTR_ERR(lis3->pdev);
0876 
0877     platform_set_drvdata(lis3->pdev, lis3);
0878     return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
0879 }
0880 
0881 void lis3lv02d_remove_fs(struct lis3lv02d *lis3)
0882 {
0883     sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
0884     platform_device_unregister(lis3->pdev);
0885     if (lis3->pm_dev) {
0886         /* Barrier after the sysfs remove */
0887         pm_runtime_barrier(lis3->pm_dev);
0888 
0889         /* SYSFS may have left chip running. Turn off if necessary */
0890         if (!pm_runtime_suspended(lis3->pm_dev))
0891             lis3lv02d_poweroff(lis3);
0892 
0893         pm_runtime_disable(lis3->pm_dev);
0894         pm_runtime_set_suspended(lis3->pm_dev);
0895     }
0896     kfree(lis3->reg_cache);
0897 }
0898 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
0899 
0900 static void lis3lv02d_8b_configure(struct lis3lv02d *lis3,
0901                 struct lis3lv02d_platform_data *p)
0902 {
0903     int err;
0904     int ctrl2 = p->hipass_ctrl;
0905 
0906     if (p->click_flags) {
0907         lis3->write(lis3, CLICK_CFG, p->click_flags);
0908         lis3->write(lis3, CLICK_TIMELIMIT, p->click_time_limit);
0909         lis3->write(lis3, CLICK_LATENCY, p->click_latency);
0910         lis3->write(lis3, CLICK_WINDOW, p->click_window);
0911         lis3->write(lis3, CLICK_THSZ, p->click_thresh_z & 0xf);
0912         lis3->write(lis3, CLICK_THSY_X,
0913             (p->click_thresh_x & 0xf) |
0914             (p->click_thresh_y << 4));
0915 
0916         if (lis3->idev) {
0917             input_set_capability(lis3->idev, EV_KEY, BTN_X);
0918             input_set_capability(lis3->idev, EV_KEY, BTN_Y);
0919             input_set_capability(lis3->idev, EV_KEY, BTN_Z);
0920         }
0921     }
0922 
0923     if (p->wakeup_flags) {
0924         lis3->write(lis3, FF_WU_CFG_1, p->wakeup_flags);
0925         lis3->write(lis3, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
0926         /* pdata value + 1 to keep this backward compatible*/
0927         lis3->write(lis3, FF_WU_DURATION_1, p->duration1 + 1);
0928         ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
0929     }
0930 
0931     if (p->wakeup_flags2) {
0932         lis3->write(lis3, FF_WU_CFG_2, p->wakeup_flags2);
0933         lis3->write(lis3, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
0934         /* pdata value + 1 to keep this backward compatible*/
0935         lis3->write(lis3, FF_WU_DURATION_2, p->duration2 + 1);
0936         ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
0937     }
0938     /* Configure hipass filters */
0939     lis3->write(lis3, CTRL_REG2, ctrl2);
0940 
0941     if (p->irq2) {
0942         err = request_threaded_irq(p->irq2,
0943                     NULL,
0944                     lis302dl_interrupt_thread2_8b,
0945                     IRQF_TRIGGER_RISING | IRQF_ONESHOT |
0946                     (p->irq_flags2 & IRQF_TRIGGER_MASK),
0947                     DRIVER_NAME, lis3);
0948         if (err < 0)
0949             pr_err("No second IRQ. Limited functionality\n");
0950     }
0951 }
0952 
0953 #ifdef CONFIG_OF
0954 int lis3lv02d_init_dt(struct lis3lv02d *lis3)
0955 {
0956     struct lis3lv02d_platform_data *pdata;
0957     struct device_node *np = lis3->of_node;
0958     u32 val;
0959     s32 sval;
0960 
0961     if (!lis3->of_node)
0962         return 0;
0963 
0964     pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
0965     if (!pdata)
0966         return -ENOMEM;
0967 
0968     if (of_get_property(np, "st,click-single-x", NULL))
0969         pdata->click_flags |= LIS3_CLICK_SINGLE_X;
0970     if (of_get_property(np, "st,click-double-x", NULL))
0971         pdata->click_flags |= LIS3_CLICK_DOUBLE_X;
0972 
0973     if (of_get_property(np, "st,click-single-y", NULL))
0974         pdata->click_flags |= LIS3_CLICK_SINGLE_Y;
0975     if (of_get_property(np, "st,click-double-y", NULL))
0976         pdata->click_flags |= LIS3_CLICK_DOUBLE_Y;
0977 
0978     if (of_get_property(np, "st,click-single-z", NULL))
0979         pdata->click_flags |= LIS3_CLICK_SINGLE_Z;
0980     if (of_get_property(np, "st,click-double-z", NULL))
0981         pdata->click_flags |= LIS3_CLICK_DOUBLE_Z;
0982 
0983     if (!of_property_read_u32(np, "st,click-threshold-x", &val))
0984         pdata->click_thresh_x = val;
0985     if (!of_property_read_u32(np, "st,click-threshold-y", &val))
0986         pdata->click_thresh_y = val;
0987     if (!of_property_read_u32(np, "st,click-threshold-z", &val))
0988         pdata->click_thresh_z = val;
0989 
0990     if (!of_property_read_u32(np, "st,click-time-limit", &val))
0991         pdata->click_time_limit = val;
0992     if (!of_property_read_u32(np, "st,click-latency", &val))
0993         pdata->click_latency = val;
0994     if (!of_property_read_u32(np, "st,click-window", &val))
0995         pdata->click_window = val;
0996 
0997     if (of_get_property(np, "st,irq1-disable", NULL))
0998         pdata->irq_cfg |= LIS3_IRQ1_DISABLE;
0999     if (of_get_property(np, "st,irq1-ff-wu-1", NULL))
1000         pdata->irq_cfg |= LIS3_IRQ1_FF_WU_1;
1001     if (of_get_property(np, "st,irq1-ff-wu-2", NULL))
1002         pdata->irq_cfg |= LIS3_IRQ1_FF_WU_2;
1003     if (of_get_property(np, "st,irq1-data-ready", NULL))
1004         pdata->irq_cfg |= LIS3_IRQ1_DATA_READY;
1005     if (of_get_property(np, "st,irq1-click", NULL))
1006         pdata->irq_cfg |= LIS3_IRQ1_CLICK;
1007 
1008     if (of_get_property(np, "st,irq2-disable", NULL))
1009         pdata->irq_cfg |= LIS3_IRQ2_DISABLE;
1010     if (of_get_property(np, "st,irq2-ff-wu-1", NULL))
1011         pdata->irq_cfg |= LIS3_IRQ2_FF_WU_1;
1012     if (of_get_property(np, "st,irq2-ff-wu-2", NULL))
1013         pdata->irq_cfg |= LIS3_IRQ2_FF_WU_2;
1014     if (of_get_property(np, "st,irq2-data-ready", NULL))
1015         pdata->irq_cfg |= LIS3_IRQ2_DATA_READY;
1016     if (of_get_property(np, "st,irq2-click", NULL))
1017         pdata->irq_cfg |= LIS3_IRQ2_CLICK;
1018 
1019     if (of_get_property(np, "st,irq-open-drain", NULL))
1020         pdata->irq_cfg |= LIS3_IRQ_OPEN_DRAIN;
1021     if (of_get_property(np, "st,irq-active-low", NULL))
1022         pdata->irq_cfg |= LIS3_IRQ_ACTIVE_LOW;
1023 
1024     if (!of_property_read_u32(np, "st,wu-duration-1", &val))
1025         pdata->duration1 = val;
1026     if (!of_property_read_u32(np, "st,wu-duration-2", &val))
1027         pdata->duration2 = val;
1028 
1029     if (of_get_property(np, "st,wakeup-x-lo", NULL))
1030         pdata->wakeup_flags |= LIS3_WAKEUP_X_LO;
1031     if (of_get_property(np, "st,wakeup-x-hi", NULL))
1032         pdata->wakeup_flags |= LIS3_WAKEUP_X_HI;
1033     if (of_get_property(np, "st,wakeup-y-lo", NULL))
1034         pdata->wakeup_flags |= LIS3_WAKEUP_Y_LO;
1035     if (of_get_property(np, "st,wakeup-y-hi", NULL))
1036         pdata->wakeup_flags |= LIS3_WAKEUP_Y_HI;
1037     if (of_get_property(np, "st,wakeup-z-lo", NULL))
1038         pdata->wakeup_flags |= LIS3_WAKEUP_Z_LO;
1039     if (of_get_property(np, "st,wakeup-z-hi", NULL))
1040         pdata->wakeup_flags |= LIS3_WAKEUP_Z_HI;
1041     if (of_get_property(np, "st,wakeup-threshold", &val))
1042         pdata->wakeup_thresh = val;
1043 
1044     if (of_get_property(np, "st,wakeup2-x-lo", NULL))
1045         pdata->wakeup_flags2 |= LIS3_WAKEUP_X_LO;
1046     if (of_get_property(np, "st,wakeup2-x-hi", NULL))
1047         pdata->wakeup_flags2 |= LIS3_WAKEUP_X_HI;
1048     if (of_get_property(np, "st,wakeup2-y-lo", NULL))
1049         pdata->wakeup_flags2 |= LIS3_WAKEUP_Y_LO;
1050     if (of_get_property(np, "st,wakeup2-y-hi", NULL))
1051         pdata->wakeup_flags2 |= LIS3_WAKEUP_Y_HI;
1052     if (of_get_property(np, "st,wakeup2-z-lo", NULL))
1053         pdata->wakeup_flags2 |= LIS3_WAKEUP_Z_LO;
1054     if (of_get_property(np, "st,wakeup2-z-hi", NULL))
1055         pdata->wakeup_flags2 |= LIS3_WAKEUP_Z_HI;
1056     if (of_get_property(np, "st,wakeup2-threshold", &val))
1057         pdata->wakeup_thresh2 = val;
1058 
1059     if (!of_property_read_u32(np, "st,highpass-cutoff-hz", &val)) {
1060         switch (val) {
1061         case 1:
1062             pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_1HZ;
1063             break;
1064         case 2:
1065             pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_2HZ;
1066             break;
1067         case 4:
1068             pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_4HZ;
1069             break;
1070         case 8:
1071             pdata->hipass_ctrl = LIS3_HIPASS_CUTFF_8HZ;
1072             break;
1073         }
1074     }
1075 
1076     if (of_get_property(np, "st,hipass1-disable", NULL))
1077         pdata->hipass_ctrl |= LIS3_HIPASS1_DISABLE;
1078     if (of_get_property(np, "st,hipass2-disable", NULL))
1079         pdata->hipass_ctrl |= LIS3_HIPASS2_DISABLE;
1080 
1081     if (of_property_read_s32(np, "st,axis-x", &sval) == 0)
1082         pdata->axis_x = sval;
1083     if (of_property_read_s32(np, "st,axis-y", &sval) == 0)
1084         pdata->axis_y = sval;
1085     if (of_property_read_s32(np, "st,axis-z", &sval) == 0)
1086         pdata->axis_z = sval;
1087 
1088     if (of_get_property(np, "st,default-rate", NULL))
1089         pdata->default_rate = val;
1090 
1091     if (of_property_read_s32(np, "st,min-limit-x", &sval) == 0)
1092         pdata->st_min_limits[0] = sval;
1093     if (of_property_read_s32(np, "st,min-limit-y", &sval) == 0)
1094         pdata->st_min_limits[1] = sval;
1095     if (of_property_read_s32(np, "st,min-limit-z", &sval) == 0)
1096         pdata->st_min_limits[2] = sval;
1097 
1098     if (of_property_read_s32(np, "st,max-limit-x", &sval) == 0)
1099         pdata->st_max_limits[0] = sval;
1100     if (of_property_read_s32(np, "st,max-limit-y", &sval) == 0)
1101         pdata->st_max_limits[1] = sval;
1102     if (of_property_read_s32(np, "st,max-limit-z", &sval) == 0)
1103         pdata->st_max_limits[2] = sval;
1104 
1105 
1106     lis3->pdata = pdata;
1107 
1108     return 0;
1109 }
1110 
1111 #else
1112 int lis3lv02d_init_dt(struct lis3lv02d *lis3)
1113 {
1114     return 0;
1115 }
1116 #endif
1117 EXPORT_SYMBOL_GPL(lis3lv02d_init_dt);
1118 
1119 /*
1120  * Initialise the accelerometer and the various subsystems.
1121  * Should be rather independent of the bus system.
1122  */
1123 int lis3lv02d_init_device(struct lis3lv02d *lis3)
1124 {
1125     int err;
1126     irq_handler_t thread_fn;
1127     int irq_flags = 0;
1128 
1129     lis3->whoami = lis3lv02d_read_8(lis3, WHO_AM_I);
1130 
1131     switch (lis3->whoami) {
1132     case WAI_12B:
1133         pr_info("12 bits sensor found\n");
1134         lis3->read_data = lis3lv02d_read_12;
1135         lis3->mdps_max_val = 2048;
1136         lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
1137         lis3->odrs = lis3_12_rates;
1138         lis3->odr_mask = CTRL1_DF0 | CTRL1_DF1;
1139         lis3->scale = LIS3_SENSITIVITY_12B;
1140         lis3->regs = lis3_wai12_regs;
1141         lis3->regs_size = ARRAY_SIZE(lis3_wai12_regs);
1142         break;
1143     case WAI_8B:
1144         pr_info("8 bits sensor found\n");
1145         lis3->read_data = lis3lv02d_read_8;
1146         lis3->mdps_max_val = 128;
1147         lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
1148         lis3->odrs = lis3_8_rates;
1149         lis3->odr_mask = CTRL1_DR;
1150         lis3->scale = LIS3_SENSITIVITY_8B;
1151         lis3->regs = lis3_wai8_regs;
1152         lis3->regs_size = ARRAY_SIZE(lis3_wai8_regs);
1153         break;
1154     case WAI_3DC:
1155         pr_info("8 bits 3DC sensor found\n");
1156         lis3->read_data = lis3lv02d_read_8;
1157         lis3->mdps_max_val = 128;
1158         lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
1159         lis3->odrs = lis3_3dc_rates;
1160         lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
1161         lis3->scale = LIS3_SENSITIVITY_8B;
1162         break;
1163     case WAI_3DLH:
1164         pr_info("16 bits lis331dlh sensor found\n");
1165         lis3->read_data = lis331dlh_read_data;
1166         lis3->mdps_max_val = 2048; /* 12 bits for 2G */
1167         lis3->shift_adj = SHIFT_ADJ_2G;
1168         lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
1169         lis3->odrs = lis3_3dlh_rates;
1170         lis3->odr_mask = CTRL1_DR0 | CTRL1_DR1;
1171         lis3->scale = LIS3DLH_SENSITIVITY_2G;
1172         break;
1173     default:
1174         pr_err("unknown sensor type 0x%X\n", lis3->whoami);
1175         return -ENODEV;
1176     }
1177 
1178     lis3->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
1179                      sizeof(lis3_wai12_regs)), GFP_KERNEL);
1180 
1181     if (lis3->reg_cache == NULL)
1182         return -ENOMEM;
1183 
1184     mutex_init(&lis3->mutex);
1185     atomic_set(&lis3->wake_thread, 0);
1186 
1187     lis3lv02d_add_fs(lis3);
1188     err = lis3lv02d_poweron(lis3);
1189     if (err) {
1190         lis3lv02d_remove_fs(lis3);
1191         return err;
1192     }
1193 
1194     if (lis3->pm_dev) {
1195         pm_runtime_set_active(lis3->pm_dev);
1196         pm_runtime_enable(lis3->pm_dev);
1197     }
1198 
1199     if (lis3lv02d_joystick_enable(lis3))
1200         pr_err("joystick initialization failed\n");
1201 
1202     /* passing in platform specific data is purely optional and only
1203      * used by the SPI transport layer at the moment */
1204     if (lis3->pdata) {
1205         struct lis3lv02d_platform_data *p = lis3->pdata;
1206 
1207         if (lis3->whoami == WAI_8B)
1208             lis3lv02d_8b_configure(lis3, p);
1209 
1210         irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK;
1211 
1212         lis3->irq_cfg = p->irq_cfg;
1213         if (p->irq_cfg)
1214             lis3->write(lis3, CTRL_REG3, p->irq_cfg);
1215 
1216         if (p->default_rate)
1217             lis3lv02d_set_odr(lis3, p->default_rate);
1218     }
1219 
1220     /* bail if we did not get an IRQ from the bus layer */
1221     if (!lis3->irq) {
1222         pr_debug("No IRQ. Disabling /dev/freefall\n");
1223         goto out;
1224     }
1225 
1226     /*
1227      * The sensor can generate interrupts for free-fall and direction
1228      * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
1229      * the things simple and _fast_ we activate it only for free-fall, so
1230      * no need to read register (very slow with ACPI). For the same reason,
1231      * we forbid shared interrupts.
1232      *
1233      * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
1234      * io-apic is not configurable (and generates a warning) but I keep it
1235      * in case of support for other hardware.
1236      */
1237     if (lis3->pdata && lis3->whoami == WAI_8B)
1238         thread_fn = lis302dl_interrupt_thread1_8b;
1239     else
1240         thread_fn = NULL;
1241 
1242     err = request_threaded_irq(lis3->irq, lis302dl_interrupt,
1243                 thread_fn,
1244                 IRQF_TRIGGER_RISING | IRQF_ONESHOT |
1245                 irq_flags,
1246                 DRIVER_NAME, lis3);
1247 
1248     if (err < 0) {
1249         pr_err("Cannot get IRQ\n");
1250         goto out;
1251     }
1252 
1253     lis3->miscdev.minor = MISC_DYNAMIC_MINOR;
1254     lis3->miscdev.name  = "freefall";
1255     lis3->miscdev.fops  = &lis3lv02d_misc_fops;
1256 
1257     if (misc_register(&lis3->miscdev))
1258         pr_err("misc_register failed\n");
1259 out:
1260     return 0;
1261 }
1262 EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
1263 
1264 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
1265 MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
1266 MODULE_LICENSE("GPL");