All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] iio: accel: STK8BA50: replace scale table with a struct
@ 2015-06-10 15:07 Tiberiu Breana
  2015-06-10 15:07 ` [PATCH 2/2] iio: accel: Add sampling rate support for STK8BA50 Tiberiu Breana
  2015-06-14 10:23 ` [PATCH 1/2] iio: accel: STK8BA50: replace scale table with a struct Jonathan Cameron
  0 siblings, 2 replies; 4+ messages in thread
From: Tiberiu Breana @ 2015-06-10 15:07 UTC (permalink / raw)
  To: linux-iio; +Cc: Jonathan Cameron

Replaced the stk8ba50_scale_table with an identically named
struct in order to make the code a bit more readable.

Signed-off-by: Tiberiu Breana <tiberiu.a.breana@intel.com>
---
v2: addressed Daniel's comments
---
 drivers/iio/accel/stk8ba50.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/accel/stk8ba50.c b/drivers/iio/accel/stk8ba50.c
index 30950c6..3302a3d 100644
--- a/drivers/iio/accel/stk8ba50.c
+++ b/drivers/iio/accel/stk8ba50.c
@@ -50,7 +50,10 @@
  *
  * Locally, the range is stored as a table index.
  */
-static const int stk8ba50_scale_table[][2] = {
+static const struct {
+	u8 reg_val;
+	u32 scale_val;
+} stk8ba50_scale_table[] = {
 	{3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
 };
 
@@ -114,7 +117,7 @@ static int stk8ba50_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		*val = 0;
-		*val2 = stk8ba50_scale_table[data->range][1];
+		*val2 = stk8ba50_scale_table[data->range].scale_val;
 		return IIO_VAL_INT_PLUS_MICRO;
 	}
 
@@ -136,7 +139,7 @@ static int stk8ba50_write_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 
 		for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++)
-			if (val2 == stk8ba50_scale_table[i][1]) {
+			if (val2 == stk8ba50_scale_table[i].scale_val) {
 				index = i;
 				break;
 			}
@@ -145,7 +148,7 @@ static int stk8ba50_write_raw(struct iio_dev *indio_dev,
 
 		ret = i2c_smbus_write_byte_data(data->client,
 				STK8BA50_REG_RANGE,
-				stk8ba50_scale_table[index][0]);
+				stk8ba50_scale_table[index].reg_val);
 		if (ret < 0)
 			dev_err(&data->client->dev,
 					"failed to set measurement range\n");
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] iio: accel: Add sampling rate support for STK8BA50
  2015-06-10 15:07 [PATCH 1/2] iio: accel: STK8BA50: replace scale table with a struct Tiberiu Breana
@ 2015-06-10 15:07 ` Tiberiu Breana
  2015-06-14 10:52   ` Jonathan Cameron
  2015-06-14 10:23 ` [PATCH 1/2] iio: accel: STK8BA50: replace scale table with a struct Jonathan Cameron
  1 sibling, 1 reply; 4+ messages in thread
From: Tiberiu Breana @ 2015-06-10 15:07 UTC (permalink / raw)
  To: linux-iio; +Cc: Jonathan Cameron

Added support for setting the STK8BA50 accelerometer's
sampling rate.

Signed-off-by: Tiberiu Breana <tiberiu.a.breana@intel.com>
---
v2: addressed Daniel's comments
---
 drivers/iio/accel/stk8ba50.c | 57 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 50 insertions(+), 7 deletions(-)

diff --git a/drivers/iio/accel/stk8ba50.c b/drivers/iio/accel/stk8ba50.c
index 3302a3d..92229bd 100644
--- a/drivers/iio/accel/stk8ba50.c
+++ b/drivers/iio/accel/stk8ba50.c
@@ -21,6 +21,7 @@
 #define STK8BA50_REG_YOUT			0x04
 #define STK8BA50_REG_ZOUT			0x06
 #define STK8BA50_REG_RANGE			0x0F
+#define STK8BA50_REG_BWSEL			0x10
 #define STK8BA50_REG_POWMODE			0x11
 #define STK8BA50_REG_SWRST			0x14
 
@@ -29,6 +30,7 @@
 #define STK8BA50_MODE_POWERBIT			BIT(7)
 #define STK8BA50_DATA_SHIFT			6
 #define STK8BA50_RESET_CMD			0xB6
+#define STK8BA50_SR_1792HZ_IDX			7
 
 #define STK8BA50_DRIVER_NAME			"stk8ba50"
 
@@ -57,19 +59,30 @@ static const struct {
 	{3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
 };
 
+/* Sample rates are stored as { <register value>, <Hz value> } */
+static const struct {
+	u8 reg_val;
+	u16 samp_freq;
+} stk8ba50_samp_freq_table[] = {
+	{0x08, 14},  {0x09, 25},  {0x0A, 56},  {0x0B, 112},
+	{0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792}
+};
+
 struct stk8ba50_data {
 	struct i2c_client *client;
 	struct mutex lock;
 	int range;
+	u8 sample_rate_idx;
 };
 
-#define STK8BA50_ACCEL_CHANNEL(reg, axis) {			\
-	.type = IIO_ACCEL,					\
-	.address = reg,						\
-	.modified = 1,						\
-	.channel2 = IIO_MOD_##axis,				\
-	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
-	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
+#define STK8BA50_ACCEL_CHANNEL(reg, axis) {				\
+	.type = IIO_ACCEL,						\
+	.address = reg,							\
+	.modified = 1,							\
+	.channel2 = IIO_MOD_##axis,					\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
+				    BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
 }
 
 static const struct iio_chan_spec stk8ba50_channels[] = {
@@ -80,8 +93,11 @@ static const struct iio_chan_spec stk8ba50_channels[] = {
 
 static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL);
 
+static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792");
+
 static struct attribute *stk8ba50_attributes[] = {
 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
+	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
 	NULL,
 };
 
@@ -119,6 +135,11 @@ static int stk8ba50_read_raw(struct iio_dev *indio_dev,
 		*val = 0;
 		*val2 = stk8ba50_scale_table[data->range].scale_val;
 		return IIO_VAL_INT_PLUS_MICRO;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		*val = stk8ba50_samp_freq_table
+				[data->sample_rate_idx].samp_freq;
+		*val2 = 0;
+		return IIO_VAL_INT;
 	}
 
 	return -EINVAL;
@@ -156,6 +177,25 @@ static int stk8ba50_write_raw(struct iio_dev *indio_dev,
 			data->range = index;
 
 		return ret;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++)
+			if (val == stk8ba50_samp_freq_table[i].samp_freq) {
+				index = i;
+				break;
+			}
+		if (index < 0)
+			return -EINVAL;
+
+		ret = i2c_smbus_write_byte_data(data->client,
+				STK8BA50_REG_BWSEL,
+				stk8ba50_samp_freq_table[index].reg_val);
+		if (ret < 0)
+			dev_err(&data->client->dev,
+					"failed to set sampling rate\n");
+		else
+			data->sample_rate_idx = index;
+
+		return ret;
 	}
 
 	return -EINVAL;
@@ -231,6 +271,9 @@ static int stk8ba50_probe(struct i2c_client *client,
 	/* The default range is +/-2g */
 	data->range = 0;
 
+	/* The default sampling rate is 1792 Hz (maximum) */
+	data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX;
+
 	ret = iio_device_register(indio_dev);
 	if (ret < 0) {
 		dev_err(&client->dev, "device_register failed\n");
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] iio: accel: STK8BA50: replace scale table with a struct
  2015-06-10 15:07 [PATCH 1/2] iio: accel: STK8BA50: replace scale table with a struct Tiberiu Breana
  2015-06-10 15:07 ` [PATCH 2/2] iio: accel: Add sampling rate support for STK8BA50 Tiberiu Breana
@ 2015-06-14 10:23 ` Jonathan Cameron
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2015-06-14 10:23 UTC (permalink / raw)
  To: Tiberiu Breana, linux-iio

On 10/06/15 16:07, Tiberiu Breana wrote:
> Replaced the stk8ba50_scale_table with an identically named
> struct in order to make the code a bit more readable.
> 
> Signed-off-by: Tiberiu Breana <tiberiu.a.breana@intel.com>
Sensible little rework.  Applied to the togreg branch of iio.git.
Initially pushed out as testing for the autobuilders to play.
Will be 4.3 material now...
> ---
> v2: addressed Daniel's comments
> ---
>  drivers/iio/accel/stk8ba50.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/iio/accel/stk8ba50.c b/drivers/iio/accel/stk8ba50.c
> index 30950c6..3302a3d 100644
> --- a/drivers/iio/accel/stk8ba50.c
> +++ b/drivers/iio/accel/stk8ba50.c
> @@ -50,7 +50,10 @@
>   *
>   * Locally, the range is stored as a table index.
>   */
> -static const int stk8ba50_scale_table[][2] = {
> +static const struct {
> +	u8 reg_val;
> +	u32 scale_val;
> +} stk8ba50_scale_table[] = {
>  	{3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
>  };
>  
> @@ -114,7 +117,7 @@ static int stk8ba50_read_raw(struct iio_dev *indio_dev,
>  		return IIO_VAL_INT;
>  	case IIO_CHAN_INFO_SCALE:
>  		*val = 0;
> -		*val2 = stk8ba50_scale_table[data->range][1];
> +		*val2 = stk8ba50_scale_table[data->range].scale_val;
>  		return IIO_VAL_INT_PLUS_MICRO;
>  	}
>  
> @@ -136,7 +139,7 @@ static int stk8ba50_write_raw(struct iio_dev *indio_dev,
>  			return -EINVAL;
>  
>  		for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++)
> -			if (val2 == stk8ba50_scale_table[i][1]) {
> +			if (val2 == stk8ba50_scale_table[i].scale_val) {
>  				index = i;
>  				break;
>  			}
> @@ -145,7 +148,7 @@ static int stk8ba50_write_raw(struct iio_dev *indio_dev,
>  
>  		ret = i2c_smbus_write_byte_data(data->client,
>  				STK8BA50_REG_RANGE,
> -				stk8ba50_scale_table[index][0]);
> +				stk8ba50_scale_table[index].reg_val);
>  		if (ret < 0)
>  			dev_err(&data->client->dev,
>  					"failed to set measurement range\n");
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add sampling rate support for STK8BA50
  2015-06-10 15:07 ` [PATCH 2/2] iio: accel: Add sampling rate support for STK8BA50 Tiberiu Breana
@ 2015-06-14 10:52   ` Jonathan Cameron
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2015-06-14 10:52 UTC (permalink / raw)
  To: Tiberiu Breana, linux-iio

On 10/06/15 16:07, Tiberiu Breana wrote:
> Added support for setting the STK8BA50 accelerometer's
> sampling rate.
> 
> Signed-off-by: Tiberiu Breana <tiberiu.a.breana@intel.com>
Applied to the togreg branch of iio.git.
Initially pushed out as testing for the autobuilder to play with it.
Thanks,

Jonathan
> ---
> v2: addressed Daniel's comments
> ---
>  drivers/iio/accel/stk8ba50.c | 57 ++++++++++++++++++++++++++++++++++++++------
>  1 file changed, 50 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/iio/accel/stk8ba50.c b/drivers/iio/accel/stk8ba50.c
> index 3302a3d..92229bd 100644
> --- a/drivers/iio/accel/stk8ba50.c
> +++ b/drivers/iio/accel/stk8ba50.c
> @@ -21,6 +21,7 @@
>  #define STK8BA50_REG_YOUT			0x04
>  #define STK8BA50_REG_ZOUT			0x06
>  #define STK8BA50_REG_RANGE			0x0F
> +#define STK8BA50_REG_BWSEL			0x10
>  #define STK8BA50_REG_POWMODE			0x11
>  #define STK8BA50_REG_SWRST			0x14
>  
> @@ -29,6 +30,7 @@
>  #define STK8BA50_MODE_POWERBIT			BIT(7)
>  #define STK8BA50_DATA_SHIFT			6
>  #define STK8BA50_RESET_CMD			0xB6
> +#define STK8BA50_SR_1792HZ_IDX			7
>  
>  #define STK8BA50_DRIVER_NAME			"stk8ba50"
>  
> @@ -57,19 +59,30 @@ static const struct {
>  	{3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
>  };
>  
> +/* Sample rates are stored as { <register value>, <Hz value> } */
> +static const struct {
> +	u8 reg_val;
> +	u16 samp_freq;
> +} stk8ba50_samp_freq_table[] = {
> +	{0x08, 14},  {0x09, 25},  {0x0A, 56},  {0x0B, 112},
> +	{0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792}
> +};
> +
>  struct stk8ba50_data {
>  	struct i2c_client *client;
>  	struct mutex lock;
>  	int range;
> +	u8 sample_rate_idx;
>  };
>  
> -#define STK8BA50_ACCEL_CHANNEL(reg, axis) {			\
> -	.type = IIO_ACCEL,					\
> -	.address = reg,						\
> -	.modified = 1,						\
> -	.channel2 = IIO_MOD_##axis,				\
> -	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> -	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> +#define STK8BA50_ACCEL_CHANNEL(reg, axis) {				\
> +	.type = IIO_ACCEL,						\
> +	.address = reg,							\
> +	.modified = 1,							\
> +	.channel2 = IIO_MOD_##axis,					\
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
> +				    BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
>  }
>  
>  static const struct iio_chan_spec stk8ba50_channels[] = {
> @@ -80,8 +93,11 @@ static const struct iio_chan_spec stk8ba50_channels[] = {
>  
>  static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL);
>  
> +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792");
> +
>  static struct attribute *stk8ba50_attributes[] = {
>  	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
> +	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
>  	NULL,
>  };
>  
> @@ -119,6 +135,11 @@ static int stk8ba50_read_raw(struct iio_dev *indio_dev,
>  		*val = 0;
>  		*val2 = stk8ba50_scale_table[data->range].scale_val;
>  		return IIO_VAL_INT_PLUS_MICRO;
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		*val = stk8ba50_samp_freq_table
> +				[data->sample_rate_idx].samp_freq;
> +		*val2 = 0;
> +		return IIO_VAL_INT;
>  	}
>  
>  	return -EINVAL;
> @@ -156,6 +177,25 @@ static int stk8ba50_write_raw(struct iio_dev *indio_dev,
>  			data->range = index;
>  
>  		return ret;
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++)
> +			if (val == stk8ba50_samp_freq_table[i].samp_freq) {
> +				index = i;
> +				break;
> +			}
> +		if (index < 0)
> +			return -EINVAL;
> +
> +		ret = i2c_smbus_write_byte_data(data->client,
> +				STK8BA50_REG_BWSEL,
> +				stk8ba50_samp_freq_table[index].reg_val);
> +		if (ret < 0)
> +			dev_err(&data->client->dev,
> +					"failed to set sampling rate\n");
> +		else
> +			data->sample_rate_idx = index;
> +
> +		return ret;
>  	}
>  
>  	return -EINVAL;
> @@ -231,6 +271,9 @@ static int stk8ba50_probe(struct i2c_client *client,
>  	/* The default range is +/-2g */
>  	data->range = 0;
>  
> +	/* The default sampling rate is 1792 Hz (maximum) */
> +	data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX;
> +
>  	ret = iio_device_register(indio_dev);
>  	if (ret < 0) {
>  		dev_err(&client->dev, "device_register failed\n");
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-06-14 10:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-10 15:07 [PATCH 1/2] iio: accel: STK8BA50: replace scale table with a struct Tiberiu Breana
2015-06-10 15:07 ` [PATCH 2/2] iio: accel: Add sampling rate support for STK8BA50 Tiberiu Breana
2015-06-14 10:52   ` Jonathan Cameron
2015-06-14 10:23 ` [PATCH 1/2] iio: accel: STK8BA50: replace scale table with a struct Jonathan Cameron

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.