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