Kelvin Cheung | bd37c99 | 2016-04-06 20:34:53 +0800 | [diff] [blame] | 1 | /* |
| 2 | * GPIO Driver for Loongson 1 SoC |
| 3 | * |
| 4 | * Copyright (C) 2015-2016 Zhang, Keguang <keguang.zhang@gmail.com> |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public |
| 7 | * License version 2. This program is licensed "as is" without any |
| 8 | * warranty of any kind, whether express or implied. |
| 9 | */ |
| 10 | |
Paul Gortmaker | 5f60450 | 2016-09-12 18:16:29 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
Kelvin Cheung | bd37c99 | 2016-04-06 20:34:53 +0800 | [diff] [blame] | 12 | #include <linux/gpio/driver.h> |
| 13 | #include <linux/platform_device.h> |
Linus Walleij | fe29416 | 2017-10-20 14:42:01 +0200 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Kelvin Cheung | bd37c99 | 2016-04-06 20:34:53 +0800 | [diff] [blame] | 15 | |
| 16 | /* Loongson 1 GPIO Register Definitions */ |
| 17 | #define GPIO_CFG 0x0 |
| 18 | #define GPIO_DIR 0x10 |
| 19 | #define GPIO_DATA 0x20 |
| 20 | #define GPIO_OUTPUT 0x30 |
| 21 | |
| 22 | static void __iomem *gpio_reg_base; |
| 23 | |
| 24 | static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset) |
| 25 | { |
Kelvin Cheung | bd37c99 | 2016-04-06 20:34:53 +0800 | [diff] [blame] | 26 | unsigned long flags; |
| 27 | |
| 28 | spin_lock_irqsave(&gc->bgpio_lock, flags); |
Linus Walleij | fe29416 | 2017-10-20 14:42:01 +0200 | [diff] [blame] | 29 | __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | BIT(offset), |
Kelvin Cheung | bd37c99 | 2016-04-06 20:34:53 +0800 | [diff] [blame] | 30 | gpio_reg_base + GPIO_CFG); |
| 31 | spin_unlock_irqrestore(&gc->bgpio_lock, flags); |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset) |
| 37 | { |
Kelvin Cheung | bd37c99 | 2016-04-06 20:34:53 +0800 | [diff] [blame] | 38 | unsigned long flags; |
| 39 | |
| 40 | spin_lock_irqsave(&gc->bgpio_lock, flags); |
Linus Walleij | fe29416 | 2017-10-20 14:42:01 +0200 | [diff] [blame] | 41 | __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~BIT(offset), |
Kelvin Cheung | bd37c99 | 2016-04-06 20:34:53 +0800 | [diff] [blame] | 42 | gpio_reg_base + GPIO_CFG); |
| 43 | spin_unlock_irqrestore(&gc->bgpio_lock, flags); |
| 44 | } |
| 45 | |
| 46 | static int ls1x_gpio_probe(struct platform_device *pdev) |
| 47 | { |
| 48 | struct device *dev = &pdev->dev; |
| 49 | struct gpio_chip *gc; |
| 50 | struct resource *res; |
| 51 | int ret; |
| 52 | |
| 53 | gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL); |
| 54 | if (!gc) |
| 55 | return -ENOMEM; |
| 56 | |
| 57 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Kelvin Cheung | bd37c99 | 2016-04-06 20:34:53 +0800 | [diff] [blame] | 58 | gpio_reg_base = devm_ioremap_resource(dev, res); |
| 59 | if (IS_ERR(gpio_reg_base)) |
| 60 | return PTR_ERR(gpio_reg_base); |
| 61 | |
| 62 | ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA, |
| 63 | gpio_reg_base + GPIO_OUTPUT, NULL, |
| 64 | NULL, gpio_reg_base + GPIO_DIR, 0); |
| 65 | if (ret) |
| 66 | goto err; |
| 67 | |
| 68 | gc->owner = THIS_MODULE; |
| 69 | gc->request = ls1x_gpio_request; |
| 70 | gc->free = ls1x_gpio_free; |
| 71 | gc->base = pdev->id * 32; |
| 72 | |
| 73 | ret = devm_gpiochip_add_data(dev, gc, NULL); |
| 74 | if (ret) |
| 75 | goto err; |
| 76 | |
| 77 | platform_set_drvdata(pdev, gc); |
| 78 | dev_info(dev, "Loongson1 GPIO driver registered\n"); |
| 79 | |
| 80 | return 0; |
| 81 | err: |
| 82 | dev_err(dev, "failed to register GPIO device\n"); |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static struct platform_driver ls1x_gpio_driver = { |
| 87 | .probe = ls1x_gpio_probe, |
| 88 | .driver = { |
| 89 | .name = "ls1x-gpio", |
| 90 | }, |
| 91 | }; |
| 92 | |
| 93 | module_platform_driver(ls1x_gpio_driver); |
| 94 | |
| 95 | MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>"); |
| 96 | MODULE_DESCRIPTION("Loongson1 GPIO driver"); |
| 97 | MODULE_LICENSE("GPL"); |