Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ADXL345/346 Three-Axis Digital Accelerometers
0004  *
0005  * Enter bugs at http://blackfin.uclinux.org/
0006  *
0007  * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
0008  */
0009 
0010 #include <linux/device.h>
0011 #include <linux/delay.h>
0012 #include <linux/input.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/irq.h>
0015 #include <linux/slab.h>
0016 #include <linux/workqueue.h>
0017 #include <linux/input/adxl34x.h>
0018 #include <linux/module.h>
0019 
0020 #include "adxl34x.h"
0021 
0022 /* ADXL345/6 Register Map */
0023 #define DEVID       0x00    /* R   Device ID */
0024 #define THRESH_TAP  0x1D    /* R/W Tap threshold */
0025 #define OFSX        0x1E    /* R/W X-axis offset */
0026 #define OFSY        0x1F    /* R/W Y-axis offset */
0027 #define OFSZ        0x20    /* R/W Z-axis offset */
0028 #define DUR     0x21    /* R/W Tap duration */
0029 #define LATENT      0x22    /* R/W Tap latency */
0030 #define WINDOW      0x23    /* R/W Tap window */
0031 #define THRESH_ACT  0x24    /* R/W Activity threshold */
0032 #define THRESH_INACT    0x25    /* R/W Inactivity threshold */
0033 #define TIME_INACT  0x26    /* R/W Inactivity time */
0034 #define ACT_INACT_CTL   0x27    /* R/W Axis enable control for activity and */
0035                 /* inactivity detection */
0036 #define THRESH_FF   0x28    /* R/W Free-fall threshold */
0037 #define TIME_FF     0x29    /* R/W Free-fall time */
0038 #define TAP_AXES    0x2A    /* R/W Axis control for tap/double tap */
0039 #define ACT_TAP_STATUS  0x2B    /* R   Source of tap/double tap */
0040 #define BW_RATE     0x2C    /* R/W Data rate and power mode control */
0041 #define POWER_CTL   0x2D    /* R/W Power saving features control */
0042 #define INT_ENABLE  0x2E    /* R/W Interrupt enable control */
0043 #define INT_MAP     0x2F    /* R/W Interrupt mapping control */
0044 #define INT_SOURCE  0x30    /* R   Source of interrupts */
0045 #define DATA_FORMAT 0x31    /* R/W Data format control */
0046 #define DATAX0      0x32    /* R   X-Axis Data 0 */
0047 #define DATAX1      0x33    /* R   X-Axis Data 1 */
0048 #define DATAY0      0x34    /* R   Y-Axis Data 0 */
0049 #define DATAY1      0x35    /* R   Y-Axis Data 1 */
0050 #define DATAZ0      0x36    /* R   Z-Axis Data 0 */
0051 #define DATAZ1      0x37    /* R   Z-Axis Data 1 */
0052 #define FIFO_CTL    0x38    /* R/W FIFO control */
0053 #define FIFO_STATUS 0x39    /* R   FIFO status */
0054 #define TAP_SIGN    0x3A    /* R   Sign and source for tap/double tap */
0055 /* Orientation ADXL346 only */
0056 #define ORIENT_CONF 0x3B    /* R/W Orientation configuration */
0057 #define ORIENT      0x3C    /* R   Orientation status */
0058 
0059 /* DEVIDs */
0060 #define ID_ADXL345  0xE5
0061 #define ID_ADXL346  0xE6
0062 
0063 /* INT_ENABLE/INT_MAP/INT_SOURCE Bits */
0064 #define DATA_READY  (1 << 7)
0065 #define SINGLE_TAP  (1 << 6)
0066 #define DOUBLE_TAP  (1 << 5)
0067 #define ACTIVITY    (1 << 4)
0068 #define INACTIVITY  (1 << 3)
0069 #define FREE_FALL   (1 << 2)
0070 #define WATERMARK   (1 << 1)
0071 #define OVERRUN     (1 << 0)
0072 
0073 /* ACT_INACT_CONTROL Bits */
0074 #define ACT_ACDC    (1 << 7)
0075 #define ACT_X_EN    (1 << 6)
0076 #define ACT_Y_EN    (1 << 5)
0077 #define ACT_Z_EN    (1 << 4)
0078 #define INACT_ACDC  (1 << 3)
0079 #define INACT_X_EN  (1 << 2)
0080 #define INACT_Y_EN  (1 << 1)
0081 #define INACT_Z_EN  (1 << 0)
0082 
0083 /* TAP_AXES Bits */
0084 #define SUPPRESS    (1 << 3)
0085 #define TAP_X_EN    (1 << 2)
0086 #define TAP_Y_EN    (1 << 1)
0087 #define TAP_Z_EN    (1 << 0)
0088 
0089 /* ACT_TAP_STATUS Bits */
0090 #define ACT_X_SRC   (1 << 6)
0091 #define ACT_Y_SRC   (1 << 5)
0092 #define ACT_Z_SRC   (1 << 4)
0093 #define ASLEEP      (1 << 3)
0094 #define TAP_X_SRC   (1 << 2)
0095 #define TAP_Y_SRC   (1 << 1)
0096 #define TAP_Z_SRC   (1 << 0)
0097 
0098 /* BW_RATE Bits */
0099 #define LOW_POWER   (1 << 4)
0100 #define RATE(x)     ((x) & 0xF)
0101 
0102 /* POWER_CTL Bits */
0103 #define PCTL_LINK   (1 << 5)
0104 #define PCTL_AUTO_SLEEP (1 << 4)
0105 #define PCTL_MEASURE    (1 << 3)
0106 #define PCTL_SLEEP  (1 << 2)
0107 #define PCTL_WAKEUP(x)  ((x) & 0x3)
0108 
0109 /* DATA_FORMAT Bits */
0110 #define SELF_TEST   (1 << 7)
0111 #define SPI     (1 << 6)
0112 #define INT_INVERT  (1 << 5)
0113 #define FULL_RES    (1 << 3)
0114 #define JUSTIFY     (1 << 2)
0115 #define RANGE(x)    ((x) & 0x3)
0116 #define RANGE_PM_2g 0
0117 #define RANGE_PM_4g 1
0118 #define RANGE_PM_8g 2
0119 #define RANGE_PM_16g    3
0120 
0121 /*
0122  * Maximum value our axis may get in full res mode for the input device
0123  * (signed 13 bits)
0124  */
0125 #define ADXL_FULLRES_MAX_VAL 4096
0126 
0127 /*
0128  * Maximum value our axis may get in fixed res mode for the input device
0129  * (signed 10 bits)
0130  */
0131 #define ADXL_FIXEDRES_MAX_VAL 512
0132 
0133 /* FIFO_CTL Bits */
0134 #define FIFO_MODE(x)    (((x) & 0x3) << 6)
0135 #define FIFO_BYPASS 0
0136 #define FIFO_FIFO   1
0137 #define FIFO_STREAM 2
0138 #define FIFO_TRIGGER    3
0139 #define TRIGGER     (1 << 5)
0140 #define SAMPLES(x)  ((x) & 0x1F)
0141 
0142 /* FIFO_STATUS Bits */
0143 #define FIFO_TRIG   (1 << 7)
0144 #define ENTRIES(x)  ((x) & 0x3F)
0145 
0146 /* TAP_SIGN Bits ADXL346 only */
0147 #define XSIGN       (1 << 6)
0148 #define YSIGN       (1 << 5)
0149 #define ZSIGN       (1 << 4)
0150 #define XTAP        (1 << 3)
0151 #define YTAP        (1 << 2)
0152 #define ZTAP        (1 << 1)
0153 
0154 /* ORIENT_CONF ADXL346 only */
0155 #define ORIENT_DEADZONE(x)  (((x) & 0x7) << 4)
0156 #define ORIENT_DIVISOR(x)   ((x) & 0x7)
0157 
0158 /* ORIENT ADXL346 only */
0159 #define ADXL346_2D_VALID        (1 << 6)
0160 #define ADXL346_2D_ORIENT(x)        (((x) & 0x30) >> 4)
0161 #define ADXL346_3D_VALID        (1 << 3)
0162 #define ADXL346_3D_ORIENT(x)        ((x) & 0x7)
0163 #define ADXL346_2D_PORTRAIT_POS     0   /* +X */
0164 #define ADXL346_2D_PORTRAIT_NEG     1   /* -X */
0165 #define ADXL346_2D_LANDSCAPE_POS    2   /* +Y */
0166 #define ADXL346_2D_LANDSCAPE_NEG    3   /* -Y */
0167 
0168 #define ADXL346_3D_FRONT        3   /* +X */
0169 #define ADXL346_3D_BACK         4   /* -X */
0170 #define ADXL346_3D_RIGHT        2   /* +Y */
0171 #define ADXL346_3D_LEFT         5   /* -Y */
0172 #define ADXL346_3D_TOP          1   /* +Z */
0173 #define ADXL346_3D_BOTTOM       6   /* -Z */
0174 
0175 #undef ADXL_DEBUG
0176 
0177 #define ADXL_X_AXIS         0
0178 #define ADXL_Y_AXIS         1
0179 #define ADXL_Z_AXIS         2
0180 
0181 #define AC_READ(ac, reg)    ((ac)->bops->read((ac)->dev, reg))
0182 #define AC_WRITE(ac, reg, val)  ((ac)->bops->write((ac)->dev, reg, val))
0183 
0184 struct axis_triple {
0185     int x;
0186     int y;
0187     int z;
0188 };
0189 
0190 struct adxl34x {
0191     struct device *dev;
0192     struct input_dev *input;
0193     struct mutex mutex; /* reentrant protection for struct */
0194     struct adxl34x_platform_data pdata;
0195     struct axis_triple swcal;
0196     struct axis_triple hwcal;
0197     struct axis_triple saved;
0198     char phys[32];
0199     unsigned orient2d_saved;
0200     unsigned orient3d_saved;
0201     bool disabled;  /* P: mutex */
0202     bool opened;    /* P: mutex */
0203     bool suspended; /* P: mutex */
0204     bool fifo_delay;
0205     int irq;
0206     unsigned model;
0207     unsigned int_mask;
0208 
0209     const struct adxl34x_bus_ops *bops;
0210 };
0211 
0212 static const struct adxl34x_platform_data adxl34x_default_init = {
0213     .tap_threshold = 35,
0214     .tap_duration = 3,
0215     .tap_latency = 20,
0216     .tap_window = 20,
0217     .tap_axis_control = ADXL_TAP_X_EN | ADXL_TAP_Y_EN | ADXL_TAP_Z_EN,
0218     .act_axis_control = 0xFF,
0219     .activity_threshold = 6,
0220     .inactivity_threshold = 4,
0221     .inactivity_time = 3,
0222     .free_fall_threshold = 8,
0223     .free_fall_time = 0x20,
0224     .data_rate = 8,
0225     .data_range = ADXL_FULL_RES,
0226 
0227     .ev_type = EV_ABS,
0228     .ev_code_x = ABS_X, /* EV_REL */
0229     .ev_code_y = ABS_Y, /* EV_REL */
0230     .ev_code_z = ABS_Z, /* EV_REL */
0231 
0232     .ev_code_tap = {BTN_TOUCH, BTN_TOUCH, BTN_TOUCH}, /* EV_KEY {x,y,z} */
0233     .power_mode = ADXL_AUTO_SLEEP | ADXL_LINK,
0234     .fifo_mode = ADXL_FIFO_STREAM,
0235     .watermark = 0,
0236 };
0237 
0238 static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
0239 {
0240     __le16 buf[3];
0241 
0242     ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
0243 
0244     mutex_lock(&ac->mutex);
0245     ac->saved.x = (s16) le16_to_cpu(buf[0]);
0246     axis->x = ac->saved.x;
0247 
0248     ac->saved.y = (s16) le16_to_cpu(buf[1]);
0249     axis->y = ac->saved.y;
0250 
0251     ac->saved.z = (s16) le16_to_cpu(buf[2]);
0252     axis->z = ac->saved.z;
0253     mutex_unlock(&ac->mutex);
0254 }
0255 
0256 static void adxl34x_service_ev_fifo(struct adxl34x *ac)
0257 {
0258     struct adxl34x_platform_data *pdata = &ac->pdata;
0259     struct axis_triple axis;
0260 
0261     adxl34x_get_triple(ac, &axis);
0262 
0263     input_event(ac->input, pdata->ev_type, pdata->ev_code_x,
0264             axis.x - ac->swcal.x);
0265     input_event(ac->input, pdata->ev_type, pdata->ev_code_y,
0266             axis.y - ac->swcal.y);
0267     input_event(ac->input, pdata->ev_type, pdata->ev_code_z,
0268             axis.z - ac->swcal.z);
0269 }
0270 
0271 static void adxl34x_report_key_single(struct input_dev *input, int key)
0272 {
0273     input_report_key(input, key, true);
0274     input_sync(input);
0275     input_report_key(input, key, false);
0276 }
0277 
0278 static void adxl34x_send_key_events(struct adxl34x *ac,
0279         struct adxl34x_platform_data *pdata, int status, int press)
0280 {
0281     int i;
0282 
0283     for (i = ADXL_X_AXIS; i <= ADXL_Z_AXIS; i++) {
0284         if (status & (1 << (ADXL_Z_AXIS - i)))
0285             input_report_key(ac->input,
0286                      pdata->ev_code_tap[i], press);
0287     }
0288 }
0289 
0290 static void adxl34x_do_tap(struct adxl34x *ac,
0291         struct adxl34x_platform_data *pdata, int status)
0292 {
0293     adxl34x_send_key_events(ac, pdata, status, true);
0294     input_sync(ac->input);
0295     adxl34x_send_key_events(ac, pdata, status, false);
0296 }
0297 
0298 static irqreturn_t adxl34x_irq(int irq, void *handle)
0299 {
0300     struct adxl34x *ac = handle;
0301     struct adxl34x_platform_data *pdata = &ac->pdata;
0302     int int_stat, tap_stat, samples, orient, orient_code;
0303 
0304     /*
0305      * ACT_TAP_STATUS should be read before clearing the interrupt
0306      * Avoid reading ACT_TAP_STATUS in case TAP detection is disabled
0307      */
0308 
0309     if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
0310         tap_stat = AC_READ(ac, ACT_TAP_STATUS);
0311     else
0312         tap_stat = 0;
0313 
0314     int_stat = AC_READ(ac, INT_SOURCE);
0315 
0316     if (int_stat & FREE_FALL)
0317         adxl34x_report_key_single(ac->input, pdata->ev_code_ff);
0318 
0319     if (int_stat & OVERRUN)
0320         dev_dbg(ac->dev, "OVERRUN\n");
0321 
0322     if (int_stat & (SINGLE_TAP | DOUBLE_TAP)) {
0323         adxl34x_do_tap(ac, pdata, tap_stat);
0324 
0325         if (int_stat & DOUBLE_TAP)
0326             adxl34x_do_tap(ac, pdata, tap_stat);
0327     }
0328 
0329     if (pdata->ev_code_act_inactivity) {
0330         if (int_stat & ACTIVITY)
0331             input_report_key(ac->input,
0332                      pdata->ev_code_act_inactivity, 1);
0333         if (int_stat & INACTIVITY)
0334             input_report_key(ac->input,
0335                      pdata->ev_code_act_inactivity, 0);
0336     }
0337 
0338     /*
0339      * ORIENTATION SENSING ADXL346 only
0340      */
0341     if (pdata->orientation_enable) {
0342         orient = AC_READ(ac, ORIENT);
0343         if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_2D) &&
0344             (orient & ADXL346_2D_VALID)) {
0345 
0346             orient_code = ADXL346_2D_ORIENT(orient);
0347             /* Report orientation only when it changes */
0348             if (ac->orient2d_saved != orient_code) {
0349                 ac->orient2d_saved = orient_code;
0350                 adxl34x_report_key_single(ac->input,
0351                     pdata->ev_codes_orient_2d[orient_code]);
0352             }
0353         }
0354 
0355         if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_3D) &&
0356             (orient & ADXL346_3D_VALID)) {
0357 
0358             orient_code = ADXL346_3D_ORIENT(orient) - 1;
0359             /* Report orientation only when it changes */
0360             if (ac->orient3d_saved != orient_code) {
0361                 ac->orient3d_saved = orient_code;
0362                 adxl34x_report_key_single(ac->input,
0363                     pdata->ev_codes_orient_3d[orient_code]);
0364             }
0365         }
0366     }
0367 
0368     if (int_stat & (DATA_READY | WATERMARK)) {
0369 
0370         if (pdata->fifo_mode)
0371             samples = ENTRIES(AC_READ(ac, FIFO_STATUS)) + 1;
0372         else
0373             samples = 1;
0374 
0375         for (; samples > 0; samples--) {
0376             adxl34x_service_ev_fifo(ac);
0377             /*
0378              * To ensure that the FIFO has
0379              * completely popped, there must be at least 5 us between
0380              * the end of reading the data registers, signified by the
0381              * transition to register 0x38 from 0x37 or the CS pin
0382              * going high, and the start of new reads of the FIFO or
0383              * reading the FIFO_STATUS register. For SPI operation at
0384              * 1.5 MHz or lower, the register addressing portion of the
0385              * transmission is sufficient delay to ensure the FIFO has
0386              * completely popped. It is necessary for SPI operation
0387              * greater than 1.5 MHz to de-assert the CS pin to ensure a
0388              * total of 5 us, which is at most 3.4 us at 5 MHz
0389              * operation.
0390              */
0391             if (ac->fifo_delay && (samples > 1))
0392                 udelay(3);
0393         }
0394     }
0395 
0396     input_sync(ac->input);
0397 
0398     return IRQ_HANDLED;
0399 }
0400 
0401 static void __adxl34x_disable(struct adxl34x *ac)
0402 {
0403     /*
0404      * A '0' places the ADXL34x into standby mode
0405      * with minimum power consumption.
0406      */
0407     AC_WRITE(ac, POWER_CTL, 0);
0408 }
0409 
0410 static void __adxl34x_enable(struct adxl34x *ac)
0411 {
0412     AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
0413 }
0414 
0415 void adxl34x_suspend(struct adxl34x *ac)
0416 {
0417     mutex_lock(&ac->mutex);
0418 
0419     if (!ac->suspended && !ac->disabled && ac->opened)
0420         __adxl34x_disable(ac);
0421 
0422     ac->suspended = true;
0423 
0424     mutex_unlock(&ac->mutex);
0425 }
0426 EXPORT_SYMBOL_GPL(adxl34x_suspend);
0427 
0428 void adxl34x_resume(struct adxl34x *ac)
0429 {
0430     mutex_lock(&ac->mutex);
0431 
0432     if (ac->suspended && !ac->disabled && ac->opened)
0433         __adxl34x_enable(ac);
0434 
0435     ac->suspended = false;
0436 
0437     mutex_unlock(&ac->mutex);
0438 }
0439 EXPORT_SYMBOL_GPL(adxl34x_resume);
0440 
0441 static ssize_t adxl34x_disable_show(struct device *dev,
0442                     struct device_attribute *attr, char *buf)
0443 {
0444     struct adxl34x *ac = dev_get_drvdata(dev);
0445 
0446     return sprintf(buf, "%u\n", ac->disabled);
0447 }
0448 
0449 static ssize_t adxl34x_disable_store(struct device *dev,
0450                      struct device_attribute *attr,
0451                      const char *buf, size_t count)
0452 {
0453     struct adxl34x *ac = dev_get_drvdata(dev);
0454     unsigned int val;
0455     int error;
0456 
0457     error = kstrtouint(buf, 10, &val);
0458     if (error)
0459         return error;
0460 
0461     mutex_lock(&ac->mutex);
0462 
0463     if (!ac->suspended && ac->opened) {
0464         if (val) {
0465             if (!ac->disabled)
0466                 __adxl34x_disable(ac);
0467         } else {
0468             if (ac->disabled)
0469                 __adxl34x_enable(ac);
0470         }
0471     }
0472 
0473     ac->disabled = !!val;
0474 
0475     mutex_unlock(&ac->mutex);
0476 
0477     return count;
0478 }
0479 
0480 static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
0481 
0482 static ssize_t adxl34x_calibrate_show(struct device *dev,
0483                       struct device_attribute *attr, char *buf)
0484 {
0485     struct adxl34x *ac = dev_get_drvdata(dev);
0486     ssize_t count;
0487 
0488     mutex_lock(&ac->mutex);
0489     count = sprintf(buf, "%d,%d,%d\n",
0490             ac->hwcal.x * 4 + ac->swcal.x,
0491             ac->hwcal.y * 4 + ac->swcal.y,
0492             ac->hwcal.z * 4 + ac->swcal.z);
0493     mutex_unlock(&ac->mutex);
0494 
0495     return count;
0496 }
0497 
0498 static ssize_t adxl34x_calibrate_store(struct device *dev,
0499                        struct device_attribute *attr,
0500                        const char *buf, size_t count)
0501 {
0502     struct adxl34x *ac = dev_get_drvdata(dev);
0503 
0504     /*
0505      * Hardware offset calibration has a resolution of 15.6 mg/LSB.
0506      * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
0507      */
0508 
0509     mutex_lock(&ac->mutex);
0510     ac->hwcal.x -= (ac->saved.x / 4);
0511     ac->swcal.x = ac->saved.x % 4;
0512 
0513     ac->hwcal.y -= (ac->saved.y / 4);
0514     ac->swcal.y = ac->saved.y % 4;
0515 
0516     ac->hwcal.z -= (ac->saved.z / 4);
0517     ac->swcal.z = ac->saved.z % 4;
0518 
0519     AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
0520     AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
0521     AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
0522     mutex_unlock(&ac->mutex);
0523 
0524     return count;
0525 }
0526 
0527 static DEVICE_ATTR(calibrate, 0664,
0528            adxl34x_calibrate_show, adxl34x_calibrate_store);
0529 
0530 static ssize_t adxl34x_rate_show(struct device *dev,
0531                  struct device_attribute *attr, char *buf)
0532 {
0533     struct adxl34x *ac = dev_get_drvdata(dev);
0534 
0535     return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
0536 }
0537 
0538 static ssize_t adxl34x_rate_store(struct device *dev,
0539                   struct device_attribute *attr,
0540                   const char *buf, size_t count)
0541 {
0542     struct adxl34x *ac = dev_get_drvdata(dev);
0543     unsigned char val;
0544     int error;
0545 
0546     error = kstrtou8(buf, 10, &val);
0547     if (error)
0548         return error;
0549 
0550     mutex_lock(&ac->mutex);
0551 
0552     ac->pdata.data_rate = RATE(val);
0553     AC_WRITE(ac, BW_RATE,
0554          ac->pdata.data_rate |
0555             (ac->pdata.low_power_mode ? LOW_POWER : 0));
0556 
0557     mutex_unlock(&ac->mutex);
0558 
0559     return count;
0560 }
0561 
0562 static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
0563 
0564 static ssize_t adxl34x_autosleep_show(struct device *dev,
0565                  struct device_attribute *attr, char *buf)
0566 {
0567     struct adxl34x *ac = dev_get_drvdata(dev);
0568 
0569     return sprintf(buf, "%u\n",
0570         ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
0571 }
0572 
0573 static ssize_t adxl34x_autosleep_store(struct device *dev,
0574                   struct device_attribute *attr,
0575                   const char *buf, size_t count)
0576 {
0577     struct adxl34x *ac = dev_get_drvdata(dev);
0578     unsigned int val;
0579     int error;
0580 
0581     error = kstrtouint(buf, 10, &val);
0582     if (error)
0583         return error;
0584 
0585     mutex_lock(&ac->mutex);
0586 
0587     if (val)
0588         ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
0589     else
0590         ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
0591 
0592     if (!ac->disabled && !ac->suspended && ac->opened)
0593         AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
0594 
0595     mutex_unlock(&ac->mutex);
0596 
0597     return count;
0598 }
0599 
0600 static DEVICE_ATTR(autosleep, 0664,
0601            adxl34x_autosleep_show, adxl34x_autosleep_store);
0602 
0603 static ssize_t adxl34x_position_show(struct device *dev,
0604                  struct device_attribute *attr, char *buf)
0605 {
0606     struct adxl34x *ac = dev_get_drvdata(dev);
0607     ssize_t count;
0608 
0609     mutex_lock(&ac->mutex);
0610     count = sprintf(buf, "(%d, %d, %d)\n",
0611             ac->saved.x, ac->saved.y, ac->saved.z);
0612     mutex_unlock(&ac->mutex);
0613 
0614     return count;
0615 }
0616 
0617 static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
0618 
0619 #ifdef ADXL_DEBUG
0620 static ssize_t adxl34x_write_store(struct device *dev,
0621                    struct device_attribute *attr,
0622                    const char *buf, size_t count)
0623 {
0624     struct adxl34x *ac = dev_get_drvdata(dev);
0625     unsigned int val;
0626     int error;
0627 
0628     /*
0629      * This allows basic ADXL register write access for debug purposes.
0630      */
0631     error = kstrtouint(buf, 16, &val);
0632     if (error)
0633         return error;
0634 
0635     mutex_lock(&ac->mutex);
0636     AC_WRITE(ac, val >> 8, val & 0xFF);
0637     mutex_unlock(&ac->mutex);
0638 
0639     return count;
0640 }
0641 
0642 static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
0643 #endif
0644 
0645 static struct attribute *adxl34x_attributes[] = {
0646     &dev_attr_disable.attr,
0647     &dev_attr_calibrate.attr,
0648     &dev_attr_rate.attr,
0649     &dev_attr_autosleep.attr,
0650     &dev_attr_position.attr,
0651 #ifdef ADXL_DEBUG
0652     &dev_attr_write.attr,
0653 #endif
0654     NULL
0655 };
0656 
0657 static const struct attribute_group adxl34x_attr_group = {
0658     .attrs = adxl34x_attributes,
0659 };
0660 
0661 static int adxl34x_input_open(struct input_dev *input)
0662 {
0663     struct adxl34x *ac = input_get_drvdata(input);
0664 
0665     mutex_lock(&ac->mutex);
0666 
0667     if (!ac->suspended && !ac->disabled)
0668         __adxl34x_enable(ac);
0669 
0670     ac->opened = true;
0671 
0672     mutex_unlock(&ac->mutex);
0673 
0674     return 0;
0675 }
0676 
0677 static void adxl34x_input_close(struct input_dev *input)
0678 {
0679     struct adxl34x *ac = input_get_drvdata(input);
0680 
0681     mutex_lock(&ac->mutex);
0682 
0683     if (!ac->suspended && !ac->disabled)
0684         __adxl34x_disable(ac);
0685 
0686     ac->opened = false;
0687 
0688     mutex_unlock(&ac->mutex);
0689 }
0690 
0691 struct adxl34x *adxl34x_probe(struct device *dev, int irq,
0692                   bool fifo_delay_default,
0693                   const struct adxl34x_bus_ops *bops)
0694 {
0695     struct adxl34x *ac;
0696     struct input_dev *input_dev;
0697     const struct adxl34x_platform_data *pdata;
0698     int err, range, i;
0699     int revid;
0700 
0701     if (!irq) {
0702         dev_err(dev, "no IRQ?\n");
0703         err = -ENODEV;
0704         goto err_out;
0705     }
0706 
0707     ac = kzalloc(sizeof(*ac), GFP_KERNEL);
0708     input_dev = input_allocate_device();
0709     if (!ac || !input_dev) {
0710         err = -ENOMEM;
0711         goto err_free_mem;
0712     }
0713 
0714     ac->fifo_delay = fifo_delay_default;
0715 
0716     pdata = dev_get_platdata(dev);
0717     if (!pdata) {
0718         dev_dbg(dev,
0719             "No platform data: Using default initialization\n");
0720         pdata = &adxl34x_default_init;
0721     }
0722 
0723     ac->pdata = *pdata;
0724     pdata = &ac->pdata;
0725 
0726     ac->input = input_dev;
0727     ac->dev = dev;
0728     ac->irq = irq;
0729     ac->bops = bops;
0730 
0731     mutex_init(&ac->mutex);
0732 
0733     input_dev->name = "ADXL34x accelerometer";
0734     revid = AC_READ(ac, DEVID);
0735 
0736     switch (revid) {
0737     case ID_ADXL345:
0738         ac->model = 345;
0739         break;
0740     case ID_ADXL346:
0741         ac->model = 346;
0742         break;
0743     default:
0744         dev_err(dev, "Failed to probe %s\n", input_dev->name);
0745         err = -ENODEV;
0746         goto err_free_mem;
0747     }
0748 
0749     snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
0750 
0751     input_dev->phys = ac->phys;
0752     input_dev->dev.parent = dev;
0753     input_dev->id.product = ac->model;
0754     input_dev->id.bustype = bops->bustype;
0755     input_dev->open = adxl34x_input_open;
0756     input_dev->close = adxl34x_input_close;
0757 
0758     input_set_drvdata(input_dev, ac);
0759 
0760     __set_bit(ac->pdata.ev_type, input_dev->evbit);
0761 
0762     if (ac->pdata.ev_type == EV_REL) {
0763         __set_bit(REL_X, input_dev->relbit);
0764         __set_bit(REL_Y, input_dev->relbit);
0765         __set_bit(REL_Z, input_dev->relbit);
0766     } else {
0767         /* EV_ABS */
0768         __set_bit(ABS_X, input_dev->absbit);
0769         __set_bit(ABS_Y, input_dev->absbit);
0770         __set_bit(ABS_Z, input_dev->absbit);
0771 
0772         if (pdata->data_range & FULL_RES)
0773             range = ADXL_FULLRES_MAX_VAL;   /* Signed 13-bit */
0774         else
0775             range = ADXL_FIXEDRES_MAX_VAL;  /* Signed 10-bit */
0776 
0777         input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
0778         input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
0779         input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
0780     }
0781 
0782     __set_bit(EV_KEY, input_dev->evbit);
0783     __set_bit(pdata->ev_code_tap[ADXL_X_AXIS], input_dev->keybit);
0784     __set_bit(pdata->ev_code_tap[ADXL_Y_AXIS], input_dev->keybit);
0785     __set_bit(pdata->ev_code_tap[ADXL_Z_AXIS], input_dev->keybit);
0786 
0787     if (pdata->ev_code_ff) {
0788         ac->int_mask = FREE_FALL;
0789         __set_bit(pdata->ev_code_ff, input_dev->keybit);
0790     }
0791 
0792     if (pdata->ev_code_act_inactivity)
0793         __set_bit(pdata->ev_code_act_inactivity, input_dev->keybit);
0794 
0795     ac->int_mask |= ACTIVITY | INACTIVITY;
0796 
0797     if (pdata->watermark) {
0798         ac->int_mask |= WATERMARK;
0799         if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
0800             ac->pdata.fifo_mode |= FIFO_STREAM;
0801     } else {
0802         ac->int_mask |= DATA_READY;
0803     }
0804 
0805     if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
0806         ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
0807 
0808     if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
0809         ac->fifo_delay = false;
0810 
0811     AC_WRITE(ac, POWER_CTL, 0);
0812 
0813     err = request_threaded_irq(ac->irq, NULL, adxl34x_irq,
0814                    IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
0815                    dev_name(dev), ac);
0816     if (err) {
0817         dev_err(dev, "irq %d busy?\n", ac->irq);
0818         goto err_free_mem;
0819     }
0820 
0821     err = sysfs_create_group(&dev->kobj, &adxl34x_attr_group);
0822     if (err)
0823         goto err_free_irq;
0824 
0825     err = input_register_device(input_dev);
0826     if (err)
0827         goto err_remove_attr;
0828 
0829     AC_WRITE(ac, OFSX, pdata->x_axis_offset);
0830     ac->hwcal.x = pdata->x_axis_offset;
0831     AC_WRITE(ac, OFSY, pdata->y_axis_offset);
0832     ac->hwcal.y = pdata->y_axis_offset;
0833     AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
0834     ac->hwcal.z = pdata->z_axis_offset;
0835     AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
0836     AC_WRITE(ac, DUR, pdata->tap_duration);
0837     AC_WRITE(ac, LATENT, pdata->tap_latency);
0838     AC_WRITE(ac, WINDOW, pdata->tap_window);
0839     AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
0840     AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
0841     AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
0842     AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
0843     AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
0844     AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
0845     AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
0846     AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
0847          (pdata->low_power_mode ? LOW_POWER : 0));
0848     AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
0849     AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
0850             SAMPLES(pdata->watermark));
0851 
0852     if (pdata->use_int2) {
0853         /* Map all INTs to INT2 */
0854         AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
0855     } else {
0856         /* Map all INTs to INT1 */
0857         AC_WRITE(ac, INT_MAP, 0);
0858     }
0859 
0860     if (ac->model == 346 && ac->pdata.orientation_enable) {
0861         AC_WRITE(ac, ORIENT_CONF,
0862             ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
0863             ORIENT_DIVISOR(ac->pdata.divisor_length));
0864 
0865         ac->orient2d_saved = 1234;
0866         ac->orient3d_saved = 1234;
0867 
0868         if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
0869             for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
0870                 __set_bit(pdata->ev_codes_orient_3d[i],
0871                       input_dev->keybit);
0872 
0873         if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
0874             for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
0875                 __set_bit(pdata->ev_codes_orient_2d[i],
0876                       input_dev->keybit);
0877     } else {
0878         ac->pdata.orientation_enable = 0;
0879     }
0880 
0881     AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
0882 
0883     ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
0884 
0885     return ac;
0886 
0887  err_remove_attr:
0888     sysfs_remove_group(&dev->kobj, &adxl34x_attr_group);
0889  err_free_irq:
0890     free_irq(ac->irq, ac);
0891  err_free_mem:
0892     input_free_device(input_dev);
0893     kfree(ac);
0894  err_out:
0895     return ERR_PTR(err);
0896 }
0897 EXPORT_SYMBOL_GPL(adxl34x_probe);
0898 
0899 void adxl34x_remove(struct adxl34x *ac)
0900 {
0901     sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
0902     free_irq(ac->irq, ac);
0903     input_unregister_device(ac->input);
0904     dev_dbg(ac->dev, "unregistered accelerometer\n");
0905     kfree(ac);
0906 }
0907 EXPORT_SYMBOL_GPL(adxl34x_remove);
0908 
0909 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
0910 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
0911 MODULE_LICENSE("GPL");