Skip to contentSkip to conversation

gpio: Add driver for PC Engines APU2/APU3 GPIOs

PATCH Superseded Florian Eckert 7 participants 10 messages Aug 1, 2018 – Aug 30, 2018 GPIO

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: Add driver for PC Engines APU2/APU3 GPIOs

Add a new device driver "gpio-apu" which will now handle the GPIOs on APU2 and APU3 devices from PC Engines.

- APU2/APU3 -> front button reset support
- APU3 -> SIM switch support

---
 drivers/gpio/Kconfig    |  10 ++
 drivers/gpio/Makefile   |   1 +
 drivers/gpio/gpio-apu.c | 344 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 355 insertions(+)
 create mode 100644 drivers/gpio/gpio-apu.c
drivers/gpio/Kconfig +10 -0
index 71c0ab46f216..9eb8977ba2e5 100644
@@ -117,6 +117,16 @@ config GPIO_AMDPT
 	  driver for GPIO functionality on Promontory IOHub
 	  Require ACPI ASL code to enumerate as a platform device.
 
+config GPIO_APU
+	tristate "PC Engines APU2/APU3 GPIO support"
+	depends on X86
+	select GPIO_GENERIC
+	help
+	  Say Y here to support GPIO functionality on APU2/APU3 boards
+	  from PC Engines.
+	  - APU2/APU3 -> front button reset support
+	  - APU3 -> SIM switch support
+
 config GPIO_ASPEED
 	tristate "Aspeed GPIO support"
 	depends on (ARCH_ASPEED || COMPILE_TEST) && OF_GPIO
drivers/gpio/Makefile +1 -0
index 1324c8f966a7..feea4effcf29 100644
@@ -29,6 +29,7 @@ obj-$(CONFIG_GPIO_ALTERA)  	+= gpio-altera.o
 obj-$(CONFIG_GPIO_ALTERA_A10SR)	+= gpio-altera-a10sr.o
 obj-$(CONFIG_GPIO_AMD8111)	+= gpio-amd8111.o
 obj-$(CONFIG_GPIO_AMDPT)	+= gpio-amdpt.o
+obj-$(CONFIG_GPIO_APU)		+= gpio-apu.o
 obj-$(CONFIG_GPIO_ARIZONA)	+= gpio-arizona.o
 obj-$(CONFIG_GPIO_ATH79)	+= gpio-ath79.o
 obj-$(CONFIG_GPIO_ASPEED)	+= gpio-aspeed.o
drivers/gpio/gpio-apu.c new file +344 -0

344 lines not on this page · raw message

Signature · 1 line
2.11.0
#1 lore raw
Quoted 7 lines · Florian Eckert

Add a new device driver "gpio-apu" which will now handle the GPIOs on APU2 and APU3 devices from PC Engines.

- APU2/APU3 -> front button reset support
- APU3 -> SIM switch support

Hi Florian, thanks for the patch!

I looped in Andy Schevchenko and Mika Westerberg who are authorities on x86 platform drivers in general and GPIO and pin control in particular so they can help out with the review.

I'm a bit confused whether these things are really GPIOs or just switches but since they can change direction they seem to be GPIOs.

I don't know if the placement of the driver is right either: I was under the impression that this type of drivers should live under drivers/platform/x86 but Andy can surely tell.

drivers/gpio/Kconfig config GPIO_AMDPT · line 123

Quoted 4 lines
drivers/gpio/Kconfig +4 -0
+config GPIO_APU
+       tristate "PC Engines APU2/APU3 GPIO support"
+       depends on X86
+       select GPIO_GENERIC

Thanks for activating the helpers! This makes everything simpler. But your driver should (and can) use them too, just make sure to call bgpio_init() with the right address offsets and everything should just work.

drivers/gpio/Kconfig config GPIO_AMDPT · line 128

Quoted 5 lines
drivers/gpio/Kconfig +5 -0
+       help
+         Say Y here to support GPIO functionality on APU2/APU3 boards
+         from PC Engines.
+         - APU2/APU3 -> front button reset support
+         - APU3 -> SIM switch support

I don't know about the approach to bundle GPIO keys into a GPIO driver, but Andy will tell.

Quoted 8 lines

+/* PC Engines APU2/APU3 GPIO device driver
+ *
+ * Copyright (C) 2018 Florian Eckert <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version

Quoted 8 lines

+ *
+ * This program is distributed in the hope that it will be useful
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>

Instead of the big bulk of license text, use the simple oneline SPDX identifier. In this case, at the top of the file:

// SPDX-License-Identifier: GPL-2.0

See Documentation/process/license-rules.rst for full info.

drivers/gpio/gpio-apu.c line 21

Quoted 3 lines
drivers/gpio/gpio-apu.c new file +3 -0
+#include <linux/dmi.h>
+#include <linux/err.h>
+#include <linux/gpio.h>

Use just <linux/driver.h> for GPIO drivers.

Quoted 1 line

+#define APU_FCH_ACPI_MMIO_BASE 0xFED80000

Hm this looks hardcoded. Isn't ACPI giving you the base address? Not that I understand ACPI, but...

Quoted 5 lines
drivers/gpio/gpio-apu.c new file +5 -0
+#define APU_FCH_GPIO_BASE      (APU_FCH_ACPI_MMIO_BASE + 0x1500)
+#define APU_GPIO_BIT_WRITE     22
+#define APU_GPIO_BIT_READ      16
+#define APU_GPIO_BIT_DIR       23
+#define APU_IOSIZE             sizeof(u32)

drivers/gpio/gpio-apu.c line 45

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +10 -0
+
+#define APU2_NUM_GPIO          1
+#define APU3_NUM_GPIO          2
+
+struct apu_gpio_pdata {
+       struct platform_device *pdev;
+       struct gpio_chip *chip;
+       unsigned long *offset;
+       void __iomem **addr;
+       int iosize; /* for devm_ioremap() */

Can you keep this in a local variable? It doesn't seem like it needs to be in the state container.

Quoted 1 line

+ spinlock_t lock;

I think checkpatch now mandates that you put in a comment about what this lock is locking.

drivers/gpio/gpio-apu.c line 50

Quoted 2 lines
drivers/gpio/gpio-apu.c new file +2 -0
+static struct apu_gpio_pdata *apu_gpio;
+static struct platform_device *keydev;

I don't know a about static singletons, but I guess this would only ever probe once, so it's probably OK, but Andy knows better.

drivers/gpio/gpio-apu.c line 71

Quoted 7 lines
drivers/gpio/gpio-apu.c new file +7 -0
+static int gpio_apu_get_dir (struct gpio_chip *chip, unsigned offset)
+{
+       u32 val;
+
+       spin_lock(&apu_gpio->lock);
+
+       val = ~ioread32(apu_gpio->addr[offset]);

Some comment on why this needs to be inversed?

Quoted 1 line

+ val = (val >> APU_GPIO_BIT_DIR) & 1;

I would write:

#include <linux/bitops.h>

val = !!(val & BIT(APU_GPIO_BIT_DIR);

or even fold into the read:

val = !!(~ioreaad() & BIT(APU_GPIO_BIT_DIR));

But this and all the other GPIO accessors go away if you use GPIO_GENERIC properly with bgpio_init() and the right addresses and flags.

drivers/gpio/gpio-apu.c line 186

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +10 -0
+static struct gpio_chip gpio_apu_chip = {
+       .label = "gpio-apu",
+       .owner = THIS_MODULE,
+       .base = -1,
+       .get_direction = gpio_apu_get_dir,
+       .direction_input = gpio_apu_dir_in,
+       .direction_output = gpio_apu_dir_out,
+       .get = gpio_apu_get_data,
+       .set = gpio_apu_set_data,
+};

So instead of a static GPIO chip leave the functions undefined and call bgpio_init() that will set them up for MMIO GPIO.

drivers/gpio/gpio-apu.c line 197

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +10 -0
+static struct gpio_keys_button apu_gpio_keys[] = {
+       {
+               .desc           = "Reset button",
+               .type           = EV_KEY,
+               .code           = KEY_RESTART,
+               .debounce_interval = 60,
+               .gpio           = 510,
+               .active_low     = 1,
+       },
+};

Andy has to tell whether he likes this idea.

Quoted 21 lines
drivers/gpio/gpio-apu.c new file +21 -0
+       apu_gpio->pdev = pdev;
+       apu_gpio->chip = &gpio_apu_chip;
+       spin_lock_init(&apu_gpio->lock);
+
+       if (dmi_match(DMI_PRODUCT_NAME, "APU3")) {
+               apu_gpio->offset = apu3_gpio_offset;
+               apu_gpio->addr = apu3_gpio_addr;
+               apu_gpio->iosize = APU_IOSIZE;
+               apu_gpio->chip->ngpio = ARRAY_SIZE(apu3_gpio_offset);
+               for( i = 0; i < ARRAY_SIZE(apu3_gpio_offset); i++) {
+                       apu3_gpio_addr[i] = devm_ioremap(&pdev->dev,
+                                       apu_gpio->offset[i], apu_gpio->iosize);
+                       if (!apu3_gpio_addr[i]) {
+                               return -ENOMEM;
+                       }
+               }
+       } else if (dmi_match(DMI_BOARD_NAME, "APU2") ||
+                  dmi_match(DMI_BOARD_NAME, "apu2") ||
+                  dmi_match(DMI_BOARD_NAME, "PC Engines apu2")) {
+               apu_gpio->offset = apu2_gpio_offset;
+               apu_gpio->addr = apu2_gpio_addr;

drivers/gpio/gpio-apu.c line 275

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +10 -0
+               apu_gpio->iosize = APU_IOSIZE;
+               apu_gpio->chip->ngpio = ARRAY_SIZE(apu2_gpio_offset);
+               for( i = 0; i < ARRAY_SIZE(apu2_gpio_offset); i++) {
+                       apu2_gpio_addr[i] = devm_ioremap(&pdev->dev,
+                                       apu_gpio->offset[i], apu_gpio->iosize);
+                       if (!apu2_gpio_addr[i]) {
+                               return -ENOMEM;
+                       }
+               }
+       }

So here, after figuring out the base addresses and all, call bgpio_init() before adding the gpio_chip, which will set up the desired access functions.

drivers/gpio/gpio-apu.c line 282

Quoted 6 lines
drivers/gpio/gpio-apu.c new file +6 -0
+       ret = gpiochip_add(&gpio_apu_chip);
+       if (ret) {
+               pr_err("Adding gpiochip failed\n");
+       }
+
+       register_gpio_keys_polled(-1, 20, ARRAY_SIZE(apu_gpio_keys), apu_gpio_keys);

I don't know if this is a good idea?

Yours,
Linus Walleij

#2 lore raw
Quoted 1 line · Linus Walleij

On Wed, Aug 1, 2018 at 1:12 PM Florian Eckert <[email protected]> wrote:

[]

Quoted 4 lines
Quoted 1 line

+ spinlock_t lock;

I think checkpatch now mandates that you put in a comment about what this lock is locking.

Please remember that checkpatch doesn't mandate anything.

Documentation/process/4.Coding.rst does have:

Certain things should always be commented. Uses of memory barriers should be accompanied by a line explaining why the barrier is necessary. The locking rules for data structures generally need to be explained somewhere.

So there should be some comment somewhere in the code for the spinlock.

#3 lore raw
In reply to Linus Walleij
Quoted 11 lines · CEST Linus Walleij
Quoted 7 lines · Florian Eckert

Add a new device driver "gpio-apu" which will now handle the GPIOs on APU2 and APU3 devices from PC Engines.

- APU2/APU3 -> front button reset support
- APU3 -> SIM switch support

Hi Florian, thanks for the patch!

There have been multiple attempts at upstreaming a gpio/pinctrl driver for the different APU SoC by now. The last attempt I remember took place on this ML too: <https://lkml.org/lkml/2017/12/20/824>

But sadly most attempts stalled, because there is already a leds-apu driver
<https://github.com/torvalds/linux/blob/master/drivers/leds/leds-apu.c>
(since the gpio-driver was never accepted) that will do in a pinch for the LEDs (which most people care about).

Quoted 3 lines

I looped in Andy Schevchenko and Mika Westerberg who are authorities on x86 platform drivers in general and GPIO and pin control in particular so they can help out with the review.

Intel helping AMD. That's nice to see ;-)

Quoted 2 lines

I'm a bit confused whether these things are really GPIOs or just switches but since they can change direction they seem to be GPIOs.

Yes, it's a real gpio/pinctrl in AMDs Tech docs.
<https://support.amd.com/TechDocs/52740_16h_Models_30h-3Fh_BKDG.pdf>

Some of the pins are muxed. I.e GPIO167-170 can either be a GPIO or used for HD audio.
"The HD audio controller supports up to four codecs with one AZ_SDIN pin from each codec. The four AZ_SDIN pins are multiplexed with GPIO167-170 (GPIOxA7-GPIOxAA). If a particular pin is to be used for HD audio functionality". There's a full table in "3.26.12.1 GPIO Registers".

As for the APUs. The vendor (PC Engines) happily provides PDFs and schematics for their boards:
<https://www.pcengines.ch/pdf/apu1.pdf>
<https://www.pcengines.ch/schema/apu1c.pdf>
<https://www.pcengines.ch/pdf/apu2.pdf>
<http://pcengines.ch/schema/apu2c.pdf>

So, it's possible to repurpose several test points as additional GPIOs and more.

Note2:
On both boards there is also a dedicated GPIO pin header J19, but these pins are controlled by the SuperIO Nuvoton NCT5104D.

Quoted 9 lines

drivers/gpio/Kconfig config GPIO_AMDPT · line 128

Quoted 9 lines
drivers/gpio/Kconfig +9 -0
+config GPIO_APU
+       tristate "PC Engines APU2/APU3 GPIO support"
+       depends on X86
+       select GPIO_GENERIC
+       help
+         Say Y here to support GPIO functionality on APU2/APU3 boards
+         from PC Engines.
+         - APU2/APU3 -> front button reset support
+         - APU3 -> SIM switch support

Well, by design this driver will sort of clash with the leds-apu driver.

Regards,
Christian

#5 lore raw
Quoted 4 lines · Florian Eckert

Hello Andy

I think this are the information you want to have.

I was rather asking for something like TRM.

By schematics, I meant a simplified GPIO buffers and pin control explained on hardware level.

Below has nothing to do with either, unfortunately.

Quoted 7 lines · Christian Lamparter
Quoted 6 lines

As for the APUs. The vendor (PC Engines) happily provides PDFs and schematics for their boards:
<https://www.pcengines.ch/pdf/apu1.pdf>
<https://www.pcengines.ch/schema/apu1c.pdf>
<https://www.pcengines.ch/pdf/apu2.pdf>
<http://pcengines.ch/schema/apu2c.pdf>

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#6 lore raw
In reply to Florian Eckert

On Wed, Aug 1, 2018 at 2:12 PM, Florian Eckert <[email protected]> wrote:

Thanks for the patch, my comments below.

Quoted 5 lines

Add a new device driver "gpio-apu" which will now handle the GPIOs on APU2 and APU3 devices from PC Engines.

- APU2/APU3 -> front button reset support
- APU3 -> SIM switch support

Good.
Can we see some specification for those platforms?

Quoted 4 lines

+/* PC Engines APU2/APU3 GPIO device driver
+ *
+ * Copyright (C) 2018 Florian Eckert <[email protected]>

Quoted 13 lines

+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * This program is distributed in the hope that it will be useful
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>

SPDX, please!

drivers/gpio/gpio-apu.c line 22

Quoted 4 lines

+ */

drivers/gpio/gpio-apu.c new file +2 -0
+#include <linux/gpio.h>
+#include <linux/gpio_keys.h>

These both looks very strange in here.

drivers/gpio/gpio-apu.c line 31

Quoted 2 lines
drivers/gpio/gpio-apu.c new file +2 -0
+#define APU_FCH_ACPI_MMIO_BASE 0xFED80000
+#define APU_FCH_GPIO_BASE      (APU_FCH_ACPI_MMIO_BASE + 0x1500)

Wow! Can we see ACPI tables for these boards? Care to share (via some file share service) output of acpidump -o tables.dat ?

drivers/gpio/gpio-apu.c line 34

Quoted 3 lines
drivers/gpio/gpio-apu.c new file +3 -0
+#define APU_GPIO_BIT_WRITE     22
+#define APU_GPIO_BIT_READ      16
+#define APU_GPIO_BIT_DIR       23

WR and RD looks shorter,
And please keep them sorted by value.

Quoted 1 line

+#define APU_IOSIZE sizeof(u32)

This is usual for x86 stuff, no need to have a definition, I think.

Quoted 17 lines
drivers/gpio/gpio-apu.c new file +17 -0
+struct apu_gpio_pdata {
+       struct platform_device *pdev;
+       struct gpio_chip *chip;
+       unsigned long *offset;
+       void __iomem **addr;
+       int iosize; /* for devm_ioremap() */
+       spinlock_t lock;
+};
+
+static struct apu_gpio_pdata *apu_gpio;
+static struct platform_device *keydev;
+
+/* APU2 */
+static unsigned long apu2_gpio_offset[APU2_NUM_GPIO] = {
+       APU_FCH_GPIO_BASE + 89 * APU_IOSIZE, //KEY
+};
+static void __iomem *apu2_gpio_addr[APU2_NUM_GPIO] = {NULL};

drivers/gpio/gpio-apu.c line 65

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +9 -0
+
+/* APU3 */
+static unsigned long apu3_gpio_offset[APU3_NUM_GPIO] = {
+       APU_FCH_GPIO_BASE + 89 * APU_IOSIZE, //KEY
+       APU_FCH_GPIO_BASE + 90 * APU_IOSIZE, //SIM
+};
+static void __iomem *apu3_gpio_addr[APU3_NUM_GPIO] = {NULL, NULL};
+
 
+static int gpio_apu_get_dir (struct gpio_chip *chip, unsigned offset)

Style! We do not use space between func and its parameter list.

drivers/gpio/gpio-apu.c line 71

Quoted 7 lines
drivers/gpio/gpio-apu.c new file +6 -0
+{
+       u32 val;
+
+       spin_lock(&apu_gpio->lock);
+
 
+       val = ~ioread32(apu_gpio->addr[offset]);

This is unusual (I mean ~). Better to leave IO alone and do bits manipulations latter on.

Quoted 1 line

+ val = (val >> APU_GPIO_BIT_DIR) & 1;

Do you need this under spin lock?

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +8 -0
+
+       spin_unlock(&apu_gpio->lock);
+
+       return val;
+}
 
 
+static int gpio_apu_get_data (struct gpio_chip *chip, unsigned offset)
+{
+       u32 val;

drivers/gpio/gpio-apu.c line 122

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +10 -0
+
+       spin_lock(&apu_gpio->lock);
+
+       val = ioread32(apu_gpio->addr[offset]);
+       val = (val >> APU_GPIO_BIT_READ) & 1;
+
+       spin_unlock(&apu_gpio->lock);
+
+       return val;
+}

Similar comments as per _get_dir().

drivers/gpio/gpio-apu.c line 210

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +8 -0
+static struct gpio_keys_button apu_gpio_keys[] = {
 
+};
+
+static void register_gpio_keys_polled(int id, unsigned poll_interval,
+                                     unsigned nbuttons,
+                                     struct gpio_keys_button *buttons)
+{
 
+}

Above must not be here.

Quoted 17 lines
drivers/gpio/gpio-apu.c new file +17 -0
+       if (dmi_match(DMI_PRODUCT_NAME, "APU3")) {
+               apu_gpio->offset = apu3_gpio_offset;
+               apu_gpio->addr = apu3_gpio_addr;
+               apu_gpio->iosize = APU_IOSIZE;
+               apu_gpio->chip->ngpio = ARRAY_SIZE(apu3_gpio_offset);
+               for( i = 0; i < ARRAY_SIZE(apu3_gpio_offset); i++) {
+                       apu3_gpio_addr[i] = devm_ioremap(&pdev->dev,
+                                       apu_gpio->offset[i], apu_gpio->iosize);
+                       if (!apu3_gpio_addr[i]) {
+                               return -ENOMEM;
+                       }
+               }
+       } else if (dmi_match(DMI_BOARD_NAME, "APU2") ||
+                  dmi_match(DMI_BOARD_NAME, "apu2") ||
+                  dmi_match(DMI_BOARD_NAME, "PC Engines apu2")) {
+               apu_gpio->offset = apu2_gpio_offset;
+               apu_gpio->addr = apu2_gpio_addr;

drivers/gpio/gpio-apu.c line 275

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +10 -0
+               apu_gpio->iosize = APU_IOSIZE;
+               apu_gpio->chip->ngpio = ARRAY_SIZE(apu2_gpio_offset);
+               for( i = 0; i < ARRAY_SIZE(apu2_gpio_offset); i++) {
+                       apu2_gpio_addr[i] = devm_ioremap(&pdev->dev,
+                                       apu_gpio->offset[i], apu_gpio->iosize);
+                       if (!apu2_gpio_addr[i]) {
+                               return -ENOMEM;
+                       }
+               }
+       }

The above should be part either as callback or driver_data of DMI entries.

Quoted 1 line

+ ret = gpiochip_add(&gpio_apu_chip);

devm_

drivers/gpio/gpio-apu.c line 279

Quoted 3 lines
drivers/gpio/gpio-apu.c new file +2 -0
+       if (ret) {
 
+               pr_err("Adding gpiochip failed\n");

dev_err(), but I consider this message completely useless.

drivers/gpio/gpio-apu.c line 283

Quoted 5 lines
drivers/gpio/gpio-apu.c new file +4 -0
+       }
 
+
+       register_gpio_keys_polled(-1, 20, ARRAY_SIZE(apu_gpio_keys), apu_gpio_keys);
+

Not part of this driver. Remove.

drivers/gpio/gpio-apu.c line 339

Quoted 5 lines
drivers/gpio/gpio-apu.c new file +4 -0
+       return ret;
+}
 
+module_init(apu_gpio_init);
+module_exit(apu_gpio_exit);

Consider to use module_platform_driver() and accompanying data structures and functions.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 6 lines · Andy Shevchenko
Quoted 2 lines

- APU2/APU3 -> front button reset support
- APU3 -> SIM switch support

Good.
Can we see some specification for those platforms?

I think the informations from Christian Lamparter are OK?

<https://www.pcengines.ch/pdf/apu1.pdf>
<https://www.pcengines.ch/schema/apu1c.pdf>
<https://www.pcengines.ch/pdf/apu2.pdf>
<http://pcengines.ch/schema/apu2c.pdf>

Quoted 7 lines
Quoted 5 lines

+ * GNU General Public License for more details
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see
<http://www.gnu.org/licenses/>

SPDX, please!

I have already updated my patch in my git to use this identifier. // SPDX-License-Identifier: GPL-2.0
This was a hint from Linus Walleji

Quoted 8 lines

drivers/gpio/gpio-apu.c line 22

Quoted 5 lines

+ */

drivers/gpio/gpio-apu.c new file +2 -0
+#include <linux/gpio.h>
+#include <linux/gpio_keys.h>

These both looks very strange in here.

On the front of the APU2/APU3 there is a SMD-push-button which is connected to one of the GPIOs. This is used as a reset button for the system (reboot/factory-reset). I am also not sure if this is the right place. But for the first review i thought this will be ok.

The geode, the old board from PC-Engines
added the key gpios to this file
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/platform/geode/alix.c#n47
mybe this is the right place too x86/platfrom/apu/apu.c?

Quoted 6 lines

drivers/gpio/gpio-apu.c line 31

Quoted 2 lines
drivers/gpio/gpio-apu.c new file +2 -0
+#define APU_FCH_ACPI_MMIO_BASE 0xFED80000
+#define APU_FCH_GPIO_BASE      (APU_FCH_ACPI_MMIO_BASE + 0x1500)

Wow! Can we see ACPI tables for these boards? Care to share (via some file share service) output of acpidump -o tables.dat ?

I have copied this from the leds-apu.c driver which is already upstream.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/leds/leds-apu.c#n43

Quoted 7 lines

drivers/gpio/gpio-apu.c line 34

Quoted 4 lines
drivers/gpio/gpio-apu.c new file +3 -0
+#define APU_GPIO_BIT_WRITE     22
+#define APU_GPIO_BIT_READ      16
+#define APU_GPIO_BIT_DIR       23

WR and RD looks shorter,
And please keep them sorted by value.

Ok will fix this.

Quoted 4 lines
Quoted 2 lines

+#define APU_IOSIZE sizeof(u32)

This is usual for x86 stuff, no need to have a definition, I think.

Ok will fix this.

Quoted 11 lines

drivers/gpio/gpio-apu.c line 65

Quoted 8 lines
drivers/gpio/gpio-apu.c new file +7 -0
+static unsigned long apu3_gpio_offset[APU3_NUM_GPIO] = {
+       APU_FCH_GPIO_BASE + 89 * APU_IOSIZE, //KEY
+       APU_FCH_GPIO_BASE + 90 * APU_IOSIZE, //SIM
+};
+static void __iomem *apu3_gpio_addr[APU3_NUM_GPIO] = {NULL, NULL};
+
 
+static int gpio_apu_get_dir (struct gpio_chip *chip, unsigned offset)

Style! We do not use space between func and its parameter list.

OK

Quoted 10 lines

drivers/gpio/gpio-apu.c line 71

Quoted 7 lines
drivers/gpio/gpio-apu.c new file +6 -0
+{
+       u32 val;
+
+       spin_lock(&apu_gpio->lock);
+
 
+       val = ~ioread32(apu_gpio->addr[offset]);

This is unusual (I mean ~). Better to leave IO alone and do bits manipulations latter on.

OK

Quoted 5 lines
Quoted 2 lines

+ val = (val >> APU_GPIO_BIT_DIR) & 1;

Do you need this under spin lock?

No i don´t.
Will fix this.
Thanks

Quoted 11 lines

drivers/gpio/gpio-apu.c line 275

Quoted 10 lines
drivers/gpio/gpio-apu.c new file +4 -0
+               apu_gpio->chip->ngpio = ARRAY_SIZE(apu2_gpio_offset);
+               for( i = 0; i < ARRAY_SIZE(apu2_gpio_offset); i++) {
+                       apu2_gpio_addr[i] = devm_ioremap(&pdev->dev,
+                                       apu_gpio->offset[i], 

apu_gpio->iosize);

drivers/gpio/gpio-apu.c new file +5 -0
+                       if (!apu2_gpio_addr[i]) {
+                               return -ENOMEM;
+                       }
+               }
+       }
Quoted 1 line

The above should be part either as callback or driver_data of DMI entries.

I have copied this from the leds-apu.c driver
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/leds/leds-apu.c#n226

Quoted 5 lines
Quoted 2 lines

+ ret = gpiochip_add(&gpio_apu_chip);

devm_

What do you mean with "devm_"?

Quoted 5 lines

drivers/gpio/gpio-apu.c line 279

Quoted 3 lines
drivers/gpio/gpio-apu.c new file +2 -0
+       if (ret) {
 
+               pr_err("Adding gpiochip failed\n");

dev_err(), but I consider this message completely useless.

Thanks will remove this too.

Quoted 10 lines
Quoted 7 lines
drivers/gpio/gpio-apu.c new file +3 -0
+       }
 
+
+       register_gpio_keys_polled(-1, 20, ARRAY_SIZE(apu_gpio_keys), 

apu_gpio_keys);
+

Not part of this driver. Remove.

As described above should this go to a file "apu.c" in the directory "apu" under
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/platform
?

Quoted 8 lines

drivers/gpio/gpio-apu.c line 339

Quoted 5 lines
drivers/gpio/gpio-apu.c new file +4 -0
+       return ret;
+}
 
+module_init(apu_gpio_init);
+module_exit(apu_gpio_exit);

Consider to use module_platform_driver() and accompanying data structures and functions.

Ok thanks will update this

Will update my patch with your hints thanks

#8 lore raw
In reply to Andy Shevchenko

On 08/03/2018 09:08 PM, Andy Shevchenko wrote:

Hi Andy,

(...)

Quoted 5 lines

drivers/gpio/gpio-apu.c line 31

Quoted 2 lines
drivers/gpio/gpio-apu.c new file +2 -0
+#define APU_FCH_ACPI_MMIO_BASE 0xFED80000
+#define APU_FCH_GPIO_BASE      (APU_FCH_ACPI_MMIO_BASE + 0x1500)

Wow! Can we see ACPI tables for these boards? Care to share (via some file share service) output of acpidump -o tables.dat ?

Please find acpidump [1]. FYI I'm PC Engines firmware maintainer (firmware is coreboot based), so I can fix required things. I'm pretty sure that ACPI tables are not in best shape.

[1] https://pastebin.com/2neC1Sri

Best Regards,
--
Piotr Król
Embedded Systems Consultant
https://3mdeb.com | @3mdeb_com

#9 lore raw
Quoted 12 lines · Piotr Król

On 08/03/2018 09:08 PM, Andy Shevchenko wrote:

Hi Andy,

(...)

Quoted 5 lines

drivers/gpio/gpio-apu.c line 31

Quoted 2 lines
drivers/gpio/gpio-apu.c new file +2 -0
+#define APU_FCH_ACPI_MMIO_BASE 0xFED80000
+#define APU_FCH_GPIO_BASE      (APU_FCH_ACPI_MMIO_BASE + 0x1500)

Wow! Can we see ACPI tables for these boards? Care to share (via some file share service) output of acpidump -o tables.dat ?

Quoted 4 lines

Please find acpidump [1]. FYI I'm PC Engines firmware maintainer (firmware is coreboot based), so I can fix required things. I'm pretty sure that ACPI tables are not in best shape.

Thanks for the dump,

By the way I have seen that the driver
https://github.com/torvalds/linux/blob/master/drivers/gpio/gpio-amdpt.c
is using the acpi to get the mmio address. I think this is the way to go to get the acpi mmio address in the gpio-apu driver as well.

What is the right "acpi_device_id" for the APU3?
https://github.com/torvalds/linux/blob/master/drivers/gpio/gpio-amdpt.c#L146