diff options
Diffstat (limited to 'drivers/sh/pfc/gpio.c')
| -rw-r--r-- | drivers/sh/pfc/gpio.c | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/drivers/sh/pfc/gpio.c b/drivers/sh/pfc/gpio.c new file mode 100644 index 000000000000..62bca98474a9 --- /dev/null +++ b/drivers/sh/pfc/gpio.c | |||
| @@ -0,0 +1,239 @@ | |||
| 1 | /* | ||
| 2 | * SuperH Pin Function Controller GPIO driver. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2008 Magnus Damm | ||
| 5 | * Copyright (C) 2009 - 2012 Paul Mundt | ||
| 6 | * | ||
| 7 | * This file is subject to the terms and conditions of the GNU General Public | ||
| 8 | * License. See the file "COPYING" in the main directory of this archive | ||
| 9 | * for more details. | ||
| 10 | */ | ||
| 11 | #define pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt | ||
| 12 | |||
| 13 | #include <linux/init.h> | ||
| 14 | #include <linux/gpio.h> | ||
| 15 | #include <linux/slab.h> | ||
| 16 | #include <linux/spinlock.h> | ||
| 17 | #include <linux/module.h> | ||
| 18 | #include <linux/platform_device.h> | ||
| 19 | #include <linux/pinctrl/consumer.h> | ||
| 20 | |||
| 21 | struct sh_pfc_chip { | ||
| 22 | struct sh_pfc *pfc; | ||
| 23 | struct gpio_chip gpio_chip; | ||
| 24 | }; | ||
| 25 | |||
| 26 | static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc) | ||
| 27 | { | ||
| 28 | return container_of(gc, struct sh_pfc_chip, gpio_chip); | ||
| 29 | } | ||
| 30 | |||
| 31 | static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc) | ||
| 32 | { | ||
| 33 | return gpio_to_pfc_chip(gc)->pfc; | ||
| 34 | } | ||
| 35 | |||
| 36 | static int sh_gpio_request(struct gpio_chip *gc, unsigned offset) | ||
| 37 | { | ||
| 38 | return pinctrl_request_gpio(offset); | ||
| 39 | } | ||
| 40 | |||
| 41 | static void sh_gpio_free(struct gpio_chip *gc, unsigned offset) | ||
| 42 | { | ||
| 43 | pinctrl_free_gpio(offset); | ||
| 44 | } | ||
| 45 | |||
| 46 | static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value) | ||
| 47 | { | ||
| 48 | struct pinmux_data_reg *dr = NULL; | ||
| 49 | int bit = 0; | ||
| 50 | |||
| 51 | if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0) | ||
| 52 | BUG(); | ||
| 53 | else | ||
| 54 | sh_pfc_write_bit(dr, bit, value); | ||
| 55 | } | ||
| 56 | |||
| 57 | static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio) | ||
| 58 | { | ||
| 59 | struct pinmux_data_reg *dr = NULL; | ||
| 60 | int bit = 0; | ||
| 61 | |||
| 62 | if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0) | ||
| 63 | return -EINVAL; | ||
| 64 | |||
| 65 | return sh_pfc_read_bit(dr, bit); | ||
| 66 | } | ||
| 67 | |||
| 68 | static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset) | ||
| 69 | { | ||
| 70 | return pinctrl_gpio_direction_input(offset); | ||
| 71 | } | ||
| 72 | |||
| 73 | static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset, | ||
| 74 | int value) | ||
| 75 | { | ||
| 76 | sh_gpio_set_value(gpio_to_pfc(gc), offset, value); | ||
| 77 | |||
| 78 | return pinctrl_gpio_direction_output(offset); | ||
| 79 | } | ||
| 80 | |||
| 81 | static int sh_gpio_get(struct gpio_chip *gc, unsigned offset) | ||
| 82 | { | ||
| 83 | return sh_gpio_get_value(gpio_to_pfc(gc), offset); | ||
| 84 | } | ||
| 85 | |||
| 86 | static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value) | ||
| 87 | { | ||
| 88 | sh_gpio_set_value(gpio_to_pfc(gc), offset, value); | ||
| 89 | } | ||
| 90 | |||
| 91 | static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset) | ||
| 92 | { | ||
| 93 | struct sh_pfc *pfc = gpio_to_pfc(gc); | ||
| 94 | pinmux_enum_t enum_id; | ||
| 95 | pinmux_enum_t *enum_ids; | ||
| 96 | int i, k, pos; | ||
| 97 | |||
| 98 | pos = 0; | ||
| 99 | enum_id = 0; | ||
| 100 | while (1) { | ||
| 101 | pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id); | ||
| 102 | if (pos <= 0 || !enum_id) | ||
| 103 | break; | ||
| 104 | |||
| 105 | for (i = 0; i < pfc->gpio_irq_size; i++) { | ||
| 106 | enum_ids = pfc->gpio_irq[i].enum_ids; | ||
| 107 | for (k = 0; enum_ids[k]; k++) { | ||
| 108 | if (enum_ids[k] == enum_id) | ||
| 109 | return pfc->gpio_irq[i].irq; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | return -ENOSYS; | ||
| 115 | } | ||
| 116 | |||
| 117 | static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip) | ||
| 118 | { | ||
| 119 | struct sh_pfc *pfc = chip->pfc; | ||
| 120 | struct gpio_chip *gc = &chip->gpio_chip; | ||
| 121 | |||
| 122 | gc->request = sh_gpio_request; | ||
| 123 | gc->free = sh_gpio_free; | ||
| 124 | gc->direction_input = sh_gpio_direction_input; | ||
| 125 | gc->get = sh_gpio_get; | ||
| 126 | gc->direction_output = sh_gpio_direction_output; | ||
| 127 | gc->set = sh_gpio_set; | ||
| 128 | gc->to_irq = sh_gpio_to_irq; | ||
| 129 | |||
| 130 | WARN_ON(pfc->first_gpio != 0); /* needs testing */ | ||
| 131 | |||
| 132 | gc->label = pfc->name; | ||
| 133 | gc->owner = THIS_MODULE; | ||
| 134 | gc->base = pfc->first_gpio; | ||
| 135 | gc->ngpio = (pfc->last_gpio - pfc->first_gpio) + 1; | ||
| 136 | } | ||
| 137 | |||
| 138 | int sh_pfc_register_gpiochip(struct sh_pfc *pfc) | ||
| 139 | { | ||
| 140 | struct sh_pfc_chip *chip; | ||
| 141 | int ret; | ||
| 142 | |||
| 143 | chip = kzalloc(sizeof(struct sh_pfc_chip), GFP_KERNEL); | ||
| 144 | if (unlikely(!chip)) | ||
| 145 | return -ENOMEM; | ||
| 146 | |||
| 147 | chip->pfc = pfc; | ||
| 148 | |||
| 149 | sh_pfc_gpio_setup(chip); | ||
| 150 | |||
| 151 | ret = gpiochip_add(&chip->gpio_chip); | ||
| 152 | if (unlikely(ret < 0)) | ||
| 153 | kfree(chip); | ||
| 154 | |||
| 155 | pr_info("%s handling gpio %d -> %d\n", | ||
| 156 | pfc->name, pfc->first_gpio, pfc->last_gpio); | ||
| 157 | |||
| 158 | return ret; | ||
| 159 | } | ||
| 160 | EXPORT_SYMBOL_GPL(sh_pfc_register_gpiochip); | ||
| 161 | |||
| 162 | static int sh_pfc_gpio_match(struct gpio_chip *gc, void *data) | ||
| 163 | { | ||
| 164 | return !!strstr(gc->label, data); | ||
| 165 | } | ||
| 166 | |||
| 167 | static int __devinit sh_pfc_gpio_probe(struct platform_device *pdev) | ||
| 168 | { | ||
| 169 | struct sh_pfc_chip *chip; | ||
| 170 | struct gpio_chip *gc; | ||
| 171 | |||
| 172 | gc = gpiochip_find("_pfc", sh_pfc_gpio_match); | ||
| 173 | if (unlikely(!gc)) { | ||
| 174 | pr_err("Cant find gpio chip\n"); | ||
| 175 | return -ENODEV; | ||
| 176 | } | ||
| 177 | |||
| 178 | chip = gpio_to_pfc_chip(gc); | ||
| 179 | platform_set_drvdata(pdev, chip); | ||
| 180 | |||
| 181 | pr_info("attaching to GPIO chip %s\n", chip->pfc->name); | ||
| 182 | |||
| 183 | return 0; | ||
| 184 | } | ||
| 185 | |||
| 186 | static int __devexit sh_pfc_gpio_remove(struct platform_device *pdev) | ||
| 187 | { | ||
| 188 | struct sh_pfc_chip *chip = platform_get_drvdata(pdev); | ||
| 189 | int ret; | ||
| 190 | |||
| 191 | ret = gpiochip_remove(&chip->gpio_chip); | ||
| 192 | if (unlikely(ret < 0)) | ||
| 193 | return ret; | ||
| 194 | |||
| 195 | kfree(chip); | ||
| 196 | return 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | static struct platform_driver sh_pfc_gpio_driver = { | ||
| 200 | .probe = sh_pfc_gpio_probe, | ||
| 201 | .remove = __devexit_p(sh_pfc_gpio_remove), | ||
| 202 | .driver = { | ||
| 203 | .name = KBUILD_MODNAME, | ||
| 204 | .owner = THIS_MODULE, | ||
| 205 | }, | ||
| 206 | }; | ||
| 207 | |||
| 208 | static struct platform_device sh_pfc_gpio_device = { | ||
| 209 | .name = KBUILD_MODNAME, | ||
| 210 | .id = -1, | ||
| 211 | }; | ||
| 212 | |||
| 213 | static int __init sh_pfc_gpio_init(void) | ||
| 214 | { | ||
| 215 | int rc; | ||
| 216 | |||
| 217 | rc = platform_driver_register(&sh_pfc_gpio_driver); | ||
| 218 | if (likely(!rc)) { | ||
| 219 | rc = platform_device_register(&sh_pfc_gpio_device); | ||
| 220 | if (unlikely(rc)) | ||
| 221 | platform_driver_unregister(&sh_pfc_gpio_driver); | ||
| 222 | } | ||
| 223 | |||
| 224 | return rc; | ||
| 225 | } | ||
| 226 | |||
| 227 | static void __exit sh_pfc_gpio_exit(void) | ||
| 228 | { | ||
| 229 | platform_device_unregister(&sh_pfc_gpio_device); | ||
| 230 | platform_driver_unregister(&sh_pfc_gpio_driver); | ||
| 231 | } | ||
| 232 | |||
| 233 | module_init(sh_pfc_gpio_init); | ||
| 234 | module_exit(sh_pfc_gpio_exit); | ||
| 235 | |||
| 236 | MODULE_AUTHOR("Magnus Damm, Paul Mundt"); | ||
| 237 | MODULE_DESCRIPTION("GPIO driver for SuperH pin function controller"); | ||
| 238 | MODULE_LICENSE("GPL v2"); | ||
| 239 | MODULE_ALIAS("platform:pfc-gpio"); | ||
