Add support for Azoteq IQS620A/621/622/624/625
Subsystems (6)
v2 cover Add support for Azoteq IQS620A/621/622/624/625
This series adds support for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors. Each device integrates numerous sensing tech- nologies in a single package.
A multi-function device (MFD) driver supports core functions common to all devices, including device identification, firmware, interrupt handling and runtime power management. The MFD driver is also responsible for adding all product-specific sub-devices.
Each device supports self-capacitive, Hall-effect, and (in some cases) mutual- inductive sensing. These functions represent keys or switches and are supported by an input driver that covers all five devices. An assortment of pwm and iio drivers supports device-specific functions including ambient light and angular position sensing.
This series was tested using the following development hardware: IQS620AEV04, IQS621EV04, IQS622EV04 and IQS624/5EV04.
Jeff LaBundy (7):
dt-bindings: Add bindings for Azoteq IQS620A/621/622/624/625
mfd: Add support for Azoteq IQS620A/621/622/624/625
input: keyboard: Add support for Azoteq IQS620A/621/622/624/625
pwm: Add support for Azoteq IQS620A PWM generator
iio: temperature: Add support for Azoteq IQS620AT temperature sensor
iio: light: Add support for Azoteq IQS621/622 ambient light sensors
iio: position: Add support for Azoteq IQS624/625 angle sensors
.../devicetree/bindings/input/iqs62x-keys.yaml | 126 ++++ Documentation/devicetree/bindings/mfd/iqs62x.yaml | 177 ++++++ .../devicetree/bindings/pwm/iqs620a-pwm.yaml | 30 + drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/light/Kconfig | 10 + drivers/iio/light/Makefile | 1 + drivers/iio/light/iqs621-als.c | 614 ++++++++++++++++++++ drivers/iio/position/Kconfig | 19 + drivers/iio/position/Makefile | 7 + drivers/iio/position/iqs624-pos.c | 284 +++++++++ drivers/iio/temperature/Kconfig | 10 + drivers/iio/temperature/Makefile | 1 + drivers/iio/temperature/iqs620at-temp.c | 97 ++++ drivers/input/keyboard/Kconfig | 10 + drivers/input/keyboard/Makefile | 1 + drivers/input/keyboard/iqs62x-keys.c | 340 +++++++++++ drivers/mfd/Kconfig | 13 + drivers/mfd/Makefile | 3 + drivers/mfd/iqs62x-core.c | 639 +++++++++++++++++++++ drivers/mfd/iqs62x-tables.c | 438 ++++++++++++++ drivers/pwm/Kconfig | 10 + drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 +++++++ include/linux/mfd/iqs62x.h | 146 +++++ 25 files changed, 3185 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/iqs62x-keys.yaml create mode 100644 Documentation/devicetree/bindings/mfd/iqs62x.yaml create mode 100644 Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml create mode 100644 drivers/iio/light/iqs621-als.c create mode 100644 drivers/iio/position/Kconfig create mode 100644 drivers/iio/position/Makefile create mode 100644 drivers/iio/position/iqs624-pos.c create mode 100644 drivers/iio/temperature/iqs620at-temp.c create mode 100644 drivers/input/keyboard/iqs62x-keys.c create mode 100644 drivers/mfd/iqs62x-core.c create mode 100644 drivers/mfd/iqs62x-tables.c create mode 100644 drivers/pwm/pwm-iqs620a.c create mode 100644 include/linux/mfd/iqs62x.h
--
2.7.4
PATCH v2 1/7 dt-bindings: Add bindings for Azoteq IQS620A/621/622/624/625
This patch adds device tree bindings for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.
A total of three bindings are presented (one MFD and two child nodes); they are submitted as a single patch because the child node bindings have no meaning in the absence of the MFD binding.
---
Changes in v2:
- Removed "prox" child node and moved "keys" and "pwm" child nodes to their own bindings
- Replaced linux,fw-file property with more common firmware-name property
- Converted all bindings to YAML
.../devicetree/bindings/input/iqs62x-keys.yaml | 126 +++++++++++++++ Documentation/devicetree/bindings/mfd/iqs62x.yaml | 177 +++++++++++++++++++++ .../devicetree/bindings/pwm/iqs620a-pwm.yaml | 30 ++++ 3 files changed, 333 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/iqs62x-keys.yaml create mode 100644 Documentation/devicetree/bindings/mfd/iqs62x.yaml create mode 100644 Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +126 -0
@@ -0,0 +1,126 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/input/iqs62x-keys.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A/621/622/624/625 Keys and Switches + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors + feature a variety of self-capacitive, mutual-inductive and Hall-effect sens- + ing capabilities that can facilitate a variety of contactless key and switch + applications. + + These functions are collectively represented by a "keys" child node from the + parent MFD driver. See Documentation/devicetree/bindings/mfd/iqs62x.yaml for + further details and examples. Sensor hardware configuration (self-capacitive + vs. mutual-inductive, etc.) is selected based on the device's firmware. + +properties: + compatible: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys + - azoteq,iqs624-keys + - azoteq,iqs625-keys + + linux,keycodes: + allOf: + - $ref: /schemas/types.yaml#/definitions/uint32-array + - minItems: 1 + maxItems: 16 + description: | + Specifies the numeric keycodes associated with each available touch or + proximity event according to the following table. An 'x' indicates the + event is supported for a given device. Specify 0 for unused events. + + ------------------------------------------------------------------------- + | # | Event | IQS620A | IQS621 | IQS622 | IQS624 | IQS625 | + ------------------------------------------------------------------------- + | 0 | CH0 Touch | x | x | x | x | x | + | | Antenna 1 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 1 | CH0 Proximity | x | x | x | x | x | + | | Antenna 1 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 2 | CH1 Touch | x | x | x | x | x | + | | Ant. 1 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 3 | CH1 Proximity | x | x | x | x | x | + ------------------------------------------------------------------------- + | 4 | CH2 Touch | x | | | | | + ------------------------------------------------------------------------- + | 5 | CH2 Proximity | x | | | | | + | | Antenna 2 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 6 | Metal (+) Touch** | x | x | | | | + | | Ant. 2 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 7 | Metal (+) Prox.** | x | x | | | | + | | Antenna 2 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 8 | Metal (-) Touch** | x | x | | | | + ------------------------------------------------------------------------- + | 9 | Metal (-) Prox.** | x | x | | | | + ------------------------------------------------------------------------- + | 10 | SAR Active*** | x | | x | | | + ------------------------------------------------------------------------- + | 11 | SAR Quick Rel.*** | x | | x | | | + ------------------------------------------------------------------------- + | 12 | SAR Movement*** | x | | x | | | + ------------------------------------------------------------------------- + | 13 | SAR Filter Halt*** | x | | x | | | + ------------------------------------------------------------------------- + | 14 | Wheel Up | | | | x | | + ------------------------------------------------------------------------- + | 15 | Wheel Down | | | | x | | + ------------------------------------------------------------------------- + * Two-channel SAR. Replaces CH0-2 plus metal touch and proximity events + if enabled via firmware. + ** "+" and "-" refer to the polarity of a channel's delta (LTA - counts), + where "LTA" is defined as the channel's long-term average. + *** One-channel SAR. Replaces CH0-2 touch and proximity events if enabled + via firmware. + +required: + - compatible + - linux,keycodes + +if: + properties: + compatible: + contains: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys +then: + patternProperties: + "^hall-switch-(north|south)$": + type: object + description: + Represents north/south-field Hall-effect sensor touch or proximity + events. Note that north/south-field orientation is reversed on the + IQS620AXzCSR device due to its flip-chip package. + + properties: + linux,code: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Numeric switch code associated with the event. + + azoteq,use-prox: + $ref: /schemas/types.yaml#/definitions/flag + description: + If present, specifies that Hall-effect sensor reporting should + use the device's wide-range proximity threshold instead of its + close-range touch threshold (default). + + required: + - linux,code + +...
Documentation/devicetree/bindings/mfd/iqs62x.yaml new file +177 -0
177 lines not on this page · raw message
Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml new file +30 -0
@@ -0,0 +1,30 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pwm/iqs620a-pwm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A PWM Generator + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A multi-function sensor generates a fixed-frequency PWM + output represented by a "pwm" child node from the parent MFD driver. See + Documentation/devicetree/bindings/mfd/iqs62x.yaml for further details as + well as an example. + +properties: + compatible: + enum: + - azoteq,iqs620a-pwm + + "#pwm-cells": + const: 2 + +required: + - compatible + - "#pwm-cells" + +...
--
2.7.4
Re: [PATCH v2 1/7] dt-bindings: Add bindings for Azoteq IQS620A/621/622/624/625
Quoted 7 lines · Jeff LaBundy
This patch adds device tree bindings for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.
A total of three bindings are presented (one MFD and two child nodes); they are submitted as a single patch because the child node bindings have no meaning in the absence of the MFD binding.
Quoted 7 lines
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Removed "prox" child node and moved "keys" and "pwm" child nodes to their own bindings
- Replaced linux,fw-file property with more common firmware-name property
- Converted all bindings to YAML
Good job for first go.
Quoted 8 lines
.../devicetree/bindings/input/iqs62x-keys.yaml | 126 +++++++++++++++ Documentation/devicetree/bindings/mfd/iqs62x.yaml | 177 +++++++++++++++++++++ .../devicetree/bindings/pwm/iqs620a-pwm.yaml | 30 ++++ 3 files changed, 333 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/iqs62x-keys.yaml create mode 100644 Documentation/devicetree/bindings/mfd/iqs62x.yaml create mode 100644 Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml
A couple of minor things below. With those fixed:
Quoted 89 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +82
-0@@ -0,0 +1,126 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/input/iqs62x-keys.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A/621/622/624/625 Keys and Switches + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors + feature a variety of self-capacitive, mutual-inductive and Hall-effect sens- + ing capabilities that can facilitate a variety of contactless key and switch + applications. + + These functions are collectively represented by a "keys" child node from the + parent MFD driver. See Documentation/devicetree/bindings/mfd/iqs62x.yaml for + further details and examples. Sensor hardware configuration (self-capacitive + vs. mutual-inductive, etc.) is selected based on the device's firmware. + +properties: + compatible: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys + - azoteq,iqs624-keys + - azoteq,iqs625-keys + + linux,keycodes: + allOf: + - $ref: /schemas/types.yaml#/definitions/uint32-array + - minItems: 1 + maxItems: 16 + description: | + Specifies the numeric keycodes associated with each available touch or + proximity event according to the following table. An 'x' indicates the + event is supported for a given device. Specify 0 for unused events. + + ------------------------------------------------------------------------- + | # | Event | IQS620A | IQS621 | IQS622 | IQS624 | IQS625 | + ------------------------------------------------------------------------- + | 0 | CH0 Touch | x | x | x | x | x | + | | Antenna 1 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 1 | CH0 Proximity | x | x | x | x | x | + | | Antenna 1 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 2 | CH1 Touch | x | x | x | x | x | + | | Ant. 1 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 3 | CH1 Proximity | x | x | x | x | x | + ------------------------------------------------------------------------- + | 4 | CH2 Touch | x | | | | | + ------------------------------------------------------------------------- + | 5 | CH2 Proximity | x | | | | | + | | Antenna 2 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 6 | Metal (+) Touch** | x | x | | | | + | | Ant. 2 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 7 | Metal (+) Prox.** | x | x | | | | + | | Antenna 2 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 8 | Metal (-) Touch** | x | x | | | | + ------------------------------------------------------------------------- + | 9 | Metal (-) Prox.** | x | x | | | | + ------------------------------------------------------------------------- + | 10 | SAR Active*** | x | | x | | | + ------------------------------------------------------------------------- + | 11 | SAR Quick Rel.*** | x | | x | | | + ------------------------------------------------------------------------- + | 12 | SAR Movement*** | x | | x | | | + ------------------------------------------------------------------------- + | 13 | SAR Filter Halt*** | x | | x | | | + ------------------------------------------------------------------------- + | 14 | Wheel Up | | | | x | | + ------------------------------------------------------------------------- + | 15 | Wheel Down | | | | x | | + -------------------------------------------------------------------------
Documentation/devicetree/bindings/input/iqs62x-keys.yaml line 92
Quoted 10 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +10
-0+ * Two-channel SAR. Replaces CH0-2 plus metal touch and proximity events + if enabled via firmware. + ** "+" and "-" refer to the polarity of a channel's delta (LTA - counts), + where "LTA" is defined as the channel's long-term average. + *** One-channel SAR. Replaces CH0-2 touch and proximity events if enabled + via firmware. + +required: + - compatible + - linux,keycodes
Add:
additionalProperties: false
Quoted 79 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +34
-0+ +if: + properties: + compatible: + contains: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys +then: + patternProperties: + "^hall-switch-(north|south)$": + type: object + description: + Represents north/south-field Hall-effect sensor touch or proximity + events. Note that north/south-field orientation is reversed on the + IQS620AXzCSR device due to its flip-chip package. + + properties: + linux,code: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Numeric switch code associated with the event. + + azoteq,use-prox: + $ref: /schemas/types.yaml#/definitions/flag + description: + If present, specifies that Hall-effect sensor reporting should + use the device's wide-range proximity threshold instead of its + close-range touch threshold (default). + + required: + - linux,code + +...Documentation/devicetree/bindings/mfd/iqs62x.yaml new file +39
-0@@ -0,0 +1,177 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mfd/iqs62x.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A/621/622/624/625 Multi-Function Sensors + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors + integrate multiple sensing technologies in a single package. + + Link to data sheets: https://www.azoteq.com/ + +properties: + compatible: + enum: + - azoteq,iqs620a + - azoteq,iqs621 + - azoteq,iqs622 + - azoteq,iqs624 + - azoteq,iqs625 + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + firmware-name: + $ref: /schemas/types.yaml#/definitions/string + description: + Specifies the name of the calibration and configuration file selected by + the driver. If this property is omitted, the name is chosen based on the + device name with ".bin" as the extension (e.g. iqs620a.bin for IQS620A). +
Documentation/devicetree/bindings/mfd/iqs62x.yaml line 49
Quoted 10 lines
Documentation/devicetree/bindings/mfd/iqs62x.yaml new file +10
-0+ keys: + $ref: ../input/iqs62x-keys.yaml + + pwm: + $ref: ../pwm/iqs620a-pwm.yaml + +required: + - compatible + - reg + - interrupts
Add:
additionalProperties: false
Quoted 152 lines
Documentation/devicetree/bindings/mfd/iqs62x.yaml new file +128
-0+ +examples: + - | + /* + * Dual capacitive buttons with additional "air button," unipolar lid + * switch and panel-mounted LED. + */ + #include <dt-bindings/input/input.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + iqs620a@44 { + compatible = "azoteq,iqs620a"; + reg = <0x44>; + interrupt-parent = <&gpio>; + interrupts = <17 IRQ_TYPE_LEVEL_LOW>; + + keys { + compatible = "azoteq,iqs620a-keys"; + + linux,keycodes = <KEY_SELECT>, + <KEY_MENU>, + <KEY_OK>, + <KEY_MENU>; + + hall-switch-south { + linux,code = <SW_LID>; + azoteq,use-prox; + }; + }; + + iqs620a_pwm: pwm { + compatible = "azoteq,iqs620a-pwm"; + #pwm-cells = <2>; + }; + }; + }; + + pwmleds { + compatible = "pwm-leds"; + + panel { + pwms = <&iqs620a_pwm 0 1000000>; + max-brightness = <255>; + }; + }; + + - | + /* Single inductive button with bipolar dock/tablet-mode switch. */ + #include <dt-bindings/input/input.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + iqs620a@44 { + compatible = "azoteq,iqs620a"; + reg = <0x44>; + interrupt-parent = <&gpio>; + interrupts = <17 IRQ_TYPE_LEVEL_LOW>; + + firmware-name = "iqs620a_coil.bin"; + + keys { + compatible = "azoteq,iqs620a-keys"; + + linux,keycodes = <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <KEY_MUTE>; + + hall-switch-north { + linux,code = <SW_DOCK>; + }; + + hall-switch-south { + linux,code = <SW_TABLET_MODE>; + }; + }; + }; + }; + + - | + /* Dual capacitive buttons with volume knob. */ + #include <dt-bindings/input/input.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + iqs624@44 { + compatible = "azoteq,iqs624"; + reg = <0x44>; + interrupt-parent = <&gpio>; + interrupts = <17 IRQ_TYPE_LEVEL_LOW>; + + keys { + compatible = "azoteq,iqs624-keys"; + + linux,keycodes = <BTN_0>, + <0>, + <BTN_1>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <KEY_VOLUMEUP>, + <KEY_VOLUMEDOWN>; + }; + }; + }; + +...Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml new file +18
-0@@ -0,0 +1,30 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pwm/iqs620a-pwm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A PWM Generator + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A multi-function sensor generates a fixed-frequency PWM + output represented by a "pwm" child node from the parent MFD driver. See + Documentation/devicetree/bindings/mfd/iqs62x.yaml for further details as + well as an example. + +properties:
Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml line 28
Quoted 10 lines
Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml new file +10
-0+ compatible: + enum: + - azoteq,iqs620a-pwm + + "#pwm-cells": + const: 2 + +required: + - compatible + - "#pwm-cells"
Add:
additionalProperties: false
Quoted 5 lines
Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml new file +2
-1+ +... --2.7.4
Re: [PATCH v2 1/7] dt-bindings: Add bindings for Azoteq IQS620A/621/622/624/625
Hi Rob,
Thank you for your prompt review and your kind words. A couple of questions and comments for you below.
Quoted 133 lines · Rob Herring
Quoted 7 lines · Jeff LaBundy
This patch adds device tree bindings for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.
A total of three bindings are presented (one MFD and two child nodes); they are submitted as a single patch because the child node bindings have no meaning in the absence of the MFD binding.
Quoted 7 lines
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Removed "prox" child node and moved "keys" and "pwm" child nodes to their own bindings
- Replaced linux,fw-file property with more common firmware-name property
- Converted all bindings to YAMLGood job for first go.
Quoted 8 lines
.../devicetree/bindings/input/iqs62x-keys.yaml | 126 +++++++++++++++ Documentation/devicetree/bindings/mfd/iqs62x.yaml | 177 +++++++++++++++++++++ .../devicetree/bindings/pwm/iqs620a-pwm.yaml | 30 ++++ 3 files changed, 333 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/iqs62x-keys.yaml create mode 100644 Documentation/devicetree/bindings/mfd/iqs62x.yaml create mode 100644 Documentation/devicetree/bindings/pwm/iqs620a-pwm.yamlA couple of minor things below. With those fixed:
Reviewed-by Rob Herring <[email protected]>Quoted 89 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +82
-0@@ -0,0 +1,126 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/input/iqs62x-keys.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A/621/622/624/625 Keys and Switches + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors + feature a variety of self-capacitive, mutual-inductive and Hall-effect sens- + ing capabilities that can facilitate a variety of contactless key and switch + applications. + + These functions are collectively represented by a "keys" child node from the + parent MFD driver. See Documentation/devicetree/bindings/mfd/iqs62x.yaml for + further details and examples. Sensor hardware configuration (self-capacitive + vs. mutual-inductive, etc.) is selected based on the device's firmware. + +properties: + compatible: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys + - azoteq,iqs624-keys + - azoteq,iqs625-keys + + linux,keycodes: + allOf: + - $ref: /schemas/types.yaml#/definitions/uint32-array + - minItems: 1 + maxItems: 16 + description: | + Specifies the numeric keycodes associated with each available touch or + proximity event according to the following table. An 'x' indicates the + event is supported for a given device. Specify 0 for unused events. + + ------------------------------------------------------------------------- + | # | Event | IQS620A | IQS621 | IQS622 | IQS624 | IQS625 | + ------------------------------------------------------------------------- + | 0 | CH0 Touch | x | x | x | x | x | + | | Antenna 1 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 1 | CH0 Proximity | x | x | x | x | x | + | | Antenna 1 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 2 | CH1 Touch | x | x | x | x | x | + | | Ant. 1 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 3 | CH1 Proximity | x | x | x | x | x | + ------------------------------------------------------------------------- + | 4 | CH2 Touch | x | | | | | + ------------------------------------------------------------------------- + | 5 | CH2 Proximity | x | | | | | + | | Antenna 2 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 6 | Metal (+) Touch** | x | x | | | | + | | Ant. 2 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 7 | Metal (+) Prox.** | x | x | | | | + | | Antenna 2 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 8 | Metal (-) Touch** | x | x | | | | + ------------------------------------------------------------------------- + | 9 | Metal (-) Prox.** | x | x | | | | + ------------------------------------------------------------------------- + | 10 | SAR Active*** | x | | x | | | + ------------------------------------------------------------------------- + | 11 | SAR Quick Rel.*** | x | | x | | | + ------------------------------------------------------------------------- + | 12 | SAR Movement*** | x | | x | | | + ------------------------------------------------------------------------- + | 13 | SAR Filter Halt*** | x | | x | | | + ------------------------------------------------------------------------- + | 14 | Wheel Up | | | | x | | + ------------------------------------------------------------------------- + | 15 | Wheel Down | | | | x | | + -------------------------------------------------------------------------Documentation/devicetree/bindings/input/iqs62x-keys.yaml line 92
Quoted 10 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +10
-0+ * Two-channel SAR. Replaces CH0-2 plus metal touch and proximity events + if enabled via firmware. + ** "+" and "-" refer to the polarity of a channel's delta (LTA - counts), + where "LTA" is defined as the channel's long-term average. + *** One-channel SAR. Replaces CH0-2 touch and proximity events if enabled + via firmware. + +required: + - compatible + - linux,keycodesAdd:
Quoted 2 lines
additionalProperties: false
When I add this, the dt_binding_check step complains that the hall switch child nodes used in the examples are unrecognized, e.g.:
iqs620a@44: keys: 'hall-switch-south' does not match any of the regexes: 'pinctrl-[0-9]+'
When I originally encountered this, I found that the mdio-mux child node in [0] seems to be a similar example and omits additionalProperties, which is why I originally did that here. Do you have any advice on how to proceed?
Quoted 25 lines
Quoted 25 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +25
-0+ +if: + properties: + compatible: + contains: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys +then: + patternProperties: + "^hall-switch-(north|south)$": + type: object + description: + Represents north/south-field Hall-effect sensor touch or proximity + events. Note that north/south-field orientation is reversed on the + IQS620AXzCSR device due to its flip-chip package. + + properties: + linux,code: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Numeric switch code associated with the event. + + azoteq,use-prox: + $ref: /schemas/types.yaml#/definitions/flag
Quoted 8 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml line 125
Quoted 8 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +8
-0+ description: + If present, specifies that Hall-effect sensor reporting should + use the device's wide-range proximity threshold instead of its + close-range touch threshold (default). + + required: + - linux,code +
Do you think I should specify additionalProperties: false within these child nodes?
Quoted 59 lines
Quoted 46 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +1
-0+...Documentation/devicetree/bindings/mfd/iqs62x.yaml new file +39
-0@@ -0,0 +1,177 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mfd/iqs62x.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A/621/622/624/625 Multi-Function Sensors + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors + integrate multiple sensing technologies in a single package. + + Link to data sheets: https://www.azoteq.com/ + +properties: + compatible: + enum: + - azoteq,iqs620a + - azoteq,iqs621 + - azoteq,iqs622 + - azoteq,iqs624 + - azoteq,iqs625 + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + firmware-name: + $ref: /schemas/types.yaml#/definitions/string + description: + Specifies the name of the calibration and configuration file selected by + the driver. If this property is omitted, the name is chosen based on the + device name with ".bin" as the extension (e.g. iqs620a.bin for IQS620A). +Documentation/devicetree/bindings/mfd/iqs62x.yaml line 49
Quoted 10 lines
Documentation/devicetree/bindings/mfd/iqs62x.yaml new file +10
-0+ keys: + $ref: ../input/iqs62x-keys.yaml + + pwm: + $ref: ../pwm/iqs620a-pwm.yaml + +required: + - compatible + - reg + - interruptsAdd:
Quoted 2 lines
additionalProperties: false
Sure thing, will do.
Quoted 165 lines
Quoted 152 lines
Documentation/devicetree/bindings/mfd/iqs62x.yaml new file +128
-0+ +examples: + - | + /* + * Dual capacitive buttons with additional "air button," unipolar lid + * switch and panel-mounted LED. + */ + #include <dt-bindings/input/input.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + iqs620a@44 { + compatible = "azoteq,iqs620a"; + reg = <0x44>; + interrupt-parent = <&gpio>; + interrupts = <17 IRQ_TYPE_LEVEL_LOW>; + + keys { + compatible = "azoteq,iqs620a-keys"; + + linux,keycodes = <KEY_SELECT>, + <KEY_MENU>, + <KEY_OK>, + <KEY_MENU>; + + hall-switch-south { + linux,code = <SW_LID>; + azoteq,use-prox; + }; + }; + + iqs620a_pwm: pwm { + compatible = "azoteq,iqs620a-pwm"; + #pwm-cells = <2>; + }; + }; + }; + + pwmleds { + compatible = "pwm-leds"; + + panel { + pwms = <&iqs620a_pwm 0 1000000>; + max-brightness = <255>; + }; + }; + + - | + /* Single inductive button with bipolar dock/tablet-mode switch. */ + #include <dt-bindings/input/input.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + iqs620a@44 { + compatible = "azoteq,iqs620a"; + reg = <0x44>; + interrupt-parent = <&gpio>; + interrupts = <17 IRQ_TYPE_LEVEL_LOW>; + + firmware-name = "iqs620a_coil.bin"; + + keys { + compatible = "azoteq,iqs620a-keys"; + + linux,keycodes = <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <KEY_MUTE>; + + hall-switch-north { + linux,code = <SW_DOCK>; + }; + + hall-switch-south { + linux,code = <SW_TABLET_MODE>; + }; + }; + }; + }; + + - | + /* Dual capacitive buttons with volume knob. */ + #include <dt-bindings/input/input.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + iqs624@44 { + compatible = "azoteq,iqs624"; + reg = <0x44>; + interrupt-parent = <&gpio>; + interrupts = <17 IRQ_TYPE_LEVEL_LOW>; + + keys { + compatible = "azoteq,iqs624-keys"; + + linux,keycodes = <BTN_0>, + <0>, + <BTN_1>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <0>, + <KEY_VOLUMEUP>, + <KEY_VOLUMEDOWN>; + }; + }; + }; + +...Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml new file +18
-0@@ -0,0 +1,30 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pwm/iqs620a-pwm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A PWM Generator + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A multi-function sensor generates a fixed-frequency PWM + output represented by a "pwm" child node from the parent MFD driver. See + Documentation/devicetree/bindings/mfd/iqs62x.yaml for further details as + well as an example. + +properties:Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml line 28
Quoted 10 lines
Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml new file +10
-0+ compatible: + enum: + - azoteq,iqs620a-pwm + + "#pwm-cells": + const: 2 + +required: + - compatible + - "#pwm-cells"Add:
Quoted 2 lines
additionalProperties: false
Sure thing, will do.
Quoted 5 lines
Quoted 5 lines
Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml new file +2
-1+ +... --2.7.4
[0] Documentation/devicetree/bindings/net/allwinner,sun8i-a83t-emac.yaml
Kind regards,
Jeff LaBundy
Rob Herring
maintainerRe: [PATCH v2 1/7] dt-bindings: Add bindings for Azoteq IQS620A/621/622/624/625
Quoted 143 lines · Jeff LaBundy
Hi Rob,
Thank you for your prompt review and your kind words. A couple of questions and comments for you below.
Quoted 133 lines · Rob Herring
Quoted 7 lines · Jeff LaBundy
This patch adds device tree bindings for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.
A total of three bindings are presented (one MFD and two child nodes); they are submitted as a single patch because the child node bindings have no meaning in the absence of the MFD binding.
Quoted 7 lines
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Removed "prox" child node and moved "keys" and "pwm" child nodes to their own bindings
- Replaced linux,fw-file property with more common firmware-name property
- Converted all bindings to YAMLGood job for first go.
Quoted 8 lines
.../devicetree/bindings/input/iqs62x-keys.yaml | 126 +++++++++++++++ Documentation/devicetree/bindings/mfd/iqs62x.yaml | 177 +++++++++++++++++++++ .../devicetree/bindings/pwm/iqs620a-pwm.yaml | 30 ++++ 3 files changed, 333 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/iqs62x-keys.yaml create mode 100644 Documentation/devicetree/bindings/mfd/iqs62x.yaml create mode 100644 Documentation/devicetree/bindings/pwm/iqs620a-pwm.yamlA couple of minor things below. With those fixed:
Reviewed-by Rob Herring <[email protected]>Quoted 89 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +82
-0@@ -0,0 +1,126 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/input/iqs62x-keys.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A/621/622/624/625 Keys and Switches + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors + feature a variety of self-capacitive, mutual-inductive and Hall-effect sens- + ing capabilities that can facilitate a variety of contactless key and switch + applications. + + These functions are collectively represented by a "keys" child node from the + parent MFD driver. See Documentation/devicetree/bindings/mfd/iqs62x.yaml for + further details and examples. Sensor hardware configuration (self-capacitive + vs. mutual-inductive, etc.) is selected based on the device's firmware. + +properties: + compatible: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys + - azoteq,iqs624-keys + - azoteq,iqs625-keys + + linux,keycodes: + allOf: + - $ref: /schemas/types.yaml#/definitions/uint32-array + - minItems: 1 + maxItems: 16 + description: | + Specifies the numeric keycodes associated with each available touch or + proximity event according to the following table. An 'x' indicates the + event is supported for a given device. Specify 0 for unused events. + + ------------------------------------------------------------------------- + | # | Event | IQS620A | IQS621 | IQS622 | IQS624 | IQS625 | + ------------------------------------------------------------------------- + | 0 | CH0 Touch | x | x | x | x | x | + | | Antenna 1 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 1 | CH0 Proximity | x | x | x | x | x | + | | Antenna 1 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 2 | CH1 Touch | x | x | x | x | x | + | | Ant. 1 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 3 | CH1 Proximity | x | x | x | x | x | + ------------------------------------------------------------------------- + | 4 | CH2 Touch | x | | | | | + ------------------------------------------------------------------------- + | 5 | CH2 Proximity | x | | | | | + | | Antenna 2 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 6 | Metal (+) Touch** | x | x | | | | + | | Ant. 2 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 7 | Metal (+) Prox.** | x | x | | | | + | | Antenna 2 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 8 | Metal (-) Touch** | x | x | | | | + ------------------------------------------------------------------------- + | 9 | Metal (-) Prox.** | x | x | | | | + ------------------------------------------------------------------------- + | 10 | SAR Active*** | x | | x | | | + ------------------------------------------------------------------------- + | 11 | SAR Quick Rel.*** | x | | x | | | + ------------------------------------------------------------------------- + | 12 | SAR Movement*** | x | | x | | | + ------------------------------------------------------------------------- + | 13 | SAR Filter Halt*** | x | | x | | | + ------------------------------------------------------------------------- + | 14 | Wheel Up | | | | x | | + ------------------------------------------------------------------------- + | 15 | Wheel Down | | | | x | | + -------------------------------------------------------------------------Documentation/devicetree/bindings/input/iqs62x-keys.yaml line 92
Quoted 10 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +10
-0+ * Two-channel SAR. Replaces CH0-2 plus metal touch and proximity events + if enabled via firmware. + ** "+" and "-" refer to the polarity of a channel's delta (LTA - counts), + where "LTA" is defined as the channel's long-term average. + *** One-channel SAR. Replaces CH0-2 touch and proximity events if enabled + via firmware. + +required: + - compatible + - linux,keycodesAdd:
Quoted 2 lines
additionalProperties: false
Quoted 8 lines
When I add this, the
dt_binding_checkstep complains that the hall switch child nodes used in the examples are unrecognized, e.g.:iqs620a@44: keys: 'hall-switch-south' does not match any of the regexes: 'pinctrl-[0-9]+'
When I originally encountered this, I found that the mdio-mux child node in [0] seems to be a similar example and omits additionalProperties, which is why I originally did that here. Do you have any advice on how to proceed?
That's because the properties are under an if/then. Your options are split the schema into 2 files to eliminate the if/then or just define "^hall-switch-(north|south)$" with just 'true' outside the if/then. A variation on the 2nd option is invert the if/then and make the schema false. Then the 'main' schema defines the full superset of properties and the if/then just filters out ones that don't apply in some cases.
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +27
-0+ +if: + properties: + compatible: + contains: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys +then: + patternProperties: + "^hall-switch-(north|south)$": + type: object + description: + Represents north/south-field Hall-effect sensor touch or proximity + events. Note that north/south-field orientation is reversed on the + IQS620AXzCSR device due to its flip-chip package. + + properties: + linux,code: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Numeric switch code associated with the event. + + azoteq,use-prox: + $ref: /schemas/types.yaml#/definitions/flag + description: + If present, specifies that Hall-effect sensor reporting should
Quoted 8 lines
Quoted 6 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml line 125
Quoted 6 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +6
-0+ use the device's wide-range proximity threshold instead of its + close-range touch threshold (default). + + required: + - linux,code +Do you think I should specify additionalProperties: false within these child nodes?
Yes.
Re: [PATCH v2 1/7] dt-bindings: Add bindings for Azoteq IQS620A/621/622/624/625
Hi Rob,
Quoted 153 lines · Rob Herring
Quoted 143 lines · Jeff LaBundy
Hi Rob,
Thank you for your prompt review and your kind words. A couple of questions and comments for you below.
Quoted 133 lines · Rob Herring
Quoted 7 lines · Jeff LaBundy
This patch adds device tree bindings for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.
A total of three bindings are presented (one MFD and two child nodes); they are submitted as a single patch because the child node bindings have no meaning in the absence of the MFD binding.
Quoted 7 lines
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Removed "prox" child node and moved "keys" and "pwm" child nodes to their own bindings
- Replaced linux,fw-file property with more common firmware-name property
- Converted all bindings to YAMLGood job for first go.
Quoted 8 lines
.../devicetree/bindings/input/iqs62x-keys.yaml | 126 +++++++++++++++ Documentation/devicetree/bindings/mfd/iqs62x.yaml | 177 +++++++++++++++++++++ .../devicetree/bindings/pwm/iqs620a-pwm.yaml | 30 ++++ 3 files changed, 333 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/iqs62x-keys.yaml create mode 100644 Documentation/devicetree/bindings/mfd/iqs62x.yaml create mode 100644 Documentation/devicetree/bindings/pwm/iqs620a-pwm.yamlA couple of minor things below. With those fixed:
Reviewed-by Rob Herring <[email protected]>Quoted 89 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +82
-0@@ -0,0 +1,126 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/input/iqs62x-keys.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Azoteq IQS620A/621/622/624/625 Keys and Switches + +maintainers: + - Jeff LaBundy <[email protected]> + +description: | + The Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors + feature a variety of self-capacitive, mutual-inductive and Hall-effect sens- + ing capabilities that can facilitate a variety of contactless key and switch + applications. + + These functions are collectively represented by a "keys" child node from the + parent MFD driver. See Documentation/devicetree/bindings/mfd/iqs62x.yaml for + further details and examples. Sensor hardware configuration (self-capacitive + vs. mutual-inductive, etc.) is selected based on the device's firmware. + +properties: + compatible: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys + - azoteq,iqs624-keys + - azoteq,iqs625-keys + + linux,keycodes: + allOf: + - $ref: /schemas/types.yaml#/definitions/uint32-array + - minItems: 1 + maxItems: 16 + description: | + Specifies the numeric keycodes associated with each available touch or + proximity event according to the following table. An 'x' indicates the + event is supported for a given device. Specify 0 for unused events. + + ------------------------------------------------------------------------- + | # | Event | IQS620A | IQS621 | IQS622 | IQS624 | IQS625 | + ------------------------------------------------------------------------- + | 0 | CH0 Touch | x | x | x | x | x | + | | Antenna 1 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 1 | CH0 Proximity | x | x | x | x | x | + | | Antenna 1 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 2 | CH1 Touch | x | x | x | x | x | + | | Ant. 1 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 3 | CH1 Proximity | x | x | x | x | x | + ------------------------------------------------------------------------- + | 4 | CH2 Touch | x | | | | | + ------------------------------------------------------------------------- + | 5 | CH2 Proximity | x | | | | | + | | Antenna 2 Prox.* | x | | | | | + ------------------------------------------------------------------------- + | 6 | Metal (+) Touch** | x | x | | | | + | | Ant. 2 Deep Touch* | x | | | | | + ------------------------------------------------------------------------- + | 7 | Metal (+) Prox.** | x | x | | | | + | | Antenna 2 Touch* | x | | | | | + ------------------------------------------------------------------------- + | 8 | Metal (-) Touch** | x | x | | | | + ------------------------------------------------------------------------- + | 9 | Metal (-) Prox.** | x | x | | | | + ------------------------------------------------------------------------- + | 10 | SAR Active*** | x | | x | | | + ------------------------------------------------------------------------- + | 11 | SAR Quick Rel.*** | x | | x | | | + ------------------------------------------------------------------------- + | 12 | SAR Movement*** | x | | x | | | + ------------------------------------------------------------------------- + | 13 | SAR Filter Halt*** | x | | x | | | + ------------------------------------------------------------------------- + | 14 | Wheel Up | | | | x | | + ------------------------------------------------------------------------- + | 15 | Wheel Down | | | | x | | + -------------------------------------------------------------------------Documentation/devicetree/bindings/input/iqs62x-keys.yaml line 92
Quoted 10 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +10
-0+ * Two-channel SAR. Replaces CH0-2 plus metal touch and proximity events + if enabled via firmware. + ** "+" and "-" refer to the polarity of a channel's delta (LTA - counts), + where "LTA" is defined as the channel's long-term average. + *** One-channel SAR. Replaces CH0-2 touch and proximity events if enabled + via firmware. + +required: + - compatible + - linux,keycodesAdd:
Quoted 2 lines
additionalProperties: false
Quoted 8 lines
When I add this, the
dt_binding_checkstep complains that the hall switch child nodes used in the examples are unrecognized, e.g.:iqs620a@44: keys: 'hall-switch-south' does not match any of the regexes: 'pinctrl-[0-9]+'
When I originally encountered this, I found that the mdio-mux child node in [0] seems to be a similar example and omits additionalProperties, which is why I originally did that here. Do you have any advice on how to proceed?
Quoted 7 lines
That's because the properties are under an if/then. Your options are split the schema into 2 files to eliminate the if/then or just define "^hall-switch-(north|south)$" with just 'true' outside the if/then. A variation on the 2nd option is invert the if/then and make the schema false. Then the 'main' schema defines the full superset of properties and the if/then just filters out ones that don't apply in some cases.
Thank you for this detailed explanation; it all makes sense now. For v3 I've opted for the variant of option (2) because it allows for a single binding as I originally intended, and prompts dt_binding_check to throw an error if a dts author mistakenly defines the hall-switch-north/south nodes within either of the two devices that don't support that feature. I also find it simpler and more intuitive.
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +27
-0+ +if: + properties: + compatible: + contains: + enum: + - azoteq,iqs620a-keys + - azoteq,iqs621-keys + - azoteq,iqs622-keys +then: + patternProperties: + "^hall-switch-(north|south)$": + type: object + description: + Represents north/south-field Hall-effect sensor touch or proximity + events. Note that north/south-field orientation is reversed on the + IQS620AXzCSR device due to its flip-chip package. + + properties: + linux,code: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Numeric switch code associated with the event. + + azoteq,use-prox: + $ref: /schemas/types.yaml#/definitions/flag + description: + If present, specifies that Hall-effect sensor reporting shouldQuoted 8 lines
Quoted 6 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml line 125
Quoted 6 lines
Documentation/devicetree/bindings/input/iqs62x-keys.yaml new file +6
-0+ use the device's wide-range proximity threshold instead of its + close-range touch threshold (default). + + required: + - linux,code +Do you think I should specify additionalProperties: false within these child nodes?
Quoted 1 line
Yes.
Sure thing, will do.
FYI, since these additional changes are quite small and I believe I've implemented them exactly as you've suggested, I plan on retaining your Reviewed-by for v3 which I'll be sending out soon. However if you find that I have misinterpreted anything, please let me know and I will fix it promptly.
Wishing you a Happy New Year,
Jeff LaBundy
PATCH v2 2/7 mfd: Add support for Azoteq IQS620A/621/622/624/625
In reply to Jeff LaBundyThis patch adds core support for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.
---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Updated iqs62x_dev_init to account for 4/8/16-MHz clock divider in start-up delays and replaced ATI timeout routine with regmap_read_poll_timeout
- Added an error message to iqs62x_irq in case device status fails to be read
- Replaced sw_num member of iqs62x_core with a local variable in iqs62x_probe as the former was unused anywhere else
- Added comments throughout iqs62x_probe to clarify how devices are matched based on the presence of calibration data
- Inverted the product and software number comparison logic in iqs62x_probe to avoid an else...continue branch
- Changed iqs62x_probe from .probe callback to .probe_new callback, thereby eliminating the otherwise unused iqs62x_id array
- Moved iqs62x_suspend and iqs62x_resume below iqs62x_remove
- Eliminated tabbed alignment of regmap_config and i2c_driver struct members
- Added register definitions for register addresses used in iqs621_cal_regs, iqs620at_cal_regs and iqs62x_devs arrays
- Removed of_compatible string from IQS622 mfd_cell struct as its proximity (now ambient light) sensing functionality need not be represented using a child node
- Dissolved union in iqs62x_event_data to allow simultaneous use of ir_flags and als_flags
- Removed temp_flags member of iqs62x_event_data, IQS62X_EVENT_TEMP register enumeration and IQS62X_EVENT_UI_HI/LO from iqs620a_event_regs (thereby re- ducing IQS62X_EVENT_SIZE to 10) as they were unused
drivers/mfd/Kconfig | 13 + drivers/mfd/Makefile | 3 + drivers/mfd/iqs62x-core.c | 639 ++++++++++++++++++++++++++++++++++++++++++++ drivers/mfd/iqs62x-tables.c | 438 ++++++++++++++++++++++++++++++ include/linux/mfd/iqs62x.h | 146 ++++++++++ 5 files changed, 1239 insertions(+) create mode 100644 drivers/mfd/iqs62x-core.c create mode 100644 drivers/mfd/iqs62x-tables.c create mode 100644 include/linux/mfd/iqs62x.h
drivers/mfd/Kconfig +13 -0
@@ -642,6 +642,19 @@ config MFD_IPAQ_MICRO AT90LS8535 microcontroller flashed with a special iPAQ firmware using the custom protocol implemented in this driver. +config MFD_IQS62X + tristate "Azoteq IQS620A/621/622/624/625 core support" + depends on I2C + select MFD_CORE + select REGMAP_I2C + help + Say Y here if you want to build core support for the Azoteq IQS620A, + IQS621, IQS622, IQS624 and IQS625 multi-function sensors. Additional + options must be selected to enable device-specific functions. + + To compile this driver as a module, choose M here: the module will + be called iqs62x. + config MFD_JANZ_CMODIO tristate "Janz CMOD-IO PCI MODULbus Carrier Board" select MFD_CORE
drivers/mfd/Makefile +3 -0
@@ -232,6 +232,9 @@ obj-$(CONFIG_MFD_DLN2) += dln2.o obj-$(CONFIG_MFD_RT5033) += rt5033.o obj-$(CONFIG_MFD_SKY81452) += sky81452.o +iqs62x-objs := iqs62x-core.o iqs62x-tables.o +obj-$(CONFIG_MFD_IQS62X) += iqs62x.o + intel-soc-pmic-objs := intel_soc_pmic_core.o intel_soc_pmic_crc.o obj-$(CONFIG_INTEL_SOC_PMIC) += intel-soc-pmic.o obj-$(CONFIG_INTEL_SOC_PMIC_BXTWC) += intel_soc_pmic_bxtwc.o
drivers/mfd/iqs62x-core.c new file +639 -0
639 lines not on this page · raw message
drivers/mfd/iqs62x-tables.c new file +438 -0
438 lines not on this page · raw message
include/linux/mfd/iqs62x.h new file +146 -0
146 lines not on this page · raw message
--
2.7.4
PATCH v2 3/7 input: keyboard: Add support for Azoteq IQS620A/621/622/624/625
In reply to Jeff LaBundyThis patch adds key and switch support for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.
---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Updated iqs62x_keys_parse_prop to use unified device property interface
- Clarified the comment in iqs62x_keys_notifier to state that wheel up or down events elicit an emulated release cycle
- Eliminated tabbed alignment of platform_driver struct members
drivers/input/keyboard/Kconfig | 10 ++ drivers/input/keyboard/Makefile | 1 + drivers/input/keyboard/iqs62x-keys.c | 340 +++++++++++++++++++++++++++++++++++ 3 files changed, 351 insertions(+) create mode 100644 drivers/input/keyboard/iqs62x-keys.c
drivers/input/keyboard/Kconfig +10 -0
@@ -663,6 +663,16 @@ config KEYBOARD_IPAQ_MICRO To compile this driver as a module, choose M here: the module will be called ipaq-micro-keys. +config KEYBOARD_IQS62X + tristate "Azoteq IQS620A/621/622/624/625 keys and switches" + depends on MFD_IQS62X + help + Say Y here to enable key and switch support for the Azoteq IQS620A, + IQS621, IQS622, IQS624 and IQS625 multi-function sensors. + + To compile this driver as a module, choose M here: the module will + be called iqs62x-keys. + config KEYBOARD_OMAP tristate "TI OMAP keypad support" depends on ARCH_OMAP1
drivers/input/keyboard/Makefile +1 -0
@@ -28,6 +28,7 @@ obj-$(CONFIG_KEYBOARD_TCA8418) += tca8418_keypad.o obj-$(CONFIG_KEYBOARD_HIL) += hil_kbd.o obj-$(CONFIG_KEYBOARD_HIL_OLD) += hilkbd.o obj-$(CONFIG_KEYBOARD_IPAQ_MICRO) += ipaq-micro-keys.o +obj-$(CONFIG_KEYBOARD_IQS62X) += iqs62x-keys.o obj-$(CONFIG_KEYBOARD_IMX) += imx_keypad.o obj-$(CONFIG_KEYBOARD_IMX_SC_KEY) += imx_sc_key.o obj-$(CONFIG_KEYBOARD_HP6XX) += jornada680_kbd.o
drivers/input/keyboard/iqs62x-keys.c new file +340 -0
340 lines not on this page · raw message
--
2.7.4
PATCH v2 4/7 pwm: Add support for Azoteq IQS620A PWM generator
In reply to Jeff LaBundyThis patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument of iqs620_pwm_apply and removed all modifications to the variable's contents
- Updated iqs620_pwm_apply to return -ENOTSUPP or -EINVAL if the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updated iqs620_pwm_apply to disable the PWM output if duty cycle is zero
- Added iqs620_pwm_get_state
- Eliminated tabbed alignment of pwm_ops and platform_driver struct members
- Moved notifier unregistration to already present iqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment in iqs620_pwm_probe to explain the order of operations
- Changed Kconfig "depends on" logic to MFD_IQS62X || COMPILE_TEST
drivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.c
drivers/pwm/Kconfig +10 -0
@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENIC
drivers/pwm/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.o
drivers/pwm/pwm-iqs620a.c new file +206 -0
206 lines not on this page · raw message
--
2.7.4
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Quoted 66 lines · Jeff LaBundy
This patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument ofiqs620_pwm_applyand removed all modifications to the variable's contents
- Updatediqs620_pwm_applyto return-ENOTSUPPor-EINVALif the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updatediqs620_pwm_applyto disable the PWM output if duty cycle is zero
- Addediqs620_pwm_get_state
- Eliminated tabbed alignment ofpwm_opsandplatform_driverstruct members
- Moved notifier unregistration to already presentiqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment iniqs620_pwm_probeto explain the order of operations
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.cdrivers/pwm/Kconfig +10
-0@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENICdrivers/pwm/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.odrivers/pwm/pwm-iqs620a.c new file +0
-0
drivers/pwm/pwm-iqs620a.c line 9
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c new file +9
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620A PWM Generator +* +* Copyright (C) 2019 Jeff LaBundy <[email protected]> +* +* Limitations: +* - The period is not guaranteed to run to completion when the duty cycle is +* changed or the output is disabled.
Do you know more details here? "not guaranteed" means that the new period starts immediately when duty_cycle or the enabled bit is written?
Quoted 47 lines
drivers/pwm/pwm-iqs620a.c new file +47
-0+* - The period is fixed to 1 ms. +*/ + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h> +#include <linux/slab.h> + +#define IQS620_PWR_SETTINGS 0xD2 +#define IQS620_PWR_SETTINGS_PWM_OUT BIT(7) + +#define IQS620_PWM_DUTY_CYCLE 0xD8 + +#define IQS620_PWM_PERIOD_NS 1000000 + +struct iqs620_pwm_private { +struct iqs62x_core *iqs62x; +struct pwm_chip chip; +struct notifier_block notifier; +}; + +static int iqs620_pwm_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct iqs62x_core *iqs62x; +unsigned int pwm_out =0 ; +int duty_scale, ret; + +if (state->polarity !=PWM_POLARITY_NORMAL ) +return -ENOTSUPP ; + +if (state->period <IQS620_PWM_PERIOD_NS ) +return -EINVAL ; + + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); + iqs62x = iqs620_pwm->iqs62x; + + duty_scale =DIV_ROUND_CLOSEST (state->duty_cycle *256 , +IQS620_PWM_PERIOD_NS ); + +if (duty_scale) { + ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE,
drivers/pwm/pwm-iqs620a.c line 66
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ min(duty_scale - 1, 0xFF)); +if (ret) +return ret; + +if (state->enabled) + pwm_out =IQS620_PWR_SETTINGS_PWM_OUT ; + } + +return regmap_update_bits (iqs62x->map,IQS620_PWR_SETTINGS , +IQS620_PWR_SETTINGS_PWM_OUT , pwm_out);
A comment explaining the semantic here would be good. I assume IQS620_PWM_DUTY_CYCLE takes a value between 0 and 255 and the resulting duty cycle is:
(IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms
.
If this is right, please use:
duty_scale = (state->duty_cycle * 256) / IQS620_PWM_PERIOD_NS
Also, when the hardware is running at
.enabled = 1, .duty_cycle = 1/256 ms, .period = 1ms
and you reconfigure to
.enabled = 0, .duty_cycle = 1ms, .period = 1ms
the output might be active for > 1/256 ms if the process is preempted between writing IQS620_PWM_DUTY_CYCLE and IQS620_PWR_SETTINGS_PWM_OUT.
Quoted 13 lines
drivers/pwm/pwm-iqs620a.c new file +13
-0+} + +static void iqs620_pwm_get_state (struct pwm_chip *chip,struct pwm_device *pwm, +struct pwm_state *state) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct iqs62x_core *iqs62x; +unsigned int val; +int ret; + + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); + iqs62x = iqs620_pwm->iqs62x; +
drivers/pwm/pwm-iqs620a.c line 89
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ ret =regmap_read (iqs62x->map,IQS620_PWR_SETTINGS , &val); +if (ret) + goto err_out; + state->enabled = val &IQS620_PWR_SETTINGS_PWM_OUT ; + + ret =regmap_read (iqs62x->map,IQS620_PWM_DUTY_CYCLE , &val); +if (ret) + goto err_out; + state->duty_cycle =DIV_ROUND_CLOSEST ((val +1 ) *IQS620_PWM_PERIOD_NS , +256 );
Please round up.
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ state->period =IQS620_PWM_PERIOD_NS ; + +err_out: +if (ret) +dev_err (iqs620_pwm->chip.dev,"Failed to get state: %d\n" , ret); +} + +static int iqs620_pwm_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{
drivers/pwm/pwm-iqs620a.c line 109
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+struct iqs620_pwm_private *iqs620_pwm; +struct pwm_state state; +int ret; + +if (!(event_flags &BIT (IQS62X_EVENT_SYS_RESET ))) +return NOTIFY_DONE ; + + iqs620_pwm = container_of(notifier, struct iqs620_pwm_private, + notifier); +pwm_get_state (&iqs620_pwm->chip.pwms[0 ], &state);
Please don't call pwm API functions in callbacks. I assume you rely on pwm_get_state returning the previously set state and that iqs620_pwm_get_state isn't called. Please use pwm->state for that.
Quoted 48 lines
drivers/pwm/pwm-iqs620a.c new file +48
-0+ ret =iqs620_pwm_apply (&iqs620_pwm->chip, + &iqs620_pwm->chip.pwms[0 ], &state); +if (ret) { +dev_err (iqs620_pwm->chip.dev, +"Failed to re-initialize device: %d\n" , ret); +return NOTIFY_BAD ; + } + +return NOTIFY_OK ; +} + +static const struct pwm_ops iqs620_pwm_ops = { + .apply = iqs620_pwm_apply, + .get_state = iqs620_pwm_get_state, + .owner =THIS_MODULE , +}; + +static int iqs620_pwm_probe (struct platform_device *pdev) +{ +struct iqs620_pwm_private *iqs620_pwm; +int ret1, ret2; + + iqs620_pwm =devm_kzalloc (&pdev->dev,sizeof (*iqs620_pwm),GFP_KERNEL ); +if (!iqs620_pwm) +return -ENOMEM ; + +platform_set_drvdata (pdev, iqs620_pwm); + iqs620_pwm->iqs62x =dev_get_drvdata (pdev->dev.parent); + + iqs620_pwm->chip.dev = &pdev->dev; + iqs620_pwm->chip.ops = &iqs620_pwm_ops; + iqs620_pwm->chip.base =-1 ; + iqs620_pwm->chip.npwm =1 ; + + ret1 =pwmchip_add (&iqs620_pwm->chip); +if (ret1) { +dev_err (&pdev->dev,"Failed to add device: %d\n" , ret1); +return ret1; + } + +/* +* Since iqs620_pwm_notifier uses iqs620_pwm->chip.pwms[], the notifier +* is not registered until pwmchip_add (which allocates that array) has +* been called. If registration fails, the newly added device has to be +* removed because the driver fails to probe and iqs620_pwm_remove will +* never be called. +*/ + iqs620_pwm->notifier.notifier_call = iqs620_pwm_notifier;
drivers/pwm/pwm-iqs620a.c line 168
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ ret1 =blocking_notifier_chain_register (&iqs620_pwm->iqs62x->nh, + &iqs620_pwm->notifier); +if (ret1) { +dev_err (&pdev->dev,"Failed to register notifier: %d\n" , ret1); + + ret2 =pwmchip_remove (&iqs620_pwm->chip); +if (ret2) { +dev_err (&pdev->dev,"Failed to remove device: %d\n" , + ret2); +return ret2;
This exitpoint is bad. The PWM driver is active but the module gets unloaded. I liked the approach from v1 better.
ret2 could be local to this block.
drivers/pwm/pwm-iqs620a.c line 149
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+ } + } + +return ret1; +}
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hi Uwe,
Thank you for your continued support on this project.
Quoted 71 lines · Uwe Kleine-König
Quoted 70 lines · Jeff LaBundy
This patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument ofiqs620_pwm_applyand removed all modifications to the variable's contents
- Updatediqs620_pwm_applyto return-ENOTSUPPor-EINVALif the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updatediqs620_pwm_applyto disable the PWM output if duty cycle is zero
- Addediqs620_pwm_get_state
- Eliminated tabbed alignment ofpwm_opsandplatform_driverstruct members
- Moved notifier unregistration to already presentiqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment iniqs620_pwm_probeto explain the order of operations
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.cdrivers/pwm/Kconfig +10
-0@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENICdrivers/pwm/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.odrivers/pwm/pwm-iqs620a.c new file +4
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620A PWM Generator +*
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 9
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+* Copyright (C) 2019 Jeff LaBundy <[email protected]> +* +* Limitations: +* - The period is not guaranteed to run to completion when the duty cycle is +* changed or the output is disabled. Do you know more details here? "not guaranteed" means that the new period starts immediately when
duty_cycleor the enabled bit is written?
Increasing the duty cycle on-the-fly (e.g. 25% to 75%) results in the following behavior (depending on where the I2C write falls):
I2C write
__ __ __ V_ ______ ______ ______ __
__| |______| |______| |_|x|__| |__| |__| |__|
^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^
The PWM continues to tick at 1 ms, but the currently running period suffers an extraneous pulse as the output is abruptly set high to "catch up" to the new duty cycle.
A similar behavior can occur if the duty cycle is decreased, meaning the output is abruptly set low if the I2C transaction completes in what has suddenly become the inactive region of the currently running period.
The PWM seems to be a simple counter that rolls over at a period of 1 ms. Both the counter and the IQS620_PWM_DUTY_CYCLE register effectively go to a comparator whose output is ANDed with IQS620_PWR_SETTINGS_PWM_OUT which then drives the PWM output.
As such, if either IQS620_PWM_DUTY_CYCLE or IQS620_PWR_SETTINGS_PWM_OUT change, so may the PWM output state depending on the counter's value at the time the I2C write is completed within the 1-ms continuous loop.
For v3 I will update the note as follows:
- Changes in duty cycle or enable/disable state are immediately reflected by the PWM output and are not aligned to the start of any period.
Quoted 62 lines
Quoted 47 lines
drivers/pwm/pwm-iqs620a.c new file +47
-0+* - The period is fixed to 1 ms. +*/ + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h> +#include <linux/slab.h> + +#define IQS620_PWR_SETTINGS 0xD2 +#define IQS620_PWR_SETTINGS_PWM_OUT BIT(7) + +#define IQS620_PWM_DUTY_CYCLE 0xD8 + +#define IQS620_PWM_PERIOD_NS 1000000 + +struct iqs620_pwm_private { +struct iqs62x_core *iqs62x; +struct pwm_chip chip; +struct notifier_block notifier; +}; + +static int iqs620_pwm_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct iqs62x_core *iqs62x; +unsigned int pwm_out =0 ; +int duty_scale, ret; + +if (state->polarity !=PWM_POLARITY_NORMAL ) +return -ENOTSUPP ; + +if (state->period <IQS620_PWM_PERIOD_NS ) +return -EINVAL ; + + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); + iqs62x = iqs620_pwm->iqs62x; + + duty_scale =DIV_ROUND_CLOSEST (state->duty_cycle *256 , +IQS620_PWM_PERIOD_NS ); + +if (duty_scale) { + ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE,drivers/pwm/pwm-iqs620a.c line 66
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ min(duty_scale - 1, 0xFF)); +if (ret) +return ret; + +if (state->enabled) + pwm_out =IQS620_PWR_SETTINGS_PWM_OUT ; + } + +return regmap_update_bits (iqs62x->map,IQS620_PWR_SETTINGS , +IQS620_PWR_SETTINGS_PWM_OUT , pwm_out);A comment explaining the semantic here would be good. I assume
IQS620_PWM_DUTY_CYCLEtakes a value between 0 and 255 and the resulting duty cycle is:
Quoted 8 lines
(IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms.
If this is right, please use:
duty_scale = (state->duty_cycle * 256) / IQS620_PWM_PERIOD_NS
Sure thing, will do. I'll add a comment and round down. Your assumption is correct as well.
Quoted 11 lines
Also, when the hardware is running at
.enabled = 1, .duty_cycle = 1/256 ms, .period = 1msand you reconfigure to
.enabled = 0, .duty_cycle = 1ms, .period = 1msthe output might be active for > 1/256 ms if the process is preempted between writing
IQS620_PWM_DUTY_CYCLEandIQS620_PWR_SETTINGS_PWM_OUT.
Good catch. I think we can solve this by writing IQS620_PWM_DUTY_CYCLE first followed by IQS620_PWR_SETTINGS_PWM_OUT when the PWM is going to be enabled, and the reverse when the PWM is going to be disabled (i.e. turn OFF to prevent a stale duty cycle from being temporarily driven).
Quoted 17 lines
Quoted 17 lines
drivers/pwm/pwm-iqs620a.c new file +17
-0+} + +static void iqs620_pwm_get_state (struct pwm_chip *chip,struct pwm_device *pwm, +struct pwm_state *state) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct iqs62x_core *iqs62x; +unsigned int val; +int ret; + + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); + iqs62x = iqs620_pwm->iqs62x; + + ret =regmap_read (iqs62x->map,IQS620_PWR_SETTINGS , &val); +if (ret) + goto err_out; + state->enabled = val &IQS620_PWR_SETTINGS_PWM_OUT ;
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 89
Quoted 6 lines
drivers/pwm/pwm-iqs620a.c new file +6
-0+ + ret =regmap_read (iqs62x->map,IQS620_PWM_DUTY_CYCLE , &val); +if (ret) + goto err_out; + state->duty_cycle =DIV_ROUND_CLOSEST ((val +1 ) *IQS620_PWM_PERIOD_NS , +256 );Please round up.
Sure thing, will do.
Quoted 16 lines
Quoted 16 lines
drivers/pwm/pwm-iqs620a.c new file +16
-0+ state->period =IQS620_PWM_PERIOD_NS ; + +err_out: +if (ret) +dev_err (iqs620_pwm->chip.dev,"Failed to get state: %d\n" , ret); +} + +static int iqs620_pwm_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct pwm_state state; +int ret; + +if (!(event_flags &BIT (IQS62X_EVENT_SYS_RESET ))) +return NOTIFY_DONE ;
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 109
Quoted 4 lines
drivers/pwm/pwm-iqs620a.c new file +4
-0+ + iqs620_pwm = container_of(notifier, struct iqs620_pwm_private, + notifier); +pwm_get_state (&iqs620_pwm->chip.pwms[0 ], &state);Please don't call pwm API functions in callbacks. I assume you rely on
pwm_get_statereturning the previously set state and thatiqs620_pwm_get_stateisn't called. Please usepwm->statefor that.
Sure thing, will do. Your assumption is correct. If pwm_get_state called chip->ops->get_state instead of return pwm->state as it does today, this function would break because it would restore the hardware using default register values (since this function follows a reset).
Just for my own understanding, are you saying the PWM framework reserves the right to update pwm_get_state to call chip->ops->get_state some time in the future? In any event I will refer to pwm->state as that is what I ultimately need here.
FWIW, I borrowed the idea from the resume callback of [0] which possibly suffers the same fate if I have understood the concern correctly.
This discussion also made me realize that we need a lock around back-to- back access to IQS620_PWR_SETTINGS_PWM_OUT and IQS620_PWM_DUTY_CYCLE in case iqs620_pwm_get_state is called while iqs620_pwm_apply restores them on behalf of iqs620_pwm_notifier. I will add that in v3.
Quoted 53 lines
Quoted 53 lines
drivers/pwm/pwm-iqs620a.c new file +53
-0+ ret =iqs620_pwm_apply (&iqs620_pwm->chip, + &iqs620_pwm->chip.pwms[0 ], &state); +if (ret) { +dev_err (iqs620_pwm->chip.dev, +"Failed to re-initialize device: %d\n" , ret); +return NOTIFY_BAD ; + } + +return NOTIFY_OK ; +} + +static const struct pwm_ops iqs620_pwm_ops = { + .apply = iqs620_pwm_apply, + .get_state = iqs620_pwm_get_state, + .owner =THIS_MODULE , +}; + +static int iqs620_pwm_probe (struct platform_device *pdev) +{ +struct iqs620_pwm_private *iqs620_pwm; +int ret1, ret2; + + iqs620_pwm =devm_kzalloc (&pdev->dev,sizeof (*iqs620_pwm),GFP_KERNEL ); +if (!iqs620_pwm) +return -ENOMEM ; + +platform_set_drvdata (pdev, iqs620_pwm); + iqs620_pwm->iqs62x =dev_get_drvdata (pdev->dev.parent); + + iqs620_pwm->chip.dev = &pdev->dev; + iqs620_pwm->chip.ops = &iqs620_pwm_ops; + iqs620_pwm->chip.base =-1 ; + iqs620_pwm->chip.npwm =1 ; + + ret1 =pwmchip_add (&iqs620_pwm->chip); +if (ret1) { +dev_err (&pdev->dev,"Failed to add device: %d\n" , ret1); +return ret1; + } + +/* +* Since iqs620_pwm_notifier uses iqs620_pwm->chip.pwms[], the notifier +* is not registered until pwmchip_add (which allocates that array) has +* been called. If registration fails, the newly added device has to be +* removed because the driver fails to probe and iqs620_pwm_remove will +* never be called. +*/ + iqs620_pwm->notifier.notifier_call = iqs620_pwm_notifier; + ret1 =blocking_notifier_chain_register (&iqs620_pwm->iqs62x->nh, + &iqs620_pwm->notifier); +if (ret1) { +dev_err (&pdev->dev,"Failed to register notifier: %d\n" , ret1); +
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 168
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+ ret2 =pwmchip_remove (&iqs620_pwm->chip); +if (ret2) { +dev_err (&pdev->dev,"Failed to remove device: %d\n" , + ret2); +return ret2;This exitpoint is bad. The PWM driver is active but the module gets unloaded. I liked the approach from v1 better.
Sure thing, I'll revert the behavior to that of v1.
Quoted 2 lines
ret2 could be local to this block.
This will go away automatically.
Quoted 12 lines
drivers/pwm/pwm-iqs620a.c line 149
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+ } + } + +return ret1; +}Best regards
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
[0] drivers/pwm/pwm-atmel-hlcdc.c
Kind regards,
Jeff LaBundy
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hello Jeff,
Quoted 103 lines · Jeff LaBundy
Quoted 71 lines · Uwe Kleine-König
Quoted 70 lines · Jeff LaBundy
This patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument ofiqs620_pwm_applyand removed all modifications to the variable's contents
- Updatediqs620_pwm_applyto return-ENOTSUPPor-EINVALif the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updatediqs620_pwm_applyto disable the PWM output if duty cycle is zero
- Addediqs620_pwm_get_state
- Eliminated tabbed alignment ofpwm_opsandplatform_driverstruct members
- Moved notifier unregistration to already presentiqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment iniqs620_pwm_probeto explain the order of operations
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.cdrivers/pwm/Kconfig +10
-0@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENICdrivers/pwm/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.odrivers/pwm/pwm-iqs620a.c new file +4
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620A PWM Generator +* Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 9
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+* Copyright (C) 2019 Jeff LaBundy <[email protected]> +* +* Limitations: +* - The period is not guaranteed to run to completion when the duty cycle is +* changed or the output is disabled. Do you know more details here? "not guaranteed" means that the new period starts immediately when
duty_cycleor the enabled bit is written?Increasing the duty cycle on-the-fly (e.g. 25% to 75%) results in the following behavior (depending on where the I2C write falls):
I2C write
__ __ __ V_ ______ ______ ______ __
__| |______| |______| |_|x|__| |__| |__| |__|
^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^The PWM continues to tick at 1 ms, but the currently running period suffers an extraneous pulse as the output is abruptly set high to "catch up" to the new duty cycle.
A similar behavior can occur if the duty cycle is decreased, meaning the output is abruptly set low if the I2C transaction completes in what has suddenly become the inactive region of the currently running period.
The PWM seems to be a simple counter that rolls over at a period of 1 ms. Both the counter and the
IQS620_PWM_DUTY_CYCLEregister effectively go to a comparator whose output is ANDed withIQS620_PWR_SETTINGS_PWM_OUTwhich then drives the PWM output.
Quoted 8 lines
As such, if either
IQS620_PWM_DUTY_CYCLEorIQS620_PWR_SETTINGS_PWM_OUTchange, so may the PWM output state depending on the counter's value at the time the I2C write is completed within the 1-ms continuous loop.For v3 I will update the note as follows:
- Changes in duty cycle or enable/disable state are immediately reflected by the PWM output and are not aligned to the start of any period.
I'd like to see a bit more information in the driver. Something about the 1ms rhythm being unaffected by the duty_cycle and enable setting. Maybe:
- The periods run continuously with a fixed length of 1 ms which is unaffected by register updates. Writing duty cycle or enable registers gets active immediately which might result in glitches.
?
Quoted 83 lines
Quoted 63 lines
Quoted 48 lines
drivers/pwm/pwm-iqs620a.c new file +47
-0+* - The period is fixed to 1 ms. +*/ + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h> +#include <linux/slab.h> + +#define IQS620_PWR_SETTINGS 0xD2 +#define IQS620_PWR_SETTINGS_PWM_OUT BIT(7) + +#define IQS620_PWM_DUTY_CYCLE 0xD8 + +#define IQS620_PWM_PERIOD_NS 1000000 + +struct iqs620_pwm_private { +struct iqs62x_core *iqs62x; +struct pwm_chip chip; +struct notifier_block notifier; +}; + +static int iqs620_pwm_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct iqs62x_core *iqs62x; +unsigned int pwm_out =0 ; +int duty_scale, ret; + +if (state->polarity !=PWM_POLARITY_NORMAL ) +return -ENOTSUPP ; + +if (state->period <IQS620_PWM_PERIOD_NS ) +return -EINVAL ; + + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); + iqs62x = iqs620_pwm->iqs62x; + + duty_scale =DIV_ROUND_CLOSEST (state->duty_cycle *256 , +IQS620_PWM_PERIOD_NS ); + +if (duty_scale) { + ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE,drivers/pwm/pwm-iqs620a.c line 66
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ min(duty_scale - 1, 0xFF)); +if (ret) +return ret; + +if (state->enabled) + pwm_out =IQS620_PWR_SETTINGS_PWM_OUT ; + } + +return regmap_update_bits (iqs62x->map,IQS620_PWR_SETTINGS , +IQS620_PWR_SETTINGS_PWM_OUT , pwm_out);A comment explaining the semantic here would be good. I assume
IQS620_PWM_DUTY_CYCLEtakes a value between 0 and 255 and the resulting duty cycle is:Quoted 8 lines
(IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms.
If this is right, please use:
duty_scale = (state->duty_cycle * 256) / IQS620_PWM_PERIOD_NSSure thing, will do. I'll add a comment and round down. Your assumption is correct as well.
Quoted 8 lines
Also, when the hardware is running at
.enabled = 1, .duty_cycle = 1/256 ms, .period = 1msand you reconfigure to
.enabled = 0, .duty_cycle = 1ms, .period = 1ms
Quoted 8 lines
Quoted 3 lines
the output might be active for > 1/256 ms if the process is preempted between writing
IQS620_PWM_DUTY_CYCLEandIQS620_PWR_SETTINGS_PWM_OUT.Good catch. I think we can solve this by writing
IQS620_PWM_DUTY_CYCLEfirst followed byIQS620_PWR_SETTINGS_PWM_OUTwhen the PWM is going to be enabled, and the reverse when the PWM is going to be disabled (i.e. turn OFF to prevent a stale duty cycle from being temporarily driven).
Sounds like a plan. After disabling you even don't need to write the duty cycle register. (But there might be a discussion ahead that .get_state should return the duty cycle.)
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c new file +9
-0+static int iqs620_pwm_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct pwm_state state; +int ret; + +if (!(event_flags &BIT (IQS62X_EVENT_SYS_RESET ))) +return NOTIFY_DONE ;Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 109
Quoted 4 lines
drivers/pwm/pwm-iqs620a.c new file +4
-0+ + iqs620_pwm = container_of(notifier, struct iqs620_pwm_private, + notifier); +pwm_get_state (&iqs620_pwm->chip.pwms[0 ], &state);Please don't call pwm API functions in callbacks. I assume you rely on
pwm_get_statereturning the previously set state and thatiqs620_pwm_get_stateisn't called. Please usepwm->statefor that.Sure thing, will do. Your assumption is correct. If
pwm_get_statecalledchip->ops->get_stateinstead of returnpwm->stateas it does today, this function would break because it would restore the hardware using default register values (since this function follows a reset).
Quoted 4 lines
Just for my own understanding, are you saying the PWM framework reserves the right to update
pwm_get_stateto callchip->ops->get_statesome time in the future? In any event I will refer topwm->stateas that is what I ultimately need here.
This already was the case for a short time before v5.4. See 01ccf903edd6 and 40a6b9a00930. (And note that the lazyness mentioned above about not needing to write duty_cycle when the PWM is off is what made the approach break however.) I don't know yet how to proceed here. Being able to get the actually implemented setting would be nice, probably it is prudent to do this with another API function.
Other than that I consider it a layer violation to call a function that is designed for consumers in a lowlevel driver. I don't know if we need locking at some time, but if the core holded a lock when .apply is called, .apply calls pwm_get_state which wanted to grab the lock again we get a dead-lock.
Quoted 2 lines
FWIW, I borrowed the idea from the resume callback of [0] which possibly suffers the same fate if I have understood the concern correctly.
Yeah, there are many drivers that are not up to date with my review requirements. The problem is a) missing time to update them and b) for some drivers it's hard to get test coverage for changes.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hi Uwe,
Quoted 119 lines · Uwe Kleine-König
Hello Jeff,
Quoted 103 lines · Jeff LaBundy
Quoted 71 lines · Uwe Kleine-König
Quoted 70 lines · Jeff LaBundy
This patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument ofiqs620_pwm_applyand removed all modifications to the variable's contents
- Updatediqs620_pwm_applyto return-ENOTSUPPor-EINVALif the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updatediqs620_pwm_applyto disable the PWM output if duty cycle is zero
- Addediqs620_pwm_get_state
- Eliminated tabbed alignment ofpwm_opsandplatform_driverstruct members
- Moved notifier unregistration to already presentiqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment iniqs620_pwm_probeto explain the order of operations
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.cdrivers/pwm/Kconfig +10
-0@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENICdrivers/pwm/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.odrivers/pwm/pwm-iqs620a.c new file +4
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620A PWM Generator +* Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 9
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+* Copyright (C) 2019 Jeff LaBundy <[email protected]> +* +* Limitations: +* - The period is not guaranteed to run to completion when the duty cycle is +* changed or the output is disabled. Do you know more details here? "not guaranteed" means that the new period starts immediately when
duty_cycleor the enabled bit is written?Increasing the duty cycle on-the-fly (e.g. 25% to 75%) results in the following behavior (depending on where the I2C write falls):
I2C write
__ __ __ V_ ______ ______ ______ __
__| |______| |______| |_|x|__| |__| |__| |__|
^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^The PWM continues to tick at 1 ms, but the currently running period suffers an extraneous pulse as the output is abruptly set high to "catch up" to the new duty cycle.
A similar behavior can occur if the duty cycle is decreased, meaning the output is abruptly set low if the I2C transaction completes in what has suddenly become the inactive region of the currently running period.
The PWM seems to be a simple counter that rolls over at a period of 1 ms. Both the counter and the
IQS620_PWM_DUTY_CYCLEregister effectively go to a comparator whose output is ANDed withIQS620_PWR_SETTINGS_PWM_OUTwhich then drives the PWM output.Quoted 8 lines
As such, if either
IQS620_PWM_DUTY_CYCLEorIQS620_PWR_SETTINGS_PWM_OUTchange, so may the PWM output state depending on the counter's value at the time the I2C write is completed within the 1-ms continuous loop.For v3 I will update the note as follows:
- Changes in duty cycle or enable/disable state are immediately reflected by the PWM output and are not aligned to the start of any period.
I'd like to see a bit more information in the driver. Something about the 1ms rhythm being unaffected by the
duty_cycleand enable setting. Maybe:
Quoted 6 lines
- The periods run continuously with a fixed length of 1 ms which is unaffected by register updates. Writing duty cycle or enable registers gets active immediately which might result in glitches.
?
I adjusted the wording a bit as per my preference and settled on the following:
- The period is fixed to 1 ms and is generated continuously despite changes to the duty cycle or enable/disable state.
- Changes to the duty cycle or enable/disable state take effect immediately and may result in a glitch during the period in which the change is made.
I believe these capture the spirit of your message; please let me know if you have any concerns.
Upon further experimentation, I found that disabling the output (which v2 does so as to simulate a 0% duty cycle) does not actively drive zero, but rather places the output in a high-impedance state with only the device's own internal leakage eventually discharging the pin.
This is fundamentally different than actively driving the pin low to make a 0% duty cycle, which does not appear to be possible at all. Therefore I have removed the control of IQS620_PWR_SETTINGS_PWM_OUT based on the duty cycle requested by the user and reverted to the behavior of v1, where the duty cycle requested by the user is mapped only to IQS620_PWM_DUTY_CYCLE.
As such, I have also added a third bullet point similar to what you first suggested following v1:
- The device cannot generate a 0% duty cycle.
Quoted 87 lines
Quoted 87 lines
Quoted 63 lines
Quoted 48 lines
drivers/pwm/pwm-iqs620a.c new file +47
-0+* - The period is fixed to 1 ms. +*/ + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h> +#include <linux/slab.h> + +#define IQS620_PWR_SETTINGS 0xD2 +#define IQS620_PWR_SETTINGS_PWM_OUT BIT(7) + +#define IQS620_PWM_DUTY_CYCLE 0xD8 + +#define IQS620_PWM_PERIOD_NS 1000000 + +struct iqs620_pwm_private { +struct iqs62x_core *iqs62x; +struct pwm_chip chip; +struct notifier_block notifier; +}; + +static int iqs620_pwm_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct iqs62x_core *iqs62x; +unsigned int pwm_out =0 ; +int duty_scale, ret; + +if (state->polarity !=PWM_POLARITY_NORMAL ) +return -ENOTSUPP ; + +if (state->period <IQS620_PWM_PERIOD_NS ) +return -EINVAL ; + + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); + iqs62x = iqs620_pwm->iqs62x; + + duty_scale =DIV_ROUND_CLOSEST (state->duty_cycle *256 , +IQS620_PWM_PERIOD_NS ); + +if (duty_scale) { + ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE,drivers/pwm/pwm-iqs620a.c line 66
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ min(duty_scale - 1, 0xFF)); +if (ret) +return ret; + +if (state->enabled) + pwm_out =IQS620_PWR_SETTINGS_PWM_OUT ; + } + +return regmap_update_bits (iqs62x->map,IQS620_PWR_SETTINGS , +IQS620_PWR_SETTINGS_PWM_OUT , pwm_out);A comment explaining the semantic here would be good. I assume
IQS620_PWM_DUTY_CYCLEtakes a value between 0 and 255 and the resulting duty cycle is:Quoted 8 lines
(IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms.
If this is right, please use:
duty_scale = (state->duty_cycle * 256) / IQS620_PWM_PERIOD_NSSure thing, will do. I'll add a comment and round down. Your assumption is correct as well.
Quoted 11 lines
Also, when the hardware is running at
.enabled = 1, .duty_cycle = 1/256 ms, .period = 1msand you reconfigure to
.enabled = 0, .duty_cycle = 1ms, .period = 1msthe output might be active for > 1/256 ms if the process is preempted between writing
IQS620_PWM_DUTY_CYCLEandIQS620_PWR_SETTINGS_PWM_OUT.
Quoted 9 lines
Quoted 4 lines
Good catch. I think we can solve this by writing
IQS620_PWM_DUTY_CYCLEfirst followed byIQS620_PWR_SETTINGS_PWM_OUTwhen the PWM is going to be enabled, and the reverse when the PWM is going to be disabled (i.e. turn OFF to prevent a stale duty cycle from being temporarily driven).Sounds like a plan. After disabling you even don't need to write the duty cycle register. (But there might be a discussion ahead that .get_state should return the duty cycle.)
For v3, I've opted to write IQS620_PWM_DUTY_CYCLE regardless of whether the PWM is enabled as a matter of principle (i.e. faithfully pass the entire state down to the hardware without making assumptions).
And since some consumers (e.g. leds-pwm, the primary use-case for this PWM) may pre-configure the duty cycle while the PWM is disabled, this method ensures the driver is already compliant in case 01ccf903edd6 returns.
Also, as mentioned above, I have dropped the automatic disabling of the PWM to simulate a 0% duty cycle if the requested duty cycle is < 1 / 256 ms since the device does not actively drive a zero with IQS620_PWR_SETTINGS_PWM_OUT = 0. In the interest of transparency, here is what I currently have for v3:
static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
struct iqs620_pwm_private *iqs620_pwm;
struct iqs62x_core *iqs62x;
int duty_scale, ret;
if (state->polarity != PWM_POLARITY_NORMAL)
return -ENOTSUPP;
if (state->period < IQS620_PWM_PERIOD_NS)
return -EINVAL;
iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
iqs62x = iqs620_pwm->iqs62x;
if (!state->enabled) {
ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS,
IQS620_PWR_SETTINGS_PWM_OUT, 0);
if (ret)
return ret;
}
/*
* The duty cycle generated by the device is calculated as follows:
*
* duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms
*
* ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255
* (inclusive). Therefore the lowest duty cycle the device can generate
* while the output is enabled is 1 / 256 ms.
*/
duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS - 1;
ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE,
clamp(duty_scale, 0, 0xFF));
if (ret)
return ret;
if (state->enabled)
ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS,
IQS620_PWR_SETTINGS_PWM_OUT, 0xFF);
return ret;
}I believe this captures all of the discussion thus far; please let me know if you have any concerns.
Quoted 36 lines
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c new file +9
-0+static int iqs620_pwm_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct pwm_state state; +int ret; + +if (!(event_flags &BIT (IQS62X_EVENT_SYS_RESET ))) +return NOTIFY_DONE ;Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 109
Quoted 4 lines
drivers/pwm/pwm-iqs620a.c new file +4
-0+ + iqs620_pwm = container_of(notifier, struct iqs620_pwm_private, + notifier); +pwm_get_state (&iqs620_pwm->chip.pwms[0 ], &state);Please don't call pwm API functions in callbacks. I assume you rely on
pwm_get_statereturning the previously set state and thatiqs620_pwm_get_stateisn't called. Please usepwm->statefor that.Sure thing, will do. Your assumption is correct. If
pwm_get_statecalledchip->ops->get_stateinstead of returnpwm->stateas it does today, this function would break because it would restore the hardware using default register values (since this function follows a reset).Quoted 4 lines
Just for my own understanding, are you saying the PWM framework reserves the right to update
pwm_get_stateto callchip->ops->get_statesome time in the future? In any event I will refer topwm->stateas that is what I ultimately need here.This already was the case for a short time before v5.4. See
01ccf903edd6and40a6b9a00930. (And note that the lazyness mentioned above about not needing to writeduty_cyclewhen the PWM is off is what made the approach break however.) I don't know yet how to proceed here. Being able to get the actually implemented setting would be nice, probably it is prudent to do this with another API function.
Quoted 6 lines
Other than that I consider it a layer violation to call a function that is designed for consumers in a lowlevel driver. I don't know if we need locking at some time, but if the core holded a lock when .apply is called, .apply calls
pwm_get_statewhich wanted to grab the lock again we get a dead-lock.
I think we may be imagining two different hazards (please correct me if I have misunderstood). Originally I thought you were warning that pwm_get_state (which simply returns pwm->state) may in the future call chip->ops->get_state instead, which here would have caused iqs620_pwm_notifier to call iqs620_pwm_apply with a reset-like state rather than the last state requested by the user (since this notifier is called after the device resets itself).
The short-lived change during the 5.4-rc phase, however, was to fill pwm->state in pwm_apply_state with the quantized state from the hardware instead of the raw state requested by the user. Whether pwm->state (accessed directly or by calling pwm_get_state) gives the raw state or the quantized output of get_state following the last call to pwm_apply_state does not change the outcome (for this particular driver) because iqs620_pwm_apply ultimately writes the same register values. Just to be safe, I've been testing with and without 01ccf903edd6 applied locally so as to validate the behavior in both scenarios.
What I missed originally is that pwm_get_state is intended for consumers only, in which case I agree it is fundamentally wrong to cannibalize it in the driver. For v3 I have changed iqs620_pwm_notifier to reference pwm->state directly since that is what is ultimately needed in the first place.
You're correct that a lock within the core would cause a deadlock; in this case I was proposing a lock around the pair of reads/writes to IQS620_PWM_DUTY_CYCLE and IQS620_PWR_SETTINGS_PWM_OUT since v2 introduced an implied relationship between the two. That would be safe since chip->ops->apply returns before pwm_apply_state calls chip->ops->get_state.
However, even that is no longer necessary since IQS620_PWR_SETTINGS_PWM_OUT is now written independently of IQS620_PWM_DUTY_CYCLE due to withdrawing support for a 0% duty cycle starting in v3. In short, I didn't end up adding any locks as there was no need.
Quoted 13 lines
Quoted 2 lines
FWIW, I borrowed the idea from the resume callback of [0] which possibly suffers the same fate if I have understood the concern correctly.
Yeah, there are many drivers that are not up to date with my review requirements. The problem is a) missing time to update them and b) for some drivers it's hard to get test coverage for changes.
Best regards
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Kind regards,
Jeff LaBundy
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hello Jeff,
Quoted 128 lines · Jeff LaBundy
Quoted 117 lines · Uwe Kleine-König
Quoted 103 lines · Jeff LaBundy
Quoted 71 lines · Uwe Kleine-König
Quoted 70 lines · Jeff LaBundy
This patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument ofiqs620_pwm_applyand removed all modifications to the variable's contents
- Updatediqs620_pwm_applyto return-ENOTSUPPor-EINVALif the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updatediqs620_pwm_applyto disable the PWM output if duty cycle is zero
- Addediqs620_pwm_get_state
- Eliminated tabbed alignment ofpwm_opsandplatform_driverstruct members
- Moved notifier unregistration to already presentiqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment iniqs620_pwm_probeto explain the order of operations
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.cdrivers/pwm/Kconfig +10
-0@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENICdrivers/pwm/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.odrivers/pwm/pwm-iqs620a.c new file +4
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620A PWM Generator +* Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 9
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+* Copyright (C) 2019 Jeff LaBundy <[email protected]> +* +* Limitations: +* - The period is not guaranteed to run to completion when the duty cycle is +* changed or the output is disabled. Do you know more details here? "not guaranteed" means that the new period starts immediately when
duty_cycleor the enabled bit is written?Increasing the duty cycle on-the-fly (e.g. 25% to 75%) results in the following behavior (depending on where the I2C write falls):
I2C write
__ __ __ V_ ______ ______ ______ __
__| |______| |______| |_|x|__| |__| |__| |__|
^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^The PWM continues to tick at 1 ms, but the currently running period suffers an extraneous pulse as the output is abruptly set high to "catch up" to the new duty cycle.
A similar behavior can occur if the duty cycle is decreased, meaning the output is abruptly set low if the I2C transaction completes in what has suddenly become the inactive region of the currently running period.
The PWM seems to be a simple counter that rolls over at a period of 1 ms. Both the counter and the
IQS620_PWM_DUTY_CYCLEregister effectively go to a comparator whose output is ANDed withIQS620_PWR_SETTINGS_PWM_OUTwhich then drives the PWM output.Quoted 8 lines
As such, if either
IQS620_PWM_DUTY_CYCLEorIQS620_PWR_SETTINGS_PWM_OUTchange, so may the PWM output state depending on the counter's value at the time the I2C write is completed within the 1-ms continuous loop.For v3 I will update the note as follows:
- Changes in duty cycle or enable/disable state are immediately reflected by the PWM output and are not aligned to the start of any period.
I'd like to see a bit more information in the driver. Something about the 1ms rhythm being unaffected by the
duty_cycleand enable setting. Maybe:Quoted 6 lines
- The periods run continuously with a fixed length of 1 ms which is unaffected by register updates. Writing duty cycle or enable registers gets active immediately which might result in glitches.
?
I adjusted the wording a bit as per my preference and settled on the following:
Quoted 7 lines
- The period is fixed to 1 ms and is generated continuously despite changes to the duty cycle or enable/disable state.
- Changes to the duty cycle or enable/disable state take effect immediately and may result in a glitch during the period in which the change is made.I believe these capture the spirit of your message; please let me know if you have any concerns.
That's fine.
Quoted 4 lines
Upon further experimentation, I found that disabling the output (which v2 does so as to simulate a 0% duty cycle) does not actively drive zero, but rather places the output in a high-impedance state with only the device's own internal leakage eventually discharging the pin.
But maybe this is still the best you can do in this case. @Thierry, what do you think?
Quoted 10 lines
This is fundamentally different than actively driving the pin low to make a 0% duty cycle, which does not appear to be possible at all. Therefore I have removed the control of
IQS620_PWR_SETTINGS_PWM_OUTbased on the duty cycle requested by the user and reverted to the behavior of v1, where the duty cycle requested by the user is mapped only toIQS620_PWM_DUTY_CYCLE.As such, I have also added a third bullet point similar to what you first suggested following v1:
- The device cannot generate a 0% duty cycle.
Then this would be:
- The device cannot actively drive a 0% duty cycle. The driver is disabled for small duty_cycles relying on a pull down on the board.
But maybe it would be more prudent to do this only if the board configuration suggests this is save?!
Quoted 134 lines
Quoted 86 lines
Quoted 86 lines
Quoted 62 lines
Quoted 47 lines
drivers/pwm/pwm-iqs620a.c new file +47
-0+* - The period is fixed to 1 ms. +*/ + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h> +#include <linux/slab.h> + +#define IQS620_PWR_SETTINGS 0xD2 +#define IQS620_PWR_SETTINGS_PWM_OUT BIT(7) + +#define IQS620_PWM_DUTY_CYCLE 0xD8 + +#define IQS620_PWM_PERIOD_NS 1000000 + +struct iqs620_pwm_private { +struct iqs62x_core *iqs62x; +struct pwm_chip chip; +struct notifier_block notifier; +}; + +static int iqs620_pwm_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct iqs62x_core *iqs62x; +unsigned int pwm_out =0 ; +int duty_scale, ret; + +if (state->polarity !=PWM_POLARITY_NORMAL ) +return -ENOTSUPP ; + +if (state->period <IQS620_PWM_PERIOD_NS ) +return -EINVAL ; + + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); + iqs62x = iqs620_pwm->iqs62x; + + duty_scale =DIV_ROUND_CLOSEST (state->duty_cycle *256 , +IQS620_PWM_PERIOD_NS ); + +if (duty_scale) { + ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE,drivers/pwm/pwm-iqs620a.c line 66
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ min(duty_scale - 1, 0xFF)); +if (ret) +return ret; + +if (state->enabled) + pwm_out =IQS620_PWR_SETTINGS_PWM_OUT ; + } + +return regmap_update_bits (iqs62x->map,IQS620_PWR_SETTINGS , +IQS620_PWR_SETTINGS_PWM_OUT , pwm_out);A comment explaining the semantic here would be good. I assume
IQS620_PWM_DUTY_CYCLEtakes a value between 0 and 255 and the resulting duty cycle is:Quoted 8 lines
(IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms.
If this is right, please use:
duty_scale = (state->duty_cycle * 256) / IQS620_PWM_PERIOD_NSSure thing, will do. I'll add a comment and round down. Your assumption is correct as well.
Quoted 11 lines
Also, when the hardware is running at
.enabled = 1, .duty_cycle = 1/256 ms, .period = 1msand you reconfigure to
.enabled = 0, .duty_cycle = 1ms, .period = 1msthe output might be active for > 1/256 ms if the process is preempted between writing
IQS620_PWM_DUTY_CYCLEandIQS620_PWR_SETTINGS_PWM_OUT.Quoted 9 lines
Quoted 4 lines
Good catch. I think we can solve this by writing
IQS620_PWM_DUTY_CYCLEfirst followed byIQS620_PWR_SETTINGS_PWM_OUTwhen the PWM is going to be enabled, and the reverse when the PWM is going to be disabled (i.e. turn OFF to prevent a stale duty cycle from being temporarily driven).Sounds like a plan. After disabling you even don't need to write the duty cycle register. (But there might be a discussion ahead that .get_state should return the duty cycle.)
For v3, I've opted to write
IQS620_PWM_DUTY_CYCLEregardless of whether the PWM is enabled as a matter of principle (i.e. faithfully pass the entire state down to the hardware without making assumptions).And since some consumers (e.g. leds-pwm, the primary use-case for this PWM) may pre-configure the duty cycle while the PWM is disabled, this method ensures the driver is already compliant in case
01ccf903edd6returns.Also, as mentioned above, I have dropped the automatic disabling of the PWM to simulate a 0% duty cycle if the requested duty cycle is < 1 / 256 ms since the device does not actively drive a zero with
IQS620_PWR_SETTINGS_PWM_OUT= 0. In the interest of transparency, here is what I currently have for v3:static int iqs620_pwm_apply(struct
pwm_chip*chip, structpwm_device*pwm,
const structpwm_state*state){ struct iqs620_pwm_private *iqs620_pwm; struct iqs62x_core *iqs62x; int duty_scale, ret; if (state->polarity != PWM_POLARITY_NORMAL) return -ENOTSUPP; if (state->period < IQS620_PWM_PERIOD_NS) return -EINVAL; iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); iqs62x = iqs620_pwm->iqs62x; if (!state->enabled) { ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS, IQS620_PWR_SETTINGS_PWM_OUT, 0); if (ret) return ret; } /* * The duty cycle generated by the device is calculated as follows:
Quoted 8 lines
* * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms * * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255 * (inclusive). Therefore the lowest duty cycle the device can generate * while the output is enabled is 1 / 256 ms. */ duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS - 1;
Hmm, this is violating the policy to implement a value not bigger than requested with state->duty_cycle == 0. I see this has downsides to not simply cheat here, but only claiming to have implemented 0% can hurt, too. pwm-rcar returns -EINVAL in this case.
Quoted 4 lines
ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE, clamp(duty_scale, 0, 0xFF)); if (ret) return ret;
I understand your motivation to configure the duty cycle also when the the request has enabled=false, but a strange side effect is that a failure to configure the dutycycle with .enabled=false isn't really a problem, is it?
(This is not a request to change anything, it's only expression of my frustration that we cannot get away without strange effects.)
Quoted 80 lines
if (state->enabled) ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS, IQS620_PWR_SETTINGS_PWM_OUT, 0xFF); return ret; }I believe this captures all of the discussion thus far; please let me know if you have any concerns.
Quoted 36 lines
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c new file +9
-0+static int iqs620_pwm_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct pwm_state state; +int ret; + +if (!(event_flags &BIT (IQS62X_EVENT_SYS_RESET ))) +return NOTIFY_DONE ;Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 109
Quoted 4 lines
drivers/pwm/pwm-iqs620a.c new file +4
-0+ + iqs620_pwm = container_of(notifier, struct iqs620_pwm_private, + notifier); +pwm_get_state (&iqs620_pwm->chip.pwms[0 ], &state);Please don't call pwm API functions in callbacks. I assume you rely on
pwm_get_statereturning the previously set state and thatiqs620_pwm_get_stateisn't called. Please usepwm->statefor that.Sure thing, will do. Your assumption is correct. If
pwm_get_statecalledchip->ops->get_stateinstead of returnpwm->stateas it does today, this function would break because it would restore the hardware using default register values (since this function follows a reset).Quoted 4 lines
Just for my own understanding, are you saying the PWM framework reserves the right to update
pwm_get_stateto callchip->ops->get_statesome time in the future? In any event I will refer topwm->stateas that is what I ultimately need here.This already was the case for a short time before v5.4. See
01ccf903edd6and40a6b9a00930. (And note that the lazyness mentioned above about not needing to writeduty_cyclewhen the PWM is off is what made the approach break however.) I don't know yet how to proceed here. Being able to get the actually implemented setting would be nice, probably it is prudent to do this with another API function.Quoted 5 lines
Other than that I consider it a layer violation to call a function that is designed for consumers in a lowlevel driver. I don't know if we need locking at some time, but if the core holded a lock when .apply is called, .apply calls
pwm_get_statewhich wanted to grab the lock again we get a dead-lock.I think we may be imagining two different hazards (please correct me if I have misunderstood). Originally I thought you were warning that
pwm_get_state(which simply returnspwm->state) may in the future callchip->ops->get_stateinstead, which here would have causediqs620_pwm_notifierto calliqs620_pwm_applywith a reset-like state rather than the last state requested by the user (since this notifier is called after the device resets itself).The short-lived change during the 5.4-rc phase, however, was to fill
pwm->stateinpwm_apply_statewith the quantized state from the hardware instead of the raw state requested by the user. Whetherpwm->state(accessed directly or by callingpwm_get_state) gives the raw state or the quantized output ofget_statefollowing the last call topwm_apply_statedoes not change the outcome (for this particular driver) becauseiqs620_pwm_applyultimately writes the same register values. Just to be safe, I've been testing with and without01ccf903edd6applied locally so as to validate the behavior in both scenarios.What I missed originally is that
pwm_get_stateis intended for consumers only, in which case I agree it is fundamentally wrong to cannibalize it in the driver. For v3 I have changediqs620_pwm_notifierto referencepwm->statedirectly since that is what is ultimately needed in the first place.You're correct that a lock within the core would cause a deadlock; in this case I was proposing a lock around the pair of reads/writes to
IQS620_PWM_DUTY_CYCLEandIQS620_PWR_SETTINGS_PWM_OUTsince v2 introduced an implied relationship between the two. That would be safe sincechip->ops->applyreturns beforepwm_apply_statecallschip->ops->get_state.
Quoted 4 lines
However, even that is no longer necessary since
IQS620_PWR_SETTINGS_PWM_OUTis now written independently ofIQS620_PWM_DUTY_CYCLEdue to withdrawing support for a 0% duty cycle starting in v3. In short, I didn't end up adding any locks as there was no need.
That's fine. I'm not sure we actually need locks in the framework in the long run because (other than clocks) a PWM always has at most a single consumer and up to now it seems reasonable that this single consumer doesn't race with itself.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hi Uwe,
I apologize for my delayed replies as I have been traveling.
Quoted 160 lines · Uwe Kleine-König
Hello Jeff,
Quoted 128 lines · Jeff LaBundy
Quoted 117 lines · Uwe Kleine-König
Quoted 103 lines · Jeff LaBundy
Quoted 71 lines · Uwe Kleine-König
Quoted 70 lines · Jeff LaBundy
This patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument ofiqs620_pwm_applyand removed all modifications to the variable's contents
- Updatediqs620_pwm_applyto return-ENOTSUPPor-EINVALif the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updatediqs620_pwm_applyto disable the PWM output if duty cycle is zero
- Addediqs620_pwm_get_state
- Eliminated tabbed alignment ofpwm_opsandplatform_driverstruct members
- Moved notifier unregistration to already presentiqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment iniqs620_pwm_probeto explain the order of operations
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.cdrivers/pwm/Kconfig +10
-0@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENICdrivers/pwm/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.odrivers/pwm/pwm-iqs620a.c new file +4
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620A PWM Generator +* Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 9
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+* Copyright (C) 2019 Jeff LaBundy <[email protected]> +* +* Limitations: +* - The period is not guaranteed to run to completion when the duty cycle is +* changed or the output is disabled. Do you know more details here? "not guaranteed" means that the new period starts immediately when
duty_cycleor the enabled bit is written?Increasing the duty cycle on-the-fly (e.g. 25% to 75%) results in the following behavior (depending on where the I2C write falls):
I2C write
__ __ __ V_ ______ ______ ______ __
__| |______| |______| |_|x|__| |__| |__| |__|
^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^The PWM continues to tick at 1 ms, but the currently running period suffers an extraneous pulse as the output is abruptly set high to "catch up" to the new duty cycle.
A similar behavior can occur if the duty cycle is decreased, meaning the output is abruptly set low if the I2C transaction completes in what has suddenly become the inactive region of the currently running period.
The PWM seems to be a simple counter that rolls over at a period of 1 ms. Both the counter and the
IQS620_PWM_DUTY_CYCLEregister effectively go to a comparator whose output is ANDed withIQS620_PWR_SETTINGS_PWM_OUTwhich then drives the PWM output.Quoted 8 lines
As such, if either
IQS620_PWM_DUTY_CYCLEorIQS620_PWR_SETTINGS_PWM_OUTchange, so may the PWM output state depending on the counter's value at the time the I2C write is completed within the 1-ms continuous loop.For v3 I will update the note as follows:
- Changes in duty cycle or enable/disable state are immediately reflected by the PWM output and are not aligned to the start of any period.
I'd like to see a bit more information in the driver. Something about the 1ms rhythm being unaffected by the
duty_cycleand enable setting. Maybe:Quoted 6 lines
- The periods run continuously with a fixed length of 1 ms which is unaffected by register updates. Writing duty cycle or enable registers gets active immediately which might result in glitches.
?
I adjusted the wording a bit as per my preference and settled on the following:
Quoted 7 lines
- The period is fixed to 1 ms and is generated continuously despite changes to the duty cycle or enable/disable state.
- Changes to the duty cycle or enable/disable state take effect immediately and may result in a glitch during the period in which the change is made.I believe these capture the spirit of your message; please let me know if you have any concerns.
That's fine.
Quoted 4 lines
Upon further experimentation, I found that disabling the output (which v2 does so as to simulate a 0% duty cycle) does not actively drive zero, but rather places the output in a high-impedance state with only the device's own internal leakage eventually discharging the pin.
But maybe this is still the best you can do in this case. @Thierry, what do you think?
Quoted 10 lines
This is fundamentally different than actively driving the pin low to make a 0% duty cycle, which does not appear to be possible at all. Therefore I have removed the control of
IQS620_PWR_SETTINGS_PWM_OUTbased on the duty cycle requested by the user and reverted to the behavior of v1, where the duty cycle requested by the user is mapped only toIQS620_PWM_DUTY_CYCLE.As such, I have also added a third bullet point similar to what you first suggested following v1:
- The device cannot generate a 0% duty cycle.
Quoted 8 lines
Then this would be:
- The device cannot actively drive a 0% duty cycle. The driver is disabled for small
duty_cyclesrelying on a pull down on the board.But maybe it would be more prudent to do this only if the board configuration suggests this is save?!
Given the policy for the actual duty cycle generated by the hardware not to exceed that which is requested by the user, it seems there are ultimately 3 options for duty_cycle below 1 / 256 ms:
1. Return -EINVAL.
2. Disable the output as in v2.
3. Add an optional boolean in the dts that identifies whether a pull-down is present; default to option (1) but use option (2) if the boolean is there.
I don't like option (1) because consumers (including leds-pwm) do in fact ask for a 0% duty cycle which would make iqs620_pwm_apply return an error. Things happen to still work since leds-pwm does not check pwm_config's return value, but I don't want to rely on this coincidence.
Option (2) is better, but I know from experience that board designers do not consult driver comments and the requirement to add a pull-down may be easily missed as it is not discussed in the data sheet (which is where that sort of information belongs, in my opinion).
Option (3) seems like overkill for such a simple PWM, and ultimately doesn't add any value because I don't want to allow option (1) behavior in any case. Whether the PWM is disabled because it is truly disabled or to simulate a 0% duty cycle as in option (2), the pull-down is ultimately required regardless of whether or not the data sheet happens to go into such detail.
Therefore I have opted to carry forward option (2) from v2 to v3. I reworded the third limitation a bit as follows:
- The device cannot generate a 0% duty cycle. For duty cycles below 1 / 256 ms, the output is disabled and relies upon an external pull-down resistor to hold the GPIO3/LTX pin low.
I did reach out to the vendor and asked them to consider recommending a pull- down resistor in a future revision of the data sheet, although at the time of this writing I have not heard back.
Quoted 143 lines
Quoted 134 lines
Quoted 86 lines
Quoted 86 lines
Quoted 62 lines
Quoted 47 lines
drivers/pwm/pwm-iqs620a.c new file +47
-0+* - The period is fixed to 1 ms. +*/ + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h> +#include <linux/slab.h> + +#define IQS620_PWR_SETTINGS 0xD2 +#define IQS620_PWR_SETTINGS_PWM_OUT BIT(7) + +#define IQS620_PWM_DUTY_CYCLE 0xD8 + +#define IQS620_PWM_PERIOD_NS 1000000 + +struct iqs620_pwm_private { +struct iqs62x_core *iqs62x; +struct pwm_chip chip; +struct notifier_block notifier; +}; + +static int iqs620_pwm_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct iqs62x_core *iqs62x; +unsigned int pwm_out =0 ; +int duty_scale, ret; + +if (state->polarity !=PWM_POLARITY_NORMAL ) +return -ENOTSUPP ; + +if (state->period <IQS620_PWM_PERIOD_NS ) +return -EINVAL ; + + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); + iqs62x = iqs620_pwm->iqs62x; + + duty_scale =DIV_ROUND_CLOSEST (state->duty_cycle *256 , +IQS620_PWM_PERIOD_NS ); + +if (duty_scale) { + ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE,drivers/pwm/pwm-iqs620a.c line 66
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10
-0+ min(duty_scale - 1, 0xFF)); +if (ret) +return ret; + +if (state->enabled) + pwm_out =IQS620_PWR_SETTINGS_PWM_OUT ; + } + +return regmap_update_bits (iqs62x->map,IQS620_PWR_SETTINGS , +IQS620_PWR_SETTINGS_PWM_OUT , pwm_out);A comment explaining the semantic here would be good. I assume
IQS620_PWM_DUTY_CYCLEtakes a value between 0 and 255 and the resulting duty cycle is:Quoted 8 lines
(IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms.
If this is right, please use:
duty_scale = (state->duty_cycle * 256) / IQS620_PWM_PERIOD_NSSure thing, will do. I'll add a comment and round down. Your assumption is correct as well.
Quoted 11 lines
Also, when the hardware is running at
.enabled = 1, .duty_cycle = 1/256 ms, .period = 1msand you reconfigure to
.enabled = 0, .duty_cycle = 1ms, .period = 1msthe output might be active for > 1/256 ms if the process is preempted between writing
IQS620_PWM_DUTY_CYCLEandIQS620_PWR_SETTINGS_PWM_OUT.Quoted 9 lines
Quoted 4 lines
Good catch. I think we can solve this by writing
IQS620_PWM_DUTY_CYCLEfirst followed byIQS620_PWR_SETTINGS_PWM_OUTwhen the PWM is going to be enabled, and the reverse when the PWM is going to be disabled (i.e. turn OFF to prevent a stale duty cycle from being temporarily driven).Sounds like a plan. After disabling you even don't need to write the duty cycle register. (But there might be a discussion ahead that .get_state should return the duty cycle.)
For v3, I've opted to write
IQS620_PWM_DUTY_CYCLEregardless of whether the PWM is enabled as a matter of principle (i.e. faithfully pass the entire state down to the hardware without making assumptions).And since some consumers (e.g. leds-pwm, the primary use-case for this PWM) may pre-configure the duty cycle while the PWM is disabled, this method ensures the driver is already compliant in case
01ccf903edd6returns.Also, as mentioned above, I have dropped the automatic disabling of the PWM to simulate a 0% duty cycle if the requested duty cycle is < 1 / 256 ms since the device does not actively drive a zero with
IQS620_PWR_SETTINGS_PWM_OUT= 0. In the interest of transparency, here is what I currently have for v3:static int iqs620_pwm_apply(struct
pwm_chip*chip, structpwm_device*pwm,
const structpwm_state*state){ struct iqs620_pwm_private *iqs620_pwm; struct iqs62x_core *iqs62x; int duty_scale, ret; if (state->polarity != PWM_POLARITY_NORMAL) return -ENOTSUPP; if (state->period < IQS620_PWM_PERIOD_NS) return -EINVAL; iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); iqs62x = iqs620_pwm->iqs62x; if (!state->enabled) { ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS, IQS620_PWR_SETTINGS_PWM_OUT, 0); if (ret) return ret; } /* * The duty cycle generated by the device is calculated as follows:Quoted 8 lines
* * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms * * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255 * (inclusive). Therefore the lowest duty cycle the device can generate * while the output is enabled is 1 / 256 ms. */ duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS - 1;
Quoted 5 lines
Hmm, this is violating the policy to implement a value not bigger than requested with
state->duty_cycle== 0. I see this has downsides to not simply cheat here, but only claiming to have implemented 0% can hurt, too. pwm-rcar returns-EINVALin this case.
That's a great point and is addressed by sticking with option (2) described above. Here is what I've got for v3:
static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
struct iqs620_pwm_private *iqs620_pwm;
struct iqs62x_core *iqs62x;
int duty_scale, ret;
if (state->polarity != PWM_POLARITY_NORMAL)
return -ENOTSUPP;
if (state->period < IQS620_PWM_PERIOD_NS)
return -EINVAL;
iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
iqs62x = iqs620_pwm->iqs62x;
mutex_lock(&iqs620_pwm->lock);
/*
* The duty cycle generated by the device is calculated as follows:
*
* duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms
*
* ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255
* (inclusive). Therefore the lowest duty cycle the device can generate
* while the output is enabled is 1 / 256 ms.
*
* For lower duty cycles (e.g. 0), the PWM output is simply disabled to
* allow an on-board pull-down resistor to hold the GPIO3/LTX pin low.
*/
duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS;
if (!state->enabled || !duty_scale) {
ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS,
IQS620_PWR_SETTINGS_PWM_OUT, 0);
if (ret)
goto err_mutex;
}
if (duty_scale) {
ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE,
min(duty_scale - 1, 0xFF));
if (ret)
goto err_mutex;
}
if (state->enabled && duty_scale)
ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS,
IQS620_PWR_SETTINGS_PWM_OUT, 0xFF);err_mutex:
mutex_unlock(&iqs620_pwm->lock); return ret; }
And for the get_state callback:
static void iqs620_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
struct pwm_state *state)
{
struct iqs620_pwm_private *iqs620_pwm;
struct iqs62x_core *iqs62x;
unsigned int val;
int ret;
iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
iqs62x = iqs620_pwm->iqs62x;
mutex_lock(&iqs620_pwm->lock);
ret = regmap_read(iqs62x->map, IQS620_PWR_SETTINGS, &val);
if (ret)
goto err_mutex;
state->enabled = val & IQS620_PWR_SETTINGS_PWM_OUT;
ret = regmap_read(iqs62x->map, IQS620_PWM_DUTY_CYCLE, &val);
if (ret)
goto err_mutex;
state->duty_cycle = DIV_ROUND_UP((val + 1) * IQS620_PWM_PERIOD_NS, 256);
state->period = IQS620_PWM_PERIOD_NS;err_mutex:
mutex_unlock(&iqs620_pwm->lock); if (ret) dev_err(iqs620_pwm->chip.dev, "Failed to get state: %d\n", ret); }
If you and/or Thierry have any concerns, please let me know.
Quoted 5 lines
Quoted 4 lines
ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE, clamp(duty_scale, 0, 0xFF)); if (ret) return ret;
Quoted 7 lines
I understand your motivation to configure the duty cycle also when the the request has enabled=false, but a strange side effect is that a failure to configure the dutycycle with .enabled=false isn't really a problem, is it?
(This is not a request to change anything, it's only expression of my frustration that we cannot get away without strange effects.)
True, but it would definitely be a problem in case 01ccf903edd6 returns and we're relying on the device's own registers to hold the PWM state.
Thinking about this more, I agree with your earlier comment that a means to get the actual (quantized) state needs to be a new API function (of integer type). Since chip->ops->get_state is void, there is no way for the callback to warn the core that a register read failed and the PWM state may be junk.
Quoted 85 lines
Quoted 80 lines
if (state->enabled) ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS, IQS620_PWR_SETTINGS_PWM_OUT, 0xFF); return ret; }I believe this captures all of the discussion thus far; please let me know if you have any concerns.
Quoted 36 lines
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
drivers/pwm/pwm-iqs620a.c new file +9
-0+static int iqs620_pwm_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{ +struct iqs620_pwm_private *iqs620_pwm; +struct pwm_state state; +int ret; + +if (!(event_flags &BIT (IQS62X_EVENT_SYS_RESET ))) +return NOTIFY_DONE ;Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 109
Quoted 4 lines
drivers/pwm/pwm-iqs620a.c new file +4
-0+ + iqs620_pwm = container_of(notifier, struct iqs620_pwm_private, + notifier); +pwm_get_state (&iqs620_pwm->chip.pwms[0 ], &state);Please don't call pwm API functions in callbacks. I assume you rely on
pwm_get_statereturning the previously set state and thatiqs620_pwm_get_stateisn't called. Please usepwm->statefor that.Sure thing, will do. Your assumption is correct. If
pwm_get_statecalledchip->ops->get_stateinstead of returnpwm->stateas it does today, this function would break because it would restore the hardware using default register values (since this function follows a reset).Quoted 4 lines
Just for my own understanding, are you saying the PWM framework reserves the right to update
pwm_get_stateto callchip->ops->get_statesome time in the future? In any event I will refer topwm->stateas that is what I ultimately need here.This already was the case for a short time before v5.4. See
01ccf903edd6and40a6b9a00930. (And note that the lazyness mentioned above about not needing to writeduty_cyclewhen the PWM is off is what made the approach break however.) I don't know yet how to proceed here. Being able to get the actually implemented setting would be nice, probably it is prudent to do this with another API function.Quoted 5 lines
Other than that I consider it a layer violation to call a function that is designed for consumers in a lowlevel driver. I don't know if we need locking at some time, but if the core holded a lock when .apply is called, .apply calls
pwm_get_statewhich wanted to grab the lock again we get a dead-lock.I think we may be imagining two different hazards (please correct me if I have misunderstood). Originally I thought you were warning that
pwm_get_state(which simply returnspwm->state) may in the future callchip->ops->get_stateinstead, which here would have causediqs620_pwm_notifierto calliqs620_pwm_applywith a reset-like state rather than the last state requested by the user (since this notifier is called after the device resets itself).The short-lived change during the 5.4-rc phase, however, was to fill
pwm->stateinpwm_apply_statewith the quantized state from the hardware instead of the raw state requested by the user. Whetherpwm->state(accessed directly or by callingpwm_get_state) gives the raw state or the quantized output ofget_statefollowing the last call topwm_apply_statedoes not change the outcome (for this particular driver) becauseiqs620_pwm_applyultimately writes the same register values. Just to be safe, I've been testing with and without01ccf903edd6applied locally so as to validate the behavior in both scenarios.What I missed originally is that
pwm_get_stateis intended for consumers only, in which case I agree it is fundamentally wrong to cannibalize it in the driver. For v3 I have changediqs620_pwm_notifierto referencepwm->statedirectly since that is what is ultimately needed in the first place.You're correct that a lock within the core would cause a deadlock; in this case I was proposing a lock around the pair of reads/writes to
IQS620_PWM_DUTY_CYCLEandIQS620_PWR_SETTINGS_PWM_OUTsince v2 introduced an implied relationship between the two. That would be safe sincechip->ops->applyreturns beforepwm_apply_statecallschip->ops->get_state.Quoted 4 lines
However, even that is no longer necessary since
IQS620_PWR_SETTINGS_PWM_OUTis now written independently ofIQS620_PWM_DUTY_CYCLEdue to withdrawing support for a 0% duty cycle starting in v3. In short, I didn't end up adding any locks as there was no need.
Quoted 5 lines
That's fine. I'm not sure we actually need locks in the framework in the long run because (other than clocks) a PWM always has at most a single consumer and up to now it seems reasonable that this single consumer doesn't race with itself.
Agreed. You will see that I have added my own lock in the apply/get_state functions now that I've restored v2's behavior, but that is to account for this device's case where its interrupt handler is in the process of re-initializing the couple of PWM- related registers when get_state is called.
Quoted 6 lines
Best regards
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Kind regards,
Jeff LaBundy
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hello Jeff,
Quoted 1 line · Jeff LaBundy
I apologize for my delayed replies as I have been traveling.
No problem, I didn't hold my breath :-)
Quoted 177 lines · Uwe Kleine-König
Quoted 158 lines · Jeff LaBundy
Quoted 128 lines · Uwe Kleine-König
Quoted 117 lines · Jeff LaBundy
Quoted 103 lines · Uwe Kleine-König
Quoted 71 lines · Jeff LaBundy
Quoted 70 lines
This patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument ofiqs620_pwm_applyand removed all modifications to the variable's contents
- Updatediqs620_pwm_applyto return-ENOTSUPPor-EINVALif the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updatediqs620_pwm_applyto disable the PWM output if duty cycle is zero
- Addediqs620_pwm_get_state
- Eliminated tabbed alignment ofpwm_opsandplatform_driverstruct members
- Moved notifier unregistration to already presentiqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment iniqs620_pwm_probeto explain the order of operations
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.cdrivers/pwm/Kconfig +10
-0@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENICdrivers/pwm/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.odrivers/pwm/pwm-iqs620a.c new file +4
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620A PWM Generator +* Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 9
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+* Copyright (C) 2019 Jeff LaBundy <[email protected]> +* +* Limitations: +* - The period is not guaranteed to run to completion when the duty cycle is +* changed or the output is disabled. Do you know more details here? "not guaranteed" means that the new period starts immediately when
duty_cycleor the enabled bit is written?Increasing the duty cycle on-the-fly (e.g. 25% to 75%) results in the following behavior (depending on where the I2C write falls):
I2C write
__ __ __ V_ ______ ______ ______ __
__| |______| |______| |_|x|__| |__| |__| |__|
^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^The PWM continues to tick at 1 ms, but the currently running period suffers an extraneous pulse as the output is abruptly set high to "catch up" to the new duty cycle.
A similar behavior can occur if the duty cycle is decreased, meaning the output is abruptly set low if the I2C transaction completes in what has suddenly become the inactive region of the currently running period.
The PWM seems to be a simple counter that rolls over at a period of 1 ms. Both the counter and the
IQS620_PWM_DUTY_CYCLEregister effectively go to a comparator whose output is ANDed withIQS620_PWR_SETTINGS_PWM_OUTwhich then drives the PWM output.Quoted 8 lines
As such, if either
IQS620_PWM_DUTY_CYCLEorIQS620_PWR_SETTINGS_PWM_OUTchange, so may the PWM output state depending on the counter's value at the time the I2C write is completed within the 1-ms continuous loop.For v3 I will update the note as follows:
- Changes in duty cycle or enable/disable state are immediately reflected by the PWM output and are not aligned to the start of any period.
I'd like to see a bit more information in the driver. Something about the 1ms rhythm being unaffected by the
duty_cycleand enable setting. Maybe:Quoted 6 lines
- The periods run continuously with a fixed length of 1 ms which is unaffected by register updates. Writing duty cycle or enable registers gets active immediately which might result in glitches.
?
I adjusted the wording a bit as per my preference and settled on the following:
Quoted 7 lines
- The period is fixed to 1 ms and is generated continuously despite changes to the duty cycle or enable/disable state.
- Changes to the duty cycle or enable/disable state take effect immediately and may result in a glitch during the period in which the change is made.I believe these capture the spirit of your message; please let me know if you have any concerns.
That's fine.
Quoted 4 lines
Upon further experimentation, I found that disabling the output (which v2 does so as to simulate a 0% duty cycle) does not actively drive zero, but rather places the output in a high-impedance state with only the device's own internal leakage eventually discharging the pin.
But maybe this is still the best you can do in this case. @Thierry, what do you think?
Quoted 10 lines
This is fundamentally different than actively driving the pin low to make a 0% duty cycle, which does not appear to be possible at all. Therefore I have removed the control of
IQS620_PWR_SETTINGS_PWM_OUTbased on the duty cycle requested by the user and reverted to the behavior of v1, where the duty cycle requested by the user is mapped only toIQS620_PWM_DUTY_CYCLE.As such, I have also added a third bullet point similar to what you first suggested following v1:
- The device cannot generate a 0% duty cycle.
Quoted 8 lines
Then this would be:
- The device cannot actively drive a 0% duty cycle. The driver is disabled for small
duty_cyclesrelying on a pull down on the board.But maybe it would be more prudent to do this only if the board configuration suggests this is save?!
Given the policy for the actual duty cycle generated by the hardware not to exceed that which is requested by the user, it seems there are ultimately 3 options for
duty_cyclebelow 1 / 256 ms:1. Return
-EINVAL.
2. Disable the output as in v2.
3. Add an optional boolean in the dts that identifies whether a pull-down is present; default to option (1) but use option (2) if the boolean is there.
Quoted 4 lines
I don't like option (1) because consumers (including leds-pwm) do in fact ask for a 0% duty cycle which would make
iqs620_pwm_applyreturn an error. Things happen to still work since leds-pwm does not check pwm_config's return value, but I don't want to rely on this coincidence.
People implementing PWM drivers seems to mostly care about leds-pwm :-) With that only nearly hitting the requested state isn't that bad. But if you control a step motor that positions a laser, you want to be sure that the request to stop moving it actually worked.
Quoted 4 lines
Option (2) is better, but I know from experience that board designers do not consult driver comments and the requirement to add a pull-down may be easily missed as it is not discussed in the data sheet (which is where that sort of information belongs, in my opinion).
Hmm, well, actually I think the problem happened earlier when the hardware designer considered providing 0% to be not important.
Quoted 5 lines
Option (3) seems like overkill for such a simple PWM, and ultimately doesn't add any value because I don't want to allow option (1) behavior in any case. Whether the PWM is disabled because it is truly disabled or to simulate a 0% duty cycle as in option (2), the pull-down is ultimately required regardless of whether or not the data sheet happens to go into such detail.
Actually I like option 3 best.
Quoted 10 lines
Therefore I have opted to carry forward option (2) from v2 to v3. I reworded the third limitation a bit as follows:
- The device cannot generate a 0% duty cycle. For duty cycles below 1 / 256 ms, the output is disabled and relies upon an external pull-down resistor to hold the GPIO3/LTX pin low.
I did reach out to the vendor and asked them to consider recommending a pull- down resistor in a future revision of the data sheet, although at the time of this writing I have not heard back.
Good.
Quoted 70 lines
Quoted 11 lines
Quoted 10 lines
/* * The duty cycle generated by the device is calculated as follows: * * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms * * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255 * (inclusive). Therefore the lowest duty cycle the device can generate * while the output is enabled is 1 / 256 ms. */ duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS - 1;Quoted 5 lines
Hmm, this is violating the policy to implement a value not bigger than requested with
state->duty_cycle== 0. I see this has downsides to not simply cheat here, but only claiming to have implemented 0% can hurt, too. pwm-rcar returns-EINVALin this case.That's a great point and is addressed by sticking with option (2) described above. Here is what I've got for v3:
static int iqs620_pwm_apply(struct
pwm_chip*chip, structpwm_device*pwm,
const structpwm_state*state){ struct iqs620_pwm_private *iqs620_pwm; struct iqs62x_core *iqs62x; int duty_scale, ret; if (state->polarity != PWM_POLARITY_NORMAL) return -ENOTSUPP; if (state->period < IQS620_PWM_PERIOD_NS) return -EINVAL; iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); iqs62x = iqs620_pwm->iqs62x; mutex_lock(&iqs620_pwm->lock); /* * The duty cycle generated by the device is calculated as follows: * * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms * * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255 * (inclusive). Therefore the lowest duty cycle the device can generate * while the output is enabled is 1 / 256 ms. * * For lower duty cycles (e.g. 0), the PWM output is simply disabled to * allow an on-board pull-down resistor to hold the GPIO3/LTX pin low. */ duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS; if (!state->enabled || !duty_scale) { ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS, IQS620_PWR_SETTINGS_PWM_OUT, 0); if (ret) goto err_mutex; } if (duty_scale) { ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE, min(duty_scale - 1, 0xFF)); if (ret) goto err_mutex; } if (state->enabled && duty_scale) ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS, IQS620_PWR_SETTINGS_PWM_OUT, 0xFF);
Quoted 5 lines
err_mutex:mutex_unlock(&iqs620_pwm->lock); return ret; }
Looks ok. (Even though it implements (2) which isn't my favorite :-)
Quoted 26 lines
And for the
get_statecallback:static void iqs620_pwm_get_state(struct
pwm_chip*chip, structpwm_device*pwm,
structpwm_state*state){ struct iqs620_pwm_private *iqs620_pwm; struct iqs62x_core *iqs62x; unsigned int val; int ret; iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); iqs62x = iqs620_pwm->iqs62x; mutex_lock(&iqs620_pwm->lock); ret = regmap_read(iqs62x->map, IQS620_PWR_SETTINGS, &val); if (ret) goto err_mutex; state->enabled = val & IQS620_PWR_SETTINGS_PWM_OUT; ret = regmap_read(iqs62x->map, IQS620_PWM_DUTY_CYCLE, &val); if (ret) goto err_mutex; state->duty_cycle = DIV_ROUND_UP((val + 1) * IQS620_PWM_PERIOD_NS, 256); state->period = IQS620_PWM_PERIOD_NS;
Quoted 8 lines
err_mutex:mutex_unlock(&iqs620_pwm->lock); if (ret) dev_err(iqs620_pwm->chip.dev, "Failed to get state: %d\n", ret); }If you and/or Thierry have any concerns, please let me know.
Looks good, too. Maybe add a comment like:
/*
* As the hardware cannot implement "enabled with
* duty_cycle == 0", we're reporting "disabled with
* duty_cycle = 1/256 ms" after 0% was requested. This is ugly
* but the best we can achieve.
*/
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines
ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE, clamp(duty_scale, 0, 0xFF)); if (ret) return ret;Quoted 7 lines
I understand your motivation to configure the duty cycle also when the the request has enabled=false, but a strange side effect is that a failure to configure the dutycycle with .enabled=false isn't really a problem, is it?
(This is not a request to change anything, it's only expression of my frustration that we cannot get away without strange effects.)
Quoted 2 lines
True, but it would definitely be a problem in case
01ccf903edd6returns and we're relying on the device's own registers to hold the PWM state.
You can assume it won't come back as is. There are too many drivers that suffer the same problem. My goal is to let the core not depend on the lowlevel drivers to memorize a duty-cycle for disabled PWMs. The details are not yet thought out. Obvious options are:
- cache the value in the core
- make consumers not depend on that
Quoted 4 lines
Thinking about this more, I agree with your earlier comment that a means to get the actual (quantized) state needs to be a new API function (of integer type). Since
chip->ops->get_stateis void, there is no way for the callback to warn the core that a register read failed and the PWM state may be junk.
Yeah, this is something I intend to change, too. .get_state should return a status code in the long run.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hi Uwe,
Quoted 194 lines · Uwe Kleine-König
Hello Jeff,
Quoted 1 line · Jeff LaBundy
I apologize for my delayed replies as I have been traveling.
No problem, I didn't hold my breath :-)
Quoted 177 lines · Uwe Kleine-König
Quoted 158 lines · Jeff LaBundy
Quoted 128 lines · Uwe Kleine-König
Quoted 117 lines · Jeff LaBundy
Quoted 103 lines · Uwe Kleine-König
Quoted 71 lines · Jeff LaBundy
Quoted 70 lines
This patch adds support for the Azoteq IQS620A, capable of generating a 1-kHz PWM output with duty cycle between 0.4% and 100% (inclusive).
Signed-off-by Jeff LaBundy <[email protected]>---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Added 'Limitations' section to introductory comments
- Replaced 'error' with 'ret' throughout
- Added const qualifier to state argument ofiqs620_pwm_applyand removed all modifications to the variable's contents
- Updatediqs620_pwm_applyto return-ENOTSUPPor-EINVALif the requested polarity is inverted or the requested period is below 1 ms, respectively
- Updatediqs620_pwm_applyto disable the PWM output if duty cycle is zero
- Addediqs620_pwm_get_state
- Eliminated tabbed alignment ofpwm_opsandplatform_driverstruct members
- Moved notifier unregistration to already presentiqs620_pwm_remove, which eliminated the need for a device-managed action and ready flag
- Added a comment iniqs620_pwm_probeto explain the order of operations
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/pwm/Kconfig | 10 +++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-iqs620a.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 drivers/pwm/pwm-iqs620a.cdrivers/pwm/Kconfig +10
-0@@ -222,6 +222,16 @@ config PWM_IMX_TPM To compile this driver as a module, choose M here: the module will be called pwm-imx-tpm. +config PWM_IQS620A + tristate "Azoteq IQS620A PWM support" + depends on MFD_IQS62X || COMPILE_TEST + help + Generic PWM framework driver for the Azoteq IQS620A multi-function + sensor. + + To compile this driver as a module, choose M here: the module will + be called pwm-iqs620a. + config PWM_JZ4740 tristate "Ingenic JZ47xx PWM support" depends on MACH_INGENICdrivers/pwm/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_IMG) += pwm-img.o obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o +obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.odrivers/pwm/pwm-iqs620a.c new file +4
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620A PWM Generator +* Quoted 9 lines
drivers/pwm/pwm-iqs620a.c line 9
Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5
-0+* Copyright (C) 2019 Jeff LaBundy <[email protected]> +* +* Limitations: +* - The period is not guaranteed to run to completion when the duty cycle is +* changed or the output is disabled. Do you know more details here? "not guaranteed" means that the new period starts immediately when
duty_cycleor the enabled bit is written?Increasing the duty cycle on-the-fly (e.g. 25% to 75%) results in the following behavior (depending on where the I2C write falls):
I2C write
__ __ __ V_ ______ ______ ______ __
__| |______| |______| |_|x|__| |__| |__| |__|
^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^---1ms---^The PWM continues to tick at 1 ms, but the currently running period suffers an extraneous pulse as the output is abruptly set high to "catch up" to the new duty cycle.
A similar behavior can occur if the duty cycle is decreased, meaning the output is abruptly set low if the I2C transaction completes in what has suddenly become the inactive region of the currently running period.
The PWM seems to be a simple counter that rolls over at a period of 1 ms. Both the counter and the
IQS620_PWM_DUTY_CYCLEregister effectively go to a comparator whose output is ANDed withIQS620_PWR_SETTINGS_PWM_OUTwhich then drives the PWM output.Quoted 8 lines
As such, if either
IQS620_PWM_DUTY_CYCLEorIQS620_PWR_SETTINGS_PWM_OUTchange, so may the PWM output state depending on the counter's value at the time the I2C write is completed within the 1-ms continuous loop.For v3 I will update the note as follows:
- Changes in duty cycle or enable/disable state are immediately reflected by the PWM output and are not aligned to the start of any period.
I'd like to see a bit more information in the driver. Something about the 1ms rhythm being unaffected by the
duty_cycleand enable setting. Maybe:Quoted 6 lines
- The periods run continuously with a fixed length of 1 ms which is unaffected by register updates. Writing duty cycle or enable registers gets active immediately which might result in glitches.
?
I adjusted the wording a bit as per my preference and settled on the following:
Quoted 7 lines
- The period is fixed to 1 ms and is generated continuously despite changes to the duty cycle or enable/disable state.
- Changes to the duty cycle or enable/disable state take effect immediately and may result in a glitch during the period in which the change is made.I believe these capture the spirit of your message; please let me know if you have any concerns.
That's fine.
Quoted 4 lines
Upon further experimentation, I found that disabling the output (which v2 does so as to simulate a 0% duty cycle) does not actively drive zero, but rather places the output in a high-impedance state with only the device's own internal leakage eventually discharging the pin.
But maybe this is still the best you can do in this case. @Thierry, what do you think?
Quoted 10 lines
This is fundamentally different than actively driving the pin low to make a 0% duty cycle, which does not appear to be possible at all. Therefore I have removed the control of
IQS620_PWR_SETTINGS_PWM_OUTbased on the duty cycle requested by the user and reverted to the behavior of v1, where the duty cycle requested by the user is mapped only toIQS620_PWM_DUTY_CYCLE.As such, I have also added a third bullet point similar to what you first suggested following v1:
- The device cannot generate a 0% duty cycle.
Quoted 8 lines
Then this would be:
- The device cannot actively drive a 0% duty cycle. The driver is disabled for small
duty_cyclesrelying on a pull down on the board.But maybe it would be more prudent to do this only if the board configuration suggests this is save?!
Given the policy for the actual duty cycle generated by the hardware not to exceed that which is requested by the user, it seems there are ultimately 3 options for
duty_cyclebelow 1 / 256 ms:1. Return
-EINVAL.
2. Disable the output as in v2.
3. Add an optional boolean in the dts that identifies whether a pull-down is present; default to option (1) but use option (2) if the boolean is there.Quoted 4 lines
I don't like option (1) because consumers (including leds-pwm) do in fact ask for a 0% duty cycle which would make
iqs620_pwm_applyreturn an error. Things happen to still work since leds-pwm does not check pwm_config's return value, but I don't want to rely on this coincidence.People implementing PWM drivers seems to mostly care about leds-pwm :-) With that only nearly hitting the requested state isn't that bad. But if you control a step motor that positions a laser, you want to be sure that the request to stop moving it actually worked.
Quoted 8 lines
Quoted 4 lines
Option (2) is better, but I know from experience that board designers do not consult driver comments and the requirement to add a pull-down may be easily missed as it is not discussed in the data sheet (which is where that sort of information belongs, in my opinion).
Hmm, well, actually I think the problem happened earlier when the hardware designer considered providing 0% to be not important.
I heard back from the vendor today; they've acknowledged the limitation and are considering adding support for 0% in a future ROM spin. In the meantime, they've agreed to describe the high-impedance behavior in the data sheet as well as include the pull-down resistor in an example schematic.
Quoted 8 lines
Quoted 5 lines
Option (3) seems like overkill for such a simple PWM, and ultimately doesn't add any value because I don't want to allow option (1) behavior in any case. Whether the PWM is disabled because it is truly disabled or to simulate a 0% duty cycle as in option (2), the pull-down is ultimately required regardless of whether or not the data sheet happens to go into such detail.
Actually I like option 3 best.
Based on your other feedback, I'm moving forward under the impression that you'll still accept option (2); please let me know if I have misunderstood (thank you for being flexible).
My argument is that even if duty cycle is limited to 1 / 256 ms within the "no pull-down present" option, the output will still be disabled anyway if state->enabled = false. In that case, the pull-down is required to prevent noise from coupling onto the high-impedance pin (which will likely be tied to the high-impedance gate of a MOSFET) and flickering an LED.
Stated another way, I do not feel option (3) is suitable because the pull- down is in fact not optional, but required in my opinion.
Quoted 126 lines
Quoted 10 lines
Therefore I have opted to carry forward option (2) from v2 to v3. I reworded the third limitation a bit as follows:
- The device cannot generate a 0% duty cycle. For duty cycles below 1 / 256 ms, the output is disabled and relies upon an external pull-down resistor to hold the GPIO3/LTX pin low.
I did reach out to the vendor and asked them to consider recommending a pull- down resistor in a future revision of the data sheet, although at the time of this writing I have not heard back.
Good.
Quoted 70 lines
Quoted 11 lines
Quoted 10 lines
/* * The duty cycle generated by the device is calculated as follows: * * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms * * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255 * (inclusive). Therefore the lowest duty cycle the device can generate * while the output is enabled is 1 / 256 ms. */ duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS - 1;Quoted 5 lines
Hmm, this is violating the policy to implement a value not bigger than requested with
state->duty_cycle== 0. I see this has downsides to not simply cheat here, but only claiming to have implemented 0% can hurt, too. pwm-rcar returns-EINVALin this case.That's a great point and is addressed by sticking with option (2) described above. Here is what I've got for v3:
static int iqs620_pwm_apply(struct
pwm_chip*chip, structpwm_device*pwm,
const structpwm_state*state){ struct iqs620_pwm_private *iqs620_pwm; struct iqs62x_core *iqs62x; int duty_scale, ret; if (state->polarity != PWM_POLARITY_NORMAL) return -ENOTSUPP; if (state->period < IQS620_PWM_PERIOD_NS) return -EINVAL; iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); iqs62x = iqs620_pwm->iqs62x; mutex_lock(&iqs620_pwm->lock); /* * The duty cycle generated by the device is calculated as follows: * * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms * * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255 * (inclusive). Therefore the lowest duty cycle the device can generate * while the output is enabled is 1 / 256 ms. * * For lower duty cycles (e.g. 0), the PWM output is simply disabled to * allow an on-board pull-down resistor to hold the GPIO3/LTX pin low. */ duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS; if (!state->enabled || !duty_scale) { ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS, IQS620_PWR_SETTINGS_PWM_OUT, 0); if (ret) goto err_mutex; } if (duty_scale) { ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE, min(duty_scale - 1, 0xFF)); if (ret) goto err_mutex; } if (state->enabled && duty_scale) ret = regmap_update_bits(iqs62x->map, IQS620_PWR_SETTINGS, IQS620_PWR_SETTINGS_PWM_OUT, 0xFF);Quoted 5 lines
err_mutex:mutex_unlock(&iqs620_pwm->lock); return ret; }Looks ok. (Even though it implements (2) which isn't my favorite :-)
Quoted 26 lines
And for the
get_statecallback:static void iqs620_pwm_get_state(struct
pwm_chip*chip, structpwm_device*pwm,
structpwm_state*state){ struct iqs620_pwm_private *iqs620_pwm; struct iqs62x_core *iqs62x; unsigned int val; int ret; iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); iqs62x = iqs620_pwm->iqs62x; mutex_lock(&iqs620_pwm->lock); ret = regmap_read(iqs62x->map, IQS620_PWR_SETTINGS, &val); if (ret) goto err_mutex; state->enabled = val & IQS620_PWR_SETTINGS_PWM_OUT; ret = regmap_read(iqs62x->map, IQS620_PWM_DUTY_CYCLE, &val); if (ret) goto err_mutex; state->duty_cycle = DIV_ROUND_UP((val + 1) * IQS620_PWM_PERIOD_NS, 256); state->period = IQS620_PWM_PERIOD_NS;Quoted 8 lines
err_mutex:mutex_unlock(&iqs620_pwm->lock); if (ret) dev_err(iqs620_pwm->chip.dev, "Failed to get state: %d\n", ret); }If you and/or Thierry have any concerns, please let me know.
Quoted 9 lines
Looks good, too. Maybe add a comment like:
/*
* As the hardware cannot implement "enabled with
*duty_cycle== 0", we're reporting "disabled with
*duty_cycle= 1/256 ms" after 0% was requested. This is ugly
* but the best we can achieve.
*/
Sure thing, will do.
Quoted 24 lines
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines
ret = regmap_write(iqs62x->map, IQS620_PWM_DUTY_CYCLE, clamp(duty_scale, 0, 0xFF)); if (ret) return ret;Quoted 7 lines
I understand your motivation to configure the duty cycle also when the the request has enabled=false, but a strange side effect is that a failure to configure the dutycycle with .enabled=false isn't really a problem, is it?
(This is not a request to change anything, it's only expression of my frustration that we cannot get away without strange effects.)Quoted 2 lines
True, but it would definitely be a problem in case
01ccf903edd6returns and we're relying on the device's own registers to hold the PWM state.You can assume it won't come back as is. There are too many drivers that suffer the same problem. My goal is to let the core not depend on the lowlevel drivers to memorize a duty-cycle for disabled PWMs. The details are not yet thought out. Obvious options are:
- cache the value in the core
- make consumers not depend on that
Quoted 8 lines
Quoted 4 lines
Thinking about this more, I agree with your earlier comment that a means to get the actual (quantized) state needs to be a new API function (of integer type). Since
chip->ops->get_stateis void, there is no way for the callback to warn the core that a register read failed and the PWM state may be junk.Yeah, this is something I intend to change, too. .get_state should return a status code in the long run.
Makes sense.
Quoted 6 lines
Best regards
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
I'll add a comment in get_state and send out a v3 after the holidays.
Kind regards,
Jeff LaBundy
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hello Jeff,
Quoted 4 lines · Jeff LaBundy
I heard back from the vendor today; they've acknowledged the limitation and are considering adding support for 0% in a future ROM spin. In the meantime, they've agreed to describe the high-impedance behavior in the data sheet as well as include the pull-down resistor in an example schematic.
Oh wow, seems like a good vendor then. :-)
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Option (3) seems like overkill for such a simple PWM, and ultimately doesn't add any value because I don't want to allow option (1) behavior in any case. Whether the PWM is disabled because it is truly disabled or to simulate a 0% duty cycle as in option (2), the pull-down is ultimately required regardless of whether or not the data sheet happens to go into such detail.
Quoted 6 lines
Quoted 2 lines
Actually I like option 3 best.
Based on your other feedback, I'm moving forward under the impression that you'll still accept option (2); please let me know if I have misunderstood (thank you for being flexible).
Yeah, that's fine. If in the end it shows that this is a bad idea we can still change to (3).
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hi Uwe,
Quoted 19 lines · Uwe Kleine-König
Hello Jeff,
Quoted 4 lines · Jeff LaBundy
I heard back from the vendor today; they've acknowledged the limitation and are considering adding support for 0% in a future ROM spin. In the meantime, they've agreed to describe the high-impedance behavior in the data sheet as well as include the pull-down resistor in an example schematic.
Oh wow, seems like a good vendor then. :-)
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Option (3) seems like overkill for such a simple PWM, and ultimately doesn't add any value because I don't want to allow option (1) behavior in any case. Whether the PWM is disabled because it is truly disabled or to simulate a 0% duty cycle as in option (2), the pull-down is ultimately required regardless of whether or not the data sheet happens to go into such detail.
Actually I like option 3 best.
Quoted 7 lines
Quoted 3 lines
Based on your other feedback, I'm moving forward under the impression that you'll still accept option (2); please let me know if I have misunderstood (thank you for being flexible).
Yeah, that's fine. If in the end it shows that this is a bad idea we can still change to (3).
Sounds great. As soon as 5.5-rc5 lands this weekend, I'll rebase v3 and send it out.
I failed to catch this in my previous reply, but the comment I've added to iqs620_pwm_get_state actually reads as follows:
/*
* Since the device cannot generate a 0% duty cycle, requests to do so
* force subsequent calls to iqs620_pwm_get_state to report the output
* as disabled with duty cycle equal to that which was in use prior to
* the request. This is not ideal, but is the best compromise based on
* the capabilities of the device.
*/
This matches the present implementation, not your proposed comment that claims duty cycle is clamped to 1 / 256 ms following a request for a 0% duty cycle.
This seems OK since the concept of a duty cycle or period aren't really relevant if the output is disabled in my opinion. However if you prefer I update iqs620_pwm_apply to clamp duty cycle to 1 / 256 ms (instead of leaving it untouched) in this case, please let me know.
Quoted 6 lines
Best regards
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Wishing you a Happy New Year,
Jeff LaBundy
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hi Jeff,
Quoted 23 lines · Jeff LaBundy
Quoted 7 lines · Uwe Kleine-König
Quoted 3 lines · Jeff LaBundy
Based on your other feedback, I'm moving forward under the impression that you'll still accept option (2); please let me know if I have misunderstood (thank you for being flexible).
Yeah, that's fine. If in the end it shows that this is a bad idea we can still change to (3).
Sounds great. As soon as 5.5-rc5 lands this weekend, I'll rebase v3 and send it out.
I failed to catch this in my previous reply, but the comment I've added to
iqs620_pwm_get_stateactually reads as follows:/*
* Since the device cannot generate a 0% duty cycle, requests to do so
* force subsequent calls toiqs620_pwm_get_stateto report the output
* as disabled with duty cycle equal to that which was in use prior to
* the request. This is not ideal, but is the best compromise based on
* the capabilities of the device.
*/
Quoted 3 lines
This matches the present implementation, not your proposed comment that claims duty cycle is clamped to 1 / 256 ms following a request for a 0% duty cycle.
Yeah, if that's the mechanism that is actually implemented, that's fine of course.
Quoted 4 lines
This seems OK since the concept of a duty cycle or period aren't really relevant if the output is disabled in my opinion. However if you prefer I update
iqs620_pwm_applyto clamp duty cycle to 1 / 256 ms (instead of leaving it untouched) in this case, please let me know.
For a disabled PWM the duty_cycle and period are not relevant, for an enabled PWM running with 0% the period matters (at least in theory) however.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hi Uwe,
Quoted 33 lines · Uwe Kleine-König
Hi Jeff,
Quoted 23 lines · Jeff LaBundy
Quoted 7 lines · Uwe Kleine-König
Quoted 3 lines · Jeff LaBundy
Based on your other feedback, I'm moving forward under the impression that you'll still accept option (2); please let me know if I have misunderstood (thank you for being flexible).
Yeah, that's fine. If in the end it shows that this is a bad idea we can still change to (3).
Sounds great. As soon as 5.5-rc5 lands this weekend, I'll rebase v3 and send it out.
I failed to catch this in my previous reply, but the comment I've added to
iqs620_pwm_get_stateactually reads as follows:/*
* Since the device cannot generate a 0% duty cycle, requests to do so
* force subsequent calls toiqs620_pwm_get_stateto report the output
* as disabled with duty cycle equal to that which was in use prior to
* the request. This is not ideal, but is the best compromise based on
* the capabilities of the device.
*/Quoted 3 lines
This matches the present implementation, not your proposed comment that claims duty cycle is clamped to 1 / 256 ms following a request for a 0% duty cycle.
Yeah, if that's the mechanism that is actually implemented, that's fine of course.
Quoted 9 lines
Quoted 4 lines
This seems OK since the concept of a duty cycle or period aren't really relevant if the output is disabled in my opinion. However if you prefer I update
iqs620_pwm_applyto clamp duty cycle to 1 / 256 ms (instead of leaving it untouched) in this case, please let me know.For a disabled PWM the
duty_cycleand period are not relevant, for an enabled PWM running with 0% the period matters (at least in theory) however.
Agreed in full. We should be covered here since we report the (fixed) period in all cases.
Quoted 6 lines
Best regards
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
I managed to send out v3 this past weekend; please let me know if you have any further feedback or you find it to be satisfactory.
Kind regards,
Jeff LaBundy
Re: [PATCH v2 4/7] pwm: Add support for Azoteq IQS620A PWM generator
Hello Jeff,
Quoted 2 lines · Jeff LaBundy
I managed to send out v3 this past weekend; please let me know if you have any further feedback or you find it to be satisfactory.
Yeah, I'm aware. It's on my todo list to take a deeper look.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
PATCH v2 5/7 iio: temperature: Add support for Azoteq IQS620AT temperature sensor
In reply to Jeff LaBundyThis patch adds support for the Azoteq IQS620AT temperature sensor, capable of reporting its absolute die temperature.
---
Changes in v2:
- Moved the driver from hwmon to iio
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Eliminated tabbed alignment of platform_driver struct members
- Changed Kconfig "depends on" logic to MFD_IQS62X || COMPILE_TEST
drivers/iio/temperature/Kconfig | 10 ++++ drivers/iio/temperature/Makefile | 1 + drivers/iio/temperature/iqs620at-temp.c | 97 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 drivers/iio/temperature/iqs620at-temp.c
drivers/iio/temperature/Kconfig +10 -0
@@ -4,6 +4,16 @@ # menu "Temperature sensors" +config IQS620AT_TEMP + tristate "Azoteq IQS620AT temperature sensor" + depends on MFD_IQS62X || COMPILE_TEST + help + Say Y here if you want to build support for the Azoteq IQS620AT + temperature sensor. + + To compile this driver as a module, choose M here: the module + will be called iqs620at-temp. + config LTC2983 tristate "Analog Devices Multi-Sensor Digital Temperature Measurement System" depends on SPI
drivers/iio/temperature/Makefile +1 -0
@@ -3,6 +3,7 @@ # Makefile for industrial I/O temperature drivers # +obj-$(CONFIG_IQS620AT_TEMP) += iqs620at-temp.o obj-$(CONFIG_LTC2983) += ltc2983.o obj-$(CONFIG_HID_SENSOR_TEMP) += hid-sensor-temperature.o obj-$(CONFIG_MAXIM_THERMOCOUPLE) += maxim_thermocouple.o
drivers/iio/temperature/iqs620at-temp.c new file +97 -0
97 lines not on this page · raw message
--
2.7.4
Re: [PATCH v2 5/7] iio: temperature: Add support for Azoteq IQS620AT temperature sensor
Quoted 4 lines · On Mon, 9 Dec 2019 00:38:38 +0000 Jeff LaBundy <[email protected]> wrote:
This patch adds support for the Azoteq IQS620AT temperature sensor, capable of reporting its absolute die temperature.
Signed-off-by Jeff LaBundy <[email protected]>
Looks good to me.
Thanks,
Jonathan
Quoted 153 lines
---
Changes in v2:
- Moved the driver from hwmon to iio
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Eliminated tabbed alignment ofplatform_driverstruct members
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/iio/temperature/Kconfig | 10 ++++ drivers/iio/temperature/Makefile | 1 + drivers/iio/temperature/iqs620at-temp.c | 97 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 drivers/iio/temperature/iqs620at-temp.cdrivers/iio/temperature/Kconfig +10
-0@@ -4,6 +4,16 @@ # menu "Temperature sensors" +config IQS620AT_TEMP + tristate "Azoteq IQS620AT temperature sensor" + depends on MFD_IQS62X || COMPILE_TEST + help + Say Y here if you want to build support for the Azoteq IQS620AT + temperature sensor. + + To compile this driver as a module, choose M here: the module + will be called iqs620at-temp. + config LTC2983 tristate "Analog Devices Multi-Sensor Digital Temperature Measurement System" depends on SPIdrivers/iio/temperature/Makefile +1
-0@@ -3,6 +3,7 @@ # Makefile for industrial I/O temperature drivers # +obj-$(CONFIG_IQS620AT_TEMP) += iqs620at-temp.o obj-$(CONFIG_LTC2983) += ltc2983.o obj-$(CONFIG_HID_SENSOR_TEMP) += hid-sensor-temperature.o obj-$(CONFIG_MAXIM_THERMOCOUPLE) += maxim_thermocouple.odrivers/iio/temperature/iqs620at-temp.c new file +97
-1@@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS620AT Temperature Sensor +* +* Copyright (C) 2019 Jeff LaBundy <[email protected]> +*/ + +#include <linux/device.h> +#include <linux/iio/iio.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/regmap.h> + +#define IQS620_TEMP_UI_OUT 0x1A + +#define IQS620_TEMP_SCALE 1000 +#define IQS620_TEMP_OFFSET (-100) + +static int iqs620_temp_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct iqs62x_core *iqs62x =iio_device_get_drvdata (indio_dev); +int ret; +__le16 val_buf; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + ret =regmap_raw_read (iqs62x->map,IQS620_TEMP_UI_OUT , &val_buf, +sizeof (val_buf)); +if (ret) +return ret; + + *val =le16_to_cpu (val_buf); +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SCALE : + *val =IQS620_TEMP_SCALE ; +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_OFFSET : + *val =IQS620_TEMP_OFFSET ; +return IIO_VAL_INT ; + +default : +return -EINVAL ; + } +} + +static const struct iio_info iqs620_temp_info = { + .read_raw = &iqs620_temp_read_raw, +}; + +static const struct iio_chan_spec iqs620_temp_channels[] = { + { + .type =IIO_TEMP , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ) | +BIT (IIO_CHAN_INFO_OFFSET ), + }, +}; + +static int iqs620_temp_probe (struct platform_device *pdev) +{ +struct iqs62x_core *iqs62x =dev_get_drvdata (pdev->dev.parent); +struct iio_dev *indio_dev; + + indio_dev =devm_iio_device_alloc (&pdev->dev,0 ); +if (!indio_dev) +return -ENOMEM ; + +iio_device_set_drvdata (indio_dev, iqs62x); + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->dev.parent = &pdev->dev; + indio_dev->channels = iqs620_temp_channels; + indio_dev->num_channels =ARRAY_SIZE (iqs620_temp_channels); + indio_dev->name = iqs62x->dev_desc->dev_name; + indio_dev->info = &iqs620_temp_info; + +return devm_iio_device_register (&pdev->dev, indio_dev); +} + +static struct platform_driver iqs620_temp_platform_driver = { + .driver = { + .name =IQS620_DRV_NAME_TEMP , + }, + .probe = iqs620_temp_probe, +}; +module_platform_driver (iqs620_temp_platform_driver); + +MODULE_AUTHOR ("Jeff LaBundy <[email protected]>" ); +MODULE_DESCRIPTION ("Azoteq IQS620AT Temperature Sensor" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:" IQS620_DRV_NAME_TEMP ); --2.7.4
PATCH v2 6/7 iio: light: Add support for Azoteq IQS621/622 ambient light sensors
In reply to Jeff LaBundyThis patch adds support for the Azoteq IQS621 and IQS622 ambient light sensors, both of which can report a four-bit representation of ambient light intensity.
The IQS621 can additionally report illuminace directly in units of lux, while the IQS622 can report a four-bit representation of infrared light intensity. Furthermore, the IQS622 can report a unitless measurement of a target's proximity to the device.
---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Merged support for the closely related IQS622 (formerly represented by a separate iio/proximity driver)
- Added support for unitless ambient light intensity (IQS621 and IQS622) and infrared light intensity (IQS622 only)
- Moved the read of IQS621_ALS_FLAGS to iqs621_als_write_event_config to account for the fact that IQS621_ALS_FLAGS may have changed in between having first been read in iqs621_als_init and the time at which events are enabled, thereby eliminating the need to call iqs621_als_init from iqs621_als_probe
- Refactored the logic in iqs621_als_notifier and added a lock to safely evaluate variables that may change in response to user action
- Added locks to iqs621_als_read_event_config/value to account for cases in which the corresponding hardware state is in the process of being updated
- Refactored the logic in iqs621_als_read/write_event_value and removed all
#defines that could instead be represented by simple math
- Based the decision whether to select the IQS622 IR touch vs. proximity threshold on the single proximity threshold written by user space, and added a comment to describe the difference between either threshold
- Replaced IIO_CHAN_INFO_RAW with IIO_CHAN_INFO_PROCESSED for the IIO_LIGHT channel (IQS621 only)
- Removed devm_add_action_or_reset failure message
- Eliminated tabbed alignment of platform_driver struct members
- Changed Kconfig "depends on" logic to MFD_IQS62X || COMPILE_TEST
drivers/iio/light/Kconfig | 10 + drivers/iio/light/Makefile | 1 + drivers/iio/light/iqs621-als.c | 614 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 625 insertions(+) create mode 100644 drivers/iio/light/iqs621-als.c
drivers/iio/light/Kconfig +10 -0
@@ -173,6 +173,16 @@ config GP2AP020A00F To compile this driver as a module, choose M here: the module will be called gp2ap020a00f. +config IQS621_ALS + tristate "Azoteq IQS621/622 ambient light sensors" + depends on MFD_IQS62X || COMPILE_TEST + help + Say Y here if you want to build support for the Azoteq IQS621 + and IQS622 ambient light sensors. + + To compile this driver as a module, choose M here: the module + will be called iqs621-als. + config SENSORS_ISL29018 tristate "Intersil 29018 light and proximity sensor" depends on I2C
drivers/iio/light/Makefile +1 -0
@@ -21,6 +21,7 @@ obj-$(CONFIG_IIO_CROS_EC_LIGHT_PROX) += cros_ec_light_prox.o obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o +obj-$(CONFIG_IQS621_ALS) += iqs621-als.o obj-$(CONFIG_SENSORS_ISL29018) += isl29018.o obj-$(CONFIG_SENSORS_ISL29028) += isl29028.o obj-$(CONFIG_ISL29125) += isl29125.o
drivers/iio/light/iqs621-als.c new file +614 -0
614 lines not on this page · raw message
--
2.7.4
Re: [PATCH v2 6/7] iio: light: Add support for Azoteq IQS621/622 ambient light sensors
Quoted 10 lines · On Mon, 9 Dec 2019 00:38:39 +0000 Jeff LaBundy <[email protected]> wrote:
This patch adds support for the Azoteq IQS621 and IQS622 ambient light sensors, both of which can report a four-bit representation of ambient light intensity.
The IQS621 can additionally report illuminace directly in units of lux, while the IQS622 can report a four-bit representation of infrared light intensity. Furthermore, the IQS622 can report a unitless measurement of a target's proximity to the device.
Signed-off-by Jeff LaBundy <[email protected]>
looks good to me.
Nice little driver :)
Jonathan
Quoted 690 lines
---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Merged support for the closely related IQS622 (formerly represented by a separate iio/proximity driver)
- Added support for unitless ambient light intensity (IQS621 and IQS622) and infrared light intensity (IQS622 only)
- Moved the read ofIQS621_ALS_FLAGStoiqs621_als_write_event_configto account for the fact thatIQS621_ALS_FLAGSmay have changed in between having first been read iniqs621_als_initand the time at which events are enabled, thereby eliminating the need to calliqs621_als_initfromiqs621_als_probe
- Refactored the logic iniqs621_als_notifierand added a lock to safely evaluate variables that may change in response to user action
- Added locks to iqs621_als_read_event_config/value to account for cases in which the corresponding hardware state is in the process of being updated
- Refactored the logic in iqs621_als_read/write_event_value and removed all#defines that could instead be represented by simple math- Based the decision whether to select the IQS622 IR touch vs. proximity threshold on the single proximity threshold written by user space, and added a comment to describe the difference between either threshold
- ReplacedIIO_CHAN_INFO_RAWwithIIO_CHAN_INFO_PROCESSEDfor theIIO_LIGHTchannel (IQS621 only)
- Removeddevm_add_action_or_resetfailure message
- Eliminated tabbed alignment ofplatform_driverstruct members
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/iio/light/Kconfig | 10 + drivers/iio/light/Makefile | 1 + drivers/iio/light/iqs621-als.c | 614 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 625 insertions(+) create mode 100644 drivers/iio/light/iqs621-als.cdrivers/iio/light/Kconfig +10
-0@@ -173,6 +173,16 @@ config GP2AP020A00F To compile this driver as a module, choose M here: the module will be called gp2ap020a00f. +config IQS621_ALS + tristate "Azoteq IQS621/622 ambient light sensors" + depends on MFD_IQS62X || COMPILE_TEST + help + Say Y here if you want to build support for the Azoteq IQS621 + and IQS622 ambient light sensors. + + To compile this driver as a module, choose M here: the module + will be called iqs621-als. + config SENSORS_ISL29018 tristate "Intersil 29018 light and proximity sensor" depends on I2Cdrivers/iio/light/Makefile +1
-0@@ -21,6 +21,7 @@ obj-$(CONFIG_IIO_CROS_EC_LIGHT_PROX) += cros_ec_light_prox.o obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o +obj-$(CONFIG_IQS621_ALS) += iqs621-als.o obj-$(CONFIG_SENSORS_ISL29018) += isl29018.o obj-$(CONFIG_SENSORS_ISL29028) += isl29028.o obj-$(CONFIG_ISL29125) += isl29125.odrivers/iio/light/iqs621-als.c new file +614
-1@@ -0,0 +1,614 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS621/622 Ambient Light Sensors +* +* Copyright (C) 2019 Jeff LaBundy <[email protected]> +*/ + +#include <linux/device.h> +#include <linux/iio/events.h> +#include <linux/iio/iio.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/platform_device.h> +#include <linux/regmap.h> + +#define IQS621_ALS_FLAGS_LIGHT BIT(7) +#define IQS621_ALS_FLAGS_RANGE GENMASK(3, 0) + +#define IQS621_ALS_UI_OUT 0x17 + +#define IQS621_ALS_THRESH_DARK 0x80 +#define IQS621_ALS_THRESH_LIGHT 0x81 + +#define IQS622_IR_RANGE 0x15 +#define IQS622_IR_FLAGS 0x16 +#define IQS622_IR_FLAGS_TOUCH BIT(1) +#define IQS622_IR_FLAGS_PROX BIT(0) + +#define IQS622_IR_UI_OUT 0x17 + +#define IQS622_IR_THRESH_PROX 0x91 +#define IQS622_IR_THRESH_TOUCH 0x92 + +struct iqs621_als_private { +struct iqs62x_core *iqs62x; +struct notifier_block notifier; +struct mutex lock; +bool light_en; +bool range_en; +bool prox_en; +u8 als_flags; +u8 ir_flags_mask; +u8 ir_flags; +u8 thresh_light; +u8 thresh_dark; +u8 thresh_prox; +}; + +static int iqs621_als_init (struct iqs621_als_private *iqs621_als) +{ +struct iqs62x_core *iqs62x = iqs621_als->iqs62x; +unsigned int event_mask =0 ; +int ret; + +switch (iqs621_als->ir_flags_mask) { +case IQS622_IR_FLAGS_TOUCH : + ret =regmap_write (iqs62x->map,IQS622_IR_THRESH_TOUCH , + iqs621_als->thresh_prox); +break ; + +case IQS622_IR_FLAGS_PROX : + ret =regmap_write (iqs62x->map,IQS622_IR_THRESH_PROX , + iqs621_als->thresh_prox); +break ; + +default : + ret =regmap_write (iqs62x->map,IQS621_ALS_THRESH_LIGHT , + iqs621_als->thresh_light); +if (ret) +return ret; + + ret =regmap_write (iqs62x->map,IQS621_ALS_THRESH_DARK , + iqs621_als->thresh_dark); + } + +if (ret) +return ret; + +if (iqs621_als->light_en || iqs621_als->range_en) + event_mask |= iqs62x->dev_desc->als_mask; + +if (iqs621_als->prox_en) + event_mask |= iqs62x->dev_desc->ir_mask; + +return regmap_update_bits (iqs62x->map,IQS620_GLBL_EVENT_MASK , + event_mask,0 ); +} + +static int iqs621_als_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{ +struct iqs62x_event_data *event_data = context; +struct iqs621_als_private *iqs621_als; +struct iio_dev *indio_dev; +bool light_new, light_old; +bool prox_new, prox_old; +u8 range_new, range_old; +s64 timestamp; +int ret; + + iqs621_als = container_of(notifier, struct iqs621_als_private, + notifier); + indio_dev =iio_priv_to_dev (iqs621_als); + timestamp =iio_get_time_ns (indio_dev); + +mutex_lock (&iqs621_als->lock); + +if (event_flags &BIT (IQS62X_EVENT_SYS_RESET )) { + ret =iqs621_als_init (iqs621_als); +if (ret) { +dev_err (indio_dev->dev.parent, +"Failed to re-initialize device: %d\n" , ret); + ret =NOTIFY_BAD ; + }else { + ret =NOTIFY_OK ; + } + + goto err_mutex; + } + +if (!iqs621_als->light_en && !iqs621_als->range_en && + !iqs621_als->prox_en) { + ret =NOTIFY_DONE ; + goto err_mutex; + } + +/* IQS621 only */ + light_new = event_data->als_flags &IQS621_ALS_FLAGS_LIGHT ; + light_old = iqs621_als->als_flags &IQS621_ALS_FLAGS_LIGHT ; + +if (iqs621_als->light_en && light_new && !light_old) +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_RISING ), + timestamp); +else if (iqs621_als->light_en && !light_new && light_old) +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_FALLING ), + timestamp); + +/* IQS621 and IQS622 */ + range_new = event_data->als_flags &IQS621_ALS_FLAGS_RANGE ; + range_old = iqs621_als->als_flags &IQS621_ALS_FLAGS_RANGE ; + +if (iqs621_als->range_en && (range_new > range_old)) +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_INTENSITY ,0 , +IIO_EV_TYPE_CHANGE , +IIO_EV_DIR_RISING ), + timestamp); +else if (iqs621_als->range_en && (range_new < range_old)) +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_INTENSITY ,0 , +IIO_EV_TYPE_CHANGE , +IIO_EV_DIR_FALLING ), + timestamp); + +/* IQS622 only */ + prox_new = event_data->ir_flags & iqs621_als->ir_flags_mask; + prox_old = iqs621_als->ir_flags & iqs621_als->ir_flags_mask; + +if (iqs621_als->prox_en && prox_new && !prox_old) +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_PROXIMITY ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_RISING ), + timestamp); +else if (iqs621_als->prox_en && !prox_new && prox_old) +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_PROXIMITY ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_FALLING ), + timestamp); + + iqs621_als->als_flags = event_data->als_flags; + iqs621_als->ir_flags = event_data->ir_flags; + ret =NOTIFY_OK ; + +err_mutex: +mutex_unlock (&iqs621_als->lock); + +return ret; +} + +static void iqs621_als_notifier_unregister (void *context) +{ +struct iqs621_als_private *iqs621_als = context; +struct iio_dev *indio_dev =iio_priv_to_dev (iqs621_als); +int ret; + + ret =blocking_notifier_chain_unregister (&iqs621_als->iqs62x->nh, + &iqs621_als->notifier); +if (ret) +dev_err (indio_dev->dev.parent, +"Failed to unregister notifier: %d\n" , ret); +} + +static int iqs621_als_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct iqs621_als_private *iqs621_als =iio_priv (indio_dev); +struct iqs62x_core *iqs62x = iqs621_als->iqs62x; +int ret; +__le16 val_buf; + +switch (chan->type) { +case IIO_INTENSITY : + ret =regmap_read (iqs62x->map, chan->address, val); +if (ret) +return ret; + + *val &=IQS621_ALS_FLAGS_RANGE ; +return IIO_VAL_INT ; + +case IIO_PROXIMITY : +case IIO_LIGHT : + ret =regmap_raw_read (iqs62x->map, chan->address, &val_buf, +sizeof (val_buf)); +if (ret) +return ret; + + *val =le16_to_cpu (val_buf); +return IIO_VAL_INT ; + +default : +return -EINVAL ; + } +} + +static int iqs621_als_read_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir) +{ +struct iqs621_als_private *iqs621_als =iio_priv (indio_dev); +int ret; + +mutex_lock (&iqs621_als->lock); + +switch (chan->type) { +case IIO_LIGHT : + ret = iqs621_als->light_en; +break ; + +case IIO_INTENSITY : + ret = iqs621_als->range_en; +break ; + +case IIO_PROXIMITY : + ret = iqs621_als->prox_en; +break ; + +default : + ret = -EINVAL ; + } + +mutex_unlock (&iqs621_als->lock); + +return ret; +} + +static int iqs621_als_write_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +int state) +{ +struct iqs621_als_private *iqs621_als =iio_priv (indio_dev); +struct iqs62x_core *iqs62x = iqs621_als->iqs62x; +unsigned int val; +int ret; + +mutex_lock (&iqs621_als->lock); + + ret =regmap_read (iqs62x->map, iqs62x->dev_desc->als_flags, &val); +if (ret) + goto err_mutex; + iqs621_als->als_flags = val; + +switch (chan->type) { +case IIO_LIGHT : + ret =regmap_update_bits (iqs62x->map,IQS620_GLBL_EVENT_MASK , + iqs62x->dev_desc->als_mask, + iqs621_als->range_en | state ?0 : +0xFF ); +if (!ret) + iqs621_als->light_en = state; +break ; + +case IIO_INTENSITY : + ret =regmap_update_bits (iqs62x->map,IQS620_GLBL_EVENT_MASK , + iqs62x->dev_desc->als_mask, + iqs621_als->light_en | state ?0 : +0xFF ); +if (!ret) + iqs621_als->range_en = state; +break ; + +case IIO_PROXIMITY : + ret =regmap_read (iqs62x->map,IQS622_IR_FLAGS , &val); +if (ret) + goto err_mutex; + iqs621_als->ir_flags = val; + + ret =regmap_update_bits (iqs62x->map,IQS620_GLBL_EVENT_MASK , + iqs62x->dev_desc->ir_mask, + state ?0 :0xFF ); +if (!ret) + iqs621_als->prox_en = state; +break ; + +default : + ret = -EINVAL ; + } + +err_mutex: +mutex_unlock (&iqs621_als->lock); + +return ret; +} + +static int iqs621_als_read_event_value (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info, +int *val,int *val2) +{ +struct iqs621_als_private *iqs621_als =iio_priv (indio_dev); +int ret =IIO_VAL_INT ; + +mutex_lock (&iqs621_als->lock); + +switch (dir) { +case IIO_EV_DIR_RISING : + *val = iqs621_als->thresh_light *16 ; +break ; + +case IIO_EV_DIR_FALLING : + *val = iqs621_als->thresh_dark *4 ; +break ; + +case IIO_EV_DIR_EITHER : +if (iqs621_als->ir_flags_mask ==IQS622_IR_FLAGS_TOUCH ) + *val = iqs621_als->thresh_prox *4 ; +else + *val = iqs621_als->thresh_prox; +break ; + +default : + ret = -EINVAL ; + } + +mutex_unlock (&iqs621_als->lock); + +return ret; +} + +static int iqs621_als_write_event_value (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info, +int val,int val2) +{ +struct iqs621_als_private *iqs621_als =iio_priv (indio_dev); +struct iqs62x_core *iqs62x = iqs621_als->iqs62x; +unsigned int thresh_reg, thresh_val; +u8 ir_flags_mask, *thresh_cache; +int ret = -EINVAL ; + +mutex_lock (&iqs621_als->lock); + +switch (dir) { +case IIO_EV_DIR_RISING : + thresh_reg =IQS621_ALS_THRESH_LIGHT ; + thresh_val = val /16 ; + + thresh_cache = &iqs621_als->thresh_light; + ir_flags_mask =0 ; +break ; + +case IIO_EV_DIR_FALLING : + thresh_reg =IQS621_ALS_THRESH_DARK ; + thresh_val = val /4 ; + + thresh_cache = &iqs621_als->thresh_dark; + ir_flags_mask =0 ; +break ; + +case IIO_EV_DIR_EITHER : +/* +* The IQS622 supports two detection thresholds, both measured +* in the same arbitrary units reported by read_raw: proximity +* (0 through 255 in steps of 1), and touch (0 through 1020 in +* steps of 4). +* +* Based on the single detection threshold chosen by the user, +* select the hardware threshold that gives the best trade-off +* between range and resolution. +* +* By default, the close-range (but coarse) touch threshold is +* chosen during probe. +*/ +switch (val) { + case 0 ... 255: + thresh_reg =IQS622_IR_THRESH_PROX ; + thresh_val = val; + + ir_flags_mask =IQS622_IR_FLAGS_PROX ; +break ; + + case 256 ... 1020: + thresh_reg =IQS622_IR_THRESH_TOUCH ; + thresh_val = val /4 ; + + ir_flags_mask =IQS622_IR_FLAGS_TOUCH ; +break ; + +default : + goto err_mutex; + } + + thresh_cache = &iqs621_als->thresh_prox; +break ; + +default : + goto err_mutex; + } + +if (thresh_val >0xFF ) + goto err_mutex; + + ret =regmap_write (iqs62x->map, thresh_reg, thresh_val); +if (ret) + goto err_mutex; + + *thresh_cache = thresh_val; + iqs621_als->ir_flags_mask = ir_flags_mask; + +err_mutex: +mutex_unlock (&iqs621_als->lock); + +return ret; +} + +static const struct iio_info iqs621_als_info = { + .read_raw = &iqs621_als_read_raw, + .read_event_config = iqs621_als_read_event_config, + .write_event_config = iqs621_als_write_event_config, + .read_event_value = iqs621_als_read_event_value, + .write_event_value = iqs621_als_write_event_value, +}; + +static const struct iio_event_spec iqs621_als_range_events[] = { + { + .type =IIO_EV_TYPE_CHANGE , + .dir =IIO_EV_DIR_EITHER , + .mask_separate =BIT (IIO_EV_INFO_ENABLE ), + }, +}; + +static const struct iio_event_spec iqs621_als_light_events[] = { + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_EITHER , + .mask_separate =BIT (IIO_EV_INFO_ENABLE ), + }, + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_RISING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ), + }, + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_FALLING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ), + }, +}; + +static const struct iio_chan_spec iqs621_als_channels[] = { + { + .type =IIO_INTENSITY , + .address =IQS621_ALS_FLAGS , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .event_spec = iqs621_als_range_events, + .num_event_specs =ARRAY_SIZE (iqs621_als_range_events), + }, + { + .type =IIO_LIGHT , + .address =IQS621_ALS_UI_OUT , + .info_mask_separate =BIT (IIO_CHAN_INFO_PROCESSED ), + .event_spec = iqs621_als_light_events, + .num_event_specs =ARRAY_SIZE (iqs621_als_light_events), + }, +}; + +static const struct iio_event_spec iqs622_als_prox_events[] = { + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_EITHER , + .mask_separate =BIT (IIO_EV_INFO_ENABLE ) | +BIT (IIO_EV_INFO_VALUE ), + }, +}; + +static const struct iio_chan_spec iqs622_als_channels[] = { + { + .type =IIO_INTENSITY , + .channel2 =IIO_MOD_LIGHT_BOTH , + .address =IQS622_ALS_FLAGS , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .event_spec = iqs621_als_range_events, + .num_event_specs =ARRAY_SIZE (iqs621_als_range_events), + .modified = true, + }, + { + .type =IIO_INTENSITY , + .channel2 =IIO_MOD_LIGHT_IR , + .address =IQS622_IR_RANGE , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .modified = true, + }, + { + .type =IIO_PROXIMITY , + .address =IQS622_IR_UI_OUT , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .event_spec = iqs622_als_prox_events, + .num_event_specs =ARRAY_SIZE (iqs622_als_prox_events), + }, +}; + +static int iqs621_als_probe (struct platform_device *pdev) +{ +struct iqs62x_core *iqs62x =dev_get_drvdata (pdev->dev.parent); +struct iqs621_als_private *iqs621_als; +struct iio_dev *indio_dev; +unsigned int val; +int ret; + + indio_dev =devm_iio_device_alloc (&pdev->dev,sizeof (*iqs621_als)); +if (!indio_dev) +return -ENOMEM ; + + iqs621_als =iio_priv (indio_dev); + iqs621_als->iqs62x = iqs62x; + +if (iqs62x->dev_desc->prod_num ==IQS622_PROD_NUM ) { + ret =regmap_read (iqs62x->map,IQS622_IR_THRESH_TOUCH , &val); +if (ret) +return ret; + iqs621_als->thresh_prox = val; + iqs621_als->ir_flags_mask =IQS622_IR_FLAGS_TOUCH ; + + indio_dev->channels = iqs622_als_channels; + indio_dev->num_channels =ARRAY_SIZE (iqs622_als_channels); + }else { + ret =regmap_read (iqs62x->map,IQS621_ALS_THRESH_LIGHT , &val); +if (ret) +return ret; + iqs621_als->thresh_light = val; + + ret =regmap_read (iqs62x->map,IQS621_ALS_THRESH_DARK , &val); +if (ret) +return ret; + iqs621_als->thresh_dark = val; + + indio_dev->channels = iqs621_als_channels; + indio_dev->num_channels =ARRAY_SIZE (iqs621_als_channels); + } + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->dev.parent = &pdev->dev; + indio_dev->name = iqs62x->dev_desc->dev_name; + indio_dev->info = &iqs621_als_info; + +mutex_init (&iqs621_als->lock); + + iqs621_als->notifier.notifier_call = iqs621_als_notifier; + ret =blocking_notifier_chain_register (&iqs621_als->iqs62x->nh, + &iqs621_als->notifier); +if (ret) { +dev_err (&pdev->dev,"Failed to register notifier: %d\n" , ret); +return ret; + } + + ret =devm_add_action_or_reset (&pdev->dev, + iqs621_als_notifier_unregister, + iqs621_als); +if (ret) +return ret; + +return devm_iio_device_register (&pdev->dev, indio_dev); +} + +static struct platform_driver iqs621_als_platform_driver = { + .driver = { + .name =IQS621_DRV_NAME_ALS , + }, + .probe = iqs621_als_probe, +}; +module_platform_driver (iqs621_als_platform_driver); + +MODULE_AUTHOR ("Jeff LaBundy <[email protected]>" ); +MODULE_DESCRIPTION ("Azoteq IQS621/622 Ambient Light Sensors" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:" IQS621_DRV_NAME_ALS ); --2.7.4
PATCH v2 7/7 iio: position: Add support for Azoteq IQS624/625 angle sensors
In reply to Jeff LaBundyThis patch adds support for the Azoteq IQS624 and IQS625 angular position sensors, capable of reporting the angle of a rotating shaft down to 1 and 10 degrees of accuracy, respectively.
This patch also introduces a home for linear and angular position sensors. Unlike resolvers, they are typically contactless and use the Hall effect.
---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Added iqs624_pos_angle_en and iqs624_pos_angle_get to remove duplicate logic previously used throughout
- Refactored the logic in iqs624_pos_notifier and added a lock to safely evaluate variables that may change in response to user action
- Refactored the logic in iqs624_pos_read_raw
- Added a lock to iqs624_pos_read_event_config to account for cases in which the corresponding hardware state is in the process of being updated
- Refactored the logic in iqs624_pos_write_event_config and read the initial angle in case it changed since having first been read in iqs624_pos_init
- Removed iqs624_pos_init as its logic has since been absorbed elsewhere
- Removed devm_add_action_or_reset failure message
- Eliminated tabbed alignment of platform_driver struct members
- Changed Kconfig "depends on" logic to MFD_IQS62X || COMPILE_TEST
drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/position/Kconfig | 19 +++ drivers/iio/position/Makefile | 7 + drivers/iio/position/iqs624-pos.c | 284 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 312 insertions(+) create mode 100644 drivers/iio/position/Kconfig create mode 100644 drivers/iio/position/Makefile create mode 100644 drivers/iio/position/iqs624-pos.c
drivers/iio/Kconfig +1 -0
@@ -88,6 +88,7 @@ source "drivers/iio/orientation/Kconfig" if IIO_TRIGGER source "drivers/iio/trigger/Kconfig" endif #IIO_TRIGGER +source "drivers/iio/position/Kconfig" source "drivers/iio/potentiometer/Kconfig" source "drivers/iio/potentiostat/Kconfig" source "drivers/iio/pressure/Kconfig"
drivers/iio/Makefile +1 -0
@@ -31,6 +31,7 @@ obj-y += light/ obj-y += magnetometer/ obj-y += multiplexer/ obj-y += orientation/ +obj-y += position/ obj-y += potentiometer/ obj-y += potentiostat/ obj-y += pressure/
drivers/iio/position/Kconfig new file +19 -0
@@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Linear and angular position sensors +# +# When adding new entries keep the list in alphabetical order + +menu "Linear and angular position sensors" + +config IQS624_POS + tristate "Azoteq IQS624/625 angular position sensors" + depends on MFD_IQS62X || COMPILE_TEST + help + Say Y here if you want to build support for the Azoteq IQS624 + and IQS625 angular position sensors. + + To compile this driver as a module, choose M here: the module + will be called iqs624-pos. + +endmenu
drivers/iio/position/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# +# Makefile for IIO linear and angular position sensors +# + +# When adding new entries keep the list in alphabetical order + +obj-$(CONFIG_IQS624_POS) += iqs624-pos.o
drivers/iio/position/iqs624-pos.c new file +284 -0
284 lines not on this page · raw message
--
2.7.4
Re: [PATCH v2 7/7] iio: position: Add support for Azoteq IQS624/625 angle sensors
Quoted 8 lines · On Mon, 9 Dec 2019 00:38:41 +0000 Jeff LaBundy <[email protected]> wrote:
This patch adds support for the Azoteq IQS624 and IQS625 angular position sensors, capable of reporting the angle of a rotating shaft down to 1 and 10 degrees of accuracy, respectively.
This patch also introduces a home for linear and angular position sensors. Unlike resolvers, they are typically contactless and use the Hall effect.
Signed-off-by Jeff LaBundy <[email protected]>
Looks good
My current assumption is that Lee will take this lot via an immutable branch in MFD once it's ready. Shout if a different path makes sense.
Quoted 383 lines
---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Addediqs624_pos_angle_enandiqs624_pos_angle_getto remove duplicate logic previously used throughout
- Refactored the logic iniqs624_pos_notifierand added a lock to safely evaluate variables that may change in response to user action
- Refactored the logic iniqs624_pos_read_raw
- Added a lock toiqs624_pos_read_event_configto account for cases in which the corresponding hardware state is in the process of being updated
- Refactored the logic iniqs624_pos_write_event_configand read the initial angle in case it changed since having first been read iniqs624_pos_init
- Removediqs624_pos_initas its logic has since been absorbed elsewhere
- Removeddevm_add_action_or_resetfailure message
- Eliminated tabbed alignment ofplatform_driverstruct members
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/position/Kconfig | 19 +++ drivers/iio/position/Makefile | 7 + drivers/iio/position/iqs624-pos.c | 284 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 312 insertions(+) create mode 100644 drivers/iio/position/Kconfig create mode 100644 drivers/iio/position/Makefile create mode 100644 drivers/iio/position/iqs624-pos.cdrivers/iio/Kconfig +1
-0@@ -88,6 +88,7 @@ source "drivers/iio/orientation/Kconfig" if IIO_TRIGGER source "drivers/iio/trigger/Kconfig" endif #IIO_TRIGGER +source "drivers/iio/position/Kconfig" source "drivers/iio/potentiometer/Kconfig" source "drivers/iio/potentiostat/Kconfig" source "drivers/iio/pressure/Kconfig"drivers/iio/Makefile +1
-0@@ -31,6 +31,7 @@ obj-y += light/ obj-y += magnetometer/ obj-y += multiplexer/ obj-y += orientation/ +obj-y += position/ obj-y += potentiometer/ obj-y += potentiostat/ obj-y += pressure/drivers/iio/position/Kconfig new file +19
-0@@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Linear and angular position sensors +# +# When adding new entries keep the list in alphabetical order + +menu "Linear and angular position sensors" + +config IQS624_POS + tristate "Azoteq IQS624/625 angular position sensors" + depends on MFD_IQS62X || COMPILE_TEST + help + Say Y here if you want to build support for the Azoteq IQS624 + and IQS625 angular position sensors. + + To compile this driver as a module, choose M here: the module + will be called iqs624-pos. + +endmenudrivers/iio/position/Makefile new file +7
-0@@ -0,0 +1,7 @@ +# +# Makefile for IIO linear and angular position sensors +# + +# When adding new entries keep the list in alphabetical order + +obj-$(CONFIG_IQS624_POS) += iqs624-pos.odrivers/iio/position/iqs624-pos.c new file +284
-1@@ -0,0 +1,284 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS624/625 Angular Position Sensors +* +* Copyright (C) 2019 Jeff LaBundy <[email protected]> +*/ + +#include <linux/device.h> +#include <linux/iio/events.h> +#include <linux/iio/iio.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/platform_device.h> +#include <linux/regmap.h> + +#define IQS624_POS_DEG_OUT 0x16 + +#define IQS624_POS_SCALE1 (314159 / 180) +#define IQS624_POS_SCALE2 100000 + +struct iqs624_pos_private { +struct iqs62x_core *iqs62x; +struct notifier_block notifier; +struct mutex lock; +bool angle_en; +u16 angle; +}; + +static int iqs624_pos_angle_en (struct iqs62x_core *iqs62x,bool angle_en) +{ +unsigned int event_mask =IQS624_HALL_UI_WHL_EVENT ; + +/* +* The IQS625 reports angular position in the form of coarse intervals, +* so only interval change events are unmasked. Conversely, the IQS624 +* reports angular position down to one degree of resolution, so wheel +* movement events are unmasked instead. +*/ +if (iqs62x->dev_desc->prod_num ==IQS625_PROD_NUM ) + event_mask =IQS624_HALL_UI_INT_EVENT ; + +return regmap_update_bits (iqs62x->map,IQS624_HALL_UI , event_mask, + angle_en ?0 :0xFF ); +} + +static int iqs624_pos_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{ +struct iqs62x_event_data *event_data = context; +struct iqs624_pos_private *iqs624_pos; +struct iqs62x_core *iqs62x; +struct iio_dev *indio_dev; +u16 angle = event_data->ui_data; +s64 timestamp; +int ret; + + iqs624_pos = container_of(notifier, struct iqs624_pos_private, + notifier); + indio_dev =iio_priv_to_dev (iqs624_pos); + timestamp =iio_get_time_ns (indio_dev); + + iqs62x = iqs624_pos->iqs62x; +if (iqs62x->dev_desc->prod_num ==IQS625_PROD_NUM ) + angle = event_data->interval; + +mutex_lock (&iqs624_pos->lock); + +if (event_flags &BIT (IQS62X_EVENT_SYS_RESET )) { + ret =iqs624_pos_angle_en (iqs62x, iqs624_pos->angle_en); +if (ret) { +dev_err (indio_dev->dev.parent, +"Failed to re-initialize device: %d\n" , ret); + ret =NOTIFY_BAD ; + }else { + ret =NOTIFY_OK ; + } + }else if (iqs624_pos->angle_en && (angle != iqs624_pos->angle)) { +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_ANGL ,0 , +IIO_EV_TYPE_CHANGE , +IIO_EV_DIR_NONE ), + timestamp); + + iqs624_pos->angle = angle; + ret =NOTIFY_OK ; + }else { + ret =NOTIFY_DONE ; + } + +mutex_unlock (&iqs624_pos->lock); + +return ret; +} + +static void iqs624_pos_notifier_unregister (void *context) +{ +struct iqs624_pos_private *iqs624_pos = context; +struct iio_dev *indio_dev =iio_priv_to_dev (iqs624_pos); +int ret; + + ret =blocking_notifier_chain_unregister (&iqs624_pos->iqs62x->nh, + &iqs624_pos->notifier); +if (ret) +dev_err (indio_dev->dev.parent, +"Failed to unregister notifier: %d\n" , ret); +} + +static int iqs624_pos_angle_get (struct iqs62x_core *iqs62x,unsigned int *val) +{ +int ret; +__le16 val_buf; + +if (iqs62x->dev_desc->prod_num ==IQS625_PROD_NUM ) +return regmap_read (iqs62x->map, iqs62x->dev_desc->interval, + val); + + ret =regmap_raw_read (iqs62x->map,IQS624_POS_DEG_OUT , &val_buf, +sizeof (val_buf)); +if (ret) +return ret; + + *val =le16_to_cpu (val_buf); + +return 0 ; +} + +static int iqs624_pos_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct iqs624_pos_private *iqs624_pos =iio_priv (indio_dev); +struct iqs62x_core *iqs62x = iqs624_pos->iqs62x; +unsigned int scale =1 ; +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + ret =iqs624_pos_angle_get (iqs62x, val); +if (ret) +return ret; + +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SCALE : +if (iqs62x->dev_desc->prod_num ==IQS625_PROD_NUM ) { + ret =regmap_read (iqs62x->map,IQS624_INTERVAL_DIV , + &scale); +if (ret) +return ret; + } + + *val = scale *IQS624_POS_SCALE1 ; + *val2 =IQS624_POS_SCALE2 ; +return IIO_VAL_FRACTIONAL ; + +default : +return -EINVAL ; + } +} + +static int iqs624_pos_read_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir) +{ +struct iqs624_pos_private *iqs624_pos =iio_priv (indio_dev); +int ret; + +mutex_lock (&iqs624_pos->lock); + ret = iqs624_pos->angle_en; +mutex_unlock (&iqs624_pos->lock); + +return ret; +} + +static int iqs624_pos_write_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +int state) +{ +struct iqs624_pos_private *iqs624_pos =iio_priv (indio_dev); +struct iqs62x_core *iqs62x = iqs624_pos->iqs62x; +unsigned int val; +int ret; + +mutex_lock (&iqs624_pos->lock); + + ret =iqs624_pos_angle_get (iqs62x, &val); +if (ret) + goto err_mutex; + + ret =iqs624_pos_angle_en (iqs62x, state); +if (ret) + goto err_mutex; + + iqs624_pos->angle = val; + iqs624_pos->angle_en = state; + +err_mutex: +mutex_unlock (&iqs624_pos->lock); + +return ret; +} + +static const struct iio_info iqs624_pos_info = { + .read_raw = &iqs624_pos_read_raw, + .read_event_config = iqs624_pos_read_event_config, + .write_event_config = iqs624_pos_write_event_config, +}; + +static const struct iio_event_spec iqs624_pos_events[] = { + { + .type =IIO_EV_TYPE_CHANGE , + .dir =IIO_EV_DIR_NONE , + .mask_separate =BIT (IIO_EV_INFO_ENABLE ), + }, +}; + +static const struct iio_chan_spec iqs624_pos_channels[] = { + { + .type =IIO_ANGL , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ), + .event_spec = iqs624_pos_events, + .num_event_specs =ARRAY_SIZE (iqs624_pos_events), + }, +}; + +static int iqs624_pos_probe (struct platform_device *pdev) +{ +struct iqs62x_core *iqs62x =dev_get_drvdata (pdev->dev.parent); +struct iqs624_pos_private *iqs624_pos; +struct iio_dev *indio_dev; +int ret; + + indio_dev =devm_iio_device_alloc (&pdev->dev,sizeof (*iqs624_pos)); +if (!indio_dev) +return -ENOMEM ; + + iqs624_pos =iio_priv (indio_dev); + iqs624_pos->iqs62x = iqs62x; + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->dev.parent = &pdev->dev; + indio_dev->channels = iqs624_pos_channels; + indio_dev->num_channels =ARRAY_SIZE (iqs624_pos_channels); + indio_dev->name = iqs62x->dev_desc->dev_name; + indio_dev->info = &iqs624_pos_info; + +mutex_init (&iqs624_pos->lock); + + iqs624_pos->notifier.notifier_call = iqs624_pos_notifier; + ret =blocking_notifier_chain_register (&iqs624_pos->iqs62x->nh, + &iqs624_pos->notifier); +if (ret) { +dev_err (&pdev->dev,"Failed to register notifier: %d\n" , ret); +return ret; + } + + ret =devm_add_action_or_reset (&pdev->dev, + iqs624_pos_notifier_unregister, + iqs624_pos); +if (ret) +return ret; + +return devm_iio_device_register (&pdev->dev, indio_dev); +} + +static struct platform_driver iqs624_pos_platform_driver = { + .driver = { + .name =IQS624_DRV_NAME_POS , + }, + .probe = iqs624_pos_probe, +}; +module_platform_driver (iqs624_pos_platform_driver); + +MODULE_AUTHOR ("Jeff LaBundy <[email protected]>" ); +MODULE_DESCRIPTION ("Azoteq IQS624/625 Angular Position Sensors" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:" IQS624_DRV_NAME_POS ); --2.7.4
Re: [PATCH v2 7/7] iio: position: Add support for Azoteq IQS624/625 angle sensors
Hi Jonathan,
Thank you for your continued support on this project.
Quoted 10 lines · Jonathan Cameron
Quoted 7 lines · On Mon, 9 Dec 2019 00:38:41 +0000 Jeff LaBundy <[email protected]> wrote:
This patch adds support for the Azoteq IQS624 and IQS625 angular position sensors, capable of reporting the angle of a rotating shaft down to 1 and 10 degrees of accuracy, respectively.
This patch also introduces a home for linear and angular position sensors. Unlike resolvers, they are typically contactless and use the Hall effect.
Quoted 8 lines
Quoted 1 line
Signed-off-by Jeff LaBundy <[email protected]>Looks good
Reviewed-by Jonathan Cameron <[email protected]>My current assumption is that Lee will take this lot via an immutable branch in MFD once it's ready. Shout if a different path makes sense.
Same here. @Lee, please let us know if you disagree.
Quoted 375 lines
Quoted 375 lines
---
Changes in v2:
- Merged 'Copyright' and 'Author' lines into one in introductory comments
- Replaced 'error' with 'ret' throughout
- Addediqs624_pos_angle_enandiqs624_pos_angle_getto remove duplicate logic previously used throughout
- Refactored the logic iniqs624_pos_notifierand added a lock to safely evaluate variables that may change in response to user action
- Refactored the logic iniqs624_pos_read_raw
- Added a lock toiqs624_pos_read_event_configto account for cases in which the corresponding hardware state is in the process of being updated
- Refactored the logic iniqs624_pos_write_event_configand read the initial angle in case it changed since having first been read iniqs624_pos_init
- Removediqs624_pos_initas its logic has since been absorbed elsewhere
- Removeddevm_add_action_or_resetfailure message
- Eliminated tabbed alignment ofplatform_driverstruct members
- ChangedKconfig"depends on" logic toMFD_IQS62X||COMPILE_TESTdrivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/position/Kconfig | 19 +++ drivers/iio/position/Makefile | 7 + drivers/iio/position/iqs624-pos.c | 284 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 312 insertions(+) create mode 100644 drivers/iio/position/Kconfig create mode 100644 drivers/iio/position/Makefile create mode 100644 drivers/iio/position/iqs624-pos.cdrivers/iio/Kconfig +1
-0@@ -88,6 +88,7 @@ source "drivers/iio/orientation/Kconfig" if IIO_TRIGGER source "drivers/iio/trigger/Kconfig" endif #IIO_TRIGGER +source "drivers/iio/position/Kconfig" source "drivers/iio/potentiometer/Kconfig" source "drivers/iio/potentiostat/Kconfig" source "drivers/iio/pressure/Kconfig"drivers/iio/Makefile +1
-0@@ -31,6 +31,7 @@ obj-y += light/ obj-y += magnetometer/ obj-y += multiplexer/ obj-y += orientation/ +obj-y += position/ obj-y += potentiometer/ obj-y += potentiostat/ obj-y += pressure/drivers/iio/position/Kconfig new file +19
-0@@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Linear and angular position sensors +# +# When adding new entries keep the list in alphabetical order + +menu "Linear and angular position sensors" + +config IQS624_POS + tristate "Azoteq IQS624/625 angular position sensors" + depends on MFD_IQS62X || COMPILE_TEST + help + Say Y here if you want to build support for the Azoteq IQS624 + and IQS625 angular position sensors. + + To compile this driver as a module, choose M here: the module + will be called iqs624-pos. + +endmenudrivers/iio/position/Makefile new file +7
-0@@ -0,0 +1,7 @@ +# +# Makefile for IIO linear and angular position sensors +# + +# When adding new entries keep the list in alphabetical order + +obj-$(CONFIG_IQS624_POS) += iqs624-pos.odrivers/iio/position/iqs624-pos.c new file +279
-0@@ -0,0 +1,284 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Azoteq IQS624/625 Angular Position Sensors +* +* Copyright (C) 2019 Jeff LaBundy <[email protected]> +*/ + +#include <linux/device.h> +#include <linux/iio/events.h> +#include <linux/iio/iio.h> +#include <linux/kernel.h> +#include <linux/mfd/iqs62x.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/platform_device.h> +#include <linux/regmap.h> + +#define IQS624_POS_DEG_OUT 0x16 + +#define IQS624_POS_SCALE1 (314159 / 180) +#define IQS624_POS_SCALE2 100000 + +struct iqs624_pos_private { +struct iqs62x_core *iqs62x; +struct notifier_block notifier; +struct mutex lock; +bool angle_en; +u16 angle; +}; + +static int iqs624_pos_angle_en (struct iqs62x_core *iqs62x,bool angle_en) +{ +unsigned int event_mask =IQS624_HALL_UI_WHL_EVENT ; + +/* +* The IQS625 reports angular position in the form of coarse intervals, +* so only interval change events are unmasked. Conversely, the IQS624 +* reports angular position down to one degree of resolution, so wheel +* movement events are unmasked instead. +*/ +if (iqs62x->dev_desc->prod_num ==IQS625_PROD_NUM ) + event_mask =IQS624_HALL_UI_INT_EVENT ; + +return regmap_update_bits (iqs62x->map,IQS624_HALL_UI , event_mask, + angle_en ?0 :0xFF ); +} + +static int iqs624_pos_notifier (struct notifier_block *notifier, +unsigned long event_flags,void *context) +{ +struct iqs62x_event_data *event_data = context; +struct iqs624_pos_private *iqs624_pos; +struct iqs62x_core *iqs62x; +struct iio_dev *indio_dev; +u16 angle = event_data->ui_data; +s64 timestamp; +int ret; + + iqs624_pos = container_of(notifier, struct iqs624_pos_private, + notifier); + indio_dev =iio_priv_to_dev (iqs624_pos); + timestamp =iio_get_time_ns (indio_dev); + + iqs62x = iqs624_pos->iqs62x; +if (iqs62x->dev_desc->prod_num ==IQS625_PROD_NUM ) + angle = event_data->interval; + +mutex_lock (&iqs624_pos->lock); + +if (event_flags &BIT (IQS62X_EVENT_SYS_RESET )) { + ret =iqs624_pos_angle_en (iqs62x, iqs624_pos->angle_en); +if (ret) { +dev_err (indio_dev->dev.parent, +"Failed to re-initialize device: %d\n" , ret); + ret =NOTIFY_BAD ; + }else { + ret =NOTIFY_OK ; + } + }else if (iqs624_pos->angle_en && (angle != iqs624_pos->angle)) { +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_ANGL ,0 , +IIO_EV_TYPE_CHANGE , +IIO_EV_DIR_NONE ), + timestamp); + + iqs624_pos->angle = angle; + ret =NOTIFY_OK ; + }else { + ret =NOTIFY_DONE ; + } + +mutex_unlock (&iqs624_pos->lock); + +return ret; +} + +static void iqs624_pos_notifier_unregister (void *context) +{ +struct iqs624_pos_private *iqs624_pos = context; +struct iio_dev *indio_dev =iio_priv_to_dev (iqs624_pos); +int ret; + + ret =blocking_notifier_chain_unregister (&iqs624_pos->iqs62x->nh, + &iqs624_pos->notifier); +if (ret) +dev_err (indio_dev->dev.parent, +"Failed to unregister notifier: %d\n" , ret); +} + +static int iqs624_pos_angle_get (struct iqs62x_core *iqs62x,unsigned int *val) +{ +int ret; +__le16 val_buf; + +if (iqs62x->dev_desc->prod_num ==IQS625_PROD_NUM ) +return regmap_read (iqs62x->map, iqs62x->dev_desc->interval, + val); + + ret =regmap_raw_read (iqs62x->map,IQS624_POS_DEG_OUT , &val_buf, +sizeof (val_buf)); +if (ret) +return ret; + + *val =le16_to_cpu (val_buf); + +return 0 ; +} + +static int iqs624_pos_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct iqs624_pos_private *iqs624_pos =iio_priv (indio_dev); +struct iqs62x_core *iqs62x = iqs624_pos->iqs62x; +unsigned int scale =1 ; +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + ret =iqs624_pos_angle_get (iqs62x, val); +if (ret) +return ret; + +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SCALE : +if (iqs62x->dev_desc->prod_num ==IQS625_PROD_NUM ) { + ret =regmap_read (iqs62x->map,IQS624_INTERVAL_DIV , + &scale); +if (ret) +return ret; + } + + *val = scale *IQS624_POS_SCALE1 ; + *val2 =IQS624_POS_SCALE2 ; +return IIO_VAL_FRACTIONAL ; + +default : +return -EINVAL ; + } +} + +static int iqs624_pos_read_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir) +{ +struct iqs624_pos_private *iqs624_pos =iio_priv (indio_dev); +int ret; + +mutex_lock (&iqs624_pos->lock); + ret = iqs624_pos->angle_en; +mutex_unlock (&iqs624_pos->lock); + +return ret; +} + +static int iqs624_pos_write_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +int state) +{ +struct iqs624_pos_private *iqs624_pos =iio_priv (indio_dev); +struct iqs62x_core *iqs62x = iqs624_pos->iqs62x; +unsigned int val; +int ret; + +mutex_lock (&iqs624_pos->lock); + + ret =iqs624_pos_angle_get (iqs62x, &val); +if (ret) + goto err_mutex; + + ret =iqs624_pos_angle_en (iqs62x, state); +if (ret) + goto err_mutex; + + iqs624_pos->angle = val; + iqs624_pos->angle_en = state; + +err_mutex: +mutex_unlock (&iqs624_pos->lock); + +return ret; +} + +static const struct iio_info iqs624_pos_info = { + .read_raw = &iqs624_pos_read_raw, + .read_event_config = iqs624_pos_read_event_config, + .write_event_config = iqs624_pos_write_event_config, +}; + +static const struct iio_event_spec iqs624_pos_events[] = { + { + .type =IIO_EV_TYPE_CHANGE , + .dir =IIO_EV_DIR_NONE , + .mask_separate =BIT (IIO_EV_INFO_ENABLE ), + }, +}; + +static const struct iio_chan_spec iqs624_pos_channels[] = { + { + .type =IIO_ANGL , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ), + .event_spec = iqs624_pos_events, + .num_event_specs =ARRAY_SIZE (iqs624_pos_events), + }, +}; + +static int iqs624_pos_probe (struct platform_device *pdev) +{ +struct iqs62x_core *iqs62x =dev_get_drvdata (pdev->dev.parent); +struct iqs624_pos_private *iqs624_pos; +struct iio_dev *indio_dev; +int ret; + + indio_dev =devm_iio_device_alloc (&pdev->dev,sizeof (*iqs624_pos)); +if (!indio_dev) +return -ENOMEM ; + + iqs624_pos =iio_priv (indio_dev); + iqs624_pos->iqs62x = iqs62x; + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->dev.parent = &pdev->dev; + indio_dev->channels = iqs624_pos_channels; + indio_dev->num_channels =ARRAY_SIZE (iqs624_pos_channels); + indio_dev->name = iqs62x->dev_desc->dev_name; + indio_dev->info = &iqs624_pos_info; + +mutex_init (&iqs624_pos->lock); + + iqs624_pos->notifier.notifier_call = iqs624_pos_notifier; + ret =blocking_notifier_chain_register (&iqs624_pos->iqs62x->nh, + &iqs624_pos->notifier); +if (ret) { +dev_err (&pdev->dev,"Failed to register notifier: %d\n" , ret); +return ret; + } + + ret =devm_add_action_or_reset (&pdev->dev, + iqs624_pos_notifier_unregister, + iqs624_pos); +if (ret) +return ret; + +return devm_iio_device_register (&pdev->dev, indio_dev); +} + +static struct platform_driver iqs624_pos_platform_driver = { + .driver = { + .name =IQS624_DRV_NAME_POS , + }, + .probe = iqs624_pos_probe, +}; +module_platform_driver (iqs624_pos_platform_driver);
Quoted 9 lines
Quoted 8 lines
drivers/iio/position/iqs624-pos.c new file +5
-1+ +MODULE_AUTHOR ("Jeff LaBundy <[email protected]>" ); +MODULE_DESCRIPTION ("Azoteq IQS624/625 Angular Position Sensors" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:" IQS624_DRV_NAME_POS ); --2.7.4
Wishing you a Happy New Year,
Jeff LaBundy
Re: [PATCH v2 7/7] iio: position: Add support for Azoteq IQS624/625 angle sensors
Quoted 17 lines · Jeff LaBundy
Hi Jonathan,
Thank you for your continued support on this project.
Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Mon, 9 Dec 2019 00:38:41 +0000 Jeff LaBundy <[email protected]> wrote:
This patch adds support for the Azoteq IQS624 and IQS625 angular position sensors, capable of reporting the angle of a rotating shaft down to 1 and 10 degrees of accuracy, respectively.
This patch also introduces a home for linear and angular position sensors. Unlike resolvers, they are typically contactless and use the Hall effect.
Signed-off-by Jeff LaBundy <[email protected]>
Quoted 8 lines
Quoted 6 lines
Looks good
Reviewed-by Jonathan Cameron <[email protected]>My current assumption is that Lee will take this lot via an immutable branch in MFD once it's ready. Shout if a different path makes sense.
Same here. @Lee, please let us know if you disagree.
That's fine.
Signature · 4 lines
Lee Jones [李琼斯] Linaro Services Technical Lead Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog