ECAP support on TI AM62x SoC
Subsystems (4)
Julien Panis
reviewerv8 cover ECAP support on TI AM62x SoC
The Enhanced Capture (ECAP) module can be used to timestamp events detected on signal input pin. It can be used for time measurements of pulse train signals.
ECAP module includes 4 timestamp capture registers. For all 4 sequenced timestamp capture events (0->1->2->3->0->...), edge polarity (falling/rising edge) can be selected.
This driver leverages counter subsystem to :
- select edge polarity for all 4 capture events (event mode)
- log timestamps for each capture event
Event polarity, and CAP0/1/2/3 timestamps give all the information about the input pulse train. Further information can easily be computed :
period and/or duty cycle if frequency is constant, elapsed time between pulses, etc...
This patchset must be applied on top of the following counter subsystem patchset :
https://lore.kernel.org/all/[email protected]/
Modifications since v7 :
- Simplify ecap_cnt_capture_get/set_evmode() functions
- Modify ecap_cnt_count_write() callback
- Modify ecap_cnt_watch_validate() callback
- Modify ecap_cnt_pol_read() callback
- Add ecap_cnt_cap_write() callback
- Modify ecap_cnt_nb_ovf_write() callback
- Use DEFINE_COUNTER_ARRAY_CAPTURE() and COUNTER_COMP_ARRAY_CAPTURE() macros
- Push COUNTER_EVENT_OVERFLOW to all 4 channels in ISR
- Redefine time_cntr and related functions as u32
- Remove ecap_cnt_capture_set_evmode() function call in probe function
Userspace commands :
### CLOCK SIGNAL ###
cd /sys/bus/counter/devices/counter0/signal0
# Get frequency
cat frequency
### INPUT SIGNAL ###
cd /sys/bus/counter/devices/counter0/signal1
# Get polarity for each capture event
cat polarity0
cat polarity1
cat polarity2
cat polarity3
# Set polarity for each capture event
echo positive > polarity0
echo negative > polarity1
echo positive > polarity2
echo negative > polarity3
### COUNT ###
cd /sys/bus/counter/devices/counter0/count0
# Get ceiling (counter max value)
cat ceiling
# Reset number of overflows & current timebase counter value echo 0 > num_overflows
echo 0 > count
# Run ECAP
echo 1 > enable
# Get number of overflows & current timebase counter value cat num_overflows
cat count
# Get captured timestamps
cat capture0
cat capture1
cat capture2
cat capture3
# Note that counter watches can also be used to get # data from userspace application
# -> see tools/counter/counter_example.c
# Pause ECAP
echo 0 > enable
Julien Panis (4):
dt-bindings: counter: add ti,am62-ecap-capture.yaml
Documentation: ABI: sysfs-bus-counter: add frequency & num_overflows
items
counter: ti-ecap-capture: capture driver support for ECAP
MAINTAINERS: add TI ECAP driver info
Documentation/ABI/testing/sysfs-bus-counter | 14 + .../counter/ti,am62-ecap-capture.yaml | 61 ++ MAINTAINERS | 7 + drivers/counter/Kconfig | 15 + drivers/counter/Makefile | 1 + drivers/counter/ti-ecap-capture.c | 621 ++++++++++++++++++ 6 files changed, 719 insertions(+) create mode 100644 Documentation/devicetree/bindings/counter/ti,am62-ecap-capture.yaml create mode 100644 drivers/counter/ti-ecap-capture.c
Signature · 1 line
2.37.3
Julien Panis
reviewerPATCH v8 1/4 dt-bindings: counter: add ti,am62-ecap-capture.yaml
This commit adds a YAML binding for TI ECAP used in capture operating mode.
--- .../counter/ti,am62-ecap-capture.yaml | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Documentation/devicetree/bindings/counter/ti,am62-ecap-capture.yaml
Documentation/devicetree/bindings/counter/ti,am62-ecap-capture.yaml new file +61 -0
@@ -0,0 +1,61 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/counter/ti,am62-ecap-capture.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Texas Instruments Enhanced Capture (eCAP) Module + +maintainers: + - Julien Panis <[email protected]> + +description: | + The eCAP module resources can be used to capture timestamps + on input signal events (falling/rising edges). + +properties: + compatible: + const: ti,am62-ecap-capture + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-names: + const: fck + + power-domains: + maxItems: 1 + +required: + - compatible + - reg + - interrupts + - clocks + - clock-names + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/arm-gic.h> + #include <dt-bindings/soc/ti,sci_pm_domain.h> + + soc { + #address-cells = <2>; + #size-cells = <2>; + + capture@23100000 { /* eCAP in capture mode on am62x */ + compatible = "ti,am62-ecap-capture"; + reg = <0x00 0x23100000 0x00 0x100>; + interrupts = <GIC_SPI 113 IRQ_TYPE_EDGE_RISING>; + power-domains = <&k3_pds 51 TI_SCI_PD_EXCLUSIVE>; + clocks = <&k3_clks 51 0>; + clock-names = "fck"; + }; + };
Signature · 1 line
2.37.3
Julien Panis
reviewerPATCH v8 2/4 Documentation: ABI: sysfs-bus-counter: add frequency & num_overflows items
In reply to Julien PanisThis commit adds frequency and num_overflows items to counter ABI file (e.g. for TI ECAP hardware used in capture operating mode).
--- Documentation/ABI/testing/sysfs-bus-counter | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Documentation/ABI/testing/sysfs-bus-counter +14 -0
@@ -209,6 +209,12 @@ Description: both edges: Any state transition. +What: /sys/bus/counter/devices/counterX/countY/num_overflows +KernelVersion: 6.1 +Contact: [email protected] +Description: + This attribute indicates the number of overflows of count Y. + What: /sys/bus/counter/devices/counterX/countY/ceiling_component_id What: /sys/bus/counter/devices/counterX/countY/floor_component_id What: /sys/bus/counter/devices/counterX/countY/count_mode_component_id
@@ -219,11 +225,13 @@ What: /sys/bus/counter/devices/counterX/countY/prescaler_component_id What: /sys/bus/counter/devices/counterX/countY/preset_component_id What: /sys/bus/counter/devices/counterX/countY/preset_enable_component_id What: /sys/bus/counter/devices/counterX/countY/signalZ_action_component_id +What: /sys/bus/counter/devices/counterX/countY/num_overflows_component_id What: /sys/bus/counter/devices/counterX/signalY/cable_fault_component_id What: /sys/bus/counter/devices/counterX/signalY/cable_fault_enable_component_id What: /sys/bus/counter/devices/counterX/signalY/filter_clock_prescaler_component_id What: /sys/bus/counter/devices/counterX/signalY/index_polarity_component_id What: /sys/bus/counter/devices/counterX/signalY/synchronous_mode_component_id +What: /sys/bus/counter/devices/counterX/signalY/frequency_component_id KernelVersion: 5.16 Contact: [email protected] Description:
@@ -364,3 +372,9 @@ Description: via index_polarity. The index function (as enabled via preset_enable) is performed synchronously with the quadrature clock on the active level of the index input. + +What: /sys/bus/counter/devices/counterX/signalY/frequency +KernelVersion: 6.1 +Contact: [email protected] +Description: + Read-only attribute that indicates the signal Y frequency, in Hz.
Signature · 1 line
2.37.3
Julien Panis
reviewerPATCH v8 3/4 counter: ti-ecap-capture: capture driver support for ECAP
In reply to Julien PanisECAP hardware on TI AM62x SoC supports capture feature. It can be used to timestamp events (falling/rising edges) detected on input signal.
This commit adds capture driver support for ECAP hardware on AM62x SoC.
In the ECAP hardware, capture pin can also be configured to be in PWM mode. Current implementation only supports capture operating mode. Hardware also supports timebase sync between multiple instances, but this driver supports simple independent capture functionality.
--- drivers/counter/Kconfig | 15 + drivers/counter/Makefile | 1 + drivers/counter/ti-ecap-capture.c | 621 ++++++++++++++++++++++++++++++ 3 files changed, 637 insertions(+) create mode 100644 drivers/counter/ti-ecap-capture.c
drivers/counter/Kconfig +15 -0
@@ -101,4 +101,19 @@ config INTEL_QEP To compile this driver as a module, choose M here: the module will be called intel-qep. +config TI_ECAP_CAPTURE + tristate "TI eCAP capture driver" + depends on ARCH_OMAP2PLUS || ARCH_DAVINCI_DA8XX || ARCH_KEYSTONE || ARCH_K3 || COMPILE_TEST + depends on HAS_IOMEM + select REGMAP_MMIO + help + Select this option to enable the Texas Instruments Enhanced Capture + (eCAP) driver in input mode. + + It can be used to timestamp events (falling/rising edges) detected + on ECAP input signal. + + To compile this driver as a module, choose M here: the module + will be called ti-ecap-capture. + endif # COUNTER
drivers/counter/Makefile +1 -0
@@ -14,3 +14,4 @@ obj-$(CONFIG_TI_EQEP) += ti-eqep.o obj-$(CONFIG_FTM_QUADDEC) += ftm-quaddec.o obj-$(CONFIG_MICROCHIP_TCB_CAPTURE) += microchip-tcb-capture.o obj-$(CONFIG_INTEL_QEP) += intel-qep.o +obj-$(CONFIG_TI_ECAP_CAPTURE) += ti-ecap-capture.o
drivers/counter/ti-ecap-capture.c new file +621 -0
621 lines not on this page · raw message
Signature · 1 line
2.37.3
Re: [PATCH v8 3/4] counter: ti-ecap-capture: capture driver support for ECAP
Quoted 11 lines · Julien Panis
ECAP hardware on TI AM62x SoC supports capture feature. It can be used to timestamp events (falling/rising edges) detected on input signal.
This commit adds capture driver support for ECAP hardware on AM62x SoC.
In the ECAP hardware, capture pin can also be configured to be in PWM mode. Current implementation only supports capture operating mode. Hardware also supports timebase sync between multiple instances, but this driver supports simple independent capture functionality.
Signed-off-by Julien Panis <[email protected]>
Hello Julien,
Comments follow inline below.
Quoted 8 lines
drivers/counter/ti-ecap-capture.c new file +8
-0+/** +* struct ecap_cnt_dev - device private data structure +* @enabled: device state +* @clk: device clock +* @regmap: device register map +* @nb_ovf: number of overflows since capture start +* @pm_ctx: device context for PM operations +*/
drivers/counter/ti-ecap-capture.c line 93
Quoted 10 lines
drivers/counter/ti-ecap-capture.c new file +10
-0+struct ecap_cnt_dev { +bool enabled; +struct clk *clk; +struct regmap *regmap; +atomic_t nb_ovf; +struct { +u8 ev_mode; +u32 time_cntr; + } pm_ctx; +};
Provide documentation for the ev_mode and time_cntr members. You probably need a lock as well to protect access to this structure or you'll end up with race problems.
Quoted 14 lines
drivers/counter/ti-ecap-capture.c new file +14
-0+static void ecap_cnt_capture_enable (struct counter_device *counter) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +pm_runtime_get_sync (counter->parent); + +/* Enable interrupts on events */ +regmap_update_bits (ecap_dev->regmap,ECAP_ECINT_EN_FLG_REG , +ECAP_EVT_EN_MASK ,ECAP_EVT_EN_MASK ); + +/* Run counter */ +regmap_update_bits (ecap_dev->regmap,ECAP_ECCTL_REG ,ECAP_ECCTL_CFG_MASK , +ECAP_SYNCO_DIS_MASK |ECAP_STOPVALUE_MASK |ECAP_ECCTL_EN_MASK ); +}
drivers/counter/ti-ecap-capture.c line 139
Quoted 10 lines
drivers/counter/ti-ecap-capture.c new file +10
-0+ +static void ecap_cnt_capture_disable (struct counter_device *counter) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +/* Disable interrupts on events */ +regmap_update_bits (ecap_dev->regmap,ECAP_ECINT_EN_FLG_REG ,ECAP_EVT_EN_MASK ,0 ); + +/* Stop counter */ +regmap_update_bits (ecap_dev->regmap,ECAP_ECCTL_REG ,ECAP_ECCTL_EN_MASK ,0 );
Shouldn't the counter be stopped before stopping the interrupts?
Quoted 14 lines
drivers/counter/ti-ecap-capture.c new file +14
-0+static int ecap_cnt_count_get_val (struct counter_device *counter,unsigned int reg,u32 *val) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); +unsigned int regval; + +pm_runtime_get_sync (counter->parent); +regmap_read (ecap_dev->regmap, reg, ®val); +pm_runtime_put_sync (counter->parent); + + *val = regval; + +return 0 ; +} +
drivers/counter/ti-ecap-capture.c line 167
Quoted 10 lines
drivers/counter/ti-ecap-capture.c new file +10
-0+static int ecap_cnt_count_set_val (struct counter_device *counter,unsigned int reg,u32 val) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +pm_runtime_get_sync (counter->parent); +regmap_write (ecap_dev->regmap, reg, val); +pm_runtime_put_sync (counter->parent); + +return 0 ; +}
The ecap_cnt_count_get_val() and ecap_cnt_count_set_val() functions only ever return 0. Redefine them as void functions and eliminate the unnecessary returns.
drivers/counter/ti-ecap-capture.c line 186
Quoted 7 lines
drivers/counter/ti-ecap-capture.c new file +7
-0+static int ecap_cnt_count_write (struct counter_device *counter, +struct counter_count *count,u64 val) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +if (ecap_dev->enabled) +return -EBUSY ;
You should return -EBUSY when the requested operation cannot be completed because the device currently performing a task -- i.e. the requested operation would stall or otherwise fail if forced. In this case, the count value actually can be set while the device is enabled, if I'm not mistaken; the count just continues increasing from the new written value (i.e. no stall/failure). Therefore, there's not need to return -EBUSY here and this check can be eliminated.
drivers/counter/ti-ecap-capture.c line 262
Quoted 8 lines
drivers/counter/ti-ecap-capture.c new file +8
-0+static int ecap_cnt_pol_write (struct counter_device *counter, +struct counter_signal *signal, +size_t idx,enum counter_signal_polarity pol) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +if (ecap_dev->enabled) +return -EBUSY ;
I suspect this check can go away for the same reason as above.
drivers/counter/ti-ecap-capture.c line 293
Quoted 8 lines
drivers/counter/ti-ecap-capture.c new file +8
-0+static int ecap_cnt_cap_write (struct counter_device *counter, +struct counter_count *count, +size_t idx,u64 cap) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +if (ecap_dev->enabled) +return -EBUSY ;
Same comment as above.
drivers/counter/ti-ecap-capture.c line 316
Quoted 7 lines
drivers/counter/ti-ecap-capture.c new file +7
-0+static int ecap_cnt_nb_ovf_write (struct counter_device *counter, +struct counter_count *count,u64 val) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +if (ecap_dev->enabled) +return -EBUSY ;
Same comment as above.
drivers/counter/ti-ecap-capture.c line 433
Quoted 3 lines
drivers/counter/ti-ecap-capture.c new file +3
-0+static struct counter_count ecap_cnt_counts[] = { + { + .id =0 ,
The id member is for differentiating between multiple Counts. You only have one Count in this driver so you don't need to set it because you never use it.
William Breathitt Gray
Julien Panis
reviewerRe: [PATCH v8 3/4] counter: ti-ecap-capture: capture driver support for ECAP
Quoted 29 lines · William Breathitt Gray
Quoted 11 lines · Julien Panis
ECAP hardware on TI AM62x SoC supports capture feature. It can be used to timestamp events (falling/rising edges) detected on input signal.
This commit adds capture driver support for ECAP hardware on AM62x SoC.
In the ECAP hardware, capture pin can also be configured to be in PWM mode. Current implementation only supports capture operating mode. Hardware also supports timebase sync between multiple instances, but this driver supports simple independent capture functionality.
Signed-off-by Julien Panis <[email protected]>Hello Julien,
Comments follow inline below.
Quoted 13 lines
drivers/counter/ti-ecap-capture.c new file +13
-0+/** +* struct ecap_cnt_dev - device private data structure +* @enabled: device state +* @clk: device clock +* @regmap: device register map +* @nb_ovf: number of overflows since capture start +* @pm_ctx: device context for PM operations +*/ +struct ecap_cnt_dev { +bool enabled; +struct clk *clk; +struct regmap *regmap; +atomic_t nb_ovf;
Quoted 8 lines
drivers/counter/ti-ecap-capture.c line 93
Quoted 5 lines
drivers/counter/ti-ecap-capture.c new file +5
-0+struct { +u8 ev_mode; +u32 time_cntr; + } pm_ctx; +};Provide documentation for the
ev_modeandtime_cntrmembers. You probably need a lock as well to protect access to this structure or you'll end up with race problems.
Hi William,
How can I end up with race problems ? pm_ctx members are only accessed at suspend (after capture/IRQ are disabled) and resume (before capture/IRQ are re-enabled). Is there any risk I did not identify ?
Julien
Quoted 108 lines
Quoted 16 lines
drivers/counter/ti-ecap-capture.c new file +14
-0+static void ecap_cnt_capture_enable (struct counter_device *counter) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +pm_runtime_get_sync (counter->parent); + +/* Enable interrupts on events */ +regmap_update_bits (ecap_dev->regmap,ECAP_ECINT_EN_FLG_REG , +ECAP_EVT_EN_MASK ,ECAP_EVT_EN_MASK ); + +/* Run counter */ +regmap_update_bits (ecap_dev->regmap,ECAP_ECCTL_REG ,ECAP_ECCTL_CFG_MASK , +ECAP_SYNCO_DIS_MASK |ECAP_STOPVALUE_MASK |ECAP_ECCTL_EN_MASK ); +}drivers/counter/ti-ecap-capture.c line 139
Quoted 10 lines
drivers/counter/ti-ecap-capture.c new file +10
-0+ +static void ecap_cnt_capture_disable (struct counter_device *counter) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +/* Disable interrupts on events */ +regmap_update_bits (ecap_dev->regmap,ECAP_ECINT_EN_FLG_REG ,ECAP_EVT_EN_MASK ,0 ); + +/* Stop counter */ +regmap_update_bits (ecap_dev->regmap,ECAP_ECCTL_REG ,ECAP_ECCTL_EN_MASK ,0 );Shouldn't the counter be stopped before stopping the interrupts?
Quoted 14 lines
drivers/counter/ti-ecap-capture.c new file +14
-0+static int ecap_cnt_count_get_val (struct counter_device *counter,unsigned int reg,u32 *val) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); +unsigned int regval; + +pm_runtime_get_sync (counter->parent); +regmap_read (ecap_dev->regmap, reg, ®val); +pm_runtime_put_sync (counter->parent); + + *val = regval; + +return 0 ; +} +drivers/counter/ti-ecap-capture.c line 167
Quoted 10 lines
drivers/counter/ti-ecap-capture.c new file +10
-0+static int ecap_cnt_count_set_val (struct counter_device *counter,unsigned int reg,u32 val) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +pm_runtime_get_sync (counter->parent); +regmap_write (ecap_dev->regmap, reg, val); +pm_runtime_put_sync (counter->parent); + +return 0 ; +}The
ecap_cnt_count_get_val()andecap_cnt_count_set_val()functions only ever return 0. Redefine them as void functions and eliminate the unnecessary returns.drivers/counter/ti-ecap-capture.c line 186
Quoted 7 lines
drivers/counter/ti-ecap-capture.c new file +7
-0+static int ecap_cnt_count_write (struct counter_device *counter, +struct counter_count *count,u64 val) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +if (ecap_dev->enabled) +return -EBUSY ;You should return
-EBUSYwhen the requested operation cannot be completed because the device currently performing a task -- i.e. the requested operation would stall or otherwise fail if forced. In this case, the count value actually can be set while the device is enabled, if I'm not mistaken; the count just continues increasing from the new written value (i.e. no stall/failure). Therefore, there's not need to return-EBUSYhere and this check can be eliminated.drivers/counter/ti-ecap-capture.c line 262
Quoted 8 lines
drivers/counter/ti-ecap-capture.c new file +8
-0+static int ecap_cnt_pol_write (struct counter_device *counter, +struct counter_signal *signal, +size_t idx,enum counter_signal_polarity pol) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +if (ecap_dev->enabled) +return -EBUSY ;I suspect this check can go away for the same reason as above.
drivers/counter/ti-ecap-capture.c line 293
Quoted 8 lines
drivers/counter/ti-ecap-capture.c new file +8
-0+static int ecap_cnt_cap_write (struct counter_device *counter, +struct counter_count *count, +size_t idx,u64 cap) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +if (ecap_dev->enabled) +return -EBUSY ;Same comment as above.
drivers/counter/ti-ecap-capture.c line 316
Quoted 7 lines
drivers/counter/ti-ecap-capture.c new file +7
-0+static int ecap_cnt_nb_ovf_write (struct counter_device *counter, +struct counter_count *count,u64 val) +{ +struct ecap_cnt_dev *ecap_dev =counter_priv (counter); + +if (ecap_dev->enabled) +return -EBUSY ;Same comment as above.
drivers/counter/ti-ecap-capture.c line 433
Quoted 3 lines
drivers/counter/ti-ecap-capture.c new file +3
-0+static struct counter_count ecap_cnt_counts[] = { + { + .id =0 ,The id member is for differentiating between multiple Counts. You only have one Count in this driver so you don't need to set it because you never use it.
William Breathitt Gray
Re: [PATCH v8 3/4] counter: ti-ecap-capture: capture driver support for ECAP
Quoted 41 lines · Julien Panis
Quoted 29 lines · William Breathitt Gray
Quoted 11 lines · Julien Panis
ECAP hardware on TI AM62x SoC supports capture feature. It can be used to timestamp events (falling/rising edges) detected on input signal.
This commit adds capture driver support for ECAP hardware on AM62x SoC.
In the ECAP hardware, capture pin can also be configured to be in PWM mode. Current implementation only supports capture operating mode. Hardware also supports timebase sync between multiple instances, but this driver supports simple independent capture functionality.
Signed-off-by Julien Panis <[email protected]>Hello Julien,
Comments follow inline below.
Quoted 13 lines
drivers/counter/ti-ecap-capture.c new file +13
-0+/** +* struct ecap_cnt_dev - device private data structure +* @enabled: device state +* @clk: device clock +* @regmap: device register map +* @nb_ovf: number of overflows since capture start +* @pm_ctx: device context for PM operations +*/ +struct ecap_cnt_dev { +bool enabled; +struct clk *clk; +struct regmap *regmap; +atomic_t nb_ovf;Quoted 8 lines
drivers/counter/ti-ecap-capture.c line 93
Quoted 5 lines
drivers/counter/ti-ecap-capture.c new file +5
-0+struct { +u8 ev_mode; +u32 time_cntr; + } pm_ctx; +};Provide documentation for the
ev_modeandtime_cntrmembers. You probably need a lock as well to protect access to this structure or you'll end up with race problems.
Quoted 8 lines
Hi William,
How can I end up with race problems ?
pm_ctxmembers are only accessed at suspend (after capture/IRQ are disabled) and resume (before capture/IRQ are re-enabled).
Is there any risk I did not identify ?Julien
I was thinking of the ecap_cnt_dev enabled member. The Counter callbacks may execute in concurrent threads, so races can appear when you access members of the ecap_cnt_dev structure in these callbacks.
Take for example this section of ecap_cnt_enable_write():
if (enable == ecap_dev->enabled)
return 0;
if (enable)
ecap_cnt_capture_enable(counter);
else
ecap_cnt_capture_disable(counter);
ecap_dev->enabled = enableSuppose two threads try to enable the count capture. A race condition is present where the two threads could see ecap_dev->enabled as false and both proceed to call ecap_cnt_capture_enable(). This results in pm_runtime_get_sync() bumping the usage count twice and we're left with a mismatch the next time ecap_cnt_capture_disable() is called.
William Breathitt Gray
Julien Panis
reviewerRe: [PATCH v8 3/4] counter: ti-ecap-capture: capture driver support for ECAP
Quoted 62 lines · William Breathitt Gray
Quoted 39 lines · Julien Panis
Quoted 29 lines · William Breathitt Gray
Quoted 11 lines · Julien Panis
ECAP hardware on TI AM62x SoC supports capture feature. It can be used to timestamp events (falling/rising edges) detected on input signal.
This commit adds capture driver support for ECAP hardware on AM62x SoC.
In the ECAP hardware, capture pin can also be configured to be in PWM mode. Current implementation only supports capture operating mode. Hardware also supports timebase sync between multiple instances, but this driver supports simple independent capture functionality.
Signed-off-by Julien Panis <[email protected]>Hello Julien,
Comments follow inline below.
Quoted 13 lines
drivers/counter/ti-ecap-capture.c new file +13
-0+/** +* struct ecap_cnt_dev - device private data structure +* @enabled: device state +* @clk: device clock +* @regmap: device register map +* @nb_ovf: number of overflows since capture start +* @pm_ctx: device context for PM operations +*/ +struct ecap_cnt_dev { +bool enabled; +struct clk *clk; +struct regmap *regmap; +atomic_t nb_ovf;Quoted 8 lines
drivers/counter/ti-ecap-capture.c line 93
Quoted 5 lines
drivers/counter/ti-ecap-capture.c new file +5
-0+struct { +u8 ev_mode; +u32 time_cntr; + } pm_ctx; +};Provide documentation for the
ev_modeandtime_cntrmembers. You probably need a lock as well to protect access to this structure or you'll end up with race problems.Quoted 8 lines
Hi William,
How can I end up with race problems ?
pm_ctxmembers are only accessed at suspend (after capture/IRQ are disabled) and resume (before capture/IRQ are re-enabled).
Is there any risk I did not identify ?Julien
I was thinking of the
ecap_cnt_devenabled member. The Counter callbacks may execute in concurrent threads, so races can appear when you access members of theecap_cnt_devstructure in these callbacks.Take for example this section of
ecap_cnt_enable_write():if (enable == ecap_dev->enabled) return 0; if (enable) ecap_cnt_capture_enable(counter); else ecap_cnt_capture_disable(counter); ecap_dev->enabled = enable
Quoted 7 lines
Suppose two threads try to enable the count capture. A race condition is present where the two threads could see
ecap_dev->enabledas false and both proceed to callecap_cnt_capture_enable(). This results inpm_runtime_get_sync()bumping the usage count twice and we're left with a mismatch the next timeecap_cnt_capture_disable()is called.William Breathitt Gray
OK, If I understand well there's the same problem with IO access with regmap ? Julien
Julien Panis
reviewerRe: [PATCH v8 3/4] counter: ti-ecap-capture: capture driver support for ECAP
Quoted 71 lines · Julien Panis
Quoted 68 lines · William Breathitt Gray
Quoted 39 lines · Julien Panis
Quoted 29 lines · William Breathitt Gray
Quoted 11 lines · Julien Panis
ECAP hardware on TI AM62x SoC supports capture feature. It can be used to timestamp events (falling/rising edges) detected on input signal.
This commit adds capture driver support for ECAP hardware on AM62x SoC.
In the ECAP hardware, capture pin can also be configured to be in PWM mode. Current implementation only supports capture operating mode. Hardware also supports timebase sync between multiple instances, but this driver supports simple independent capture functionality.
Signed-off-by Julien Panis <[email protected]>Hello Julien,
Comments follow inline below.
Quoted 13 lines
drivers/counter/ti-ecap-capture.c new file +13
-0+/** +* struct ecap_cnt_dev - device private data structure +* @enabled: device state +* @clk: device clock +* @regmap: device register map +* @nb_ovf: number of overflows since capture start +* @pm_ctx: device context for PM operations +*/ +struct ecap_cnt_dev { + bool enabled; + struct clk *clk; + struct regmap *regmap; + atomic_t nb_ovf;Quoted 8 lines
Quoted 5 lines
+ struct {
+ u8ev_mode;
+ u32time_cntr;
+ }pm_ctx;
+};Provide documentation for the
ev_modeandtime_cntrmembers. You probably need a lock as well to protect access to this structure or you'll end up with race problems.Quoted 8 lines
Hi William,
How can I end up with race problems ?
pm_ctxmembers are only accessed at suspend (after capture/IRQ are disabled) and resume (before capture/IRQ are re-enabled).
Is there any risk I did not identify ?Julien
I was thinking of the
ecap_cnt_devenabled member. The Counter callbacks may execute in concurrent threads, so races can appear when you access members of theecap_cnt_devstructure in these callbacks.Take for example this section of
ecap_cnt_enable_write():if (enable ==
ecap_dev->enabled) return 0;
if (enable)ecap_cnt_capture_enable(counter); elseecap_cnt_capture_disable(counter);ecap_dev->enabled= enableSuppose two threads try to enable the count capture. A race condition is present where the two threads could see
ecap_dev->enabledas false and both proceed to callecap_cnt_capture_enable(). This results inpm_runtime_get_sync()bumping the usage count twice and we're left with a mismatch the next timeecap_cnt_capture_disable()is called.
Quoted 4 lines
Quoted 1 line
William Breathitt Gray
OK, If I understand well there's the same problem with IO access with regmap ? Julien
[ERRATUM] It seems that some spinlock is already used by regmap API. So, only the 'enabled' member needs a lock.
Re: [PATCH v8 3/4] counter: ti-ecap-capture: capture driver support for ECAP
Quoted 79 lines · Julien Panis
Quoted 76 lines · Julien Panis
Quoted 73 lines · William Breathitt Gray
Quoted 44 lines · Julien Panis
Quoted 32 lines · William Breathitt Gray
Quoted 7 lines · Julien Panis
ECAP hardware on TI AM62x SoC supports capture feature. It can be used
to timestamp events (falling/rising edges) detected on input signal.This commit adds capture driver support for ECAP
hardware on AM62x SoC.Quoted 7 lines
In the ECAP hardware, capture pin can also be configured to be in PWM mode. Current implementation only supports capture operating mode.
Hardware also supports timebase sync between multiple instances, but this driver supports simple independent capture functionality.Signed-off-by Julien Panis <[email protected]>Hello Julien,
Comments follow inline below.
Quoted 13 lines
drivers/counter/ti-ecap-capture.c new file +13
-0+/** +* struct ecap_cnt_dev - device private data structure +* @enabled: device state +* @clk: device clock +* @regmap: device register map +* @nb_ovf: number of overflows since capture start +* @pm_ctx: device context for PM operations +*/ +struct ecap_cnt_dev { + bool enabled; + struct clk *clk; + struct regmap *regmap; + atomic_t nb_ovf;Quoted 8 lines
Quoted 5 lines
+ struct {
+ u8ev_mode;
+ u32time_cntr;
+ }pm_ctx;
+};Provide documentation for the
ev_modeandtime_cntrmembers. You probably need a lock as well to protect access to this structure or you'll end up with race problems.Hi William,
Quoted 8 lines
How can I end up with race problems ?
pm_ctxmembers are only accessed at
suspend (after capture/IRQ are disabled) and resume (before capture/IRQ are
re-enabled).
Is there any risk I did not identify ?Julien
I was thinking of the
ecap_cnt_devenabled member. The Counter callbacks may execute in concurrent threads, so races can appear when you access members of theecap_cnt_devstructure in these callbacks.Take for example this section of
ecap_cnt_enable_write():if (enable ==
ecap_dev->enabled) return 0;
if (enable)ecap_cnt_capture_enable(counter); elseecap_cnt_capture_disable(counter);ecap_dev->enabled= enableSuppose two threads try to enable the count capture. A race condition is present where the two threads could see
ecap_dev->enabledas false and both proceed to callecap_cnt_capture_enable(). This results inpm_runtime_get_sync()bumping the usage count twice and we're left with a mismatch the next timeecap_cnt_capture_disable()is called.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
William Breathitt Gray
OK, If I understand well there's the same problem with IO access with regmap ?
Julien[ERRATUM] It seems that some spinlock is already used by regmap API. So, only the 'enabled' member needs a lock.
Keep in mind that although individual regmap API calls can be considered atomic, the order of the calls made could be a point of race if there are concurrent threads. However, in your particular driver it looks you're fine as long as access to 'enabled' is protected.
William Breathitt Gray
Julien Panis
reviewerPATCH v8 4/4 MAINTAINERS: add TI ECAP driver info
In reply to Julien PanisThis commit adds driver info for TI ECAP used in capture operating mode.
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -20322,6 +20322,13 @@ T: git git://linuxtv.org/mhadli/v4l-dvb-davinci_devices.git F: drivers/media/platform/ti/davinci/ F: include/media/davinci/ +TI ENHANCED CAPTURE (eCAP) DRIVER +M: Julien Panis <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/counter/ti,am62-ecap-capture.yaml +F: drivers/counter/ti-ecap-capture.c + TI ENHANCED QUADRATURE ENCODER PULSE (eQEP) DRIVER R: David Lechner <[email protected]> L: [email protected]
Signature · 1 line
2.37.3