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