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