Improvements and Enhancements for AD5791 DAC Driver
Subsystems (3)
v3 cover Improvements and Enhancements for AD5791 DAC Driver
From: Axel Haslam <[email protected]>
These patches aim to improve on the ad5791 driver:
- make use of chip_info / match tables, and drop device enum id.
- Add reset, clr and ldac gpios that have to be set to the correct level in case they are not hardwired on the setup/PCB.
- simplify probe by using the devm_* functions to automatically free resources.
---
Changes in v3:
- v2 is missing the version prefix. Im sending v3 just with the added review-by tag.
- Add review-by tag from David Lechner
- Link to V2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Fix probe error print using uninitialized ret.
- Add documentation for new struct parameters
- Add review-by tags to device tree bindings
- Link to V1: https://lore.kernel.org/all/CAKXjFTPwN2TYW6sq1kj3miZ0f5OqKX0aTk8eGf1sj9TBk1_e=A@mail.gmail.com/T/
Axel Haslam (6):
dt-bindings: iio: dac: ad5791: Add optional reset, clr and ldac gpios
dt-bindings: iio: dac: ad5791: Add required voltage supplies
iio: dac: ad5791: Include chip_info in device match tables
iio: dac: ad5791: Add reset, clr and ldac gpios
iio: dac: ad5791: Use devm_regulator_get_enable_read_voltage
iio: dac: ad5791: Use devm_iio_device_register
.../bindings/iio/dac/adi,ad5791.yaml | 39 ++++ drivers/iio/dac/ad5791.c | 203 ++++++++---------- 2 files changed, 131 insertions(+), 111 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/6 dt-bindings: iio: dac: ad5791: Add optional reset, clr and ldac gpios
From: Axel Haslam <[email protected]>
Depending on board layout, the ad57xx may need control of reset, clear, and ldac pins by the host driver. Add optional bindings for these gpios.
--- .../devicetree/bindings/iio/dac/adi,ad5791.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
Documentation/devicetree/bindings/iio/dac/adi,ad5791.yaml +15 -0
@@ -31,6 +31,17 @@ properties: gain of two configuration. type: boolean + reset-gpios: + maxItems: 1 + + clear-gpios: + maxItems: 1 + + ldac-gpios: + description: + LDAC pin to be used as a hardware trigger to update the DAC channels. + maxItems: 1 + required: - compatible - reg
@@ -44,6 +55,7 @@ unevaluatedProperties: false examples: - | + #include <dt-bindings/gpio/gpio.h> spi { #address-cells = <1>; #size-cells = <0>;
@@ -53,6 +65,9 @@ examples: reg = <0>; vss-supply = <&dac_vss>; vdd-supply = <&dac_vdd>; + reset-gpios = <&gpio_bd 16 GPIO_ACTIVE_LOW>; + clear-gpios = <&gpio_bd 17 GPIO_ACTIVE_LOW>; + ldac-gpios = <&gpio_bd 18 GPIO_ACTIVE_HIGH>; }; }; ...
Signature · 1 line
2.34.1
PATCH v3 2/6 dt-bindings: iio: dac: ad5791: Add required voltage supplies
In reply to [email protected]From: Axel Haslam <[email protected]>
Vcc, iovcc, vrefp, and vrefn are needed for the DAC to work. Add them as required bindings for ad5791.
--- .../bindings/iio/dac/adi,ad5791.yaml | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/iio/dac/adi,ad5791.yaml +24 -0
@@ -26,6 +26,22 @@ properties: vdd-supply: true vss-supply: true + vcc-supply: + description: + Supply that powers the chip. + + iovcc-supply: + description: + Supply for the digital interface. + + vrefp-supply: + description: + Positive referance input voltage range. From 5v to (vdd - 2.5) + + vrefn-supply: + description: + Negative referance input voltage range. From (vss + 2.5) to 0. + adi,rbuf-gain2-en: description: Specify to allow an external amplifier to be connected in a gain of two configuration.
@@ -47,6 +63,10 @@ required: - reg - vdd-supply - vss-supply + - vcc-supply + - iovcc-supply + - vrefp-supply + - vrefn-supply allOf: - $ref: /schemas/spi/spi-peripheral-props.yaml#
@@ -65,6 +85,10 @@ examples: reg = <0>; vss-supply = <&dac_vss>; vdd-supply = <&dac_vdd>; + vcc-supply = <&dac_vcc>; + iovcc-supply = <&dac_iovcc>; + vrefp-supply = <&dac_vrefp>; + vrefn-supply = <&dac_vrefn>; reset-gpios = <&gpio_bd 16 GPIO_ACTIVE_LOW>; clear-gpios = <&gpio_bd 17 GPIO_ACTIVE_LOW>; ldac-gpios = <&gpio_bd 18 GPIO_ACTIVE_HIGH>;
Signature · 1 line
2.34.1
PATCH v3 3/6 iio: dac: ad5791: Include chip_info in device match tables
In reply to [email protected]From: Axel Haslam <[email protected]>
Include a chip info struct in device SPI and device OF match tables to provide channel definitions for each particular ADC model and drop device enum.
--- drivers/iio/dac/ad5791.c | 110 +++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 57 deletions(-)
drivers/iio/dac/ad5791.c +53 -57
@@ -61,11 +61,14 @@/** * struct ad5791_chip_info - chip specific information +* @name: name of the dac chip +* @channel: channel specification * @get_lin_comp: function pointer to the device specific function */ -struct ad5791_chip_info { -int (*get_lin_comp) (unsigned int span); +const char *name; +const struct iio_chan_spec channel; +int (*get_lin_comp)(unsigned int span); };/**
@@ -98,13 +101,6 @@ struct ad5791_state { } data[3 ]__aligned(IIO_DMA_MINALIGN) ; }; -enum ad5791_supported_device_ids { -ID_AD5760 , -ID_AD5780 , -ID_AD5781 , -ID_AD5791 , -}; -static int ad5791_spi_write (struct ad5791_state *st,u8 addr,u32 val) { st->data[0].d32 = cpu_to_be32(AD5791_CMD_WRITE |
@@ -228,20 +224,6 @@ static int ad5780_get_lin_comp(unsigned int span)else return AD5780_LINCOMP_10_20 ; } -static const struct ad5791_chip_info ad5791_chip_info_tbl[] = { - [ID_AD5760 ] = { - .get_lin_comp = ad5780_get_lin_comp, - }, - [ID_AD5780 ] = { - .get_lin_comp = ad5780_get_lin_comp, - }, - [ID_AD5781 ] = { - .get_lin_comp = ad5791_get_lin_comp, - }, - [ID_AD5791 ] = { - .get_lin_comp = ad5791_get_lin_comp, - }, -}; static int ad5791_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
@@ -289,30 +271,34 @@ static const struct iio_chan_spec_ext_info ad5791_ext_info[] = { { }, }; -#define AD5791_CHAN (bits, _shift) { \ - .type = IIO_VOLTAGE, \ - .output = 1, \ - .indexed = 1, \ - .address = AD5791_ADDR_DAC0, \ - .channel = 0, \ - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ - BIT(IIO_CHAN_INFO_OFFSET), \ - .scan_type = { \ - .sign = 'u', \ - .realbits = (bits), \ - .storagebits = 24, \ - .shift = (_shift), \ - }, \ - .ext_info = ad5791_ext_info, \ +#define AD5791_DEFINE_CHIP_INFO (_name, bits, _shift, _lin_comp) \ +static const struct ad5791_chip_info _name##_chip_info = { \ + .name = #_name, \ + .get_lin_comp = &(_lin_comp), \ + .channel = { \ + .type = IIO_VOLTAGE, \ + .output = 1, \ + .indexed = 1, \ + .address = AD5791_ADDR_DAC0, \ + .channel = 0, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ + BIT(IIO_CHAN_INFO_OFFSET), \ + .scan_type = { \ + .sign = 'u', \ + .realbits = (bits), \ + .storagebits = 24, \ + .shift = (_shift), \ + }, \ + .ext_info = ad5791_ext_info, \ + }, \ } -static const struct iio_chan_spec ad5791_channels[] = { - [ID_AD5760 ] =AD5791_CHAN (16 ,4 ), - [ID_AD5780 ] =AD5791_CHAN (18 ,2 ), - [ID_AD5781 ] =AD5791_CHAN (18 ,2 ), - [ID_AD5791 ] =AD5791_CHAN (20 ,0 ) -}; +AD5791_DEFINE_CHIP_INFO (ad5760,16 ,4 , ad5780_get_lin_comp); +AD5791_DEFINE_CHIP_INFO (ad5780,18 ,2 , ad5780_get_lin_comp); +AD5791_DEFINE_CHIP_INFO (ad5781,18 ,2 , ad5791_get_lin_comp); +AD5791_DEFINE_CHIP_INFO (ad5790,20 ,0 , ad5791_get_lin_comp); +AD5791_DEFINE_CHIP_INFO (ad5791,20 ,0 , ad5791_get_lin_comp); static int ad5791_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
@@ -400,9 +386,9 @@ static int ad5791_probe(struct spi_device *spi)if (ret) goto error_disable_reg_neg; - st->chip_info = &ad5791_chip_info_tbl[spi_get_device_id (spi) - ->driver_data]; - + st->chip_info =spi_get_device_match_data (spi); +if (!st->chip_info) +return dev_err_probe (&spi->dev, -EINVAL ,"no chip info\n" ); st->ctrl =AD5761_CTRL_LINCOMP (st->chip_info->get_lin_comp (st->vref_mv)) | (use_rbuf_gain2 ? 0 : AD5791_CTRL_RBUF) |
@@ -416,10 +402,9 @@ static int ad5791_probe(struct spi_device *spi)spi_set_drvdata (spi, indio_dev); indio_dev->info = &ad5791_info; indio_dev->modes =INDIO_DIRECT_MODE ; - indio_dev->channels - = &ad5791_channels[spi_get_device_id (spi)->driver_data]; + indio_dev->channels = &st->chip_info->channel; indio_dev->num_channels =1 ; - indio_dev->name =spi_get_device_id (st->spi)->name; + indio_dev->name = st->chip_info->name; ret =iio_device_register (indio_dev);if (ret) goto error_disable_reg_neg;
@@ -448,19 +433,30 @@ static void ad5791_remove(struct spi_device *spi)regulator_disable (st->reg_vss); } +static const struct of_device_id ad5791_of_match[] = { + { .compatible ="adi,ad5760" , .data = &ad5760_chip_info }, + { .compatible ="adi,ad5780" , .data = &ad5780_chip_info }, + { .compatible ="adi,ad5781" , .data = &ad5781_chip_info }, + { .compatible ="adi,ad5790" , .data = &ad5790_chip_info }, + { .compatible ="adi,ad5791" , .data = &ad5791_chip_info }, + { } +}; +MODULE_DEVICE_TABLE (of, ad5791_of_match); +static const struct spi_device_id ad5791_id[] = { - {"ad5760" ,ID_AD5760 }, - {"ad5780" ,ID_AD5780 }, - {"ad5781" ,ID_AD5781 }, - {"ad5790" ,ID_AD5791 }, - {"ad5791" ,ID_AD5791 }, - {} + {"ad5760" , (kernel_ulong_t)&ad5760_chip_info }, + {"ad5780" , (kernel_ulong_t)&ad5780_chip_info }, + {"ad5781" , (kernel_ulong_t)&ad5781_chip_info }, + {"ad5790" , (kernel_ulong_t)&ad5790_chip_info }, + {"ad5791" , (kernel_ulong_t)&ad5791_chip_info }, + { } };MODULE_DEVICE_TABLE (spi, ad5791_id);static struct spi_driver ad5791_driver = { .driver = { .name ="ad5791" , + .of_match_table = ad5791_of_match, }, .probe = ad5791_probe, .remove = ad5791_remove,
Signature · 1 line
2.34.1
PATCH v3 4/6 iio: dac: ad5791: Add reset, clr and ldac gpios
In reply to [email protected]From: Axel Haslam <[email protected]>
The ad7591 has reset, clr and ldac gpios. For the DAC to output data continuously written to the data register the state of these gpios needs to be set by the driver.
Add these gpios to the driver making them optional in case they are fixed on the pcb.
--- drivers/iio/dac/ad5791.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-)
drivers/iio/dac/ad5791.c +31 -3
@@ -9,6 +9,7 @@#include <linux/interrupt.h> #include <linux/fs.h> #include <linux/device.h> +#include <linux/delay.h> #include <linux/kernel.h> #include <linux/spi/spi.h> #include <linux/slab.h>
@@ -76,6 +77,9 @@ struct ad5791_chip_info {* @spi: spi_device * @reg_vdd: positive supply regulator * @reg_vss: negative supply regulator +* @gpio_reset: reset gpio +* @gpio_clear: clear gpio +* @gpio_ldac: load dac gpio * @chip_info: chip model specific constants * @vref_mv: actual reference voltage used * @vref_neg_mv: voltage of the negative supply
@@ -88,6 +92,9 @@ struct ad5791_state {struct spi_device *spi;struct regulator *reg_vdd;struct regulator *reg_vss; +struct gpio_desc *gpio_reset; +struct gpio_desc *gpio_clear; +struct gpio_desc *gpio_ldac;const struct ad5791_chip_info *chip_info;unsigned short vref_mv;unsigned int vref_neg_mv;
@@ -337,6 +344,22 @@ static int ad5791_probe(struct spi_device *spi)if (!indio_dev)return -ENOMEM ; st =iio_priv (indio_dev); + + st->gpio_reset =devm_gpiod_get_optional (&spi->dev,"reset" , +GPIOD_OUT_HIGH ); +if (IS_ERR (st->gpio_reset)) +return PTR_ERR (st->gpio_reset); + + st->gpio_clear =devm_gpiod_get_optional (&spi->dev,"clear" , +GPIOD_OUT_LOW ); +if (IS_ERR (st->gpio_clear)) +return PTR_ERR (st->gpio_clear); + + st->gpio_ldac =devm_gpiod_get_optional (&spi->dev,"ldac" , +GPIOD_OUT_HIGH ); +if (IS_ERR (st->gpio_ldac)) +return PTR_ERR (st->gpio_ldac); + st->reg_vdd =devm_regulator_get (&spi->dev,"vdd" );if (!IS_ERR (st->reg_vdd)) { ret =regulator_enable (st->reg_vdd);
@@ -382,9 +405,14 @@ static int ad5791_probe(struct spi_device *spi)dev_warn (&spi->dev,"reference voltage unspecified\n" ); } - ret =ad5791_spi_write (st,AD5791_ADDR_SW_CTRL ,AD5791_SWCTRL_RESET ); -if (ret) - goto error_disable_reg_neg; +if (st->gpio_reset) { +fsleep (20 ); +gpiod_set_value_cansleep (st->gpio_reset,0 ); + }else { + ret =ad5791_spi_write (st,AD5791_ADDR_SW_CTRL ,AD5791_SWCTRL_RESET ); +if (ret) + goto error_disable_reg_neg; + } st->chip_info =spi_get_device_match_data (spi);if (!st->chip_info)
Signature · 1 line
2.34.1
PATCH v3 5/6 iio: dac: ad5791: Use devm_regulator_get_enable_read_voltage
In reply to [email protected]From: Axel Haslam <[email protected]>
Simplify probe by using of the devm_regulator_get_enable_read_voltage.
--- drivers/iio/dac/ad5791.c | 58 ++++++++++------------------------------ 1 file changed, 14 insertions(+), 44 deletions(-)
drivers/iio/dac/ad5791.c +14 -44
@@ -360,32 +360,6 @@ static int ad5791_probe(struct spi_device *spi)if (IS_ERR (st->gpio_ldac))return PTR_ERR (st->gpio_ldac); - st->reg_vdd =devm_regulator_get (&spi->dev,"vdd" ); -if (!IS_ERR (st->reg_vdd)) { - ret =regulator_enable (st->reg_vdd); -if (ret) -return ret; - - ret =regulator_get_voltage (st->reg_vdd); -if (ret <0 ) - goto error_disable_reg_pos; - - pos_voltage_uv = ret; - } - - st->reg_vss =devm_regulator_get (&spi->dev,"vss" ); -if (!IS_ERR (st->reg_vss)) { - ret =regulator_enable (st->reg_vss); -if (ret) - goto error_disable_reg_pos; - - ret =regulator_get_voltage (st->reg_vss); -if (ret <0 ) - goto error_disable_reg_neg; - - neg_voltage_uv = ret; - } - st->pwr_down = true; st->spi = spi;
@@ -395,7 +369,17 @@ static int ad5791_probe(struct spi_device *spi) use_rbuf_gain2 =device_property_read_bool (&spi->dev,"adi,rbuf-gain2-en" ); -if (!IS_ERR (st->reg_vss) && !IS_ERR (st->reg_vdd)) { + pos_voltage_uv =devm_regulator_get_enable_read_voltage (&spi->dev,"vdd" ); +if (pos_voltage_uv <0 && pos_voltage_uv != -ENODEV ) +return dev_err_probe (&spi->dev, pos_voltage_uv, +"failed to get vdd voltage\n" ); + + neg_voltage_uv =devm_regulator_get_enable_read_voltage (&spi->dev,"vss" ); +if (neg_voltage_uv <0 && neg_voltage_uv != -ENODEV ) +return dev_err_probe (&spi->dev, neg_voltage_uv, +"failed to get vss voltage\n" ); + +if (neg_voltage_uv >=0 && pos_voltage_uv >=0 ) { st->vref_mv = (pos_voltage_uv + neg_voltage_uv) /1000 ; st->vref_neg_mv = neg_voltage_uv /1000 ; }else if (pdata) {
@@ -411,7 +395,7 @@ static int ad5791_probe(struct spi_device *spi) }else { ret =ad5791_spi_write (st,AD5791_ADDR_SW_CTRL ,AD5791_SWCTRL_RESET );if (ret) - goto error_disable_reg_neg; +return dev_err_probe (&spi->dev, ret,"fail to reset\n" ); } st->chip_info =spi_get_device_match_data (spi);
@@ -425,7 +409,7 @@ static int ad5791_probe(struct spi_device *spi) ret =ad5791_spi_write (st,AD5791_ADDR_CTRL , st->ctrl |AD5791_CTRL_OPGND |AD5791_CTRL_DACTRI );if (ret) - goto error_disable_reg_neg; +return dev_err_probe (&spi->dev, ret,"fail to write ctrl register\n" );spi_set_drvdata (spi, indio_dev); indio_dev->info = &ad5791_info;
@@ -435,30 +419,16 @@ static int ad5791_probe(struct spi_device *spi) indio_dev->name = st->chip_info->name; ret =iio_device_register (indio_dev);if (ret) - goto error_disable_reg_neg; +return dev_err_probe (&spi->dev, ret,"unable to register iio device\n" );return 0 ; - -error_disable_reg_neg: -if (!IS_ERR (st->reg_vss)) -regulator_disable (st->reg_vss); -error_disable_reg_pos: -if (!IS_ERR (st->reg_vdd)) -regulator_disable (st->reg_vdd); -return ret; }static void ad5791_remove (struct spi_device *spi) {struct iio_dev *indio_dev =spi_get_drvdata (spi); -struct ad5791_state *st =iio_priv (indio_dev);iio_device_unregister (indio_dev); -if (!IS_ERR (st->reg_vdd)) -regulator_disable (st->reg_vdd); - -if (!IS_ERR (st->reg_vss)) -regulator_disable (st->reg_vss); }static const struct of_device_id ad5791_of_match[] = {
Signature · 1 line
2.34.1
PATCH v3 6/6 iio: dac: ad5791: Use devm_iio_device_register
In reply to [email protected]From: Axel Haslam <[email protected]>
Use devm_iio_device_register to automatically free the iio device. since this is the last remaining resource that was not automatically freed, we can drop the ".remove" callback.
--- drivers/iio/dac/ad5791.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/iio/dac/ad5791.c +1 -14
@@ -411,24 +411,12 @@ static int ad5791_probe(struct spi_device *spi)if (ret)return dev_err_probe (&spi->dev, ret,"fail to write ctrl register\n" ); -spi_set_drvdata (spi, indio_dev); indio_dev->info = &ad5791_info; indio_dev->modes =INDIO_DIRECT_MODE ; indio_dev->channels = &st->chip_info->channel; indio_dev->num_channels =1 ; indio_dev->name = st->chip_info->name; - ret =iio_device_register (indio_dev); -if (ret) -return dev_err_probe (&spi->dev, ret,"unable to register iio device\n" ); - -return 0 ; -} - -static void ad5791_remove (struct spi_device *spi) -{ -struct iio_dev *indio_dev =spi_get_drvdata (spi); - -iio_device_unregister (indio_dev); +return devm_iio_device_register (&spi->dev, indio_dev); }static const struct of_device_id ad5791_of_match[] = {
@@ -457,7 +445,6 @@ static struct spi_driver ad5791_driver = { .of_match_table = ad5791_of_match, }, .probe = ad5791_probe, - .remove = ad5791_remove, .id_table = ad5791_id, };module_spi_driver (ad5791_driver);
Signature · 1 line
2.34.1
Quoted 7 lines · On Thu, 31 Oct 2024 08:17:40 +0100 [email protected] wrote:
From: Axel Haslam <[email protected]>
These patches aim to improve on the ad5791 driver:
- make use ofchip_info/ match tables, and drop device enum id.
- Add reset, clr and ldac gpios that have to be set to the correct level in case they are not hardwired on the setup/PCB.
- simplify probe by using the devm_* functions to automatically free resources.
Applied. Thanks,
Quoted 24 lines
---
Changes in v3:
- v2 is missing the version prefix. Im sending v3 just with the added review-by tag.
- Add review-by tag from David Lechner
- Link to V2: https://lore.kernel.org/all/[email protected]/Changes in v2:
- Fix probe error print using uninitialized ret.
- Add documentation for new struct parameters
- Add review-by tags to device tree bindings
- Link to V1: https://lore.kernel.org/all/CAKXjFTPwN2TYW6sq1kj3miZ0f5OqKX0aTk8eGf1sj9TBk1_e=A@mail.gmail.com/T/Axel Haslam (6):
dt-bindings: iio: dac: ad5791: Add optional reset, clr and ldac gpios
dt-bindings: iio: dac: ad5791: Add required voltage supplies
iio: dac: ad5791: Includechip_infoin device match tables
iio: dac: ad5791: Add reset, clr and ldac gpios
iio: dac: ad5791: Usedevm_regulator_get_enable_read_voltage
iio: dac: ad5791: Usedevm_iio_device_register.../bindings/iio/dac/adi,ad5791.yaml | 39 ++++ drivers/iio/dac/ad5791.c | 203 ++++++++---------- 2 files changed, 131 insertions(+), 111 deletions(-)