Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Pin Function Controller pinmux support. |
| 3 | * |
| 4 | * Copyright (C) 2012 Paul Mundt |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | */ |
Paul Mundt | a2d3aff | 2012-07-11 17:21:04 +0900 | [diff] [blame] | 10 | #define pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/sh_pfc.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/slab.h> |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 17 | #include <linux/spinlock.h> |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/pinctrl/consumer.h> |
| 20 | #include <linux/pinctrl/pinctrl.h> |
| 21 | #include <linux/pinctrl/pinconf.h> |
| 22 | #include <linux/pinctrl/pinmux.h> |
| 23 | #include <linux/pinctrl/pinconf-generic.h> |
| 24 | |
| 25 | struct sh_pfc_pinctrl { |
| 26 | struct pinctrl_dev *pctl; |
| 27 | struct sh_pfc *pfc; |
| 28 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 29 | struct pinmux_gpio **functions; |
| 30 | unsigned int nr_functions; |
| 31 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 32 | struct pinctrl_pin_desc *pads; |
| 33 | unsigned int nr_pads; |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 34 | |
| 35 | spinlock_t lock; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static struct sh_pfc_pinctrl *sh_pfc_pmx; |
| 39 | |
Paul Mundt | e3f805e | 2012-07-17 15:48:18 +0900 | [diff] [blame^] | 40 | static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev) |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 41 | { |
Paul Mundt | e3f805e | 2012-07-17 15:48:18 +0900 | [diff] [blame^] | 42 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 43 | |
| 44 | return pmx->nr_pads; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 45 | } |
| 46 | |
Paul Mundt | e3f805e | 2012-07-17 15:48:18 +0900 | [diff] [blame^] | 47 | static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 48 | unsigned selector) |
| 49 | { |
Paul Mundt | e3f805e | 2012-07-17 15:48:18 +0900 | [diff] [blame^] | 50 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 51 | |
| 52 | return pmx->pads[selector].name; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned group, |
| 56 | const unsigned **pins, unsigned *num_pins) |
| 57 | { |
Paul Mundt | e3f805e | 2012-07-17 15:48:18 +0900 | [diff] [blame^] | 58 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 59 | |
| 60 | *pins = &pmx->pads[group].number; |
| 61 | *num_pins = 1; |
| 62 | |
| 63 | return 0; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static struct pinctrl_ops sh_pfc_pinctrl_ops = { |
Paul Mundt | e3f805e | 2012-07-17 15:48:18 +0900 | [diff] [blame^] | 67 | .get_groups_count = sh_pfc_get_groups_count, |
| 68 | .get_group_name = sh_pfc_get_group_name, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 69 | .get_group_pins = sh_pfc_get_group_pins, |
| 70 | }; |
| 71 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 72 | static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev) |
| 73 | { |
| 74 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 75 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 76 | return pmx->nr_functions; |
| 77 | } |
| 78 | |
| 79 | static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev, |
| 80 | unsigned selector) |
| 81 | { |
| 82 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 83 | |
| 84 | return pmx->functions[selector]->name; |
| 85 | } |
| 86 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 87 | static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, unsigned func, |
| 88 | const char * const **groups, |
| 89 | unsigned * const num_groups) |
| 90 | { |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 91 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 92 | |
| 93 | *groups = &pmx->functions[func]->name; |
| 94 | *num_groups = 1; |
| 95 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int sh_pfc_noop_enable(struct pinctrl_dev *pctldev, unsigned func, |
| 100 | unsigned group) |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static void sh_pfc_noop_disable(struct pinctrl_dev *pctldev, unsigned func, |
| 106 | unsigned group) |
| 107 | { |
| 108 | } |
| 109 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 110 | static inline int sh_pfc_config_function(struct sh_pfc *pfc, unsigned offset) |
| 111 | { |
| 112 | if (sh_pfc_config_gpio(pfc, offset, |
| 113 | PINMUX_TYPE_FUNCTION, |
| 114 | GPIO_CFG_DRYRUN) != 0) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | if (sh_pfc_config_gpio(pfc, offset, |
| 118 | PINMUX_TYPE_FUNCTION, |
| 119 | GPIO_CFG_REQ) != 0) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 125 | static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev, |
| 126 | struct pinctrl_gpio_range *range, |
| 127 | unsigned offset) |
| 128 | { |
| 129 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 130 | struct sh_pfc *pfc = pmx->pfc; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 131 | unsigned long flags; |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 132 | int ret, pinmux_type; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 133 | |
| 134 | spin_lock_irqsave(&pfc->lock, flags); |
| 135 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 136 | pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 137 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 138 | switch (pinmux_type) { |
| 139 | case PINMUX_TYPE_FUNCTION: |
| 140 | pr_notice_once("Use of GPIO API for function requests is " |
| 141 | "deprecated, convert to pinctrl\n"); |
| 142 | /* handle for now */ |
| 143 | ret = sh_pfc_config_function(pfc, offset); |
| 144 | if (unlikely(ret < 0)) |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 145 | goto err; |
| 146 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 147 | break; |
| 148 | case PINMUX_TYPE_GPIO: |
| 149 | break; |
| 150 | default: |
| 151 | pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type); |
| 152 | return -ENOTSUPP; |
| 153 | } |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 154 | |
| 155 | ret = 0; |
| 156 | |
| 157 | err: |
| 158 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev, |
| 164 | struct pinctrl_gpio_range *range, |
| 165 | unsigned offset) |
| 166 | { |
| 167 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 168 | struct sh_pfc *pfc = pmx->pfc; |
| 169 | unsigned long flags; |
| 170 | int pinmux_type; |
| 171 | |
| 172 | spin_lock_irqsave(&pfc->lock, flags); |
| 173 | |
| 174 | pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE; |
| 175 | |
| 176 | sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE); |
| 177 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 178 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 179 | } |
| 180 | |
| 181 | static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 182 | struct pinctrl_gpio_range *range, |
| 183 | unsigned offset, bool input) |
| 184 | { |
| 185 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 186 | struct sh_pfc *pfc = pmx->pfc; |
| 187 | unsigned long flags; |
| 188 | int pinmux_type, new_pinmux_type; |
| 189 | int ret = -EINVAL; |
| 190 | |
| 191 | new_pinmux_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT; |
| 192 | |
| 193 | spin_lock_irqsave(&pfc->lock, flags); |
| 194 | |
| 195 | pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE; |
| 196 | |
| 197 | switch (pinmux_type) { |
| 198 | case PINMUX_TYPE_GPIO: |
| 199 | break; |
| 200 | case PINMUX_TYPE_OUTPUT: |
| 201 | case PINMUX_TYPE_INPUT: |
| 202 | case PINMUX_TYPE_INPUT_PULLUP: |
| 203 | case PINMUX_TYPE_INPUT_PULLDOWN: |
| 204 | sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE); |
| 205 | break; |
| 206 | default: |
| 207 | goto err; |
| 208 | } |
| 209 | |
| 210 | if (sh_pfc_config_gpio(pfc, offset, |
| 211 | new_pinmux_type, |
| 212 | GPIO_CFG_DRYRUN) != 0) |
| 213 | goto err; |
| 214 | |
| 215 | if (sh_pfc_config_gpio(pfc, offset, |
| 216 | new_pinmux_type, |
| 217 | GPIO_CFG_REQ) != 0) |
| 218 | BUG(); |
| 219 | |
| 220 | pfc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE; |
| 221 | pfc->gpios[offset].flags |= new_pinmux_type; |
| 222 | |
| 223 | ret = 0; |
| 224 | |
| 225 | err: |
| 226 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static struct pinmux_ops sh_pfc_pinmux_ops = { |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 232 | .get_functions_count = sh_pfc_get_functions_count, |
| 233 | .get_function_name = sh_pfc_get_function_name, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 234 | .get_function_groups = sh_pfc_get_function_groups, |
| 235 | .enable = sh_pfc_noop_enable, |
| 236 | .disable = sh_pfc_noop_disable, |
| 237 | .gpio_request_enable = sh_pfc_gpio_request_enable, |
| 238 | .gpio_disable_free = sh_pfc_gpio_disable_free, |
| 239 | .gpio_set_direction = sh_pfc_gpio_set_direction, |
| 240 | }; |
| 241 | |
| 242 | static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin, |
| 243 | unsigned long *config) |
| 244 | { |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 245 | enum pin_config_param param = (enum pin_config_param)(*config); |
| 246 | |
| 247 | switch (param) { |
| 248 | default: |
| 249 | break; |
| 250 | } |
| 251 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 252 | return -ENOTSUPP; |
| 253 | } |
| 254 | |
| 255 | static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin, |
| 256 | unsigned long config) |
| 257 | { |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
| 261 | static struct pinconf_ops sh_pfc_pinconf_ops = { |
| 262 | .is_generic = true, |
| 263 | .pin_config_get = sh_pfc_pinconf_get, |
| 264 | .pin_config_set = sh_pfc_pinconf_set, |
| 265 | }; |
| 266 | |
| 267 | static struct pinctrl_gpio_range sh_pfc_gpio_range = { |
| 268 | .name = KBUILD_MODNAME, |
| 269 | .id = 0, |
| 270 | }; |
| 271 | |
| 272 | static struct pinctrl_desc sh_pfc_pinctrl_desc = { |
| 273 | .name = KBUILD_MODNAME, |
| 274 | .owner = THIS_MODULE, |
| 275 | .pctlops = &sh_pfc_pinctrl_ops, |
| 276 | .pmxops = &sh_pfc_pinmux_ops, |
| 277 | .confops = &sh_pfc_pinconf_ops, |
| 278 | }; |
| 279 | |
| 280 | int sh_pfc_register_pinctrl(struct sh_pfc *pfc) |
| 281 | { |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 282 | sh_pfc_pmx = kzalloc(sizeof(struct sh_pfc_pinctrl), GFP_KERNEL); |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 283 | if (unlikely(!sh_pfc_pmx)) |
| 284 | return -ENOMEM; |
| 285 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 286 | spin_lock_init(&sh_pfc_pmx->lock); |
| 287 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 288 | sh_pfc_pmx->pfc = pfc; |
| 289 | |
| 290 | return 0; |
| 291 | } |
Paul Mundt | 1acbbb4ed | 2012-07-17 15:21:47 +0900 | [diff] [blame] | 292 | EXPORT_SYMBOL_GPL(sh_pfc_register_pinctrl); |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 293 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 294 | static inline void __devinit sh_pfc_map_one_gpio(struct sh_pfc *pfc, |
| 295 | struct sh_pfc_pinctrl *pmx, |
| 296 | struct pinmux_gpio *gpio, |
| 297 | unsigned offset) |
| 298 | { |
| 299 | struct pinmux_data_reg *dummy; |
| 300 | unsigned long flags; |
| 301 | int bit; |
| 302 | |
| 303 | gpio->flags &= ~PINMUX_FLAG_TYPE; |
| 304 | |
| 305 | if (sh_pfc_get_data_reg(pfc, offset, &dummy, &bit) == 0) |
| 306 | gpio->flags |= PINMUX_TYPE_GPIO; |
| 307 | else { |
| 308 | gpio->flags |= PINMUX_TYPE_FUNCTION; |
| 309 | |
| 310 | spin_lock_irqsave(&pmx->lock, flags); |
| 311 | pmx->nr_functions++; |
| 312 | spin_unlock_irqrestore(&pmx->lock, flags); |
| 313 | } |
| 314 | } |
| 315 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 316 | /* pinmux ranges -> pinctrl pin descs */ |
| 317 | static int __devinit sh_pfc_map_gpios(struct sh_pfc *pfc, |
| 318 | struct sh_pfc_pinctrl *pmx) |
| 319 | { |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 320 | unsigned long flags; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 321 | int i; |
| 322 | |
| 323 | pmx->nr_pads = pfc->last_gpio - pfc->first_gpio + 1; |
| 324 | |
| 325 | pmx->pads = kmalloc(sizeof(struct pinctrl_pin_desc) * pmx->nr_pads, |
| 326 | GFP_KERNEL); |
| 327 | if (unlikely(!pmx->pads)) { |
| 328 | pmx->nr_pads = 0; |
| 329 | return -ENOMEM; |
| 330 | } |
| 331 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 332 | spin_lock_irqsave(&pfc->lock, flags); |
| 333 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 334 | /* |
| 335 | * We don't necessarily have a 1:1 mapping between pin and linux |
| 336 | * GPIO number, as the latter maps to the associated enum_id. |
| 337 | * Care needs to be taken to translate back to pin space when |
| 338 | * dealing with any pin configurations. |
| 339 | */ |
| 340 | for (i = 0; i < pmx->nr_pads; i++) { |
| 341 | struct pinctrl_pin_desc *pin = pmx->pads + i; |
| 342 | struct pinmux_gpio *gpio = pfc->gpios + i; |
| 343 | |
| 344 | pin->number = pfc->first_gpio + i; |
| 345 | pin->name = gpio->name; |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 346 | |
Paul Mundt | e3e7945 | 2012-07-17 15:23:11 +0900 | [diff] [blame] | 347 | /* XXX */ |
| 348 | if (unlikely(!gpio->enum_id)) |
| 349 | continue; |
| 350 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 351 | sh_pfc_map_one_gpio(pfc, pmx, gpio, i); |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 352 | } |
| 353 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 354 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 355 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 356 | sh_pfc_pinctrl_desc.pins = pmx->pads; |
| 357 | sh_pfc_pinctrl_desc.npins = pmx->nr_pads; |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 362 | static int __devinit sh_pfc_map_functions(struct sh_pfc *pfc, |
| 363 | struct sh_pfc_pinctrl *pmx) |
| 364 | { |
| 365 | unsigned long flags; |
| 366 | int i, fn; |
| 367 | |
| 368 | pmx->functions = kzalloc(pmx->nr_functions * sizeof(void *), |
| 369 | GFP_KERNEL); |
| 370 | if (unlikely(!pmx->functions)) |
| 371 | return -ENOMEM; |
| 372 | |
| 373 | spin_lock_irqsave(&pmx->lock, flags); |
| 374 | |
| 375 | for (i = fn = 0; i < pmx->nr_pads; i++) { |
| 376 | struct pinmux_gpio *gpio = pfc->gpios + i; |
| 377 | |
| 378 | if ((gpio->flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_FUNCTION) |
| 379 | pmx->functions[fn++] = gpio; |
| 380 | } |
| 381 | |
| 382 | spin_unlock_irqrestore(&pmx->lock, flags); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 387 | static int __devinit sh_pfc_pinctrl_probe(struct platform_device *pdev) |
| 388 | { |
| 389 | struct sh_pfc *pfc; |
| 390 | int ret; |
| 391 | |
| 392 | if (unlikely(!sh_pfc_pmx)) |
| 393 | return -ENODEV; |
| 394 | |
| 395 | pfc = sh_pfc_pmx->pfc; |
| 396 | |
| 397 | ret = sh_pfc_map_gpios(pfc, sh_pfc_pmx); |
| 398 | if (unlikely(ret != 0)) |
| 399 | return ret; |
| 400 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 401 | ret = sh_pfc_map_functions(pfc, sh_pfc_pmx); |
| 402 | if (unlikely(ret != 0)) |
| 403 | goto free_pads; |
| 404 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 405 | sh_pfc_pmx->pctl = pinctrl_register(&sh_pfc_pinctrl_desc, &pdev->dev, |
| 406 | sh_pfc_pmx); |
| 407 | if (IS_ERR(sh_pfc_pmx->pctl)) { |
| 408 | ret = PTR_ERR(sh_pfc_pmx->pctl); |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 409 | goto free_functions; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | sh_pfc_gpio_range.npins = pfc->last_gpio - pfc->first_gpio + 1; |
| 413 | sh_pfc_gpio_range.base = pfc->first_gpio; |
| 414 | sh_pfc_gpio_range.pin_base = pfc->first_gpio; |
| 415 | |
| 416 | pinctrl_add_gpio_range(sh_pfc_pmx->pctl, &sh_pfc_gpio_range); |
| 417 | |
| 418 | platform_set_drvdata(pdev, sh_pfc_pmx); |
| 419 | |
| 420 | return 0; |
| 421 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 422 | free_functions: |
| 423 | kfree(sh_pfc_pmx->functions); |
| 424 | free_pads: |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 425 | kfree(sh_pfc_pmx->pads); |
| 426 | kfree(sh_pfc_pmx); |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 427 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | static int __devexit sh_pfc_pinctrl_remove(struct platform_device *pdev) |
| 432 | { |
| 433 | struct sh_pfc_pinctrl *pmx = platform_get_drvdata(pdev); |
| 434 | |
| 435 | pinctrl_remove_gpio_range(pmx->pctl, &sh_pfc_gpio_range); |
| 436 | pinctrl_unregister(pmx->pctl); |
| 437 | |
| 438 | platform_set_drvdata(pdev, NULL); |
| 439 | |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 440 | kfree(sh_pfc_pmx->functions); |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 441 | kfree(sh_pfc_pmx->pads); |
| 442 | kfree(sh_pfc_pmx); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static struct platform_driver sh_pfc_pinctrl_driver = { |
| 448 | .probe = sh_pfc_pinctrl_probe, |
| 449 | .remove = __devexit_p(sh_pfc_pinctrl_remove), |
| 450 | .driver = { |
| 451 | .name = KBUILD_MODNAME, |
| 452 | .owner = THIS_MODULE, |
| 453 | }, |
| 454 | }; |
| 455 | |
| 456 | static struct platform_device sh_pfc_pinctrl_device = { |
| 457 | .name = KBUILD_MODNAME, |
| 458 | .id = -1, |
| 459 | }; |
| 460 | |
| 461 | static int __init sh_pfc_pinctrl_init(void) |
| 462 | { |
| 463 | int rc; |
| 464 | |
| 465 | rc = platform_driver_register(&sh_pfc_pinctrl_driver); |
| 466 | if (likely(!rc)) { |
| 467 | rc = platform_device_register(&sh_pfc_pinctrl_device); |
| 468 | if (unlikely(rc)) |
| 469 | platform_driver_unregister(&sh_pfc_pinctrl_driver); |
| 470 | } |
| 471 | |
| 472 | return rc; |
| 473 | } |
| 474 | |
| 475 | static void __exit sh_pfc_pinctrl_exit(void) |
| 476 | { |
| 477 | platform_driver_unregister(&sh_pfc_pinctrl_driver); |
| 478 | } |
| 479 | |
| 480 | subsys_initcall(sh_pfc_pinctrl_init); |
| 481 | module_exit(sh_pfc_pinctrl_exit); |