Skip to contentSkip to conversation

Add support for Azoteq IQS620A/621/622/624/625

PATCH Superseded Jeff LaBundy 2 participants 12 messages Jan 6, 2020 – Jan 16, 2020

Subsystems (6)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 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     | 132 +++++
 Documentation/devicetree/bindings/mfd/iqs62x.yaml  | 179 ++++++
 .../devicetree/bindings/pwm/iqs620a-pwm.yaml       |  32 ++
 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                          | 254 ++++++++
 include/linux/mfd/iqs62x.h                         | 146 +++++
 25 files changed, 3243 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

#1 lore raw

PATCH v3 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 v3:
- Specified 'additionalProperties: false' within the parent MFD node and all child nodes ("keys", "hall-switch-north/south" and "pwm")
- Defined the "hall-switch-north/south" child nodes unconditionally and then inverted the subsequent if/then to filter them from devices for which that functionality is unavailable
- Added Reviewed-by trailer

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     | 132 +++++++++++++++
 Documentation/devicetree/bindings/mfd/iqs62x.yaml  | 179 +++++++++++++++++++++
 .../devicetree/bindings/pwm/iqs620a-pwm.yaml       |  32 ++++
 3 files changed, 343 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 +132 -0
new file mode 100644
index 0000000..5625c22
@@ -0,0 +1,132 @@
+# 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.
+
+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
+
+    additionalProperties: false
+
+if:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - azoteq,iqs624-keys
+          - azoteq,iqs625-keys
+then:
+  patternProperties:
+    "^hall-switch-(north|south)$": false
+
+required:
+  - compatible
+  - linux,keycodes
+
+additionalProperties: false
+
+...
Documentation/devicetree/bindings/mfd/iqs62x.yaml new file +179 -0

179 lines not on this page · raw message

Documentation/devicetree/bindings/pwm/iqs620a-pwm.yaml new file +32 -0
new file mode 100644
index 0000000..1d7c27b
@@ -0,0 +1,32 @@
+# 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"
+
+additionalProperties: false
+
+...

--
2.7.4

#2 lore raw

PATCH v3 2/7 mfd: Add support for Azoteq IQS620A/621/622/624/625

In reply to Jeff LaBundy

This patch adds core support for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.

---
Changes in v3:
- None

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
index 4209008..151984c 100644
@@ -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
index aed99f0..c4fc26b 100644
@@ -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

#3 lore raw

PATCH v3 3/7 input: keyboard: Add support for Azoteq IQS620A/621/622/624/625

In reply to Jeff LaBundy

This patch adds key and switch support for the Azoteq IQS620A, IQS621, IQS622, IQS624 and IQS625 multi-function sensors.

---
Changes in v3:
- None

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
index 4706ff0..28de965 100644
@@ -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
index f5b1752..1d689fd 100644
@@ -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

#4 lore raw

PATCH v3 4/7 pwm: Add support for Azoteq IQS620A PWM generator

In reply to 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).

---
Changes in v3:
- Updated the commit message to say "~0.4%" instead of "0.4%"
- Clarified the effect of duty cycle and state changes in the 'Limitations' section and added a restriction regarding 0% duty cycle
- Added a comment in iqs620_pwm_apply to explain how duty cycle is derived
- Updated iqs620_pwm_apply to disable the output first and enable it last to prevent temporarily driving a stale duty cycle
- Rounded the calculation for duty cycle up and down in iqs620_pwm_get_state and iqs620_pwm_apply, respectively
- Added a comment in iqs620_pwm_get_state to explain what it reports follow- ing requests to set duty cycle to 0%
- Added a lock to prevent back-to-back access of IQS620_PWR_SETTINGS_PWM_OUT and IQS620_PWM_DUTY_CYCLE from being interrupted
- Updated iqs620_pwm_notifier to reference pwm->state directly as opposed to calling pwm_get_state
- Moved notifier unregistration back to a device-managed action
- Added a completion to prevent iqs620_pwm_notifier from referencing the pwm_chip structure until it has been initialized by pwmchip_add

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 | 254 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 265 insertions(+)
 create mode 100644 drivers/pwm/pwm-iqs620a.c
drivers/pwm/Kconfig +10 -0
index bd21655..60bcf6c 100644
@@ -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
index 9a47507..a59c710 100644
@@ -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 +254 -0

254 lines not on this page · raw message

--
2.7.4

#5 lore raw

Re: [PATCH v3 4/7] pwm: Add support for Azoteq IQS620A PWM generator

Hello Jeff,

Quoted 2 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).

Overall a very nice driver, some minor issues below.

Quoted 146 lines

---
Changes in v3:
- Updated the commit message to say "~0.4%" instead of "0.4%"
- Clarified the effect of duty cycle and state changes in the 'Limitations' section and added a restriction regarding 0% duty cycle
- Added a comment in iqs620_pwm_apply to explain how duty cycle is derived
- Updated iqs620_pwm_apply to disable the output first and enable it last to prevent temporarily driving a stale duty cycle
- Rounded the calculation for duty cycle up and down in iqs620_pwm_get_state and iqs620_pwm_apply, respectively
- Added a comment in iqs620_pwm_get_state to explain what it reports follow- ing requests to set duty cycle to 0%
- Added a lock to prevent back-to-back access of IQS620_PWR_SETTINGS_PWM_OUT and IQS620_PWM_DUTY_CYCLE from being interrupted
- Updated iqs620_pwm_notifier to reference pwm->state directly as opposed to calling pwm_get_state
- Moved notifier unregistration back to a device-managed action
- Added a completion to prevent iqs620_pwm_notifier from referencing the pwm_chip structure until it has been initialized by pwmchip_add

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 | 254 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 265 insertions(+)
 create mode 100644 drivers/pwm/pwm-iqs620a.c
drivers/pwm/Kconfig +10 -0
index bd21655..60bcf6c 100644
@@ -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
index 9a47507..a59c710 100644
@@ -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 +64 -0
new file mode 100644
index 0000000..ee5d8b5
@@ -0,0 +1,254 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Azoteq IQS620A PWM Generator
+ *
+ * Copyright (C) 2019 Jeff LaBundy <[email protected]>
+ *
+ * Limitations:
+ * - 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.
+ * - 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.
+ */
+
+#include <linux/completion.h>
+#include <linux/device.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/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;
+	struct completion chip_ready;
+	struct mutex lock;
+};
+
+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:
+	 *

drivers/pwm/pwm-iqs620a.c line 74

Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10 -0
+	 * 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;

minor optimisation: You could do the division before grabbing the lock. (But unsure if this actually gives a better result or the compiler is clever enough to do this.)

Quoted 53 lines
drivers/pwm/pwm-iqs620a.c new file +53 -0
+
+	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;
+}
+
+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);
+
+	/*
+	 * Since the device cannot generate a 0% duty cycle, requests to do so
+	 * cause 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.
+	 */
+	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;

drivers/pwm/pwm-iqs620a.c line 137

Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10 -0
+	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);
+}
+

I thought we dicussed having a comment here, saying something like:

The device might reset when [...] and as a result looses it's configuration. So the registers must be rewritten when this happens to restore the expected operation.

Is it worth to issue a warning when this happens?

drivers/pwm/pwm-iqs620a.c line 149

Quoted 12 lines
drivers/pwm/pwm-iqs620a.c new file +12 -0
+static int iqs620_pwm_notifier(struct notifier_block *notifier,
+			       unsigned long event_flags, void *context)
+{
+	struct iqs620_pwm_private *iqs620_pwm;
+	int ret;
+
+	iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
+				  notifier);
+
+	if (!completion_done(&iqs620_pwm->chip_ready) ||
+	    !(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
+		return NOTIFY_DONE;

Is here a (relevant?) race? Consider the notifier triggers just when pwmchip_add returned, (maybe even a consumer configured the device) and before complete_all() is called. With my limited knowledge about notifiers I'd say waiting for the completion here might be reasonable and safe.

Quoted 17 lines
drivers/pwm/pwm-iqs620a.c new file +17 -0
+	ret = iqs620_pwm_apply(&iqs620_pwm->chip, &iqs620_pwm->chip.pwms[0],
+			       &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,
+};
+

drivers/pwm/pwm-iqs620a.c line 177

Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10 -0
+static void iqs620_pwm_notifier_unregister(void *context)
+{
+	struct iqs620_pwm_private *iqs620_pwm = context;
+	int ret;
+
+	ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
+						 &iqs620_pwm->notifier);
+	if (ret)
+		dev_err(iqs620_pwm->chip.dev,
+			"Failed to unregister notifier: %d\n", ret);
	dev_err(iqs620_pwm->chip.dev,
		"Failed to unregister notifier: %pe\n", ERR_PTR(ret));

gives a nicer output. (Also applies to other error messages of course.)

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#6 lore raw

Re: [PATCH v3 4/7] pwm: Add support for Azoteq IQS620A PWM generator

Hi Uwe,

Thank you for your kind words and thorough review.

Quoted 160 lines · Uwe Kleine-König

Hello Jeff,

Quoted 2 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).

Overall a very nice driver, some minor issues below.

Quoted 152 lines

---
Changes in v3:
- Updated the commit message to say "~0.4%" instead of "0.4%"
- Clarified the effect of duty cycle and state changes in the 'Limitations' section and added a restriction regarding 0% duty cycle
- Added a comment in iqs620_pwm_apply to explain how duty cycle is derived
- Updated iqs620_pwm_apply to disable the output first and enable it last to prevent temporarily driving a stale duty cycle
- Rounded the calculation for duty cycle up and down in iqs620_pwm_get_state and iqs620_pwm_apply, respectively
- Added a comment in iqs620_pwm_get_state to explain what it reports follow- ing requests to set duty cycle to 0%
- Added a lock to prevent back-to-back access of IQS620_PWR_SETTINGS_PWM_OUT and IQS620_PWM_DUTY_CYCLE from being interrupted
- Updated iqs620_pwm_notifier to reference pwm->state directly as opposed to calling pwm_get_state
- Moved notifier unregistration back to a device-managed action
- Added a completion to prevent iqs620_pwm_notifier from referencing the pwm_chip structure until it has been initialized by pwmchip_add

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 | 254 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 265 insertions(+)
 create mode 100644 drivers/pwm/pwm-iqs620a.c
drivers/pwm/Kconfig +10 -0
index bd21655..60bcf6c 100644
@@ -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
index 9a47507..a59c710 100644
@@ -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 +70 -0
new file mode 100644
index 0000000..ee5d8b5
@@ -0,0 +1,254 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Azoteq IQS620A PWM Generator
+ *
+ * Copyright (C) 2019 Jeff LaBundy <[email protected]>
+ *
+ * Limitations:
+ * - 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.
+ * - 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.
+ */
+
+#include <linux/completion.h>
+#include <linux/device.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/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;
+	struct completion chip_ready;
+	struct mutex lock;
+};
+
+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.
+	 *
Quoted 9 lines

drivers/pwm/pwm-iqs620a.c line 74

Quoted 4 lines
drivers/pwm/pwm-iqs620a.c new file +4 -0
+	 * 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;

minor optimisation: You could do the division before grabbing the lock. (But unsure if this actually gives a better result or the compiler is clever enough to do this.)

That's a great point. Regardless of the result I think it is better to re-order as you suggest, simply to keep with the idea of locking around only the minimum necessary code.

Quoted 66 lines
Quoted 53 lines
drivers/pwm/pwm-iqs620a.c new file +53 -0
+
+	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;
+}
+
+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);
+
+	/*
+	 * Since the device cannot generate a 0% duty cycle, requests to do so
+	 * cause 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.
+	 */
+	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;

drivers/pwm/pwm-iqs620a.c line 137

Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10 -0
+	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);
+}
+

I thought we dicussed having a comment here, saying something like:

Quoted 6 lines

The device might reset when [...] and as a result looses it's configuration. So the registers must be rewritten when this happens to restore the expected operation.

Is it worth to issue a warning when this happens?

The detailed comments and an error message have always been in iqs62x_irq of the parent MFD driver. The pwm is only one of up to three sub-devices that subscribe to the chain and must update their locally owned registers after the MFD handles the interrupt and restores the device's firmware. I opted to keep these comments in the common MFD rather than repeating throughout the series.

Quoted 10 lines
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10 -0
+static int iqs620_pwm_notifier(struct notifier_block *notifier,
+			       unsigned long event_flags, void *context)
+{
+	struct iqs620_pwm_private *iqs620_pwm;
+	int ret;
+
+	iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
+				  notifier);
+
+	if (!completion_done(&iqs620_pwm->chip_ready) ||
Quoted 9 lines

drivers/pwm/pwm-iqs620a.c line 149

Quoted 2 lines
drivers/pwm/pwm-iqs620a.c new file +2 -0
+	    !(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
+		return NOTIFY_DONE;

Is here a (relevant?) race? Consider the notifier triggers just when pwmchip_add returned, (maybe even a consumer configured the device) and before complete_all() is called. With my limited knowledge about notifiers I'd say waiting for the completion here might be reasonable and safe.

Great catch; this is theoretically possible. The problem with waiting, however, is if the notifier is triggered right away during probe but probe returns early due to an error (and completion never happens).

At this point, I think the best option is to simply cache the values written to IQS620_PWR_SETTINGS_PWM_OUT and IQS620_PWM_DUTY_CYCLE and restore them from the notifier, which is essentially what is done for the IIO drivers in this series.

This simple solution avoids messy tear-down paths and race conditions resulting from trying to piggy-back chip->pwms[0] before it may be available. As an added bonus it also alleviates any register access from get_state, which could simply use the cached values to return the quantized duty cycle.

Quoted 31 lines
Quoted 17 lines
drivers/pwm/pwm-iqs620a.c new file +17 -0
+	ret = iqs620_pwm_apply(&iqs620_pwm->chip, &iqs620_pwm->chip.pwms[0],
+			       &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,
+};
+

drivers/pwm/pwm-iqs620a.c line 177

Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10 -0
+static void iqs620_pwm_notifier_unregister(void *context)
+{
+	struct iqs620_pwm_private *iqs620_pwm = context;
+	int ret;
+
+	ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
+						 &iqs620_pwm->notifier);
+	if (ret)
+		dev_err(iqs620_pwm->chip.dev,
+			"Failed to unregister notifier: %d\n", ret);
	dev_err(iqs620_pwm->chip.dev,
		"Failed to unregister notifier: %pe\n", ERR_PTR(ret));
Quoted 2 lines

gives a nicer output. (Also applies to other error messages of course.)

I don't disagree, but this gives me some pause. If I made this change here, I'd prefer to do so across the series for consistency. However, I am hesitant to do so at this stage in the review since several patches are somewhat stable by now (unless there was a compelling reason from a functional perspective).

Another reason is that there are many dev_err cases throughout this series, and adopting this very recently introduced functionality would make the series even harder to back port to the present lot of LTS kernels.

Unless this is a deal breaker, I'd like to pass on this for v4. However, please let me know if you feel strongly about it. In the meantime, I'll get started on the couple of other changes discussed here.

Quoted 6 lines

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

Kind regards,
Jeff LaBundy

#7 lore raw

Re: [PATCH v3 4/7] pwm: Add support for Azoteq IQS620A PWM generator

Hello Jeff,

Quoted 1 line · Jeff LaBundy

Thank you for your kind words and thorough review.

Great my feedback is welcome.

Quoted 8 lines · Uwe Kleine-König
Quoted 7 lines · Jeff LaBundy

I thought we dicussed having a comment here, saying something like:

The device might reset when [...] and as a result looses it's configuration. So the registers must be rewritten when this happens to restore the expected operation.

Quoted 7 lines
Quoted 1 line

Is it worth to issue a warning when this happens?

The detailed comments and an error message have always been in iqs62x_irq of the parent MFD driver. The pwm is only one of up to three sub-devices that subscribe to the chain and must update their locally owned registers after the MFD handles the interrupt and restores the device's firmware. I opted to keep these comments in the common MFD rather than repeating throughout the series.

That's fine then, a comment that the parent driver issues a message would be great then.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/pwm/pwm-iqs620a.c new file +10 -0
+static int iqs620_pwm_notifier(struct notifier_block *notifier,
+			       unsigned long event_flags, void *context)
+{
+	struct iqs620_pwm_private *iqs620_pwm;
+	int ret;
+
+	iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
+				  notifier);
+
+	if (!completion_done(&iqs620_pwm->chip_ready) ||
Quoted 8 lines

drivers/pwm/pwm-iqs620a.c line 149

Quoted 2 lines
drivers/pwm/pwm-iqs620a.c new file +2 -0
+	    !(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
+		return NOTIFY_DONE;

Is here a (relevant?) race? Consider the notifier triggers just when pwmchip_add returned, (maybe even a consumer configured the device) and before complete_all() is called. With my limited knowledge about notifiers I'd say waiting for the completion here might be reasonable and safe.

Quoted 3 lines

Great catch; this is theoretically possible. The problem with waiting, however, is if the notifier is triggered right away during probe but probe returns early due to an error (and completion never happens).

OK, the error path would need to complete .chip_ready then and the notifier then check for this error. Indeed messy.

Quoted 3 lines

At this point, I think the best option is to simply cache the values written to IQS620_PWR_SETTINGS_PWM_OUT and IQS620_PWM_DUTY_CYCLE and restore them from the notifier, which is essentially what is done for the IIO drivers in this series.

Sounds good.

Quoted 17 lines
Quoted 11 lines

drivers/pwm/pwm-iqs620a.c line 177

Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5 -0
+	ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
+						 &iqs620_pwm->notifier);
+	if (ret)
+		dev_err(iqs620_pwm->chip.dev,
+			"Failed to unregister notifier: %d\n", ret);
	dev_err(iqs620_pwm->chip.dev,
		"Failed to unregister notifier: %pe\n", ERR_PTR(ret));

gives a nicer output. (Also applies to other error messages of course.)

I don't disagree, but this gives me some pause. If I made this change here, I'd prefer to do so across the series for consistency. However, I am hesitant to do so at this stage in the review since several patches are somewhat stable by now (unless there was a compelling reason from a functional perspective).

Quoted 7 lines

Another reason is that there are many dev_err cases throughout this series, and adopting this very recently introduced functionality would make the series even harder to back port to the present lot of LTS kernels.

Unless this is a deal breaker, I'd like to pass on this for v4. However, please let me know if you feel strongly about it. In the meantime, I'll get started on the couple of other changes discussed here.

OK, being able to backport is a valid excuse. Consistency over the whole series wouldn't be one of my reasons, your mileage obviously varies (which is OK).

This can also be done later. Conversion to this is on my todo-list (not at the top though), but if you beat me to it, I won't be angry :-)

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#8 lore raw

Re: [PATCH v3 4/7] pwm: Add support for Azoteq IQS620A PWM generator

Hi Uwe,

Quoted 17 lines · Uwe Kleine-König

Hello Jeff,

Quoted 1 line · Jeff LaBundy

Thank you for your kind words and thorough review.

Great my feedback is welcome.

Quoted 10 lines · Uwe Kleine-König
Quoted 8 lines · Jeff LaBundy

I thought we dicussed having a comment here, saying something like:

The device might reset when [...] and as a result looses it's configuration. So the registers must be rewritten when this happens to restore the expected operation.

Is it worth to issue a warning when this happens?

Quoted 8 lines
Quoted 5 lines

The detailed comments and an error message have always been in iqs62x_irq of the parent MFD driver. The pwm is only one of up to three sub-devices that subscribe to the chain and must update their locally owned registers after the MFD handles the interrupt and restores the device's firmware. I opted to keep these comments in the common MFD rather than repeating throughout the series.

That's fine then, a comment that the parent driver issues a message would be great then.

Sure thing, will do.

Quoted 58 lines
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/pwm/pwm-iqs620a.c new file +10 -0
+static int iqs620_pwm_notifier(struct notifier_block *notifier,
+			       unsigned long event_flags, void *context)
+{
+	struct iqs620_pwm_private *iqs620_pwm;
+	int ret;
+
+	iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
+				  notifier);
+
+	if (!completion_done(&iqs620_pwm->chip_ready) ||
Quoted 8 lines

drivers/pwm/pwm-iqs620a.c line 149

Quoted 2 lines
drivers/pwm/pwm-iqs620a.c new file +2 -0
+	    !(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
+		return NOTIFY_DONE;

Is here a (relevant?) race? Consider the notifier triggers just when pwmchip_add returned, (maybe even a consumer configured the device) and before complete_all() is called. With my limited knowledge about notifiers I'd say waiting for the completion here might be reasonable and safe.

Quoted 3 lines

Great catch; this is theoretically possible. The problem with waiting, however, is if the notifier is triggered right away during probe but probe returns early due to an error (and completion never happens).

OK, the error path would need to complete .chip_ready then and the notifier then check for this error. Indeed messy.

Quoted 3 lines

At this point, I think the best option is to simply cache the values written to IQS620_PWR_SETTINGS_PWM_OUT and IQS620_PWM_DUTY_CYCLE and restore them from the notifier, which is essentially what is done for the IIO drivers in this series.

Sounds good.

Quoted 17 lines
Quoted 11 lines

drivers/pwm/pwm-iqs620a.c line 177

Quoted 5 lines
drivers/pwm/pwm-iqs620a.c new file +5 -0
+	ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
+						 &iqs620_pwm->notifier);
+	if (ret)
+		dev_err(iqs620_pwm->chip.dev,
+			"Failed to unregister notifier: %d\n", ret);
	dev_err(iqs620_pwm->chip.dev,
		"Failed to unregister notifier: %pe\n", ERR_PTR(ret));

gives a nicer output. (Also applies to other error messages of course.)

I don't disagree, but this gives me some pause. If I made this change here, I'd prefer to do so across the series for consistency. However, I am hesitant to do so at this stage in the review since several patches are somewhat stable by now (unless there was a compelling reason from a functional perspective).

Quoted 7 lines

Another reason is that there are many dev_err cases throughout this series, and adopting this very recently introduced functionality would make the series even harder to back port to the present lot of LTS kernels.

Unless this is a deal breaker, I'd like to pass on this for v4. However, please let me know if you feel strongly about it. In the meantime, I'll get started on the couple of other changes discussed here.

Quoted 7 lines

OK, being able to backport is a valid excuse. Consistency over the whole series wouldn't be one of my reasons, your mileage obviously varies (which is OK).

This can also be done later. Conversion to this is on my todo-list (not at the top though), but if you beat me to it, I won't be angry :-)

Thank you for your understanding.

Quoted 6 lines

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

I'll send out v4 in the next day or so after I finish some testing.

Kind regards,
Jeff LaBundy

#9 lore raw

PATCH v3 5/7 iio: temperature: Add support for Azoteq IQS620AT temperature sensor

In reply to Jeff LaBundy

This patch adds support for the Azoteq IQS620AT temperature sensor, capable of reporting its absolute die temperature.

---
Changes in v3:
- Added Reviewed-by trailer

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
index e1ccb40..f1f2a14 100644
@@ -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
index d6b850b..90c1131 100644
@@ -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

#10 lore raw

PATCH v3 6/7 iio: light: Add support for Azoteq IQS621/622 ambient light sensors

In reply to Jeff LaBundy

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.

---
Changes in v3:
- Added Reviewed-by trailer

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
index 9968f98..baf7958b 100644
@@ -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
index c98d1ce..988e8f4 100644
@@ -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

#11 lore raw

PATCH v3 7/7 iio: position: Add support for Azoteq IQS624/625 angle sensors

In reply to Jeff LaBundy

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.

---
Changes in v3:
- Added Reviewed-by trailer

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
index 5bd5185..d5c073a 100644
@@ -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
index bff682a..1712011 100644
@@ -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
new file mode 100644
index 0000000..eda67f0
@@ -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
new file mode 100644
index 0000000..3cbe7a7
@@ -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