From patchwork Sat Oct 29 13:20:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Haigh X-Patchwork-Id: 80049 Delivered-To: patch@linaro.org Received: by 10.80.142.83 with SMTP id 19csp1687953edx; Sat, 29 Oct 2016 06:26:38 -0700 (PDT) X-Received: by 10.99.42.80 with SMTP id q77mr28038656pgq.3.1477747598579; Sat, 29 Oct 2016 06:26:38 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k88si14426832pfg.182.2016.10.29.06.26.38 for ; Sat, 29 Oct 2016 06:26:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@crc.id.au; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752308AbcJ2N0h (ORCPT ); Sat, 29 Oct 2016 09:26:37 -0400 Received: from mail.crc.id.au ([203.56.246.92]:37928 "EHLO mail.crc.id.au" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751702AbcJ2N0g (ORCPT ); Sat, 29 Oct 2016 09:26:36 -0400 X-Greylist: delayed 333 seconds by postgrey-1.27 at vger.kernel.org; Sat, 29 Oct 2016 09:26:35 EDT Received: from dhcp-10-1-1-170.lan.crc.id.au (2407-e400-b000-201-1cd8-4a98-e637-6d27-cpe.spintel.net.au [IPv6:2407:e400:b000:201:1cd8:4a98:e637:6d27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mail.crc.id.au (Postfix) with ESMTPSA id B32223A00F6 for ; Sun, 30 Oct 2016 00:20:57 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crc.id.au; s=default; t=1477747257; bh=cMMLz9uqZ0qm+pC9GCpFV5l5Kl5Jlvbrs+8994StDks=; h=To:From:Subject:Date; b=lSyf8UHnaDoV28OwmOlUtwHfeHpX65watyu1Vzdi+pSHdu2D7xt9E91+HJdISyDeO h+lmAxnHeuDM97x7VssBB+dOQmkxyPG4iFUKo86c73CtV0i0qW3iOBnis0vEYc2wv7 N/zgtUF1xTVf42jm2B96jJTVEvPmas/U6eQjHwhA= To: linux-leds@vger.kernel.org From: Steven Haigh Subject: [PATCH] Add LED support for the PC Engines APU2. Message-ID: Date: Sun, 30 Oct 2016 00:20:50 +1100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Sender: linux-leds-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Patch attached. Please include me in the reply as I'm not part of this list. -- Steven Haigh Email: netwiz@crc.id.au Web: https://www.crc.id.au Phone: (03) 9001 6090 - 0412 935 897 diff -Nur linux-4.4.27.orig/drivers/leds/Kconfig linux-4.4.27/drivers/leds/Kconfig --- linux-4.4.27.orig/drivers/leds/Kconfig 2016-10-22 21:27:13.000000000 +1100 +++ linux-4.4.27/drivers/leds/Kconfig 2016-10-30 00:12:13.879529690 +1100 @@ -49,6 +49,15 @@ help This option enables support for the LEDs on the AAT1290. +config LEDS_APU2 + tristate "LED Support for the PCEngines APU2 board" + depends on LEDS_CLASS + depends on GPIOLIB + help + This option enables support for the CPU GPIO pins on the PCEngines + APU2 board, as well as adds system support for the reset button and + front panel LEDs. + config LEDS_BCM6328 tristate "LED Support for Broadcom BCM6328" depends on LEDS_CLASS diff -Nur linux-4.4.27.orig/drivers/leds/leds-apu2.c linux-4.4.27/drivers/leds/leds-apu2.c --- linux-4.4.27.orig/drivers/leds/leds-apu2.c 1970-01-01 10:00:00.000000000 +1000 +++ linux-4.4.27/drivers/leds/leds-apu2.c 2016-10-30 00:09:58.252529690 +1100 @@ -0,0 +1,371 @@ +/* + * APU2 LED/GPIO Driver + * Copyright (c) 2016 Christian Lamparter + * + * Based on gpio-apu2.c - AMD FCH GPIO support for PC-Engines APU-2 board + * + * Copyright (c) 2015 Carsten Spiess + * + * 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, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + + #include + #include + #include + + #define DEVNAME "gpio-apu2" + + #define FCH_ACPI_MMIO_BASE 0xFED80000 + #define FCH_GPIO_BASE (FCH_ACPI_MMIO_BASE + 0x1500) + #define FCH_GPIO_SIZE 0x300 + + #define APU_NUM_GPIO 4 + + #define GPIO_BIT_DIR 23 + #define GPIO_BIT_WRITE 22 + #define GPIO_BIT_READ 16 + + /* internal variables */ + static struct pci_dev *gpio_apu2_pci; + static DEFINE_SPINLOCK ( gpio_lock); + + /* the watchdog platform device */ + static struct platform_device *gpio_apu2_platform_device; + static struct platform_device *leddev; + static struct platform_device *keydev; + + static const struct pci_device_id gpio_apu2_pci_tbl[] ={ + {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, PCI_ANY_ID, PCI_ANY_ID}, + { 0, } /* End of list */ + }; + MODULE_DEVICE_TABLE ( pci, gpio_apu2_pci_tbl); + + /* EGPIO89=GPIO32, AGPIO68=GPIO57, AGPIO69=GPIO58, AGPIO70=GPIO59 */ + static u8 gpio_offset[APU_NUM_GPIO] = {89, 68, 69, 70}; + + static void __iomem *gpio_addr[APU_NUM_GPIO] = {NULL, NULL, NULL, NULL}; + + static int gpio_apu2_get_dir (struct gpio_chip *chip, unsigned offset) + { + u32 val; + + val = ~ioread32 (gpio_addr[offset]); + + return (val >> GPIO_BIT_DIR) & 1; + } + + static int gpio_apu2_dir_in (struct gpio_chip *gc, unsigned offset) + { + u32 val; + + spin_lock_bh (&gpio_lock); + + val = ioread32 (gpio_addr[offset]); + val &= ~BIT(GPIO_BIT_DIR); + iowrite32 (val, gpio_addr[offset]); + + spin_unlock_bh (&gpio_lock); + + return 0; + } + + static int gpio_apu2_dir_out (struct gpio_chip *chip, unsigned offset, + int value) + { + u32 val; + + spin_lock_bh (&gpio_lock); + + val = ioread32 (gpio_addr[offset]); + val |= BIT(GPIO_BIT_DIR); + iowrite32 (val, gpio_addr[offset]); + + spin_unlock_bh (&gpio_lock); + + return 0; + } + + static int gpio_apu2_get_data (struct gpio_chip *chip, unsigned offset) + { + u32 val; + + val = ioread32 (gpio_addr[offset]); + + return (val >> GPIO_BIT_READ) & 1; + } + + static void gpio_apu2_set_data (struct gpio_chip *chip, unsigned offset, int value) + { + u32 val; + + spin_lock_bh (&gpio_lock); + + val = ioread32 (gpio_addr[offset]); + + if (value) + val |= BIT(GPIO_BIT_WRITE); + else + val &= ~BIT(GPIO_BIT_WRITE); + + iowrite32 (val, gpio_addr[offset]); + + spin_unlock_bh (&gpio_lock); + } + + static struct gpio_chip gpio_apu2_chip = { + .label = DEVNAME, + .owner = THIS_MODULE, + .base = -1, + .ngpio = APU_NUM_GPIO, + .get_direction = gpio_apu2_get_dir, + .direction_input = gpio_apu2_dir_in, + .direction_output = gpio_apu2_dir_out, + .get = gpio_apu2_get_data, + .set = gpio_apu2_set_data, + }; + + /* + * + */ + static int gpio_apu2_probe (struct platform_device *dev) + { + int ret = 0; + int i; + struct pci_dev *pci_dev = NULL; + + /* Match the device name/model */ + if (!dmi_match(DMI_BOARD_VENDOR, "PC Engines") || !dmi_match(DMI_BOARD_NAME, "APU2")) { + pr_err ("%s: APU2 board not detected! Found: %s %s\n", DEVNAME, DMI_BOARD_VENDOR, DMI_BOARD_NAME); + return -EACCES; + } + + /* Match the PCI device */ + for_each_pci_dev (pci_dev) { + if (pci_match_id (gpio_apu2_pci_tbl, pci_dev) != NULL) { + gpio_apu2_pci = pci_dev; + break; + } + } + + if (!gpio_apu2_pci) + return -ENODEV; + + pr_info ("%s: PCI Revision ID: 0x%x\n", DEVNAME, gpio_apu2_pci->revision); + + /* Determine type of southbridge chipset */ + if (gpio_apu2_pci->revision < 0x40) { + return -EACCES; + } + + /* Request memory region for GPIO's */ + if (!devm_request_mem_region (&dev->dev, FCH_GPIO_BASE, + FCH_GPIO_SIZE, DEVNAME)){ + pr_err ("%s: request GPIO mem region failed\n", DEVNAME); + return -ENXIO; + } + + /* Map IO's for GPIO's */ + for (i = 0; i < APU_NUM_GPIO; i++) { + gpio_addr[i] = devm_ioremap (&dev->dev, + FCH_GPIO_BASE + (gpio_offset[i] * sizeof (u32)), sizeof (u32)); + if (!gpio_addr[i]) { + pr_err ("%s: map GPIO%d address failed\n", DEVNAME, gpio_offset[i]); + return -ENXIO; + } + } + + gpio_apu2_chip.dev = &dev->dev; + ret = gpiochip_add (&gpio_apu2_chip); + if (ret) { + pr_err ("%s: adding gpiochip failed\n", DEVNAME); + } + + return ret; + } + + static int gpio_apu2_remove (struct platform_device *dev) + { + #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0) + int ret; + ret = gpiochip_remove (&gpio_apu2_chip); + #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0) */ + gpiochip_remove (&gpio_apu2_chip); + #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0) */ + return 0; + } + + static struct platform_driver gpio_apu2_driver = { + .probe = gpio_apu2_probe, + .remove = gpio_apu2_remove, + .driver = { + .owner = THIS_MODULE, + .name = DEVNAME + } + }; + + static struct gpio_led apu2_leds_gpio[] = { + { + .name = "apu2:green:power", + .gpio = 509, + .active_low = 1, + }, + { + .name = "apu2:green:led2", + .gpio = 510, + .active_low = 1, + }, + { + .name = "apu2:green:led3", + .gpio = 511, + .active_low = 1, + }, + }; + + static struct gpio_keys_button apu2_gpio_keys[] = { + { + .desc = "Reset button", + .type = EV_KEY, + .code = KEY_RESTART, + .debounce_interval = 60, + .gpio = 508, + .active_low = 1, + }, + }; + + static void register_gpio_keys_polled(int id, unsigned poll_interval, + unsigned nbuttons, + struct gpio_keys_button *buttons) + { + struct gpio_keys_platform_data pdata = { }; + int err; + + keydev = platform_device_alloc("gpio-keys-polled", id); + if (!keydev) { + printk(KERN_ERR "Failed to allocate gpio-keys platform device\n"); + return; + } + + pdata.poll_interval = poll_interval; + pdata.nbuttons = nbuttons; + pdata.buttons = buttons; + + err = platform_device_add_data(keydev, &pdata, sizeof(pdata)); + if (err) { + dev_err(&keydev->dev, "failed to add platform data to key driver (%d)", err); + goto err_put_pdev; + } + + err = platform_device_add(keydev); + if (err) { + dev_err(&keydev->dev, "failed to register key platform device (%d)", err); + goto err_put_pdev; + } + + return; + + err_put_pdev: + platform_device_put(keydev); + keydev = NULL; + } + + static void register_leds_gpio(int id, unsigned num_leds, struct gpio_led *leds) + { + struct gpio_led_platform_data pdata = { }; + int err; + + leddev = platform_device_alloc("leds-gpio", id); + if (!leddev) { + printk(KERN_ERR "Failed to allocate leds-gpio platform device\n"); + return; + } + + pdata.num_leds = num_leds; + pdata.leds = leds; + + err = platform_device_add_data(leddev, &pdata, sizeof(pdata)); + if (err) { + dev_err(&leddev->dev, "failed to add platform data to key driver (%d)", err); + goto err_put_pdev; + } + + err = platform_device_add(leddev); + if (err) { + dev_err(&leddev->dev, "failed to register key platform device (%d)", err); + goto err_put_pdev; + } + + return; + + err_put_pdev: + platform_device_put(leddev); + leddev = NULL; + } + + static int __init gpio_apu2_init (void) + { + int err; + + pr_info ("%s: load APU2/LED GPIO driver module\n", DEVNAME); + + err = platform_driver_register (&gpio_apu2_driver); + if (err) + goto exit; + + gpio_apu2_platform_device = platform_device_register_simple (DEVNAME, -1, NULL, 0); + if (IS_ERR(gpio_apu2_platform_device)) { + err = PTR_ERR(gpio_apu2_platform_device); + goto exit_driver; + } + + pr_info ("%s: APU2 GPIO/LED driver module loaded\n", DEVNAME); + + register_leds_gpio(-1, ARRAY_SIZE(apu2_leds_gpio), apu2_leds_gpio); + register_gpio_keys_polled(-1, 20, ARRAY_SIZE(apu2_gpio_keys), apu2_gpio_keys); + return 0; + + exit_driver: + platform_driver_unregister (&gpio_apu2_driver); + exit: + return err; + } + + static void __exit gpio_apu2_exit (void) + { + platform_device_unregister (gpio_apu2_platform_device); + platform_device_unregister (leddev); + platform_device_unregister (keydev); + platform_driver_unregister (&gpio_apu2_driver); + pr_info ("%s: APU2 GPIO/LED driver module unloaded\n", DEVNAME); + } + + MODULE_AUTHOR ("Carsten Spiess "); + MODULE_DESCRIPTION("GPIO driver for AMD FCH on PC-Engines APU-2"); + MODULE_LICENSE("GPL"); + + module_init (gpio_apu2_init); + module_exit (gpio_apu2_exit); diff -Nur linux-4.4.27.orig/drivers/leds/Makefile linux-4.4.27/drivers/leds/Makefile --- linux-4.4.27.orig/drivers/leds/Makefile 2016-10-22 21:27:13.000000000 +1100 +++ linux-4.4.27/drivers/leds/Makefile 2016-10-30 00:11:04.031529690 +1100 @@ -8,6 +8,7 @@ # LED Platform Drivers obj-$(CONFIG_LEDS_88PM860X) += leds-88pm860x.o obj-$(CONFIG_LEDS_AAT1290) += leds-aat1290.o +obj-$(CONFIG_LEDS_APU2) += leds-apu2.o obj-$(CONFIG_LEDS_BCM6328) += leds-bcm6328.o obj-$(CONFIG_LEDS_BCM6358) += leds-bcm6358.o obj-$(CONFIG_LEDS_BD2802) += leds-bd2802.o