iio: adc: ad7192: Improvements
v4 cover iio: adc: ad7192: Improvements
Dear everyone,
Thank you very much for your feedback!
Here is the updated series. Please consider applying the commits in order!
Kind regards,
Alisa-Dariana Roman.
v3 -> v4
add clock provider patches
add clean up dev patch
as Nuno Sa pointed out, the old implementation of clock config was not
buggy, so the update clock config patches are no longer fixes
PATCH v4 1/5 iio: adc: ad7192: Clean up dev
Clean up by using a local variable struct device *dev. Also use dev_err_probe where possible.
--- drivers/iio/adc/ad7192.c | 65 +++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 35 deletions(-)
drivers/iio/adc/ad7192.c +30 -35
@@ -1196,17 +1196,16 @@ static void ad7192_reg_disable(void *reg)static int ad7192_probe (struct spi_device *spi) { +struct device *dev = &spi->dev;struct ad7192_state *st;struct iio_dev *indio_dev;struct regulator *aincom;int ret; -if (!spi->irq) { -dev_err (&spi->dev,"no IRQ?\n" ); -return -ENODEV ; - } +if (!spi->irq) +return dev_err_probe (dev, -ENODEV ,"Failed to get IRQ\n" ); - indio_dev =devm_iio_device_alloc (&spi->dev,sizeof (*st)); + indio_dev =devm_iio_device_alloc (dev,sizeof (*st));if (!indio_dev)return -ENOMEM ;
@@ -1219,71 +1218,69 @@ static int ad7192_probe(struct spi_device *spi)* Newer firmware should provide a zero volt fixed supply if wired to * ground. */ - aincom =devm_regulator_get_optional (&spi->dev,"aincom" ); + aincom =devm_regulator_get_optional (dev,"aincom" );if (IS_ERR (aincom)) {if (PTR_ERR (aincom) != -ENODEV ) -return dev_err_probe (&spi->dev,PTR_ERR (aincom), +return dev_err_probe (dev,PTR_ERR (aincom),"Failed to get AINCOM supply\n" ); st->aincom_mv =0 ; }else { ret =regulator_enable (aincom);if (ret) -return dev_err_probe (&spi->dev, ret, +return dev_err_probe (dev, ret,"Failed to enable specified AINCOM supply\n" ); - ret =devm_add_action_or_reset (&spi->dev, ad7192_reg_disable, aincom); + ret =devm_add_action_or_reset (dev, ad7192_reg_disable, aincom);if (ret)return ret; ret =regulator_get_voltage (aincom);if (ret <0 ) -return dev_err_probe (&spi->dev, ret, +return dev_err_probe (dev, ret,"Device tree error, AINCOM voltage undefined\n" ); st->aincom_mv = ret /MILLI ; } - st->avdd =devm_regulator_get (&spi->dev,"avdd" ); + st->avdd =devm_regulator_get (dev,"avdd" );if (IS_ERR (st->avdd))return PTR_ERR (st->avdd); ret =regulator_enable (st->avdd); -if (ret) { -dev_err (&spi->dev,"Failed to enable specified AVdd supply\n" ); -return ret; - } +if (ret) +return dev_err_probe (dev, ret, +"Failed to enable specified AVdd supply\n" ); - ret =devm_add_action_or_reset (&spi->dev, ad7192_reg_disable, st->avdd); + ret =devm_add_action_or_reset (dev, ad7192_reg_disable, st->avdd);if (ret)return ret; - ret =devm_regulator_get_enable (&spi->dev,"dvdd" ); + ret =devm_regulator_get_enable (dev,"dvdd" );if (ret) -return dev_err_probe (&spi->dev, ret,"Failed to enable specified DVdd supply\n" ); +return dev_err_probe (dev, ret,"Failed to enable specified DVdd supply\n" ); - st->vref =devm_regulator_get_optional (&spi->dev,"vref" ); + st->vref =devm_regulator_get_optional (dev,"vref" );if (IS_ERR (st->vref)) {if (PTR_ERR (st->vref) != -ENODEV )return PTR_ERR (st->vref); ret =regulator_get_voltage (st->avdd);if (ret <0 ) -return dev_err_probe (&spi->dev, ret, +return dev_err_probe (dev, ret,"Device tree error, AVdd voltage undefined\n" ); }else { ret =regulator_enable (st->vref); -if (ret) { -dev_err (&spi->dev,"Failed to enable specified Vref supply\n" ); -return ret; - } +if (ret) +return dev_err_probe (dev, ret, +"Failed to enable specified Vref supply\n" ); - ret =devm_add_action_or_reset (&spi->dev, ad7192_reg_disable, st->vref); + ret =devm_add_action_or_reset (dev, ad7192_reg_disable, st->vref);if (ret)return ret; ret =regulator_get_voltage (st->vref);if (ret <0 ) -return dev_err_probe (&spi->dev, ret, +return dev_err_probe (dev, ret,"Device tree error, Vref voltage undefined\n" ); } st->int_vref_mv = ret /1000 ;
@@ -1305,13 +1302,13 @@ static int ad7192_probe(struct spi_device *spi)if (ret)return ret; - ret =devm_ad_sd_setup_buffer_and_trigger (&spi->dev, indio_dev); + ret =devm_ad_sd_setup_buffer_and_trigger (dev, indio_dev);if (ret)return ret; st->fclk =AD7192_INT_FREQ_MHZ ; - st->mclk =devm_clk_get_optional_enabled (&spi->dev,"mclk" ); + st->mclk =devm_clk_get_optional_enabled (dev,"mclk" );if (IS_ERR (st->mclk))return PTR_ERR (st->mclk);
@@ -1320,18 +1317,16 @@ static int ad7192_probe(struct spi_device *spi)if (st->clock_sel ==AD7192_CLK_EXT_MCLK1_2 || st->clock_sel ==AD7192_CLK_EXT_MCLK2 ) { st->fclk =clk_get_rate (st->mclk); -if (!ad7192_valid_external_frequency (st->fclk)) { -dev_err (&spi->dev, -"External clock frequency out of bounds\n" ); -return -EINVAL ; - } +if (!ad7192_valid_external_frequency (st->fclk)) +return dev_err_probe (dev, -EINVAL , +"External clock frequency out of bounds\n" ); } - ret =ad7192_setup (indio_dev, &spi->dev); + ret =ad7192_setup (indio_dev, dev);if (ret)return ret; -return devm_iio_device_register (&spi->dev, indio_dev); +return devm_iio_device_register (dev, indio_dev); }static const struct of_device_id ad7192_of_match[] = {
Signature · 1 line
2.34.1
Jonathan Cameron
maintainerRe: [PATCH v4 1/5] iio: adc: ad7192: Clean up dev
Quoted 4 lines · On Thu, 13 Jun 2024 14:39:57 +0300 Alisa-Dariana Roman…
Clean up by using a local variable struct device *dev. Also use
dev_err_probewhere possible.Signed-off-by Alisa-Dariana Roman <[email protected]>
Applied to the togreg branch of iio.git and pushed out as testing
Thanks,
Jonathan
PATCH v4 2/5 dt-bindings: iio: adc: ad7192: Update clock config
In reply to Alisa-Dariana RomanThere are actually 4 configuration modes of clock source for AD719X devices. Either a crystal can be attached externally between MCLK1 and MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 pin. The other 2 modes make use of the 4.92MHz internal clock.
Add clock name xtal alongside mclk. When an external crystal is attached, xtal should be chosen. When an external clock is used, mclk should be chosen.
The presence of an external clock source is optional, not required. When absent, internal clock is used. Modify required property accordingly and modify second example to showcase this.
--- .../devicetree/bindings/iio/adc/adi,ad7192.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +7 -7
@@ -39,11 +39,15 @@ properties: clocks: maxItems: 1 - description: phandle to the master clock (mclk) + description: | + Optionally, either a crystal can be attached externally between MCLK1 and + MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 + pin. If absent, internal 4.92MHz clock is used. clock-names: - items: - - const: mclk + enum: + - xtal + - mclk interrupts: maxItems: 1
@@ -135,8 +139,6 @@ patternProperties: required: - compatible - reg - - clocks - - clock-names - interrupts - dvdd-supply - avdd-supply
@@ -202,8 +204,6 @@ examples: spi-max-frequency = <1000000>; spi-cpol; spi-cpha; - clocks = <&ad7192_mclk>; - clock-names = "mclk"; interrupts = <25 0x2>; interrupt-parent = <&gpio>; aincom-supply = <&aincom>;
Signature · 1 line
2.34.1
Re: [PATCH v4 2/5] dt-bindings: iio: adc: ad7192: Update clock config
Quoted 8 lines · Alisa-Dariana Roman
There are actually 4 configuration modes of clock source for AD719X devices. Either a crystal can be attached externally between MCLK1 and MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 pin. The other 2 modes make use of the 4.92MHz internal clock.
Add clock name xtal alongside mclk. When an external crystal is attached, xtal should be chosen. When an external clock is used, mclk should be chosen.
This is still missing an explanation of why a new name is needed.
Hint: do you need to change register settings to use one versus the other?
Quoted 54 lines
The presence of an external clock source is optional, not required. When absent, internal clock is used. Modify required property accordingly and modify second example to showcase this.
Signed-off-by Alisa-Dariana Roman <[email protected]>--- .../devicetree/bindings/iio/adc/adi,ad7192.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +7
-7@@ -39,11 +39,15 @@ properties: clocks: maxItems: 1 - description: phandle to the master clock (mclk) + description: | + Optionally, either a crystal can be attached externally between MCLK1 and + MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 + pin. If absent, internal 4.92MHz clock is used. clock-names: - items: - - const: mclk + enum: + - xtal + - mclk interrupts: maxItems: 1@@ -135,8 +139,6 @@ patternProperties: required: - compatible - reg - - clocks - - clock-names - interrupts - dvdd-supply - avdd-supply@@ -202,8 +204,6 @@ examples: spi-max-frequency = <1000000>; spi-cpol; spi-cpha; - clocks = <&ad7192_mclk>; - clock-names = "mclk"; interrupts = <25 0x2>; interrupt-parent = <&gpio>; aincom-supply = <&aincom>;Signature · 1 line
2.34.1
Jonathan Cameron
maintainerRe: [PATCH v4 2/5] dt-bindings: iio: adc: ad7192: Update clock config
Quoted 6 lines · On Thu, 13 Jun 2024 17:41:52 +0100 Conor Dooley <[email protected]> wrote:
Quoted 5 lines · Alisa-Dariana Roman
There are actually 4 configuration modes of clock source for AD719X devices. Either a crystal can be attached externally between MCLK1 and MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 pin. The other 2 modes make use of the 4.92MHz internal clock.
Quoted 7 lines
Quoted 3 lines
Add clock name xtal alongside mclk. When an external crystal is attached, xtal should be chosen. When an external clock is used, mclk should be chosen.
This is still missing an explanation of why a new name is needed.
Hint: do you need to change register settings to use one versus the other?
Absolutely - dt reviewer shouldn't need to look at the code to find this out as it wastes their over subscribed time!
Quoted 17 lines
Quoted 17 lines
The presence of an external clock source is optional, not required. When absent, internal clock is used. Modify required property accordingly and modify second example to showcase this.
Signed-off-by Alisa-Dariana Roman <[email protected]>--- .../devicetree/bindings/iio/adc/adi,ad7192.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +0
-0@@ -39,11 +39,15 @@ properties:
Quoted 7 lines
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml properties: · line 45
Quoted 7 lines
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +4
-1clocks: maxItems: 1 - description: phandle to the master clock (mclk) + description: | + Optionally, either a crystal can be attached externally between MCLK1 and + MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 + pin. If absent, internal 4.92MHz clock is used.
Trivial but doesn't need to be formatted, so don't think the | matters.
Quoted 17 lines
Quoted 17 lines
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +3
-4clock-names: - items: - - const: mclk + enum: + - xtal + - mclk interrupts: maxItems: 1@@ -135,8 +139,6 @@ patternProperties: required: - compatible - reg - - clocks - - clock-names - interrupts
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml examples: · line 206
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +0
-2- dvdd-supply - avdd-supply@@ -202,8 +204,6 @@ examples: spi-max-frequency = <1000000>; spi-cpol; spi-cpha; - clocks = <&ad7192_mclk>; - clock-names = "mclk";
Why drop it from the example? It's a valid setting to have one after all.
Quoted 6 lines
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +0
-0interrupts = <25 0x2>; interrupt-parent = <&gpio>; aincom-supply = <&aincom>;Signature · 1 line
2.34.1
PATCH v4 3/5 iio: adc: ad7192: Update clock config
In reply to Alisa-Dariana RomanThere are actually 4 configuration modes of clock source for AD719X devices. Either a crystal can be attached externally between MCLK1 and MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 pin. The other 2 modes make use of the 4.92MHz internal clock.
Removed properties adi,int-clock-output-enable and adi,clock-xtal were undocumented. Use cleaner alternative of configuring external clock by using clock names mclk and xtal.
Removed functionality of AD7192_CLK_INT_CO restored in complementary patch.
--- drivers/iio/adc/ad7192.c | 56 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 28 deletions(-)
drivers/iio/adc/ad7192.c +28 -28
@@ -398,25 +398,37 @@ static inline bool ad7192_valid_external_frequency(u32 freq) freq <= AD7192_EXT_FREQ_MHZ_MAX); } -static int ad7192_clock_select (struct ad7192_state *st) +static const char *const ad7192_clock_names[] = { +"xtal" , +"mclk" +}; + +static int ad7192_clock_setup (struct ad7192_state *st) {struct device *dev = &st->sd.spi->dev; -unsigned int clock_sel; - - clock_sel =AD7192_CLK_INT ; +int ret; -/* use internal clock */ -if (!st->mclk) { -if (device_property_read_bool (dev,"adi,int-clock-output-enable" )) - clock_sel =AD7192_CLK_INT_CO ; + ret =device_property_match_property_string (dev,"clock-names" , + ad7192_clock_names, +ARRAY_SIZE (ad7192_clock_names)); +if (ret <0 ) { + st->clock_sel =AD7192_CLK_INT ; + st->fclk =AD7192_INT_FREQ_MHZ ; }else { -if (device_property_read_bool (dev,"adi,clock-xtal" )) - clock_sel =AD7192_CLK_EXT_MCLK1_2 ; -else - clock_sel =AD7192_CLK_EXT_MCLK2 ; + st->clock_sel =AD7192_CLK_EXT_MCLK1_2 + ret; + + st->mclk =devm_clk_get_enabled (dev, ad7192_clock_names[ret]); +if (IS_ERR (st->mclk)) +return dev_err_probe (dev,PTR_ERR (st->mclk), +"Failed to get mclk\n" ); + + st->fclk =clk_get_rate (st->mclk); +if (!ad7192_valid_external_frequency (st->fclk)) +return dev_err_probe (dev, -EINVAL , +"External clock frequency out of bounds\n" ); } -return clock_sel; +return 0 ; }static int ad7192_setup (struct iio_dev *indio_dev,struct device *dev)
@@ -1306,21 +1318,9 @@ static int ad7192_probe(struct spi_device *spi)if (ret)return ret; - st->fclk =AD7192_INT_FREQ_MHZ ; - - st->mclk =devm_clk_get_optional_enabled (dev,"mclk" ); -if (IS_ERR (st->mclk)) -return PTR_ERR (st->mclk); - - st->clock_sel =ad7192_clock_select (st); - -if (st->clock_sel ==AD7192_CLK_EXT_MCLK1_2 || - st->clock_sel ==AD7192_CLK_EXT_MCLK2 ) { - st->fclk =clk_get_rate (st->mclk); -if (!ad7192_valid_external_frequency (st->fclk)) -return dev_err_probe (dev, -EINVAL , -"External clock frequency out of bounds\n" ); - } + ret =ad7192_clock_setup (st); +if (ret) +return ret; ret =ad7192_setup (indio_dev, dev);if (ret)
Signature · 1 line
2.34.1
PATCH v4 4/5 dt-bindings: iio: adc: ad7192: Add clock provider
In reply to Alisa-Dariana RomanInternal clock of AD719X devices can be made available on MCLK2 pin. Add clock provider to support this functionality.
--- .../devicetree/bindings/iio/adc/adi,ad7192.yaml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +10 -1
@@ -42,13 +42,20 @@ properties: description: | Optionally, either a crystal can be attached externally between MCLK1 and MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 - pin. If absent, internal 4.92MHz clock is used. + pin. If absent, internal 4.92MHz clock is used, which can be made + available on MCLK2 pin. clock-names: enum: - xtal - mclk + "#clock-cells": + const: 0 + + clock-output-names: + maxItems: 1 + interrupts: maxItems: 1
@@ -204,6 +211,8 @@ examples: spi-max-frequency = <1000000>; spi-cpol; spi-cpha; + #clock-cells = <0>; + clock-output-names = "ad7194_mclk"; interrupts = <25 0x2>; interrupt-parent = <&gpio>; aincom-supply = <&aincom>;
Signature · 1 line
2.34.1
Re: [PATCH v4 4/5] dt-bindings: iio: adc: ad7192: Add clock provider
Quoted 20 lines · Alisa-Dariana Roman
Internal clock of AD719X devices can be made available on MCLK2 pin. Add clock provider to support this functionality.
Signed-off-by Alisa-Dariana Roman <[email protected]>--- .../devicetree/bindings/iio/adc/adi,ad7192.yaml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +2
-1@@ -42,13 +42,20 @@ properties: description: | Optionally, either a crystal can be attached externally between MCLK1 and MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 - pin. If absent, internal 4.92MHz clock is used. + pin. If absent, internal 4.92MHz clock is used, which can be made + available on MCLK2 pin.
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml properties: · line 57
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +5
-0clock-names: enum: - xtal - mclk + "#clock-cells": + const: 0 + + clock-output-names: + maxItems: 1
Why do you need an output name when you have exactly one clock?
PATCH v4 5/5 iio: adc: ad7192: Add clock provider
In reply to Alisa-Dariana RomanInternal clock of AD719X devices can be made available on MCLK2 pin. Add clock provider to support this functionality.
--- drivers/iio/adc/ad7192.c | 90 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+)
drivers/iio/adc/ad7192.c +90 -0
@@ -8,6 +8,7 @@#include <linux/interrupt.h> #include <linux/bitfield.h> #include <linux/clk.h> +#include <linux/clk-provider.h> #include <linux/device.h> #include <linux/kernel.h> #include <linux/slab.h>
@@ -203,6 +204,7 @@ struct ad7192_state {struct regulator *avdd;struct regulator *vref;struct clk *mclk; +struct clk_hw int_clk_hw;u16 int_vref_mv;u32 aincom_mv;u32 fclk;
@@ -403,6 +405,90 @@ static const char *const ad7192_clock_names[] = {"mclk" }; +static struct ad7192_state *clk_hw_to_ad7192 (struct clk_hw *hw) +{ + return container_of(hw, struct ad7192_state, int_clk_hw); +} + +static unsigned long ad7192_clk_recalc_rate (struct clk_hw *hw, +unsigned long parent_rate) +{ +return AD7192_INT_FREQ_MHZ ; +} + +static int ad7192_clk_output_is_enabled (struct clk_hw *hw) +{ +struct ad7192_state *st =clk_hw_to_ad7192 (hw); + +return st->clock_sel ==AD7192_CLK_INT_CO ; +} + +static int ad7192_clk_prepare (struct clk_hw *hw) +{ +struct ad7192_state *st =clk_hw_to_ad7192 (hw); +int ret; + + st->mode &= ~AD7192_MODE_CLKSRC_MASK ; + st->mode |=AD7192_CLK_INT_CO ; + + ret =ad_sd_write_reg (&st->sd,AD7192_REG_MODE ,3 , st->mode); +if (ret) +return ret; + + st->clock_sel =AD7192_CLK_INT_CO ; + +return 0 ; +} + +static void ad7192_clk_unprepare (struct clk_hw *hw) +{ +struct ad7192_state *st =clk_hw_to_ad7192 (hw); +int ret; + + st->mode &= ~AD7192_MODE_CLKSRC_MASK ; + st->mode |=AD7192_CLK_INT ; + + ret =ad_sd_write_reg (&st->sd,AD7192_REG_MODE ,3 , st->mode); +if (ret) +return ; + + st->clock_sel =AD7192_CLK_INT ; +} + +static const struct clk_ops ad7192_int_clk_ops = { + .recalc_rate = ad7192_clk_recalc_rate, + .is_enabled = ad7192_clk_output_is_enabled, + .prepare = ad7192_clk_prepare, + .unprepare = ad7192_clk_unprepare, +}; + +static int ad7192_register_clk_provider (struct ad7192_state *st) +{ +struct device *dev = &st->sd.spi->dev; +struct clk_init_data init = {}; +const char *clk_name; +int ret; + +if (!IS_ENABLED (CONFIG_COMMON_CLK )) +return 0 ; + + ret =device_property_read_string (dev,"clock-output-names" , &clk_name); +if (ret) +return dev_err_probe (dev, ret, +"Failed to get clock-output-names\n" ); + + init.name = clk_name; + init.ops = &ad7192_int_clk_ops; + + st->int_clk_hw.init = &init; + ret =devm_clk_hw_register (dev, &st->int_clk_hw); +if (ret) +return ret; + +return devm_of_clk_add_hw_provider (dev, of_clk_hw_simple_get, + &st->int_clk_hw); +} +static int ad7192_clock_setup (struct ad7192_state *st) {struct device *dev = &st->sd.spi->dev;
@@ -414,6 +500,10 @@ static int ad7192_clock_setup(struct ad7192_state *st)if (ret <0 ) { st->clock_sel =AD7192_CLK_INT ; st->fclk =AD7192_INT_FREQ_MHZ ; + + ret =ad7192_register_clk_provider (st); +if (ret) +return ret; }else { st->clock_sel =AD7192_CLK_EXT_MCLK1_2 + ret;
Signature · 1 line
2.34.1