linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] staging: iio: adc: ad7280a: channel and attr init refactor
@ 2018-12-12 17:02 Slawomir Stepien
  2018-12-12 17:02 ` [PATCH 1/2] staging: iio: adc: ad7280a: split ad7280_channel_init() to more functions Slawomir Stepien
  2018-12-12 17:02 ` [PATCH 2/2] staging: iio: adc: ad7280a: split ad7280_attr_init() " Slawomir Stepien
  0 siblings, 2 replies; 7+ messages in thread
From: Slawomir Stepien @ 2018-12-12 17:02 UTC (permalink / raw)
  To: lars, Michael.Hennerich, jic23, knaack.h, pmeerw; +Cc: linux-iio, gregkh

This small series enhances the readability of the code by refactoring the
ad7280_channel_init and ad7280_attr_init functions.

The readability is enhanced by splitting the functions into more functions that
do more specific operations.

Since v1:
* The patch "staging: iio: adc: ad7280a: add braces to complex for loops" has
   been removed. It's not needed anymore.

Slawomir Stepien (2):
  staging: iio: adc: ad7280a: split ad7280_channel_init() to more
    functions
  staging: iio: adc: ad7280a: split ad7280_attr_init() to more functions

 drivers/staging/iio/adc/ad7280a.c | 243 +++++++++++++++++++-----------
 1 file changed, 158 insertions(+), 85 deletions(-)

-- 
2.19.2


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

* [PATCH 1/2] staging: iio: adc: ad7280a: split ad7280_channel_init() to more functions
  2018-12-12 17:02 [PATCH v2 0/2] staging: iio: adc: ad7280a: channel and attr init refactor Slawomir Stepien
@ 2018-12-12 17:02 ` Slawomir Stepien
  2018-12-12 17:19   ` Slawomir Stepien
  2018-12-12 17:02 ` [PATCH 2/2] staging: iio: adc: ad7280a: split ad7280_attr_init() " Slawomir Stepien
  1 sibling, 1 reply; 7+ messages in thread
From: Slawomir Stepien @ 2018-12-12 17:02 UTC (permalink / raw)
  To: lars, Michael.Hennerich, jic23, knaack.h, pmeerw; +Cc: linux-iio, gregkh

The ad7280_channel_init function has been split into more specific
functions to increase the code readability.

The setting of channel's scan_type.shift to 0, has been removed, since
it is the default value.

Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
Since v1:
* Added ad7280_init_dev_channels function.
* New functions are now using struct iio_chan_spec rather than struct
  ad7280_state.
* Channel's number on device calculation has been moved to macros.
* scan_type.shift assigned has been removed.
---
 drivers/staging/iio/adc/ad7280a.c | 133 +++++++++++++++++++-----------
 1 file changed, 86 insertions(+), 47 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7280a.c b/drivers/staging/iio/adc/ad7280a.c
index 14f6a3ced060..8439a4e23228 100644
--- a/drivers/staging/iio/adc/ad7280a.c
+++ b/drivers/staging/iio/adc/ad7280a.c
@@ -97,6 +97,10 @@
 #define AD7280A_NUM_CH			(AD7280A_AUX_ADC_6 - \
 					AD7280A_CELL_VOLTAGE_1 + 1)
 
+#define AD7280A_CALC_VOLTAGE_CHAN_NUM(d, c) ((d * AD7280A_CELLS_PER_DEV) + c)
+#define AD7280A_CALC_TEMP_CHAN_NUM(d, c)    ((d * AD7280A_CELLS_PER_DEV) + \
+					     c - AD7280A_CELLS_PER_DEV)
+
 #define AD7280A_DEVADDR_MASTER		0
 #define AD7280A_DEVADDR_ALL		0x1F
 /* 5-bit device address is sent LSB first */
@@ -496,63 +500,98 @@ static const struct attribute_group ad7280_attrs_group = {
 	.attrs = ad7280_attributes,
 };
 
+static void ad7280_voltage_channel_init(struct iio_chan_spec *chan, int i)
+{
+	chan->type = IIO_VOLTAGE;
+	chan->differential = 1;
+	chan->channel = i;
+	chan->channel2 = chan->channel + 1;
+}
+
+static void ad7280_temp_channel_init(struct iio_chan_spec *chan, int i)
+{
+	chan->type = IIO_TEMP;
+	chan->channel = i;
+}
+
+static void ad7280_common_fields_init(struct iio_chan_spec *chan, int addr,
+				      int cnt)
+{
+	chan->indexed = 1;
+	chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
+	chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
+	chan->address = addr;
+	chan->scan_index = cnt;
+	chan->scan_type.sign = 'u';
+	chan->scan_type.realbits = 12;
+	chan->scan_type.storagebits = 32;
+}
+
+static void ad7280_total_voltage_channel_init(struct iio_chan_spec *chan,
+					      int cnt, int dev)
+{
+	chan->type = IIO_VOLTAGE;
+	chan->differential = 1;
+	chan->channel = 0;
+	chan->channel2 = dev * AD7280A_CELLS_PER_DEV;
+	chan->address = AD7280A_ALL_CELLS;
+	chan->indexed = 1;
+	chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
+	chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
+	chan->scan_index = cnt;
+	chan->scan_type.sign = 'u';
+	chan->scan_type.realbits = 32;
+	chan->scan_type.storagebits = 32;
+}
+
+static void ad7280_timestamp_channel_init(struct iio_chan_spec *chan, int cnt)
+{
+	chan->type = IIO_TIMESTAMP;
+	chan->channel = -1;
+	chan->scan_index = cnt;
+	chan->scan_type.sign = 's';
+	chan->scan_type.realbits = 64;
+	chan->scan_type.storagebits = 64;
+}
+
+static void ad7280_init_dev_channels(struct ad7280_state *st, int dev, int *cnt)
+{
+	int addr, ch, i;
+	struct iio_chan_spec *chan;
+
+	for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_AUX_ADC_6; ch++) {
+		chan = &st->channels[*cnt];
+
+		if (ch < AD7280A_AUX_ADC_1) {
+			i = AD7280A_CALC_VOLTAGE_CHAN_NUM(dev, ch);
+			ad7280_voltage_channel_init(chan, i);
+		} else {
+			i = AD7280A_CALC_TEMP_CHAN_NUM(dev, ch);
+			ad7280_temp_channel_init(chan, i);
+		}
+
+		addr = ad7280a_devaddr(dev) << 8 | ch;
+		ad7280_common_fields_init(chan, addr, *cnt);
+
+		(*cnt)++;
+	}
+}
+
 static int ad7280_channel_init(struct ad7280_state *st)
 {
-	int dev, ch, cnt;
+	int dev, cnt = 0;
 
 	st->channels = devm_kcalloc(&st->spi->dev, (st->slave_num + 1) * 12 + 2,
 				    sizeof(*st->channels), GFP_KERNEL);
 	if (!st->channels)
 		return -ENOMEM;
 
-	for (dev = 0, cnt = 0; dev <= st->slave_num; dev++)
-		for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_AUX_ADC_6;
-			ch++, cnt++) {
-			if (ch < AD7280A_AUX_ADC_1) {
-				st->channels[cnt].type = IIO_VOLTAGE;
-				st->channels[cnt].differential = 1;
-				st->channels[cnt].channel = (dev * 6) + ch;
-				st->channels[cnt].channel2 =
-					st->channels[cnt].channel + 1;
-			} else {
-				st->channels[cnt].type = IIO_TEMP;
-				st->channels[cnt].channel = (dev * 6) + ch - 6;
-			}
-			st->channels[cnt].indexed = 1;
-			st->channels[cnt].info_mask_separate =
-				BIT(IIO_CHAN_INFO_RAW);
-			st->channels[cnt].info_mask_shared_by_type =
-				BIT(IIO_CHAN_INFO_SCALE);
-			st->channels[cnt].address =
-				ad7280a_devaddr(dev) << 8 | ch;
-			st->channels[cnt].scan_index = cnt;
-			st->channels[cnt].scan_type.sign = 'u';
-			st->channels[cnt].scan_type.realbits = 12;
-			st->channels[cnt].scan_type.storagebits = 32;
-			st->channels[cnt].scan_type.shift = 0;
-		}
+	for (dev = 0; dev <= st->slave_num; dev++)
+		ad7280_init_dev_channels(st, dev, &cnt);
 
-	st->channels[cnt].type = IIO_VOLTAGE;
-	st->channels[cnt].differential = 1;
-	st->channels[cnt].channel = 0;
-	st->channels[cnt].channel2 = dev * 6;
-	st->channels[cnt].address = AD7280A_ALL_CELLS;
-	st->channels[cnt].indexed = 1;
-	st->channels[cnt].info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
-	st->channels[cnt].info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
-	st->channels[cnt].scan_index = cnt;
-	st->channels[cnt].scan_type.sign = 'u';
-	st->channels[cnt].scan_type.realbits = 32;
-	st->channels[cnt].scan_type.storagebits = 32;
-	st->channels[cnt].scan_type.shift = 0;
+	ad7280_total_voltage_channel_init(&st->channels[cnt], cnt, dev);
 	cnt++;
-	st->channels[cnt].type = IIO_TIMESTAMP;
-	st->channels[cnt].channel = -1;
-	st->channels[cnt].scan_index = cnt;
-	st->channels[cnt].scan_type.sign = 's';
-	st->channels[cnt].scan_type.realbits = 64;
-	st->channels[cnt].scan_type.storagebits = 64;
-	st->channels[cnt].scan_type.shift = 0;
+	ad7280_timestamp_channel_init(&st->channels[cnt], cnt);
 
 	return cnt + 1;
 }
-- 
2.19.2


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

* [PATCH 2/2] staging: iio: adc: ad7280a: split ad7280_attr_init() to more functions
  2018-12-12 17:02 [PATCH v2 0/2] staging: iio: adc: ad7280a: channel and attr init refactor Slawomir Stepien
  2018-12-12 17:02 ` [PATCH 1/2] staging: iio: adc: ad7280a: split ad7280_channel_init() to more functions Slawomir Stepien
@ 2018-12-12 17:02 ` Slawomir Stepien
  2018-12-12 17:20   ` Slawomir Stepien
  1 sibling, 1 reply; 7+ messages in thread
From: Slawomir Stepien @ 2018-12-12 17:02 UTC (permalink / raw)
  To: lars, Michael.Hennerich, jic23, knaack.h, pmeerw; +Cc: linux-iio, gregkh

The ad7280_attr_init function has been split into more specific
functions to increase the code readability.

Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
Since v1:
* Added ad7280_init_dev_attrs function.
* The new functions now operates on struct iio_dev_attr rather than on struct
  ad7280_state.
---
 drivers/staging/iio/adc/ad7280a.c | 110 +++++++++++++++++++-----------
 1 file changed, 72 insertions(+), 38 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7280a.c b/drivers/staging/iio/adc/ad7280a.c
index 8439a4e23228..d9df12665176 100644
--- a/drivers/staging/iio/adc/ad7280a.c
+++ b/drivers/staging/iio/adc/ad7280a.c
@@ -596,11 +596,75 @@ static int ad7280_channel_init(struct ad7280_state *st)
 	return cnt + 1;
 }
 
-static int ad7280_attr_init(struct ad7280_state *st)
+static int ad7280_balance_switch_attr_init(struct iio_dev_attr *attr,
+					   struct device *dev, int addr, int i)
+{
+	attr->address = addr;
+	attr->dev_attr.attr.mode = 0644;
+	attr->dev_attr.show = ad7280_show_balance_sw;
+	attr->dev_attr.store = ad7280_store_balance_sw;
+	attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
+						  "in%d-in%d_balance_switch_en",
+						  i, i + 1);
+	if (!attr->dev_attr.attr.name)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static int ad7280_balance_timer_attr_init(struct iio_dev_attr *attr,
+					  struct device *dev, int addr, int i)
+{
+	attr->address = addr;
+	attr->dev_attr.attr.mode = 0644;
+	attr->dev_attr.show = ad7280_show_balance_timer;
+	attr->dev_attr.store = ad7280_store_balance_timer;
+	attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
+						  "in%d-in%d_balance_timer",
+						  i, i + 1);
+	if (!attr->dev_attr.attr.name)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static int ad7280_init_dev_attrs(struct ad7280_state *st, int dev, int *cnt)
 {
-	int dev, ch, cnt;
-	unsigned int index;
+	int addr, ch, i, ret;
 	struct iio_dev_attr *iio_attr;
+	struct device *sdev = &st->spi->dev;
+
+	for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_CELL_VOLTAGE_6; ch++) {
+		iio_attr = &st->iio_attr[*cnt];
+		addr = ad7280a_devaddr(dev) << 8 | ch;
+		i = dev * AD7280A_CELLS_PER_DEV + ch;
+
+		ret = ad7280_balance_switch_attr_init(iio_attr, sdev, addr, i);
+		if (ret < 0)
+			return ret;
+
+		ad7280_attributes[*cnt] = &iio_attr->dev_attr.attr;
+
+		(*cnt)++;
+		iio_attr = &st->iio_attr[*cnt];
+		addr = ad7280a_devaddr(dev) << 8 | (AD7280A_CB1_TIMER + ch);
+
+		ret = ad7280_balance_timer_attr_init(iio_attr, sdev, addr, i);
+		if (ret < 0)
+			return ret;
+
+		ad7280_attributes[*cnt] = &iio_attr->dev_attr.attr;
+		(*cnt)++;
+	}
+
+	ad7280_attributes[*cnt] = NULL;
+
+	return 0;
+}
+
+static int ad7280_attr_init(struct ad7280_state *st)
+{
+	int dev, cnt = 0, ret;
 
 	st->iio_attr = devm_kcalloc(&st->spi->dev, 2, sizeof(*st->iio_attr) *
 				    (st->slave_num + 1) * AD7280A_CELLS_PER_DEV,
@@ -608,41 +672,11 @@ static int ad7280_attr_init(struct ad7280_state *st)
 	if (!st->iio_attr)
 		return -ENOMEM;
 
-	for (dev = 0, cnt = 0; dev <= st->slave_num; dev++)
-		for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_CELL_VOLTAGE_6;
-			ch++, cnt++) {
-			iio_attr = &st->iio_attr[cnt];
-			index = dev * AD7280A_CELLS_PER_DEV + ch;
-			iio_attr->address = ad7280a_devaddr(dev) << 8 | ch;
-			iio_attr->dev_attr.attr.mode = 0644;
-			iio_attr->dev_attr.show = ad7280_show_balance_sw;
-			iio_attr->dev_attr.store = ad7280_store_balance_sw;
-			iio_attr->dev_attr.attr.name =
-				devm_kasprintf(&st->spi->dev, GFP_KERNEL,
-					       "in%d-in%d_balance_switch_en",
-					       index, index + 1);
-			if (!iio_attr->dev_attr.attr.name)
-				return -ENOMEM;
-
-			ad7280_attributes[cnt] = &iio_attr->dev_attr.attr;
-			cnt++;
-			iio_attr = &st->iio_attr[cnt];
-			iio_attr->address = ad7280a_devaddr(dev) << 8 |
-				(AD7280A_CB1_TIMER + ch);
-			iio_attr->dev_attr.attr.mode = 0644;
-			iio_attr->dev_attr.show = ad7280_show_balance_timer;
-			iio_attr->dev_attr.store = ad7280_store_balance_timer;
-			iio_attr->dev_attr.attr.name =
-				devm_kasprintf(&st->spi->dev, GFP_KERNEL,
-					       "in%d-in%d_balance_timer",
-					       index, index + 1);
-			if (!iio_attr->dev_attr.attr.name)
-				return -ENOMEM;
-
-			ad7280_attributes[cnt] = &iio_attr->dev_attr.attr;
-		}
-
-	ad7280_attributes[cnt] = NULL;
+	for (dev = 0; dev <= st->slave_num; dev++) {
+		ret = ad7280_init_dev_attrs(st, dev, &cnt);
+		if (ret < 0)
+			return ret;
+	}
 
 	return 0;
 }
-- 
2.19.2


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

* Re: [PATCH 1/2] staging: iio: adc: ad7280a: split ad7280_channel_init() to more functions
  2018-12-12 17:02 ` [PATCH 1/2] staging: iio: adc: ad7280a: split ad7280_channel_init() to more functions Slawomir Stepien
@ 2018-12-12 17:19   ` Slawomir Stepien
  2018-12-16 12:33     ` Jonathan Cameron
  0 siblings, 1 reply; 7+ messages in thread
From: Slawomir Stepien @ 2018-12-12 17:19 UTC (permalink / raw)
  To: lars, Michael.Hennerich, jic23, knaack.h, pmeerw; +Cc: linux-iio, gregkh

On gru 12, 2018 18:02, Slawomir Stepien wrote:
> The ad7280_channel_init function has been split into more specific
> functions to increase the code readability.
> 
> The setting of channel's scan_type.shift to 0, has been removed, since
> it is the default value.
> 
> Signed-off-by: Slawomir Stepien <sst@poczta.fm>
> ---
> Since v1:
> * Added ad7280_init_dev_channels function.
> * New functions are now using struct iio_chan_spec rather than struct
>   ad7280_state.
> * Channel's number on device calculation has been moved to macros.
> * scan_type.shift assigned has been removed.
> ---

Sorry about not marking this as v2 in the subject line :( I'll fix that on next
version (if that happens ;)).

-- 
Slawomir Stepien

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

* Re: [PATCH 2/2] staging: iio: adc: ad7280a: split ad7280_attr_init() to more functions
  2018-12-12 17:02 ` [PATCH 2/2] staging: iio: adc: ad7280a: split ad7280_attr_init() " Slawomir Stepien
@ 2018-12-12 17:20   ` Slawomir Stepien
  2018-12-16 12:35     ` Jonathan Cameron
  0 siblings, 1 reply; 7+ messages in thread
From: Slawomir Stepien @ 2018-12-12 17:20 UTC (permalink / raw)
  To: lars, Michael.Hennerich, jic23, knaack.h, pmeerw; +Cc: linux-iio, gregkh

On gru 12, 2018 18:02, Slawomir Stepien wrote:
> The ad7280_attr_init function has been split into more specific
> functions to increase the code readability.
> 
> Signed-off-by: Slawomir Stepien <sst@poczta.fm>
> ---
> Since v1:
> * Added ad7280_init_dev_attrs function.
> * The new functions now operates on struct iio_dev_attr rather than on struct
>   ad7280_state.
> ---

Sorry about not marking this as v2 in the subject line :( I'll fix that on next
version (if that happens ;)).

-- 
Slawomir Stepien

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

* Re: [PATCH 1/2] staging: iio: adc: ad7280a: split ad7280_channel_init() to more functions
  2018-12-12 17:19   ` Slawomir Stepien
@ 2018-12-16 12:33     ` Jonathan Cameron
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Cameron @ 2018-12-16 12:33 UTC (permalink / raw)
  To: Slawomir Stepien
  Cc: lars, Michael.Hennerich, knaack.h, pmeerw, linux-iio, gregkh

On Wed, 12 Dec 2018 18:19:06 +0100
Slawomir Stepien <sst@poczta.fm> wrote:

> On gru 12, 2018 18:02, Slawomir Stepien wrote:
> > The ad7280_channel_init function has been split into more specific
> > functions to increase the code readability.
> > 
> > The setting of channel's scan_type.shift to 0, has been removed, since
> > it is the default value.
> > 
> > Signed-off-by: Slawomir Stepien <sst@poczta.fm>
> > ---
> > Since v1:
> > * Added ad7280_init_dev_channels function.
> > * New functions are now using struct iio_chan_spec rather than struct
> >   ad7280_state.
> > * Channel's number on device calculation has been moved to macros.
> > * scan_type.shift assigned has been removed.
> > ---  
> 
> Sorry about not marking this as v2 in the subject line :( I'll fix that on next
> version (if that happens ;)).
> 
Applied to the togreg branch of iio.git and pushed out as testing for
the autobuilders to see if they can break it.

Thanks,

Jonathan



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

* Re: [PATCH 2/2] staging: iio: adc: ad7280a: split ad7280_attr_init() to more functions
  2018-12-12 17:20   ` Slawomir Stepien
@ 2018-12-16 12:35     ` Jonathan Cameron
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Cameron @ 2018-12-16 12:35 UTC (permalink / raw)
  To: Slawomir Stepien
  Cc: lars, Michael.Hennerich, knaack.h, pmeerw, linux-iio, gregkh

On Wed, 12 Dec 2018 18:20:05 +0100
Slawomir Stepien <sst@poczta.fm> wrote:

> On gru 12, 2018 18:02, Slawomir Stepien wrote:
> > The ad7280_attr_init function has been split into more specific
> > functions to increase the code readability.
> > 
> > Signed-off-by: Slawomir Stepien <sst@poczta.fm>
> > ---
> > Since v1:
> > * Added ad7280_init_dev_attrs function.
> > * The new functions now operates on struct iio_dev_attr rather than on struct
> >   ad7280_state.
> > ---  
> 
> Sorry about not marking this as v2 in the subject line :( I'll fix that on next
> version (if that happens ;)).
> 

Applied to the togreg branch of iio.git and pushed out as testing for the
autobuilders to play with it.

Thanks,

Jonathan

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

end of thread, other threads:[~2018-12-16 12:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-12 17:02 [PATCH v2 0/2] staging: iio: adc: ad7280a: channel and attr init refactor Slawomir Stepien
2018-12-12 17:02 ` [PATCH 1/2] staging: iio: adc: ad7280a: split ad7280_channel_init() to more functions Slawomir Stepien
2018-12-12 17:19   ` Slawomir Stepien
2018-12-16 12:33     ` Jonathan Cameron
2018-12-12 17:02 ` [PATCH 2/2] staging: iio: adc: ad7280a: split ad7280_attr_init() " Slawomir Stepien
2018-12-12 17:20   ` Slawomir Stepien
2018-12-16 12:35     ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).