iio: adc: at91_adc: fix NULL trigger dereference and convert to devm
Subsystems (3)
v5 cover iio: adc: at91_adc: fix NULL trigger dereference and convert to devm
This series fixes a NULL pointer dereference during trigger teardown in at91_adc and modernizes the driver to use firmware-agnostic property APIs and device-managed (devm) resources.
When the "atmel,adc-use-external-triggers" property is omitted from the Device Tree, at91_adc_trigger_init() leaves external trigger slots in st->trig as NULL. Patch 1 provides a minimal fix in at91_adc for easy backporting to stable kernels, while the subsequent patches modernize the driver and convert it to devm_* APIs (removing the manual teardown code and the st->trig array altogether).
Changes in v5:
- Patch 1: Keep the minimal NULL check in at91_adc instead of modifying iio_trigger_unregister(), and explicitly note in the commit message that it is a minimal fix for backporting before devm removal (as requested by Jonathan Cameron).
- Patch 2: Drop the redundant dev_err_probe() message after devm_request_irq() in a separate patch.
- Patch 3: Switch from of_device_get_match_data() to device_get_match_data() and check for NULL in a separate patch.
- Patch 4: Convert of_property_*() calls in at91_adc_probe() and at91_adc_probe_dt_ts() (renamed to at91_adc_probe_ts()) to device_property_*() APIs, removing the local device_node pointer and dropping "in the DT" from missing-property logs. Since no OF APIs remain, replace <linux/of.h> with <linux/mod_devicetable.h>.
- Patch 5: Introduce local struct device *dev in at91_adc_probe() and switch devm_iio_device_alloc() to sizeof(*st).
- Patch 6: Pass struct device *dev instead of struct platform_device *pdev to at91_ts_register() in a separate patch.
- Patch 7: Replace raw 1000 with HZ_PER_KHZ from <linux/units.h>.
- Patch 8: Convert the remaining buffer, trigger, touchscreen input, and IIO device registrations to devm_* APIs, use unsigned int for the trigger loop index, drop the now-unused st->trig array, drop the redundant input->dev.parent assignment, and remove at91_adc_remove().
Changes in v4:
- Split the series into 3 atomic patches: (1) standalone NULL trigger teardown fix for stable backporting, (2) local struct device pointer cleanup, and (3) full devm_* conversion.
Changes in v3:
- Address review feedback from Andy Shevchenko on v2 (drop redundant -ENOMEM log, use local struct device pointer, scope loop index to the for-loop, and convert error paths to dev_err_probe()).
Changes in v2:
- Drop the simple NULL check approach from v1 and refactor the driver to use device-managed (devm_*) APIs as suggested by Jonathan Cameron, removing manual cleanup functions and at91_adc_remove(), and inlining trigger initialization into at91_adc_probe().
Abdelnasser Hussein (8):
iio: adc: at91_adc: skip unpopulated triggers on teardown
iio: adc: at91_adc: drop duplicate IRQ allocation error message
iio: adc: at91_adc: switch to device_get_match_data()
iio: adc: at91_adc: convert to device_property_*() APIs
iio: adc: at91_adc: use local struct device pointer in probe
iio: adc: at91_adc: pass struct device to at91_ts_register()
iio: adc: at91_adc: use HZ_PER_KHZ instead of raw 1000
iio: adc: at91_adc: convert to device-managed resources
drivers/iio/adc/at91_adc.c | 266 +++++++++++-------------------------- 1 file changed, 77 insertions(+), 189 deletions(-)
Signature · 1 line
2.56.0
PATCH v5 1/8 iio: adc: at91_adc: skip unpopulated triggers on teardown
The driver keeps an array of triggers and leaves the slots for external triggers unpopulated (NULL) when the "atmel,adc-use-external-triggers" property is not set. Both the error rollback path in at91_adc_trigger_init() and at91_adc_trigger_remove() walk the whole array and call iio_trigger_unregister() and iio_trigger_free() on every entry, including the empty ones, which dereferences NULL.
Skip unpopulated trigger slots in both loops.
Make a minimal fix to simplify backporting. Moving over to device managed cleanup will shortly remove this code.
--- drivers/iio/adc/at91_adc.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/iio/adc/at91_adc.c +6 -0
@@ -660,6 +660,9 @@ static int at91_adc_trigger_init(struct iio_dev *idev) error_trigger:for (i--; i >=0 ; i--) { +if (!st->trig[i]) +continue ; +iio_trigger_unregister (st->trig[i]);iio_trigger_free (st->trig[i]); }
@@ -673,6 +676,9 @@ static void at91_adc_trigger_remove(struct iio_dev *idev)int i;for (i =0 ; i < st->caps->trigger_number; i++) { +if (!st->trig[i]) +continue ; +iio_trigger_unregister (st->trig[i]);iio_trigger_free (st->trig[i]); }
Signature · 1 line
2.56.0
Re: [PATCH v5 1/8] iio: adc: at91_adc: skip unpopulated triggers on teardown
Quoted 9 lines · Abdelnasser Hussein
The driver keeps an array of triggers and leaves the slots for external triggers unpopulated (NULL) when the "atmel,adc-use-external-triggers" property is not set. Both the error rollback path in
at91_adc_trigger_init()andat91_adc_trigger_remove()walk the whole array and calliio_trigger_unregister()andiio_trigger_free()on every entry, including the empty ones, which dereferences NULL.Skip unpopulated trigger slots in both loops.
Quoted 7 lines
Make a minimal fix to simplify backporting. Moving over to device managed cleanup will shortly remove this code.
Fixes 0e589d5fb317 ("ARM: AT91: IIO: Add AT91 ADC driver.")Signed-off-by Abdelnasser Hussein <[email protected]>---
Signature · 2 lines
Kind regards, Joshua Crofts
PATCH v5 2/8 iio: adc: at91_adc: drop duplicate IRQ allocation error message
In reply to Abdelnasser Husseindevm_request_irq() already logs an error message when it fails. Drop the additional dev_err_probe() wrapper and simply return the error code to avoid reporting the same failure twice.
--- drivers/iio/adc/at91_adc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/adc/at91_adc.c +1 -2
@@ -1074,8 +1074,7 @@ static int at91_adc_probe(struct platform_device *pdev) at91_adc_rl_interrupt,0 , pdev->dev.driver->name, idev);if (ret) -return dev_err_probe (&pdev->dev, ret, -"Failed to allocate IRQ.\n" ); +return ret; st->clk =devm_clk_get_enabled (&pdev->dev,"adc_clk" );if (IS_ERR (st->clk))
Signature · 1 line
2.56.0
Re: [PATCH v5 2/8] iio: adc: at91_adc: drop duplicate IRQ allocation error message
Quoted 6 lines · Abdelnasser Hussein
devm_request_irq()already logs an error message when it fails. Drop the additionaldev_err_probe()wrapper and simply return the error code to avoid reporting the same failure twice.Signed-off-by Abdelnasser Hussein <[email protected]>---
Signature · 2 lines
Kind regards, Joshua Crofts
PATCH v5 3/8 iio: adc: at91_adc: switch to device_get_match_data()
In reply to Abdelnasser HusseinUse the firmware-agnostic device_get_match_data() instead of the OF-specific of_device_get_match_data() to retrieve the device capabilities.
Check the returned pointer so that probing a device without matching capabilities fails cleanly instead of dereferencing NULL later on.
--- drivers/iio/adc/at91_adc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/adc/at91_adc.c +4 -1
@@ -18,6 +18,7 @@#include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> +#include <linux/property.h> #include <linux/sched.h> #include <linux/slab.h> #include <linux/wait.h>
@@ -1002,7 +1003,9 @@ static int at91_adc_probe(struct platform_device *pdev) st =iio_priv (idev); - st->caps =of_device_get_match_data (&pdev->dev); + st->caps =device_get_match_data (&pdev->dev); +if (!st->caps) +return -ENODEV ; st->use_external =of_property_read_bool (node,"atmel,adc-use-external-triggers" );
Signature · 1 line
2.56.0
Re: [PATCH v5 3/8] iio: adc: at91_adc: switch to device_get_match_data()
Quoted 25 lines · Abdelnasser Hussein
Use the firmware-agnostic
device_get_match_data()instead of the OF-specificof_device_get_match_data()to retrieve the device capabilities.Check the returned pointer so that probing a device without matching capabilities fails cleanly instead of dereferencing NULL later on.
Signed-off-by Abdelnasser Hussein <[email protected]>--- drivers/iio/adc/at91_adc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/iio/adc/at91_adc.c +1
-0@@ -18,6 +18,7 @@#include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> +#include <linux/property.h> #include <linux/sched.h> #include <linux/slab.h> #include <linux/wait.h>
drivers/iio/adc/at91_adc.c static int at91_adc_probe(struct platform_device *pdev) · line 1008
Quoted 7 lines
drivers/iio/adc/at91_adc.c +3
-1@@ -1002,7 +1003,9 @@ static int at91_adc_probe(struct platform_device *pdev) st =iio_priv (idev); - st->caps =of_device_get_match_data (&pdev->dev); + st->caps =device_get_match_data (&pdev->dev); +if (!st->caps) +return -ENODEV ;
-ENODATA is better.
Signature · 2 lines
Kind regards, Joshua Crofts
PATCH v5 4/8 iio: adc: at91_adc: convert to device_property_*() APIs
In reply to Abdelnasser HusseinUse the firmware-agnostic device_property_*() helpers instead of the OF-specific property readers, so the driver does not depend on a device node being available in probe.
Update at91_adc_probe_dt_ts() to take a struct device and read its properties through device_property_read_u32() as well. Since the helper no longer uses DT-specific APIs, rename it from at91_adc_probe_dt_ts() to at91_adc_probe_ts(). This removes the need for a struct device_node pointer in at91_adc_probe(); as a result, at91_adc_probe_ts() now logs against dev (&pdev->dev) instead of &idev->dev.
Drop the " in the DT" suffix from the missing property messages to match the firmware-agnostic property APIs.
With no OF APIs or struct device_node left in the file, drop the <linux/of.h> include and add <linux/mod_devicetable.h> for struct of_device_id.
--- drivers/iio/adc/at91_adc.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/iio/adc/at91_adc.c +17 -17
@@ -15,8 +15,8 @@#include <linux/interrupt.h> #include <linux/jiffies.h> #include <linux/kernel.h> +#include <linux/mod_devicetable.h> #include <linux/module.h> -#include <linux/of.h> #include <linux/platform_device.h> #include <linux/property.h> #include <linux/sched.h>
@@ -790,13 +790,12 @@ static u32 calc_startup_ticks_9x5(u32 startup_time, u32 adc_clk_khz)return ticks; } -static int at91_adc_probe_dt_ts (struct device_node *node, -struct at91_adc_state *st,struct device *dev) +static int at91_adc_probe_ts (struct device *dev,struct at91_adc_state *st) {int ret;u32 prop; - ret =of_property_read_u32 (node,"atmel,adc-ts-wires" , &prop); + ret =device_property_read_u32 (dev,"atmel,adc-ts-wires" , &prop);if (ret) {dev_info (dev,"ADC Touch screen is disabled.\n" );return 0 ;
@@ -815,7 +814,7 @@ static int at91_adc_probe_dt_ts(struct device_node *node,if (!st->caps->has_tsmr)return 0 ; prop =0 ; -of_property_read_u32 (node,"atmel,adc-ts-pressure-threshold" , &prop); +device_property_read_u32 (dev,"atmel,adc-ts-pressure-threshold" , &prop); st->ts_pressure_threshold = prop;if (st->ts_pressure_threshold) {return 0 ;
@@ -990,7 +989,6 @@ static void at91_ts_unregister(struct at91_adc_state *st)static int at91_adc_probe (struct platform_device *pdev) {unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim; -struct device_node *node = pdev->dev.of_node;int ret;struct iio_dev *idev;struct at91_adc_state *st;
@@ -1007,32 +1005,34 @@ static int at91_adc_probe(struct platform_device *pdev)if (!st->caps)return -ENODEV ; - st->use_external =of_property_read_bool (node,"atmel,adc-use-external-triggers" ); + st->use_external =device_property_read_bool (&pdev->dev, +"atmel,adc-use-external-triggers" ); -if (of_property_read_u32 (node,"atmel,adc-channels-used" , &prop)) +if (device_property_read_u32 (&pdev->dev,"atmel,adc-channels-used" , &prop))return dev_err_probe (&idev->dev, -EINVAL , -"Missing adc-channels-used property in the DT.\n" ); +"Missing adc-channels-used property.\n" ); st->channels_mask = prop; - st->sleep_mode =of_property_read_bool (node,"atmel,adc-sleep-mode" ); + st->sleep_mode =device_property_read_bool (&pdev->dev, +"atmel,adc-sleep-mode" ); -if (of_property_read_u32 (node,"atmel,adc-startup-time" , &prop)) +if (device_property_read_u32 (&pdev->dev,"atmel,adc-startup-time" , &prop))return dev_err_probe (&idev->dev, -EINVAL , -"Missing adc-startup-time property in the DT.\n" ); +"Missing adc-startup-time property.\n" ); st->startup_time = prop; prop =0 ; -of_property_read_u32 (node,"atmel,adc-sample-hold-time" , &prop); +device_property_read_u32 (&pdev->dev,"atmel,adc-sample-hold-time" , &prop); st->sample_hold_time = prop; -if (of_property_read_u32 (node,"atmel,adc-vref" , &prop)) +if (device_property_read_u32 (&pdev->dev,"atmel,adc-vref" , &prop))return dev_err_probe (&idev->dev, -EINVAL , -"Missing adc-vref property in the DT.\n" ); +"Missing adc-vref property.\n" ); st->vref_mv = prop; st->res = st->caps->high_res_bits;if (st->caps->low_res_bits && - !of_property_read_string (node,"atmel,adc-use-res" , &s) + !device_property_read_string (&pdev->dev,"atmel,adc-use-res" , &s) && !strcmp (s,"lowres" )) st->res = st->caps->low_res_bits;
@@ -1043,7 +1043,7 @@ static int at91_adc_probe(struct platform_device *pdev)/* Check if touchscreen is supported. */ if (st->caps->has_ts) { - ret =at91_adc_probe_dt_ts (node, st, &idev->dev); + ret =at91_adc_probe_ts (&pdev->dev, st);if (ret)return ret; }
Signature · 1 line
2.56.0
Re: [PATCH v5 4/8] iio: adc: at91_adc: convert to device_property_*() APIs
Quoted 28 lines · Abdelnasser Hussein
Use the firmware-agnostic device_property_*() helpers instead of the OF-specific property readers, so the driver does not depend on a device node being available in probe.
Update
at91_adc_probe_dt_ts()to take a struct device and read its properties throughdevice_property_read_u32()as well. Since the helper no longer uses DT-specific APIs, rename it fromat91_adc_probe_dt_ts()toat91_adc_probe_ts(). This removes the need for a structdevice_nodepointer inat91_adc_probe(); as a result,at91_adc_probe_ts()now logs against dev (&pdev->dev) instead of&idev->dev.Drop the " in the DT" suffix from the missing property messages to match the firmware-agnostic property APIs.
With no OF APIs or struct
device_nodeleft in the file, drop the <linux/of.h> include and add <linux/mod_devicetable.h> for structof_device_id.Signed-off-by Abdelnasser Hussein <[email protected]>--- drivers/iio/adc/at91_adc.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)drivers/iio/adc/at91_adc.c +0
-0
drivers/iio/adc/at91_adc.c line 18
Quoted 4 lines
drivers/iio/adc/at91_adc.c +1
-0@@ -15,8 +15,8 @@#include <linux/interrupt.h> #include <linux/jiffies.h> #include <linux/kernel.h> +#include <linux/mod_devicetable.h>
Remove this, The corresponding device table is pulled from platform_device.h.
Signature · 2 lines
Kind regards, Joshua Crofts
Re: [PATCH v5 4/8] iio: adc: at91_adc: convert to device_property_*() APIs
In reply to Abdelnasser HusseinHello,
Quoted 3 lines · Abdelnasser Hussein
With no OF APIs or struct
device_nodeleft in the file, drop the <linux/of.h> include and add <linux/mod_devicetable.h> for structof_device_id.
That's wrong. <linux/mod_devicetable.h> is a dependency hog and should not be used any more. You can rely on <linux/platform_device.h> to provide of_device_id and just drop <linux/mod_devicetable.h>.
Best regards
Uwe
Re: [PATCH v5 4/8] iio: adc: at91_adc: convert to device_property_*() APIs
In reply to Abdelnasser HusseinQuoted 17 lines · Abdelnasser Hussein
Use the firmware-agnostic device_property_*() helpers instead of the OF-specific property readers, so the driver does not depend on a device node being available in probe.
Update
at91_adc_probe_dt_ts()to take a struct device and read its properties throughdevice_property_read_u32()as well. Since the helper no longer uses DT-specific APIs, rename it fromat91_adc_probe_dt_ts()toat91_adc_probe_ts(). This removes the need for a structdevice_nodepointer inat91_adc_probe(); as a result,at91_adc_probe_ts()now logs against dev (&pdev->dev) instead of&idev->dev.Drop the " in the DT" suffix from the missing property messages to match the firmware-agnostic property APIs.
With no OF APIs or struct
device_nodeleft in the file, drop the <linux/of.h> include and add <linux/mod_devicetable.h> for structof_device_id.
...
drivers/iio/adc/at91_adc.c static void at91_ts_unregister(struct at91_adc_state *st) · line 993
Quoted 4 lines
static int
at91_adc_probe(struct platform_device *pdev){drivers/iio/adc/at91_adc.c +0
-1unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim; -struct device_node *node = pdev->dev.of_node;
struct device *dev = &pdev->dev; const char *propname;
Quoted 3 lines
drivers/iio/adc/at91_adc.c +0
-0int ret;struct iio_dev *idev;struct at91_adc_state *st;
...
drivers/iio/adc/at91_adc.c static int at91_adc_probe(struct platform_device *pdev) · line 1009
Quoted 3 lines
drivers/iio/adc/at91_adc.c +2
-1- st->use_external =of_property_read_bool (node,"atmel,adc-use-external-triggers" ); + st->use_external =device_property_read_bool (&pdev->dev, +"atmel,adc-use-external-triggers" );
propname = "atmel,adc-use-external-triggers"; st->use_external = device_property_read_bool(dev, propname);
drivers/iio/adc/at91_adc.c static int at91_adc_probe(struct platform_device *pdev) · line 1013
Quoted 5 lines
drivers/iio/adc/at91_adc.c +2
-2-if (of_property_read_u32 (node,"atmel,adc-channels-used" , &prop)) +if (device_property_read_u32 (&pdev->dev,"atmel,adc-channels-used" , &prop))return dev_err_probe (&idev->dev, -EINVAL , -"Missing adc-channels-used property in the DT.\n" ); +"Missing adc-channels-used property.\n" );
propname = "atmel,adc-channels-used"; if (device_property_read_u32(dev, propname, &prop)) return dev_err_probe(&idev->dev, -EINVAL, "Missing %s property.\n", propname);
BUT... this needs to be fixed first to unshadow error code
propname = "atmel,adc-channels-used"; ret = device_property_read_u32(dev, propname, &prop); if (ret) return dev_err_probe(&idev->dev, ret, "Missing %s property.\n", propname);
AND... what the heck is idev->dev now? Please, fix that before even this patch, use same device pointer in the call and in the message. In the end it should be
propname = "atmel,adc-channels-used"; ret = device_property_read_u32(dev, propname, &prop); if (ret) return dev_err_probe(dev, ret, "Missing %s property.\n", propname);
Quoted 1 line
st->channels_mask= prop;
All the same for other properties.
...
drivers/iio/adc/at91_adc.c static int at91_adc_probe(struct platform_device *pdev)
Quoted 6 lines
drivers/iio/adc/at91_adc.c +1
-1st->res = st->caps->high_res_bits;if (st->caps->low_res_bits && - !of_property_read_string (node,"atmel,adc-use-res" , &s) + !device_property_read_string (&pdev->dev,"atmel,adc-use-res" , &s) && !strcmp (s,"lowres" ))
This is device_property_match_string() I believe.
Quoted 1 line
st->res=st->caps->low_res_bits;
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v5 5/8 iio: adc: at91_adc: use local struct device pointer in probe
In reply to Abdelnasser HusseinIntroduce a local struct device pointer in at91_adc_probe() and use it in place of &pdev->dev and &idev->dev throughout the function.
Also switch devm_iio_device_alloc() to use sizeof(*st) instead of spelling out the full private structure type.
This is a preparatory cleanup to reduce the diff noise of the upcoming conversion to device-managed resources.
As a result, messages previously logged against the IIO device are now printed using the platform device being probed.
--- drivers/iio/adc/at91_adc.c | 61 +++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 31 deletions(-)
drivers/iio/adc/at91_adc.c +30 -31
@@ -989,68 +989,67 @@ static void at91_ts_unregister(struct at91_adc_state *st)static int at91_adc_probe (struct platform_device *pdev) {unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim; +struct device *dev = &pdev->dev;int ret;struct iio_dev *idev;struct at91_adc_state *st;u32 reg, prop;const char *s; - idev =devm_iio_device_alloc (&pdev->dev,sizeof (struct at91_adc_state )); + idev =devm_iio_device_alloc (dev,sizeof (*st));if (!idev)return -ENOMEM ; st =iio_priv (idev); - st->caps =device_get_match_data (&pdev->dev); + st->caps =device_get_match_data (dev);if (!st->caps)return -ENODEV ; - st->use_external =device_property_read_bool (&pdev->dev, -"atmel,adc-use-external-triggers" ); + st->use_external =device_property_read_bool (dev,"atmel,adc-use-external-triggers" ); -if (device_property_read_u32 (&pdev->dev,"atmel,adc-channels-used" , &prop)) -return dev_err_probe (&idev->dev, -EINVAL , +if (device_property_read_u32 (dev,"atmel,adc-channels-used" , &prop)) +return dev_err_probe (dev, -EINVAL ,"Missing adc-channels-used property.\n" ); st->channels_mask = prop; - st->sleep_mode =device_property_read_bool (&pdev->dev, -"atmel,adc-sleep-mode" ); + st->sleep_mode =device_property_read_bool (dev,"atmel,adc-sleep-mode" ); -if (device_property_read_u32 (&pdev->dev,"atmel,adc-startup-time" , &prop)) -return dev_err_probe (&idev->dev, -EINVAL , +if (device_property_read_u32 (dev,"atmel,adc-startup-time" , &prop)) +return dev_err_probe (dev, -EINVAL ,"Missing adc-startup-time property.\n" ); st->startup_time = prop; prop =0 ; -device_property_read_u32 (&pdev->dev,"atmel,adc-sample-hold-time" , &prop); +device_property_read_u32 (dev,"atmel,adc-sample-hold-time" , &prop); st->sample_hold_time = prop; -if (device_property_read_u32 (&pdev->dev,"atmel,adc-vref" , &prop)) -return dev_err_probe (&idev->dev, -EINVAL , +if (device_property_read_u32 (dev,"atmel,adc-vref" , &prop)) +return dev_err_probe (dev, -EINVAL ,"Missing adc-vref property.\n" ); st->vref_mv = prop; st->res = st->caps->high_res_bits;if (st->caps->low_res_bits && - !device_property_read_string (&pdev->dev,"atmel,adc-use-res" , &s) + !device_property_read_string (dev,"atmel,adc-use-res" , &s) && !strcmp (s,"lowres" )) st->res = st->caps->low_res_bits; -dev_info (&idev->dev,"Resolution used: %u bits\n" , st->res); +dev_info (dev,"Resolution used: %u bits\n" , st->res); st->registers = &st->caps->registers; st->num_channels = st->caps->num_channels;/* Check if touchscreen is supported. */ if (st->caps->has_ts) { - ret =at91_adc_probe_ts (&pdev->dev, st); + ret =at91_adc_probe_ts (dev, st);if (ret)return ret; }platform_set_drvdata (pdev, idev); - idev->name =dev_name (&pdev->dev); + idev->name =dev_name (dev); idev->modes =INDIO_DIRECT_MODE ; idev->info = &at91_adc_info;
@@ -1069,24 +1068,24 @@ static int at91_adc_probe(struct platform_device *pdev)at91_adc_writel (st,AT91_ADC_IDR ,0xFFFFFFFF );if (st->caps->has_tsmr) - ret =devm_request_irq (&pdev->dev, st->irq, + ret =devm_request_irq (dev, st->irq, at91_adc_9x5_interrupt,0 , - pdev->dev.driver->name, idev); + dev->driver->name, idev);else - ret =devm_request_irq (&pdev->dev, st->irq, + ret =devm_request_irq (dev, st->irq, at91_adc_rl_interrupt,0 , - pdev->dev.driver->name, idev); + dev->driver->name, idev);if (ret)return ret; - st->clk =devm_clk_get_enabled (&pdev->dev,"adc_clk" ); + st->clk =devm_clk_get_enabled (dev,"adc_clk" );if (IS_ERR (st->clk)) -return dev_err_probe (&pdev->dev,PTR_ERR (st->clk), +return dev_err_probe (dev,PTR_ERR (st->clk),"Could not prepare or enable the clock.\n" ); - st->adc_clk =devm_clk_get_enabled (&pdev->dev,"adc_op_clk" ); + st->adc_clk =devm_clk_get_enabled (dev,"adc_op_clk" );if (IS_ERR (st->adc_clk)) -return dev_err_probe (&pdev->dev,PTR_ERR (st->adc_clk), +return dev_err_probe (dev,PTR_ERR (st->adc_clk),"Could not prepare or enable the ADC clock.\n" );/*
@@ -1098,13 +1097,13 @@ static int at91_adc_probe(struct platform_device *pdev) adc_clk =clk_get_rate (st->adc_clk); adc_clk_khz = adc_clk /1000 ; -dev_dbg (&pdev->dev,"Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n" , +dev_dbg (dev,"Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n" , mstrclk, adc_clk); prsc = (mstrclk / (2 * adc_clk)) -1 ;if (!st->startup_time) -return dev_err_probe (&pdev->dev, -EINVAL , +return dev_err_probe (dev, -EINVAL ,"No startup time available.\n" ); ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
@@ -1131,7 +1130,7 @@ static int at91_adc_probe(struct platform_device *pdev)/* Setup the ADC channels available on the board */ ret =at91_adc_channel_init (idev);if (ret <0 ) -return dev_err_probe (&pdev->dev, ret, +return dev_err_probe (dev, ret,"Couldn't initialize the channels.\n" );init_waitqueue_head (&st->wq_data_avail);
@@ -1145,12 +1144,12 @@ static int at91_adc_probe(struct platform_device *pdev)if (!st->touchscreen_type) { ret =at91_adc_buffer_init (idev);if (ret <0 ) -return dev_err_probe (&pdev->dev, ret, +return dev_err_probe (dev, ret,"Couldn't initialize the buffer.\n" ); ret =at91_adc_trigger_init (idev);if (ret <0 ) { -dev_err (&pdev->dev,"Couldn't setup the triggers.\n" ); +dev_err (dev,"Couldn't setup the triggers.\n" );at91_adc_buffer_remove (idev);return ret; }
@@ -1164,7 +1163,7 @@ static int at91_adc_probe(struct platform_device *pdev) ret =iio_device_register (idev);if (ret <0 ) { -dev_err (&pdev->dev,"Couldn't register the device.\n" ); +dev_err (dev,"Couldn't register the device.\n" ); goto error_iio_device_register; }
Signature · 1 line
2.56.0
Re: [PATCH v5 5/8] iio: adc: at91_adc: use local struct device pointer in probe
Quoted 6 lines · Abdelnasser Hussein
Introduce a local struct device pointer in
at91_adc_probe()and use it in place of&pdev->devand&idev->devthroughout the function.Also switch
devm_iio_device_alloc()to usesizeof(*st)instead of spelling out the full private structure type.
Quoted 8 lines
This is a preparatory cleanup to reduce the diff noise of the upcoming conversion to device-managed resources.
As a result, messages previously logged against the IIO device are now printed using the platform device being probed.
Signed-off-by Abdelnasser Hussein <[email protected]>---
Signature · 2 lines
Kind regards, Joshua Crofts
Re: [PATCH v5 5/8] iio: adc: at91_adc: use local struct device pointer in probe
In reply to Abdelnasser HusseinQuoted 11 lines · Abdelnasser Hussein
Introduce a local struct device pointer in
at91_adc_probe()and use it in place of&pdev->devand&idev->devthroughout the function.Also switch
devm_iio_device_alloc()to usesizeof(*st)instead of spelling out the full private structure type.This is a preparatory cleanup to reduce the diff noise of the upcoming conversion to device-managed resources.
As a result, messages previously logged against the IIO device are now printed using the platform device being probed.
Yes, but part of that may be already done in the previous patch as you touched the very same lines. Here should be just a residue (of what has not been touched by previous changes).
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v5 6/8 iio: adc: at91_adc: pass struct device to at91_ts_register()
In reply to Abdelnasser HusseinThe at91_ts_register() helper only uses its platform_device argument to obtain the parent struct device. Pass that device directly and simplify the function signature and its call site.
--- drivers/iio/adc/at91_adc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/iio/adc/at91_adc.c +4 -5
@@ -925,8 +925,7 @@ static int at91_ts_hw_init(struct iio_dev *idev, u32 adc_clk_khz)return 0 ; } -static int at91_ts_register (struct iio_dev *idev, -struct platform_device *pdev) +static int at91_ts_register (struct iio_dev *idev,struct device *dev) {struct at91_adc_state *st =iio_priv (idev);struct input_dev *input;
@@ -940,7 +939,7 @@ static int at91_ts_register(struct iio_dev *idev, input->name =DRIVER_NAME ; input->id.bustype =BUS_HOST ; - input->dev.parent = &pdev->dev; + input->dev.parent = dev; input->open = atmel_ts_open; input->close = atmel_ts_close;
@@ -955,7 +954,7 @@ static int at91_ts_register(struct iio_dev *idev,input_set_abs_params (input,ABS_PRESSURE ,0 ,0xffffff ,0 ,0 ); }else {if (st->touchscreen_type !=ATMEL_ADC_TOUCHSCREEN_4WIRE ) { -dev_err (&pdev->dev, +dev_err (dev,"This touchscreen controller only support 4 wires\n" ); ret = -EINVAL ; goto err;
@@ -1154,7 +1153,7 @@ static int at91_adc_probe(struct platform_device *pdev)return ret; } }else { - ret =at91_ts_register (idev, pdev); + ret =at91_ts_register (idev, dev);if (ret)return ret;
Signature · 1 line
2.56.0
Re: [PATCH v5 6/8] iio: adc: at91_adc: pass struct device to at91_ts_register()
Quoted 6 lines · Abdelnasser Hussein
The
at91_ts_register()helper only uses itsplatform_deviceargument to obtain the parent struct device. Pass that device directly and simplify the function signature and its call site.Signed-off-by Abdelnasser Hussein <[email protected]>---
Signature · 2 lines
Kind regards, Joshua Crofts
Re: [PATCH v5 6/8] iio: adc: at91_adc: pass struct device to at91_ts_register()
In reply to Abdelnasser HusseinQuoted 3 lines · Abdelnasser Hussein
The
at91_ts_register()helper only uses itsplatform_deviceargument to obtain the parent struct device. Pass that device directly and simplify the function signature and its call site.
Perhaps Suggested-by: ?
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v5 7/8 iio: adc: at91_adc: use HZ_PER_KHZ instead of raw 1000
In reply to Abdelnasser HusseinUse the HZ_PER_KHZ constant from <linux/units.h> when converting the ADC clock rate to kilohertz instead of open-coding the divisor.
--- drivers/iio/adc/at91_adc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/adc/at91_adc.c +2 -1
@@ -21,6 +21,7 @@#include <linux/property.h> #include <linux/sched.h> #include <linux/slab.h> +#include <linux/units.h> #include <linux/wait.h> #include <linux/iio/iio.h>
@@ -1094,7 +1095,7 @@ static int at91_adc_probe(struct platform_device *pdev)*/ mstrclk =clk_get_rate (st->clk); adc_clk =clk_get_rate (st->adc_clk); - adc_clk_khz = adc_clk /1000 ; + adc_clk_khz = adc_clk /HZ_PER_KHZ ;dev_dbg (dev,"Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n" , mstrclk, adc_clk);
Signature · 1 line
2.56.0
Re: [PATCH v5 7/8] iio: adc: at91_adc: use HZ_PER_KHZ instead of raw 1000
Quoted 5 lines · Abdelnasser Hussein
Use the
HZ_PER_KHZconstant from <linux/units.h> when converting the ADC clock rate to kilohertz instead of open-coding the divisor.Signed-off-by Abdelnasser Hussein <[email protected]>---
Signature · 2 lines
Kind regards, Joshua Crofts
PATCH v5 8/8 iio: adc: at91_adc: convert to device-managed resources
In reply to Abdelnasser HusseinConvert the driver to device-managed APIs so that all resources are freed automatically on probe failure and on driver detach:
- allocate and register the IIO triggers directly in at91_adc_probe() with devm_iio_trigger_alloc() and devm_iio_trigger_register(), dropping at91_adc_allocate_trigger(), at91_adc_trigger_init() and at91_adc_trigger_remove() along with their manual rollback paths. The st->trig array is no longer needed and is removed as well, since the triggers are now owned by devres
- set up the triggered buffer with devm_iio_triggered_buffer_setup() directly in at91_adc_probe(), dropping at91_adc_buffer_init() and at91_adc_buffer_remove()
- allocate the touchscreen input device with devm_input_allocate_device(), dropping at91_ts_unregister() and the manual input_free_device() error path in at91_ts_register()
- register the IIO device with devm_iio_device_register()
Redundant allocation error messages ("Failed to allocate TS device!" and "Couldn't setup the triggers.") are dropped, and at91_ts_register() now uses dev_err_probe() to report its remaining error. Errors from devm_iio_trigger_register() are reported with dev_err_probe() as well.
at91_adc_probe() no longer needs any error unwinding labels and at91_adc_remove() is removed together with the platform driver's .remove callback.
--- drivers/iio/adc/at91_adc.c | 187 +++++++------------------------------ 1 file changed, 34 insertions(+), 153 deletions(-)
drivers/iio/adc/at91_adc.c +34 -153
@@ -230,7 +230,6 @@ struct at91_adc_state {u32 startup_time;u8 sample_hold_time;bool sleep_mode; -struct iio_trigger **trig;bool use_external;u32 vref_mv;u32 res;/* resolution used for conversions */
@@ -607,96 +606,6 @@ static const struct iio_trigger_ops at91_adc_trigger_ops = { .set_trigger_state = &at91_adc_configure_trigger, }; -static struct iio_trigger *at91_adc_allocate_trigger (struct iio_dev *idev, -const struct at91_adc_trigger *trigger) -{ -struct iio_trigger *trig; -int ret; - - trig =iio_trigger_alloc (idev->dev.parent,"%s-dev%d-%s" , idev->name, -iio_device_id (idev), trigger->name); -if (trig ==NULL ) -return NULL ; - -iio_trigger_set_drvdata (trig, idev); - trig->ops = &at91_adc_trigger_ops; - - ret =iio_trigger_register (trig); -if (ret) { -iio_trigger_free (trig); -return NULL ; - } - -return trig; -} - -static int at91_adc_trigger_init (struct iio_dev *idev) -{ -struct at91_adc_state *st =iio_priv (idev); -int i, ret; - - st->trig =devm_kcalloc (&idev->dev, - st->caps->trigger_number,sizeof (*st->trig), -GFP_KERNEL ); - -if (st->trig ==NULL ) { - ret = -ENOMEM ; - goto error_ret; - } - -for (i =0 ; i < st->caps->trigger_number; i++) { -if (st->caps->triggers[i].is_external && !(st->use_external)) -continue ; - - st->trig[i] =at91_adc_allocate_trigger (idev, - st->caps->triggers + i); -if (st->trig[i] ==NULL ) { -dev_err (&idev->dev, -"Could not allocate trigger %d\n" , i); - ret = -ENOMEM ; - goto error_trigger; - } - } - -return 0 ; - -error_trigger: -for (i--; i >=0 ; i--) { -if (!st->trig[i]) -continue ; - -iio_trigger_unregister (st->trig[i]); -iio_trigger_free (st->trig[i]); - } -error_ret: -return ret; -} - -static void at91_adc_trigger_remove (struct iio_dev *idev) -{ -struct at91_adc_state *st =iio_priv (idev); -int i; - -for (i =0 ; i < st->caps->trigger_number; i++) { -if (!st->trig[i]) -continue ; - -iio_trigger_unregister (st->trig[i]); -iio_trigger_free (st->trig[i]); - } -} - -static int at91_adc_buffer_init (struct iio_dev *idev) -{ -return iio_triggered_buffer_setup (idev, &iio_pollfunc_store_time, - &at91_adc_trigger_handler,NULL ); -} - -static void at91_adc_buffer_remove (struct iio_dev *idev) -{ -iio_triggered_buffer_cleanup (idev); -} -static int at91_adc_read_raw (struct iio_dev *idev,struct iio_chan_spec const *chan,int *val,int *val2,long mask)
@@ -930,17 +839,13 @@ static int at91_ts_register(struct iio_dev *idev, struct device *dev) {struct at91_adc_state *st =iio_priv (idev);struct input_dev *input; -int ret; - input =input_allocate_device (); -if (!input) { -dev_err (&idev->dev,"Failed to allocate TS device!\n" ); + input =devm_input_allocate_device (dev); +if (!input)return -ENOMEM ; - } input->name =DRIVER_NAME ; input->id.bustype =BUS_HOST ; - input->dev.parent = dev; input->open = atmel_ts_open; input->close = atmel_ts_close;
@@ -954,12 +859,9 @@ static int at91_ts_register(struct iio_dev *idev, struct device *dev) 0, 0);input_set_abs_params (input,ABS_PRESSURE ,0 ,0xffffff ,0 ,0 ); }else { -if (st->touchscreen_type !=ATMEL_ADC_TOUCHSCREEN_4WIRE ) { -dev_err (dev, -"This touchscreen controller only support 4 wires\n" ); - ret = -EINVAL ; - goto err; - } +if (st->touchscreen_type !=ATMEL_ADC_TOUCHSCREEN_4WIRE ) +return dev_err_probe (dev, -EINVAL , +"This touchscreen controller only support 4 wires\n" );input_set_abs_params (input,ABS_X ,0 , (1 <<MAX_RLPOS_BITS ) -1 ,0 ,0 );
@@ -970,20 +872,7 @@ static int at91_ts_register(struct iio_dev *idev, struct device *dev) st->ts_input = input;input_set_drvdata (input, st); - ret =input_register_device (input); -if (ret) - goto err; - -return ret; - -err: -input_free_device (input); -return ret; -} - -static void at91_ts_unregister (struct at91_adc_state *st) -{ -input_unregister_device (st->ts_input); +return input_register_device (input); }static int at91_adc_probe (struct platform_device *pdev)
@@ -1142,16 +1031,34 @@ static int at91_adc_probe(struct platform_device *pdev)* trigger for classic adc. */ if (!st->touchscreen_type) { - ret =at91_adc_buffer_init (idev); -if (ret <0 ) + ret =devm_iio_triggered_buffer_setup (dev, idev, + &iio_pollfunc_store_time, + &at91_adc_trigger_handler, +NULL ); +if (ret)return dev_err_probe (dev, ret,"Couldn't initialize the buffer.\n" ); - ret =at91_adc_trigger_init (idev); -if (ret <0 ) { -dev_err (dev,"Couldn't setup the triggers.\n" ); -at91_adc_buffer_remove (idev); -return ret; +for (unsigned int i =0 ; i < st->caps->trigger_number; i++) { +struct iio_trigger *trig; + +if (st->caps->triggers[i].is_external && !st->use_external) +continue ; + + trig =devm_iio_trigger_alloc (dev,"%s-dev%d-%s" , + idev->name, +iio_device_id (idev), + st->caps->triggers[i].name); +if (!trig) +return -ENOMEM ; + +iio_trigger_set_drvdata (trig, idev); + trig->ops = &at91_adc_trigger_ops; + + ret =devm_iio_trigger_register (dev, trig); +if (ret) +return dev_err_probe (dev, ret, +"Couldn't register trigger %u.\n" , i); } }else { ret =at91_ts_register (idev, dev);
@@ -1161,36 +1068,11 @@ static int at91_adc_probe(struct platform_device *pdev)at91_ts_hw_init (idev, adc_clk_khz); } - ret =iio_device_register (idev); -if (ret <0 ) { -dev_err (dev,"Couldn't register the device.\n" ); - goto error_iio_device_register; - } + ret =devm_iio_device_register (dev, idev); +if (ret) +return dev_err_probe (dev, ret,"Couldn't register the device.\n" );return 0 ; - -error_iio_device_register: -if (!st->touchscreen_type) { -at91_adc_trigger_remove (idev); -at91_adc_buffer_remove (idev); - }else { -at91_ts_unregister (st); - } -return ret; -} - -static void at91_adc_remove (struct platform_device *pdev) -{ -struct iio_dev *idev =platform_get_drvdata (pdev); -struct at91_adc_state *st =iio_priv (idev); - -iio_device_unregister (idev); -if (!st->touchscreen_type) { -at91_adc_trigger_remove (idev); -at91_adc_buffer_remove (idev); - }else { -at91_ts_unregister (st); - } }static int at91_adc_suspend (struct device *dev)
@@ -1340,7 +1222,6 @@ MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);static struct platform_driver at91_adc_driver = { .probe = at91_adc_probe, - .remove = at91_adc_remove, .driver = { .name =DRIVER_NAME , .of_match_table = at91_adc_dt_ids,
Signature · 1 line
2.56.0