Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1 | /* |
| 2 | * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's SoC's. |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com |
| 6 | * Copyright (c) 2012 Linaro Ltd |
| 7 | * http://www.linaro.org |
| 8 | * |
| 9 | * Author: Thomas Abraham <thomas.ab@samsung.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This driver implements the Samsung pinctrl driver. It supports setting up of |
| 17 | * pinmux and pinconf configurations. The gpiolib interface is also included. |
| 18 | * External interrupt (gpio and wakeup) support are not included in this driver |
| 19 | * but provides extensions to which platform specific implementation of the gpio |
| 20 | * and wakeup interrupts can be hooked to. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/io.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <linux/gpio.h> |
Tomasz Figa | a19fe2d | 2012-10-11 10:11:20 +0200 | [diff] [blame] | 29 | #include <linux/irqdomain.h> |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 30 | #include <linux/spinlock.h> |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 31 | #include <linux/syscore_ops.h> |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 32 | |
Sachin Kamat | ebe629a | 2014-07-10 17:33:27 +0530 | [diff] [blame] | 33 | #include "../core.h" |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 34 | #include "pinctrl-samsung.h" |
| 35 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 36 | /* list of all possible config options supported */ |
Axel Lin | d5fd5da | 2012-12-10 09:54:03 +0900 | [diff] [blame] | 37 | static struct pin_config { |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 38 | const char *property; |
| 39 | enum pincfg_type param; |
| 40 | } cfg_params[] = { |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 41 | { "samsung,pin-pud", PINCFG_TYPE_PUD }, |
| 42 | { "samsung,pin-drv", PINCFG_TYPE_DRV }, |
| 43 | { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN }, |
| 44 | { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN }, |
Tomasz Figa | 2700bc0 | 2014-07-02 17:41:04 +0200 | [diff] [blame] | 45 | { "samsung,pin-val", PINCFG_TYPE_DAT }, |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 46 | }; |
| 47 | |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 48 | /* Global list of devices (struct samsung_pinctrl_drv_data) */ |
Sachin Kamat | b940897 | 2013-06-24 16:50:21 +0530 | [diff] [blame] | 49 | static LIST_HEAD(drvdata_list); |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 50 | |
Sachin Kamat | 6fb6f1ba | 2012-11-24 11:20:51 +0900 | [diff] [blame] | 51 | static unsigned int pin_base; |
Tomasz Figa | 40ba622 | 2012-10-11 10:11:09 +0200 | [diff] [blame] | 52 | |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 53 | static inline struct samsung_pin_bank *gc_to_pin_bank(struct gpio_chip *gc) |
| 54 | { |
| 55 | return container_of(gc, struct samsung_pin_bank, gpio_chip); |
| 56 | } |
| 57 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 58 | static int samsung_get_group_count(struct pinctrl_dev *pctldev) |
| 59 | { |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 60 | struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 61 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 62 | return pmx->nr_groups; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 65 | static const char *samsung_get_group_name(struct pinctrl_dev *pctldev, |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 66 | unsigned group) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 67 | { |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 68 | struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 69 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 70 | return pmx->pin_groups[group].name; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 71 | } |
| 72 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 73 | static int samsung_get_group_pins(struct pinctrl_dev *pctldev, |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 74 | unsigned group, |
| 75 | const unsigned **pins, |
| 76 | unsigned *num_pins) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 77 | { |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 78 | struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 79 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 80 | *pins = pmx->pin_groups[group].pins; |
| 81 | *num_pins = pmx->pin_groups[group].num_pins; |
| 82 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 86 | static int reserve_map(struct device *dev, struct pinctrl_map **map, |
| 87 | unsigned *reserved_maps, unsigned *num_maps, |
| 88 | unsigned reserve) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 89 | { |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 90 | unsigned old_num = *reserved_maps; |
| 91 | unsigned new_num = *num_maps + reserve; |
| 92 | struct pinctrl_map *new_map; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 93 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 94 | if (old_num >= new_num) |
| 95 | return 0; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 96 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 97 | new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL); |
| 98 | if (!new_map) { |
| 99 | dev_err(dev, "krealloc(map) failed\n"); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 100 | return -ENOMEM; |
| 101 | } |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 102 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 103 | memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map)); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 104 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 105 | *map = new_map; |
| 106 | *reserved_maps = new_num; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 107 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 108 | return 0; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 109 | } |
| 110 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 111 | static int add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps, |
| 112 | unsigned *num_maps, const char *group, |
| 113 | const char *function) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 114 | { |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 115 | if (WARN_ON(*num_maps == *reserved_maps)) |
| 116 | return -ENOSPC; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 117 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 118 | (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP; |
| 119 | (*map)[*num_maps].data.mux.group = group; |
| 120 | (*map)[*num_maps].data.mux.function = function; |
| 121 | (*num_maps)++; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int add_map_configs(struct device *dev, struct pinctrl_map **map, |
| 127 | unsigned *reserved_maps, unsigned *num_maps, |
| 128 | const char *group, unsigned long *configs, |
| 129 | unsigned num_configs) |
| 130 | { |
| 131 | unsigned long *dup_configs; |
| 132 | |
| 133 | if (WARN_ON(*num_maps == *reserved_maps)) |
| 134 | return -ENOSPC; |
| 135 | |
| 136 | dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs), |
| 137 | GFP_KERNEL); |
| 138 | if (!dup_configs) { |
| 139 | dev_err(dev, "kmemdup(configs) failed\n"); |
| 140 | return -ENOMEM; |
| 141 | } |
| 142 | |
| 143 | (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_GROUP; |
| 144 | (*map)[*num_maps].data.configs.group_or_pin = group; |
| 145 | (*map)[*num_maps].data.configs.configs = dup_configs; |
| 146 | (*map)[*num_maps].data.configs.num_configs = num_configs; |
| 147 | (*num_maps)++; |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int add_config(struct device *dev, unsigned long **configs, |
| 153 | unsigned *num_configs, unsigned long config) |
| 154 | { |
| 155 | unsigned old_num = *num_configs; |
| 156 | unsigned new_num = old_num + 1; |
| 157 | unsigned long *new_configs; |
| 158 | |
| 159 | new_configs = krealloc(*configs, sizeof(*new_configs) * new_num, |
| 160 | GFP_KERNEL); |
| 161 | if (!new_configs) { |
| 162 | dev_err(dev, "krealloc(configs) failed\n"); |
| 163 | return -ENOMEM; |
| 164 | } |
| 165 | |
| 166 | new_configs[old_num] = config; |
| 167 | |
| 168 | *configs = new_configs; |
| 169 | *num_configs = new_num; |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void samsung_dt_free_map(struct pinctrl_dev *pctldev, |
| 175 | struct pinctrl_map *map, |
| 176 | unsigned num_maps) |
| 177 | { |
| 178 | int i; |
| 179 | |
| 180 | for (i = 0; i < num_maps; i++) |
| 181 | if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP) |
| 182 | kfree(map[i].data.configs.configs); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 183 | |
| 184 | kfree(map); |
| 185 | } |
| 186 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 187 | static int samsung_dt_subnode_to_map(struct samsung_pinctrl_drv_data *drvdata, |
| 188 | struct device *dev, |
| 189 | struct device_node *np, |
| 190 | struct pinctrl_map **map, |
| 191 | unsigned *reserved_maps, |
| 192 | unsigned *num_maps) |
| 193 | { |
| 194 | int ret, i; |
| 195 | u32 val; |
| 196 | unsigned long config; |
| 197 | unsigned long *configs = NULL; |
| 198 | unsigned num_configs = 0; |
| 199 | unsigned reserve; |
| 200 | struct property *prop; |
| 201 | const char *group; |
| 202 | bool has_func = false; |
| 203 | |
| 204 | ret = of_property_read_u32(np, "samsung,pin-function", &val); |
| 205 | if (!ret) |
| 206 | has_func = true; |
| 207 | |
| 208 | for (i = 0; i < ARRAY_SIZE(cfg_params); i++) { |
| 209 | ret = of_property_read_u32(np, cfg_params[i].property, &val); |
| 210 | if (!ret) { |
| 211 | config = PINCFG_PACK(cfg_params[i].param, val); |
| 212 | ret = add_config(dev, &configs, &num_configs, config); |
| 213 | if (ret < 0) |
| 214 | goto exit; |
| 215 | /* EINVAL=missing, which is fine since it's optional */ |
| 216 | } else if (ret != -EINVAL) { |
| 217 | dev_err(dev, "could not parse property %s\n", |
| 218 | cfg_params[i].property); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | reserve = 0; |
| 223 | if (has_func) |
| 224 | reserve++; |
| 225 | if (num_configs) |
| 226 | reserve++; |
| 227 | ret = of_property_count_strings(np, "samsung,pins"); |
| 228 | if (ret < 0) { |
| 229 | dev_err(dev, "could not parse property samsung,pins\n"); |
| 230 | goto exit; |
| 231 | } |
| 232 | reserve *= ret; |
| 233 | |
| 234 | ret = reserve_map(dev, map, reserved_maps, num_maps, reserve); |
| 235 | if (ret < 0) |
| 236 | goto exit; |
| 237 | |
| 238 | of_property_for_each_string(np, "samsung,pins", prop, group) { |
| 239 | if (has_func) { |
| 240 | ret = add_map_mux(map, reserved_maps, |
| 241 | num_maps, group, np->full_name); |
| 242 | if (ret < 0) |
| 243 | goto exit; |
| 244 | } |
| 245 | |
| 246 | if (num_configs) { |
| 247 | ret = add_map_configs(dev, map, reserved_maps, |
| 248 | num_maps, group, configs, |
| 249 | num_configs); |
| 250 | if (ret < 0) |
| 251 | goto exit; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | ret = 0; |
| 256 | |
| 257 | exit: |
| 258 | kfree(configs); |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 263 | struct device_node *np_config, |
| 264 | struct pinctrl_map **map, |
| 265 | unsigned *num_maps) |
| 266 | { |
| 267 | struct samsung_pinctrl_drv_data *drvdata; |
| 268 | unsigned reserved_maps; |
| 269 | struct device_node *np; |
| 270 | int ret; |
| 271 | |
| 272 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 273 | |
| 274 | reserved_maps = 0; |
| 275 | *map = NULL; |
| 276 | *num_maps = 0; |
| 277 | |
| 278 | if (!of_get_child_count(np_config)) |
| 279 | return samsung_dt_subnode_to_map(drvdata, pctldev->dev, |
| 280 | np_config, map, |
| 281 | &reserved_maps, |
| 282 | num_maps); |
| 283 | |
| 284 | for_each_child_of_node(np_config, np) { |
| 285 | ret = samsung_dt_subnode_to_map(drvdata, pctldev->dev, np, map, |
| 286 | &reserved_maps, num_maps); |
| 287 | if (ret < 0) { |
| 288 | samsung_dt_free_map(pctldev, *map, *num_maps); |
| 289 | return ret; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 296 | /* list of pinctrl callbacks for the pinctrl core */ |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 297 | static const struct pinctrl_ops samsung_pctrl_ops = { |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 298 | .get_groups_count = samsung_get_group_count, |
| 299 | .get_group_name = samsung_get_group_name, |
| 300 | .get_group_pins = samsung_get_group_pins, |
| 301 | .dt_node_to_map = samsung_dt_node_to_map, |
| 302 | .dt_free_map = samsung_dt_free_map, |
| 303 | }; |
| 304 | |
| 305 | /* check if the selector is a valid pin function selector */ |
| 306 | static int samsung_get_functions_count(struct pinctrl_dev *pctldev) |
| 307 | { |
| 308 | struct samsung_pinctrl_drv_data *drvdata; |
| 309 | |
| 310 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 311 | return drvdata->nr_functions; |
| 312 | } |
| 313 | |
| 314 | /* return the name of the pin function specified */ |
| 315 | static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev, |
| 316 | unsigned selector) |
| 317 | { |
| 318 | struct samsung_pinctrl_drv_data *drvdata; |
| 319 | |
| 320 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 321 | return drvdata->pmx_functions[selector].name; |
| 322 | } |
| 323 | |
| 324 | /* return the groups associated for the specified function selector */ |
| 325 | static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev, |
| 326 | unsigned selector, const char * const **groups, |
| 327 | unsigned * const num_groups) |
| 328 | { |
| 329 | struct samsung_pinctrl_drv_data *drvdata; |
| 330 | |
| 331 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 332 | *groups = drvdata->pmx_functions[selector].groups; |
| 333 | *num_groups = drvdata->pmx_functions[selector].num_groups; |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * given a pin number that is local to a pin controller, find out the pin bank |
| 339 | * and the register base of the pin bank. |
| 340 | */ |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame] | 341 | static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata, |
| 342 | unsigned pin, void __iomem **reg, u32 *offset, |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 343 | struct samsung_pin_bank **bank) |
| 344 | { |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 345 | struct samsung_pin_bank *b; |
| 346 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 347 | b = drvdata->pin_banks; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 348 | |
| 349 | while ((pin >= b->pin_base) && |
| 350 | ((b->pin_base + b->nr_pins - 1) < pin)) |
| 351 | b++; |
| 352 | |
| 353 | *reg = drvdata->virt_base + b->pctl_offset; |
| 354 | *offset = pin - b->pin_base; |
| 355 | if (bank) |
| 356 | *bank = b; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* enable or disable a pinmux function */ |
| 360 | static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector, |
| 361 | unsigned group, bool enable) |
| 362 | { |
| 363 | struct samsung_pinctrl_drv_data *drvdata; |
Tomasz Figa | 94ce944 | 2014-09-23 21:05:39 +0200 | [diff] [blame] | 364 | const struct samsung_pin_bank_type *type; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 365 | struct samsung_pin_bank *bank; |
| 366 | void __iomem *reg; |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 367 | u32 mask, shift, data, pin_offset; |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 368 | unsigned long flags; |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 369 | const struct samsung_pmx_func *func; |
| 370 | const struct samsung_pin_group *grp; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 371 | |
| 372 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 373 | func = &drvdata->pmx_functions[selector]; |
| 374 | grp = &drvdata->pin_groups[group]; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 375 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 376 | pin_to_reg_bank(drvdata, grp->pins[0] - drvdata->pin_base, |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 377 | ®, &pin_offset, &bank); |
| 378 | type = bank->type; |
| 379 | mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1; |
| 380 | shift = pin_offset * type->fld_width[PINCFG_TYPE_FUNC]; |
| 381 | if (shift >= 32) { |
| 382 | /* Some banks have two config registers */ |
| 383 | shift -= 32; |
| 384 | reg += 4; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 385 | } |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 386 | |
| 387 | spin_lock_irqsave(&bank->slock, flags); |
| 388 | |
| 389 | data = readl(reg + type->reg_offset[PINCFG_TYPE_FUNC]); |
| 390 | data &= ~(mask << shift); |
| 391 | if (enable) |
| 392 | data |= func->val << shift; |
| 393 | writel(data, reg + type->reg_offset[PINCFG_TYPE_FUNC]); |
| 394 | |
| 395 | spin_unlock_irqrestore(&bank->slock, flags); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* enable a specified pinmux by writing to registers */ |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 399 | static int samsung_pinmux_set_mux(struct pinctrl_dev *pctldev, |
| 400 | unsigned selector, |
| 401 | unsigned group) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 402 | { |
| 403 | samsung_pinmux_setup(pctldev, selector, group, true); |
| 404 | return 0; |
| 405 | } |
| 406 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 407 | /* list of pinmux callbacks for the pinmux vertical in pinctrl core */ |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 408 | static const struct pinmux_ops samsung_pinmux_ops = { |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 409 | .get_functions_count = samsung_get_functions_count, |
| 410 | .get_function_name = samsung_pinmux_get_fname, |
| 411 | .get_function_groups = samsung_pinmux_get_groups, |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 412 | .set_mux = samsung_pinmux_set_mux, |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | /* set or get the pin config settings for a specified pin */ |
| 416 | static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin, |
| 417 | unsigned long *config, bool set) |
| 418 | { |
| 419 | struct samsung_pinctrl_drv_data *drvdata; |
Tomasz Figa | 94ce944 | 2014-09-23 21:05:39 +0200 | [diff] [blame] | 420 | const struct samsung_pin_bank_type *type; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 421 | struct samsung_pin_bank *bank; |
| 422 | void __iomem *reg_base; |
| 423 | enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config); |
| 424 | u32 data, width, pin_offset, mask, shift; |
| 425 | u32 cfg_value, cfg_reg; |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 426 | unsigned long flags; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 427 | |
| 428 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 429 | pin_to_reg_bank(drvdata, pin - drvdata->pin_base, ®_base, |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 430 | &pin_offset, &bank); |
Tomasz Figa | 499147c | 2013-03-18 22:31:52 +0100 | [diff] [blame] | 431 | type = bank->type; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 432 | |
Tomasz Figa | 499147c | 2013-03-18 22:31:52 +0100 | [diff] [blame] | 433 | if (cfg_type >= PINCFG_TYPE_NUM || !type->fld_width[cfg_type]) |
Tomasz Figa | 7c367d3 | 2012-10-11 10:11:07 +0200 | [diff] [blame] | 434 | return -EINVAL; |
| 435 | |
Tomasz Figa | 499147c | 2013-03-18 22:31:52 +0100 | [diff] [blame] | 436 | width = type->fld_width[cfg_type]; |
Tomasz Figa | 43fc9e7 | 2013-03-18 22:31:53 +0100 | [diff] [blame] | 437 | cfg_reg = type->reg_offset[cfg_type]; |
Tomasz Figa | 499147c | 2013-03-18 22:31:52 +0100 | [diff] [blame] | 438 | |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 439 | spin_lock_irqsave(&bank->slock, flags); |
| 440 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 441 | mask = (1 << width) - 1; |
| 442 | shift = pin_offset * width; |
| 443 | data = readl(reg_base + cfg_reg); |
| 444 | |
| 445 | if (set) { |
| 446 | cfg_value = PINCFG_UNPACK_VALUE(*config); |
| 447 | data &= ~(mask << shift); |
| 448 | data |= (cfg_value << shift); |
| 449 | writel(data, reg_base + cfg_reg); |
| 450 | } else { |
| 451 | data >>= shift; |
| 452 | data &= mask; |
| 453 | *config = PINCFG_PACK(cfg_type, data); |
| 454 | } |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 455 | |
| 456 | spin_unlock_irqrestore(&bank->slock, flags); |
| 457 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | /* set the pin config settings for a specified pin */ |
| 462 | static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 463 | unsigned long *configs, unsigned num_configs) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 464 | { |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 465 | int i, ret; |
| 466 | |
| 467 | for (i = 0; i < num_configs; i++) { |
| 468 | ret = samsung_pinconf_rw(pctldev, pin, &configs[i], true); |
| 469 | if (ret < 0) |
| 470 | return ret; |
| 471 | } /* for each config */ |
| 472 | |
| 473 | return 0; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* get the pin config settings for a specified pin */ |
| 477 | static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, |
| 478 | unsigned long *config) |
| 479 | { |
| 480 | return samsung_pinconf_rw(pctldev, pin, config, false); |
| 481 | } |
| 482 | |
| 483 | /* set the pin config settings for a specified pin group */ |
| 484 | static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev, |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 485 | unsigned group, unsigned long *configs, |
| 486 | unsigned num_configs) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 487 | { |
| 488 | struct samsung_pinctrl_drv_data *drvdata; |
| 489 | const unsigned int *pins; |
| 490 | unsigned int cnt; |
| 491 | |
| 492 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 493 | pins = drvdata->pin_groups[group].pins; |
| 494 | |
| 495 | for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 496 | samsung_pinconf_set(pctldev, pins[cnt], configs, num_configs); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | /* get the pin config settings for a specified pin group */ |
| 502 | static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev, |
| 503 | unsigned int group, unsigned long *config) |
| 504 | { |
| 505 | struct samsung_pinctrl_drv_data *drvdata; |
| 506 | const unsigned int *pins; |
| 507 | |
| 508 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 509 | pins = drvdata->pin_groups[group].pins; |
| 510 | samsung_pinconf_get(pctldev, pins[0], config); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */ |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 515 | static const struct pinconf_ops samsung_pinconf_ops = { |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 516 | .pin_config_get = samsung_pinconf_get, |
| 517 | .pin_config_set = samsung_pinconf_set, |
| 518 | .pin_config_group_get = samsung_pinconf_group_get, |
| 519 | .pin_config_group_set = samsung_pinconf_group_set, |
| 520 | }; |
| 521 | |
| 522 | /* gpiolib gpio_set callback function */ |
| 523 | static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value) |
| 524 | { |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 525 | struct samsung_pin_bank *bank = gc_to_pin_bank(gc); |
Tomasz Figa | 94ce944 | 2014-09-23 21:05:39 +0200 | [diff] [blame] | 526 | const struct samsung_pin_bank_type *type = bank->type; |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 527 | unsigned long flags; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 528 | void __iomem *reg; |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 529 | u32 data; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 530 | |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 531 | reg = bank->drvdata->virt_base + bank->pctl_offset; |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame] | 532 | |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 533 | spin_lock_irqsave(&bank->slock, flags); |
| 534 | |
Tomasz Figa | 43fc9e7 | 2013-03-18 22:31:53 +0100 | [diff] [blame] | 535 | data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]); |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 536 | data &= ~(1 << offset); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 537 | if (value) |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 538 | data |= 1 << offset; |
Tomasz Figa | 43fc9e7 | 2013-03-18 22:31:53 +0100 | [diff] [blame] | 539 | writel(data, reg + type->reg_offset[PINCFG_TYPE_DAT]); |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 540 | |
| 541 | spin_unlock_irqrestore(&bank->slock, flags); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* gpiolib gpio_get callback function */ |
| 545 | static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 546 | { |
| 547 | void __iomem *reg; |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 548 | u32 data; |
| 549 | struct samsung_pin_bank *bank = gc_to_pin_bank(gc); |
Tomasz Figa | 94ce944 | 2014-09-23 21:05:39 +0200 | [diff] [blame] | 550 | const struct samsung_pin_bank_type *type = bank->type; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 551 | |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 552 | reg = bank->drvdata->virt_base + bank->pctl_offset; |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame] | 553 | |
Tomasz Figa | 43fc9e7 | 2013-03-18 22:31:53 +0100 | [diff] [blame] | 554 | data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]); |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 555 | data >>= offset; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 556 | data &= 1; |
| 557 | return data; |
| 558 | } |
| 559 | |
| 560 | /* |
Tomasz Figa | 18c28ca | 2014-07-02 17:40:59 +0200 | [diff] [blame] | 561 | * The calls to gpio_direction_output() and gpio_direction_input() |
| 562 | * leads to this function call. |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 563 | */ |
Tomasz Figa | 18c28ca | 2014-07-02 17:40:59 +0200 | [diff] [blame] | 564 | static int samsung_gpio_set_direction(struct gpio_chip *gc, |
| 565 | unsigned offset, bool input) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 566 | { |
Tomasz Figa | 94ce944 | 2014-09-23 21:05:39 +0200 | [diff] [blame] | 567 | const struct samsung_pin_bank_type *type; |
Tomasz Figa | 18c28ca | 2014-07-02 17:40:59 +0200 | [diff] [blame] | 568 | struct samsung_pin_bank *bank; |
| 569 | struct samsung_pinctrl_drv_data *drvdata; |
| 570 | void __iomem *reg; |
| 571 | u32 data, mask, shift; |
| 572 | unsigned long flags; |
| 573 | |
| 574 | bank = gc_to_pin_bank(gc); |
| 575 | type = bank->type; |
| 576 | drvdata = bank->drvdata; |
| 577 | |
| 578 | reg = drvdata->virt_base + bank->pctl_offset + |
| 579 | type->reg_offset[PINCFG_TYPE_FUNC]; |
| 580 | |
| 581 | mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1; |
| 582 | shift = offset * type->fld_width[PINCFG_TYPE_FUNC]; |
| 583 | if (shift >= 32) { |
| 584 | /* Some banks have two config registers */ |
| 585 | shift -= 32; |
| 586 | reg += 4; |
| 587 | } |
| 588 | |
| 589 | spin_lock_irqsave(&bank->slock, flags); |
| 590 | |
| 591 | data = readl(reg); |
| 592 | data &= ~(mask << shift); |
| 593 | if (!input) |
| 594 | data |= FUNC_OUTPUT << shift; |
| 595 | writel(data, reg); |
| 596 | |
| 597 | spin_unlock_irqrestore(&bank->slock, flags); |
| 598 | |
| 599 | return 0; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 600 | } |
| 601 | |
Tomasz Figa | 18c28ca | 2014-07-02 17:40:59 +0200 | [diff] [blame] | 602 | /* gpiolib gpio_direction_input callback function. */ |
| 603 | static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 604 | { |
| 605 | return samsung_gpio_set_direction(gc, offset, true); |
| 606 | } |
| 607 | |
| 608 | /* gpiolib gpio_direction_output callback function. */ |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 609 | static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset, |
| 610 | int value) |
| 611 | { |
| 612 | samsung_gpio_set(gc, offset, value); |
Tomasz Figa | 18c28ca | 2014-07-02 17:40:59 +0200 | [diff] [blame] | 613 | return samsung_gpio_set_direction(gc, offset, false); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* |
Tomasz Figa | a19fe2d | 2012-10-11 10:11:20 +0200 | [diff] [blame] | 617 | * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin |
| 618 | * and a virtual IRQ, if not already present. |
| 619 | */ |
| 620 | static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 621 | { |
| 622 | struct samsung_pin_bank *bank = gc_to_pin_bank(gc); |
| 623 | unsigned int virq; |
| 624 | |
| 625 | if (!bank->irq_domain) |
| 626 | return -ENXIO; |
| 627 | |
| 628 | virq = irq_create_mapping(bank->irq_domain, offset); |
| 629 | |
| 630 | return (virq) ? : -ENXIO; |
| 631 | } |
| 632 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 633 | static struct samsung_pin_group *samsung_pinctrl_create_groups( |
| 634 | struct device *dev, |
| 635 | struct samsung_pinctrl_drv_data *drvdata, |
| 636 | unsigned int *cnt) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 637 | { |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 638 | struct pinctrl_desc *ctrldesc = &drvdata->pctl; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 639 | struct samsung_pin_group *groups, *grp; |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 640 | const struct pinctrl_pin_desc *pdesc; |
| 641 | int i; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 642 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 643 | groups = devm_kzalloc(dev, ctrldesc->npins * sizeof(*groups), |
| 644 | GFP_KERNEL); |
| 645 | if (!groups) |
| 646 | return ERR_PTR(-EINVAL); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 647 | grp = groups; |
| 648 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 649 | pdesc = ctrldesc->pins; |
| 650 | for (i = 0; i < ctrldesc->npins; ++i, ++pdesc, ++grp) { |
| 651 | grp->name = pdesc->name; |
| 652 | grp->pins = &pdesc->number; |
| 653 | grp->num_pins = 1; |
| 654 | } |
| 655 | |
| 656 | *cnt = ctrldesc->npins; |
| 657 | return groups; |
| 658 | } |
| 659 | |
| 660 | static int samsung_pinctrl_create_function(struct device *dev, |
| 661 | struct samsung_pinctrl_drv_data *drvdata, |
| 662 | struct device_node *func_np, |
| 663 | struct samsung_pmx_func *func) |
| 664 | { |
| 665 | int npins; |
| 666 | int ret; |
| 667 | int i; |
| 668 | |
| 669 | if (of_property_read_u32(func_np, "samsung,pin-function", &func->val)) |
| 670 | return 0; |
| 671 | |
| 672 | npins = of_property_count_strings(func_np, "samsung,pins"); |
| 673 | if (npins < 1) { |
| 674 | dev_err(dev, "invalid pin list in %s node", func_np->name); |
| 675 | return -EINVAL; |
| 676 | } |
| 677 | |
| 678 | func->name = func_np->full_name; |
| 679 | |
| 680 | func->groups = devm_kzalloc(dev, npins * sizeof(char *), GFP_KERNEL); |
| 681 | if (!func->groups) |
| 682 | return -ENOMEM; |
| 683 | |
| 684 | for (i = 0; i < npins; ++i) { |
| 685 | const char *gname; |
| 686 | |
| 687 | ret = of_property_read_string_index(func_np, "samsung,pins", |
| 688 | i, &gname); |
| 689 | if (ret) { |
| 690 | dev_err(dev, |
| 691 | "failed to read pin name %d from %s node\n", |
| 692 | i, func_np->name); |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | func->groups[i] = gname; |
| 697 | } |
| 698 | |
| 699 | func->num_groups = npins; |
| 700 | return 1; |
| 701 | } |
| 702 | |
| 703 | static struct samsung_pmx_func *samsung_pinctrl_create_functions( |
| 704 | struct device *dev, |
| 705 | struct samsung_pinctrl_drv_data *drvdata, |
| 706 | unsigned int *cnt) |
| 707 | { |
| 708 | struct samsung_pmx_func *functions, *func; |
| 709 | struct device_node *dev_np = dev->of_node; |
| 710 | struct device_node *cfg_np; |
| 711 | unsigned int func_cnt = 0; |
| 712 | int ret; |
| 713 | |
| 714 | /* |
| 715 | * Iterate over all the child nodes of the pin controller node |
| 716 | * and create pin groups and pin function lists. |
| 717 | */ |
| 718 | for_each_child_of_node(dev_np, cfg_np) { |
| 719 | struct device_node *func_np; |
| 720 | |
| 721 | if (!of_get_child_count(cfg_np)) { |
| 722 | if (!of_find_property(cfg_np, |
| 723 | "samsung,pin-function", NULL)) |
| 724 | continue; |
| 725 | ++func_cnt; |
| 726 | continue; |
| 727 | } |
| 728 | |
| 729 | for_each_child_of_node(cfg_np, func_np) { |
| 730 | if (!of_find_property(func_np, |
| 731 | "samsung,pin-function", NULL)) |
| 732 | continue; |
| 733 | ++func_cnt; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | functions = devm_kzalloc(dev, func_cnt * sizeof(*functions), |
| 738 | GFP_KERNEL); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 739 | if (!functions) { |
| 740 | dev_err(dev, "failed to allocate memory for function list\n"); |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 741 | return ERR_PTR(-EINVAL); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 742 | } |
| 743 | func = functions; |
| 744 | |
| 745 | /* |
| 746 | * Iterate over all the child nodes of the pin controller node |
| 747 | * and create pin groups and pin function lists. |
| 748 | */ |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 749 | func_cnt = 0; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 750 | for_each_child_of_node(dev_np, cfg_np) { |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 751 | struct device_node *func_np; |
| 752 | |
| 753 | if (!of_get_child_count(cfg_np)) { |
| 754 | ret = samsung_pinctrl_create_function(dev, drvdata, |
| 755 | cfg_np, func); |
| 756 | if (ret < 0) |
| 757 | return ERR_PTR(ret); |
| 758 | if (ret > 0) { |
| 759 | ++func; |
| 760 | ++func_cnt; |
| 761 | } |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 762 | continue; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 763 | } |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 764 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 765 | for_each_child_of_node(cfg_np, func_np) { |
| 766 | ret = samsung_pinctrl_create_function(dev, drvdata, |
| 767 | func_np, func); |
| 768 | if (ret < 0) |
| 769 | return ERR_PTR(ret); |
| 770 | if (ret > 0) { |
| 771 | ++func; |
| 772 | ++func_cnt; |
| 773 | } |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 774 | } |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 775 | } |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 776 | |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 777 | *cnt = func_cnt; |
| 778 | return functions; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Parse the information about all the available pin groups and pin functions |
| 783 | * from device node of the pin-controller. A pin group is formed with all |
| 784 | * the pins listed in the "samsung,pins" property. |
| 785 | */ |
| 786 | |
| 787 | static int samsung_pinctrl_parse_dt(struct platform_device *pdev, |
| 788 | struct samsung_pinctrl_drv_data *drvdata) |
| 789 | { |
| 790 | struct device *dev = &pdev->dev; |
| 791 | struct samsung_pin_group *groups; |
| 792 | struct samsung_pmx_func *functions; |
| 793 | unsigned int grp_cnt = 0, func_cnt = 0; |
| 794 | |
| 795 | groups = samsung_pinctrl_create_groups(dev, drvdata, &grp_cnt); |
| 796 | if (IS_ERR(groups)) { |
| 797 | dev_err(dev, "failed to parse pin groups\n"); |
| 798 | return PTR_ERR(groups); |
| 799 | } |
| 800 | |
| 801 | functions = samsung_pinctrl_create_functions(dev, drvdata, &func_cnt); |
| 802 | if (IS_ERR(functions)) { |
| 803 | dev_err(dev, "failed to parse pin functions\n"); |
Fabio Estevam | 44a074f | 2015-06-01 09:57:04 -0300 | [diff] [blame] | 804 | return PTR_ERR(functions); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | drvdata->pin_groups = groups; |
| 808 | drvdata->nr_groups = grp_cnt; |
| 809 | drvdata->pmx_functions = functions; |
Tomasz Figa | 9a2c1c3 | 2014-07-02 17:41:03 +0200 | [diff] [blame] | 810 | drvdata->nr_functions = func_cnt; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | /* register the pinctrl interface with the pinctrl subsystem */ |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 816 | static int samsung_pinctrl_register(struct platform_device *pdev, |
| 817 | struct samsung_pinctrl_drv_data *drvdata) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 818 | { |
| 819 | struct pinctrl_desc *ctrldesc = &drvdata->pctl; |
| 820 | struct pinctrl_pin_desc *pindesc, *pdesc; |
| 821 | struct samsung_pin_bank *pin_bank; |
| 822 | char *pin_names; |
| 823 | int pin, bank, ret; |
| 824 | |
| 825 | ctrldesc->name = "samsung-pinctrl"; |
| 826 | ctrldesc->owner = THIS_MODULE; |
| 827 | ctrldesc->pctlops = &samsung_pctrl_ops; |
| 828 | ctrldesc->pmxops = &samsung_pinmux_ops; |
| 829 | ctrldesc->confops = &samsung_pinconf_ops; |
| 830 | |
| 831 | pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) * |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 832 | drvdata->nr_pins, GFP_KERNEL); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 833 | if (!pindesc) { |
| 834 | dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n"); |
| 835 | return -ENOMEM; |
| 836 | } |
| 837 | ctrldesc->pins = pindesc; |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 838 | ctrldesc->npins = drvdata->nr_pins; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 839 | |
| 840 | /* dynamically populate the pin number and pin name for pindesc */ |
| 841 | for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++) |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 842 | pdesc->number = pin + drvdata->pin_base; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 843 | |
| 844 | /* |
| 845 | * allocate space for storing the dynamically generated names for all |
| 846 | * the pins which belong to this pin-controller. |
| 847 | */ |
| 848 | pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH * |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 849 | drvdata->nr_pins, GFP_KERNEL); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 850 | if (!pin_names) { |
| 851 | dev_err(&pdev->dev, "mem alloc for pin names failed\n"); |
| 852 | return -ENOMEM; |
| 853 | } |
| 854 | |
| 855 | /* for each pin, the name of the pin is pin-bank name + pin number */ |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 856 | for (bank = 0; bank < drvdata->nr_banks; bank++) { |
| 857 | pin_bank = &drvdata->pin_banks[bank]; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 858 | for (pin = 0; pin < pin_bank->nr_pins; pin++) { |
| 859 | sprintf(pin_names, "%s-%d", pin_bank->name, pin); |
| 860 | pdesc = pindesc + pin_bank->pin_base + pin; |
| 861 | pdesc->name = pin_names; |
| 862 | pin_names += PIN_NAME_LENGTH; |
| 863 | } |
| 864 | } |
| 865 | |
Tomasz Figa | 529301c | 2013-08-20 18:16:21 +0200 | [diff] [blame] | 866 | ret = samsung_pinctrl_parse_dt(pdev, drvdata); |
| 867 | if (ret) |
| 868 | return ret; |
| 869 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 870 | drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 871 | if (IS_ERR(drvdata->pctl_dev)) { |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 872 | dev_err(&pdev->dev, "could not register pinctrl driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 873 | return PTR_ERR(drvdata->pctl_dev); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 874 | } |
| 875 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 876 | for (bank = 0; bank < drvdata->nr_banks; ++bank) { |
| 877 | pin_bank = &drvdata->pin_banks[bank]; |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 878 | pin_bank->grange.name = pin_bank->name; |
| 879 | pin_bank->grange.id = bank; |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 880 | pin_bank->grange.pin_base = drvdata->pin_base |
Tomasz Figa | 6c6ce62 | 2014-07-02 17:41:00 +0200 | [diff] [blame] | 881 | + pin_bank->pin_base; |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 882 | pin_bank->grange.base = pin_bank->gpio_chip.base; |
| 883 | pin_bank->grange.npins = pin_bank->gpio_chip.ngpio; |
| 884 | pin_bank->grange.gc = &pin_bank->gpio_chip; |
| 885 | pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange); |
| 886 | } |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 887 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 888 | return 0; |
| 889 | } |
| 890 | |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 891 | static const struct gpio_chip samsung_gpiolib_chip = { |
Jonas Gorski | 98c85d5 | 2015-10-11 17:34:19 +0200 | [diff] [blame] | 892 | .request = gpiochip_generic_request, |
| 893 | .free = gpiochip_generic_free, |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 894 | .set = samsung_gpio_set, |
| 895 | .get = samsung_gpio_get, |
| 896 | .direction_input = samsung_gpio_direction_input, |
| 897 | .direction_output = samsung_gpio_direction_output, |
Tomasz Figa | a19fe2d | 2012-10-11 10:11:20 +0200 | [diff] [blame] | 898 | .to_irq = samsung_gpio_to_irq, |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 899 | .owner = THIS_MODULE, |
| 900 | }; |
| 901 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 902 | /* register the gpiolib interface with the gpiolib subsystem */ |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 903 | static int samsung_gpiolib_register(struct platform_device *pdev, |
| 904 | struct samsung_pinctrl_drv_data *drvdata) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 905 | { |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 906 | struct samsung_pin_bank *bank = drvdata->pin_banks; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 907 | struct gpio_chip *gc; |
| 908 | int ret; |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 909 | int i; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 910 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 911 | for (i = 0; i < drvdata->nr_banks; ++i, ++bank) { |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 912 | bank->gpio_chip = samsung_gpiolib_chip; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 913 | |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 914 | gc = &bank->gpio_chip; |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 915 | gc->base = drvdata->pin_base + bank->pin_base; |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 916 | gc->ngpio = bank->nr_pins; |
| 917 | gc->dev = &pdev->dev; |
| 918 | gc->of_node = bank->of_node; |
| 919 | gc->label = bank->name; |
| 920 | |
| 921 | ret = gpiochip_add(gc); |
| 922 | if (ret) { |
| 923 | dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n", |
| 924 | gc->label, ret); |
| 925 | goto fail; |
| 926 | } |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | return 0; |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 930 | |
| 931 | fail: |
| 932 | for (--i, --bank; i >= 0; --i, --bank) |
Linus Walleij | 2fcea6c | 2014-09-16 15:05:41 -0700 | [diff] [blame] | 933 | gpiochip_remove(&bank->gpio_chip); |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 934 | return ret; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /* unregister the gpiolib interface with the gpiolib subsystem */ |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 938 | static int samsung_gpiolib_unregister(struct platform_device *pdev, |
| 939 | struct samsung_pinctrl_drv_data *drvdata) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 940 | { |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 941 | struct samsung_pin_bank *bank = drvdata->pin_banks; |
Tomasz Figa | d3a7b9e | 2012-10-11 10:11:17 +0200 | [diff] [blame] | 942 | int i; |
| 943 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 944 | for (i = 0; i < drvdata->nr_banks; ++i, ++bank) |
Linus Walleij | 2fcea6c | 2014-09-16 15:05:41 -0700 | [diff] [blame] | 945 | gpiochip_remove(&bank->gpio_chip); |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 946 | |
Linus Walleij | 2fcea6c | 2014-09-16 15:05:41 -0700 | [diff] [blame] | 947 | return 0; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | static const struct of_device_id samsung_pinctrl_dt_match[]; |
| 951 | |
| 952 | /* retrieve the soc specific data */ |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 953 | static const struct samsung_pin_ctrl * |
| 954 | samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data *d, |
| 955 | struct platform_device *pdev) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 956 | { |
| 957 | int id; |
| 958 | const struct of_device_id *match; |
Tomasz Figa | 6defe9a | 2012-10-11 10:11:14 +0200 | [diff] [blame] | 959 | struct device_node *node = pdev->dev.of_node; |
Tomasz Figa | ab66378 | 2012-10-11 10:11:13 +0200 | [diff] [blame] | 960 | struct device_node *np; |
Tomasz Figa | 8100cf4 | 2014-09-23 21:05:41 +0200 | [diff] [blame] | 961 | const struct samsung_pin_bank_data *bdata; |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 962 | const struct samsung_pin_ctrl *ctrl; |
Tomasz Figa | 40ba622 | 2012-10-11 10:11:09 +0200 | [diff] [blame] | 963 | struct samsung_pin_bank *bank; |
| 964 | int i; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 965 | |
Tomasz Figa | 6defe9a | 2012-10-11 10:11:14 +0200 | [diff] [blame] | 966 | id = of_alias_get_id(node, "pinctrl"); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 967 | if (id < 0) { |
| 968 | dev_err(&pdev->dev, "failed to get alias id\n"); |
Tomasz Figa | 8799327 | 2014-09-23 21:05:37 +0200 | [diff] [blame] | 969 | return ERR_PTR(-ENOENT); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 970 | } |
| 971 | match = of_match_node(samsung_pinctrl_dt_match, node); |
Tomasz Figa | 40ba622 | 2012-10-11 10:11:09 +0200 | [diff] [blame] | 972 | ctrl = (struct samsung_pin_ctrl *)match->data + id; |
| 973 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 974 | d->suspend = ctrl->suspend; |
| 975 | d->resume = ctrl->resume; |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 976 | d->nr_banks = ctrl->nr_banks; |
Tomasz Figa | 8100cf4 | 2014-09-23 21:05:41 +0200 | [diff] [blame] | 977 | d->pin_banks = devm_kcalloc(&pdev->dev, d->nr_banks, |
| 978 | sizeof(*d->pin_banks), GFP_KERNEL); |
| 979 | if (!d->pin_banks) |
| 980 | return ERR_PTR(-ENOMEM); |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 981 | |
| 982 | bank = d->pin_banks; |
Tomasz Figa | 8100cf4 | 2014-09-23 21:05:41 +0200 | [diff] [blame] | 983 | bdata = ctrl->pin_banks; |
| 984 | for (i = 0; i < ctrl->nr_banks; ++i, ++bdata, ++bank) { |
| 985 | bank->type = bdata->type; |
| 986 | bank->pctl_offset = bdata->pctl_offset; |
| 987 | bank->nr_pins = bdata->nr_pins; |
| 988 | bank->eint_func = bdata->eint_func; |
| 989 | bank->eint_type = bdata->eint_type; |
| 990 | bank->eint_mask = bdata->eint_mask; |
| 991 | bank->eint_offset = bdata->eint_offset; |
| 992 | bank->name = bdata->name; |
| 993 | |
Tomasz Figa | 1984695 | 2013-03-18 22:31:50 +0100 | [diff] [blame] | 994 | spin_lock_init(&bank->slock); |
Tomasz Figa | 6defe9a | 2012-10-11 10:11:14 +0200 | [diff] [blame] | 995 | bank->drvdata = d; |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 996 | bank->pin_base = d->nr_pins; |
| 997 | d->nr_pins += bank->nr_pins; |
Tomasz Figa | 40ba622 | 2012-10-11 10:11:09 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Tomasz Figa | ab66378 | 2012-10-11 10:11:13 +0200 | [diff] [blame] | 1000 | for_each_child_of_node(node, np) { |
| 1001 | if (!of_find_property(np, "gpio-controller", NULL)) |
| 1002 | continue; |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 1003 | bank = d->pin_banks; |
| 1004 | for (i = 0; i < d->nr_banks; ++i, ++bank) { |
Tomasz Figa | ab66378 | 2012-10-11 10:11:13 +0200 | [diff] [blame] | 1005 | if (!strcmp(bank->name, np->name)) { |
| 1006 | bank->of_node = np; |
| 1007 | break; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 1012 | d->pin_base = pin_base; |
| 1013 | pin_base += d->nr_pins; |
Tomasz Figa | 40ba622 | 2012-10-11 10:11:09 +0200 | [diff] [blame] | 1014 | |
| 1015 | return ctrl; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1016 | } |
| 1017 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 1018 | static int samsung_pinctrl_probe(struct platform_device *pdev) |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1019 | { |
| 1020 | struct samsung_pinctrl_drv_data *drvdata; |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 1021 | const struct samsung_pin_ctrl *ctrl; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1022 | struct device *dev = &pdev->dev; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1023 | struct resource *res; |
| 1024 | int ret; |
| 1025 | |
| 1026 | if (!dev->of_node) { |
| 1027 | dev_err(dev, "device tree node not found\n"); |
| 1028 | return -ENODEV; |
| 1029 | } |
| 1030 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1031 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
| 1032 | if (!drvdata) { |
| 1033 | dev_err(dev, "failed to allocate memory for driver's " |
| 1034 | "private data\n"); |
| 1035 | return -ENOMEM; |
| 1036 | } |
Tomasz Figa | 6defe9a | 2012-10-11 10:11:14 +0200 | [diff] [blame] | 1037 | |
| 1038 | ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev); |
Tomasz Figa | 8799327 | 2014-09-23 21:05:37 +0200 | [diff] [blame] | 1039 | if (IS_ERR(ctrl)) { |
Tomasz Figa | 6defe9a | 2012-10-11 10:11:14 +0200 | [diff] [blame] | 1040 | dev_err(&pdev->dev, "driver data not available\n"); |
Tomasz Figa | 8799327 | 2014-09-23 21:05:37 +0200 | [diff] [blame] | 1041 | return PTR_ERR(ctrl); |
Tomasz Figa | 6defe9a | 2012-10-11 10:11:14 +0200 | [diff] [blame] | 1042 | } |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1043 | drvdata->dev = dev; |
| 1044 | |
| 1045 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 9e0c1fb | 2013-01-21 11:09:14 +0100 | [diff] [blame] | 1046 | drvdata->virt_base = devm_ioremap_resource(&pdev->dev, res); |
| 1047 | if (IS_ERR(drvdata->virt_base)) |
| 1048 | return PTR_ERR(drvdata->virt_base); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1049 | |
| 1050 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 1051 | if (res) |
| 1052 | drvdata->irq = res->start; |
| 1053 | |
| 1054 | ret = samsung_gpiolib_register(pdev, drvdata); |
| 1055 | if (ret) |
| 1056 | return ret; |
| 1057 | |
| 1058 | ret = samsung_pinctrl_register(pdev, drvdata); |
| 1059 | if (ret) { |
| 1060 | samsung_gpiolib_unregister(pdev, drvdata); |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | if (ctrl->eint_gpio_init) |
| 1065 | ctrl->eint_gpio_init(drvdata); |
| 1066 | if (ctrl->eint_wkup_init) |
| 1067 | ctrl->eint_wkup_init(drvdata); |
| 1068 | |
| 1069 | platform_set_drvdata(pdev, drvdata); |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* Add to the global list */ |
| 1072 | list_add_tail(&drvdata->node, &drvdata_list); |
| 1073 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1074 | return 0; |
| 1075 | } |
| 1076 | |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1077 | #ifdef CONFIG_PM |
| 1078 | |
| 1079 | /** |
| 1080 | * samsung_pinctrl_suspend_dev - save pinctrl state for suspend for a device |
| 1081 | * |
| 1082 | * Save data for all banks handled by this device. |
| 1083 | */ |
| 1084 | static void samsung_pinctrl_suspend_dev( |
| 1085 | struct samsung_pinctrl_drv_data *drvdata) |
| 1086 | { |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1087 | void __iomem *virt_base = drvdata->virt_base; |
| 1088 | int i; |
| 1089 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 1090 | for (i = 0; i < drvdata->nr_banks; i++) { |
| 1091 | struct samsung_pin_bank *bank = &drvdata->pin_banks[i]; |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1092 | void __iomem *reg = virt_base + bank->pctl_offset; |
Tomasz Figa | 94ce944 | 2014-09-23 21:05:39 +0200 | [diff] [blame] | 1093 | const u8 *offs = bank->type->reg_offset; |
| 1094 | const u8 *widths = bank->type->fld_width; |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1095 | enum pincfg_type type; |
| 1096 | |
| 1097 | /* Registers without a powerdown config aren't lost */ |
| 1098 | if (!widths[PINCFG_TYPE_CON_PDN]) |
| 1099 | continue; |
| 1100 | |
| 1101 | for (type = 0; type < PINCFG_TYPE_NUM; type++) |
| 1102 | if (widths[type]) |
| 1103 | bank->pm_save[type] = readl(reg + offs[type]); |
| 1104 | |
| 1105 | if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) { |
| 1106 | /* Some banks have two config registers */ |
| 1107 | bank->pm_save[PINCFG_TYPE_NUM] = |
| 1108 | readl(reg + offs[PINCFG_TYPE_FUNC] + 4); |
| 1109 | pr_debug("Save %s @ %p (con %#010x %08x)\n", |
| 1110 | bank->name, reg, |
| 1111 | bank->pm_save[PINCFG_TYPE_FUNC], |
| 1112 | bank->pm_save[PINCFG_TYPE_NUM]); |
| 1113 | } else { |
| 1114 | pr_debug("Save %s @ %p (con %#010x)\n", bank->name, |
| 1115 | reg, bank->pm_save[PINCFG_TYPE_FUNC]); |
| 1116 | } |
| 1117 | } |
Tomasz Figa | 21c2199 | 2013-05-17 18:24:30 +0200 | [diff] [blame] | 1118 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 1119 | if (drvdata->suspend) |
| 1120 | drvdata->suspend(drvdata); |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * samsung_pinctrl_resume_dev - restore pinctrl state from suspend for a device |
| 1125 | * |
| 1126 | * Restore one of the banks that was saved during suspend. |
| 1127 | * |
| 1128 | * We don't bother doing anything complicated to avoid glitching lines since |
| 1129 | * we're called before pad retention is turned off. |
| 1130 | */ |
| 1131 | static void samsung_pinctrl_resume_dev(struct samsung_pinctrl_drv_data *drvdata) |
| 1132 | { |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1133 | void __iomem *virt_base = drvdata->virt_base; |
| 1134 | int i; |
| 1135 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 1136 | if (drvdata->resume) |
| 1137 | drvdata->resume(drvdata); |
Tomasz Figa | 21c2199 | 2013-05-17 18:24:30 +0200 | [diff] [blame] | 1138 | |
Tomasz Figa | 1bf00d7 | 2014-09-23 21:05:40 +0200 | [diff] [blame] | 1139 | for (i = 0; i < drvdata->nr_banks; i++) { |
| 1140 | struct samsung_pin_bank *bank = &drvdata->pin_banks[i]; |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1141 | void __iomem *reg = virt_base + bank->pctl_offset; |
Tomasz Figa | 94ce944 | 2014-09-23 21:05:39 +0200 | [diff] [blame] | 1142 | const u8 *offs = bank->type->reg_offset; |
| 1143 | const u8 *widths = bank->type->fld_width; |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1144 | enum pincfg_type type; |
| 1145 | |
| 1146 | /* Registers without a powerdown config aren't lost */ |
| 1147 | if (!widths[PINCFG_TYPE_CON_PDN]) |
| 1148 | continue; |
| 1149 | |
| 1150 | if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) { |
| 1151 | /* Some banks have two config registers */ |
| 1152 | pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n", |
| 1153 | bank->name, reg, |
| 1154 | readl(reg + offs[PINCFG_TYPE_FUNC]), |
| 1155 | readl(reg + offs[PINCFG_TYPE_FUNC] + 4), |
| 1156 | bank->pm_save[PINCFG_TYPE_FUNC], |
| 1157 | bank->pm_save[PINCFG_TYPE_NUM]); |
| 1158 | writel(bank->pm_save[PINCFG_TYPE_NUM], |
| 1159 | reg + offs[PINCFG_TYPE_FUNC] + 4); |
| 1160 | } else { |
| 1161 | pr_debug("%s @ %p (con %#010x => %#010x)\n", bank->name, |
| 1162 | reg, readl(reg + offs[PINCFG_TYPE_FUNC]), |
| 1163 | bank->pm_save[PINCFG_TYPE_FUNC]); |
| 1164 | } |
| 1165 | for (type = 0; type < PINCFG_TYPE_NUM; type++) |
| 1166 | if (widths[type]) |
| 1167 | writel(bank->pm_save[type], reg + offs[type]); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * samsung_pinctrl_suspend - save pinctrl state for suspend |
| 1173 | * |
| 1174 | * Save data for all banks across all devices. |
| 1175 | */ |
| 1176 | static int samsung_pinctrl_suspend(void) |
| 1177 | { |
| 1178 | struct samsung_pinctrl_drv_data *drvdata; |
| 1179 | |
| 1180 | list_for_each_entry(drvdata, &drvdata_list, node) { |
| 1181 | samsung_pinctrl_suspend_dev(drvdata); |
| 1182 | } |
| 1183 | |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | /** |
| 1188 | * samsung_pinctrl_resume - restore pinctrl state for suspend |
| 1189 | * |
| 1190 | * Restore data for all banks across all devices. |
| 1191 | */ |
| 1192 | static void samsung_pinctrl_resume(void) |
| 1193 | { |
| 1194 | struct samsung_pinctrl_drv_data *drvdata; |
| 1195 | |
| 1196 | list_for_each_entry_reverse(drvdata, &drvdata_list, node) { |
| 1197 | samsung_pinctrl_resume_dev(drvdata); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | #else |
| 1202 | #define samsung_pinctrl_suspend NULL |
| 1203 | #define samsung_pinctrl_resume NULL |
| 1204 | #endif |
| 1205 | |
| 1206 | static struct syscore_ops samsung_pinctrl_syscore_ops = { |
| 1207 | .suspend = samsung_pinctrl_suspend, |
| 1208 | .resume = samsung_pinctrl_resume, |
| 1209 | }; |
| 1210 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1211 | static const struct of_device_id samsung_pinctrl_dt_match[] = { |
Tomasz Figa | d5517be | 2013-03-18 22:31:51 +0100 | [diff] [blame] | 1212 | #ifdef CONFIG_PINCTRL_EXYNOS |
Tomasz Figa | d97f5b9 | 2014-04-14 10:45:47 +0900 | [diff] [blame] | 1213 | { .compatible = "samsung,exynos3250-pinctrl", |
| 1214 | .data = (void *)exynos3250_pin_ctrl }, |
Kukjin Kim | b533c86 | 2013-01-02 16:05:42 -0800 | [diff] [blame] | 1215 | { .compatible = "samsung,exynos4210-pinctrl", |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1216 | .data = (void *)exynos4210_pin_ctrl }, |
Kukjin Kim | b533c86 | 2013-01-02 16:05:42 -0800 | [diff] [blame] | 1217 | { .compatible = "samsung,exynos4x12-pinctrl", |
Tomasz Figa | 6edc794 | 2012-11-07 08:44:59 +0900 | [diff] [blame] | 1218 | .data = (void *)exynos4x12_pin_ctrl }, |
Tomasz Figa | 2891ba2 | 2014-10-27 10:21:18 +0900 | [diff] [blame] | 1219 | { .compatible = "samsung,exynos4415-pinctrl", |
| 1220 | .data = (void *)exynos4415_pin_ctrl }, |
Thomas Abraham | f67faf4 | 2012-12-28 10:37:27 -0800 | [diff] [blame] | 1221 | { .compatible = "samsung,exynos5250-pinctrl", |
| 1222 | .data = (void *)exynos5250_pin_ctrl }, |
Young-Gun Jang | 9a8b607 | 2014-02-05 11:51:28 +0530 | [diff] [blame] | 1223 | { .compatible = "samsung,exynos5260-pinctrl", |
| 1224 | .data = (void *)exynos5260_pin_ctrl }, |
Hakjoo Kim | 023e06d | 2015-03-15 23:00:32 +0100 | [diff] [blame] | 1225 | { .compatible = "samsung,exynos5410-pinctrl", |
| 1226 | .data = (void *)exynos5410_pin_ctrl }, |
Leela Krishna Amudala | 983dbeb | 2013-06-19 22:16:26 +0900 | [diff] [blame] | 1227 | { .compatible = "samsung,exynos5420-pinctrl", |
| 1228 | .data = (void *)exynos5420_pin_ctrl }, |
Chanwoo Choi | 3c5ecc9 | 2015-01-21 15:43:11 +0900 | [diff] [blame] | 1229 | { .compatible = "samsung,exynos5433-pinctrl", |
| 1230 | .data = (void *)exynos5433_pin_ctrl }, |
Mateusz Krawczuk | 608a26a | 2013-08-27 15:08:10 +0200 | [diff] [blame] | 1231 | { .compatible = "samsung,s5pv210-pinctrl", |
| 1232 | .data = (void *)s5pv210_pin_ctrl }, |
Naveen Krishna Ch | 50cea0c | 2014-10-09 19:24:32 +0530 | [diff] [blame] | 1233 | { .compatible = "samsung,exynos7-pinctrl", |
| 1234 | .data = (void *)exynos7_pin_ctrl }, |
Tomasz Figa | d5517be | 2013-03-18 22:31:51 +0100 | [diff] [blame] | 1235 | #endif |
Tomasz Figa | 61dd726 | 2013-03-18 22:31:55 +0100 | [diff] [blame] | 1236 | #ifdef CONFIG_PINCTRL_S3C64XX |
| 1237 | { .compatible = "samsung,s3c64xx-pinctrl", |
| 1238 | .data = s3c64xx_pin_ctrl }, |
| 1239 | #endif |
Heiko Stuebner | af99a75 | 2013-05-21 00:56:13 +0900 | [diff] [blame] | 1240 | #ifdef CONFIG_PINCTRL_S3C24XX |
| 1241 | { .compatible = "samsung,s3c2412-pinctrl", |
| 1242 | .data = s3c2412_pin_ctrl }, |
| 1243 | { .compatible = "samsung,s3c2416-pinctrl", |
| 1244 | .data = s3c2416_pin_ctrl }, |
| 1245 | { .compatible = "samsung,s3c2440-pinctrl", |
| 1246 | .data = s3c2440_pin_ctrl }, |
| 1247 | { .compatible = "samsung,s3c2450-pinctrl", |
| 1248 | .data = s3c2450_pin_ctrl }, |
| 1249 | #endif |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1250 | {}, |
| 1251 | }; |
| 1252 | MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match); |
| 1253 | |
| 1254 | static struct platform_driver samsung_pinctrl_driver = { |
| 1255 | .probe = samsung_pinctrl_probe, |
| 1256 | .driver = { |
| 1257 | .name = "samsung-pinctrl", |
Sachin Kamat | 606fca9 | 2013-09-28 17:38:48 +0530 | [diff] [blame] | 1258 | .of_match_table = samsung_pinctrl_dt_match, |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1259 | }, |
| 1260 | }; |
| 1261 | |
| 1262 | static int __init samsung_pinctrl_drv_register(void) |
| 1263 | { |
Doug Anderson | d9f9986 | 2013-05-16 21:33:18 -0700 | [diff] [blame] | 1264 | /* |
| 1265 | * Register syscore ops for save/restore of registers across suspend. |
| 1266 | * It's important to ensure that this driver is running at an earlier |
| 1267 | * initcall level than any arch-specific init calls that install syscore |
| 1268 | * ops that turn off pad retention (like exynos_pm_resume). |
| 1269 | */ |
| 1270 | register_syscore_ops(&samsung_pinctrl_syscore_ops); |
| 1271 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 1272 | return platform_driver_register(&samsung_pinctrl_driver); |
| 1273 | } |
| 1274 | postcore_initcall(samsung_pinctrl_drv_register); |
| 1275 | |
| 1276 | static void __exit samsung_pinctrl_drv_unregister(void) |
| 1277 | { |
| 1278 | platform_driver_unregister(&samsung_pinctrl_driver); |
| 1279 | } |
| 1280 | module_exit(samsung_pinctrl_drv_unregister); |
| 1281 | |
| 1282 | MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>"); |
| 1283 | MODULE_DESCRIPTION("Samsung pinctrl driver"); |
| 1284 | MODULE_LICENSE("GPL v2"); |