linux/drivers/iio/accel/mma9551.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

624 lines
14 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* Freescale MMA9551L Intelligent Motion-Sensing Platform driver
* Copyright (c) 2014, Intel Corporation.
*/
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/events.h>
#include <linux/pm_runtime.h>
#include "mma9551_core.h"
#define MMA9551_DRV_NAME "mma9551"
#define MMA9551_IRQ_NAME "mma9551_event"
#define MMA9551_GPIO_COUNT 4
/* Tilt application (inclination in IIO terms). */
#define MMA9551_TILT_XZ_ANG_REG 0x00
#define MMA9551_TILT_YZ_ANG_REG 0x01
#define MMA9551_TILT_XY_ANG_REG 0x02
#define MMA9551_TILT_ANGFLG BIT(7)
#define MMA9551_TILT_QUAD_REG 0x03
#define MMA9551_TILT_XY_QUAD_SHIFT 0
#define MMA9551_TILT_YZ_QUAD_SHIFT 2
#define MMA9551_TILT_XZ_QUAD_SHIFT 4
#define MMA9551_TILT_CFG_REG 0x01
#define MMA9551_TILT_ANG_THRESH_MASK GENMASK(3, 0)
#define MMA9551_DEFAULT_SAMPLE_RATE 122 /* Hz */
/* Tilt events are mapped to the first three GPIO pins. */
enum mma9551_tilt_axis {
mma9551_x = 0,
mma9551_y,
mma9551_z,
};
struct mma9551_data {
struct i2c_client *client;
struct mutex mutex;
int event_enabled[3];
int irqs[MMA9551_GPIO_COUNT];
};
static int mma9551_read_incli_chan(struct i2c_client *client,
const struct iio_chan_spec *chan,
int *val)
{
u8 quad_shift, angle, quadrant;
u16 reg_addr;
int ret;
switch (chan->channel2) {
case IIO_MOD_X:
reg_addr = MMA9551_TILT_YZ_ANG_REG;
quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT;
break;
case IIO_MOD_Y:
reg_addr = MMA9551_TILT_XZ_ANG_REG;
quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT;
break;
case IIO_MOD_Z:
reg_addr = MMA9551_TILT_XY_ANG_REG;
quad_shift = MMA9551_TILT_XY_QUAD_SHIFT;
break;
default:
return -EINVAL;
}
ret = mma9551_set_power_state(client, true);
if (ret < 0)
return ret;
ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
reg_addr, &angle);
if (ret < 0)
goto out_poweroff;
ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
MMA9551_TILT_QUAD_REG, &quadrant);
if (ret < 0)
goto out_poweroff;
angle &= ~MMA9551_TILT_ANGFLG;
quadrant = (quadrant >> quad_shift) & 0x03;
if (quadrant == 1 || quadrant == 3)
*val = 90 * (quadrant + 1) - angle;
else
*val = angle + 90 * quadrant;
ret = IIO_VAL_INT;
out_poweroff:
mma9551_set_power_state(client, false);
return ret;
}
static int mma9551_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
struct mma9551_data *data = iio_priv(indio_dev);
int ret;
switch (mask) {
case IIO_CHAN_INFO_PROCESSED:
switch (chan->type) {
case IIO_INCLI:
mutex_lock(&data->mutex);
ret = mma9551_read_incli_chan(data->client, chan, val);
mutex_unlock(&data->mutex);
return ret;
default:
return -EINVAL;
}
case IIO_CHAN_INFO_RAW:
switch (chan->type) {
case IIO_ACCEL:
mutex_lock(&data->mutex);
ret = mma9551_read_accel_chan(data->client,
chan, val, val2);
mutex_unlock(&data->mutex);
return ret;
default:
return -EINVAL;
}
case IIO_CHAN_INFO_SCALE:
switch (chan->type) {
case IIO_ACCEL:
return mma9551_read_accel_scale(val, val2);
default:
return -EINVAL;
}
default:
return -EINVAL;
}
}
static int mma9551_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 mma9551_data *data = iio_priv(indio_dev);
switch (chan->type) {
case IIO_INCLI:
/* IIO counts axes from 1, because IIO_NO_MOD is 0. */
return data->event_enabled[chan->channel2 - 1];
default:
return -EINVAL;
}
}
static int mma9551_config_incli_event(struct iio_dev *indio_dev,
enum iio_modifier axis,
int state)
{
struct mma9551_data *data = iio_priv(indio_dev);
enum mma9551_tilt_axis mma_axis;
int ret;
/* IIO counts axes from 1, because IIO_NO_MOD is 0. */
mma_axis = axis - 1;
if (data->event_enabled[mma_axis] == state)
return 0;
if (state == 0) {
ret = mma9551_gpio_config(data->client,
(enum mma9551_gpio_pin)mma_axis,
MMA9551_APPID_NONE, 0, 0);
if (ret < 0)
return ret;
ret = mma9551_set_power_state(data->client, false);
if (ret < 0)
return ret;
} else {
int bitnum;
/* Bit 7 of each angle register holds the angle flag. */
switch (axis) {
case IIO_MOD_X:
bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG;
break;
case IIO_MOD_Y:
bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG;
break;
case IIO_MOD_Z:
bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG;
break;
default:
return -EINVAL;
}
ret = mma9551_set_power_state(data->client, true);
if (ret < 0)
return ret;
ret = mma9551_gpio_config(data->client,
(enum mma9551_gpio_pin)mma_axis,
MMA9551_APPID_TILT, bitnum, 0);
if (ret < 0) {
mma9551_set_power_state(data->client, false);
return ret;
}
}
data->event_enabled[mma_axis] = state;
return ret;
}
static int mma9551_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 mma9551_data *data = iio_priv(indio_dev);
int ret;
switch (chan->type) {
case IIO_INCLI:
mutex_lock(&data->mutex);
ret = mma9551_config_incli_event(indio_dev,
chan->channel2, state);
mutex_unlock(&data->mutex);
return ret;
default:
return -EINVAL;
}
}
static int mma9551_write_event_value(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 mma9551_data *data = iio_priv(indio_dev);
int ret;
switch (chan->type) {
case IIO_INCLI:
if (val2 != 0 || val < 1 || val > 10)
return -EINVAL;
mutex_lock(&data->mutex);
ret = mma9551_update_config_bits(data->client,
MMA9551_APPID_TILT,
MMA9551_TILT_CFG_REG,
MMA9551_TILT_ANG_THRESH_MASK,
val);
mutex_unlock(&data->mutex);
return ret;
default:
return -EINVAL;
}
}
static int mma9551_read_event_value(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 mma9551_data *data = iio_priv(indio_dev);
int ret;
u8 tmp;
switch (chan->type) {
case IIO_INCLI:
mutex_lock(&data->mutex);
ret = mma9551_read_config_byte(data->client,
MMA9551_APPID_TILT,
MMA9551_TILT_CFG_REG, &tmp);
mutex_unlock(&data->mutex);
if (ret < 0)
return ret;
*val = tmp & MMA9551_TILT_ANG_THRESH_MASK;
*val2 = 0;
return IIO_VAL_INT;
default:
return -EINVAL;
}
}
static const struct iio_event_spec mma9551_incli_event = {
.type = IIO_EV_TYPE_ROC,
.dir = IIO_EV_DIR_RISING,
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
.mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
};
#define MMA9551_INCLI_CHANNEL(axis) { \
.type = IIO_INCLI, \
.modified = 1, \
.channel2 = axis, \
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
.event_spec = &mma9551_incli_event, \
.num_event_specs = 1, \
}
static const struct iio_chan_spec mma9551_channels[] = {
MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
MMA9551_INCLI_CHANNEL(IIO_MOD_X),
MMA9551_INCLI_CHANNEL(IIO_MOD_Y),
MMA9551_INCLI_CHANNEL(IIO_MOD_Z),
};
static const struct iio_info mma9551_info = {
.read_raw = mma9551_read_raw,
.read_event_config = mma9551_read_event_config,
.write_event_config = mma9551_write_event_config,
.read_event_value = mma9551_read_event_value,
.write_event_value = mma9551_write_event_value,
};
static irqreturn_t mma9551_event_handler(int irq, void *private)
{
struct iio_dev *indio_dev = private;
struct mma9551_data *data = iio_priv(indio_dev);
int i, ret, mma_axis = -1;
u16 reg;
u8 val;
mutex_lock(&data->mutex);
for (i = 0; i < 3; i++)
if (irq == data->irqs[i]) {
mma_axis = i;
break;
}
if (mma_axis == -1) {
/* IRQ was triggered on 4th line, which we don't use. */
dev_warn(&data->client->dev,
"irq triggered on unused line %d\n", data->irqs[3]);
goto out;
}
switch (mma_axis) {
case mma9551_x:
reg = MMA9551_TILT_YZ_ANG_REG;
break;
case mma9551_y:
reg = MMA9551_TILT_XZ_ANG_REG;
break;
case mma9551_z:
reg = MMA9551_TILT_XY_ANG_REG;
break;
}
/*
* Read the angle even though we don't use it, otherwise we
* won't get any further interrupts.
*/
ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT,
reg, &val);
if (ret < 0) {
dev_err(&data->client->dev,
"error %d reading tilt register in IRQ\n", ret);
goto out;
}
iio_push_event(indio_dev,
IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1),
IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING),
iio_get_time_ns(indio_dev));
out:
mutex_unlock(&data->mutex);
return IRQ_HANDLED;
}
static int mma9551_init(struct mma9551_data *data)
{
int ret;
ret = mma9551_read_version(data->client);
if (ret)
return ret;
return mma9551_set_device_state(data->client, true);
}
static int mma9551_gpio_probe(struct iio_dev *indio_dev)
{
struct gpio_desc *gpio;
int i, ret;
struct mma9551_data *data = iio_priv(indio_dev);
struct device *dev = &data->client->dev;
for (i = 0; i < MMA9551_GPIO_COUNT; i++) {
gpio = devm_gpiod_get_index(dev, NULL, i, GPIOD_IN);
if (IS_ERR(gpio)) {
dev_err(dev, "acpi gpio get index failed\n");
return PTR_ERR(gpio);
}
ret = gpiod_to_irq(gpio);
if (ret < 0)
return ret;
data->irqs[i] = ret;
ret = devm_request_threaded_irq(dev, data->irqs[i],
NULL, mma9551_event_handler,
IRQF_TRIGGER_RISING | IRQF_ONESHOT,
MMA9551_IRQ_NAME, indio_dev);
if (ret < 0) {
dev_err(dev, "request irq %d failed\n", data->irqs[i]);
return ret;
}
dev_dbg(dev, "gpio resource, no:%d irq:%d\n",
desc_to_gpio(gpio), data->irqs[i]);
}
return 0;
}
static const char *mma9551_match_acpi_device(struct device *dev)
{
const struct acpi_device_id *id;
id = acpi_match_device(dev->driver->acpi_match_table, dev);
if (!id)
return NULL;
return dev_name(dev);
}
static int mma9551_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct mma9551_data *data;
struct iio_dev *indio_dev;
const char *name = NULL;
int ret;
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
if (!indio_dev)
return -ENOMEM;
data = iio_priv(indio_dev);
i2c_set_clientdata(client, indio_dev);
data->client = client;
if (id)
name = id->name;
else if (ACPI_HANDLE(&client->dev))
name = mma9551_match_acpi_device(&client->dev);
ret = mma9551_init(data);
if (ret < 0)
return ret;
mutex_init(&data->mutex);
indio_dev->channels = mma9551_channels;
indio_dev->num_channels = ARRAY_SIZE(mma9551_channels);
indio_dev->name = name;
indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->info = &mma9551_info;
ret = mma9551_gpio_probe(indio_dev);
if (ret < 0)
goto out_poweroff;
ret = pm_runtime_set_active(&client->dev);
if (ret < 0)
goto out_poweroff;
pm_runtime_enable(&client->dev);
pm_runtime_set_autosuspend_delay(&client->dev,
MMA9551_AUTO_SUSPEND_DELAY_MS);
pm_runtime_use_autosuspend(&client->dev);
ret = iio_device_register(indio_dev);
if (ret < 0) {
dev_err(&client->dev, "unable to register iio device\n");
goto err_pm_cleanup;
}
return 0;
err_pm_cleanup:
pm_runtime_dont_use_autosuspend(&client->dev);
pm_runtime_disable(&client->dev);
out_poweroff:
mma9551_set_device_state(client, false);
return ret;
}
static int mma9551_remove(struct i2c_client *client)
{
struct iio_dev *indio_dev = i2c_get_clientdata(client);
struct mma9551_data *data = iio_priv(indio_dev);
iio_device_unregister(indio_dev);
pm_runtime_disable(&client->dev);
pm_runtime_set_suspended(&client->dev);
mutex_lock(&data->mutex);
mma9551_set_device_state(data->client, false);
mutex_unlock(&data->mutex);
return 0;
}
static int mma9551_runtime_suspend(struct device *dev)
{
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
struct mma9551_data *data = iio_priv(indio_dev);
int ret;
mutex_lock(&data->mutex);
ret = mma9551_set_device_state(data->client, false);
mutex_unlock(&data->mutex);
if (ret < 0) {
dev_err(&data->client->dev, "powering off device failed\n");
return -EAGAIN;
}
return 0;
}
static int mma9551_runtime_resume(struct device *dev)
{
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
struct mma9551_data *data = iio_priv(indio_dev);
int ret;
ret = mma9551_set_device_state(data->client, true);
if (ret < 0)
return ret;
mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE);
return 0;
}
static int mma9551_suspend(struct device *dev)
{
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
struct mma9551_data *data = iio_priv(indio_dev);
int ret;
mutex_lock(&data->mutex);
ret = mma9551_set_device_state(data->client, false);
mutex_unlock(&data->mutex);
return ret;
}
static int mma9551_resume(struct device *dev)
{
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
struct mma9551_data *data = iio_priv(indio_dev);
int ret;
mutex_lock(&data->mutex);
ret = mma9551_set_device_state(data->client, true);
mutex_unlock(&data->mutex);
return ret;
}
static const struct dev_pm_ops mma9551_pm_ops = {
SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume)
RUNTIME_PM_OPS(mma9551_runtime_suspend, mma9551_runtime_resume, NULL)
};
static const struct acpi_device_id mma9551_acpi_match[] = {
{"MMA9551", 0},
{},
};
MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match);
static const struct i2c_device_id mma9551_id[] = {
{"mma9551", 0},
{}
};
MODULE_DEVICE_TABLE(i2c, mma9551_id);
static struct i2c_driver mma9551_driver = {
.driver = {
.name = MMA9551_DRV_NAME,
.acpi_match_table = ACPI_PTR(mma9551_acpi_match),
.pm = pm_ptr(&mma9551_pm_ops),
},
.probe = mma9551_probe,
.remove = mma9551_remove,
.id_table = mma9551_id,
};
module_i2c_driver(mma9551_driver);
MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");
MODULE_IMPORT_NS(IIO_MMA9551);