PC Engines APU/APU2 led driver

Alan Mizrahi
2017-09-11T05:44:53Z

[-- Attachment #1: Type: text/plain, Size: 3797 bytes --]

Hello,

I am writing a driver for the leds in PC Engines APU and APU2 boards
(see the attachment).

There was an attempt at this before (see
https://www.spinics.net/lists/linux-leds/msg07904.html), but it was not
accepted due to several issues.
So I tried to rewrite it completely, using leds-mlxpld.c as a base, and
it seems to be working.

But I'm getting this oops when trying to rmmod the module:

[52615.090143] BUG: unable to handle kernel paging request at
ffffc900000c5618
[52615.097435] IP: [<ffffffff811dd099>] ioread32+0x29/0x30
[52615.102835] PGD 11a00e067
[52615.105461] PUD 11a00f067
[52615.108251] PMD 11a014067
[52615.109543] PTE 0

[52615.113072] Oops: 0000 [#1] PREEMPT SMP
[52615.117150] Modules linked in: ledtrig_heartbeat arc4 ath10k_pci
ath10k_core ath mac80211 crct10dif_pclmul crc32_pclmul crc32c_intel
ghash_clmulni_intel aesni_
intel leds_apu(-) aes_x86_64 lrw gf128mul glue_helper ablk_helper cryptd
ccp sdhci_pci sdhci cfg80211 mmc_core led_class sha256_generic
sha1_generic rng_core
[52615.146823] CPU: 1 PID: 892 Comm: rmmod Not tainted 4.9.47 #37
[52615.152864] Hardware name: PC Engines APU2/APU2, BIOS 4.0.7 02/28/2017
[52615.159656] task: ffff880118538b40 task.stack: ffffc90001480000
[52615.165917] RIP: 0010:[<ffffffff811dd099>] [<ffffffff811dd099>]
ioread32+0x29/0x30
[52615.173967] RSP: 0018:ffffc90001483dd0 EFLAGS: 00010292
[52615.179479] RAX: 0000000000000000 RBX: ffff8801185292c8 RCX:
000000018080007d
[52615.186933] RDX: 0000000000000001 RSI: 0000000000000000 RDI:
ffffc900000c5618
[52615.194342] RBP: 0000000000000000 R08: 0000000000000001 R09:
ffff88011ec0e8c0
[52615.201709] R10: ffff88011a004460 R11: ffff88011a001c00 R12:
0000000000000008
[52615.209057] R13: ffff88011a004440 R14: ffff88011a0042c0 R15:
00000000024ae010
[52615.216527] FS: 00007f665d212700(0000) GS:ffff88011ec80000(0000)
knlGS:0000000000000000
[52615.224873] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[52615.230828] CR2: ffffc900000c5618 CR3: 00000001180bc000 CR4:
00000000000406e0
[52615.238273] Stack:
[52615.240306] ffffffffa0101023 ffff8801185292c8 ffff8801185293a0
ffffffffa0010076
[52615.248041] ffffc90001483e08 ffff8801185f8010 ffffffff812923c0
ffff88011a312f00
[52615.255846] ffff88011a004460 ffff8801185f8010 ffffffffa01011e8
ffffffff816370e0
[52615.263610] Call Trace:
[52615.266136] [<ffffffffa0101023>] ? apu2_led_brightness_set+0x23/0x58
[leds_apu]
[52615.273805] [<ffffffffa0010076>] ? led_classdev_unregister+0x46/0xa0
[led_class]
[52615.281594] [<ffffffff812923c0>] ? release_nodes+0xf0/0x1b8
[52615.287483] [<ffffffff8128f615>] ? __device_release_driver+0x9d/0x140
[52615.294381] [<ffffffff8128fe41>] ? device_release_driver+0x19/0x28
[52615.300799] [<ffffffff8128eabb>] ? bus_remove_device+0xeb/0x130
[52615.307016] [<ffffffff8128bb79>] ? device_del+0x121/0x258
[52615.312729] [<ffffffff8129158e>] ? platform_device_del+0x1e/0x70
[52615.319110] [<ffffffff812915e9>] ? platform_device_unregister+0x9/0x20
[52615.325925] [<ffffffffa01010b1>] ? apu_led_exit+0xf/0x1b [leds_apu]
[52615.332471] [<ffffffff810919dc>] ? SyS_delete_module+0x18c/0x1d0
[52615.338761] [<ffffffff810010b6>] ? exit_to_usermode_loop+0x66/0x70
[52615.345245] [<ffffffff813fb560>] ? entry_SYSCALL_64_fastpath+0x13/0x94
[52615.352092] Code: 40 00 48 81 ff ff ff 03 00 77 20 48 81 ff 00 00 01
00 76 05 0f b7 d7 ed c3 48 c7 c6 fc df 4d 81 e8 35 ff ff ff b8 ff ff ff
ff c3 <8b> 07 c3 0f 1f 40 00 48 81 fe ff ff 03 00 48 89 f2 77 1f 48 81
[52615.373340] RIP [<ffffffff811dd099>] ioread32+0x29/0x30
[52615.378891] RSP <ffffc90001483dd0>
[52615.382547] CR2: ffffc900000c5618
[52615.385932] ---[ end trace 88328a1715960e7c ]---
[52615.390731] note: rmmod[892] exited with preempt_count 1

Any ideas and recommendations would be greatly appreciated.

Best regards,

Alan

[-- Attachment #2: leds-apu.c --]
[-- Type: text/plain, Size: 8390 bytes --]

/*
* drivers/leds/leds-apu.c
* Copyright (C) 2017 Alan Mizrahi, alan at mizrahi dot com dot ve
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <linux/dmi.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#define APU1_FCH_ACPI_MMIO_BASE 0xFED80000
#define APU1_FCH_GPIO_BASE (APU1_FCH_ACPI_MMIO_BASE + 0x1BD)
#define APU1_LEDON 0x08
#define APU1_LEDOFF 0xC8
#define APU1_NUM_GPIO 3
#define APU1_IOSIZE sizeof(u8)

#define APU2_FCH_ACPI_MMIO_BASE 0xFED80000
#define APU2_FCH_GPIO_BASE (APU2_FCH_ACPI_MMIO_BASE + 0x1500)
#define APU2_GPIO_BIT_WRITE 22
#define APU2_APU2_NUM_GPIO 4
#define APU2_IOSIZE sizeof(u32)

/* LED access parameters */
struct apu_param {
void __iomem *addr; /* for ioread/iowrite */
};

/* LED private data */
struct apu_led_priv {
struct led_classdev cdev;
struct apu_param param;
};
#define cdev_to_priv(c) container_of(c, struct apu_led_priv, cdev)

/* LED profile */
struct apu_led_profile {
const char *name;
enum led_brightness brightness;
unsigned long offset; /* for devm_ioremap */
};

/* Supported platform types */
enum apu_led_platform_types {
LED_PLATFORM_APU1,
LED_PLATFORM_APU2,
};

struct apu_led_pdata {
struct platform_device *pdev;
struct apu_led_priv *pled;
struct apu_led_profile *profile;
enum apu_led_platform_types platform;
int num_led_instances;
int iosize; /* for devm_ioremap() */
spinlock_t lock;
};

static struct apu_led_pdata *apu_led;

static struct apu_led_profile apu1_led_profile[] = {
{ "apu:1", 1, APU1_FCH_GPIO_BASE + 0 * APU1_IOSIZE },
{ "apu:2", LED_OFF, APU1_FCH_GPIO_BASE + 1 * APU1_IOSIZE },
{ "apu:3", LED_OFF, APU1_FCH_GPIO_BASE + 2 * APU1_IOSIZE },
};

static struct apu_led_profile apu2_led_profile[] = {
{ "apu2:1", 1, APU2_FCH_GPIO_BASE + 68 * APU2_IOSIZE },
{ "apu2:2", LED_OFF, APU2_FCH_GPIO_BASE + 69 * APU2_IOSIZE },
{ "apu2:3", LED_OFF, APU2_FCH_GPIO_BASE + 70 * APU2_IOSIZE },
};

static struct dmi_system_id apu_led_dmi_table[] __initdata = {
{
.ident = "apu",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
DMI_MATCH(DMI_PRODUCT_NAME, "APU")
}
},
{
.ident = "apu2",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
DMI_MATCH(DMI_BOARD_NAME, "APU2")
}
},
{}
};
MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table);

static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value)
{
struct apu_led_priv *pled = cdev_to_priv(led);

spin_lock(&apu_led->lock);
iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr);
spin_unlock(&apu_led->lock);

return;
}

static void apu2_led_brightness_set(struct led_classdev *led, enum led_brightness value)
{
struct apu_led_priv *pled = cdev_to_priv(led);
u32 value_new;

spin_lock(&apu_led->lock);

value_new = ioread32(pled->param.addr);

if (value)
value_new &= ~BIT(APU2_GPIO_BIT_WRITE);
else
value_new |= BIT(APU2_GPIO_BIT_WRITE);

iowrite32(value_new, pled->param.addr);

spin_unlock(&apu_led->lock);
return;
}

/*
* Does the hardware support this?
*
static int apu1_led_blink_set(struct led_classdev *led, unsigned long *delay_on, unsigned long *delay_off)
{
struct apu_led_priv *pled = cdev_to_priv(led);
return 0;
}
static int apu2_led_blink_set(struct led_classdev *led, unsigned long *delay_on, unsigned long *delay_off)
{
struct apu_led_priv *pled = cdev_to_priv(led);
return 0;
}
*/

static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld)
{
int i;
int err;

apu_led->pled = devm_kzalloc(dev, sizeof(struct apu_led_priv) * apu_led->num_led_instances, GFP_KERNEL);

if (!apu_led->pled)
return -ENOMEM;

for (i = 0; i < apu_led->num_led_instances; i++) {
apu_led->pled[i].cdev.name = apu_led->profile[i].name;
apu_led->pled[i].cdev.brightness = apu_led->profile[i].brightness;
apu_led->pled[i].cdev.max_brightness = 1;
if (apu_led->platform == LED_PLATFORM_APU1) {
apu_led->pled[i].cdev.brightness_set = apu1_led_brightness_set;
/* apu_led->pled[i].cdev.blink_set = apu1_led_blink_set; */
} else if (apu_led->platform == LED_PLATFORM_APU2) {
apu_led->pled[i].cdev.brightness_set = apu2_led_brightness_set;
/* apu_led->pled[i].cdev.blink_set = apu2_led_blink_set; */
}
apu_led->pled[i].cdev.flags = LED_CORE_SUSPENDRESUME;

err = devm_led_classdev_register(dev, &apu_led->pled[i].cdev);
if (err)
return err;

apu_led->pled[i].param.addr = devm_ioremap(dev, apu_led->profile[i].offset, apu_led->iosize);
if (!apu_led->pled[i].param.addr)
return -ENOMEM;

if (apu_led->profile[i].brightness)
apu_led->pled[i].cdev.brightness_set(&apu_led->pled[i].cdev, apu_led->profile[i].brightness);
}

return 0;
}

static int __init apu_led_probe(struct platform_device *pdev)
{
apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL);

if (!apu_led)
return -ENOMEM;

apu_led->pdev = pdev;

if (dmi_match(DMI_BOARD_NAME, "APU")) {
apu_led->profile = apu1_led_profile;
apu_led->platform = LED_PLATFORM_APU1;
apu_led->num_led_instances = ARRAY_SIZE(apu1_led_profile);
apu_led->iosize = APU1_IOSIZE;
} else if (dmi_match(DMI_BOARD_NAME, "APU2")) {
apu_led->profile = apu2_led_profile;
apu_led->platform = LED_PLATFORM_APU2;
apu_led->num_led_instances = ARRAY_SIZE(apu2_led_profile);
apu_led->iosize = APU2_IOSIZE;
}

spin_lock_init(&apu_led->lock);
return apu_led_config(&pdev->dev, apu_led);
}

static struct platform_driver apu_led_driver = {
.driver = {
.name = KBUILD_MODNAME,
},
};

static int __init apu_led_init(void)
{
struct platform_device *pdev;
int err;

if (!dmi_match(DMI_SYS_VENDOR, "PC Engines")) {
pr_err("No PC Engines board detected\n");
return -ENODEV;
}
if (!(dmi_match(DMI_PRODUCT_NAME, "APU") || dmi_match(DMI_PRODUCT_NAME, "APU2"))) {
printk(KERN_ERR "Unknown PC Engines board: %s\n", dmi_get_system_info(DMI_PRODUCT_NAME));
return -ENODEV;
}

pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
if (IS_ERR(pdev)) {
pr_err("Device allocation failed\n");
return PTR_ERR(pdev);
}

err = platform_driver_probe(&apu_led_driver, apu_led_probe);
if (err) {
pr_err("Probe platform driver failed\n");
platform_device_unregister(pdev);
}

return err;
}

static void __exit apu_led_exit(void)
{
platform_device_unregister(apu_led->pdev);
platform_driver_unregister(&apu_led_driver);
}

module_init(apu_led_init);
module_exit(apu_led_exit);

MODULE_AUTHOR("Alan Mizrahi, alan at mizrahi dot com dot ve");
MODULE_DESCRIPTION("PC Engines APU family LED driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:leds_apu");