Changes in v5:
- Organize counter subsystem to its own drivers/counter/ directory
- Remove Simple Counter and Quadrature Counter interfaces
- Remove simple_write callback from Generic Counter interface
- Refactor documentation to utilize Sphinx for code inclusion
- Remove dummy-counter driver
- Remove unnecessary character device
- Enforce interface via opaque callback value types and functions
- Add ENUM macros for Signal, Count, and device extensions
- Resolve const qualifier discard warnings by utilizing containers
- Remove "synapses" attribute
- Reorganize Signal and Count attributes to respective subdirectories
- Append "_list" suffix to the "actions" and "functions" symbols
- Reimplement all 104-QUAD-8 extension attributes
- Add STM32 Timer quadrature encoder driver
Overview
========
This version of the patchset has been primarily a simplification and
stablization effort. As was brought up in review of the previous
release, the Simple Counter and Quadrature Counter interfaces added a
lot of complexity for little benefit to the ABI. I decided keeping it
simple is a good approach for an interface, as it should keep the system
code easier to maintainer while still allowing consumers the flexibility
to utilize the interface to their requirements.
Opaque structures
=================
I still believe it is important to maintain some control over the
expected data types accessed by the Generic Counter interface so I have
introduced three opaque structures to represent Signal and Count values:
* struct signal_read_value
* struct count_read_value
* struct count_write_value
As their names imply, these three opaque structs representing the Signal
read value, Count read value, and Count write value. Drivers should not
interact with these opaque structures directly, but rather utilize the
following functions respectively:
* set_signal_read_value
* set_count_read_value
* get_count_write_value
These functions respectively take a "type" parameter which specifies the
the data type the driver expects. For Count values, the following two
types are introduced in this patchset:
* COUNT_POSITION_UNSIGNED:
Position represented as an unsigned integer
* COUNT_POSITION_SIGNED:
Position represented as a signed integer
Similarly, Signal values may be represented by the following type
introduced in this patchset:
* SIGNAL_LEVEL:
Input signal level represented by two discrete states:
low (SIGNAL_LEVEL_LOW) or high (SIGNAL_LEVEL_HIGH)
I expect new types to be introduced as future counter drivers are
merged, thus allowing the interface to grow in a controlled fashion.
Helper macros
=============
This patchset introduces the following six helper macros for defining
extension attributes:
* COUNTER_SIGNAL_ENUM
* COUNTER_SIGNAL_ENUM_AVAILABLE
* COUNTER_COUNT_ENUM
* COUNTER_COUNT_ENUM_AVAILABLE
* COUNTER_DEVICE_ENUM
* COUNTER_DEVICE_ENUM_AVAILABLE
These macros were based off of the IIO_ENUM and IIO_ENUM_AVAILABLE
macros and fulfill a similar function for the Generic Counter interface:
simplify boilerplate code for enum Signal, Count, or global counter
device extension attributes.
Compiler warnings resolution
============================
In the previous revision of this patchset, several const qualifier
discard warnings were displayed due to the generic use of the
counter_attribute_create function to map a struct counter_device_attr
to an attribute callback. The essential issue was passing a Signal,
Count, or extension's component data (which may be const) via the void
pointer provided in struct counter_device_attr.
To resolve this issue, I implemented containers for each distinct type
of attribute. The respective container is allocated for the attribute,
populated with the relevant data, and then passed via the existing
struct counter_device_attr code. This method allows the container to
maintain the necessary const qualifier for the component data, while
still permitting the generic behavior of counter_attribute_create.
The containers are purely a system implementation detail, so they are
not exposed outside of generic-counter.c file, but I'll list them here
for reference:
* struct signal_comp_t
* struct name_comp_t
* struct signal_ext_comp_t
* struct action_comp_t
* struct action_avail_comp_t
* struct count_comp_t
* struct count_ext_comp_t
* struct funct_avail_comp_t
* struct ext_comp_t
Signal and Count subdirectories
===============================
The Signal and Count attributes are now organized within respective
subdirectories. A subdirectory is created and populated for each Signal
in the following format, where X is the unqiue ID of the counter device
and Y is the unique ID of the respective Signal:
/sys/bus/counter/devices/counterX/signalY
Similarly, each Count has a subdirectory created to house its relevant
sysfs attributes, where Y is the unique ID of the respective Count:
/sys/bus/counter/devices/counterX/countY
In addition, I have removed the "countY_synapses" attribute because it
did not conform to the sysfs rule of one attribute one thing. I believe
this would be better implemented inside the respective Count's
subdirectory as a further "synapses" subdirectory with links within to
the respective Signal subdirectories. However, I'm not quite sure how to
implement this yet; struct attribute_group appears to allow only one
level of subdirectories, so to support two or more would require another
approach that I will need to research.
Something to consider if we do implement "synapses" subdirectories is
whether it is convenient to place the "action" and "action_available"
attributes within to keep synapse information together.
STM32 timer quadrature encoder driver
=====================================
Benjamin Gaignard is providing a counter driver in patchset for the
STM32 timer quadrature encoder. The driver makes use of the Generic
Counter interface and its simplicity conveniently serves as a further
reference example for future authors.
Now that the 104-QUAD-8 driver has all extension attributes
reimplemented to utilize the Generic Counter interface, I decided to
drop the dummer-counter in favor of providing drivers for real devices
to serve as example -- this gives up the benefit of supporting real
devices to make sure the interface is robust and adequately useful.
Although not included with this patchset, the stm32-lptimer-cnt.c file
can also be updated with Generic Counter interface support. Supporting
more devices to see what is needed or lacking should help the Generic
Counter interface evolve.
Merge conflict warning
======================
Changes in the ISA_BUS_API Kconfig option are expected for the 4.17
merge, and I expect a subsequent merge conflict to pop up regarding the
104_QUAD_8 Kconfig option. In particular the following patch has been
picked up in the gpio subsystem tree for merge during the 4.17 window:
https://patchwork.ozlabs.org/patch/853987/
To resolve this conflict, the 104_QUAD_8 Kconfig option simply needs to
select ISA_BUS_API rather than depend on it.
William Breathitt Gray
Benjamin Gaignard (3):
dt-bindings: counter: Document stm32 quadrature encoder
counter: stm32-timer-cnt: Add sysfs documentation
Counter: Add STM32 Timer quadrature encoder
William Breathitt Gray (5):
counter: Introduce the Generic Counter interface
counter: Documentation: Add Generic Counter sysfs documentation
docs: Add Generic Counter interface documentation
counter: 104-quad-8: Add Generic Counter interface support
counter: 104-quad-8: Documentation: Add Generic Counter sysfs
documentation
Documentation/ABI/testing/sysfs-bus-counter | 120 ++
.../ABI/testing/sysfs-bus-counter-104-quad-8 | 115 ++
.../ABI/testing/sysfs-bus-counter-stm32-timer-cnt | 21 +
.../bindings/counter/stm32-timer-cnt.txt | 26 +
.../devicetree/bindings/mfd/stm32-timers.txt | 7 +
Documentation/driver-api/generic-counter.rst | 321 +++++
Documentation/driver-api/index.rst | 1 +
MAINTAINERS | 14 +-
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/counter/104-quad-8.c | 1261 +++++++++++++++++
drivers/counter/Kconfig | 58 +
drivers/{iio => }/counter/Makefile | 6 +-
drivers/counter/generic-counter.c | 1416 ++++++++++++++++++++
drivers/{iio => }/counter/stm32-lptimer-cnt.c | 0
drivers/counter/stm32-timer-cnt.c | 356 +++++
drivers/iio/Kconfig | 1 -
drivers/iio/Makefile | 1 -
drivers/iio/counter/104-quad-8.c | 596 --------
drivers/iio/counter/Kconfig | 33 -
include/linux/counter.h | 524 ++++++++
21 files changed, 4246 insertions(+), 634 deletions(-)
create mode 100644 Documentation/ABI/testing/sysfs-bus-counter
create mode 100644 Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
create mode 100644 Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt
create mode 100644 Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt
create mode 100644 Documentation/driver-api/generic-counter.rst
create mode 100644 drivers/counter/104-quad-8.c
create mode 100644 drivers/counter/Kconfig
rename drivers/{iio => }/counter/Makefile (52%)
create mode 100644 drivers/counter/generic-counter.c
rename drivers/{iio => }/counter/stm32-lptimer-cnt.c (100%)
create mode 100644 drivers/counter/stm32-timer-cnt.c
delete mode 100644 drivers/iio/counter/104-quad-8.c
delete mode 100644 drivers/iio/counter/Kconfig
create mode 100644 include/linux/counter.h
--
2.16.2
This patch adds standard documentation for the userspace sysfs
attributes of the Generic Counter interface.
Signed-off-by: William Breathitt Gray <[email protected]>
---
Documentation/ABI/testing/sysfs-bus-counter | 120 ++++++++++++++++++++++++++++
MAINTAINERS | 1 +
2 files changed, 121 insertions(+)
create mode 100644 Documentation/ABI/testing/sysfs-bus-counter
diff --git a/Documentation/ABI/testing/sysfs-bus-counter b/Documentation/ABI/testing/sysfs-bus-counter
new file mode 100644
index 000000000000..64785aab1763
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-counter
@@ -0,0 +1,120 @@
+What: /sys/bus/counter/devices/counterX/countY/count
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Count data of Count Y. The following data types are available:
+
+ COUNT_POSITION_UNSIGNED:
+ Unsigned integer value representing position.
+
+ COUNT_POSITION_SIGNED:
+ Signed integer value representing position.
+
+What: /sys/bus/counter/devices/counterX/countY/function
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Count function mode of Count Y; count function evaluation is
+ triggered by conditions specified by the countY_signalZ_action
+ attributes. The following count functions are available:
+
+ Increase:
+ Accumulated count is incremented.
+
+ Decrease:
+ Accumulated count is decremented.
+
+ Pulse-Direction:
+ Rising edges on quadrature pair signal A updates the
+ respective count. The input level of quadrature pair
+ signal B determines direction.
+
+ Quadrature x1:
+ If direction is forward, rising edges on quadrature pair
+ signal A updates the respective count; if the direction
+ is backward, falling edges on quadrature pair signal A
+ updates the respective count. Quadrature encoding
+ determines the direction.
+
+ Quadrature x2:
+ Any state transition on quadrature pair signal A updates
+ the respective count. Quadrature encoding determines the
+ direction.
+
+ Quadrature x4:
+ Any state transition on either quadrature pair signals
+ updates the respective count. Quadrature encoding
+ determines the direction.
+
+What: /sys/bus/counter/devices/counterX/countY/function_available
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Discrete set of available count function modes for the
+ configuration of the respective Count Y are listed in this file.
+
+What: /sys/bus/counter/devices/counterX/countY/name
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Read-only attribute that indicates the device-specific name of
+ Count Y. If possible, this should match the name of the
+ respective channel as it appears in the device datasheet
+ documentation text.
+
+What: /sys/bus/counter/devices/counterX/countY/signalZ_action
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Action mode of Count Y for Signal Z. This attribute indicates
+ the condition of Signal Z that triggers the count function
+ evaluation for Count Y. The following action modes are
+ available:
+
+ None:
+ Signal does not trigger the count function. In
+ Pulse-Direction count function mode, this Signal is
+ evaluated as Direction.
+
+ Rising Edge:
+ Low state transitions to high state.
+
+ Falling Edge:
+ High state transitions to low state.
+
+ Both Edges:
+ Any state transition.
+
+What: /sys/bus/counter/devices/counterX/countY/signalZ_action_available
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Discrete set of available action modes are listed in this file
+ for the configuration of the respective Synapse associating
+ Signal Z to Count Y.
+
+What: /sys/bus/counter/devices/counterX/name
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Read-only attribute that indicates the device-specific name of
+ the Counter. This should match the name of the device as it
+ appears in its respective datasheet documentation text.
+
+What: /sys/bus/counter/devices/counterX/signalY/signal
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Signal data of Signal Y. The following data types are available:
+
+ SIGNAL_LEVEL:
+ Respective input has two discrete states: Low and High.
+
+What: /sys/bus/counter/devices/counterX/signalY/name
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Read-only attribute that indicates the device-specific name of
+ Signal Y. If possible, this should match the name of the
+ respective signal as it appears in the device datasheet
+ documentation text.
diff --git a/MAINTAINERS b/MAINTAINERS
index 2be01a95b7a5..2a7bf2f84272 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3672,6 +3672,7 @@ COUNTER SUBSYSTEM
M: William Breathitt Gray <[email protected]>
L: [email protected]
S: Maintained
+F: Documentation/ABI/testing/sysfs-bus-counter*
F: drivers/counter/
F: include/linux/counter.h
--
2.16.2
This patch introduces the Generic Counter interface for supporting
counter devices.
In the context of the Generic Counter interface, a counter is defined as
a device that reports one or more "counts" based on the state changes of
one or more "signals" as evaluated by a defined "count function."
Driver callbacks should be provided to communicate with the device: to
read and write various Signals and Counts, and to set and get the
"action mode" and "count function" for various Synapses and Counts
respectively.
To support a counter device, a driver must first allocate the available
Counter Signals via counter_signal structures. These Signals should
be stored as an array and set to the signals array member of an
allocated counter_device structure before the Counter is registered to
the system.
Counter Counts may be allocated via counter_count structures, and
respective Counter Signal associations (Synapses) made via
counter_synapse structures. Associated counter_synapse structures are
stored as an array and set to the the synapses array member of the
respective counter_count structure. These counter_count structures are
set to the counts array member of an allocated counter_device structure
before the Counter is registered to the system.
A counter device is registered to the system by passing the respective
initialized counter_device structure to the counter_register function;
similarly, the counter_unregister function unregisters the respective
Counter. The devm_counter_register and devm_counter_unregister functions
serve as device memory-managed versions of the counter_register and
counter_unregister functions respectively.
Signed-off-by: William Breathitt Gray <[email protected]>
---
MAINTAINERS | 7 +
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/{iio => }/counter/104-quad-8.c | 0
drivers/{iio => }/counter/Kconfig | 23 +-
drivers/{iio => }/counter/Makefile | 5 +-
drivers/counter/generic-counter.c | 1416 +++++++++++++++++++++++++
drivers/{iio => }/counter/stm32-lptimer-cnt.c | 0
drivers/iio/Kconfig | 1 -
drivers/iio/Makefile | 1 -
include/linux/counter.h | 524 +++++++++
11 files changed, 1973 insertions(+), 7 deletions(-)
rename drivers/{iio => }/counter/104-quad-8.c (100%)
rename drivers/{iio => }/counter/Kconfig (56%)
rename drivers/{iio => }/counter/Makefile (62%)
create mode 100644 drivers/counter/generic-counter.c
rename drivers/{iio => }/counter/stm32-lptimer-cnt.c (100%)
create mode 100644 include/linux/counter.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 885d20072d97..2be01a95b7a5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3668,6 +3668,13 @@ W: http://www.fi.muni.cz/~kas/cosa/
S: Maintained
F: drivers/net/wan/cosa*
+COUNTER SUBSYSTEM
+M: William Breathitt Gray <[email protected]>
+L: [email protected]
+S: Maintained
+F: drivers/counter/
+F: include/linux/counter.h
+
CPMAC ETHERNET DRIVER
M: Florian Fainelli <[email protected]>
L: [email protected]
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 879dc0604cba..21a67f49c17b 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -165,6 +165,8 @@ source "drivers/memory/Kconfig"
source "drivers/iio/Kconfig"
+source "drivers/counter/Kconfig"
+
source "drivers/ntb/Kconfig"
source "drivers/vme/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index 24cd47014657..5914c78688c3 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -165,6 +165,7 @@ obj-$(CONFIG_PM_DEVFREQ) += devfreq/
obj-$(CONFIG_EXTCON) += extcon/
obj-$(CONFIG_MEMORY) += memory/
obj-$(CONFIG_IIO) += iio/
+obj-$(CONFIG_COUNTER) += counter/
obj-$(CONFIG_VME_BUS) += vme/
obj-$(CONFIG_IPACK_BUS) += ipack/
obj-$(CONFIG_NTB) += ntb/
diff --git a/drivers/iio/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
similarity index 100%
rename from drivers/iio/counter/104-quad-8.c
rename to drivers/counter/104-quad-8.c
diff --git a/drivers/iio/counter/Kconfig b/drivers/counter/Kconfig
similarity index 56%
rename from drivers/iio/counter/Kconfig
rename to drivers/counter/Kconfig
index 474e1ac4e7c0..0b28d3ff524b 100644
--- a/drivers/iio/counter/Kconfig
+++ b/drivers/counter/Kconfig
@@ -3,11 +3,25 @@
#
# When adding new entries keep the list in alphabetical order
-menu "Counters"
+menuconfig COUNTER
+ tristate "Counter support"
+ help
+ Provides Generic Counter interface support for counter devices.
+
+ Counter devices are prevalent within a diverse spectrum of industries.
+ The ubiquitous presence of these devices necessitates a common
+ interface and standard of interaction and exposure. This driver API
+ attempts to resolve the issue of duplicate code found among existing
+ counter device drivers by providing a generic counter interface for
+ consumption. The Generic Counter interface enables drivers to support
+ and expose a common set of components and functionality present in
+ counter devices.
+
+if COUNTER
config 104_QUAD_8
tristate "ACCES 104-QUAD-8 driver"
- depends on PC104 && X86 && ISA_BUS_API
+ depends on PC104 && X86 && ISA_BUS_API && IIO
help
Say yes here to build support for the ACCES 104-QUAD-8 quadrature
encoder counter/interface device family (104-QUAD-8, 104-QUAD-4).
@@ -23,11 +37,12 @@ config 104_QUAD_8
config STM32_LPTIMER_CNT
tristate "STM32 LP Timer encoder counter driver"
- depends on MFD_STM32_LPTIMER || COMPILE_TEST
+ depends on (MFD_STM32_LPTIMER || COMPILE_TEST) && IIO
help
Select this option to enable STM32 Low-Power Timer quadrature encoder
and counter driver.
To compile this driver as a module, choose M here: the
module will be called stm32-lptimer-cnt.
-endmenu
+
+endif # COUNTER
diff --git a/drivers/iio/counter/Makefile b/drivers/counter/Makefile
similarity index 62%
rename from drivers/iio/counter/Makefile
rename to drivers/counter/Makefile
index 1b9a896eb488..d721a40aa4a2 100644
--- a/drivers/iio/counter/Makefile
+++ b/drivers/counter/Makefile
@@ -1,8 +1,11 @@
#
-# Makefile for IIO counter devices
+# Makefile for Counter devices
#
# When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_COUNTER) += counter.o
+counter-y := generic-counter.o
+
obj-$(CONFIG_104_QUAD_8) += 104-quad-8.o
obj-$(CONFIG_STM32_LPTIMER_CNT) += stm32-lptimer-cnt.o
diff --git a/drivers/counter/generic-counter.c b/drivers/counter/generic-counter.c
new file mode 100644
index 000000000000..03803356aac8
--- /dev/null
+++ b/drivers/counter/generic-counter.c
@@ -0,0 +1,1416 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Generic Counter interface
+ * Copyright (C) 2017 William Breathitt Gray
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/export.h>
+#include <linux/fs.h>
+#include <linux/gfp.h>
+#include <linux/idr.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/sysfs.h>
+#include <linux/types.h>
+
+#include <linux/counter.h>
+
+ssize_t counter_signal_enum_read(struct counter_device *counter,
+ struct counter_signal *signal, void *priv, char *buf)
+{
+ const struct counter_signal_enum_ext *const e = priv;
+ int err;
+ size_t index;
+
+ if (!e->get)
+ return -EINVAL;
+
+ err = e->get(counter, signal, &index);
+ if (err)
+ return err;
+
+ if (index >= e->num_items)
+ return -EINVAL;
+
+ return scnprintf(buf, PAGE_SIZE, "%s\n", e->items[index]);
+}
+EXPORT_SYMBOL(counter_signal_enum_read);
+
+ssize_t counter_signal_enum_write(struct counter_device *counter,
+ struct counter_signal *signal, void *priv, const char *buf, size_t len)
+{
+ const struct counter_signal_enum_ext *const e = priv;
+ ssize_t index;
+ int err;
+
+ if (!e->set)
+ return -EINVAL;
+
+ index = __sysfs_match_string(e->items, e->num_items, buf);
+ if (index < 0)
+ return index;
+
+ err = e->set(counter, signal, index);
+ if (err)
+ return err;
+
+ return len;
+}
+EXPORT_SYMBOL(counter_signal_enum_write);
+
+ssize_t counter_signal_enum_available_read(struct counter_device *counter,
+ struct counter_signal *signal, void *priv, char *buf)
+{
+ const struct counter_signal_enum_ext *const e = priv;
+ size_t i;
+ size_t len = 0;
+
+ if (!e->num_items)
+ return 0;
+
+ for (i = 0; i < e->num_items; i++)
+ len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
+ e->items[i]);
+
+ return len;
+}
+EXPORT_SYMBOL(counter_signal_enum_available_read);
+
+ssize_t counter_count_enum_read(struct counter_device *counter,
+ struct counter_count *count, void *priv, char *buf)
+{
+ const struct counter_count_enum_ext *const e = priv;
+ int err;
+ size_t index;
+
+ if (!e->get)
+ return -EINVAL;
+
+ err = e->get(counter, count, &index);
+ if (err)
+ return err;
+
+ if (index >= e->num_items)
+ return -EINVAL;
+
+ return scnprintf(buf, PAGE_SIZE, "%s\n", e->items[index]);
+}
+EXPORT_SYMBOL(counter_count_enum_read);
+
+ssize_t counter_count_enum_write(struct counter_device *counter,
+ struct counter_count *count, void *priv, const char *buf, size_t len)
+{
+ const struct counter_count_enum_ext *const e = priv;
+ ssize_t index;
+ int err;
+
+ if (!e->set)
+ return -EINVAL;
+
+ index = __sysfs_match_string(e->items, e->num_items, buf);
+ if (index < 0)
+ return index;
+
+ err = e->set(counter, count, index);
+ if (err)
+ return err;
+
+ return len;
+}
+EXPORT_SYMBOL(counter_count_enum_write);
+
+ssize_t counter_count_enum_available_read(struct counter_device *counter,
+ struct counter_count *count, void *priv, char *buf)
+{
+ const struct counter_count_enum_ext *const e = priv;
+ size_t i;
+ size_t len = 0;
+
+ if (!e->num_items)
+ return 0;
+
+ for (i = 0; i < e->num_items; i++)
+ len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
+ e->items[i]);
+
+ return len;
+}
+EXPORT_SYMBOL(counter_count_enum_available_read);
+
+ssize_t counter_device_enum_read(struct counter_device *counter, void *priv,
+ char *buf)
+{
+ const struct counter_device_enum_ext *const e = priv;
+ int err;
+ size_t index;
+
+ if (!e->get)
+ return -EINVAL;
+
+ err = e->get(counter, &index);
+ if (err)
+ return err;
+
+ if (index >= e->num_items)
+ return -EINVAL;
+
+ return scnprintf(buf, PAGE_SIZE, "%s\n", e->items[index]);
+}
+EXPORT_SYMBOL(counter_device_enum_read);
+
+ssize_t counter_device_enum_write(struct counter_device *counter, void *priv,
+ const char *buf, size_t len)
+{
+ const struct counter_device_enum_ext *const e = priv;
+ ssize_t index;
+ int err;
+
+ if (!e->set)
+ return -EINVAL;
+
+ index = __sysfs_match_string(e->items, e->num_items, buf);
+ if (index < 0)
+ return index;
+
+ err = e->set(counter, index);
+ if (err)
+ return err;
+
+ return len;
+}
+EXPORT_SYMBOL(counter_device_enum_write);
+
+ssize_t counter_device_enum_available_read(struct counter_device *counter,
+ void *priv, char *buf)
+{
+ const struct counter_device_enum_ext *const e = priv;
+ size_t i;
+ size_t len = 0;
+
+ if (!e->num_items)
+ return 0;
+
+ for (i = 0; i < e->num_items; i++)
+ len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
+ e->items[i]);
+
+ return len;
+}
+EXPORT_SYMBOL(counter_device_enum_available_read);
+
+static const char *const signal_level_str[] = {
+ [SIGNAL_LEVEL_LOW] = "low",
+ [SIGNAL_LEVEL_HIGH] = "high"
+};
+
+/**
+ * set_signal_read_value - set signal_read_value data
+ * @val: signal_read_value structure to set
+ * @type: property Signal data represents
+ * @data: Signal data
+ *
+ * This function sets an opaque signal_read_value structure with the provided
+ * Signal data.
+ */
+void set_signal_read_value(struct signal_read_value *const val,
+ const enum signal_value_type type, void *const data)
+{
+ if (type == SIGNAL_LEVEL)
+ val->len = scnprintf(val->buf, PAGE_SIZE, "%s\n",
+ signal_level_str[*(enum signal_level *)data]);
+ else
+ val->len = 0;
+}
+EXPORT_SYMBOL(set_signal_read_value);
+
+/**
+ * set_count_read_value - set count_read_value data
+ * @val: count_read_value structure to set
+ * @type: property Count data represents
+ * @data: Count data
+ *
+ * This function sets an opaque count_read_value structure with the provided
+ * Count data.
+ */
+void set_count_read_value(struct count_read_value *const val,
+ const enum count_value_type type, void *const data)
+{
+ switch (type) {
+ case COUNT_POSITION_UNSIGNED:
+ val->len = scnprintf(val->buf, PAGE_SIZE, "%lu\n",
+ *(unsigned long *)data);
+ break;
+ case COUNT_POSITION_SIGNED:
+ val->len = scnprintf(val->buf, PAGE_SIZE, "%ld\n",
+ *(long *)data);
+ break;
+ default:
+ val->len = 0;
+ }
+}
+EXPORT_SYMBOL(set_count_read_value);
+
+/**
+ * get_count_write_value - get count_write_value data
+ * @data: Count data
+ * @type: property Count data represents
+ * @val: count_write_value structure containing data
+ *
+ * This function extracts Count data from the provided opaque count_write_value
+ * structure and stores it at the address provided by @data.
+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */
+int get_count_write_value(void *const data,
+ const enum count_value_type type,
+ const struct count_write_value *const val)
+{
+ int err;
+
+ switch (type) {
+ case COUNT_POSITION_UNSIGNED:
+ err = kstrtoul(val->buf, 0, data);
+ if (err)
+ return err;
+ break;
+ case COUNT_POSITION_SIGNED:
+ err = kstrtol(val->buf, 0, data);
+ if (err)
+ return err;
+ break;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL(get_count_write_value);
+
+struct counter_device_attr {
+ struct device_attribute dev_attr;
+ struct list_head l;
+ void *component;
+};
+
+static int counter_attribute_create(
+ struct counter_device_attr_group *const group,
+ const char *const prefix,
+ const char *const name,
+ ssize_t (*show)(struct device *dev, struct device_attribute *attr,
+ char *buf),
+ ssize_t (*store)(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t len),
+ void *const component)
+{
+ struct counter_device_attr *counter_attr;
+ struct device_attribute *dev_attr;
+ int err;
+ struct list_head *const attr_list = &group->attr_list;
+
+ /* Allocate a Counter device attribute */
+ counter_attr = kzalloc(sizeof(*counter_attr), GFP_KERNEL);
+ if (!counter_attr)
+ return -ENOMEM;
+ dev_attr = &counter_attr->dev_attr;
+
+ sysfs_attr_init(&dev_attr->attr);
+
+ /* Configure device attribute */
+ dev_attr->attr.name = kasprintf(GFP_KERNEL, "%s%s", prefix, name);
+ if (!dev_attr->attr.name) {
+ err = -ENOMEM;
+ goto err_free_counter_attr;
+ }
+ if (show) {
+ dev_attr->attr.mode |= 0444;
+ dev_attr->show = show;
+ }
+ if (store) {
+ dev_attr->attr.mode |= 0200;
+ dev_attr->store = store;
+ }
+
+ /* Store associated Counter component with attribute */
+ counter_attr->component = component;
+
+ /* Keep track of the attribute for later cleanup */
+ list_add(&counter_attr->l, attr_list);
+ group->num_attr++;
+
+ return 0;
+
+err_free_counter_attr:
+ kfree(counter_attr);
+ return err;
+}
+
+#define to_counter_attr(_dev_attr) \
+ container_of(_dev_attr, struct counter_device_attr, dev_attr)
+
+struct signal_comp_t {
+ struct counter_signal *signal;
+};
+
+static ssize_t counter_signal_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct signal_comp_t *const component = devattr->component;
+ struct counter_signal *const signal = component->signal;
+ int err;
+ struct signal_read_value val = { .buf = buf };
+
+ err = counter->signal_read(counter, signal, &val);
+ if (err)
+ return err;
+
+ return val.len;
+}
+
+struct name_comp_t {
+ const char *name;
+};
+
+static ssize_t counter_device_attr_name_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ const struct name_comp_t *const comp = to_counter_attr(attr)->component;
+
+ return scnprintf(buf, PAGE_SIZE, "%s\n", comp->name);
+}
+
+struct signal_ext_comp_t {
+ struct counter_signal *signal;
+ const struct counter_signal_ext *ext;
+};
+
+static ssize_t counter_signal_ext_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct signal_ext_comp_t *const component = devattr->component;
+ const struct counter_signal_ext *const ext = component->ext;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ struct counter_signal *const signal = component->signal;
+
+ return ext->read(counter, signal, ext->priv, buf);
+}
+
+static ssize_t counter_signal_ext_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t len)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct signal_ext_comp_t *const component = devattr->component;
+ const struct counter_signal_ext *const ext = component->ext;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ struct counter_signal *const signal = component->signal;
+
+ return ext->write(counter, signal, ext->priv, buf, len);
+}
+
+static int counter_signal_ext_register(
+ struct counter_device_attr_group *const group,
+ struct counter_signal *const signal)
+{
+ const size_t num_ext = signal->num_ext;
+ size_t i;
+ const struct counter_signal_ext *ext;
+ struct signal_ext_comp_t *signal_ext_comp;
+ int err;
+
+ /* Return early if no extensions */
+ if (!signal->ext || !num_ext)
+ return 0;
+
+ /* Create an attribute for each extension */
+ for (i = 0 ; i < num_ext; i++) {
+ ext = signal->ext + i;
+
+ /* Allocate signal_ext attribute component */
+ signal_ext_comp = kmalloc(sizeof(*signal_ext_comp), GFP_KERNEL);
+ if (!signal_ext_comp)
+ return -ENOMEM;
+ signal_ext_comp->signal = signal;
+ signal_ext_comp->ext = ext;
+
+ /* Allocate a Counter device attribute */
+ err = counter_attribute_create(group, "", ext->name,
+ (ext->read) ? counter_signal_ext_show : NULL,
+ (ext->write) ? counter_signal_ext_store : NULL,
+ signal_ext_comp);
+ if (err) {
+ kfree(signal_ext_comp);
+ return err;
+ }
+ }
+
+ return 0;
+}
+
+static int counter_signal_attributes_create(
+ struct counter_device_attr_group *const group,
+ const struct counter_device *const counter,
+ struct counter_signal *const signal)
+{
+ struct signal_comp_t *signal_comp;
+ int err;
+ struct name_comp_t *name_comp;
+
+ /* Allocate Signal attribute component */
+ signal_comp = kmalloc(sizeof(*signal_comp), GFP_KERNEL);
+ if (!signal_comp)
+ return -ENOMEM;
+ signal_comp->signal = signal;
+
+ /* Create main Signal attribute */
+ err = counter_attribute_create(group, "", "signal",
+ (counter->signal_read) ? counter_signal_show : NULL, NULL,
+ signal_comp);
+ if (err) {
+ kfree(signal_comp);
+ return err;
+ }
+
+ /* Create Signal name attribute */
+ if (signal->name) {
+ /* Allocate name attribute component */
+ name_comp = kmalloc(sizeof(*name_comp), GFP_KERNEL);
+ if (!name_comp)
+ return -ENOMEM;
+ name_comp->name = signal->name;
+
+ /* Allocate Signal name attribute */
+ err = counter_attribute_create(group, "", "name",
+ counter_device_attr_name_show, NULL, name_comp);
+ if (err) {
+ kfree(name_comp);
+ return err;
+ }
+ }
+
+ /* Register Signal extension attributes */
+ return counter_signal_ext_register(group, signal);
+}
+
+static int counter_signals_register(
+ struct counter_device_attr_group *const groups_list,
+ const struct counter_device *const counter)
+{
+ const size_t num_signals = counter->num_signals;
+ struct counter_device_state *const device_state = counter->device_state;
+ struct device *const dev = &device_state->dev;
+ size_t i;
+ struct counter_signal *signal;
+ const char *name;
+ int err;
+
+ /* At least one Signal must be defined */
+ if (!counter->signals || !num_signals) {
+ dev_err(dev, "Signals undefined\n");
+ return -EINVAL;
+ }
+
+ /* Register each Signal */
+ for (i = 0; i < num_signals; i++) {
+ signal = counter->signals + i;
+
+ /* Generate Signal attribute directory name */
+ name = kasprintf(GFP_KERNEL, "signal%d", signal->id);
+ if (!name)
+ return -ENOMEM;
+
+ groups_list[i].attr_group.name = name;
+
+ /* Create all attributes associated with Signal */
+ err = counter_signal_attributes_create(groups_list + i, counter,
+ signal);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
+static const char *const synapse_action_str[] = {
+ [SYNAPSE_ACTION_NONE] = "none",
+ [SYNAPSE_ACTION_RISING_EDGE] = "rising edge",
+ [SYNAPSE_ACTION_FALLING_EDGE] = "falling edge",
+ [SYNAPSE_ACTION_BOTH_EDGES] = "both edges"
+};
+
+struct action_comp_t {
+ struct counter_synapse *synapse;
+ struct counter_count *count;
+};
+
+static ssize_t counter_action_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ int err;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ const struct action_comp_t *const component = devattr->component;
+ struct counter_count *const count = component->count;
+ struct counter_synapse *const synapse = component->synapse;
+ size_t action_index;
+ enum synapse_action action;
+
+ err = counter->action_get(counter, count, synapse, &action_index);
+ if (err)
+ return err;
+
+ synapse->action = action_index;
+
+ action = synapse->actions_list[action_index];
+ return scnprintf(buf, PAGE_SIZE, "%s\n", synapse_action_str[action]);
+}
+
+static ssize_t counter_action_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t len)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct action_comp_t *const component = devattr->component;
+ struct counter_synapse *const synapse = component->synapse;
+ size_t action_index;
+ const size_t num_actions = synapse->num_actions;
+ enum synapse_action action;
+ int err;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ struct counter_count *const count = component->count;
+
+ /* Find requested action mode */
+ for (action_index = 0; action_index < num_actions; action_index++) {
+ action = synapse->actions_list[action_index];
+ if (sysfs_streq(buf, synapse_action_str[action]))
+ break;
+ }
+ /* If requested action mode not found */
+ if (action_index >= num_actions)
+ return -EINVAL;
+
+ err = counter->action_set(counter, count, synapse, action_index);
+ if (err)
+ return err;
+
+ synapse->action = action_index;
+
+ return len;
+}
+
+struct action_avail_comp_t {
+ const enum synapse_action *actions_list;
+ size_t num_actions;
+};
+
+static ssize_t counter_synapse_action_available_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct action_avail_comp_t *const component = devattr->component;
+ const enum synapse_action *const actions_list = component->actions_list;
+ const size_t num_actions = component->num_actions;
+ size_t i;
+ enum synapse_action action;
+ ssize_t len = 0;
+
+ for (i = 0; i < num_actions; i++) {
+ action = actions_list[i];
+ len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
+ synapse_action_str[action]);
+ }
+
+ return len;
+}
+
+static int counter_synapses_register(
+ struct counter_device_attr_group *const group,
+ const struct counter_device *const counter,
+ struct counter_count *const count, const char *const count_attr_name)
+{
+ const size_t num_synapses = count->num_synapses;
+ struct device *const dev = &counter->device_state->dev;
+ size_t i;
+ struct counter_synapse *synapse;
+ const char *prefix;
+ struct action_comp_t *action_comp;
+ int err;
+ struct action_avail_comp_t *avail_comp;
+
+ /* At least one Synapse must be defined */
+ if (!count->synapses || !num_synapses) {
+ dev_err(dev, "Count '%d' Synapses undefined\n", count->id);
+ return -EINVAL;
+ }
+
+ /* Register each Synapse */
+ for (i = 0; i < num_synapses; i++) {
+ synapse = count->synapses + i;
+
+ /* Ensure all Synapses have a defined Signal */
+ if (!synapse->signal) {
+ dev_err(dev,
+ "Count '%d' Synapse '%zu' Signal undefined\n",
+ count->id, i);
+ return -EINVAL;
+ }
+
+ /* At least one action mode must be defined for each Synapse */
+ if (!synapse->actions_list || !synapse->num_actions) {
+ dev_err(dev,
+ "Count '%d' Signal '%d' action modes undefined\n",
+ count->id, synapse->signal->id);
+ return -EINVAL;
+ }
+
+ /* Generate attribute prefix */
+ prefix = kasprintf(GFP_KERNEL, "signal%d_",
+ synapse->signal->id);
+ if (!prefix)
+ return -ENOMEM;
+
+ /* Allocate action attribute component */
+ action_comp = kmalloc(sizeof(*action_comp), GFP_KERNEL);
+ if (!action_comp) {
+ err = -ENOMEM;
+ goto err_free_prefix;
+ }
+ action_comp->synapse = synapse;
+ action_comp->count = count;
+
+ /* Create action attribute */
+ err = counter_attribute_create(group, prefix, "action",
+ (counter->action_get) ? counter_action_show : NULL,
+ (counter->action_set) ? counter_action_store : NULL,
+ action_comp);
+ if (err) {
+ kfree(action_comp);
+ goto err_free_prefix;
+ }
+
+ /* Allocate action available attribute component */
+ avail_comp = kmalloc(sizeof(*avail_comp), GFP_KERNEL);
+ if (!avail_comp) {
+ err = -ENOMEM;
+ goto err_free_prefix;
+ }
+ avail_comp->actions_list = synapse->actions_list;
+ avail_comp->num_actions = synapse->num_actions;
+
+ /* Create action_available attribute */
+ err = counter_attribute_create(group, prefix,
+ "action_available",
+ counter_synapse_action_available_show, NULL,
+ avail_comp);
+ if (err) {
+ kfree(avail_comp);
+ goto err_free_prefix;
+ }
+
+ kfree(prefix);
+ }
+
+ return 0;
+
+err_free_prefix:
+ kfree(prefix);
+ return err;
+}
+
+struct count_comp_t {
+ struct counter_count *count;
+};
+
+static ssize_t counter_count_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct count_comp_t *const component = devattr->component;
+ struct counter_count *const count = component->count;
+ int err;
+ struct count_read_value val = { .buf = buf };
+
+ err = counter->count_read(counter, count, &val);
+ if (err)
+ return err;
+
+ return val.len;
+}
+
+static ssize_t counter_count_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t len)
+{
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct count_comp_t *const component = devattr->component;
+ struct counter_count *const count = component->count;
+ int err;
+ struct count_write_value val = { .buf = buf };
+
+ err = counter->count_write(counter, count, &val);
+ if (err)
+ return err;
+
+ return len;
+}
+
+static const char *const count_function_str[] = {
+ [COUNT_FUNCTION_INCREASE] = "increase",
+ [COUNT_FUNCTION_DECREASE] = "decrease",
+ [COUNT_FUNCTION_PULSE_DIRECTION] = "pulse-direction",
+ [COUNT_FUNCTION_QUADRATURE_X1] = "quadrature x1",
+ [COUNT_FUNCTION_QUADRATURE_X2] = "quadrature x2",
+ [COUNT_FUNCTION_QUADRATURE_X4] = "quadrature x4"
+};
+
+static ssize_t counter_function_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ int err;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct count_comp_t *const component = devattr->component;
+ struct counter_count *const count = component->count;
+ size_t func_index;
+ enum count_function function;
+
+ err = counter->function_get(counter, count, &func_index);
+ if (err)
+ return err;
+
+ count->function = func_index;
+
+ function = count->functions_list[func_index];
+ return scnprintf(buf, PAGE_SIZE, "%s\n", count_function_str[function]);
+}
+
+static ssize_t counter_function_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t len)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct count_comp_t *const component = devattr->component;
+ struct counter_count *const count = component->count;
+ const size_t num_functions = count->num_functions;
+ size_t func_index;
+ enum count_function function;
+ int err;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+
+ /* Find requested Count function mode */
+ for (func_index = 0; func_index < num_functions; func_index++) {
+ function = count->functions_list[func_index];
+ if (sysfs_streq(buf, count_function_str[function]))
+ break;
+ }
+ /* Return error if requested Count function mode not found */
+ if (func_index >= num_functions)
+ return -EINVAL;
+
+ err = counter->function_set(counter, count, func_index);
+ if (err)
+ return err;
+
+ count->function = func_index;
+
+ return len;
+}
+
+struct count_ext_comp_t {
+ struct counter_count *count;
+ const struct counter_count_ext *ext;
+};
+
+static ssize_t counter_count_ext_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct count_ext_comp_t *const comp = devattr->component;
+ const struct counter_count_ext *const ext = comp->ext;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ struct counter_count *const count = comp->count;
+
+ return ext->read(counter, count, ext->priv, buf);
+}
+
+static ssize_t counter_count_ext_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t len)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct count_ext_comp_t *const comp = devattr->component;
+ const struct counter_count_ext *const ext = comp->ext;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ struct counter_count *const count = comp->count;
+
+ return ext->write(counter, count, ext->priv, buf, len);
+}
+
+static int counter_count_ext_register(
+ struct counter_device_attr_group *const group,
+ struct counter_count *const count)
+{
+ const size_t num_ext = count->num_ext;
+ size_t i;
+ const struct counter_count_ext *ext;
+ struct count_ext_comp_t *count_ext_comp;
+ int err;
+
+ /* Return early if no extensions */
+ if (!count->ext || !num_ext)
+ return 0;
+
+ /* Create an attribute for each extension */
+ for (i = 0 ; i < num_ext; i++) {
+ ext = count->ext + i;
+
+ /* Allocate count_ext attribute component */
+ count_ext_comp = kmalloc(sizeof(*count_ext_comp), GFP_KERNEL);
+ if (!count_ext_comp)
+ return -ENOMEM;
+ count_ext_comp->count = count;
+ count_ext_comp->ext = ext;
+
+ /* Allocate count_ext attribute */
+ err = counter_attribute_create(group, "", ext->name,
+ (ext->read) ? counter_count_ext_show : NULL,
+ (ext->write) ? counter_count_ext_store : NULL,
+ count_ext_comp);
+ if (err) {
+ kfree(count_ext_comp);
+ return err;
+ }
+ }
+
+ return 0;
+}
+
+struct func_avail_comp_t {
+ const enum count_function *functions_list;
+ size_t num_functions;
+};
+
+static ssize_t counter_count_function_available_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct func_avail_comp_t *const component = devattr->component;
+ const enum count_function *const func_list = component->functions_list;
+ const size_t num_functions = component->num_functions;
+ size_t i;
+ enum count_function function;
+ ssize_t len = 0;
+
+ for (i = 0; i < num_functions; i++) {
+ function = func_list[i];
+ len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
+ count_function_str[function]);
+ }
+
+ return len;
+}
+
+static int counter_count_attributes_create(
+ struct counter_device_attr_group *const group,
+ const struct counter_device *const counter,
+ struct counter_count *const count)
+{
+ struct count_comp_t *count_comp;
+ int err;
+ struct count_comp_t *func_comp;
+ struct func_avail_comp_t *avail_comp;
+ struct name_comp_t *name_comp;
+
+ /* Allocate count attribute component */
+ count_comp = kmalloc(sizeof(*count_comp), GFP_KERNEL);
+ if (!count_comp)
+ return -ENOMEM;
+ count_comp->count = count;
+
+ /* Create main Count attribute */
+ err = counter_attribute_create(group, "", "count",
+ (counter->count_read) ? counter_count_show : NULL,
+ (counter->count_write) ? counter_count_store : NULL,
+ count_comp);
+ if (err) {
+ kfree(count_comp);
+ return err;
+ }
+
+ /* Allocate function attribute component */
+ func_comp = kmalloc(sizeof(*func_comp), GFP_KERNEL);
+ if (!func_comp)
+ return -ENOMEM;
+ func_comp->count = count;
+
+ /* Create Count function attribute */
+ err = counter_attribute_create(group, "", "function",
+ (counter->function_get) ? counter_function_show : NULL,
+ (counter->function_set) ? counter_function_store : NULL,
+ func_comp);
+ if (err) {
+ kfree(func_comp);
+ return err;
+ }
+
+ /* Allocate function available attribute component */
+ avail_comp = kmalloc(sizeof(*avail_comp), GFP_KERNEL);
+ if (!avail_comp)
+ return -ENOMEM;
+ avail_comp->functions_list = count->functions_list;
+ avail_comp->num_functions = count->num_functions;
+
+ /* Create Count function_available attribute */
+ err = counter_attribute_create(group, "", "function_available",
+ counter_count_function_available_show, NULL, avail_comp);
+ if (err) {
+ kfree(avail_comp);
+ return err;
+ }
+
+ /* Create Count name attribute */
+ if (count->name) {
+ /* Allocate name attribute component */
+ name_comp = kmalloc(sizeof(*name_comp), GFP_KERNEL);
+ if (!name_comp)
+ return -ENOMEM;
+ name_comp->name = count->name;
+
+ err = counter_attribute_create(group, "", "name",
+ counter_device_attr_name_show, NULL, name_comp);
+ if (err) {
+ kfree(name_comp);
+ return err;
+ }
+ }
+
+ /* Register Count extension attributes */
+ return counter_count_ext_register(group, count);
+}
+
+static int counter_counts_register(
+ struct counter_device_attr_group *const groups_list,
+ const struct counter_device *const counter)
+{
+ const size_t num_counts = counter->num_counts;
+ struct device *const dev = &counter->device_state->dev;
+ size_t i;
+ struct counter_count *count;
+ const char *name;
+ int err;
+
+ /* At least one Count must be defined */
+ if (!counter->counts || !num_counts) {
+ dev_err(dev, "Counts undefined\n");
+ return -EINVAL;
+ }
+
+ /* Register each Count */
+ for (i = 0; i < num_counts; i++) {
+ count = counter->counts + i;
+
+ /* At least one function mode must be defined for each Count */
+ if (!count->functions_list || !count->num_functions) {
+ dev_err(dev, "Count '%d' function modes undefined\n",
+ count->id);
+ return -EINVAL;
+ }
+
+ /* Generate Count attribute directory name */
+ name = kasprintf(GFP_KERNEL, "count%d", count->id);
+ if (!name)
+ return -ENOMEM;
+ groups_list[i].attr_group.name = name;
+
+ /* Register the Synapses associated with each Count */
+ err = counter_synapses_register(groups_list + i, counter, count,
+ name);
+ if (err)
+ return err;
+
+ /* Create all attributes associated with Count */
+ err = counter_count_attributes_create(groups_list + i, counter,
+ count);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
+static struct bus_type counter_bus_type = {
+ .name = "counter"
+};
+
+static int __init counter_init(void)
+{
+ return bus_register(&counter_bus_type);
+}
+
+static void __exit counter_exit(void)
+{
+ bus_unregister(&counter_bus_type);
+}
+
+static void free_counter_device_attr_list(struct list_head *attr_list)
+{
+ struct counter_device_attr *p, *n;
+
+ list_for_each_entry_safe(p, n, attr_list, l) {
+ kfree(p->dev_attr.attr.name);
+ kfree(p->component);
+ list_del(&p->l);
+ kfree(p);
+ }
+}
+
+static void free_counter_device_groups_list(
+ struct counter_device_state *const device_state)
+{
+ struct counter_device_attr_group *group;
+ size_t i;
+
+ for (i = 0; i < device_state->num_groups; i++) {
+ group = device_state->groups_list + i;
+
+ kfree(group->attr_group.name);
+ kfree(group->attr_group.attrs);
+ free_counter_device_attr_list(&group->attr_list);
+ }
+
+ kfree(device_state->groups_list);
+}
+
+/* Provides a unique ID for each counter device */
+static DEFINE_IDA(counter_ida);
+
+static void counter_device_release(struct device *dev)
+{
+ struct counter_device *const counter = dev_get_drvdata(dev);
+ struct counter_device_state *const device_state = counter->device_state;
+
+ kfree(device_state->groups);
+ free_counter_device_groups_list(device_state);
+ ida_simple_remove(&counter_ida, device_state->id);
+ kfree(device_state);
+}
+
+static struct device_type counter_device_type = {
+ .name = "counter_device",
+ .release = counter_device_release
+};
+
+struct ext_comp_t {
+ const struct counter_device_ext *ext;
+};
+
+static ssize_t counter_device_ext_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct ext_comp_t *const component = devattr->component;
+ const struct counter_device_ext *const ext = component->ext;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+
+ return ext->read(counter, ext->priv, buf);
+}
+
+static ssize_t counter_device_ext_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t len)
+{
+ const struct counter_device_attr *const devattr = to_counter_attr(attr);
+ const struct ext_comp_t *const component = devattr->component;
+ const struct counter_device_ext *const ext = component->ext;
+ struct counter_device *const counter = dev_get_drvdata(dev);
+
+ return ext->write(counter, ext->priv, buf, len);
+}
+
+static int counter_device_ext_register(
+ struct counter_device_attr_group *const group,
+ struct counter_device *const counter)
+{
+ const size_t num_ext = counter->num_ext;
+ struct ext_comp_t *ext_comp;
+ size_t i;
+ const struct counter_device_ext *ext;
+ int err;
+
+ /* Return early if no extensions */
+ if (!counter->ext || !num_ext)
+ return 0;
+
+ /* Create an attribute for each extension */
+ for (i = 0 ; i < num_ext; i++) {
+ ext = counter->ext + i;
+
+ /* Allocate extension attribute component */
+ ext_comp = kmalloc(sizeof(*ext_comp), GFP_KERNEL);
+ if (!ext_comp)
+ return -ENOMEM;
+ ext_comp->ext = ext;
+
+ /* Allocate extension attribute */
+ err = counter_attribute_create(group, "", ext->name,
+ (ext->read) ? counter_device_ext_show : NULL,
+ (ext->write) ? counter_device_ext_store : NULL,
+ ext_comp);
+ if (err) {
+ kfree(ext_comp);
+ return err;
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * counter_register - register Counter to the system
+ * @counter: pointer to Counter to register
+ *
+ * This function registers a Counter to the system. A sysfs "counter" directory
+ * will be created and populated with sysfs attributes correlating with the
+ * Counter Signals, Synapses, and Counts respectively.
+ */
+int counter_register(struct counter_device *const counter)
+{
+ struct counter_device_state *device_state;
+ int err;
+ size_t i = 0;
+ size_t groups_offset = 0;
+ struct name_comp_t *name_comp;
+ struct counter_device_attr_group *group;
+ size_t j;
+ struct counter_device_attr *p;
+
+ if (!counter)
+ return -EINVAL;
+
+ /* Allocate internal state container for Counter device */
+ device_state = kzalloc(sizeof(*device_state), GFP_KERNEL);
+ if (!device_state)
+ return -ENOMEM;
+ counter->device_state = device_state;
+
+ /* Acquire unique ID */
+ device_state->id = ida_simple_get(&counter_ida, 0, 0, GFP_KERNEL);
+ if (device_state->id < 0) {
+ err = device_state->id;
+ goto err_free_device_state;
+ }
+
+ /* Configure device structure for Counter */
+ device_state->dev.type = &counter_device_type;
+ device_state->dev.bus = &counter_bus_type;
+ if (counter->parent) {
+ device_state->dev.parent = counter->parent;
+ device_state->dev.of_node = counter->parent->of_node;
+ }
+ dev_set_name(&device_state->dev, "counter%d", device_state->id);
+ device_initialize(&device_state->dev);
+ dev_set_drvdata(&device_state->dev, counter);
+
+ /* Allocate space for attribute groups (signals. counts, and ext) */
+ device_state->num_groups =
+ counter->num_signals + counter->num_counts + 1;
+ device_state->groups_list = kcalloc(device_state->num_groups,
+ sizeof(*device_state->groups_list), GFP_KERNEL);
+ if (!device_state->groups_list) {
+ err = -ENOMEM;
+ goto err_free_id;
+ }
+
+ /* Initialize attribute lists */
+ for (i = 0; i < device_state->num_groups; i++)
+ INIT_LIST_HEAD(&device_state->groups_list[i].attr_list);
+
+ /* Verify Signals are valid and register */
+ err = counter_signals_register(device_state->groups_list, counter);
+ if (err)
+ goto err_free_groups_list;
+ groups_offset += counter->num_signals;
+
+ /* Verify Counts and respective Synapses are valid and register */
+ err = counter_counts_register(device_state->groups_list + groups_offset,
+ counter);
+ if (err)
+ goto err_free_groups_list;
+ groups_offset += counter->num_counts;
+
+ /* Register Counter device extension attributes */
+ err = counter_device_ext_register(
+ device_state->groups_list + groups_offset, counter);
+ if (err)
+ goto err_free_groups_list;
+
+ /* Account for name attribute */
+ if (counter->name) {
+ /* Allocate name attribute component */
+ name_comp = kmalloc(sizeof(*name_comp), GFP_KERNEL);
+ if (!name_comp) {
+ err = -ENOMEM;
+ goto err_free_groups_list;
+ }
+ name_comp->name = counter->name;
+
+ err = counter_attribute_create(
+ device_state->groups_list + groups_offset, "", "name",
+ counter_device_attr_name_show, NULL, name_comp);
+ if (err) {
+ kfree(name_comp);
+ goto err_free_groups_list;
+ }
+ }
+
+ /* Allocate attribute groups for association with device */
+ device_state->groups = kcalloc(device_state->num_groups + 1,
+ sizeof(*device_state->groups), GFP_KERNEL);
+ if (!device_state->groups) {
+ err = -ENOMEM;
+ goto err_free_groups_list;
+ }
+ /* Prepare each group of attributes for association */
+ for (i = 0; i < device_state->num_groups; i++) {
+ group = device_state->groups_list + i;
+
+ /* Allocate space for attribute pointers in attribute group */
+ group->attr_group.attrs = kcalloc(group->num_attr + 1,
+ sizeof(*group->attr_group.attrs), GFP_KERNEL);
+ if (!group->attr_group.attrs) {
+ err = -ENOMEM;
+ goto err_free_groups;
+ }
+
+ /* Add attribute pointers to attribute group */
+ j = 0;
+ list_for_each_entry(p, &group->attr_list, l)
+ group->attr_group.attrs[j++] = &p->dev_attr.attr;
+
+ /* Group attributes in attribute group */
+ device_state->groups[i] = &group->attr_group;
+ }
+ /* Associate attributes with device */
+ device_state->dev.groups = device_state->groups;
+
+ /* Add device to system */
+ err = device_add(&device_state->dev);
+ if (err)
+ goto err_free_groups;
+
+ return 0;
+
+err_free_groups:
+ kfree(counter->device_state->groups);
+err_free_groups_list:
+ free_counter_device_groups_list(counter->device_state);
+err_free_id:
+ ida_simple_remove(&counter_ida, counter->device_state->id);
+err_free_device_state:
+ kfree(counter->device_state);
+ return err;
+}
+EXPORT_SYMBOL(counter_register);
+
+/**
+ * counter_unregister - unregister Counter from the system
+ * @counter: pointer to Counter to unregister
+ *
+ * The Counter is unregistered from the system; all allocated memory is freed.
+ */
+void counter_unregister(struct counter_device *const counter)
+{
+ if (counter)
+ device_del(&counter->device_state->dev);
+}
+EXPORT_SYMBOL(counter_unregister);
+
+static void devm_counter_unreg(struct device *dev, void *res)
+{
+ counter_unregister(*(struct counter_device **)res);
+}
+
+/**
+ * devm_counter_register - Resource-managed counter_register
+ * @dev: device to allocate counter_device for
+ * @counter: pointer to Counter to register
+ *
+ * Managed counter_register. The Counter registered with this function is
+ * automatically unregistered on driver detach. This function calls
+ * counter_register internally. Refer to that function for more information.
+ *
+ * If an Counter registered with this function needs to be unregistered
+ * separately, devm_counter_unregister must be used.
+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */
+int devm_counter_register(struct device *dev,
+ struct counter_device *const counter)
+{
+ struct counter_device **ptr;
+ int ret;
+
+ ptr = devres_alloc(devm_counter_unreg, sizeof(*ptr), GFP_KERNEL);
+ if (!ptr)
+ return -ENOMEM;
+
+ ret = counter_register(counter);
+ if (!ret) {
+ *ptr = counter;
+ devres_add(dev, ptr);
+ } else
+ devres_free(ptr);
+
+ return ret;
+}
+EXPORT_SYMBOL(devm_counter_register);
+
+static int devm_counter_match(struct device *dev, void *res, void *data)
+{
+ struct counter_device **r = res;
+
+ if (!r || !*r) {
+ WARN_ON(!r || !*r);
+ return 0;
+ }
+
+ return *r == data;
+}
+
+/**
+ * devm_counter_unregister - Resource-managed counter_unregister
+ * @dev: device this counter_device belongs to
+ * @counter: pointer to Counter associated with the device
+ *
+ * Unregister Counter registered with devm_counter_register.
+ */
+void devm_counter_unregister(struct device *dev,
+ struct counter_device *const counter)
+{
+ int rc;
+
+ rc = devres_release(dev, devm_counter_unreg,
+ devm_counter_match, counter);
+ WARN_ON(rc);
+}
+EXPORT_SYMBOL(devm_counter_unregister);
+
+subsys_initcall(counter_init);
+module_exit(counter_exit);
+
+MODULE_AUTHOR("William Breathitt Gray <[email protected]>");
+MODULE_DESCRIPTION("Generic Counter interface");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/counter/stm32-lptimer-cnt.c b/drivers/counter/stm32-lptimer-cnt.c
similarity index 100%
rename from drivers/iio/counter/stm32-lptimer-cnt.c
rename to drivers/counter/stm32-lptimer-cnt.c
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index b3c8c6ef0dff..423d86e368ea 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -73,7 +73,6 @@ source "drivers/iio/adc/Kconfig"
source "drivers/iio/amplifiers/Kconfig"
source "drivers/iio/chemical/Kconfig"
source "drivers/iio/common/Kconfig"
-source "drivers/iio/counter/Kconfig"
source "drivers/iio/dac/Kconfig"
source "drivers/iio/dummy/Kconfig"
source "drivers/iio/frequency/Kconfig"
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index b16b2e9ddc40..6a80ebddb54c 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -19,7 +19,6 @@ obj-y += amplifiers/
obj-y += buffer/
obj-y += chemical/
obj-y += common/
-obj-y += counter/
obj-y += dac/
obj-y += dummy/
obj-y += gyro/
diff --git a/include/linux/counter.h b/include/linux/counter.h
new file mode 100644
index 000000000000..aefca382624e
--- /dev/null
+++ b/include/linux/counter.h
@@ -0,0 +1,524 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Counter interface
+ * Copyright (C) 2017 William Breathitt Gray
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+#ifndef _COUNTER_H_
+#define _COUNTER_H_
+
+#include <linux/device.h>
+#include <linux/types.h>
+
+struct counter_device;
+struct counter_signal;
+
+/**
+ * struct counter_signal_ext - Counter Signal extensions
+ * @name: attribute name
+ * @read: read callback for this attribute; may be NULL
+ * @write: write callback for this attribute; may be NULL
+ * @priv: data private to the driver
+ */
+struct counter_signal_ext {
+ const char *name;
+ ssize_t (*read)(struct counter_device *counter,
+ struct counter_signal *signal, void *priv,
+ char *buf);
+ ssize_t (*write)(struct counter_device *counter,
+ struct counter_signal *signal, void *priv,
+ const char *buf, size_t len);
+ void *priv;
+};
+
+/**
+ * struct counter_signal - Counter Signal node
+ * @id: unique ID used to identify signal
+ * @name: device-specific Signal name; ideally, this should match the name
+ * as it appears in the datasheet documentation
+ * @ext: optional array of Counter Signal extensions
+ * @num_ext: number of Counter Signal extensions specified in @ext
+ * @priv: optional private data supplied by driver
+ */
+struct counter_signal {
+ int id;
+ const char *name;
+
+ const struct counter_signal_ext *ext;
+ size_t num_ext;
+
+ void *priv;
+};
+
+/**
+ * struct counter_signal_enum_ext - Signal enum extension attribute
+ * @items: Array of strings
+ * @num_items: Number of items specified in @items
+ * @set: Set callback function; may be NULL
+ * @get: Get callback function; may be NULL
+ *
+ * The counter_signal_enum_ext structure can be used to implement enum style
+ * Signal extension attributes. Enum style attributes are those which have a set
+ * of strings that map to unsigned integer values. The Generic Counter Signal
+ * enum extension helper code takes care of mapping between value and string, as
+ * well as generating a "_available" file which contains a list of all available
+ * items. The get callback is used to query the currently active item; the index
+ * of the item within the respective items array is returned via the 'item'
+ * parameter. The set callback is called when the attribute is updated; the
+ * 'item' parameter contains the index of the newly activated item within the
+ * respective items array.
+ */
+struct counter_signal_enum_ext {
+ const char * const *items;
+ size_t num_items;
+ int (*get)(struct counter_device *counter,
+ struct counter_signal *signal,
+ size_t *item);
+ int (*set)(struct counter_device *counter,
+ struct counter_signal *signal,
+ size_t item);
+};
+
+extern ssize_t counter_signal_enum_read(struct counter_device *counter,
+ struct counter_signal *signal, void *priv, char *buf);
+extern ssize_t counter_signal_enum_write(struct counter_device *counter,
+ struct counter_signal *signal, void *priv, const char *buf, size_t len);
+
+/**
+ * COUNTER_SIGNAL_ENUM() - Initialize Signal enum extension
+ * @_name: Attribute name
+ * @_e: Pointer to a counter_count_enum structure
+ *
+ * This should usually be used together with COUNTER_SIGNAL_ENUM_AVAILABLE()
+ */
+#define COUNTER_SIGNAL_ENUM(_name, _e) \
+{ \
+ .name = (_name), \
+ .read = counter_signal_enum_read, \
+ .write = counter_signal_enum_write, \
+ .priv = (_e) \
+}
+
+extern ssize_t counter_signal_enum_available_read(
+ struct counter_device *counter, struct counter_signal *signal,
+ void *priv, char *buf);
+
+/**
+ * COUNTER_SIGNAL_ENUM_AVAILABLE() - Initialize Signal enum available extension
+ * @_name: Attribute name ("_available" will be appended to the name)
+ * @_e: Pointer to a counter_signal_enum structure
+ *
+ * Creates a read only attribute that lists all the available enum items in a
+ * newline separated list. This should usually be used together with
+ * COUNTER_SIGNAL_ENUM()
+ */
+#define COUNTER_SIGNAL_ENUM_AVAILABLE(_name, _e) \
+{ \
+ .name = (_name "_available"), \
+ .read = counter_signal_enum_available_read, \
+ .priv = (_e) \
+}
+
+enum synapse_action {
+ SYNAPSE_ACTION_NONE = 0,
+ SYNAPSE_ACTION_RISING_EDGE,
+ SYNAPSE_ACTION_FALLING_EDGE,
+ SYNAPSE_ACTION_BOTH_EDGES
+};
+
+/**
+ * struct counter_synapse - Counter Synapse node
+ * @action: index of current action mode
+ * @actions_list: array of available action modes
+ * @num_actions: number of action modes specified in @actions_list
+ * @signal: pointer to associated signal
+ */
+struct counter_synapse {
+ size_t action;
+ const enum synapse_action *actions_list;
+ size_t num_actions;
+
+ struct counter_signal *signal;
+};
+
+struct counter_count;
+
+/**
+ * struct counter_count_ext - Counter Count extension
+ * @name: attribute name
+ * @read: read callback for this attribute; may be NULL
+ * @write: write callback for this attribute; may be NULL
+ * @priv: data private to the driver
+ */
+struct counter_count_ext {
+ const char *name;
+ ssize_t (*read)(struct counter_device *counter,
+ struct counter_count *count, void *priv,
+ char *buf);
+ ssize_t (*write)(struct counter_device *counter,
+ struct counter_count *count, void *priv,
+ const char *buf, size_t len);
+ void *priv;
+};
+
+enum count_function {
+ COUNT_FUNCTION_INCREASE = 0,
+ COUNT_FUNCTION_DECREASE,
+ COUNT_FUNCTION_PULSE_DIRECTION,
+ COUNT_FUNCTION_QUADRATURE_X1,
+ COUNT_FUNCTION_QUADRATURE_X2,
+ COUNT_FUNCTION_QUADRATURE_X4
+};
+
+/**
+ * struct counter_count - Counter Count node
+ * @id: unique ID used to identify Count
+ * @name: device-specific Count name; ideally, this should match
+ * the name as it appears in the datasheet documentation
+ * @function: index of current function mode
+ * @functions_list: array available function modes
+ * @num_functions: number of function modes specified in @functions_list
+ * @synapses: array of synapses for initialization
+ * @num_synapses: number of synapses specified in @synapses
+ * @ext: optional array of Counter Count extensions
+ * @num_ext: number of Counter Count extensions specified in @ext
+ * @priv: optional private data supplied by driver
+ */
+struct counter_count {
+ int id;
+ const char *name;
+
+ size_t function;
+ const enum count_function *functions_list;
+ size_t num_functions;
+
+ struct counter_synapse *synapses;
+ size_t num_synapses;
+
+ const struct counter_count_ext *ext;
+ size_t num_ext;
+
+ void *priv;
+};
+
+/**
+ * struct counter_count_enum_ext - Count enum extension attribute
+ * @items: Array of strings
+ * @num_items: Number of items specified in @items
+ * @set: Set callback function; may be NULL
+ * @get: Get callback function; may be NULL
+ *
+ * The counter_count_enum_ext structure can be used to implement enum style
+ * Count extension attributes. Enum style attributes are those which have a set
+ * of strings that map to unsigned integer values. The Generic Counter Count
+ * enum extension helper code takes care of mapping between value and string, as
+ * well as generating a "_available" file which contains a list of all available
+ * items. The get callback is used to query the currently active item; the index
+ * of the item within the respective items array is returned via the 'item'
+ * parameter. The set callback is called when the attribute is updated; the
+ * 'item' parameter contains the index of the newly activated item within the
+ * respective items array.
+ */
+struct counter_count_enum_ext {
+ const char * const *items;
+ size_t num_items;
+ int (*get)(struct counter_device *counter,
+ struct counter_count *count,
+ size_t *item);
+ int (*set)(struct counter_device *counter,
+ struct counter_count *count,
+ size_t item);
+};
+
+extern ssize_t counter_count_enum_read(struct counter_device *counter,
+ struct counter_count *count, void *priv, char *buf);
+extern ssize_t counter_count_enum_write(struct counter_device *counter,
+ struct counter_count *count, void *priv, const char *buf, size_t len);
+
+/**
+ * COUNTER_COUNT_ENUM() - Initialize Count enum extension
+ * @_name: Attribute name
+ * @_e: Pointer to a counter_count_enum structure
+ *
+ * This should usually be used together with COUNTER_COUNT_ENUM_AVAILABLE()
+ */
+#define COUNTER_COUNT_ENUM(_name, _e) \
+{ \
+ .name = (_name), \
+ .read = counter_count_enum_read, \
+ .write = counter_count_enum_write, \
+ .priv = (_e) \
+}
+
+extern ssize_t counter_count_enum_available_read(struct counter_device *counter,
+ struct counter_count *count, void *priv, char *buf);
+
+/**
+ * COUNTER_COUNT_ENUM_AVAILABLE() - Initialize Count enum available extension
+ * @_name: Attribute name ("_available" will be appended to the name)
+ * @_e: Pointer to a counter_count_enum structure
+ *
+ * Creates a read only attribute that lists all the available enum items in a
+ * newline separated list. This should usually be used together with
+ * COUNTER_COUNT_ENUM()
+ */
+#define COUNTER_COUNT_ENUM_AVAILABLE(_name, _e) \
+{ \
+ .name = (_name "_available"), \
+ .read = counter_count_enum_available_read, \
+ .priv = (_e) \
+}
+
+/**
+ * struct counter_device_attr_group - internal container for attribute group
+ * @attr_group: Counter sysfs attributes group
+ * @attr_list: list to keep track of created Counter sysfs attributes
+ * @num_attr: number of Counter sysfs attributes
+ */
+struct counter_device_attr_group {
+ struct attribute_group attr_group;
+ struct list_head attr_list;
+ size_t num_attr;
+};
+
+/**
+ * struct counter_device_state - internal state container for a Counter device
+ * @id: unique ID used to identify the Counter
+ * @dev: internal device structure
+ * @groups_list attribute groups list (groups for Signals, Counts, and ext)
+ * @num_groups number of attribute groups containers
+ * @groups: Counter sysfs attribute groups (used to populate @dev.groups)
+ */
+struct counter_device_state {
+ int id;
+ struct device dev;
+ struct counter_device_attr_group *groups_list;
+ size_t num_groups;
+ const struct attribute_group **groups;
+};
+
+/**
+ * struct signal_read_value - Opaque Signal read value
+ * @buf: string representation of Signal read value
+ * @len: length of string in @buf
+ */
+struct signal_read_value {
+ char *buf;
+ size_t len;
+};
+
+/**
+ * struct count_read_value - Opaque Count read value
+ * @buf: string representation of Count read value
+ * @len: length of string in @buf
+ */
+struct count_read_value {
+ char *buf;
+ size_t len;
+};
+
+/**
+ * struct count_write_value - Opaque Count write value
+ * @buf: string representation of Count write value
+ */
+struct count_write_value {
+ const char *buf;
+};
+
+/**
+ * struct counter_device_ext - Counter device extension
+ * @name: attribute name
+ * @read: read callback for this attribute; may be NULL
+ * @write: write callback for this attribute; may be NULL
+ * @priv: data private to the driver
+ */
+struct counter_device_ext {
+ const char *name;
+ ssize_t (*read)(struct counter_device *counter, void *priv,
+ char *buf);
+ ssize_t (*write)(struct counter_device *counter, void *priv,
+ const char *buf, size_t len);
+ void *priv;
+};
+
+/**
+ * struct counter_device_enum_ext - Counter enum extension attribute
+ * @items: Array of strings
+ * @num_items: Number of items specified in @items
+ * @set: Set callback function; may be NULL
+ * @get: Get callback function; may be NULL
+ *
+ * The counter_device_enum_ext structure can be used to implement enum style
+ * Counter extension attributes. Enum style attributes are those which have a
+ * set of strings that map to unsigned integer values. The Generic Counter enum
+ * extension helper code takes care of mapping between value and string, as well
+ * as generating a "_available" file which contains a list of all available
+ * items. The get callback is used to query the currently active item; the index
+ * of the item within the respective items array is returned via the 'item'
+ * parameter. The set callback is called when the attribute is updated; the
+ * 'item' parameter contains the index of the newly activated item within the
+ * respective items array.
+ */
+struct counter_device_enum_ext {
+ const char * const *items;
+ size_t num_items;
+ int (*get)(struct counter_device *counter,
+ size_t *item);
+ int (*set)(struct counter_device *counter,
+ size_t item);
+};
+
+extern ssize_t counter_device_enum_read(struct counter_device *counter,
+ void *priv, char *buf);
+extern ssize_t counter_device_enum_write(struct counter_device *counter,
+ void *priv, const char *buf, size_t len);
+
+/**
+ * COUNTER_DEVICE_ENUM() - Initialize Counter enum extension
+ * @_name: Attribute name
+ * @_e: Pointer to a counter_device_enum structure
+ *
+ * This should usually be used together with COUNTER_DEVICE_ENUM_AVAILABLE()
+ */
+#define COUNTER_DEVICE_ENUM(_name, _e) \
+{ \
+ .name = (_name), \
+ .read = counter_device_enum_read, \
+ .write = counter_device_enum_write, \
+ .priv = (_e) \
+}
+
+extern ssize_t counter_device_enum_available_read(
+ struct counter_device *counter, void *priv, char *buf);
+
+/**
+ * COUNTER_DEVICE_ENUM_AVAILABLE() - Initialize Counter enum available extension
+ * @_name: Attribute name ("_available" will be appended to the name)
+ * @_e: Pointer to a counter_device_enum structure
+ *
+ * Creates a read only attribute that lists all the available enum items in a
+ * newline separated list. This should usually be used together with
+ * COUNTER_DEVICE_ENUM()
+ */
+#define COUNTER_DEVICE_ENUM_AVAILABLE(_name, _e) \
+{ \
+ .name = (_name "_available"), \
+ .read = counter_device_enum_available_read, \
+ .priv = (_e) \
+}
+
+/**
+ * struct counter_device - Counter data structure
+ * @name: name of the device as it appears in the datasheet
+ * @parent: optional parent device providing the counters
+ * @device_state: internal device state container
+ * @signal_read: optional read callback for Signal attribute. The read
+ * value of the respective Signal should be passed back via
+ * the val parameter. val points to an opaque type which
+ * should be set only via the set_signal_read_value
+ * function.
+ * @count_read: optional read callback for Count attribute. The read
+ * value of the respective Count should be passed back via
+ * the val parameter. val points to an opaque type which
+ * should be set only via the set_count_read_value
+ * function.
+ * @count_write: optional write callback for Count attribute. The write
+ * value for the respective Count is passed in via the val
+ * parameter. val points to an opaque type which should be
+ * access only via the get_count_write_value function.
+ * @function_get: function to get the current count function mode. Returns
+ * 0 on success and negative error code on error. The index
+ * of the respective Count's returned function mode should
+ * be passed back via the function parameter.
+ * @function_set: function to set the count function mode. function is the
+ * index of the requested function mode from the respective
+ * Count's functions_list array.
+ * @action_get: function to get the current action mode. Returns 0 on
+ * success and negative error code on error. The index of
+ * the respective Signal's returned action mode should be
+ * passed back via the action parameter.
+ * @action_set: function to set the action mode. action is the index of
+ * the requested action mode from the respective Synapse's
+ * actions_list array.
+ * @signals: array of Signals
+ * @num_signals: number of Signals specified in @signals
+ * @counts: array of Counts
+ * @num_counts: number of Counts specified in @counts
+ * @ext: optional array of Counter device extensions
+ * @num_ext: number of Counter device extensions specified in @ext
+ * @priv: optional private data supplied by driver
+ */
+struct counter_device {
+ const char *name;
+ struct device *parent;
+ struct counter_device_state *device_state;
+
+ int (*signal_read)(struct counter_device *counter,
+ struct counter_signal *signal,
+ struct signal_read_value *val);
+ int (*count_read)(struct counter_device *counter,
+ struct counter_count *count,
+ struct count_read_value *val);
+ int (*count_write)(struct counter_device *counter,
+ struct counter_count *count,
+ struct count_write_value *val);
+ int (*function_get)(struct counter_device *counter,
+ struct counter_count *count, size_t *function);
+ int (*function_set)(struct counter_device *counter,
+ struct counter_count *count, size_t function);
+ int (*action_get)(struct counter_device *counter,
+ struct counter_count *count,
+ struct counter_synapse *synapse, size_t *action);
+ int (*action_set)(struct counter_device *counter,
+ struct counter_count *count,
+ struct counter_synapse *synapse, size_t action);
+
+ struct counter_signal *signals;
+ size_t num_signals;
+ struct counter_count *counts;
+ size_t num_counts;
+
+ const struct counter_device_ext *ext;
+ size_t num_ext;
+
+ void *priv;
+};
+
+enum signal_level {
+ SIGNAL_LEVEL_LOW = 0,
+ SIGNAL_LEVEL_HIGH
+};
+
+enum signal_value_type {
+ SIGNAL_LEVEL = 0
+};
+
+enum count_value_type {
+ COUNT_POSITION_UNSIGNED = 0,
+ COUNT_POSITION_SIGNED
+};
+
+extern void set_signal_read_value(struct signal_read_value *const val,
+ const enum signal_value_type type, void *const data);
+extern void set_count_read_value(struct count_read_value *const val,
+ const enum count_value_type type, void *const data);
+extern int get_count_write_value(void *const data,
+ const enum count_value_type type,
+ const struct count_write_value *const val);
+
+extern int counter_register(struct counter_device *const counter);
+extern void counter_unregister(struct counter_device *const counter);
+extern int devm_counter_register(struct device *dev,
+ struct counter_device *const counter);
+extern void devm_counter_unregister(struct device *dev,
+ struct counter_device *const counter);
+
+#endif /* _COUNTER_H_ */
--
2.16.2
This patch adds standard documentation for the Generic Counter interface
userspace sysfs attributes of the 104-QUAD-8 driver.
Signed-off-by: William Breathitt Gray <[email protected]>
---
.../ABI/testing/sysfs-bus-counter-104-quad-8 | 115 +++++++++++++++++++++
MAINTAINERS | 1 +
2 files changed, 116 insertions(+)
create mode 100644 Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
diff --git a/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8 b/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
new file mode 100644
index 000000000000..4269b438185a
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
@@ -0,0 +1,115 @@
+What: /sys/bus/counter/devices/counterX/countY_count_mode
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Count mode for channel Y. The preset value for channel Y is used
+ by the count mode where required. The following count modes are
+ available:
+
+ Normal:
+ Counting is continuous in either direction.
+
+ Range Limit:
+ An upper or lower limit is set, mimicking limit switches
+ in the mechanical counterpart. The upper limit is set to
+ the preset value, while the lower limit is set to 0. The
+ counter freezes at count = preset when counting up, and
+ at count = 0 when counting down. At either of these
+ limits, the counting is resumed only when the count
+ direction is reversed.
+
+ Non-recycle:
+ Counter is disabled whenever a 24-bit count overflow or
+ underflow takes place. The counter is re-enabled when a
+ new count value is loaded to the counter via a preset
+ operation or write to raw.
+
+ Modulo-N:
+ A count boundary is set between 0 and the preset value.
+ The counter is reset to 0 at count = preset when
+ counting up, while the counter is set to the preset
+ value at count = 0 when counting down; the counter does
+ not freeze at the bundary points, but counts
+ continuously throughout.
+
+What: /sys/bus/counter/devices/counterX/countY_count_mode_available
+What: /sys/bus/counter/devices/counterX/countY_noise_error_available
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Discrete set of available values for the respective Count Y
+ configuration are listed in this file.
+
+What: /sys/bus/counter/devices/counterX/countY_direction
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Read-only attribute that indicates the count direction of
+ Count Y. Two count directions are available: Forward and
+ Backward.
+
+What: /sys/bus/counter/devices/counterX/countY_enable
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Whether channel Y inputs A and B are enabled. Valid attribute
+ values are boolean.
+
+What: /sys/bus/counter/devices/counterX/countY_noise_error
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Read-only attribute that indicates whether excessive noise is
+ present at the channel Y count inputs in quadrature clock mode;
+ irrelevant in non-quadrature (Pulse-Direction) clock mode.
+
+What: /sys/bus/counter/devices/counterX/countY_preset
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ If the counter device supports preset registers, the preset
+ count for channel Y is provided by this attribute.
+
+What: /sys/bus/counter/devices/counterX/countY_preset_enable
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Whether to set channel Y counter with channel Y preset value
+ when channel Y index input is active, or continuously count.
+ Valid attribute values are boolean.
+
+What: /sys/bus/counter/devices/counterX/signalY_index_polarity
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Active level of channel Y-16 index input; irrelevant in
+ non-synchronous load mode.
+
+What: /sys/bus/counter/devices/counterX/signalY_index_polarity_available
+What: /sys/bus/counter/devices/counterX/signalY_synchronous_mode_available
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Discrete set of available values for the respective Signal Y
+ configuration are listed in this file.
+
+What: /sys/bus/counter/devices/counterX/signalY_synchronous_mode
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Configure channel Y-16 counter for non-synchronous or
+ synchronous load mode. Synchronous load mode cannot be selected
+ in non-quadrature (Pulse-Direction) clock mode.
+
+ Non-synchronous:
+ A logic low level is the active level at this index
+ input. The index function (as enabled via preset_enable)
+ is performed directly on the active level of the index
+ input.
+
+ Synchronous:
+ Intended for interfacing with encoder Index output in
+ quadrature clock mode. The active level is configured
+ 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.
diff --git a/MAINTAINERS b/MAINTAINERS
index febe27a9962e..8134050d175a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -270,6 +270,7 @@ ACCES 104-QUAD-8 DRIVER
M: William Breathitt Gray <[email protected]>
L: [email protected]
S: Maintained
+F: Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
F: Documentation/ABI/testing/sysfs-bus-iio-counter-104-quad-8
F: drivers/counter/104-quad-8.c
--
2.16.2
From: Benjamin Gaignard <[email protected]>
Add bindings for STM32 Timer quadrature encoder.
It is a sub-node of STM32 Timer which implement the
counter part of the hardware.
Signed-off-by: Benjamin Gaignard <[email protected]>
Signed-off-by: William Breathitt Gray <[email protected]>
---
.../bindings/counter/stm32-timer-cnt.txt | 26 ++++++++++++++++++++++
.../devicetree/bindings/mfd/stm32-timers.txt | 7 ++++++
2 files changed, 33 insertions(+)
create mode 100644 Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt
diff --git a/Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt b/Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt
new file mode 100644
index 000000000000..377728128bef
--- /dev/null
+++ b/Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt
@@ -0,0 +1,26 @@
+STMicroelectronics STM32 Timer quadrature encoder
+
+STM32 Timer provides quadrature encoder counter mode to detect
+angular position and direction of rotary elements,
+from IN1 and IN2 input signals.
+
+Must be a sub-node of an STM32 Timer device tree node.
+See ../mfd/stm32-timers.txt for details about the parent node.
+
+Required properties:
+- compatible: Must be "st,stm32-timer-counter".
+- pinctrl-names: Set to "default".
+- pinctrl-0: List of phandles pointing to pin configuration nodes,
+ to set IN1/IN2 pins in mode of operation for Low-Power
+ Timer input on external pin.
+
+Example:
+ timers@40010000 {
+ compatible = "st,stm32-timers";
+ ...
+ counter {
+ compatible = "st,stm32-timer-counter";
+ pinctrl-names = "default";
+ pinctrl-0 = <&tim1_in_pins>;
+ };
+ };
diff --git a/Documentation/devicetree/bindings/mfd/stm32-timers.txt b/Documentation/devicetree/bindings/mfd/stm32-timers.txt
index 1db6e0057a63..ff9c14ada30b 100644
--- a/Documentation/devicetree/bindings/mfd/stm32-timers.txt
+++ b/Documentation/devicetree/bindings/mfd/stm32-timers.txt
@@ -23,6 +23,7 @@ Optional parameters:
Optional subnodes:
- pwm: See ../pwm/pwm-stm32.txt
- timer: See ../iio/timer/stm32-timer-trigger.txt
+- counter: See ../counter/stm32-timer-cnt.txt
Example:
timers@40010000 {
@@ -43,4 +44,10 @@ Example:
compatible = "st,stm32-timer-trigger";
reg = <0>;
};
+
+ counter {
+ compatible = "st,stm32-timer-counter";
+ pinctrl-names = "default";
+ pinctrl-0 = <&tim1_in_pins>;
+ };
};
--
2.16.2
This patch adds support for the Generic Counter interface to the
104-QUAD-8 driver. The existing 104-QUAD-8 device interface should not
be affected by this patch; all changes are intended as supplemental
additions as perceived by the user.
Generic Counter Counts are created for the eight quadrature channel
counts, as well as their respective quadrature A and B Signals (which
are associated via respective Synapse structures) and respective index
Signals.
The new Generic Counter interface sysfs attributes are intended to
expose the same functionality and data available via the existing
104-QUAD-8 IIO device interface; the Generic Counter interface serves
to provide the respective functionality and data in a standard way
expected of counter devices.
Signed-off-by: William Breathitt Gray <[email protected]>
---
MAINTAINERS | 4 +-
drivers/counter/104-quad-8.c | 685 ++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 677 insertions(+), 12 deletions(-)
diff --git a/MAINTAINERS b/MAINTAINERS
index a71dff6eae87..febe27a9962e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -266,12 +266,12 @@ L: [email protected]
S: Maintained
F: drivers/gpio/gpio-104-idio-16.c
-ACCES 104-QUAD-8 IIO DRIVER
+ACCES 104-QUAD-8 DRIVER
M: William Breathitt Gray <[email protected]>
L: [email protected]
S: Maintained
F: Documentation/ABI/testing/sysfs-bus-iio-counter-104-quad-8
-F: drivers/iio/counter/104-quad-8.c
+F: drivers/counter/104-quad-8.c
ACCES PCI-IDIO-16 GPIO DRIVER
M: William Breathitt Gray <[email protected]>
diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index b56985078d8c..5db314fcb08d 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0-only
/*
* IIO driver for the ACCES 104-QUAD-8
* Copyright (C) 2016 William Breathitt Gray
@@ -14,6 +15,7 @@
* This driver supports the ACCES 104-QUAD-8 and ACCES 104-QUAD-4.
*/
#include <linux/bitops.h>
+#include <linux/counter.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/iio/iio.h>
@@ -37,6 +39,7 @@ MODULE_PARM_DESC(base, "ACCES 104-QUAD-8 base addresses");
/**
* struct quad8_iio - IIO device private data structure
+ * @counter: instance of the counter_device
* @preset: array of preset values
* @count_mode: array of count mode configurations
* @quadrature_mode: array of quadrature mode configurations
@@ -48,6 +51,7 @@ MODULE_PARM_DESC(base, "ACCES 104-QUAD-8 base addresses");
* @base: base port address of the IIO device
*/
struct quad8_iio {
+ struct counter_device counter;
unsigned int preset[QUAD8_NUM_COUNTERS];
unsigned int count_mode[QUAD8_NUM_COUNTERS];
unsigned int quadrature_mode[QUAD8_NUM_COUNTERS];
@@ -527,24 +531,665 @@ static const struct iio_chan_spec quad8_channels[] = {
QUAD8_COUNT_CHAN(7), QUAD8_INDEX_CHAN(7)
};
+static int quad8_signal_read(struct counter_device *counter,
+ struct counter_signal *signal, struct signal_read_value *val)
+{
+ const struct quad8_iio *const priv = counter->priv;
+ unsigned int state;
+ enum signal_level level;
+
+ /* Only Index signal levels can be read */
+ if (signal->id < 16)
+ return -EINVAL;
+
+ state = inb(priv->base + 0x16) & BIT(signal->id - 16);
+
+ level = (state) ? SIGNAL_LEVEL_HIGH : SIGNAL_LEVEL_LOW;
+
+ set_signal_read_value(val, SIGNAL_LEVEL, &level);
+
+ return 0;
+}
+
+static int quad8_count_read(struct counter_device *counter,
+ struct counter_count *count, struct count_read_value *val)
+{
+ const struct quad8_iio *const priv = counter->priv;
+ const int base_offset = priv->base + 2 * count->id;
+ unsigned int flags;
+ unsigned int borrow;
+ unsigned int carry;
+ unsigned long position;
+ int i;
+
+ flags = inb(base_offset + 1);
+ borrow = flags & BIT(0);
+ carry = !!(flags & BIT(1));
+
+ /* Borrow XOR Carry effectively doubles count range */
+ position = (unsigned long)(borrow ^ carry) << 24;
+
+ /* Reset Byte Pointer; transfer Counter to Output Latch */
+ outb(0x11, base_offset + 1);
+
+ for (i = 0; i < 3; i++)
+ position |= (unsigned long)inb(base_offset) << (8 * i);
+
+ set_count_read_value(val, COUNT_POSITION_UNSIGNED, &position);
+
+ return 0;
+}
+
+static int quad8_count_write(struct counter_device *counter,
+ struct counter_count *count, struct count_write_value *val)
+{
+ const struct quad8_iio *const priv = counter->priv;
+ const int base_offset = priv->base + 2 * count->id;
+ int err;
+ unsigned long position;
+ int i;
+
+ err = get_count_write_value(&position, COUNT_POSITION_UNSIGNED, val);
+ if (err)
+ return err;
+
+ /* Only 24-bit values are supported */
+ if (position > 0xFFFFFF)
+ return -EINVAL;
+
+ /* Reset Byte Pointer */
+ outb(0x01, base_offset + 1);
+
+ /* Counter can only be set via Preset Register */
+ for (i = 0; i < 3; i++)
+ outb(position >> (8 * i), base_offset);
+
+ /* Transfer Preset Register to Counter */
+ outb(0x08, base_offset + 1);
+
+ /* Reset Byte Pointer */
+ outb(0x01, base_offset + 1);
+
+ /* Set Preset Register back to original value */
+ position = priv->preset[count->id];
+ for (i = 0; i < 3; i++)
+ outb(position >> (8 * i), base_offset);
+
+ /* Reset Borrow, Carry, Compare, and Sign flags */
+ outb(0x02, base_offset + 1);
+ /* Reset Error flag */
+ outb(0x06, base_offset + 1);
+
+ return 0;
+}
+
+enum quad8_count_function {
+ QUAD8_COUNT_FUNCTION_PULSE_DIRECTION = 0,
+ QUAD8_COUNT_FUNCTION_QUADRATURE_X1,
+ QUAD8_COUNT_FUNCTION_QUADRATURE_X2,
+ QUAD8_COUNT_FUNCTION_QUADRATURE_X4
+};
+
+static enum count_function quad8_count_functions_list[] = {
+ [QUAD8_COUNT_FUNCTION_PULSE_DIRECTION] = COUNT_FUNCTION_PULSE_DIRECTION,
+ [QUAD8_COUNT_FUNCTION_QUADRATURE_X1] = COUNT_FUNCTION_QUADRATURE_X1,
+ [QUAD8_COUNT_FUNCTION_QUADRATURE_X2] = COUNT_FUNCTION_QUADRATURE_X2,
+ [QUAD8_COUNT_FUNCTION_QUADRATURE_X4] = COUNT_FUNCTION_QUADRATURE_X4
+};
+
+static int quad8_function_get(struct counter_device *counter,
+ struct counter_count *count, size_t *function)
+{
+ const struct quad8_iio *const priv = counter->priv;
+ const int id = count->id;
+ const unsigned int quadrature_mode = priv->quadrature_mode[id];
+ const unsigned int scale = priv->quadrature_scale[id];
+
+ if (quadrature_mode)
+ switch (scale) {
+ case 0:
+ *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X1;
+ break;
+ case 1:
+ *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X2;
+ break;
+ case 2:
+ *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X4;
+ break;
+ }
+ else
+ *function = QUAD8_COUNT_FUNCTION_PULSE_DIRECTION;
+
+ return 0;
+}
+
+static int quad8_function_set(struct counter_device *counter,
+ struct counter_count *count, size_t function)
+{
+ struct quad8_iio *const priv = counter->priv;
+ const int id = count->id;
+ unsigned int *const quadrature_mode = priv->quadrature_mode + id;
+ unsigned int *const scale = priv->quadrature_scale + id;
+ unsigned int mode_cfg = priv->count_mode[id] << 1;
+ unsigned int *const synchronous_mode = priv->synchronous_mode + id;
+ const unsigned int idr_cfg = priv->index_polarity[id] << 1;
+ const int base_offset = priv->base + 2 * id + 1;
+
+ if (function == QUAD8_COUNT_FUNCTION_PULSE_DIRECTION) {
+ *quadrature_mode = 0;
+
+ /* Quadrature scaling only available in quadrature mode */
+ *scale = 0;
+
+ /* Synchronous function not supported in non-quadrature mode */
+ if (*synchronous_mode) {
+ *synchronous_mode = 0;
+ /* Disable synchronous function mode */
+ outb(0x60 | idr_cfg, base_offset);
+ }
+ } else {
+ *quadrature_mode = 1;
+
+ switch (function) {
+ case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
+ *scale = 0;
+ mode_cfg |= 0x8;
+ break;
+ case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
+ *scale = 1;
+ mode_cfg |= 0x10;
+ break;
+ case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
+ *scale = 2;
+ mode_cfg |= 0x18;
+ break;
+ }
+ }
+
+ /* Load mode configuration to Counter Mode Register */
+ outb(0x20 | mode_cfg, base_offset);
+
+ return 0;
+}
+
+enum count_direction {
+ COUNT_DIRECTION_FORWARD = 0,
+ COUNT_DIRECTION_BACKWARD
+};
+
+static void quad8_direction_get(struct counter_device *counter,
+ struct counter_count *count, enum count_direction *direction)
+{
+ const struct quad8_iio *const priv = counter->priv;
+ unsigned int ud_flag;
+ const unsigned int flag_addr = priv->base + 2 * count->id + 1;
+
+ /* U/D flag: nonzero = up, zero = down */
+ ud_flag = inb(flag_addr) & BIT(5);
+
+ *direction = (ud_flag) ? COUNT_DIRECTION_FORWARD :
+ COUNT_DIRECTION_BACKWARD;
+}
+
+enum quad8_synapse_action {
+ QUAD8_SYNAPSE_ACTION_NONE = 0,
+ QUAD8_SYNAPSE_ACTION_RISING_EDGE,
+ QUAD8_SYNAPSE_ACTION_FALLING_EDGE,
+ QUAD8_SYNAPSE_ACTION_BOTH_EDGES
+};
+
+static enum synapse_action quad8_index_actions_list[] = {
+ [QUAD8_SYNAPSE_ACTION_NONE] = SYNAPSE_ACTION_NONE,
+ [QUAD8_SYNAPSE_ACTION_RISING_EDGE] = SYNAPSE_ACTION_RISING_EDGE
+};
+
+static enum synapse_action quad8_synapse_actions_list[] = {
+ [QUAD8_SYNAPSE_ACTION_NONE] = SYNAPSE_ACTION_NONE,
+ [QUAD8_SYNAPSE_ACTION_RISING_EDGE] = SYNAPSE_ACTION_RISING_EDGE,
+ [QUAD8_SYNAPSE_ACTION_FALLING_EDGE] = SYNAPSE_ACTION_FALLING_EDGE,
+ [QUAD8_SYNAPSE_ACTION_BOTH_EDGES] = SYNAPSE_ACTION_BOTH_EDGES
+};
+
+static int quad8_action_get(struct counter_device *counter,
+ struct counter_count *count, struct counter_synapse *synapse,
+ size_t *action)
+{
+ struct quad8_iio *const priv = counter->priv;
+ int err;
+ size_t function;
+ const size_t signal_a_id = count->synapses[0].signal->id;
+ enum count_direction direction;
+
+ /* Handle Index signals */
+ if (synapse->signal->id >= 16) {
+ if (priv->preset_enable[count->id])
+ *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
+ else
+ *action = QUAD8_SYNAPSE_ACTION_NONE;
+
+ return 0;
+ }
+
+ err = counter->function_get(counter, count, &function);
+ if (err)
+ return err;
+
+ /* Default action mode */
+ *action = QUAD8_SYNAPSE_ACTION_NONE;
+
+ /* Determine action mode based on current count function mode */
+ switch (function) {
+ case QUAD8_COUNT_FUNCTION_PULSE_DIRECTION:
+ if (synapse->signal->id == signal_a_id)
+ *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
+ break;
+ case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
+ if (synapse->signal->id == signal_a_id) {
+ quad8_direction_get(counter, count, &direction);
+
+ if (direction == COUNT_DIRECTION_FORWARD)
+ *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
+ else
+ *action = QUAD8_SYNAPSE_ACTION_FALLING_EDGE;
+ }
+ break;
+ case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
+ if (synapse->signal->id == signal_a_id)
+ *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
+ break;
+ case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
+ *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
+ break;
+ }
+
+ return 0;
+}
+
+static int quad8_index_polarity_get(struct counter_device *counter,
+ struct counter_signal *signal, size_t *index_polarity)
+{
+ const struct quad8_iio *const priv = counter->priv;
+ const size_t channel_id = signal->id - 16;
+
+ *index_polarity = priv->index_polarity[channel_id];
+
+ return 0;
+}
+
+static int quad8_index_polarity_set(struct counter_device *counter,
+ struct counter_signal *signal, size_t index_polarity)
+{
+ struct quad8_iio *const priv = counter->priv;
+ const size_t channel_id = signal->id - 16;
+ const unsigned int idr_cfg = priv->synchronous_mode[channel_id] |
+ index_polarity << 1;
+ const int base_offset = priv->base + 2 * channel_id + 1;
+
+ priv->index_polarity[channel_id] = index_polarity;
+
+ /* Load Index Control configuration to Index Control Register */
+ outb(0x60 | idr_cfg, base_offset);
+
+ return 0;
+}
+
+static struct counter_signal_enum_ext quad8_index_pol_enum = {
+ .items = quad8_index_polarity_modes,
+ .num_items = ARRAY_SIZE(quad8_index_polarity_modes),
+ .get = quad8_index_polarity_get,
+ .set = quad8_index_polarity_set
+};
+
+static int quad8_synchronous_mode_get(struct counter_device *counter,
+ struct counter_signal *signal, size_t *synchronous_mode)
+{
+ const struct quad8_iio *const priv = counter->priv;
+ const size_t channel_id = signal->id - 16;
+
+ *synchronous_mode = priv->synchronous_mode[channel_id];
+
+ return 0;
+}
+
+static int quad8_synchronous_mode_set(struct counter_device *counter,
+ struct counter_signal *signal, size_t synchronous_mode)
+{
+ struct quad8_iio *const priv = counter->priv;
+ const size_t channel_id = signal->id - 16;
+ const unsigned int idr_cfg = synchronous_mode |
+ priv->index_polarity[channel_id] << 1;
+ const int base_offset = priv->base + 2 * channel_id + 1;
+
+ /* Index function must be non-synchronous in non-quadrature mode */
+ if (synchronous_mode && !priv->quadrature_mode[channel_id])
+ return -EINVAL;
+
+ priv->synchronous_mode[channel_id] = synchronous_mode;
+
+ /* Load Index Control configuration to Index Control Register */
+ outb(0x60 | idr_cfg, base_offset);
+
+ return 0;
+}
+
+static struct counter_signal_enum_ext quad8_syn_mode_enum = {
+ .items = quad8_synchronous_modes,
+ .num_items = ARRAY_SIZE(quad8_synchronous_modes),
+ .get = quad8_synchronous_mode_get,
+ .set = quad8_synchronous_mode_set
+};
+
+static int quad8_count_mode_get(struct counter_device *counter,
+ struct counter_count *count, size_t *count_mode)
+{
+ const struct quad8_iio *const priv = counter->priv;
+
+ *count_mode = priv->count_mode[count->id];
+
+ return 0;
+}
+
+static int quad8_count_mode_set(struct counter_device *counter,
+ struct counter_count *count, size_t count_mode)
+{
+ struct quad8_iio *const priv = counter->priv;
+ unsigned int mode_cfg = count_mode << 1;
+ const int base_offset = priv->base + 2 * count->id + 1;
+
+ priv->count_mode[count->id] = count_mode;
+
+ /* Add quadrature mode configuration */
+ if (priv->quadrature_mode[count->id])
+ mode_cfg |= (priv->quadrature_scale[count->id] + 1) << 3;
+
+ /* Load mode configuration to Counter Mode Register */
+ outb(0x20 | mode_cfg, base_offset);
+
+ return 0;
+}
+
+static struct counter_count_enum_ext quad8_cnt_mode_enum = {
+ .items = quad8_count_modes,
+ .num_items = ARRAY_SIZE(quad8_count_modes),
+ .get = quad8_count_mode_get,
+ .set = quad8_count_mode_set
+};
+
+static const char *const quad8_count_direction_str[] = {
+ [COUNT_DIRECTION_FORWARD] = "forward",
+ [COUNT_DIRECTION_BACKWARD] = "backward"
+};
+
+static ssize_t quad8_count_direction_read(struct counter_device *counter,
+ struct counter_count *count, void *priv, char *buf)
+{
+ enum count_direction direction;
+
+ quad8_direction_get(counter, count, &direction);
+
+ return scnprintf(buf, PAGE_SIZE, "%s\n",
+ quad8_count_direction_str[direction]);
+}
+
+static ssize_t quad8_count_enable_read(struct counter_device *counter,
+ struct counter_count *count, void *private, char *buf)
+{
+ const struct quad8_iio *const priv = counter->priv;
+
+ return scnprintf(buf, PAGE_SIZE, "%u\n", priv->ab_enable[count->id]);
+}
+
+static ssize_t quad8_count_enable_write(struct counter_device *counter,
+ struct counter_count *count, void *private, const char *buf, size_t len)
+{
+ struct quad8_iio *const priv = counter->priv;
+ const int base_offset = priv->base + 2 * count->id;
+ int err;
+ bool ab_enable;
+ unsigned int ior_cfg;
+
+ err = kstrtobool(buf, &ab_enable);
+ if (err)
+ return err;
+
+ priv->ab_enable[count->id] = ab_enable;
+
+ ior_cfg = ab_enable | priv->preset_enable[count->id] << 1;
+
+ /* Load I/O control configuration */
+ outb(0x40 | ior_cfg, base_offset + 1);
+
+ return len;
+}
+
+static int quad8_noise_error_get(struct counter_device *counter,
+ struct counter_count *count, size_t *noise_error)
+{
+ const struct quad8_iio *const priv = counter->priv;
+ const int base_offset = priv->base + 2 * count->id + 1;
+
+ *noise_error = !!(inb(base_offset) & BIT(4));
+
+ return 0;
+}
+
+static struct counter_count_enum_ext quad8_noise_err_enum = {
+ .items = quad8_noise_error_states,
+ .num_items = ARRAY_SIZE(quad8_noise_error_states),
+ .get = quad8_noise_error_get
+};
+
+static ssize_t quad8_count_preset_read(struct counter_device *counter,
+ struct counter_count *count, void *private, char *buf)
+{
+ const struct quad8_iio *const priv = counter->priv;
+
+ return snprintf(buf, PAGE_SIZE, "%u\n", priv->preset[count->id]);
+}
+
+static ssize_t quad8_count_preset_write(struct counter_device *counter,
+ struct counter_count *count, void *private, const char *buf, size_t len)
+{
+ struct quad8_iio *const priv = counter->priv;
+ const int base_offset = priv->base + 2 * count->id;
+ unsigned int preset;
+ int ret;
+ int i;
+
+ ret = kstrtouint(buf, 0, &preset);
+ if (ret)
+ return ret;
+
+ /* Only 24-bit values are supported */
+ if (preset > 0xFFFFFF)
+ return -EINVAL;
+
+ priv->preset[count->id] = preset;
+
+ /* Reset Byte Pointer */
+ outb(0x01, base_offset + 1);
+
+ /* Set Preset Register */
+ for (i = 0; i < 3; i++)
+ outb(preset >> (8 * i), base_offset);
+
+ return len;
+}
+
+static ssize_t quad8_count_preset_enable_read(struct counter_device *counter,
+ struct counter_count *count, void *private, char *buf)
+{
+ const struct quad8_iio *const priv = counter->priv;
+
+ return snprintf(buf, PAGE_SIZE, "%u\n",
+ !priv->preset_enable[count->id]);
+}
+
+static ssize_t quad8_count_preset_enable_write(struct counter_device *counter,
+ struct counter_count *count, void *private, const char *buf, size_t len)
+{
+ struct quad8_iio *const priv = counter->priv;
+ const int base_offset = priv->base + 2 * count->id + 1;
+ bool preset_enable;
+ int ret;
+ unsigned int ior_cfg;
+
+ ret = kstrtobool(buf, &preset_enable);
+ if (ret)
+ return ret;
+
+ /* Preset enable is active low in Input/Output Control register */
+ preset_enable = !preset_enable;
+
+ priv->preset_enable[count->id] = preset_enable;
+
+ ior_cfg = priv->ab_enable[count->id] | (unsigned int)preset_enable << 1;
+
+ /* Load I/O control configuration to Input / Output Control Register */
+ outb(0x40 | ior_cfg, base_offset);
+
+ return len;
+}
+
+static const struct counter_signal_ext quad8_index_ext[] = {
+ COUNTER_SIGNAL_ENUM("index_polarity", &quad8_index_pol_enum),
+ COUNTER_SIGNAL_ENUM_AVAILABLE("index_polarity", &quad8_index_pol_enum),
+ COUNTER_SIGNAL_ENUM("synchronous_mode", &quad8_syn_mode_enum),
+ COUNTER_SIGNAL_ENUM_AVAILABLE("synchronous_mode", &quad8_syn_mode_enum)
+};
+
+#define QUAD8_QUAD_SIGNAL(_id, _name) { \
+ .id = (_id), \
+ .name = (_name) \
+}
+
+#define QUAD8_INDEX_SIGNAL(_id, _name) { \
+ .id = (_id), \
+ .name = (_name), \
+ .ext = quad8_index_ext, \
+ .num_ext = ARRAY_SIZE(quad8_index_ext) \
+}
+
+static struct counter_signal quad8_signals[] = {
+ QUAD8_QUAD_SIGNAL(0, "Channel 1 Quadrature A"),
+ QUAD8_QUAD_SIGNAL(1, "Channel 1 Quadrature B"),
+ QUAD8_QUAD_SIGNAL(2, "Channel 2 Quadrature A"),
+ QUAD8_QUAD_SIGNAL(3, "Channel 2 Quadrature B"),
+ QUAD8_QUAD_SIGNAL(4, "Channel 3 Quadrature A"),
+ QUAD8_QUAD_SIGNAL(5, "Channel 3 Quadrature B"),
+ QUAD8_QUAD_SIGNAL(6, "Channel 4 Quadrature A"),
+ QUAD8_QUAD_SIGNAL(7, "Channel 4 Quadrature B"),
+ QUAD8_QUAD_SIGNAL(8, "Channel 5 Quadrature A"),
+ QUAD8_QUAD_SIGNAL(9, "Channel 5 Quadrature B"),
+ QUAD8_QUAD_SIGNAL(10, "Channel 6 Quadrature A"),
+ QUAD8_QUAD_SIGNAL(11, "Channel 6 Quadrature B"),
+ QUAD8_QUAD_SIGNAL(12, "Channel 7 Quadrature A"),
+ QUAD8_QUAD_SIGNAL(13, "Channel 7 Quadrature B"),
+ QUAD8_QUAD_SIGNAL(14, "Channel 8 Quadrature A"),
+ QUAD8_QUAD_SIGNAL(15, "Channel 8 Quadrature B"),
+ QUAD8_INDEX_SIGNAL(16, "Channel 1 Index"),
+ QUAD8_INDEX_SIGNAL(17, "Channel 2 Index"),
+ QUAD8_INDEX_SIGNAL(18, "Channel 3 Index"),
+ QUAD8_INDEX_SIGNAL(19, "Channel 4 Index"),
+ QUAD8_INDEX_SIGNAL(20, "Channel 5 Index"),
+ QUAD8_INDEX_SIGNAL(21, "Channel 6 Index"),
+ QUAD8_INDEX_SIGNAL(22, "Channel 7 Index"),
+ QUAD8_INDEX_SIGNAL(23, "Channel 8 Index")
+};
+
+#define QUAD8_COUNT_SYNAPSES(_id) { \
+ { \
+ .actions_list = quad8_synapse_actions_list, \
+ .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
+ .signal = quad8_signals + 2 * (_id) \
+ }, \
+ { \
+ .actions_list = quad8_synapse_actions_list, \
+ .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
+ .signal = quad8_signals + 2 * (_id) + 1 \
+ }, \
+ { \
+ .actions_list = quad8_index_actions_list, \
+ .num_actions = ARRAY_SIZE(quad8_index_actions_list), \
+ .signal = quad8_signals + 2 * (_id) + 16 \
+ } \
+}
+
+static struct counter_synapse quad8_count_synapses[][3] = {
+ QUAD8_COUNT_SYNAPSES(0), QUAD8_COUNT_SYNAPSES(1),
+ QUAD8_COUNT_SYNAPSES(2), QUAD8_COUNT_SYNAPSES(3),
+ QUAD8_COUNT_SYNAPSES(4), QUAD8_COUNT_SYNAPSES(5),
+ QUAD8_COUNT_SYNAPSES(6), QUAD8_COUNT_SYNAPSES(7)
+};
+
+static const struct counter_count_ext quad8_count_ext[] = {
+ COUNTER_COUNT_ENUM("count_mode", &quad8_cnt_mode_enum),
+ COUNTER_COUNT_ENUM_AVAILABLE("count_mode", &quad8_cnt_mode_enum),
+ {
+ .name = "direction",
+ .read = quad8_count_direction_read
+ },
+ {
+ .name = "enable",
+ .read = quad8_count_enable_read,
+ .write = quad8_count_enable_write
+ },
+ COUNTER_COUNT_ENUM("noise_error", &quad8_noise_err_enum),
+ COUNTER_COUNT_ENUM_AVAILABLE("noise_error", &quad8_noise_err_enum),
+ {
+ .name = "preset",
+ .read = quad8_count_preset_read,
+ .write = quad8_count_preset_write
+ },
+ {
+ .name = "preset_enable",
+ .read = quad8_count_preset_enable_read,
+ .write = quad8_count_preset_enable_write
+ }
+};
+
+#define QUAD8_COUNT(_id, _cntname) { \
+ .id = (_id), \
+ .name = (_cntname), \
+ .functions_list = quad8_count_functions_list, \
+ .num_functions = ARRAY_SIZE(quad8_count_functions_list), \
+ .synapses = quad8_count_synapses[(_id)], \
+ .num_synapses = 2, \
+ .ext = quad8_count_ext, \
+ .num_ext = ARRAY_SIZE(quad8_count_ext) \
+}
+
+static struct counter_count quad8_counts[] = {
+ QUAD8_COUNT(0, "Channel 1 Count"),
+ QUAD8_COUNT(1, "Channel 2 Count"),
+ QUAD8_COUNT(2, "Channel 3 Count"),
+ QUAD8_COUNT(3, "Channel 4 Count"),
+ QUAD8_COUNT(4, "Channel 5 Count"),
+ QUAD8_COUNT(5, "Channel 6 Count"),
+ QUAD8_COUNT(6, "Channel 7 Count"),
+ QUAD8_COUNT(7, "Channel 8 Count")
+};
+
static int quad8_probe(struct device *dev, unsigned int id)
{
struct iio_dev *indio_dev;
- struct quad8_iio *priv;
+ struct quad8_iio *quad8iio;
int i, j;
unsigned int base_offset;
+ int err;
- indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
- if (!indio_dev)
- return -ENOMEM;
-
- if (!devm_request_region(dev, base[id], QUAD8_EXTENT,
- dev_name(dev))) {
+ if (!devm_request_region(dev, base[id], QUAD8_EXTENT, dev_name(dev))) {
dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
base[id], base[id] + QUAD8_EXTENT);
return -EBUSY;
}
+ /* Allocate IIO device; this also allocates driver data structure */
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*quad8iio));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ /* Initialize IIO device */
indio_dev->info = &quad8_info;
indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->num_channels = ARRAY_SIZE(quad8_channels);
@@ -552,8 +1197,22 @@ static int quad8_probe(struct device *dev, unsigned int id)
indio_dev->name = dev_name(dev);
indio_dev->dev.parent = dev;
- priv = iio_priv(indio_dev);
- priv->base = base[id];
+ /* Initialize Counter device and driver data */
+ quad8iio = iio_priv(indio_dev);
+ quad8iio->counter.name = dev_name(dev);
+ quad8iio->counter.parent = dev;
+ quad8iio->counter.signal_read = quad8_signal_read;
+ quad8iio->counter.count_read = quad8_count_read;
+ quad8iio->counter.count_write = quad8_count_write;
+ quad8iio->counter.function_get = quad8_function_get;
+ quad8iio->counter.function_set = quad8_function_set;
+ quad8iio->counter.action_get = quad8_action_get;
+ quad8iio->counter.counts = quad8_counts;
+ quad8iio->counter.num_counts = ARRAY_SIZE(quad8_counts);
+ quad8iio->counter.signals = quad8_signals;
+ quad8iio->counter.num_signals = ARRAY_SIZE(quad8_signals);
+ quad8iio->counter.priv = quad8iio;
+ quad8iio->base = base[id];
/* Reset all counters and disable interrupt function */
outb(0x01, base[id] + 0x11);
@@ -579,7 +1238,13 @@ static int quad8_probe(struct device *dev, unsigned int id)
/* Enable all counters */
outb(0x00, base[id] + 0x11);
- return devm_iio_device_register(dev, indio_dev);
+ /* Register IIO device */
+ err = devm_iio_device_register(dev, indio_dev);
+ if (err)
+ return err;
+
+ /* Register Counter device */
+ return devm_counter_register(dev, &quad8iio->counter);
}
static struct isa_driver quad8_driver = {
--
2.16.2
From: Benjamin Gaignard <[email protected]>
Implement counter part of the STM32 timer hardware block
by using counter API.
Hardware only support X2 and X4 quadrature modes.
A Preset value can to set to define the maximum value
reachable by the counter.
Signed-off-by: Benjamin Gaignard <[email protected]>
Signed-off-by: William Breathitt Gray <[email protected]>
---
drivers/counter/Kconfig | 10 ++
drivers/counter/Makefile | 1 +
drivers/counter/stm32-timer-cnt.c | 356 ++++++++++++++++++++++++++++++++++++++
3 files changed, 367 insertions(+)
create mode 100644 drivers/counter/stm32-timer-cnt.c
diff --git a/drivers/counter/Kconfig b/drivers/counter/Kconfig
index 0b28d3ff524b..d758279081ac 100644
--- a/drivers/counter/Kconfig
+++ b/drivers/counter/Kconfig
@@ -35,6 +35,16 @@ config 104_QUAD_8
The base port addresses for the devices may be configured via the base
array module parameter.
+config STM32_TIMER_CNT
+ tristate "STM32 Timer encoder counter driver"
+ depends on MFD_STM32_TIMERS || COMPILE_TEST
+ help
+ Select this option to enable STM32 Timer quadrature encoder
+ and counter driver.
+
+ To compile this driver as a module, choose M here: the
+ module will be called stm32-timer-cnt.
+
config STM32_LPTIMER_CNT
tristate "STM32 LP Timer encoder counter driver"
depends on (MFD_STM32_LPTIMER || COMPILE_TEST) && IIO
diff --git a/drivers/counter/Makefile b/drivers/counter/Makefile
index d721a40aa4a2..9c037d7d1ed6 100644
--- a/drivers/counter/Makefile
+++ b/drivers/counter/Makefile
@@ -8,4 +8,5 @@ obj-$(CONFIG_COUNTER) += counter.o
counter-y := generic-counter.o
obj-$(CONFIG_104_QUAD_8) += 104-quad-8.o
+obj-$(CONFIG_STM32_TIMER_CNT) += stm32-timer-cnt.o
obj-$(CONFIG_STM32_LPTIMER_CNT) += stm32-lptimer-cnt.o
diff --git a/drivers/counter/stm32-timer-cnt.c b/drivers/counter/stm32-timer-cnt.c
new file mode 100644
index 000000000000..14085615e880
--- /dev/null
+++ b/drivers/counter/stm32-timer-cnt.c
@@ -0,0 +1,356 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * STM32 Timer Encoder and Counter driver
+ *
+ * Copyright (C) STMicroelectronics 2018
+ *
+ * Author: Benjamin Gaignard <[email protected]>
+ *
+ */
+#include <linux/counter.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/types.h>
+#include <linux/mfd/stm32-timers.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+#define TIM_CCMR_CCXS (BIT(8) | BIT(0))
+#define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
+ TIM_CCMR_IC1F | TIM_CCMR_IC2F)
+#define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
+ TIM_CCER_CC2P | TIM_CCER_CC2NP)
+
+struct stm32_timer_cnt {
+ struct counter_device counter;
+ struct regmap *regmap;
+ struct clk *clk;
+ u32 preset;
+};
+
+enum stm32_count_function {
+ STM32_COUNT_FUNCTION_QUADRATURE_X2,
+ STM32_COUNT_FUNCTION_QUADRATURE_X4,
+};
+
+static enum count_function stm32_count_functions[] = {
+ [STM32_COUNT_FUNCTION_QUADRATURE_X2] = COUNT_FUNCTION_QUADRATURE_X2,
+ [STM32_COUNT_FUNCTION_QUADRATURE_X4] = COUNT_FUNCTION_QUADRATURE_X4
+};
+
+static int stm32_count_read(struct counter_device *counter,
+ struct counter_count *count,
+ struct count_read_value *val)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ u32 cnt;
+
+ regmap_read(priv->regmap, TIM_CNT, &cnt);
+ set_count_read_value(val, COUNT_POSITION_UNSIGNED, &cnt);
+
+ return 0;
+}
+
+static int stm32_count_write(struct counter_device *counter,
+ struct counter_count *count,
+ struct count_write_value *val)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ u32 cnt;
+ int err;
+
+ err = get_count_write_value(&cnt, COUNT_POSITION_UNSIGNED, val);
+ if (err)
+ return err;
+
+ if (cnt > priv->preset)
+ return -EINVAL;
+
+ return regmap_write(priv->regmap, TIM_CNT, cnt);
+}
+
+static int stm32_count_function_get(struct counter_device *counter,
+ struct counter_count *count,
+ size_t *function)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ u32 smcr;
+
+ regmap_read(priv->regmap, TIM_SMCR, &smcr);
+
+ switch (smcr & TIM_SMCR_SMS) {
+ case 1:
+ *function = STM32_COUNT_FUNCTION_QUADRATURE_X2;
+ return 0;
+ case 3:
+ *function = STM32_COUNT_FUNCTION_QUADRATURE_X4;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
+static int stm32_count_function_set(struct counter_device *counter,
+ struct counter_count *count,
+ size_t function)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ u32 cr1, sms;
+
+ switch (function) {
+ case STM32_COUNT_FUNCTION_QUADRATURE_X2:
+ sms = 1;
+ break;
+ case STM32_COUNT_FUNCTION_QUADRATURE_X4:
+ sms = 3;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ /* Store enable status */
+ regmap_read(priv->regmap, TIM_CR1, &cr1);
+
+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
+
+ /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
+ regmap_write(priv->regmap, TIM_ARR, priv->preset);
+
+ regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
+
+ /* Make sure that registers are updated */
+ regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
+
+ /* Restore the enable status */
+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
+
+ return 0;
+}
+
+static ssize_t stm32_count_direction_read(struct counter_device *counter,
+ struct counter_count *count,
+ void *private, char *buf)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ const char *direction;
+ u32 cr1;
+
+ regmap_read(priv->regmap, TIM_CR1, &cr1);
+ direction = (cr1 & TIM_CR1_DIR) ? "backward" : "forward";
+
+ return scnprintf(buf, PAGE_SIZE, "%s\n", direction);
+}
+
+static ssize_t stm32_count_preset_read(struct counter_device *counter,
+ struct counter_count *count,
+ void *private, char *buf)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ u32 arr;
+
+ regmap_read(priv->regmap, TIM_ARR, &arr);
+
+ return snprintf(buf, PAGE_SIZE, "%u\n", arr);
+}
+
+static ssize_t stm32_count_preset_write(struct counter_device *counter,
+ struct counter_count *count,
+ void *private,
+ const char *buf, size_t len)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ unsigned int preset;
+ int ret;
+
+ ret = kstrtouint(buf, 0, &preset);
+ if (ret)
+ return ret;
+
+ /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
+ regmap_write(priv->regmap, TIM_ARR, preset);
+
+ priv->preset = preset;
+ return len;
+}
+
+static ssize_t stm32_count_enable_read(struct counter_device *counter,
+ struct counter_count *count,
+ void *private, char *buf)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ u32 cr1;
+
+ regmap_read(priv->regmap, TIM_CR1, &cr1);
+
+ return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)(cr1 & TIM_CR1_CEN));
+}
+
+static ssize_t stm32_count_enable_write(struct counter_device *counter,
+ struct counter_count *count,
+ void *private,
+ const char *buf, size_t len)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ int err;
+ u32 cr1;
+ bool enable;
+
+ err = kstrtobool(buf, &enable);
+ if (err)
+ return err;
+
+ if (enable) {
+ regmap_read(priv->regmap, TIM_CR1, &cr1);
+ if (!(cr1 & TIM_CR1_CEN))
+ clk_enable(priv->clk);
+
+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
+ TIM_CR1_CEN);
+ } else {
+ regmap_read(priv->regmap, TIM_CR1, &cr1);
+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
+ if (cr1 & TIM_CR1_CEN)
+ clk_disable(priv->clk);
+ }
+
+ return len;
+}
+
+static const struct counter_count_ext stm32_count_ext[] = {
+ {
+ .name = "direction",
+ .read = stm32_count_direction_read,
+ },
+ {
+ .name = "enable",
+ .read = stm32_count_enable_read,
+ .write = stm32_count_enable_write
+ },
+ {
+ .name = "preset",
+ .read = stm32_count_preset_read,
+ .write = stm32_count_preset_write
+ },
+};
+
+enum stm32_synapse_action {
+ STM32_SYNAPSE_ACTION_RISING_EDGE = 0,
+ STM32_SYNAPSE_ACTION_BOTH_EDGES
+};
+
+static enum synapse_action stm32_synapse_actions[] = {
+ [STM32_SYNAPSE_ACTION_RISING_EDGE] = SYNAPSE_ACTION_RISING_EDGE,
+ [STM32_SYNAPSE_ACTION_BOTH_EDGES] = SYNAPSE_ACTION_BOTH_EDGES
+};
+
+static int stm32_action_get(struct counter_device *counter,
+ struct counter_count *count,
+ struct counter_synapse *synapse,
+ size_t *action)
+{
+ struct stm32_timer_cnt *const priv = counter->priv;
+ u32 smcr;
+
+ regmap_read(priv->regmap, TIM_SMCR, &smcr);
+
+ switch (smcr & TIM_SMCR_SMS) {
+ case 1:
+ *action = STM32_SYNAPSE_ACTION_RISING_EDGE;
+ return 0;
+ case 3:
+ *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
+static struct counter_signal stm32_signals[] = {
+ {
+ .id = 0,
+ .name = "Channel 1 Quadrature A"
+ },
+ {
+ .id = 1,
+ .name = "Channel 1 Quadrature B"
+ }
+};
+
+static struct counter_synapse stm32_count_synapses[] = {
+ {
+ .actions_list = stm32_synapse_actions,
+ .num_actions = ARRAY_SIZE(stm32_synapse_actions),
+ .signal = &stm32_signals[0]
+ },
+ {
+ .actions_list = stm32_synapse_actions,
+ .num_actions = ARRAY_SIZE(stm32_synapse_actions),
+ .signal = &stm32_signals[1]
+ }
+};
+
+static struct counter_count stm32_counts = {
+ .id = 0,
+ .name = "Channel 1 Count",
+ .functions_list = stm32_count_functions,
+ .num_functions = ARRAY_SIZE(stm32_count_functions),
+ .synapses = stm32_count_synapses,
+ .num_synapses = ARRAY_SIZE(stm32_count_synapses),
+ .ext = stm32_count_ext,
+ .num_ext = ARRAY_SIZE(stm32_count_ext)
+};
+
+static int stm32_timer_cnt_probe(struct platform_device *pdev)
+{
+ struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
+ struct device *dev = &pdev->dev;
+ struct stm32_timer_cnt *priv;
+
+ if (IS_ERR_OR_NULL(ddata))
+ return -EINVAL;
+
+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ priv->regmap = ddata->regmap;
+ priv->clk = ddata->clk;
+ priv->preset = ddata->max_arr;
+
+ priv->counter.name = dev_name(dev);
+ priv->counter.parent = dev;
+ priv->counter.count_read = stm32_count_read;
+ priv->counter.count_write = stm32_count_write;
+ priv->counter.function_get = stm32_count_function_get;
+ priv->counter.function_set = stm32_count_function_set;
+ priv->counter.action_get = stm32_action_get;
+ priv->counter.counts = &stm32_counts;
+ priv->counter.num_counts = 1;
+ priv->counter.signals = stm32_signals;
+ priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
+ priv->counter.priv = priv;
+
+ /* Register Counter device */
+ return devm_counter_register(dev, &priv->counter);
+}
+
+static const struct of_device_id stm32_timer_cnt_of_match[] = {
+ { .compatible = "st,stm32-timer-counter", },
+ {},
+};
+MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
+
+static struct platform_driver stm32_timer_cnt_driver = {
+ .probe = stm32_timer_cnt_probe,
+ .driver = {
+ .name = "stm32-timer-counter",
+ .of_match_table = stm32_timer_cnt_of_match,
+ },
+};
+module_platform_driver(stm32_timer_cnt_driver);
+
+MODULE_AUTHOR("Benjamin Gaignard <[email protected]>");
+MODULE_ALIAS("platform:stm32-timer-counter");
+MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
+MODULE_LICENSE("GPL v2");
--
2.16.2
From: Benjamin Gaignard <[email protected]>
In addition of the generic sysfs-bus-counter ABI stm32-timer-cnt
offerts three functionality:
- enable the counter
- set preset value
- allow to read counter direction
Signed-off-by: Benjamin Gaignard <[email protected]>
Signed-off-by: William Breathitt Gray <[email protected]>
---
.../ABI/testing/sysfs-bus-counter-stm32-timer-cnt | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt
diff --git a/Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt b/Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt
new file mode 100644
index 000000000000..73328e8e6d7e
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt
@@ -0,0 +1,21 @@
+What: /sys/bus/counter/devices/counterX/countY_direction
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Read-only attribute that indicates the count direction of
+ Count Y. Two count directions are available: Forward and
+ Backward.
+
+What: /sys/bus/counter/devices/counterX/countY_enable
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ Whether channel Y inputs A and B are enabled. Valid attribute
+ values are boolean.
+
+What: /sys/bus/counter/devices/counterX/countY_preset
+KernelVersion: 4.17
+Contact: [email protected]
+Description:
+ If the counter device supports preset registers, the preset
+ count for channel Y is provided by this attribute.
--
2.16.2
This patch adds high-level documentation about the Generic Counter
interface.
Signed-off-by: William Breathitt Gray <[email protected]>
---
Documentation/driver-api/generic-counter.rst | 321 +++++++++++++++++++++++++++
Documentation/driver-api/index.rst | 1 +
MAINTAINERS | 1 +
3 files changed, 323 insertions(+)
create mode 100644 Documentation/driver-api/generic-counter.rst
diff --git a/Documentation/driver-api/generic-counter.rst b/Documentation/driver-api/generic-counter.rst
new file mode 100644
index 000000000000..bce0cbc31963
--- /dev/null
+++ b/Documentation/driver-api/generic-counter.rst
@@ -0,0 +1,321 @@
+=========================
+Generic Counter Interface
+=========================
+
+Introduction
+============
+
+Counter devices are prevalent within a diverse spectrum of industries.
+The ubiquitous presence of these devices necessitates a common interface
+and standard of interaction and exposure. This driver API attempts to
+resolve the issue of duplicate code found among existing counter device
+drivers by introducing a generic counter interface for consumption. The
+Generic Counter interface enables drivers to support and expose a common
+set of components and functionality present in counter devices.
+
+Theory
+======
+
+Counter devices can vary greatly in design, but regardless of whether
+some devices are quadrature encoder counters or tally counters, all
+counter devices consist of a core set of components. This core set of
+components, shared by all counter devices, is what forms the essence of
+the Generic Counter interface.
+
+There are three core components to a counter:
+
+ COUNT
+ -----
+ A Count represents the count data for a set of Signals. The
+ Generic Counter interface provides the following available count
+ data types:
+
+ * COUNT_POSITION_UNSIGNED:
+ Unsigned integer value representing position.
+
+ * COUNT_POSITION_SIGNED:
+ Signed integer value representing position.
+
+ A Count has a count function mode which represents the update
+ behavior for the count data. The Generic Counter interface
+ provides the following available count function modes:
+
+ * Increase:
+ Accumulated count is incremented.
+
+ * Decrease:
+ Accumulated count is decremented.
+
+ * Pulse-Direction:
+ Rising edges on quadrature pair signal A updates
+ the respective count. The input level of
+ quadrature pair signal B determines direction.
+
+ * Quadrature x1:
+ If direction is forward, rising edges on
+ quadrature pair signal A updates the respective
+ count; if the direction is backward, falling
+ edges on quadrature pair signal A updates the
+ respective count. Quadrature encoding determines
+ the direction.
+
+ * Quadrature x2:
+ Any state transition on quadrature pair signal A
+ updates the respective count. Quadrature
+ encoding determines the direction.
+
+ * Quadrature x4:
+ Any state transition on either quadrature pair
+ signals updates the respective count. Quadrature
+ encoding determines the direction.
+
+ A Count has a set of one or more associated Signals.
+
+ SIGNAL
+ ------
+ A Signal represents a counter input data; this is the input data
+ that is analyzed by the counter to determine the count data;
+ e.g. a quadrature signal output line of a rotary encoder. Not
+ all counter devices provide user access to the Signal data.
+
+ The Generic Counter interface provides the following available
+ signal data types for when the Signal data is available for user
+ access:
+
+ * SIGNAL_LEVEL_LOW:
+ Signal line is in a low state.
+
+ * SIGNAL_LEVEL_HIGH:
+ Signal line is in a high state.
+
+ A Signal may be associated to one or more Counts.
+
+ SYNAPSE
+ -------
+ A Synapse represents the association of a Signal with a
+ respective Count. Signal data affects respective Count data, and
+ the Synapse represents this relationship.
+
+ The Synapse action mode specifies the Signal data condition
+ which triggers the respective Count's count function evaluation
+ to update the count data. The Generic Counter interface provides
+ the following available action modes:
+
+ * None:
+ Signal does not trigger the count function. In
+ Pulse-Direction count function mode, this Signal
+ is evaluated as Direction.
+ * Rising Edge:
+ Low state transitions to high state.
+ * Falling Edge:
+ High state transitions to low state.
+ * Both Edges:
+ Any state transition.
+
+
+A counter is defined as a set of input signals associated to count data
+that are generated by the evaluation of the state of the associated
+input signals as defined by the respective count functions. Within the
+context of the Generic Counter interface, a counter consists of Counts
+each associated to a set of Signals, whose respective Synapse instances
+represent the count function update conditions for the associated
+Counts.
+
+Paradigm
+========
+
+The most basic counter device may be expressed as a single Count
+associated with a single Signal via a single Synapse. Take for example
+a counter device which simply accumulates a count of rising edges on a
+source input line.
+
+ Count Synapse Signal
+ ----- ------- ------
++---------------------+
+| Data: Count | Rising Edge ________
+| Function: Increase | <------------- / Source \
+| | ____________
++---------------------+
+
+In this example, the Signal is a source input line with a pulsing
+voltage, while the Count is a persistent count value which is repeatedly
+incremented. The Signal is associated with the respective Count via a
+Synapse. The increase function is triggered by the Signal data condition
+specified by the Synapse -- in this case a rising edge condition on the
+voltage input line. In summary, the counter device existence and
+behavior is aptly represented by respective Count, Signal, and Synapse
+components: a rising edge condition triggers an increase function on an
+accumulating count datum.
+
+A counter device is not limited to a single Signal; in fact, in theory
+many Signals may be associated with even a single Count. For example, a
+quadrature encoder counter device can keep track of position based on
+the states of two input lines.
+
+ Count Synapse Signal
+ ----- ------- ------
++-------------------------+
+| Data: Position | Both Edges ___
+| Function: Quadrature x4 | <------------ / A \
+| | _______
+| |
+| | Both Edges ___
+| | <------------ / B \
+| | _______
++-------------------------+
+
+In this example, two Signals (quadrature encoder lines A and B) are
+associated to a single Count: a rising or falling edge on either A or B
+triggers the "Quadrature x4" function which determines the direction of
+movement and updates the respective position data. The "Quadrature x4"
+function is likely implemented in the hardware of the quadrature encoder
+counter device; the Count, Signals, and Synapses simply represent this
+hardware behavior and functionality.
+
+Signals associated to the same Count can have differing Synapse action
+mode conditions. For example, a quadrature encoder counter device
+operating in a non-quadrature Pulse-Direction mode could have one input
+line dedicated for movement and a second input line dedicated for
+direction.
+
+ Count Synapse Signal
+ ----- ------- ------
++---------------------------+
+| Data: Position | Rising Edge ___
+| Function: Pulse-Direction | <------------- / A \ (Movement)
+| | _______
+| |
+| | None ___
+| | <------------- / B \ (Direction)
+| | _______
++---------------------------+
+
+Only Signal A triggers the "Pulse-Direction" update function, but the
+instantaneous state of Signal B is still required in order to know the
+direction so that the position data may be properly updated. Ultimately,
+both Signals are associated to the same Count via two respective
+Synapses, but only one Synapse has an active action mode condition which
+triggers the respective count function while the other is left with a
+"None" condition action mode to indicate its respective Signal's
+availability for state evaluation despite its non-triggering mode.
+
+Keep in mind that the Signal, Synapse, and Count are abstract
+representations which do not need to be closely married to their
+respective physical sources. This allows the user of a counter to
+divorce themselves from the nuances of physical components (such as
+whether an input line is differential or single-ended) and instead focus
+on the core idea of what the data and process represent (e.g. position
+as interpreted from quadrature encoding data).
+
+Userspace Interface
+===================
+
+Several sysfs attributes are generated by the Generic Counter interface,
+and reside under the /sys/bus/counter/devices/counterX directory, where
+counterX refers to the respective counter device. Please see
+Documentation/ABI/testing/sys-bus-counter-generic-sysfs for detailed
+information on each Generic Counter interface sysfs attribute.
+
+Through these sysfs attributes, programs and scripts may interact with
+the Generic Counter paradigm Counts, Signals, and Synapses of respective
+counter devices.
+
+Driver API
+==========
+
+Driver authors may utilize the Generic Counter interface in their code
+by including the include/linux/iio/counter.h header file. This header
+file provides several core data structures, function prototypes, and
+macros for defining a counter device.
+
+.. kernel-doc:: include/linux/counter.h
+ :internal:
+
+.. kernel-doc:: drivers/counter/generic-counter.c
+ :export:
+
+Implementation
+==============
+
+To support a counter device, a driver must first allocate the available
+Counter Signals via counter_signal structures. These Signals should
+be stored as an array and set to the signals array member of an
+allocated counter_device structure before the Counter is registered to
+the system.
+
+Counter Counts may be allocated via counter_count structures, and
+respective Counter Signal associations (Synapses) made via
+counter_synapse structures. Associated counter_synapse structures are
+stored as an array and set to the the synapses array member of the
+respective counter_count structure. These counter_count structures are
+set to the counts array member of an allocated counter_device structure
+before the Counter is registered to the system.
+
+Driver callbacks should be provided to the counter_device structure in
+order to communicate with the device: to read and write various Signals
+and Counts, and to set and get the "action mode" and "function mode" for
+various Synapses and Counts respectively.
+
+A defined counter_device structure may be registered to the system by
+passing it to the counter_register function, and unregistered by passing
+it to the counter_unregister function. Similarly, the
+devm_counter_register and devm_counter_unregister functions may be used
+if device memory-managed registration is desired.
+
+Extension sysfs attributes can be created for auxiliary functionality
+and data by passing in defined counter_device_ext, counter_count_ext,
+and counter_signal_ext structures. In these cases, the
+counter_device_ext structure is used for global configuration of the
+respective Counter device, while the counter_count_ext and
+counter_signal_ext structures allow for auxiliary exposure and
+configuration of a specific Count or Signal respectively.
+
+Architecture
+============
+
+When the Generic Counter interface counter module is loaded, the
+counter_init function is called which registers a bus_type named
+"counter" to the system. Subsequently, when the module is unloaded, the
+counter_exit function is called which unregisters the bus_type named
+"counter" from the system.
+
+Counter devices are registered to the system via the counter_register
+function, and later removed via the counter_unregister function. The
+counter_register function establishes a unique ID for the Counter
+device and creates a respective sysfs directory, where X is the
+mentioned unique ID:
+
+ /sys/bus/counter/devices/counterX
+
+Sysfs attributes are created within the counterX directory to expose
+functionality, configurations, and data relating to the Counts, Signals,
+and Synapses of the Counter device, as well as options and information
+for the Counter device itself.
+
+Each Signal has a directory created to house its relevant sysfs
+attributes, where Y is the unique ID of the respective Signal:
+
+ /sys/bus/counter/devices/counterX/signalY
+
+Similarly, each Count has a directory created to house its relevant
+sysfs attributes, where Y is the unique ID of the respective Count:
+
+ /sys/bus/counter/devices/counterX/countY
+
+For a more detailed breakdown of the available Generic Counter interface
+sysfs attributes, please refer to the
+Documentation/ABI/testing/sys-bus-counter file.
+
+The Signals and Counts associated with the Counter device are registered
+to the system as well by the counter_register function. The
+signal_read/signal_write driver callbacks are associated to their
+respective Signal attributes, while the count_read/count_write and
+function_get/function_set driver callbacks are associated to their
+respective Count attributes; similarly, the same is true for the
+action_get/action_set driver callbacks and their respective Synapse
+attributes. If a driver callback is left undefined, then the respective
+read/write permission is left disabled for the relevant attributes.
+
+Similarly, extension sysfs attributes are created for the defined
+counter_device_ext, counter_count_ext, and counter_signal_ext
+structures that are passed in.
diff --git a/Documentation/driver-api/index.rst b/Documentation/driver-api/index.rst
index e9b41b1634f3..57e6e2c1d063 100644
--- a/Documentation/driver-api/index.rst
+++ b/Documentation/driver-api/index.rst
@@ -25,6 +25,7 @@ available subsections can be seen below.
frame-buffer
regulator
iio/index
+ generic-counter
input
usb/index
pci
diff --git a/MAINTAINERS b/MAINTAINERS
index 2a7bf2f84272..a71dff6eae87 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3673,6 +3673,7 @@ M: William Breathitt Gray <[email protected]>
L: [email protected]
S: Maintained
F: Documentation/ABI/testing/sysfs-bus-counter*
+F: Documentation/driver-api/generic-counter.rst
F: drivers/counter/
F: include/linux/counter.h
--
2.16.2
On 03/09/2018 10:42 AM, William Breathitt Gray wrote:
> This patch adds high-level documentation about the Generic Counter
> interface.
>
> Signed-off-by: William Breathitt Gray <[email protected]>
> ---
> Documentation/driver-api/generic-counter.rst | 321 +++++++++++++++++++++++++++
> Documentation/driver-api/index.rst | 1 +
> MAINTAINERS | 1 +
> 3 files changed, 323 insertions(+)
> create mode 100644 Documentation/driver-api/generic-counter.rst
>
> diff --git a/Documentation/driver-api/generic-counter.rst b/Documentation/driver-api/generic-counter.rst
> new file mode 100644
> index 000000000000..bce0cbc31963
> --- /dev/null
> +++ b/Documentation/driver-api/generic-counter.rst
> @@ -0,0 +1,321 @@
[snip]
> +There are three core components to a counter:
> +
> + COUNT
> + -----
> + A Count represents the count data for a set of Signals. The
> + Generic Counter interface provides the following available count
> + data types:
> +
> + * COUNT_POSITION_UNSIGNED:
> + Unsigned integer value representing position.
> +
> + * COUNT_POSITION_SIGNED:
> + Signed integer value representing position.
> +
> + A Count has a count function mode which represents the update
> + behavior for the count data. The Generic Counter interface
> + provides the following available count function modes:
> +
> + * Increase:
> + Accumulated count is incremented.
> +
> + * Decrease:
> + Accumulated count is decremented.
> +
> + * Pulse-Direction:
> + Rising edges on quadrature pair signal A updates
> + the respective count. The input level of
> + quadrature pair signal B determines direction.
> +
> + * Quadrature x1:
> + If direction is forward, rising edges on
> + quadrature pair signal A updates the respective
> + count; if the direction is backward, falling
> + edges on quadrature pair signal A updates the
> + respective count. Quadrature encoding determines
> + the direction.
> +
> + * Quadrature x2:
> + Any state transition on quadrature pair signal A
> + updates the respective count. Quadrature
> + encoding determines the direction.
> +
> + * Quadrature x4:
> + Any state transition on either quadrature pair
> + signals updates the respective count. Quadrature
change <TAB> ^^^ to <SPACE>
> + encoding determines the direction.
> +
> + A Count has a set of one or more associated Signals.
> +
> + SIGNAL
> + ------
> + A Signal represents a counter input data; this is the input data
> + that is analyzed by the counter to determine the count data;
> + e.g. a quadrature signal output line of a rotary encoder. Not
> + all counter devices provide user access to the Signal data.
> +
> + The Generic Counter interface provides the following available
> + signal data types for when the Signal data is available for user
> + access:
> +
> + * SIGNAL_LEVEL_LOW:
> + Signal line is in a low state.
> +
> + * SIGNAL_LEVEL_HIGH:
> + Signal line is in a high state.
> +
> + A Signal may be associated to one or more Counts.
with (?)
Hm, there are around 8 or so instances of "associated to" here -- and at least
one of "associated with" (to my surprise :). But it's no big deal.
Reviewed-by: Randy Dunlap <[email protected]>
thanks,
--
~Randy
On Fri, 9 Mar 2018 13:43:06 -0500
William Breathitt Gray <[email protected]> wrote:
> This patch adds support for the Generic Counter interface to the
> 104-QUAD-8 driver. The existing 104-QUAD-8 device interface should not
> be affected by this patch; all changes are intended as supplemental
> additions as perceived by the user.
>
> Generic Counter Counts are created for the eight quadrature channel
> counts, as well as their respective quadrature A and B Signals (which
> are associated via respective Synapse structures) and respective index
> Signals.
>
> The new Generic Counter interface sysfs attributes are intended to
> expose the same functionality and data available via the existing
> 104-QUAD-8 IIO device interface; the Generic Counter interface serves
> to provide the respective functionality and data in a standard way
> expected of counter devices.
>
> Signed-off-by: William Breathitt Gray <[email protected]>
We should definitely think about deprecating the existing IIO interface
as soon as we can (could be a few years though!)
One suggestion inline that you should add an ops structure for the
counter callback functions so you can take them static constant.
I missed that when reviewing the earlier patch.
Jonathan
> ---
> MAINTAINERS | 4 +-
> drivers/counter/104-quad-8.c | 685 ++++++++++++++++++++++++++++++++++++++++++-
> 2 files changed, 677 insertions(+), 12 deletions(-)
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index a71dff6eae87..febe27a9962e 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -266,12 +266,12 @@ L: [email protected]
> S: Maintained
> F: drivers/gpio/gpio-104-idio-16.c
>
> -ACCES 104-QUAD-8 IIO DRIVER
> +ACCES 104-QUAD-8 DRIVER
> M: William Breathitt Gray <[email protected]>
> L: [email protected]
> S: Maintained
> F: Documentation/ABI/testing/sysfs-bus-iio-counter-104-quad-8
> -F: drivers/iio/counter/104-quad-8.c
> +F: drivers/counter/104-quad-8.c
>
> ACCES PCI-IDIO-16 GPIO DRIVER
> M: William Breathitt Gray <[email protected]>
> diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
> index b56985078d8c..5db314fcb08d 100644
> --- a/drivers/counter/104-quad-8.c
> +++ b/drivers/counter/104-quad-8.c
> @@ -1,3 +1,4 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> /*
> * IIO driver for the ACCES 104-QUAD-8
> * Copyright (C) 2016 William Breathitt Gray
> @@ -14,6 +15,7 @@
> * This driver supports the ACCES 104-QUAD-8 and ACCES 104-QUAD-4.
> */
> #include <linux/bitops.h>
> +#include <linux/counter.h>
> #include <linux/device.h>
> #include <linux/errno.h>
> #include <linux/iio/iio.h>
> @@ -37,6 +39,7 @@ MODULE_PARM_DESC(base, "ACCES 104-QUAD-8 base addresses");
>
> /**
> * struct quad8_iio - IIO device private data structure
> + * @counter: instance of the counter_device
> * @preset: array of preset values
> * @count_mode: array of count mode configurations
> * @quadrature_mode: array of quadrature mode configurations
> @@ -48,6 +51,7 @@ MODULE_PARM_DESC(base, "ACCES 104-QUAD-8 base addresses");
> * @base: base port address of the IIO device
> */
> struct quad8_iio {
> + struct counter_device counter;
> unsigned int preset[QUAD8_NUM_COUNTERS];
> unsigned int count_mode[QUAD8_NUM_COUNTERS];
> unsigned int quadrature_mode[QUAD8_NUM_COUNTERS];
> @@ -527,24 +531,665 @@ static const struct iio_chan_spec quad8_channels[] = {
> QUAD8_COUNT_CHAN(7), QUAD8_INDEX_CHAN(7)
> };
>
> +static int quad8_signal_read(struct counter_device *counter,
> + struct counter_signal *signal, struct signal_read_value *val)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> + unsigned int state;
> + enum signal_level level;
> +
> + /* Only Index signal levels can be read */
> + if (signal->id < 16)
> + return -EINVAL;
> +
> + state = inb(priv->base + 0x16) & BIT(signal->id - 16);
> +
> + level = (state) ? SIGNAL_LEVEL_HIGH : SIGNAL_LEVEL_LOW;
> +
> + set_signal_read_value(val, SIGNAL_LEVEL, &level);
> +
> + return 0;
> +}
> +
> +static int quad8_count_read(struct counter_device *counter,
> + struct counter_count *count, struct count_read_value *val)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> + const int base_offset = priv->base + 2 * count->id;
> + unsigned int flags;
> + unsigned int borrow;
> + unsigned int carry;
> + unsigned long position;
> + int i;
> +
> + flags = inb(base_offset + 1);
> + borrow = flags & BIT(0);
> + carry = !!(flags & BIT(1));
> +
> + /* Borrow XOR Carry effectively doubles count range */
> + position = (unsigned long)(borrow ^ carry) << 24;
> +
> + /* Reset Byte Pointer; transfer Counter to Output Latch */
> + outb(0x11, base_offset + 1);
> +
> + for (i = 0; i < 3; i++)
> + position |= (unsigned long)inb(base_offset) << (8 * i);
> +
> + set_count_read_value(val, COUNT_POSITION_UNSIGNED, &position);
> +
> + return 0;
> +}
> +
> +static int quad8_count_write(struct counter_device *counter,
> + struct counter_count *count, struct count_write_value *val)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> + const int base_offset = priv->base + 2 * count->id;
> + int err;
> + unsigned long position;
> + int i;
> +
> + err = get_count_write_value(&position, COUNT_POSITION_UNSIGNED, val);
> + if (err)
> + return err;
> +
> + /* Only 24-bit values are supported */
> + if (position > 0xFFFFFF)
> + return -EINVAL;
> +
> + /* Reset Byte Pointer */
> + outb(0x01, base_offset + 1);
> +
> + /* Counter can only be set via Preset Register */
> + for (i = 0; i < 3; i++)
> + outb(position >> (8 * i), base_offset);
> +
> + /* Transfer Preset Register to Counter */
> + outb(0x08, base_offset + 1);
> +
> + /* Reset Byte Pointer */
> + outb(0x01, base_offset + 1);
> +
> + /* Set Preset Register back to original value */
> + position = priv->preset[count->id];
> + for (i = 0; i < 3; i++)
> + outb(position >> (8 * i), base_offset);
> +
> + /* Reset Borrow, Carry, Compare, and Sign flags */
> + outb(0x02, base_offset + 1);
> + /* Reset Error flag */
> + outb(0x06, base_offset + 1);
> +
> + return 0;
> +}
> +
> +enum quad8_count_function {
> + QUAD8_COUNT_FUNCTION_PULSE_DIRECTION = 0,
> + QUAD8_COUNT_FUNCTION_QUADRATURE_X1,
> + QUAD8_COUNT_FUNCTION_QUADRATURE_X2,
> + QUAD8_COUNT_FUNCTION_QUADRATURE_X4
> +};
> +
> +static enum count_function quad8_count_functions_list[] = {
> + [QUAD8_COUNT_FUNCTION_PULSE_DIRECTION] = COUNT_FUNCTION_PULSE_DIRECTION,
> + [QUAD8_COUNT_FUNCTION_QUADRATURE_X1] = COUNT_FUNCTION_QUADRATURE_X1,
> + [QUAD8_COUNT_FUNCTION_QUADRATURE_X2] = COUNT_FUNCTION_QUADRATURE_X2,
> + [QUAD8_COUNT_FUNCTION_QUADRATURE_X4] = COUNT_FUNCTION_QUADRATURE_X4
> +};
> +
> +static int quad8_function_get(struct counter_device *counter,
> + struct counter_count *count, size_t *function)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> + const int id = count->id;
> + const unsigned int quadrature_mode = priv->quadrature_mode[id];
> + const unsigned int scale = priv->quadrature_scale[id];
> +
> + if (quadrature_mode)
> + switch (scale) {
> + case 0:
> + *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X1;
> + break;
> + case 1:
> + *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X2;
> + break;
> + case 2:
> + *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X4;
> + break;
> + }
> + else
> + *function = QUAD8_COUNT_FUNCTION_PULSE_DIRECTION;
> +
> + return 0;
> +}
> +
> +static int quad8_function_set(struct counter_device *counter,
> + struct counter_count *count, size_t function)
> +{
> + struct quad8_iio *const priv = counter->priv;
> + const int id = count->id;
> + unsigned int *const quadrature_mode = priv->quadrature_mode + id;
> + unsigned int *const scale = priv->quadrature_scale + id;
> + unsigned int mode_cfg = priv->count_mode[id] << 1;
> + unsigned int *const synchronous_mode = priv->synchronous_mode + id;
> + const unsigned int idr_cfg = priv->index_polarity[id] << 1;
> + const int base_offset = priv->base + 2 * id + 1;
> +
> + if (function == QUAD8_COUNT_FUNCTION_PULSE_DIRECTION) {
> + *quadrature_mode = 0;
> +
> + /* Quadrature scaling only available in quadrature mode */
> + *scale = 0;
> +
> + /* Synchronous function not supported in non-quadrature mode */
> + if (*synchronous_mode) {
> + *synchronous_mode = 0;
> + /* Disable synchronous function mode */
> + outb(0x60 | idr_cfg, base_offset);
> + }
> + } else {
> + *quadrature_mode = 1;
> +
> + switch (function) {
> + case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
> + *scale = 0;
> + mode_cfg |= 0x8;
> + break;
> + case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
> + *scale = 1;
> + mode_cfg |= 0x10;
> + break;
> + case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
> + *scale = 2;
> + mode_cfg |= 0x18;
> + break;
> + }
> + }
> +
> + /* Load mode configuration to Counter Mode Register */
> + outb(0x20 | mode_cfg, base_offset);
> +
> + return 0;
> +}
> +
> +enum count_direction {
> + COUNT_DIRECTION_FORWARD = 0,
> + COUNT_DIRECTION_BACKWARD
> +};
> +
> +static void quad8_direction_get(struct counter_device *counter,
> + struct counter_count *count, enum count_direction *direction)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> + unsigned int ud_flag;
> + const unsigned int flag_addr = priv->base + 2 * count->id + 1;
> +
> + /* U/D flag: nonzero = up, zero = down */
> + ud_flag = inb(flag_addr) & BIT(5);
> +
> + *direction = (ud_flag) ? COUNT_DIRECTION_FORWARD :
> + COUNT_DIRECTION_BACKWARD;
> +}
> +
> +enum quad8_synapse_action {
> + QUAD8_SYNAPSE_ACTION_NONE = 0,
> + QUAD8_SYNAPSE_ACTION_RISING_EDGE,
> + QUAD8_SYNAPSE_ACTION_FALLING_EDGE,
> + QUAD8_SYNAPSE_ACTION_BOTH_EDGES
> +};
> +
> +static enum synapse_action quad8_index_actions_list[] = {
> + [QUAD8_SYNAPSE_ACTION_NONE] = SYNAPSE_ACTION_NONE,
> + [QUAD8_SYNAPSE_ACTION_RISING_EDGE] = SYNAPSE_ACTION_RISING_EDGE
> +};
> +
> +static enum synapse_action quad8_synapse_actions_list[] = {
> + [QUAD8_SYNAPSE_ACTION_NONE] = SYNAPSE_ACTION_NONE,
> + [QUAD8_SYNAPSE_ACTION_RISING_EDGE] = SYNAPSE_ACTION_RISING_EDGE,
> + [QUAD8_SYNAPSE_ACTION_FALLING_EDGE] = SYNAPSE_ACTION_FALLING_EDGE,
> + [QUAD8_SYNAPSE_ACTION_BOTH_EDGES] = SYNAPSE_ACTION_BOTH_EDGES
> +};
> +
> +static int quad8_action_get(struct counter_device *counter,
> + struct counter_count *count, struct counter_synapse *synapse,
> + size_t *action)
> +{
> + struct quad8_iio *const priv = counter->priv;
> + int err;
> + size_t function;
> + const size_t signal_a_id = count->synapses[0].signal->id;
> + enum count_direction direction;
> +
> + /* Handle Index signals */
> + if (synapse->signal->id >= 16) {
> + if (priv->preset_enable[count->id])
> + *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
> + else
> + *action = QUAD8_SYNAPSE_ACTION_NONE;
> +
> + return 0;
> + }
> +
> + err = counter->function_get(counter, count, &function);
> + if (err)
> + return err;
> +
> + /* Default action mode */
> + *action = QUAD8_SYNAPSE_ACTION_NONE;
> +
> + /* Determine action mode based on current count function mode */
> + switch (function) {
> + case QUAD8_COUNT_FUNCTION_PULSE_DIRECTION:
> + if (synapse->signal->id == signal_a_id)
> + *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
> + break;
> + case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
> + if (synapse->signal->id == signal_a_id) {
> + quad8_direction_get(counter, count, &direction);
> +
> + if (direction == COUNT_DIRECTION_FORWARD)
> + *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
> + else
> + *action = QUAD8_SYNAPSE_ACTION_FALLING_EDGE;
> + }
> + break;
> + case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
> + if (synapse->signal->id == signal_a_id)
> + *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
> + break;
> + case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
> + *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
> + break;
> + }
> +
> + return 0;
> +}
> +
> +static int quad8_index_polarity_get(struct counter_device *counter,
> + struct counter_signal *signal, size_t *index_polarity)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> + const size_t channel_id = signal->id - 16;
> +
> + *index_polarity = priv->index_polarity[channel_id];
> +
> + return 0;
> +}
> +
> +static int quad8_index_polarity_set(struct counter_device *counter,
> + struct counter_signal *signal, size_t index_polarity)
> +{
> + struct quad8_iio *const priv = counter->priv;
> + const size_t channel_id = signal->id - 16;
> + const unsigned int idr_cfg = priv->synchronous_mode[channel_id] |
> + index_polarity << 1;
> + const int base_offset = priv->base + 2 * channel_id + 1;
> +
> + priv->index_polarity[channel_id] = index_polarity;
> +
> + /* Load Index Control configuration to Index Control Register */
> + outb(0x60 | idr_cfg, base_offset);
> +
> + return 0;
> +}
> +
> +static struct counter_signal_enum_ext quad8_index_pol_enum = {
> + .items = quad8_index_polarity_modes,
> + .num_items = ARRAY_SIZE(quad8_index_polarity_modes),
> + .get = quad8_index_polarity_get,
> + .set = quad8_index_polarity_set
> +};
> +
> +static int quad8_synchronous_mode_get(struct counter_device *counter,
> + struct counter_signal *signal, size_t *synchronous_mode)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> + const size_t channel_id = signal->id - 16;
> +
> + *synchronous_mode = priv->synchronous_mode[channel_id];
> +
> + return 0;
> +}
> +
> +static int quad8_synchronous_mode_set(struct counter_device *counter,
> + struct counter_signal *signal, size_t synchronous_mode)
> +{
> + struct quad8_iio *const priv = counter->priv;
> + const size_t channel_id = signal->id - 16;
> + const unsigned int idr_cfg = synchronous_mode |
> + priv->index_polarity[channel_id] << 1;
> + const int base_offset = priv->base + 2 * channel_id + 1;
> +
> + /* Index function must be non-synchronous in non-quadrature mode */
> + if (synchronous_mode && !priv->quadrature_mode[channel_id])
> + return -EINVAL;
> +
> + priv->synchronous_mode[channel_id] = synchronous_mode;
> +
> + /* Load Index Control configuration to Index Control Register */
> + outb(0x60 | idr_cfg, base_offset);
> +
> + return 0;
> +}
> +
> +static struct counter_signal_enum_ext quad8_syn_mode_enum = {
> + .items = quad8_synchronous_modes,
> + .num_items = ARRAY_SIZE(quad8_synchronous_modes),
> + .get = quad8_synchronous_mode_get,
> + .set = quad8_synchronous_mode_set
> +};
> +
> +static int quad8_count_mode_get(struct counter_device *counter,
> + struct counter_count *count, size_t *count_mode)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> +
> + *count_mode = priv->count_mode[count->id];
> +
> + return 0;
> +}
> +
> +static int quad8_count_mode_set(struct counter_device *counter,
> + struct counter_count *count, size_t count_mode)
> +{
> + struct quad8_iio *const priv = counter->priv;
> + unsigned int mode_cfg = count_mode << 1;
> + const int base_offset = priv->base + 2 * count->id + 1;
> +
> + priv->count_mode[count->id] = count_mode;
> +
> + /* Add quadrature mode configuration */
> + if (priv->quadrature_mode[count->id])
> + mode_cfg |= (priv->quadrature_scale[count->id] + 1) << 3;
> +
> + /* Load mode configuration to Counter Mode Register */
> + outb(0x20 | mode_cfg, base_offset);
> +
> + return 0;
> +}
> +
> +static struct counter_count_enum_ext quad8_cnt_mode_enum = {
> + .items = quad8_count_modes,
> + .num_items = ARRAY_SIZE(quad8_count_modes),
> + .get = quad8_count_mode_get,
> + .set = quad8_count_mode_set
> +};
> +
> +static const char *const quad8_count_direction_str[] = {
> + [COUNT_DIRECTION_FORWARD] = "forward",
> + [COUNT_DIRECTION_BACKWARD] = "backward"
> +};
> +
> +static ssize_t quad8_count_direction_read(struct counter_device *counter,
> + struct counter_count *count, void *priv, char *buf)
> +{
> + enum count_direction direction;
> +
> + quad8_direction_get(counter, count, &direction);
> +
> + return scnprintf(buf, PAGE_SIZE, "%s\n",
> + quad8_count_direction_str[direction]);
> +}
> +
> +static ssize_t quad8_count_enable_read(struct counter_device *counter,
> + struct counter_count *count, void *private, char *buf)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> +
> + return scnprintf(buf, PAGE_SIZE, "%u\n", priv->ab_enable[count->id]);
> +}
> +
> +static ssize_t quad8_count_enable_write(struct counter_device *counter,
> + struct counter_count *count, void *private, const char *buf, size_t len)
> +{
> + struct quad8_iio *const priv = counter->priv;
> + const int base_offset = priv->base + 2 * count->id;
> + int err;
> + bool ab_enable;
> + unsigned int ior_cfg;
> +
> + err = kstrtobool(buf, &ab_enable);
> + if (err)
> + return err;
> +
> + priv->ab_enable[count->id] = ab_enable;
> +
> + ior_cfg = ab_enable | priv->preset_enable[count->id] << 1;
> +
> + /* Load I/O control configuration */
> + outb(0x40 | ior_cfg, base_offset + 1);
> +
> + return len;
> +}
> +
> +static int quad8_noise_error_get(struct counter_device *counter,
> + struct counter_count *count, size_t *noise_error)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> + const int base_offset = priv->base + 2 * count->id + 1;
> +
> + *noise_error = !!(inb(base_offset) & BIT(4));
> +
> + return 0;
> +}
> +
> +static struct counter_count_enum_ext quad8_noise_err_enum = {
> + .items = quad8_noise_error_states,
> + .num_items = ARRAY_SIZE(quad8_noise_error_states),
> + .get = quad8_noise_error_get
> +};
> +
> +static ssize_t quad8_count_preset_read(struct counter_device *counter,
> + struct counter_count *count, void *private, char *buf)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> +
> + return snprintf(buf, PAGE_SIZE, "%u\n", priv->preset[count->id]);
> +}
> +
> +static ssize_t quad8_count_preset_write(struct counter_device *counter,
> + struct counter_count *count, void *private, const char *buf, size_t len)
> +{
> + struct quad8_iio *const priv = counter->priv;
> + const int base_offset = priv->base + 2 * count->id;
> + unsigned int preset;
> + int ret;
> + int i;
> +
> + ret = kstrtouint(buf, 0, &preset);
> + if (ret)
> + return ret;
> +
> + /* Only 24-bit values are supported */
> + if (preset > 0xFFFFFF)
> + return -EINVAL;
> +
> + priv->preset[count->id] = preset;
> +
> + /* Reset Byte Pointer */
> + outb(0x01, base_offset + 1);
> +
> + /* Set Preset Register */
> + for (i = 0; i < 3; i++)
> + outb(preset >> (8 * i), base_offset);
> +
> + return len;
> +}
> +
> +static ssize_t quad8_count_preset_enable_read(struct counter_device *counter,
> + struct counter_count *count, void *private, char *buf)
> +{
> + const struct quad8_iio *const priv = counter->priv;
> +
> + return snprintf(buf, PAGE_SIZE, "%u\n",
> + !priv->preset_enable[count->id]);
> +}
> +
> +static ssize_t quad8_count_preset_enable_write(struct counter_device *counter,
> + struct counter_count *count, void *private, const char *buf, size_t len)
> +{
> + struct quad8_iio *const priv = counter->priv;
> + const int base_offset = priv->base + 2 * count->id + 1;
> + bool preset_enable;
> + int ret;
> + unsigned int ior_cfg;
> +
> + ret = kstrtobool(buf, &preset_enable);
> + if (ret)
> + return ret;
> +
> + /* Preset enable is active low in Input/Output Control register */
> + preset_enable = !preset_enable;
> +
> + priv->preset_enable[count->id] = preset_enable;
> +
> + ior_cfg = priv->ab_enable[count->id] | (unsigned int)preset_enable << 1;
> +
> + /* Load I/O control configuration to Input / Output Control Register */
> + outb(0x40 | ior_cfg, base_offset);
> +
> + return len;
> +}
> +
> +static const struct counter_signal_ext quad8_index_ext[] = {
> + COUNTER_SIGNAL_ENUM("index_polarity", &quad8_index_pol_enum),
> + COUNTER_SIGNAL_ENUM_AVAILABLE("index_polarity", &quad8_index_pol_enum),
> + COUNTER_SIGNAL_ENUM("synchronous_mode", &quad8_syn_mode_enum),
> + COUNTER_SIGNAL_ENUM_AVAILABLE("synchronous_mode", &quad8_syn_mode_enum)
> +};
> +
> +#define QUAD8_QUAD_SIGNAL(_id, _name) { \
> + .id = (_id), \
> + .name = (_name) \
> +}
> +
> +#define QUAD8_INDEX_SIGNAL(_id, _name) { \
> + .id = (_id), \
> + .name = (_name), \
> + .ext = quad8_index_ext, \
> + .num_ext = ARRAY_SIZE(quad8_index_ext) \
> +}
> +
> +static struct counter_signal quad8_signals[] = {
> + QUAD8_QUAD_SIGNAL(0, "Channel 1 Quadrature A"),
> + QUAD8_QUAD_SIGNAL(1, "Channel 1 Quadrature B"),
> + QUAD8_QUAD_SIGNAL(2, "Channel 2 Quadrature A"),
> + QUAD8_QUAD_SIGNAL(3, "Channel 2 Quadrature B"),
> + QUAD8_QUAD_SIGNAL(4, "Channel 3 Quadrature A"),
> + QUAD8_QUAD_SIGNAL(5, "Channel 3 Quadrature B"),
> + QUAD8_QUAD_SIGNAL(6, "Channel 4 Quadrature A"),
> + QUAD8_QUAD_SIGNAL(7, "Channel 4 Quadrature B"),
> + QUAD8_QUAD_SIGNAL(8, "Channel 5 Quadrature A"),
> + QUAD8_QUAD_SIGNAL(9, "Channel 5 Quadrature B"),
> + QUAD8_QUAD_SIGNAL(10, "Channel 6 Quadrature A"),
> + QUAD8_QUAD_SIGNAL(11, "Channel 6 Quadrature B"),
> + QUAD8_QUAD_SIGNAL(12, "Channel 7 Quadrature A"),
> + QUAD8_QUAD_SIGNAL(13, "Channel 7 Quadrature B"),
> + QUAD8_QUAD_SIGNAL(14, "Channel 8 Quadrature A"),
> + QUAD8_QUAD_SIGNAL(15, "Channel 8 Quadrature B"),
> + QUAD8_INDEX_SIGNAL(16, "Channel 1 Index"),
> + QUAD8_INDEX_SIGNAL(17, "Channel 2 Index"),
> + QUAD8_INDEX_SIGNAL(18, "Channel 3 Index"),
> + QUAD8_INDEX_SIGNAL(19, "Channel 4 Index"),
> + QUAD8_INDEX_SIGNAL(20, "Channel 5 Index"),
> + QUAD8_INDEX_SIGNAL(21, "Channel 6 Index"),
> + QUAD8_INDEX_SIGNAL(22, "Channel 7 Index"),
> + QUAD8_INDEX_SIGNAL(23, "Channel 8 Index")
> +};
> +
> +#define QUAD8_COUNT_SYNAPSES(_id) { \
> + { \
> + .actions_list = quad8_synapse_actions_list, \
> + .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
> + .signal = quad8_signals + 2 * (_id) \
> + }, \
> + { \
> + .actions_list = quad8_synapse_actions_list, \
> + .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
> + .signal = quad8_signals + 2 * (_id) + 1 \
> + }, \
> + { \
> + .actions_list = quad8_index_actions_list, \
> + .num_actions = ARRAY_SIZE(quad8_index_actions_list), \
> + .signal = quad8_signals + 2 * (_id) + 16 \
> + } \
> +}
> +
> +static struct counter_synapse quad8_count_synapses[][3] = {
> + QUAD8_COUNT_SYNAPSES(0), QUAD8_COUNT_SYNAPSES(1),
> + QUAD8_COUNT_SYNAPSES(2), QUAD8_COUNT_SYNAPSES(3),
> + QUAD8_COUNT_SYNAPSES(4), QUAD8_COUNT_SYNAPSES(5),
> + QUAD8_COUNT_SYNAPSES(6), QUAD8_COUNT_SYNAPSES(7)
> +};
> +
> +static const struct counter_count_ext quad8_count_ext[] = {
> + COUNTER_COUNT_ENUM("count_mode", &quad8_cnt_mode_enum),
> + COUNTER_COUNT_ENUM_AVAILABLE("count_mode", &quad8_cnt_mode_enum),
> + {
> + .name = "direction",
> + .read = quad8_count_direction_read
> + },
> + {
> + .name = "enable",
> + .read = quad8_count_enable_read,
> + .write = quad8_count_enable_write
> + },
> + COUNTER_COUNT_ENUM("noise_error", &quad8_noise_err_enum),
> + COUNTER_COUNT_ENUM_AVAILABLE("noise_error", &quad8_noise_err_enum),
> + {
> + .name = "preset",
> + .read = quad8_count_preset_read,
> + .write = quad8_count_preset_write
> + },
> + {
> + .name = "preset_enable",
> + .read = quad8_count_preset_enable_read,
> + .write = quad8_count_preset_enable_write
> + }
> +};
> +
> +#define QUAD8_COUNT(_id, _cntname) { \
> + .id = (_id), \
> + .name = (_cntname), \
> + .functions_list = quad8_count_functions_list, \
> + .num_functions = ARRAY_SIZE(quad8_count_functions_list), \
> + .synapses = quad8_count_synapses[(_id)], \
> + .num_synapses = 2, \
> + .ext = quad8_count_ext, \
> + .num_ext = ARRAY_SIZE(quad8_count_ext) \
> +}
> +
> +static struct counter_count quad8_counts[] = {
> + QUAD8_COUNT(0, "Channel 1 Count"),
> + QUAD8_COUNT(1, "Channel 2 Count"),
> + QUAD8_COUNT(2, "Channel 3 Count"),
> + QUAD8_COUNT(3, "Channel 4 Count"),
> + QUAD8_COUNT(4, "Channel 5 Count"),
> + QUAD8_COUNT(5, "Channel 6 Count"),
> + QUAD8_COUNT(6, "Channel 7 Count"),
> + QUAD8_COUNT(7, "Channel 8 Count")
> +};
> +
> static int quad8_probe(struct device *dev, unsigned int id)
> {
> struct iio_dev *indio_dev;
> - struct quad8_iio *priv;
> + struct quad8_iio *quad8iio;
> int i, j;
> unsigned int base_offset;
> + int err;
>
> - indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
> - if (!indio_dev)
> - return -ENOMEM;
> -
> - if (!devm_request_region(dev, base[id], QUAD8_EXTENT,
> - dev_name(dev))) {
> + if (!devm_request_region(dev, base[id], QUAD8_EXTENT, dev_name(dev))) {
> dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
> base[id], base[id] + QUAD8_EXTENT);
> return -EBUSY;
> }
>
> + /* Allocate IIO device; this also allocates driver data structure */
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*quad8iio));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + /* Initialize IIO device */
> indio_dev->info = &quad8_info;
> indio_dev->modes = INDIO_DIRECT_MODE;
> indio_dev->num_channels = ARRAY_SIZE(quad8_channels);
> @@ -552,8 +1197,22 @@ static int quad8_probe(struct device *dev, unsigned int id)
> indio_dev->name = dev_name(dev);
> indio_dev->dev.parent = dev;
>
> - priv = iio_priv(indio_dev);
> - priv->base = base[id];
> + /* Initialize Counter device and driver data */
> + quad8iio = iio_priv(indio_dev);
> + quad8iio->counter.name = dev_name(dev);
> + quad8iio->counter.parent = dev;
> + quad8iio->counter.signal_read = quad8_signal_read;
> + quad8iio->counter.count_read = quad8_count_read;
> + quad8iio->counter.count_write = quad8_count_write;
> + quad8iio->counter.function_get = quad8_function_get;
> + quad8iio->counter.function_set = quad8_function_set;
I missed this when looking at the earlier patch, but
it would be good to pull all of these function pointers out
into an ops structure. The big advantages are that we can
then take them constant (a good thing to do for security).
Also becomes one assignment in here rather than lots.
> + quad8iio->counter.action_get = quad8_action_get;
> + quad8iio->counter.counts = quad8_counts;
> + quad8iio->counter.num_counts = ARRAY_SIZE(quad8_counts);
> + quad8iio->counter.signals = quad8_signals;
> + quad8iio->counter.num_signals = ARRAY_SIZE(quad8_signals);
> + quad8iio->counter.priv = quad8iio;
> + quad8iio->base = base[id];
>
> /* Reset all counters and disable interrupt function */
> outb(0x01, base[id] + 0x11);
> @@ -579,7 +1238,13 @@ static int quad8_probe(struct device *dev, unsigned int id)
> /* Enable all counters */
> outb(0x00, base[id] + 0x11);
>
> - return devm_iio_device_register(dev, indio_dev);
> + /* Register IIO device */
> + err = devm_iio_device_register(dev, indio_dev);
> + if (err)
> + return err;
> +
> + /* Register Counter device */
> + return devm_counter_register(dev, &quad8iio->counter);
> }
>
> static struct isa_driver quad8_driver = {
On Fri, 9 Mar 2018 13:43:19 -0500
William Breathitt Gray <[email protected]> wrote:
> This patch adds standard documentation for the Generic Counter interface
> userspace sysfs attributes of the 104-QUAD-8 driver.
>
> Signed-off-by: William Breathitt Gray <[email protected]>
A few minor comments inline...
Some of this seems generic and common enough you should just put it in the
main docs straight away rather that waiting for more devices to use it.
Jonathan
> ---
> .../ABI/testing/sysfs-bus-counter-104-quad-8 | 115 +++++++++++++++++++++
> MAINTAINERS | 1 +
> 2 files changed, 116 insertions(+)
> create mode 100644 Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
>
> diff --git a/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8 b/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
> new file mode 100644
> index 000000000000..4269b438185a
> --- /dev/null
> +++ b/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
> @@ -0,0 +1,115 @@
> +What: /sys/bus/counter/devices/counterX/countY_count_mode
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Count mode for channel Y. The preset value for channel Y is used
> + by the count mode where required. The following count modes are
> + available:
> +
> + Normal:
> + Counting is continuous in either direction.
> +
> + Range Limit:
> + An upper or lower limit is set, mimicking limit switches
> + in the mechanical counterpart. The upper limit is set to
> + the preset value, while the lower limit is set to 0. The
> + counter freezes at count = preset when counting up, and
> + at count = 0 when counting down. At either of these
> + limits, the counting is resumed only when the count
> + direction is reversed.
> +
> + Non-recycle:
> + Counter is disabled whenever a 24-bit count overflow or
> + underflow takes place. The counter is re-enabled when a
> + new count value is loaded to the counter via a preset
> + operation or write to raw.
> +
> + Modulo-N:
> + A count boundary is set between 0 and the preset value.
> + The counter is reset to 0 at count = preset when
> + counting up, while the counter is set to the preset
> + value at count = 0 when counting down; the counter does
> + not freeze at the bundary points, but counts
> + continuously throughout.
This worries me a little in that you will end up with all sorts of subtle
variations around these concepts and hence end up with an impossible to
generalize userspace interface...
> +
> +What: /sys/bus/counter/devices/counterX/countY_count_mode_available
> +What: /sys/bus/counter/devices/counterX/countY_noise_error_available
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Discrete set of available values for the respective Count Y
> + configuration are listed in this file.
> +
> +What: /sys/bus/counter/devices/counterX/countY_direction
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Read-only attribute that indicates the count direction of
> + Count Y. Two count directions are available: Forward and
> + Backward.
Is this telling us which way it is currently counting? I would imagine
it's generic inversion control, but this description doesn't make that clear.
> +
> +What: /sys/bus/counter/devices/counterX/countY_enable
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Whether channel Y inputs A and B are enabled. Valid attribute
> + values are boolean.
Why would you disable them? I'm unclear on what userspace would do with this.
> +
> +What: /sys/bus/counter/devices/counterX/countY_noise_error
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Read-only attribute that indicates whether excessive noise is
> + present at the channel Y count inputs in quadrature clock mode;
> + irrelevant in non-quadrature (Pulse-Direction) clock mode.
If you are going to report errors like this I would suggest trying to have
a generic form that is easy for userspace to match.
countY_error_noise would allow easy presentation of all errors of the
form
countY_error_<error type> as a list based on <error type>
> +
> +What: /sys/bus/counter/devices/counterX/countY_preset
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + If the counter device supports preset registers, the preset
> + count for channel Y is provided by this attribute.
> +
> +What: /sys/bus/counter/devices/counterX/countY_preset_enable
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Whether to set channel Y counter with channel Y preset value
> + when channel Y index input is active, or continuously count.
> + Valid attribute values are boolean.
> +
> +What: /sys/bus/counter/devices/counterX/signalY_index_polarity
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Active level of channel Y-16 index input; irrelevant in
> + non-synchronous load mode.
This seems like a generic control that should be in the main docs?
don't use Y-16 to identify the channel. Use "the associated channel to signal Y".
> +
> +What: /sys/bus/counter/devices/counterX/signalY_index_polarity_available
> +What: /sys/bus/counter/devices/counterX/signalY_synchronous_mode_available
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Discrete set of available values for the respective Signal Y
> + configuration are listed in this file.
> +
> +What: /sys/bus/counter/devices/counterX/signalY_synchronous_mode
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Configure channel Y-16 counter for non-synchronous or
> + synchronous load mode. Synchronous load mode cannot be selected
> + in non-quadrature (Pulse-Direction) clock mode.
> +
> + Non-synchronous:
> + A logic low level is the active level at this index
> + input. The index function (as enabled via preset_enable)
> + is performed directly on the active level of the index
> + input.
> +
> + Synchronous:
> + Intended for interfacing with encoder Index output in
> + quadrature clock mode. The active level is configured
> + 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.
> diff --git a/MAINTAINERS b/MAINTAINERS
> index febe27a9962e..8134050d175a 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -270,6 +270,7 @@ ACCES 104-QUAD-8 DRIVER
> M: William Breathitt Gray <[email protected]>
> L: [email protected]
> S: Maintained
> +F: Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
> F: Documentation/ABI/testing/sysfs-bus-iio-counter-104-quad-8
> F: drivers/counter/104-quad-8.c
>
On Fri, 9 Mar 2018 13:43:32 -0500
William Breathitt Gray <[email protected]> wrote:
> From: Benjamin Gaignard <[email protected]>
>
> Add bindings for STM32 Timer quadrature encoder.
> It is a sub-node of STM32 Timer which implement the
> counter part of the hardware.
>
> Signed-off-by: Benjamin Gaignard <[email protected]>
> Signed-off-by: William Breathitt Gray <[email protected]>
You are missing the device tree list and maintainers +CC
looks fine to me.
Jonathan
> ---
> .../bindings/counter/stm32-timer-cnt.txt | 26 ++++++++++++++++++++++
> .../devicetree/bindings/mfd/stm32-timers.txt | 7 ++++++
> 2 files changed, 33 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt
>
> diff --git a/Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt b/Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt
> new file mode 100644
> index 000000000000..377728128bef
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/counter/stm32-timer-cnt.txt
> @@ -0,0 +1,26 @@
> +STMicroelectronics STM32 Timer quadrature encoder
> +
> +STM32 Timer provides quadrature encoder counter mode to detect
> +angular position and direction of rotary elements,
> +from IN1 and IN2 input signals.
> +
> +Must be a sub-node of an STM32 Timer device tree node.
> +See ../mfd/stm32-timers.txt for details about the parent node.
> +
> +Required properties:
> +- compatible: Must be "st,stm32-timer-counter".
> +- pinctrl-names: Set to "default".
> +- pinctrl-0: List of phandles pointing to pin configuration nodes,
> + to set IN1/IN2 pins in mode of operation for Low-Power
> + Timer input on external pin.
> +
> +Example:
> + timers@40010000 {
> + compatible = "st,stm32-timers";
> + ...
> + counter {
> + compatible = "st,stm32-timer-counter";
> + pinctrl-names = "default";
> + pinctrl-0 = <&tim1_in_pins>;
> + };
> + };
> diff --git a/Documentation/devicetree/bindings/mfd/stm32-timers.txt b/Documentation/devicetree/bindings/mfd/stm32-timers.txt
> index 1db6e0057a63..ff9c14ada30b 100644
> --- a/Documentation/devicetree/bindings/mfd/stm32-timers.txt
> +++ b/Documentation/devicetree/bindings/mfd/stm32-timers.txt
> @@ -23,6 +23,7 @@ Optional parameters:
> Optional subnodes:
> - pwm: See ../pwm/pwm-stm32.txt
> - timer: See ../iio/timer/stm32-timer-trigger.txt
> +- counter: See ../counter/stm32-timer-cnt.txt
>
> Example:
> timers@40010000 {
> @@ -43,4 +44,10 @@ Example:
> compatible = "st,stm32-timer-trigger";
> reg = <0>;
> };
> +
> + counter {
> + compatible = "st,stm32-timer-counter";
> + pinctrl-names = "default";
> + pinctrl-0 = <&tim1_in_pins>;
> + };
> };
On Fri, 9 Mar 2018 13:43:44 -0500
William Breathitt Gray <[email protected]> wrote:
> From: Benjamin Gaignard <[email protected]>
>
> In addition of the generic sysfs-bus-counter ABI stm32-timer-cnt
> offerts three functionality:
> - enable the counter
> - set preset value
> - allow to read counter direction
>
> Signed-off-by: Benjamin Gaignard <[email protected]>
> Signed-off-by: William Breathitt Gray <[email protected]>
My main thought here is that there is nothing which doesn't belong
in sysfs-bus-counter to my mind anyway.
Jonathan
> ---
> .../ABI/testing/sysfs-bus-counter-stm32-timer-cnt | 21 +++++++++++++++++++++
> 1 file changed, 21 insertions(+)
> create mode 100644 Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt
>
> diff --git a/Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt b/Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt
> new file mode 100644
> index 000000000000..73328e8e6d7e
> --- /dev/null
> +++ b/Documentation/ABI/testing/sysfs-bus-counter-stm32-timer-cnt
> @@ -0,0 +1,21 @@
> +What: /sys/bus/counter/devices/counterX/countY_direction
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Read-only attribute that indicates the count direction of
> + Count Y. Two count directions are available: Forward and
> + Backward.
Given both existing devices have this, I'm guessing it is generic enough
to graduate to the generic sysfs documentation file.
> +
> +What: /sys/bus/counter/devices/counterX/countY_enable
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Whether channel Y inputs A and B are enabled. Valid attribute
> + values are boolean.
Given for the previous device we had individual inputs (which to my mind
made no sense to disable separately) perhaps we should have this as the
generic option. If a driver needs to separately disable the two signals
then that is up to it to sort out.
At least this way we reduce the guessing any userspace code will need
to do in order to turns things on.
> +
> +What: /sys/bus/counter/devices/counterX/countY_preset
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + If the counter device supports preset registers, the preset
> + count for channel Y is provided by this attribute.
Again, looks pretty generic...
On Fri, 9 Mar 2018 13:43:55 -0500
William Breathitt Gray <[email protected]> wrote:
> From: Benjamin Gaignard <[email protected]>
>
> Implement counter part of the STM32 timer hardware block
> by using counter API.
> Hardware only support X2 and X4 quadrature modes.
> A Preset value can to set to define the maximum value
> reachable by the counter.
>
> Signed-off-by: Benjamin Gaignard <[email protected]>
> Signed-off-by: William Breathitt Gray <[email protected]>
Looks good to me.
The only changes I would make are around things I've suggested for the core.
Nice little driver :)
Jonathan
> ---
> drivers/counter/Kconfig | 10 ++
> drivers/counter/Makefile | 1 +
> drivers/counter/stm32-timer-cnt.c | 356 ++++++++++++++++++++++++++++++++++++++
> 3 files changed, 367 insertions(+)
> create mode 100644 drivers/counter/stm32-timer-cnt.c
>
> diff --git a/drivers/counter/Kconfig b/drivers/counter/Kconfig
> index 0b28d3ff524b..d758279081ac 100644
> --- a/drivers/counter/Kconfig
> +++ b/drivers/counter/Kconfig
> @@ -35,6 +35,16 @@ config 104_QUAD_8
> The base port addresses for the devices may be configured via the base
> array module parameter.
>
> +config STM32_TIMER_CNT
> + tristate "STM32 Timer encoder counter driver"
> + depends on MFD_STM32_TIMERS || COMPILE_TEST
> + help
> + Select this option to enable STM32 Timer quadrature encoder
> + and counter driver.
> +
> + To compile this driver as a module, choose M here: the
> + module will be called stm32-timer-cnt.
> +
> config STM32_LPTIMER_CNT
> tristate "STM32 LP Timer encoder counter driver"
> depends on (MFD_STM32_LPTIMER || COMPILE_TEST) && IIO
> diff --git a/drivers/counter/Makefile b/drivers/counter/Makefile
> index d721a40aa4a2..9c037d7d1ed6 100644
> --- a/drivers/counter/Makefile
> +++ b/drivers/counter/Makefile
> @@ -8,4 +8,5 @@ obj-$(CONFIG_COUNTER) += counter.o
> counter-y := generic-counter.o
>
> obj-$(CONFIG_104_QUAD_8) += 104-quad-8.o
> +obj-$(CONFIG_STM32_TIMER_CNT) += stm32-timer-cnt.o
> obj-$(CONFIG_STM32_LPTIMER_CNT) += stm32-lptimer-cnt.o
> diff --git a/drivers/counter/stm32-timer-cnt.c b/drivers/counter/stm32-timer-cnt.c
> new file mode 100644
> index 000000000000..14085615e880
> --- /dev/null
> +++ b/drivers/counter/stm32-timer-cnt.c
> @@ -0,0 +1,356 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * STM32 Timer Encoder and Counter driver
> + *
> + * Copyright (C) STMicroelectronics 2018
> + *
> + * Author: Benjamin Gaignard <[email protected]>
> + *
> + */
> +#include <linux/counter.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/types.h>
> +#include <linux/mfd/stm32-timers.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +
> +#define TIM_CCMR_CCXS (BIT(8) | BIT(0))
> +#define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
> + TIM_CCMR_IC1F | TIM_CCMR_IC2F)
> +#define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
> + TIM_CCER_CC2P | TIM_CCER_CC2NP)
> +
> +struct stm32_timer_cnt {
> + struct counter_device counter;
> + struct regmap *regmap;
> + struct clk *clk;
> + u32 preset;
> +};
> +
> +enum stm32_count_function {
> + STM32_COUNT_FUNCTION_QUADRATURE_X2,
> + STM32_COUNT_FUNCTION_QUADRATURE_X4,
> +};
> +
> +static enum count_function stm32_count_functions[] = {
> + [STM32_COUNT_FUNCTION_QUADRATURE_X2] = COUNT_FUNCTION_QUADRATURE_X2,
> + [STM32_COUNT_FUNCTION_QUADRATURE_X4] = COUNT_FUNCTION_QUADRATURE_X4
> +};
> +
> +static int stm32_count_read(struct counter_device *counter,
> + struct counter_count *count,
> + struct count_read_value *val)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + u32 cnt;
> +
> + regmap_read(priv->regmap, TIM_CNT, &cnt);
> + set_count_read_value(val, COUNT_POSITION_UNSIGNED, &cnt);
> +
> + return 0;
> +}
> +
> +static int stm32_count_write(struct counter_device *counter,
> + struct counter_count *count,
> + struct count_write_value *val)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + u32 cnt;
> + int err;
> +
> + err = get_count_write_value(&cnt, COUNT_POSITION_UNSIGNED, val);
> + if (err)
> + return err;
> +
> + if (cnt > priv->preset)
> + return -EINVAL;
> +
> + return regmap_write(priv->regmap, TIM_CNT, cnt);
> +}
> +
> +static int stm32_count_function_get(struct counter_device *counter,
> + struct counter_count *count,
> + size_t *function)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + u32 smcr;
> +
> + regmap_read(priv->regmap, TIM_SMCR, &smcr);
> +
> + switch (smcr & TIM_SMCR_SMS) {
> + case 1:
> + *function = STM32_COUNT_FUNCTION_QUADRATURE_X2;
> + return 0;
> + case 3:
> + *function = STM32_COUNT_FUNCTION_QUADRATURE_X4;
> + return 0;
> + }
> +
> + return -EINVAL;
> +}
> +
> +static int stm32_count_function_set(struct counter_device *counter,
> + struct counter_count *count,
> + size_t function)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + u32 cr1, sms;
> +
> + switch (function) {
> + case STM32_COUNT_FUNCTION_QUADRATURE_X2:
> + sms = 1;
> + break;
> + case STM32_COUNT_FUNCTION_QUADRATURE_X4:
> + sms = 3;
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + /* Store enable status */
> + regmap_read(priv->regmap, TIM_CR1, &cr1);
> +
> + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
> +
> + /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
> + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
> + regmap_write(priv->regmap, TIM_ARR, priv->preset);
> +
> + regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
> +
> + /* Make sure that registers are updated */
> + regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
> +
> + /* Restore the enable status */
> + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
> +
> + return 0;
> +}
> +
> +static ssize_t stm32_count_direction_read(struct counter_device *counter,
> + struct counter_count *count,
> + void *private, char *buf)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + const char *direction;
> + u32 cr1;
> +
> + regmap_read(priv->regmap, TIM_CR1, &cr1);
> + direction = (cr1 & TIM_CR1_DIR) ? "backward" : "forward";
> +
> + return scnprintf(buf, PAGE_SIZE, "%s\n", direction);
> +}
> +
> +static ssize_t stm32_count_preset_read(struct counter_device *counter,
> + struct counter_count *count,
> + void *private, char *buf)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + u32 arr;
> +
> + regmap_read(priv->regmap, TIM_ARR, &arr);
> +
> + return snprintf(buf, PAGE_SIZE, "%u\n", arr);
> +}
> +
> +static ssize_t stm32_count_preset_write(struct counter_device *counter,
> + struct counter_count *count,
> + void *private,
> + const char *buf, size_t len)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + unsigned int preset;
> + int ret;
> +
> + ret = kstrtouint(buf, 0, &preset);
> + if (ret)
> + return ret;
> +
> + /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
> + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
> + regmap_write(priv->regmap, TIM_ARR, preset);
> +
> + priv->preset = preset;
> + return len;
> +}
> +
> +static ssize_t stm32_count_enable_read(struct counter_device *counter,
> + struct counter_count *count,
> + void *private, char *buf)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + u32 cr1;
> +
> + regmap_read(priv->regmap, TIM_CR1, &cr1);
> +
> + return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)(cr1 & TIM_CR1_CEN));
> +}
> +
> +static ssize_t stm32_count_enable_write(struct counter_device *counter,
> + struct counter_count *count,
> + void *private,
> + const char *buf, size_t len)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + int err;
> + u32 cr1;
> + bool enable;
> +
> + err = kstrtobool(buf, &enable);
> + if (err)
> + return err;
> +
> + if (enable) {
> + regmap_read(priv->regmap, TIM_CR1, &cr1);
> + if (!(cr1 & TIM_CR1_CEN))
> + clk_enable(priv->clk);
> +
> + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
> + TIM_CR1_CEN);
> + } else {
> + regmap_read(priv->regmap, TIM_CR1, &cr1);
> + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
> + if (cr1 & TIM_CR1_CEN)
> + clk_disable(priv->clk);
> + }
> +
> + return len;
> +}
> +
> +static const struct counter_count_ext stm32_count_ext[] = {
> + {
> + .name = "direction",
> + .read = stm32_count_direction_read,
> + },
> + {
> + .name = "enable",
> + .read = stm32_count_enable_read,
> + .write = stm32_count_enable_write
> + },
> + {
> + .name = "preset",
> + .read = stm32_count_preset_read,
> + .write = stm32_count_preset_write
> + },
> +};
> +
> +enum stm32_synapse_action {
> + STM32_SYNAPSE_ACTION_RISING_EDGE = 0,
> + STM32_SYNAPSE_ACTION_BOTH_EDGES
> +};
> +
> +static enum synapse_action stm32_synapse_actions[] = {
> + [STM32_SYNAPSE_ACTION_RISING_EDGE] = SYNAPSE_ACTION_RISING_EDGE,
> + [STM32_SYNAPSE_ACTION_BOTH_EDGES] = SYNAPSE_ACTION_BOTH_EDGES
> +};
> +
> +static int stm32_action_get(struct counter_device *counter,
> + struct counter_count *count,
> + struct counter_synapse *synapse,
> + size_t *action)
> +{
> + struct stm32_timer_cnt *const priv = counter->priv;
> + u32 smcr;
> +
> + regmap_read(priv->regmap, TIM_SMCR, &smcr);
> +
> + switch (smcr & TIM_SMCR_SMS) {
> + case 1:
> + *action = STM32_SYNAPSE_ACTION_RISING_EDGE;
> + return 0;
> + case 3:
> + *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
> + return 0;
> + }
> +
> + return -EINVAL;
> +}
> +
> +static struct counter_signal stm32_signals[] = {
> + {
> + .id = 0,
> + .name = "Channel 1 Quadrature A"
> + },
> + {
> + .id = 1,
> + .name = "Channel 1 Quadrature B"
> + }
> +};
> +
> +static struct counter_synapse stm32_count_synapses[] = {
> + {
> + .actions_list = stm32_synapse_actions,
> + .num_actions = ARRAY_SIZE(stm32_synapse_actions),
> + .signal = &stm32_signals[0]
> + },
> + {
> + .actions_list = stm32_synapse_actions,
> + .num_actions = ARRAY_SIZE(stm32_synapse_actions),
> + .signal = &stm32_signals[1]
> + }
> +};
> +
> +static struct counter_count stm32_counts = {
> + .id = 0,
> + .name = "Channel 1 Count",
> + .functions_list = stm32_count_functions,
> + .num_functions = ARRAY_SIZE(stm32_count_functions),
> + .synapses = stm32_count_synapses,
> + .num_synapses = ARRAY_SIZE(stm32_count_synapses),
> + .ext = stm32_count_ext,
> + .num_ext = ARRAY_SIZE(stm32_count_ext)
> +};
> +
> +static int stm32_timer_cnt_probe(struct platform_device *pdev)
> +{
> + struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
> + struct device *dev = &pdev->dev;
> + struct stm32_timer_cnt *priv;
> +
> + if (IS_ERR_OR_NULL(ddata))
> + return -EINVAL;
> +
> + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> + if (!priv)
> + return -ENOMEM;
> +
> + priv->regmap = ddata->regmap;
> + priv->clk = ddata->clk;
> + priv->preset = ddata->max_arr;
> +
> + priv->counter.name = dev_name(dev);
> + priv->counter.parent = dev;
> + priv->counter.count_read = stm32_count_read;
> + priv->counter.count_write = stm32_count_write;
> + priv->counter.function_get = stm32_count_function_get;
> + priv->counter.function_set = stm32_count_function_set;
> + priv->counter.action_get = stm32_action_get;
> + priv->counter.counts = &stm32_counts;
> + priv->counter.num_counts = 1;
> + priv->counter.signals = stm32_signals;
> + priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
> + priv->counter.priv = priv;
> +
> + /* Register Counter device */
> + return devm_counter_register(dev, &priv->counter);
> +}
> +
> +static const struct of_device_id stm32_timer_cnt_of_match[] = {
> + { .compatible = "st,stm32-timer-counter", },
> + {},
> +};
> +MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
> +
> +static struct platform_driver stm32_timer_cnt_driver = {
> + .probe = stm32_timer_cnt_probe,
> + .driver = {
> + .name = "stm32-timer-counter",
> + .of_match_table = stm32_timer_cnt_of_match,
> + },
> +};
> +module_platform_driver(stm32_timer_cnt_driver);
> +
> +MODULE_AUTHOR("Benjamin Gaignard <[email protected]>");
> +MODULE_ALIAS("platform:stm32-timer-counter");
> +MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
> +MODULE_LICENSE("GPL v2");
On Fri, 9 Mar 2018 13:42:23 -0500
William Breathitt Gray <[email protected]> wrote:
> This patch introduces the Generic Counter interface for supporting
> counter devices.
>
> In the context of the Generic Counter interface, a counter is defined as
> a device that reports one or more "counts" based on the state changes of
> one or more "signals" as evaluated by a defined "count function."
>
> Driver callbacks should be provided to communicate with the device: to
> read and write various Signals and Counts, and to set and get the
> "action mode" and "count function" for various Synapses and Counts
> respectively.
>
> To support a counter device, a driver must first allocate the available
> Counter Signals via counter_signal structures. These Signals should
> be stored as an array and set to the signals array member of an
> allocated counter_device structure before the Counter is registered to
> the system.
>
> Counter Counts may be allocated via counter_count structures, and
> respective Counter Signal associations (Synapses) made via
> counter_synapse structures. Associated counter_synapse structures are
> stored as an array and set to the the synapses array member of the
> respective counter_count structure. These counter_count structures are
> set to the counts array member of an allocated counter_device structure
> before the Counter is registered to the system.
>
> A counter device is registered to the system by passing the respective
> initialized counter_device structure to the counter_register function;
> similarly, the counter_unregister function unregisters the respective
> Counter. The devm_counter_register and devm_counter_unregister functions
> serve as device memory-managed versions of the counter_register and
> counter_unregister functions respectively.
>
> Signed-off-by: William Breathitt Gray <[email protected]>
Hi William,
I would leave the existing drivers where they are until you are ready
to convert them. I.e. Do the moves as separate patches from this one
as it just adds noise here and they aren't ready immediately.
The externs in the header add code for no benefit and make it hard
to align the parameters nicely. I would drop them all.
Few other minor bits and bobs inline. There is a lot of 'automatic'
cleanup in here, but I think you have missed a few cases where the
attribute element hasn't 'yet' been added to the list. (I may be
missing something)
Fundamentally looks good though.
Jonathan
> ---
> MAINTAINERS | 7 +
> drivers/Kconfig | 2 +
> drivers/Makefile | 1 +
> drivers/{iio => }/counter/104-quad-8.c | 0
> drivers/{iio => }/counter/Kconfig | 23 +-
> drivers/{iio => }/counter/Makefile | 5 +-
> drivers/counter/generic-counter.c | 1416 +++++++++++++++++++++++++
> drivers/{iio => }/counter/stm32-lptimer-cnt.c | 0
> drivers/iio/Kconfig | 1 -
> drivers/iio/Makefile | 1 -
> include/linux/counter.h | 524 +++++++++
> 11 files changed, 1973 insertions(+), 7 deletions(-)
> rename drivers/{iio => }/counter/104-quad-8.c (100%)
> rename drivers/{iio => }/counter/Kconfig (56%)
> rename drivers/{iio => }/counter/Makefile (62%)
> create mode 100644 drivers/counter/generic-counter.c
> rename drivers/{iio => }/counter/stm32-lptimer-cnt.c (100%)
> create mode 100644 include/linux/counter.h
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 885d20072d97..2be01a95b7a5 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -3668,6 +3668,13 @@ W: http://www.fi.muni.cz/~kas/cosa/
> S: Maintained
> F: drivers/net/wan/cosa*
>
> +COUNTER SUBSYSTEM
> +M: William Breathitt Gray <[email protected]>
> +L: [email protected]
> +S: Maintained
> +F: drivers/counter/
> +F: include/linux/counter.h
> +
> CPMAC ETHERNET DRIVER
> M: Florian Fainelli <[email protected]>
> L: [email protected]
> diff --git a/drivers/Kconfig b/drivers/Kconfig
> index 879dc0604cba..21a67f49c17b 100644
> --- a/drivers/Kconfig
> +++ b/drivers/Kconfig
> @@ -165,6 +165,8 @@ source "drivers/memory/Kconfig"
>
> source "drivers/iio/Kconfig"
>
> +source "drivers/counter/Kconfig"
> +
> source "drivers/ntb/Kconfig"
>
> source "drivers/vme/Kconfig"
> diff --git a/drivers/Makefile b/drivers/Makefile
> index 24cd47014657..5914c78688c3 100644
> --- a/drivers/Makefile
> +++ b/drivers/Makefile
> @@ -165,6 +165,7 @@ obj-$(CONFIG_PM_DEVFREQ) += devfreq/
> obj-$(CONFIG_EXTCON) += extcon/
> obj-$(CONFIG_MEMORY) += memory/
> obj-$(CONFIG_IIO) += iio/
> +obj-$(CONFIG_COUNTER) += counter/
> obj-$(CONFIG_VME_BUS) += vme/
> obj-$(CONFIG_IPACK_BUS) += ipack/
> obj-$(CONFIG_NTB) += ntb/
> diff --git a/drivers/iio/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
> similarity index 100%
> rename from drivers/iio/counter/104-quad-8.c
> rename to drivers/counter/104-quad-8.c
> diff --git a/drivers/iio/counter/Kconfig b/drivers/counter/Kconfig
> similarity index 56%
> rename from drivers/iio/counter/Kconfig
> rename to drivers/counter/Kconfig
> index 474e1ac4e7c0..0b28d3ff524b 100644
> --- a/drivers/iio/counter/Kconfig
> +++ b/drivers/counter/Kconfig
> @@ -3,11 +3,25 @@
> #
> # When adding new entries keep the list in alphabetical order
>
> -menu "Counters"
> +menuconfig COUNTER
> + tristate "Counter support"
> + help
> + Provides Generic Counter interface support for counter devices.
> +
> + Counter devices are prevalent within a diverse spectrum of industries.
> + The ubiquitous presence of these devices necessitates a common
> + interface and standard of interaction and exposure. This driver API
> + attempts to resolve the issue of duplicate code found among existing
> + counter device drivers by providing a generic counter interface for
> + consumption. The Generic Counter interface enables drivers to support
> + and expose a common set of components and functionality present in
> + counter devices.
> +
> +if COUNTER
>
> config 104_QUAD_8
> tristate "ACCES 104-QUAD-8 driver"
> - depends on PC104 && X86 && ISA_BUS_API
> + depends on PC104 && X86 && ISA_BUS_API && IIO
> help
> Say yes here to build support for the ACCES 104-QUAD-8 quadrature
> encoder counter/interface device family (104-QUAD-8, 104-QUAD-4).
> @@ -23,11 +37,12 @@ config 104_QUAD_8
>
> config STM32_LPTIMER_CNT
> tristate "STM32 LP Timer encoder counter driver"
> - depends on MFD_STM32_LPTIMER || COMPILE_TEST
> + depends on (MFD_STM32_LPTIMER || COMPILE_TEST) && IIO
> help
> Select this option to enable STM32 Low-Power Timer quadrature encoder
> and counter driver.
>
> To compile this driver as a module, choose M here: the
> module will be called stm32-lptimer-cnt.
> -endmenu
> +
> +endif # COUNTER
> diff --git a/drivers/iio/counter/Makefile b/drivers/counter/Makefile
> similarity index 62%
> rename from drivers/iio/counter/Makefile
> rename to drivers/counter/Makefile
> index 1b9a896eb488..d721a40aa4a2 100644
> --- a/drivers/iio/counter/Makefile
> +++ b/drivers/counter/Makefile
> @@ -1,8 +1,11 @@
> #
> -# Makefile for IIO counter devices
> +# Makefile for Counter devices
> #
>
> # When adding new entries keep the list in alphabetical order
>
> +obj-$(CONFIG_COUNTER) += counter.o
> +counter-y := generic-counter.o
> +
> obj-$(CONFIG_104_QUAD_8) += 104-quad-8.o
> obj-$(CONFIG_STM32_LPTIMER_CNT) += stm32-lptimer-cnt.o
> diff --git a/drivers/counter/generic-counter.c b/drivers/counter/generic-counter.c
> new file mode 100644
> index 000000000000..03803356aac8
> --- /dev/null
> +++ b/drivers/counter/generic-counter.c
> @@ -0,0 +1,1416 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Generic Counter interface
> + * Copyright (C) 2017 William Breathitt Gray
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License, version 2, as
> + * published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * General Public License for more details.
> + */
> +#include <linux/device.h>
> +#include <linux/err.h>
> +#include <linux/export.h>
> +#include <linux/fs.h>
> +#include <linux/gfp.h>
> +#include <linux/idr.h>
> +#include <linux/init.h>
> +#include <linux/kernel.h>
> +#include <linux/list.h>
> +#include <linux/module.h>
> +#include <linux/printk.h>
> +#include <linux/slab.h>
> +#include <linux/string.h>
> +#include <linux/sysfs.h>
> +#include <linux/types.h>
> +
> +#include <linux/counter.h>
> +
> +ssize_t counter_signal_enum_read(struct counter_device *counter,
> + struct counter_signal *signal, void *priv, char *buf)
> +{
> + const struct counter_signal_enum_ext *const e = priv;
> + int err;
> + size_t index;
> +
> + if (!e->get)
> + return -EINVAL;
> +
> + err = e->get(counter, signal, &index);
> + if (err)
> + return err;
> +
> + if (index >= e->num_items)
> + return -EINVAL;
> +
> + return scnprintf(buf, PAGE_SIZE, "%s\n", e->items[index]);
> +}
> +EXPORT_SYMBOL(counter_signal_enum_read);
> +
> +ssize_t counter_signal_enum_write(struct counter_device *counter,
> + struct counter_signal *signal, void *priv, const char *buf, size_t len)
> +{
> + const struct counter_signal_enum_ext *const e = priv;
> + ssize_t index;
> + int err;
> +
> + if (!e->set)
> + return -EINVAL;
> +
> + index = __sysfs_match_string(e->items, e->num_items, buf);
> + if (index < 0)
> + return index;
> +
> + err = e->set(counter, signal, index);
> + if (err)
> + return err;
> +
> + return len;
> +}
> +EXPORT_SYMBOL(counter_signal_enum_write);
> +
> +ssize_t counter_signal_enum_available_read(struct counter_device *counter,
> + struct counter_signal *signal, void *priv, char *buf)
> +{
> + const struct counter_signal_enum_ext *const e = priv;
> + size_t i;
> + size_t len = 0;
> +
> + if (!e->num_items)
> + return 0;
> +
> + for (i = 0; i < e->num_items; i++)
> + len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
> + e->items[i]);
> +
> + return len;
> +}
> +EXPORT_SYMBOL(counter_signal_enum_available_read);
> +
> +ssize_t counter_count_enum_read(struct counter_device *counter,
> + struct counter_count *count, void *priv, char *buf)
> +{
> + const struct counter_count_enum_ext *const e = priv;
> + int err;
> + size_t index;
> +
> + if (!e->get)
> + return -EINVAL;
> +
> + err = e->get(counter, count, &index);
> + if (err)
> + return err;
> +
> + if (index >= e->num_items)
> + return -EINVAL;
> +
> + return scnprintf(buf, PAGE_SIZE, "%s\n", e->items[index]);
> +}
> +EXPORT_SYMBOL(counter_count_enum_read);
> +
> +ssize_t counter_count_enum_write(struct counter_device *counter,
> + struct counter_count *count, void *priv, const char *buf, size_t len)
> +{
> + const struct counter_count_enum_ext *const e = priv;
> + ssize_t index;
> + int err;
> +
> + if (!e->set)
> + return -EINVAL;
> +
> + index = __sysfs_match_string(e->items, e->num_items, buf);
> + if (index < 0)
> + return index;
> +
> + err = e->set(counter, count, index);
> + if (err)
> + return err;
> +
> + return len;
> +}
> +EXPORT_SYMBOL(counter_count_enum_write);
> +
> +ssize_t counter_count_enum_available_read(struct counter_device *counter,
> + struct counter_count *count, void *priv, char *buf)
> +{
> + const struct counter_count_enum_ext *const e = priv;
> + size_t i;
> + size_t len = 0;
> +
> + if (!e->num_items)
> + return 0;
> +
> + for (i = 0; i < e->num_items; i++)
> + len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
> + e->items[i]);
> +
> + return len;
> +}
> +EXPORT_SYMBOL(counter_count_enum_available_read);
> +
> +ssize_t counter_device_enum_read(struct counter_device *counter, void *priv,
> + char *buf)
> +{
> + const struct counter_device_enum_ext *const e = priv;
> + int err;
> + size_t index;
> +
> + if (!e->get)
> + return -EINVAL;
> +
> + err = e->get(counter, &index);
> + if (err)
> + return err;
> +
> + if (index >= e->num_items)
> + return -EINVAL;
> +
> + return scnprintf(buf, PAGE_SIZE, "%s\n", e->items[index]);
> +}
> +EXPORT_SYMBOL(counter_device_enum_read);
> +
> +ssize_t counter_device_enum_write(struct counter_device *counter, void *priv,
> + const char *buf, size_t len)
> +{
> + const struct counter_device_enum_ext *const e = priv;
> + ssize_t index;
> + int err;
> +
> + if (!e->set)
> + return -EINVAL;
> +
> + index = __sysfs_match_string(e->items, e->num_items, buf);
> + if (index < 0)
> + return index;
> +
> + err = e->set(counter, index);
> + if (err)
> + return err;
> +
> + return len;
> +}
> +EXPORT_SYMBOL(counter_device_enum_write);
> +
> +ssize_t counter_device_enum_available_read(struct counter_device *counter,
> + void *priv, char *buf)
> +{
> + const struct counter_device_enum_ext *const e = priv;
> + size_t i;
> + size_t len = 0;
> +
> + if (!e->num_items)
> + return 0;
> +
> + for (i = 0; i < e->num_items; i++)
> + len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
> + e->items[i]);
> +
> + return len;
> +}
> +EXPORT_SYMBOL(counter_device_enum_available_read);
> +
> +static const char *const signal_level_str[] = {
> + [SIGNAL_LEVEL_LOW] = "low",
> + [SIGNAL_LEVEL_HIGH] = "high"
> +};
> +
> +/**
> + * set_signal_read_value - set signal_read_value data
> + * @val: signal_read_value structure to set
> + * @type: property Signal data represents
> + * @data: Signal data
> + *
> + * This function sets an opaque signal_read_value structure with the provided
> + * Signal data.
> + */
> +void set_signal_read_value(struct signal_read_value *const val,
> + const enum signal_value_type type, void *const data)
> +{
> + if (type == SIGNAL_LEVEL)
> + val->len = scnprintf(val->buf, PAGE_SIZE, "%s\n",
> + signal_level_str[*(enum signal_level *)data]);
> + else
> + val->len = 0;
> +}
> +EXPORT_SYMBOL(set_signal_read_value);
> +
> +/**
> + * set_count_read_value - set count_read_value data
> + * @val: count_read_value structure to set
> + * @type: property Count data represents
> + * @data: Count data
> + *
> + * This function sets an opaque count_read_value structure with the provided
> + * Count data.
> + */
> +void set_count_read_value(struct count_read_value *const val,
> + const enum count_value_type type, void *const data)
> +{
> + switch (type) {
> + case COUNT_POSITION_UNSIGNED:
> + val->len = scnprintf(val->buf, PAGE_SIZE, "%lu\n",
> + *(unsigned long *)data);
> + break;
> + case COUNT_POSITION_SIGNED:
> + val->len = scnprintf(val->buf, PAGE_SIZE, "%ld\n",
> + *(long *)data);
> + break;
> + default:
> + val->len = 0;
> + }
> +}
> +EXPORT_SYMBOL(set_count_read_value);
> +
> +/**
> + * get_count_write_value - get count_write_value data
> + * @data: Count data
> + * @type: property Count data represents
> + * @val: count_write_value structure containing data
> + *
> + * This function extracts Count data from the provided opaque count_write_value
> + * structure and stores it at the address provided by @data.
> + *
> + * RETURNS:
> + * 0 on success, negative error number on failure.
> + */
> +int get_count_write_value(void *const data,
> + const enum count_value_type type,
> + const struct count_write_value *const val)
> +{
> + int err;
> +
> + switch (type) {
> + case COUNT_POSITION_UNSIGNED:
> + err = kstrtoul(val->buf, 0, data);
> + if (err)
> + return err;
> + break;
> + case COUNT_POSITION_SIGNED:
> + err = kstrtol(val->buf, 0, data);
> + if (err)
> + return err;
> + break;
> + }
> +
> + return 0;
> +}
> +EXPORT_SYMBOL(get_count_write_value);
> +
> +struct counter_device_attr {
> + struct device_attribute dev_attr;
> + struct list_head l;
> + void *component;
> +};
> +
> +static int counter_attribute_create(
> + struct counter_device_attr_group *const group,
> + const char *const prefix,
> + const char *const name,
> + ssize_t (*show)(struct device *dev, struct device_attribute *attr,
> + char *buf),
> + ssize_t (*store)(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t len),
> + void *const component)
> +{
> + struct counter_device_attr *counter_attr;
> + struct device_attribute *dev_attr;
> + int err;
> + struct list_head *const attr_list = &group->attr_list;
> +
> + /* Allocate a Counter device attribute */
> + counter_attr = kzalloc(sizeof(*counter_attr), GFP_KERNEL);
> + if (!counter_attr)
> + return -ENOMEM;
> + dev_attr = &counter_attr->dev_attr;
> +
> + sysfs_attr_init(&dev_attr->attr);
> +
> + /* Configure device attribute */
> + dev_attr->attr.name = kasprintf(GFP_KERNEL, "%s%s", prefix, name);
> + if (!dev_attr->attr.name) {
> + err = -ENOMEM;
> + goto err_free_counter_attr;
> + }
> + if (show) {
> + dev_attr->attr.mode |= 0444;
> + dev_attr->show = show;
> + }
> + if (store) {
> + dev_attr->attr.mode |= 0200;
> + dev_attr->store = store;
> + }
> +
> + /* Store associated Counter component with attribute */
> + counter_attr->component = component;
> +
> + /* Keep track of the attribute for later cleanup */
> + list_add(&counter_attr->l, attr_list);
> + group->num_attr++;
> +
> + return 0;
> +
> +err_free_counter_attr:
> + kfree(counter_attr);
> + return err;
> +}
> +
> +#define to_counter_attr(_dev_attr) \
> + container_of(_dev_attr, struct counter_device_attr, dev_attr)
> +
> +struct signal_comp_t {
> + struct counter_signal *signal;
> +};
> +
> +static ssize_t counter_signal_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct signal_comp_t *const component = devattr->component;
> + struct counter_signal *const signal = component->signal;
> + int err;
> + struct signal_read_value val = { .buf = buf };
> +
> + err = counter->signal_read(counter, signal, &val);
> + if (err)
> + return err;
> +
> + return val.len;
> +}
> +
> +struct name_comp_t {
> + const char *name;
> +};
> +
> +static ssize_t counter_device_attr_name_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + const struct name_comp_t *const comp = to_counter_attr(attr)->component;
> +
> + return scnprintf(buf, PAGE_SIZE, "%s\n", comp->name);
> +}
> +
> +struct signal_ext_comp_t {
> + struct counter_signal *signal;
> + const struct counter_signal_ext *ext;
> +};
> +
> +static ssize_t counter_signal_ext_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct signal_ext_comp_t *const component = devattr->component;
> + const struct counter_signal_ext *const ext = component->ext;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + struct counter_signal *const signal = component->signal;
> +
> + return ext->read(counter, signal, ext->priv, buf);
> +}
> +
> +static ssize_t counter_signal_ext_store(struct device *dev,
> + struct device_attribute *attr, const char *buf, size_t len)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct signal_ext_comp_t *const component = devattr->component;
> + const struct counter_signal_ext *const ext = component->ext;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + struct counter_signal *const signal = component->signal;
> +
> + return ext->write(counter, signal, ext->priv, buf, len);
> +}
> +
> +static int counter_signal_ext_register(
> + struct counter_device_attr_group *const group,
> + struct counter_signal *const signal)
> +{
> + const size_t num_ext = signal->num_ext;
> + size_t i;
> + const struct counter_signal_ext *ext;
> + struct signal_ext_comp_t *signal_ext_comp;
> + int err;
> +
> + /* Return early if no extensions */
> + if (!signal->ext || !num_ext)
> + return 0;
> +
> + /* Create an attribute for each extension */
> + for (i = 0 ; i < num_ext; i++) {
> + ext = signal->ext + i;
> +
> + /* Allocate signal_ext attribute component */
> + signal_ext_comp = kmalloc(sizeof(*signal_ext_comp), GFP_KERNEL);
> + if (!signal_ext_comp)
> + return -ENOMEM;
> + signal_ext_comp->signal = signal;
> + signal_ext_comp->ext = ext;
> +
> + /* Allocate a Counter device attribute */
> + err = counter_attribute_create(group, "", ext->name,
> + (ext->read) ? counter_signal_ext_show : NULL,
> + (ext->write) ? counter_signal_ext_store : NULL,
> + signal_ext_comp);
> + if (err) {
> + kfree(signal_ext_comp);
> + return err;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static int counter_signal_attributes_create(
> + struct counter_device_attr_group *const group,
> + const struct counter_device *const counter,
> + struct counter_signal *const signal)
> +{
> + struct signal_comp_t *signal_comp;
> + int err;
> + struct name_comp_t *name_comp;
> +
> + /* Allocate Signal attribute component */
> + signal_comp = kmalloc(sizeof(*signal_comp), GFP_KERNEL);
> + if (!signal_comp)
> + return -ENOMEM;
> + signal_comp->signal = signal;
> +
> + /* Create main Signal attribute */
> + err = counter_attribute_create(group, "", "signal",
> + (counter->signal_read) ? counter_signal_show : NULL, NULL,
> + signal_comp);
> + if (err) {
> + kfree(signal_comp);
> + return err;
> + }
> +
> + /* Create Signal name attribute */
> + if (signal->name) {
> + /* Allocate name attribute component */
> + name_comp = kmalloc(sizeof(*name_comp), GFP_KERNEL);
> + if (!name_comp)
> + return -ENOMEM;
> + name_comp->name = signal->name;
> +
> + /* Allocate Signal name attribute */
> + err = counter_attribute_create(group, "", "name",
> + counter_device_attr_name_show, NULL, name_comp);
> + if (err) {
> + kfree(name_comp);
> + return err;
> + }
> + }
> +
> + /* Register Signal extension attributes */
> + return counter_signal_ext_register(group, signal);
> +}
> +
> +static int counter_signals_register(
> + struct counter_device_attr_group *const groups_list,
> + const struct counter_device *const counter)
> +{
> + const size_t num_signals = counter->num_signals;
> + struct counter_device_state *const device_state = counter->device_state;
> + struct device *const dev = &device_state->dev;
> + size_t i;
> + struct counter_signal *signal;
> + const char *name;
> + int err;
> +
> + /* At least one Signal must be defined */
> + if (!counter->signals || !num_signals) {
> + dev_err(dev, "Signals undefined\n");
> + return -EINVAL;
> + }
> +
> + /* Register each Signal */
> + for (i = 0; i < num_signals; i++) {
> + signal = counter->signals + i;
> +
> + /* Generate Signal attribute directory name */
> + name = kasprintf(GFP_KERNEL, "signal%d", signal->id);
> + if (!name)
> + return -ENOMEM;
> +
> + groups_list[i].attr_group.name = name;
> +
> + /* Create all attributes associated with Signal */
> + err = counter_signal_attributes_create(groups_list + i, counter,
> + signal);
> + if (err)
> + return err;
free name on error. The attr hasn't been added to the list yet so I don't think
it will be automatically freed.
> + }
> +
> + return 0;
> +}
> +
> +static const char *const synapse_action_str[] = {
> + [SYNAPSE_ACTION_NONE] = "none",
> + [SYNAPSE_ACTION_RISING_EDGE] = "rising edge",
> + [SYNAPSE_ACTION_FALLING_EDGE] = "falling edge",
> + [SYNAPSE_ACTION_BOTH_EDGES] = "both edges"
> +};
> +
> +struct action_comp_t {
> + struct counter_synapse *synapse;
> + struct counter_count *count;
> +};
> +
> +static ssize_t counter_action_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + int err;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + const struct action_comp_t *const component = devattr->component;
> + struct counter_count *const count = component->count;
> + struct counter_synapse *const synapse = component->synapse;
> + size_t action_index;
> + enum synapse_action action;
> +
> + err = counter->action_get(counter, count, synapse, &action_index);
> + if (err)
> + return err;
> +
> + synapse->action = action_index;
> +
> + action = synapse->actions_list[action_index];
> + return scnprintf(buf, PAGE_SIZE, "%s\n", synapse_action_str[action]);
> +}
> +
> +static ssize_t counter_action_store(struct device *dev,
> + struct device_attribute *attr, const char *buf, size_t len)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct action_comp_t *const component = devattr->component;
> + struct counter_synapse *const synapse = component->synapse;
> + size_t action_index;
> + const size_t num_actions = synapse->num_actions;
> + enum synapse_action action;
> + int err;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + struct counter_count *const count = component->count;
> +
> + /* Find requested action mode */
> + for (action_index = 0; action_index < num_actions; action_index++) {
> + action = synapse->actions_list[action_index];
> + if (sysfs_streq(buf, synapse_action_str[action]))
> + break;
> + }
> + /* If requested action mode not found */
> + if (action_index >= num_actions)
> + return -EINVAL;
> +
> + err = counter->action_set(counter, count, synapse, action_index);
> + if (err)
> + return err;
> +
> + synapse->action = action_index;
> +
> + return len;
> +}
> +
> +struct action_avail_comp_t {
> + const enum synapse_action *actions_list;
> + size_t num_actions;
> +};
> +
> +static ssize_t counter_synapse_action_available_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct action_avail_comp_t *const component = devattr->component;
> + const enum synapse_action *const actions_list = component->actions_list;
> + const size_t num_actions = component->num_actions;
> + size_t i;
> + enum synapse_action action;
> + ssize_t len = 0;
> +
> + for (i = 0; i < num_actions; i++) {
> + action = actions_list[i];
> + len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
> + synapse_action_str[action]);
> + }
> +
> + return len;
> +}
> +
> +static int counter_synapses_register(
> + struct counter_device_attr_group *const group,
> + const struct counter_device *const counter,
> + struct counter_count *const count, const char *const count_attr_name)
> +{
> + const size_t num_synapses = count->num_synapses;
> + struct device *const dev = &counter->device_state->dev;
> + size_t i;
> + struct counter_synapse *synapse;
> + const char *prefix;
> + struct action_comp_t *action_comp;
> + int err;
> + struct action_avail_comp_t *avail_comp;
> +
> + /* At least one Synapse must be defined */
> + if (!count->synapses || !num_synapses) {
> + dev_err(dev, "Count '%d' Synapses undefined\n", count->id);
> + return -EINVAL;
> + }
> +
> + /* Register each Synapse */
> + for (i = 0; i < num_synapses; i++) {
> + synapse = count->synapses + i;
> +
> + /* Ensure all Synapses have a defined Signal */
> + if (!synapse->signal) {
> + dev_err(dev,
> + "Count '%d' Synapse '%zu' Signal undefined\n",
> + count->id, i);
> + return -EINVAL;
> + }
> +
> + /* At least one action mode must be defined for each Synapse */
> + if (!synapse->actions_list || !synapse->num_actions) {
> + dev_err(dev,
> + "Count '%d' Signal '%d' action modes undefined\n",
> + count->id, synapse->signal->id);
> + return -EINVAL;
> + }
> +
> + /* Generate attribute prefix */
> + prefix = kasprintf(GFP_KERNEL, "signal%d_",
> + synapse->signal->id);
> + if (!prefix)
> + return -ENOMEM;
> +
> + /* Allocate action attribute component */
> + action_comp = kmalloc(sizeof(*action_comp), GFP_KERNEL);
> + if (!action_comp) {
> + err = -ENOMEM;
> + goto err_free_prefix;
> + }
> + action_comp->synapse = synapse;
> + action_comp->count = count;
> +
> + /* Create action attribute */
> + err = counter_attribute_create(group, prefix, "action",
> + (counter->action_get) ? counter_action_show : NULL,
> + (counter->action_set) ? counter_action_store : NULL,
> + action_comp);
> + if (err) {
> + kfree(action_comp);
> + goto err_free_prefix;
> + }
> +
> + /* Allocate action available attribute component */
> + avail_comp = kmalloc(sizeof(*avail_comp), GFP_KERNEL);
> + if (!avail_comp) {
> + err = -ENOMEM;
> + goto err_free_prefix;
> + }
> + avail_comp->actions_list = synapse->actions_list;
> + avail_comp->num_actions = synapse->num_actions;
> +
> + /* Create action_available attribute */
> + err = counter_attribute_create(group, prefix,
> + "action_available",
> + counter_synapse_action_available_show, NULL,
> + avail_comp);
> + if (err) {
> + kfree(avail_comp);
> + goto err_free_prefix;
> + }
> +
> + kfree(prefix);
> + }
> +
> + return 0;
> +
> +err_free_prefix:
> + kfree(prefix);
> + return err;
> +}
> +
> +struct count_comp_t {
> + struct counter_count *count;
> +};
> +
> +static ssize_t counter_count_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct count_comp_t *const component = devattr->component;
> + struct counter_count *const count = component->count;
> + int err;
> + struct count_read_value val = { .buf = buf };
> +
> + err = counter->count_read(counter, count, &val);
> + if (err)
> + return err;
> +
> + return val.len;
> +}
> +
> +static ssize_t counter_count_store(struct device *dev,
> + struct device_attribute *attr, const char *buf, size_t len)
> +{
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct count_comp_t *const component = devattr->component;
> + struct counter_count *const count = component->count;
> + int err;
> + struct count_write_value val = { .buf = buf };
> +
> + err = counter->count_write(counter, count, &val);
> + if (err)
> + return err;
> +
> + return len;
> +}
> +
> +static const char *const count_function_str[] = {
> + [COUNT_FUNCTION_INCREASE] = "increase",
> + [COUNT_FUNCTION_DECREASE] = "decrease",
> + [COUNT_FUNCTION_PULSE_DIRECTION] = "pulse-direction",
> + [COUNT_FUNCTION_QUADRATURE_X1] = "quadrature x1",
> + [COUNT_FUNCTION_QUADRATURE_X2] = "quadrature x2",
> + [COUNT_FUNCTION_QUADRATURE_X4] = "quadrature x4"
> +};
> +
> +static ssize_t counter_function_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + int err;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct count_comp_t *const component = devattr->component;
> + struct counter_count *const count = component->count;
> + size_t func_index;
> + enum count_function function;
> +
> + err = counter->function_get(counter, count, &func_index);
> + if (err)
> + return err;
> +
> + count->function = func_index;
> +
> + function = count->functions_list[func_index];
> + return scnprintf(buf, PAGE_SIZE, "%s\n", count_function_str[function]);
> +}
> +
> +static ssize_t counter_function_store(struct device *dev,
> + struct device_attribute *attr, const char *buf, size_t len)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct count_comp_t *const component = devattr->component;
> + struct counter_count *const count = component->count;
> + const size_t num_functions = count->num_functions;
> + size_t func_index;
> + enum count_function function;
> + int err;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> +
> + /* Find requested Count function mode */
> + for (func_index = 0; func_index < num_functions; func_index++) {
> + function = count->functions_list[func_index];
> + if (sysfs_streq(buf, count_function_str[function]))
> + break;
> + }
> + /* Return error if requested Count function mode not found */
> + if (func_index >= num_functions)
> + return -EINVAL;
> +
> + err = counter->function_set(counter, count, func_index);
> + if (err)
> + return err;
> +
> + count->function = func_index;
> +
> + return len;
> +}
> +
> +struct count_ext_comp_t {
> + struct counter_count *count;
> + const struct counter_count_ext *ext;
> +};
> +
> +static ssize_t counter_count_ext_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct count_ext_comp_t *const comp = devattr->component;
> + const struct counter_count_ext *const ext = comp->ext;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + struct counter_count *const count = comp->count;
> +
> + return ext->read(counter, count, ext->priv, buf);
Use comp->count directly and drop the local variable.
> +}
> +
> +static ssize_t counter_count_ext_store(struct device *dev,
> + struct device_attribute *attr, const char *buf, size_t len)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct count_ext_comp_t *const comp = devattr->component;
> + const struct counter_count_ext *const ext = comp->ext;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + struct counter_count *const count = comp->count;
> +
> + return ext->write(counter, count, ext->priv, buf, len);
> +}
> +
> +static int counter_count_ext_register(
> + struct counter_device_attr_group *const group,
> + struct counter_count *const count)
> +{
> + const size_t num_ext = count->num_ext;
> + size_t i;
> + const struct counter_count_ext *ext;
> + struct count_ext_comp_t *count_ext_comp;
> + int err;
> +
> + /* Return early if no extensions */
> + if (!count->ext || !num_ext)
> + return 0;
> +
> + /* Create an attribute for each extension */
> + for (i = 0 ; i < num_ext; i++) {
> + ext = count->ext + i;
> +
> + /* Allocate count_ext attribute component */
> + count_ext_comp = kmalloc(sizeof(*count_ext_comp), GFP_KERNEL);
> + if (!count_ext_comp)
> + return -ENOMEM;
> + count_ext_comp->count = count;
> + count_ext_comp->ext = ext;
> +
> + /* Allocate count_ext attribute */
> + err = counter_attribute_create(group, "", ext->name,
> + (ext->read) ? counter_count_ext_show : NULL,
> + (ext->write) ? counter_count_ext_store : NULL,
> + count_ext_comp);
> + if (err) {
> + kfree(count_ext_comp);
> + return err;
> + }
> + }
> +
> + return 0;
> +}
> +
> +struct func_avail_comp_t {
> + const enum count_function *functions_list;
> + size_t num_functions;
> +};
> +
> +static ssize_t counter_count_function_available_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct func_avail_comp_t *const component = devattr->component;
> + const enum count_function *const func_list = component->functions_list;
> + const size_t num_functions = component->num_functions;
> + size_t i;
> + enum count_function function;
> + ssize_t len = 0;
> +
> + for (i = 0; i < num_functions; i++) {
> + function = func_list[i];
> + len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
> + count_function_str[function]);
> + }
> +
> + return len;
> +}
> +
> +static int counter_count_attributes_create(
> + struct counter_device_attr_group *const group,
> + const struct counter_device *const counter,
> + struct counter_count *const count)
> +{
> + struct count_comp_t *count_comp;
> + int err;
> + struct count_comp_t *func_comp;
> + struct func_avail_comp_t *avail_comp;
> + struct name_comp_t *name_comp;
> +
> + /* Allocate count attribute component */
> + count_comp = kmalloc(sizeof(*count_comp), GFP_KERNEL);
> + if (!count_comp)
> + return -ENOMEM;
> + count_comp->count = count;
> +
> + /* Create main Count attribute */
> + err = counter_attribute_create(group, "", "count",
> + (counter->count_read) ? counter_count_show : NULL,
> + (counter->count_write) ? counter_count_store : NULL,
> + count_comp);
> + if (err) {
> + kfree(count_comp);
> + return err;
> + }
> +
> + /* Allocate function attribute component */
> + func_comp = kmalloc(sizeof(*func_comp), GFP_KERNEL);
> + if (!func_comp)
> + return -ENOMEM;
> + func_comp->count = count;
> +
> + /* Create Count function attribute */
> + err = counter_attribute_create(group, "", "function",
> + (counter->function_get) ? counter_function_show : NULL,
> + (counter->function_set) ? counter_function_store : NULL,
> + func_comp);
> + if (err) {
> + kfree(func_comp);
> + return err;
> + }
> +
> + /* Allocate function available attribute component */
> + avail_comp = kmalloc(sizeof(*avail_comp), GFP_KERNEL);
> + if (!avail_comp)
> + return -ENOMEM;
> + avail_comp->functions_list = count->functions_list;
> + avail_comp->num_functions = count->num_functions;
> +
> + /* Create Count function_available attribute */
> + err = counter_attribute_create(group, "", "function_available",
> + counter_count_function_available_show, NULL, avail_comp);
> + if (err) {
> + kfree(avail_comp);
> + return err;
> + }
> +
> + /* Create Count name attribute */
> + if (count->name) {
> + /* Allocate name attribute component */
> + name_comp = kmalloc(sizeof(*name_comp), GFP_KERNEL);
> + if (!name_comp)
> + return -ENOMEM;
> + name_comp->name = count->name;
> +
> + err = counter_attribute_create(group, "", "name",
> + counter_device_attr_name_show, NULL, name_comp);
> + if (err) {
> + kfree(name_comp);
> + return err;
> + }
> + }
> +
> + /* Register Count extension attributes */
> + return counter_count_ext_register(group, count);
> +}
> +
> +static int counter_counts_register(
> + struct counter_device_attr_group *const groups_list,
> + const struct counter_device *const counter)
> +{
> + const size_t num_counts = counter->num_counts;
> + struct device *const dev = &counter->device_state->dev;
> + size_t i;
> + struct counter_count *count;
> + const char *name;
> + int err;
> +
> + /* At least one Count must be defined */
> + if (!counter->counts || !num_counts) {
> + dev_err(dev, "Counts undefined\n");
> + return -EINVAL;
> + }
> +
> + /* Register each Count */
> + for (i = 0; i < num_counts; i++) {
> + count = counter->counts + i;
Is counter->counts ever set anywhere?
> +
> + /* At least one function mode must be defined for each Count */
> + if (!count->functions_list || !count->num_functions) {
> + dev_err(dev, "Count '%d' function modes undefined\n",
> + count->id);
> + return -EINVAL;
> + }
> +
> + /* Generate Count attribute directory name */
> + name = kasprintf(GFP_KERNEL, "count%d", count->id);
> + if (!name)
> + return -ENOMEM;
> + groups_list[i].attr_group.name = name;
> +
> + /* Register the Synapses associated with each Count */
> + err = counter_synapses_register(groups_list + i, counter, count,
> + name);
> + if (err)
Handling, would expect errors to cause a direct unwind of antying this
function has done that isn't automatically handled elsewhere. So far we
have set the name to some memory allocated but nothing else. I think
that needs freeing as we haven't added it to the list yet?
> + return err;
> +
> + /* Create all attributes associated with Count */
> + err = counter_count_attributes_create(groups_list + i, counter,
> + count);
> + if (err)
> + return err;
> + }
> +
> + return 0;
> +}
> +
> +static struct bus_type counter_bus_type = {
> + .name = "counter"
> +};
> +
> +static int __init counter_init(void)
> +{
> + return bus_register(&counter_bus_type);
> +}
> +
> +static void __exit counter_exit(void)
> +{
> + bus_unregister(&counter_bus_type);
> +}
I would expect these to be found next to the init and exit function
usage in subsystem_initcall and friends.
> +
> +static void free_counter_device_attr_list(struct list_head *attr_list)
> +{
> + struct counter_device_attr *p, *n;
> +
> + list_for_each_entry_safe(p, n, attr_list, l) {
> + kfree(p->dev_attr.attr.name);
> + kfree(p->component);
> + list_del(&p->l);
> + kfree(p);
> + }
> +}
> +
> +static void free_counter_device_groups_list(
> + struct counter_device_state *const device_state)
> +{
> + struct counter_device_attr_group *group;
> + size_t i;
> +
> + for (i = 0; i < device_state->num_groups; i++) {
> + group = device_state->groups_list + i;
> +
> + kfree(group->attr_group.name);
> + kfree(group->attr_group.attrs);
> + free_counter_device_attr_list(&group->attr_list);
> + }
> +
> + kfree(device_state->groups_list);
> +}
> +
> +/* Provides a unique ID for each counter device */
> +static DEFINE_IDA(counter_ida);
> +
> +static void counter_device_release(struct device *dev)
> +{
> + struct counter_device *const counter = dev_get_drvdata(dev);
> + struct counter_device_state *const device_state = counter->device_state;
> +
> + kfree(device_state->groups);
> + free_counter_device_groups_list(device_state);
> + ida_simple_remove(&counter_ida, device_state->id);
> + kfree(device_state);
> +}
> +
> +static struct device_type counter_device_type = {
> + .name = "counter_device",
> + .release = counter_device_release
> +};
> +
> +struct ext_comp_t {
> + const struct counter_device_ext *ext;
> +};
> +
> +static ssize_t counter_device_ext_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct ext_comp_t *const component = devattr->component;
> + const struct counter_device_ext *const ext = component->ext;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> +
> + return ext->read(counter, ext->priv, buf);
Can thin down some of the local variables.
return ext->read(dev_get_drvdata(dev), ext->priv, buf);
> +}
> +
> +static ssize_t counter_device_ext_store(struct device *dev,
> + struct device_attribute *attr, const char *buf, size_t len)
> +{
> + const struct counter_device_attr *const devattr = to_counter_attr(attr);
> + const struct ext_comp_t *const component = devattr->component;
> + const struct counter_device_ext *const ext = component->ext;
> + struct counter_device *const counter = dev_get_drvdata(dev);
> +
> + return ext->write(counter, ext->priv, buf, len);
> +}
> +
> +static int counter_device_ext_register(
> + struct counter_device_attr_group *const group,
> + struct counter_device *const counter)
> +{
> + const size_t num_ext = counter->num_ext;
> + struct ext_comp_t *ext_comp;
> + size_t i;
> + const struct counter_device_ext *ext;
> + int err;
> +
> + /* Return early if no extensions */
> + if (!counter->ext || !num_ext)
> + return 0;
> +
> + /* Create an attribute for each extension */
> + for (i = 0 ; i < num_ext; i++) {
> + ext = counter->ext + i;
> +
> + /* Allocate extension attribute component */
> + ext_comp = kmalloc(sizeof(*ext_comp), GFP_KERNEL);
> + if (!ext_comp)
> + return -ENOMEM;
> + ext_comp->ext = ext;
> +
> + /* Allocate extension attribute */
> + err = counter_attribute_create(group, "", ext->name,
> + (ext->read) ? counter_device_ext_show : NULL,
> + (ext->write) ? counter_device_ext_store : NULL,
> + ext_comp);
> + if (err) {
> + kfree(ext_comp);
> + return err;
> + }
> + }
> +
> + return 0;
> +}
> +
> +/**
> + * counter_register - register Counter to the system
> + * @counter: pointer to Counter to register
> + *
> + * This function registers a Counter to the system. A sysfs "counter" directory
> + * will be created and populated with sysfs attributes correlating with the
> + * Counter Signals, Synapses, and Counts respectively.
> + */
> +int counter_register(struct counter_device *const counter)
> +{
> + struct counter_device_state *device_state;
> + int err;
> + size_t i = 0;
> + size_t groups_offset = 0;
> + struct name_comp_t *name_comp;
> + struct counter_device_attr_group *group;
> + size_t j;
> + struct counter_device_attr *p;
> +
> + if (!counter)
> + return -EINVAL;
This one seems a little crazy to handle in here. I really hope we know
that before we try registering it!
> +
> + /* Allocate internal state container for Counter device */
> + device_state = kzalloc(sizeof(*device_state), GFP_KERNEL);
> + if (!device_state)
> + return -ENOMEM;
> + counter->device_state = device_state;
> +
> + /* Acquire unique ID */
> + device_state->id = ida_simple_get(&counter_ida, 0, 0, GFP_KERNEL);
> + if (device_state->id < 0) {
> + err = device_state->id;
> + goto err_free_device_state;
> + }
> +
> + /* Configure device structure for Counter */
> + device_state->dev.type = &counter_device_type;
> + device_state->dev.bus = &counter_bus_type;
> + if (counter->parent) {
> + device_state->dev.parent = counter->parent;
> + device_state->dev.of_node = counter->parent->of_node;
> + }
> + dev_set_name(&device_state->dev, "counter%d", device_state->id);
> + device_initialize(&device_state->dev);
> + dev_set_drvdata(&device_state->dev, counter);
> +
> + /* Allocate space for attribute groups (signals. counts, and ext) */
> + device_state->num_groups =
> + counter->num_signals + counter->num_counts + 1;
> + device_state->groups_list = kcalloc(device_state->num_groups,
> + sizeof(*device_state->groups_list), GFP_KERNEL);
If you want to use a unified function to 'undo' this block
free_counter_device_groups_list then I would expect to see this
block as a separate function to which that one is matched.
The flow here is rather complex vs the remove. Not sure there is an
easy way to clean it up though.
> + if (!device_state->groups_list) {
> + err = -ENOMEM;
> + goto err_free_id;
> + }
> +
> + /* Initialize attribute lists */
> + for (i = 0; i < device_state->num_groups; i++)
> + INIT_LIST_HEAD(&device_state->groups_list[i].attr_list);
> +
> + /* Verify Signals are valid and register */
> + err = counter_signals_register(device_state->groups_list, counter);
> + if (err)
> + goto err_free_groups_list;
> + groups_offset += counter->num_signals;
> +
> + /* Verify Counts and respective Synapses are valid and register */
> + err = counter_counts_register(device_state->groups_list + groups_offset,
> + counter);
> + if (err)
> + goto err_free_groups_list;
> + groups_offset += counter->num_counts;
> +
> + /* Register Counter device extension attributes */
> + err = counter_device_ext_register(
> + device_state->groups_list + groups_offset, counter);
> + if (err)
> + goto err_free_groups_list;
> +
> + /* Account for name attribute */
> + if (counter->name) {
> + /* Allocate name attribute component */
> + name_comp = kmalloc(sizeof(*name_comp), GFP_KERNEL);
> + if (!name_comp) {
> + err = -ENOMEM;
> + goto err_free_groups_list;
> + }
> + name_comp->name = counter->name;
> +
> + err = counter_attribute_create(
> + device_state->groups_list + groups_offset, "", "name",
> + counter_device_attr_name_show, NULL, name_comp);
> + if (err) {
> + kfree(name_comp);
> + goto err_free_groups_list;
> + }
> + }
> +
> + /* Allocate attribute groups for association with device */
> + device_state->groups = kcalloc(device_state->num_groups + 1,
> + sizeof(*device_state->groups), GFP_KERNEL);
> + if (!device_state->groups) {
> + err = -ENOMEM;
> + goto err_free_groups_list;
> + }
> + /* Prepare each group of attributes for association */
> + for (i = 0; i < device_state->num_groups; i++) {
> + group = device_state->groups_list + i;
> +
> + /* Allocate space for attribute pointers in attribute group */
> + group->attr_group.attrs = kcalloc(group->num_attr + 1,
> + sizeof(*group->attr_group.attrs), GFP_KERNEL);
> + if (!group->attr_group.attrs) {
> + err = -ENOMEM;
> + goto err_free_groups;
> + }
> +
> + /* Add attribute pointers to attribute group */
> + j = 0;
> + list_for_each_entry(p, &group->attr_list, l)
> + group->attr_group.attrs[j++] = &p->dev_attr.attr;
> +
> + /* Group attributes in attribute group */
> + device_state->groups[i] = &group->attr_group;
> + }
> + /* Associate attributes with device */
> + device_state->dev.groups = device_state->groups;
> +
> + /* Add device to system */
> + err = device_add(&device_state->dev);
> + if (err)
> + goto err_free_groups;
> +
> + return 0;
> +
Given release is responsible for the normal cleanup, it would have
been easier to review if we had release as the next function after
this one.
> +err_free_groups:
> + kfree(counter->device_state->groups);
I think you are always fine with device_state->* using local variable.
> +err_free_groups_list:
> + free_counter_device_groups_list(counter->device_state);
> +err_free_id:
> + ida_simple_remove(&counter_ida, counter->device_state->id);
> +err_free_device_state:
> + kfree(counter->device_state);
> + return err;
> +}
> +EXPORT_SYMBOL(counter_register);
> +
> +/**
> + * counter_unregister - unregister Counter from the system
> + * @counter: pointer to Counter to unregister
> + *
> + * The Counter is unregistered from the system; all allocated memory is freed.
> + */
> +void counter_unregister(struct counter_device *const counter)
> +{
> + if (counter)
> + device_del(&counter->device_state->dev);
> +}
> +EXPORT_SYMBOL(counter_unregister);
> +
> +static void devm_counter_unreg(struct device *dev, void *res)
> +{
> + counter_unregister(*(struct counter_device **)res);
> +}
> +
> +/**
> + * devm_counter_register - Resource-managed counter_register
> + * @dev: device to allocate counter_device for
> + * @counter: pointer to Counter to register
> + *
> + * Managed counter_register. The Counter registered with this function is
> + * automatically unregistered on driver detach. This function calls
> + * counter_register internally. Refer to that function for more information.
> + *
> + * If an Counter registered with this function needs to be unregistered
> + * separately, devm_counter_unregister must be used.
> + *
> + * RETURNS:
> + * 0 on success, negative error number on failure.
> + */
> +int devm_counter_register(struct device *dev,
> + struct counter_device *const counter)
Where possible align with the opening bracket.
checkpatch.pl --strict (but take into account some of the warnings will
be silly so don't fix them all).
> +{
> + struct counter_device **ptr;
> + int ret;
> +
> + ptr = devres_alloc(devm_counter_unreg, sizeof(*ptr), GFP_KERNEL);
> + if (!ptr)
> + return -ENOMEM;
> +
> + ret = counter_register(counter);
> + if (!ret) {
> + *ptr = counter;
> + devres_add(dev, ptr);
> + } else
> + devres_free(ptr);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL(devm_counter_register);
> +
> +static int devm_counter_match(struct device *dev, void *res, void *data)
> +{
> + struct counter_device **r = res;
> +
> + if (!r || !*r) {
> + WARN_ON(!r || !*r);
> + return 0;
> + }
> +
> + return *r == data;
> +}
> +
> +/**
> + * devm_counter_unregister - Resource-managed counter_unregister
> + * @dev: device this counter_device belongs to
> + * @counter: pointer to Counter associated with the device
> + *
> + * Unregister Counter registered with devm_counter_register.
> + */
> +void devm_counter_unregister(struct device *dev,
> + struct counter_device *const counter)
> +{
> + int rc;
> +
> + rc = devres_release(dev, devm_counter_unreg,
> + devm_counter_match, counter);
> + WARN_ON(rc);
> +}
> +EXPORT_SYMBOL(devm_counter_unregister);
> +
> +subsys_initcall(counter_init);
> +module_exit(counter_exit);
> +
> +MODULE_AUTHOR("William Breathitt Gray <[email protected]>");
> +MODULE_DESCRIPTION("Generic Counter interface");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/iio/counter/stm32-lptimer-cnt.c b/drivers/counter/stm32-lptimer-cnt.c
> similarity index 100%
> rename from drivers/iio/counter/stm32-lptimer-cnt.c
> rename to drivers/counter/stm32-lptimer-cnt.c
> diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
> index b3c8c6ef0dff..423d86e368ea 100644
> --- a/drivers/iio/Kconfig
> +++ b/drivers/iio/Kconfig
> @@ -73,7 +73,6 @@ source "drivers/iio/adc/Kconfig"
> source "drivers/iio/amplifiers/Kconfig"
> source "drivers/iio/chemical/Kconfig"
> source "drivers/iio/common/Kconfig"
> -source "drivers/iio/counter/Kconfig"
> source "drivers/iio/dac/Kconfig"
> source "drivers/iio/dummy/Kconfig"
> source "drivers/iio/frequency/Kconfig"
> diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
> index b16b2e9ddc40..6a80ebddb54c 100644
> --- a/drivers/iio/Makefile
> +++ b/drivers/iio/Makefile
> @@ -19,7 +19,6 @@ obj-y += amplifiers/
> obj-y += buffer/
> obj-y += chemical/
> obj-y += common/
> -obj-y += counter/
Hmm. I would do this later after you have everything up to move the drivers
over.
Just adds noise to this complex patch.
> obj-y += dac/
> obj-y += dummy/
> obj-y += gyro/
> diff --git a/include/linux/counter.h b/include/linux/counter.h
> new file mode 100644
> index 000000000000..aefca382624e
> --- /dev/null
> +++ b/include/linux/counter.h
> @@ -0,0 +1,524 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Counter interface
> + * Copyright (C) 2017 William Breathitt Gray
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License, version 2, as
> + * published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * General Public License for more details.
> + */
> +#ifndef _COUNTER_H_
> +#define _COUNTER_H_
> +
> +#include <linux/device.h>
> +#include <linux/types.h>
> +
> +struct counter_device;
> +struct counter_signal;
> +
> +/**
> + * struct counter_signal_ext - Counter Signal extensions
> + * @name: attribute name
> + * @read: read callback for this attribute; may be NULL
> + * @write: write callback for this attribute; may be NULL
> + * @priv: data private to the driver
> + */
> +struct counter_signal_ext {
> + const char *name;
> + ssize_t (*read)(struct counter_device *counter,
> + struct counter_signal *signal, void *priv,
> + char *buf);
> + ssize_t (*write)(struct counter_device *counter,
> + struct counter_signal *signal, void *priv,
> + const char *buf, size_t len);
> + void *priv;
> +};
> +
> +/**
> + * struct counter_signal - Counter Signal node
> + * @id: unique ID used to identify signal
> + * @name: device-specific Signal name; ideally, this should match the name
> + * as it appears in the datasheet documentation
> + * @ext: optional array of Counter Signal extensions
> + * @num_ext: number of Counter Signal extensions specified in @ext
> + * @priv: optional private data supplied by driver
> + */
> +struct counter_signal {
> + int id;
> + const char *name;
> +
> + const struct counter_signal_ext *ext;
> + size_t num_ext;
> +
> + void *priv;
> +};
> +
> +/**
> + * struct counter_signal_enum_ext - Signal enum extension attribute
> + * @items: Array of strings
> + * @num_items: Number of items specified in @items
> + * @set: Set callback function; may be NULL
> + * @get: Get callback function; may be NULL
> + *
> + * The counter_signal_enum_ext structure can be used to implement enum style
> + * Signal extension attributes. Enum style attributes are those which have a set
> + * of strings that map to unsigned integer values. The Generic Counter Signal
> + * enum extension helper code takes care of mapping between value and string, as
> + * well as generating a "_available" file which contains a list of all available
> + * items. The get callback is used to query the currently active item; the index
> + * of the item within the respective items array is returned via the 'item'
> + * parameter. The set callback is called when the attribute is updated; the
> + * 'item' parameter contains the index of the newly activated item within the
> + * respective items array.
> + */
> +struct counter_signal_enum_ext {
> + const char * const *items;
> + size_t num_items;
> + int (*get)(struct counter_device *counter,
> + struct counter_signal *signal,
> + size_t *item);
> + int (*set)(struct counter_device *counter,
> + struct counter_signal *signal,
> + size_t item);
> +};
> +
> +extern ssize_t counter_signal_enum_read(struct counter_device *counter,
> + struct counter_signal *signal, void *priv, char *buf);
> +extern ssize_t counter_signal_enum_write(struct counter_device *counter,
> + struct counter_signal *signal, void *priv, const char *buf, size_t len);
> +
> +/**
> + * COUNTER_SIGNAL_ENUM() - Initialize Signal enum extension
> + * @_name: Attribute name
> + * @_e: Pointer to a counter_count_enum structure
> + *
> + * This should usually be used together with COUNTER_SIGNAL_ENUM_AVAILABLE()
> + */
> +#define COUNTER_SIGNAL_ENUM(_name, _e) \
> +{ \
> + .name = (_name), \
> + .read = counter_signal_enum_read, \
> + .write = counter_signal_enum_write, \
> + .priv = (_e) \
> +}
> +
> +extern ssize_t counter_signal_enum_available_read(
> + struct counter_device *counter, struct counter_signal *signal,
> + void *priv, char *buf);
> +
> +/**
> + * COUNTER_SIGNAL_ENUM_AVAILABLE() - Initialize Signal enum available extension
> + * @_name: Attribute name ("_available" will be appended to the name)
> + * @_e: Pointer to a counter_signal_enum structure
> + *
> + * Creates a read only attribute that lists all the available enum items in a
> + * newline separated list. This should usually be used together with
> + * COUNTER_SIGNAL_ENUM()
> + */
> +#define COUNTER_SIGNAL_ENUM_AVAILABLE(_name, _e) \
> +{ \
> + .name = (_name "_available"), \
> + .read = counter_signal_enum_available_read, \
> + .priv = (_e) \
> +}
> +
> +enum synapse_action {
> + SYNAPSE_ACTION_NONE = 0,
> + SYNAPSE_ACTION_RISING_EDGE,
> + SYNAPSE_ACTION_FALLING_EDGE,
> + SYNAPSE_ACTION_BOTH_EDGES
> +};
> +
> +/**
> + * struct counter_synapse - Counter Synapse node
> + * @action: index of current action mode
> + * @actions_list: array of available action modes
> + * @num_actions: number of action modes specified in @actions_list
> + * @signal: pointer to associated signal
> + */
> +struct counter_synapse {
> + size_t action;
> + const enum synapse_action *actions_list;
> + size_t num_actions;
> +
> + struct counter_signal *signal;
> +};
> +
> +struct counter_count;
> +
> +/**
> + * struct counter_count_ext - Counter Count extension
> + * @name: attribute name
> + * @read: read callback for this attribute; may be NULL
> + * @write: write callback for this attribute; may be NULL
> + * @priv: data private to the driver
> + */
> +struct counter_count_ext {
> + const char *name;
> + ssize_t (*read)(struct counter_device *counter,
> + struct counter_count *count, void *priv,
> + char *buf);
> + ssize_t (*write)(struct counter_device *counter,
> + struct counter_count *count, void *priv,
> + const char *buf, size_t len);
> + void *priv;
> +};
> +
> +enum count_function {
> + COUNT_FUNCTION_INCREASE = 0,
> + COUNT_FUNCTION_DECREASE,
> + COUNT_FUNCTION_PULSE_DIRECTION,
> + COUNT_FUNCTION_QUADRATURE_X1,
> + COUNT_FUNCTION_QUADRATURE_X2,
> + COUNT_FUNCTION_QUADRATURE_X4
> +};
> +
> +/**
> + * struct counter_count - Counter Count node
> + * @id: unique ID used to identify Count
> + * @name: device-specific Count name; ideally, this should match
> + * the name as it appears in the datasheet documentation
> + * @function: index of current function mode
> + * @functions_list: array available function modes
> + * @num_functions: number of function modes specified in @functions_list
> + * @synapses: array of synapses for initialization
> + * @num_synapses: number of synapses specified in @synapses
> + * @ext: optional array of Counter Count extensions
> + * @num_ext: number of Counter Count extensions specified in @ext
> + * @priv: optional private data supplied by driver
> + */
> +struct counter_count {
> + int id;
> + const char *name;
> +
> + size_t function;
> + const enum count_function *functions_list;
> + size_t num_functions;
> +
> + struct counter_synapse *synapses;
> + size_t num_synapses;
> +
> + const struct counter_count_ext *ext;
> + size_t num_ext;
> +
> + void *priv;
> +};
> +
> +/**
> + * struct counter_count_enum_ext - Count enum extension attribute
> + * @items: Array of strings
> + * @num_items: Number of items specified in @items
> + * @set: Set callback function; may be NULL
> + * @get: Get callback function; may be NULL
> + *
> + * The counter_count_enum_ext structure can be used to implement enum style
> + * Count extension attributes. Enum style attributes are those which have a set
> + * of strings that map to unsigned integer values. The Generic Counter Count
> + * enum extension helper code takes care of mapping between value and string, as
> + * well as generating a "_available" file which contains a list of all available
> + * items. The get callback is used to query the currently active item; the index
> + * of the item within the respective items array is returned via the 'item'
> + * parameter. The set callback is called when the attribute is updated; the
> + * 'item' parameter contains the index of the newly activated item within the
> + * respective items array.
> + */
> +struct counter_count_enum_ext {
> + const char * const *items;
> + size_t num_items;
> + int (*get)(struct counter_device *counter,
> + struct counter_count *count,
> + size_t *item);
> + int (*set)(struct counter_device *counter,
> + struct counter_count *count,
> + size_t item);
> +};
> +
> +extern ssize_t counter_count_enum_read(struct counter_device *counter,
> + struct counter_count *count, void *priv, char *buf);
> +extern ssize_t counter_count_enum_write(struct counter_device *counter,
> + struct counter_count *count, void *priv, const char *buf, size_t len);
> +
> +/**
> + * COUNTER_COUNT_ENUM() - Initialize Count enum extension
> + * @_name: Attribute name
> + * @_e: Pointer to a counter_count_enum structure
> + *
> + * This should usually be used together with COUNTER_COUNT_ENUM_AVAILABLE()
> + */
> +#define COUNTER_COUNT_ENUM(_name, _e) \
> +{ \
> + .name = (_name), \
> + .read = counter_count_enum_read, \
> + .write = counter_count_enum_write, \
> + .priv = (_e) \
> +}
> +
> +extern ssize_t counter_count_enum_available_read(struct counter_device *counter,
> + struct counter_count *count, void *priv, char *buf);
> +
> +/**
> + * COUNTER_COUNT_ENUM_AVAILABLE() - Initialize Count enum available extension
> + * @_name: Attribute name ("_available" will be appended to the name)
> + * @_e: Pointer to a counter_count_enum structure
> + *
> + * Creates a read only attribute that lists all the available enum items in a
> + * newline separated list. This should usually be used together with
> + * COUNTER_COUNT_ENUM()
> + */
> +#define COUNTER_COUNT_ENUM_AVAILABLE(_name, _e) \
> +{ \
> + .name = (_name "_available"), \
> + .read = counter_count_enum_available_read, \
> + .priv = (_e) \
> +}
> +
> +/**
> + * struct counter_device_attr_group - internal container for attribute group
> + * @attr_group: Counter sysfs attributes group
> + * @attr_list: list to keep track of created Counter sysfs attributes
> + * @num_attr: number of Counter sysfs attributes
> + */
> +struct counter_device_attr_group {
> + struct attribute_group attr_group;
> + struct list_head attr_list;
> + size_t num_attr;
> +};
> +
> +/**
> + * struct counter_device_state - internal state container for a Counter device
> + * @id: unique ID used to identify the Counter
> + * @dev: internal device structure
> + * @groups_list attribute groups list (groups for Signals, Counts, and ext)
> + * @num_groups number of attribute groups containers
> + * @groups: Counter sysfs attribute groups (used to populate @dev.groups)
> + */
> +struct counter_device_state {
> + int id;
> + struct device dev;
> + struct counter_device_attr_group *groups_list;
> + size_t num_groups;
> + const struct attribute_group **groups;
> +};
> +
> +/**
> + * struct signal_read_value - Opaque Signal read value
> + * @buf: string representation of Signal read value
> + * @len: length of string in @buf
> + */
> +struct signal_read_value {
> + char *buf;
> + size_t len;
> +};
> +
> +/**
> + * struct count_read_value - Opaque Count read value
> + * @buf: string representation of Count read value
> + * @len: length of string in @buf
I wonder if you are ever going to want to have in kernel consumers.
Using strings this early level would make that hard.
I'm also unclear on why it makes sense to do so given count
is always an integer - Potentially things could get interesting
when you are either signed or unsigned and matching the number of
bits (s16, u16 or similar).
Given this is in kernel interface though, nothing stops you modifying
it later if you change your mind about this.
> + */
> +struct count_read_value {
> + char *buf;
> + size_t len;
> +};
> +
> +/**
> + * struct count_write_value - Opaque Count write value
> + * @buf: string representation of Count write value
> + */
> +struct count_write_value {
> + const char *buf;
> +};
> +
> +/**
> + * struct counter_device_ext - Counter device extension
> + * @name: attribute name
> + * @read: read callback for this attribute; may be NULL
> + * @write: write callback for this attribute; may be NULL
> + * @priv: data private to the driver
> + */
> +struct counter_device_ext {
> + const char *name;
> + ssize_t (*read)(struct counter_device *counter, void *priv,
> + char *buf);
> + ssize_t (*write)(struct counter_device *counter, void *priv,
> + const char *buf, size_t len);
> + void *priv;
> +};
> +
> +/**
> + * struct counter_device_enum_ext - Counter enum extension attribute
> + * @items: Array of strings
> + * @num_items: Number of items specified in @items
> + * @set: Set callback function; may be NULL
> + * @get: Get callback function; may be NULL
> + *
> + * The counter_device_enum_ext structure can be used to implement enum style
> + * Counter extension attributes. Enum style attributes are those which have a
> + * set of strings that map to unsigned integer values. The Generic Counter enum
> + * extension helper code takes care of mapping between value and string, as well
> + * as generating a "_available" file which contains a list of all available
> + * items. The get callback is used to query the currently active item; the index
> + * of the item within the respective items array is returned via the 'item'
> + * parameter. The set callback is called when the attribute is updated; the
> + * 'item' parameter contains the index of the newly activated item within the
> + * respective items array.
> + */
> +struct counter_device_enum_ext {
> + const char * const *items;
> + size_t num_items;
> + int (*get)(struct counter_device *counter,
> + size_t *item);
> + int (*set)(struct counter_device *counter,
> + size_t item);
> +};
> +
> +extern ssize_t counter_device_enum_read(struct counter_device *counter,
> + void *priv, char *buf);
> +extern ssize_t counter_device_enum_write(struct counter_device *counter,
> + void *priv, const char *buf, size_t len);
> +
> +/**
> + * COUNTER_DEVICE_ENUM() - Initialize Counter enum extension
> + * @_name: Attribute name
> + * @_e: Pointer to a counter_device_enum structure
> + *
> + * This should usually be used together with COUNTER_DEVICE_ENUM_AVAILABLE()
> + */
> +#define COUNTER_DEVICE_ENUM(_name, _e) \
> +{ \
> + .name = (_name), \
> + .read = counter_device_enum_read, \
> + .write = counter_device_enum_write, \
> + .priv = (_e) \
> +}
> +
> +extern ssize_t counter_device_enum_available_read(
> + struct counter_device *counter, void *priv, char *buf);
> +
> +/**
> + * COUNTER_DEVICE_ENUM_AVAILABLE() - Initialize Counter enum available extension
> + * @_name: Attribute name ("_available" will be appended to the name)
> + * @_e: Pointer to a counter_device_enum structure
> + *
> + * Creates a read only attribute that lists all the available enum items in a
> + * newline separated list. This should usually be used together with
> + * COUNTER_DEVICE_ENUM()
> + */
> +#define COUNTER_DEVICE_ENUM_AVAILABLE(_name, _e) \
> +{ \
> + .name = (_name "_available"), \
> + .read = counter_device_enum_available_read, \
> + .priv = (_e) \
> +}
> +
> +/**
> + * struct counter_device - Counter data structure
> + * @name: name of the device as it appears in the datasheet
> + * @parent: optional parent device providing the counters
> + * @device_state: internal device state container
> + * @signal_read: optional read callback for Signal attribute. The read
> + * value of the respective Signal should be passed back via
> + * the val parameter. val points to an opaque type which
> + * should be set only via the set_signal_read_value
> + * function.
> + * @count_read: optional read callback for Count attribute. The read
> + * value of the respective Count should be passed back via
> + * the val parameter. val points to an opaque type which
> + * should be set only via the set_count_read_value
> + * function.
> + * @count_write: optional write callback for Count attribute. The write
> + * value for the respective Count is passed in via the val
> + * parameter. val points to an opaque type which should be
> + * access only via the get_count_write_value function.
> + * @function_get: function to get the current count function mode. Returns
> + * 0 on success and negative error code on error. The index
> + * of the respective Count's returned function mode should
> + * be passed back via the function parameter.
> + * @function_set: function to set the count function mode. function is the
> + * index of the requested function mode from the respective
> + * Count's functions_list array.
> + * @action_get: function to get the current action mode. Returns 0 on
> + * success and negative error code on error. The index of
> + * the respective Signal's returned action mode should be
> + * passed back via the action parameter.
> + * @action_set: function to set the action mode. action is the index of
> + * the requested action mode from the respective Synapse's
> + * actions_list array.
> + * @signals: array of Signals
> + * @num_signals: number of Signals specified in @signals
> + * @counts: array of Counts
> + * @num_counts: number of Counts specified in @counts
> + * @ext: optional array of Counter device extensions
> + * @num_ext: number of Counter device extensions specified in @ext
> + * @priv: optional private data supplied by driver
> + */
> +struct counter_device {
> + const char *name;
> + struct device *parent;
> + struct counter_device_state *device_state;
> +
Ah, this email was still in my outbox! (see later comment that says
I missed this). I would pull all these function pointers out to an
ops structure. They will tend to be fixed for a given device
so there are lots of advantages in code simplicity and also being
able to make function pointers constant is always good for security.
> + int (*signal_read)(struct counter_device *counter,
> + struct counter_signal *signal,
> + struct signal_read_value *val);
> + int (*count_read)(struct counter_device *counter,
> + struct counter_count *count,
> + struct count_read_value *val);
> + int (*count_write)(struct counter_device *counter,
> + struct counter_count *count,
> + struct count_write_value *val);
> + int (*function_get)(struct counter_device *counter,
> + struct counter_count *count, size_t *function);
> + int (*function_set)(struct counter_device *counter,
> + struct counter_count *count, size_t function);
> + int (*action_get)(struct counter_device *counter,
> + struct counter_count *count,
> + struct counter_synapse *synapse, size_t *action);
> + int (*action_set)(struct counter_device *counter,
> + struct counter_count *count,
> + struct counter_synapse *synapse, size_t action);
> +
> + struct counter_signal *signals;
> + size_t num_signals;
> + struct counter_count *counts;
> + size_t num_counts;
> +
> + const struct counter_device_ext *ext;
> + size_t num_ext;
> +
> + void *priv;
> +};
> +
> +enum signal_level {
> + SIGNAL_LEVEL_LOW = 0,
> + SIGNAL_LEVEL_HIGH
> +};
> +
> +enum signal_value_type {
> + SIGNAL_LEVEL = 0
This one surprised me. Only one option?
> +};
> +
> +enum count_value_type {
> + COUNT_POSITION_UNSIGNED = 0,
> + COUNT_POSITION_SIGNED
> +};
> +
> +extern void set_signal_read_value(struct signal_read_value *const val,
> + const enum signal_value_type type, void *const data);
Why extern?
Also, where possible align parameters with the opening bracket.
> +extern void set_count_read_value(struct count_read_value *const val,
> + const enum count_value_type type, void *const data);
> +extern int get_count_write_value(void *const data,
> + const enum count_value_type type,
> + const struct count_write_value *const val);
> +
> +extern int counter_register(struct counter_device *const counter);
> +extern void counter_unregister(struct counter_device *const counter);
> +extern int devm_counter_register(struct device *dev,
> + struct counter_device *const counter);
> +extern void devm_counter_unregister(struct device *dev,
> + struct counter_device *const counter);
> +
> +#endif /* _COUNTER_H_ */
On Fri, 9 Mar 2018 13:42:48 -0500
William Breathitt Gray <[email protected]> wrote:
> This patch adds high-level documentation about the Generic Counter
> interface.
>
> Signed-off-by: William Breathitt Gray <[email protected]>
Other than the little issues Randy raised and one more inline from
me this looks good to me.
I would suggest you check whitespace in general to make sure there
are not other odd mixes of tabs and spaces hiding in here.
Reviewed-by: Jonathan Cameron <[email protected]>
> ---
> Documentation/driver-api/generic-counter.rst | 321 +++++++++++++++++++++++++++
> Documentation/driver-api/index.rst | 1 +
> MAINTAINERS | 1 +
> 3 files changed, 323 insertions(+)
> create mode 100644 Documentation/driver-api/generic-counter.rst
>
> diff --git a/Documentation/driver-api/generic-counter.rst b/Documentation/driver-api/generic-counter.rst
> new file mode 100644
> index 000000000000..bce0cbc31963
> --- /dev/null
> +++ b/Documentation/driver-api/generic-counter.rst
> @@ -0,0 +1,321 @@
> +=========================
> +Generic Counter Interface
> +=========================
> +
> +Introduction
> +============
> +
> +Counter devices are prevalent within a diverse spectrum of industries.
> +The ubiquitous presence of these devices necessitates a common interface
> +and standard of interaction and exposure. This driver API attempts to
> +resolve the issue of duplicate code found among existing counter device
> +drivers by introducing a generic counter interface for consumption. The
> +Generic Counter interface enables drivers to support and expose a common
> +set of components and functionality present in counter devices.
> +
> +Theory
> +======
> +
> +Counter devices can vary greatly in design, but regardless of whether
> +some devices are quadrature encoder counters or tally counters, all
> +counter devices consist of a core set of components. This core set of
> +components, shared by all counter devices, is what forms the essence of
> +the Generic Counter interface.
> +
> +There are three core components to a counter:
> +
> + COUNT
> + -----
> + A Count represents the count data for a set of Signals. The
> + Generic Counter interface provides the following available count
> + data types:
> +
> + * COUNT_POSITION_UNSIGNED:
> + Unsigned integer value representing position.
> +
> + * COUNT_POSITION_SIGNED:
> + Signed integer value representing position.
> +
> + A Count has a count function mode which represents the update
> + behavior for the count data. The Generic Counter interface
> + provides the following available count function modes:
> +
> + * Increase:
> + Accumulated count is incremented.
> +
> + * Decrease:
> + Accumulated count is decremented.
> +
> + * Pulse-Direction:
> + Rising edges on quadrature pair signal A updates
> + the respective count. The input level of
> + quadrature pair signal B determines direction.
> +
> + * Quadrature x1:
> + If direction is forward, rising edges on
> + quadrature pair signal A updates the respective
> + count; if the direction is backward, falling
> + edges on quadrature pair signal A updates the
> + respective count. Quadrature encoding determines
> + the direction.
> +
> + * Quadrature x2:
> + Any state transition on quadrature pair signal A
> + updates the respective count. Quadrature
> + encoding determines the direction.
> +
> + * Quadrature x4:
> + Any state transition on either quadrature pair
> + signals updates the respective count. Quadrature
> + encoding determines the direction.
> +
> + A Count has a set of one or more associated Signals.
> +
> + SIGNAL
> + ------
> + A Signal represents a counter input data; this is the input data
> + that is analyzed by the counter to determine the count data;
> + e.g. a quadrature signal output line of a rotary encoder. Not
> + all counter devices provide user access to the Signal data.
> +
> + The Generic Counter interface provides the following available
> + signal data types for when the Signal data is available for user
> + access:
> +
> + * SIGNAL_LEVEL_LOW:
> + Signal line is in a low state.
> +
> + * SIGNAL_LEVEL_HIGH:
> + Signal line is in a high state.
Something odd in formatting here as those two bullet points should
be aligned.
> +
> + A Signal may be associated to one or more Counts.
> +
> + SYNAPSE
> + -------
> + A Synapse represents the association of a Signal with a
> + respective Count. Signal data affects respective Count data, and
> + the Synapse represents this relationship.
> +
> + The Synapse action mode specifies the Signal data condition
> + which triggers the respective Count's count function evaluation
> + to update the count data. The Generic Counter interface provides
> + the following available action modes:
> +
> + * None:
> + Signal does not trigger the count function. In
> + Pulse-Direction count function mode, this Signal
> + is evaluated as Direction.
> + * Rising Edge:
> + Low state transitions to high state.
> + * Falling Edge:
> + High state transitions to low state.
> + * Both Edges:
> + Any state transition.
> +
> +
> +A counter is defined as a set of input signals associated to count data
> +that are generated by the evaluation of the state of the associated
> +input signals as defined by the respective count functions. Within the
> +context of the Generic Counter interface, a counter consists of Counts
> +each associated to a set of Signals, whose respective Synapse instances
> +represent the count function update conditions for the associated
> +Counts.
> +
> +Paradigm
> +========
> +
> +The most basic counter device may be expressed as a single Count
> +associated with a single Signal via a single Synapse. Take for example
> +a counter device which simply accumulates a count of rising edges on a
> +source input line.
> +
> + Count Synapse Signal
> + ----- ------- ------
> ++---------------------+
> +| Data: Count | Rising Edge ________
> +| Function: Increase | <------------- / Source \
> +| | ____________
> ++---------------------+
> +
> +In this example, the Signal is a source input line with a pulsing
> +voltage, while the Count is a persistent count value which is repeatedly
> +incremented. The Signal is associated with the respective Count via a
> +Synapse. The increase function is triggered by the Signal data condition
> +specified by the Synapse -- in this case a rising edge condition on the
> +voltage input line. In summary, the counter device existence and
> +behavior is aptly represented by respective Count, Signal, and Synapse
> +components: a rising edge condition triggers an increase function on an
> +accumulating count datum.
> +
> +A counter device is not limited to a single Signal; in fact, in theory
> +many Signals may be associated with even a single Count. For example, a
> +quadrature encoder counter device can keep track of position based on
> +the states of two input lines.
> +
> + Count Synapse Signal
> + ----- ------- ------
> ++-------------------------+
> +| Data: Position | Both Edges ___
> +| Function: Quadrature x4 | <------------ / A \
> +| | _______
> +| |
> +| | Both Edges ___
> +| | <------------ / B \
> +| | _______
> ++-------------------------+
> +
> +In this example, two Signals (quadrature encoder lines A and B) are
> +associated to a single Count: a rising or falling edge on either A or B
> +triggers the "Quadrature x4" function which determines the direction of
> +movement and updates the respective position data. The "Quadrature x4"
> +function is likely implemented in the hardware of the quadrature encoder
> +counter device; the Count, Signals, and Synapses simply represent this
> +hardware behavior and functionality.
> +
> +Signals associated to the same Count can have differing Synapse action
> +mode conditions. For example, a quadrature encoder counter device
> +operating in a non-quadrature Pulse-Direction mode could have one input
> +line dedicated for movement and a second input line dedicated for
> +direction.
> +
> + Count Synapse Signal
> + ----- ------- ------
> ++---------------------------+
> +| Data: Position | Rising Edge ___
> +| Function: Pulse-Direction | <------------- / A \ (Movement)
> +| | _______
> +| |
> +| | None ___
> +| | <------------- / B \ (Direction)
> +| | _______
> ++---------------------------+
> +
> +Only Signal A triggers the "Pulse-Direction" update function, but the
> +instantaneous state of Signal B is still required in order to know the
> +direction so that the position data may be properly updated. Ultimately,
> +both Signals are associated to the same Count via two respective
> +Synapses, but only one Synapse has an active action mode condition which
> +triggers the respective count function while the other is left with a
> +"None" condition action mode to indicate its respective Signal's
> +availability for state evaluation despite its non-triggering mode.
> +
> +Keep in mind that the Signal, Synapse, and Count are abstract
> +representations which do not need to be closely married to their
> +respective physical sources. This allows the user of a counter to
> +divorce themselves from the nuances of physical components (such as
> +whether an input line is differential or single-ended) and instead focus
> +on the core idea of what the data and process represent (e.g. position
> +as interpreted from quadrature encoding data).
> +
> +Userspace Interface
> +===================
> +
> +Several sysfs attributes are generated by the Generic Counter interface,
> +and reside under the /sys/bus/counter/devices/counterX directory, where
> +counterX refers to the respective counter device. Please see
> +Documentation/ABI/testing/sys-bus-counter-generic-sysfs for detailed
> +information on each Generic Counter interface sysfs attribute.
> +
> +Through these sysfs attributes, programs and scripts may interact with
> +the Generic Counter paradigm Counts, Signals, and Synapses of respective
> +counter devices.
> +
> +Driver API
> +==========
> +
> +Driver authors may utilize the Generic Counter interface in their code
> +by including the include/linux/iio/counter.h header file. This header
> +file provides several core data structures, function prototypes, and
> +macros for defining a counter device.
> +
> +.. kernel-doc:: include/linux/counter.h
> + :internal:
> +
> +.. kernel-doc:: drivers/counter/generic-counter.c
> + :export:
> +
> +Implementation
> +==============
> +
> +To support a counter device, a driver must first allocate the available
> +Counter Signals via counter_signal structures. These Signals should
> +be stored as an array and set to the signals array member of an
> +allocated counter_device structure before the Counter is registered to
> +the system.
> +
> +Counter Counts may be allocated via counter_count structures, and
> +respective Counter Signal associations (Synapses) made via
> +counter_synapse structures. Associated counter_synapse structures are
> +stored as an array and set to the the synapses array member of the
> +respective counter_count structure. These counter_count structures are
> +set to the counts array member of an allocated counter_device structure
> +before the Counter is registered to the system.
> +
> +Driver callbacks should be provided to the counter_device structure in
> +order to communicate with the device: to read and write various Signals
> +and Counts, and to set and get the "action mode" and "function mode" for
> +various Synapses and Counts respectively.
> +
> +A defined counter_device structure may be registered to the system by
> +passing it to the counter_register function, and unregistered by passing
> +it to the counter_unregister function. Similarly, the
> +devm_counter_register and devm_counter_unregister functions may be used
> +if device memory-managed registration is desired.
> +
> +Extension sysfs attributes can be created for auxiliary functionality
> +and data by passing in defined counter_device_ext, counter_count_ext,
> +and counter_signal_ext structures. In these cases, the
> +counter_device_ext structure is used for global configuration of the
> +respective Counter device, while the counter_count_ext and
> +counter_signal_ext structures allow for auxiliary exposure and
> +configuration of a specific Count or Signal respectively.
> +
> +Architecture
> +============
> +
> +When the Generic Counter interface counter module is loaded, the
> +counter_init function is called which registers a bus_type named
> +"counter" to the system. Subsequently, when the module is unloaded, the
> +counter_exit function is called which unregisters the bus_type named
> +"counter" from the system.
> +
> +Counter devices are registered to the system via the counter_register
> +function, and later removed via the counter_unregister function. The
> +counter_register function establishes a unique ID for the Counter
> +device and creates a respective sysfs directory, where X is the
> +mentioned unique ID:
> +
> + /sys/bus/counter/devices/counterX
> +
> +Sysfs attributes are created within the counterX directory to expose
> +functionality, configurations, and data relating to the Counts, Signals,
> +and Synapses of the Counter device, as well as options and information
> +for the Counter device itself.
> +
> +Each Signal has a directory created to house its relevant sysfs
> +attributes, where Y is the unique ID of the respective Signal:
> +
> + /sys/bus/counter/devices/counterX/signalY
> +
> +Similarly, each Count has a directory created to house its relevant
> +sysfs attributes, where Y is the unique ID of the respective Count:
> +
> + /sys/bus/counter/devices/counterX/countY
> +
> +For a more detailed breakdown of the available Generic Counter interface
> +sysfs attributes, please refer to the
> +Documentation/ABI/testing/sys-bus-counter file.
> +
> +The Signals and Counts associated with the Counter device are registered
> +to the system as well by the counter_register function. The
> +signal_read/signal_write driver callbacks are associated to their
> +respective Signal attributes, while the count_read/count_write and
> +function_get/function_set driver callbacks are associated to their
> +respective Count attributes; similarly, the same is true for the
> +action_get/action_set driver callbacks and their respective Synapse
> +attributes. If a driver callback is left undefined, then the respective
> +read/write permission is left disabled for the relevant attributes.
> +
> +Similarly, extension sysfs attributes are created for the defined
> +counter_device_ext, counter_count_ext, and counter_signal_ext
> +structures that are passed in.
> diff --git a/Documentation/driver-api/index.rst b/Documentation/driver-api/index.rst
> index e9b41b1634f3..57e6e2c1d063 100644
> --- a/Documentation/driver-api/index.rst
> +++ b/Documentation/driver-api/index.rst
> @@ -25,6 +25,7 @@ available subsections can be seen below.
> frame-buffer
> regulator
> iio/index
> + generic-counter
> input
> usb/index
> pci
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 2a7bf2f84272..a71dff6eae87 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -3673,6 +3673,7 @@ M: William Breathitt Gray <[email protected]>
> L: [email protected]
> S: Maintained
> F: Documentation/ABI/testing/sysfs-bus-counter*
> +F: Documentation/driver-api/generic-counter.rst
> F: drivers/counter/
> F: include/linux/counter.h
>
On Fri, 9 Mar 2018 13:42:35 -0500
William Breathitt Gray <[email protected]> wrote:
> This patch adds standard documentation for the userspace sysfs
> attributes of the Generic Counter interface.
>
> Signed-off-by: William Breathitt Gray <[email protected]>
Good, few comments inline.
Jonathan
> ---
> Documentation/ABI/testing/sysfs-bus-counter | 120 ++++++++++++++++++++++++++++
> MAINTAINERS | 1 +
> 2 files changed, 121 insertions(+)
> create mode 100644 Documentation/ABI/testing/sysfs-bus-counter
>
> diff --git a/Documentation/ABI/testing/sysfs-bus-counter b/Documentation/ABI/testing/sysfs-bus-counter
> new file mode 100644
> index 000000000000..64785aab1763
> --- /dev/null
> +++ b/Documentation/ABI/testing/sysfs-bus-counter
> @@ -0,0 +1,120 @@
> +What: /sys/bus/counter/devices/counterX/countY/count
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Count data of Count Y. The following data types are available:
> +
> + COUNT_POSITION_UNSIGNED:
> + Unsigned integer value representing position.
> +
> + COUNT_POSITION_SIGNED:
> + Signed integer value representing position.
Why are these data types relevant in this document?
You are outputting a string. Userspace doesn't really need to know
more than that.
> +
> +What: /sys/bus/counter/devices/counterX/countY/function
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Count function mode of Count Y; count function evaluation is
> + triggered by conditions specified by the countY_signalZ_action
> + attributes. The following count functions are available:
> +
> + Increase:
> + Accumulated count is incremented.
> +
> + Decrease:
> + Accumulated count is decremented.
> +
> + Pulse-Direction:
> + Rising edges on quadrature pair signal A updates the
> + respective count. The input level of quadrature pair
> + signal B determines direction.
> +
> + Quadrature x1:
> + If direction is forward, rising edges on quadrature pair
> + signal A updates the respective count; if the direction
> + is backward, falling edges on quadrature pair signal A
> + updates the respective count. Quadrature encoding
> + determines the direction.
> +
> + Quadrature x2:
> + Any state transition on quadrature pair signal A updates
> + the respective count. Quadrature encoding determines the
> + direction.
> +
> + Quadrature x4:
> + Any state transition on either quadrature pair signals
> + updates the respective count. Quadrature encoding
> + determines the direction.
> +
> +What: /sys/bus/counter/devices/counterX/countY/function_available
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Discrete set of available count function modes for the
> + configuration of the respective Count Y are listed in this file.
How is this list delineated? There are several competing options in different
kernel subsystems so this isn't exactly obvious ;)
> +
> +What: /sys/bus/counter/devices/counterX/countY/name
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Read-only attribute that indicates the device-specific name of
> + Count Y. If possible, this should match the name of the
> + respective channel as it appears in the device datasheet
> + documentation text.
> +
> +What: /sys/bus/counter/devices/counterX/countY/signalZ_action
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Action mode of Count Y for Signal Z. This attribute indicates
> + the condition of Signal Z that triggers the count function
> + evaluation for Count Y. The following action modes are
> + available:
> +
> + None:
> + Signal does not trigger the count function. In
> + Pulse-Direction count function mode, this Signal is
> + evaluated as Direction.
> +
> + Rising Edge:
> + Low state transitions to high state.
> +
> + Falling Edge:
> + High state transitions to low state.
> +
> + Both Edges:
> + Any state transition.
> +
> +What: /sys/bus/counter/devices/counterX/countY/signalZ_action_available
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Discrete set of available action modes are listed in this file
> + for the configuration of the respective Synapse associating
> + Signal Z to Count Y.
> +
> +What: /sys/bus/counter/devices/counterX/name
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Read-only attribute that indicates the device-specific name of
> + the Counter. This should match the name of the device as it
> + appears in its respective datasheet documentation text.
> +
> +What: /sys/bus/counter/devices/counterX/signalY/signal
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Signal data of Signal Y. The following data types are available:
> +
> + SIGNAL_LEVEL:
> + Respective input has two discrete states: Low and High.
Formatting is rather inconsistent. For the type we had Capital then lowercase
here all caps? Or is this an integer from an enum? Definitely prefer
a human readable version if possible This is also rather pointless until
we have more than one option ;)
> +
> +What: /sys/bus/counter/devices/counterX/signalY/name
> +KernelVersion: 4.17
> +Contact: [email protected]
> +Description:
> + Read-only attribute that indicates the device-specific name of
> + Signal Y. If possible, this should match the name of the
> + respective signal as it appears in the device datasheet
> + documentation text.
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 2be01a95b7a5..2a7bf2f84272 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -3672,6 +3672,7 @@ COUNTER SUBSYSTEM
> M: William Breathitt Gray <[email protected]>
> L: [email protected]
> S: Maintained
> +F: Documentation/ABI/testing/sysfs-bus-counter*
> F: drivers/counter/
> F: include/linux/counter.h
>
On Sat, 2018-03-24 at 17:33 +0000, Jonathan Cameron wrote:
> checkpatch.pl --strict (but take into account some of the warnings will
> be silly so don't fix them all).
What checkpatch output is silly?
On Sun, 25 Mar 2018 00:46:10 -0700
Joe Perches <[email protected]> wrote:
> On Sat, 2018-03-24 at 17:33 +0000, Jonathan Cameron wrote:
> > checkpatch.pl --strict (but take into account some of the warnings will
> > be silly so don't fix them all).
>
> What checkpatch output is silly?
Sorry - bad word choice. It guides you to where you should look very
well, but sometimes the patch author should consider the warning and
decide to ignore it.
Great tool - not magic!
Jonathan
> --
> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
On Sun, 2018-03-25 at 17:56 +0100, Jonathan Cameron wrote:
> On Sun, 25 Mar 2018 00:46:10 -0700
> Joe Perches <[email protected]> wrote:
>
> > On Sat, 2018-03-24 at 17:33 +0000, Jonathan Cameron wrote:
> > > checkpatch.pl --strict (but take into account some of the warnings will
> > > be silly so don't fix them all).
> >
> > What checkpatch output is silly?
>
> Sorry - bad word choice.
No worries. Just checking.
> It guides you to where you should look very
> well, but sometimes the patch author should consider the warning and
> decide to ignore it.
Very true. It's a brainless tool and all who use
it should seriously consider ignoring its output
whenever they deem appropriate.
On Sat, Mar 24, 2018 at 05:33:58PM +0000, Jonathan Cameron wrote:
>On Fri, 9 Mar 2018 13:42:23 -0500
>William Breathitt Gray <[email protected]> wrote:
>
>> This patch introduces the Generic Counter interface for supporting
>> counter devices.
>>
>> In the context of the Generic Counter interface, a counter is defined as
>> a device that reports one or more "counts" based on the state changes of
>> one or more "signals" as evaluated by a defined "count function."
>>
>> Driver callbacks should be provided to communicate with the device: to
>> read and write various Signals and Counts, and to set and get the
>> "action mode" and "count function" for various Synapses and Counts
>> respectively.
>>
>> To support a counter device, a driver must first allocate the available
>> Counter Signals via counter_signal structures. These Signals should
>> be stored as an array and set to the signals array member of an
>> allocated counter_device structure before the Counter is registered to
>> the system.
>>
>> Counter Counts may be allocated via counter_count structures, and
>> respective Counter Signal associations (Synapses) made via
>> counter_synapse structures. Associated counter_synapse structures are
>> stored as an array and set to the the synapses array member of the
>> respective counter_count structure. These counter_count structures are
>> set to the counts array member of an allocated counter_device structure
>> before the Counter is registered to the system.
>>
>> A counter device is registered to the system by passing the respective
>> initialized counter_device structure to the counter_register function;
>> similarly, the counter_unregister function unregisters the respective
>> Counter. The devm_counter_register and devm_counter_unregister functions
>> serve as device memory-managed versions of the counter_register and
>> counter_unregister functions respectively.
>>
>> Signed-off-by: William Breathitt Gray <[email protected]>
>
>Hi William,
>
>I would leave the existing drivers where they are until you are ready
>to convert them. I.e. Do the moves as separate patches from this one
>as it just adds noise here and they aren't ready immediately.
>
>The externs in the header add code for no benefit and make it hard
>to align the parameters nicely. I would drop them all.
>
>Few other minor bits and bobs inline. There is a lot of 'automatic'
>cleanup in here, but I think you have missed a few cases where the
>attribute element hasn't 'yet' been added to the list. (I may be
>missing something)
>
>Fundamentally looks good though.
>
>Jonathan
Hi Jonathan,
Most of these are simple cleanups so I don't anticipate any trouble
incorporating them in version 6 of this patchset. :-)
Regarding the "name" strings allocated throughout, these are freed later
in the free_counter_device_groups_list function (which is called on
error codes passed up). This unwinding is hard to follow so I think I'll
refactor these code blocks to perform frees closer to the relevant
memory allocations; despite the additional code, I expect the clearer
logic will aid future debugging endevors.
Some minor comments follow.
William Breathitt Gray
[...]
>> +static int counter_counts_register(
>> + struct counter_device_attr_group *const groups_list,
>> + const struct counter_device *const counter)
>> +{
>> + const size_t num_counts = counter->num_counts;
>> + struct device *const dev = &counter->device_state->dev;
>> + size_t i;
>> + struct counter_count *count;
>> + const char *name;
>> + int err;
>> +
>> + /* At least one Count must be defined */
>> + if (!counter->counts || !num_counts) {
>> + dev_err(dev, "Counts undefined\n");
>> + return -EINVAL;
>> + }
>> +
>> + /* Register each Count */
>> + for (i = 0; i < num_counts; i++) {
>> + count = counter->counts + i;
>Is counter->counts ever set anywhere?
This is the array of struct counter_count defined by the consuming
driver. The preceding null checks are sanity checks -- drivers should
always set the counts and num_counts members before calling the
counter_register function.
Since drivers are required to set these members, perhaps I should
remove the sanity checks as superfluous since it is unlikely an unset
counts or num_counts member would pass unnoticed by driver authors,
reviews, and maintainers. Would it make sense to remove this
conditional?
>> +/**
>> + * devm_counter_register - Resource-managed counter_register
>> + * @dev: device to allocate counter_device for
>> + * @counter: pointer to Counter to register
>> + *
>> + * Managed counter_register. The Counter registered with this function is
>> + * automatically unregistered on driver detach. This function calls
>> + * counter_register internally. Refer to that function for more information.
>> + *
>> + * If an Counter registered with this function needs to be unregistered
>> + * separately, devm_counter_unregister must be used.
>> + *
>> + * RETURNS:
>> + * 0 on success, negative error number on failure.
>> + */
>> +int devm_counter_register(struct device *dev,
>> + struct counter_device *const counter)
>Where possible align with the opening bracket.
>checkpatch.pl --strict (but take into account some of the warnings will
>be silly so don't fix them all).
I'll try this out and see how it looks with everything aligned to the
opening bracket.
One worry I have is in the case of parameter definitions that are wide
such as in the counter_attribute_create function, which has two function
pointers as parameters. In cases like these, aligning to the opening
brackets would produce very vertical parameter lists.
Should I mix and match, i.e. align to the opening brackets for some
functions while permitting others to follow a single tab alignment, or
would it be better to commit to a single alignment style throughout the
entire file?
[...]
>> +/**
>> + * struct count_read_value - Opaque Count read value
>> + * @buf: string representation of Count read value
>> + * @len: length of string in @buf
>I wonder if you are ever going to want to have in kernel consumers.
>Using strings this early level would make that hard.
>
>I'm also unclear on why it makes sense to do so given count
>is always an integer - Potentially things could get interesting
>when you are either signed or unsigned and matching the number of
>bits (s16, u16 or similar).
>
>Given this is in kernel interface though, nothing stops you modifying
>it later if you change your mind about this.
Yes, the idea here is to keep it opaque so that the implementation can
change independently without requiring changes to consuming drivers.
Although counts right now are integers, there may be drivers in the
future which require another type such as floating-point, so I want to
keep it generic enough to support those type of devices in the future
without causing drastic changes to the existing drivers that depend on
the Generic Counter API.
Since the struct count_read_value is opaque, the decision to use strings
here was for my own convenience since I can pass the buf member directly
to the relevant attribute show and store functions; this implementation
can easily change for any future requirements.
[...]
>> +enum signal_value_type {
>> + SIGNAL_LEVEL = 0
>This one surprised me. Only one option?
At present it does seem silly to declare an enum for a single option,
but I want to keep the paradigm established by enum count_value_type
consistent with an enum signal_value_type.
Currently, only the 104-QUAD-8 driver provides a signal_read callback,
so we only have the SIGNAL_LEVEL type defined to represent a Signal
low/high state. Since the Generic Counter paradigm is flexible enough to
represent Signals of various types, I expect future counter driver
patches to add their signal types as new enum signal_value_type options
as required. Although, I anticipate SIGNAL_LEVEL serving the majority of
counter devices just fine; I don't see many devices requiring Signal
representations other than low/high.
On Sat, Mar 24, 2018 at 05:21:40PM +0000, Jonathan Cameron wrote:
>On Fri, 9 Mar 2018 13:43:19 -0500
>William Breathitt Gray <[email protected]> wrote:
>
>> This patch adds standard documentation for the Generic Counter interface
>> userspace sysfs attributes of the 104-QUAD-8 driver.
>>
>> Signed-off-by: William Breathitt Gray <[email protected]>
>A few minor comments inline...
>Some of this seems generic and common enough you should just put it in the
>main docs straight away rather that waiting for more devices to use it.
>
>Jonathan
That sounds reasonable so I'll move some of these into the main Generic
Counter sysfs documentation file.
Some comments inline follow.
William Breathitt Gray
>
>> ---
>> .../ABI/testing/sysfs-bus-counter-104-quad-8 | 115 +++++++++++++++++++++
>> MAINTAINERS | 1 +
>> 2 files changed, 116 insertions(+)
>> create mode 100644 Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
>>
>> diff --git a/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8 b/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
>> new file mode 100644
>> index 000000000000..4269b438185a
>> --- /dev/null
>> +++ b/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
>> @@ -0,0 +1,115 @@
>> +What: /sys/bus/counter/devices/counterX/countY_count_mode
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Count mode for channel Y. The preset value for channel Y is used
>> + by the count mode where required. The following count modes are
>> + available:
>> +
>> + Normal:
>> + Counting is continuous in either direction.
>> +
>> + Range Limit:
>> + An upper or lower limit is set, mimicking limit switches
>> + in the mechanical counterpart. The upper limit is set to
>> + the preset value, while the lower limit is set to 0. The
>> + counter freezes at count = preset when counting up, and
>> + at count = 0 when counting down. At either of these
>> + limits, the counting is resumed only when the count
>> + direction is reversed.
>> +
>> + Non-recycle:
>> + Counter is disabled whenever a 24-bit count overflow or
>> + underflow takes place. The counter is re-enabled when a
>> + new count value is loaded to the counter via a preset
>> + operation or write to raw.
>> +
>> + Modulo-N:
>> + A count boundary is set between 0 and the preset value.
>> + The counter is reset to 0 at count = preset when
>> + counting up, while the counter is set to the preset
>> + value at count = 0 when counting down; the counter does
>> + not freeze at the bundary points, but counts
>> + continuously throughout.
>
>This worries me a little in that you will end up with all sorts of subtle
>variations around these concepts and hence end up with an impossible to
>generalize userspace interface...
There is a potention for a deal of variations due to the diverse range
of counter devices out there -- in particular, I worry about the
confusion of similar functionality using slightly different names (e.g.
"Normal" may be named "Continuous" in another driver) and vice versa.
Perhaps I should standardize these count modes in the main Generic
Counter sysfs documentation file.
A difficulty with standardizing these count modes is how to handle
description cases such as "Non-recycle" mode whose limit range is that
of the count register size (24-bit). This limit range would be different
in a device of a different count register size; for example, the LS7366R
is a 32-bit version of this counter device with the same interface, but
which has a 32-bit limit in "Non-recycle" mode. However, this may not be
such a problem since the datasheet for these devices does use a more
generic description of this mode which I can utilize: "counter disabled
with carry or borrow, re-enabled with reset or load."
The count_mode attribute is core enough to Generic Counter paradigm that
I expect it to appear in many Counter drivers, so I'll move this to the
main Generic Counter sysfs documentation file for better standardization
of its modes.
>
>> +
>> +What: /sys/bus/counter/devices/counterX/countY_count_mode_available
>> +What: /sys/bus/counter/devices/counterX/countY_noise_error_available
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Discrete set of available values for the respective Count Y
>> + configuration are listed in this file.
>> +
>> +What: /sys/bus/counter/devices/counterX/countY_direction
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Read-only attribute that indicates the count direction of
>> + Count Y. Two count directions are available: Forward and
>> + Backward.
>Is this telling us which way it is currently counting? I would imagine
>it's generic inversion control, but this description doesn't make that clear.
The nature of quadrature encoding allows direction of movement to be
determined -- in terms of count data this direction represents whether
the count value is increasing or decreasing. For the 104-QUAD-8 device,
this "direction" is a read-only value provided by the hardware
evaluation of the A and B input lines.
However, I can imagine some simple counter devices permitting write
operations to configure a direction as a form of inversion control for
the counter. This attribute is generic enough to include in the main
Generic Counter sysfs documentation file, so I'll move it there and add
a more detailed explanation of its read and write functionality.
>
>> +
>> +What: /sys/bus/counter/devices/counterX/countY_enable
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Whether channel Y inputs A and B are enabled. Valid attribute
>> + values are boolean.
>Why would you disable them? I'm unclear on what userspace would do with this.
Truthfully, I haven't found much use for this functionality in my
applications, but several devices provide it so I have exposed it here.
I believe the intention is to allow users to pause a counter temporarily
(i.e. by ignoring changes on the A and B inputs) and then pick up where
they left off.
I think a possible real life use case would look like this: a conveyor
system tracks total movement, reaches the end of a production run and
pauses the counter, rehomes the conveyor belt, then restarts the counter
and continues counting where it left off.
This count enable functionality is generic enough that I can also move
it to the main Generic Counter sysfs documentation file, but I'll give
it a more generic description without referencing the A and B input
lines.
>
>> +
>> +What: /sys/bus/counter/devices/counterX/countY_noise_error
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Read-only attribute that indicates whether excessive noise is
>> + present at the channel Y count inputs in quadrature clock mode;
>> + irrelevant in non-quadrature (Pulse-Direction) clock mode.
>If you are going to report errors like this I would suggest trying to have
>a generic form that is easy for userspace to match.
>countY_error_noise would allow easy presentation of all errors of the
>form
>countY_error_<error type> as a list based on <error type>
>
>> +
>> +What: /sys/bus/counter/devices/counterX/countY_preset
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + If the counter device supports preset registers, the preset
>> + count for channel Y is provided by this attribute.
>> +
>> +What: /sys/bus/counter/devices/counterX/countY_preset_enable
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Whether to set channel Y counter with channel Y preset value
>> + when channel Y index input is active, or continuously count.
>> + Valid attribute values are boolean.
>> +
>> +What: /sys/bus/counter/devices/counterX/signalY_index_polarity
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Active level of channel Y-16 index input; irrelevant in
>> + non-synchronous load mode.
>This seems like a generic control that should be in the main docs?
>don't use Y-16 to identify the channel. Use "the associated channel to signal Y".
>
>> +
>> +What: /sys/bus/counter/devices/counterX/signalY_index_polarity_available
>> +What: /sys/bus/counter/devices/counterX/signalY_synchronous_mode_available
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Discrete set of available values for the respective Signal Y
>> + configuration are listed in this file.
>> +
>> +What: /sys/bus/counter/devices/counterX/signalY_synchronous_mode
>> +KernelVersion: 4.17
>> +Contact: [email protected]
>> +Description:
>> + Configure channel Y-16 counter for non-synchronous or
>> + synchronous load mode. Synchronous load mode cannot be selected
>> + in non-quadrature (Pulse-Direction) clock mode.
>> +
>> + Non-synchronous:
>> + A logic low level is the active level at this index
>> + input. The index function (as enabled via preset_enable)
>> + is performed directly on the active level of the index
>> + input.
>> +
>> + Synchronous:
>> + Intended for interfacing with encoder Index output in
>> + quadrature clock mode. The active level is configured
>> + 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.
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index febe27a9962e..8134050d175a 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -270,6 +270,7 @@ ACCES 104-QUAD-8 DRIVER
>> M: William Breathitt Gray <[email protected]>
>> L: [email protected]
>> S: Maintained
>> +F: Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
>> F: Documentation/ABI/testing/sysfs-bus-iio-counter-104-quad-8
>> F: drivers/counter/104-quad-8.c
>>
>
On Sat, 31 Mar 2018 20:41:54 -0400
William Breathitt Gray <[email protected]> wrote:
> On Sat, Mar 24, 2018 at 05:33:58PM +0000, Jonathan Cameron wrote:
> >On Fri, 9 Mar 2018 13:42:23 -0500
> >William Breathitt Gray <[email protected]> wrote:
> >
> >> This patch introduces the Generic Counter interface for supporting
> >> counter devices.
> >>
> >> In the context of the Generic Counter interface, a counter is defined as
> >> a device that reports one or more "counts" based on the state changes of
> >> one or more "signals" as evaluated by a defined "count function."
> >>
> >> Driver callbacks should be provided to communicate with the device: to
> >> read and write various Signals and Counts, and to set and get the
> >> "action mode" and "count function" for various Synapses and Counts
> >> respectively.
> >>
> >> To support a counter device, a driver must first allocate the available
> >> Counter Signals via counter_signal structures. These Signals should
> >> be stored as an array and set to the signals array member of an
> >> allocated counter_device structure before the Counter is registered to
> >> the system.
> >>
> >> Counter Counts may be allocated via counter_count structures, and
> >> respective Counter Signal associations (Synapses) made via
> >> counter_synapse structures. Associated counter_synapse structures are
> >> stored as an array and set to the the synapses array member of the
> >> respective counter_count structure. These counter_count structures are
> >> set to the counts array member of an allocated counter_device structure
> >> before the Counter is registered to the system.
> >>
> >> A counter device is registered to the system by passing the respective
> >> initialized counter_device structure to the counter_register function;
> >> similarly, the counter_unregister function unregisters the respective
> >> Counter. The devm_counter_register and devm_counter_unregister functions
> >> serve as device memory-managed versions of the counter_register and
> >> counter_unregister functions respectively.
> >>
> >> Signed-off-by: William Breathitt Gray <[email protected]>
> >
> >Hi William,
> >
> >I would leave the existing drivers where they are until you are ready
> >to convert them. I.e. Do the moves as separate patches from this one
> >as it just adds noise here and they aren't ready immediately.
> >
> >The externs in the header add code for no benefit and make it hard
> >to align the parameters nicely. I would drop them all.
> >
> >Few other minor bits and bobs inline. There is a lot of 'automatic'
> >cleanup in here, but I think you have missed a few cases where the
> >attribute element hasn't 'yet' been added to the list. (I may be
> >missing something)
> >
> >Fundamentally looks good though.
> >
> >Jonathan
>
> Hi Jonathan,
>
> Most of these are simple cleanups so I don't anticipate any trouble
> incorporating them in version 6 of this patchset. :-)
>
> Regarding the "name" strings allocated throughout, these are freed later
> in the free_counter_device_groups_list function (which is called on
> error codes passed up). This unwinding is hard to follow so I think I'll
> refactor these code blocks to perform frees closer to the relevant
> memory allocations; despite the additional code, I expect the clearer
> logic will aid future debugging endevors.
>
> Some minor comments follow.
>
> William Breathitt Gray
>
> [...]
>
> >> +static int counter_counts_register(
> >> + struct counter_device_attr_group *const groups_list,
> >> + const struct counter_device *const counter)
> >> +{
> >> + const size_t num_counts = counter->num_counts;
> >> + struct device *const dev = &counter->device_state->dev;
> >> + size_t i;
> >> + struct counter_count *count;
> >> + const char *name;
> >> + int err;
> >> +
> >> + /* At least one Count must be defined */
> >> + if (!counter->counts || !num_counts) {
> >> + dev_err(dev, "Counts undefined\n");
> >> + return -EINVAL;
> >> + }
> >> +
> >> + /* Register each Count */
> >> + for (i = 0; i < num_counts; i++) {
> >> + count = counter->counts + i;
> >Is counter->counts ever set anywhere?
>
> This is the array of struct counter_count defined by the consuming
> driver. The preceding null checks are sanity checks -- drivers should
> always set the counts and num_counts members before calling the
> counter_register function.
>
> Since drivers are required to set these members, perhaps I should
> remove the sanity checks as superfluous since it is unlikely an unset
> counts or num_counts member would pass unnoticed by driver authors,
> reviews, and maintainers. Would it make sense to remove this
> conditional?
I'd leave the there - don't do much harm and might catch something
one day!
>
> >> +/**
> >> + * devm_counter_register - Resource-managed counter_register
> >> + * @dev: device to allocate counter_device for
> >> + * @counter: pointer to Counter to register
> >> + *
> >> + * Managed counter_register. The Counter registered with this function is
> >> + * automatically unregistered on driver detach. This function calls
> >> + * counter_register internally. Refer to that function for more information.
> >> + *
> >> + * If an Counter registered with this function needs to be unregistered
> >> + * separately, devm_counter_unregister must be used.
> >> + *
> >> + * RETURNS:
> >> + * 0 on success, negative error number on failure.
> >> + */
> >> +int devm_counter_register(struct device *dev,
> >> + struct counter_device *const counter)
> >Where possible align with the opening bracket.
> >checkpatch.pl --strict (but take into account some of the warnings will
> >be silly so don't fix them all).
>
> I'll try this out and see how it looks with everything aligned to the
> opening bracket.
>
> One worry I have is in the case of parameter definitions that are wide
> such as in the counter_attribute_create function, which has two function
> pointers as parameters. In cases like these, aligning to the opening
> brackets would produce very vertical parameter lists.
>
> Should I mix and match, i.e. align to the opening brackets for some
> functions while permitting others to follow a single tab alignment, or
> would it be better to commit to a single alignment style throughout the
> entire file?
Align what you can sensibly do. So yes, mixed styles preferred as
there should only be a few cases where they can't be aligned and that
is the standard kernel style.
>
> [...]
>
> >> +/**
> >> + * struct count_read_value - Opaque Count read value
> >> + * @buf: string representation of Count read value
> >> + * @len: length of string in @buf
> >I wonder if you are ever going to want to have in kernel consumers.
> >Using strings this early level would make that hard.
> >
> >I'm also unclear on why it makes sense to do so given count
> >is always an integer - Potentially things could get interesting
> >when you are either signed or unsigned and matching the number of
> >bits (s16, u16 or similar).
> >
> >Given this is in kernel interface though, nothing stops you modifying
> >it later if you change your mind about this.
>
> Yes, the idea here is to keep it opaque so that the implementation can
> change independently without requiring changes to consuming drivers.
> Although counts right now are integers, there may be drivers in the
> future which require another type such as floating-point, so I want to
> keep it generic enough to support those type of devices in the future
> without causing drastic changes to the existing drivers that depend on
> the Generic Counter API.
>
> Since the struct count_read_value is opaque, the decision to use strings
> here was for my own convenience since I can pass the buf member directly
> to the relevant attribute show and store functions; this implementation
> can easily change for any future requirements.
>
> [...]
>
> >> +enum signal_value_type {
> >> + SIGNAL_LEVEL = 0
> >This one surprised me. Only one option?
>
> At present it does seem silly to declare an enum for a single option,
> but I want to keep the paradigm established by enum count_value_type
> consistent with an enum signal_value_type.
>
> Currently, only the 104-QUAD-8 driver provides a signal_read callback,
> so we only have the SIGNAL_LEVEL type defined to represent a Signal
> low/high state. Since the Generic Counter paradigm is flexible enough to
> represent Signals of various types, I expect future counter driver
> patches to add their signal types as new enum signal_value_type options
> as required. Although, I anticipate SIGNAL_LEVEL serving the majority of
> counter devices just fine; I don't see many devices requiring Signal
> representations other than low/high.
Fair enough.
Jonathan
> --
> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
On Mon, 2 Apr 2018 15:41:47 -0400
William Breathitt Gray <[email protected]> wrote:
> On Sat, Mar 24, 2018 at 05:21:40PM +0000, Jonathan Cameron wrote:
> >On Fri, 9 Mar 2018 13:43:19 -0500
> >William Breathitt Gray <[email protected]> wrote:
> >
> >> This patch adds standard documentation for the Generic Counter interface
> >> userspace sysfs attributes of the 104-QUAD-8 driver.
> >>
> >> Signed-off-by: William Breathitt Gray <[email protected]>
> >A few minor comments inline...
> >Some of this seems generic and common enough you should just put it in the
> >main docs straight away rather that waiting for more devices to use it.
> >
> >Jonathan
>
> That sounds reasonable so I'll move some of these into the main Generic
> Counter sysfs documentation file.
>
> Some comments inline follow.
>
> William Breathitt Gray
>
> >
> >> ---
> >> .../ABI/testing/sysfs-bus-counter-104-quad-8 | 115 +++++++++++++++++++++
> >> MAINTAINERS | 1 +
> >> 2 files changed, 116 insertions(+)
> >> create mode 100644 Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
> >>
> >> diff --git a/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8 b/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
> >> new file mode 100644
> >> index 000000000000..4269b438185a
> >> --- /dev/null
> >> +++ b/Documentation/ABI/testing/sysfs-bus-counter-104-quad-8
> >> @@ -0,0 +1,115 @@
> >> +What: /sys/bus/counter/devices/counterX/countY_count_mode
> >> +KernelVersion: 4.17
> >> +Contact: [email protected]
> >> +Description:
> >> + Count mode for channel Y. The preset value for channel Y is used
> >> + by the count mode where required. The following count modes are
> >> + available:
> >> +
> >> + Normal:
> >> + Counting is continuous in either direction.
> >> +
> >> + Range Limit:
> >> + An upper or lower limit is set, mimicking limit switches
> >> + in the mechanical counterpart. The upper limit is set to
> >> + the preset value, while the lower limit is set to 0. The
> >> + counter freezes at count = preset when counting up, and
> >> + at count = 0 when counting down. At either of these
> >> + limits, the counting is resumed only when the count
> >> + direction is reversed.
> >> +
> >> + Non-recycle:
> >> + Counter is disabled whenever a 24-bit count overflow or
> >> + underflow takes place. The counter is re-enabled when a
> >> + new count value is loaded to the counter via a preset
> >> + operation or write to raw.
> >> +
> >> + Modulo-N:
> >> + A count boundary is set between 0 and the preset value.
> >> + The counter is reset to 0 at count = preset when
> >> + counting up, while the counter is set to the preset
> >> + value at count = 0 when counting down; the counter does
> >> + not freeze at the bundary points, but counts
> >> + continuously throughout.
> >
> >This worries me a little in that you will end up with all sorts of subtle
> >variations around these concepts and hence end up with an impossible to
> >generalize userspace interface...
>
> There is a potention for a deal of variations due to the diverse range
> of counter devices out there -- in particular, I worry about the
> confusion of similar functionality using slightly different names (e.g.
> "Normal" may be named "Continuous" in another driver) and vice versa.
> Perhaps I should standardize these count modes in the main Generic
> Counter sysfs documentation file.
I would definitely standardize these. Any new type can then be added.
At least for devices that do the same thing we'll have the same naming!
>
> A difficulty with standardizing these count modes is how to handle
> description cases such as "Non-recycle" mode whose limit range is that
> of the count register size (24-bit). This limit range would be different
> in a device of a different count register size; for example, the LS7366R
> is a 32-bit version of this counter device with the same interface, but
> which has a 32-bit limit in "Non-recycle" mode. However, this may not be
> such a problem since the datasheet for these devices does use a more
> generic description of this mode which I can utilize: "counter disabled
> with carry or borrow, re-enabled with reset or load."
Describe the range as a separate attribute?
>
> The count_mode attribute is core enough to Generic Counter paradigm that
> I expect it to appear in many Counter drivers, so I'll move this to the
> main Generic Counter sysfs documentation file for better standardization
> of its modes.
>
Great
> >
> >> +
> >> +What: /sys/bus/counter/devices/counterX/countY_count_mode_available
> >> +What: /sys/bus/counter/devices/counterX/countY_noise_error_available
> >> +KernelVersion: 4.17
> >> +Contact: [email protected]
> >> +Description:
> >> + Discrete set of available values for the respective Count Y
> >> + configuration are listed in this file.
> >> +
> >> +What: /sys/bus/counter/devices/counterX/countY_direction
> >> +KernelVersion: 4.17
> >> +Contact: [email protected]
> >> +Description:
> >> + Read-only attribute that indicates the count direction of
> >> + Count Y. Two count directions are available: Forward and
> >> + Backward.
> >Is this telling us which way it is currently counting? I would imagine
> >it's generic inversion control, but this description doesn't make that clear.
>
> The nature of quadrature encoding allows direction of movement to be
> determined -- in terms of count data this direction represents whether
> the count value is increasing or decreasing. For the 104-QUAD-8 device,
> this "direction" is a read-only value provided by the hardware
> evaluation of the A and B input lines.
Ah fair enough. I would add a bit more text to give that quadrature
example (I'd forgotten about that and was thinking of completely
differently).
>
> However, I can imagine some simple counter devices permitting write
> operations to configure a direction as a form of inversion control for
> the counter. This attribute is generic enough to include in the main
> Generic Counter sysfs documentation file, so I'll move it there and add
> a more detailed explanation of its read and write functionality.
I would be careful about having the same attribute for those two use
cases as they aren't really comparable.
>
> >
> >> +
> >> +What: /sys/bus/counter/devices/counterX/countY_enable
> >> +KernelVersion: 4.17
> >> +Contact: [email protected]
> >> +Description:
> >> + Whether channel Y inputs A and B are enabled. Valid attribute
> >> + values are boolean.
> >Why would you disable them? I'm unclear on what userspace would do with this.
>
> Truthfully, I haven't found much use for this functionality in my
> applications, but several devices provide it so I have exposed it here.
> I believe the intention is to allow users to pause a counter temporarily
> (i.e. by ignoring changes on the A and B inputs) and then pick up where
> they left off.
>
> I think a possible real life use case would look like this: a conveyor
> system tracks total movement, reaches the end of a production run and
> pauses the counter, rehomes the conveyor belt, then restarts the counter
> and continues counting where it left off.
>
> This count enable functionality is generic enough that I can also move
> it to the main Generic Counter sysfs documentation file, but I'll give
> it a more generic description without referencing the A and B input
> lines.
>
That's a good enough explanation. I'd add the conveyor example to the
docs so people don't wonder what sort of thing it is for in future!
> >
...