Support ROHM KX134ACR-LBZ
Subsystems (4)
Matti Vaittinen
maintainercover Support ROHM KX134ACR-LBZ
Patch series introducing support for ROHM KX134ACR-LBZ accelerometer
This series adds support for ROHM KX124ACR-LBZ. The KX134ACR-LBZ is almost identical to the KX132ACR-LBZ. The differencies visible to the driver are different g-ranges and the "Who am I" -identification register's value.
This series does also convert parts of the GTS helpers and the kx022a driver to use __cleanup - based scoped free/unlock operations.
The patch 1/5 contains GTS helper change, which is independent from the rest of the series. It can be applied/rejected independently.
Patch 2/5 changes kx022a to use scoped mutexes. It can also be applied as an independent improvement even if the kx134acr-lbz support was not added.
Patches 3-5/5 are all related to supporting kx134acr-lbz. 3/5 adding mechanisms for supporting sensors with different g-ranges, 4/5 being the binding doc and 5/5 bringing the actual support in the driver.
---
Matti Vaittinen (5):
iio: gts: Simplify using __free
iio: accel: kx022a: Use cleanup.h helpers
iio: accel: kx022a: Support ICs with different G-ranges
dt-bindings: ROHM KX134ACR-LBZ
iio: kx022a: Support ROHM KX134ACR-LBZ
.../bindings/iio/accel/kionix,kx022a.yaml | 4 +- drivers/iio/accel/kionix-kx022a-i2c.c | 2 + drivers/iio/accel/kionix-kx022a-spi.c | 2 + drivers/iio/accel/kionix-kx022a.c | 129 +++++++++++------- drivers/iio/accel/kionix-kx022a.h | 4 + drivers/iio/industrialio-gts-helper.c | 17 +-- 6 files changed, 98 insertions(+), 60 deletions(-)
base-commit: 20fd1383cd616d61b2a79967da1221dc6cfb8430
Signature · 1 line
2.47.0
Matti Vaittinen
maintainerPATCH 1/5 iio: gts: Simplify using __free
The error path in the gain_to_scaletables() uses goto for unwinding an allocation on failure. This can be slightly simplified by using the automated free when exiting the scope.
Use __free(kfree) and drop the goto based error handling.
--- drivers/iio/industrialio-gts-helper.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/iio/industrialio-gts-helper.c +7 -10
@@ -4,6 +4,7 @@* Copyright (c) 2023 Matti Vaittinen <[email protected]> */ +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/errno.h> #include <linux/export.h>
@@ -168,7 +169,7 @@ static int iio_gts_gain_cmp(const void *a, const void *b)static int gain_to_scaletables (struct iio_gts *gts,int **gains,int **scales) {int ret, i, j, new_idx, time_idx; -int *all_gains; + int *all_gains __free(kfree) = NULL;size_t gain_bytes;for (i =0 ; i < gts->num_itime; i++) {
@@ -232,10 +233,9 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) gts->avail_all_scales_table =kcalloc (new_idx,2 *sizeof (int ),GFP_KERNEL ); -if (!gts->avail_all_scales_table) { - ret = -ENOMEM ; - goto free_out; - } +if (!gts->avail_all_scales_table) +return -ENOMEM ; + gts->num_avail_all_scales = new_idx;for (i =0 ; i < gts->num_avail_all_scales; i++) {
@@ -246,14 +246,11 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)if (ret) {kfree (gts->avail_all_scales_table); gts->num_avail_all_scales =0 ; - goto free_out; +return ret; } } -free_out: -kfree (all_gains); - -return ret; +return 0 ; }/**
Signature · 1 line
2.47.0
Matti Vaittinen
maintainerPATCH 2/5 iio: accel: kx022a: Use cleanup.h helpers
In reply to Matti VaittinenA few functions in KX022A need to use mutex for protecting the enabling/disabling of the measurement while configurations are being made. Some of the functions can be slightly simplified by using the __cleanup based scoped mutexes, which allows dropping the goto based unlocking at error path.
Simplify error paths using guard(mutex).
--- drivers/iio/accel/kionix-kx022a.c | 61 ++++++++++++------------------- 1 file changed, 23 insertions(+), 38 deletions(-)
drivers/iio/accel/kionix-kx022a.c +23 -38
@@ -5,6 +5,7 @@* ROHM/KIONIX accelerometer driver */ +#include <linux/cleanup.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/interrupt.h>
@@ -448,7 +449,7 @@ static void kx022a_reg2scale(unsigned int val, unsigned int *val1, *val2 = kx022a_scale_table[val][1 ]; } -static int kx022a_turn_on_off_unlocked (struct kx022a_data *data,bool on) +static int __kx022a_turn_on_off (struct kx022a_data *data,bool on) {int ret;
@@ -469,7 +470,7 @@ static int kx022a_turn_off_lock(struct kx022a_data *data)int ret;mutex_lock (&data->mutex); - ret =kx022a_turn_on_off_unlocked (data, false); + ret =__kx022a_turn_on_off (data, false);if (ret)mutex_unlock (&data->mutex);
@@ -480,7 +481,7 @@ static int kx022a_turn_on_unlock(struct kx022a_data *data) {int ret; - ret =kx022a_turn_on_off_unlocked (data, true); + ret =__kx022a_turn_on_off (data, true);mutex_unlock (&data->mutex);return ret;
@@ -912,18 +913,19 @@ static int kx022a_fifo_disable(struct kx022a_data *data) {int ret =0 ; - ret =kx022a_turn_off_lock (data); +guard (mutex)(&data->mutex); + ret =__kx022a_turn_on_off (data, false);if (ret)return ret; ret =regmap_clear_bits (data->regmap, data->ien_reg,KX022A_MASK_WMI );if (ret) - goto unlock_out; +return ret; ret =regmap_clear_bits (data->regmap, data->chip_info->buf_cntl2,KX022A_MASK_BUF_EN );if (ret) - goto unlock_out; +return ret; data->state &= ~KX022A_STATE_FIFO ;
@@ -931,12 +933,7 @@ static int kx022a_fifo_disable(struct kx022a_data *data)kfree (data->fifo_buffer); -return kx022a_turn_on_unlock (data); - -unlock_out: -mutex_unlock (&data->mutex); - -return ret; +return __kx022a_turn_on_off (data, true); }static int kx022a_buffer_predisable (struct iio_dev *idev)
@@ -959,33 +956,29 @@ static int kx022a_fifo_enable(struct kx022a_data *data)if (!data->fifo_buffer)return -ENOMEM ; - ret =kx022a_turn_off_lock (data); +guard (mutex)(&data->mutex); + ret =__kx022a_turn_on_off (data, false);if (ret)return ret;/* Update watermark to HW */ ret =kx022a_fifo_set_wmi (data);if (ret) - goto unlock_out; +return ret;/* Enable buffer */ ret =regmap_set_bits (data->regmap, data->chip_info->buf_cntl2,KX022A_MASK_BUF_EN );if (ret) - goto unlock_out; +return ret; data->state |=KX022A_STATE_FIFO ; ret =regmap_set_bits (data->regmap, data->ien_reg,KX022A_MASK_WMI );if (ret) - goto unlock_out; - -return kx022a_turn_on_unlock (data); - -unlock_out: -mutex_unlock (&data->mutex); +return ret; -return ret; +return __kx022a_turn_on_off (data, true); }static int kx022a_buffer_postenable (struct iio_dev *idev)
@@ -1053,7 +1046,7 @@ static irqreturn_t kx022a_irq_thread_handler(int irq, void *private)struct kx022a_data *data =iio_priv (idev);irqreturn_t ret =IRQ_NONE ; -mutex_lock (&data->mutex); +guard (mutex)(&data->mutex);if (data->trigger_enabled) {iio_trigger_poll_nested (data->trig);
@@ -1068,8 +1061,6 @@ static irqreturn_t kx022a_irq_thread_handler(int irq, void *private) ret =IRQ_HANDLED ; } -mutex_unlock (&data->mutex); -return ret; }
@@ -1079,32 +1070,26 @@ static int kx022a_trigger_set_state(struct iio_trigger *trig,struct kx022a_data *data =iio_trigger_get_drvdata (trig);int ret =0 ; -mutex_lock (&data->mutex); +guard (mutex)(&data->mutex);if (data->trigger_enabled == state) - goto unlock_out; +return 0 ;if (data->state &KX022A_STATE_FIFO ) {dev_warn (data->dev,"Can't set trigger when FIFO enabled\n" ); - ret = -EBUSY ; - goto unlock_out; +return -EBUSY ; } - ret =kx022a_turn_on_off_unlocked (data, false); + ret =__kx022a_turn_on_off (data, false);if (ret) - goto unlock_out; +return ret; data->trigger_enabled = state; ret =kx022a_set_drdy_irq (data, state);if (ret) - goto unlock_out; - - ret =kx022a_turn_on_off_unlocked (data, true); - -unlock_out: -mutex_unlock (&data->mutex); +return ret; -return ret; +return __kx022a_turn_on_off (data, true); }static const struct iio_trigger_ops kx022a_trigger_ops = {
Signature · 1 line
2.47.0
Matti Vaittinen
maintainerPATCH 3/5 iio: accel: kx022a: Support ICs with different G-ranges
In reply to Matti VaittinenThe register interface of the ROHM KX134ACR-LBZ accelerometer is almost identical to the KX132ACR-LBZ. Main difference between these accelerometers is that the KX134ACR-LBZ supports G-ranges +/- 8, 16, 32 and 64G. All the other sensors supported by the kx022a driver can measure +/- 2, 4, 8 and 16G.
Prepare supporting the KX134ACR-LBZ with different G-ranges by storing a pointer to the scale tables in IC specific structure.
--- drivers/iio/accel/kionix-kx022a.c | 32 ++++++++++++++++++++----------- drivers/iio/accel/kionix-kx022a.h | 2 ++ 2 files changed, 23 insertions(+), 11 deletions(-)
drivers/iio/accel/kionix-kx022a.c +21 -11
@@ -413,6 +413,8 @@ static int kx022a_read_avail(struct iio_dev *indio_dev, const int **vals, int *type, int *length, long mask) { + struct kx022a_data *data = iio_priv(indio_dev); +switch (mask) {case IIO_CHAN_INFO_SAMP_FREQ : *vals = (const int *)kx022a_accel_samp_freq_table;
@@ -421,9 +423,8 @@ static int kx022a_read_avail(struct iio_dev *indio_dev, *type =IIO_VAL_INT_PLUS_MICRO ;return IIO_AVAIL_LIST ;case IIO_CHAN_INFO_SCALE : - *vals = (const int *)kx022a_scale_table; - *length =ARRAY_SIZE (kx022a_scale_table) * -ARRAY_SIZE (kx022a_scale_table[0 ]); + *vals = (const int *)data->chip_info->scale_table; + *length = data->chip_info->scale_table_size; *type =IIO_VAL_INT_PLUS_NANO ;return IIO_AVAIL_LIST ;default :
@@ -439,14 +440,14 @@ static void kx022a_reg2freq(unsigned int val, int *val1, int *val2) *val2 = kx022a_accel_samp_freq_table[val &KX022A_MASK_ODR ][1 ]; } -static void kx022a_reg2scale (unsigned int val,unsigned int *val1, -unsigned int *val2) +static void kx022a_reg2scale (struct kx022a_data *data,unsigned int val, +unsigned int *val1,unsigned int *val2) { val &=KX022A_MASK_GSEL ; val >>=KX022A_GSEL_SHIFT ; - *val1 = kx022a_scale_table[val][0 ]; - *val2 = kx022a_scale_table[val][1 ]; + *val1 = data->chip_info->scale_table[val][0 ]; + *val2 = data->chip_info->scale_table[val][1 ]; }static int __kx022a_turn_on_off (struct kx022a_data *data,bool on)
@@ -544,11 +545,11 @@ static int kx022a_write_raw(struct iio_dev *idev,kx022a_turn_on_unlock (data);break ;case IIO_CHAN_INFO_SCALE : - n =ARRAY_SIZE (kx022a_scale_table); + n = data->chip_info->scale_table_size /2 ;while (n-- >0 ) -if (val == kx022a_scale_table[n][0 ] && - val2 == kx022a_scale_table[n][1 ]) +if (val == data->chip_info->scale_table[n][0 ] && + val2 == data->chip_info->scale_table[n][1 ])break ;if (n <0 ) { ret = -EINVAL ;
@@ -643,7 +644,7 @@ static int kx022a_read_raw(struct iio_dev *idev,if (ret <0 )return ret; -kx022a_reg2scale (regval, val, val2); +kx022a_reg2scale (data, regval, val, val2);return IIO_VAL_INT_PLUS_NANO ; }
@@ -1143,6 +1144,9 @@ const struct kx022a_chip_info kx022a_chip_info = { .regmap_config = &kx022a_regmap_config, .channels = kx022a_channels, .num_channels = ARRAY_SIZE(kx022a_channels), + .scale_table = kx022a_scale_table, + .scale_table_size =ARRAY_SIZE (kx022a_scale_table) * + ARRAY_SIZE(kx022a_scale_table[0]), .fifo_length = KX022A_FIFO_LENGTH, .who = KX022A_REG_WHO, .id = KX022A_ID,
@@ -1168,6 +1172,9 @@ const struct kx022a_chip_info kx132_chip_info = { .regmap_config = &kx132_regmap_config, .channels = kx132_channels, .num_channels = ARRAY_SIZE(kx132_channels), + .scale_table = kx022a_scale_table, + .scale_table_size =ARRAY_SIZE (kx022a_scale_table) * + ARRAY_SIZE(kx022a_scale_table[0]), .fifo_length = KX132_FIFO_LENGTH, .who = KX132_REG_WHO, .id = KX132_ID,
@@ -1201,6 +1208,9 @@ const struct kx022a_chip_info kx132acr_chip_info = { .regmap_config = &kx022a_regmap_config, .channels = kx022a_channels, .num_channels = ARRAY_SIZE(kx022a_channels), + .scale_table = kx022a_scale_table, + .scale_table_size =ARRAY_SIZE (kx022a_scale_table) * + ARRAY_SIZE(kx022a_scale_table[0]), .fifo_length = KX022A_FIFO_LENGTH, .who = KX022A_REG_WHO, .id = KX132ACR_LBZ_ID,
drivers/iio/accel/kionix-kx022a.h +2 -0
@@ -161,6 +161,8 @@ struct kx022a_data;struct kx022a_chip_info {const char *name;const struct regmap_config *regmap_config; +const int (*scale_table)[2 ]; +const int scale_table_size;const struct iio_chan_spec *channels;unsigned int num_channels;unsigned int fifo_length;
Signature · 1 line
2.47.0
Matti Vaittinen
maintainerPATCH 4/5 dt-bindings: ROHM KX134ACR-LBZ
In reply to Matti VaittinenAdd compatible and information for ROHM KX134ACR-LBZ accelerometer.
--- .../devicetree/bindings/iio/accel/kionix,kx022a.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/accel/kionix,kx022a.yaml +3 -1
@@ -11,7 +11,8 @@ maintainers: description: | KX022A, KX132ACR-LBZ and KX132-1211 are 3-axis accelerometers supporting - +/- 2G, 4G, 8G and 16G ranges, variable output data-rates and a + +/- 2G, 4G, 8G and 16G ranges. The KX134ACR-LBZ supports +/- 8G, 16G, + 32G and 64G. All the sensors also have variable output data-rates and a hardware-fifo buffering. These accelerometers can be accessed either via I2C or SPI.
@@ -21,6 +22,7 @@ properties: - kionix,kx022a - kionix,kx132-1211 - rohm,kx132acr-lbz + - rohm,kx134acr-lbz reg: maxItems: 1
Signature · 1 line
2.47.0
Re: [PATCH 4/5] dt-bindings: ROHM KX134ACR-LBZ
Quoted 1 line · Matti Vaittinen
Add compatible and information for ROHM KX134ACR-LBZ accelerometer.
The commit message mention what makes this device incompatible - but I'll let you away with it the description below contains it.
Quoted 31 lines
Signed-off-by Matti Vaittinen <[email protected]>--- .../devicetree/bindings/iio/accel/kionix,kx022a.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)Documentation/devicetree/bindings/iio/accel/kionix,kx022a.yaml +3
-1@@ -11,7 +11,8 @@ maintainers: description: | KX022A, KX132ACR-LBZ and KX132-1211 are 3-axis accelerometers supporting - +/- 2G, 4G, 8G and 16G ranges, variable output data-rates and a + +/- 2G, 4G, 8G and 16G ranges. The KX134ACR-LBZ supports +/- 8G, 16G, + 32G and 64G. All the sensors also have variable output data-rates and a hardware-fifo buffering. These accelerometers can be accessed either via I2C or SPI.@@ -21,6 +22,7 @@ properties: - kionix,kx022a - kionix,kx132-1211 - rohm,kx132acr-lbz + - rohm,kx134acr-lbz reg: maxItems: 1Signature · 1 line
2.47.0
Matti Vaittinen
maintainerRe: [PATCH 4/5] dt-bindings: ROHM KX134ACR-LBZ
Quoted 6 lines · Conor Dooley
Quoted 1 line · Matti Vaittinen
Add compatible and information for ROHM KX134ACR-LBZ accelerometer.
The commit message mention what makes this device incompatible - but I'll let you away with it the description below contains it.
Acked-by Conor Dooley <[email protected]>
Thanks for pointing it out Conor. I agree the commit message should describe the KX134ACR-LBZ a bit better. I'll improve the commit message to include the g-range difference if I re-spin this series - and try to remember this if I get to support also some other variants :) A very good head's up for me as I didn't even think of describing the differences in commit message!
Yours,
-- Matti
Matti Vaittinen
maintainerPATCH 5/5 iio: kx022a: Support ROHM KX134ACR-LBZ
In reply to Matti VaittinenThe register interface of the ROHM KX134ACR-LBZ accelerometer is almost identical to the KX132ACR-LBZ. The main difference between these accelerometers is that the KX134ACR-LBZ supports different G-ranges. The driver can model this by informing different scale to users. Also, the content of the "who_am_I" register is different.
Add an ID and scales for the KX134ACR-LBZ.
--- drivers/iio/accel/kionix-kx022a-i2c.c | 2 ++ drivers/iio/accel/kionix-kx022a-spi.c | 2 ++ drivers/iio/accel/kionix-kx022a.c | 36 +++++++++++++++++++++++++++ drivers/iio/accel/kionix-kx022a.h | 2 ++ 4 files changed, 42 insertions(+)
drivers/iio/accel/kionix-kx022a-i2c.c +2 -0
@@ -39,6 +39,7 @@ static const struct i2c_device_id kx022a_i2c_id[] = { { .name = "kx022a", .driver_data = (kernel_ulong_t)&kx022a_chip_info }, { .name = "kx132-1211", .driver_data = (kernel_ulong_t)&kx132_chip_info }, { .name = "kx132acr-lbz", .driver_data = (kernel_ulong_t)&kx132acr_chip_info }, + { .name = "kx134acr-lbz", .driver_data = (kernel_ulong_t)&kx134acr_chip_info }, { } };MODULE_DEVICE_TABLE (i2c, kx022a_i2c_id);
@@ -47,6 +48,7 @@ static const struct of_device_id kx022a_of_match[] = { { .compatible = "kionix,kx022a", .data = &kx022a_chip_info }, { .compatible = "kionix,kx132-1211", .data = &kx132_chip_info }, { .compatible = "rohm,kx132acr-lbz", .data = &kx132acr_chip_info }, + { .compatible = "rohm,kx134acr-lbz", .data = &kx134acr_chip_info }, { } };MODULE_DEVICE_TABLE (of, kx022a_of_match);
drivers/iio/accel/kionix-kx022a-spi.c +2 -0
@@ -39,6 +39,7 @@ static const struct spi_device_id kx022a_id[] = { { .name = "kx022a", .driver_data = (kernel_ulong_t)&kx022a_chip_info }, { .name = "kx132-1211", .driver_data = (kernel_ulong_t)&kx132_chip_info }, { .name = "kx132acr-lbz", .driver_data = (kernel_ulong_t)&kx132acr_chip_info }, + { .name = "kx134acr-lbz", .driver_data = (kernel_ulong_t)&kx134acr_chip_info }, { } };MODULE_DEVICE_TABLE (spi, kx022a_id);
@@ -47,6 +48,7 @@ static const struct of_device_id kx022a_of_match[] = { { .compatible = "kionix,kx022a", .data = &kx022a_chip_info }, { .compatible = "kionix,kx132-1211", .data = &kx132_chip_info }, { .compatible = "rohm,kx132acr-lbz", .data = &kx132acr_chip_info }, + { .compatible = "rohm,kx134acr-lbz", .data = &kx134acr_chip_info }, { } };MODULE_DEVICE_TABLE (of, kx022a_of_match);
drivers/iio/accel/kionix-kx022a.c +36 -0
@@ -408,6 +408,14 @@ static const int kx022a_scale_table[][2] = { { 0, 4788403 }, }; +/* KX134ACR-LBZ ranges are (+/-) 8, 16, 32, 64 G */ +static const int kx134acr_lbz_scale_table[][2 ] = { + {0 ,2394202 }, + {0 ,4788403 }, + {0 ,9576807 }, + {0 ,19153613 }, +}; + static int kx022a_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, const int **vals, int *type, int *length,
@@ -1231,6 +1239,34 @@ const struct kx022a_chip_info kx132acr_chip_info = { };EXPORT_SYMBOL_NS_GPL (kx132acr_chip_info,IIO_KX022A ); +const struct kx022a_chip_info kx134acr_chip_info = { + .name ="kx134acr-lbz" , + .regmap_config = &kx022a_regmap_config, + .channels = kx022a_channels, + .num_channels =ARRAY_SIZE (kx022a_channels), + .scale_table = kx134acr_lbz_scale_table, + .scale_table_size =ARRAY_SIZE (kx134acr_lbz_scale_table) * +ARRAY_SIZE (kx134acr_lbz_scale_table[0 ]), + .fifo_length =KX022A_FIFO_LENGTH , + .who =KX022A_REG_WHO , + .id =KX134ACR_LBZ_ID , + .cntl =KX022A_REG_CNTL , + .cntl2 =KX022A_REG_CNTL2 , + .odcntl =KX022A_REG_ODCNTL , + .buf_cntl1 =KX022A_REG_BUF_CNTL1 , + .buf_cntl2 =KX022A_REG_BUF_CNTL2 , + .buf_clear =KX022A_REG_BUF_CLEAR , + .buf_status1 =KX022A_REG_BUF_STATUS_1 , + .buf_read =KX022A_REG_BUF_READ , + .inc1 =KX022A_REG_INC1 , + .inc4 =KX022A_REG_INC4 , + .inc5 =KX022A_REG_INC5 , + .inc6 =KX022A_REG_INC6 , + .xout_l =KX022A_REG_XOUT_L , + .get_fifo_bytes_available = kx022a_get_fifo_bytes_available, +}; +EXPORT_SYMBOL_NS_GPL (kx134acr_chip_info,IIO_KX022A ); +int kx022a_probe_internal (struct device *dev,const struct kx022a_chip_info *chip_info) {static const char *const regulator_names[] = {"io-vdd" ,"vdd" };
drivers/iio/accel/kionix-kx022a.h +2 -0
@@ -14,6 +14,7 @@#define KX022A_REG_WHO 0x0f#define KX022A_ID 0xc8#define KX132ACR_LBZ_ID 0xd8 +#define KX134ACR_LBZ_ID 0xcc#define KX022A_REG_CNTL2 0x19#define KX022A_MASK_SRST BIT(7)
@@ -190,5 +191,6 @@ int kx022a_probe_internal(struct device *dev, const struct kx022a_chip_info *chiextern const struct kx022a_chip_info kx022a_chip_info;extern const struct kx022a_chip_info kx132_chip_info;extern const struct kx022a_chip_info kx132acr_chip_info; +extern const struct kx022a_chip_info kx134acr_chip_info;#endif
Signature · 1 line
2.47.0