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