iio: accel: adxl345: Add spi-3wire feature
Subsystems (4)
v7 cover iio: accel: adxl345: Add spi-3wire feature
Pass a function setup() as pointer from SPI/I2C specific modules to the core module. Implement setup() to pass the spi-3wire bus option, if declared in the device-tree.
In the core module then update data_format register configuration bits instead of overwriting it. The changes allow to remove a data_range field.
---
V1 -> V2: Split into spi-3wire and refactoring
V2 -> V3: Split further, focus on needed changesets
V3 -> V4: Drop "Remove single info instances";
split "Group bus configuration" into separat comment patch; reorder patch set
V4 -> V5: Refrase comments; Align comments to 75; rebuild FORMAT_MASK by
available flags; fix indention
V5 -> V6: Remove FORMAT_MASK by a local variable on call site;
Refrase comments;
Remove unneeded include
V6 -> V7: Restructure optional passing the setup() to core's probe()
Guarantee that initially a regmap_write() was called to init all bits to a defined state
- When a setup() e.g. for 3wire is passed, then call regmap_write() inside the setup(). In the following core's probe() has to call regmap_update()
- When NULL is passed, then call regmap_write() in core's probe()
- Refactoring: remove obvious comments and simplify code
Lothar Rubusch (8):
iio: accel: adxl345: Make data_range obsolete
iio: accel: adxl345: Group bus configuration
iio: accel: adxl345: Move defines to header
dt-bindings: iio: accel: adxl345: Add spi-3wire
iio: accel: adxl345: Pass function pointer to core
iio: accel: adxl345: Reorder probe initialization
iio: accel: adxl345: Add comment to probe
iio: accel: adxl345: Add spi-3wire option
.../bindings/iio/accel/adi,adxl345.yaml | 2 + drivers/iio/accel/adxl345.h | 36 +++++++- drivers/iio/accel/adxl345_core.c | 92 ++++++++++--------- drivers/iio/accel/adxl345_i2c.c | 2 +- drivers/iio/accel/adxl345_spi.c | 10 +- 5 files changed, 94 insertions(+), 48 deletions(-)
Signature · 1 line
2.25.1
PATCH v7 1/8 iio: accel: adxl345: Make data_range obsolete
Replace write() data_format by regmap_update_bits() to keep bus specific pre-configuration which might have happened before on the same register. The bus specific bits in data_format register then need to be masked out,
Remove the data_range field from the struct adxl345_data, because it is not used anymore.
--- drivers/iio/accel/adxl345_core.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/iio/accel/adxl345_core.c +13 -7
@@ -37,7 +37,11 @@#define ADXL345_POWER_CTL_MEASURE BIT(3)#define ADXL345_POWER_CTL_STANDBY 0x00 -#define ADXL345_DATA_FORMAT_FULL_RES BIT(3)/* Up to 13-bits resolution */ +#define ADXL345_DATA_FORMAT_RANGE GENMASK(1, 0)/* Set the g range */ +#define ADXL345_DATA_FORMAT_JUSTIFY BIT(2)/* Left-justified (MSB) mode */ +#define ADXL345_DATA_FORMAT_FULL_RES BIT(3)/* Up to 13-bits resolution */ +#define ADXL345_DATA_FORMAT_SELF_TEST BIT(7)/* Enable a self test */ +#define ADXL345_DATA_FORMAT_2G 0#define ADXL345_DATA_FORMAT_4G 1#define ADXL345_DATA_FORMAT_8G 2
@@ -48,7 +52,6 @@struct adxl345_data {const struct adxl345_chip_info *info;struct regmap *regmap; -u8 data_range; };#define ADXL345_CHANNEL (index, axis) { \
@@ -202,6 +205,10 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap)struct adxl345_data *data;struct iio_dev *indio_dev;u32 regval; +unsigned int data_format_mask = (ADXL345_DATA_FORMAT_RANGE | +ADXL345_DATA_FORMAT_JUSTIFY | +ADXL345_DATA_FORMAT_FULL_RES | +ADXL345_DATA_FORMAT_SELF_TEST );int ret; ret =regmap_read (regmap,ADXL345_REG_DEVID , ®val);
@@ -218,15 +225,14 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap) data =iio_priv (indio_dev); data->regmap = regmap; -/* Enable full-resolution mode */ - data->data_range =ADXL345_DATA_FORMAT_FULL_RES ; data->info =device_get_match_data (dev);if (!data->info)return -ENODEV ; - ret =regmap_write (data->regmap,ADXL345_REG_DATA_FORMAT , - data->data_range); -if (ret <0 ) +/* Enable full-resolution mode */ + ret =regmap_update_bits (regmap,ADXL345_REG_DATA_FORMAT , + data_format_mask,ADXL345_DATA_FORMAT_FULL_RES ); +if (ret)return dev_err_probe (dev, ret,"Failed to set data range\n" ); indio_dev->name = data->info->name;
Signature · 1 line
2.25.1
PATCH v7 2/8 iio: accel: adxl345: Group bus configuration
In reply to Lothar RubuschGroup the indio_dev initialization and bus configuration for improved readability.
--- drivers/iio/accel/adxl345_core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/iio/accel/adxl345_core.c +6 -6
@@ -229,18 +229,18 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap)if (!data->info)return -ENODEV ; -/* Enable full-resolution mode */ - ret =regmap_update_bits (regmap,ADXL345_REG_DATA_FORMAT , - data_format_mask,ADXL345_DATA_FORMAT_FULL_RES ); -if (ret) -return dev_err_probe (dev, ret,"Failed to set data range\n" ); - indio_dev->name = data->info->name; indio_dev->info = &adxl345_info; indio_dev->modes =INDIO_DIRECT_MODE ; indio_dev->channels = adxl345_channels; indio_dev->num_channels =ARRAY_SIZE (adxl345_channels); +/* Enable full-resolution mode */ + ret =regmap_update_bits (regmap,ADXL345_REG_DATA_FORMAT , + data_format_mask,ADXL345_DATA_FORMAT_FULL_RES ); +if (ret) +return dev_err_probe (dev, ret,"Failed to set data range\n" ); +/* Enable measurement mode */ ret =adxl345_powerup (data->regmap);if (ret <0 )
Signature · 1 line
2.25.1
PATCH v7 3/8 iio: accel: adxl345: Move defines to header
In reply to Lothar RubuschMove defines from core to the header file. Keep the defines block together in one location.
--- drivers/iio/accel/adxl345.h | 32 ++++++++++++++++++++++++++++++++ drivers/iio/accel/adxl345_core.c | 32 -------------------------------- 2 files changed, 32 insertions(+), 32 deletions(-)
drivers/iio/accel/adxl345.h +32 -0
@@ -8,6 +8,38 @@#ifndef _ADXL345_H_#define _ADXL345_H_ +#define ADXL345_REG_DEVID 0x00 +#define ADXL345_REG_OFSX 0x1E +#define ADXL345_REG_OFSY 0x1F +#define ADXL345_REG_OFSZ 0x20 +#define ADXL345_REG_OFS_AXIS (index) (ADXL345_REG_OFSX + (index)) +#define ADXL345_REG_BW_RATE 0x2C +#define ADXL345_REG_POWER_CTL 0x2D +#define ADXL345_REG_DATA_FORMAT 0x31 +#define ADXL345_REG_DATAX0 0x32 +#define ADXL345_REG_DATAY0 0x34 +#define ADXL345_REG_DATAZ0 0x36 +#define ADXL345_REG_DATA_AXIS (index) \ + (ADXL345_REG_DATAX0 + (index) * sizeof(__le16)) + +#define ADXL345_BW_RATE GENMASK(3, 0) +#define ADXL345_BASE_RATE_NANO_HZ 97656250LL + +#define ADXL345_POWER_CTL_MEASURE BIT(3) +#define ADXL345_POWER_CTL_STANDBY 0x00 + +#define ADXL345_DATA_FORMAT_RANGE GENMASK(1, 0)/* Set the g range */ +#define ADXL345_DATA_FORMAT_JUSTIFY BIT(2)/* Left-justified (MSB) mode */ +#define ADXL345_DATA_FORMAT_FULL_RES BIT(3)/* Up to 13-bits resolution */ +#define ADXL345_DATA_FORMAT_SELF_TEST BIT(7)/* Enable a self test */ + +#define ADXL345_DATA_FORMAT_2G 0 +#define ADXL345_DATA_FORMAT_4G 1 +#define ADXL345_DATA_FORMAT_8G 2 +#define ADXL345_DATA_FORMAT_16G 3 + +#define ADXL345_DEVID 0xE5 +/* * In full-resolution mode, scale factor is maintained at ~4 mg/LSB * in all g ranges.
drivers/iio/accel/adxl345_core.c +0 -32
@@ -17,38 +17,6 @@#include "adxl345.h" -#define ADXL345_REG_DEVID 0x00 -#define ADXL345_REG_OFSX 0x1e -#define ADXL345_REG_OFSY 0x1f -#define ADXL345_REG_OFSZ 0x20 -#define ADXL345_REG_OFS_AXIS (index) (ADXL345_REG_OFSX + (index)) -#define ADXL345_REG_BW_RATE 0x2C -#define ADXL345_REG_POWER_CTL 0x2D -#define ADXL345_REG_DATA_FORMAT 0x31 -#define ADXL345_REG_DATAX0 0x32 -#define ADXL345_REG_DATAY0 0x34 -#define ADXL345_REG_DATAZ0 0x36 -#define ADXL345_REG_DATA_AXIS (index) \ - (ADXL345_REG_DATAX0 + (index) * sizeof(__le16)) - -#define ADXL345_BW_RATE GENMASK(3, 0) -#define ADXL345_BASE_RATE_NANO_HZ 97656250LL - -#define ADXL345_POWER_CTL_MEASURE BIT(3) -#define ADXL345_POWER_CTL_STANDBY 0x00 - -#define ADXL345_DATA_FORMAT_RANGE GENMASK(1, 0)/* Set the g range */ -#define ADXL345_DATA_FORMAT_JUSTIFY BIT(2)/* Left-justified (MSB) mode */ -#define ADXL345_DATA_FORMAT_FULL_RES BIT(3)/* Up to 13-bits resolution */ -#define ADXL345_DATA_FORMAT_SELF_TEST BIT(7)/* Enable a self test */ - -#define ADXL345_DATA_FORMAT_2G 0 -#define ADXL345_DATA_FORMAT_4G 1 -#define ADXL345_DATA_FORMAT_8G 2 -#define ADXL345_DATA_FORMAT_16G 3 - -#define ADXL345_DEVID 0xE5 -struct adxl345_data {const struct adxl345_chip_info *info;struct regmap *regmap;
Signature · 1 line
2.25.1
PATCH v7 4/8 dt-bindings: iio: accel: adxl345: Add spi-3wire
In reply to Lothar RubuschAdd spi-3wire because the device allows to be configured for spi 3-wire communication.
--- Documentation/devicetree/bindings/iio/accel/adi,adxl345.yaml | 2 ++ 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/iio/accel/adi,adxl345.yaml +2 -0
@@ -32,6 +32,8 @@ properties: spi-cpol: true + spi-3wire: true + interrupts: maxItems: 1
Signature · 1 line
2.25.1
PATCH v7 5/8 iio: accel: adxl345: Pass function pointer to core
In reply to Lothar RubuschProvide a way for bus specific pre-configuration by adding a function pointer argument to the driver core's probe() function, and keep the driver core implementation bus independent.
In case NULL was passed, a regmap_write() shall initialize all bits of the data_format register, else regmap_update() is used. In this way spi and i2c are covered.
--- drivers/iio/accel/adxl345.h | 3 ++- drivers/iio/accel/adxl345_core.c | 32 +++++++++++++++++++++++++------- drivers/iio/accel/adxl345_i2c.c | 2 +- drivers/iio/accel/adxl345_spi.c | 2 +- 4 files changed, 29 insertions(+), 10 deletions(-)
drivers/iio/accel/adxl345.h +2 -1
@@ -60,6 +60,7 @@ struct adxl345_chip_info { int uscale; }; -int adxl345_core_probe (struct device *dev,struct regmap *regmap); +int adxl345_core_probe (struct device *dev,struct regmap *regmap, +int (*setup)(struct device *,struct regmap *));#endif /* _ADXL345_H_ */
drivers/iio/accel/adxl345_core.c +25 -7
@@ -168,7 +168,8 @@ static void adxl345_powerdown(void *regmap)regmap_write (regmap,ADXL345_REG_POWER_CTL ,ADXL345_POWER_CTL_STANDBY ); } -int adxl345_core_probe (struct device *dev,struct regmap *regmap) +int adxl345_core_probe (struct device *dev,struct regmap *regmap, +int (*setup)(struct device *,struct regmap *)) {struct adxl345_data *data;struct iio_dev *indio_dev;
@@ -179,6 +180,29 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap) ADXL345_DATA_FORMAT_SELF_TEST);int ret; +if (setup) { +/* Perform optional initial bus specific configuration */ + ret =setup (dev, regmap); +if (ret) +return ret; + +/* Enable full-resolution mode */ + ret =regmap_update_bits (regmap,ADXL345_REG_DATA_FORMAT , + data_format_mask, +ADXL345_DATA_FORMAT_FULL_RES ); +if (ret) +return dev_err_probe (dev, ret, +"Failed to set data range\n" ); + + }else { +/* Enable full-resolution mode (init all data_format bits) */ + ret =regmap_write (regmap,ADXL345_REG_DATA_FORMAT , +ADXL345_DATA_FORMAT_FULL_RES ); +if (ret) +return dev_err_probe (dev, ret, +"Failed to set data range\n" ); + } + ret =regmap_read (regmap,ADXL345_REG_DEVID , ®val);if (ret <0 )return dev_err_probe (dev, ret,"Error reading device ID\n" );
@@ -203,12 +227,6 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap) indio_dev->channels = adxl345_channels; indio_dev->num_channels =ARRAY_SIZE (adxl345_channels); -/* Enable full-resolution mode */ - ret =regmap_update_bits (regmap,ADXL345_REG_DATA_FORMAT , - data_format_mask,ADXL345_DATA_FORMAT_FULL_RES ); -if (ret) -return dev_err_probe (dev, ret,"Failed to set data range\n" ); -/* Enable measurement mode */ ret =adxl345_powerup (data->regmap);if (ret <0 )
drivers/iio/accel/adxl345_i2c.c +1 -1
@@ -27,7 +27,7 @@ static int adxl345_i2c_probe(struct i2c_client *client)if (IS_ERR (regmap))return dev_err_probe (&client->dev,PTR_ERR (regmap),"Error initializing regmap\n" ); -return adxl345_core_probe (&client->dev, regmap); +return adxl345_core_probe (&client->dev, regmap,NULL ); }static const struct adxl345_chip_info adxl345_i2c_info = {
drivers/iio/accel/adxl345_spi.c +1 -1
@@ -33,7 +33,7 @@ static int adxl345_spi_probe(struct spi_device *spi)if (IS_ERR (regmap))return dev_err_probe (&spi->dev,PTR_ERR (regmap),"Error initializing regmap\n" ); -return adxl345_core_probe (&spi->dev, regmap); +return adxl345_core_probe (&spi->dev, regmap,NULL ); }static const struct adxl345_chip_info adxl345_spi_info = {
Signature · 1 line
2.25.1
PATCH v7 6/8 iio: accel: adxl345: Reorder probe initialization
In reply to Lothar RubuschBring indio_dev, setup() and data initialization to begin of the probe() function to increase readability. Access members through data pointer to assure implicitely the driver's data instance is correctly initialized and functional.
--- drivers/iio/accel/adxl345_core.c | 40 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-)
drivers/iio/accel/adxl345_core.c +20 -20
@@ -180,14 +180,30 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap, ADXL345_DATA_FORMAT_SELF_TEST);int ret; + indio_dev =devm_iio_device_alloc (dev,sizeof (*data)); +if (!indio_dev) +return -ENOMEM ; + + data =iio_priv (indio_dev); + data->regmap = regmap; + data->info =device_get_match_data (dev); +if (!data->info) +return -ENODEV ; + + indio_dev->name = data->info->name; + indio_dev->info = &adxl345_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->channels = adxl345_channels; + indio_dev->num_channels =ARRAY_SIZE (adxl345_channels); +if (setup) {/* Perform optional initial bus specific configuration */ - ret =setup (dev, regmap); + ret =setup (dev, data->regmap);if (ret)return ret;/* Enable full-resolution mode */ - ret =regmap_update_bits (regmap,ADXL345_REG_DATA_FORMAT , + ret =regmap_update_bits (data->regmap,ADXL345_REG_DATA_FORMAT , data_format_mask,ADXL345_DATA_FORMAT_FULL_RES );if (ret)
@@ -196,14 +212,14 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap, }else {/* Enable full-resolution mode (init all data_format bits) */ - ret =regmap_write (regmap,ADXL345_REG_DATA_FORMAT , + ret =regmap_write (data->regmap,ADXL345_REG_DATA_FORMAT ,ADXL345_DATA_FORMAT_FULL_RES );if (ret)return dev_err_probe (dev, ret,"Failed to set data range\n" ); } - ret =regmap_read (regmap,ADXL345_REG_DEVID , ®val); + ret =regmap_read (data->regmap,ADXL345_REG_DEVID , ®val);if (ret <0 )return dev_err_probe (dev, ret,"Error reading device ID\n" );
@@ -211,22 +227,6 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,return dev_err_probe (dev, -ENODEV ,"Invalid device ID: %x, expected %x\n" , regval,ADXL345_DEVID ); - indio_dev =devm_iio_device_alloc (dev,sizeof (*data)); -if (!indio_dev) -return -ENOMEM ; - - data =iio_priv (indio_dev); - data->regmap = regmap; - data->info =device_get_match_data (dev); -if (!data->info) -return -ENODEV ; - - indio_dev->name = data->info->name; - indio_dev->info = &adxl345_info; - indio_dev->modes =INDIO_DIRECT_MODE ; - indio_dev->channels = adxl345_channels; - indio_dev->num_channels =ARRAY_SIZE (adxl345_channels); -/* Enable measurement mode */ ret =adxl345_powerup (data->regmap);if (ret <0 )
Signature · 1 line
2.25.1
PATCH v7 7/8 iio: accel: adxl345: Add comment to probe
In reply to Lothar RubuschAdd a comment to the probe() function to describe the passed function pointer argument in particular.
--- drivers/iio/accel/adxl345_core.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/iio/accel/adxl345_core.c +10 -0
@@ -168,6 +168,16 @@ static void adxl345_powerdown(void *regmap)regmap_write (regmap,ADXL345_REG_POWER_CTL ,ADXL345_POWER_CTL_STANDBY ); } +/** +* adxl345_core_probe() - probe and setup for the adxl345 accelerometer, +* also covers the adlx375 accelerometer +* @dev: Driver model representation of the device +* @regmap: Regmap instance for the device +* @setup: Setup routine to be executed right before the standard device +* setup +* +* Return: 0 on success, negative errno on error +*/ int adxl345_core_probe (struct device *dev,struct regmap *regmap,int (*setup)(struct device *,struct regmap *)) {
Signature · 1 line
2.25.1
PATCH v7 8/8 iio: accel: adxl345: Add spi-3wire option
In reply to Lothar RubuschAdd a setup function implementation to the spi module to enable spi-3wire when specified in the device-tree. If spi-3wire is not specified in the device-tree, NULL is returned as bus pre-initialization. This behavior is identical to the i2c initialization, hence the default initialization.
--- drivers/iio/accel/adxl345.h | 1 + drivers/iio/accel/adxl345_spi.c | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-)
drivers/iio/accel/adxl345.h +1 -0
@@ -31,6 +31,7 @@#define ADXL345_DATA_FORMAT_RANGE GENMASK(1, 0)/* Set the g range */ #define ADXL345_DATA_FORMAT_JUSTIFY BIT(2)/* Left-justified (MSB) mode */ #define ADXL345_DATA_FORMAT_FULL_RES BIT(3)/* Up to 13-bits resolution */ +#define ADXL345_DATA_FORMAT_SPI_3WIRE BIT(6)/* 3-wire SPI mode */ #define ADXL345_DATA_FORMAT_SELF_TEST BIT(7)/* Enable a self test */ #define ADXL345_DATA_FORMAT_2G 0
drivers/iio/accel/adxl345_spi.c +9 -1
@@ -20,6 +20,11 @@ static const struct regmap_config adxl345_spi_regmap_config = { .read_flag_mask = BIT(7) | BIT(6), }; +static int adxl345_spi_setup (struct device *dev,struct regmap *regmap) +{ +return regmap_write (regmap,ADXL345_REG_DATA_FORMAT ,ADXL345_DATA_FORMAT_SPI_3WIRE ); +} +static int adxl345_spi_probe (struct spi_device *spi) {struct regmap *regmap;
@@ -33,7 +38,10 @@ static int adxl345_spi_probe(struct spi_device *spi)if (IS_ERR (regmap))return dev_err_probe (&spi->dev,PTR_ERR (regmap),"Error initializing regmap\n" ); -return adxl345_core_probe (&spi->dev, regmap,NULL ); +if (spi->mode &SPI_3WIRE ) +return adxl345_core_probe (&spi->dev, regmap, adxl345_spi_setup); +else +return adxl345_core_probe (&spi->dev, regmap,NULL ); }static const struct adxl345_chip_info adxl345_spi_info = {
Signature · 1 line
2.25.1
Jonathan Cameron
maintainerQuoted 8 lines · On Mon, 1 Apr 2024 19:48:58 +0000 Lothar Rubusch <[email protected]> wrote:
Pass a function
setup()as pointer from SPI/I2C specific modules to the core module. Implementsetup()to pass the spi-3wire bus option, if declared in the device-tree.In the core module then update
data_formatregister configuration bits instead of overwriting it. The changes allow to remove adata_rangefield.Signed-off-by Lothar Rubusch <[email protected]>
Series applied to the togreg branch of iio.git and pushed out initially as testing to let 0-day work it's magic (find build issues for us!)
I'll push it out for linux-next to pick up sometime in next week (ish)
Jonathan
Quoted 38 lines
---
V1 -> V2: Split into spi-3wire and refactoring
V2 -> V3: Split further, focus on needed changesets
V3 -> V4: Drop "Remove single info instances";
split "Group bus configuration" into separat comment patch; reorder patch set
V4 -> V5: Refrase comments; Align comments to 75; rebuildFORMAT_MASKby
available flags; fix indention
V5 -> V6: RemoveFORMAT_MASKby a local variable on call site;
Refrase comments;
Remove unneeded include
V6 -> V7: Restructure optional passing thesetup()to core'sprobe()
Guarantee that initially aregmap_write()was called to init all bits to a defined state
- When asetup()e.g. for 3wire is passed, then callregmap_write()inside thesetup(). In the following core'sprobe()has to callregmap_update()
- When NULL is passed, then callregmap_write()in core'sprobe()
- Refactoring: remove obvious comments and simplify codeLothar Rubusch (8):
iio: accel: adxl345: Makedata_rangeobsolete
iio: accel: adxl345: Group bus configuration
iio: accel: adxl345: Move defines to header
dt-bindings: iio: accel: adxl345: Add spi-3wire
iio: accel: adxl345: Pass function pointer to core
iio: accel: adxl345: Reorder probe initialization
iio: accel: adxl345: Add comment to probe
iio: accel: adxl345: Add spi-3wire option.../bindings/iio/accel/adi,adxl345.yaml | 2 + drivers/iio/accel/adxl345.h | 36 +++++++- drivers/iio/accel/adxl345_core.c | 92 ++++++++++--------- drivers/iio/accel/adxl345_i2c.c | 2 +- drivers/iio/accel/adxl345_spi.c | 10 +- 5 files changed, 94 insertions(+), 48 deletions(-)