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