GPIO bulk changes for the v5.10 kernel cycle:

Core changes:
 
 - The big core change is the updated (v2) userspace character
   device API. This corrects badly designed 64-bit alignment around
   the line events. We also add the debounce request feature.
   This echoes the often quotes passage from Frederick Brooks
   "The mythical man-month" to always throw one away, which we
   have seen before in things such as V4L2. So we put in a new
   one and deprecate and obsolete the old one.
 
 - All example tools in tools/gpio/* are migrated to the new API
   to set a good example. The libgpiod userspace library has been
   augmented to use this new API pretty much from day 1.
 
 - Some misc API hardening by using strn* function calls has been
   added as well.
 
 - Use the simpler IDA interface for GPIO chip instance enumeration.
 
 - Add device core function for counting string arrays in
   device properties.
 
 - Provide a generic library function kfree_strarray() that can
   be used throughout the kernel.
 
 Driver enhancements:
 
 - The DesignWare dwapb-gpio driver has been enhanced and now
   uses the IRQ handling in the gpiolib core.
 
 - The mockup and aggregator drivers have seen some substantial
   code clean-up and now use more of the core kernel
   inftrastructure.
 
 - Misc cleanups using dev_err_probe().
 
 - The MXC drivers (Freescale/NXP) can now be built modularized,
   which makes modularized GKI Android kernels happy.
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCAAdFiEElDRnuGcz/wPCXQWMQRCzN7AZXXMFAl+FdjkACgkQQRCzN7AZ
 XXMYgQ/+JgpHrp7yS1IkS1KiAxHdeIGnKzloTCQQo1JxYEymAnIeMwo/iWAk5wHu
 NeJIEVxD0YzZwoI3BXbnO5Qy/62g1z7Ik8ToIa0TiFMwYxz5a7lqsiHwpBgHa50h
 T2N8FRFdslVrhpUYBH4Q9wlfYxTki4FwdTD6aaoFFGcMwIVJXWyaYzE+o+qEUEne
 VaPsGoNhRKTdKASP3c6+zbbPonzpZW7s/wvIBQAyBgPxEizlL97RzzX3bSSraoCX
 i0NsDLHMe+9twqE064KN+CYu0Cy80etQSQsYcfnstVshMuY9+WC1YdyJqzYMciuQ
 CYUIQBeskft86IBlsEU/fNCbV+FeAgrxRW6TJK7Hn+sUWZ5+UGdpJ03UE1hA3jjO
 SniwG0vpqvZIkio49B6h51VdjNqVJn+AE8tN3hCzqpFknblXgJOVysD7RS7rNM6D
 flV1bCsUYtC6jN43qsGFiRYLE9ml2iUxFFoBQUaAEh+pXgUzPTQqD7aSjyzmE3x2
 uapKXgxN0dCNH+tFXij73Ro4bYf4ZTZhx3Z3XoEUNEyJpl8fE1bv1SZ2EykOmK8g
 c78fAmT0vG3xYZvK10WZj4zuHV6GlPAYVm/MlhB7QHsrF3wa9vervOuqhEPmp2th
 hTsVj/Zlz0SSDLncMQL64B7gbxOmzOYlVRxIkSrDEXUOFU7kiWE=
 =8CE2
 -----END PGP SIGNATURE-----

Merge tag 'gpio-v5.10-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio

Pull GPIO updates from Linus Walleij:
 "This time very little driver changes but lots of core changes.

  We have some interesting cooperative work for ARM and Intel alike,
  making the GPIO subsystem more and more suitable for industrial
  systems and the like, in addition to the in-kernel users.

  We touch driver core (device properties) and lib/* by adding one
  simple string array free function, these are authored by Andy
  Shevchenko who is a well known and recognized core helpers maintainers
  so this should be fine.

  We also see some Android GKI-related modularization in the MXC
  drivers.

  Core changes:

   - The big core change is the updated (v2) userspace character device
     API.

     This corrects badly designed 64-bit alignment around the line
     events. We also add the debounce request feature. This echoes the
     often quotes passage from Frederick Brooks "The mythical man-month"
     to always throw one away, which we have seen before in things such
     as V4L2. So we put in a new one and deprecate and obsolete the old
     one.

   - All example tools in tools/gpio/* are migrated to the new API to
     set a good example. The libgpiod userspace library has been
     augmented to use this new API pretty much from day 1.

   - Some misc API hardening by using strn* function calls has been
     added as well.

   - Use the simpler IDA interface for GPIO chip instance enumeration.

   - Add device core function for counting string arrays in device
     properties.

   - Provide a generic library function kfree_strarray() that can be
     used throughout the kernel.

  Driver enhancements:

   - The DesignWare dwapb-gpio driver has been enhanced and now uses the
     IRQ handling in the gpiolib core.

   - The mockup and aggregator drivers have seen some substantial code
     clean-up and now use more of the core kernel inftrastructure.

   - Misc cleanups using dev_err_probe().

   - The MXC drivers (Freescale/NXP) can now be built modularized, which
     makes modularized GKI Android kernels happy"

* tag 'gpio-v5.10-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio: (73 commits)
  gpiolib: Update header block in gpiolib-cdev.h
  gpiolib: cdev: switch from kstrdup() to kstrndup()
  docs: gpio: add a new document to its index.rst
  gpio: pca953x: Add support for the NXP PCAL9554B/C
  tools: gpio: add debounce support to gpio-event-mon
  tools: gpio: add multi-line monitoring to gpio-event-mon
  tools: gpio: port gpio-event-mon to v2 uAPI
  tools: gpio: port gpio-hammer to v2 uAPI
  tools: gpio: rename nlines to num_lines
  tools: gpio: port gpio-watch to v2 uAPI
  tools: gpio: port lsgpio to v2 uAPI
  gpio: uapi: document uAPI v1 as deprecated
  gpiolib: cdev: support setting debounce
  gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
  gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
  gpiolib: cdev: support edge detection for uAPI v2
  gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
  gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
  gpiolib: add build option for CDEV v1 ABI
  gpiolib: make cdev a build option
  ...
This commit is contained in:
Linus Torvalds 2020-10-13 10:09:33 -07:00
commit 0486beaf88
46 changed files with 2848 additions and 824 deletions

View File

@ -0,0 +1,50 @@
.. SPDX-License-Identifier: GPL-2.0-only
GPIO Testing Driver
===================
The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
chips for testing purposes. The lines exposed by these chips can be accessed
using the standard GPIO character device interface as well as manipulated
using the dedicated debugfs directory structure.
Creating simulated chips using module params
--------------------------------------------
When loading the gpio-mockup driver a number of parameters can be passed to the
module.
gpio_mockup_ranges
This parameter takes an argument in the form of an array of integer
pairs. Each pair defines the base GPIO number (if any) and the number
of lines exposed by the chip. If the base GPIO is -1, the gpiolib
will assign it automatically.
Example: gpio_mockup_ranges=-1,8,-1,16,405,4
The line above creates three chips. The first one will expose 8 lines,
the second 16 and the third 4. The base GPIO for the third chip is set
to 405 while for two first chips it will be assigned automatically.
gpio_named_lines
This parameter doesn't take any arguments. It lets the driver know that
GPIO lines exposed by it should be named.
The name format is: gpio-mockup-X-Y where X is mockup chip's ID
and Y is the line offset.
Manipulating simulated lines
----------------------------
Each mockup chip creates its own subdirectory in /sys/kernel/debug/gpio-mockup/.
The directory is named after the chip's label. A symlink is also created, named
after the chip's name, which points to the label directory.
Inside each subdirectory, there's a separate attribute for each GPIO line. The
name of the attribute represents the line's offset in the chip.
Reading from a line attribute returns the current value. Writing to it (0 or 1)
changes the configuration of the simulated pull-up/pull-down resistor
(1 - pull-up, 0 - pull-down).

View File

@ -9,6 +9,7 @@ gpio
gpio-aggregator
sysfs
gpio-mockup
.. only:: subproject and html

View File

@ -11,12 +11,33 @@ maintainers:
properties:
compatible:
enum:
- fsl,imx1-gpio
- fsl,imx21-gpio
- fsl,imx31-gpio
- fsl,imx35-gpio
- fsl,imx7d-gpio
oneOf:
- enum:
- fsl,imx1-gpio
- fsl,imx21-gpio
- fsl,imx31-gpio
- fsl,imx35-gpio
- fsl,imx7d-gpio
- items:
- const: fsl,imx35-gpio
- const: fsl,imx31-gpio
- items:
- enum:
- fsl,imx50-gpio
- fsl,imx51-gpio
- fsl,imx53-gpio
- fsl,imx6q-gpio
- fsl,imx6sl-gpio
- fsl,imx6sll-gpio
- fsl,imx6sx-gpio
- fsl,imx6ul-gpio
- fsl,imx7d-gpio
- fsl,imx8mm-gpio
- fsl,imx8mn-gpio
- fsl,imx8mp-gpio
- fsl,imx8mq-gpio
- fsl,imx8qxp-gpio
- const: fsl,imx35-gpio
reg:
maxItems: 1
@ -41,6 +62,28 @@ properties:
const: 2
gpio-controller: true
gpio-line-names: true
gpio-ranges: true
power-domains:
maxItems: 1
patternProperties:
"^(hog-[0-9]+|.+-hog(-[0-9]+)?)$":
type: object
properties:
gpio-hog: true
gpios: true
input: true
output-high: true
output-low: true
line-name: true
required:
- gpio-hog
- gpios
additionalProperties: false
required:
- compatible

View File

@ -1,58 +0,0 @@
* MAX732x-compatible I/O expanders
Required properties:
- compatible: Should be one of the following:
- "maxim,max7319": For the Maxim MAX7319
- "maxim,max7320": For the Maxim MAX7320
- "maxim,max7321": For the Maxim MAX7321
- "maxim,max7322": For the Maxim MAX7322
- "maxim,max7323": For the Maxim MAX7323
- "maxim,max7324": For the Maxim MAX7324
- "maxim,max7325": For the Maxim MAX7325
- "maxim,max7326": For the Maxim MAX7326
- "maxim,max7327": For the Maxim MAX7327
- reg: I2C slave address for this device.
- gpio-controller: Marks the device node as a GPIO controller.
- #gpio-cells: Should be 2.
- first cell is the GPIO number
- second cell specifies GPIO flags, as defined in <dt-bindings/gpio/gpio.h>.
Only the GPIO_ACTIVE_HIGH and GPIO_ACTIVE_LOW flags are supported.
Optional properties:
The I/O expander can detect input state changes, and thus optionally act as
an interrupt controller. When the expander interrupt line is connected all the
following properties must be set. For more information please see the
interrupt controller device tree bindings documentation available at
Documentation/devicetree/bindings/interrupt-controller/interrupts.txt.
- interrupt-controller: Identifies the node as an interrupt controller.
- #interrupt-cells: Number of cells to encode an interrupt source, shall be 2.
- first cell is the pin number
- second cell is used to specify flags
- interrupts: Interrupt specifier for the controllers interrupt.
Please refer to gpio.txt in this directory for details of the common GPIO
bindings used by client devices.
Example 1. MAX7325 with interrupt support enabled (CONFIG_GPIO_MAX732X_IRQ=y):
expander: max7325@6d {
compatible = "maxim,max7325";
reg = <0x6d>;
gpio-controller;
#gpio-cells = <2>;
interrupt-controller;
#interrupt-cells = <2>;
interrupt-parent = <&gpio4>;
interrupts = <29 IRQ_TYPE_EDGE_FALLING>;
};
Example 2. MAX7325 with interrupt support disabled (CONFIG_GPIO_MAX732X_IRQ=n):
expander: max7325@6d {
compatible = "maxim,max7325";
reg = <0x6d>;
gpio-controller;
#gpio-cells = <2>;
};

View File

@ -1,90 +0,0 @@
* NXP PCA953x I2C GPIO multiplexer
Required properties:
- compatible: Has to contain one of the following:
nxp,pca6416
nxp,pca9505
nxp,pca9534
nxp,pca9535
nxp,pca9536
nxp,pca9537
nxp,pca9538
nxp,pca9539
nxp,pca9554
nxp,pca9555
nxp,pca9556
nxp,pca9557
nxp,pca9574
nxp,pca9575
nxp,pca9698
nxp,pcal6416
nxp,pcal6524
nxp,pcal9535
nxp,pcal9555a
maxim,max7310
maxim,max7312
maxim,max7313
maxim,max7315
ti,pca6107
ti,pca9536
ti,tca6408
ti,tca6416
ti,tca6424
ti,tca9539
ti,tca9554
onnn,cat9554
onnn,pca9654
exar,xra1202
- gpio-controller: if used as gpio expander.
- #gpio-cells: if used as gpio expander.
- interrupt-controller: if to be used as interrupt expander.
- #interrupt-cells: if to be used as interrupt expander.
Optional properties:
- interrupts: interrupt specifier for the device's interrupt output.
- reset-gpios: GPIO specification for the RESET input. This is an
active low signal to the PCA953x.
- vcc-supply: power supply regulator.
Example:
gpio@20 {
compatible = "nxp,pca9505";
reg = <0x20>;
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_pca9505>;
gpio-controller;
#gpio-cells = <2>;
interrupt-parent = <&gpio3>;
interrupts = <23 IRQ_TYPE_LEVEL_LOW>;
};
Example with Interrupts:
gpio99: gpio@22 {
compatible = "nxp,pcal6524";
reg = <0x22>;
interrupt-parent = <&gpio6>;
interrupts = <1 IRQ_TYPE_EDGE_FALLING>; /* gpio6_161 */
interrupt-controller;
#interrupt-cells = <2>;
vcc-supply = <&vdds_1v8_main>;
gpio-controller;
#gpio-cells = <2>;
gpio-line-names =
"hdmi-ct-hpd", "hdmi.ls-oe", "p02", "p03", "vibra", "fault2", "p06", "p07",
"en-usb", "en-host1", "en-host2", "chg-int", "p14", "p15", "mic-int", "en-modem",
"shdn-hs-amp", "chg-status+red", "green", "blue", "en-esata", "fault1", "p26", "p27";
};
ts3a227@3b {
compatible = "ti,ts3a227e";
reg = <0x3b>;
interrupt-parent = <&gpio99>;
interrupts = <14 IRQ_TYPE_EDGE_RISING>;
ti,micbias = <0>; /* 2.1V */
};

View File

@ -0,0 +1,232 @@
# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
%YAML 1.2
---
$id: http://devicetree.org/schemas/gpio/gpio-pca95xx.yaml#
$schema: http://devicetree.org/meta-schemas/core.yaml#
title: NXP PCA95xx I2C GPIO multiplexer
maintainers:
- Krzysztof Kozlowski <krzk@kernel.org>
description: |+
Bindings for the family of I2C GPIO multiplexers/expanders: NXP PCA95xx,
Maxim MAX73xx
properties:
compatible:
enum:
- exar,xra1202
- maxim,max7310
- maxim,max7312
- maxim,max7313
- maxim,max7315
- maxim,max7319
- maxim,max7320
- maxim,max7321
- maxim,max7322
- maxim,max7323
- maxim,max7324
- maxim,max7325
- maxim,max7326
- maxim,max7327
- nxp,pca6416
- nxp,pca9505
- nxp,pca9534
- nxp,pca9535
- nxp,pca9536
- nxp,pca9537
- nxp,pca9538
- nxp,pca9539
- nxp,pca9554
- nxp,pca9555
- nxp,pca9556
- nxp,pca9557
- nxp,pca9574
- nxp,pca9575
- nxp,pca9698
- nxp,pcal6416
- nxp,pcal6524
- nxp,pcal9535
- nxp,pcal9555a
- onnn,cat9554
- onnn,pca9654
- ti,pca6107
- ti,pca9536
- ti,tca6408
- ti,tca6416
- ti,tca6424
- ti,tca9539
- ti,tca9554
reg:
maxItems: 1
gpio-controller: true
'#gpio-cells':
const: 2
gpio-line-names:
minItems: 1
maxItems: 32
interrupts:
maxItems: 1
interrupt-controller: true
'#interrupt-cells':
const: 2
reset-gpios:
description:
GPIO specification for the RESET input. This is an active low signal to
the PCA953x. Not valid for Maxim MAX732x devices.
vcc-supply:
description:
Optional power supply. Not valid for Maxim MAX732x devices.
wakeup-source:
$ref: /schemas/types.yaml#/definitions/flag
patternProperties:
"^(hog-[0-9]+|.+-hog(-[0-9]+)?)$":
type: object
properties:
gpio-hog: true
gpios: true
input: true
output-high: true
output-low: true
line-name: true
required:
- gpio-hog
- gpios
additionalProperties: false
required:
- compatible
- reg
- gpio-controller
- "#gpio-cells"
additionalProperties: false
allOf:
- if:
properties:
compatible:
contains:
enum:
- maxim,max7320
- maxim,max7321
- maxim,max7322
- maxim,max7323
- maxim,max7324
- maxim,max7325
- maxim,max7326
- maxim,max7327
then:
properties:
reset-gpios: false
vcc-supply: false
examples:
- |
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/interrupt-controller/irq.h>
i2c0 {
#address-cells = <1>;
#size-cells = <0>;
gpio@20 {
compatible = "nxp,pca9505";
reg = <0x20>;
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_pca9505>;
gpio-controller;
#gpio-cells = <2>;
interrupt-parent = <&gpio3>;
interrupts = <23 IRQ_TYPE_LEVEL_LOW>;
usb3-sata-sel-hog {
gpio-hog;
gpios = <4 GPIO_ACTIVE_HIGH>;
output-low;
line-name = "usb3_sata_sel";
};
};
};
- |
#include <dt-bindings/interrupt-controller/irq.h>
i2c1 {
#address-cells = <1>;
#size-cells = <0>;
gpio99: gpio@22 {
compatible = "nxp,pcal6524";
reg = <0x22>;
interrupt-parent = <&gpio6>;
interrupts = <1 IRQ_TYPE_EDGE_FALLING>; /* gpio6_161 */
interrupt-controller;
#interrupt-cells = <2>;
vcc-supply = <&vdds_1v8_main>;
gpio-controller;
#gpio-cells = <2>;
gpio-line-names = "hdmi-ct-hpd", "hdmi.ls-oe", "p02", "p03",
"vibra", "fault2", "p06", "p07", "en-usb",
"en-host1", "en-host2", "chg-int", "p14", "p15",
"mic-int", "en-modem", "shdn-hs-amp",
"chg-status+red", "green", "blue", "en-esata",
"fault1", "p26", "p27";
};
ts3a227@3b {
compatible = "ti,ts3a227e";
reg = <0x3b>;
interrupt-parent = <&gpio99>;
interrupts = <14 IRQ_TYPE_EDGE_RISING>;
ti,micbias = <0>; /* 2.1V */
};
};
- |
#include <dt-bindings/interrupt-controller/irq.h>
i2c2 {
#address-cells = <1>;
#size-cells = <0>;
/* MAX7325 with interrupt support enabled */
gpio@6d {
compatible = "maxim,max7325";
reg = <0x6d>;
gpio-controller;
#gpio-cells = <2>;
interrupt-controller;
#interrupt-cells = <2>;
interrupt-parent = <&gpio4>;
interrupts = <29 IRQ_TYPE_EDGE_FALLING>;
};
};
- |
i2c3 {
#address-cells = <1>;
#size-cells = <0>;
/* MAX7325 with interrupt support disabled */
gpio@6e {
compatible = "maxim,max7325";
reg = <0x6e>;
gpio-controller;
#gpio-cells = <2>;
};
};

View File

@ -51,7 +51,10 @@ properties:
gpio-controller: true
gpio-line-names: true
gpio-ranges:
minItems: 1
maxItems: 8
required:

View File

@ -37,6 +37,7 @@ properties:
- renesas,gpio-r8a774a1 # RZ/G2M
- renesas,gpio-r8a774b1 # RZ/G2N
- renesas,gpio-r8a774c0 # RZ/G2E
- renesas,gpio-r8a774e1 # RZ/G2H
- renesas,gpio-r8a7795 # R-Car H3
- renesas,gpio-r8a7796 # R-Car M3-W
- renesas,gpio-r8a77961 # R-Car M3-W+

View File

@ -61,8 +61,14 @@ patternProperties:
'#gpio-cells':
const: 2
ngpios:
default: 32
minimum: 1
maximum: 32
snps,nr-gpios:
description: The number of GPIO pins exported by the port.
deprecated: true
$ref: /schemas/types.yaml#/definitions/uint32
default: 32
minimum: 1

View File

@ -306,10 +306,6 @@ properties:
- nuvoton,npct601
# Nuvoton Temperature Sensor
- nuvoton,w83773g
# Octal SMBus and I2C registered interface
- nxp,pca9556
# 8-bit I2C-bus and SMBus I/O port with reset
- nxp,pca9557
# OKI ML86V7667 video decoder
- oki,ml86v7667
# OV5642: Color CMOS QSXGA (5-megapixel) Image Sensor with OmniBSI and Embedded TrueFocus

View File

@ -66,8 +66,33 @@ config GPIO_SYSFS
This ABI is deprecated. If you want to use GPIO from userspace,
use the character device /dev/gpiochipN with the appropriate
ioctl() operations instead. The character device is always
available.
ioctl() operations instead.
config GPIO_CDEV
bool
prompt "Character device (/dev/gpiochipN) support" if EXPERT
default y
help
Say Y here to add the character device /dev/gpiochipN interface
for GPIOs. The character device allows userspace to control GPIOs
using ioctl() operations.
Only say N if you are sure that the GPIO character device is not
required.
If unsure, say Y.
config GPIO_CDEV_V1
bool "Support GPIO ABI Version 1"
default y
depends on GPIO_CDEV
help
Say Y here to support version 1 of the GPIO CDEV ABI.
This ABI version is deprecated.
Please use the latest ABI for new developments.
If unsure, say Y.
config GPIO_GENERIC
depends on HAS_IOMEM # Only for IOMEM drivers
@ -202,7 +227,7 @@ config GPIO_DAVINCI
config GPIO_DWAPB
tristate "Synopsys DesignWare APB GPIO driver"
select GPIO_GENERIC
select GENERIC_IRQ_CHIP
select GPIOLIB_IRQCHIP
help
Say Y or M here to build support for the Synopsys DesignWare APB
GPIO block.
@ -397,7 +422,7 @@ config GPIO_MVEBU
select REGMAP_MMIO
config GPIO_MXC
def_bool y
tristate "i.MX GPIO support"
depends on ARCH_MXC || COMPILE_TEST
select GPIO_GENERIC
select GENERIC_IRQ_CHIP

View File

@ -6,9 +6,8 @@ ccflags-$(CONFIG_DEBUG_GPIO) += -DDEBUG
obj-$(CONFIG_GPIOLIB) += gpiolib.o
obj-$(CONFIG_GPIOLIB) += gpiolib-devres.o
obj-$(CONFIG_GPIOLIB) += gpiolib-legacy.o
obj-$(CONFIG_GPIOLIB) += gpiolib-devprop.o
obj-$(CONFIG_GPIOLIB) += gpiolib-cdev.o
obj-$(CONFIG_OF_GPIO) += gpiolib-of.o
obj-$(CONFIG_GPIO_CDEV) += gpiolib-cdev.o
obj-$(CONFIG_GPIO_SYSFS) += gpiolib-sysfs.o
obj-$(CONFIG_GPIO_ACPI) += gpiolib-acpi.o

View File

@ -333,20 +333,14 @@ static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
return gpiod_get_value(fwd->descs[offset]);
}
static int gpio_fwd_get_multiple(struct gpio_chip *chip, unsigned long *mask,
static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
unsigned long *bits)
{
struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
unsigned long *values, flags = 0;
struct gpio_desc **descs;
unsigned long *values;
unsigned int i, j = 0;
int error;
if (chip->can_sleep)
mutex_lock(&fwd->mlock);
else
spin_lock_irqsave(&fwd->slock, flags);
/* Both values bitmap and desc pointers are stored in tmp[] */
values = &fwd->tmp[0];
descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
@ -356,16 +350,32 @@ static int gpio_fwd_get_multiple(struct gpio_chip *chip, unsigned long *mask,
descs[j++] = fwd->descs[i];
error = gpiod_get_array_value(j, descs, NULL, values);
if (!error) {
j = 0;
for_each_set_bit(i, mask, fwd->chip.ngpio)
__assign_bit(i, bits, test_bit(j++, values));
}
if (error)
return error;
if (chip->can_sleep)
j = 0;
for_each_set_bit(i, mask, fwd->chip.ngpio)
__assign_bit(i, bits, test_bit(j++, values));
return 0;
}
static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
unsigned long *mask, unsigned long *bits)
{
struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
unsigned long flags;
int error;
if (chip->can_sleep) {
mutex_lock(&fwd->mlock);
error = gpio_fwd_get_multiple(fwd, mask, bits);
mutex_unlock(&fwd->mlock);
else
} else {
spin_lock_irqsave(&fwd->slock, flags);
error = gpio_fwd_get_multiple(fwd, mask, bits);
spin_unlock_irqrestore(&fwd->slock, flags);
}
return error;
}
@ -377,19 +387,13 @@ static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
gpiod_set_value(fwd->descs[offset], value);
}
static void gpio_fwd_set_multiple(struct gpio_chip *chip, unsigned long *mask,
static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
unsigned long *bits)
{
struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
unsigned long *values, flags = 0;
struct gpio_desc **descs;
unsigned long *values;
unsigned int i, j = 0;
if (chip->can_sleep)
mutex_lock(&fwd->mlock);
else
spin_lock_irqsave(&fwd->slock, flags);
/* Both values bitmap and desc pointers are stored in tmp[] */
values = &fwd->tmp[0];
descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
@ -400,11 +404,23 @@ static void gpio_fwd_set_multiple(struct gpio_chip *chip, unsigned long *mask,
}
gpiod_set_array_value(j, descs, NULL, values);
}
if (chip->can_sleep)
static void gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
unsigned long *mask, unsigned long *bits)
{
struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
unsigned long flags;
if (chip->can_sleep) {
mutex_lock(&fwd->mlock);
gpio_fwd_set_multiple(fwd, mask, bits);
mutex_unlock(&fwd->mlock);
else
} else {
spin_lock_irqsave(&fwd->slock, flags);
gpio_fwd_set_multiple(fwd, mask, bits);
spin_unlock_irqrestore(&fwd->slock, flags);
}
}
static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
@ -470,9 +486,9 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
chip->direction_input = gpio_fwd_direction_input;
chip->direction_output = gpio_fwd_direction_output;
chip->get = gpio_fwd_get;
chip->get_multiple = gpio_fwd_get_multiple;
chip->get_multiple = gpio_fwd_get_multiple_locked;
chip->set = gpio_fwd_set;
chip->set_multiple = gpio_fwd_set_multiple;
chip->set_multiple = gpio_fwd_set_multiple_locked;
chip->base = -1;
chip->ngpio = ngpios;
fwd->descs = descs;

View File

@ -590,10 +590,7 @@ static int bcm_kona_gpio_probe(struct platform_device *pdev)
dev_err(dev, "Couldn't determine # GPIO banks\n");
return -ENOENT;
} else if (ret < 0) {
if (ret != -EPROBE_DEFER)
dev_err(dev, "Couldn't determine GPIO banks: (%pe)\n",
ERR_PTR(ret));
return ret;
return dev_err_probe(dev, ret, "Couldn't determine GPIO banks\n");
}
kona_gpio->num_bank = ret;

View File

@ -237,12 +237,8 @@ static int davinci_gpio_probe(struct platform_device *pdev)
for (i = 0; i < nirq; i++) {
chips->irqs[i] = platform_get_irq(pdev, i);
if (chips->irqs[i] < 0) {
if (chips->irqs[i] != -EPROBE_DEFER)
dev_info(dev, "IRQ not populated, err = %d\n",
chips->irqs[i]);
return chips->irqs[i];
}
if (chips->irqs[i] < 0)
return dev_err_probe(dev, chips->irqs[i], "IRQ not populated\n");
}
chips->chip.label = dev_name(dev);

View File

@ -13,7 +13,6 @@
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
@ -83,22 +82,29 @@ struct dwapb_context {
};
#endif
struct dwapb_gpio_port_irqchip {
struct irq_chip irqchip;
unsigned int nr_irqs;
unsigned int irq[DWAPB_MAX_GPIOS];
};
struct dwapb_gpio_port {
struct gpio_chip gc;
bool is_registered;
struct dwapb_gpio_port_irqchip *pirq;
struct dwapb_gpio *gpio;
#ifdef CONFIG_PM_SLEEP
struct dwapb_context *ctx;
#endif
unsigned int idx;
};
#define to_dwapb_gpio(_gc) \
(container_of(_gc, struct dwapb_gpio_port, gc)->gpio)
struct dwapb_gpio {
struct device *dev;
void __iomem *regs;
struct dwapb_gpio_port *ports;
unsigned int nr_ports;
struct irq_domain *domain;
unsigned int flags;
struct reset_control *rst;
struct clk_bulk_data clks[DWAPB_NR_CLOCKS];
@ -147,14 +153,6 @@ static inline void dwapb_write(struct dwapb_gpio *gpio, unsigned int offset,
gc->write_reg(reg_base + gpio_reg_convert(gpio, offset), val);
}
static int dwapb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
{
struct dwapb_gpio_port *port = gpiochip_get_data(gc);
struct dwapb_gpio *gpio = port->gpio;
return irq_find_mapping(gpio->domain, offset);
}
static struct dwapb_gpio_port *dwapb_offs_to_port(struct dwapb_gpio *gpio, unsigned int offs)
{
struct dwapb_gpio_port *port;
@ -162,7 +160,7 @@ static struct dwapb_gpio_port *dwapb_offs_to_port(struct dwapb_gpio *gpio, unsig
for (i = 0; i < gpio->nr_ports; i++) {
port = &gpio->ports[i];
if (port->idx == offs / 32)
if (port->idx == offs / DWAPB_MAX_GPIOS)
return port;
}
@ -182,7 +180,7 @@ static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
pol = dwapb_read(gpio, GPIO_INT_POLARITY);
/* Just read the current value right out of the data register */
val = gc->get(gc, offs % 32);
val = gc->get(gc, offs % DWAPB_MAX_GPIOS);
if (val)
pol &= ~BIT(offs);
else
@ -193,12 +191,13 @@ static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
{
struct gpio_chip *gc = &gpio->ports[0].gc;
unsigned long irq_status;
irq_hw_number_t hwirq;
irq_status = dwapb_read(gpio, GPIO_INTSTATUS);
for_each_set_bit(hwirq, &irq_status, 32) {
int gpio_irq = irq_find_mapping(gpio->domain, hwirq);
for_each_set_bit(hwirq, &irq_status, DWAPB_MAX_GPIOS) {
int gpio_irq = irq_find_mapping(gc->irq.domain, hwirq);
u32 irq_type = irq_get_trigger_type(gpio_irq);
generic_handle_irq(gpio_irq);
@ -220,11 +219,53 @@ static void dwapb_irq_handler(struct irq_desc *desc)
chained_irq_exit(chip, desc);
}
static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
{
return IRQ_RETVAL(dwapb_do_irq(dev_id));
}
static void dwapb_irq_ack(struct irq_data *d)
{
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
u32 val = BIT(irqd_to_hwirq(d));
unsigned long flags;
spin_lock_irqsave(&gc->bgpio_lock, flags);
dwapb_write(gpio, GPIO_PORTA_EOI, val);
spin_unlock_irqrestore(&gc->bgpio_lock, flags);
}
static void dwapb_irq_mask(struct irq_data *d)
{
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
unsigned long flags;
u32 val;
spin_lock_irqsave(&gc->bgpio_lock, flags);
val = dwapb_read(gpio, GPIO_INTMASK) | BIT(irqd_to_hwirq(d));
dwapb_write(gpio, GPIO_INTMASK, val);
spin_unlock_irqrestore(&gc->bgpio_lock, flags);
}
static void dwapb_irq_unmask(struct irq_data *d)
{
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
unsigned long flags;
u32 val;
spin_lock_irqsave(&gc->bgpio_lock, flags);
val = dwapb_read(gpio, GPIO_INTMASK) & ~BIT(irqd_to_hwirq(d));
dwapb_write(gpio, GPIO_INTMASK, val);
spin_unlock_irqrestore(&gc->bgpio_lock, flags);
}
static void dwapb_irq_enable(struct irq_data *d)
{
struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = igc->private;
struct gpio_chip *gc = &gpio->ports[0].gc;
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
unsigned long flags;
u32 val;
@ -237,9 +278,8 @@ static void dwapb_irq_enable(struct irq_data *d)
static void dwapb_irq_disable(struct irq_data *d)
{
struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = igc->private;
struct gpio_chip *gc = &gpio->ports[0].gc;
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
unsigned long flags;
u32 val;
@ -252,9 +292,8 @@ static void dwapb_irq_disable(struct irq_data *d)
static int dwapb_irq_set_type(struct irq_data *d, u32 type)
{
struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = igc->private;
struct gpio_chip *gc = &gpio->ports[0].gc;
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
irq_hw_number_t bit = irqd_to_hwirq(d);
unsigned long level, polarity, flags;
@ -288,7 +327,10 @@ static int dwapb_irq_set_type(struct irq_data *d, u32 type)
break;
}
irq_setup_alt_chip(d, type);
if (type & IRQ_TYPE_LEVEL_MASK)
irq_set_handler_locked(d, handle_level_irq);
else if (type & IRQ_TYPE_EDGE_BOTH)
irq_set_handler_locked(d, handle_edge_irq);
dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
if (type != IRQ_TYPE_EDGE_BOTH)
@ -349,84 +391,67 @@ static int dwapb_gpio_set_config(struct gpio_chip *gc, unsigned offset,
return dwapb_gpio_set_debounce(gc, offset, debounce);
}
static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
static int dwapb_convert_irqs(struct dwapb_gpio_port_irqchip *pirq,
struct dwapb_port_property *pp)
{
return IRQ_RETVAL(dwapb_do_irq(dev_id));
int i;
/* Group all available IRQs into an array of parental IRQs. */
for (i = 0; i < pp->ngpio; ++i) {
if (!pp->irq[i])
continue;
pirq->irq[pirq->nr_irqs++] = pp->irq[i];
}
return pirq->nr_irqs ? 0 : -ENOENT;
}
static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
struct dwapb_gpio_port *port,
struct dwapb_port_property *pp)
{
struct dwapb_gpio_port_irqchip *pirq;
struct gpio_chip *gc = &port->gc;
struct fwnode_handle *fwnode = pp->fwnode;
struct irq_chip_generic *irq_gc = NULL;
unsigned int ngpio = gc->ngpio;
struct irq_chip_type *ct;
irq_hw_number_t hwirq;
int err, i;
struct gpio_irq_chip *girq;
int err;
if (memchr_inv(pp->irq, 0, sizeof(pp->irq)) == NULL) {
pirq = devm_kzalloc(gpio->dev, sizeof(*pirq), GFP_KERNEL);
if (!pirq)
return;
if (dwapb_convert_irqs(pirq, pp)) {
dev_warn(gpio->dev, "no IRQ for port%d\n", pp->idx);
return;
goto err_kfree_pirq;
}
gpio->domain = irq_domain_create_linear(fwnode, ngpio,
&irq_generic_chip_ops, gpio);
if (!gpio->domain)
return;
girq = &gc->irq;
girq->handler = handle_bad_irq;
girq->default_type = IRQ_TYPE_NONE;
err = irq_alloc_domain_generic_chips(gpio->domain, ngpio, 2,
DWAPB_DRIVER_NAME, handle_bad_irq,
IRQ_NOREQUEST, 0,
IRQ_GC_INIT_NESTED_LOCK);
if (err) {
dev_info(gpio->dev, "irq_alloc_domain_generic_chips failed\n");
irq_domain_remove(gpio->domain);
gpio->domain = NULL;
return;
}
irq_gc = irq_get_domain_generic_chip(gpio->domain, 0);
if (!irq_gc) {
irq_domain_remove(gpio->domain);
gpio->domain = NULL;
return;
}
irq_gc->reg_base = gpio->regs;
irq_gc->private = gpio;
for (i = 0; i < 2; i++) {
ct = &irq_gc->chip_types[i];
ct->chip.irq_ack = irq_gc_ack_set_bit;
ct->chip.irq_mask = irq_gc_mask_set_bit;
ct->chip.irq_unmask = irq_gc_mask_clr_bit;
ct->chip.irq_set_type = dwapb_irq_set_type;
ct->chip.irq_enable = dwapb_irq_enable;
ct->chip.irq_disable = dwapb_irq_disable;
port->pirq = pirq;
pirq->irqchip.name = DWAPB_DRIVER_NAME;
pirq->irqchip.irq_ack = dwapb_irq_ack;
pirq->irqchip.irq_mask = dwapb_irq_mask;
pirq->irqchip.irq_unmask = dwapb_irq_unmask;
pirq->irqchip.irq_set_type = dwapb_irq_set_type;
pirq->irqchip.irq_enable = dwapb_irq_enable;
pirq->irqchip.irq_disable = dwapb_irq_disable;
#ifdef CONFIG_PM_SLEEP
ct->chip.irq_set_wake = dwapb_irq_set_wake;
pirq->irqchip.irq_set_wake = dwapb_irq_set_wake;
#endif
ct->regs.ack = gpio_reg_convert(gpio, GPIO_PORTA_EOI);
ct->regs.mask = gpio_reg_convert(gpio, GPIO_INTMASK);
ct->type = IRQ_TYPE_LEVEL_MASK;
}
irq_gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
irq_gc->chip_types[0].handler = handle_level_irq;
irq_gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
irq_gc->chip_types[1].handler = handle_edge_irq;
if (!pp->irq_shared) {
int i;
for (i = 0; i < pp->ngpio; i++) {
if (pp->irq[i])
irq_set_chained_handler_and_data(pp->irq[i],
dwapb_irq_handler, gpio);
}
girq->num_parents = pirq->nr_irqs;
girq->parents = pirq->irq;
girq->parent_handler_data = gpio;
girq->parent_handler = dwapb_irq_handler;
} else {
/* This will let us handle the parent IRQ in the driver */
girq->num_parents = 0;
girq->parents = NULL;
girq->parent_handler = NULL;
/*
* Request a shared IRQ since where MFD would have devices
* using the same irq pin
@ -436,33 +461,16 @@ static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
IRQF_SHARED, DWAPB_DRIVER_NAME, gpio);
if (err) {
dev_err(gpio->dev, "error requesting IRQ\n");
irq_domain_remove(gpio->domain);
gpio->domain = NULL;
return;
goto err_kfree_pirq;
}
}
for (hwirq = 0; hwirq < ngpio; hwirq++)
irq_create_mapping(gpio->domain, hwirq);
girq->chip = &pirq->irqchip;
port->gc.to_irq = dwapb_gpio_to_irq;
}
return;
static void dwapb_irq_teardown(struct dwapb_gpio *gpio)
{
struct dwapb_gpio_port *port = &gpio->ports[0];
struct gpio_chip *gc = &port->gc;
unsigned int ngpio = gc->ngpio;
irq_hw_number_t hwirq;
if (!gpio->domain)
return;
for (hwirq = 0; hwirq < ngpio; hwirq++)
irq_dispose_mapping(irq_find_mapping(gpio->domain, hwirq));
irq_domain_remove(gpio->domain);
gpio->domain = NULL;
err_kfree_pirq:
devm_kfree(gpio->dev, pirq);
}
static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
@ -510,36 +518,16 @@ static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
if (pp->idx == 0)
dwapb_configure_irqs(gpio, port, pp);
err = gpiochip_add_data(&port->gc, port);
err = devm_gpiochip_add_data(gpio->dev, &port->gc, port);
if (err) {
dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
port->idx);
return err;
}
/* Add GPIO-signaled ACPI event support */
acpi_gpiochip_request_interrupts(&port->gc);
port->is_registered = true;
return 0;
}
static void dwapb_gpio_unregister(struct dwapb_gpio *gpio)
{
unsigned int m;
for (m = 0; m < gpio->nr_ports; ++m) {
struct dwapb_gpio_port *port = &gpio->ports[m];
if (!port->is_registered)
continue;
acpi_gpiochip_free_interrupts(&port->gc);
gpiochip_remove(&port->gc);
}
}
static void dwapb_get_irq(struct device *dev, struct fwnode_handle *fwnode,
struct dwapb_port_property *pp)
{
@ -594,11 +582,12 @@ static struct dwapb_platform_data *dwapb_gpio_get_pdata(struct device *dev)
return ERR_PTR(-EINVAL);
}
if (fwnode_property_read_u32(fwnode, "snps,nr-gpios", &pp->ngpio)) {
if (fwnode_property_read_u32(fwnode, "ngpios", &pp->ngpio) &&
fwnode_property_read_u32(fwnode, "snps,nr-gpios", &pp->ngpio)) {
dev_info(dev,
"failed to get number of gpios for port%d\n",
i);
pp->ngpio = 32;
pp->ngpio = DWAPB_MAX_GPIOS;
}
pp->irq_shared = false;
@ -615,6 +604,62 @@ static struct dwapb_platform_data *dwapb_gpio_get_pdata(struct device *dev)
return pdata;
}
static void dwapb_assert_reset(void *data)
{
struct dwapb_gpio *gpio = data;
reset_control_assert(gpio->rst);
}
static int dwapb_get_reset(struct dwapb_gpio *gpio)
{
int err;
gpio->rst = devm_reset_control_get_optional_shared(gpio->dev, NULL);
if (IS_ERR(gpio->rst)) {
dev_err(gpio->dev, "Cannot get reset descriptor\n");
return PTR_ERR(gpio->rst);
}
err = reset_control_deassert(gpio->rst);
if (err) {
dev_err(gpio->dev, "Cannot deassert reset lane\n");
return err;
}
return devm_add_action_or_reset(gpio->dev, dwapb_assert_reset, gpio);
}
static void dwapb_disable_clks(void *data)
{
struct dwapb_gpio *gpio = data;
clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
}
static int dwapb_get_clks(struct dwapb_gpio *gpio)
{
int err;
/* Optional bus and debounce clocks */
gpio->clks[0].id = "bus";
gpio->clks[1].id = "db";
err = devm_clk_bulk_get_optional(gpio->dev, DWAPB_NR_CLOCKS,
gpio->clks);
if (err) {
dev_err(gpio->dev, "Cannot get APB/Debounce clocks\n");
return err;
}
err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
if (err) {
dev_err(gpio->dev, "Cannot enable APB/Debounce clocks\n");
return err;
}
return devm_add_action_or_reset(gpio->dev, dwapb_disable_clks, gpio);
}
static const struct of_device_id dwapb_of_match[] = {
{ .compatible = "snps,dw-apb-gpio", .data = (void *)0},
{ .compatible = "apm,xgene-gpio-v2", .data = (void *)GPIO_REG_OFFSET_V2},
@ -654,11 +699,9 @@ static int dwapb_gpio_probe(struct platform_device *pdev)
gpio->dev = &pdev->dev;
gpio->nr_ports = pdata->nports;
gpio->rst = devm_reset_control_get_optional_shared(dev, NULL);
if (IS_ERR(gpio->rst))
return PTR_ERR(gpio->rst);
reset_control_deassert(gpio->rst);
err = dwapb_get_reset(gpio);
if (err)
return err;
gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
sizeof(*gpio->ports), GFP_KERNEL);
@ -669,49 +712,17 @@ static int dwapb_gpio_probe(struct platform_device *pdev)
if (IS_ERR(gpio->regs))
return PTR_ERR(gpio->regs);
/* Optional bus and debounce clocks */
gpio->clks[0].id = "bus";
gpio->clks[1].id = "db";
err = devm_clk_bulk_get_optional(&pdev->dev, DWAPB_NR_CLOCKS,
gpio->clks);
if (err) {
dev_err(&pdev->dev, "Cannot get APB/Debounce clocks\n");
err = dwapb_get_clks(gpio);
if (err)
return err;
}
err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
if (err) {
dev_err(&pdev->dev, "Cannot enable APB/Debounce clocks\n");
return err;
}
gpio->flags = (uintptr_t)device_get_match_data(dev);
for (i = 0; i < gpio->nr_ports; i++) {
err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
if (err)
goto out_unregister;
return err;
}
platform_set_drvdata(pdev, gpio);
return 0;
out_unregister:
dwapb_gpio_unregister(gpio);
dwapb_irq_teardown(gpio);
clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
return err;
}
static int dwapb_gpio_remove(struct platform_device *pdev)
{
struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
dwapb_gpio_unregister(gpio);
dwapb_irq_teardown(gpio);
reset_control_assert(gpio->rst);
clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
return 0;
}
@ -815,7 +826,6 @@ static struct platform_driver dwapb_gpio_driver = {
.acpi_match_table = dwapb_acpi_match,
},
.probe = dwapb_gpio_probe,
.remove = dwapb_gpio_remove,
};
module_platform_driver(dwapb_gpio_driver);

View File

@ -7,10 +7,10 @@
* Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/debugfs.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio/driver.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irq_sim.h>
@ -19,21 +19,19 @@
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/string_helpers.h>
#include <linux/uaccess.h>
#include "gpiolib.h"
#define GPIO_MOCKUP_NAME "gpio-mockup"
#define GPIO_MOCKUP_MAX_GC 10
/*
* We're storing two values per chip: the GPIO base and the number
* of GPIO lines.
*/
#define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
/* Maximum of three properties + the sentinel. */
#define GPIO_MOCKUP_MAX_PROP 4
#define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
/* Maximum of four properties + the sentinel. */
#define GPIO_MOCKUP_MAX_PROP 5
/*
* struct gpio_pin_status - structure describing a GPIO status
@ -375,31 +373,6 @@ static void gpio_mockup_debugfs_setup(struct device *dev,
debugfs_create_file(name, 0200, chip->dbg_dir, priv,
&gpio_mockup_debugfs_ops);
}
return;
}
static int gpio_mockup_name_lines(struct device *dev,
struct gpio_mockup_chip *chip)
{
struct gpio_chip *gc = &chip->gc;
char **names;
int i;
names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
if (!names)
return -ENOMEM;
for (i = 0; i < gc->ngpio; i++) {
names[i] = devm_kasprintf(dev, GFP_KERNEL,
"%s-%d", gc->label, i);
if (!names[i])
return -ENOMEM;
}
gc->names = (const char *const *)names;
return 0;
}
static void gpio_mockup_dispose_mappings(void *data)
@ -434,21 +407,14 @@ static int gpio_mockup_probe(struct platform_device *pdev)
if (rv)
return rv;
rv = device_property_read_string(dev, "chip-name", &name);
rv = device_property_read_string(dev, "chip-label", &name);
if (rv)
name = NULL;
name = dev_name(dev);
chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
if (!name) {
name = devm_kasprintf(dev, GFP_KERNEL,
"%s-%c", pdev->name, pdev->id + 'A');
if (!name)
return -ENOMEM;
}
mutex_init(&chip->lock);
gc = &chip->gc;
@ -476,12 +442,6 @@ static int gpio_mockup_probe(struct platform_device *pdev)
for (i = 0; i < gc->ngpio; i++)
chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
if (device_property_read_bool(dev, "named-gpio-lines")) {
rv = gpio_mockup_name_lines(dev, chip);
if (rv)
return rv;
}
chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
gc->ngpio);
if (IS_ERR(chip->irq_sim_domain))
@ -502,7 +462,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)
static struct platform_driver gpio_mockup_driver = {
.driver = {
.name = GPIO_MOCKUP_NAME,
.name = "gpio-mockup",
},
.probe = gpio_mockup_probe,
};
@ -522,14 +482,80 @@ static void gpio_mockup_unregister_pdevs(void)
}
}
static int __init gpio_mockup_init(void)
static __init char **gpio_mockup_make_line_names(const char *label,
unsigned int num_lines)
{
unsigned int i;
char **names;
names = kcalloc(num_lines + 1, sizeof(char *), GFP_KERNEL);
if (!names)
return NULL;
for (i = 0; i < num_lines; i++) {
names[i] = kasprintf(GFP_KERNEL, "%s-%u", label, i);
if (!names[i]) {
kfree_strarray(names, i);
return NULL;
}
}
return names;
}
static int __init gpio_mockup_register_chip(int idx)
{
struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
int i, prop, num_chips, err = 0, base;
struct platform_device_info pdevinfo;
struct platform_device *pdev;
char **line_names = NULL;
char chip_label[32];
int prop = 0, base;
u16 ngpio;
memset(properties, 0, sizeof(properties));
memset(&pdevinfo, 0, sizeof(pdevinfo));
snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label);
base = gpio_mockup_range_base(idx);
if (base >= 0)
properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base);
ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
: gpio_mockup_range_ngpio(idx) - base;
properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
if (gpio_mockup_named_lines) {
line_names = gpio_mockup_make_line_names(chip_label, ngpio);
if (!line_names)
return -ENOMEM;
properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
"gpio-line-names", line_names, ngpio);
}
pdevinfo.name = "gpio-mockup";
pdevinfo.id = idx;
pdevinfo.properties = properties;
pdev = platform_device_register_full(&pdevinfo);
kfree_strarray(line_names, ngpio);
if (IS_ERR(pdev)) {
pr_err("error registering device");
return PTR_ERR(pdev);
}
gpio_mockup_pdevs[idx] = pdev;
return 0;
}
static int __init gpio_mockup_init(void)
{
int i, num_chips, err;
if ((gpio_mockup_num_ranges < 2) ||
(gpio_mockup_num_ranges % 2) ||
(gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
@ -551,43 +577,19 @@ static int __init gpio_mockup_init(void)
err = platform_driver_register(&gpio_mockup_driver);
if (err) {
gpio_mockup_err("error registering platform driver\n");
pr_err("error registering platform driver\n");
debugfs_remove_recursive(gpio_mockup_dbg_dir);
return err;
}
for (i = 0; i < num_chips; i++) {
memset(properties, 0, sizeof(properties));
memset(&pdevinfo, 0, sizeof(pdevinfo));
prop = 0;
base = gpio_mockup_range_base(i);
if (base >= 0)
properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
base);
ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
: gpio_mockup_range_ngpio(i) - base;
properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
if (gpio_mockup_named_lines)
properties[prop++] = PROPERTY_ENTRY_BOOL(
"named-gpio-lines");
pdevinfo.name = GPIO_MOCKUP_NAME;
pdevinfo.id = i;
pdevinfo.properties = properties;
pdev = platform_device_register_full(&pdevinfo);
if (IS_ERR(pdev)) {
gpio_mockup_err("error registering device");
err = gpio_mockup_register_chip(i);
if (err) {
platform_driver_unregister(&gpio_mockup_driver);
gpio_mockup_unregister_pdevs();
debugfs_remove_recursive(gpio_mockup_dbg_dir);
return PTR_ERR(pdev);
return err;
}
gpio_mockup_pdevs[i] = pdev;
}
return 0;

View File

@ -47,27 +47,6 @@ struct mpc8xxx_gpio_chip {
unsigned int irqn;
};
/* The GPIO Input Buffer Enable register(GPIO_IBE) is used to
* control the input enable of each individual GPIO port.
* When an individual GPIO ports direction is set to
* input (GPIO_GPDIR[DRn=0]), the associated input enable must be
* set (GPIOxGPIE[IEn]=1) to propagate the port value to the GPIO
* Data Register.
*/
static int ls1028a_gpio_dir_in_init(struct gpio_chip *gc)
{
unsigned long flags;
struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
spin_lock_irqsave(&gc->bgpio_lock, flags);
gc->write_reg(mpc8xxx_gc->regs + GPIO_IBE, 0xffffffff);
spin_unlock_irqrestore(&gc->bgpio_lock, flags);
return 0;
}
/*
* This hardware has a big endian bit assignment such that GPIO line 0 is
* connected to bit 31, line 1 to bit 30 ... line 31 to bit 0.
@ -283,7 +262,6 @@ static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
};
struct mpc8xxx_gpio_devtype {
int (*gpio_dir_in_init)(struct gpio_chip *chip);
int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
int (*gpio_get)(struct gpio_chip *, unsigned int);
int (*irq_set_type)(struct irq_data *, unsigned int);
@ -294,11 +272,6 @@ static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
.irq_set_type = mpc512x_irq_set_type,
};
static const struct mpc8xxx_gpio_devtype ls1028a_gpio_devtype = {
.gpio_dir_in_init = ls1028a_gpio_dir_in_init,
.irq_set_type = mpc8xxx_irq_set_type,
};
static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
.gpio_dir_out = mpc5125_gpio_dir_out,
.irq_set_type = mpc512x_irq_set_type,
@ -319,8 +292,8 @@ static const struct of_device_id mpc8xxx_gpio_ids[] = {
{ .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
{ .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
{ .compatible = "fsl,pq3-gpio", },
{ .compatible = "fsl,ls1028a-gpio", .data = &ls1028a_gpio_devtype, },
{ .compatible = "fsl,ls1088a-gpio", .data = &ls1028a_gpio_devtype, },
{ .compatible = "fsl,ls1028a-gpio", },
{ .compatible = "fsl,ls1088a-gpio", },
{ .compatible = "fsl,qoriq-gpio", },
{}
};
@ -389,7 +362,16 @@ static int mpc8xxx_probe(struct platform_device *pdev)
gc->to_irq = mpc8xxx_gpio_to_irq;
if (of_device_is_compatible(np, "fsl,qoriq-gpio"))
/*
* The GPIO Input Buffer Enable register(GPIO_IBE) is used to control
* the input enable of each individual GPIO port. When an individual
* GPIO ports direction is set to input (GPIO_GPDIR[DRn=0]), the
* associated input enable must be set (GPIOxGPIE[IEn]=1) to propagate
* the port value to the GPIO Data Register.
*/
if (of_device_is_compatible(np, "fsl,qoriq-gpio") ||
of_device_is_compatible(np, "fsl,ls1028a-gpio") ||
of_device_is_compatible(np, "fsl,ls1088a-gpio"))
gc->write_reg(mpc8xxx_gc->regs + GPIO_IBE, 0xffffffff);
ret = gpiochip_add_data(gc, mpc8xxx_gc);
@ -411,9 +393,6 @@ static int mpc8xxx_probe(struct platform_device *pdev)
/* ack and mask all irqs */
gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 0xffffffff);
gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 0);
/* enable input buffer */
if (devtype->gpio_dir_in_init)
devtype->gpio_dir_in_init(gc);
ret = devm_request_irq(&pdev->dev, mpc8xxx_gc->irqn,
mpc8xxx_gpio_irq_cascade,

View File

@ -15,6 +15,7 @@
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/irqchip/chained_irq.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/syscore_ops.h>
@ -158,6 +159,7 @@ static const struct of_device_id mxc_gpio_dt_ids[] = {
{ .compatible = "fsl,imx7d-gpio", .data = &mxc_gpio_devtype[IMX35_GPIO], },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, mxc_gpio_dt_ids);
/*
* MX2 has one interrupt *for all* gpio ports. The list is used
@ -604,3 +606,7 @@ static int __init gpio_mxc_init(void)
return platform_driver_register(&mxc_gpio_driver);
}
subsys_initcall(gpio_mxc_init);
MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
MODULE_DESCRIPTION("i.MX GPIO Driver");
MODULE_LICENSE("GPL");

View File

@ -1394,10 +1394,7 @@ static int omap_gpio_probe(struct platform_device *pdev)
if (bank->irq <= 0) {
if (!bank->irq)
bank->irq = -ENXIO;
if (bank->irq != -EPROBE_DEFER)
dev_err(dev,
"can't get irq resource ret=%d\n", bank->irq);
return bank->irq;
return dev_err_probe(dev, bank->irq, "can't get irq resource\n");
}
bank->chip.parent = dev;

View File

@ -90,6 +90,7 @@ static const struct i2c_device_id pca953x_id[] = {
{ "pcal6416", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
{ "pcal6524", 24 | PCA953X_TYPE | PCA_LATCH_INT, },
{ "pcal9535", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
{ "pcal9554b", 8 | PCA953X_TYPE | PCA_LATCH_INT, },
{ "pcal9555a", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
{ "max7310", 8 | PCA953X_TYPE, },
@ -1018,12 +1019,9 @@ static int pca953x_probe(struct i2c_client *client,
chip->client = client;
reg = devm_regulator_get(&client->dev, "vcc");
if (IS_ERR(reg)) {
ret = PTR_ERR(reg);
if (ret != -EPROBE_DEFER)
dev_err(&client->dev, "reg get err: %d\n", ret);
return ret;
}
if (IS_ERR(reg))
return dev_err_probe(&client->dev, PTR_ERR(reg), "reg get err\n");
ret = regulator_enable(reg);
if (ret) {
dev_err(&client->dev, "reg en err: %d\n", ret);
@ -1255,6 +1253,7 @@ static const struct of_device_id pca953x_dt_ids[] = {
{ .compatible = "nxp,pcal6416", .data = OF_953X(16, PCA_LATCH_INT), },
{ .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
{ .compatible = "nxp,pcal9535", .data = OF_953X(16, PCA_LATCH_INT), },
{ .compatible = "nxp,pcal9554b", .data = OF_953X( 8, PCA_LATCH_INT), },
{ .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), },
{ .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },

View File

@ -148,12 +148,9 @@ static int pisosr_gpio_probe(struct spi_device *spi)
return -ENOMEM;
gpio->load_gpio = devm_gpiod_get_optional(dev, "load", GPIOD_OUT_LOW);
if (IS_ERR(gpio->load_gpio)) {
ret = PTR_ERR(gpio->load_gpio);
if (ret != -EPROBE_DEFER)
dev_err(dev, "Unable to allocate load GPIO\n");
return ret;
}
if (IS_ERR(gpio->load_gpio))
return dev_err_probe(dev, PTR_ERR(gpio->load_gpio),
"Unable to allocate load GPIO\n");
mutex_init(&gpio->lock);

View File

@ -41,7 +41,10 @@
#define XWAY_STP_4HZ BIT(23)
#define XWAY_STP_8HZ BIT(24)
#define XWAY_STP_10HZ (BIT(24) | BIT(23))
#define XWAY_STP_SPEED_MASK (0xf << 23)
#define XWAY_STP_SPEED_MASK (BIT(23) | BIT(24) | BIT(25) | BIT(26) | BIT(27))
#define XWAY_STP_FPIS_VALUE BIT(21)
#define XWAY_STP_FPIS_MASK (BIT(20) | BIT(21))
/* clock source for automatic update */
#define XWAY_STP_UPD_FPI BIT(31)
@ -54,7 +57,9 @@
/* 2 groups of 3 bits can be driven by the phys */
#define XWAY_STP_PHY_MASK 0x7
#define XWAY_STP_PHY1_SHIFT 27
#define XWAY_STP_PHY2_SHIFT 15
#define XWAY_STP_PHY2_SHIFT 3
#define XWAY_STP_PHY3_SHIFT 6
#define XWAY_STP_PHY4_SHIFT 15
/* STP has 3 groups of 8 bits */
#define XWAY_STP_GROUP0 BIT(0)
@ -80,6 +85,8 @@ struct xway_stp {
u8 dsl; /* the 2 LSBs can be driven by the dsl core */
u8 phy1; /* 3 bits can be driven by phy1 */
u8 phy2; /* 3 bits can be driven by phy2 */
u8 phy3; /* 3 bits can be driven by phy3 */
u8 phy4; /* 3 bits can be driven by phy4 */
u8 reserved; /* mask out the hw driven bits in gpio_request */
};
@ -114,7 +121,8 @@ static void xway_stp_set(struct gpio_chip *gc, unsigned gpio, int val)
else
chip->shadow &= ~BIT(gpio);
xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0);
xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
if (!chip->reserved)
xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
}
/**
@ -188,16 +196,37 @@ static void xway_stp_hw_init(struct xway_stp *chip)
chip->phy2 << XWAY_STP_PHY2_SHIFT,
XWAY_STP_CON1);
if (of_machine_is_compatible("lantiq,grx390")
|| of_machine_is_compatible("lantiq,ar10")) {
xway_stp_w32_mask(chip->virt,
XWAY_STP_PHY_MASK << XWAY_STP_PHY3_SHIFT,
chip->phy3 << XWAY_STP_PHY3_SHIFT,
XWAY_STP_CON1);
}
if (of_machine_is_compatible("lantiq,grx390")) {
xway_stp_w32_mask(chip->virt,
XWAY_STP_PHY_MASK << XWAY_STP_PHY4_SHIFT,
chip->phy4 << XWAY_STP_PHY4_SHIFT,
XWAY_STP_CON1);
}
/* mask out the hw driven bits in gpio_request */
chip->reserved = (chip->phy2 << 5) | (chip->phy1 << 2) | chip->dsl;
chip->reserved = (chip->phy4 << 11) | (chip->phy3 << 8) | (chip->phy2 << 5)
| (chip->phy1 << 2) | chip->dsl;
/*
* if we have pins that are driven by hw, we need to tell the stp what
* clock to use as a timer.
*/
if (chip->reserved)
if (chip->reserved) {
xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK,
XWAY_STP_UPD_FPI, XWAY_STP_CON1);
xway_stp_w32_mask(chip->virt, XWAY_STP_SPEED_MASK,
XWAY_STP_10HZ, XWAY_STP_CON1);
xway_stp_w32_mask(chip->virt, XWAY_STP_FPIS_MASK,
XWAY_STP_FPIS_VALUE, XWAY_STP_CON1);
}
}
static int xway_stp_probe(struct platform_device *pdev)
@ -242,13 +271,26 @@ static int xway_stp_probe(struct platform_device *pdev)
/* find out which gpios are controlled by the phys */
if (of_machine_is_compatible("lantiq,ar9") ||
of_machine_is_compatible("lantiq,gr9") ||
of_machine_is_compatible("lantiq,vr9")) {
of_machine_is_compatible("lantiq,vr9") ||
of_machine_is_compatible("lantiq,ar10") ||
of_machine_is_compatible("lantiq,grx390")) {
if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy))
chip->phy1 = phy & XWAY_STP_PHY_MASK;
if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy))
chip->phy2 = phy & XWAY_STP_PHY_MASK;
}
if (of_machine_is_compatible("lantiq,ar10") ||
of_machine_is_compatible("lantiq,grx390")) {
if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy3", &phy))
chip->phy3 = phy & XWAY_STP_PHY_MASK;
}
if (of_machine_is_compatible("lantiq,grx390")) {
if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy4", &phy))
chip->phy4 = phy & XWAY_STP_PHY_MASK;
}
/* check which edge trigger we should use, default to a falling edge */
if (!of_find_property(pdev->dev.of_node, "lantiq,rising", NULL))
chip->edge = XWAY_STP_FALLING;

View File

@ -19,9 +19,9 @@
* These registers are modified under the irq bus lock and cached to avoid
* unnecessary writes in bus_sync_unlock.
*/
enum { REG_IBE, REG_IEV, REG_IS, REG_IE };
enum { REG_IBE, REG_IEV, REG_IS, REG_IE, REG_DIRECT };
#define CACHE_NR_REGS 4
#define CACHE_NR_REGS 5
#define CACHE_NR_BANKS 3
struct tc3589x_gpio {
@ -200,6 +200,7 @@ static void tc3589x_gpio_irq_sync_unlock(struct irq_data *d)
[REG_IEV] = TC3589x_GPIOIEV0,
[REG_IS] = TC3589x_GPIOIS0,
[REG_IE] = TC3589x_GPIOIE0,
[REG_DIRECT] = TC3589x_DIRECT0,
};
int i, j;
@ -228,6 +229,7 @@ static void tc3589x_gpio_irq_mask(struct irq_data *d)
int mask = BIT(offset % 8);
tc3589x_gpio->regs[REG_IE][regoffset] &= ~mask;
tc3589x_gpio->regs[REG_DIRECT][regoffset] |= mask;
}
static void tc3589x_gpio_irq_unmask(struct irq_data *d)
@ -239,6 +241,7 @@ static void tc3589x_gpio_irq_unmask(struct irq_data *d)
int mask = BIT(offset % 8);
tc3589x_gpio->regs[REG_IE][regoffset] |= mask;
tc3589x_gpio->regs[REG_DIRECT][regoffset] &= ~mask;
}
static struct irq_chip tc3589x_gpio_irq_chip = {
@ -334,6 +337,17 @@ static int tc3589x_gpio_probe(struct platform_device *pdev)
if (ret < 0)
return ret;
/* For tc35894, have to disable Direct KBD interrupts,
* else IRQST will always be 0x20, IRQN low level, can't
* clear the irq status.
* TODO: need more test on other tc3589x chip.
*
*/
ret = tc3589x_reg_write(tc3589x, TC3589x_DKBDMSK,
TC3589x_DKBDMSK_ELINT | TC3589x_DKBDMSK_EINT);
if (ret < 0)
return ret;
ret = devm_request_threaded_irq(&pdev->dev,
irq, NULL, tc3589x_gpio_irq,
IRQF_ONESHOT, "tc3589x-gpio",

View File

@ -929,11 +929,9 @@ static int zynq_gpio_probe(struct platform_device *pdev)
/* Retrieve GPIO clock */
gpio->clk = devm_clk_get(&pdev->dev, NULL);
if (IS_ERR(gpio->clk)) {
if (PTR_ERR(gpio->clk) != -EPROBE_DEFER)
dev_err(&pdev->dev, "input clock not found.\n");
return PTR_ERR(gpio->clk);
}
if (IS_ERR(gpio->clk))
return dev_err_probe(&pdev->dev, PTR_ERR(gpio->clk), "input clock not found.\n");
ret = clk_prepare_enable(gpio->clk);
if (ret) {
dev_err(&pdev->dev, "Unable to enable clock.\n");

View File

@ -1221,9 +1221,6 @@ void acpi_gpiochip_add(struct gpio_chip *chip)
return;
}
if (!chip->names)
devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
acpi_gpiochip_request_regions(acpi_gpio);
acpi_gpiochip_scan_gpios(acpi_gpio);
acpi_walk_dep_device_list(handle);

File diff suppressed because it is too large Load Diff

View File

@ -3,9 +3,26 @@
#ifndef GPIOLIB_CDEV_H
#define GPIOLIB_CDEV_H
#include <linux/device.h>
#include <linux/types.h>
struct gpio_device;
#ifdef CONFIG_GPIO_CDEV
int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt);
void gpiolib_cdev_unregister(struct gpio_device *gdev);
#else
static inline int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
{
return 0;
}
static inline void gpiolib_cdev_unregister(struct gpio_device *gdev)
{
}
#endif /* CONFIG_GPIO_CDEV */
#endif /* GPIOLIB_CDEV_H */

View File

@ -1,63 +0,0 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Device property helpers for GPIO chips.
*
* Copyright (C) 2016, Intel Corporation
* Author: Mika Westerberg <mika.westerberg@linux.intel.com>
*/
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio/driver.h>
#include <linux/export.h>
#include "gpiolib.h"
/**
* devprop_gpiochip_set_names - Set GPIO line names using device properties
* @chip: GPIO chip whose lines should be named, if possible
* @fwnode: Property Node containing the gpio-line-names property
*
* Looks for device property "gpio-line-names" and if it exists assigns
* GPIO line names for the chip. The memory allocated for the assigned
* names belong to the underlying firmware node and should not be released
* by the caller.
*/
void devprop_gpiochip_set_names(struct gpio_chip *chip,
const struct fwnode_handle *fwnode)
{
struct gpio_device *gdev = chip->gpiodev;
const char **names;
int ret, i;
int count;
count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
NULL, 0);
if (count < 0)
return;
if (count > gdev->ngpio) {
dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
count, gdev->ngpio);
count = gdev->ngpio;
}
names = kcalloc(count, sizeof(*names), GFP_KERNEL);
if (!names)
return;
ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
names, count);
if (ret < 0) {
dev_warn(&gdev->dev, "failed to read GPIO line names\n");
kfree(names);
return;
}
for (i = 0; i < count; i++)
gdev->descs[i].name = names[i];
kfree(names);
}
EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names);

View File

@ -1026,11 +1026,6 @@ int of_gpiochip_add(struct gpio_chip *chip)
if (ret)
return ret;
/* If the chip defines names itself, these take precedence */
if (!chip->names)
devprop_gpiochip_set_names(chip,
of_fwnode_handle(chip->of_node));
of_node_get(chip->of_node);
ret = of_gpiochip_scan_gpios(chip);

View File

@ -340,9 +340,6 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
struct gpio_device *gdev = gc->gpiodev;
int i;
if (!gc->names)
return 0;
/* First check all names if they are unique */
for (i = 0; i != gc->ngpio; ++i) {
struct gpio_desc *gpio;
@ -361,6 +358,57 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
return 0;
}
/*
* devprop_gpiochip_set_names - Set GPIO line names using device properties
* @chip: GPIO chip whose lines should be named, if possible
*
* Looks for device property "gpio-line-names" and if it exists assigns
* GPIO line names for the chip. The memory allocated for the assigned
* names belong to the underlying software node and should not be released
* by the caller.
*/
static int devprop_gpiochip_set_names(struct gpio_chip *chip)
{
struct gpio_device *gdev = chip->gpiodev;
struct device *dev = chip->parent;
const char **names;
int ret, i;
int count;
/* GPIO chip may not have a parent device whose properties we inspect. */
if (!dev)
return 0;
count = device_property_string_array_count(dev, "gpio-line-names");
if (count < 0)
return 0;
if (count > gdev->ngpio) {
dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
count, gdev->ngpio);
count = gdev->ngpio;
}
names = kcalloc(count, sizeof(*names), GFP_KERNEL);
if (!names)
return -ENOMEM;
ret = device_property_read_string_array(dev, "gpio-line-names",
names, count);
if (ret < 0) {
dev_warn(&gdev->dev, "failed to read GPIO line names\n");
kfree(names);
return ret;
}
for (i = 0; i < count; i++)
gdev->descs[i].name = names[i];
kfree(names);
return 0;
}
static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
{
unsigned long *p;
@ -426,7 +474,7 @@ static void gpiodevice_release(struct device *dev)
struct gpio_device *gdev = dev_get_drvdata(dev);
list_del(&gdev->list);
ida_simple_remove(&gpio_ida, gdev->id);
ida_free(&gpio_ida, gdev->id);
kfree_const(gdev->label);
kfree(gdev->descs);
kfree(gdev);
@ -537,7 +585,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
gc->of_node = gdev->dev.of_node;
#endif
gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
if (gdev->id < 0) {
ret = gdev->id;
goto err_free_gdev;
@ -621,7 +669,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
INIT_LIST_HEAD(&gdev->pin_ranges);
#endif
ret = gpiochip_set_desc_names(gc);
if (gc->names)
ret = gpiochip_set_desc_names(gc);
else
ret = devprop_gpiochip_set_names(gc);
if (ret)
goto err_remove_from_list;
@ -705,7 +756,7 @@ err_free_label:
err_free_descs:
kfree(gdev->descs);
err_free_ida:
ida_simple_remove(&gpio_ida, gdev->id);
ida_free(&gpio_ida, gdev->id);
err_free_gdev:
/* failures here can mean systems won't boot... */
pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
@ -2041,9 +2092,14 @@ static bool gpiod_free_commit(struct gpio_desc *desc)
clear_bit(FLAG_PULL_UP, &desc->flags);
clear_bit(FLAG_PULL_DOWN, &desc->flags);
clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
clear_bit(FLAG_EDGE_RISING, &desc->flags);
clear_bit(FLAG_EDGE_FALLING, &desc->flags);
clear_bit(FLAG_IS_HOGGED, &desc->flags);
#ifdef CONFIG_OF_DYNAMIC
desc->hog = NULL;
#endif
#ifdef CONFIG_GPIO_CDEV
WRITE_ONCE(desc->debounce_period_us, 0);
#endif
ret = true;
}
@ -4402,31 +4458,18 @@ static int gpiolib_seq_show(struct seq_file *s, void *v)
return 0;
}
static const struct seq_operations gpiolib_seq_ops = {
static const struct seq_operations gpiolib_sops = {
.start = gpiolib_seq_start,
.next = gpiolib_seq_next,
.stop = gpiolib_seq_stop,
.show = gpiolib_seq_show,
};
static int gpiolib_open(struct inode *inode, struct file *file)
{
return seq_open(file, &gpiolib_seq_ops);
}
static const struct file_operations gpiolib_operations = {
.owner = THIS_MODULE,
.open = gpiolib_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
DEFINE_SEQ_ATTRIBUTE(gpiolib);
static int __init gpiolib_debugfs_init(void)
{
/* /sys/kernel/debug/gpio */
debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
&gpiolib_operations);
debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
return 0;
}
subsys_initcall(gpiolib_debugfs_init);

View File

@ -114,6 +114,8 @@ struct gpio_desc {
#define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
#define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
#define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
#define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */
#define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */
/* Connection label */
const char *label;
@ -122,6 +124,10 @@ struct gpio_desc {
#ifdef CONFIG_OF_DYNAMIC
struct device_node *hog;
#endif
#ifdef CONFIG_GPIO_CDEV
/* debounce period in microseconds */
unsigned int debounce_period_us;
#endif
};
int gpiod_request(struct gpio_desc *desc, const char *label);

View File

@ -23,6 +23,7 @@ config PINCTRL_BCM2835
select PINMUX
select PINCONF
select GENERIC_PINCONF
select GPIOLIB
select GPIOLIB_IRQCHIP
default ARCH_BCM2835 || ARCH_BRCMSTB
help

View File

@ -756,9 +756,6 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
enum gpiod_flags dflags);
void gpiochip_free_own_desc(struct gpio_desc *desc);
void devprop_gpiochip_set_names(struct gpio_chip *gc,
const struct fwnode_handle *fwnode);
#ifdef CONFIG_GPIOLIB
/* lock/unlock as IRQ */

View File

@ -19,6 +19,9 @@ enum tx3589x_block {
#define TC3589x_RSTCTRL_KBDRST (1 << 1)
#define TC3589x_RSTCTRL_GPIRST (1 << 0)
#define TC3589x_DKBDMSK_ELINT (1 << 1)
#define TC3589x_DKBDMSK_EINT (1 << 0)
/* Keyboard Configuration Registers */
#define TC3589x_KBDSETTLE_REG 0x01
#define TC3589x_KBDBOUNCE 0x02
@ -101,6 +104,9 @@ enum tx3589x_block {
#define TC3589x_GPIOODM2 0xE4
#define TC3589x_GPIOODE2 0xE5
#define TC3589x_DIRECT0 0xEC
#define TC3589x_DKBDMSK 0xF3
#define TC3589x_INT_GPIIRQ 0
#define TC3589x_INT_TI0IRQ 1
#define TC3589x_INT_TI1IRQ 2

View File

@ -6,12 +6,14 @@
#ifndef GPIO_DW_APB_H
#define GPIO_DW_APB_H
#define DWAPB_MAX_GPIOS 32
struct dwapb_port_property {
struct fwnode_handle *fwnode;
unsigned int idx;
unsigned int ngpio;
unsigned int gpio_base;
int irq[32];
int irq[DWAPB_MAX_GPIOS];
bool irq_shared;
};

View File

@ -170,6 +170,12 @@ static inline int device_property_count_u64(struct device *dev, const char *prop
return device_property_read_u64_array(dev, propname, NULL, 0);
}
static inline int device_property_string_array_count(struct device *dev,
const char *propname)
{
return device_property_read_string_array(dev, propname, NULL, 0);
}
static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
const char *propname)
{
@ -224,6 +230,13 @@ static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
}
static inline int
fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
const char *propname)
{
return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
}
struct software_node;
/**

View File

@ -94,4 +94,6 @@ char *kstrdup_quotable(const char *src, gfp_t gfp);
char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
void kfree_strarray(char **array, size_t n);
#endif

View File

@ -11,9 +11,17 @@
#ifndef _UAPI_GPIO_H_
#define _UAPI_GPIO_H_
#include <linux/const.h>
#include <linux/ioctl.h>
#include <linux/types.h>
/*
* The maximum size of name and label arrays.
*
* Must be a multiple of 8 to ensure 32/64-bit alignment of structs.
*/
#define GPIO_MAX_NAME_SIZE 32
/**
* struct gpiochip_info - Information about a certain GPIO chip
* @name: the Linux kernel name of this GPIO chip
@ -22,11 +30,273 @@
* @lines: number of GPIO lines on this chip
*/
struct gpiochip_info {
char name[32];
char label[32];
char name[GPIO_MAX_NAME_SIZE];
char label[GPIO_MAX_NAME_SIZE];
__u32 lines;
};
/*
* Maximum number of requested lines.
*
* Must be no greater than 64, as bitmaps are restricted here to 64-bits
* for simplicity, and a multiple of 2 to ensure 32/64-bit alignment of
* structs.
*/
#define GPIO_V2_LINES_MAX 64
/*
* The maximum number of configuration attributes associated with a line
* request.
*/
#define GPIO_V2_LINE_NUM_ATTRS_MAX 10
/**
* enum gpio_v2_line_flag - &struct gpio_v2_line_attribute.flags values
* @GPIO_V2_LINE_FLAG_USED: line is not available for request
* @GPIO_V2_LINE_FLAG_ACTIVE_LOW: line active state is physical low
* @GPIO_V2_LINE_FLAG_INPUT: line is an input
* @GPIO_V2_LINE_FLAG_OUTPUT: line is an output
* @GPIO_V2_LINE_FLAG_EDGE_RISING: line detects rising (inactive to active)
* edges
* @GPIO_V2_LINE_FLAG_EDGE_FALLING: line detects falling (active to
* inactive) edges
* @GPIO_V2_LINE_FLAG_OPEN_DRAIN: line is an open drain output
* @GPIO_V2_LINE_FLAG_OPEN_SOURCE: line is an open source output
* @GPIO_V2_LINE_FLAG_BIAS_PULL_UP: line has pull-up bias enabled
* @GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN: line has pull-down bias enabled
* @GPIO_V2_LINE_FLAG_BIAS_DISABLED: line has bias disabled
*/
enum gpio_v2_line_flag {
GPIO_V2_LINE_FLAG_USED = _BITULL(0),
GPIO_V2_LINE_FLAG_ACTIVE_LOW = _BITULL(1),
GPIO_V2_LINE_FLAG_INPUT = _BITULL(2),
GPIO_V2_LINE_FLAG_OUTPUT = _BITULL(3),
GPIO_V2_LINE_FLAG_EDGE_RISING = _BITULL(4),
GPIO_V2_LINE_FLAG_EDGE_FALLING = _BITULL(5),
GPIO_V2_LINE_FLAG_OPEN_DRAIN = _BITULL(6),
GPIO_V2_LINE_FLAG_OPEN_SOURCE = _BITULL(7),
GPIO_V2_LINE_FLAG_BIAS_PULL_UP = _BITULL(8),
GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN = _BITULL(9),
GPIO_V2_LINE_FLAG_BIAS_DISABLED = _BITULL(10),
};
/**
* struct gpio_v2_line_values - Values of GPIO lines
* @bits: a bitmap containing the value of the lines, set to 1 for active
* and 0 for inactive.
* @mask: a bitmap identifying the lines to get or set, with each bit
* number corresponding to the index into &struct
* gpio_v2_line_request.offsets.
*/
struct gpio_v2_line_values {
__aligned_u64 bits;
__aligned_u64 mask;
};
/**
* enum gpio_v2_line_attr_id - &struct gpio_v2_line_attribute.id values
* identifying which field of the attribute union is in use.
* @GPIO_V2_LINE_ATTR_ID_FLAGS: flags field is in use
* @GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES: values field is in use
* @GPIO_V2_LINE_ATTR_ID_DEBOUNCE: debounce_period_us is in use
*/
enum gpio_v2_line_attr_id {
GPIO_V2_LINE_ATTR_ID_FLAGS = 1,
GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES = 2,
GPIO_V2_LINE_ATTR_ID_DEBOUNCE = 3,
};
/**
* struct gpio_v2_line_attribute - a configurable attribute of a line
* @id: attribute identifier with value from &enum gpio_v2_line_attr_id
* @padding: reserved for future use and must be zero filled
* @flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO
* line, with values from enum gpio_v2_line_flag, such as
* GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed
* together. This overrides the default flags contained in the &struct
* gpio_v2_line_config for the associated line.
* @values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap
* containing the values to which the lines will be set, with each bit
* number corresponding to the index into &struct
* gpio_v2_line_request.offsets.
* @debounce_period_us: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired
* debounce period, in microseconds
*/
struct gpio_v2_line_attribute {
__u32 id;
__u32 padding;
union {
__aligned_u64 flags;
__aligned_u64 values;
__u32 debounce_period_us;
};
};
/**
* struct gpio_v2_line_config_attribute - a configuration attribute
* associated with one or more of the requested lines.
* @attr: the configurable attribute
* @mask: a bitmap identifying the lines to which the attribute applies,
* with each bit number corresponding to the index into &struct
* gpio_v2_line_request.offsets.
*/
struct gpio_v2_line_config_attribute {
struct gpio_v2_line_attribute attr;
__aligned_u64 mask;
};
/**
* struct gpio_v2_line_config - Configuration for GPIO lines
* @flags: flags for the GPIO lines, with values from enum
* gpio_v2_line_flag, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW,
* GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together. This is the default for
* all requested lines but may be overridden for particular lines using
* attrs.
* @num_attrs: the number of attributes in attrs
* @padding: reserved for future use and must be zero filled
* @attrs: the configuration attributes associated with the requested
* lines. Any attribute should only be associated with a particular line
* once. If an attribute is associated with a line multiple times then the
* first occurrence (i.e. lowest index) has precedence.
*/
struct gpio_v2_line_config {
__aligned_u64 flags;
__u32 num_attrs;
/* Pad to fill implicit padding and reserve space for future use. */
__u32 padding[5];
struct gpio_v2_line_config_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX];
};
/**
* struct gpio_v2_line_request - Information about a request for GPIO lines
* @offsets: an array of desired lines, specified by offset index for the
* associated GPIO chip
* @consumer: a desired consumer label for the selected GPIO lines such as
* "my-bitbanged-relay"
* @config: requested configuration for the lines.
* @num_lines: number of lines requested in this request, i.e. the number
* of valid fields in the GPIO_V2_LINES_MAX sized arrays, set to 1 to
* request a single line
* @event_buffer_size: a suggested minimum number of line events that the
* kernel should buffer. This is only relevant if edge detection is
* enabled in the configuration. Note that this is only a suggested value
* and the kernel may allocate a larger buffer or cap the size of the
* buffer. If this field is zero then the buffer size defaults to a minimum
* of num_lines*16.
* @padding: reserved for future use and must be zero filled
* @fd: if successful this field will contain a valid anonymous file handle
* after a GPIO_GET_LINE_IOCTL operation, zero or negative value means
* error
*/
struct gpio_v2_line_request {
__u32 offsets[GPIO_V2_LINES_MAX];
char consumer[GPIO_MAX_NAME_SIZE];
struct gpio_v2_line_config config;
__u32 num_lines;
__u32 event_buffer_size;
/* Pad to fill implicit padding and reserve space for future use. */
__u32 padding[5];
__s32 fd;
};
/**
* struct gpio_v2_line_info - Information about a certain GPIO line
* @name: the name of this GPIO line, such as the output pin of the line on
* the chip, a rail or a pin header name on a board, as specified by the
* GPIO chip, may be empty
* @consumer: a functional name for the consumer of this GPIO line as set
* by whatever is using it, will be empty if there is no current user but
* may also be empty if the consumer doesn't set this up
* @flags: flags for the GPIO line, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW,
* GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together
* @offset: the local offset on this GPIO chip, fill this in when
* requesting the line information from the kernel
* @num_attrs: the number of attributes in attrs
* @attrs: the configuration attributes associated with the line
* @padding: reserved for future use
*/
struct gpio_v2_line_info {
char name[GPIO_MAX_NAME_SIZE];
char consumer[GPIO_MAX_NAME_SIZE];
__u32 offset;
__u32 num_attrs;
__aligned_u64 flags;
struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX];
/* Space reserved for future use. */
__u32 padding[4];
};
/**
* enum gpio_v2_line_changed_type - &struct gpio_v2_line_changed.event_type
* values
* @GPIO_V2_LINE_CHANGED_REQUESTED: line has been requested
* @GPIO_V2_LINE_CHANGED_RELEASED: line has been released
* @GPIO_V2_LINE_CHANGED_CONFIG: line has been reconfigured
*/
enum gpio_v2_line_changed_type {
GPIO_V2_LINE_CHANGED_REQUESTED = 1,
GPIO_V2_LINE_CHANGED_RELEASED = 2,
GPIO_V2_LINE_CHANGED_CONFIG = 3,
};
/**
* struct gpio_v2_line_info_changed - Information about a change in status
* of a GPIO line
* @info: updated line information
* @timestamp_ns: estimate of time of status change occurrence, in nanoseconds
* @event_type: the type of change with a value from enum
* gpio_v2_line_changed_type
* @padding: reserved for future use
*/
struct gpio_v2_line_info_changed {
struct gpio_v2_line_info info;
__aligned_u64 timestamp_ns;
__u32 event_type;
/* Pad struct to 64-bit boundary and reserve space for future use. */
__u32 padding[5];
};
/**
* enum gpio_v2_line_event_id - &struct gpio_v2_line_event.id values
* @GPIO_V2_LINE_EVENT_RISING_EDGE: event triggered by a rising edge
* @GPIO_V2_LINE_EVENT_FALLING_EDGE: event triggered by a falling edge
*/
enum gpio_v2_line_event_id {
GPIO_V2_LINE_EVENT_RISING_EDGE = 1,
GPIO_V2_LINE_EVENT_FALLING_EDGE = 2,
};
/**
* struct gpio_v2_line_event - The actual event being pushed to userspace
* @timestamp_ns: best estimate of time of event occurrence, in nanoseconds.
* The timestamp_ns is read from CLOCK_MONOTONIC and is intended to allow the
* accurate measurement of the time between events. It does not provide
* the wall-clock time.
* @id: event identifier with value from enum gpio_v2_line_event_id
* @offset: the offset of the line that triggered the event
* @seqno: the sequence number for this event in the sequence of events for
* all the lines in this line request
* @line_seqno: the sequence number for this event in the sequence of
* events on this particular line
* @padding: reserved for future use
*/
struct gpio_v2_line_event {
__aligned_u64 timestamp_ns;
__u32 id;
__u32 offset;
__u32 seqno;
__u32 line_seqno;
/* Space reserved for future use. */
__u32 padding[6];
};
/*
* ABI v1
*
* This version of the ABI is deprecated.
* Use the latest version of the ABI, defined above, instead.
*/
/* Informational flags */
#define GPIOLINE_FLAG_KERNEL (1UL << 0) /* Line used by the kernel */
#define GPIOLINE_FLAG_IS_OUT (1UL << 1)
@ -48,12 +318,15 @@ struct gpiochip_info {
* @consumer: a functional name for the consumer of this GPIO line as set by
* whatever is using it, will be empty if there is no current user but may
* also be empty if the consumer doesn't set this up
*
* This struct is part of ABI v1 and is deprecated.
* Use struct gpio_v2_line_info instead.
*/
struct gpioline_info {
__u32 line_offset;
__u32 flags;
char name[32];
char consumer[32];
char name[GPIO_MAX_NAME_SIZE];
char consumer[GPIO_MAX_NAME_SIZE];
};
/* Maximum number of requested handles */
@ -79,6 +352,9 @@ enum {
* guarantee there are no implicit holes between it and subsequent members.
* The 20-byte padding at the end makes sure we don't add any implicit padding
* at the end of the structure on 64-bit architectures.
*
* This struct is part of ABI v1 and is deprecated.
* Use struct gpio_v2_line_info_changed instead.
*/
struct gpioline_info_changed {
struct gpioline_info info;
@ -118,12 +394,15 @@ struct gpioline_info_changed {
* @fd: if successful this field will contain a valid anonymous file handle
* after a GPIO_GET_LINEHANDLE_IOCTL operation, zero or negative value
* means error
*
* This struct is part of ABI v1 and is deprecated.
* Use struct gpio_v2_line_request instead.
*/
struct gpiohandle_request {
__u32 lineoffsets[GPIOHANDLES_MAX];
__u32 flags;
__u8 default_values[GPIOHANDLES_MAX];
char consumer_label[32];
char consumer_label[GPIO_MAX_NAME_SIZE];
__u32 lines;
int fd;
};
@ -137,6 +416,9 @@ struct gpiohandle_request {
* this specifies the default output value, should be 0 (low) or
* 1 (high), anything else than 0 or 1 will be interpreted as 1 (high)
* @padding: reserved for future use and should be zero filled
*
* This struct is part of ABI v1 and is deprecated.
* Use struct gpio_v2_line_config instead.
*/
struct gpiohandle_config {
__u32 flags;
@ -144,21 +426,19 @@ struct gpiohandle_config {
__u32 padding[4]; /* padding for future use */
};
#define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0a, struct gpiohandle_config)
/**
* struct gpiohandle_data - Information of values on a GPIO handle
* @values: when getting the state of lines this contains the current
* state of a line, when setting the state of lines these should contain
* the desired target state
*
* This struct is part of ABI v1 and is deprecated.
* Use struct gpio_v2_line_values instead.
*/
struct gpiohandle_data {
__u8 values[GPIOHANDLES_MAX];
};
#define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data)
#define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data)
/* Eventrequest flags */
#define GPIOEVENT_REQUEST_RISING_EDGE (1UL << 0)
#define GPIOEVENT_REQUEST_FALLING_EDGE (1UL << 1)
@ -177,12 +457,15 @@ struct gpiohandle_data {
* @fd: if successful this field will contain a valid anonymous file handle
* after a GPIO_GET_LINEEVENT_IOCTL operation, zero or negative value
* means error
*
* This struct is part of ABI v1 and is deprecated.
* Use struct gpio_v2_line_request instead.
*/
struct gpioevent_request {
__u32 lineoffset;
__u32 handleflags;
__u32 eventflags;
char consumer_label[32];
char consumer_label[GPIO_MAX_NAME_SIZE];
int fd;
};
@ -196,17 +479,42 @@ struct gpioevent_request {
* struct gpioevent_data - The actual event being pushed to userspace
* @timestamp: best estimate of time of event occurrence, in nanoseconds
* @id: event identifier
*
* This struct is part of ABI v1 and is deprecated.
* Use struct gpio_v2_line_event instead.
*/
struct gpioevent_data {
__u64 timestamp;
__u32 id;
};
/*
* v1 and v2 ioctl()s
*/
#define GPIO_GET_CHIPINFO_IOCTL _IOR(0xB4, 0x01, struct gpiochip_info)
#define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0C, __u32)
/*
* v2 ioctl()s
*/
#define GPIO_V2_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x05, struct gpio_v2_line_info)
#define GPIO_V2_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x06, struct gpio_v2_line_info)
#define GPIO_V2_GET_LINE_IOCTL _IOWR(0xB4, 0x07, struct gpio_v2_line_request)
#define GPIO_V2_LINE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0D, struct gpio_v2_line_config)
#define GPIO_V2_LINE_GET_VALUES_IOCTL _IOWR(0xB4, 0x0E, struct gpio_v2_line_values)
#define GPIO_V2_LINE_SET_VALUES_IOCTL _IOWR(0xB4, 0x0F, struct gpio_v2_line_values)
/*
* v1 ioctl()s
*
* These ioctl()s are deprecated. Use the v2 equivalent instead.
*/
#define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info)
#define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0b, struct gpioline_info)
#define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0c, __u32)
#define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request)
#define GPIO_GET_LINEEVENT_IOCTL _IOWR(0xB4, 0x04, struct gpioevent_request)
#define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data)
#define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data)
#define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0A, struct gpiohandle_config)
#define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0B, struct gpioline_info)
#endif /* _UAPI_GPIO_H_ */

View File

@ -649,3 +649,26 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
return pathname;
}
EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
/**
* kfree_strarray - free a number of dynamically allocated strings contained
* in an array and the array itself
*
* @array: Dynamically allocated array of strings to free.
* @n: Number of strings (starting from the beginning of the array) to free.
*
* Passing a non-NULL @array and @n == 0 as well as NULL @array are valid
* use-cases. If @array is NULL, the function does nothing.
*/
void kfree_strarray(char **array, size_t n)
{
unsigned int i;
if (!array)
return;
for (i = 0; i < n; i++)
kfree(array[i]);
kfree(array);
}
EXPORT_SYMBOL_GPL(kfree_strarray);

View File

@ -23,17 +23,17 @@
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/gpio.h>
#include "gpio-utils.h"
int monitor_device(const char *device_name,
unsigned int line,
uint32_t handleflags,
uint32_t eventflags,
unsigned int *lines,
unsigned int num_lines,
struct gpio_v2_line_config *config,
unsigned int loops)
{
struct gpioevent_request req;
struct gpiohandle_data data;
struct gpio_v2_line_values values;
char *chrdev_name;
int fd;
int cfd, lfd;
int ret;
int i = 0;
@ -41,44 +41,55 @@ int monitor_device(const char *device_name,
if (ret < 0)
return -ENOMEM;
fd = open(chrdev_name, 0);
if (fd == -1) {
cfd = open(chrdev_name, 0);
if (cfd == -1) {
ret = -errno;
fprintf(stderr, "Failed to open %s\n", chrdev_name);
goto exit_free_name;
}
req.lineoffset = line;
req.handleflags = handleflags;
req.eventflags = eventflags;
strcpy(req.consumer_label, "gpio-event-mon");
ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req);
if (ret == -1) {
ret = -errno;
fprintf(stderr, "Failed to issue GET EVENT "
"IOCTL (%d)\n",
ret);
goto exit_close_error;
}
ret = gpiotools_request_line(device_name, lines, num_lines, config,
"gpio-event-mon");
if (ret < 0)
goto exit_device_close;
else
lfd = ret;
/* Read initial states */
ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
if (ret == -1) {
ret = -errno;
fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
"VALUES IOCTL (%d)\n",
values.mask = 0;
values.bits = 0;
for (i = 0; i < num_lines; i++)
gpiotools_set_bit(&values.mask, i);
ret = gpiotools_get_values(lfd, &values);
if (ret < 0) {
fprintf(stderr,
"Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n",
ret);
goto exit_close_error;
goto exit_line_close;
}
fprintf(stdout, "Monitoring line %d on %s\n", line, device_name);
fprintf(stdout, "Initial line value: %d\n", data.values[0]);
if (num_lines == 1) {
fprintf(stdout, "Monitoring line %d on %s\n", lines[0], device_name);
fprintf(stdout, "Initial line value: %d\n",
gpiotools_test_bit(values.bits, 0));
} else {
fprintf(stdout, "Monitoring lines %d", lines[0]);
for (i = 1; i < num_lines - 1; i++)
fprintf(stdout, ", %d", lines[i]);
fprintf(stdout, " and %d on %s\n", lines[i], device_name);
fprintf(stdout, "Initial line values: %d",
gpiotools_test_bit(values.bits, 0));
for (i = 1; i < num_lines - 1; i++)
fprintf(stdout, ", %d",
gpiotools_test_bit(values.bits, i));
fprintf(stdout, " and %d\n",
gpiotools_test_bit(values.bits, i));
}
while (1) {
struct gpioevent_data event;
struct gpio_v2_line_event event;
ret = read(req.fd, &event, sizeof(event));
ret = read(lfd, &event, sizeof(event));
if (ret == -1) {
if (errno == -EAGAIN) {
fprintf(stderr, "nothing available\n");
@ -96,12 +107,14 @@ int monitor_device(const char *device_name,
ret = -EIO;
break;
}
fprintf(stdout, "GPIO EVENT %llu: ", event.timestamp);
fprintf(stdout, "GPIO EVENT at %llu on line %d (%d|%d) ",
event.timestamp_ns, event.offset, event.line_seqno,
event.seqno);
switch (event.id) {
case GPIOEVENT_EVENT_RISING_EDGE:
case GPIO_V2_LINE_EVENT_RISING_EDGE:
fprintf(stdout, "rising edge");
break;
case GPIOEVENT_EVENT_FALLING_EDGE:
case GPIO_V2_LINE_EVENT_FALLING_EDGE:
fprintf(stdout, "falling edge");
break;
default:
@ -114,8 +127,11 @@ int monitor_device(const char *device_name,
break;
}
exit_close_error:
if (close(fd) == -1)
exit_line_close:
if (close(lfd) == -1)
perror("Failed to close line file");
exit_device_close:
if (close(cfd) == -1)
perror("Failed to close GPIO character device file");
exit_free_name:
free(chrdev_name);
@ -127,29 +143,37 @@ void print_usage(void)
fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
"Listen to events on GPIO lines, 0->1 1->0\n"
" -n <name> Listen on GPIOs on a named device (must be stated)\n"
" -o <n> Offset to monitor\n"
" -o <n> Offset of line to monitor (may be repeated)\n"
" -d Set line as open drain\n"
" -s Set line as open source\n"
" -r Listen for rising edges\n"
" -f Listen for falling edges\n"
" -b <n> Debounce the line with period n microseconds\n"
" [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
" -? This helptext\n"
"\n"
"Example:\n"
"gpio-event-mon -n gpiochip0 -o 4 -r -f\n"
"gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n"
);
}
#define EDGE_FLAGS \
(GPIO_V2_LINE_FLAG_EDGE_RISING | \
GPIO_V2_LINE_FLAG_EDGE_FALLING)
int main(int argc, char **argv)
{
const char *device_name = NULL;
unsigned int line = -1;
unsigned int lines[GPIO_V2_LINES_MAX];
unsigned int num_lines = 0;
unsigned int loops = 0;
uint32_t handleflags = GPIOHANDLE_REQUEST_INPUT;
uint32_t eventflags = 0;
int c;
struct gpio_v2_line_config config;
int c, attr, i;
unsigned long debounce_period_us = 0;
while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) {
memset(&config, 0, sizeof(config));
config.flags = GPIO_V2_LINE_FLAG_INPUT;
while ((c = getopt(argc, argv, "c:n:o:b:dsrf?")) != -1) {
switch (c) {
case 'c':
loops = strtoul(optarg, NULL, 10);
@ -158,19 +182,27 @@ int main(int argc, char **argv)
device_name = optarg;
break;
case 'o':
line = strtoul(optarg, NULL, 10);
if (num_lines >= GPIO_V2_LINES_MAX) {
print_usage();
return -1;
}
lines[num_lines] = strtoul(optarg, NULL, 10);
num_lines++;
break;
case 'b':
debounce_period_us = strtoul(optarg, NULL, 10);
break;
case 'd':
handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN;
config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
break;
case 's':
handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE;
config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
break;
case 'r':
eventflags |= GPIOEVENT_REQUEST_RISING_EDGE;
config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
break;
case 'f':
eventflags |= GPIOEVENT_REQUEST_FALLING_EDGE;
config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
break;
case '?':
print_usage();
@ -178,15 +210,23 @@ int main(int argc, char **argv)
}
}
if (!device_name || line == -1) {
if (debounce_period_us) {
attr = config.num_attrs;
config.num_attrs++;
for (i = 0; i < num_lines; i++)
gpiotools_set_bit(&config.attrs[attr].mask, i);
config.attrs[attr].attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
config.attrs[attr].attr.debounce_period_us = debounce_period_us;
}
if (!device_name || num_lines == 0) {
print_usage();
return -1;
}
if (!eventflags) {
if (!(config.flags & EDGE_FLAGS)) {
printf("No flags specified, listening on both rising and "
"falling edges\n");
eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
config.flags |= EDGE_FLAGS;
}
return monitor_device(device_name, line, handleflags,
eventflags, loops);
return monitor_device(device_name, lines, num_lines, &config, loops);
}

View File

@ -22,39 +22,46 @@
#include <linux/gpio.h>
#include "gpio-utils.h"
int hammer_device(const char *device_name, unsigned int *lines, int nlines,
int hammer_device(const char *device_name, unsigned int *lines, int num_lines,
unsigned int loops)
{
struct gpiohandle_data data;
struct gpio_v2_line_values values;
struct gpio_v2_line_config config;
char swirr[] = "-\\|/";
int fd;
int ret;
int i, j;
unsigned int iteration = 0;
memset(&data.values, 0, sizeof(data.values));
ret = gpiotools_request_linehandle(device_name, lines, nlines,
GPIOHANDLE_REQUEST_OUTPUT, &data,
"gpio-hammer");
memset(&config, 0, sizeof(config));
config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
ret = gpiotools_request_line(device_name, lines, num_lines,
&config, "gpio-hammer");
if (ret < 0)
goto exit_error;
else
fd = ret;
ret = gpiotools_get_values(fd, &data);
values.mask = 0;
values.bits = 0;
for (i = 0; i < num_lines; i++)
gpiotools_set_bit(&values.mask, i);
ret = gpiotools_get_values(fd, &values);
if (ret < 0)
goto exit_close_error;
fprintf(stdout, "Hammer lines [");
for (i = 0; i < nlines; i++) {
for (i = 0; i < num_lines; i++) {
fprintf(stdout, "%d", lines[i]);
if (i != (nlines - 1))
if (i != (num_lines - 1))
fprintf(stdout, ", ");
}
fprintf(stdout, "] on %s, initial states: [", device_name);
for (i = 0; i < nlines; i++) {
fprintf(stdout, "%d", data.values[i]);
if (i != (nlines - 1))
for (i = 0; i < num_lines; i++) {
fprintf(stdout, "%d", gpiotools_test_bit(values.bits, i));
if (i != (num_lines - 1))
fprintf(stdout, ", ");
}
fprintf(stdout, "]\n");
@ -63,15 +70,15 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines,
j = 0;
while (1) {
/* Invert all lines so we blink */
for (i = 0; i < nlines; i++)
data.values[i] = !data.values[i];
for (i = 0; i < num_lines; i++)
gpiotools_change_bit(&values.bits, i);
ret = gpiotools_set_values(fd, &data);
ret = gpiotools_set_values(fd, &values);
if (ret < 0)
goto exit_close_error;
/* Re-read values to get status */
ret = gpiotools_get_values(fd, &data);
ret = gpiotools_get_values(fd, &values);
if (ret < 0)
goto exit_close_error;
@ -81,9 +88,10 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines,
j = 0;
fprintf(stdout, "[");
for (i = 0; i < nlines; i++) {
fprintf(stdout, "%d: %d", lines[i], data.values[i]);
if (i != (nlines - 1))
for (i = 0; i < num_lines; i++) {
fprintf(stdout, "%d: %d", lines[i],
gpiotools_test_bit(values.bits, i));
if (i != (num_lines - 1))
fprintf(stdout, ", ");
}
fprintf(stdout, "]\r");
@ -97,7 +105,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines,
ret = 0;
exit_close_error:
gpiotools_release_linehandle(fd);
gpiotools_release_line(fd);
exit_error:
return ret;
}
@ -121,7 +129,7 @@ int main(int argc, char **argv)
const char *device_name = NULL;
unsigned int lines[GPIOHANDLES_MAX];
unsigned int loops = 0;
int nlines;
int num_lines;
int c;
int i;
@ -158,11 +166,11 @@ int main(int argc, char **argv)
return -1;
}
nlines = i;
num_lines = i;
if (!device_name || !nlines) {
if (!device_name || !num_lines) {
print_usage();
return -1;
}
return hammer_device(device_name, lines, nlines, loops);
return hammer_device(device_name, lines, num_lines, loops);
}

View File

@ -38,7 +38,7 @@
* such as "gpiochip0"
* @lines: An array desired lines, specified by offset
* index for the associated GPIO device.
* @nline: The number of lines to request.
* @num_lines: The number of lines to request.
* @flag: The new flag for requsted gpio. Reference
* "linux/gpio.h" for the meaning of flag.
* @data: Default value will be set to gpio when flag is
@ -56,7 +56,7 @@
* On failure return the errno.
*/
int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
unsigned int nlines, unsigned int flag,
unsigned int num_lines, unsigned int flag,
struct gpiohandle_data *data,
const char *consumer_label)
{
@ -78,12 +78,12 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
goto exit_free_name;
}
for (i = 0; i < nlines; i++)
for (i = 0; i < num_lines; i++)
req.lineoffsets[i] = lines[i];
req.flags = flag;
strcpy(req.consumer_label, consumer_label);
req.lines = nlines;
req.lines = num_lines;
if (flag & GPIOHANDLE_REQUEST_OUTPUT)
memcpy(req.default_values, data, sizeof(req.default_values));
@ -100,20 +100,87 @@ exit_free_name:
free(chrdev_name);
return ret < 0 ? ret : req.fd;
}
/**
* gpiotools_request_line() - request gpio lines in a gpiochip
* @device_name: The name of gpiochip without prefix "/dev/",
* such as "gpiochip0"
* @lines: An array desired lines, specified by offset
* index for the associated GPIO device.
* @num_lines: The number of lines to request.
* @config: The new config for requested gpio. Reference
* "linux/gpio.h" for config details.
* @consumer: The name of consumer, such as "sysfs",
* "powerkey". This is useful for other users to
* know who is using.
*
* Request gpio lines through the ioctl provided by chardev. User
* could call gpiotools_set_values() and gpiotools_get_values() to
* read and write respectively through the returned fd. Call
* gpiotools_release_line() to release these lines after that.
*
* Return: On success return the fd;
* On failure return the errno.
*/
int gpiotools_request_line(const char *device_name, unsigned int *lines,
unsigned int num_lines,
struct gpio_v2_line_config *config,
const char *consumer)
{
struct gpio_v2_line_request req;
char *chrdev_name;
int fd;
int i;
int ret;
ret = asprintf(&chrdev_name, "/dev/%s", device_name);
if (ret < 0)
return -ENOMEM;
fd = open(chrdev_name, 0);
if (fd == -1) {
ret = -errno;
fprintf(stderr, "Failed to open %s, %s\n",
chrdev_name, strerror(errno));
goto exit_free_name;
}
memset(&req, 0, sizeof(req));
for (i = 0; i < num_lines; i++)
req.offsets[i] = lines[i];
req.config = *config;
strcpy(req.consumer, consumer);
req.num_lines = num_lines;
ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req);
if (ret == -1) {
ret = -errno;
fprintf(stderr, "Failed to issue %s (%d), %s\n",
"GPIO_GET_LINE_IOCTL", ret, strerror(errno));
}
if (close(fd) == -1)
perror("Failed to close GPIO character device file");
exit_free_name:
free(chrdev_name);
return ret < 0 ? ret : req.fd;
}
/**
* gpiotools_set_values(): Set the value of gpio(s)
* @fd: The fd returned by
* gpiotools_request_linehandle().
* @data: The array of values want to set.
* gpiotools_request_line().
* @values: The array of values want to set.
*
* Return: On success return 0;
* On failure return the errno.
*/
int gpiotools_set_values(const int fd, struct gpiohandle_data *data)
int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values)
{
int ret;
ret = ioctl(fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, data);
ret = ioctl(fd, GPIO_V2_LINE_SET_VALUES_IOCTL, values);
if (ret == -1) {
ret = -errno;
fprintf(stderr, "Failed to issue %s (%d), %s\n",
@ -127,17 +194,17 @@ int gpiotools_set_values(const int fd, struct gpiohandle_data *data)
/**
* gpiotools_get_values(): Get the value of gpio(s)
* @fd: The fd returned by
* gpiotools_request_linehandle().
* @data: The array of values get from hardware.
* gpiotools_request_line().
* @values: The array of values get from hardware.
*
* Return: On success return 0;
* On failure return the errno.
*/
int gpiotools_get_values(const int fd, struct gpiohandle_data *data)
int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values)
{
int ret;
ret = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, data);
ret = ioctl(fd, GPIO_V2_LINE_GET_VALUES_IOCTL, values);
if (ret == -1) {
ret = -errno;
fprintf(stderr, "Failed to issue %s (%d), %s\n",
@ -169,6 +236,27 @@ int gpiotools_release_linehandle(const int fd)
return ret;
}
/**
* gpiotools_release_line(): Release the line(s) of gpiochip
* @fd: The fd returned by
* gpiotools_request_line().
*
* Return: On success return 0;
* On failure return the errno.
*/
int gpiotools_release_line(const int fd)
{
int ret;
ret = close(fd);
if (ret == -1) {
perror("Failed to close GPIO LINE device file");
ret = -errno;
}
return ret;
}
/**
* gpiotools_get(): Get value from specific line
* @device_name: The name of gpiochip without prefix "/dev/",
@ -180,11 +268,14 @@ int gpiotools_release_linehandle(const int fd)
*/
int gpiotools_get(const char *device_name, unsigned int line)
{
struct gpiohandle_data data;
int ret;
unsigned int value;
unsigned int lines[] = {line};
gpiotools_gets(device_name, lines, 1, &data);
return data.values[0];
ret = gpiotools_gets(device_name, lines, 1, &value);
if (ret)
return ret;
return value;
}
@ -194,28 +285,36 @@ int gpiotools_get(const char *device_name, unsigned int line)
* such as "gpiochip0".
* @lines: An array desired lines, specified by offset
* index for the associated GPIO device.
* @nline: The number of lines to request.
* @data: The array of values get from gpiochip.
* @num_lines: The number of lines to request.
* @values: The array of values get from gpiochip.
*
* Return: On success return 0;
* On failure return the errno.
*/
int gpiotools_gets(const char *device_name, unsigned int *lines,
unsigned int nlines, struct gpiohandle_data *data)
unsigned int num_lines, unsigned int *values)
{
int fd;
int fd, i;
int ret;
int ret_close;
struct gpio_v2_line_config config;
struct gpio_v2_line_values lv;
ret = gpiotools_request_linehandle(device_name, lines, nlines,
GPIOHANDLE_REQUEST_INPUT, data,
CONSUMER);
memset(&config, 0, sizeof(config));
config.flags = GPIO_V2_LINE_FLAG_INPUT;
ret = gpiotools_request_line(device_name, lines, num_lines,
&config, CONSUMER);
if (ret < 0)
return ret;
fd = ret;
ret = gpiotools_get_values(fd, data);
ret_close = gpiotools_release_linehandle(fd);
for (i = 0; i < num_lines; i++)
gpiotools_set_bit(&lv.mask, i);
ret = gpiotools_get_values(fd, &lv);
if (!ret)
for (i = 0; i < num_lines; i++)
values[i] = gpiotools_test_bit(lv.bits, i);
ret_close = gpiotools_release_line(fd);
return ret < 0 ? ret : ret_close;
}
@ -232,11 +331,9 @@ int gpiotools_gets(const char *device_name, unsigned int *lines,
int gpiotools_set(const char *device_name, unsigned int line,
unsigned int value)
{
struct gpiohandle_data data;
unsigned int lines[] = {line};
data.values[0] = value;
return gpiotools_sets(device_name, lines, 1, &data);
return gpiotools_sets(device_name, lines, 1, &value);
}
/**
@ -245,23 +342,32 @@ int gpiotools_set(const char *device_name, unsigned int line,
* such as "gpiochip0".
* @lines: An array desired lines, specified by offset
* index for the associated GPIO device.
* @nline: The number of lines to request.
* @data: The array of values set to gpiochip, must be
* @num_lines: The number of lines to request.
* @value: The array of values set to gpiochip, must be
* 0(low) or 1(high).
*
* Return: On success return 0;
* On failure return the errno.
*/
int gpiotools_sets(const char *device_name, unsigned int *lines,
unsigned int nlines, struct gpiohandle_data *data)
unsigned int num_lines, unsigned int *values)
{
int ret;
int ret, i;
struct gpio_v2_line_config config;
ret = gpiotools_request_linehandle(device_name, lines, nlines,
GPIOHANDLE_REQUEST_OUTPUT, data,
CONSUMER);
memset(&config, 0, sizeof(config));
config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
config.num_attrs = 1;
config.attrs[0].attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES;
for (i = 0; i < num_lines; i++) {
gpiotools_set_bit(&config.attrs[0].mask, i);
gpiotools_assign_bit(&config.attrs[0].attr.values,
i, values[i]);
}
ret = gpiotools_request_line(device_name, lines, num_lines,
&config, CONSUMER);
if (ret < 0)
return ret;
return gpiotools_release_linehandle(ret);
return gpiotools_release_line(ret);
}

View File

@ -12,7 +12,9 @@
#ifndef _GPIO_UTILS_H_
#define _GPIO_UTILS_H_
#include <stdbool.h>
#include <string.h>
#include <linux/types.h>
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
@ -23,19 +25,55 @@ static inline int check_prefix(const char *str, const char *prefix)
}
int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
unsigned int nlines, unsigned int flag,
unsigned int num_lines, unsigned int flag,
struct gpiohandle_data *data,
const char *consumer_label);
int gpiotools_set_values(const int fd, struct gpiohandle_data *data);
int gpiotools_get_values(const int fd, struct gpiohandle_data *data);
int gpiotools_release_linehandle(const int fd);
int gpiotools_request_line(const char *device_name,
unsigned int *lines,
unsigned int num_lines,
struct gpio_v2_line_config *config,
const char *consumer);
int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values);
int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values);
int gpiotools_release_line(const int fd);
int gpiotools_get(const char *device_name, unsigned int line);
int gpiotools_gets(const char *device_name, unsigned int *lines,
unsigned int nlines, struct gpiohandle_data *data);
unsigned int num_lines, unsigned int *values);
int gpiotools_set(const char *device_name, unsigned int line,
unsigned int value);
int gpiotools_sets(const char *device_name, unsigned int *lines,
unsigned int nlines, struct gpiohandle_data *data);
unsigned int num_lines, unsigned int *values);
/* helper functions for gpio_v2_line_values bits */
static inline void gpiotools_set_bit(__u64 *b, int n)
{
*b |= _BITULL(n);
}
static inline void gpiotools_change_bit(__u64 *b, int n)
{
*b ^= _BITULL(n);
}
static inline void gpiotools_clear_bit(__u64 *b, int n)
{
*b &= ~_BITULL(n);
}
static inline int gpiotools_test_bit(__u64 b, int n)
{
return !!(b & _BITULL(n));
}
static inline void gpiotools_assign_bit(__u64 *b, int n, bool value)
{
if (value)
gpiotools_set_bit(b, n);
else
gpiotools_clear_bit(b, n);
}
#endif /* _GPIO_UTILS_H_ */

View File

@ -21,8 +21,8 @@
int main(int argc, char **argv)
{
struct gpioline_info_changed chg;
struct gpioline_info req;
struct gpio_v2_line_info_changed chg;
struct gpio_v2_line_info req;
struct pollfd pfd;
int fd, i, j, ret;
char *event, *end;
@ -40,11 +40,11 @@ int main(int argc, char **argv)
for (i = 0, j = 2; i < argc - 2; i++, j++) {
memset(&req, 0, sizeof(req));
req.line_offset = strtoul(argv[j], &end, 0);
req.offset = strtoul(argv[j], &end, 0);
if (*end != '\0')
goto err_usage;
ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req);
ret = ioctl(fd, GPIO_V2_GET_LINEINFO_WATCH_IOCTL, &req);
if (ret) {
perror("unable to set up line watch");
return EXIT_FAILURE;
@ -71,13 +71,13 @@ int main(int argc, char **argv)
}
switch (chg.event_type) {
case GPIOLINE_CHANGED_REQUESTED:
case GPIO_V2_LINE_CHANGED_REQUESTED:
event = "requested";
break;
case GPIOLINE_CHANGED_RELEASED:
case GPIO_V2_LINE_CHANGED_RELEASED:
event = "released";
break;
case GPIOLINE_CHANGED_CONFIG:
case GPIO_V2_LINE_CHANGED_CONFIG:
event = "config changed";
break;
default:
@ -87,7 +87,7 @@ int main(int argc, char **argv)
}
printf("line %u: %s at %llu\n",
chg.info.line_offset, event, chg.timestamp);
chg.info.offset, event, chg.timestamp_ns);
}
}

View File

@ -25,57 +25,73 @@
struct gpio_flag {
char *name;
unsigned long mask;
unsigned long long mask;
};
struct gpio_flag flagnames[] = {
{
.name = "kernel",
.mask = GPIOLINE_FLAG_KERNEL,
.name = "used",
.mask = GPIO_V2_LINE_FLAG_USED,
},
{
.name = "input",
.mask = GPIO_V2_LINE_FLAG_INPUT,
},
{
.name = "output",
.mask = GPIOLINE_FLAG_IS_OUT,
.mask = GPIO_V2_LINE_FLAG_OUTPUT,
},
{
.name = "active-low",
.mask = GPIOLINE_FLAG_ACTIVE_LOW,
.mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW,
},
{
.name = "open-drain",
.mask = GPIOLINE_FLAG_OPEN_DRAIN,
.mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN,
},
{
.name = "open-source",
.mask = GPIOLINE_FLAG_OPEN_SOURCE,
.mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE,
},
{
.name = "pull-up",
.mask = GPIOLINE_FLAG_BIAS_PULL_UP,
.mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP,
},
{
.name = "pull-down",
.mask = GPIOLINE_FLAG_BIAS_PULL_DOWN,
.mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN,
},
{
.name = "bias-disabled",
.mask = GPIOLINE_FLAG_BIAS_DISABLE,
.mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED,
},
};
void print_flags(unsigned long flags)
static void print_attributes(struct gpio_v2_line_info *info)
{
int i;
int printed = 0;
const char *field_format = "%s";
for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
if (flags & flagnames[i].mask) {
if (printed)
fprintf(stdout, " ");
fprintf(stdout, "%s", flagnames[i].name);
printed++;
if (info->flags & flagnames[i].mask) {
fprintf(stdout, field_format, flagnames[i].name);
field_format = ", %s";
}
}
if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) &&
(info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING))
fprintf(stdout, field_format, "both-edges");
else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING)
fprintf(stdout, field_format, "rising-edge");
else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
fprintf(stdout, field_format, "falling-edge");
for (i = 0; i < info->num_attrs; i++) {
if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE)
fprintf(stdout, ", debounce_period=%dusec",
info->attrs[0].debounce_period_us);
}
}
int list_device(const char *device_name)
@ -109,18 +125,18 @@ int list_device(const char *device_name)
/* Loop over the lines and print info */
for (i = 0; i < cinfo.lines; i++) {
struct gpioline_info linfo;
struct gpio_v2_line_info linfo;
memset(&linfo, 0, sizeof(linfo));
linfo.line_offset = i;
linfo.offset = i;
ret = ioctl(fd, GPIO_GET_LINEINFO_IOCTL, &linfo);
ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
if (ret == -1) {
ret = -errno;
perror("Failed to issue LINEINFO IOCTL\n");
goto exit_close_error;
}
fprintf(stdout, "\tline %2d:", linfo.line_offset);
fprintf(stdout, "\tline %2d:", linfo.offset);
if (linfo.name[0])
fprintf(stdout, " \"%s\"", linfo.name);
else
@ -131,7 +147,7 @@ int list_device(const char *device_name)
fprintf(stdout, " unused");
if (linfo.flags) {
fprintf(stdout, " [");
print_flags(linfo.flags);
print_attributes(&linfo);
fprintf(stdout, "]");
}
fprintf(stdout, "\n");