Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Pinctrl driver for Rockchip SoCs |
| 3 | * |
| 4 | * Copyright (c) 2013 MundoReader S.L. |
| 5 | * Author: Heiko Stuebner <heiko@sntech.de> |
| 6 | * |
| 7 | * With some ideas taken from pinctrl-samsung: |
| 8 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 9 | * http://www.samsung.com |
| 10 | * Copyright (c) 2012 Linaro Ltd |
| 11 | * http://www.linaro.org |
| 12 | * |
| 13 | * and pinctrl-at91: |
| 14 | * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License version 2 as published |
| 18 | * by the Free Software Foundation. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <linux/bitops.h> |
| 30 | #include <linux/gpio.h> |
| 31 | #include <linux/of_address.h> |
| 32 | #include <linux/of_irq.h> |
| 33 | #include <linux/pinctrl/machine.h> |
| 34 | #include <linux/pinctrl/pinconf.h> |
| 35 | #include <linux/pinctrl/pinctrl.h> |
| 36 | #include <linux/pinctrl/pinmux.h> |
| 37 | #include <linux/pinctrl/pinconf-generic.h> |
| 38 | #include <linux/irqchip/chained_irq.h> |
Heiko Stübner | 7e865ab | 2013-07-23 13:34:20 +0200 | [diff] [blame] | 39 | #include <linux/clk.h> |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 40 | #include <linux/regmap.h> |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 41 | #include <linux/mfd/syscon.h> |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 42 | #include <dt-bindings/pinctrl/rockchip.h> |
| 43 | |
| 44 | #include "core.h" |
| 45 | #include "pinconf.h" |
| 46 | |
| 47 | /* GPIO control registers */ |
| 48 | #define GPIO_SWPORT_DR 0x00 |
| 49 | #define GPIO_SWPORT_DDR 0x04 |
| 50 | #define GPIO_INTEN 0x30 |
| 51 | #define GPIO_INTMASK 0x34 |
| 52 | #define GPIO_INTTYPE_LEVEL 0x38 |
| 53 | #define GPIO_INT_POLARITY 0x3c |
| 54 | #define GPIO_INT_STATUS 0x40 |
| 55 | #define GPIO_INT_RAWSTATUS 0x44 |
| 56 | #define GPIO_DEBOUNCE 0x48 |
| 57 | #define GPIO_PORTS_EOI 0x4c |
| 58 | #define GPIO_EXT_PORT 0x50 |
| 59 | #define GPIO_LS_SYNC 0x60 |
| 60 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 61 | enum rockchip_pinctrl_type { |
| 62 | RK2928, |
| 63 | RK3066B, |
| 64 | RK3188, |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 65 | RK3288, |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 66 | RK3368, |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 69 | /** |
| 70 | * Encode variants of iomux registers into a type variable |
| 71 | */ |
| 72 | #define IOMUX_GPIO_ONLY BIT(0) |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 73 | #define IOMUX_WIDTH_4BIT BIT(1) |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 74 | #define IOMUX_SOURCE_PMU BIT(2) |
Heiko Stübner | 62f4922 | 2014-06-16 01:37:49 +0200 | [diff] [blame] | 75 | #define IOMUX_UNROUTED BIT(3) |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * @type: iomux variant using IOMUX_* constants |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 79 | * @offset: if initialized to -1 it will be autocalculated, by specifying |
| 80 | * an initial offset value the relevant source offset can be reset |
| 81 | * to a new value for autocalculating the following iomux registers. |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 82 | */ |
| 83 | struct rockchip_iomux { |
| 84 | int type; |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 85 | int offset; |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 88 | /** |
| 89 | * @reg_base: register base of the gpio bank |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 90 | * @reg_pull: optional separate register for additional pull settings |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 91 | * @clk: clock of the gpio bank |
| 92 | * @irq: interrupt of the gpio bank |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 93 | * @saved_masks: Saved content of GPIO_INTEN at suspend time. |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 94 | * @pin_base: first pin number |
| 95 | * @nr_pins: number of pins in this bank |
| 96 | * @name: name of the bank |
| 97 | * @bank_num: number of the bank, to account for holes |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 98 | * @iomux: array describing the 4 iomux sources of the bank |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 99 | * @valid: are all necessary informations present |
| 100 | * @of_node: dt node of this bank |
| 101 | * @drvdata: common pinctrl basedata |
| 102 | * @domain: irqdomain of the gpio bank |
| 103 | * @gpio_chip: gpiolib chip |
| 104 | * @grange: gpio range |
| 105 | * @slock: spinlock for the gpio bank |
| 106 | */ |
| 107 | struct rockchip_pin_bank { |
| 108 | void __iomem *reg_base; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 109 | struct regmap *regmap_pull; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 110 | struct clk *clk; |
| 111 | int irq; |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 112 | u32 saved_masks; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 113 | u32 pin_base; |
| 114 | u8 nr_pins; |
| 115 | char *name; |
| 116 | u8 bank_num; |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 117 | struct rockchip_iomux iomux[4]; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 118 | bool valid; |
| 119 | struct device_node *of_node; |
| 120 | struct rockchip_pinctrl *drvdata; |
| 121 | struct irq_domain *domain; |
| 122 | struct gpio_chip gpio_chip; |
| 123 | struct pinctrl_gpio_range grange; |
| 124 | spinlock_t slock; |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 125 | u32 toggle_edge_mode; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | #define PIN_BANK(id, pins, label) \ |
| 129 | { \ |
| 130 | .bank_num = id, \ |
| 131 | .nr_pins = pins, \ |
| 132 | .name = label, \ |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 133 | .iomux = { \ |
| 134 | { .offset = -1 }, \ |
| 135 | { .offset = -1 }, \ |
| 136 | { .offset = -1 }, \ |
| 137 | { .offset = -1 }, \ |
| 138 | }, \ |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 141 | #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \ |
| 142 | { \ |
| 143 | .bank_num = id, \ |
| 144 | .nr_pins = pins, \ |
| 145 | .name = label, \ |
| 146 | .iomux = { \ |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 147 | { .type = iom0, .offset = -1 }, \ |
| 148 | { .type = iom1, .offset = -1 }, \ |
| 149 | { .type = iom2, .offset = -1 }, \ |
| 150 | { .type = iom3, .offset = -1 }, \ |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 151 | }, \ |
| 152 | } |
| 153 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 154 | /** |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 155 | */ |
| 156 | struct rockchip_pin_ctrl { |
| 157 | struct rockchip_pin_bank *pin_banks; |
| 158 | u32 nr_banks; |
| 159 | u32 nr_pins; |
| 160 | char *label; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 161 | enum rockchip_pinctrl_type type; |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 162 | int grf_mux_offset; |
| 163 | int pmu_mux_offset; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 164 | void (*pull_calc_reg)(struct rockchip_pin_bank *bank, |
| 165 | int pin_num, struct regmap **regmap, |
| 166 | int *reg, u8 *bit); |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 167 | void (*drv_calc_reg)(struct rockchip_pin_bank *bank, |
| 168 | int pin_num, struct regmap **regmap, |
| 169 | int *reg, u8 *bit); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | struct rockchip_pin_config { |
| 173 | unsigned int func; |
| 174 | unsigned long *configs; |
| 175 | unsigned int nconfigs; |
| 176 | }; |
| 177 | |
| 178 | /** |
| 179 | * struct rockchip_pin_group: represent group of pins of a pinmux function. |
| 180 | * @name: name of the pin group, used to lookup the group. |
| 181 | * @pins: the pins included in this group. |
| 182 | * @npins: number of pins included in this group. |
| 183 | * @func: the mux function number to be programmed when selected. |
| 184 | * @configs: the config values to be set for each pin |
| 185 | * @nconfigs: number of configs for each pin |
| 186 | */ |
| 187 | struct rockchip_pin_group { |
| 188 | const char *name; |
| 189 | unsigned int npins; |
| 190 | unsigned int *pins; |
| 191 | struct rockchip_pin_config *data; |
| 192 | }; |
| 193 | |
| 194 | /** |
| 195 | * struct rockchip_pmx_func: represent a pin function. |
| 196 | * @name: name of the pin function, used to lookup the function. |
| 197 | * @groups: one or more names of pin groups that provide this function. |
| 198 | * @num_groups: number of groups included in @groups. |
| 199 | */ |
| 200 | struct rockchip_pmx_func { |
| 201 | const char *name; |
| 202 | const char **groups; |
| 203 | u8 ngroups; |
| 204 | }; |
| 205 | |
| 206 | struct rockchip_pinctrl { |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 207 | struct regmap *regmap_base; |
Heiko Stübner | bfc7a42 | 2014-05-05 13:58:00 +0200 | [diff] [blame] | 208 | int reg_size; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 209 | struct regmap *regmap_pull; |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 210 | struct regmap *regmap_pmu; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 211 | struct device *dev; |
| 212 | struct rockchip_pin_ctrl *ctrl; |
| 213 | struct pinctrl_desc pctl; |
| 214 | struct pinctrl_dev *pctl_dev; |
| 215 | struct rockchip_pin_group *groups; |
| 216 | unsigned int ngroups; |
| 217 | struct rockchip_pmx_func *functions; |
| 218 | unsigned int nfunctions; |
| 219 | }; |
| 220 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 221 | static struct regmap_config rockchip_regmap_config = { |
| 222 | .reg_bits = 32, |
| 223 | .val_bits = 32, |
| 224 | .reg_stride = 4, |
| 225 | }; |
| 226 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 227 | static inline struct rockchip_pin_bank *gc_to_pin_bank(struct gpio_chip *gc) |
| 228 | { |
| 229 | return container_of(gc, struct rockchip_pin_bank, gpio_chip); |
| 230 | } |
| 231 | |
| 232 | static const inline struct rockchip_pin_group *pinctrl_name_to_group( |
| 233 | const struct rockchip_pinctrl *info, |
| 234 | const char *name) |
| 235 | { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 236 | int i; |
| 237 | |
| 238 | for (i = 0; i < info->ngroups; i++) { |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 239 | if (!strcmp(info->groups[i].name, name)) |
| 240 | return &info->groups[i]; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 243 | return NULL; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* |
| 247 | * given a pin number that is local to a pin controller, find out the pin bank |
| 248 | * and the register base of the pin bank. |
| 249 | */ |
| 250 | static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info, |
| 251 | unsigned pin) |
| 252 | { |
| 253 | struct rockchip_pin_bank *b = info->ctrl->pin_banks; |
| 254 | |
Axel Lin | 51578b9 | 2013-08-23 15:49:00 +0800 | [diff] [blame] | 255 | while (pin >= (b->pin_base + b->nr_pins)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 256 | b++; |
| 257 | |
| 258 | return b; |
| 259 | } |
| 260 | |
| 261 | static struct rockchip_pin_bank *bank_num_to_bank( |
| 262 | struct rockchip_pinctrl *info, |
| 263 | unsigned num) |
| 264 | { |
| 265 | struct rockchip_pin_bank *b = info->ctrl->pin_banks; |
| 266 | int i; |
| 267 | |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 268 | for (i = 0; i < info->ctrl->nr_banks; i++, b++) { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 269 | if (b->bank_num == num) |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 270 | return b; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 273 | return ERR_PTR(-EINVAL); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Pinctrl_ops handling |
| 278 | */ |
| 279 | |
| 280 | static int rockchip_get_groups_count(struct pinctrl_dev *pctldev) |
| 281 | { |
| 282 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 283 | |
| 284 | return info->ngroups; |
| 285 | } |
| 286 | |
| 287 | static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev, |
| 288 | unsigned selector) |
| 289 | { |
| 290 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 291 | |
| 292 | return info->groups[selector].name; |
| 293 | } |
| 294 | |
| 295 | static int rockchip_get_group_pins(struct pinctrl_dev *pctldev, |
| 296 | unsigned selector, const unsigned **pins, |
| 297 | unsigned *npins) |
| 298 | { |
| 299 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 300 | |
| 301 | if (selector >= info->ngroups) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | *pins = info->groups[selector].pins; |
| 305 | *npins = info->groups[selector].npins; |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 311 | struct device_node *np, |
| 312 | struct pinctrl_map **map, unsigned *num_maps) |
| 313 | { |
| 314 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 315 | const struct rockchip_pin_group *grp; |
| 316 | struct pinctrl_map *new_map; |
| 317 | struct device_node *parent; |
| 318 | int map_num = 1; |
| 319 | int i; |
| 320 | |
| 321 | /* |
| 322 | * first find the group of this node and check if we need to create |
| 323 | * config maps for pins |
| 324 | */ |
| 325 | grp = pinctrl_name_to_group(info, np->name); |
| 326 | if (!grp) { |
| 327 | dev_err(info->dev, "unable to find group for node %s\n", |
| 328 | np->name); |
| 329 | return -EINVAL; |
| 330 | } |
| 331 | |
| 332 | map_num += grp->npins; |
| 333 | new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, |
| 334 | GFP_KERNEL); |
| 335 | if (!new_map) |
| 336 | return -ENOMEM; |
| 337 | |
| 338 | *map = new_map; |
| 339 | *num_maps = map_num; |
| 340 | |
| 341 | /* create mux map */ |
| 342 | parent = of_get_parent(np); |
| 343 | if (!parent) { |
| 344 | devm_kfree(pctldev->dev, new_map); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | new_map[0].type = PIN_MAP_TYPE_MUX_GROUP; |
| 348 | new_map[0].data.mux.function = parent->name; |
| 349 | new_map[0].data.mux.group = np->name; |
| 350 | of_node_put(parent); |
| 351 | |
| 352 | /* create config map */ |
| 353 | new_map++; |
| 354 | for (i = 0; i < grp->npins; i++) { |
| 355 | new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN; |
| 356 | new_map[i].data.configs.group_or_pin = |
| 357 | pin_get_name(pctldev, grp->pins[i]); |
| 358 | new_map[i].data.configs.configs = grp->data[i].configs; |
| 359 | new_map[i].data.configs.num_configs = grp->data[i].nconfigs; |
| 360 | } |
| 361 | |
| 362 | dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n", |
| 363 | (*map)->data.mux.function, (*map)->data.mux.group, map_num); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static void rockchip_dt_free_map(struct pinctrl_dev *pctldev, |
| 369 | struct pinctrl_map *map, unsigned num_maps) |
| 370 | { |
| 371 | } |
| 372 | |
| 373 | static const struct pinctrl_ops rockchip_pctrl_ops = { |
| 374 | .get_groups_count = rockchip_get_groups_count, |
| 375 | .get_group_name = rockchip_get_group_name, |
| 376 | .get_group_pins = rockchip_get_group_pins, |
| 377 | .dt_node_to_map = rockchip_dt_node_to_map, |
| 378 | .dt_free_map = rockchip_dt_free_map, |
| 379 | }; |
| 380 | |
| 381 | /* |
| 382 | * Hardware access |
| 383 | */ |
| 384 | |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 385 | static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin) |
| 386 | { |
| 387 | struct rockchip_pinctrl *info = bank->drvdata; |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 388 | int iomux_num = (pin / 8); |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 389 | struct regmap *regmap; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 390 | unsigned int val; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 391 | int reg, ret, mask; |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 392 | u8 bit; |
| 393 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 394 | if (iomux_num > 3) |
| 395 | return -EINVAL; |
| 396 | |
Heiko Stübner | 62f4922 | 2014-06-16 01:37:49 +0200 | [diff] [blame] | 397 | if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) { |
| 398 | dev_err(info->dev, "pin %d is unrouted\n", pin); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 402 | if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 403 | return RK_FUNC_GPIO; |
| 404 | |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 405 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 406 | ? info->regmap_pmu : info->regmap_base; |
| 407 | |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 408 | /* get basic quadrupel of mux registers and the correct reg inside */ |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 409 | mask = (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) ? 0xf : 0x3; |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 410 | reg = bank->iomux[iomux_num].offset; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 411 | if (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) { |
| 412 | if ((pin % 8) >= 4) |
| 413 | reg += 0x4; |
| 414 | bit = (pin % 4) * 4; |
| 415 | } else { |
| 416 | bit = (pin % 8) * 2; |
| 417 | } |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 418 | |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 419 | ret = regmap_read(regmap, reg, &val); |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 420 | if (ret) |
| 421 | return ret; |
| 422 | |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 423 | return ((val >> bit) & mask); |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 426 | /* |
| 427 | * Set a new mux function for a pin. |
| 428 | * |
| 429 | * The register is divided into the upper and lower 16 bit. When changing |
| 430 | * a value, the previous register value is not read and changed. Instead |
| 431 | * it seems the changed bits are marked in the upper 16 bit, while the |
| 432 | * changed value gets set in the same offset in the lower 16 bit. |
| 433 | * All pin settings seem to be 2 bit wide in both the upper and lower |
| 434 | * parts. |
| 435 | * @bank: pin bank to change |
| 436 | * @pin: pin to change |
| 437 | * @mux: new mux function to set |
| 438 | */ |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 439 | static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 440 | { |
| 441 | struct rockchip_pinctrl *info = bank->drvdata; |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 442 | int iomux_num = (pin / 8); |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 443 | struct regmap *regmap; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 444 | int reg, ret, mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 445 | unsigned long flags; |
| 446 | u8 bit; |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 447 | u32 data, rmask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 448 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 449 | if (iomux_num > 3) |
| 450 | return -EINVAL; |
| 451 | |
Heiko Stübner | 62f4922 | 2014-06-16 01:37:49 +0200 | [diff] [blame] | 452 | if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) { |
| 453 | dev_err(info->dev, "pin %d is unrouted\n", pin); |
| 454 | return -EINVAL; |
| 455 | } |
| 456 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 457 | if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) { |
Heiko Stübner | c4a532de | 2014-03-26 00:57:52 +0100 | [diff] [blame] | 458 | if (mux != RK_FUNC_GPIO) { |
| 459 | dev_err(info->dev, |
| 460 | "pin %d only supports a gpio mux\n", pin); |
| 461 | return -ENOTSUPP; |
| 462 | } else { |
| 463 | return 0; |
| 464 | } |
| 465 | } |
| 466 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 467 | dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n", |
| 468 | bank->bank_num, pin, mux); |
| 469 | |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 470 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 471 | ? info->regmap_pmu : info->regmap_base; |
| 472 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 473 | /* get basic quadrupel of mux registers and the correct reg inside */ |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 474 | mask = (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) ? 0xf : 0x3; |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 475 | reg = bank->iomux[iomux_num].offset; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 476 | if (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) { |
| 477 | if ((pin % 8) >= 4) |
| 478 | reg += 0x4; |
| 479 | bit = (pin % 4) * 4; |
| 480 | } else { |
| 481 | bit = (pin % 8) * 2; |
| 482 | } |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 483 | |
| 484 | spin_lock_irqsave(&bank->slock, flags); |
| 485 | |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 486 | data = (mask << (bit + 16)); |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 487 | rmask = data | (data >> 16); |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 488 | data |= (mux & mask) << bit; |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 489 | ret = regmap_update_bits(regmap, reg, rmask, data); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 490 | |
| 491 | spin_unlock_irqrestore(&bank->slock, flags); |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 492 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 493 | return ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 496 | #define RK2928_PULL_OFFSET 0x118 |
| 497 | #define RK2928_PULL_PINS_PER_REG 16 |
| 498 | #define RK2928_PULL_BANK_STRIDE 8 |
| 499 | |
| 500 | static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 501 | int pin_num, struct regmap **regmap, |
| 502 | int *reg, u8 *bit) |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 503 | { |
| 504 | struct rockchip_pinctrl *info = bank->drvdata; |
| 505 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 506 | *regmap = info->regmap_base; |
| 507 | *reg = RK2928_PULL_OFFSET; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 508 | *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE; |
| 509 | *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4; |
| 510 | |
| 511 | *bit = pin_num % RK2928_PULL_PINS_PER_REG; |
| 512 | }; |
| 513 | |
Heiko Stübner | bfc7a42 | 2014-05-05 13:58:00 +0200 | [diff] [blame] | 514 | #define RK3188_PULL_OFFSET 0x164 |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 515 | #define RK3188_PULL_BITS_PER_PIN 2 |
| 516 | #define RK3188_PULL_PINS_PER_REG 8 |
| 517 | #define RK3188_PULL_BANK_STRIDE 16 |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 518 | #define RK3188_PULL_PMU_OFFSET 0x64 |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 519 | |
| 520 | static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 521 | int pin_num, struct regmap **regmap, |
| 522 | int *reg, u8 *bit) |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 523 | { |
| 524 | struct rockchip_pinctrl *info = bank->drvdata; |
| 525 | |
| 526 | /* The first 12 pins of the first bank are located elsewhere */ |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 527 | if (bank->bank_num == 0 && pin_num < 12) { |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 528 | *regmap = info->regmap_pmu ? info->regmap_pmu |
| 529 | : bank->regmap_pull; |
| 530 | *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 531 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 532 | *bit = pin_num % RK3188_PULL_PINS_PER_REG; |
| 533 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 534 | } else { |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 535 | *regmap = info->regmap_pull ? info->regmap_pull |
| 536 | : info->regmap_base; |
| 537 | *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET; |
| 538 | |
Heiko Stübner | bfc7a42 | 2014-05-05 13:58:00 +0200 | [diff] [blame] | 539 | /* correct the offset, as it is the 2nd pull register */ |
| 540 | *reg -= 4; |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 541 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 542 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 543 | |
| 544 | /* |
| 545 | * The bits in these registers have an inverse ordering |
| 546 | * with the lowest pin being in bits 15:14 and the highest |
| 547 | * pin in bits 1:0 |
| 548 | */ |
| 549 | *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG); |
| 550 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 551 | } |
| 552 | } |
| 553 | |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 554 | #define RK3288_PULL_OFFSET 0x140 |
| 555 | static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 556 | int pin_num, struct regmap **regmap, |
| 557 | int *reg, u8 *bit) |
| 558 | { |
| 559 | struct rockchip_pinctrl *info = bank->drvdata; |
| 560 | |
| 561 | /* The first 24 pins of the first bank are located in PMU */ |
| 562 | if (bank->bank_num == 0) { |
| 563 | *regmap = info->regmap_pmu; |
| 564 | *reg = RK3188_PULL_PMU_OFFSET; |
| 565 | |
| 566 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 567 | *bit = pin_num % RK3188_PULL_PINS_PER_REG; |
| 568 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 569 | } else { |
| 570 | *regmap = info->regmap_base; |
| 571 | *reg = RK3288_PULL_OFFSET; |
| 572 | |
| 573 | /* correct the offset, as we're starting with the 2nd bank */ |
| 574 | *reg -= 0x10; |
| 575 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 576 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 577 | |
| 578 | *bit = (pin_num % RK3188_PULL_PINS_PER_REG); |
| 579 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 580 | } |
| 581 | } |
| 582 | |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 583 | #define RK3288_DRV_PMU_OFFSET 0x70 |
| 584 | #define RK3288_DRV_GRF_OFFSET 0x1c0 |
| 585 | #define RK3288_DRV_BITS_PER_PIN 2 |
| 586 | #define RK3288_DRV_PINS_PER_REG 8 |
| 587 | #define RK3288_DRV_BANK_STRIDE 16 |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 588 | |
| 589 | static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 590 | int pin_num, struct regmap **regmap, |
| 591 | int *reg, u8 *bit) |
| 592 | { |
| 593 | struct rockchip_pinctrl *info = bank->drvdata; |
| 594 | |
| 595 | /* The first 24 pins of the first bank are located in PMU */ |
| 596 | if (bank->bank_num == 0) { |
| 597 | *regmap = info->regmap_pmu; |
| 598 | *reg = RK3288_DRV_PMU_OFFSET; |
| 599 | |
| 600 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 601 | *bit = pin_num % RK3288_DRV_PINS_PER_REG; |
| 602 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 603 | } else { |
| 604 | *regmap = info->regmap_base; |
| 605 | *reg = RK3288_DRV_GRF_OFFSET; |
| 606 | |
| 607 | /* correct the offset, as we're starting with the 2nd bank */ |
| 608 | *reg -= 0x10; |
| 609 | *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE; |
| 610 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 611 | |
| 612 | *bit = (pin_num % RK3288_DRV_PINS_PER_REG); |
| 613 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 614 | } |
| 615 | } |
| 616 | |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 617 | #define RK3368_PULL_GRF_OFFSET 0x100 |
| 618 | #define RK3368_PULL_PMU_OFFSET 0x10 |
| 619 | |
| 620 | static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 621 | int pin_num, struct regmap **regmap, |
| 622 | int *reg, u8 *bit) |
| 623 | { |
| 624 | struct rockchip_pinctrl *info = bank->drvdata; |
| 625 | |
| 626 | /* The first 32 pins of the first bank are located in PMU */ |
| 627 | if (bank->bank_num == 0) { |
| 628 | *regmap = info->regmap_pmu; |
| 629 | *reg = RK3368_PULL_PMU_OFFSET; |
| 630 | |
| 631 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 632 | *bit = pin_num % RK3188_PULL_PINS_PER_REG; |
| 633 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 634 | } else { |
| 635 | *regmap = info->regmap_base; |
| 636 | *reg = RK3368_PULL_GRF_OFFSET; |
| 637 | |
| 638 | /* correct the offset, as we're starting with the 2nd bank */ |
| 639 | *reg -= 0x10; |
| 640 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 641 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 642 | |
| 643 | *bit = (pin_num % RK3188_PULL_PINS_PER_REG); |
| 644 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | #define RK3368_DRV_PMU_OFFSET 0x20 |
| 649 | #define RK3368_DRV_GRF_OFFSET 0x200 |
| 650 | |
| 651 | static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 652 | int pin_num, struct regmap **regmap, |
| 653 | int *reg, u8 *bit) |
| 654 | { |
| 655 | struct rockchip_pinctrl *info = bank->drvdata; |
| 656 | |
| 657 | /* The first 32 pins of the first bank are located in PMU */ |
| 658 | if (bank->bank_num == 0) { |
| 659 | *regmap = info->regmap_pmu; |
| 660 | *reg = RK3368_DRV_PMU_OFFSET; |
| 661 | |
| 662 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 663 | *bit = pin_num % RK3288_DRV_PINS_PER_REG; |
| 664 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 665 | } else { |
| 666 | *regmap = info->regmap_base; |
| 667 | *reg = RK3368_DRV_GRF_OFFSET; |
| 668 | |
| 669 | /* correct the offset, as we're starting with the 2nd bank */ |
| 670 | *reg -= 0x10; |
| 671 | *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE; |
| 672 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 673 | |
| 674 | *bit = (pin_num % RK3288_DRV_PINS_PER_REG); |
| 675 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 676 | } |
| 677 | } |
| 678 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 679 | static int rockchip_perpin_drv_list[] = { 2, 4, 8, 12 }; |
| 680 | |
| 681 | static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank, |
| 682 | int pin_num) |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 683 | { |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 684 | struct rockchip_pinctrl *info = bank->drvdata; |
| 685 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 686 | struct regmap *regmap; |
| 687 | int reg, ret; |
| 688 | u32 data; |
| 689 | u8 bit; |
| 690 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 691 | ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 692 | |
| 693 | ret = regmap_read(regmap, reg, &data); |
| 694 | if (ret) |
| 695 | return ret; |
| 696 | |
| 697 | data >>= bit; |
| 698 | data &= (1 << RK3288_DRV_BITS_PER_PIN) - 1; |
| 699 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 700 | return rockchip_perpin_drv_list[data]; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 701 | } |
| 702 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 703 | static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank, |
| 704 | int pin_num, int strength) |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 705 | { |
| 706 | struct rockchip_pinctrl *info = bank->drvdata; |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 707 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 708 | struct regmap *regmap; |
| 709 | unsigned long flags; |
| 710 | int reg, ret, i; |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 711 | u32 data, rmask; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 712 | u8 bit; |
| 713 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 714 | ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 715 | |
| 716 | ret = -EINVAL; |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 717 | for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list); i++) { |
| 718 | if (rockchip_perpin_drv_list[i] == strength) { |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 719 | ret = i; |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | if (ret < 0) { |
| 725 | dev_err(info->dev, "unsupported driver strength %d\n", |
| 726 | strength); |
| 727 | return ret; |
| 728 | } |
| 729 | |
| 730 | spin_lock_irqsave(&bank->slock, flags); |
| 731 | |
| 732 | /* enable the write to the equivalent lower bits */ |
| 733 | data = ((1 << RK3288_DRV_BITS_PER_PIN) - 1) << (bit + 16); |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 734 | rmask = data | (data >> 16); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 735 | data |= (ret << bit); |
| 736 | |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 737 | ret = regmap_update_bits(regmap, reg, rmask, data); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 738 | spin_unlock_irqrestore(&bank->slock, flags); |
| 739 | |
| 740 | return ret; |
| 741 | } |
| 742 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 743 | static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num) |
| 744 | { |
| 745 | struct rockchip_pinctrl *info = bank->drvdata; |
| 746 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 747 | struct regmap *regmap; |
| 748 | int reg, ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 749 | u8 bit; |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 750 | u32 data; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 751 | |
| 752 | /* rk3066b does support any pulls */ |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 753 | if (ctrl->type == RK3066B) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 754 | return PIN_CONFIG_BIAS_DISABLE; |
| 755 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 756 | ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit); |
| 757 | |
| 758 | ret = regmap_read(regmap, reg, &data); |
| 759 | if (ret) |
| 760 | return ret; |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 761 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 762 | switch (ctrl->type) { |
| 763 | case RK2928: |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 764 | return !(data & BIT(bit)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 765 | ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT |
| 766 | : PIN_CONFIG_BIAS_DISABLE; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 767 | case RK3188: |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 768 | case RK3288: |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 769 | case RK3368: |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 770 | data >>= bit; |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 771 | data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1; |
| 772 | |
| 773 | switch (data) { |
| 774 | case 0: |
| 775 | return PIN_CONFIG_BIAS_DISABLE; |
| 776 | case 1: |
| 777 | return PIN_CONFIG_BIAS_PULL_UP; |
| 778 | case 2: |
| 779 | return PIN_CONFIG_BIAS_PULL_DOWN; |
| 780 | case 3: |
| 781 | return PIN_CONFIG_BIAS_BUS_HOLD; |
| 782 | } |
| 783 | |
| 784 | dev_err(info->dev, "unknown pull setting\n"); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 785 | return -EIO; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 786 | default: |
| 787 | dev_err(info->dev, "unsupported pinctrl type\n"); |
| 788 | return -EINVAL; |
| 789 | }; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | static int rockchip_set_pull(struct rockchip_pin_bank *bank, |
| 793 | int pin_num, int pull) |
| 794 | { |
| 795 | struct rockchip_pinctrl *info = bank->drvdata; |
| 796 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 797 | struct regmap *regmap; |
| 798 | int reg, ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 799 | unsigned long flags; |
| 800 | u8 bit; |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 801 | u32 data, rmask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 802 | |
| 803 | dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n", |
| 804 | bank->bank_num, pin_num, pull); |
| 805 | |
| 806 | /* rk3066b does support any pulls */ |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 807 | if (ctrl->type == RK3066B) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 808 | return pull ? -EINVAL : 0; |
| 809 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 810 | ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 811 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 812 | switch (ctrl->type) { |
| 813 | case RK2928: |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 814 | spin_lock_irqsave(&bank->slock, flags); |
| 815 | |
| 816 | data = BIT(bit + 16); |
| 817 | if (pull == PIN_CONFIG_BIAS_DISABLE) |
| 818 | data |= BIT(bit); |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 819 | ret = regmap_write(regmap, reg, data); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 820 | |
| 821 | spin_unlock_irqrestore(&bank->slock, flags); |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 822 | break; |
| 823 | case RK3188: |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 824 | case RK3288: |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 825 | case RK3368: |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 826 | spin_lock_irqsave(&bank->slock, flags); |
| 827 | |
| 828 | /* enable the write to the equivalent lower bits */ |
| 829 | data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16); |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 830 | rmask = data | (data >> 16); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 831 | |
| 832 | switch (pull) { |
| 833 | case PIN_CONFIG_BIAS_DISABLE: |
| 834 | break; |
| 835 | case PIN_CONFIG_BIAS_PULL_UP: |
| 836 | data |= (1 << bit); |
| 837 | break; |
| 838 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 839 | data |= (2 << bit); |
| 840 | break; |
| 841 | case PIN_CONFIG_BIAS_BUS_HOLD: |
| 842 | data |= (3 << bit); |
| 843 | break; |
| 844 | default: |
Dan Carpenter | d32c3e2 | 2013-11-14 11:22:54 +0300 | [diff] [blame] | 845 | spin_unlock_irqrestore(&bank->slock, flags); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 846 | dev_err(info->dev, "unsupported pull setting %d\n", |
| 847 | pull); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 851 | ret = regmap_update_bits(regmap, reg, rmask, data); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 852 | |
| 853 | spin_unlock_irqrestore(&bank->slock, flags); |
| 854 | break; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 855 | default: |
| 856 | dev_err(info->dev, "unsupported pinctrl type\n"); |
| 857 | return -EINVAL; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 860 | return ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Pinmux_ops handling |
| 865 | */ |
| 866 | |
| 867 | static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev) |
| 868 | { |
| 869 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 870 | |
| 871 | return info->nfunctions; |
| 872 | } |
| 873 | |
| 874 | static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev, |
| 875 | unsigned selector) |
| 876 | { |
| 877 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 878 | |
| 879 | return info->functions[selector].name; |
| 880 | } |
| 881 | |
| 882 | static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev, |
| 883 | unsigned selector, const char * const **groups, |
| 884 | unsigned * const num_groups) |
| 885 | { |
| 886 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 887 | |
| 888 | *groups = info->functions[selector].groups; |
| 889 | *num_groups = info->functions[selector].ngroups; |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 894 | static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector, |
| 895 | unsigned group) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 896 | { |
| 897 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 898 | const unsigned int *pins = info->groups[group].pins; |
| 899 | const struct rockchip_pin_config *data = info->groups[group].data; |
| 900 | struct rockchip_pin_bank *bank; |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 901 | int cnt, ret = 0; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 902 | |
| 903 | dev_dbg(info->dev, "enable function %s group %s\n", |
| 904 | info->functions[selector].name, info->groups[group].name); |
| 905 | |
| 906 | /* |
| 907 | * for each pin in the pin group selected, program the correspoding pin |
| 908 | * pin function number in the config register. |
| 909 | */ |
| 910 | for (cnt = 0; cnt < info->groups[group].npins; cnt++) { |
| 911 | bank = pin_to_bank(info, pins[cnt]); |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 912 | ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base, |
| 913 | data[cnt].func); |
| 914 | if (ret) |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | if (ret) { |
| 919 | /* revert the already done pin settings */ |
| 920 | for (cnt--; cnt >= 0; cnt--) |
| 921 | rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0); |
| 922 | |
| 923 | return ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 929 | /* |
| 930 | * The calls to gpio_direction_output() and gpio_direction_input() |
| 931 | * leads to this function call (via the pinctrl_gpio_direction_{input|output}() |
| 932 | * function called from the gpiolib interface). |
| 933 | */ |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 934 | static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip, |
| 935 | int pin, bool input) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 936 | { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 937 | struct rockchip_pin_bank *bank; |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 938 | int ret; |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 939 | unsigned long flags; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 940 | u32 data; |
| 941 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 942 | bank = gc_to_pin_bank(chip); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 943 | |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 944 | ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO); |
| 945 | if (ret < 0) |
| 946 | return ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 947 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 948 | clk_enable(bank->clk); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 949 | spin_lock_irqsave(&bank->slock, flags); |
| 950 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 951 | data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); |
| 952 | /* set bit to 1 for output, 0 for input */ |
| 953 | if (!input) |
| 954 | data |= BIT(pin); |
| 955 | else |
| 956 | data &= ~BIT(pin); |
| 957 | writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR); |
| 958 | |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 959 | spin_unlock_irqrestore(&bank->slock, flags); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 960 | clk_disable(bank->clk); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 961 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 962 | return 0; |
| 963 | } |
| 964 | |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 965 | static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 966 | struct pinctrl_gpio_range *range, |
| 967 | unsigned offset, bool input) |
| 968 | { |
| 969 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 970 | struct gpio_chip *chip; |
| 971 | int pin; |
| 972 | |
| 973 | chip = range->gc; |
| 974 | pin = offset - chip->base; |
| 975 | dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n", |
| 976 | offset, range->name, pin, input ? "input" : "output"); |
| 977 | |
| 978 | return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base, |
| 979 | input); |
| 980 | } |
| 981 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 982 | static const struct pinmux_ops rockchip_pmx_ops = { |
| 983 | .get_functions_count = rockchip_pmx_get_funcs_count, |
| 984 | .get_function_name = rockchip_pmx_get_func_name, |
| 985 | .get_function_groups = rockchip_pmx_get_groups, |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 986 | .set_mux = rockchip_pmx_set, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 987 | .gpio_set_direction = rockchip_pmx_gpio_set_direction, |
| 988 | }; |
| 989 | |
| 990 | /* |
| 991 | * Pinconf_ops handling |
| 992 | */ |
| 993 | |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 994 | static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl, |
| 995 | enum pin_config_param pull) |
| 996 | { |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 997 | switch (ctrl->type) { |
| 998 | case RK2928: |
| 999 | return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT || |
| 1000 | pull == PIN_CONFIG_BIAS_DISABLE); |
| 1001 | case RK3066B: |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1002 | return pull ? false : true; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1003 | case RK3188: |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 1004 | case RK3288: |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 1005 | case RK3368: |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1006 | return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT); |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1009 | return false; |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 1012 | static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value); |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1013 | static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset); |
| 1014 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1015 | /* set the pin config settings for a specified pin */ |
| 1016 | static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1017 | unsigned long *configs, unsigned num_configs) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1018 | { |
| 1019 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1020 | struct rockchip_pin_bank *bank = pin_to_bank(info, pin); |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1021 | enum pin_config_param param; |
| 1022 | u16 arg; |
| 1023 | int i; |
| 1024 | int rc; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1025 | |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1026 | for (i = 0; i < num_configs; i++) { |
| 1027 | param = pinconf_to_config_param(configs[i]); |
| 1028 | arg = pinconf_to_config_argument(configs[i]); |
| 1029 | |
| 1030 | switch (param) { |
| 1031 | case PIN_CONFIG_BIAS_DISABLE: |
| 1032 | rc = rockchip_set_pull(bank, pin - bank->pin_base, |
| 1033 | param); |
| 1034 | if (rc) |
| 1035 | return rc; |
| 1036 | break; |
| 1037 | case PIN_CONFIG_BIAS_PULL_UP: |
| 1038 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 1039 | case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1040 | case PIN_CONFIG_BIAS_BUS_HOLD: |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1041 | if (!rockchip_pinconf_pull_valid(info->ctrl, param)) |
| 1042 | return -ENOTSUPP; |
| 1043 | |
| 1044 | if (!arg) |
| 1045 | return -EINVAL; |
| 1046 | |
| 1047 | rc = rockchip_set_pull(bank, pin - bank->pin_base, |
| 1048 | param); |
| 1049 | if (rc) |
| 1050 | return rc; |
| 1051 | break; |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1052 | case PIN_CONFIG_OUTPUT: |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 1053 | rockchip_gpio_set(&bank->gpio_chip, |
| 1054 | pin - bank->pin_base, arg); |
| 1055 | rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip, |
| 1056 | pin - bank->pin_base, false); |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1057 | if (rc) |
| 1058 | return rc; |
| 1059 | break; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1060 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 1061 | /* rk3288 is the first with per-pin drive-strength */ |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1062 | if (!info->ctrl->drv_calc_reg) |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1063 | return -ENOTSUPP; |
| 1064 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1065 | rc = rockchip_set_drive_perpin(bank, |
| 1066 | pin - bank->pin_base, arg); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1067 | if (rc < 0) |
| 1068 | return rc; |
| 1069 | break; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1070 | default: |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1071 | return -ENOTSUPP; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1072 | break; |
| 1073 | } |
| 1074 | } /* for each config */ |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | /* get the pin config settings for a specified pin */ |
| 1080 | static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, |
| 1081 | unsigned long *config) |
| 1082 | { |
| 1083 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1084 | struct rockchip_pin_bank *bank = pin_to_bank(info, pin); |
| 1085 | enum pin_config_param param = pinconf_to_config_param(*config); |
Heiko Stübner | dab3eba | 2014-04-23 14:27:51 +0200 | [diff] [blame] | 1086 | u16 arg; |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1087 | int rc; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1088 | |
| 1089 | switch (param) { |
| 1090 | case PIN_CONFIG_BIAS_DISABLE: |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1091 | if (rockchip_get_pull(bank, pin - bank->pin_base) != param) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1092 | return -EINVAL; |
| 1093 | |
Heiko Stübner | dab3eba | 2014-04-23 14:27:51 +0200 | [diff] [blame] | 1094 | arg = 0; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1095 | break; |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1096 | case PIN_CONFIG_BIAS_PULL_UP: |
| 1097 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 1098 | case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1099 | case PIN_CONFIG_BIAS_BUS_HOLD: |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1100 | if (!rockchip_pinconf_pull_valid(info->ctrl, param)) |
| 1101 | return -ENOTSUPP; |
| 1102 | |
| 1103 | if (rockchip_get_pull(bank, pin - bank->pin_base) != param) |
| 1104 | return -EINVAL; |
| 1105 | |
Heiko Stübner | dab3eba | 2014-04-23 14:27:51 +0200 | [diff] [blame] | 1106 | arg = 1; |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1107 | break; |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1108 | case PIN_CONFIG_OUTPUT: |
| 1109 | rc = rockchip_get_mux(bank, pin - bank->pin_base); |
| 1110 | if (rc != RK_FUNC_GPIO) |
| 1111 | return -EINVAL; |
| 1112 | |
| 1113 | rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base); |
| 1114 | if (rc < 0) |
| 1115 | return rc; |
| 1116 | |
| 1117 | arg = rc ? 1 : 0; |
| 1118 | break; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1119 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 1120 | /* rk3288 is the first with per-pin drive-strength */ |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1121 | if (!info->ctrl->drv_calc_reg) |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1122 | return -ENOTSUPP; |
| 1123 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1124 | rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1125 | if (rc < 0) |
| 1126 | return rc; |
| 1127 | |
| 1128 | arg = rc; |
| 1129 | break; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1130 | default: |
| 1131 | return -ENOTSUPP; |
| 1132 | break; |
| 1133 | } |
| 1134 | |
Heiko Stübner | dab3eba | 2014-04-23 14:27:51 +0200 | [diff] [blame] | 1135 | *config = pinconf_to_config_packed(param, arg); |
| 1136 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | static const struct pinconf_ops rockchip_pinconf_ops = { |
| 1141 | .pin_config_get = rockchip_pinconf_get, |
| 1142 | .pin_config_set = rockchip_pinconf_set, |
Heiko Stübner | ed62f2f | 2014-07-20 01:48:45 +0200 | [diff] [blame] | 1143 | .is_generic = true, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1144 | }; |
| 1145 | |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 1146 | static const struct of_device_id rockchip_bank_match[] = { |
| 1147 | { .compatible = "rockchip,gpio-bank" }, |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1148 | { .compatible = "rockchip,rk3188-gpio-bank0" }, |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 1149 | {}, |
| 1150 | }; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1151 | |
| 1152 | static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info, |
| 1153 | struct device_node *np) |
| 1154 | { |
| 1155 | struct device_node *child; |
| 1156 | |
| 1157 | for_each_child_of_node(np, child) { |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 1158 | if (of_match_node(rockchip_bank_match, child)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1159 | continue; |
| 1160 | |
| 1161 | info->nfunctions++; |
| 1162 | info->ngroups += of_get_child_count(child); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | static int rockchip_pinctrl_parse_groups(struct device_node *np, |
| 1167 | struct rockchip_pin_group *grp, |
| 1168 | struct rockchip_pinctrl *info, |
| 1169 | u32 index) |
| 1170 | { |
| 1171 | struct rockchip_pin_bank *bank; |
| 1172 | int size; |
| 1173 | const __be32 *list; |
| 1174 | int num; |
| 1175 | int i, j; |
| 1176 | int ret; |
| 1177 | |
| 1178 | dev_dbg(info->dev, "group(%d): %s\n", index, np->name); |
| 1179 | |
| 1180 | /* Initialise group */ |
| 1181 | grp->name = np->name; |
| 1182 | |
| 1183 | /* |
| 1184 | * the binding format is rockchip,pins = <bank pin mux CONFIG>, |
| 1185 | * do sanity check and calculate pins number |
| 1186 | */ |
| 1187 | list = of_get_property(np, "rockchip,pins", &size); |
| 1188 | /* we do not check return since it's safe node passed down */ |
| 1189 | size /= sizeof(*list); |
| 1190 | if (!size || size % 4) { |
| 1191 | dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n"); |
| 1192 | return -EINVAL; |
| 1193 | } |
| 1194 | |
| 1195 | grp->npins = size / 4; |
| 1196 | |
| 1197 | grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int), |
| 1198 | GFP_KERNEL); |
| 1199 | grp->data = devm_kzalloc(info->dev, grp->npins * |
| 1200 | sizeof(struct rockchip_pin_config), |
| 1201 | GFP_KERNEL); |
| 1202 | if (!grp->pins || !grp->data) |
| 1203 | return -ENOMEM; |
| 1204 | |
| 1205 | for (i = 0, j = 0; i < size; i += 4, j++) { |
| 1206 | const __be32 *phandle; |
| 1207 | struct device_node *np_config; |
| 1208 | |
| 1209 | num = be32_to_cpu(*list++); |
| 1210 | bank = bank_num_to_bank(info, num); |
| 1211 | if (IS_ERR(bank)) |
| 1212 | return PTR_ERR(bank); |
| 1213 | |
| 1214 | grp->pins[j] = bank->pin_base + be32_to_cpu(*list++); |
| 1215 | grp->data[j].func = be32_to_cpu(*list++); |
| 1216 | |
| 1217 | phandle = list++; |
| 1218 | if (!phandle) |
| 1219 | return -EINVAL; |
| 1220 | |
| 1221 | np_config = of_find_node_by_phandle(be32_to_cpup(phandle)); |
Soren Brinkmann | dd4d01f | 2015-01-09 07:43:46 -0800 | [diff] [blame] | 1222 | ret = pinconf_generic_parse_dt_config(np_config, NULL, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1223 | &grp->data[j].configs, &grp->data[j].nconfigs); |
| 1224 | if (ret) |
| 1225 | return ret; |
| 1226 | } |
| 1227 | |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static int rockchip_pinctrl_parse_functions(struct device_node *np, |
| 1232 | struct rockchip_pinctrl *info, |
| 1233 | u32 index) |
| 1234 | { |
| 1235 | struct device_node *child; |
| 1236 | struct rockchip_pmx_func *func; |
| 1237 | struct rockchip_pin_group *grp; |
| 1238 | int ret; |
| 1239 | static u32 grp_index; |
| 1240 | u32 i = 0; |
| 1241 | |
| 1242 | dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name); |
| 1243 | |
| 1244 | func = &info->functions[index]; |
| 1245 | |
| 1246 | /* Initialise function */ |
| 1247 | func->name = np->name; |
| 1248 | func->ngroups = of_get_child_count(np); |
| 1249 | if (func->ngroups <= 0) |
| 1250 | return 0; |
| 1251 | |
| 1252 | func->groups = devm_kzalloc(info->dev, |
| 1253 | func->ngroups * sizeof(char *), GFP_KERNEL); |
| 1254 | if (!func->groups) |
| 1255 | return -ENOMEM; |
| 1256 | |
| 1257 | for_each_child_of_node(np, child) { |
| 1258 | func->groups[i] = child->name; |
| 1259 | grp = &info->groups[grp_index++]; |
| 1260 | ret = rockchip_pinctrl_parse_groups(child, grp, info, i++); |
| 1261 | if (ret) |
| 1262 | return ret; |
| 1263 | } |
| 1264 | |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | static int rockchip_pinctrl_parse_dt(struct platform_device *pdev, |
| 1269 | struct rockchip_pinctrl *info) |
| 1270 | { |
| 1271 | struct device *dev = &pdev->dev; |
| 1272 | struct device_node *np = dev->of_node; |
| 1273 | struct device_node *child; |
| 1274 | int ret; |
| 1275 | int i; |
| 1276 | |
| 1277 | rockchip_pinctrl_child_count(info, np); |
| 1278 | |
| 1279 | dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions); |
| 1280 | dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups); |
| 1281 | |
| 1282 | info->functions = devm_kzalloc(dev, info->nfunctions * |
| 1283 | sizeof(struct rockchip_pmx_func), |
| 1284 | GFP_KERNEL); |
| 1285 | if (!info->functions) { |
| 1286 | dev_err(dev, "failed to allocate memory for function list\n"); |
| 1287 | return -EINVAL; |
| 1288 | } |
| 1289 | |
| 1290 | info->groups = devm_kzalloc(dev, info->ngroups * |
| 1291 | sizeof(struct rockchip_pin_group), |
| 1292 | GFP_KERNEL); |
| 1293 | if (!info->groups) { |
| 1294 | dev_err(dev, "failed allocate memory for ping group list\n"); |
| 1295 | return -EINVAL; |
| 1296 | } |
| 1297 | |
| 1298 | i = 0; |
| 1299 | |
| 1300 | for_each_child_of_node(np, child) { |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 1301 | if (of_match_node(rockchip_bank_match, child)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1302 | continue; |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 1303 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1304 | ret = rockchip_pinctrl_parse_functions(child, info, i++); |
| 1305 | if (ret) { |
| 1306 | dev_err(&pdev->dev, "failed to parse function\n"); |
| 1307 | return ret; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | static int rockchip_pinctrl_register(struct platform_device *pdev, |
| 1315 | struct rockchip_pinctrl *info) |
| 1316 | { |
| 1317 | struct pinctrl_desc *ctrldesc = &info->pctl; |
| 1318 | struct pinctrl_pin_desc *pindesc, *pdesc; |
| 1319 | struct rockchip_pin_bank *pin_bank; |
| 1320 | int pin, bank, ret; |
| 1321 | int k; |
| 1322 | |
| 1323 | ctrldesc->name = "rockchip-pinctrl"; |
| 1324 | ctrldesc->owner = THIS_MODULE; |
| 1325 | ctrldesc->pctlops = &rockchip_pctrl_ops; |
| 1326 | ctrldesc->pmxops = &rockchip_pmx_ops; |
| 1327 | ctrldesc->confops = &rockchip_pinconf_ops; |
| 1328 | |
| 1329 | pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) * |
| 1330 | info->ctrl->nr_pins, GFP_KERNEL); |
| 1331 | if (!pindesc) { |
| 1332 | dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n"); |
| 1333 | return -ENOMEM; |
| 1334 | } |
| 1335 | ctrldesc->pins = pindesc; |
| 1336 | ctrldesc->npins = info->ctrl->nr_pins; |
| 1337 | |
| 1338 | pdesc = pindesc; |
| 1339 | for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) { |
| 1340 | pin_bank = &info->ctrl->pin_banks[bank]; |
| 1341 | for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) { |
| 1342 | pdesc->number = k; |
| 1343 | pdesc->name = kasprintf(GFP_KERNEL, "%s-%d", |
| 1344 | pin_bank->name, pin); |
| 1345 | pdesc++; |
| 1346 | } |
| 1347 | } |
| 1348 | |
Doug Anderson | 0fb7dcb | 2014-10-21 10:47:34 -0700 | [diff] [blame] | 1349 | ret = rockchip_pinctrl_parse_dt(pdev, info); |
| 1350 | if (ret) |
| 1351 | return ret; |
| 1352 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1353 | info->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, info); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1354 | if (IS_ERR(info->pctl_dev)) { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1355 | dev_err(&pdev->dev, "could not register pinctrl driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1356 | return PTR_ERR(info->pctl_dev); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | for (bank = 0; bank < info->ctrl->nr_banks; ++bank) { |
| 1360 | pin_bank = &info->ctrl->pin_banks[bank]; |
| 1361 | pin_bank->grange.name = pin_bank->name; |
| 1362 | pin_bank->grange.id = bank; |
| 1363 | pin_bank->grange.pin_base = pin_bank->pin_base; |
| 1364 | pin_bank->grange.base = pin_bank->gpio_chip.base; |
| 1365 | pin_bank->grange.npins = pin_bank->gpio_chip.ngpio; |
| 1366 | pin_bank->grange.gc = &pin_bank->gpio_chip; |
| 1367 | pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange); |
| 1368 | } |
| 1369 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * GPIO handling |
| 1375 | */ |
| 1376 | |
Axel Lin | 0351c28 | 2013-08-27 22:30:17 +0800 | [diff] [blame] | 1377 | static int rockchip_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 1378 | { |
| 1379 | return pinctrl_request_gpio(chip->base + offset); |
| 1380 | } |
| 1381 | |
| 1382 | static void rockchip_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 1383 | { |
| 1384 | pinctrl_free_gpio(chip->base + offset); |
| 1385 | } |
| 1386 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1387 | static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value) |
| 1388 | { |
| 1389 | struct rockchip_pin_bank *bank = gc_to_pin_bank(gc); |
| 1390 | void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR; |
| 1391 | unsigned long flags; |
| 1392 | u32 data; |
| 1393 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1394 | clk_enable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1395 | spin_lock_irqsave(&bank->slock, flags); |
| 1396 | |
| 1397 | data = readl(reg); |
| 1398 | data &= ~BIT(offset); |
| 1399 | if (value) |
| 1400 | data |= BIT(offset); |
| 1401 | writel(data, reg); |
| 1402 | |
| 1403 | spin_unlock_irqrestore(&bank->slock, flags); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1404 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * Returns the level of the pin for input direction and setting of the DR |
| 1409 | * register for output gpios. |
| 1410 | */ |
| 1411 | static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 1412 | { |
| 1413 | struct rockchip_pin_bank *bank = gc_to_pin_bank(gc); |
| 1414 | u32 data; |
| 1415 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1416 | clk_enable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1417 | data = readl(bank->reg_base + GPIO_EXT_PORT); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1418 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1419 | data >>= offset; |
| 1420 | data &= 1; |
| 1421 | return data; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * gpiolib gpio_direction_input callback function. The setting of the pin |
| 1426 | * mux function as 'gpio input' will be handled by the pinctrl susbsystem |
| 1427 | * interface. |
| 1428 | */ |
| 1429 | static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 1430 | { |
| 1431 | return pinctrl_gpio_direction_input(gc->base + offset); |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * gpiolib gpio_direction_output callback function. The setting of the pin |
| 1436 | * mux function as 'gpio output' will be handled by the pinctrl susbsystem |
| 1437 | * interface. |
| 1438 | */ |
| 1439 | static int rockchip_gpio_direction_output(struct gpio_chip *gc, |
| 1440 | unsigned offset, int value) |
| 1441 | { |
| 1442 | rockchip_gpio_set(gc, offset, value); |
| 1443 | return pinctrl_gpio_direction_output(gc->base + offset); |
| 1444 | } |
| 1445 | |
| 1446 | /* |
| 1447 | * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin |
| 1448 | * and a virtual IRQ, if not already present. |
| 1449 | */ |
| 1450 | static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 1451 | { |
| 1452 | struct rockchip_pin_bank *bank = gc_to_pin_bank(gc); |
| 1453 | unsigned int virq; |
| 1454 | |
| 1455 | if (!bank->domain) |
| 1456 | return -ENXIO; |
| 1457 | |
| 1458 | virq = irq_create_mapping(bank->domain, offset); |
| 1459 | |
| 1460 | return (virq) ? : -ENXIO; |
| 1461 | } |
| 1462 | |
| 1463 | static const struct gpio_chip rockchip_gpiolib_chip = { |
Axel Lin | 0351c28 | 2013-08-27 22:30:17 +0800 | [diff] [blame] | 1464 | .request = rockchip_gpio_request, |
| 1465 | .free = rockchip_gpio_free, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1466 | .set = rockchip_gpio_set, |
| 1467 | .get = rockchip_gpio_get, |
| 1468 | .direction_input = rockchip_gpio_direction_input, |
| 1469 | .direction_output = rockchip_gpio_direction_output, |
| 1470 | .to_irq = rockchip_gpio_to_irq, |
| 1471 | .owner = THIS_MODULE, |
| 1472 | }; |
| 1473 | |
| 1474 | /* |
| 1475 | * Interrupt handling |
| 1476 | */ |
| 1477 | |
Thomas Gleixner | 415f748 | 2015-07-13 01:52:00 +0200 | [diff] [blame] | 1478 | static void rockchip_irq_demux(unsigned int __irq, struct irq_desc *desc) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1479 | { |
Jiang Liu | 5663bb2 | 2015-06-04 12:13:16 +0800 | [diff] [blame] | 1480 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 1481 | struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1482 | u32 pend; |
| 1483 | |
| 1484 | dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name); |
| 1485 | |
| 1486 | chained_irq_enter(chip, desc); |
| 1487 | |
| 1488 | pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS); |
| 1489 | |
| 1490 | while (pend) { |
Thomas Gleixner | 415f748 | 2015-07-13 01:52:00 +0200 | [diff] [blame] | 1491 | unsigned int irq, virq; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1492 | |
| 1493 | irq = __ffs(pend); |
| 1494 | pend &= ~BIT(irq); |
| 1495 | virq = irq_linear_revmap(bank->domain, irq); |
| 1496 | |
| 1497 | if (!virq) { |
| 1498 | dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq); |
| 1499 | continue; |
| 1500 | } |
| 1501 | |
| 1502 | dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq); |
| 1503 | |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1504 | /* |
| 1505 | * Triggering IRQ on both rising and falling edge |
| 1506 | * needs manual intervention. |
| 1507 | */ |
| 1508 | if (bank->toggle_edge_mode & BIT(irq)) { |
Doug Anderson | 53b1bfc | 2014-12-22 10:47:29 -0800 | [diff] [blame] | 1509 | u32 data, data_old, polarity; |
| 1510 | unsigned long flags; |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1511 | |
Doug Anderson | 53b1bfc | 2014-12-22 10:47:29 -0800 | [diff] [blame] | 1512 | data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT); |
| 1513 | do { |
| 1514 | spin_lock_irqsave(&bank->slock, flags); |
| 1515 | |
| 1516 | polarity = readl_relaxed(bank->reg_base + |
| 1517 | GPIO_INT_POLARITY); |
| 1518 | if (data & BIT(irq)) |
| 1519 | polarity &= ~BIT(irq); |
| 1520 | else |
| 1521 | polarity |= BIT(irq); |
| 1522 | writel(polarity, |
| 1523 | bank->reg_base + GPIO_INT_POLARITY); |
| 1524 | |
| 1525 | spin_unlock_irqrestore(&bank->slock, flags); |
| 1526 | |
| 1527 | data_old = data; |
| 1528 | data = readl_relaxed(bank->reg_base + |
| 1529 | GPIO_EXT_PORT); |
| 1530 | } while ((data & BIT(irq)) != (data_old & BIT(irq))); |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1533 | generic_handle_irq(virq); |
| 1534 | } |
| 1535 | |
| 1536 | chained_irq_exit(chip, desc); |
| 1537 | } |
| 1538 | |
| 1539 | static int rockchip_irq_set_type(struct irq_data *d, unsigned int type) |
| 1540 | { |
| 1541 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 1542 | struct rockchip_pin_bank *bank = gc->private; |
| 1543 | u32 mask = BIT(d->hwirq); |
| 1544 | u32 polarity; |
| 1545 | u32 level; |
| 1546 | u32 data; |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1547 | unsigned long flags; |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 1548 | int ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1549 | |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1550 | /* make sure the pin is configured as gpio input */ |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 1551 | ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO); |
| 1552 | if (ret < 0) |
| 1553 | return ret; |
| 1554 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1555 | clk_enable(bank->clk); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1556 | spin_lock_irqsave(&bank->slock, flags); |
| 1557 | |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1558 | data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); |
| 1559 | data &= ~mask; |
| 1560 | writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR); |
| 1561 | |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1562 | spin_unlock_irqrestore(&bank->slock, flags); |
| 1563 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1564 | if (type & IRQ_TYPE_EDGE_BOTH) |
Thomas Gleixner | 2dbf1bc | 2015-06-23 15:52:50 +0200 | [diff] [blame] | 1565 | irq_set_handler_locked(d, handle_edge_irq); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1566 | else |
Thomas Gleixner | 2dbf1bc | 2015-06-23 15:52:50 +0200 | [diff] [blame] | 1567 | irq_set_handler_locked(d, handle_level_irq); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1568 | |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1569 | spin_lock_irqsave(&bank->slock, flags); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1570 | irq_gc_lock(gc); |
| 1571 | |
| 1572 | level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL); |
| 1573 | polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY); |
| 1574 | |
| 1575 | switch (type) { |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1576 | case IRQ_TYPE_EDGE_BOTH: |
| 1577 | bank->toggle_edge_mode |= mask; |
| 1578 | level |= mask; |
| 1579 | |
| 1580 | /* |
| 1581 | * Determine gpio state. If 1 next interrupt should be falling |
| 1582 | * otherwise rising. |
| 1583 | */ |
| 1584 | data = readl(bank->reg_base + GPIO_EXT_PORT); |
| 1585 | if (data & mask) |
| 1586 | polarity &= ~mask; |
| 1587 | else |
| 1588 | polarity |= mask; |
| 1589 | break; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1590 | case IRQ_TYPE_EDGE_RISING: |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1591 | bank->toggle_edge_mode &= ~mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1592 | level |= mask; |
| 1593 | polarity |= mask; |
| 1594 | break; |
| 1595 | case IRQ_TYPE_EDGE_FALLING: |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1596 | bank->toggle_edge_mode &= ~mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1597 | level |= mask; |
| 1598 | polarity &= ~mask; |
| 1599 | break; |
| 1600 | case IRQ_TYPE_LEVEL_HIGH: |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1601 | bank->toggle_edge_mode &= ~mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1602 | level &= ~mask; |
| 1603 | polarity |= mask; |
| 1604 | break; |
| 1605 | case IRQ_TYPE_LEVEL_LOW: |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 1606 | bank->toggle_edge_mode &= ~mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1607 | level &= ~mask; |
| 1608 | polarity &= ~mask; |
| 1609 | break; |
| 1610 | default: |
Axel Lin | 7cc5f97 | 2013-06-23 08:48:34 +0800 | [diff] [blame] | 1611 | irq_gc_unlock(gc); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1612 | spin_unlock_irqrestore(&bank->slock, flags); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1613 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1614 | return -EINVAL; |
| 1615 | } |
| 1616 | |
| 1617 | writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL); |
| 1618 | writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY); |
| 1619 | |
| 1620 | irq_gc_unlock(gc); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1621 | spin_unlock_irqrestore(&bank->slock, flags); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1622 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1623 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1624 | return 0; |
| 1625 | } |
| 1626 | |
Doug Anderson | 68bda47 | 2014-11-19 14:51:32 -0800 | [diff] [blame] | 1627 | static void rockchip_irq_suspend(struct irq_data *d) |
| 1628 | { |
| 1629 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 1630 | struct rockchip_pin_bank *bank = gc->private; |
| 1631 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1632 | clk_enable(bank->clk); |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 1633 | bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK); |
| 1634 | irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1635 | clk_disable(bank->clk); |
Doug Anderson | 68bda47 | 2014-11-19 14:51:32 -0800 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | static void rockchip_irq_resume(struct irq_data *d) |
| 1639 | { |
| 1640 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 1641 | struct rockchip_pin_bank *bank = gc->private; |
| 1642 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1643 | clk_enable(bank->clk); |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 1644 | irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1645 | clk_disable(bank->clk); |
| 1646 | } |
| 1647 | |
| 1648 | static void rockchip_irq_gc_mask_clr_bit(struct irq_data *d) |
| 1649 | { |
| 1650 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 1651 | struct rockchip_pin_bank *bank = gc->private; |
| 1652 | |
| 1653 | clk_enable(bank->clk); |
| 1654 | irq_gc_mask_clr_bit(d); |
| 1655 | } |
| 1656 | |
| 1657 | void rockchip_irq_gc_mask_set_bit(struct irq_data *d) |
| 1658 | { |
| 1659 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 1660 | struct rockchip_pin_bank *bank = gc->private; |
| 1661 | |
| 1662 | irq_gc_mask_set_bit(d); |
| 1663 | clk_disable(bank->clk); |
Doug Anderson | f2dd028 | 2014-11-19 14:51:33 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1666 | static int rockchip_interrupts_register(struct platform_device *pdev, |
| 1667 | struct rockchip_pinctrl *info) |
| 1668 | { |
| 1669 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 1670 | struct rockchip_pin_bank *bank = ctrl->pin_banks; |
| 1671 | unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN; |
| 1672 | struct irq_chip_generic *gc; |
| 1673 | int ret; |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1674 | int i, j; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1675 | |
| 1676 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
| 1677 | if (!bank->valid) { |
| 1678 | dev_warn(&pdev->dev, "bank %s is not valid\n", |
| 1679 | bank->name); |
| 1680 | continue; |
| 1681 | } |
| 1682 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1683 | ret = clk_enable(bank->clk); |
| 1684 | if (ret) { |
| 1685 | dev_err(&pdev->dev, "failed to enable clock for bank %s\n", |
| 1686 | bank->name); |
| 1687 | continue; |
| 1688 | } |
| 1689 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1690 | bank->domain = irq_domain_add_linear(bank->of_node, 32, |
| 1691 | &irq_generic_chip_ops, NULL); |
| 1692 | if (!bank->domain) { |
| 1693 | dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n", |
| 1694 | bank->name); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1695 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1696 | continue; |
| 1697 | } |
| 1698 | |
| 1699 | ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1, |
| 1700 | "rockchip_gpio_irq", handle_level_irq, |
| 1701 | clr, 0, IRQ_GC_INIT_MASK_CACHE); |
| 1702 | if (ret) { |
| 1703 | dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n", |
| 1704 | bank->name); |
| 1705 | irq_domain_remove(bank->domain); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1706 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1707 | continue; |
| 1708 | } |
| 1709 | |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 1710 | /* |
| 1711 | * Linux assumes that all interrupts start out disabled/masked. |
| 1712 | * Our driver only uses the concept of masked and always keeps |
| 1713 | * things enabled, so for us that's all masked and all enabled. |
| 1714 | */ |
| 1715 | writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK); |
| 1716 | writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN); |
| 1717 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1718 | gc = irq_get_domain_generic_chip(bank->domain, 0); |
| 1719 | gc->reg_base = bank->reg_base; |
| 1720 | gc->private = bank; |
Doug Anderson | f2dd028 | 2014-11-19 14:51:33 -0800 | [diff] [blame] | 1721 | gc->chip_types[0].regs.mask = GPIO_INTMASK; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1722 | gc->chip_types[0].regs.ack = GPIO_PORTS_EOI; |
| 1723 | gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit; |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1724 | gc->chip_types[0].chip.irq_mask = rockchip_irq_gc_mask_set_bit; |
| 1725 | gc->chip_types[0].chip.irq_unmask = |
| 1726 | rockchip_irq_gc_mask_clr_bit; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1727 | gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake; |
Doug Anderson | 68bda47 | 2014-11-19 14:51:32 -0800 | [diff] [blame] | 1728 | gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend; |
| 1729 | gc->chip_types[0].chip.irq_resume = rockchip_irq_resume; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1730 | gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type; |
Doug Anderson | 876d716 | 2014-10-21 10:47:32 -0700 | [diff] [blame] | 1731 | gc->wake_enabled = IRQ_MSK(bank->nr_pins); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1732 | |
Thomas Gleixner | 03051bc | 2015-06-21 21:11:06 +0200 | [diff] [blame] | 1733 | irq_set_chained_handler_and_data(bank->irq, |
| 1734 | rockchip_irq_demux, bank); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1735 | |
| 1736 | /* map the gpio irqs here, when the clock is still running */ |
| 1737 | for (j = 0 ; j < 32 ; j++) |
| 1738 | irq_create_mapping(bank->domain, j); |
| 1739 | |
| 1740 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | static int rockchip_gpiolib_register(struct platform_device *pdev, |
| 1747 | struct rockchip_pinctrl *info) |
| 1748 | { |
| 1749 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 1750 | struct rockchip_pin_bank *bank = ctrl->pin_banks; |
| 1751 | struct gpio_chip *gc; |
| 1752 | int ret; |
| 1753 | int i; |
| 1754 | |
| 1755 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
| 1756 | if (!bank->valid) { |
| 1757 | dev_warn(&pdev->dev, "bank %s is not valid\n", |
| 1758 | bank->name); |
| 1759 | continue; |
| 1760 | } |
| 1761 | |
| 1762 | bank->gpio_chip = rockchip_gpiolib_chip; |
| 1763 | |
| 1764 | gc = &bank->gpio_chip; |
| 1765 | gc->base = bank->pin_base; |
| 1766 | gc->ngpio = bank->nr_pins; |
| 1767 | gc->dev = &pdev->dev; |
| 1768 | gc->of_node = bank->of_node; |
| 1769 | gc->label = bank->name; |
| 1770 | |
| 1771 | ret = gpiochip_add(gc); |
| 1772 | if (ret) { |
| 1773 | dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n", |
| 1774 | gc->label, ret); |
| 1775 | goto fail; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | rockchip_interrupts_register(pdev, info); |
| 1780 | |
| 1781 | return 0; |
| 1782 | |
| 1783 | fail: |
| 1784 | for (--i, --bank; i >= 0; --i, --bank) { |
| 1785 | if (!bank->valid) |
| 1786 | continue; |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 1787 | gpiochip_remove(&bank->gpio_chip); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1788 | } |
| 1789 | return ret; |
| 1790 | } |
| 1791 | |
| 1792 | static int rockchip_gpiolib_unregister(struct platform_device *pdev, |
| 1793 | struct rockchip_pinctrl *info) |
| 1794 | { |
| 1795 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 1796 | struct rockchip_pin_bank *bank = ctrl->pin_banks; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1797 | int i; |
| 1798 | |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 1799 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1800 | if (!bank->valid) |
| 1801 | continue; |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 1802 | gpiochip_remove(&bank->gpio_chip); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1803 | } |
| 1804 | |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 1805 | return 0; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | static int rockchip_get_bank_data(struct rockchip_pin_bank *bank, |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 1809 | struct rockchip_pinctrl *info) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1810 | { |
| 1811 | struct resource res; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1812 | void __iomem *base; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1813 | |
| 1814 | if (of_address_to_resource(bank->of_node, 0, &res)) { |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 1815 | dev_err(info->dev, "cannot find IO resource for bank\n"); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1816 | return -ENOENT; |
| 1817 | } |
| 1818 | |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 1819 | bank->reg_base = devm_ioremap_resource(info->dev, &res); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1820 | if (IS_ERR(bank->reg_base)) |
| 1821 | return PTR_ERR(bank->reg_base); |
| 1822 | |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1823 | /* |
| 1824 | * special case, where parts of the pull setting-registers are |
| 1825 | * part of the PMU register space |
| 1826 | */ |
| 1827 | if (of_device_is_compatible(bank->of_node, |
| 1828 | "rockchip,rk3188-gpio-bank0")) { |
Heiko Stübner | a658efa | 2014-05-05 13:59:30 +0200 | [diff] [blame] | 1829 | struct device_node *node; |
Heiko Stübner | bfc7a42 | 2014-05-05 13:58:00 +0200 | [diff] [blame] | 1830 | |
Heiko Stübner | a658efa | 2014-05-05 13:59:30 +0200 | [diff] [blame] | 1831 | node = of_parse_phandle(bank->of_node->parent, |
| 1832 | "rockchip,pmu", 0); |
| 1833 | if (!node) { |
| 1834 | if (of_address_to_resource(bank->of_node, 1, &res)) { |
| 1835 | dev_err(info->dev, "cannot find IO resource for bank\n"); |
| 1836 | return -ENOENT; |
| 1837 | } |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1838 | |
Heiko Stübner | a658efa | 2014-05-05 13:59:30 +0200 | [diff] [blame] | 1839 | base = devm_ioremap_resource(info->dev, &res); |
| 1840 | if (IS_ERR(base)) |
| 1841 | return PTR_ERR(base); |
| 1842 | rockchip_regmap_config.max_register = |
| 1843 | resource_size(&res) - 4; |
| 1844 | rockchip_regmap_config.name = |
| 1845 | "rockchip,rk3188-gpio-bank0-pull"; |
| 1846 | bank->regmap_pull = devm_regmap_init_mmio(info->dev, |
| 1847 | base, |
| 1848 | &rockchip_regmap_config); |
| 1849 | } |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1850 | } |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 1851 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1852 | bank->irq = irq_of_parse_and_map(bank->of_node, 0); |
| 1853 | |
| 1854 | bank->clk = of_clk_get(bank->of_node, 0); |
| 1855 | if (IS_ERR(bank->clk)) |
| 1856 | return PTR_ERR(bank->clk); |
| 1857 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1858 | return clk_prepare(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | static const struct of_device_id rockchip_pinctrl_dt_match[]; |
| 1862 | |
| 1863 | /* retrieve the soc specific data */ |
| 1864 | static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data( |
| 1865 | struct rockchip_pinctrl *d, |
| 1866 | struct platform_device *pdev) |
| 1867 | { |
| 1868 | const struct of_device_id *match; |
| 1869 | struct device_node *node = pdev->dev.of_node; |
| 1870 | struct device_node *np; |
| 1871 | struct rockchip_pin_ctrl *ctrl; |
| 1872 | struct rockchip_pin_bank *bank; |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 1873 | int grf_offs, pmu_offs, i, j; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1874 | |
| 1875 | match = of_match_node(rockchip_pinctrl_dt_match, node); |
| 1876 | ctrl = (struct rockchip_pin_ctrl *)match->data; |
| 1877 | |
| 1878 | for_each_child_of_node(node, np) { |
| 1879 | if (!of_find_property(np, "gpio-controller", NULL)) |
| 1880 | continue; |
| 1881 | |
| 1882 | bank = ctrl->pin_banks; |
| 1883 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
| 1884 | if (!strcmp(bank->name, np->name)) { |
| 1885 | bank->of_node = np; |
| 1886 | |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 1887 | if (!rockchip_get_bank_data(bank, d)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1888 | bank->valid = true; |
| 1889 | |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 1895 | grf_offs = ctrl->grf_mux_offset; |
| 1896 | pmu_offs = ctrl->pmu_mux_offset; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1897 | bank = ctrl->pin_banks; |
| 1898 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 1899 | int bank_pins = 0; |
| 1900 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1901 | spin_lock_init(&bank->slock); |
| 1902 | bank->drvdata = d; |
| 1903 | bank->pin_base = ctrl->nr_pins; |
| 1904 | ctrl->nr_pins += bank->nr_pins; |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 1905 | |
| 1906 | /* calculate iomux offsets */ |
| 1907 | for (j = 0; j < 4; j++) { |
| 1908 | struct rockchip_iomux *iom = &bank->iomux[j]; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 1909 | int inc; |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 1910 | |
| 1911 | if (bank_pins >= bank->nr_pins) |
| 1912 | break; |
| 1913 | |
| 1914 | /* preset offset value, set new start value */ |
| 1915 | if (iom->offset >= 0) { |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 1916 | if (iom->type & IOMUX_SOURCE_PMU) |
| 1917 | pmu_offs = iom->offset; |
| 1918 | else |
| 1919 | grf_offs = iom->offset; |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 1920 | } else { /* set current offset */ |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 1921 | iom->offset = (iom->type & IOMUX_SOURCE_PMU) ? |
| 1922 | pmu_offs : grf_offs; |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | dev_dbg(d->dev, "bank %d, iomux %d has offset 0x%x\n", |
| 1926 | i, j, iom->offset); |
| 1927 | |
| 1928 | /* |
| 1929 | * Increase offset according to iomux width. |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 1930 | * 4bit iomux'es are spread over two registers. |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 1931 | */ |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 1932 | inc = (iom->type & IOMUX_WIDTH_4BIT) ? 8 : 4; |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 1933 | if (iom->type & IOMUX_SOURCE_PMU) |
| 1934 | pmu_offs += inc; |
| 1935 | else |
| 1936 | grf_offs += inc; |
Heiko Stübner | 6bc0d121 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 1937 | |
| 1938 | bank_pins += 8; |
| 1939 | } |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | return ctrl; |
| 1943 | } |
| 1944 | |
Chris Zhong | 8dca933 | 2014-10-29 19:52:00 +0800 | [diff] [blame] | 1945 | #define RK3288_GRF_GPIO6C_IOMUX 0x64 |
| 1946 | #define GPIO6C6_SEL_WRITE_ENABLE BIT(28) |
| 1947 | |
| 1948 | static u32 rk3288_grf_gpio6c_iomux; |
| 1949 | |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 1950 | static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev) |
| 1951 | { |
| 1952 | struct rockchip_pinctrl *info = dev_get_drvdata(dev); |
Chris Zhong | 8dca933 | 2014-10-29 19:52:00 +0800 | [diff] [blame] | 1953 | int ret = pinctrl_force_sleep(info->pctl_dev); |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 1954 | |
Chris Zhong | 8dca933 | 2014-10-29 19:52:00 +0800 | [diff] [blame] | 1955 | if (ret) |
| 1956 | return ret; |
| 1957 | |
| 1958 | /* |
| 1959 | * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save |
| 1960 | * the setting here, and restore it at resume. |
| 1961 | */ |
| 1962 | if (info->ctrl->type == RK3288) { |
| 1963 | ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX, |
| 1964 | &rk3288_grf_gpio6c_iomux); |
| 1965 | if (ret) { |
| 1966 | pinctrl_force_default(info->pctl_dev); |
| 1967 | return ret; |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | return 0; |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | static int __maybe_unused rockchip_pinctrl_resume(struct device *dev) |
| 1975 | { |
| 1976 | struct rockchip_pinctrl *info = dev_get_drvdata(dev); |
Chris Zhong | 8dca933 | 2014-10-29 19:52:00 +0800 | [diff] [blame] | 1977 | int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX, |
| 1978 | rk3288_grf_gpio6c_iomux | |
| 1979 | GPIO6C6_SEL_WRITE_ENABLE); |
| 1980 | |
| 1981 | if (ret) |
| 1982 | return ret; |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 1983 | |
| 1984 | return pinctrl_force_default(info->pctl_dev); |
| 1985 | } |
| 1986 | |
| 1987 | static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend, |
| 1988 | rockchip_pinctrl_resume); |
| 1989 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1990 | static int rockchip_pinctrl_probe(struct platform_device *pdev) |
| 1991 | { |
| 1992 | struct rockchip_pinctrl *info; |
| 1993 | struct device *dev = &pdev->dev; |
| 1994 | struct rockchip_pin_ctrl *ctrl; |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 1995 | struct device_node *np = pdev->dev.of_node, *node; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1996 | struct resource *res; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1997 | void __iomem *base; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1998 | int ret; |
| 1999 | |
| 2000 | if (!dev->of_node) { |
| 2001 | dev_err(dev, "device tree node not found\n"); |
| 2002 | return -ENODEV; |
| 2003 | } |
| 2004 | |
| 2005 | info = devm_kzalloc(dev, sizeof(struct rockchip_pinctrl), GFP_KERNEL); |
| 2006 | if (!info) |
| 2007 | return -ENOMEM; |
| 2008 | |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 2009 | info->dev = dev; |
| 2010 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2011 | ctrl = rockchip_pinctrl_get_soc_data(info, pdev); |
| 2012 | if (!ctrl) { |
| 2013 | dev_err(dev, "driver data not available\n"); |
| 2014 | return -EINVAL; |
| 2015 | } |
| 2016 | info->ctrl = ctrl; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2017 | |
Heiko Stübner | 1e747e5 | 2014-05-05 13:59:51 +0200 | [diff] [blame] | 2018 | node = of_parse_phandle(np, "rockchip,grf", 0); |
| 2019 | if (node) { |
| 2020 | info->regmap_base = syscon_node_to_regmap(node); |
| 2021 | if (IS_ERR(info->regmap_base)) |
| 2022 | return PTR_ERR(info->regmap_base); |
| 2023 | } else { |
| 2024 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 2025 | base = devm_ioremap_resource(&pdev->dev, res); |
| 2026 | if (IS_ERR(base)) |
| 2027 | return PTR_ERR(base); |
| 2028 | |
| 2029 | rockchip_regmap_config.max_register = resource_size(res) - 4; |
Heiko Stübner | 1e747e5 | 2014-05-05 13:59:51 +0200 | [diff] [blame] | 2030 | rockchip_regmap_config.name = "rockchip,pinctrl"; |
| 2031 | info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base, |
| 2032 | &rockchip_regmap_config); |
| 2033 | |
| 2034 | /* to check for the old dt-bindings */ |
| 2035 | info->reg_size = resource_size(res); |
| 2036 | |
| 2037 | /* Honor the old binding, with pull registers as 2nd resource */ |
| 2038 | if (ctrl->type == RK3188 && info->reg_size < 0x200) { |
| 2039 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 2040 | base = devm_ioremap_resource(&pdev->dev, res); |
| 2041 | if (IS_ERR(base)) |
| 2042 | return PTR_ERR(base); |
| 2043 | |
| 2044 | rockchip_regmap_config.max_register = |
| 2045 | resource_size(res) - 4; |
| 2046 | rockchip_regmap_config.name = "rockchip,pinctrl-pull"; |
| 2047 | info->regmap_pull = devm_regmap_init_mmio(&pdev->dev, |
| 2048 | base, |
| 2049 | &rockchip_regmap_config); |
| 2050 | } |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 2051 | } |
| 2052 | |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 2053 | /* try to find the optional reference to the pmu syscon */ |
| 2054 | node = of_parse_phandle(np, "rockchip,pmu", 0); |
| 2055 | if (node) { |
| 2056 | info->regmap_pmu = syscon_node_to_regmap(node); |
| 2057 | if (IS_ERR(info->regmap_pmu)) |
| 2058 | return PTR_ERR(info->regmap_pmu); |
| 2059 | } |
| 2060 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2061 | ret = rockchip_gpiolib_register(pdev, info); |
| 2062 | if (ret) |
| 2063 | return ret; |
| 2064 | |
| 2065 | ret = rockchip_pinctrl_register(pdev, info); |
| 2066 | if (ret) { |
| 2067 | rockchip_gpiolib_unregister(pdev, info); |
| 2068 | return ret; |
| 2069 | } |
| 2070 | |
| 2071 | platform_set_drvdata(pdev, info); |
| 2072 | |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
| 2076 | static struct rockchip_pin_bank rk2928_pin_banks[] = { |
| 2077 | PIN_BANK(0, 32, "gpio0"), |
| 2078 | PIN_BANK(1, 32, "gpio1"), |
| 2079 | PIN_BANK(2, 32, "gpio2"), |
| 2080 | PIN_BANK(3, 32, "gpio3"), |
| 2081 | }; |
| 2082 | |
| 2083 | static struct rockchip_pin_ctrl rk2928_pin_ctrl = { |
| 2084 | .pin_banks = rk2928_pin_banks, |
| 2085 | .nr_banks = ARRAY_SIZE(rk2928_pin_banks), |
| 2086 | .label = "RK2928-GPIO", |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 2087 | .type = RK2928, |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 2088 | .grf_mux_offset = 0xa8, |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 2089 | .pull_calc_reg = rk2928_calc_pull_reg_and_bit, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2090 | }; |
| 2091 | |
| 2092 | static struct rockchip_pin_bank rk3066a_pin_banks[] = { |
| 2093 | PIN_BANK(0, 32, "gpio0"), |
| 2094 | PIN_BANK(1, 32, "gpio1"), |
| 2095 | PIN_BANK(2, 32, "gpio2"), |
| 2096 | PIN_BANK(3, 32, "gpio3"), |
| 2097 | PIN_BANK(4, 32, "gpio4"), |
| 2098 | PIN_BANK(6, 16, "gpio6"), |
| 2099 | }; |
| 2100 | |
| 2101 | static struct rockchip_pin_ctrl rk3066a_pin_ctrl = { |
| 2102 | .pin_banks = rk3066a_pin_banks, |
| 2103 | .nr_banks = ARRAY_SIZE(rk3066a_pin_banks), |
| 2104 | .label = "RK3066a-GPIO", |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 2105 | .type = RK2928, |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 2106 | .grf_mux_offset = 0xa8, |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 2107 | .pull_calc_reg = rk2928_calc_pull_reg_and_bit, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2108 | }; |
| 2109 | |
| 2110 | static struct rockchip_pin_bank rk3066b_pin_banks[] = { |
| 2111 | PIN_BANK(0, 32, "gpio0"), |
| 2112 | PIN_BANK(1, 32, "gpio1"), |
| 2113 | PIN_BANK(2, 32, "gpio2"), |
| 2114 | PIN_BANK(3, 32, "gpio3"), |
| 2115 | }; |
| 2116 | |
| 2117 | static struct rockchip_pin_ctrl rk3066b_pin_ctrl = { |
| 2118 | .pin_banks = rk3066b_pin_banks, |
| 2119 | .nr_banks = ARRAY_SIZE(rk3066b_pin_banks), |
| 2120 | .label = "RK3066b-GPIO", |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 2121 | .type = RK3066B, |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 2122 | .grf_mux_offset = 0x60, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2123 | }; |
| 2124 | |
| 2125 | static struct rockchip_pin_bank rk3188_pin_banks[] = { |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 2126 | PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0), |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2127 | PIN_BANK(1, 32, "gpio1"), |
| 2128 | PIN_BANK(2, 32, "gpio2"), |
| 2129 | PIN_BANK(3, 32, "gpio3"), |
| 2130 | }; |
| 2131 | |
| 2132 | static struct rockchip_pin_ctrl rk3188_pin_ctrl = { |
| 2133 | .pin_banks = rk3188_pin_banks, |
| 2134 | .nr_banks = ARRAY_SIZE(rk3188_pin_banks), |
| 2135 | .label = "RK3188-GPIO", |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 2136 | .type = RK3188, |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 2137 | .grf_mux_offset = 0x60, |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 2138 | .pull_calc_reg = rk3188_calc_pull_reg_and_bit, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2139 | }; |
| 2140 | |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 2141 | static struct rockchip_pin_bank rk3288_pin_banks[] = { |
| 2142 | PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU, |
| 2143 | IOMUX_SOURCE_PMU, |
| 2144 | IOMUX_SOURCE_PMU, |
| 2145 | IOMUX_UNROUTED |
| 2146 | ), |
| 2147 | PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED, |
| 2148 | IOMUX_UNROUTED, |
| 2149 | IOMUX_UNROUTED, |
| 2150 | 0 |
| 2151 | ), |
| 2152 | PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED), |
| 2153 | PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT), |
| 2154 | PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT, |
| 2155 | IOMUX_WIDTH_4BIT, |
| 2156 | 0, |
| 2157 | 0 |
| 2158 | ), |
| 2159 | PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED, |
| 2160 | 0, |
| 2161 | 0, |
| 2162 | IOMUX_UNROUTED |
| 2163 | ), |
| 2164 | PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED), |
| 2165 | PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0, |
| 2166 | 0, |
| 2167 | IOMUX_WIDTH_4BIT, |
| 2168 | IOMUX_UNROUTED |
| 2169 | ), |
| 2170 | PIN_BANK(8, 16, "gpio8"), |
| 2171 | }; |
| 2172 | |
| 2173 | static struct rockchip_pin_ctrl rk3288_pin_ctrl = { |
| 2174 | .pin_banks = rk3288_pin_banks, |
| 2175 | .nr_banks = ARRAY_SIZE(rk3288_pin_banks), |
| 2176 | .label = "RK3288-GPIO", |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 2177 | .type = RK3288, |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 2178 | .grf_mux_offset = 0x0, |
| 2179 | .pmu_mux_offset = 0x84, |
| 2180 | .pull_calc_reg = rk3288_calc_pull_reg_and_bit, |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 2181 | .drv_calc_reg = rk3288_calc_drv_reg_and_bit, |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 2182 | }; |
| 2183 | |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 2184 | static struct rockchip_pin_bank rk3368_pin_banks[] = { |
| 2185 | PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU, |
| 2186 | IOMUX_SOURCE_PMU, |
| 2187 | IOMUX_SOURCE_PMU, |
| 2188 | IOMUX_SOURCE_PMU |
| 2189 | ), |
| 2190 | PIN_BANK(1, 32, "gpio1"), |
| 2191 | PIN_BANK(2, 32, "gpio2"), |
| 2192 | PIN_BANK(3, 32, "gpio3"), |
| 2193 | }; |
| 2194 | |
| 2195 | static struct rockchip_pin_ctrl rk3368_pin_ctrl = { |
| 2196 | .pin_banks = rk3368_pin_banks, |
| 2197 | .nr_banks = ARRAY_SIZE(rk3368_pin_banks), |
| 2198 | .label = "RK3368-GPIO", |
| 2199 | .type = RK3368, |
| 2200 | .grf_mux_offset = 0x0, |
| 2201 | .pmu_mux_offset = 0x0, |
| 2202 | .pull_calc_reg = rk3368_calc_pull_reg_and_bit, |
| 2203 | .drv_calc_reg = rk3368_calc_drv_reg_and_bit, |
| 2204 | }; |
| 2205 | |
| 2206 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2207 | static const struct of_device_id rockchip_pinctrl_dt_match[] = { |
| 2208 | { .compatible = "rockchip,rk2928-pinctrl", |
| 2209 | .data = (void *)&rk2928_pin_ctrl }, |
| 2210 | { .compatible = "rockchip,rk3066a-pinctrl", |
| 2211 | .data = (void *)&rk3066a_pin_ctrl }, |
| 2212 | { .compatible = "rockchip,rk3066b-pinctrl", |
| 2213 | .data = (void *)&rk3066b_pin_ctrl }, |
| 2214 | { .compatible = "rockchip,rk3188-pinctrl", |
| 2215 | .data = (void *)&rk3188_pin_ctrl }, |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 2216 | { .compatible = "rockchip,rk3288-pinctrl", |
| 2217 | .data = (void *)&rk3288_pin_ctrl }, |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 2218 | { .compatible = "rockchip,rk3368-pinctrl", |
| 2219 | .data = (void *)&rk3368_pin_ctrl }, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2220 | {}, |
| 2221 | }; |
| 2222 | MODULE_DEVICE_TABLE(of, rockchip_pinctrl_dt_match); |
| 2223 | |
| 2224 | static struct platform_driver rockchip_pinctrl_driver = { |
| 2225 | .probe = rockchip_pinctrl_probe, |
| 2226 | .driver = { |
| 2227 | .name = "rockchip-pinctrl", |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 2228 | .pm = &rockchip_pinctrl_dev_pm_ops, |
Axel Lin | 0be9e70 | 2013-08-23 14:27:53 +0800 | [diff] [blame] | 2229 | .of_match_table = rockchip_pinctrl_dt_match, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2230 | }, |
| 2231 | }; |
| 2232 | |
| 2233 | static int __init rockchip_pinctrl_drv_register(void) |
| 2234 | { |
| 2235 | return platform_driver_register(&rockchip_pinctrl_driver); |
| 2236 | } |
| 2237 | postcore_initcall(rockchip_pinctrl_drv_register); |
| 2238 | |
| 2239 | MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>"); |
| 2240 | MODULE_DESCRIPTION("Rockchip pinctrl driver"); |
| 2241 | MODULE_LICENSE("GPL v2"); |