xref: /linux/drivers/iio/accel/msa311.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
11ca2cfbcSDmitry Rokosov // SPDX-License-Identifier: GPL-2.0
21ca2cfbcSDmitry Rokosov /*
31ca2cfbcSDmitry Rokosov  * MEMSensing digital 3-Axis accelerometer
41ca2cfbcSDmitry Rokosov  *
51ca2cfbcSDmitry Rokosov  * MSA311 is a tri-axial, low-g accelerometer with I2C digital output for
61ca2cfbcSDmitry Rokosov  * sensitivity consumer applications. It has dynamic user-selectable full
71ca2cfbcSDmitry Rokosov  * scales range of +-2g/+-4g/+-8g/+-16g and allows acceleration measurements
81ca2cfbcSDmitry Rokosov  * with output data rates from 1Hz to 1000Hz.
91ca2cfbcSDmitry Rokosov  *
101ca2cfbcSDmitry Rokosov  * MSA311 is available in an ultra small (2mm x 2mm, height 0.95mm) LGA package
111ca2cfbcSDmitry Rokosov  * and is guaranteed to operate over -40C to +85C.
121ca2cfbcSDmitry Rokosov  *
131ca2cfbcSDmitry Rokosov  * This driver supports following MSA311 features:
141ca2cfbcSDmitry Rokosov  *     - IIO interface
151ca2cfbcSDmitry Rokosov  *     - Different power modes: NORMAL, SUSPEND
161ca2cfbcSDmitry Rokosov  *     - ODR (Output Data Rate) selection
171ca2cfbcSDmitry Rokosov  *     - Scale selection
181ca2cfbcSDmitry Rokosov  *     - IIO triggered buffer
191ca2cfbcSDmitry Rokosov  *     - NEW_DATA interrupt + trigger
201ca2cfbcSDmitry Rokosov  *
211ca2cfbcSDmitry Rokosov  * Below features to be done:
221ca2cfbcSDmitry Rokosov  *     - Motion Events: ACTIVE, TAP, ORIENT, FREEFALL
231ca2cfbcSDmitry Rokosov  *     - Low Power mode
241ca2cfbcSDmitry Rokosov  *
251ca2cfbcSDmitry Rokosov  * Copyright (c) 2022, SberDevices. All Rights Reserved.
261ca2cfbcSDmitry Rokosov  *
271ca2cfbcSDmitry Rokosov  * Author: Dmitry Rokosov <ddrokosov@sberdevices.ru>
281ca2cfbcSDmitry Rokosov  */
291ca2cfbcSDmitry Rokosov 
301ca2cfbcSDmitry Rokosov #include <linux/i2c.h>
311ca2cfbcSDmitry Rokosov #include <linux/mod_devicetable.h>
321ca2cfbcSDmitry Rokosov #include <linux/module.h>
331ca2cfbcSDmitry Rokosov #include <linux/pm.h>
341ca2cfbcSDmitry Rokosov #include <linux/pm_runtime.h>
351ca2cfbcSDmitry Rokosov #include <linux/regmap.h>
36de39695dSAndy Shevchenko #include <linux/string_choices.h>
371ca2cfbcSDmitry Rokosov #include <linux/units.h>
381ca2cfbcSDmitry Rokosov 
391ca2cfbcSDmitry Rokosov #include <linux/iio/buffer.h>
401ca2cfbcSDmitry Rokosov #include <linux/iio/iio.h>
411ca2cfbcSDmitry Rokosov #include <linux/iio/sysfs.h>
421ca2cfbcSDmitry Rokosov #include <linux/iio/trigger.h>
431ca2cfbcSDmitry Rokosov #include <linux/iio/trigger_consumer.h>
441ca2cfbcSDmitry Rokosov #include <linux/iio/triggered_buffer.h>
451ca2cfbcSDmitry Rokosov 
461ca2cfbcSDmitry Rokosov #define MSA311_SOFT_RESET_REG     0x00
471ca2cfbcSDmitry Rokosov #define MSA311_PARTID_REG         0x01
481ca2cfbcSDmitry Rokosov #define MSA311_ACC_X_REG          0x02
491ca2cfbcSDmitry Rokosov #define MSA311_ACC_Y_REG          0x04
501ca2cfbcSDmitry Rokosov #define MSA311_ACC_Z_REG          0x06
511ca2cfbcSDmitry Rokosov #define MSA311_MOTION_INT_REG     0x09
521ca2cfbcSDmitry Rokosov #define MSA311_DATA_INT_REG       0x0A
531ca2cfbcSDmitry Rokosov #define MSA311_TAP_ACTIVE_STS_REG 0x0B
541ca2cfbcSDmitry Rokosov #define MSA311_ORIENT_STS_REG     0x0C
551ca2cfbcSDmitry Rokosov #define MSA311_RANGE_REG          0x0F
561ca2cfbcSDmitry Rokosov #define MSA311_ODR_REG            0x10
571ca2cfbcSDmitry Rokosov #define MSA311_PWR_MODE_REG       0x11
581ca2cfbcSDmitry Rokosov #define MSA311_SWAP_POLARITY_REG  0x12
591ca2cfbcSDmitry Rokosov #define MSA311_INT_SET_0_REG      0x16
601ca2cfbcSDmitry Rokosov #define MSA311_INT_SET_1_REG      0x17
611ca2cfbcSDmitry Rokosov #define MSA311_INT_MAP_0_REG      0x19
621ca2cfbcSDmitry Rokosov #define MSA311_INT_MAP_1_REG      0x1A
631ca2cfbcSDmitry Rokosov #define MSA311_INT_CONFIG_REG     0x20
641ca2cfbcSDmitry Rokosov #define MSA311_INT_LATCH_REG      0x21
651ca2cfbcSDmitry Rokosov #define MSA311_FREEFALL_DUR_REG   0x22
661ca2cfbcSDmitry Rokosov #define MSA311_FREEFALL_TH_REG    0x23
671ca2cfbcSDmitry Rokosov #define MSA311_FREEFALL_HY_REG    0x24
681ca2cfbcSDmitry Rokosov #define MSA311_ACTIVE_DUR_REG     0x27
691ca2cfbcSDmitry Rokosov #define MSA311_ACTIVE_TH_REG      0x28
701ca2cfbcSDmitry Rokosov #define MSA311_TAP_DUR_REG        0x2A
711ca2cfbcSDmitry Rokosov #define MSA311_TAP_TH_REG         0x2B
721ca2cfbcSDmitry Rokosov #define MSA311_ORIENT_HY_REG      0x2C
731ca2cfbcSDmitry Rokosov #define MSA311_Z_BLOCK_REG        0x2D
741ca2cfbcSDmitry Rokosov #define MSA311_OFFSET_X_REG       0x38
751ca2cfbcSDmitry Rokosov #define MSA311_OFFSET_Y_REG       0x39
761ca2cfbcSDmitry Rokosov #define MSA311_OFFSET_Z_REG       0x3A
771ca2cfbcSDmitry Rokosov 
781ca2cfbcSDmitry Rokosov enum msa311_fields {
791ca2cfbcSDmitry Rokosov 	/* Soft_Reset */
801ca2cfbcSDmitry Rokosov 	F_SOFT_RESET_I2C, F_SOFT_RESET_SPI,
811ca2cfbcSDmitry Rokosov 	/* Motion_Interrupt */
821ca2cfbcSDmitry Rokosov 	F_ORIENT_INT, F_S_TAP_INT, F_D_TAP_INT, F_ACTIVE_INT, F_FREEFALL_INT,
831ca2cfbcSDmitry Rokosov 	/* Data_Interrupt */
841ca2cfbcSDmitry Rokosov 	F_NEW_DATA_INT,
851ca2cfbcSDmitry Rokosov 	/* Tap_Active_Status */
861ca2cfbcSDmitry Rokosov 	F_TAP_SIGN, F_TAP_FIRST_X, F_TAP_FIRST_Y, F_TAP_FIRST_Z, F_ACTV_SIGN,
871ca2cfbcSDmitry Rokosov 	F_ACTV_FIRST_X, F_ACTV_FIRST_Y, F_ACTV_FIRST_Z,
881ca2cfbcSDmitry Rokosov 	/* Orientation_Status */
891ca2cfbcSDmitry Rokosov 	F_ORIENT_Z, F_ORIENT_X_Y,
901ca2cfbcSDmitry Rokosov 	/* Range */
911ca2cfbcSDmitry Rokosov 	F_FS,
921ca2cfbcSDmitry Rokosov 	/* ODR */
931ca2cfbcSDmitry Rokosov 	F_X_AXIS_DIS, F_Y_AXIS_DIS, F_Z_AXIS_DIS, F_ODR,
941ca2cfbcSDmitry Rokosov 	/* Power Mode/Bandwidth */
951ca2cfbcSDmitry Rokosov 	F_PWR_MODE, F_LOW_POWER_BW,
961ca2cfbcSDmitry Rokosov 	/* Swap_Polarity */
971ca2cfbcSDmitry Rokosov 	F_X_POLARITY, F_Y_POLARITY, F_Z_POLARITY, F_X_Y_SWAP,
981ca2cfbcSDmitry Rokosov 	/* Int_Set_0 */
991ca2cfbcSDmitry Rokosov 	F_ORIENT_INT_EN, F_S_TAP_INT_EN, F_D_TAP_INT_EN, F_ACTIVE_INT_EN_Z,
1001ca2cfbcSDmitry Rokosov 	F_ACTIVE_INT_EN_Y, F_ACTIVE_INT_EN_X,
1011ca2cfbcSDmitry Rokosov 	/* Int_Set_1 */
1021ca2cfbcSDmitry Rokosov 	F_NEW_DATA_INT_EN, F_FREEFALL_INT_EN,
1031ca2cfbcSDmitry Rokosov 	/* Int_Map_0 */
1041ca2cfbcSDmitry Rokosov 	F_INT1_ORIENT, F_INT1_S_TAP, F_INT1_D_TAP, F_INT1_ACTIVE,
1051ca2cfbcSDmitry Rokosov 	F_INT1_FREEFALL,
1061ca2cfbcSDmitry Rokosov 	/* Int_Map_1 */
1071ca2cfbcSDmitry Rokosov 	F_INT1_NEW_DATA,
1081ca2cfbcSDmitry Rokosov 	/* Int_Config */
1091ca2cfbcSDmitry Rokosov 	F_INT1_OD, F_INT1_LVL,
1101ca2cfbcSDmitry Rokosov 	/* Int_Latch */
1111ca2cfbcSDmitry Rokosov 	F_RESET_INT, F_LATCH_INT,
1121ca2cfbcSDmitry Rokosov 	/* Freefall_Hy */
1131ca2cfbcSDmitry Rokosov 	F_FREEFALL_MODE, F_FREEFALL_HY,
1141ca2cfbcSDmitry Rokosov 	/* Active_Dur */
1151ca2cfbcSDmitry Rokosov 	F_ACTIVE_DUR,
1161ca2cfbcSDmitry Rokosov 	/* Tap_Dur */
1171ca2cfbcSDmitry Rokosov 	F_TAP_QUIET, F_TAP_SHOCK, F_TAP_DUR,
1181ca2cfbcSDmitry Rokosov 	/* Tap_Th */
1191ca2cfbcSDmitry Rokosov 	F_TAP_TH,
1201ca2cfbcSDmitry Rokosov 	/* Orient_Hy */
1211ca2cfbcSDmitry Rokosov 	F_ORIENT_HYST, F_ORIENT_BLOCKING, F_ORIENT_MODE,
1221ca2cfbcSDmitry Rokosov 	/* Z_Block */
1231ca2cfbcSDmitry Rokosov 	F_Z_BLOCKING,
1241ca2cfbcSDmitry Rokosov 	/* End of register map */
1251ca2cfbcSDmitry Rokosov 	F_MAX_FIELDS,
1261ca2cfbcSDmitry Rokosov };
1271ca2cfbcSDmitry Rokosov 
1281ca2cfbcSDmitry Rokosov static const struct reg_field msa311_reg_fields[] = {
1291ca2cfbcSDmitry Rokosov 	/* Soft_Reset */
1301ca2cfbcSDmitry Rokosov 	[F_SOFT_RESET_I2C] = REG_FIELD(MSA311_SOFT_RESET_REG, 2, 2),
1311ca2cfbcSDmitry Rokosov 	[F_SOFT_RESET_SPI] = REG_FIELD(MSA311_SOFT_RESET_REG, 5, 5),
1321ca2cfbcSDmitry Rokosov 	/* Motion_Interrupt */
1331ca2cfbcSDmitry Rokosov 	[F_ORIENT_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 6, 6),
1341ca2cfbcSDmitry Rokosov 	[F_S_TAP_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 5, 5),
1351ca2cfbcSDmitry Rokosov 	[F_D_TAP_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 4, 4),
1361ca2cfbcSDmitry Rokosov 	[F_ACTIVE_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 2, 2),
1371ca2cfbcSDmitry Rokosov 	[F_FREEFALL_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 0, 0),
1381ca2cfbcSDmitry Rokosov 	/* Data_Interrupt */
1391ca2cfbcSDmitry Rokosov 	[F_NEW_DATA_INT] = REG_FIELD(MSA311_DATA_INT_REG, 0, 0),
1401ca2cfbcSDmitry Rokosov 	/* Tap_Active_Status */
1411ca2cfbcSDmitry Rokosov 	[F_TAP_SIGN] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 7, 7),
1421ca2cfbcSDmitry Rokosov 	[F_TAP_FIRST_X] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 6, 6),
1431ca2cfbcSDmitry Rokosov 	[F_TAP_FIRST_Y] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 5, 5),
1441ca2cfbcSDmitry Rokosov 	[F_TAP_FIRST_Z] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 4, 4),
1451ca2cfbcSDmitry Rokosov 	[F_ACTV_SIGN] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 3, 3),
1461ca2cfbcSDmitry Rokosov 	[F_ACTV_FIRST_X] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 2, 2),
1471ca2cfbcSDmitry Rokosov 	[F_ACTV_FIRST_Y] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 1, 1),
1481ca2cfbcSDmitry Rokosov 	[F_ACTV_FIRST_Z] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 0, 0),
1491ca2cfbcSDmitry Rokosov 	/* Orientation_Status */
1501ca2cfbcSDmitry Rokosov 	[F_ORIENT_Z] = REG_FIELD(MSA311_ORIENT_STS_REG, 6, 6),
1511ca2cfbcSDmitry Rokosov 	[F_ORIENT_X_Y] = REG_FIELD(MSA311_ORIENT_STS_REG, 4, 5),
1521ca2cfbcSDmitry Rokosov 	/* Range */
1531ca2cfbcSDmitry Rokosov 	[F_FS] = REG_FIELD(MSA311_RANGE_REG, 0, 1),
1541ca2cfbcSDmitry Rokosov 	/* ODR */
1551ca2cfbcSDmitry Rokosov 	[F_X_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 7, 7),
1561ca2cfbcSDmitry Rokosov 	[F_Y_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 6, 6),
1571ca2cfbcSDmitry Rokosov 	[F_Z_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 5, 5),
1581ca2cfbcSDmitry Rokosov 	[F_ODR] = REG_FIELD(MSA311_ODR_REG, 0, 3),
1591ca2cfbcSDmitry Rokosov 	/* Power Mode/Bandwidth */
1601ca2cfbcSDmitry Rokosov 	[F_PWR_MODE] = REG_FIELD(MSA311_PWR_MODE_REG, 6, 7),
1611ca2cfbcSDmitry Rokosov 	[F_LOW_POWER_BW] = REG_FIELD(MSA311_PWR_MODE_REG, 1, 4),
1621ca2cfbcSDmitry Rokosov 	/* Swap_Polarity */
1631ca2cfbcSDmitry Rokosov 	[F_X_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 3, 3),
1641ca2cfbcSDmitry Rokosov 	[F_Y_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 2, 2),
1651ca2cfbcSDmitry Rokosov 	[F_Z_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 1, 1),
1661ca2cfbcSDmitry Rokosov 	[F_X_Y_SWAP] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 0, 0),
1671ca2cfbcSDmitry Rokosov 	/* Int_Set_0 */
1681ca2cfbcSDmitry Rokosov 	[F_ORIENT_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 6, 6),
1691ca2cfbcSDmitry Rokosov 	[F_S_TAP_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 5, 5),
1701ca2cfbcSDmitry Rokosov 	[F_D_TAP_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 4, 4),
1711ca2cfbcSDmitry Rokosov 	[F_ACTIVE_INT_EN_Z] = REG_FIELD(MSA311_INT_SET_0_REG, 2, 2),
1721ca2cfbcSDmitry Rokosov 	[F_ACTIVE_INT_EN_Y] = REG_FIELD(MSA311_INT_SET_0_REG, 1, 1),
1731ca2cfbcSDmitry Rokosov 	[F_ACTIVE_INT_EN_X] = REG_FIELD(MSA311_INT_SET_0_REG, 0, 0),
1741ca2cfbcSDmitry Rokosov 	/* Int_Set_1 */
1751ca2cfbcSDmitry Rokosov 	[F_NEW_DATA_INT_EN] = REG_FIELD(MSA311_INT_SET_1_REG, 4, 4),
1761ca2cfbcSDmitry Rokosov 	[F_FREEFALL_INT_EN] = REG_FIELD(MSA311_INT_SET_1_REG, 3, 3),
1771ca2cfbcSDmitry Rokosov 	/* Int_Map_0 */
1781ca2cfbcSDmitry Rokosov 	[F_INT1_ORIENT] = REG_FIELD(MSA311_INT_MAP_0_REG, 6, 6),
1791ca2cfbcSDmitry Rokosov 	[F_INT1_S_TAP] = REG_FIELD(MSA311_INT_MAP_0_REG, 5, 5),
1801ca2cfbcSDmitry Rokosov 	[F_INT1_D_TAP] = REG_FIELD(MSA311_INT_MAP_0_REG, 4, 4),
1811ca2cfbcSDmitry Rokosov 	[F_INT1_ACTIVE] = REG_FIELD(MSA311_INT_MAP_0_REG, 2, 2),
1821ca2cfbcSDmitry Rokosov 	[F_INT1_FREEFALL] = REG_FIELD(MSA311_INT_MAP_0_REG, 0, 0),
1831ca2cfbcSDmitry Rokosov 	/* Int_Map_1 */
1841ca2cfbcSDmitry Rokosov 	[F_INT1_NEW_DATA] = REG_FIELD(MSA311_INT_MAP_1_REG, 0, 0),
1851ca2cfbcSDmitry Rokosov 	/* Int_Config */
1861ca2cfbcSDmitry Rokosov 	[F_INT1_OD] = REG_FIELD(MSA311_INT_CONFIG_REG, 1, 1),
1871ca2cfbcSDmitry Rokosov 	[F_INT1_LVL] = REG_FIELD(MSA311_INT_CONFIG_REG, 0, 0),
1881ca2cfbcSDmitry Rokosov 	/* Int_Latch */
1891ca2cfbcSDmitry Rokosov 	[F_RESET_INT] = REG_FIELD(MSA311_INT_LATCH_REG, 7, 7),
1901ca2cfbcSDmitry Rokosov 	[F_LATCH_INT] = REG_FIELD(MSA311_INT_LATCH_REG, 0, 3),
1911ca2cfbcSDmitry Rokosov 	/* Freefall_Hy */
1921ca2cfbcSDmitry Rokosov 	[F_FREEFALL_MODE] = REG_FIELD(MSA311_FREEFALL_HY_REG, 2, 2),
1931ca2cfbcSDmitry Rokosov 	[F_FREEFALL_HY] = REG_FIELD(MSA311_FREEFALL_HY_REG, 0, 1),
1941ca2cfbcSDmitry Rokosov 	/* Active_Dur */
1951ca2cfbcSDmitry Rokosov 	[F_ACTIVE_DUR] = REG_FIELD(MSA311_ACTIVE_DUR_REG, 0, 1),
1961ca2cfbcSDmitry Rokosov 	/* Tap_Dur */
1971ca2cfbcSDmitry Rokosov 	[F_TAP_QUIET] = REG_FIELD(MSA311_TAP_DUR_REG, 7, 7),
1981ca2cfbcSDmitry Rokosov 	[F_TAP_SHOCK] = REG_FIELD(MSA311_TAP_DUR_REG, 6, 6),
1991ca2cfbcSDmitry Rokosov 	[F_TAP_DUR] = REG_FIELD(MSA311_TAP_DUR_REG, 0, 2),
2001ca2cfbcSDmitry Rokosov 	/* Tap_Th */
2011ca2cfbcSDmitry Rokosov 	[F_TAP_TH] = REG_FIELD(MSA311_TAP_TH_REG, 0, 4),
2021ca2cfbcSDmitry Rokosov 	/* Orient_Hy */
2031ca2cfbcSDmitry Rokosov 	[F_ORIENT_HYST] = REG_FIELD(MSA311_ORIENT_HY_REG, 4, 6),
2041ca2cfbcSDmitry Rokosov 	[F_ORIENT_BLOCKING] = REG_FIELD(MSA311_ORIENT_HY_REG, 2, 3),
2051ca2cfbcSDmitry Rokosov 	[F_ORIENT_MODE] = REG_FIELD(MSA311_ORIENT_HY_REG, 0, 1),
2061ca2cfbcSDmitry Rokosov 	/* Z_Block */
2071ca2cfbcSDmitry Rokosov 	[F_Z_BLOCKING] = REG_FIELD(MSA311_Z_BLOCK_REG, 0, 3),
2081ca2cfbcSDmitry Rokosov };
2091ca2cfbcSDmitry Rokosov 
2101ca2cfbcSDmitry Rokosov #define MSA311_WHO_AM_I 0x13
2111ca2cfbcSDmitry Rokosov 
2121ca2cfbcSDmitry Rokosov /*
2131ca2cfbcSDmitry Rokosov  * Possible Full Scale ranges
2141ca2cfbcSDmitry Rokosov  *
2151ca2cfbcSDmitry Rokosov  * Axis data is 12-bit signed value, so
2161ca2cfbcSDmitry Rokosov  *
2171ca2cfbcSDmitry Rokosov  * fs0 = (2 + 2) * 9.81 / (2^11) = 0.009580
2181ca2cfbcSDmitry Rokosov  * fs1 = (4 + 4) * 9.81 / (2^11) = 0.019160
2191ca2cfbcSDmitry Rokosov  * fs2 = (8 + 8) * 9.81 / (2^11) = 0.038320
2201ca2cfbcSDmitry Rokosov  * fs3 = (16 + 16) * 9.81 / (2^11) = 0.076641
2211ca2cfbcSDmitry Rokosov  */
2221ca2cfbcSDmitry Rokosov enum {
2231ca2cfbcSDmitry Rokosov 	MSA311_FS_2G,
2241ca2cfbcSDmitry Rokosov 	MSA311_FS_4G,
2251ca2cfbcSDmitry Rokosov 	MSA311_FS_8G,
2261ca2cfbcSDmitry Rokosov 	MSA311_FS_16G,
2271ca2cfbcSDmitry Rokosov };
2281ca2cfbcSDmitry Rokosov 
2291ca2cfbcSDmitry Rokosov struct iio_decimal_fract {
2301ca2cfbcSDmitry Rokosov 	int integral;
2311ca2cfbcSDmitry Rokosov 	int microfract;
2321ca2cfbcSDmitry Rokosov };
2331ca2cfbcSDmitry Rokosov 
2341ca2cfbcSDmitry Rokosov static const struct iio_decimal_fract msa311_fs_table[] = {
2351ca2cfbcSDmitry Rokosov 	{0, 9580}, {0, 19160}, {0, 38320}, {0, 76641},
2361ca2cfbcSDmitry Rokosov };
2371ca2cfbcSDmitry Rokosov 
2381ca2cfbcSDmitry Rokosov /* Possible Output Data Rate values */
2391ca2cfbcSDmitry Rokosov enum {
2401ca2cfbcSDmitry Rokosov 	MSA311_ODR_1_HZ,
2411ca2cfbcSDmitry Rokosov 	MSA311_ODR_1_95_HZ,
2421ca2cfbcSDmitry Rokosov 	MSA311_ODR_3_9_HZ,
2431ca2cfbcSDmitry Rokosov 	MSA311_ODR_7_81_HZ,
2441ca2cfbcSDmitry Rokosov 	MSA311_ODR_15_63_HZ,
2451ca2cfbcSDmitry Rokosov 	MSA311_ODR_31_25_HZ,
2461ca2cfbcSDmitry Rokosov 	MSA311_ODR_62_5_HZ,
2471ca2cfbcSDmitry Rokosov 	MSA311_ODR_125_HZ,
2481ca2cfbcSDmitry Rokosov 	MSA311_ODR_250_HZ,
2491ca2cfbcSDmitry Rokosov 	MSA311_ODR_500_HZ,
2501ca2cfbcSDmitry Rokosov 	MSA311_ODR_1000_HZ,
2511ca2cfbcSDmitry Rokosov };
2521ca2cfbcSDmitry Rokosov 
2531ca2cfbcSDmitry Rokosov static const struct iio_decimal_fract msa311_odr_table[] = {
2541ca2cfbcSDmitry Rokosov 	{1, 0}, {1, 950000}, {3, 900000}, {7, 810000}, {15, 630000},
2551ca2cfbcSDmitry Rokosov 	{31, 250000}, {62, 500000}, {125, 0}, {250, 0}, {500, 0}, {1000, 0},
2561ca2cfbcSDmitry Rokosov };
2571ca2cfbcSDmitry Rokosov 
2581ca2cfbcSDmitry Rokosov /* All supported power modes */
2591ca2cfbcSDmitry Rokosov #define MSA311_PWR_MODE_NORMAL  0b00
2601ca2cfbcSDmitry Rokosov #define MSA311_PWR_MODE_LOW     0b01
2611ca2cfbcSDmitry Rokosov #define MSA311_PWR_MODE_UNKNOWN 0b10
2621ca2cfbcSDmitry Rokosov #define MSA311_PWR_MODE_SUSPEND 0b11
2631ca2cfbcSDmitry Rokosov static const char * const msa311_pwr_modes[] = {
2641ca2cfbcSDmitry Rokosov 	[MSA311_PWR_MODE_NORMAL] = "normal",
2651ca2cfbcSDmitry Rokosov 	[MSA311_PWR_MODE_LOW] = "low",
2661ca2cfbcSDmitry Rokosov 	[MSA311_PWR_MODE_UNKNOWN] = "unknown",
2671ca2cfbcSDmitry Rokosov 	[MSA311_PWR_MODE_SUSPEND] = "suspend",
2681ca2cfbcSDmitry Rokosov };
2691ca2cfbcSDmitry Rokosov 
2701ca2cfbcSDmitry Rokosov /* Autosuspend delay */
2711ca2cfbcSDmitry Rokosov #define MSA311_PWR_SLEEP_DELAY_MS 2000
2721ca2cfbcSDmitry Rokosov 
2731ca2cfbcSDmitry Rokosov /* Possible INT1 types and levels */
2741ca2cfbcSDmitry Rokosov enum {
2751ca2cfbcSDmitry Rokosov 	MSA311_INT1_OD_PUSH_PULL,
2761ca2cfbcSDmitry Rokosov 	MSA311_INT1_OD_OPEN_DRAIN,
2771ca2cfbcSDmitry Rokosov };
2781ca2cfbcSDmitry Rokosov 
2791ca2cfbcSDmitry Rokosov enum {
2801ca2cfbcSDmitry Rokosov 	MSA311_INT1_LVL_LOW,
2811ca2cfbcSDmitry Rokosov 	MSA311_INT1_LVL_HIGH,
2821ca2cfbcSDmitry Rokosov };
2831ca2cfbcSDmitry Rokosov 
2841ca2cfbcSDmitry Rokosov /* Latch INT modes */
2851ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_NOT_LATCHED 0b0000
2861ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_250MS       0b0001
2871ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_500MS       0b0010
2881ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_1S          0b0011
2891ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_2S          0b0100
2901ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_4S          0b0101
2911ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_8S          0b0110
2921ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_1MS         0b1010
2931ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_2MS         0b1011
2941ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_25MS        0b1100
2951ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_50MS        0b1101
2961ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_100MS       0b1110
2971ca2cfbcSDmitry Rokosov #define MSA311_LATCH_INT_LATCHED     0b0111
2981ca2cfbcSDmitry Rokosov 
2991ca2cfbcSDmitry Rokosov static const struct regmap_range msa311_readonly_registers[] = {
3001ca2cfbcSDmitry Rokosov 	regmap_reg_range(MSA311_PARTID_REG, MSA311_ORIENT_STS_REG),
3011ca2cfbcSDmitry Rokosov };
3021ca2cfbcSDmitry Rokosov 
3031ca2cfbcSDmitry Rokosov static const struct regmap_access_table msa311_writeable_table = {
3041ca2cfbcSDmitry Rokosov 	.no_ranges = msa311_readonly_registers,
3051ca2cfbcSDmitry Rokosov 	.n_no_ranges = ARRAY_SIZE(msa311_readonly_registers),
3061ca2cfbcSDmitry Rokosov };
3071ca2cfbcSDmitry Rokosov 
3081ca2cfbcSDmitry Rokosov static const struct regmap_range msa311_writeonly_registers[] = {
3091ca2cfbcSDmitry Rokosov 	regmap_reg_range(MSA311_SOFT_RESET_REG, MSA311_SOFT_RESET_REG),
3101ca2cfbcSDmitry Rokosov };
3111ca2cfbcSDmitry Rokosov 
3121ca2cfbcSDmitry Rokosov static const struct regmap_access_table msa311_readable_table = {
3131ca2cfbcSDmitry Rokosov 	.no_ranges = msa311_writeonly_registers,
3141ca2cfbcSDmitry Rokosov 	.n_no_ranges = ARRAY_SIZE(msa311_writeonly_registers),
3151ca2cfbcSDmitry Rokosov };
3161ca2cfbcSDmitry Rokosov 
3171ca2cfbcSDmitry Rokosov static const struct regmap_range msa311_volatile_registers[] = {
3181ca2cfbcSDmitry Rokosov 	regmap_reg_range(MSA311_ACC_X_REG, MSA311_ORIENT_STS_REG),
3191ca2cfbcSDmitry Rokosov };
3201ca2cfbcSDmitry Rokosov 
3211ca2cfbcSDmitry Rokosov static const struct regmap_access_table msa311_volatile_table = {
3221ca2cfbcSDmitry Rokosov 	.yes_ranges = msa311_volatile_registers,
3231ca2cfbcSDmitry Rokosov 	.n_yes_ranges = ARRAY_SIZE(msa311_volatile_registers),
3241ca2cfbcSDmitry Rokosov };
3251ca2cfbcSDmitry Rokosov 
3261ca2cfbcSDmitry Rokosov static const struct regmap_config msa311_regmap_config = {
3271ca2cfbcSDmitry Rokosov 	.name = "msa311",
3281ca2cfbcSDmitry Rokosov 	.reg_bits = 8,
3291ca2cfbcSDmitry Rokosov 	.val_bits = 8,
3301ca2cfbcSDmitry Rokosov 	.max_register = MSA311_OFFSET_Z_REG,
3311ca2cfbcSDmitry Rokosov 	.wr_table = &msa311_writeable_table,
3321ca2cfbcSDmitry Rokosov 	.rd_table = &msa311_readable_table,
3331ca2cfbcSDmitry Rokosov 	.volatile_table = &msa311_volatile_table,
3341ca2cfbcSDmitry Rokosov 	.cache_type = REGCACHE_RBTREE,
3351ca2cfbcSDmitry Rokosov };
3361ca2cfbcSDmitry Rokosov 
3371ca2cfbcSDmitry Rokosov #define MSA311_GENMASK(field) ({                \
3381ca2cfbcSDmitry Rokosov 	typeof(&(msa311_reg_fields)[0]) _field; \
3391ca2cfbcSDmitry Rokosov 	_field = &msa311_reg_fields[(field)];   \
3401ca2cfbcSDmitry Rokosov 	GENMASK(_field->msb, _field->lsb);      \
3411ca2cfbcSDmitry Rokosov })
3421ca2cfbcSDmitry Rokosov 
3431ca2cfbcSDmitry Rokosov /**
3441ca2cfbcSDmitry Rokosov  * struct msa311_priv - MSA311 internal private state
3451ca2cfbcSDmitry Rokosov  * @regs: Underlying I2C bus adapter used to abstract slave
3461ca2cfbcSDmitry Rokosov  *        register accesses
3471ca2cfbcSDmitry Rokosov  * @fields: Abstract objects for each registers fields access
3481ca2cfbcSDmitry Rokosov  * @dev: Device handler associated with appropriate bus client
3491ca2cfbcSDmitry Rokosov  * @lock: Protects msa311 device state between setup and data access routines
3501ca2cfbcSDmitry Rokosov  *        (power transitions, samp_freq/scale tune, retrieving axes data, etc)
3511ca2cfbcSDmitry Rokosov  * @chip_name: Chip name in the format "msa311-%02x" % partid
3521ca2cfbcSDmitry Rokosov  * @new_data_trig: Optional NEW_DATA interrupt driven trigger used
3531ca2cfbcSDmitry Rokosov  *                 to notify external consumers a new sample is ready
3541ca2cfbcSDmitry Rokosov  */
3551ca2cfbcSDmitry Rokosov struct msa311_priv {
3561ca2cfbcSDmitry Rokosov 	struct regmap *regs;
3571ca2cfbcSDmitry Rokosov 	struct regmap_field *fields[F_MAX_FIELDS];
3581ca2cfbcSDmitry Rokosov 
3591ca2cfbcSDmitry Rokosov 	struct device *dev;
3601ca2cfbcSDmitry Rokosov 	struct mutex lock;
3611ca2cfbcSDmitry Rokosov 	char *chip_name;
3621ca2cfbcSDmitry Rokosov 
3631ca2cfbcSDmitry Rokosov 	struct iio_trigger *new_data_trig;
3641ca2cfbcSDmitry Rokosov };
3651ca2cfbcSDmitry Rokosov 
3661ca2cfbcSDmitry Rokosov enum msa311_si {
3671ca2cfbcSDmitry Rokosov 	MSA311_SI_X,
3681ca2cfbcSDmitry Rokosov 	MSA311_SI_Y,
3691ca2cfbcSDmitry Rokosov 	MSA311_SI_Z,
3701ca2cfbcSDmitry Rokosov 	MSA311_SI_TIMESTAMP,
3711ca2cfbcSDmitry Rokosov };
3721ca2cfbcSDmitry Rokosov 
3731ca2cfbcSDmitry Rokosov #define MSA311_ACCEL_CHANNEL(axis) {                                        \
3741ca2cfbcSDmitry Rokosov 	.type = IIO_ACCEL,                                                  \
3751ca2cfbcSDmitry Rokosov 	.modified = 1,                                                      \
3761ca2cfbcSDmitry Rokosov 	.channel2 = IIO_MOD_##axis,                                         \
3771ca2cfbcSDmitry Rokosov 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                       \
3781ca2cfbcSDmitry Rokosov 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |              \
3791ca2cfbcSDmitry Rokosov 				    BIT(IIO_CHAN_INFO_SAMP_FREQ),           \
3801ca2cfbcSDmitry Rokosov 	.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) |    \
3811ca2cfbcSDmitry Rokosov 					      BIT(IIO_CHAN_INFO_SAMP_FREQ), \
3821ca2cfbcSDmitry Rokosov 	.scan_index = MSA311_SI_##axis,                                     \
3831ca2cfbcSDmitry Rokosov 	.scan_type = {                                                      \
3841ca2cfbcSDmitry Rokosov 		.sign = 's',                                                \
3851ca2cfbcSDmitry Rokosov 		.realbits = 12,                                             \
3861ca2cfbcSDmitry Rokosov 		.storagebits = 16,                                          \
3871ca2cfbcSDmitry Rokosov 		.shift = 4,                                                 \
3881ca2cfbcSDmitry Rokosov 		.endianness = IIO_LE,                                       \
3891ca2cfbcSDmitry Rokosov 	},                                                                  \
3901ca2cfbcSDmitry Rokosov 	.datasheet_name = "ACC_"#axis,                                      \
3911ca2cfbcSDmitry Rokosov }
3921ca2cfbcSDmitry Rokosov 
3931ca2cfbcSDmitry Rokosov static const struct iio_chan_spec msa311_channels[] = {
3941ca2cfbcSDmitry Rokosov 	MSA311_ACCEL_CHANNEL(X),
3951ca2cfbcSDmitry Rokosov 	MSA311_ACCEL_CHANNEL(Y),
3961ca2cfbcSDmitry Rokosov 	MSA311_ACCEL_CHANNEL(Z),
3971ca2cfbcSDmitry Rokosov 	IIO_CHAN_SOFT_TIMESTAMP(MSA311_SI_TIMESTAMP),
3981ca2cfbcSDmitry Rokosov };
3991ca2cfbcSDmitry Rokosov 
4001ca2cfbcSDmitry Rokosov /**
4011ca2cfbcSDmitry Rokosov  * msa311_get_odr() - Read Output Data Rate (ODR) value from MSA311 accel
4021ca2cfbcSDmitry Rokosov  * @msa311: MSA311 internal private state
4031ca2cfbcSDmitry Rokosov  * @odr: output ODR value
4041ca2cfbcSDmitry Rokosov  *
4051ca2cfbcSDmitry Rokosov  * This function should be called under msa311->lock.
4061ca2cfbcSDmitry Rokosov  *
4071ca2cfbcSDmitry Rokosov  * Return: 0 on success, -ERRNO in other failures
4081ca2cfbcSDmitry Rokosov  */
msa311_get_odr(struct msa311_priv * msa311,unsigned int * odr)4091ca2cfbcSDmitry Rokosov static int msa311_get_odr(struct msa311_priv *msa311, unsigned int *odr)
4101ca2cfbcSDmitry Rokosov {
4111ca2cfbcSDmitry Rokosov 	int err;
4121ca2cfbcSDmitry Rokosov 
4131ca2cfbcSDmitry Rokosov 	err = regmap_field_read(msa311->fields[F_ODR], odr);
4141ca2cfbcSDmitry Rokosov 	if (err)
4151ca2cfbcSDmitry Rokosov 		return err;
4161ca2cfbcSDmitry Rokosov 
4171ca2cfbcSDmitry Rokosov 	/*
4181ca2cfbcSDmitry Rokosov 	 * Filter the same 1000Hz ODR register values based on datasheet info.
4191ca2cfbcSDmitry Rokosov 	 * ODR can be equal to 1010-1111 for 1000Hz, but function returns 1010
4201ca2cfbcSDmitry Rokosov 	 * all the time.
4211ca2cfbcSDmitry Rokosov 	 */
4221ca2cfbcSDmitry Rokosov 	if (*odr > MSA311_ODR_1000_HZ)
4231ca2cfbcSDmitry Rokosov 		*odr = MSA311_ODR_1000_HZ;
4241ca2cfbcSDmitry Rokosov 
4251ca2cfbcSDmitry Rokosov 	return 0;
4261ca2cfbcSDmitry Rokosov }
4271ca2cfbcSDmitry Rokosov 
4281ca2cfbcSDmitry Rokosov /**
4291ca2cfbcSDmitry Rokosov  * msa311_set_odr() - Setup Output Data Rate (ODR) value for MSA311 accel
4301ca2cfbcSDmitry Rokosov  * @msa311: MSA311 internal private state
4311ca2cfbcSDmitry Rokosov  * @odr: requested ODR value
4321ca2cfbcSDmitry Rokosov  *
4331ca2cfbcSDmitry Rokosov  * This function should be called under msa311->lock. Possible ODR values:
4341ca2cfbcSDmitry Rokosov  *     - 1Hz (not available in normal mode)
4351ca2cfbcSDmitry Rokosov  *     - 1.95Hz (not available in normal mode)
4361ca2cfbcSDmitry Rokosov  *     - 3.9Hz
4371ca2cfbcSDmitry Rokosov  *     - 7.81Hz
4381ca2cfbcSDmitry Rokosov  *     - 15.63Hz
4391ca2cfbcSDmitry Rokosov  *     - 31.25Hz
4401ca2cfbcSDmitry Rokosov  *     - 62.5Hz
4411ca2cfbcSDmitry Rokosov  *     - 125Hz
4421ca2cfbcSDmitry Rokosov  *     - 250Hz
4431ca2cfbcSDmitry Rokosov  *     - 500Hz
4441ca2cfbcSDmitry Rokosov  *     - 1000Hz
4451ca2cfbcSDmitry Rokosov  *
4461ca2cfbcSDmitry Rokosov  * Return: 0 on success, -EINVAL for bad ODR value in the certain power mode,
4471ca2cfbcSDmitry Rokosov  *         -ERRNO in other failures
4481ca2cfbcSDmitry Rokosov  */
msa311_set_odr(struct msa311_priv * msa311,unsigned int odr)4491ca2cfbcSDmitry Rokosov static int msa311_set_odr(struct msa311_priv *msa311, unsigned int odr)
4501ca2cfbcSDmitry Rokosov {
4511ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
4521ca2cfbcSDmitry Rokosov 	unsigned int pwr_mode;
4531ca2cfbcSDmitry Rokosov 	bool good_odr;
4541ca2cfbcSDmitry Rokosov 	int err;
4551ca2cfbcSDmitry Rokosov 
4561ca2cfbcSDmitry Rokosov 	err = regmap_field_read(msa311->fields[F_PWR_MODE], &pwr_mode);
4571ca2cfbcSDmitry Rokosov 	if (err)
4581ca2cfbcSDmitry Rokosov 		return err;
4591ca2cfbcSDmitry Rokosov 
4601ca2cfbcSDmitry Rokosov 	/* Filter bad ODR values */
4611ca2cfbcSDmitry Rokosov 	if (pwr_mode == MSA311_PWR_MODE_NORMAL)
4621ca2cfbcSDmitry Rokosov 		good_odr = (odr > MSA311_ODR_1_95_HZ);
4631ca2cfbcSDmitry Rokosov 	else
4641ca2cfbcSDmitry Rokosov 		good_odr = false;
4651ca2cfbcSDmitry Rokosov 
4661ca2cfbcSDmitry Rokosov 	if (!good_odr) {
4671ca2cfbcSDmitry Rokosov 		dev_err(dev,
4681ca2cfbcSDmitry Rokosov 			"can't set odr %u.%06uHz, not available in %s mode\n",
4691ca2cfbcSDmitry Rokosov 			msa311_odr_table[odr].integral,
4701ca2cfbcSDmitry Rokosov 			msa311_odr_table[odr].microfract,
4711ca2cfbcSDmitry Rokosov 			msa311_pwr_modes[pwr_mode]);
4721ca2cfbcSDmitry Rokosov 		return -EINVAL;
4731ca2cfbcSDmitry Rokosov 	}
4741ca2cfbcSDmitry Rokosov 
4751ca2cfbcSDmitry Rokosov 	return regmap_field_write(msa311->fields[F_ODR], odr);
4761ca2cfbcSDmitry Rokosov }
4771ca2cfbcSDmitry Rokosov 
4781ca2cfbcSDmitry Rokosov /**
4791ca2cfbcSDmitry Rokosov  * msa311_wait_for_next_data() - Wait next accel data available after resume
4801ca2cfbcSDmitry Rokosov  * @msa311: MSA311 internal private state
4811ca2cfbcSDmitry Rokosov  *
4821ca2cfbcSDmitry Rokosov  * Return: 0 on success, -EINTR if msleep() was interrupted,
4831ca2cfbcSDmitry Rokosov  *         -ERRNO in other failures
4841ca2cfbcSDmitry Rokosov  */
msa311_wait_for_next_data(struct msa311_priv * msa311)4851ca2cfbcSDmitry Rokosov static int msa311_wait_for_next_data(struct msa311_priv *msa311)
4861ca2cfbcSDmitry Rokosov {
4871ca2cfbcSDmitry Rokosov 	static const unsigned int unintr_thresh_ms = 20;
4881ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
4891ca2cfbcSDmitry Rokosov 	unsigned long freq_uhz;
4901ca2cfbcSDmitry Rokosov 	unsigned long wait_ms;
4911ca2cfbcSDmitry Rokosov 	unsigned int odr;
4921ca2cfbcSDmitry Rokosov 	int err;
4931ca2cfbcSDmitry Rokosov 
4941ca2cfbcSDmitry Rokosov 	err = msa311_get_odr(msa311, &odr);
4951ca2cfbcSDmitry Rokosov 	if (err) {
4961ca2cfbcSDmitry Rokosov 		dev_err(dev, "can't get actual frequency (%pe)\n",
4971ca2cfbcSDmitry Rokosov 			ERR_PTR(err));
4981ca2cfbcSDmitry Rokosov 		return err;
4991ca2cfbcSDmitry Rokosov 	}
5001ca2cfbcSDmitry Rokosov 
5011ca2cfbcSDmitry Rokosov 	/*
5021ca2cfbcSDmitry Rokosov 	 * After msa311 resuming is done, we need to wait for data
5031ca2cfbcSDmitry Rokosov 	 * to be refreshed by accel logic.
5041ca2cfbcSDmitry Rokosov 	 * A certain timeout is calculated based on the current ODR value.
5051ca2cfbcSDmitry Rokosov 	 * If requested timeout isn't so long (let's assume 20ms),
5061ca2cfbcSDmitry Rokosov 	 * we can wait for next data in uninterruptible sleep.
5071ca2cfbcSDmitry Rokosov 	 */
5081ca2cfbcSDmitry Rokosov 	freq_uhz = msa311_odr_table[odr].integral * MICROHZ_PER_HZ +
5091ca2cfbcSDmitry Rokosov 		   msa311_odr_table[odr].microfract;
5101ca2cfbcSDmitry Rokosov 	wait_ms = (MICROHZ_PER_HZ / freq_uhz) * MSEC_PER_SEC;
5111ca2cfbcSDmitry Rokosov 
5121ca2cfbcSDmitry Rokosov 	if (wait_ms < unintr_thresh_ms)
5131ca2cfbcSDmitry Rokosov 		usleep_range(wait_ms * USEC_PER_MSEC,
5141ca2cfbcSDmitry Rokosov 			     unintr_thresh_ms * USEC_PER_MSEC);
5151ca2cfbcSDmitry Rokosov 	else if (msleep_interruptible(wait_ms))
5161ca2cfbcSDmitry Rokosov 		return -EINTR;
5171ca2cfbcSDmitry Rokosov 
5181ca2cfbcSDmitry Rokosov 	return 0;
5191ca2cfbcSDmitry Rokosov }
5201ca2cfbcSDmitry Rokosov 
5211ca2cfbcSDmitry Rokosov /**
5221ca2cfbcSDmitry Rokosov  * msa311_set_pwr_mode() - Install certain MSA311 power mode
5231ca2cfbcSDmitry Rokosov  * @msa311: MSA311 internal private state
5241ca2cfbcSDmitry Rokosov  * @mode: Power mode can be equal to NORMAL or SUSPEND
5251ca2cfbcSDmitry Rokosov  *
5261ca2cfbcSDmitry Rokosov  * This function should be called under msa311->lock.
5271ca2cfbcSDmitry Rokosov  *
5281ca2cfbcSDmitry Rokosov  * Return: 0 on success, -ERRNO on failure
5291ca2cfbcSDmitry Rokosov  */
msa311_set_pwr_mode(struct msa311_priv * msa311,unsigned int mode)5301ca2cfbcSDmitry Rokosov static int msa311_set_pwr_mode(struct msa311_priv *msa311, unsigned int mode)
5311ca2cfbcSDmitry Rokosov {
5321ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
5331ca2cfbcSDmitry Rokosov 	unsigned int prev_mode;
5341ca2cfbcSDmitry Rokosov 	int err;
5351ca2cfbcSDmitry Rokosov 
5361ca2cfbcSDmitry Rokosov 	if (mode >= ARRAY_SIZE(msa311_pwr_modes))
5371ca2cfbcSDmitry Rokosov 		return -EINVAL;
5381ca2cfbcSDmitry Rokosov 
5391ca2cfbcSDmitry Rokosov 	dev_dbg(dev, "transition to %s mode\n", msa311_pwr_modes[mode]);
5401ca2cfbcSDmitry Rokosov 
5411ca2cfbcSDmitry Rokosov 	err = regmap_field_read(msa311->fields[F_PWR_MODE], &prev_mode);
5421ca2cfbcSDmitry Rokosov 	if (err)
5431ca2cfbcSDmitry Rokosov 		return err;
5441ca2cfbcSDmitry Rokosov 
5451ca2cfbcSDmitry Rokosov 	err = regmap_field_write(msa311->fields[F_PWR_MODE], mode);
5461ca2cfbcSDmitry Rokosov 	if (err)
5471ca2cfbcSDmitry Rokosov 		return err;
5481ca2cfbcSDmitry Rokosov 
5491ca2cfbcSDmitry Rokosov 	/* Wait actual data if we wake up */
5501ca2cfbcSDmitry Rokosov 	if (prev_mode == MSA311_PWR_MODE_SUSPEND &&
5511ca2cfbcSDmitry Rokosov 	    mode == MSA311_PWR_MODE_NORMAL)
5521ca2cfbcSDmitry Rokosov 		return msa311_wait_for_next_data(msa311);
5531ca2cfbcSDmitry Rokosov 
5541ca2cfbcSDmitry Rokosov 	return 0;
5551ca2cfbcSDmitry Rokosov }
5561ca2cfbcSDmitry Rokosov 
5571ca2cfbcSDmitry Rokosov /**
5581ca2cfbcSDmitry Rokosov  * msa311_get_axis() - Read MSA311 accel data for certain IIO channel axis spec
5591ca2cfbcSDmitry Rokosov  * @msa311: MSA311 internal private state
5601ca2cfbcSDmitry Rokosov  * @chan: IIO channel specification
5611ca2cfbcSDmitry Rokosov  * @axis: Output accel axis data for requested IIO channel spec
5621ca2cfbcSDmitry Rokosov  *
5631ca2cfbcSDmitry Rokosov  * This function should be called under msa311->lock.
5641ca2cfbcSDmitry Rokosov  *
5651ca2cfbcSDmitry Rokosov  * Return: 0 on success, -EINVAL for unknown IIO channel specification,
5661ca2cfbcSDmitry Rokosov  *         -ERRNO in other failures
5671ca2cfbcSDmitry Rokosov  */
msa311_get_axis(struct msa311_priv * msa311,const struct iio_chan_spec * const chan,__le16 * axis)5681ca2cfbcSDmitry Rokosov static int msa311_get_axis(struct msa311_priv *msa311,
5691ca2cfbcSDmitry Rokosov 			   const struct iio_chan_spec * const chan,
5701ca2cfbcSDmitry Rokosov 			   __le16 *axis)
5711ca2cfbcSDmitry Rokosov {
5721ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
5731ca2cfbcSDmitry Rokosov 	unsigned int axis_reg;
5741ca2cfbcSDmitry Rokosov 
5751ca2cfbcSDmitry Rokosov 	if (chan->scan_index < MSA311_SI_X || chan->scan_index > MSA311_SI_Z) {
5761ca2cfbcSDmitry Rokosov 		dev_err(dev, "invalid scan_index value [%d]\n",
5771ca2cfbcSDmitry Rokosov 			chan->scan_index);
5781ca2cfbcSDmitry Rokosov 		return -EINVAL;
5791ca2cfbcSDmitry Rokosov 	}
5801ca2cfbcSDmitry Rokosov 
5811ca2cfbcSDmitry Rokosov 	/* Axes data layout has 2 byte gap for each axis starting from X axis */
5821ca2cfbcSDmitry Rokosov 	axis_reg = MSA311_ACC_X_REG + (chan->scan_index << 1);
5831ca2cfbcSDmitry Rokosov 
5841ca2cfbcSDmitry Rokosov 	return regmap_bulk_read(msa311->regs, axis_reg, axis, sizeof(*axis));
5851ca2cfbcSDmitry Rokosov }
5861ca2cfbcSDmitry Rokosov 
msa311_read_raw_data(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2)5871ca2cfbcSDmitry Rokosov static int msa311_read_raw_data(struct iio_dev *indio_dev,
5881ca2cfbcSDmitry Rokosov 				struct iio_chan_spec const *chan,
5891ca2cfbcSDmitry Rokosov 				int *val, int *val2)
5901ca2cfbcSDmitry Rokosov {
5911ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
5921ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
5931ca2cfbcSDmitry Rokosov 	__le16 axis;
5941ca2cfbcSDmitry Rokosov 	int err;
5951ca2cfbcSDmitry Rokosov 
5961ca2cfbcSDmitry Rokosov 	err = pm_runtime_resume_and_get(dev);
5971ca2cfbcSDmitry Rokosov 	if (err)
5981ca2cfbcSDmitry Rokosov 		return err;
5991ca2cfbcSDmitry Rokosov 
6001ca2cfbcSDmitry Rokosov 	err = iio_device_claim_direct_mode(indio_dev);
6011ca2cfbcSDmitry Rokosov 	if (err)
6021ca2cfbcSDmitry Rokosov 		return err;
6031ca2cfbcSDmitry Rokosov 
6041ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
6051ca2cfbcSDmitry Rokosov 	err = msa311_get_axis(msa311, chan, &axis);
6061ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
6071ca2cfbcSDmitry Rokosov 
6081ca2cfbcSDmitry Rokosov 	iio_device_release_direct_mode(indio_dev);
6091ca2cfbcSDmitry Rokosov 
6101ca2cfbcSDmitry Rokosov 	pm_runtime_mark_last_busy(dev);
6111ca2cfbcSDmitry Rokosov 	pm_runtime_put_autosuspend(dev);
6121ca2cfbcSDmitry Rokosov 
6131ca2cfbcSDmitry Rokosov 	if (err) {
6141ca2cfbcSDmitry Rokosov 		dev_err(dev, "can't get axis %s (%pe)\n",
6151ca2cfbcSDmitry Rokosov 			chan->datasheet_name, ERR_PTR(err));
6161ca2cfbcSDmitry Rokosov 		return err;
6171ca2cfbcSDmitry Rokosov 	}
6181ca2cfbcSDmitry Rokosov 
6191ca2cfbcSDmitry Rokosov 	/*
6201ca2cfbcSDmitry Rokosov 	 * Axis data format is:
6211ca2cfbcSDmitry Rokosov 	 * ACC_X = (ACC_X_MSB[7:0] << 4) | ACC_X_LSB[7:4]
6221ca2cfbcSDmitry Rokosov 	 */
6231ca2cfbcSDmitry Rokosov 	*val = sign_extend32(le16_to_cpu(axis) >> chan->scan_type.shift,
6241ca2cfbcSDmitry Rokosov 			     chan->scan_type.realbits - 1);
6251ca2cfbcSDmitry Rokosov 
6261ca2cfbcSDmitry Rokosov 	return IIO_VAL_INT;
6271ca2cfbcSDmitry Rokosov }
6281ca2cfbcSDmitry Rokosov 
msa311_read_scale(struct iio_dev * indio_dev,int * val,int * val2)6291ca2cfbcSDmitry Rokosov static int msa311_read_scale(struct iio_dev *indio_dev, int *val, int *val2)
6301ca2cfbcSDmitry Rokosov {
6311ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
6321ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
6331ca2cfbcSDmitry Rokosov 	unsigned int fs;
6341ca2cfbcSDmitry Rokosov 	int err;
6351ca2cfbcSDmitry Rokosov 
6361ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
6371ca2cfbcSDmitry Rokosov 	err = regmap_field_read(msa311->fields[F_FS], &fs);
6381ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
6391ca2cfbcSDmitry Rokosov 	if (err) {
6401ca2cfbcSDmitry Rokosov 		dev_err(dev, "can't get actual scale (%pe)\n", ERR_PTR(err));
6411ca2cfbcSDmitry Rokosov 		return err;
6421ca2cfbcSDmitry Rokosov 	}
6431ca2cfbcSDmitry Rokosov 
6441ca2cfbcSDmitry Rokosov 	*val = msa311_fs_table[fs].integral;
6451ca2cfbcSDmitry Rokosov 	*val2 = msa311_fs_table[fs].microfract;
6461ca2cfbcSDmitry Rokosov 
6471ca2cfbcSDmitry Rokosov 	return IIO_VAL_INT_PLUS_MICRO;
6481ca2cfbcSDmitry Rokosov }
6491ca2cfbcSDmitry Rokosov 
msa311_read_samp_freq(struct iio_dev * indio_dev,int * val,int * val2)6501ca2cfbcSDmitry Rokosov static int msa311_read_samp_freq(struct iio_dev *indio_dev,
6511ca2cfbcSDmitry Rokosov 				 int *val, int *val2)
6521ca2cfbcSDmitry Rokosov {
6531ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
6541ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
6551ca2cfbcSDmitry Rokosov 	unsigned int odr;
6561ca2cfbcSDmitry Rokosov 	int err;
6571ca2cfbcSDmitry Rokosov 
6581ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
6591ca2cfbcSDmitry Rokosov 	err = msa311_get_odr(msa311, &odr);
6601ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
6611ca2cfbcSDmitry Rokosov 	if (err) {
6621ca2cfbcSDmitry Rokosov 		dev_err(dev, "can't get actual frequency (%pe)\n",
6631ca2cfbcSDmitry Rokosov 			ERR_PTR(err));
6641ca2cfbcSDmitry Rokosov 		return err;
6651ca2cfbcSDmitry Rokosov 	}
6661ca2cfbcSDmitry Rokosov 
6671ca2cfbcSDmitry Rokosov 	*val = msa311_odr_table[odr].integral;
6681ca2cfbcSDmitry Rokosov 	*val2 = msa311_odr_table[odr].microfract;
6691ca2cfbcSDmitry Rokosov 
6701ca2cfbcSDmitry Rokosov 	return IIO_VAL_INT_PLUS_MICRO;
6711ca2cfbcSDmitry Rokosov }
6721ca2cfbcSDmitry Rokosov 
msa311_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)6731ca2cfbcSDmitry Rokosov static int msa311_read_raw(struct iio_dev *indio_dev,
6741ca2cfbcSDmitry Rokosov 			   struct iio_chan_spec const *chan,
6751ca2cfbcSDmitry Rokosov 			   int *val, int *val2, long mask)
6761ca2cfbcSDmitry Rokosov {
6771ca2cfbcSDmitry Rokosov 	switch (mask) {
6781ca2cfbcSDmitry Rokosov 	case IIO_CHAN_INFO_RAW:
6791ca2cfbcSDmitry Rokosov 		return msa311_read_raw_data(indio_dev, chan, val, val2);
6801ca2cfbcSDmitry Rokosov 
6811ca2cfbcSDmitry Rokosov 	case IIO_CHAN_INFO_SCALE:
6821ca2cfbcSDmitry Rokosov 		return msa311_read_scale(indio_dev, val, val2);
6831ca2cfbcSDmitry Rokosov 
6841ca2cfbcSDmitry Rokosov 	case IIO_CHAN_INFO_SAMP_FREQ:
6851ca2cfbcSDmitry Rokosov 		return msa311_read_samp_freq(indio_dev, val, val2);
6861ca2cfbcSDmitry Rokosov 
6871ca2cfbcSDmitry Rokosov 	default:
6881ca2cfbcSDmitry Rokosov 		return -EINVAL;
6891ca2cfbcSDmitry Rokosov 	}
6901ca2cfbcSDmitry Rokosov }
6911ca2cfbcSDmitry Rokosov 
msa311_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)6921ca2cfbcSDmitry Rokosov static int msa311_read_avail(struct iio_dev *indio_dev,
6931ca2cfbcSDmitry Rokosov 			     struct iio_chan_spec const *chan,
6941ca2cfbcSDmitry Rokosov 			     const int **vals, int *type,
6951ca2cfbcSDmitry Rokosov 			     int *length, long mask)
6961ca2cfbcSDmitry Rokosov {
6971ca2cfbcSDmitry Rokosov 	switch (mask) {
6981ca2cfbcSDmitry Rokosov 	case IIO_CHAN_INFO_SAMP_FREQ:
6991ca2cfbcSDmitry Rokosov 		*vals = (int *)msa311_odr_table;
7001ca2cfbcSDmitry Rokosov 		*type = IIO_VAL_INT_PLUS_MICRO;
7011ca2cfbcSDmitry Rokosov 		/* ODR value has 2 ints (integer and fractional parts) */
7021ca2cfbcSDmitry Rokosov 		*length = ARRAY_SIZE(msa311_odr_table) * 2;
7031ca2cfbcSDmitry Rokosov 		return IIO_AVAIL_LIST;
7041ca2cfbcSDmitry Rokosov 
7051ca2cfbcSDmitry Rokosov 	case IIO_CHAN_INFO_SCALE:
7061ca2cfbcSDmitry Rokosov 		*vals = (int *)msa311_fs_table;
7071ca2cfbcSDmitry Rokosov 		*type = IIO_VAL_INT_PLUS_MICRO;
7081ca2cfbcSDmitry Rokosov 		/* FS value has 2 ints (integer and fractional parts) */
7091ca2cfbcSDmitry Rokosov 		*length = ARRAY_SIZE(msa311_fs_table) * 2;
7101ca2cfbcSDmitry Rokosov 		return IIO_AVAIL_LIST;
7111ca2cfbcSDmitry Rokosov 
7121ca2cfbcSDmitry Rokosov 	default:
7131ca2cfbcSDmitry Rokosov 		return -EINVAL;
7141ca2cfbcSDmitry Rokosov 	}
7151ca2cfbcSDmitry Rokosov }
7161ca2cfbcSDmitry Rokosov 
msa311_write_scale(struct iio_dev * indio_dev,int val,int val2)7171ca2cfbcSDmitry Rokosov static int msa311_write_scale(struct iio_dev *indio_dev, int val, int val2)
7181ca2cfbcSDmitry Rokosov {
7191ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
7201ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
7211ca2cfbcSDmitry Rokosov 	unsigned int fs;
7221ca2cfbcSDmitry Rokosov 	int err;
7231ca2cfbcSDmitry Rokosov 
7241ca2cfbcSDmitry Rokosov 	/* We do not have fs >= 1, so skip such values */
7251ca2cfbcSDmitry Rokosov 	if (val)
7261ca2cfbcSDmitry Rokosov 		return 0;
7271ca2cfbcSDmitry Rokosov 
7281ca2cfbcSDmitry Rokosov 	err = pm_runtime_resume_and_get(dev);
7291ca2cfbcSDmitry Rokosov 	if (err)
7301ca2cfbcSDmitry Rokosov 		return err;
7311ca2cfbcSDmitry Rokosov 
7321ca2cfbcSDmitry Rokosov 	err = -EINVAL;
7331ca2cfbcSDmitry Rokosov 	for (fs = 0; fs < ARRAY_SIZE(msa311_fs_table); fs++)
7341ca2cfbcSDmitry Rokosov 		/* Do not check msa311_fs_table[fs].integral, it's always 0 */
7351ca2cfbcSDmitry Rokosov 		if (val2 == msa311_fs_table[fs].microfract) {
7361ca2cfbcSDmitry Rokosov 			mutex_lock(&msa311->lock);
7371ca2cfbcSDmitry Rokosov 			err = regmap_field_write(msa311->fields[F_FS], fs);
7381ca2cfbcSDmitry Rokosov 			mutex_unlock(&msa311->lock);
7391ca2cfbcSDmitry Rokosov 			break;
7401ca2cfbcSDmitry Rokosov 		}
7411ca2cfbcSDmitry Rokosov 
7421ca2cfbcSDmitry Rokosov 	pm_runtime_mark_last_busy(dev);
7431ca2cfbcSDmitry Rokosov 	pm_runtime_put_autosuspend(dev);
7441ca2cfbcSDmitry Rokosov 
7451ca2cfbcSDmitry Rokosov 	if (err)
7461ca2cfbcSDmitry Rokosov 		dev_err(dev, "can't update scale (%pe)\n", ERR_PTR(err));
7471ca2cfbcSDmitry Rokosov 
7481ca2cfbcSDmitry Rokosov 	return err;
7491ca2cfbcSDmitry Rokosov }
7501ca2cfbcSDmitry Rokosov 
msa311_write_samp_freq(struct iio_dev * indio_dev,int val,int val2)7511ca2cfbcSDmitry Rokosov static int msa311_write_samp_freq(struct iio_dev *indio_dev, int val, int val2)
7521ca2cfbcSDmitry Rokosov {
7531ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
7541ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
7551ca2cfbcSDmitry Rokosov 	unsigned int odr;
7561ca2cfbcSDmitry Rokosov 	int err;
7571ca2cfbcSDmitry Rokosov 
7581ca2cfbcSDmitry Rokosov 	err = pm_runtime_resume_and_get(dev);
7591ca2cfbcSDmitry Rokosov 	if (err)
7601ca2cfbcSDmitry Rokosov 		return err;
7611ca2cfbcSDmitry Rokosov 
7621ca2cfbcSDmitry Rokosov 	/*
7631ca2cfbcSDmitry Rokosov 	 * Sampling frequency changing is prohibited when buffer mode is
7641ca2cfbcSDmitry Rokosov 	 * enabled, because sometimes MSA311 chip returns outliers during
7651ca2cfbcSDmitry Rokosov 	 * frequency values growing up in the read operation moment.
7661ca2cfbcSDmitry Rokosov 	 */
7671ca2cfbcSDmitry Rokosov 	err = iio_device_claim_direct_mode(indio_dev);
7681ca2cfbcSDmitry Rokosov 	if (err)
7691ca2cfbcSDmitry Rokosov 		return err;
7701ca2cfbcSDmitry Rokosov 
7711ca2cfbcSDmitry Rokosov 	err = -EINVAL;
7721ca2cfbcSDmitry Rokosov 	for (odr = 0; odr < ARRAY_SIZE(msa311_odr_table); odr++)
7731ca2cfbcSDmitry Rokosov 		if (val == msa311_odr_table[odr].integral &&
7741ca2cfbcSDmitry Rokosov 		    val2 == msa311_odr_table[odr].microfract) {
7751ca2cfbcSDmitry Rokosov 			mutex_lock(&msa311->lock);
7761ca2cfbcSDmitry Rokosov 			err = msa311_set_odr(msa311, odr);
7771ca2cfbcSDmitry Rokosov 			mutex_unlock(&msa311->lock);
7781ca2cfbcSDmitry Rokosov 			break;
7791ca2cfbcSDmitry Rokosov 		}
7801ca2cfbcSDmitry Rokosov 
7811ca2cfbcSDmitry Rokosov 	iio_device_release_direct_mode(indio_dev);
7821ca2cfbcSDmitry Rokosov 
7831ca2cfbcSDmitry Rokosov 	pm_runtime_mark_last_busy(dev);
7841ca2cfbcSDmitry Rokosov 	pm_runtime_put_autosuspend(dev);
7851ca2cfbcSDmitry Rokosov 
7861ca2cfbcSDmitry Rokosov 	if (err)
7871ca2cfbcSDmitry Rokosov 		dev_err(dev, "can't update frequency (%pe)\n", ERR_PTR(err));
7881ca2cfbcSDmitry Rokosov 
7891ca2cfbcSDmitry Rokosov 	return err;
7901ca2cfbcSDmitry Rokosov }
7911ca2cfbcSDmitry Rokosov 
msa311_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)7921ca2cfbcSDmitry Rokosov static int msa311_write_raw(struct iio_dev *indio_dev,
7931ca2cfbcSDmitry Rokosov 			    struct iio_chan_spec const *chan,
7941ca2cfbcSDmitry Rokosov 			    int val, int val2, long mask)
7951ca2cfbcSDmitry Rokosov {
7961ca2cfbcSDmitry Rokosov 	switch (mask) {
7971ca2cfbcSDmitry Rokosov 	case IIO_CHAN_INFO_SCALE:
7981ca2cfbcSDmitry Rokosov 		return msa311_write_scale(indio_dev, val, val2);
7991ca2cfbcSDmitry Rokosov 
8001ca2cfbcSDmitry Rokosov 	case IIO_CHAN_INFO_SAMP_FREQ:
8011ca2cfbcSDmitry Rokosov 		return msa311_write_samp_freq(indio_dev, val, val2);
8021ca2cfbcSDmitry Rokosov 
8031ca2cfbcSDmitry Rokosov 	default:
8041ca2cfbcSDmitry Rokosov 		return -EINVAL;
8051ca2cfbcSDmitry Rokosov 	}
8061ca2cfbcSDmitry Rokosov }
8071ca2cfbcSDmitry Rokosov 
msa311_debugfs_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)8081ca2cfbcSDmitry Rokosov static int msa311_debugfs_reg_access(struct iio_dev *indio_dev,
8091ca2cfbcSDmitry Rokosov 				     unsigned int reg, unsigned int writeval,
8101ca2cfbcSDmitry Rokosov 				     unsigned int *readval)
8111ca2cfbcSDmitry Rokosov {
8121ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
8131ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
8141ca2cfbcSDmitry Rokosov 	int err;
8151ca2cfbcSDmitry Rokosov 
8161ca2cfbcSDmitry Rokosov 	if (reg > regmap_get_max_register(msa311->regs))
8171ca2cfbcSDmitry Rokosov 		return -EINVAL;
8181ca2cfbcSDmitry Rokosov 
8191ca2cfbcSDmitry Rokosov 	err = pm_runtime_resume_and_get(dev);
8201ca2cfbcSDmitry Rokosov 	if (err)
8211ca2cfbcSDmitry Rokosov 		return err;
8221ca2cfbcSDmitry Rokosov 
8231ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
8241ca2cfbcSDmitry Rokosov 
8251ca2cfbcSDmitry Rokosov 	if (readval)
8261ca2cfbcSDmitry Rokosov 		err = regmap_read(msa311->regs, reg, readval);
8271ca2cfbcSDmitry Rokosov 	else
8281ca2cfbcSDmitry Rokosov 		err = regmap_write(msa311->regs, reg, writeval);
8291ca2cfbcSDmitry Rokosov 
8301ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
8311ca2cfbcSDmitry Rokosov 
8321ca2cfbcSDmitry Rokosov 	pm_runtime_mark_last_busy(dev);
8331ca2cfbcSDmitry Rokosov 	pm_runtime_put_autosuspend(dev);
8341ca2cfbcSDmitry Rokosov 
8351ca2cfbcSDmitry Rokosov 	if (err)
8361ca2cfbcSDmitry Rokosov 		dev_err(dev, "can't %s register %u from debugfs (%pe)\n",
8371ca2cfbcSDmitry Rokosov 			str_read_write(readval), reg, ERR_PTR(err));
8381ca2cfbcSDmitry Rokosov 
8391ca2cfbcSDmitry Rokosov 	return err;
8401ca2cfbcSDmitry Rokosov }
8411ca2cfbcSDmitry Rokosov 
msa311_buffer_preenable(struct iio_dev * indio_dev)8421ca2cfbcSDmitry Rokosov static int msa311_buffer_preenable(struct iio_dev *indio_dev)
8431ca2cfbcSDmitry Rokosov {
8441ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
8451ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
8461ca2cfbcSDmitry Rokosov 
8471ca2cfbcSDmitry Rokosov 	return pm_runtime_resume_and_get(dev);
8481ca2cfbcSDmitry Rokosov }
8491ca2cfbcSDmitry Rokosov 
msa311_buffer_postdisable(struct iio_dev * indio_dev)8501ca2cfbcSDmitry Rokosov static int msa311_buffer_postdisable(struct iio_dev *indio_dev)
8511ca2cfbcSDmitry Rokosov {
8521ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
8531ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
8541ca2cfbcSDmitry Rokosov 
8551ca2cfbcSDmitry Rokosov 	pm_runtime_mark_last_busy(dev);
8561ca2cfbcSDmitry Rokosov 	pm_runtime_put_autosuspend(dev);
8571ca2cfbcSDmitry Rokosov 
8581ca2cfbcSDmitry Rokosov 	return 0;
8591ca2cfbcSDmitry Rokosov }
8601ca2cfbcSDmitry Rokosov 
msa311_set_new_data_trig_state(struct iio_trigger * trig,bool state)8611ca2cfbcSDmitry Rokosov static int msa311_set_new_data_trig_state(struct iio_trigger *trig, bool state)
8621ca2cfbcSDmitry Rokosov {
8631ca2cfbcSDmitry Rokosov 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
8641ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
8651ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
8661ca2cfbcSDmitry Rokosov 	int err;
8671ca2cfbcSDmitry Rokosov 
8681ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
8691ca2cfbcSDmitry Rokosov 	err = regmap_field_write(msa311->fields[F_NEW_DATA_INT_EN], state);
8701ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
8711ca2cfbcSDmitry Rokosov 	if (err)
8721ca2cfbcSDmitry Rokosov 		dev_err(dev,
8731ca2cfbcSDmitry Rokosov 			"can't %s buffer due to new_data_int failure (%pe)\n",
8741ca2cfbcSDmitry Rokosov 			str_enable_disable(state), ERR_PTR(err));
8751ca2cfbcSDmitry Rokosov 
8761ca2cfbcSDmitry Rokosov 	return err;
8771ca2cfbcSDmitry Rokosov }
8781ca2cfbcSDmitry Rokosov 
msa311_validate_device(struct iio_trigger * trig,struct iio_dev * indio_dev)8791ca2cfbcSDmitry Rokosov static int msa311_validate_device(struct iio_trigger *trig,
8801ca2cfbcSDmitry Rokosov 				  struct iio_dev *indio_dev)
8811ca2cfbcSDmitry Rokosov {
8821ca2cfbcSDmitry Rokosov 	return iio_trigger_get_drvdata(trig) == indio_dev ? 0 : -EINVAL;
8831ca2cfbcSDmitry Rokosov }
8841ca2cfbcSDmitry Rokosov 
msa311_buffer_thread(int irq,void * p)8851ca2cfbcSDmitry Rokosov static irqreturn_t msa311_buffer_thread(int irq, void *p)
8861ca2cfbcSDmitry Rokosov {
8871ca2cfbcSDmitry Rokosov 	struct iio_poll_func *pf = p;
8881ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(pf->indio_dev);
8891ca2cfbcSDmitry Rokosov 	struct iio_dev *indio_dev = pf->indio_dev;
8901ca2cfbcSDmitry Rokosov 	const struct iio_chan_spec *chan;
8911ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
8921ca2cfbcSDmitry Rokosov 	int bit, err, i = 0;
8931ca2cfbcSDmitry Rokosov 	__le16 axis;
8941ca2cfbcSDmitry Rokosov 	struct {
8951ca2cfbcSDmitry Rokosov 		__le16 channels[MSA311_SI_Z + 1];
8961ca2cfbcSDmitry Rokosov 		s64 ts __aligned(8);
8971ca2cfbcSDmitry Rokosov 	} buf;
8981ca2cfbcSDmitry Rokosov 
8991ca2cfbcSDmitry Rokosov 	memset(&buf, 0, sizeof(buf));
9001ca2cfbcSDmitry Rokosov 
9011ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
9021ca2cfbcSDmitry Rokosov 
903*e5003b60SNuno Sa 	iio_for_each_active_channel(indio_dev, bit) {
9041ca2cfbcSDmitry Rokosov 		chan = &msa311_channels[bit];
9051ca2cfbcSDmitry Rokosov 
9061ca2cfbcSDmitry Rokosov 		err = msa311_get_axis(msa311, chan, &axis);
9071ca2cfbcSDmitry Rokosov 		if (err) {
9081ca2cfbcSDmitry Rokosov 			mutex_unlock(&msa311->lock);
9091ca2cfbcSDmitry Rokosov 			dev_err(dev, "can't get axis %s (%pe)\n",
9101ca2cfbcSDmitry Rokosov 				chan->datasheet_name, ERR_PTR(err));
9111ca2cfbcSDmitry Rokosov 			goto notify_done;
9121ca2cfbcSDmitry Rokosov 		}
9131ca2cfbcSDmitry Rokosov 
9141ca2cfbcSDmitry Rokosov 		buf.channels[i++] = axis;
9151ca2cfbcSDmitry Rokosov 	}
9161ca2cfbcSDmitry Rokosov 
9171ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
9181ca2cfbcSDmitry Rokosov 
9191ca2cfbcSDmitry Rokosov 	iio_push_to_buffers_with_timestamp(indio_dev, &buf,
9201ca2cfbcSDmitry Rokosov 					   iio_get_time_ns(indio_dev));
9211ca2cfbcSDmitry Rokosov 
9221ca2cfbcSDmitry Rokosov notify_done:
9231ca2cfbcSDmitry Rokosov 	iio_trigger_notify_done(indio_dev->trig);
9241ca2cfbcSDmitry Rokosov 
9251ca2cfbcSDmitry Rokosov 	return IRQ_HANDLED;
9261ca2cfbcSDmitry Rokosov }
9271ca2cfbcSDmitry Rokosov 
msa311_irq_thread(int irq,void * p)9281ca2cfbcSDmitry Rokosov static irqreturn_t msa311_irq_thread(int irq, void *p)
9291ca2cfbcSDmitry Rokosov {
9301ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(p);
9311ca2cfbcSDmitry Rokosov 	unsigned int new_data_int_enabled;
9321ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
9331ca2cfbcSDmitry Rokosov 	int err;
9341ca2cfbcSDmitry Rokosov 
9351ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
9361ca2cfbcSDmitry Rokosov 
9371ca2cfbcSDmitry Rokosov 	/*
9381ca2cfbcSDmitry Rokosov 	 * We do not check NEW_DATA int status, because based on the
9391ca2cfbcSDmitry Rokosov 	 * specification it's cleared automatically after a fixed time.
9401ca2cfbcSDmitry Rokosov 	 * So just check that is enabled by driver logic.
9411ca2cfbcSDmitry Rokosov 	 */
9421ca2cfbcSDmitry Rokosov 	err = regmap_field_read(msa311->fields[F_NEW_DATA_INT_EN],
9431ca2cfbcSDmitry Rokosov 				&new_data_int_enabled);
9441ca2cfbcSDmitry Rokosov 
9451ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
9461ca2cfbcSDmitry Rokosov 	if (err) {
9471ca2cfbcSDmitry Rokosov 		dev_err(dev, "can't read new_data interrupt state (%pe)\n",
9481ca2cfbcSDmitry Rokosov 			ERR_PTR(err));
9491ca2cfbcSDmitry Rokosov 		return IRQ_NONE;
9501ca2cfbcSDmitry Rokosov 	}
9511ca2cfbcSDmitry Rokosov 
9521ca2cfbcSDmitry Rokosov 	if (new_data_int_enabled)
953f700e55eSMehdi Djait 		iio_trigger_poll_nested(msa311->new_data_trig);
9541ca2cfbcSDmitry Rokosov 
9551ca2cfbcSDmitry Rokosov 	return IRQ_HANDLED;
9561ca2cfbcSDmitry Rokosov }
9571ca2cfbcSDmitry Rokosov 
9581ca2cfbcSDmitry Rokosov static const struct iio_info msa311_info = {
9591ca2cfbcSDmitry Rokosov 	.read_raw = msa311_read_raw,
9601ca2cfbcSDmitry Rokosov 	.read_avail = msa311_read_avail,
9611ca2cfbcSDmitry Rokosov 	.write_raw = msa311_write_raw,
9621ca2cfbcSDmitry Rokosov 	.debugfs_reg_access = msa311_debugfs_reg_access,
9631ca2cfbcSDmitry Rokosov };
9641ca2cfbcSDmitry Rokosov 
9651ca2cfbcSDmitry Rokosov static const struct iio_buffer_setup_ops msa311_buffer_setup_ops = {
9661ca2cfbcSDmitry Rokosov 	.preenable = msa311_buffer_preenable,
9671ca2cfbcSDmitry Rokosov 	.postdisable = msa311_buffer_postdisable,
9681ca2cfbcSDmitry Rokosov };
9691ca2cfbcSDmitry Rokosov 
9701ca2cfbcSDmitry Rokosov static const struct iio_trigger_ops msa311_new_data_trig_ops = {
9711ca2cfbcSDmitry Rokosov 	.set_trigger_state = msa311_set_new_data_trig_state,
9721ca2cfbcSDmitry Rokosov 	.validate_device = msa311_validate_device,
9731ca2cfbcSDmitry Rokosov };
9741ca2cfbcSDmitry Rokosov 
msa311_check_partid(struct msa311_priv * msa311)9751ca2cfbcSDmitry Rokosov static int msa311_check_partid(struct msa311_priv *msa311)
9761ca2cfbcSDmitry Rokosov {
9771ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
9781ca2cfbcSDmitry Rokosov 	unsigned int partid;
9791ca2cfbcSDmitry Rokosov 	int err;
9801ca2cfbcSDmitry Rokosov 
9811ca2cfbcSDmitry Rokosov 	err = regmap_read(msa311->regs, MSA311_PARTID_REG, &partid);
9821ca2cfbcSDmitry Rokosov 	if (err)
9831ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err, "failed to read partid\n");
9841ca2cfbcSDmitry Rokosov 
9851ca2cfbcSDmitry Rokosov 	if (partid != MSA311_WHO_AM_I)
9861ca2cfbcSDmitry Rokosov 		dev_warn(dev, "invalid partid (%#x), expected (%#x)\n",
9871ca2cfbcSDmitry Rokosov 			 partid, MSA311_WHO_AM_I);
9881ca2cfbcSDmitry Rokosov 
9891ca2cfbcSDmitry Rokosov 	msa311->chip_name = devm_kasprintf(dev, GFP_KERNEL,
9901ca2cfbcSDmitry Rokosov 					   "msa311-%02x", partid);
9911ca2cfbcSDmitry Rokosov 	if (!msa311->chip_name)
9921ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, -ENOMEM, "can't alloc chip name\n");
9931ca2cfbcSDmitry Rokosov 
9941ca2cfbcSDmitry Rokosov 	return 0;
9951ca2cfbcSDmitry Rokosov }
9961ca2cfbcSDmitry Rokosov 
msa311_soft_reset(struct msa311_priv * msa311)9971ca2cfbcSDmitry Rokosov static int msa311_soft_reset(struct msa311_priv *msa311)
9981ca2cfbcSDmitry Rokosov {
9991ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
10001ca2cfbcSDmitry Rokosov 	int err;
10011ca2cfbcSDmitry Rokosov 
10021ca2cfbcSDmitry Rokosov 	err = regmap_write(msa311->regs, MSA311_SOFT_RESET_REG,
10031ca2cfbcSDmitry Rokosov 			   MSA311_GENMASK(F_SOFT_RESET_I2C) |
10041ca2cfbcSDmitry Rokosov 			   MSA311_GENMASK(F_SOFT_RESET_SPI));
10051ca2cfbcSDmitry Rokosov 	if (err)
10061ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err, "can't soft reset all logic\n");
10071ca2cfbcSDmitry Rokosov 
10081ca2cfbcSDmitry Rokosov 	return 0;
10091ca2cfbcSDmitry Rokosov }
10101ca2cfbcSDmitry Rokosov 
msa311_chip_init(struct msa311_priv * msa311)10111ca2cfbcSDmitry Rokosov static int msa311_chip_init(struct msa311_priv *msa311)
10121ca2cfbcSDmitry Rokosov {
10131ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
10141ca2cfbcSDmitry Rokosov 	const char zero_bulk[2] = { };
10151ca2cfbcSDmitry Rokosov 	int err;
10161ca2cfbcSDmitry Rokosov 
10171ca2cfbcSDmitry Rokosov 	err = regmap_write(msa311->regs, MSA311_RANGE_REG, MSA311_FS_16G);
10181ca2cfbcSDmitry Rokosov 	if (err)
10191ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err, "failed to setup accel range\n");
10201ca2cfbcSDmitry Rokosov 
10211ca2cfbcSDmitry Rokosov 	/* Disable all interrupts by default */
10221ca2cfbcSDmitry Rokosov 	err = regmap_bulk_write(msa311->regs, MSA311_INT_SET_0_REG,
10231ca2cfbcSDmitry Rokosov 				zero_bulk, sizeof(zero_bulk));
10241ca2cfbcSDmitry Rokosov 	if (err)
10251ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
10261ca2cfbcSDmitry Rokosov 				     "can't disable set0/set1 interrupts\n");
10271ca2cfbcSDmitry Rokosov 
10281ca2cfbcSDmitry Rokosov 	/* Unmap all INT1 interrupts by default */
10291ca2cfbcSDmitry Rokosov 	err = regmap_bulk_write(msa311->regs, MSA311_INT_MAP_0_REG,
10301ca2cfbcSDmitry Rokosov 				zero_bulk, sizeof(zero_bulk));
10311ca2cfbcSDmitry Rokosov 	if (err)
10321ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
10331ca2cfbcSDmitry Rokosov 				     "failed to unmap map0/map1 interrupts\n");
10341ca2cfbcSDmitry Rokosov 
10351ca2cfbcSDmitry Rokosov 	/* Disable all axes by default */
10364ed403d8STrevor Gamblin 	err = regmap_clear_bits(msa311->regs, MSA311_ODR_REG,
10371ca2cfbcSDmitry Rokosov 				MSA311_GENMASK(F_X_AXIS_DIS) |
10381ca2cfbcSDmitry Rokosov 				MSA311_GENMASK(F_Y_AXIS_DIS) |
10394ed403d8STrevor Gamblin 				MSA311_GENMASK(F_Z_AXIS_DIS));
10401ca2cfbcSDmitry Rokosov 	if (err)
10411ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err, "can't enable all axes\n");
10421ca2cfbcSDmitry Rokosov 
10431ca2cfbcSDmitry Rokosov 	err = msa311_set_odr(msa311, MSA311_ODR_125_HZ);
10441ca2cfbcSDmitry Rokosov 	if (err)
10451ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
10461ca2cfbcSDmitry Rokosov 				     "failed to set accel frequency\n");
10471ca2cfbcSDmitry Rokosov 
10481ca2cfbcSDmitry Rokosov 	return 0;
10491ca2cfbcSDmitry Rokosov }
10501ca2cfbcSDmitry Rokosov 
msa311_setup_interrupts(struct msa311_priv * msa311)10511ca2cfbcSDmitry Rokosov static int msa311_setup_interrupts(struct msa311_priv *msa311)
10521ca2cfbcSDmitry Rokosov {
10531ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
10541ca2cfbcSDmitry Rokosov 	struct i2c_client *i2c = to_i2c_client(dev);
10551ca2cfbcSDmitry Rokosov 	struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
10561ca2cfbcSDmitry Rokosov 	struct iio_trigger *trig;
10571ca2cfbcSDmitry Rokosov 	int err;
10581ca2cfbcSDmitry Rokosov 
10591ca2cfbcSDmitry Rokosov 	/* Keep going without interrupts if no initialized I2C IRQ */
10601ca2cfbcSDmitry Rokosov 	if (i2c->irq <= 0)
10611ca2cfbcSDmitry Rokosov 		return 0;
10621ca2cfbcSDmitry Rokosov 
10631ca2cfbcSDmitry Rokosov 	err = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
10641ca2cfbcSDmitry Rokosov 					msa311_irq_thread, IRQF_ONESHOT,
10651ca2cfbcSDmitry Rokosov 					msa311->chip_name, indio_dev);
10661ca2cfbcSDmitry Rokosov 	if (err)
10671ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err, "failed to request IRQ\n");
10681ca2cfbcSDmitry Rokosov 
10691ca2cfbcSDmitry Rokosov 	trig = devm_iio_trigger_alloc(dev, "%s-new-data", msa311->chip_name);
10701ca2cfbcSDmitry Rokosov 	if (!trig)
10711ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, -ENOMEM,
10721ca2cfbcSDmitry Rokosov 				     "can't allocate newdata trigger\n");
10731ca2cfbcSDmitry Rokosov 
10741ca2cfbcSDmitry Rokosov 	msa311->new_data_trig = trig;
10751ca2cfbcSDmitry Rokosov 	msa311->new_data_trig->ops = &msa311_new_data_trig_ops;
10761ca2cfbcSDmitry Rokosov 	iio_trigger_set_drvdata(msa311->new_data_trig, indio_dev);
10771ca2cfbcSDmitry Rokosov 
10781ca2cfbcSDmitry Rokosov 	err = devm_iio_trigger_register(dev, msa311->new_data_trig);
10791ca2cfbcSDmitry Rokosov 	if (err)
10801ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
10811ca2cfbcSDmitry Rokosov 				     "can't register newdata trigger\n");
10821ca2cfbcSDmitry Rokosov 
10831ca2cfbcSDmitry Rokosov 	err = regmap_field_write(msa311->fields[F_INT1_OD],
10841ca2cfbcSDmitry Rokosov 				 MSA311_INT1_OD_PUSH_PULL);
10851ca2cfbcSDmitry Rokosov 	if (err)
10861ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
10871ca2cfbcSDmitry Rokosov 				     "can't enable push-pull interrupt\n");
10881ca2cfbcSDmitry Rokosov 
10891ca2cfbcSDmitry Rokosov 	err = regmap_field_write(msa311->fields[F_INT1_LVL],
10901ca2cfbcSDmitry Rokosov 				 MSA311_INT1_LVL_HIGH);
10911ca2cfbcSDmitry Rokosov 	if (err)
10921ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
10931ca2cfbcSDmitry Rokosov 				     "can't set active interrupt level\n");
10941ca2cfbcSDmitry Rokosov 
10951ca2cfbcSDmitry Rokosov 	err = regmap_field_write(msa311->fields[F_LATCH_INT],
10961ca2cfbcSDmitry Rokosov 				 MSA311_LATCH_INT_LATCHED);
10971ca2cfbcSDmitry Rokosov 	if (err)
10981ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
10991ca2cfbcSDmitry Rokosov 				     "can't latch interrupt\n");
11001ca2cfbcSDmitry Rokosov 
11011ca2cfbcSDmitry Rokosov 	err = regmap_field_write(msa311->fields[F_RESET_INT], 1);
11021ca2cfbcSDmitry Rokosov 	if (err)
11031ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
11041ca2cfbcSDmitry Rokosov 				     "can't reset interrupt\n");
11051ca2cfbcSDmitry Rokosov 
11061ca2cfbcSDmitry Rokosov 	err = regmap_field_write(msa311->fields[F_INT1_NEW_DATA], 1);
11071ca2cfbcSDmitry Rokosov 	if (err)
11081ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
11091ca2cfbcSDmitry Rokosov 				     "can't map new data interrupt\n");
11101ca2cfbcSDmitry Rokosov 
11111ca2cfbcSDmitry Rokosov 	return 0;
11121ca2cfbcSDmitry Rokosov }
11131ca2cfbcSDmitry Rokosov 
msa311_regmap_init(struct msa311_priv * msa311)11141ca2cfbcSDmitry Rokosov static int msa311_regmap_init(struct msa311_priv *msa311)
11151ca2cfbcSDmitry Rokosov {
11161ca2cfbcSDmitry Rokosov 	struct regmap_field **fields = msa311->fields;
11171ca2cfbcSDmitry Rokosov 	struct device *dev = msa311->dev;
11181ca2cfbcSDmitry Rokosov 	struct i2c_client *i2c = to_i2c_client(dev);
11191ca2cfbcSDmitry Rokosov 	struct regmap *regmap;
11201ca2cfbcSDmitry Rokosov 	int i;
11211ca2cfbcSDmitry Rokosov 
11221ca2cfbcSDmitry Rokosov 	regmap = devm_regmap_init_i2c(i2c, &msa311_regmap_config);
11231ca2cfbcSDmitry Rokosov 	if (IS_ERR(regmap))
11241ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, PTR_ERR(regmap),
11251ca2cfbcSDmitry Rokosov 				     "failed to register i2c regmap\n");
11261ca2cfbcSDmitry Rokosov 
11271ca2cfbcSDmitry Rokosov 	msa311->regs = regmap;
11281ca2cfbcSDmitry Rokosov 
11291ca2cfbcSDmitry Rokosov 	for (i = 0; i < F_MAX_FIELDS; i++) {
11301ca2cfbcSDmitry Rokosov 		fields[i] = devm_regmap_field_alloc(dev,
11311ca2cfbcSDmitry Rokosov 						    msa311->regs,
11321ca2cfbcSDmitry Rokosov 						    msa311_reg_fields[i]);
11331ca2cfbcSDmitry Rokosov 		if (IS_ERR(msa311->fields[i]))
11341ca2cfbcSDmitry Rokosov 			return dev_err_probe(dev, PTR_ERR(msa311->fields[i]),
11351ca2cfbcSDmitry Rokosov 					     "can't alloc field[%d]\n", i);
11361ca2cfbcSDmitry Rokosov 	}
11371ca2cfbcSDmitry Rokosov 
11381ca2cfbcSDmitry Rokosov 	return 0;
11391ca2cfbcSDmitry Rokosov }
11401ca2cfbcSDmitry Rokosov 
msa311_powerdown(void * msa311)11411ca2cfbcSDmitry Rokosov static void msa311_powerdown(void *msa311)
11421ca2cfbcSDmitry Rokosov {
11431ca2cfbcSDmitry Rokosov 	msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_SUSPEND);
11441ca2cfbcSDmitry Rokosov }
11451ca2cfbcSDmitry Rokosov 
msa311_probe(struct i2c_client * i2c)11461ca2cfbcSDmitry Rokosov static int msa311_probe(struct i2c_client *i2c)
11471ca2cfbcSDmitry Rokosov {
11481ca2cfbcSDmitry Rokosov 	struct device *dev = &i2c->dev;
11491ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311;
11501ca2cfbcSDmitry Rokosov 	struct iio_dev *indio_dev;
11511ca2cfbcSDmitry Rokosov 	int err;
11521ca2cfbcSDmitry Rokosov 
11531ca2cfbcSDmitry Rokosov 	indio_dev = devm_iio_device_alloc(dev, sizeof(*msa311));
11541ca2cfbcSDmitry Rokosov 	if (!indio_dev)
11551ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, -ENOMEM,
11561ca2cfbcSDmitry Rokosov 				     "IIO device allocation failed\n");
11571ca2cfbcSDmitry Rokosov 
11581ca2cfbcSDmitry Rokosov 	msa311 = iio_priv(indio_dev);
11591ca2cfbcSDmitry Rokosov 	msa311->dev = dev;
11601ca2cfbcSDmitry Rokosov 	i2c_set_clientdata(i2c, indio_dev);
11611ca2cfbcSDmitry Rokosov 
11621ca2cfbcSDmitry Rokosov 	err = msa311_regmap_init(msa311);
11631ca2cfbcSDmitry Rokosov 	if (err)
11641ca2cfbcSDmitry Rokosov 		return err;
11651ca2cfbcSDmitry Rokosov 
11661ca2cfbcSDmitry Rokosov 	mutex_init(&msa311->lock);
11671ca2cfbcSDmitry Rokosov 
1168eefa008bSJonathan Cameron 	err = devm_regulator_get_enable(dev, "vdd");
11691ca2cfbcSDmitry Rokosov 	if (err)
1170eefa008bSJonathan Cameron 		return dev_err_probe(dev, err, "can't get vdd supply\n");
11711ca2cfbcSDmitry Rokosov 
11721ca2cfbcSDmitry Rokosov 	err = msa311_check_partid(msa311);
11731ca2cfbcSDmitry Rokosov 	if (err)
11741ca2cfbcSDmitry Rokosov 		return err;
11751ca2cfbcSDmitry Rokosov 
11761ca2cfbcSDmitry Rokosov 	err = msa311_soft_reset(msa311);
11771ca2cfbcSDmitry Rokosov 	if (err)
11781ca2cfbcSDmitry Rokosov 		return err;
11791ca2cfbcSDmitry Rokosov 
11801ca2cfbcSDmitry Rokosov 	err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_NORMAL);
11811ca2cfbcSDmitry Rokosov 	if (err)
11821ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err, "failed to power on device\n");
11831ca2cfbcSDmitry Rokosov 
11841ca2cfbcSDmitry Rokosov 	/*
11851ca2cfbcSDmitry Rokosov 	 * Register powerdown deferred callback which suspends the chip
11861ca2cfbcSDmitry Rokosov 	 * after module unloaded.
11871ca2cfbcSDmitry Rokosov 	 *
11881ca2cfbcSDmitry Rokosov 	 * MSA311 should be in SUSPEND mode in the two cases:
11891ca2cfbcSDmitry Rokosov 	 * 1) When driver is loaded, but we do not have any data or
11901ca2cfbcSDmitry Rokosov 	 *    configuration requests to it (we are solving it using
11911ca2cfbcSDmitry Rokosov 	 *    autosuspend feature).
11921ca2cfbcSDmitry Rokosov 	 * 2) When driver is unloaded and device is not used (devm action is
11931ca2cfbcSDmitry Rokosov 	 *    used in this case).
11941ca2cfbcSDmitry Rokosov 	 */
11951ca2cfbcSDmitry Rokosov 	err = devm_add_action_or_reset(dev, msa311_powerdown, msa311);
11961ca2cfbcSDmitry Rokosov 	if (err)
11971ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err, "can't add powerdown action\n");
11981ca2cfbcSDmitry Rokosov 
11991ca2cfbcSDmitry Rokosov 	err = pm_runtime_set_active(dev);
12001ca2cfbcSDmitry Rokosov 	if (err)
12011ca2cfbcSDmitry Rokosov 		return err;
12021ca2cfbcSDmitry Rokosov 
12031ca2cfbcSDmitry Rokosov 	err = devm_pm_runtime_enable(dev);
12041ca2cfbcSDmitry Rokosov 	if (err)
12051ca2cfbcSDmitry Rokosov 		return err;
12061ca2cfbcSDmitry Rokosov 
12071ca2cfbcSDmitry Rokosov 	pm_runtime_get_noresume(dev);
12081ca2cfbcSDmitry Rokosov 	pm_runtime_set_autosuspend_delay(dev, MSA311_PWR_SLEEP_DELAY_MS);
12091ca2cfbcSDmitry Rokosov 	pm_runtime_use_autosuspend(dev);
12101ca2cfbcSDmitry Rokosov 
12111ca2cfbcSDmitry Rokosov 	err = msa311_chip_init(msa311);
12121ca2cfbcSDmitry Rokosov 	if (err)
12131ca2cfbcSDmitry Rokosov 		return err;
12141ca2cfbcSDmitry Rokosov 
12151ca2cfbcSDmitry Rokosov 	indio_dev->modes = INDIO_DIRECT_MODE;
12161ca2cfbcSDmitry Rokosov 	indio_dev->channels = msa311_channels;
12171ca2cfbcSDmitry Rokosov 	indio_dev->num_channels = ARRAY_SIZE(msa311_channels);
12181ca2cfbcSDmitry Rokosov 	indio_dev->name = msa311->chip_name;
12191ca2cfbcSDmitry Rokosov 	indio_dev->info = &msa311_info;
12201ca2cfbcSDmitry Rokosov 
12211ca2cfbcSDmitry Rokosov 	err = devm_iio_triggered_buffer_setup(dev, indio_dev,
12221ca2cfbcSDmitry Rokosov 					      iio_pollfunc_store_time,
12231ca2cfbcSDmitry Rokosov 					      msa311_buffer_thread,
12241ca2cfbcSDmitry Rokosov 					      &msa311_buffer_setup_ops);
12251ca2cfbcSDmitry Rokosov 	if (err)
12261ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err,
12271ca2cfbcSDmitry Rokosov 				     "can't setup IIO trigger buffer\n");
12281ca2cfbcSDmitry Rokosov 
12291ca2cfbcSDmitry Rokosov 	err = msa311_setup_interrupts(msa311);
12301ca2cfbcSDmitry Rokosov 	if (err)
12311ca2cfbcSDmitry Rokosov 		return err;
12321ca2cfbcSDmitry Rokosov 
12331ca2cfbcSDmitry Rokosov 	pm_runtime_mark_last_busy(dev);
12341ca2cfbcSDmitry Rokosov 	pm_runtime_put_autosuspend(dev);
12351ca2cfbcSDmitry Rokosov 
12361ca2cfbcSDmitry Rokosov 	err = devm_iio_device_register(dev, indio_dev);
12371ca2cfbcSDmitry Rokosov 	if (err)
12381ca2cfbcSDmitry Rokosov 		return dev_err_probe(dev, err, "IIO device register failed\n");
12391ca2cfbcSDmitry Rokosov 
12401ca2cfbcSDmitry Rokosov 	return 0;
12411ca2cfbcSDmitry Rokosov }
12421ca2cfbcSDmitry Rokosov 
msa311_runtime_suspend(struct device * dev)12431ca2cfbcSDmitry Rokosov static int msa311_runtime_suspend(struct device *dev)
12441ca2cfbcSDmitry Rokosov {
12451ca2cfbcSDmitry Rokosov 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
12461ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
12471ca2cfbcSDmitry Rokosov 	int err;
12481ca2cfbcSDmitry Rokosov 
12491ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
12501ca2cfbcSDmitry Rokosov 	err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_SUSPEND);
12511ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
12521ca2cfbcSDmitry Rokosov 	if (err)
12531ca2cfbcSDmitry Rokosov 		dev_err(dev, "failed to power off device (%pe)\n",
12541ca2cfbcSDmitry Rokosov 			ERR_PTR(err));
12551ca2cfbcSDmitry Rokosov 
12561ca2cfbcSDmitry Rokosov 	return err;
12571ca2cfbcSDmitry Rokosov }
12581ca2cfbcSDmitry Rokosov 
msa311_runtime_resume(struct device * dev)12591ca2cfbcSDmitry Rokosov static int msa311_runtime_resume(struct device *dev)
12601ca2cfbcSDmitry Rokosov {
12611ca2cfbcSDmitry Rokosov 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
12621ca2cfbcSDmitry Rokosov 	struct msa311_priv *msa311 = iio_priv(indio_dev);
12631ca2cfbcSDmitry Rokosov 	int err;
12641ca2cfbcSDmitry Rokosov 
12651ca2cfbcSDmitry Rokosov 	mutex_lock(&msa311->lock);
12661ca2cfbcSDmitry Rokosov 	err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_NORMAL);
12671ca2cfbcSDmitry Rokosov 	mutex_unlock(&msa311->lock);
12681ca2cfbcSDmitry Rokosov 	if (err)
12691ca2cfbcSDmitry Rokosov 		dev_err(dev, "failed to power on device (%pe)\n",
12701ca2cfbcSDmitry Rokosov 			ERR_PTR(err));
12711ca2cfbcSDmitry Rokosov 
12721ca2cfbcSDmitry Rokosov 	return err;
12731ca2cfbcSDmitry Rokosov }
12741ca2cfbcSDmitry Rokosov 
12751ca2cfbcSDmitry Rokosov static DEFINE_RUNTIME_DEV_PM_OPS(msa311_pm_ops, msa311_runtime_suspend,
12761ca2cfbcSDmitry Rokosov 				 msa311_runtime_resume, NULL);
12771ca2cfbcSDmitry Rokosov 
12781ca2cfbcSDmitry Rokosov static const struct i2c_device_id msa311_i2c_id[] = {
12791ca2cfbcSDmitry Rokosov 	{ .name = "msa311" },
12801ca2cfbcSDmitry Rokosov 	{ }
12811ca2cfbcSDmitry Rokosov };
12821ca2cfbcSDmitry Rokosov MODULE_DEVICE_TABLE(i2c, msa311_i2c_id);
12831ca2cfbcSDmitry Rokosov 
12841ca2cfbcSDmitry Rokosov static const struct of_device_id msa311_of_match[] = {
12851ca2cfbcSDmitry Rokosov 	{ .compatible = "memsensing,msa311" },
12861ca2cfbcSDmitry Rokosov 	{ }
12871ca2cfbcSDmitry Rokosov };
12881ca2cfbcSDmitry Rokosov MODULE_DEVICE_TABLE(of, msa311_of_match);
12891ca2cfbcSDmitry Rokosov 
12901ca2cfbcSDmitry Rokosov static struct i2c_driver msa311_driver = {
12911ca2cfbcSDmitry Rokosov 	.driver = {
12921ca2cfbcSDmitry Rokosov 		.name = "msa311",
12931ca2cfbcSDmitry Rokosov 		.of_match_table = msa311_of_match,
12941ca2cfbcSDmitry Rokosov 		.pm = pm_ptr(&msa311_pm_ops),
12951ca2cfbcSDmitry Rokosov 	},
12967cf15f42SUwe Kleine-König 	.probe		= msa311_probe,
12971ca2cfbcSDmitry Rokosov 	.id_table	= msa311_i2c_id,
12981ca2cfbcSDmitry Rokosov };
12991ca2cfbcSDmitry Rokosov module_i2c_driver(msa311_driver);
13001ca2cfbcSDmitry Rokosov 
13011ca2cfbcSDmitry Rokosov MODULE_AUTHOR("Dmitry Rokosov <ddrokosov@sberdevices.ru>");
13021ca2cfbcSDmitry Rokosov MODULE_DESCRIPTION("MEMSensing MSA311 3-axis accelerometer driver");
13031ca2cfbcSDmitry Rokosov MODULE_LICENSE("GPL");
1304