Re: [PATCH v4 1/2] iio: dac: Add AD5758 support

2018-07-04 Thread Popa, Stefan Serban
On Du, 2018-07-01 at 00:26 +0300, Andy Shevchenko wrote:
> On Fri, Jun 29, 2018 at 11:38 AM, Stefan Popa  wrote:
> > 
> > The AD5758 is a single channel DAC with 16-bit precision which uses the
> > SPI interface that operates at clock rates up to 50MHz.
> > 
> > The output can be configured as voltage or current and is available on a
> > single terminal.
> > 
> > Datasheet:
> > http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf
> Thanks for an update.
> Few comments below.
> 
> > 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> Perhaps keep them ordered?
> 
> > 
> > +
> > +#include 
> > +#include 
> > +
> > 
> > +#include 
> ASM? Hmm...
> 
> > 
> > +static int cmpfunc(const void *a, const void *b)
> > +{
> > +   return (*(int *)a - *(int *)b);
> Surrounding parens are not needed.
> 
> > 
> > +}
> > +
> > 
> > +static int ad5758_find_closest_match(const int *array,
> > +unsigned int size, int val)
> > +{
> > +   int i;
> > +
> > +   for (i = 0; i < size; i++) {
> > +   if (val <= array[i])
> > +   return i;
> > +   }
> > +
> > +   return size - 1;
> > +}
> Isn't it what bsearch() covers as well?
> 

bsearch() looks for an item in an array and returns NULL if it is not found.
This function returns the index of the closest value to val even if there is 
no exact match.

> > 
> > +   do {
> > +   ret = ad5758_spi_reg_read(st, reg);
> > +   if (ret < 0)
> > +   return ret;
> > +
> > +   if (!(ret & mask))
> > +   return 0;
> > +
> > 
> > +   udelay(100);
> If it's not called from atomic context, perhaps switch to usleep_range() ?
> 
> > 
> > +   } while (--timeout);
> 
> > 
> > +static int ad5758_soft_reset(struct ad5758_state *st)
> > +{
> > +   int ret;
> > +
> > +   ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1);
> > +   if (ret < 0)
> > +   return ret;
> > +
> > +   ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2);
> > +
> > 
> > +   /* Perform a software reset and wait 100us */
> > +   udelay(100);
> Ditto.
> 
> > 
> > +
> > +   return ret;
> > +}
> > 
> > +static int ad5758_find_out_range(struct ad5758_state *st,
> > +const struct ad5758_range *range,
> > +unsigned int size,
> > +int min, int max)
> > +{
> > +   int i;
> > +
> > +   for (i = 0; i < size; i++) {
> > +   if ((min == range[i].min) && (max == range[i].max)) {
> > +   st->out_range.reg = range[i].reg;
> > +   st->out_range.min = range[i].min;
> > +   st->out_range.max = range[i].max;
> > +
> > +   return 0;
> > +   }
> > +   }
> One more candidate to use bsearch().
> 

I am not sure if bsearch() will work in this case since it requires that the 
given 
array is sorted. Moreover, both ad5758_voltage_range[] and 
ad5758_current_range[] 
contain duplicate elements.

> > 
> > +
> > +   return -EINVAL;
> > +}
> > 
> > +   index = (int *) bsearch(, ad5758_dc_dc_ilim,
> > +   ARRAY_SIZE(ad5758_dc_dc_ilim),
> > +   sizeof(int), cmpfunc);
> I'm not sure you need that casting.
> 

Re: [PATCH v4 1/2] iio: dac: Add AD5758 support

2018-07-04 Thread Popa, Stefan Serban
On Du, 2018-07-01 at 00:26 +0300, Andy Shevchenko wrote:
> On Fri, Jun 29, 2018 at 11:38 AM, Stefan Popa  wrote:
> > 
> > The AD5758 is a single channel DAC with 16-bit precision which uses the
> > SPI interface that operates at clock rates up to 50MHz.
> > 
> > The output can be configured as voltage or current and is available on a
> > single terminal.
> > 
> > Datasheet:
> > http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf
> Thanks for an update.
> Few comments below.
> 
> > 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> Perhaps keep them ordered?
> 
> > 
> > +
> > +#include 
> > +#include 
> > +
> > 
> > +#include 
> ASM? Hmm...
> 
> > 
> > +static int cmpfunc(const void *a, const void *b)
> > +{
> > +   return (*(int *)a - *(int *)b);
> Surrounding parens are not needed.
> 
> > 
> > +}
> > +
> > 
> > +static int ad5758_find_closest_match(const int *array,
> > +unsigned int size, int val)
> > +{
> > +   int i;
> > +
> > +   for (i = 0; i < size; i++) {
> > +   if (val <= array[i])
> > +   return i;
> > +   }
> > +
> > +   return size - 1;
> > +}
> Isn't it what bsearch() covers as well?
> 

bsearch() looks for an item in an array and returns NULL if it is not found.
This function returns the index of the closest value to val even if there is 
no exact match.

> > 
> > +   do {
> > +   ret = ad5758_spi_reg_read(st, reg);
> > +   if (ret < 0)
> > +   return ret;
> > +
> > +   if (!(ret & mask))
> > +   return 0;
> > +
> > 
> > +   udelay(100);
> If it's not called from atomic context, perhaps switch to usleep_range() ?
> 
> > 
> > +   } while (--timeout);
> 
> > 
> > +static int ad5758_soft_reset(struct ad5758_state *st)
> > +{
> > +   int ret;
> > +
> > +   ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1);
> > +   if (ret < 0)
> > +   return ret;
> > +
> > +   ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2);
> > +
> > 
> > +   /* Perform a software reset and wait 100us */
> > +   udelay(100);
> Ditto.
> 
> > 
> > +
> > +   return ret;
> > +}
> > 
> > +static int ad5758_find_out_range(struct ad5758_state *st,
> > +const struct ad5758_range *range,
> > +unsigned int size,
> > +int min, int max)
> > +{
> > +   int i;
> > +
> > +   for (i = 0; i < size; i++) {
> > +   if ((min == range[i].min) && (max == range[i].max)) {
> > +   st->out_range.reg = range[i].reg;
> > +   st->out_range.min = range[i].min;
> > +   st->out_range.max = range[i].max;
> > +
> > +   return 0;
> > +   }
> > +   }
> One more candidate to use bsearch().
> 

I am not sure if bsearch() will work in this case since it requires that the 
given 
array is sorted. Moreover, both ad5758_voltage_range[] and 
ad5758_current_range[] 
contain duplicate elements.

> > 
> > +
> > +   return -EINVAL;
> > +}
> > 
> > +   index = (int *) bsearch(, ad5758_dc_dc_ilim,
> > +   ARRAY_SIZE(ad5758_dc_dc_ilim),
> > +   sizeof(int), cmpfunc);
> I'm not sure you need that casting.
> 

Re: [PATCH v4 1/2] iio: dac: Add AD5758 support

2018-06-30 Thread Andy Shevchenko
On Fri, Jun 29, 2018 at 11:38 AM, Stefan Popa  wrote:
> The AD5758 is a single channel DAC with 16-bit precision which uses the
> SPI interface that operates at clock rates up to 50MHz.
>
> The output can be configured as voltage or current and is available on a
> single terminal.
>
> Datasheet:
> http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf

Thanks for an update.
Few comments below.

> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 

Perhaps keep them ordered?

> +
> +#include 
> +#include 
> +

> +#include 

ASM? Hmm...

> +static int cmpfunc(const void *a, const void *b)
> +{
> +   return (*(int *)a - *(int *)b);

Surrounding parens are not needed.

> +}
> +

> +static int ad5758_find_closest_match(const int *array,
> +unsigned int size, int val)
> +{
> +   int i;
> +
> +   for (i = 0; i < size; i++) {
> +   if (val <= array[i])
> +   return i;
> +   }
> +
> +   return size - 1;
> +}

Isn't it what bsearch() covers as well?

> +   do {
> +   ret = ad5758_spi_reg_read(st, reg);
> +   if (ret < 0)
> +   return ret;
> +
> +   if (!(ret & mask))
> +   return 0;
> +

> +   udelay(100);

If it's not called from atomic context, perhaps switch to usleep_range() ?

> +   } while (--timeout);


> +static int ad5758_soft_reset(struct ad5758_state *st)
> +{
> +   int ret;
> +
> +   ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1);
> +   if (ret < 0)
> +   return ret;
> +
> +   ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2);
> +

> +   /* Perform a software reset and wait 100us */
> +   udelay(100);

Ditto.

> +
> +   return ret;
> +}

> +static int ad5758_find_out_range(struct ad5758_state *st,
> +const struct ad5758_range *range,
> +unsigned int size,
> +int min, int max)
> +{
> +   int i;
> +
> +   for (i = 0; i < size; i++) {
> +   if ((min == range[i].min) && (max == range[i].max)) {
> +   st->out_range.reg = range[i].reg;
> +   st->out_range.min = range[i].min;
> +   st->out_range.max = range[i].max;
> +
> +   return 0;
> +   }
> +   }

One more candidate to use bsearch().

> +
> +   return -EINVAL;
> +}

> +   index = (int *) bsearch(, ad5758_dc_dc_ilim,
> +   ARRAY_SIZE(ad5758_dc_dc_ilim),
> +   sizeof(int), cmpfunc);

I'm not sure you need that casting.

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH v4 1/2] iio: dac: Add AD5758 support

2018-06-30 Thread Andy Shevchenko
On Fri, Jun 29, 2018 at 11:38 AM, Stefan Popa  wrote:
> The AD5758 is a single channel DAC with 16-bit precision which uses the
> SPI interface that operates at clock rates up to 50MHz.
>
> The output can be configured as voltage or current and is available on a
> single terminal.
>
> Datasheet:
> http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf

Thanks for an update.
Few comments below.

> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 

Perhaps keep them ordered?

> +
> +#include 
> +#include 
> +

> +#include 

ASM? Hmm...

> +static int cmpfunc(const void *a, const void *b)
> +{
> +   return (*(int *)a - *(int *)b);

Surrounding parens are not needed.

> +}
> +

> +static int ad5758_find_closest_match(const int *array,
> +unsigned int size, int val)
> +{
> +   int i;
> +
> +   for (i = 0; i < size; i++) {
> +   if (val <= array[i])
> +   return i;
> +   }
> +
> +   return size - 1;
> +}

Isn't it what bsearch() covers as well?

> +   do {
> +   ret = ad5758_spi_reg_read(st, reg);
> +   if (ret < 0)
> +   return ret;
> +
> +   if (!(ret & mask))
> +   return 0;
> +

> +   udelay(100);

If it's not called from atomic context, perhaps switch to usleep_range() ?

> +   } while (--timeout);


> +static int ad5758_soft_reset(struct ad5758_state *st)
> +{
> +   int ret;
> +
> +   ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1);
> +   if (ret < 0)
> +   return ret;
> +
> +   ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2);
> +

> +   /* Perform a software reset and wait 100us */
> +   udelay(100);

Ditto.

> +
> +   return ret;
> +}

> +static int ad5758_find_out_range(struct ad5758_state *st,
> +const struct ad5758_range *range,
> +unsigned int size,
> +int min, int max)
> +{
> +   int i;
> +
> +   for (i = 0; i < size; i++) {
> +   if ((min == range[i].min) && (max == range[i].max)) {
> +   st->out_range.reg = range[i].reg;
> +   st->out_range.min = range[i].min;
> +   st->out_range.max = range[i].max;
> +
> +   return 0;
> +   }
> +   }

One more candidate to use bsearch().

> +
> +   return -EINVAL;
> +}

> +   index = (int *) bsearch(, ad5758_dc_dc_ilim,
> +   ARRAY_SIZE(ad5758_dc_dc_ilim),
> +   sizeof(int), cmpfunc);

I'm not sure you need that casting.

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH v4 1/2] iio: dac: Add AD5758 support

2018-06-30 Thread Jonathan Cameron
On Fri, 29 Jun 2018 11:38:36 +0300
Stefan Popa  wrote:

> The AD5758 is a single channel DAC with 16-bit precision which uses the
> SPI interface that operates at clock rates up to 50MHz.
> 
> The output can be configured as voltage or current and is available on a
> single terminal.
> 
> Datasheet:
> http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf
> 
> Signed-off-by: Stefan Popa 

Hi Stefan,

A few minors + the way you currently handle the choice of voltage vs
current prevents you having multiple instances of this device with
different settings.   I would pick between between two constant
options of the whole structure.  Alternative would be to make a fresh
copy of the structure for each instance of the device, but that is
fiddlier for such a simple case.

Jonathan

> ---
> Changes in v4:
>   - fixed kbuild test robot warnings.
> Changes in v3:
>   - AD5758 can be both a current and voltage output DAC. The
> decision is made based on the DT and the channel type is set
> during probe.
>   - dc-dc-mode, range-microvolt and range-microamp are required
> properties.
>   - Introduced a slew-time-us property from which slew rate clock
> and slew rate step are calculated using a best match algorithm.
>   - Dropped the union from ad5758_state struct.
>   - Introduced a IIO_CHAN_INFO_OFFSET case part of ad5758_read_raw().
>   - Added a TODO comment which specifies that CRC is not supported.
>   - Kept the includes in order and removed the unused ones.
>   - Removed unused macros and shortened the lengthy ones.
>   - Renamed AD5758_REG_WRITE to AD5758_WR_FLAG_MSK.
>   - Added an explanation for enum ad5758_output_range.
>   - Used bsearch() instead of ad5758_get_array_index().
>   - Reduced the delays.
>   - strtobool() -> kstrtobool().
> 
> Changes in v2:
>   - removed unnecessary parenthesis in AD5758_REG_WRITE macro.
>   - added missing documentation fields of ad5758_state struct.
>   - changed the type of pwr_down attribute to bool.
>   - changed ad5758_dc_dc_ilimt[] to ad5758_dc_dc_ilim[].
>   - ad5758_spi_reg_write() now returns spi_write(st->spi,
> >data[0].d32, sizeof(st->data[0].d32));
>   - removed unnecessary new line in ad5758_calib_mem_refresh().
>   - changed the type of the mode parameter in
> ad5758_set_dc_dc_conv_mode() from unsigned int to enum
> ad5758_dc_dc_mode.
>   - removed unnecessary parenthesis in ad5758_slew_rate_config().
>   - changed the type of the enable parameter in
> ad5758_fault_prot_switch_en() from unsigned char to bool.
>   - the same as above, but for ad5758_internal_buffers_en().
>   - added a missing mutex_unlock() in ad5758_reg_access().
>   - moved the mutex_unlock() in ad5758_read_raw() and removed the
> unreachable return.
>   - returned directly where it was possible in ad5758_write_raw().
>   - removed the channel, scan_type and scan_index fields.
>   - in ad5758_parse_dt(), added missing "\n", and specified what the
> default mode actually is.
>   - returned directly at the end of ad5758_init().
>   - in ad5758_probe() used device managed for registering the device
> and returned directly without the error message.
> 
>  MAINTAINERS  |   7 +
>  drivers/iio/dac/Kconfig  |  10 +
>  drivers/iio/dac/Makefile |   1 +
>  drivers/iio/dac/ad5758.c | 899 
> +++
>  4 files changed, 917 insertions(+)
>  create mode 100644 drivers/iio/dac/ad5758.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 00e9670..12d102d 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -796,6 +796,13 @@ M:   Michael Hanselmann 
>  S:   Supported
>  F:   drivers/macintosh/ams/
>  
> +ANALOG DEVICES INC AD5758 DRIVER
> +M:   Stefan Popa 
> +L:   linux-...@vger.kernel.org
> +W:   http://ez.analog.com/community/linux-device-drivers
> +S:   Supported
> +F:   drivers/iio/dac/ad5758.c
> +
>  ANALOG DEVICES INC AD5686 DRIVER
>  M:   Stefan Popa 
>  L:   linux...@vger.kernel.org
> diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
> index 06e90de..80beb64 100644
> --- a/drivers/iio/dac/Kconfig
> +++ b/drivers/iio/dac/Kconfig
> @@ -167,6 +167,16 @@ config AD5755
> To compile this driver as a module, choose M here: the
> module will be called ad5755.
>  
> +config AD5758
> + tristate "Analog Devices AD5758 DAC driver"
> + depends on SPI_MASTER
> + help
> +   Say yes here to build support for Analog Devices AD5758 single channel
> +   Digital to Analog Converter.
> +
> +   To compile this driver as a module, choose M here: the
> +   module will be called ad5758.
> +
>  config AD5761
>   tristate "Analog Devices AD5761/61R/21/21R DAC driver"
>   depends on SPI_MASTER
> diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
> index 

Re: [PATCH v4 1/2] iio: dac: Add AD5758 support

2018-06-30 Thread Jonathan Cameron
On Fri, 29 Jun 2018 11:38:36 +0300
Stefan Popa  wrote:

> The AD5758 is a single channel DAC with 16-bit precision which uses the
> SPI interface that operates at clock rates up to 50MHz.
> 
> The output can be configured as voltage or current and is available on a
> single terminal.
> 
> Datasheet:
> http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf
> 
> Signed-off-by: Stefan Popa 

Hi Stefan,

A few minors + the way you currently handle the choice of voltage vs
current prevents you having multiple instances of this device with
different settings.   I would pick between between two constant
options of the whole structure.  Alternative would be to make a fresh
copy of the structure for each instance of the device, but that is
fiddlier for such a simple case.

Jonathan

> ---
> Changes in v4:
>   - fixed kbuild test robot warnings.
> Changes in v3:
>   - AD5758 can be both a current and voltage output DAC. The
> decision is made based on the DT and the channel type is set
> during probe.
>   - dc-dc-mode, range-microvolt and range-microamp are required
> properties.
>   - Introduced a slew-time-us property from which slew rate clock
> and slew rate step are calculated using a best match algorithm.
>   - Dropped the union from ad5758_state struct.
>   - Introduced a IIO_CHAN_INFO_OFFSET case part of ad5758_read_raw().
>   - Added a TODO comment which specifies that CRC is not supported.
>   - Kept the includes in order and removed the unused ones.
>   - Removed unused macros and shortened the lengthy ones.
>   - Renamed AD5758_REG_WRITE to AD5758_WR_FLAG_MSK.
>   - Added an explanation for enum ad5758_output_range.
>   - Used bsearch() instead of ad5758_get_array_index().
>   - Reduced the delays.
>   - strtobool() -> kstrtobool().
> 
> Changes in v2:
>   - removed unnecessary parenthesis in AD5758_REG_WRITE macro.
>   - added missing documentation fields of ad5758_state struct.
>   - changed the type of pwr_down attribute to bool.
>   - changed ad5758_dc_dc_ilimt[] to ad5758_dc_dc_ilim[].
>   - ad5758_spi_reg_write() now returns spi_write(st->spi,
> >data[0].d32, sizeof(st->data[0].d32));
>   - removed unnecessary new line in ad5758_calib_mem_refresh().
>   - changed the type of the mode parameter in
> ad5758_set_dc_dc_conv_mode() from unsigned int to enum
> ad5758_dc_dc_mode.
>   - removed unnecessary parenthesis in ad5758_slew_rate_config().
>   - changed the type of the enable parameter in
> ad5758_fault_prot_switch_en() from unsigned char to bool.
>   - the same as above, but for ad5758_internal_buffers_en().
>   - added a missing mutex_unlock() in ad5758_reg_access().
>   - moved the mutex_unlock() in ad5758_read_raw() and removed the
> unreachable return.
>   - returned directly where it was possible in ad5758_write_raw().
>   - removed the channel, scan_type and scan_index fields.
>   - in ad5758_parse_dt(), added missing "\n", and specified what the
> default mode actually is.
>   - returned directly at the end of ad5758_init().
>   - in ad5758_probe() used device managed for registering the device
> and returned directly without the error message.
> 
>  MAINTAINERS  |   7 +
>  drivers/iio/dac/Kconfig  |  10 +
>  drivers/iio/dac/Makefile |   1 +
>  drivers/iio/dac/ad5758.c | 899 
> +++
>  4 files changed, 917 insertions(+)
>  create mode 100644 drivers/iio/dac/ad5758.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 00e9670..12d102d 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -796,6 +796,13 @@ M:   Michael Hanselmann 
>  S:   Supported
>  F:   drivers/macintosh/ams/
>  
> +ANALOG DEVICES INC AD5758 DRIVER
> +M:   Stefan Popa 
> +L:   linux-...@vger.kernel.org
> +W:   http://ez.analog.com/community/linux-device-drivers
> +S:   Supported
> +F:   drivers/iio/dac/ad5758.c
> +
>  ANALOG DEVICES INC AD5686 DRIVER
>  M:   Stefan Popa 
>  L:   linux...@vger.kernel.org
> diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
> index 06e90de..80beb64 100644
> --- a/drivers/iio/dac/Kconfig
> +++ b/drivers/iio/dac/Kconfig
> @@ -167,6 +167,16 @@ config AD5755
> To compile this driver as a module, choose M here: the
> module will be called ad5755.
>  
> +config AD5758
> + tristate "Analog Devices AD5758 DAC driver"
> + depends on SPI_MASTER
> + help
> +   Say yes here to build support for Analog Devices AD5758 single channel
> +   Digital to Analog Converter.
> +
> +   To compile this driver as a module, choose M here: the
> +   module will be called ad5758.
> +
>  config AD5761
>   tristate "Analog Devices AD5761/61R/21/21R DAC driver"
>   depends on SPI_MASTER
> diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
> index 

Re: [PATCH v4 1/2] iio: dac: Add AD5758 support

2018-06-29 Thread Sean Nyekjaer




On 29-06-2018 10:38, Stefan Popa wrote:

The AD5758 is a single channel DAC with 16-bit precision which uses the
SPI interface that operates at clock rates up to 50MHz.

The output can be configured as voltage or current and is available on a
single terminal.

Datasheet:
http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf

Signed-off-by: Stefan Popa 

Reviewed-by:  Sean Nyekjaer 

---
Changes in v4:
- fixed kbuild test robot warnings.
Changes in v3:
- AD5758 can be both a current and voltage output DAC. The
  decision is made based on the DT and the channel type is set
  during probe.
- dc-dc-mode, range-microvolt and range-microamp are required
  properties.
- Introduced a slew-time-us property from which slew rate clock
  and slew rate step are calculated using a best match algorithm.
- Dropped the union from ad5758_state struct.
- Introduced a IIO_CHAN_INFO_OFFSET case part of ad5758_read_raw().
- Added a TODO comment which specifies that CRC is not supported.
- Kept the includes in order and removed the unused ones.
- Removed unused macros and shortened the lengthy ones.
- Renamed AD5758_REG_WRITE to AD5758_WR_FLAG_MSK.
- Added an explanation for enum ad5758_output_range.
- Used bsearch() instead of ad5758_get_array_index().
- Reduced the delays.
- strtobool() -> kstrtobool().

Changes in v2:
- removed unnecessary parenthesis in AD5758_REG_WRITE macro.
- added missing documentation fields of ad5758_state struct.
- changed the type of pwr_down attribute to bool.
- changed ad5758_dc_dc_ilimt[] to ad5758_dc_dc_ilim[].
- ad5758_spi_reg_write() now returns spi_write(st->spi,
  >data[0].d32, sizeof(st->data[0].d32));
- removed unnecessary new line in ad5758_calib_mem_refresh().
- changed the type of the mode parameter in
  ad5758_set_dc_dc_conv_mode() from unsigned int to enum
  ad5758_dc_dc_mode.
- removed unnecessary parenthesis in ad5758_slew_rate_config().
- changed the type of the enable parameter in
  ad5758_fault_prot_switch_en() from unsigned char to bool.
- the same as above, but for ad5758_internal_buffers_en().
- added a missing mutex_unlock() in ad5758_reg_access().
- moved the mutex_unlock() in ad5758_read_raw() and removed the
  unreachable return.
- returned directly where it was possible in ad5758_write_raw().
- removed the channel, scan_type and scan_index fields.
- in ad5758_parse_dt(), added missing "\n", and specified what the
  default mode actually is.
- returned directly at the end of ad5758_init().
- in ad5758_probe() used device managed for registering the device
  and returned directly without the error message.

  MAINTAINERS  |   7 +
  drivers/iio/dac/Kconfig  |  10 +
  drivers/iio/dac/Makefile |   1 +
  drivers/iio/dac/ad5758.c | 899 +++
  4 files changed, 917 insertions(+)
  create mode 100644 drivers/iio/dac/ad5758.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 00e9670..12d102d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -796,6 +796,13 @@ M: Michael Hanselmann 
  S:Supported
  F:drivers/macintosh/ams/
  
+ANALOG DEVICES INC AD5758 DRIVER

+M: Stefan Popa 
+L: linux-...@vger.kernel.org
+W: http://ez.analog.com/community/linux-device-drivers
+S: Supported
+F: drivers/iio/dac/ad5758.c
+
  ANALOG DEVICES INC AD5686 DRIVER
  M:Stefan Popa 
  L:linux...@vger.kernel.org
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index 06e90de..80beb64 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -167,6 +167,16 @@ config AD5755
  To compile this driver as a module, choose M here: the
  module will be called ad5755.
  
+config AD5758

+   tristate "Analog Devices AD5758 DAC driver"
+   depends on SPI_MASTER
+   help
+ Say yes here to build support for Analog Devices AD5758 single channel
+ Digital to Analog Converter.
+
+ To compile this driver as a module, choose M here: the
+ module will be called ad5758.
+
  config AD5761
tristate "Analog Devices AD5761/61R/21/21R DAC driver"
depends on SPI_MASTER
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index 57aa230..a1b37cf 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD5592R_BASE) += ad5592r-base.o
  obj-$(CONFIG_AD5592R) += ad5592r.o
  obj-$(CONFIG_AD5593R) += ad5593r.o
  obj-$(CONFIG_AD5755) += ad5755.o
+obj-$(CONFIG_AD5755) += ad5758.o
  obj-$(CONFIG_AD5761) += ad5761.o
  obj-$(CONFIG_AD5764) += ad5764.o
  obj-$(CONFIG_AD5791) += ad5791.o
diff --git a/drivers/iio/dac/ad5758.c b/drivers/iio/dac/ad5758.c
new file 

Re: [PATCH v4 1/2] iio: dac: Add AD5758 support

2018-06-29 Thread Sean Nyekjaer




On 29-06-2018 10:38, Stefan Popa wrote:

The AD5758 is a single channel DAC with 16-bit precision which uses the
SPI interface that operates at clock rates up to 50MHz.

The output can be configured as voltage or current and is available on a
single terminal.

Datasheet:
http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf

Signed-off-by: Stefan Popa 

Reviewed-by:  Sean Nyekjaer 

---
Changes in v4:
- fixed kbuild test robot warnings.
Changes in v3:
- AD5758 can be both a current and voltage output DAC. The
  decision is made based on the DT and the channel type is set
  during probe.
- dc-dc-mode, range-microvolt and range-microamp are required
  properties.
- Introduced a slew-time-us property from which slew rate clock
  and slew rate step are calculated using a best match algorithm.
- Dropped the union from ad5758_state struct.
- Introduced a IIO_CHAN_INFO_OFFSET case part of ad5758_read_raw().
- Added a TODO comment which specifies that CRC is not supported.
- Kept the includes in order and removed the unused ones.
- Removed unused macros and shortened the lengthy ones.
- Renamed AD5758_REG_WRITE to AD5758_WR_FLAG_MSK.
- Added an explanation for enum ad5758_output_range.
- Used bsearch() instead of ad5758_get_array_index().
- Reduced the delays.
- strtobool() -> kstrtobool().

Changes in v2:
- removed unnecessary parenthesis in AD5758_REG_WRITE macro.
- added missing documentation fields of ad5758_state struct.
- changed the type of pwr_down attribute to bool.
- changed ad5758_dc_dc_ilimt[] to ad5758_dc_dc_ilim[].
- ad5758_spi_reg_write() now returns spi_write(st->spi,
  >data[0].d32, sizeof(st->data[0].d32));
- removed unnecessary new line in ad5758_calib_mem_refresh().
- changed the type of the mode parameter in
  ad5758_set_dc_dc_conv_mode() from unsigned int to enum
  ad5758_dc_dc_mode.
- removed unnecessary parenthesis in ad5758_slew_rate_config().
- changed the type of the enable parameter in
  ad5758_fault_prot_switch_en() from unsigned char to bool.
- the same as above, but for ad5758_internal_buffers_en().
- added a missing mutex_unlock() in ad5758_reg_access().
- moved the mutex_unlock() in ad5758_read_raw() and removed the
  unreachable return.
- returned directly where it was possible in ad5758_write_raw().
- removed the channel, scan_type and scan_index fields.
- in ad5758_parse_dt(), added missing "\n", and specified what the
  default mode actually is.
- returned directly at the end of ad5758_init().
- in ad5758_probe() used device managed for registering the device
  and returned directly without the error message.

  MAINTAINERS  |   7 +
  drivers/iio/dac/Kconfig  |  10 +
  drivers/iio/dac/Makefile |   1 +
  drivers/iio/dac/ad5758.c | 899 +++
  4 files changed, 917 insertions(+)
  create mode 100644 drivers/iio/dac/ad5758.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 00e9670..12d102d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -796,6 +796,13 @@ M: Michael Hanselmann 
  S:Supported
  F:drivers/macintosh/ams/
  
+ANALOG DEVICES INC AD5758 DRIVER

+M: Stefan Popa 
+L: linux-...@vger.kernel.org
+W: http://ez.analog.com/community/linux-device-drivers
+S: Supported
+F: drivers/iio/dac/ad5758.c
+
  ANALOG DEVICES INC AD5686 DRIVER
  M:Stefan Popa 
  L:linux...@vger.kernel.org
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index 06e90de..80beb64 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -167,6 +167,16 @@ config AD5755
  To compile this driver as a module, choose M here: the
  module will be called ad5755.
  
+config AD5758

+   tristate "Analog Devices AD5758 DAC driver"
+   depends on SPI_MASTER
+   help
+ Say yes here to build support for Analog Devices AD5758 single channel
+ Digital to Analog Converter.
+
+ To compile this driver as a module, choose M here: the
+ module will be called ad5758.
+
  config AD5761
tristate "Analog Devices AD5761/61R/21/21R DAC driver"
depends on SPI_MASTER
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index 57aa230..a1b37cf 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD5592R_BASE) += ad5592r-base.o
  obj-$(CONFIG_AD5592R) += ad5592r.o
  obj-$(CONFIG_AD5593R) += ad5593r.o
  obj-$(CONFIG_AD5755) += ad5755.o
+obj-$(CONFIG_AD5755) += ad5758.o
  obj-$(CONFIG_AD5761) += ad5761.o
  obj-$(CONFIG_AD5764) += ad5764.o
  obj-$(CONFIG_AD5791) += ad5791.o
diff --git a/drivers/iio/dac/ad5758.c b/drivers/iio/dac/ad5758.c
new file 

[PATCH v4 1/2] iio: dac: Add AD5758 support

2018-06-29 Thread Stefan Popa
The AD5758 is a single channel DAC with 16-bit precision which uses the
SPI interface that operates at clock rates up to 50MHz.

The output can be configured as voltage or current and is available on a
single terminal.

Datasheet:
http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf

Signed-off-by: Stefan Popa 
---
Changes in v4:
- fixed kbuild test robot warnings.
Changes in v3:
- AD5758 can be both a current and voltage output DAC. The
  decision is made based on the DT and the channel type is set
  during probe.
- dc-dc-mode, range-microvolt and range-microamp are required
  properties.
- Introduced a slew-time-us property from which slew rate clock
  and slew rate step are calculated using a best match algorithm.
- Dropped the union from ad5758_state struct.
- Introduced a IIO_CHAN_INFO_OFFSET case part of ad5758_read_raw().
- Added a TODO comment which specifies that CRC is not supported.
- Kept the includes in order and removed the unused ones.
- Removed unused macros and shortened the lengthy ones.
- Renamed AD5758_REG_WRITE to AD5758_WR_FLAG_MSK.
- Added an explanation for enum ad5758_output_range.
- Used bsearch() instead of ad5758_get_array_index().
- Reduced the delays.
- strtobool() -> kstrtobool().

Changes in v2:
- removed unnecessary parenthesis in AD5758_REG_WRITE macro.
- added missing documentation fields of ad5758_state struct.
- changed the type of pwr_down attribute to bool.
- changed ad5758_dc_dc_ilimt[] to ad5758_dc_dc_ilim[].
- ad5758_spi_reg_write() now returns spi_write(st->spi,
  >data[0].d32, sizeof(st->data[0].d32));
- removed unnecessary new line in ad5758_calib_mem_refresh().
- changed the type of the mode parameter in
  ad5758_set_dc_dc_conv_mode() from unsigned int to enum
  ad5758_dc_dc_mode.
- removed unnecessary parenthesis in ad5758_slew_rate_config().
- changed the type of the enable parameter in
  ad5758_fault_prot_switch_en() from unsigned char to bool.
- the same as above, but for ad5758_internal_buffers_en().
- added a missing mutex_unlock() in ad5758_reg_access().
- moved the mutex_unlock() in ad5758_read_raw() and removed the
  unreachable return.
- returned directly where it was possible in ad5758_write_raw().
- removed the channel, scan_type and scan_index fields.
- in ad5758_parse_dt(), added missing "\n", and specified what the
  default mode actually is.
- returned directly at the end of ad5758_init().
- in ad5758_probe() used device managed for registering the device
  and returned directly without the error message.

 MAINTAINERS  |   7 +
 drivers/iio/dac/Kconfig  |  10 +
 drivers/iio/dac/Makefile |   1 +
 drivers/iio/dac/ad5758.c | 899 +++
 4 files changed, 917 insertions(+)
 create mode 100644 drivers/iio/dac/ad5758.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 00e9670..12d102d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -796,6 +796,13 @@ M: Michael Hanselmann 
 S: Supported
 F: drivers/macintosh/ams/
 
+ANALOG DEVICES INC AD5758 DRIVER
+M: Stefan Popa 
+L: linux-...@vger.kernel.org
+W: http://ez.analog.com/community/linux-device-drivers
+S: Supported
+F: drivers/iio/dac/ad5758.c
+
 ANALOG DEVICES INC AD5686 DRIVER
 M: Stefan Popa 
 L: linux...@vger.kernel.org
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index 06e90de..80beb64 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -167,6 +167,16 @@ config AD5755
  To compile this driver as a module, choose M here: the
  module will be called ad5755.
 
+config AD5758
+   tristate "Analog Devices AD5758 DAC driver"
+   depends on SPI_MASTER
+   help
+ Say yes here to build support for Analog Devices AD5758 single channel
+ Digital to Analog Converter.
+
+ To compile this driver as a module, choose M here: the
+ module will be called ad5758.
+
 config AD5761
tristate "Analog Devices AD5761/61R/21/21R DAC driver"
depends on SPI_MASTER
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index 57aa230..a1b37cf 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD5592R_BASE) += ad5592r-base.o
 obj-$(CONFIG_AD5592R) += ad5592r.o
 obj-$(CONFIG_AD5593R) += ad5593r.o
 obj-$(CONFIG_AD5755) += ad5755.o
+obj-$(CONFIG_AD5755) += ad5758.o
 obj-$(CONFIG_AD5761) += ad5761.o
 obj-$(CONFIG_AD5764) += ad5764.o
 obj-$(CONFIG_AD5791) += ad5791.o
diff --git a/drivers/iio/dac/ad5758.c b/drivers/iio/dac/ad5758.c
new file mode 100644
index 000..bd1bed7
--- /dev/null
+++ b/drivers/iio/dac/ad5758.c
@@ -0,0 

[PATCH v4 1/2] iio: dac: Add AD5758 support

2018-06-29 Thread Stefan Popa
The AD5758 is a single channel DAC with 16-bit precision which uses the
SPI interface that operates at clock rates up to 50MHz.

The output can be configured as voltage or current and is available on a
single terminal.

Datasheet:
http://www.analog.com/media/en/technical-documentation/data-sheets/ad5758.pdf

Signed-off-by: Stefan Popa 
---
Changes in v4:
- fixed kbuild test robot warnings.
Changes in v3:
- AD5758 can be both a current and voltage output DAC. The
  decision is made based on the DT and the channel type is set
  during probe.
- dc-dc-mode, range-microvolt and range-microamp are required
  properties.
- Introduced a slew-time-us property from which slew rate clock
  and slew rate step are calculated using a best match algorithm.
- Dropped the union from ad5758_state struct.
- Introduced a IIO_CHAN_INFO_OFFSET case part of ad5758_read_raw().
- Added a TODO comment which specifies that CRC is not supported.
- Kept the includes in order and removed the unused ones.
- Removed unused macros and shortened the lengthy ones.
- Renamed AD5758_REG_WRITE to AD5758_WR_FLAG_MSK.
- Added an explanation for enum ad5758_output_range.
- Used bsearch() instead of ad5758_get_array_index().
- Reduced the delays.
- strtobool() -> kstrtobool().

Changes in v2:
- removed unnecessary parenthesis in AD5758_REG_WRITE macro.
- added missing documentation fields of ad5758_state struct.
- changed the type of pwr_down attribute to bool.
- changed ad5758_dc_dc_ilimt[] to ad5758_dc_dc_ilim[].
- ad5758_spi_reg_write() now returns spi_write(st->spi,
  >data[0].d32, sizeof(st->data[0].d32));
- removed unnecessary new line in ad5758_calib_mem_refresh().
- changed the type of the mode parameter in
  ad5758_set_dc_dc_conv_mode() from unsigned int to enum
  ad5758_dc_dc_mode.
- removed unnecessary parenthesis in ad5758_slew_rate_config().
- changed the type of the enable parameter in
  ad5758_fault_prot_switch_en() from unsigned char to bool.
- the same as above, but for ad5758_internal_buffers_en().
- added a missing mutex_unlock() in ad5758_reg_access().
- moved the mutex_unlock() in ad5758_read_raw() and removed the
  unreachable return.
- returned directly where it was possible in ad5758_write_raw().
- removed the channel, scan_type and scan_index fields.
- in ad5758_parse_dt(), added missing "\n", and specified what the
  default mode actually is.
- returned directly at the end of ad5758_init().
- in ad5758_probe() used device managed for registering the device
  and returned directly without the error message.

 MAINTAINERS  |   7 +
 drivers/iio/dac/Kconfig  |  10 +
 drivers/iio/dac/Makefile |   1 +
 drivers/iio/dac/ad5758.c | 899 +++
 4 files changed, 917 insertions(+)
 create mode 100644 drivers/iio/dac/ad5758.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 00e9670..12d102d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -796,6 +796,13 @@ M: Michael Hanselmann 
 S: Supported
 F: drivers/macintosh/ams/
 
+ANALOG DEVICES INC AD5758 DRIVER
+M: Stefan Popa 
+L: linux-...@vger.kernel.org
+W: http://ez.analog.com/community/linux-device-drivers
+S: Supported
+F: drivers/iio/dac/ad5758.c
+
 ANALOG DEVICES INC AD5686 DRIVER
 M: Stefan Popa 
 L: linux...@vger.kernel.org
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index 06e90de..80beb64 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -167,6 +167,16 @@ config AD5755
  To compile this driver as a module, choose M here: the
  module will be called ad5755.
 
+config AD5758
+   tristate "Analog Devices AD5758 DAC driver"
+   depends on SPI_MASTER
+   help
+ Say yes here to build support for Analog Devices AD5758 single channel
+ Digital to Analog Converter.
+
+ To compile this driver as a module, choose M here: the
+ module will be called ad5758.
+
 config AD5761
tristate "Analog Devices AD5761/61R/21/21R DAC driver"
depends on SPI_MASTER
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index 57aa230..a1b37cf 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD5592R_BASE) += ad5592r-base.o
 obj-$(CONFIG_AD5592R) += ad5592r.o
 obj-$(CONFIG_AD5593R) += ad5593r.o
 obj-$(CONFIG_AD5755) += ad5755.o
+obj-$(CONFIG_AD5755) += ad5758.o
 obj-$(CONFIG_AD5761) += ad5761.o
 obj-$(CONFIG_AD5764) += ad5764.o
 obj-$(CONFIG_AD5791) += ad5791.o
diff --git a/drivers/iio/dac/ad5758.c b/drivers/iio/dac/ad5758.c
new file mode 100644
index 000..bd1bed7
--- /dev/null
+++ b/drivers/iio/dac/ad5758.c
@@ -0,0