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> |
| 29 | |
| 30 | #include "core.h" |
| 31 | #include "pinctrl-samsung.h" |
| 32 | |
| 33 | #define GROUP_SUFFIX "-grp" |
| 34 | #define GSUFFIX_LEN sizeof(GROUP_SUFFIX) |
| 35 | #define FUNCTION_SUFFIX "-mux" |
| 36 | #define FSUFFIX_LEN sizeof(FUNCTION_SUFFIX) |
| 37 | |
| 38 | /* list of all possible config options supported */ |
| 39 | struct pin_config { |
| 40 | char *prop_cfg; |
| 41 | unsigned int cfg_type; |
| 42 | } pcfgs[] = { |
| 43 | { "samsung,pin-pud", PINCFG_TYPE_PUD }, |
| 44 | { "samsung,pin-drv", PINCFG_TYPE_DRV }, |
| 45 | { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN }, |
| 46 | { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN }, |
| 47 | }; |
| 48 | |
| 49 | /* check if the selector is a valid pin group selector */ |
| 50 | static int samsung_get_group_count(struct pinctrl_dev *pctldev) |
| 51 | { |
| 52 | struct samsung_pinctrl_drv_data *drvdata; |
| 53 | |
| 54 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 55 | return drvdata->nr_groups; |
| 56 | } |
| 57 | |
| 58 | /* return the name of the group selected by the group selector */ |
| 59 | static const char *samsung_get_group_name(struct pinctrl_dev *pctldev, |
| 60 | unsigned selector) |
| 61 | { |
| 62 | struct samsung_pinctrl_drv_data *drvdata; |
| 63 | |
| 64 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 65 | return drvdata->pin_groups[selector].name; |
| 66 | } |
| 67 | |
| 68 | /* return the pin numbers associated with the specified group */ |
| 69 | static int samsung_get_group_pins(struct pinctrl_dev *pctldev, |
| 70 | unsigned selector, const unsigned **pins, unsigned *num_pins) |
| 71 | { |
| 72 | struct samsung_pinctrl_drv_data *drvdata; |
| 73 | |
| 74 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 75 | *pins = drvdata->pin_groups[selector].pins; |
| 76 | *num_pins = drvdata->pin_groups[selector].num_pins; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* create pinctrl_map entries by parsing device tree nodes */ |
| 81 | static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 82 | struct device_node *np, struct pinctrl_map **maps, |
| 83 | unsigned *nmaps) |
| 84 | { |
| 85 | struct device *dev = pctldev->dev; |
| 86 | struct pinctrl_map *map; |
| 87 | unsigned long *cfg = NULL; |
| 88 | char *gname, *fname; |
| 89 | int cfg_cnt = 0, map_cnt = 0, idx = 0; |
| 90 | |
| 91 | /* count the number of config options specfied in the node */ |
| 92 | for (idx = 0; idx < ARRAY_SIZE(pcfgs); idx++) { |
| 93 | if (of_find_property(np, pcfgs[idx].prop_cfg, NULL)) |
| 94 | cfg_cnt++; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Find out the number of map entries to create. All the config options |
| 99 | * can be accomadated into a single config map entry. |
| 100 | */ |
| 101 | if (cfg_cnt) |
| 102 | map_cnt = 1; |
| 103 | if (of_find_property(np, "samsung,pin-function", NULL)) |
| 104 | map_cnt++; |
| 105 | if (!map_cnt) { |
| 106 | dev_err(dev, "node %s does not have either config or function " |
| 107 | "configurations\n", np->name); |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | |
| 111 | /* Allocate memory for pin-map entries */ |
| 112 | map = kzalloc(sizeof(*map) * map_cnt, GFP_KERNEL); |
| 113 | if (!map) { |
| 114 | dev_err(dev, "could not alloc memory for pin-maps\n"); |
| 115 | return -ENOMEM; |
| 116 | } |
| 117 | *nmaps = 0; |
| 118 | |
| 119 | /* |
| 120 | * Allocate memory for pin group name. The pin group name is derived |
| 121 | * from the node name from which these map entries are be created. |
| 122 | */ |
| 123 | gname = kzalloc(strlen(np->name) + GSUFFIX_LEN, GFP_KERNEL); |
| 124 | if (!gname) { |
| 125 | dev_err(dev, "failed to alloc memory for group name\n"); |
| 126 | goto free_map; |
| 127 | } |
| 128 | sprintf(gname, "%s%s", np->name, GROUP_SUFFIX); |
| 129 | |
| 130 | /* |
| 131 | * don't have config options? then skip over to creating function |
| 132 | * map entries. |
| 133 | */ |
| 134 | if (!cfg_cnt) |
| 135 | goto skip_cfgs; |
| 136 | |
| 137 | /* Allocate memory for config entries */ |
| 138 | cfg = kzalloc(sizeof(*cfg) * cfg_cnt, GFP_KERNEL); |
| 139 | if (!cfg) { |
| 140 | dev_err(dev, "failed to alloc memory for configs\n"); |
| 141 | goto free_gname; |
| 142 | } |
| 143 | |
| 144 | /* Prepare a list of config settings */ |
| 145 | for (idx = 0, cfg_cnt = 0; idx < ARRAY_SIZE(pcfgs); idx++) { |
| 146 | u32 value; |
| 147 | if (!of_property_read_u32(np, pcfgs[idx].prop_cfg, &value)) |
| 148 | cfg[cfg_cnt++] = |
| 149 | PINCFG_PACK(pcfgs[idx].cfg_type, value); |
| 150 | } |
| 151 | |
| 152 | /* create the config map entry */ |
| 153 | map[*nmaps].data.configs.group_or_pin = gname; |
| 154 | map[*nmaps].data.configs.configs = cfg; |
| 155 | map[*nmaps].data.configs.num_configs = cfg_cnt; |
| 156 | map[*nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP; |
| 157 | *nmaps += 1; |
| 158 | |
| 159 | skip_cfgs: |
| 160 | /* create the function map entry */ |
| 161 | if (of_find_property(np, "samsung,pin-function", NULL)) { |
| 162 | fname = kzalloc(strlen(np->name) + FSUFFIX_LEN, GFP_KERNEL); |
| 163 | if (!fname) { |
| 164 | dev_err(dev, "failed to alloc memory for func name\n"); |
| 165 | goto free_cfg; |
| 166 | } |
| 167 | sprintf(fname, "%s%s", np->name, FUNCTION_SUFFIX); |
| 168 | |
| 169 | map[*nmaps].data.mux.group = gname; |
| 170 | map[*nmaps].data.mux.function = fname; |
| 171 | map[*nmaps].type = PIN_MAP_TYPE_MUX_GROUP; |
| 172 | *nmaps += 1; |
| 173 | } |
| 174 | |
| 175 | *maps = map; |
| 176 | return 0; |
| 177 | |
| 178 | free_cfg: |
| 179 | kfree(cfg); |
| 180 | free_gname: |
| 181 | kfree(gname); |
| 182 | free_map: |
| 183 | kfree(map); |
| 184 | return -ENOMEM; |
| 185 | } |
| 186 | |
| 187 | /* free the memory allocated to hold the pin-map table */ |
| 188 | static void samsung_dt_free_map(struct pinctrl_dev *pctldev, |
| 189 | struct pinctrl_map *map, unsigned num_maps) |
| 190 | { |
| 191 | int idx; |
| 192 | |
| 193 | for (idx = 0; idx < num_maps; idx++) { |
| 194 | if (map[idx].type == PIN_MAP_TYPE_MUX_GROUP) { |
| 195 | kfree(map[idx].data.mux.function); |
| 196 | if (!idx) |
| 197 | kfree(map[idx].data.mux.group); |
| 198 | } else if (map->type == PIN_MAP_TYPE_CONFIGS_GROUP) { |
| 199 | kfree(map[idx].data.configs.configs); |
| 200 | if (!idx) |
| 201 | kfree(map[idx].data.configs.group_or_pin); |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | kfree(map); |
| 206 | } |
| 207 | |
| 208 | /* list of pinctrl callbacks for the pinctrl core */ |
| 209 | static struct pinctrl_ops samsung_pctrl_ops = { |
| 210 | .get_groups_count = samsung_get_group_count, |
| 211 | .get_group_name = samsung_get_group_name, |
| 212 | .get_group_pins = samsung_get_group_pins, |
| 213 | .dt_node_to_map = samsung_dt_node_to_map, |
| 214 | .dt_free_map = samsung_dt_free_map, |
| 215 | }; |
| 216 | |
| 217 | /* check if the selector is a valid pin function selector */ |
| 218 | static int samsung_get_functions_count(struct pinctrl_dev *pctldev) |
| 219 | { |
| 220 | struct samsung_pinctrl_drv_data *drvdata; |
| 221 | |
| 222 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 223 | return drvdata->nr_functions; |
| 224 | } |
| 225 | |
| 226 | /* return the name of the pin function specified */ |
| 227 | static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev, |
| 228 | unsigned selector) |
| 229 | { |
| 230 | struct samsung_pinctrl_drv_data *drvdata; |
| 231 | |
| 232 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 233 | return drvdata->pmx_functions[selector].name; |
| 234 | } |
| 235 | |
| 236 | /* return the groups associated for the specified function selector */ |
| 237 | static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev, |
| 238 | unsigned selector, const char * const **groups, |
| 239 | unsigned * const num_groups) |
| 240 | { |
| 241 | struct samsung_pinctrl_drv_data *drvdata; |
| 242 | |
| 243 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 244 | *groups = drvdata->pmx_functions[selector].groups; |
| 245 | *num_groups = drvdata->pmx_functions[selector].num_groups; |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * given a pin number that is local to a pin controller, find out the pin bank |
| 251 | * and the register base of the pin bank. |
| 252 | */ |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 253 | static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata, |
| 254 | unsigned pin, void __iomem **reg, u32 *offset, |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 255 | struct samsung_pin_bank **bank) |
| 256 | { |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 257 | struct samsung_pin_bank *b; |
| 258 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 259 | b = drvdata->ctrl->pin_banks; |
| 260 | |
| 261 | while ((pin >= b->pin_base) && |
| 262 | ((b->pin_base + b->nr_pins - 1) < pin)) |
| 263 | b++; |
| 264 | |
| 265 | *reg = drvdata->virt_base + b->pctl_offset; |
| 266 | *offset = pin - b->pin_base; |
| 267 | if (bank) |
| 268 | *bank = b; |
| 269 | |
| 270 | /* some banks have two config registers in a single bank */ |
| 271 | if (*offset * b->func_width > BITS_PER_LONG) |
| 272 | *reg += 4; |
| 273 | } |
| 274 | |
| 275 | /* enable or disable a pinmux function */ |
| 276 | static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector, |
| 277 | unsigned group, bool enable) |
| 278 | { |
| 279 | struct samsung_pinctrl_drv_data *drvdata; |
| 280 | const unsigned int *pins; |
| 281 | struct samsung_pin_bank *bank; |
| 282 | void __iomem *reg; |
| 283 | u32 mask, shift, data, pin_offset, cnt; |
| 284 | |
| 285 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 286 | pins = drvdata->pin_groups[group].pins; |
| 287 | |
| 288 | /* |
| 289 | * for each pin in the pin group selected, program the correspoding pin |
| 290 | * pin function number in the config register. |
| 291 | */ |
| 292 | for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) { |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 293 | pin_to_reg_bank(drvdata, pins[cnt] - drvdata->ctrl->base, |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 294 | ®, &pin_offset, &bank); |
| 295 | mask = (1 << bank->func_width) - 1; |
| 296 | shift = pin_offset * bank->func_width; |
| 297 | |
| 298 | data = readl(reg); |
| 299 | data &= ~(mask << shift); |
| 300 | if (enable) |
| 301 | data |= drvdata->pin_groups[group].func << shift; |
| 302 | writel(data, reg); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* enable a specified pinmux by writing to registers */ |
| 307 | static int samsung_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector, |
| 308 | unsigned group) |
| 309 | { |
| 310 | samsung_pinmux_setup(pctldev, selector, group, true); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* disable a specified pinmux by writing to registers */ |
| 315 | static void samsung_pinmux_disable(struct pinctrl_dev *pctldev, |
| 316 | unsigned selector, unsigned group) |
| 317 | { |
| 318 | samsung_pinmux_setup(pctldev, selector, group, false); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * The calls to gpio_direction_output() and gpio_direction_input() |
| 323 | * leads to this function call (via the pinctrl_gpio_direction_{input|output}() |
| 324 | * function called from the gpiolib interface). |
| 325 | */ |
| 326 | static int samsung_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 327 | struct pinctrl_gpio_range *range, unsigned offset, bool input) |
| 328 | { |
| 329 | struct samsung_pin_bank *bank; |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 330 | struct samsung_pinctrl_drv_data *drvdata; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 331 | void __iomem *reg; |
| 332 | u32 data, pin_offset, mask, shift; |
| 333 | |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 334 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 335 | |
| 336 | pin_to_reg_bank(drvdata, offset, ®, &pin_offset, &bank); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 337 | mask = (1 << bank->func_width) - 1; |
| 338 | shift = pin_offset * bank->func_width; |
| 339 | |
| 340 | data = readl(reg); |
| 341 | data &= ~(mask << shift); |
| 342 | if (!input) |
| 343 | data |= FUNC_OUTPUT << shift; |
| 344 | writel(data, reg); |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /* list of pinmux callbacks for the pinmux vertical in pinctrl core */ |
| 349 | static struct pinmux_ops samsung_pinmux_ops = { |
| 350 | .get_functions_count = samsung_get_functions_count, |
| 351 | .get_function_name = samsung_pinmux_get_fname, |
| 352 | .get_function_groups = samsung_pinmux_get_groups, |
| 353 | .enable = samsung_pinmux_enable, |
| 354 | .disable = samsung_pinmux_disable, |
| 355 | .gpio_set_direction = samsung_pinmux_gpio_set_direction, |
| 356 | }; |
| 357 | |
| 358 | /* set or get the pin config settings for a specified pin */ |
| 359 | static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin, |
| 360 | unsigned long *config, bool set) |
| 361 | { |
| 362 | struct samsung_pinctrl_drv_data *drvdata; |
| 363 | struct samsung_pin_bank *bank; |
| 364 | void __iomem *reg_base; |
| 365 | enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config); |
| 366 | u32 data, width, pin_offset, mask, shift; |
| 367 | u32 cfg_value, cfg_reg; |
| 368 | |
| 369 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 370 | pin_to_reg_bank(drvdata, pin - drvdata->ctrl->base, ®_base, |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 371 | &pin_offset, &bank); |
| 372 | |
| 373 | switch (cfg_type) { |
| 374 | case PINCFG_TYPE_PUD: |
| 375 | width = bank->pud_width; |
| 376 | cfg_reg = PUD_REG; |
| 377 | break; |
| 378 | case PINCFG_TYPE_DRV: |
| 379 | width = bank->drv_width; |
| 380 | cfg_reg = DRV_REG; |
| 381 | break; |
| 382 | case PINCFG_TYPE_CON_PDN: |
| 383 | width = bank->conpdn_width; |
| 384 | cfg_reg = CONPDN_REG; |
| 385 | break; |
| 386 | case PINCFG_TYPE_PUD_PDN: |
| 387 | width = bank->pudpdn_width; |
| 388 | cfg_reg = PUDPDN_REG; |
| 389 | break; |
| 390 | default: |
| 391 | WARN_ON(1); |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
Tomasz Figa | 7c367d3 | 2012-10-11 10:11:07 +0200 | [diff] [blame] | 395 | if (!width) |
| 396 | return -EINVAL; |
| 397 | |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 398 | mask = (1 << width) - 1; |
| 399 | shift = pin_offset * width; |
| 400 | data = readl(reg_base + cfg_reg); |
| 401 | |
| 402 | if (set) { |
| 403 | cfg_value = PINCFG_UNPACK_VALUE(*config); |
| 404 | data &= ~(mask << shift); |
| 405 | data |= (cfg_value << shift); |
| 406 | writel(data, reg_base + cfg_reg); |
| 407 | } else { |
| 408 | data >>= shift; |
| 409 | data &= mask; |
| 410 | *config = PINCFG_PACK(cfg_type, data); |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* set the pin config settings for a specified pin */ |
| 416 | static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 417 | unsigned long config) |
| 418 | { |
| 419 | return samsung_pinconf_rw(pctldev, pin, &config, true); |
| 420 | } |
| 421 | |
| 422 | /* get the pin config settings for a specified pin */ |
| 423 | static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, |
| 424 | unsigned long *config) |
| 425 | { |
| 426 | return samsung_pinconf_rw(pctldev, pin, config, false); |
| 427 | } |
| 428 | |
| 429 | /* set the pin config settings for a specified pin group */ |
| 430 | static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev, |
| 431 | unsigned group, unsigned long config) |
| 432 | { |
| 433 | struct samsung_pinctrl_drv_data *drvdata; |
| 434 | const unsigned int *pins; |
| 435 | unsigned int cnt; |
| 436 | |
| 437 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 438 | pins = drvdata->pin_groups[group].pins; |
| 439 | |
| 440 | for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) |
| 441 | samsung_pinconf_set(pctldev, pins[cnt], config); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | /* get the pin config settings for a specified pin group */ |
| 447 | static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev, |
| 448 | unsigned int group, unsigned long *config) |
| 449 | { |
| 450 | struct samsung_pinctrl_drv_data *drvdata; |
| 451 | const unsigned int *pins; |
| 452 | |
| 453 | drvdata = pinctrl_dev_get_drvdata(pctldev); |
| 454 | pins = drvdata->pin_groups[group].pins; |
| 455 | samsung_pinconf_get(pctldev, pins[0], config); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */ |
| 460 | static struct pinconf_ops samsung_pinconf_ops = { |
| 461 | .pin_config_get = samsung_pinconf_get, |
| 462 | .pin_config_set = samsung_pinconf_set, |
| 463 | .pin_config_group_get = samsung_pinconf_group_get, |
| 464 | .pin_config_group_set = samsung_pinconf_group_set, |
| 465 | }; |
| 466 | |
| 467 | /* gpiolib gpio_set callback function */ |
| 468 | static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value) |
| 469 | { |
| 470 | void __iomem *reg; |
| 471 | u32 pin_offset, data; |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 472 | struct samsung_pinctrl_drv_data *drvdata; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 473 | |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 474 | drvdata = dev_get_drvdata(gc->dev); |
| 475 | |
| 476 | pin_to_reg_bank(drvdata, offset, ®, &pin_offset, NULL); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 477 | data = readl(reg + DAT_REG); |
| 478 | data &= ~(1 << pin_offset); |
| 479 | if (value) |
| 480 | data |= 1 << pin_offset; |
| 481 | writel(data, reg + DAT_REG); |
| 482 | } |
| 483 | |
| 484 | /* gpiolib gpio_get callback function */ |
| 485 | static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 486 | { |
| 487 | void __iomem *reg; |
| 488 | u32 pin_offset, data; |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 489 | struct samsung_pinctrl_drv_data *drvdata; |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 490 | |
Tomasz Figa | 62f14c0 | 2012-10-11 10:11:08 +0200 | [diff] [blame^] | 491 | drvdata = dev_get_drvdata(gc->dev); |
| 492 | |
| 493 | pin_to_reg_bank(drvdata, offset, ®, &pin_offset, NULL); |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 494 | data = readl(reg + DAT_REG); |
| 495 | data >>= pin_offset; |
| 496 | data &= 1; |
| 497 | return data; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * gpiolib gpio_direction_input callback function. The setting of the pin |
| 502 | * mux function as 'gpio input' will be handled by the pinctrl susbsystem |
| 503 | * interface. |
| 504 | */ |
| 505 | static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 506 | { |
| 507 | return pinctrl_gpio_direction_input(gc->base + offset); |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * gpiolib gpio_direction_output callback function. The setting of the pin |
| 512 | * mux function as 'gpio output' will be handled by the pinctrl susbsystem |
| 513 | * interface. |
| 514 | */ |
| 515 | static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset, |
| 516 | int value) |
| 517 | { |
| 518 | samsung_gpio_set(gc, offset, value); |
| 519 | return pinctrl_gpio_direction_output(gc->base + offset); |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Parse the pin names listed in the 'samsung,pins' property and convert it |
| 524 | * into a list of gpio numbers are create a pin group from it. |
| 525 | */ |
| 526 | static int __init samsung_pinctrl_parse_dt_pins(struct platform_device *pdev, |
| 527 | struct device_node *cfg_np, struct pinctrl_desc *pctl, |
| 528 | unsigned int **pin_list, unsigned int *npins) |
| 529 | { |
| 530 | struct device *dev = &pdev->dev; |
| 531 | struct property *prop; |
| 532 | struct pinctrl_pin_desc const *pdesc = pctl->pins; |
| 533 | unsigned int idx = 0, cnt; |
| 534 | const char *pin_name; |
| 535 | |
| 536 | *npins = of_property_count_strings(cfg_np, "samsung,pins"); |
| 537 | if (*npins < 0) { |
| 538 | dev_err(dev, "invalid pin list in %s node", cfg_np->name); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
| 542 | *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL); |
| 543 | if (!*pin_list) { |
| 544 | dev_err(dev, "failed to allocate memory for pin list\n"); |
| 545 | return -ENOMEM; |
| 546 | } |
| 547 | |
| 548 | of_property_for_each_string(cfg_np, "samsung,pins", prop, pin_name) { |
| 549 | for (cnt = 0; cnt < pctl->npins; cnt++) { |
| 550 | if (pdesc[cnt].name) { |
| 551 | if (!strcmp(pin_name, pdesc[cnt].name)) { |
| 552 | (*pin_list)[idx++] = pdesc[cnt].number; |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | if (cnt == pctl->npins) { |
| 558 | dev_err(dev, "pin %s not valid in %s node\n", |
| 559 | pin_name, cfg_np->name); |
| 560 | devm_kfree(dev, *pin_list); |
| 561 | return -EINVAL; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Parse the information about all the available pin groups and pin functions |
| 570 | * from device node of the pin-controller. A pin group is formed with all |
| 571 | * the pins listed in the "samsung,pins" property. |
| 572 | */ |
| 573 | static int __init samsung_pinctrl_parse_dt(struct platform_device *pdev, |
| 574 | struct samsung_pinctrl_drv_data *drvdata) |
| 575 | { |
| 576 | struct device *dev = &pdev->dev; |
| 577 | struct device_node *dev_np = dev->of_node; |
| 578 | struct device_node *cfg_np; |
| 579 | struct samsung_pin_group *groups, *grp; |
| 580 | struct samsung_pmx_func *functions, *func; |
| 581 | unsigned *pin_list; |
| 582 | unsigned int npins, grp_cnt, func_idx = 0; |
| 583 | char *gname, *fname; |
| 584 | int ret; |
| 585 | |
| 586 | grp_cnt = of_get_child_count(dev_np); |
| 587 | if (!grp_cnt) |
| 588 | return -EINVAL; |
| 589 | |
| 590 | groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL); |
| 591 | if (!groups) { |
| 592 | dev_err(dev, "failed allocate memory for ping group list\n"); |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | grp = groups; |
| 596 | |
| 597 | functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL); |
| 598 | if (!functions) { |
| 599 | dev_err(dev, "failed to allocate memory for function list\n"); |
| 600 | return -EINVAL; |
| 601 | } |
| 602 | func = functions; |
| 603 | |
| 604 | /* |
| 605 | * Iterate over all the child nodes of the pin controller node |
| 606 | * and create pin groups and pin function lists. |
| 607 | */ |
| 608 | for_each_child_of_node(dev_np, cfg_np) { |
| 609 | u32 function; |
| 610 | if (of_find_property(cfg_np, "interrupt-controller", NULL)) |
| 611 | continue; |
| 612 | |
| 613 | ret = samsung_pinctrl_parse_dt_pins(pdev, cfg_np, |
| 614 | &drvdata->pctl, &pin_list, &npins); |
| 615 | if (ret) |
| 616 | return ret; |
| 617 | |
| 618 | /* derive pin group name from the node name */ |
| 619 | gname = devm_kzalloc(dev, strlen(cfg_np->name) + GSUFFIX_LEN, |
| 620 | GFP_KERNEL); |
| 621 | if (!gname) { |
| 622 | dev_err(dev, "failed to alloc memory for group name\n"); |
| 623 | return -ENOMEM; |
| 624 | } |
| 625 | sprintf(gname, "%s%s", cfg_np->name, GROUP_SUFFIX); |
| 626 | |
| 627 | grp->name = gname; |
| 628 | grp->pins = pin_list; |
| 629 | grp->num_pins = npins; |
| 630 | of_property_read_u32(cfg_np, "samsung,pin-function", &function); |
| 631 | grp->func = function; |
| 632 | grp++; |
| 633 | |
| 634 | if (!of_find_property(cfg_np, "samsung,pin-function", NULL)) |
| 635 | continue; |
| 636 | |
| 637 | /* derive function name from the node name */ |
| 638 | fname = devm_kzalloc(dev, strlen(cfg_np->name) + FSUFFIX_LEN, |
| 639 | GFP_KERNEL); |
| 640 | if (!fname) { |
| 641 | dev_err(dev, "failed to alloc memory for func name\n"); |
| 642 | return -ENOMEM; |
| 643 | } |
| 644 | sprintf(fname, "%s%s", cfg_np->name, FUNCTION_SUFFIX); |
| 645 | |
| 646 | func->name = fname; |
| 647 | func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL); |
| 648 | if (!func->groups) { |
| 649 | dev_err(dev, "failed to alloc memory for group list " |
| 650 | "in pin function"); |
| 651 | return -ENOMEM; |
| 652 | } |
| 653 | func->groups[0] = gname; |
| 654 | func->num_groups = 1; |
| 655 | func++; |
| 656 | func_idx++; |
| 657 | } |
| 658 | |
| 659 | drvdata->pin_groups = groups; |
| 660 | drvdata->nr_groups = grp_cnt; |
| 661 | drvdata->pmx_functions = functions; |
| 662 | drvdata->nr_functions = func_idx; |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | /* register the pinctrl interface with the pinctrl subsystem */ |
| 668 | static int __init samsung_pinctrl_register(struct platform_device *pdev, |
| 669 | struct samsung_pinctrl_drv_data *drvdata) |
| 670 | { |
| 671 | struct pinctrl_desc *ctrldesc = &drvdata->pctl; |
| 672 | struct pinctrl_pin_desc *pindesc, *pdesc; |
| 673 | struct samsung_pin_bank *pin_bank; |
| 674 | char *pin_names; |
| 675 | int pin, bank, ret; |
| 676 | |
| 677 | ctrldesc->name = "samsung-pinctrl"; |
| 678 | ctrldesc->owner = THIS_MODULE; |
| 679 | ctrldesc->pctlops = &samsung_pctrl_ops; |
| 680 | ctrldesc->pmxops = &samsung_pinmux_ops; |
| 681 | ctrldesc->confops = &samsung_pinconf_ops; |
| 682 | |
| 683 | pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) * |
| 684 | drvdata->ctrl->nr_pins, GFP_KERNEL); |
| 685 | if (!pindesc) { |
| 686 | dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n"); |
| 687 | return -ENOMEM; |
| 688 | } |
| 689 | ctrldesc->pins = pindesc; |
| 690 | ctrldesc->npins = drvdata->ctrl->nr_pins; |
| 691 | ctrldesc->npins = drvdata->ctrl->nr_pins; |
| 692 | |
| 693 | /* dynamically populate the pin number and pin name for pindesc */ |
| 694 | for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++) |
| 695 | pdesc->number = pin + drvdata->ctrl->base; |
| 696 | |
| 697 | /* |
| 698 | * allocate space for storing the dynamically generated names for all |
| 699 | * the pins which belong to this pin-controller. |
| 700 | */ |
| 701 | pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH * |
| 702 | drvdata->ctrl->nr_pins, GFP_KERNEL); |
| 703 | if (!pin_names) { |
| 704 | dev_err(&pdev->dev, "mem alloc for pin names failed\n"); |
| 705 | return -ENOMEM; |
| 706 | } |
| 707 | |
| 708 | /* for each pin, the name of the pin is pin-bank name + pin number */ |
| 709 | for (bank = 0; bank < drvdata->ctrl->nr_banks; bank++) { |
| 710 | pin_bank = &drvdata->ctrl->pin_banks[bank]; |
| 711 | for (pin = 0; pin < pin_bank->nr_pins; pin++) { |
| 712 | sprintf(pin_names, "%s-%d", pin_bank->name, pin); |
| 713 | pdesc = pindesc + pin_bank->pin_base + pin; |
| 714 | pdesc->name = pin_names; |
| 715 | pin_names += PIN_NAME_LENGTH; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata); |
| 720 | if (!drvdata->pctl_dev) { |
| 721 | dev_err(&pdev->dev, "could not register pinctrl driver\n"); |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | drvdata->grange.name = "samsung-pctrl-gpio-range"; |
| 726 | drvdata->grange.id = 0; |
| 727 | drvdata->grange.base = drvdata->ctrl->base; |
| 728 | drvdata->grange.npins = drvdata->ctrl->nr_pins; |
| 729 | drvdata->grange.gc = drvdata->gc; |
| 730 | pinctrl_add_gpio_range(drvdata->pctl_dev, &drvdata->grange); |
| 731 | |
| 732 | ret = samsung_pinctrl_parse_dt(pdev, drvdata); |
| 733 | if (ret) { |
| 734 | pinctrl_unregister(drvdata->pctl_dev); |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | /* register the gpiolib interface with the gpiolib subsystem */ |
| 742 | static int __init samsung_gpiolib_register(struct platform_device *pdev, |
| 743 | struct samsung_pinctrl_drv_data *drvdata) |
| 744 | { |
| 745 | struct gpio_chip *gc; |
| 746 | int ret; |
| 747 | |
| 748 | gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL); |
| 749 | if (!gc) { |
| 750 | dev_err(&pdev->dev, "mem alloc for gpio_chip failed\n"); |
| 751 | return -ENOMEM; |
| 752 | } |
| 753 | |
| 754 | drvdata->gc = gc; |
| 755 | gc->base = drvdata->ctrl->base; |
| 756 | gc->ngpio = drvdata->ctrl->nr_pins; |
| 757 | gc->dev = &pdev->dev; |
| 758 | gc->set = samsung_gpio_set; |
| 759 | gc->get = samsung_gpio_get; |
| 760 | gc->direction_input = samsung_gpio_direction_input; |
| 761 | gc->direction_output = samsung_gpio_direction_output; |
| 762 | gc->label = drvdata->ctrl->label; |
| 763 | gc->owner = THIS_MODULE; |
| 764 | ret = gpiochip_add(gc); |
| 765 | if (ret) { |
| 766 | dev_err(&pdev->dev, "failed to register gpio_chip %s, error " |
| 767 | "code: %d\n", gc->label, ret); |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | /* unregister the gpiolib interface with the gpiolib subsystem */ |
| 775 | static int __init samsung_gpiolib_unregister(struct platform_device *pdev, |
| 776 | struct samsung_pinctrl_drv_data *drvdata) |
| 777 | { |
| 778 | int ret = gpiochip_remove(drvdata->gc); |
| 779 | if (ret) { |
| 780 | dev_err(&pdev->dev, "gpio chip remove failed\n"); |
| 781 | return ret; |
| 782 | } |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static const struct of_device_id samsung_pinctrl_dt_match[]; |
| 787 | |
| 788 | /* retrieve the soc specific data */ |
Tomasz Figa | 2f0253f | 2012-09-21 07:34:04 +0900 | [diff] [blame] | 789 | static struct samsung_pin_ctrl *samsung_pinctrl_get_soc_data( |
Thomas Abraham | 30574f0 | 2012-09-07 06:07:19 +0900 | [diff] [blame] | 790 | struct platform_device *pdev) |
| 791 | { |
| 792 | int id; |
| 793 | const struct of_device_id *match; |
| 794 | const struct device_node *node = pdev->dev.of_node; |
| 795 | |
| 796 | id = of_alias_get_id(pdev->dev.of_node, "pinctrl"); |
| 797 | if (id < 0) { |
| 798 | dev_err(&pdev->dev, "failed to get alias id\n"); |
| 799 | return NULL; |
| 800 | } |
| 801 | match = of_match_node(samsung_pinctrl_dt_match, node); |
| 802 | return (struct samsung_pin_ctrl *)match->data + id; |
| 803 | } |
| 804 | |
| 805 | static int __devinit samsung_pinctrl_probe(struct platform_device *pdev) |
| 806 | { |
| 807 | struct samsung_pinctrl_drv_data *drvdata; |
| 808 | struct device *dev = &pdev->dev; |
| 809 | struct samsung_pin_ctrl *ctrl; |
| 810 | struct resource *res; |
| 811 | int ret; |
| 812 | |
| 813 | if (!dev->of_node) { |
| 814 | dev_err(dev, "device tree node not found\n"); |
| 815 | return -ENODEV; |
| 816 | } |
| 817 | |
| 818 | ctrl = samsung_pinctrl_get_soc_data(pdev); |
| 819 | if (!ctrl) { |
| 820 | dev_err(&pdev->dev, "driver data not available\n"); |
| 821 | return -EINVAL; |
| 822 | } |
| 823 | |
| 824 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
| 825 | if (!drvdata) { |
| 826 | dev_err(dev, "failed to allocate memory for driver's " |
| 827 | "private data\n"); |
| 828 | return -ENOMEM; |
| 829 | } |
| 830 | drvdata->ctrl = ctrl; |
| 831 | drvdata->dev = dev; |
| 832 | |
| 833 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 834 | if (!res) { |
| 835 | dev_err(dev, "cannot find IO resource\n"); |
| 836 | return -ENOENT; |
| 837 | } |
| 838 | |
| 839 | drvdata->virt_base = devm_request_and_ioremap(&pdev->dev, res); |
| 840 | if (!drvdata->virt_base) { |
| 841 | dev_err(dev, "ioremap failed\n"); |
| 842 | return -ENODEV; |
| 843 | } |
| 844 | |
| 845 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 846 | if (res) |
| 847 | drvdata->irq = res->start; |
| 848 | |
| 849 | ret = samsung_gpiolib_register(pdev, drvdata); |
| 850 | if (ret) |
| 851 | return ret; |
| 852 | |
| 853 | ret = samsung_pinctrl_register(pdev, drvdata); |
| 854 | if (ret) { |
| 855 | samsung_gpiolib_unregister(pdev, drvdata); |
| 856 | return ret; |
| 857 | } |
| 858 | |
| 859 | if (ctrl->eint_gpio_init) |
| 860 | ctrl->eint_gpio_init(drvdata); |
| 861 | if (ctrl->eint_wkup_init) |
| 862 | ctrl->eint_wkup_init(drvdata); |
| 863 | |
| 864 | platform_set_drvdata(pdev, drvdata); |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static const struct of_device_id samsung_pinctrl_dt_match[] = { |
| 869 | { .compatible = "samsung,pinctrl-exynos4210", |
| 870 | .data = (void *)exynos4210_pin_ctrl }, |
| 871 | {}, |
| 872 | }; |
| 873 | MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match); |
| 874 | |
| 875 | static struct platform_driver samsung_pinctrl_driver = { |
| 876 | .probe = samsung_pinctrl_probe, |
| 877 | .driver = { |
| 878 | .name = "samsung-pinctrl", |
| 879 | .owner = THIS_MODULE, |
| 880 | .of_match_table = of_match_ptr(samsung_pinctrl_dt_match), |
| 881 | }, |
| 882 | }; |
| 883 | |
| 884 | static int __init samsung_pinctrl_drv_register(void) |
| 885 | { |
| 886 | return platform_driver_register(&samsung_pinctrl_driver); |
| 887 | } |
| 888 | postcore_initcall(samsung_pinctrl_drv_register); |
| 889 | |
| 890 | static void __exit samsung_pinctrl_drv_unregister(void) |
| 891 | { |
| 892 | platform_driver_unregister(&samsung_pinctrl_driver); |
| 893 | } |
| 894 | module_exit(samsung_pinctrl_drv_unregister); |
| 895 | |
| 896 | MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>"); |
| 897 | MODULE_DESCRIPTION("Samsung pinctrl driver"); |
| 898 | MODULE_LICENSE("GPL v2"); |