Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 1 | /* |
Huacai Chen | cbfb3ea | 2015-04-01 10:20:09 +0800 | [diff] [blame] | 2 | * Loongson-2F/3A/3B GPIO Support |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 3 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 4 | * Copyright (c) 2008 Richard Liu, STMicroelectronics <richard.liu@st.com> |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 5 | * Copyright (c) 2008-2010 Arnaud Patard <apatard@mandriva.com> |
Huacai Chen | cbfb3ea | 2015-04-01 10:20:09 +0800 | [diff] [blame] | 6 | * Copyright (c) 2013 Hongbing Hu <huhb@lemote.com> |
| 7 | * Copyright (c) 2014 Huacai Chen <chenhc@lemote.com> |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/err.h> |
Linus Walleij | f105edf | 2018-04-13 10:28:21 +0200 | [diff] [blame] | 20 | #include <linux/gpio/driver.h> |
Linus Walleij | 70e703e | 2018-04-13 12:05:36 +0200 | [diff] [blame^] | 21 | #include <linux/platform_device.h> |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 22 | #include <asm/types.h> |
| 23 | #include <loongson.h> |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 24 | |
| 25 | #define STLS2F_N_GPIO 4 |
Huacai Chen | cbfb3ea | 2015-04-01 10:20:09 +0800 | [diff] [blame] | 26 | #define STLS3A_N_GPIO 16 |
| 27 | |
| 28 | #ifdef CONFIG_CPU_LOONGSON3 |
| 29 | #define LOONGSON_N_GPIO STLS3A_N_GPIO |
| 30 | #else |
| 31 | #define LOONGSON_N_GPIO STLS2F_N_GPIO |
| 32 | #endif |
| 33 | |
| 34 | #define LOONGSON_GPIO_IN_OFFSET 16 |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 35 | |
| 36 | static DEFINE_SPINLOCK(gpio_lock); |
| 37 | |
Huacai Chen | cbfb3ea | 2015-04-01 10:20:09 +0800 | [diff] [blame] | 38 | static int loongson_gpio_get_value(struct gpio_chip *chip, unsigned gpio) |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 39 | { |
Huacai Chen | df5dade | 2015-04-01 10:20:07 +0800 | [diff] [blame] | 40 | u32 val; |
| 41 | u32 mask; |
| 42 | |
Huacai Chen | cbfb3ea | 2015-04-01 10:20:09 +0800 | [diff] [blame] | 43 | mask = 1 << (gpio + LOONGSON_GPIO_IN_OFFSET); |
Huacai Chen | df5dade | 2015-04-01 10:20:07 +0800 | [diff] [blame] | 44 | spin_lock(&gpio_lock); |
| 45 | val = LOONGSON_GPIODATA; |
| 46 | spin_unlock(&gpio_lock); |
| 47 | |
| 48 | return (val & mask) != 0; |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Huacai Chen | cbfb3ea | 2015-04-01 10:20:09 +0800 | [diff] [blame] | 51 | static void loongson_gpio_set_value(struct gpio_chip *chip, |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 52 | unsigned gpio, int value) |
| 53 | { |
Huacai Chen | df5dade | 2015-04-01 10:20:07 +0800 | [diff] [blame] | 54 | u32 val; |
| 55 | u32 mask; |
| 56 | |
| 57 | mask = 1 << gpio; |
| 58 | |
| 59 | spin_lock(&gpio_lock); |
| 60 | val = LOONGSON_GPIODATA; |
| 61 | if (value) |
| 62 | val |= mask; |
| 63 | else |
| 64 | val &= (~mask); |
| 65 | LOONGSON_GPIODATA = val; |
| 66 | spin_unlock(&gpio_lock); |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Linus Walleij | f105edf | 2018-04-13 10:28:21 +0200 | [diff] [blame] | 69 | static int loongson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 70 | { |
| 71 | u32 temp; |
| 72 | u32 mask; |
| 73 | |
| 74 | spin_lock(&gpio_lock); |
| 75 | mask = 1 << gpio; |
| 76 | temp = LOONGSON_GPIOIE; |
| 77 | temp |= mask; |
| 78 | LOONGSON_GPIOIE = temp; |
| 79 | spin_unlock(&gpio_lock); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int loongson_gpio_direction_output(struct gpio_chip *chip, |
| 85 | unsigned gpio, int level) |
| 86 | { |
| 87 | u32 temp; |
| 88 | u32 mask; |
| 89 | |
| 90 | loongson_gpio_set_value(chip, gpio, level); |
| 91 | spin_lock(&gpio_lock); |
| 92 | mask = 1 << gpio; |
| 93 | temp = LOONGSON_GPIOIE; |
| 94 | temp &= (~mask); |
| 95 | LOONGSON_GPIOIE = temp; |
| 96 | spin_unlock(&gpio_lock); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Linus Walleij | 70e703e | 2018-04-13 12:05:36 +0200 | [diff] [blame^] | 101 | static int loongson_gpio_probe(struct platform_device *pdev) |
| 102 | { |
| 103 | struct gpio_chip *gc; |
| 104 | struct device *dev = &pdev->dev; |
| 105 | |
| 106 | gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL); |
| 107 | if (!gc) |
| 108 | return -ENOMEM; |
| 109 | |
| 110 | gc->label = "loongson-gpio-chip"; |
| 111 | gc->base = 0; |
| 112 | gc->ngpio = LOONGSON_N_GPIO; |
| 113 | gc->get = loongson_gpio_get_value; |
| 114 | gc->set = loongson_gpio_set_value; |
| 115 | gc->direction_input = loongson_gpio_direction_input; |
| 116 | gc->direction_output = loongson_gpio_direction_output; |
| 117 | |
| 118 | return gpiochip_add_data(gc, NULL); |
| 119 | } |
| 120 | |
| 121 | static struct platform_driver loongson_gpio_driver = { |
| 122 | .driver = { |
| 123 | .name = "loongson-gpio", |
| 124 | }, |
| 125 | .probe = loongson_gpio_probe, |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
Huacai Chen | cbfb3ea | 2015-04-01 10:20:09 +0800 | [diff] [blame] | 128 | static int __init loongson_gpio_setup(void) |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 129 | { |
Linus Walleij | 70e703e | 2018-04-13 12:05:36 +0200 | [diff] [blame^] | 130 | struct platform_device *pdev; |
| 131 | int ret; |
| 132 | |
| 133 | ret = platform_driver_register(&loongson_gpio_driver); |
| 134 | if (ret) { |
| 135 | pr_err("error registering loongson GPIO driver\n"); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | pdev = platform_device_register_simple("loongson-gpio", -1, NULL, 0); |
| 140 | return PTR_ERR_OR_ZERO(pdev); |
Arnaud Patard | c197da9 | 2010-04-29 11:58:54 +0200 | [diff] [blame] | 141 | } |
Huacai Chen | cbfb3ea | 2015-04-01 10:20:09 +0800 | [diff] [blame] | 142 | postcore_initcall(loongson_gpio_setup); |