linux-stable/drivers/iio/accel/fxls8962af-core.c
Greg Kroah-Hartman cc6ce5ac2c First set of new device support, fixes, cleanups and features for IIO in 5.18
This cycle we had quite a few series that applied similar changes
 to lots of drivers. To keep this description manageable I have
 called those out in their own section rather than per driver.
 
 Particularly pleased to see the long running AFE precision series
 going in this cycle.
 
 Series includes some late breaking fixes.
 
 New device support
 * adi,ada4250 amplifier
   - New driver and dt bindings for this programmable gain amplifier.
 * adi,admv1014 microwave down-converter
   - New driver, dt bindings and some device specific ABI that
     may be generalized as more drivers for devices similar to this
     are added.
 * adi,admv4420 K Band down-converter.
   - New driver and dt bindings.
 * adi,adxl367 accelerometer driver.
   - New driver, dt-bindings + some new IIO ABI definitions to support
     reference magnitude events where an estimate of the acceleration
     due to gravity has been removed.
   - A few fixes as follow up patches.
 * adi,ltc2688 DAC with toggle and dither modes.
   - New driver and bindings. Includes some new driver specific (for now)
     ABI for handling toggle mode and the addition of a dither waveform to
     the DAC output.
 * AFE (analog front end) add support for additional types of analog device
   in front of an ADC.
   - RTD temperature sensors with dt bindings.
   - Temperature transducers wit dt bindings.
   - Related cleanup and features listed in other sections below.
 * maxim,ds3502 potentiometer.
   - Add support to ds1803 driver which required significant rework.
 * mediatek,mt2701-auxadc driver
   - Add mediatek,mt8186-auxadc - id table and chip specific info only.
 * semtech,sx9324, semtech,ax9360
   - Substantial refactoring of sx9310 to extract core logic for reuse
     into a separate module
   - New driver using this supporting sx9324 proximity sensors.
   - New driver using this supporting sx9360 proximity sensors.
 * silan,sc7a20
   - Compatible with the st,lis2dh (or nearly anyway) so add ID and
     chip specific info to enable support. Also silan vendor ID added
     for dt-bindings.
 
 Staging graduation
 * adi,ad7280a monitoring ADC for stacked lithium-ion batteries in
   electric cars and similar.
   - Substantial rework of driver required to bring inline with current
     IIO best practice. An unusual device in IIO so some interesting features
     we may see more of in future.
 
 Multiple driver/core cleanup
 - Use sysfs_emit() in simple locations where there is no path to change
   to various core created attributes.
 - Trivial white space fixes around inconsistency between space after { and
   before } in id tables.
 - Introduce new handling for fractional types to avoid repeated similar
   implementations. Use this in 3 drivers. Note this is also targeted
   at future use in the AFE driver and was motivated by discussions
   around the precision related work on that driver.
 - of related header cleanups - drop of*.h and add mod_devicetable.h as
   appropriate.
 - Move a number of symbol exports into IIO_* namespaces.  Two categories,
   1) Library used by multiple drivers e.g. st_sensors
   2) Core driver module exporting functions used by bus specific modules.
   A few related cleanups in this set.
 - Switch from CONFIG_PM_* guards to new DEFINE_SIMPLE_DEV_PM_OPS() and
   similar to simplify drivers and take advantage of these new macros
   allowing the compiler to do the job or removing unused code without
   the need for __maybe_unused markings. Conversion of other drivers to
   these new macros ongoing.
 
 Features
 * adi,adf4350
   - Switch from of specific to generic device properties enabling use with
     other firmware types.
 * adi,adx345
   - Switch from of specific to generic device properties.
   - Add ACPI ID ADS0345
   - Related driver cleanup.
 * adi,hmc425a
   - Switch from of specific to generic device properties.
 * afe analog rescaler driver
   - Wider range of types supported for scale.
   - Support offset.
   - Kunit tests.
 * atlas,ezo-sensor
   - Convert from of to device properties.
 * fsl,mma8452
   - Support mount matrix.
 * infineon,dps310:
   - Add ACPI ID IFX3100.
 * invensense,mpu6050
   - Convert to generic device properties.
 * maxim,ds1803
   - Add out_raw_available before supporting more devices.
   - Convert from of specific to device properties.
 * samsung,ssp_sensors
   - Convert from of specific to device properties.
 * st,stm32-timer trigger
   - Convert from of specific to device properties.
 * ti,hdc101x
   - Add ACPI ID TXNW1010.
 * ti,tsc2046:
   - Add read_raw support to enable use of iio_hwmon and similar.
 
 Fixes / cleanup.
 * mailmap
   - Update for Cai Huoqing
 * MAINTAINERS
   - Fix Analog Devices related links.
   - Add entry for ADRF6780
   - Add entry for ADMV1013
   - Add entry for AD7293
   - Add entry for ADMV8818
   - Update files listed for adis-lib
 * iio core:
   - Fix wrong comment about current_mode being something a driver should
     ever access.
   - Use struct_size() rather than open coding in industrialio-hw-consumer
 * adi,axl355
   - Use units.h definitions instead of local versions.
 * adi,adis-lib
   - Simplify *updated_bits() macro
   - Whitespace cleanup.
 * afe - Note many of these fixes only apply to particular configurations
   so the problems have probably not been seen in the wild, but will be
   visible with new usecases enabled this cycle.
   - Fix application of consumer scale for IIO_VAL_INT.
   - Apply a scale of 1 when no scale is provided.
   - Make best effort to establish a valid offset value for fractional
     cases.
   - Use s64 for scale calculations where parameters may be signed.
   - Tidy up include order.
   - Improve accuracy for small fractional sales
   - Reduce risk of integer overflow.
 * ams,as3935
   - Use devm_delayed_work_autocancel() to replace open coded equivalent.
 * aspeed,adc
   - Fix wrong use of divider flag.
 * atmel,sama5d2-adc
   - Relax atmel,trigger-edge-type to optional.
   - Drop Ludovic Desroches from listed maintainers of the dt-binding
     inline with previous MAINTAINERS entry update.
 * fsl,mma8452
   - Fix probing when i2c_device_id used.
   - dev_get_drvdata() on the iio_dev->dev, no longer returns iio_dev.
     Use dev_to_iio_dev() instead. Note the original path in here
     worked more by luck than design.
 * invensense,mpu6050
   - Drop ACPI_PTR() protection to avoid an unused warning.
   - Use fact ACPI_COMPANION() returns null when ACPI_HANDLE() does to
     simplify handling.
 * motorola,cpcap-adc
   - Drop unused assignment.
 * qcom,spmi-adc
   - Fix wrong example of 'reg' in binding document.
 * renesas,rzg2l-adc
   - Trivial typo fix.
 * semtech,sx9360
   - Fix wrong register handling for event generation.
 * st_sensors
   - Allow manual disabling of I2C or SPI module if not needed for a particular
     board. Default is still to enable the bus specific module if
     appropriate bus is supported.
 * st,lsm6dsx
   - dev_get_drvdata() on the iio_dev->dev, no longer returns iio_dev.
     Use dev_to_iio_dev() instead.
 * ti,palmas-gpadc
   - Split the interrupt fields in the dt-binding example
 * ti,tsc2046
   - Rework state machine to improve readability after recent debugging of
     an issue fixed elsewhere.
   - Add a sanity check to avoid very large memory allocations if a crazy
     delay is specified.
 * ti,twl6030
   - Add error handling if devm_request_threaded_irq() fails.
 * xilinx,ams
   - Use devm_delayed_work_autocancel() instead of open coding equivalent.
   - Fix missing required clock entry in dt-binding.
   - Fix miss counting of channels resulting in ps channels not
     being enabled.
   - Fix incorrect values written to sequencer registers.
   - Fix sequence for single channel reading.
 -----BEGIN PGP SIGNATURE-----
 
 iQJFBAABCAAvFiEEbilms4eEBlKRJoGxVIU0mcT0FogFAmIfdM8RHGppYzIzQGtl
 cm5lbC5vcmcACgkQVIU0mcT0FoiGlQ//UyRpMX9Bv97LAbMDnqIHLYroTLJA3WFQ
 AaL/DKB1cVjBCoHlp24qaQrmncvifPF7sKJGKWf7yCHL5fraAYL/kHsCo/jECTho
 QOk9QaPAMP9ChOoVoP8iz5qrdF2qyoFUG69bX+QYeKhSKzcK1QPRTQ13LIL43d9p
 OJX47Cu7FfFwuAs5VKSVgpcII0tctv+Fdo6BkkeI+6w/vx2sFSzRaqRtc1ZU4Uav
 s51dM9JMos52e/G8yQAEOC24QUId4EHxo7QR8WjzZ47yIHRulpYwM6pWAtvOqEy9
 eV++yz581+Uqs/qaDDk8nJdpa8aEv/NvfAK6gufB9UOWziMoR3G1pPFWoOLbcyIt
 IcUG+QyyEiIlmlwDE/m2OcSMzsxgrkEHNb3SE7ZkWZKP8OasGdVMHa7yEKCgLmzM
 S8EY9TsNA50A2VtowAPrdk74TVG2WeIDvEH2MMAUMjgW2DzsW9cmwFrziyj7ZPLX
 onoEjd/kpL2zzAArEadvzD1z1lLJcOUWn8ST2kbPQG8n/rp5y2u5PvgWRoO9zJlD
 ztX614XYRgRUhMrgb0q0nCTi07mnBZrR3P8Hnx1HOoZon/DIqPSL7NumITG09cQc
 fHqewQOU/WqoTH4tNvfywnBL/VAcxKFlc0B2rWIvp6dD5b0TU34ZdebcjLT1zYeC
 6YQKbRaRjVg=
 =UnWv
 -----END PGP SIGNATURE-----

Merge tag 'iio-for-5.18a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into char-misc-next

Jonathan writes:

First set of new device support, fixes, cleanups and features for IIO in 5.18

This cycle we had quite a few series that applied similar changes
to lots of drivers. To keep this description manageable I have
called those out in their own section rather than per driver.

Particularly pleased to see the long running AFE precision series
going in this cycle.

Series includes some late breaking fixes.

New device support
* adi,ada4250 amplifier
  - New driver and dt bindings for this programmable gain amplifier.
* adi,admv1014 microwave down-converter
  - New driver, dt bindings and some device specific ABI that
    may be generalized as more drivers for devices similar to this
    are added.
* adi,admv4420 K Band down-converter.
  - New driver and dt bindings.
* adi,adxl367 accelerometer driver.
  - New driver, dt-bindings + some new IIO ABI definitions to support
    reference magnitude events where an estimate of the acceleration
    due to gravity has been removed.
  - A few fixes as follow up patches.
* adi,ltc2688 DAC with toggle and dither modes.
  - New driver and bindings. Includes some new driver specific (for now)
    ABI for handling toggle mode and the addition of a dither waveform to
    the DAC output.
* AFE (analog front end) add support for additional types of analog device
  in front of an ADC.
  - RTD temperature sensors with dt bindings.
  - Temperature transducers wit dt bindings.
  - Related cleanup and features listed in other sections below.
* maxim,ds3502 potentiometer.
  - Add support to ds1803 driver which required significant rework.
* mediatek,mt2701-auxadc driver
  - Add mediatek,mt8186-auxadc - id table and chip specific info only.
* semtech,sx9324, semtech,ax9360
  - Substantial refactoring of sx9310 to extract core logic for reuse
    into a separate module
  - New driver using this supporting sx9324 proximity sensors.
  - New driver using this supporting sx9360 proximity sensors.
* silan,sc7a20
  - Compatible with the st,lis2dh (or nearly anyway) so add ID and
    chip specific info to enable support. Also silan vendor ID added
    for dt-bindings.

Staging graduation
* adi,ad7280a monitoring ADC for stacked lithium-ion batteries in
  electric cars and similar.
  - Substantial rework of driver required to bring inline with current
    IIO best practice. An unusual device in IIO so some interesting features
    we may see more of in future.

Multiple driver/core cleanup
- Use sysfs_emit() in simple locations where there is no path to change
  to various core created attributes.
- Trivial white space fixes around inconsistency between space after { and
  before } in id tables.
- Introduce new handling for fractional types to avoid repeated similar
  implementations. Use this in 3 drivers. Note this is also targeted
  at future use in the AFE driver and was motivated by discussions
  around the precision related work on that driver.
- of related header cleanups - drop of*.h and add mod_devicetable.h as
  appropriate.
- Move a number of symbol exports into IIO_* namespaces.  Two categories,
  1) Library used by multiple drivers e.g. st_sensors
  2) Core driver module exporting functions used by bus specific modules.
  A few related cleanups in this set.
- Switch from CONFIG_PM_* guards to new DEFINE_SIMPLE_DEV_PM_OPS() and
  similar to simplify drivers and take advantage of these new macros
  allowing the compiler to do the job or removing unused code without
  the need for __maybe_unused markings. Conversion of other drivers to
  these new macros ongoing.

Features
* adi,adf4350
  - Switch from of specific to generic device properties enabling use with
    other firmware types.
* adi,adx345
  - Switch from of specific to generic device properties.
  - Add ACPI ID ADS0345
  - Related driver cleanup.
* adi,hmc425a
  - Switch from of specific to generic device properties.
* afe analog rescaler driver
  - Wider range of types supported for scale.
  - Support offset.
  - Kunit tests.
* atlas,ezo-sensor
  - Convert from of to device properties.
* fsl,mma8452
  - Support mount matrix.
* infineon,dps310:
  - Add ACPI ID IFX3100.
* invensense,mpu6050
  - Convert to generic device properties.
* maxim,ds1803
  - Add out_raw_available before supporting more devices.
  - Convert from of specific to device properties.
* samsung,ssp_sensors
  - Convert from of specific to device properties.
* st,stm32-timer trigger
  - Convert from of specific to device properties.
* ti,hdc101x
  - Add ACPI ID TXNW1010.
* ti,tsc2046:
  - Add read_raw support to enable use of iio_hwmon and similar.

Fixes / cleanup.
* mailmap
  - Update for Cai Huoqing
* MAINTAINERS
  - Fix Analog Devices related links.
  - Add entry for ADRF6780
  - Add entry for ADMV1013
  - Add entry for AD7293
  - Add entry for ADMV8818
  - Update files listed for adis-lib
* iio core:
  - Fix wrong comment about current_mode being something a driver should
    ever access.
  - Use struct_size() rather than open coding in industrialio-hw-consumer
* adi,axl355
  - Use units.h definitions instead of local versions.
* adi,adis-lib
  - Simplify *updated_bits() macro
  - Whitespace cleanup.
* afe - Note many of these fixes only apply to particular configurations
  so the problems have probably not been seen in the wild, but will be
  visible with new usecases enabled this cycle.
  - Fix application of consumer scale for IIO_VAL_INT.
  - Apply a scale of 1 when no scale is provided.
  - Make best effort to establish a valid offset value for fractional
    cases.
  - Use s64 for scale calculations where parameters may be signed.
  - Tidy up include order.
  - Improve accuracy for small fractional sales
  - Reduce risk of integer overflow.
* ams,as3935
  - Use devm_delayed_work_autocancel() to replace open coded equivalent.
* aspeed,adc
  - Fix wrong use of divider flag.
* atmel,sama5d2-adc
  - Relax atmel,trigger-edge-type to optional.
  - Drop Ludovic Desroches from listed maintainers of the dt-binding
    inline with previous MAINTAINERS entry update.
* fsl,mma8452
  - Fix probing when i2c_device_id used.
  - dev_get_drvdata() on the iio_dev->dev, no longer returns iio_dev.
    Use dev_to_iio_dev() instead. Note the original path in here
    worked more by luck than design.
* invensense,mpu6050
  - Drop ACPI_PTR() protection to avoid an unused warning.
  - Use fact ACPI_COMPANION() returns null when ACPI_HANDLE() does to
    simplify handling.
* motorola,cpcap-adc
  - Drop unused assignment.
* qcom,spmi-adc
  - Fix wrong example of 'reg' in binding document.
* renesas,rzg2l-adc
  - Trivial typo fix.
* semtech,sx9360
  - Fix wrong register handling for event generation.
* st_sensors
  - Allow manual disabling of I2C or SPI module if not needed for a particular
    board. Default is still to enable the bus specific module if
    appropriate bus is supported.
* st,lsm6dsx
  - dev_get_drvdata() on the iio_dev->dev, no longer returns iio_dev.
    Use dev_to_iio_dev() instead.
* ti,palmas-gpadc
  - Split the interrupt fields in the dt-binding example
* ti,tsc2046
  - Rework state machine to improve readability after recent debugging of
    an issue fixed elsewhere.
  - Add a sanity check to avoid very large memory allocations if a crazy
    delay is specified.
* ti,twl6030
  - Add error handling if devm_request_threaded_irq() fails.
* xilinx,ams
  - Use devm_delayed_work_autocancel() instead of open coding equivalent.
  - Fix missing required clock entry in dt-binding.
  - Fix miss counting of channels resulting in ps channels not
    being enabled.
  - Fix incorrect values written to sequencer registers.
  - Fix sequence for single channel reading.

* tag 'iio-for-5.18a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (245 commits)
  iio: adc: xilinx-ams: Fix single channel switching sequence
  iio: adc: xilinx-ams: Fixed wrong sequencer register settings
  iio: adc: xilinx-ams: Fixed missing PS channels
  dt-bindings: iio: adc: zynqmp_ams: Add clock entry
  iio: accel: mma8452: use the correct logic to get mma8452_data
  iio: adc: aspeed: Add divider flag to fix incorrect voltage reading.
  iio: imu: st_lsm6dsx: use dev_to_iio_dev() to get iio_dev struct
  dt-bindings: iio: Add ltc2688 documentation
  iio: ABI: add ABI file for the LTC2688 DAC
  iio: dac: add support for ltc2688
  dt-bindings: iio: afe: add bindings for temperature transducers
  dt-bindings: iio: afe: add bindings for temperature-sense-rtd
  iio: afe: rescale: add temperature transducers
  iio: afe: rescale: add RTD temperature sensor support
  iio: test: add basic tests for the iio-rescale driver
  iio: afe: rescale: reduce risk of integer overflow
  iio: afe: rescale: fix accuracy for small fractional scales
  iio: afe: rescale: add offset support
  iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support
  iio: afe: rescale: expose scale processing function
  ...
2022-03-18 12:41:32 +01:00

1313 lines
32 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// SPDX-License-Identifier: GPL-2.0
/*
* NXP FXLS8962AF/FXLS8964AF Accelerometer Core Driver
*
* Copyright 2021 Connected Cars A/S
*
* Datasheet:
* https://www.nxp.com/docs/en/data-sheet/FXLS8962AF.pdf
* https://www.nxp.com/docs/en/data-sheet/FXLS8964AF.pdf
*
* Errata:
* https://www.nxp.com/docs/en/errata/ES_FXLS8962AF.pdf
*/
#include <linux/bits.h>
#include <linux/bitfield.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/of_irq.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/regmap.h>
#include <linux/iio/buffer.h>
#include <linux/iio/events.h>
#include <linux/iio/iio.h>
#include <linux/iio/kfifo_buf.h>
#include <linux/iio/sysfs.h>
#include "fxls8962af.h"
#define FXLS8962AF_INT_STATUS 0x00
#define FXLS8962AF_INT_STATUS_SRC_BOOT BIT(0)
#define FXLS8962AF_INT_STATUS_SRC_SDCD_OT BIT(4)
#define FXLS8962AF_INT_STATUS_SRC_BUF BIT(5)
#define FXLS8962AF_INT_STATUS_SRC_DRDY BIT(7)
#define FXLS8962AF_TEMP_OUT 0x01
#define FXLS8962AF_VECM_LSB 0x02
#define FXLS8962AF_OUT_X_LSB 0x04
#define FXLS8962AF_OUT_Y_LSB 0x06
#define FXLS8962AF_OUT_Z_LSB 0x08
#define FXLS8962AF_BUF_STATUS 0x0b
#define FXLS8962AF_BUF_STATUS_BUF_CNT GENMASK(5, 0)
#define FXLS8962AF_BUF_STATUS_BUF_OVF BIT(6)
#define FXLS8962AF_BUF_STATUS_BUF_WMRK BIT(7)
#define FXLS8962AF_BUF_X_LSB 0x0c
#define FXLS8962AF_BUF_Y_LSB 0x0e
#define FXLS8962AF_BUF_Z_LSB 0x10
#define FXLS8962AF_PROD_REV 0x12
#define FXLS8962AF_WHO_AM_I 0x13
#define FXLS8962AF_SYS_MODE 0x14
#define FXLS8962AF_SENS_CONFIG1 0x15
#define FXLS8962AF_SENS_CONFIG1_ACTIVE BIT(0)
#define FXLS8962AF_SENS_CONFIG1_RST BIT(7)
#define FXLS8962AF_SC1_FSR_MASK GENMASK(2, 1)
#define FXLS8962AF_SC1_FSR_PREP(x) FIELD_PREP(FXLS8962AF_SC1_FSR_MASK, (x))
#define FXLS8962AF_SC1_FSR_GET(x) FIELD_GET(FXLS8962AF_SC1_FSR_MASK, (x))
#define FXLS8962AF_SENS_CONFIG2 0x16
#define FXLS8962AF_SENS_CONFIG3 0x17
#define FXLS8962AF_SC3_WAKE_ODR_MASK GENMASK(7, 4)
#define FXLS8962AF_SC3_WAKE_ODR_PREP(x) FIELD_PREP(FXLS8962AF_SC3_WAKE_ODR_MASK, (x))
#define FXLS8962AF_SC3_WAKE_ODR_GET(x) FIELD_GET(FXLS8962AF_SC3_WAKE_ODR_MASK, (x))
#define FXLS8962AF_SENS_CONFIG4 0x18
#define FXLS8962AF_SC4_INT_PP_OD_MASK BIT(1)
#define FXLS8962AF_SC4_INT_PP_OD_PREP(x) FIELD_PREP(FXLS8962AF_SC4_INT_PP_OD_MASK, (x))
#define FXLS8962AF_SC4_INT_POL_MASK BIT(0)
#define FXLS8962AF_SC4_INT_POL_PREP(x) FIELD_PREP(FXLS8962AF_SC4_INT_POL_MASK, (x))
#define FXLS8962AF_SENS_CONFIG5 0x19
#define FXLS8962AF_WAKE_IDLE_LSB 0x1b
#define FXLS8962AF_SLEEP_IDLE_LSB 0x1c
#define FXLS8962AF_ASLP_COUNT_LSB 0x1e
#define FXLS8962AF_INT_EN 0x20
#define FXLS8962AF_INT_EN_SDCD_OT_EN BIT(5)
#define FXLS8962AF_INT_EN_BUF_EN BIT(6)
#define FXLS8962AF_INT_PIN_SEL 0x21
#define FXLS8962AF_INT_PIN_SEL_MASK GENMASK(7, 0)
#define FXLS8962AF_INT_PIN_SEL_INT1 0x00
#define FXLS8962AF_INT_PIN_SEL_INT2 GENMASK(7, 0)
#define FXLS8962AF_OFF_X 0x22
#define FXLS8962AF_OFF_Y 0x23
#define FXLS8962AF_OFF_Z 0x24
#define FXLS8962AF_BUF_CONFIG1 0x26
#define FXLS8962AF_BC1_BUF_MODE_MASK GENMASK(6, 5)
#define FXLS8962AF_BC1_BUF_MODE_PREP(x) FIELD_PREP(FXLS8962AF_BC1_BUF_MODE_MASK, (x))
#define FXLS8962AF_BUF_CONFIG2 0x27
#define FXLS8962AF_BUF_CONFIG2_BUF_WMRK GENMASK(5, 0)
#define FXLS8962AF_ORIENT_STATUS 0x28
#define FXLS8962AF_ORIENT_CONFIG 0x29
#define FXLS8962AF_ORIENT_DBCOUNT 0x2a
#define FXLS8962AF_ORIENT_BF_ZCOMP 0x2b
#define FXLS8962AF_ORIENT_THS_REG 0x2c
#define FXLS8962AF_SDCD_INT_SRC1 0x2d
#define FXLS8962AF_SDCD_INT_SRC1_X_OT BIT(5)
#define FXLS8962AF_SDCD_INT_SRC1_X_POL BIT(4)
#define FXLS8962AF_SDCD_INT_SRC1_Y_OT BIT(3)
#define FXLS8962AF_SDCD_INT_SRC1_Y_POL BIT(2)
#define FXLS8962AF_SDCD_INT_SRC1_Z_OT BIT(1)
#define FXLS8962AF_SDCD_INT_SRC1_Z_POL BIT(0)
#define FXLS8962AF_SDCD_INT_SRC2 0x2e
#define FXLS8962AF_SDCD_CONFIG1 0x2f
#define FXLS8962AF_SDCD_CONFIG1_Z_OT_EN BIT(3)
#define FXLS8962AF_SDCD_CONFIG1_Y_OT_EN BIT(4)
#define FXLS8962AF_SDCD_CONFIG1_X_OT_EN BIT(5)
#define FXLS8962AF_SDCD_CONFIG1_OT_ELE BIT(7)
#define FXLS8962AF_SDCD_CONFIG2 0x30
#define FXLS8962AF_SDCD_CONFIG2_SDCD_EN BIT(7)
#define FXLS8962AF_SC2_REF_UPDM_AC GENMASK(6, 5)
#define FXLS8962AF_SDCD_OT_DBCNT 0x31
#define FXLS8962AF_SDCD_WT_DBCNT 0x32
#define FXLS8962AF_SDCD_LTHS_LSB 0x33
#define FXLS8962AF_SDCD_UTHS_LSB 0x35
#define FXLS8962AF_SELF_TEST_CONFIG1 0x37
#define FXLS8962AF_SELF_TEST_CONFIG2 0x38
#define FXLS8962AF_MAX_REG 0x38
#define FXLS8962AF_DEVICE_ID 0x62
#define FXLS8964AF_DEVICE_ID 0x84
/* Raw temp channel offset */
#define FXLS8962AF_TEMP_CENTER_VAL 25
#define FXLS8962AF_AUTO_SUSPEND_DELAY_MS 2000
#define FXLS8962AF_FIFO_LENGTH 32
#define FXLS8962AF_SCALE_TABLE_LEN 4
#define FXLS8962AF_SAMP_FREQ_TABLE_LEN 13
static const int fxls8962af_scale_table[FXLS8962AF_SCALE_TABLE_LEN][2] = {
{0, IIO_G_TO_M_S_2(980000)},
{0, IIO_G_TO_M_S_2(1950000)},
{0, IIO_G_TO_M_S_2(3910000)},
{0, IIO_G_TO_M_S_2(7810000)},
};
static const int fxls8962af_samp_freq_table[FXLS8962AF_SAMP_FREQ_TABLE_LEN][2] = {
{3200, 0}, {1600, 0}, {800, 0}, {400, 0}, {200, 0}, {100, 0},
{50, 0}, {25, 0}, {12, 500000}, {6, 250000}, {3, 125000},
{1, 563000}, {0, 781000},
};
struct fxls8962af_chip_info {
const char *name;
const struct iio_chan_spec *channels;
int num_channels;
u8 chip_id;
};
struct fxls8962af_data {
struct regmap *regmap;
const struct fxls8962af_chip_info *chip_info;
struct regulator *vdd_reg;
struct {
__le16 channels[3];
s64 ts __aligned(8);
} scan;
int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */
struct iio_mount_matrix orientation;
int irq;
u8 watermark;
u8 enable_event;
u16 lower_thres;
u16 upper_thres;
};
const struct regmap_config fxls8962af_i2c_regmap_conf = {
.reg_bits = 8,
.val_bits = 8,
.max_register = FXLS8962AF_MAX_REG,
};
EXPORT_SYMBOL_NS_GPL(fxls8962af_i2c_regmap_conf, IIO_FXLS8962AF);
const struct regmap_config fxls8962af_spi_regmap_conf = {
.reg_bits = 8,
.pad_bits = 8,
.val_bits = 8,
.max_register = FXLS8962AF_MAX_REG,
};
EXPORT_SYMBOL_NS_GPL(fxls8962af_spi_regmap_conf, IIO_FXLS8962AF);
enum {
fxls8962af_idx_x,
fxls8962af_idx_y,
fxls8962af_idx_z,
fxls8962af_idx_ts,
};
enum fxls8962af_int_pin {
FXLS8962AF_PIN_INT1,
FXLS8962AF_PIN_INT2,
};
static int fxls8962af_power_on(struct fxls8962af_data *data)
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
ret = pm_runtime_resume_and_get(dev);
if (ret)
dev_err(dev, "failed to power on\n");
return ret;
}
static int fxls8962af_power_off(struct fxls8962af_data *data)
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
pm_runtime_mark_last_busy(dev);
ret = pm_runtime_put_autosuspend(dev);
if (ret)
dev_err(dev, "failed to power off\n");
return ret;
}
static int fxls8962af_standby(struct fxls8962af_data *data)
{
return regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG1,
FXLS8962AF_SENS_CONFIG1_ACTIVE, 0);
}
static int fxls8962af_active(struct fxls8962af_data *data)
{
return regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG1,
FXLS8962AF_SENS_CONFIG1_ACTIVE, 1);
}
static int fxls8962af_is_active(struct fxls8962af_data *data)
{
unsigned int reg;
int ret;
ret = regmap_read(data->regmap, FXLS8962AF_SENS_CONFIG1, &reg);
if (ret)
return ret;
return reg & FXLS8962AF_SENS_CONFIG1_ACTIVE;
}
static int fxls8962af_get_out(struct fxls8962af_data *data,
struct iio_chan_spec const *chan, int *val)
{
struct device *dev = regmap_get_device(data->regmap);
__le16 raw_val;
int is_active;
int ret;
is_active = fxls8962af_is_active(data);
if (!is_active) {
ret = fxls8962af_power_on(data);
if (ret)
return ret;
}
ret = regmap_bulk_read(data->regmap, chan->address,
&raw_val, sizeof(data->lower_thres));
if (!is_active)
fxls8962af_power_off(data);
if (ret) {
dev_err(dev, "failed to get out reg 0x%lx\n", chan->address);
return ret;
}
*val = sign_extend32(le16_to_cpu(raw_val),
chan->scan_type.realbits - 1);
return IIO_VAL_INT;
}
static int fxls8962af_read_avail(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
const int **vals, int *type, int *length,
long mask)
{
switch (mask) {
case IIO_CHAN_INFO_SCALE:
*type = IIO_VAL_INT_PLUS_NANO;
*vals = (int *)fxls8962af_scale_table;
*length = ARRAY_SIZE(fxls8962af_scale_table) * 2;
return IIO_AVAIL_LIST;
case IIO_CHAN_INFO_SAMP_FREQ:
*type = IIO_VAL_INT_PLUS_MICRO;
*vals = (int *)fxls8962af_samp_freq_table;
*length = ARRAY_SIZE(fxls8962af_samp_freq_table) * 2;
return IIO_AVAIL_LIST;
default:
return -EINVAL;
}
}
static int fxls8962af_write_raw_get_fmt(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
long mask)
{
switch (mask) {
case IIO_CHAN_INFO_SCALE:
return IIO_VAL_INT_PLUS_NANO;
case IIO_CHAN_INFO_SAMP_FREQ:
return IIO_VAL_INT_PLUS_MICRO;
default:
return IIO_VAL_INT_PLUS_NANO;
}
}
static int fxls8962af_update_config(struct fxls8962af_data *data, u8 reg,
u8 mask, u8 val)
{
int ret;
int is_active;
is_active = fxls8962af_is_active(data);
if (is_active) {
ret = fxls8962af_standby(data);
if (ret)
return ret;
}
ret = regmap_update_bits(data->regmap, reg, mask, val);
if (ret)
return ret;
if (is_active) {
ret = fxls8962af_active(data);
if (ret)
return ret;
}
return 0;
}
static int fxls8962af_set_full_scale(struct fxls8962af_data *data, u32 scale)
{
int i;
for (i = 0; i < ARRAY_SIZE(fxls8962af_scale_table); i++)
if (scale == fxls8962af_scale_table[i][1])
break;
if (i == ARRAY_SIZE(fxls8962af_scale_table))
return -EINVAL;
return fxls8962af_update_config(data, FXLS8962AF_SENS_CONFIG1,
FXLS8962AF_SC1_FSR_MASK,
FXLS8962AF_SC1_FSR_PREP(i));
}
static unsigned int fxls8962af_read_full_scale(struct fxls8962af_data *data,
int *val)
{
int ret;
unsigned int reg;
u8 range_idx;
ret = regmap_read(data->regmap, FXLS8962AF_SENS_CONFIG1, &reg);
if (ret)
return ret;
range_idx = FXLS8962AF_SC1_FSR_GET(reg);
*val = fxls8962af_scale_table[range_idx][1];
return IIO_VAL_INT_PLUS_NANO;
}
static int fxls8962af_set_samp_freq(struct fxls8962af_data *data, u32 val,
u32 val2)
{
int i;
for (i = 0; i < ARRAY_SIZE(fxls8962af_samp_freq_table); i++)
if (val == fxls8962af_samp_freq_table[i][0] &&
val2 == fxls8962af_samp_freq_table[i][1])
break;
if (i == ARRAY_SIZE(fxls8962af_samp_freq_table))
return -EINVAL;
return fxls8962af_update_config(data, FXLS8962AF_SENS_CONFIG3,
FXLS8962AF_SC3_WAKE_ODR_MASK,
FXLS8962AF_SC3_WAKE_ODR_PREP(i));
}
static unsigned int fxls8962af_read_samp_freq(struct fxls8962af_data *data,
int *val, int *val2)
{
int ret;
unsigned int reg;
u8 range_idx;
ret = regmap_read(data->regmap, FXLS8962AF_SENS_CONFIG3, &reg);
if (ret)
return ret;
range_idx = FXLS8962AF_SC3_WAKE_ODR_GET(reg);
*val = fxls8962af_samp_freq_table[range_idx][0];
*val2 = fxls8962af_samp_freq_table[range_idx][1];
return IIO_VAL_INT_PLUS_MICRO;
}
static int fxls8962af_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
switch (mask) {
case IIO_CHAN_INFO_RAW:
switch (chan->type) {
case IIO_TEMP:
case IIO_ACCEL:
return fxls8962af_get_out(data, chan, val);
default:
return -EINVAL;
}
case IIO_CHAN_INFO_OFFSET:
if (chan->type != IIO_TEMP)
return -EINVAL;
*val = FXLS8962AF_TEMP_CENTER_VAL;
return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
*val = 0;
return fxls8962af_read_full_scale(data, val2);
case IIO_CHAN_INFO_SAMP_FREQ:
return fxls8962af_read_samp_freq(data, val, val2);
default:
return -EINVAL;
}
}
static int fxls8962af_write_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val, int val2, long mask)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
int ret;
switch (mask) {
case IIO_CHAN_INFO_SCALE:
if (val != 0)
return -EINVAL;
ret = iio_device_claim_direct_mode(indio_dev);
if (ret)
return ret;
ret = fxls8962af_set_full_scale(data, val2);
iio_device_release_direct_mode(indio_dev);
return ret;
case IIO_CHAN_INFO_SAMP_FREQ:
ret = iio_device_claim_direct_mode(indio_dev);
if (ret)
return ret;
ret = fxls8962af_set_samp_freq(data, val, val2);
iio_device_release_direct_mode(indio_dev);
return ret;
default:
return -EINVAL;
}
}
static int fxls8962af_event_setup(struct fxls8962af_data *data, int state)
{
/* Enable wakeup interrupt */
int mask = FXLS8962AF_INT_EN_SDCD_OT_EN;
int value = state ? mask : 0;
return regmap_update_bits(data->regmap, FXLS8962AF_INT_EN, mask, value);
}
static int fxls8962af_set_watermark(struct iio_dev *indio_dev, unsigned val)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
if (val > FXLS8962AF_FIFO_LENGTH)
val = FXLS8962AF_FIFO_LENGTH;
data->watermark = val;
return 0;
}
static int __fxls8962af_set_thresholds(struct fxls8962af_data *data,
const struct iio_chan_spec *chan,
enum iio_event_direction dir,
int val)
{
switch (dir) {
case IIO_EV_DIR_FALLING:
data->lower_thres = val;
return regmap_bulk_write(data->regmap, FXLS8962AF_SDCD_LTHS_LSB,
&data->lower_thres, sizeof(data->lower_thres));
case IIO_EV_DIR_RISING:
data->upper_thres = val;
return regmap_bulk_write(data->regmap, FXLS8962AF_SDCD_UTHS_LSB,
&data->upper_thres, sizeof(data->upper_thres));
default:
return -EINVAL;
}
}
static int fxls8962af_read_event(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan,
enum iio_event_type type,
enum iio_event_direction dir,
enum iio_event_info info,
int *val, int *val2)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
int ret;
if (type != IIO_EV_TYPE_THRESH)
return -EINVAL;
switch (dir) {
case IIO_EV_DIR_FALLING:
ret = regmap_bulk_read(data->regmap, FXLS8962AF_SDCD_LTHS_LSB,
&data->lower_thres, sizeof(data->lower_thres));
if (ret)
return ret;
*val = sign_extend32(data->lower_thres, chan->scan_type.realbits - 1);
return IIO_VAL_INT;
case IIO_EV_DIR_RISING:
ret = regmap_bulk_read(data->regmap, FXLS8962AF_SDCD_UTHS_LSB,
&data->upper_thres, sizeof(data->upper_thres));
if (ret)
return ret;
*val = sign_extend32(data->upper_thres, chan->scan_type.realbits - 1);
return IIO_VAL_INT;
default:
return -EINVAL;
}
}
static int fxls8962af_write_event(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan,
enum iio_event_type type,
enum iio_event_direction dir,
enum iio_event_info info,
int val, int val2)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
int ret, val_masked;
if (type != IIO_EV_TYPE_THRESH)
return -EINVAL;
if (val < -2048 || val > 2047)
return -EINVAL;
if (data->enable_event)
return -EBUSY;
val_masked = val & GENMASK(11, 0);
if (fxls8962af_is_active(data)) {
ret = fxls8962af_standby(data);
if (ret)
return ret;
ret = __fxls8962af_set_thresholds(data, chan, dir, val_masked);
if (ret)
return ret;
return fxls8962af_active(data);
} else {
return __fxls8962af_set_thresholds(data, chan, dir, val_masked);
}
}
static int
fxls8962af_read_event_config(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan,
enum iio_event_type type,
enum iio_event_direction dir)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
if (type != IIO_EV_TYPE_THRESH)
return -EINVAL;
switch (chan->channel2) {
case IIO_MOD_X:
return !!(FXLS8962AF_SDCD_CONFIG1_X_OT_EN & data->enable_event);
case IIO_MOD_Y:
return !!(FXLS8962AF_SDCD_CONFIG1_Y_OT_EN & data->enable_event);
case IIO_MOD_Z:
return !!(FXLS8962AF_SDCD_CONFIG1_Z_OT_EN & data->enable_event);
default:
return -EINVAL;
}
}
static int
fxls8962af_write_event_config(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan,
enum iio_event_type type,
enum iio_event_direction dir, int state)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
u8 enable_event, enable_bits;
int ret, value;
if (type != IIO_EV_TYPE_THRESH)
return -EINVAL;
switch (chan->channel2) {
case IIO_MOD_X:
enable_bits = FXLS8962AF_SDCD_CONFIG1_X_OT_EN;
break;
case IIO_MOD_Y:
enable_bits = FXLS8962AF_SDCD_CONFIG1_Y_OT_EN;
break;
case IIO_MOD_Z:
enable_bits = FXLS8962AF_SDCD_CONFIG1_Z_OT_EN;
break;
default:
return -EINVAL;
}
if (state)
enable_event = data->enable_event | enable_bits;
else
enable_event = data->enable_event & ~enable_bits;
if (data->enable_event == enable_event)
return 0;
ret = fxls8962af_standby(data);
if (ret)
return ret;
/* Enable events */
value = enable_event | FXLS8962AF_SDCD_CONFIG1_OT_ELE;
ret = regmap_write(data->regmap, FXLS8962AF_SDCD_CONFIG1, value);
if (ret)
return ret;
/*
* Enable update of SDCD_REF_X/Y/Z values with the current decimated and
* trimmed X/Y/Z acceleration input data. This allows for acceleration
* slope detection with Data(n) to Data(n1) always used as the input
* to the window comparator.
*/
value = enable_event ?
FXLS8962AF_SDCD_CONFIG2_SDCD_EN | FXLS8962AF_SC2_REF_UPDM_AC :
0x00;
ret = regmap_write(data->regmap, FXLS8962AF_SDCD_CONFIG2, value);
if (ret)
return ret;
ret = fxls8962af_event_setup(data, state);
if (ret)
return ret;
data->enable_event = enable_event;
if (data->enable_event) {
fxls8962af_active(data);
ret = fxls8962af_power_on(data);
} else {
ret = iio_device_claim_direct_mode(indio_dev);
if (ret)
return ret;
/* Not in buffered mode so disable power */
ret = fxls8962af_power_off(data);
iio_device_release_direct_mode(indio_dev);
}
return ret;
}
static const struct iio_event_spec fxls8962af_event[] = {
{
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_EITHER,
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
},
{
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_FALLING,
.mask_separate = BIT(IIO_EV_INFO_VALUE),
},
{
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_RISING,
.mask_separate = BIT(IIO_EV_INFO_VALUE),
},
};
#define FXLS8962AF_CHANNEL(axis, reg, idx) { \
.type = IIO_ACCEL, \
.address = reg, \
.modified = 1, \
.channel2 = IIO_MOD_##axis, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
BIT(IIO_CHAN_INFO_SAMP_FREQ), \
.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) | \
BIT(IIO_CHAN_INFO_SAMP_FREQ), \
.scan_index = idx, \
.scan_type = { \
.sign = 's', \
.realbits = 12, \
.storagebits = 16, \
.shift = 4, \
.endianness = IIO_BE, \
}, \
.event_spec = fxls8962af_event, \
.num_event_specs = ARRAY_SIZE(fxls8962af_event), \
}
#define FXLS8962AF_TEMP_CHANNEL { \
.type = IIO_TEMP, \
.address = FXLS8962AF_TEMP_OUT, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_OFFSET),\
.scan_index = -1, \
.scan_type = { \
.realbits = 8, \
.storagebits = 8, \
}, \
}
static const struct iio_chan_spec fxls8962af_channels[] = {
FXLS8962AF_CHANNEL(X, FXLS8962AF_OUT_X_LSB, fxls8962af_idx_x),
FXLS8962AF_CHANNEL(Y, FXLS8962AF_OUT_Y_LSB, fxls8962af_idx_y),
FXLS8962AF_CHANNEL(Z, FXLS8962AF_OUT_Z_LSB, fxls8962af_idx_z),
IIO_CHAN_SOFT_TIMESTAMP(fxls8962af_idx_ts),
FXLS8962AF_TEMP_CHANNEL,
};
static const struct fxls8962af_chip_info fxls_chip_info_table[] = {
[fxls8962af] = {
.chip_id = FXLS8962AF_DEVICE_ID,
.name = "fxls8962af",
.channels = fxls8962af_channels,
.num_channels = ARRAY_SIZE(fxls8962af_channels),
},
[fxls8964af] = {
.chip_id = FXLS8964AF_DEVICE_ID,
.name = "fxls8964af",
.channels = fxls8962af_channels,
.num_channels = ARRAY_SIZE(fxls8962af_channels),
},
};
static const struct iio_info fxls8962af_info = {
.read_raw = &fxls8962af_read_raw,
.write_raw = &fxls8962af_write_raw,
.write_raw_get_fmt = fxls8962af_write_raw_get_fmt,
.read_event_value = fxls8962af_read_event,
.write_event_value = fxls8962af_write_event,
.read_event_config = fxls8962af_read_event_config,
.write_event_config = fxls8962af_write_event_config,
.read_avail = fxls8962af_read_avail,
.hwfifo_set_watermark = fxls8962af_set_watermark,
};
static int fxls8962af_reset(struct fxls8962af_data *data)
{
struct device *dev = regmap_get_device(data->regmap);
unsigned int reg;
int ret;
ret = regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG1,
FXLS8962AF_SENS_CONFIG1_RST,
FXLS8962AF_SENS_CONFIG1_RST);
if (ret)
return ret;
/* TBOOT1, TBOOT2, specifies we have to wait between 1 - 17.7ms */
ret = regmap_read_poll_timeout(data->regmap, FXLS8962AF_INT_STATUS, reg,
(reg & FXLS8962AF_INT_STATUS_SRC_BOOT),
1000, 18000);
if (ret == -ETIMEDOUT)
dev_err(dev, "reset timeout, int_status = 0x%x\n", reg);
return ret;
}
static int __fxls8962af_fifo_set_mode(struct fxls8962af_data *data, bool onoff)
{
int ret;
/* Enable watermark at max fifo size */
ret = regmap_update_bits(data->regmap, FXLS8962AF_BUF_CONFIG2,
FXLS8962AF_BUF_CONFIG2_BUF_WMRK,
data->watermark);
if (ret)
return ret;
return regmap_update_bits(data->regmap, FXLS8962AF_BUF_CONFIG1,
FXLS8962AF_BC1_BUF_MODE_MASK,
FXLS8962AF_BC1_BUF_MODE_PREP(onoff));
}
static int fxls8962af_buffer_preenable(struct iio_dev *indio_dev)
{
return fxls8962af_power_on(iio_priv(indio_dev));
}
static int fxls8962af_buffer_postenable(struct iio_dev *indio_dev)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
int ret;
fxls8962af_standby(data);
/* Enable buffer interrupt */
ret = regmap_update_bits(data->regmap, FXLS8962AF_INT_EN,
FXLS8962AF_INT_EN_BUF_EN,
FXLS8962AF_INT_EN_BUF_EN);
if (ret)
return ret;
ret = __fxls8962af_fifo_set_mode(data, true);
fxls8962af_active(data);
return ret;
}
static int fxls8962af_buffer_predisable(struct iio_dev *indio_dev)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
int ret;
fxls8962af_standby(data);
/* Disable buffer interrupt */
ret = regmap_update_bits(data->regmap, FXLS8962AF_INT_EN,
FXLS8962AF_INT_EN_BUF_EN, 0);
if (ret)
return ret;
ret = __fxls8962af_fifo_set_mode(data, false);
if (data->enable_event)
fxls8962af_active(data);
return ret;
}
static int fxls8962af_buffer_postdisable(struct iio_dev *indio_dev)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
if (!data->enable_event)
fxls8962af_power_off(data);
return 0;
}
static const struct iio_buffer_setup_ops fxls8962af_buffer_ops = {
.preenable = fxls8962af_buffer_preenable,
.postenable = fxls8962af_buffer_postenable,
.predisable = fxls8962af_buffer_predisable,
.postdisable = fxls8962af_buffer_postdisable,
};
static int fxls8962af_i2c_raw_read_errata3(struct fxls8962af_data *data,
u16 *buffer, int samples,
int sample_length)
{
int i, ret;
for (i = 0; i < samples; i++) {
ret = regmap_raw_read(data->regmap, FXLS8962AF_BUF_X_LSB,
&buffer[i * 3], sample_length);
if (ret)
return ret;
}
return 0;
}
static int fxls8962af_fifo_transfer(struct fxls8962af_data *data,
u16 *buffer, int samples)
{
struct device *dev = regmap_get_device(data->regmap);
int sample_length = 3 * sizeof(*buffer);
int total_length = samples * sample_length;
int ret;
if (i2c_verify_client(dev))
/*
* Due to errata bug:
* E3: FIFO burst read operation error using I2C interface
* We have to avoid burst reads on I2C..
*/
ret = fxls8962af_i2c_raw_read_errata3(data, buffer, samples,
sample_length);
else
ret = regmap_raw_read(data->regmap, FXLS8962AF_BUF_X_LSB, buffer,
total_length);
if (ret)
dev_err(dev, "Error transferring data from fifo: %d\n", ret);
return ret;
}
static int fxls8962af_fifo_flush(struct iio_dev *indio_dev)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
struct device *dev = regmap_get_device(data->regmap);
u16 buffer[FXLS8962AF_FIFO_LENGTH * 3];
uint64_t sample_period;
unsigned int reg;
int64_t tstamp;
int ret, i;
u8 count;
ret = regmap_read(data->regmap, FXLS8962AF_BUF_STATUS, &reg);
if (ret)
return ret;
if (reg & FXLS8962AF_BUF_STATUS_BUF_OVF) {
dev_err(dev, "Buffer overflow");
return -EOVERFLOW;
}
count = reg & FXLS8962AF_BUF_STATUS_BUF_CNT;
if (!count)
return 0;
data->old_timestamp = data->timestamp;
data->timestamp = iio_get_time_ns(indio_dev);
/*
* Approximate timestamps for each of the sample based on the sampling,
* frequency, timestamp for last sample and number of samples.
*/
sample_period = (data->timestamp - data->old_timestamp);
do_div(sample_period, count);
tstamp = data->timestamp - (count - 1) * sample_period;
ret = fxls8962af_fifo_transfer(data, buffer, count);
if (ret)
return ret;
/* Demux hw FIFO into kfifo. */
for (i = 0; i < count; i++) {
int j, bit;
j = 0;
for_each_set_bit(bit, indio_dev->active_scan_mask,
indio_dev->masklength) {
memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
sizeof(data->scan.channels[0]));
}
iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
tstamp);
tstamp += sample_period;
}
return count;
}
static int fxls8962af_event_interrupt(struct iio_dev *indio_dev)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
s64 ts = iio_get_time_ns(indio_dev);
unsigned int reg;
u64 ev_code;
int ret;
ret = regmap_read(data->regmap, FXLS8962AF_SDCD_INT_SRC1, &reg);
if (ret)
return ret;
if (reg & FXLS8962AF_SDCD_INT_SRC1_X_OT) {
ev_code = reg & FXLS8962AF_SDCD_INT_SRC1_X_POL ?
IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING;
iio_push_event(indio_dev,
IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
IIO_EV_TYPE_THRESH, ev_code), ts);
}
if (reg & FXLS8962AF_SDCD_INT_SRC1_Y_OT) {
ev_code = reg & FXLS8962AF_SDCD_INT_SRC1_Y_POL ?
IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING;
iio_push_event(indio_dev,
IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
IIO_EV_TYPE_THRESH, ev_code), ts);
}
if (reg & FXLS8962AF_SDCD_INT_SRC1_Z_OT) {
ev_code = reg & FXLS8962AF_SDCD_INT_SRC1_Z_POL ?
IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING;
iio_push_event(indio_dev,
IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
IIO_EV_TYPE_THRESH, ev_code), ts);
}
return 0;
}
static irqreturn_t fxls8962af_interrupt(int irq, void *p)
{
struct iio_dev *indio_dev = p;
struct fxls8962af_data *data = iio_priv(indio_dev);
unsigned int reg;
int ret;
ret = regmap_read(data->regmap, FXLS8962AF_INT_STATUS, &reg);
if (ret)
return IRQ_NONE;
if (reg & FXLS8962AF_INT_STATUS_SRC_BUF) {
ret = fxls8962af_fifo_flush(indio_dev);
if (ret < 0)
return IRQ_NONE;
return IRQ_HANDLED;
}
if (reg & FXLS8962AF_INT_STATUS_SRC_SDCD_OT) {
ret = fxls8962af_event_interrupt(indio_dev);
if (ret < 0)
return IRQ_NONE;
return IRQ_HANDLED;
}
return IRQ_NONE;
}
static void fxls8962af_regulator_disable(void *data_ptr)
{
struct fxls8962af_data *data = data_ptr;
regulator_disable(data->vdd_reg);
}
static void fxls8962af_pm_disable(void *dev_ptr)
{
struct device *dev = dev_ptr;
struct iio_dev *indio_dev = dev_get_drvdata(dev);
pm_runtime_disable(dev);
pm_runtime_set_suspended(dev);
pm_runtime_put_noidle(dev);
fxls8962af_standby(iio_priv(indio_dev));
}
static void fxls8962af_get_irq(struct device_node *of_node,
enum fxls8962af_int_pin *pin)
{
int irq;
irq = of_irq_get_byname(of_node, "INT2");
if (irq > 0) {
*pin = FXLS8962AF_PIN_INT2;
return;
}
*pin = FXLS8962AF_PIN_INT1;
}
static int fxls8962af_irq_setup(struct iio_dev *indio_dev, int irq)
{
struct fxls8962af_data *data = iio_priv(indio_dev);
struct device *dev = regmap_get_device(data->regmap);
unsigned long irq_type;
bool irq_active_high;
enum fxls8962af_int_pin int_pin;
u8 int_pin_sel;
int ret;
fxls8962af_get_irq(dev->of_node, &int_pin);
switch (int_pin) {
case FXLS8962AF_PIN_INT1:
int_pin_sel = FXLS8962AF_INT_PIN_SEL_INT1;
break;
case FXLS8962AF_PIN_INT2:
int_pin_sel = FXLS8962AF_INT_PIN_SEL_INT2;
break;
default:
dev_err(dev, "unsupported int pin selected\n");
return -EINVAL;
}
ret = regmap_update_bits(data->regmap, FXLS8962AF_INT_PIN_SEL,
FXLS8962AF_INT_PIN_SEL_MASK, int_pin_sel);
if (ret)
return ret;
irq_type = irqd_get_trigger_type(irq_get_irq_data(irq));
switch (irq_type) {
case IRQF_TRIGGER_HIGH:
case IRQF_TRIGGER_RISING:
irq_active_high = true;
break;
case IRQF_TRIGGER_LOW:
case IRQF_TRIGGER_FALLING:
irq_active_high = false;
break;
default:
dev_info(dev, "mode %lx unsupported\n", irq_type);
return -EINVAL;
}
ret = regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG4,
FXLS8962AF_SC4_INT_POL_MASK,
FXLS8962AF_SC4_INT_POL_PREP(irq_active_high));
if (ret)
return ret;
if (device_property_read_bool(dev, "drive-open-drain")) {
ret = regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG4,
FXLS8962AF_SC4_INT_PP_OD_MASK,
FXLS8962AF_SC4_INT_PP_OD_PREP(1));
if (ret)
return ret;
irq_type |= IRQF_SHARED;
}
return devm_request_threaded_irq(dev,
irq,
NULL, fxls8962af_interrupt,
irq_type | IRQF_ONESHOT,
indio_dev->name, indio_dev);
}
int fxls8962af_core_probe(struct device *dev, struct regmap *regmap, int irq)
{
struct fxls8962af_data *data;
struct iio_dev *indio_dev;
unsigned int reg;
int ret, i;
indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
if (!indio_dev)
return -ENOMEM;
data = iio_priv(indio_dev);
dev_set_drvdata(dev, indio_dev);
data->regmap = regmap;
data->irq = irq;
ret = iio_read_mount_matrix(dev, &data->orientation);
if (ret)
return ret;
data->vdd_reg = devm_regulator_get(dev, "vdd");
if (IS_ERR(data->vdd_reg))
return dev_err_probe(dev, PTR_ERR(data->vdd_reg),
"Failed to get vdd regulator\n");
ret = regulator_enable(data->vdd_reg);
if (ret) {
dev_err(dev, "Failed to enable vdd regulator: %d\n", ret);
return ret;
}
ret = devm_add_action_or_reset(dev, fxls8962af_regulator_disable, data);
if (ret)
return ret;
ret = regmap_read(data->regmap, FXLS8962AF_WHO_AM_I, &reg);
if (ret)
return ret;
for (i = 0; i < ARRAY_SIZE(fxls_chip_info_table); i++) {
if (fxls_chip_info_table[i].chip_id == reg) {
data->chip_info = &fxls_chip_info_table[i];
break;
}
}
if (i == ARRAY_SIZE(fxls_chip_info_table)) {
dev_err(dev, "failed to match device in table\n");
return -ENXIO;
}
indio_dev->channels = data->chip_info->channels;
indio_dev->num_channels = data->chip_info->num_channels;
indio_dev->name = data->chip_info->name;
indio_dev->info = &fxls8962af_info;
indio_dev->modes = INDIO_DIRECT_MODE;
ret = fxls8962af_reset(data);
if (ret)
return ret;
if (irq) {
ret = fxls8962af_irq_setup(indio_dev, irq);
if (ret)
return ret;
ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
INDIO_BUFFER_SOFTWARE,
&fxls8962af_buffer_ops);
if (ret)
return ret;
}
ret = pm_runtime_set_active(dev);
if (ret)
return ret;
pm_runtime_enable(dev);
pm_runtime_set_autosuspend_delay(dev, FXLS8962AF_AUTO_SUSPEND_DELAY_MS);
pm_runtime_use_autosuspend(dev);
ret = devm_add_action_or_reset(dev, fxls8962af_pm_disable, dev);
if (ret)
return ret;
if (device_property_read_bool(dev, "wakeup-source"))
device_init_wakeup(dev, true);
return devm_iio_device_register(dev, indio_dev);
}
EXPORT_SYMBOL_NS_GPL(fxls8962af_core_probe, IIO_FXLS8962AF);
static int __maybe_unused fxls8962af_runtime_suspend(struct device *dev)
{
struct fxls8962af_data *data = iio_priv(dev_get_drvdata(dev));
int ret;
ret = fxls8962af_standby(data);
if (ret) {
dev_err(dev, "powering off device failed\n");
return ret;
}
return 0;
}
static int __maybe_unused fxls8962af_runtime_resume(struct device *dev)
{
struct fxls8962af_data *data = iio_priv(dev_get_drvdata(dev));
return fxls8962af_active(data);
}
static int __maybe_unused fxls8962af_suspend(struct device *dev)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct fxls8962af_data *data = iio_priv(indio_dev);
if (device_may_wakeup(dev) && data->enable_event) {
enable_irq_wake(data->irq);
/*
* Disable buffer, as the buffer is so small the device will wake
* almost immediately.
*/
if (iio_buffer_enabled(indio_dev))
fxls8962af_buffer_predisable(indio_dev);
} else {
fxls8962af_runtime_suspend(dev);
}
return 0;
}
static int __maybe_unused fxls8962af_resume(struct device *dev)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct fxls8962af_data *data = iio_priv(indio_dev);
if (device_may_wakeup(dev) && data->enable_event) {
disable_irq_wake(data->irq);
if (iio_buffer_enabled(indio_dev))
fxls8962af_buffer_postenable(indio_dev);
} else {
fxls8962af_runtime_resume(dev);
}
return 0;
}
const struct dev_pm_ops fxls8962af_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(fxls8962af_suspend, fxls8962af_resume)
SET_RUNTIME_PM_OPS(fxls8962af_runtime_suspend,
fxls8962af_runtime_resume, NULL)
};
EXPORT_SYMBOL_NS_GPL(fxls8962af_pm_ops, IIO_FXLS8962AF);
MODULE_AUTHOR("Sean Nyekjaer <sean@geanix.com>");
MODULE_DESCRIPTION("NXP FXLS8962AF/FXLS8964AF accelerometer driver");
MODULE_LICENSE("GPL v2");