Lines Matching +full:digital +full:- +full:input +full:- +full:threshold +full:- +full:mode +full:- +full:fixed
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ADXL345/346 Three-Axis Digital Accelerometers
13 #include <linux/input.h>
18 #include <linux/input/adxl34x.h>
25 #define THRESH_TAP 0x1D /* R/W Tap threshold */
26 #define OFSX 0x1E /* R/W X-axis offset */
27 #define OFSY 0x1F /* R/W Y-axis offset */
28 #define OFSZ 0x20 /* R/W Z-axis offset */
32 #define THRESH_ACT 0x24 /* R/W Activity threshold */
33 #define THRESH_INACT 0x25 /* R/W Inactivity threshold */
37 #define THRESH_FF 0x28 /* R/W Free-fall threshold */
38 #define TIME_FF 0x29 /* R/W Free-fall time */
41 #define BW_RATE 0x2C /* R/W Data rate and power mode control */
47 #define DATAX0 0x32 /* R X-Axis Data 0 */
48 #define DATAX1 0x33 /* R X-Axis Data 1 */
49 #define DATAY0 0x34 /* R Y-Axis Data 0 */
50 #define DATAY1 0x35 /* R Y-Axis Data 1 */
51 #define DATAZ0 0x36 /* R Z-Axis Data 0 */
52 #define DATAZ1 0x37 /* R Z-Axis Data 1 */
123 * Maximum value our axis may get in full res mode for the input device
129 * Maximum value our axis may get in fixed res mode for the input device
165 #define ADXL346_2D_PORTRAIT_NEG 1 /* -X */
167 #define ADXL346_2D_LANDSCAPE_NEG 3 /* -Y */
170 #define ADXL346_3D_BACK 4 /* -X */
172 #define ADXL346_3D_LEFT 5 /* -Y */
174 #define ADXL346_3D_BOTTOM 6 /* -Z */
182 #define AC_READ(ac, reg) ((ac)->bops->read((ac)->dev, reg))
183 #define AC_WRITE(ac, reg, val) ((ac)->bops->write((ac)->dev, reg, val))
193 struct input_dev *input;
243 ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
245 guard(mutex)(&ac->mutex);
247 ac->saved.x = (s16) le16_to_cpu(buf[0]);
248 axis->x = ac->saved.x;
250 ac->saved.y = (s16) le16_to_cpu(buf[1]);
251 axis->y = ac->saved.y;
253 ac->saved.z = (s16) le16_to_cpu(buf[2]);
254 axis->z = ac->saved.z;
259 struct adxl34x_platform_data *pdata = &ac->pdata;
264 input_event(ac->input, pdata->ev_type, pdata->ev_code_x,
265 axis.x - ac->swcal.x);
266 input_event(ac->input, pdata->ev_type, pdata->ev_code_y,
267 axis.y - ac->swcal.y);
268 input_event(ac->input, pdata->ev_type, pdata->ev_code_z,
269 axis.z - ac->swcal.z);
272 static void adxl34x_report_key_single(struct input_dev *input, int key)
274 input_report_key(input, key, true);
275 input_sync(input);
276 input_report_key(input, key, false);
285 if (status & (1 << (ADXL_Z_AXIS - i)))
286 input_report_key(ac->input,
287 pdata->ev_code_tap[i], press);
295 input_sync(ac->input);
302 struct adxl34x_platform_data *pdata = &ac->pdata;
310 if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
318 adxl34x_report_key_single(ac->input, pdata->ev_code_ff);
321 dev_dbg(ac->dev, "OVERRUN\n");
330 if (pdata->ev_code_act_inactivity) {
332 input_report_key(ac->input,
333 pdata->ev_code_act_inactivity, 1);
335 input_report_key(ac->input,
336 pdata->ev_code_act_inactivity, 0);
342 if (pdata->orientation_enable) {
344 if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_2D) &&
349 if (ac->orient2d_saved != orient_code) {
350 ac->orient2d_saved = orient_code;
351 adxl34x_report_key_single(ac->input,
352 pdata->ev_codes_orient_2d[orient_code]);
356 if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_3D) &&
359 orient_code = ADXL346_3D_ORIENT(orient) - 1;
361 if (ac->orient3d_saved != orient_code) {
362 ac->orient3d_saved = orient_code;
363 adxl34x_report_key_single(ac->input,
364 pdata->ev_codes_orient_3d[orient_code]);
371 if (pdata->fifo_mode)
376 for (; samples > 0; samples--) {
388 * greater than 1.5 MHz to de-assert the CS pin to ensure a
392 if (ac->fifo_delay && (samples > 1))
397 input_sync(ac->input);
405 * A '0' places the ADXL34x into standby mode
413 AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
420 guard(mutex)(&ac->mutex);
422 if (!ac->suspended && !ac->disabled && ac->opened)
425 ac->suspended = true;
434 guard(mutex)(&ac->mutex);
436 if (ac->suspended && !ac->disabled && ac->opened)
439 ac->suspended = false;
449 return sprintf(buf, "%u\n", ac->disabled);
464 guard(mutex)(&ac->mutex);
466 if (!ac->suspended && ac->opened) {
468 if (!ac->disabled)
471 if (ac->disabled)
476 ac->disabled = !!val;
488 guard(mutex)(&ac->mutex);
491 ac->hwcal.x * 4 + ac->swcal.x,
492 ac->hwcal.y * 4 + ac->swcal.y,
493 ac->hwcal.z * 4 + ac->swcal.z);
507 guard(mutex)(&ac->mutex);
509 ac->hwcal.x -= (ac->saved.x / 4);
510 ac->swcal.x = ac->saved.x % 4;
512 ac->hwcal.y -= (ac->saved.y / 4);
513 ac->swcal.y = ac->saved.y % 4;
515 ac->hwcal.z -= (ac->saved.z / 4);
516 ac->swcal.z = ac->saved.z % 4;
518 AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
519 AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
520 AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
533 return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
548 guard(mutex)(&ac->mutex);
550 ac->pdata.data_rate = RATE(val);
552 ac->pdata.data_rate |
553 (ac->pdata.low_power_mode ? LOW_POWER : 0));
566 ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
581 guard(mutex)(&ac->mutex);
584 ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
586 ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
588 if (!ac->disabled && !ac->suspended && ac->opened)
589 AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
602 guard(mutex)(&ac->mutex);
605 ac->saved.x, ac->saved.y, ac->saved.z);
626 guard(mutex)(&ac->mutex);
657 static int adxl34x_input_open(struct input_dev *input)
659 struct adxl34x *ac = input_get_drvdata(input);
661 guard(mutex)(&ac->mutex);
663 if (!ac->suspended && !ac->disabled)
666 ac->opened = true;
671 static void adxl34x_input_close(struct input_dev *input)
673 struct adxl34x *ac = input_get_drvdata(input);
675 guard(mutex)(&ac->mutex);
677 if (!ac->suspended && !ac->disabled)
680 ac->opened = false;
695 return ERR_PTR(-ENODEV);
700 return ERR_PTR(-ENOMEM);
704 return ERR_PTR(-ENOMEM);
706 ac->fifo_delay = fifo_delay_default;
715 ac->pdata = *pdata;
716 pdata = &ac->pdata;
718 ac->input = input_dev;
719 ac->dev = dev;
720 ac->irq = irq;
721 ac->bops = bops;
723 mutex_init(&ac->mutex);
725 input_dev->name = "ADXL34x accelerometer";
730 ac->model = 345;
733 ac->model = 346;
736 dev_err(dev, "Failed to probe %s\n", input_dev->name);
737 return ERR_PTR(-ENODEV);
740 snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
742 input_dev->phys = ac->phys;
743 input_dev->id.product = ac->model;
744 input_dev->id.bustype = bops->bustype;
745 input_dev->open = adxl34x_input_open;
746 input_dev->close = adxl34x_input_close;
750 if (ac->pdata.ev_type == EV_REL) {
756 if (pdata->data_range & FULL_RES)
757 range = ADXL_FULLRES_MAX_VAL; /* Signed 13-bit */
759 range = ADXL_FIXEDRES_MAX_VAL; /* Signed 10-bit */
761 input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
762 input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
763 input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
766 input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_X_AXIS]);
767 input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_Y_AXIS]);
768 input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_Z_AXIS]);
770 if (pdata->ev_code_ff) {
771 ac->int_mask = FREE_FALL;
772 input_set_capability(input_dev, EV_KEY, pdata->ev_code_ff);
775 if (pdata->ev_code_act_inactivity)
777 pdata->ev_code_act_inactivity);
779 ac->int_mask |= ACTIVITY | INACTIVITY;
781 if (pdata->watermark) {
782 ac->int_mask |= WATERMARK;
783 if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
784 ac->pdata.fifo_mode |= FIFO_STREAM;
786 ac->int_mask |= DATA_READY;
789 if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
790 ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
792 if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
793 ac->fifo_delay = false;
797 error = devm_request_threaded_irq(dev, ac->irq, NULL, adxl34x_irq,
800 dev_err(dev, "irq %d busy?\n", ac->irq);
808 AC_WRITE(ac, OFSX, pdata->x_axis_offset);
809 ac->hwcal.x = pdata->x_axis_offset;
810 AC_WRITE(ac, OFSY, pdata->y_axis_offset);
811 ac->hwcal.y = pdata->y_axis_offset;
812 AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
813 ac->hwcal.z = pdata->z_axis_offset;
814 AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
815 AC_WRITE(ac, DUR, pdata->tap_duration);
816 AC_WRITE(ac, LATENT, pdata->tap_latency);
817 AC_WRITE(ac, WINDOW, pdata->tap_window);
818 AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
819 AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
820 AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
821 AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
822 AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
823 AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
824 AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
825 AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
826 (pdata->low_power_mode ? LOW_POWER : 0));
827 AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
828 AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
829 SAMPLES(pdata->watermark));
831 if (pdata->use_int2) {
833 AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
839 if (ac->model == 346 && ac->pdata.orientation_enable) {
841 ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
842 ORIENT_DIVISOR(ac->pdata.divisor_length));
844 ac->orient2d_saved = 1234;
845 ac->orient3d_saved = 1234;
847 if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
848 for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
850 pdata->ev_codes_orient_3d[i]);
852 if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
853 for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
855 pdata->ev_codes_orient_2d[i]);
857 ac->pdata.orientation_enable = 0;
860 AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
862 ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
871 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");