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" |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 27 | #include "../core.h" |
| 28 | #include "../pinconf.h" |
Laurent Pinchart | f916513 | 2012-12-15 23:50:44 +0100 | [diff] [blame] | 29 | |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 30 | struct sh_pfc_pin_config { |
| 31 | u32 type; |
| 32 | }; |
| 33 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 34 | struct sh_pfc_pinctrl { |
| 35 | struct pinctrl_dev *pctl; |
Laurent Pinchart | dcc427e | 2013-02-16 16:38:30 +0100 | [diff] [blame] | 36 | struct pinctrl_desc pctl_desc; |
Laurent Pinchart | dcc427e | 2013-02-16 16:38:30 +0100 | [diff] [blame] | 37 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 38 | struct sh_pfc *pfc; |
| 39 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 40 | struct pinctrl_pin_desc *pins; |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 41 | struct sh_pfc_pin_config *configs; |
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 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 48 | return pmx->pfc->info->nr_groups; |
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 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 56 | return pmx->pfc->info->groups[selector].name; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 57 | } |
| 58 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 59 | static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 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 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 64 | *pins = pmx->pfc->info->groups[selector].pins; |
| 65 | *num_pins = pmx->pfc->info->groups[selector].nr_pins; |
Paul Mundt | e3f805e | 2012-07-17 15:48:18 +0900 | [diff] [blame] | 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 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 87 | return pmx->pfc->info->nr_functions; |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 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 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 95 | return pmx->pfc->info->functions[selector].name; |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 98 | static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, |
| 99 | unsigned selector, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 100 | const char * const **groups, |
| 101 | unsigned * const num_groups) |
| 102 | { |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 103 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 104 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 105 | *groups = pmx->pfc->info->functions[selector].groups; |
| 106 | *num_groups = pmx->pfc->info->functions[selector].nr_groups; |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 107 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 111 | static int sh_pfc_func_enable(struct pinctrl_dev *pctldev, unsigned selector, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 112 | unsigned group) |
| 113 | { |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 114 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 115 | struct sh_pfc *pfc = pmx->pfc; |
| 116 | const struct sh_pfc_pin_group *grp = &pfc->info->groups[group]; |
| 117 | unsigned long flags; |
| 118 | unsigned int i; |
Laurent Pinchart | b705c05 | 2013-03-10 16:38:23 +0100 | [diff] [blame] | 119 | int ret = 0; |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 120 | |
| 121 | spin_lock_irqsave(&pfc->lock, flags); |
| 122 | |
| 123 | for (i = 0; i < grp->nr_pins; ++i) { |
Laurent Pinchart | b705c05 | 2013-03-10 16:38:23 +0100 | [diff] [blame] | 124 | ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION); |
| 125 | if (ret < 0) |
| 126 | break; |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 129 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 130 | return ret; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 133 | static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 134 | unsigned group) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev, |
| 139 | struct pinctrl_gpio_range *range, |
| 140 | unsigned offset) |
| 141 | { |
| 142 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 143 | struct sh_pfc *pfc = pmx->pfc; |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 144 | int idx = sh_pfc_get_pin_index(pfc, offset); |
| 145 | struct sh_pfc_pin_config *cfg = &pmx->configs[idx]; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 146 | unsigned long flags; |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 147 | int ret; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 148 | |
| 149 | spin_lock_irqsave(&pfc->lock, flags); |
| 150 | |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 151 | switch (cfg->type) { |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 152 | case PINMUX_TYPE_GPIO: |
Paul Mundt | 16d74eb | 2012-09-25 11:51:05 +0900 | [diff] [blame] | 153 | case PINMUX_TYPE_INPUT: |
| 154 | case PINMUX_TYPE_OUTPUT: |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 155 | break; |
Laurent Pinchart | 2119f7c | 2012-11-29 13:03:53 +0100 | [diff] [blame] | 156 | case PINMUX_TYPE_FUNCTION: |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 157 | default: |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 158 | pr_err("Unsupported mux type (%d), bailing...\n", cfg->type); |
Laurent Pinchart | 077664a | 2012-09-14 20:25:48 +0200 | [diff] [blame] | 159 | ret = -ENOTSUPP; |
| 160 | goto err; |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 161 | } |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 162 | |
| 163 | ret = 0; |
| 164 | |
| 165 | err: |
| 166 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev, |
| 172 | struct pinctrl_gpio_range *range, |
| 173 | unsigned offset) |
| 174 | { |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 178 | struct pinctrl_gpio_range *range, |
| 179 | unsigned offset, bool input) |
| 180 | { |
| 181 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
Laurent Pinchart | 0d00f00 | 2013-03-10 16:55:19 +0100 | [diff] [blame^] | 182 | struct sh_pfc *pfc = pmx->pfc; |
| 183 | int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT; |
| 184 | int idx = sh_pfc_get_pin_index(pfc, offset); |
| 185 | struct sh_pfc_pin_config *cfg = &pmx->configs[idx]; |
| 186 | const struct sh_pfc_pin *pin = &pfc->info->pins[idx]; |
| 187 | unsigned int mark = pin->enum_id; |
| 188 | unsigned long flags; |
| 189 | int ret; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 190 | |
Laurent Pinchart | 0d00f00 | 2013-03-10 16:55:19 +0100 | [diff] [blame^] | 191 | spin_lock_irqsave(&pfc->lock, flags); |
| 192 | |
| 193 | switch (cfg->type) { |
| 194 | case PINMUX_TYPE_GPIO: |
| 195 | case PINMUX_TYPE_OUTPUT: |
| 196 | case PINMUX_TYPE_INPUT: |
| 197 | case PINMUX_TYPE_INPUT_PULLUP: |
| 198 | case PINMUX_TYPE_INPUT_PULLDOWN: |
| 199 | break; |
| 200 | default: |
| 201 | ret = -EINVAL; |
| 202 | goto done; |
| 203 | } |
| 204 | |
| 205 | ret = sh_pfc_config_mux(pfc, mark, new_type); |
| 206 | if (ret < 0) |
| 207 | goto done; |
| 208 | |
| 209 | cfg->type = new_type; |
| 210 | |
| 211 | done: |
| 212 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 213 | |
| 214 | return ret; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Laurent Pinchart | fe330ce | 2013-02-15 16:04:47 +0100 | [diff] [blame] | 217 | static const struct pinmux_ops sh_pfc_pinmux_ops = { |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 218 | .get_functions_count = sh_pfc_get_functions_count, |
| 219 | .get_function_name = sh_pfc_get_function_name, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 220 | .get_function_groups = sh_pfc_get_function_groups, |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 221 | .enable = sh_pfc_func_enable, |
| 222 | .disable = sh_pfc_func_disable, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 223 | .gpio_request_enable = sh_pfc_gpio_request_enable, |
| 224 | .gpio_disable_free = sh_pfc_gpio_disable_free, |
| 225 | .gpio_set_direction = sh_pfc_gpio_set_direction, |
| 226 | }; |
| 227 | |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 228 | /* Check whether the requested parameter is supported for a pin. */ |
| 229 | static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin, |
| 230 | enum pin_config_param param) |
| 231 | { |
| 232 | int idx = sh_pfc_get_pin_index(pfc, _pin); |
| 233 | const struct sh_pfc_pin *pin = &pfc->info->pins[idx]; |
| 234 | |
| 235 | switch (param) { |
| 236 | case PIN_CONFIG_BIAS_DISABLE: |
| 237 | return true; |
| 238 | |
| 239 | case PIN_CONFIG_BIAS_PULL_UP: |
| 240 | return pin->configs & SH_PFC_PIN_CFG_PULL_UP; |
| 241 | |
| 242 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 243 | return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN; |
| 244 | |
| 245 | default: |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | |
Laurent Pinchart | 934cb02 | 2013-02-14 22:35:09 +0100 | [diff] [blame] | 250 | static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 251 | unsigned long *config) |
| 252 | { |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 253 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 254 | struct sh_pfc *pfc = pmx->pfc; |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 255 | enum pin_config_param param = pinconf_to_config_param(*config); |
| 256 | unsigned long flags; |
| 257 | unsigned int bias; |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 258 | |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 259 | if (!sh_pfc_pinconf_validate(pfc, _pin, param)) |
| 260 | return -ENOTSUPP; |
| 261 | |
| 262 | switch (param) { |
| 263 | case PIN_CONFIG_BIAS_DISABLE: |
| 264 | case PIN_CONFIG_BIAS_PULL_UP: |
| 265 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 266 | if (!pfc->info->ops || !pfc->info->ops->get_bias) |
| 267 | return -ENOTSUPP; |
| 268 | |
| 269 | spin_lock_irqsave(&pfc->lock, flags); |
| 270 | bias = pfc->info->ops->get_bias(pfc, _pin); |
| 271 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 272 | |
| 273 | if (bias != param) |
| 274 | return -EINVAL; |
| 275 | |
| 276 | *config = 0; |
| 277 | break; |
| 278 | |
| 279 | default: |
| 280 | return -ENOTSUPP; |
| 281 | } |
Paul Mundt | d93a891 | 2012-07-11 17:17:10 +0900 | [diff] [blame] | 282 | |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 283 | return 0; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 284 | } |
| 285 | |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 286 | static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 287 | unsigned long config) |
| 288 | { |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 289 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 290 | struct sh_pfc *pfc = pmx->pfc; |
| 291 | enum pin_config_param param = pinconf_to_config_param(config); |
| 292 | unsigned long flags; |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 293 | |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 294 | if (!sh_pfc_pinconf_validate(pfc, _pin, param)) |
| 295 | return -ENOTSUPP; |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 296 | |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 297 | switch (param) { |
| 298 | case PIN_CONFIG_BIAS_PULL_UP: |
| 299 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 300 | case PIN_CONFIG_BIAS_DISABLE: |
| 301 | if (!pfc->info->ops || !pfc->info->ops->set_bias) |
| 302 | return -ENOTSUPP; |
| 303 | |
| 304 | spin_lock_irqsave(&pfc->lock, flags); |
| 305 | pfc->info->ops->set_bias(pfc, _pin, param); |
| 306 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 307 | |
| 308 | break; |
| 309 | |
| 310 | default: |
| 311 | return -ENOTSUPP; |
| 312 | } |
| 313 | |
| 314 | return 0; |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 315 | } |
| 316 | |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 317 | static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group, |
| 318 | unsigned long config) |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 319 | { |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 320 | struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev); |
| 321 | const unsigned int *pins; |
| 322 | unsigned int num_pins; |
| 323 | unsigned int i; |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 324 | |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 325 | pins = pmx->pfc->info->groups[group].pins; |
| 326 | num_pins = pmx->pfc->info->groups[group].nr_pins; |
Paul Mundt | fdd85ec | 2012-07-20 16:39:09 +0900 | [diff] [blame] | 327 | |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 328 | for (i = 0; i < num_pins; ++i) |
| 329 | sh_pfc_pinconf_set(pctldev, pins[i], config); |
| 330 | |
| 331 | return 0; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 332 | } |
| 333 | |
Laurent Pinchart | fe330ce | 2013-02-15 16:04:47 +0100 | [diff] [blame] | 334 | static const struct pinconf_ops sh_pfc_pinconf_ops = { |
Laurent Pinchart | c58d9c1 | 2013-03-10 16:44:02 +0100 | [diff] [blame] | 335 | .is_generic = true, |
| 336 | .pin_config_get = sh_pfc_pinconf_get, |
| 337 | .pin_config_set = sh_pfc_pinconf_set, |
| 338 | .pin_config_group_set = sh_pfc_pinconf_group_set, |
| 339 | .pin_config_config_dbg_show = pinconf_generic_dump_config, |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 340 | }; |
| 341 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 342 | /* PFC ranges -> pinctrl pin descs */ |
| 343 | static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx) |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 344 | { |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 345 | const struct pinmux_range *ranges; |
| 346 | struct pinmux_range def_range; |
| 347 | unsigned int nr_ranges; |
| 348 | unsigned int nr_pins; |
| 349 | unsigned int i; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 350 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 351 | if (pfc->info->ranges == NULL) { |
| 352 | def_range.begin = 0; |
| 353 | def_range.end = pfc->info->nr_pins - 1; |
| 354 | ranges = &def_range; |
| 355 | nr_ranges = 1; |
| 356 | } else { |
| 357 | ranges = pfc->info->ranges; |
| 358 | nr_ranges = pfc->info->nr_ranges; |
| 359 | } |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 360 | |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 361 | pmx->pins = devm_kzalloc(pfc->dev, |
| 362 | sizeof(*pmx->pins) * pfc->info->nr_pins, |
Laurent Pinchart | 1724acf | 2012-12-15 23:50:48 +0100 | [diff] [blame] | 363 | GFP_KERNEL); |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 364 | if (unlikely(!pmx->pins)) |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 365 | return -ENOMEM; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 366 | |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 367 | pmx->configs = devm_kzalloc(pfc->dev, |
| 368 | sizeof(*pmx->configs) * pfc->info->nr_pins, |
| 369 | GFP_KERNEL); |
| 370 | if (unlikely(!pmx->configs)) |
| 371 | return -ENOMEM; |
| 372 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 373 | for (i = 0, nr_pins = 0; i < nr_ranges; ++i) { |
| 374 | const struct pinmux_range *range = &ranges[i]; |
| 375 | unsigned int number; |
| 376 | |
| 377 | for (number = range->begin; number <= range->end; |
| 378 | number++, nr_pins++) { |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 379 | struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins]; |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 380 | struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins]; |
Laurent Pinchart | cd3c1be | 2013-02-16 18:47:05 +0100 | [diff] [blame] | 381 | const struct sh_pfc_pin *info = |
| 382 | &pfc->info->pins[nr_pins]; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 383 | |
| 384 | pin->number = number; |
| 385 | pin->name = info->name; |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 386 | cfg->type = PINMUX_TYPE_GPIO; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 387 | } |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 388 | } |
| 389 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 390 | pfc->nr_pins = ranges[nr_ranges-1].end + 1; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 391 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 392 | return nr_ranges; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 393 | } |
| 394 | |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 395 | int sh_pfc_register_pinctrl(struct sh_pfc *pfc) |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 396 | { |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 397 | struct sh_pfc_pinctrl *pmx; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 398 | int nr_ranges; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 399 | |
Laurent Pinchart | 1724acf | 2012-12-15 23:50:48 +0100 | [diff] [blame] | 400 | pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL); |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 401 | if (unlikely(!pmx)) |
| 402 | return -ENOMEM; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 403 | |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 404 | pmx->pfc = pfc; |
| 405 | pfc->pinctrl = pmx; |
| 406 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 407 | nr_ranges = sh_pfc_map_pins(pfc, pmx); |
| 408 | if (unlikely(nr_ranges < 0)) |
| 409 | return nr_ranges; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 410 | |
Laurent Pinchart | dcc427e | 2013-02-16 16:38:30 +0100 | [diff] [blame] | 411 | pmx->pctl_desc.name = DRV_NAME; |
| 412 | pmx->pctl_desc.owner = THIS_MODULE; |
| 413 | pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops; |
| 414 | pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops; |
| 415 | pmx->pctl_desc.confops = &sh_pfc_pinconf_ops; |
Laurent Pinchart | 3d8d9f1 | 2013-01-03 14:33:13 +0100 | [diff] [blame] | 416 | pmx->pctl_desc.pins = pmx->pins; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 417 | pmx->pctl_desc.npins = pfc->info->nr_pins; |
Laurent Pinchart | dcc427e | 2013-02-16 16:38:30 +0100 | [diff] [blame] | 418 | |
| 419 | pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx); |
Wei Yongjun | fd9d05b | 2013-03-11 22:08:12 +0800 | [diff] [blame] | 420 | if (pmx->pctl == NULL) |
| 421 | return -EINVAL; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 422 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 423 | return 0; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 424 | } |
| 425 | |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 426 | int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc) |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 427 | { |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 428 | struct sh_pfc_pinctrl *pmx = pfc->pinctrl; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 429 | |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 430 | pinctrl_unregister(pmx->pctl); |
| 431 | |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 432 | pfc->pinctrl = NULL; |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 433 | return 0; |
| 434 | } |