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 | |
Paul Gortmaker | 2f43620 | 2016-08-23 17:19:42 -0400 | [diff] [blame] | 26 | #include <linux/init.h> |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 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 { |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 62 | RV1108, |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 63 | RK2928, |
| 64 | RK3066B, |
| 65 | RK3188, |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 66 | RK3288, |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 67 | RK3368, |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 68 | RK3399, |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 71 | /** |
| 72 | * Encode variants of iomux registers into a type variable |
| 73 | */ |
| 74 | #define IOMUX_GPIO_ONLY BIT(0) |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 75 | #define IOMUX_WIDTH_4BIT BIT(1) |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 76 | #define IOMUX_SOURCE_PMU BIT(2) |
Heiko Stübner | 62f4922 | 2014-06-16 01:37:49 +0200 | [diff] [blame] | 77 | #define IOMUX_UNROUTED BIT(3) |
david.wu | 8b6c6f9 | 2017-02-10 18:23:47 +0800 | [diff] [blame] | 78 | #define IOMUX_WIDTH_3BIT BIT(4) |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 79 | #define IOMUX_RECALCED BIT(5) |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * @type: iomux variant using IOMUX_* constants |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 83 | * @offset: if initialized to -1 it will be autocalculated, by specifying |
| 84 | * an initial offset value the relevant source offset can be reset |
| 85 | * to a new value for autocalculating the following iomux registers. |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 86 | */ |
| 87 | struct rockchip_iomux { |
| 88 | int type; |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 89 | int offset; |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 92 | /** |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 93 | * enum type index corresponding to rockchip_perpin_drv_list arrays index. |
| 94 | */ |
| 95 | enum rockchip_pin_drv_type { |
| 96 | DRV_TYPE_IO_DEFAULT = 0, |
| 97 | DRV_TYPE_IO_1V8_OR_3V0, |
| 98 | DRV_TYPE_IO_1V8_ONLY, |
| 99 | DRV_TYPE_IO_1V8_3V0_AUTO, |
| 100 | DRV_TYPE_IO_3V3_ONLY, |
| 101 | DRV_TYPE_MAX |
| 102 | }; |
| 103 | |
| 104 | /** |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 105 | * enum type index corresponding to rockchip_pull_list arrays index. |
| 106 | */ |
| 107 | enum rockchip_pin_pull_type { |
| 108 | PULL_TYPE_IO_DEFAULT = 0, |
| 109 | PULL_TYPE_IO_1V8_ONLY, |
| 110 | PULL_TYPE_MAX |
| 111 | }; |
| 112 | |
| 113 | /** |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 114 | * @drv_type: drive strength variant using rockchip_perpin_drv_type |
| 115 | * @offset: if initialized to -1 it will be autocalculated, by specifying |
| 116 | * an initial offset value the relevant source offset can be reset |
| 117 | * to a new value for autocalculating the following drive strength |
| 118 | * registers. if used chips own cal_drv func instead to calculate |
| 119 | * registers offset, the variant could be ignored. |
| 120 | */ |
| 121 | struct rockchip_drv { |
| 122 | enum rockchip_pin_drv_type drv_type; |
| 123 | int offset; |
| 124 | }; |
| 125 | |
| 126 | /** |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 127 | * @reg_base: register base of the gpio bank |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 128 | * @reg_pull: optional separate register for additional pull settings |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 129 | * @clk: clock of the gpio bank |
| 130 | * @irq: interrupt of the gpio bank |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 131 | * @saved_masks: Saved content of GPIO_INTEN at suspend time. |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 132 | * @pin_base: first pin number |
| 133 | * @nr_pins: number of pins in this bank |
| 134 | * @name: name of the bank |
| 135 | * @bank_num: number of the bank, to account for holes |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 136 | * @iomux: array describing the 4 iomux sources of the bank |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 137 | * @drv: array describing the 4 drive strength sources of the bank |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 138 | * @pull_type: array describing the 4 pull type sources of the bank |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 139 | * @valid: are all necessary informations present |
| 140 | * @of_node: dt node of this bank |
| 141 | * @drvdata: common pinctrl basedata |
| 142 | * @domain: irqdomain of the gpio bank |
| 143 | * @gpio_chip: gpiolib chip |
| 144 | * @grange: gpio range |
| 145 | * @slock: spinlock for the gpio bank |
John Keeping | 88bb942 | 2017-03-23 10:59:31 +0000 | [diff] [blame] | 146 | * @irq_lock: bus lock for irq chip |
| 147 | * @new_irqs: newly configured irqs which must be muxed as GPIOs in |
| 148 | * irq_bus_sync_unlock() |
David Wu | bd35b9b | 2017-05-26 15:20:20 +0800 | [diff] [blame] | 149 | * @route_mask: bits describing the routing pins of per bank |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 150 | */ |
| 151 | struct rockchip_pin_bank { |
| 152 | void __iomem *reg_base; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 153 | struct regmap *regmap_pull; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 154 | struct clk *clk; |
| 155 | int irq; |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 156 | u32 saved_masks; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 157 | u32 pin_base; |
| 158 | u8 nr_pins; |
| 159 | char *name; |
| 160 | u8 bank_num; |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 161 | struct rockchip_iomux iomux[4]; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 162 | struct rockchip_drv drv[4]; |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 163 | enum rockchip_pin_pull_type pull_type[4]; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 164 | bool valid; |
| 165 | struct device_node *of_node; |
| 166 | struct rockchip_pinctrl *drvdata; |
| 167 | struct irq_domain *domain; |
| 168 | struct gpio_chip gpio_chip; |
| 169 | struct pinctrl_gpio_range grange; |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 170 | raw_spinlock_t slock; |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 171 | u32 toggle_edge_mode; |
John Keeping | 88bb942 | 2017-03-23 10:59:31 +0000 | [diff] [blame] | 172 | struct mutex irq_lock; |
| 173 | u32 new_irqs; |
David Wu | bd35b9b | 2017-05-26 15:20:20 +0800 | [diff] [blame] | 174 | u32 route_mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | #define PIN_BANK(id, pins, label) \ |
| 178 | { \ |
| 179 | .bank_num = id, \ |
| 180 | .nr_pins = pins, \ |
| 181 | .name = label, \ |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 182 | .iomux = { \ |
| 183 | { .offset = -1 }, \ |
| 184 | { .offset = -1 }, \ |
| 185 | { .offset = -1 }, \ |
| 186 | { .offset = -1 }, \ |
| 187 | }, \ |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 190 | #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \ |
| 191 | { \ |
| 192 | .bank_num = id, \ |
| 193 | .nr_pins = pins, \ |
| 194 | .name = label, \ |
| 195 | .iomux = { \ |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 196 | { .type = iom0, .offset = -1 }, \ |
| 197 | { .type = iom1, .offset = -1 }, \ |
| 198 | { .type = iom2, .offset = -1 }, \ |
| 199 | { .type = iom3, .offset = -1 }, \ |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 200 | }, \ |
| 201 | } |
| 202 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 203 | #define PIN_BANK_DRV_FLAGS(id, pins, label, type0, type1, type2, type3) \ |
| 204 | { \ |
| 205 | .bank_num = id, \ |
| 206 | .nr_pins = pins, \ |
| 207 | .name = label, \ |
| 208 | .iomux = { \ |
| 209 | { .offset = -1 }, \ |
| 210 | { .offset = -1 }, \ |
| 211 | { .offset = -1 }, \ |
| 212 | { .offset = -1 }, \ |
| 213 | }, \ |
| 214 | .drv = { \ |
| 215 | { .drv_type = type0, .offset = -1 }, \ |
| 216 | { .drv_type = type1, .offset = -1 }, \ |
| 217 | { .drv_type = type2, .offset = -1 }, \ |
| 218 | { .drv_type = type3, .offset = -1 }, \ |
| 219 | }, \ |
| 220 | } |
| 221 | |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 222 | #define PIN_BANK_DRV_FLAGS_PULL_FLAGS(id, pins, label, drv0, drv1, \ |
| 223 | drv2, drv3, pull0, pull1, \ |
| 224 | pull2, pull3) \ |
| 225 | { \ |
| 226 | .bank_num = id, \ |
| 227 | .nr_pins = pins, \ |
| 228 | .name = label, \ |
| 229 | .iomux = { \ |
| 230 | { .offset = -1 }, \ |
| 231 | { .offset = -1 }, \ |
| 232 | { .offset = -1 }, \ |
| 233 | { .offset = -1 }, \ |
| 234 | }, \ |
| 235 | .drv = { \ |
| 236 | { .drv_type = drv0, .offset = -1 }, \ |
| 237 | { .drv_type = drv1, .offset = -1 }, \ |
| 238 | { .drv_type = drv2, .offset = -1 }, \ |
| 239 | { .drv_type = drv3, .offset = -1 }, \ |
| 240 | }, \ |
| 241 | .pull_type[0] = pull0, \ |
| 242 | .pull_type[1] = pull1, \ |
| 243 | .pull_type[2] = pull2, \ |
| 244 | .pull_type[3] = pull3, \ |
| 245 | } |
| 246 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 247 | #define PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(id, pins, label, iom0, iom1, \ |
| 248 | iom2, iom3, drv0, drv1, drv2, \ |
| 249 | drv3, offset0, offset1, \ |
| 250 | offset2, offset3) \ |
| 251 | { \ |
| 252 | .bank_num = id, \ |
| 253 | .nr_pins = pins, \ |
| 254 | .name = label, \ |
| 255 | .iomux = { \ |
| 256 | { .type = iom0, .offset = -1 }, \ |
| 257 | { .type = iom1, .offset = -1 }, \ |
| 258 | { .type = iom2, .offset = -1 }, \ |
| 259 | { .type = iom3, .offset = -1 }, \ |
| 260 | }, \ |
| 261 | .drv = { \ |
| 262 | { .drv_type = drv0, .offset = offset0 }, \ |
| 263 | { .drv_type = drv1, .offset = offset1 }, \ |
| 264 | { .drv_type = drv2, .offset = offset2 }, \ |
| 265 | { .drv_type = drv3, .offset = offset3 }, \ |
| 266 | }, \ |
| 267 | } |
| 268 | |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 269 | #define PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(id, pins, \ |
| 270 | label, iom0, iom1, iom2, \ |
| 271 | iom3, drv0, drv1, drv2, \ |
| 272 | drv3, offset0, offset1, \ |
| 273 | offset2, offset3, pull0, \ |
| 274 | pull1, pull2, pull3) \ |
| 275 | { \ |
| 276 | .bank_num = id, \ |
| 277 | .nr_pins = pins, \ |
| 278 | .name = label, \ |
| 279 | .iomux = { \ |
| 280 | { .type = iom0, .offset = -1 }, \ |
| 281 | { .type = iom1, .offset = -1 }, \ |
| 282 | { .type = iom2, .offset = -1 }, \ |
| 283 | { .type = iom3, .offset = -1 }, \ |
| 284 | }, \ |
| 285 | .drv = { \ |
| 286 | { .drv_type = drv0, .offset = offset0 }, \ |
| 287 | { .drv_type = drv1, .offset = offset1 }, \ |
| 288 | { .drv_type = drv2, .offset = offset2 }, \ |
| 289 | { .drv_type = drv3, .offset = offset3 }, \ |
| 290 | }, \ |
| 291 | .pull_type[0] = pull0, \ |
| 292 | .pull_type[1] = pull1, \ |
| 293 | .pull_type[2] = pull2, \ |
| 294 | .pull_type[3] = pull3, \ |
| 295 | } |
| 296 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 297 | /** |
David Wu | bd35b9b | 2017-05-26 15:20:20 +0800 | [diff] [blame] | 298 | * struct rockchip_mux_recalced_data: represent a pin iomux data. |
| 299 | * @bank_num: bank number. |
| 300 | * @pin: index at register or used to calc index. |
| 301 | * @func: the min pin. |
| 302 | * @route_offset: the max pin. |
| 303 | * @route_val: the register offset. |
| 304 | */ |
| 305 | struct rockchip_mux_route_data { |
| 306 | u8 bank_num; |
| 307 | u8 pin; |
| 308 | u8 func; |
| 309 | u32 route_offset; |
| 310 | u32 route_val; |
| 311 | }; |
| 312 | |
| 313 | /** |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 314 | */ |
| 315 | struct rockchip_pin_ctrl { |
| 316 | struct rockchip_pin_bank *pin_banks; |
| 317 | u32 nr_banks; |
| 318 | u32 nr_pins; |
| 319 | char *label; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 320 | enum rockchip_pinctrl_type type; |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 321 | int grf_mux_offset; |
| 322 | int pmu_mux_offset; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 323 | int grf_drv_offset; |
| 324 | int pmu_drv_offset; |
David Wu | bd35b9b | 2017-05-26 15:20:20 +0800 | [diff] [blame] | 325 | struct rockchip_mux_route_data *iomux_routes; |
| 326 | u32 niomux_routes; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 327 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 328 | void (*pull_calc_reg)(struct rockchip_pin_bank *bank, |
| 329 | int pin_num, struct regmap **regmap, |
| 330 | int *reg, u8 *bit); |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 331 | void (*drv_calc_reg)(struct rockchip_pin_bank *bank, |
| 332 | int pin_num, struct regmap **regmap, |
| 333 | int *reg, u8 *bit); |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 334 | void (*iomux_recalc)(u8 bank_num, int pin, int *reg, |
| 335 | u8 *bit, int *mask); |
david.wu | e3b357d | 2017-03-02 15:11:23 +0800 | [diff] [blame] | 336 | int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank, |
| 337 | int pin_num, struct regmap **regmap, |
| 338 | int *reg, u8 *bit); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 339 | }; |
| 340 | |
| 341 | struct rockchip_pin_config { |
| 342 | unsigned int func; |
| 343 | unsigned long *configs; |
| 344 | unsigned int nconfigs; |
| 345 | }; |
| 346 | |
| 347 | /** |
| 348 | * struct rockchip_pin_group: represent group of pins of a pinmux function. |
| 349 | * @name: name of the pin group, used to lookup the group. |
| 350 | * @pins: the pins included in this group. |
| 351 | * @npins: number of pins included in this group. |
| 352 | * @func: the mux function number to be programmed when selected. |
| 353 | * @configs: the config values to be set for each pin |
| 354 | * @nconfigs: number of configs for each pin |
| 355 | */ |
| 356 | struct rockchip_pin_group { |
| 357 | const char *name; |
| 358 | unsigned int npins; |
| 359 | unsigned int *pins; |
| 360 | struct rockchip_pin_config *data; |
| 361 | }; |
| 362 | |
| 363 | /** |
| 364 | * struct rockchip_pmx_func: represent a pin function. |
| 365 | * @name: name of the pin function, used to lookup the function. |
| 366 | * @groups: one or more names of pin groups that provide this function. |
| 367 | * @num_groups: number of groups included in @groups. |
| 368 | */ |
| 369 | struct rockchip_pmx_func { |
| 370 | const char *name; |
| 371 | const char **groups; |
| 372 | u8 ngroups; |
| 373 | }; |
| 374 | |
| 375 | struct rockchip_pinctrl { |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 376 | struct regmap *regmap_base; |
Heiko Stübner | bfc7a42 | 2014-05-05 13:58:00 +0200 | [diff] [blame] | 377 | int reg_size; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 378 | struct regmap *regmap_pull; |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 379 | struct regmap *regmap_pmu; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 380 | struct device *dev; |
| 381 | struct rockchip_pin_ctrl *ctrl; |
| 382 | struct pinctrl_desc pctl; |
| 383 | struct pinctrl_dev *pctl_dev; |
| 384 | struct rockchip_pin_group *groups; |
| 385 | unsigned int ngroups; |
| 386 | struct rockchip_pmx_func *functions; |
| 387 | unsigned int nfunctions; |
| 388 | }; |
| 389 | |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 390 | /** |
| 391 | * struct rockchip_mux_recalced_data: represent a pin iomux data. |
| 392 | * @num: bank number. |
| 393 | * @pin: pin number. |
| 394 | * @bit: index at register. |
| 395 | * @reg: register offset. |
| 396 | * @mask: mask bit |
| 397 | */ |
| 398 | struct rockchip_mux_recalced_data { |
| 399 | u8 num; |
| 400 | u8 pin; |
| 401 | u8 reg; |
| 402 | u8 bit; |
| 403 | u8 mask; |
| 404 | }; |
| 405 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 406 | static struct regmap_config rockchip_regmap_config = { |
| 407 | .reg_bits = 32, |
| 408 | .val_bits = 32, |
| 409 | .reg_stride = 4, |
| 410 | }; |
| 411 | |
Arnd Bergmann | 56411f3 | 2016-06-13 17:18:34 +0200 | [diff] [blame] | 412 | static inline const struct rockchip_pin_group *pinctrl_name_to_group( |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 413 | const struct rockchip_pinctrl *info, |
| 414 | const char *name) |
| 415 | { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 416 | int i; |
| 417 | |
| 418 | for (i = 0; i < info->ngroups; i++) { |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 419 | if (!strcmp(info->groups[i].name, name)) |
| 420 | return &info->groups[i]; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 423 | return NULL; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* |
| 427 | * given a pin number that is local to a pin controller, find out the pin bank |
| 428 | * and the register base of the pin bank. |
| 429 | */ |
| 430 | static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info, |
| 431 | unsigned pin) |
| 432 | { |
| 433 | struct rockchip_pin_bank *b = info->ctrl->pin_banks; |
| 434 | |
Axel Lin | 51578b9 | 2013-08-23 15:49:00 +0800 | [diff] [blame] | 435 | while (pin >= (b->pin_base + b->nr_pins)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 436 | b++; |
| 437 | |
| 438 | return b; |
| 439 | } |
| 440 | |
| 441 | static struct rockchip_pin_bank *bank_num_to_bank( |
| 442 | struct rockchip_pinctrl *info, |
| 443 | unsigned num) |
| 444 | { |
| 445 | struct rockchip_pin_bank *b = info->ctrl->pin_banks; |
| 446 | int i; |
| 447 | |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 448 | for (i = 0; i < info->ctrl->nr_banks; i++, b++) { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 449 | if (b->bank_num == num) |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 450 | return b; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Axel Lin | 1cb9539 | 2013-08-21 10:28:50 +0800 | [diff] [blame] | 453 | return ERR_PTR(-EINVAL); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Pinctrl_ops handling |
| 458 | */ |
| 459 | |
| 460 | static int rockchip_get_groups_count(struct pinctrl_dev *pctldev) |
| 461 | { |
| 462 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 463 | |
| 464 | return info->ngroups; |
| 465 | } |
| 466 | |
| 467 | static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev, |
| 468 | unsigned selector) |
| 469 | { |
| 470 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 471 | |
| 472 | return info->groups[selector].name; |
| 473 | } |
| 474 | |
| 475 | static int rockchip_get_group_pins(struct pinctrl_dev *pctldev, |
| 476 | unsigned selector, const unsigned **pins, |
| 477 | unsigned *npins) |
| 478 | { |
| 479 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 480 | |
| 481 | if (selector >= info->ngroups) |
| 482 | return -EINVAL; |
| 483 | |
| 484 | *pins = info->groups[selector].pins; |
| 485 | *npins = info->groups[selector].npins; |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 491 | struct device_node *np, |
| 492 | struct pinctrl_map **map, unsigned *num_maps) |
| 493 | { |
| 494 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 495 | const struct rockchip_pin_group *grp; |
| 496 | struct pinctrl_map *new_map; |
| 497 | struct device_node *parent; |
| 498 | int map_num = 1; |
| 499 | int i; |
| 500 | |
| 501 | /* |
| 502 | * first find the group of this node and check if we need to create |
| 503 | * config maps for pins |
| 504 | */ |
| 505 | grp = pinctrl_name_to_group(info, np->name); |
| 506 | if (!grp) { |
| 507 | dev_err(info->dev, "unable to find group for node %s\n", |
| 508 | np->name); |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
| 512 | map_num += grp->npins; |
| 513 | new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, |
| 514 | GFP_KERNEL); |
| 515 | if (!new_map) |
| 516 | return -ENOMEM; |
| 517 | |
| 518 | *map = new_map; |
| 519 | *num_maps = map_num; |
| 520 | |
| 521 | /* create mux map */ |
| 522 | parent = of_get_parent(np); |
| 523 | if (!parent) { |
| 524 | devm_kfree(pctldev->dev, new_map); |
| 525 | return -EINVAL; |
| 526 | } |
| 527 | new_map[0].type = PIN_MAP_TYPE_MUX_GROUP; |
| 528 | new_map[0].data.mux.function = parent->name; |
| 529 | new_map[0].data.mux.group = np->name; |
| 530 | of_node_put(parent); |
| 531 | |
| 532 | /* create config map */ |
| 533 | new_map++; |
| 534 | for (i = 0; i < grp->npins; i++) { |
| 535 | new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN; |
| 536 | new_map[i].data.configs.group_or_pin = |
| 537 | pin_get_name(pctldev, grp->pins[i]); |
| 538 | new_map[i].data.configs.configs = grp->data[i].configs; |
| 539 | new_map[i].data.configs.num_configs = grp->data[i].nconfigs; |
| 540 | } |
| 541 | |
| 542 | dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n", |
| 543 | (*map)->data.mux.function, (*map)->data.mux.group, map_num); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static void rockchip_dt_free_map(struct pinctrl_dev *pctldev, |
| 549 | struct pinctrl_map *map, unsigned num_maps) |
| 550 | { |
| 551 | } |
| 552 | |
| 553 | static const struct pinctrl_ops rockchip_pctrl_ops = { |
| 554 | .get_groups_count = rockchip_get_groups_count, |
| 555 | .get_group_name = rockchip_get_group_name, |
| 556 | .get_group_pins = rockchip_get_group_pins, |
| 557 | .dt_node_to_map = rockchip_dt_node_to_map, |
| 558 | .dt_free_map = rockchip_dt_free_map, |
| 559 | }; |
| 560 | |
| 561 | /* |
| 562 | * Hardware access |
| 563 | */ |
| 564 | |
david.wu | 3818e4a | 2017-02-10 18:23:49 +0800 | [diff] [blame] | 565 | static const struct rockchip_mux_recalced_data rk3328_mux_recalced_data[] = { |
| 566 | { |
| 567 | .num = 2, |
| 568 | .pin = 12, |
| 569 | .reg = 0x24, |
| 570 | .bit = 8, |
| 571 | .mask = 0x3 |
| 572 | }, { |
| 573 | .num = 2, |
| 574 | .pin = 15, |
| 575 | .reg = 0x28, |
| 576 | .bit = 0, |
| 577 | .mask = 0x7 |
| 578 | }, { |
| 579 | .num = 2, |
| 580 | .pin = 23, |
| 581 | .reg = 0x30, |
| 582 | .bit = 14, |
| 583 | .mask = 0x3 |
| 584 | }, |
| 585 | }; |
| 586 | |
| 587 | static void rk3328_recalc_mux(u8 bank_num, int pin, int *reg, |
| 588 | u8 *bit, int *mask) |
| 589 | { |
| 590 | const struct rockchip_mux_recalced_data *data = NULL; |
| 591 | int i; |
| 592 | |
| 593 | for (i = 0; i < ARRAY_SIZE(rk3328_mux_recalced_data); i++) |
| 594 | if (rk3328_mux_recalced_data[i].num == bank_num && |
| 595 | rk3328_mux_recalced_data[i].pin == pin) { |
| 596 | data = &rk3328_mux_recalced_data[i]; |
| 597 | break; |
| 598 | } |
| 599 | |
| 600 | if (!data) |
| 601 | return; |
| 602 | |
| 603 | *reg = data->reg; |
| 604 | *mask = data->mask; |
| 605 | *bit = data->bit; |
| 606 | } |
| 607 | |
David Wu | d4970ee | 2017-05-26 15:20:21 +0800 | [diff] [blame] | 608 | static struct rockchip_mux_route_data rk3228_mux_route_data[] = { |
| 609 | { |
| 610 | /* pwm0-0 */ |
| 611 | .bank_num = 0, |
| 612 | .pin = 26, |
| 613 | .func = 1, |
| 614 | .route_offset = 0x50, |
| 615 | .route_val = BIT(16), |
| 616 | }, { |
| 617 | /* pwm0-1 */ |
| 618 | .bank_num = 3, |
| 619 | .pin = 21, |
| 620 | .func = 1, |
| 621 | .route_offset = 0x50, |
| 622 | .route_val = BIT(16) | BIT(0), |
| 623 | }, { |
| 624 | /* pwm1-0 */ |
| 625 | .bank_num = 0, |
| 626 | .pin = 27, |
| 627 | .func = 1, |
| 628 | .route_offset = 0x50, |
| 629 | .route_val = BIT(16 + 1), |
| 630 | }, { |
| 631 | /* pwm1-1 */ |
| 632 | .bank_num = 0, |
| 633 | .pin = 30, |
| 634 | .func = 2, |
| 635 | .route_offset = 0x50, |
| 636 | .route_val = BIT(16 + 1) | BIT(1), |
| 637 | }, { |
| 638 | /* pwm2-0 */ |
| 639 | .bank_num = 0, |
| 640 | .pin = 28, |
| 641 | .func = 1, |
| 642 | .route_offset = 0x50, |
| 643 | .route_val = BIT(16 + 2), |
| 644 | }, { |
| 645 | /* pwm2-1 */ |
| 646 | .bank_num = 1, |
| 647 | .pin = 12, |
| 648 | .func = 2, |
| 649 | .route_offset = 0x50, |
| 650 | .route_val = BIT(16 + 2) | BIT(2), |
| 651 | }, { |
| 652 | /* pwm3-0 */ |
| 653 | .bank_num = 3, |
| 654 | .pin = 26, |
| 655 | .func = 1, |
| 656 | .route_offset = 0x50, |
| 657 | .route_val = BIT(16 + 3), |
| 658 | }, { |
| 659 | /* pwm3-1 */ |
| 660 | .bank_num = 1, |
| 661 | .pin = 11, |
| 662 | .func = 2, |
| 663 | .route_offset = 0x50, |
| 664 | .route_val = BIT(16 + 3) | BIT(3), |
| 665 | }, { |
| 666 | /* sdio-0_d0 */ |
| 667 | .bank_num = 1, |
| 668 | .pin = 1, |
| 669 | .func = 1, |
| 670 | .route_offset = 0x50, |
| 671 | .route_val = BIT(16 + 4), |
| 672 | }, { |
| 673 | /* sdio-1_d0 */ |
| 674 | .bank_num = 3, |
| 675 | .pin = 2, |
| 676 | .func = 1, |
| 677 | .route_offset = 0x50, |
| 678 | .route_val = BIT(16 + 4) | BIT(4), |
| 679 | }, { |
| 680 | /* spi-0_rx */ |
| 681 | .bank_num = 0, |
| 682 | .pin = 13, |
| 683 | .func = 2, |
| 684 | .route_offset = 0x50, |
| 685 | .route_val = BIT(16 + 5), |
| 686 | }, { |
| 687 | /* spi-1_rx */ |
| 688 | .bank_num = 2, |
| 689 | .pin = 0, |
| 690 | .func = 2, |
| 691 | .route_offset = 0x50, |
| 692 | .route_val = BIT(16 + 5) | BIT(5), |
| 693 | }, { |
| 694 | /* emmc-0_cmd */ |
| 695 | .bank_num = 1, |
| 696 | .pin = 22, |
| 697 | .func = 2, |
| 698 | .route_offset = 0x50, |
| 699 | .route_val = BIT(16 + 7), |
| 700 | }, { |
| 701 | /* emmc-1_cmd */ |
| 702 | .bank_num = 2, |
| 703 | .pin = 4, |
| 704 | .func = 2, |
| 705 | .route_offset = 0x50, |
| 706 | .route_val = BIT(16 + 7) | BIT(7), |
| 707 | }, { |
| 708 | /* uart2-0_rx */ |
| 709 | .bank_num = 1, |
| 710 | .pin = 19, |
| 711 | .func = 2, |
| 712 | .route_offset = 0x50, |
| 713 | .route_val = BIT(16 + 8), |
| 714 | }, { |
| 715 | /* uart2-1_rx */ |
| 716 | .bank_num = 1, |
| 717 | .pin = 10, |
| 718 | .func = 2, |
| 719 | .route_offset = 0x50, |
| 720 | .route_val = BIT(16 + 8) | BIT(8), |
| 721 | }, { |
| 722 | /* uart1-0_rx */ |
| 723 | .bank_num = 1, |
| 724 | .pin = 10, |
| 725 | .func = 1, |
| 726 | .route_offset = 0x50, |
| 727 | .route_val = BIT(16 + 11), |
| 728 | }, { |
| 729 | /* uart1-1_rx */ |
| 730 | .bank_num = 3, |
| 731 | .pin = 13, |
| 732 | .func = 1, |
| 733 | .route_offset = 0x50, |
| 734 | .route_val = BIT(16 + 11) | BIT(11), |
| 735 | }, |
| 736 | }; |
| 737 | |
David Wu | cedc964 | 2017-05-26 15:20:22 +0800 | [diff] [blame] | 738 | static struct rockchip_mux_route_data rk3328_mux_route_data[] = { |
| 739 | { |
| 740 | /* uart2dbg_rxm0 */ |
| 741 | .bank_num = 1, |
| 742 | .pin = 1, |
| 743 | .func = 2, |
| 744 | .route_offset = 0x50, |
| 745 | .route_val = BIT(16) | BIT(16 + 1), |
| 746 | }, { |
| 747 | /* uart2dbg_rxm1 */ |
| 748 | .bank_num = 2, |
| 749 | .pin = 1, |
| 750 | .func = 1, |
| 751 | .route_offset = 0x50, |
| 752 | .route_val = BIT(16) | BIT(16 + 1) | BIT(0), |
| 753 | }, { |
| 754 | /* gmac-m1-optimized_rxd0 */ |
| 755 | .bank_num = 1, |
| 756 | .pin = 11, |
| 757 | .func = 2, |
| 758 | .route_offset = 0x50, |
| 759 | .route_val = BIT(16 + 2) | BIT(16 + 10) | BIT(2) | BIT(10), |
| 760 | }, { |
| 761 | /* pdm_sdi0m0 */ |
| 762 | .bank_num = 2, |
| 763 | .pin = 19, |
| 764 | .func = 2, |
| 765 | .route_offset = 0x50, |
| 766 | .route_val = BIT(16 + 3), |
| 767 | }, { |
| 768 | /* pdm_sdi0m1 */ |
| 769 | .bank_num = 1, |
| 770 | .pin = 23, |
| 771 | .func = 3, |
| 772 | .route_offset = 0x50, |
| 773 | .route_val = BIT(16 + 3) | BIT(3), |
| 774 | }, { |
| 775 | /* spi_rxdm2 */ |
| 776 | .bank_num = 3, |
| 777 | .pin = 2, |
| 778 | .func = 4, |
| 779 | .route_offset = 0x50, |
| 780 | .route_val = BIT(16 + 4) | BIT(16 + 5) | BIT(5), |
| 781 | }, { |
| 782 | /* i2s2_sdim0 */ |
| 783 | .bank_num = 1, |
| 784 | .pin = 24, |
| 785 | .func = 1, |
| 786 | .route_offset = 0x50, |
| 787 | .route_val = BIT(16 + 6), |
| 788 | }, { |
| 789 | /* i2s2_sdim1 */ |
| 790 | .bank_num = 3, |
| 791 | .pin = 2, |
| 792 | .func = 6, |
| 793 | .route_offset = 0x50, |
| 794 | .route_val = BIT(16 + 6) | BIT(6), |
| 795 | }, { |
| 796 | /* card_iom1 */ |
| 797 | .bank_num = 2, |
| 798 | .pin = 22, |
| 799 | .func = 3, |
| 800 | .route_offset = 0x50, |
| 801 | .route_val = BIT(16 + 7) | BIT(7), |
| 802 | }, { |
| 803 | /* tsp_d5m1 */ |
| 804 | .bank_num = 2, |
| 805 | .pin = 16, |
| 806 | .func = 3, |
| 807 | .route_offset = 0x50, |
| 808 | .route_val = BIT(16 + 8) | BIT(8), |
| 809 | }, { |
| 810 | /* cif_data5m1 */ |
| 811 | .bank_num = 2, |
| 812 | .pin = 16, |
| 813 | .func = 4, |
| 814 | .route_offset = 0x50, |
| 815 | .route_val = BIT(16 + 9) | BIT(9), |
| 816 | }, |
| 817 | }; |
| 818 | |
David Wu | accc1ce | 2017-05-26 15:20:23 +0800 | [diff] [blame^] | 819 | static struct rockchip_mux_route_data rk3399_mux_route_data[] = { |
| 820 | { |
| 821 | /* uart2dbga_rx */ |
| 822 | .bank_num = 4, |
| 823 | .pin = 8, |
| 824 | .func = 2, |
| 825 | .route_offset = 0xe21c, |
| 826 | .route_val = BIT(16 + 10) | BIT(16 + 11), |
| 827 | }, { |
| 828 | /* uart2dbgb_rx */ |
| 829 | .bank_num = 4, |
| 830 | .pin = 16, |
| 831 | .func = 2, |
| 832 | .route_offset = 0xe21c, |
| 833 | .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10), |
| 834 | }, { |
| 835 | /* uart2dbgc_rx */ |
| 836 | .bank_num = 4, |
| 837 | .pin = 19, |
| 838 | .func = 1, |
| 839 | .route_offset = 0xe21c, |
| 840 | .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11), |
| 841 | }, { |
| 842 | /* pcie_clkreqn */ |
| 843 | .bank_num = 2, |
| 844 | .pin = 26, |
| 845 | .func = 2, |
| 846 | .route_offset = 0xe21c, |
| 847 | .route_val = BIT(16 + 14), |
| 848 | }, { |
| 849 | /* pcie_clkreqnb */ |
| 850 | .bank_num = 4, |
| 851 | .pin = 24, |
| 852 | .func = 1, |
| 853 | .route_offset = 0xe21c, |
| 854 | .route_val = BIT(16 + 14) | BIT(14), |
| 855 | }, |
| 856 | }; |
| 857 | |
David Wu | bd35b9b | 2017-05-26 15:20:20 +0800 | [diff] [blame] | 858 | static bool rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin, |
| 859 | int mux, u32 *reg, u32 *value) |
| 860 | { |
| 861 | struct rockchip_pinctrl *info = bank->drvdata; |
| 862 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 863 | struct rockchip_mux_route_data *data; |
| 864 | int i; |
| 865 | |
| 866 | for (i = 0; i < ctrl->niomux_routes; i++) { |
| 867 | data = &ctrl->iomux_routes[i]; |
| 868 | if ((data->bank_num == bank->bank_num) && |
| 869 | (data->pin == pin) && (data->func == mux)) |
| 870 | break; |
| 871 | } |
| 872 | |
| 873 | if (i >= ctrl->niomux_routes) |
| 874 | return false; |
| 875 | |
| 876 | *reg = data->route_offset; |
| 877 | *value = data->route_val; |
| 878 | |
| 879 | return true; |
| 880 | } |
| 881 | |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 882 | static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin) |
| 883 | { |
| 884 | struct rockchip_pinctrl *info = bank->drvdata; |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 885 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 886 | int iomux_num = (pin / 8); |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 887 | struct regmap *regmap; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 888 | unsigned int val; |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 889 | int reg, ret, mask, mux_type; |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 890 | u8 bit; |
| 891 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 892 | if (iomux_num > 3) |
| 893 | return -EINVAL; |
| 894 | |
Heiko Stübner | 62f4922 | 2014-06-16 01:37:49 +0200 | [diff] [blame] | 895 | if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) { |
| 896 | dev_err(info->dev, "pin %d is unrouted\n", pin); |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 900 | if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 901 | return RK_FUNC_GPIO; |
| 902 | |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 903 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 904 | ? info->regmap_pmu : info->regmap_base; |
| 905 | |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 906 | /* get basic quadrupel of mux registers and the correct reg inside */ |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 907 | mux_type = bank->iomux[iomux_num].type; |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 908 | reg = bank->iomux[iomux_num].offset; |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 909 | if (mux_type & IOMUX_WIDTH_4BIT) { |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 910 | if ((pin % 8) >= 4) |
| 911 | reg += 0x4; |
| 912 | bit = (pin % 4) * 4; |
david.wu | 8b6c6f9 | 2017-02-10 18:23:47 +0800 | [diff] [blame] | 913 | mask = 0xf; |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 914 | } else if (mux_type & IOMUX_WIDTH_3BIT) { |
david.wu | 8b6c6f9 | 2017-02-10 18:23:47 +0800 | [diff] [blame] | 915 | if ((pin % 8) >= 5) |
| 916 | reg += 0x4; |
| 917 | bit = (pin % 8 % 5) * 3; |
| 918 | mask = 0x7; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 919 | } else { |
| 920 | bit = (pin % 8) * 2; |
david.wu | 8b6c6f9 | 2017-02-10 18:23:47 +0800 | [diff] [blame] | 921 | mask = 0x3; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 922 | } |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 923 | |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 924 | if (ctrl->iomux_recalc && (mux_type & IOMUX_RECALCED)) |
| 925 | ctrl->iomux_recalc(bank->bank_num, pin, ®, &bit, &mask); |
| 926 | |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 927 | ret = regmap_read(regmap, reg, &val); |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 928 | if (ret) |
| 929 | return ret; |
| 930 | |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 931 | return ((val >> bit) & mask); |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 932 | } |
| 933 | |
John Keeping | 05709c3 | 2017-03-23 10:59:30 +0000 | [diff] [blame] | 934 | static int rockchip_verify_mux(struct rockchip_pin_bank *bank, |
| 935 | int pin, int mux) |
| 936 | { |
| 937 | struct rockchip_pinctrl *info = bank->drvdata; |
| 938 | int iomux_num = (pin / 8); |
| 939 | |
| 940 | if (iomux_num > 3) |
| 941 | return -EINVAL; |
| 942 | |
| 943 | if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) { |
| 944 | dev_err(info->dev, "pin %d is unrouted\n", pin); |
| 945 | return -EINVAL; |
| 946 | } |
| 947 | |
| 948 | if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) { |
| 949 | if (mux != RK_FUNC_GPIO) { |
| 950 | dev_err(info->dev, |
| 951 | "pin %d only supports a gpio mux\n", pin); |
| 952 | return -ENOTSUPP; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 959 | /* |
| 960 | * Set a new mux function for a pin. |
| 961 | * |
| 962 | * The register is divided into the upper and lower 16 bit. When changing |
| 963 | * a value, the previous register value is not read and changed. Instead |
| 964 | * it seems the changed bits are marked in the upper 16 bit, while the |
| 965 | * changed value gets set in the same offset in the lower 16 bit. |
| 966 | * All pin settings seem to be 2 bit wide in both the upper and lower |
| 967 | * parts. |
| 968 | * @bank: pin bank to change |
| 969 | * @pin: pin to change |
| 970 | * @mux: new mux function to set |
| 971 | */ |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 972 | 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] | 973 | { |
| 974 | struct rockchip_pinctrl *info = bank->drvdata; |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 975 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 976 | int iomux_num = (pin / 8); |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 977 | struct regmap *regmap; |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 978 | int reg, ret, mask, mux_type; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 979 | u8 bit; |
David Wu | bd35b9b | 2017-05-26 15:20:20 +0800 | [diff] [blame] | 980 | u32 data, rmask, route_reg, route_val; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 981 | |
John Keeping | 05709c3 | 2017-03-23 10:59:30 +0000 | [diff] [blame] | 982 | ret = rockchip_verify_mux(bank, pin, mux); |
| 983 | if (ret < 0) |
| 984 | return ret; |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 985 | |
John Keeping | 05709c3 | 2017-03-23 10:59:30 +0000 | [diff] [blame] | 986 | if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) |
| 987 | return 0; |
Heiko Stübner | c4a532de | 2014-03-26 00:57:52 +0100 | [diff] [blame] | 988 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 989 | dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n", |
| 990 | bank->bank_num, pin, mux); |
| 991 | |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 992 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 993 | ? info->regmap_pmu : info->regmap_base; |
| 994 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 995 | /* get basic quadrupel of mux registers and the correct reg inside */ |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 996 | mux_type = bank->iomux[iomux_num].type; |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 997 | reg = bank->iomux[iomux_num].offset; |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 998 | if (mux_type & IOMUX_WIDTH_4BIT) { |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 999 | if ((pin % 8) >= 4) |
| 1000 | reg += 0x4; |
| 1001 | bit = (pin % 4) * 4; |
david.wu | 8b6c6f9 | 2017-02-10 18:23:47 +0800 | [diff] [blame] | 1002 | mask = 0xf; |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 1003 | } else if (mux_type & IOMUX_WIDTH_3BIT) { |
david.wu | 8b6c6f9 | 2017-02-10 18:23:47 +0800 | [diff] [blame] | 1004 | if ((pin % 8) >= 5) |
| 1005 | reg += 0x4; |
| 1006 | bit = (pin % 8 % 5) * 3; |
| 1007 | mask = 0x7; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 1008 | } else { |
| 1009 | bit = (pin % 8) * 2; |
david.wu | 8b6c6f9 | 2017-02-10 18:23:47 +0800 | [diff] [blame] | 1010 | mask = 0x3; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 1011 | } |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1012 | |
david.wu | ea262ad | 2017-02-10 18:23:48 +0800 | [diff] [blame] | 1013 | if (ctrl->iomux_recalc && (mux_type & IOMUX_RECALCED)) |
| 1014 | ctrl->iomux_recalc(bank->bank_num, pin, ®, &bit, &mask); |
| 1015 | |
David Wu | bd35b9b | 2017-05-26 15:20:20 +0800 | [diff] [blame] | 1016 | if (bank->route_mask & BIT(pin)) { |
| 1017 | if (rockchip_get_mux_route(bank, pin, mux, &route_reg, |
| 1018 | &route_val)) { |
| 1019 | ret = regmap_write(regmap, route_reg, route_val); |
| 1020 | if (ret) |
| 1021 | return ret; |
| 1022 | } |
| 1023 | } |
| 1024 | |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 1025 | data = (mask << (bit + 16)); |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 1026 | rmask = data | (data >> 16); |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 1027 | data |= (mux & mask) << bit; |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 1028 | ret = regmap_update_bits(regmap, reg, rmask, data); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1029 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1030 | return ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1033 | #define RV1108_PULL_PMU_OFFSET 0x10 |
| 1034 | #define RV1108_PULL_OFFSET 0x110 |
| 1035 | #define RV1108_PULL_PINS_PER_REG 8 |
| 1036 | #define RV1108_PULL_BITS_PER_PIN 2 |
| 1037 | #define RV1108_PULL_BANK_STRIDE 16 |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1038 | |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1039 | static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1040 | int pin_num, struct regmap **regmap, |
| 1041 | int *reg, u8 *bit) |
| 1042 | { |
| 1043 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1044 | |
| 1045 | /* The first 24 pins of the first bank are located in PMU */ |
| 1046 | if (bank->bank_num == 0) { |
| 1047 | *regmap = info->regmap_pmu; |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1048 | *reg = RV1108_PULL_PMU_OFFSET; |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1049 | } else { |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1050 | *reg = RV1108_PULL_OFFSET; |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1051 | *regmap = info->regmap_base; |
| 1052 | /* correct the offset, as we're starting with the 2nd bank */ |
| 1053 | *reg -= 0x10; |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1054 | *reg += bank->bank_num * RV1108_PULL_BANK_STRIDE; |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1057 | *reg += ((pin_num / RV1108_PULL_PINS_PER_REG) * 4); |
| 1058 | *bit = (pin_num % RV1108_PULL_PINS_PER_REG); |
| 1059 | *bit *= RV1108_PULL_BITS_PER_PIN; |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1060 | } |
| 1061 | |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1062 | #define RV1108_DRV_PMU_OFFSET 0x20 |
| 1063 | #define RV1108_DRV_GRF_OFFSET 0x210 |
| 1064 | #define RV1108_DRV_BITS_PER_PIN 2 |
| 1065 | #define RV1108_DRV_PINS_PER_REG 8 |
| 1066 | #define RV1108_DRV_BANK_STRIDE 16 |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1067 | |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1068 | static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1069 | int pin_num, struct regmap **regmap, |
| 1070 | int *reg, u8 *bit) |
| 1071 | { |
| 1072 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1073 | |
| 1074 | /* The first 24 pins of the first bank are located in PMU */ |
| 1075 | if (bank->bank_num == 0) { |
| 1076 | *regmap = info->regmap_pmu; |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1077 | *reg = RV1108_DRV_PMU_OFFSET; |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1078 | } else { |
| 1079 | *regmap = info->regmap_base; |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1080 | *reg = RV1108_DRV_GRF_OFFSET; |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1081 | |
| 1082 | /* correct the offset, as we're starting with the 2nd bank */ |
| 1083 | *reg -= 0x10; |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1084 | *reg += bank->bank_num * RV1108_DRV_BANK_STRIDE; |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1087 | *reg += ((pin_num / RV1108_DRV_PINS_PER_REG) * 4); |
| 1088 | *bit = pin_num % RV1108_DRV_PINS_PER_REG; |
| 1089 | *bit *= RV1108_DRV_BITS_PER_PIN; |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 1090 | } |
| 1091 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1092 | #define RK2928_PULL_OFFSET 0x118 |
| 1093 | #define RK2928_PULL_PINS_PER_REG 16 |
| 1094 | #define RK2928_PULL_BANK_STRIDE 8 |
| 1095 | |
| 1096 | 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] | 1097 | int pin_num, struct regmap **regmap, |
| 1098 | int *reg, u8 *bit) |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1099 | { |
| 1100 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1101 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1102 | *regmap = info->regmap_base; |
| 1103 | *reg = RK2928_PULL_OFFSET; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1104 | *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE; |
| 1105 | *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4; |
| 1106 | |
| 1107 | *bit = pin_num % RK2928_PULL_PINS_PER_REG; |
| 1108 | }; |
| 1109 | |
Heiko Stübner | bfc7a42 | 2014-05-05 13:58:00 +0200 | [diff] [blame] | 1110 | #define RK3188_PULL_OFFSET 0x164 |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1111 | #define RK3188_PULL_BITS_PER_PIN 2 |
| 1112 | #define RK3188_PULL_PINS_PER_REG 8 |
| 1113 | #define RK3188_PULL_BANK_STRIDE 16 |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 1114 | #define RK3188_PULL_PMU_OFFSET 0x64 |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1115 | |
| 1116 | 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] | 1117 | int pin_num, struct regmap **regmap, |
| 1118 | int *reg, u8 *bit) |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1119 | { |
| 1120 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1121 | |
| 1122 | /* The first 12 pins of the first bank are located elsewhere */ |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 1123 | if (bank->bank_num == 0 && pin_num < 12) { |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 1124 | *regmap = info->regmap_pmu ? info->regmap_pmu |
| 1125 | : bank->regmap_pull; |
| 1126 | *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1127 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1128 | *bit = pin_num % RK3188_PULL_PINS_PER_REG; |
| 1129 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1130 | } else { |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1131 | *regmap = info->regmap_pull ? info->regmap_pull |
| 1132 | : info->regmap_base; |
| 1133 | *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET; |
| 1134 | |
Heiko Stübner | bfc7a42 | 2014-05-05 13:58:00 +0200 | [diff] [blame] | 1135 | /* correct the offset, as it is the 2nd pull register */ |
| 1136 | *reg -= 4; |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1137 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 1138 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 1139 | |
| 1140 | /* |
| 1141 | * The bits in these registers have an inverse ordering |
| 1142 | * with the lowest pin being in bits 15:14 and the highest |
| 1143 | * pin in bits 1:0 |
| 1144 | */ |
| 1145 | *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG); |
| 1146 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1147 | } |
| 1148 | } |
| 1149 | |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 1150 | #define RK3288_PULL_OFFSET 0x140 |
| 1151 | static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1152 | int pin_num, struct regmap **regmap, |
| 1153 | int *reg, u8 *bit) |
| 1154 | { |
| 1155 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1156 | |
| 1157 | /* The first 24 pins of the first bank are located in PMU */ |
| 1158 | if (bank->bank_num == 0) { |
| 1159 | *regmap = info->regmap_pmu; |
| 1160 | *reg = RK3188_PULL_PMU_OFFSET; |
| 1161 | |
| 1162 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 1163 | *bit = pin_num % RK3188_PULL_PINS_PER_REG; |
| 1164 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1165 | } else { |
| 1166 | *regmap = info->regmap_base; |
| 1167 | *reg = RK3288_PULL_OFFSET; |
| 1168 | |
| 1169 | /* correct the offset, as we're starting with the 2nd bank */ |
| 1170 | *reg -= 0x10; |
| 1171 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 1172 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 1173 | |
| 1174 | *bit = (pin_num % RK3188_PULL_PINS_PER_REG); |
| 1175 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1179 | #define RK3288_DRV_PMU_OFFSET 0x70 |
| 1180 | #define RK3288_DRV_GRF_OFFSET 0x1c0 |
| 1181 | #define RK3288_DRV_BITS_PER_PIN 2 |
| 1182 | #define RK3288_DRV_PINS_PER_REG 8 |
| 1183 | #define RK3288_DRV_BANK_STRIDE 16 |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1184 | |
| 1185 | static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1186 | int pin_num, struct regmap **regmap, |
| 1187 | int *reg, u8 *bit) |
| 1188 | { |
| 1189 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1190 | |
| 1191 | /* The first 24 pins of the first bank are located in PMU */ |
| 1192 | if (bank->bank_num == 0) { |
| 1193 | *regmap = info->regmap_pmu; |
| 1194 | *reg = RK3288_DRV_PMU_OFFSET; |
| 1195 | |
| 1196 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 1197 | *bit = pin_num % RK3288_DRV_PINS_PER_REG; |
| 1198 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 1199 | } else { |
| 1200 | *regmap = info->regmap_base; |
| 1201 | *reg = RK3288_DRV_GRF_OFFSET; |
| 1202 | |
| 1203 | /* correct the offset, as we're starting with the 2nd bank */ |
| 1204 | *reg -= 0x10; |
| 1205 | *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE; |
| 1206 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 1207 | |
| 1208 | *bit = (pin_num % RK3288_DRV_PINS_PER_REG); |
| 1209 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 1210 | } |
| 1211 | } |
| 1212 | |
Jeffy Chen | fea0fe6 | 2015-12-09 17:04:06 +0800 | [diff] [blame] | 1213 | #define RK3228_PULL_OFFSET 0x100 |
| 1214 | |
| 1215 | static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1216 | int pin_num, struct regmap **regmap, |
| 1217 | int *reg, u8 *bit) |
| 1218 | { |
| 1219 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1220 | |
| 1221 | *regmap = info->regmap_base; |
| 1222 | *reg = RK3228_PULL_OFFSET; |
| 1223 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 1224 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 1225 | |
| 1226 | *bit = (pin_num % RK3188_PULL_PINS_PER_REG); |
| 1227 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1228 | } |
| 1229 | |
| 1230 | #define RK3228_DRV_GRF_OFFSET 0x200 |
| 1231 | |
| 1232 | static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1233 | int pin_num, struct regmap **regmap, |
| 1234 | int *reg, u8 *bit) |
| 1235 | { |
| 1236 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1237 | |
| 1238 | *regmap = info->regmap_base; |
| 1239 | *reg = RK3228_DRV_GRF_OFFSET; |
| 1240 | *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE; |
| 1241 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 1242 | |
| 1243 | *bit = (pin_num % RK3288_DRV_PINS_PER_REG); |
| 1244 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 1245 | } |
| 1246 | |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 1247 | #define RK3368_PULL_GRF_OFFSET 0x100 |
| 1248 | #define RK3368_PULL_PMU_OFFSET 0x10 |
| 1249 | |
| 1250 | static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1251 | int pin_num, struct regmap **regmap, |
| 1252 | int *reg, u8 *bit) |
| 1253 | { |
| 1254 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1255 | |
| 1256 | /* The first 32 pins of the first bank are located in PMU */ |
| 1257 | if (bank->bank_num == 0) { |
| 1258 | *regmap = info->regmap_pmu; |
| 1259 | *reg = RK3368_PULL_PMU_OFFSET; |
| 1260 | |
| 1261 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 1262 | *bit = pin_num % RK3188_PULL_PINS_PER_REG; |
| 1263 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1264 | } else { |
| 1265 | *regmap = info->regmap_base; |
| 1266 | *reg = RK3368_PULL_GRF_OFFSET; |
| 1267 | |
| 1268 | /* correct the offset, as we're starting with the 2nd bank */ |
| 1269 | *reg -= 0x10; |
| 1270 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 1271 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 1272 | |
| 1273 | *bit = (pin_num % RK3188_PULL_PINS_PER_REG); |
| 1274 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | #define RK3368_DRV_PMU_OFFSET 0x20 |
| 1279 | #define RK3368_DRV_GRF_OFFSET 0x200 |
| 1280 | |
| 1281 | static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1282 | int pin_num, struct regmap **regmap, |
| 1283 | int *reg, u8 *bit) |
| 1284 | { |
| 1285 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1286 | |
| 1287 | /* The first 32 pins of the first bank are located in PMU */ |
| 1288 | if (bank->bank_num == 0) { |
| 1289 | *regmap = info->regmap_pmu; |
| 1290 | *reg = RK3368_DRV_PMU_OFFSET; |
| 1291 | |
| 1292 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 1293 | *bit = pin_num % RK3288_DRV_PINS_PER_REG; |
| 1294 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 1295 | } else { |
| 1296 | *regmap = info->regmap_base; |
| 1297 | *reg = RK3368_DRV_GRF_OFFSET; |
| 1298 | |
| 1299 | /* correct the offset, as we're starting with the 2nd bank */ |
| 1300 | *reg -= 0x10; |
| 1301 | *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE; |
| 1302 | *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); |
| 1303 | |
| 1304 | *bit = (pin_num % RK3288_DRV_PINS_PER_REG); |
| 1305 | *bit *= RK3288_DRV_BITS_PER_PIN; |
| 1306 | } |
| 1307 | } |
| 1308 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1309 | #define RK3399_PULL_GRF_OFFSET 0xe040 |
| 1310 | #define RK3399_PULL_PMU_OFFSET 0x40 |
| 1311 | #define RK3399_DRV_3BITS_PER_PIN 3 |
| 1312 | |
| 1313 | static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1314 | int pin_num, struct regmap **regmap, |
| 1315 | int *reg, u8 *bit) |
| 1316 | { |
| 1317 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1318 | |
| 1319 | /* The bank0:16 and bank1:32 pins are located in PMU */ |
| 1320 | if ((bank->bank_num == 0) || (bank->bank_num == 1)) { |
| 1321 | *regmap = info->regmap_pmu; |
| 1322 | *reg = RK3399_PULL_PMU_OFFSET; |
| 1323 | |
| 1324 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 1325 | |
| 1326 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 1327 | *bit = pin_num % RK3188_PULL_PINS_PER_REG; |
| 1328 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1329 | } else { |
| 1330 | *regmap = info->regmap_base; |
| 1331 | *reg = RK3399_PULL_GRF_OFFSET; |
| 1332 | |
| 1333 | /* correct the offset, as we're starting with the 3rd bank */ |
| 1334 | *reg -= 0x20; |
| 1335 | *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; |
| 1336 | *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); |
| 1337 | |
| 1338 | *bit = (pin_num % RK3188_PULL_PINS_PER_REG); |
| 1339 | *bit *= RK3188_PULL_BITS_PER_PIN; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1344 | int pin_num, struct regmap **regmap, |
| 1345 | int *reg, u8 *bit) |
| 1346 | { |
| 1347 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1348 | int drv_num = (pin_num / 8); |
| 1349 | |
| 1350 | /* The bank0:16 and bank1:32 pins are located in PMU */ |
| 1351 | if ((bank->bank_num == 0) || (bank->bank_num == 1)) |
| 1352 | *regmap = info->regmap_pmu; |
| 1353 | else |
| 1354 | *regmap = info->regmap_base; |
| 1355 | |
| 1356 | *reg = bank->drv[drv_num].offset; |
| 1357 | if ((bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) || |
| 1358 | (bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY)) |
| 1359 | *bit = (pin_num % 8) * 3; |
| 1360 | else |
| 1361 | *bit = (pin_num % 8) * 2; |
| 1362 | } |
| 1363 | |
| 1364 | static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = { |
| 1365 | { 2, 4, 8, 12, -1, -1, -1, -1 }, |
| 1366 | { 3, 6, 9, 12, -1, -1, -1, -1 }, |
| 1367 | { 5, 10, 15, 20, -1, -1, -1, -1 }, |
| 1368 | { 4, 6, 8, 10, 12, 14, 16, 18 }, |
| 1369 | { 4, 7, 10, 13, 16, 19, 22, 26 } |
| 1370 | }; |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1371 | |
| 1372 | static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank, |
| 1373 | int pin_num) |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1374 | { |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1375 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1376 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1377 | struct regmap *regmap; |
| 1378 | int reg, ret; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1379 | u32 data, temp, rmask_bits; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1380 | u8 bit; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1381 | int drv_type = bank->drv[pin_num / 8].drv_type; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1382 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1383 | ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1384 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1385 | switch (drv_type) { |
| 1386 | case DRV_TYPE_IO_1V8_3V0_AUTO: |
| 1387 | case DRV_TYPE_IO_3V3_ONLY: |
| 1388 | rmask_bits = RK3399_DRV_3BITS_PER_PIN; |
| 1389 | switch (bit) { |
| 1390 | case 0 ... 12: |
| 1391 | /* regular case, nothing to do */ |
| 1392 | break; |
| 1393 | case 15: |
| 1394 | /* |
| 1395 | * drive-strength offset is special, as it is |
| 1396 | * spread over 2 registers |
| 1397 | */ |
| 1398 | ret = regmap_read(regmap, reg, &data); |
| 1399 | if (ret) |
| 1400 | return ret; |
| 1401 | |
| 1402 | ret = regmap_read(regmap, reg + 0x4, &temp); |
| 1403 | if (ret) |
| 1404 | return ret; |
| 1405 | |
| 1406 | /* |
| 1407 | * the bit data[15] contains bit 0 of the value |
| 1408 | * while temp[1:0] contains bits 2 and 1 |
| 1409 | */ |
| 1410 | data >>= 15; |
| 1411 | temp &= 0x3; |
| 1412 | temp <<= 1; |
| 1413 | data |= temp; |
| 1414 | |
| 1415 | return rockchip_perpin_drv_list[drv_type][data]; |
| 1416 | case 18 ... 21: |
| 1417 | /* setting fully enclosed in the second register */ |
| 1418 | reg += 4; |
| 1419 | bit -= 16; |
| 1420 | break; |
| 1421 | default: |
| 1422 | dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n", |
| 1423 | bit, drv_type); |
| 1424 | return -EINVAL; |
| 1425 | } |
| 1426 | |
| 1427 | break; |
| 1428 | case DRV_TYPE_IO_DEFAULT: |
| 1429 | case DRV_TYPE_IO_1V8_OR_3V0: |
| 1430 | case DRV_TYPE_IO_1V8_ONLY: |
| 1431 | rmask_bits = RK3288_DRV_BITS_PER_PIN; |
| 1432 | break; |
| 1433 | default: |
| 1434 | dev_err(info->dev, "unsupported pinctrl drive type: %d\n", |
| 1435 | drv_type); |
| 1436 | return -EINVAL; |
| 1437 | } |
| 1438 | |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1439 | ret = regmap_read(regmap, reg, &data); |
| 1440 | if (ret) |
| 1441 | return ret; |
| 1442 | |
| 1443 | data >>= bit; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1444 | data &= (1 << rmask_bits) - 1; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1445 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1446 | return rockchip_perpin_drv_list[drv_type][data]; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1449 | static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank, |
| 1450 | int pin_num, int strength) |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1451 | { |
| 1452 | struct rockchip_pinctrl *info = bank->drvdata; |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1453 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1454 | struct regmap *regmap; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1455 | int reg, ret, i; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1456 | u32 data, rmask, rmask_bits, temp; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1457 | u8 bit; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1458 | int drv_type = bank->drv[pin_num / 8].drv_type; |
| 1459 | |
| 1460 | dev_dbg(info->dev, "setting drive of GPIO%d-%d to %d\n", |
| 1461 | bank->bank_num, pin_num, strength); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1462 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1463 | ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1464 | |
| 1465 | ret = -EINVAL; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1466 | for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) { |
| 1467 | if (rockchip_perpin_drv_list[drv_type][i] == strength) { |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1468 | ret = i; |
| 1469 | break; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1470 | } else if (rockchip_perpin_drv_list[drv_type][i] < 0) { |
| 1471 | ret = rockchip_perpin_drv_list[drv_type][i]; |
| 1472 | break; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | if (ret < 0) { |
| 1477 | dev_err(info->dev, "unsupported driver strength %d\n", |
| 1478 | strength); |
| 1479 | return ret; |
| 1480 | } |
| 1481 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1482 | switch (drv_type) { |
| 1483 | case DRV_TYPE_IO_1V8_3V0_AUTO: |
| 1484 | case DRV_TYPE_IO_3V3_ONLY: |
| 1485 | rmask_bits = RK3399_DRV_3BITS_PER_PIN; |
| 1486 | switch (bit) { |
| 1487 | case 0 ... 12: |
| 1488 | /* regular case, nothing to do */ |
| 1489 | break; |
| 1490 | case 15: |
| 1491 | /* |
| 1492 | * drive-strength offset is special, as it is spread |
| 1493 | * over 2 registers, the bit data[15] contains bit 0 |
| 1494 | * of the value while temp[1:0] contains bits 2 and 1 |
| 1495 | */ |
| 1496 | data = (ret & 0x1) << 15; |
| 1497 | temp = (ret >> 0x1) & 0x3; |
| 1498 | |
| 1499 | rmask = BIT(15) | BIT(31); |
| 1500 | data |= BIT(31); |
| 1501 | ret = regmap_update_bits(regmap, reg, rmask, data); |
John Keeping | f07bedc | 2017-03-23 10:59:28 +0000 | [diff] [blame] | 1502 | if (ret) |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1503 | return ret; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1504 | |
| 1505 | rmask = 0x3 | (0x3 << 16); |
| 1506 | temp |= (0x3 << 16); |
| 1507 | reg += 0x4; |
| 1508 | ret = regmap_update_bits(regmap, reg, rmask, temp); |
| 1509 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1510 | return ret; |
| 1511 | case 18 ... 21: |
| 1512 | /* setting fully enclosed in the second register */ |
| 1513 | reg += 4; |
| 1514 | bit -= 16; |
| 1515 | break; |
| 1516 | default: |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1517 | dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n", |
| 1518 | bit, drv_type); |
| 1519 | return -EINVAL; |
| 1520 | } |
| 1521 | break; |
| 1522 | case DRV_TYPE_IO_DEFAULT: |
| 1523 | case DRV_TYPE_IO_1V8_OR_3V0: |
| 1524 | case DRV_TYPE_IO_1V8_ONLY: |
| 1525 | rmask_bits = RK3288_DRV_BITS_PER_PIN; |
| 1526 | break; |
| 1527 | default: |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1528 | dev_err(info->dev, "unsupported pinctrl drive type: %d\n", |
| 1529 | drv_type); |
| 1530 | return -EINVAL; |
| 1531 | } |
| 1532 | |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1533 | /* enable the write to the equivalent lower bits */ |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1534 | data = ((1 << rmask_bits) - 1) << (bit + 16); |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 1535 | rmask = data | (data >> 16); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1536 | data |= (ret << bit); |
| 1537 | |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 1538 | ret = regmap_update_bits(regmap, reg, rmask, data); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1539 | |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 1543 | static int rockchip_pull_list[PULL_TYPE_MAX][4] = { |
| 1544 | { |
| 1545 | PIN_CONFIG_BIAS_DISABLE, |
| 1546 | PIN_CONFIG_BIAS_PULL_UP, |
| 1547 | PIN_CONFIG_BIAS_PULL_DOWN, |
| 1548 | PIN_CONFIG_BIAS_BUS_HOLD |
| 1549 | }, |
| 1550 | { |
| 1551 | PIN_CONFIG_BIAS_DISABLE, |
| 1552 | PIN_CONFIG_BIAS_PULL_DOWN, |
| 1553 | PIN_CONFIG_BIAS_DISABLE, |
| 1554 | PIN_CONFIG_BIAS_PULL_UP |
| 1555 | }, |
| 1556 | }; |
| 1557 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1558 | static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num) |
| 1559 | { |
| 1560 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1561 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1562 | struct regmap *regmap; |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 1563 | int reg, ret, pull_type; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1564 | u8 bit; |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1565 | u32 data; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1566 | |
| 1567 | /* rk3066b does support any pulls */ |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1568 | if (ctrl->type == RK3066B) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1569 | return PIN_CONFIG_BIAS_DISABLE; |
| 1570 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1571 | ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit); |
| 1572 | |
| 1573 | ret = regmap_read(regmap, reg, &data); |
| 1574 | if (ret) |
| 1575 | return ret; |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1576 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1577 | switch (ctrl->type) { |
| 1578 | case RK2928: |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1579 | return !(data & BIT(bit)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1580 | ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT |
| 1581 | : PIN_CONFIG_BIAS_DISABLE; |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1582 | case RV1108: |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1583 | case RK3188: |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 1584 | case RK3288: |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 1585 | case RK3368: |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1586 | case RK3399: |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 1587 | pull_type = bank->pull_type[pin_num / 8]; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1588 | data >>= bit; |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1589 | data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1; |
| 1590 | |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 1591 | return rockchip_pull_list[pull_type][data]; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1592 | default: |
| 1593 | dev_err(info->dev, "unsupported pinctrl type\n"); |
| 1594 | return -EINVAL; |
| 1595 | }; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | static int rockchip_set_pull(struct rockchip_pin_bank *bank, |
| 1599 | int pin_num, int pull) |
| 1600 | { |
| 1601 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1602 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1603 | struct regmap *regmap; |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 1604 | int reg, ret, i, pull_type; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1605 | u8 bit; |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 1606 | u32 data, rmask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1607 | |
| 1608 | dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n", |
| 1609 | bank->bank_num, pin_num, pull); |
| 1610 | |
| 1611 | /* rk3066b does support any pulls */ |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1612 | if (ctrl->type == RK3066B) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1613 | return pull ? -EINVAL : 0; |
| 1614 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1615 | ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1616 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1617 | switch (ctrl->type) { |
| 1618 | case RK2928: |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1619 | data = BIT(bit + 16); |
| 1620 | if (pull == PIN_CONFIG_BIAS_DISABLE) |
| 1621 | data |= BIT(bit); |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1622 | ret = regmap_write(regmap, reg, data); |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1623 | break; |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1624 | case RV1108: |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1625 | case RK3188: |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 1626 | case RK3288: |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 1627 | case RK3368: |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1628 | case RK3399: |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 1629 | pull_type = bank->pull_type[pin_num / 8]; |
| 1630 | ret = -EINVAL; |
| 1631 | for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]); |
| 1632 | i++) { |
| 1633 | if (rockchip_pull_list[pull_type][i] == pull) { |
| 1634 | ret = i; |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | if (ret < 0) { |
| 1640 | dev_err(info->dev, "unsupported pull setting %d\n", |
| 1641 | pull); |
| 1642 | return ret; |
| 1643 | } |
| 1644 | |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1645 | /* enable the write to the equivalent lower bits */ |
| 1646 | data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16); |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 1647 | rmask = data | (data >> 16); |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 1648 | data |= (ret << bit); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1649 | |
Sonny Rao | 99e872d | 2014-07-31 22:58:00 -0700 | [diff] [blame] | 1650 | ret = regmap_update_bits(regmap, reg, rmask, data); |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1651 | break; |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1652 | default: |
| 1653 | dev_err(info->dev, "unsupported pinctrl type\n"); |
| 1654 | return -EINVAL; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1655 | } |
| 1656 | |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 1657 | return ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1658 | } |
| 1659 | |
david.wu | 728d3f5 | 2017-03-02 15:11:24 +0800 | [diff] [blame] | 1660 | #define RK3328_SCHMITT_BITS_PER_PIN 1 |
| 1661 | #define RK3328_SCHMITT_PINS_PER_REG 16 |
| 1662 | #define RK3328_SCHMITT_BANK_STRIDE 8 |
| 1663 | #define RK3328_SCHMITT_GRF_OFFSET 0x380 |
| 1664 | |
| 1665 | static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank, |
| 1666 | int pin_num, |
| 1667 | struct regmap **regmap, |
| 1668 | int *reg, u8 *bit) |
| 1669 | { |
| 1670 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1671 | |
| 1672 | *regmap = info->regmap_base; |
| 1673 | *reg = RK3328_SCHMITT_GRF_OFFSET; |
| 1674 | |
| 1675 | *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE; |
| 1676 | *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4); |
| 1677 | *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG; |
| 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
david.wu | e3b357d | 2017-03-02 15:11:23 +0800 | [diff] [blame] | 1682 | static int rockchip_get_schmitt(struct rockchip_pin_bank *bank, int pin_num) |
| 1683 | { |
| 1684 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1685 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 1686 | struct regmap *regmap; |
| 1687 | int reg, ret; |
| 1688 | u8 bit; |
| 1689 | u32 data; |
| 1690 | |
| 1691 | ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit); |
| 1692 | if (ret) |
| 1693 | return ret; |
| 1694 | |
| 1695 | ret = regmap_read(regmap, reg, &data); |
| 1696 | if (ret) |
| 1697 | return ret; |
| 1698 | |
| 1699 | data >>= bit; |
| 1700 | return data & 0x1; |
| 1701 | } |
| 1702 | |
| 1703 | static int rockchip_set_schmitt(struct rockchip_pin_bank *bank, |
| 1704 | int pin_num, int enable) |
| 1705 | { |
| 1706 | struct rockchip_pinctrl *info = bank->drvdata; |
| 1707 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 1708 | struct regmap *regmap; |
| 1709 | int reg, ret; |
david.wu | e3b357d | 2017-03-02 15:11:23 +0800 | [diff] [blame] | 1710 | u8 bit; |
| 1711 | u32 data, rmask; |
| 1712 | |
| 1713 | dev_dbg(info->dev, "setting input schmitt of GPIO%d-%d to %d\n", |
| 1714 | bank->bank_num, pin_num, enable); |
| 1715 | |
| 1716 | ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit); |
| 1717 | if (ret) |
| 1718 | return ret; |
| 1719 | |
david.wu | e3b357d | 2017-03-02 15:11:23 +0800 | [diff] [blame] | 1720 | /* enable the write to the equivalent lower bits */ |
| 1721 | data = BIT(bit + 16) | (enable << bit); |
| 1722 | rmask = BIT(bit + 16) | BIT(bit); |
| 1723 | |
John Keeping | f07bedc | 2017-03-23 10:59:28 +0000 | [diff] [blame] | 1724 | return regmap_update_bits(regmap, reg, rmask, data); |
david.wu | e3b357d | 2017-03-02 15:11:23 +0800 | [diff] [blame] | 1725 | } |
| 1726 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1727 | /* |
| 1728 | * Pinmux_ops handling |
| 1729 | */ |
| 1730 | |
| 1731 | static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev) |
| 1732 | { |
| 1733 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1734 | |
| 1735 | return info->nfunctions; |
| 1736 | } |
| 1737 | |
| 1738 | static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev, |
| 1739 | unsigned selector) |
| 1740 | { |
| 1741 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1742 | |
| 1743 | return info->functions[selector].name; |
| 1744 | } |
| 1745 | |
| 1746 | static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev, |
| 1747 | unsigned selector, const char * const **groups, |
| 1748 | unsigned * const num_groups) |
| 1749 | { |
| 1750 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1751 | |
| 1752 | *groups = info->functions[selector].groups; |
| 1753 | *num_groups = info->functions[selector].ngroups; |
| 1754 | |
| 1755 | return 0; |
| 1756 | } |
| 1757 | |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 1758 | static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector, |
| 1759 | unsigned group) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1760 | { |
| 1761 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1762 | const unsigned int *pins = info->groups[group].pins; |
| 1763 | const struct rockchip_pin_config *data = info->groups[group].data; |
| 1764 | struct rockchip_pin_bank *bank; |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 1765 | int cnt, ret = 0; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1766 | |
| 1767 | dev_dbg(info->dev, "enable function %s group %s\n", |
| 1768 | info->functions[selector].name, info->groups[group].name); |
| 1769 | |
| 1770 | /* |
| 1771 | * for each pin in the pin group selected, program the correspoding pin |
| 1772 | * pin function number in the config register. |
| 1773 | */ |
| 1774 | for (cnt = 0; cnt < info->groups[group].npins; cnt++) { |
| 1775 | bank = pin_to_bank(info, pins[cnt]); |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 1776 | ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base, |
| 1777 | data[cnt].func); |
| 1778 | if (ret) |
| 1779 | break; |
| 1780 | } |
| 1781 | |
| 1782 | if (ret) { |
| 1783 | /* revert the already done pin settings */ |
| 1784 | for (cnt--; cnt >= 0; cnt--) |
| 1785 | rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0); |
| 1786 | |
| 1787 | return ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
Caesar Wang | 6ba20a0 | 2016-03-15 15:55:45 +0800 | [diff] [blame] | 1793 | static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset) |
| 1794 | { |
| 1795 | struct rockchip_pin_bank *bank = gpiochip_get_data(chip); |
| 1796 | u32 data; |
| 1797 | |
| 1798 | data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); |
| 1799 | |
| 1800 | return !(data & BIT(offset)); |
| 1801 | } |
| 1802 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1803 | /* |
| 1804 | * The calls to gpio_direction_output() and gpio_direction_input() |
| 1805 | * leads to this function call (via the pinctrl_gpio_direction_{input|output}() |
| 1806 | * function called from the gpiolib interface). |
| 1807 | */ |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 1808 | static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip, |
| 1809 | int pin, bool input) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1810 | { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1811 | struct rockchip_pin_bank *bank; |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 1812 | int ret; |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1813 | unsigned long flags; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1814 | u32 data; |
| 1815 | |
Linus Walleij | 03bf81f | 2015-12-08 09:39:13 +0100 | [diff] [blame] | 1816 | bank = gpiochip_get_data(chip); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1817 | |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 1818 | ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO); |
| 1819 | if (ret < 0) |
| 1820 | return ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1821 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1822 | clk_enable(bank->clk); |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 1823 | raw_spin_lock_irqsave(&bank->slock, flags); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1824 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1825 | data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); |
| 1826 | /* set bit to 1 for output, 0 for input */ |
| 1827 | if (!input) |
| 1828 | data |= BIT(pin); |
| 1829 | else |
| 1830 | data &= ~BIT(pin); |
| 1831 | writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR); |
| 1832 | |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 1833 | raw_spin_unlock_irqrestore(&bank->slock, flags); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 1834 | clk_disable(bank->clk); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 1835 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1836 | return 0; |
| 1837 | } |
| 1838 | |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 1839 | static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 1840 | struct pinctrl_gpio_range *range, |
| 1841 | unsigned offset, bool input) |
| 1842 | { |
| 1843 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1844 | struct gpio_chip *chip; |
| 1845 | int pin; |
| 1846 | |
| 1847 | chip = range->gc; |
| 1848 | pin = offset - chip->base; |
| 1849 | dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n", |
| 1850 | offset, range->name, pin, input ? "input" : "output"); |
| 1851 | |
| 1852 | return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base, |
| 1853 | input); |
| 1854 | } |
| 1855 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1856 | static const struct pinmux_ops rockchip_pmx_ops = { |
| 1857 | .get_functions_count = rockchip_pmx_get_funcs_count, |
| 1858 | .get_function_name = rockchip_pmx_get_func_name, |
| 1859 | .get_function_groups = rockchip_pmx_get_groups, |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 1860 | .set_mux = rockchip_pmx_set, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1861 | .gpio_set_direction = rockchip_pmx_gpio_set_direction, |
| 1862 | }; |
| 1863 | |
| 1864 | /* |
| 1865 | * Pinconf_ops handling |
| 1866 | */ |
| 1867 | |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1868 | static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl, |
| 1869 | enum pin_config_param pull) |
| 1870 | { |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1871 | switch (ctrl->type) { |
| 1872 | case RK2928: |
| 1873 | return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT || |
| 1874 | pull == PIN_CONFIG_BIAS_DISABLE); |
| 1875 | case RK3066B: |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1876 | return pull ? false : true; |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 1877 | case RV1108: |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1878 | case RK3188: |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 1879 | case RK3288: |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 1880 | case RK3368: |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 1881 | case RK3399: |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1882 | return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT); |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1883 | } |
| 1884 | |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 1885 | return false; |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1886 | } |
| 1887 | |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 1888 | 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] | 1889 | static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset); |
| 1890 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1891 | /* set the pin config settings for a specified pin */ |
| 1892 | static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1893 | unsigned long *configs, unsigned num_configs) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1894 | { |
| 1895 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1896 | struct rockchip_pin_bank *bank = pin_to_bank(info, pin); |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1897 | enum pin_config_param param; |
Mika Westerberg | 58957d2 | 2017-01-23 15:34:32 +0300 | [diff] [blame] | 1898 | u32 arg; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1899 | int i; |
| 1900 | int rc; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1901 | |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1902 | for (i = 0; i < num_configs; i++) { |
| 1903 | param = pinconf_to_config_param(configs[i]); |
| 1904 | arg = pinconf_to_config_argument(configs[i]); |
| 1905 | |
| 1906 | switch (param) { |
| 1907 | case PIN_CONFIG_BIAS_DISABLE: |
| 1908 | rc = rockchip_set_pull(bank, pin - bank->pin_base, |
| 1909 | param); |
| 1910 | if (rc) |
| 1911 | return rc; |
| 1912 | break; |
| 1913 | case PIN_CONFIG_BIAS_PULL_UP: |
| 1914 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 1915 | case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1916 | case PIN_CONFIG_BIAS_BUS_HOLD: |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1917 | if (!rockchip_pinconf_pull_valid(info->ctrl, param)) |
| 1918 | return -ENOTSUPP; |
| 1919 | |
| 1920 | if (!arg) |
| 1921 | return -EINVAL; |
| 1922 | |
| 1923 | rc = rockchip_set_pull(bank, pin - bank->pin_base, |
| 1924 | param); |
| 1925 | if (rc) |
| 1926 | return rc; |
| 1927 | break; |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1928 | case PIN_CONFIG_OUTPUT: |
Doug Anderson | e5c2c9d | 2014-10-21 10:47:33 -0700 | [diff] [blame] | 1929 | rockchip_gpio_set(&bank->gpio_chip, |
| 1930 | pin - bank->pin_base, arg); |
| 1931 | rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip, |
| 1932 | pin - bank->pin_base, false); |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1933 | if (rc) |
| 1934 | return rc; |
| 1935 | break; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1936 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 1937 | /* rk3288 is the first with per-pin drive-strength */ |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1938 | if (!info->ctrl->drv_calc_reg) |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1939 | return -ENOTSUPP; |
| 1940 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 1941 | rc = rockchip_set_drive_perpin(bank, |
| 1942 | pin - bank->pin_base, arg); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 1943 | if (rc < 0) |
| 1944 | return rc; |
| 1945 | break; |
david.wu | e3b357d | 2017-03-02 15:11:23 +0800 | [diff] [blame] | 1946 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 1947 | if (!info->ctrl->schmitt_calc_reg) |
| 1948 | return -ENOTSUPP; |
| 1949 | |
| 1950 | rc = rockchip_set_schmitt(bank, |
| 1951 | pin - bank->pin_base, arg); |
| 1952 | if (rc < 0) |
| 1953 | return rc; |
| 1954 | break; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1955 | default: |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1956 | return -ENOTSUPP; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 1957 | break; |
| 1958 | } |
| 1959 | } /* for each config */ |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1960 | |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
| 1964 | /* get the pin config settings for a specified pin */ |
| 1965 | static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, |
| 1966 | unsigned long *config) |
| 1967 | { |
| 1968 | struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 1969 | struct rockchip_pin_bank *bank = pin_to_bank(info, pin); |
| 1970 | enum pin_config_param param = pinconf_to_config_param(*config); |
Heiko Stübner | dab3eba | 2014-04-23 14:27:51 +0200 | [diff] [blame] | 1971 | u16 arg; |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1972 | int rc; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1973 | |
| 1974 | switch (param) { |
| 1975 | case PIN_CONFIG_BIAS_DISABLE: |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1976 | if (rockchip_get_pull(bank, pin - bank->pin_base) != param) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1977 | return -EINVAL; |
| 1978 | |
Heiko Stübner | dab3eba | 2014-04-23 14:27:51 +0200 | [diff] [blame] | 1979 | arg = 0; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 1980 | break; |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1981 | case PIN_CONFIG_BIAS_PULL_UP: |
| 1982 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 1983 | case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 1984 | case PIN_CONFIG_BIAS_BUS_HOLD: |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1985 | if (!rockchip_pinconf_pull_valid(info->ctrl, param)) |
| 1986 | return -ENOTSUPP; |
| 1987 | |
| 1988 | if (rockchip_get_pull(bank, pin - bank->pin_base) != param) |
| 1989 | return -EINVAL; |
| 1990 | |
Heiko Stübner | dab3eba | 2014-04-23 14:27:51 +0200 | [diff] [blame] | 1991 | arg = 1; |
Heiko Stübner | 44b6d93 | 2013-06-16 17:41:16 +0200 | [diff] [blame] | 1992 | break; |
Heiko Stübner | a076e2e | 2014-04-23 14:28:59 +0200 | [diff] [blame] | 1993 | case PIN_CONFIG_OUTPUT: |
| 1994 | rc = rockchip_get_mux(bank, pin - bank->pin_base); |
| 1995 | if (rc != RK_FUNC_GPIO) |
| 1996 | return -EINVAL; |
| 1997 | |
| 1998 | rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base); |
| 1999 | if (rc < 0) |
| 2000 | return rc; |
| 2001 | |
| 2002 | arg = rc ? 1 : 0; |
| 2003 | break; |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 2004 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 2005 | /* rk3288 is the first with per-pin drive-strength */ |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 2006 | if (!info->ctrl->drv_calc_reg) |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 2007 | return -ENOTSUPP; |
| 2008 | |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 2009 | rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base); |
Heiko Stübner | b547c80 | 2014-07-20 01:50:11 +0200 | [diff] [blame] | 2010 | if (rc < 0) |
| 2011 | return rc; |
| 2012 | |
| 2013 | arg = rc; |
| 2014 | break; |
david.wu | e3b357d | 2017-03-02 15:11:23 +0800 | [diff] [blame] | 2015 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 2016 | if (!info->ctrl->schmitt_calc_reg) |
| 2017 | return -ENOTSUPP; |
| 2018 | |
| 2019 | rc = rockchip_get_schmitt(bank, pin - bank->pin_base); |
| 2020 | if (rc < 0) |
| 2021 | return rc; |
| 2022 | |
| 2023 | arg = rc; |
| 2024 | break; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2025 | default: |
| 2026 | return -ENOTSUPP; |
| 2027 | break; |
| 2028 | } |
| 2029 | |
Heiko Stübner | dab3eba | 2014-04-23 14:27:51 +0200 | [diff] [blame] | 2030 | *config = pinconf_to_config_packed(param, arg); |
| 2031 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2032 | return 0; |
| 2033 | } |
| 2034 | |
| 2035 | static const struct pinconf_ops rockchip_pinconf_ops = { |
| 2036 | .pin_config_get = rockchip_pinconf_get, |
| 2037 | .pin_config_set = rockchip_pinconf_set, |
Heiko Stübner | ed62f2f | 2014-07-20 01:48:45 +0200 | [diff] [blame] | 2038 | .is_generic = true, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2039 | }; |
| 2040 | |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 2041 | static const struct of_device_id rockchip_bank_match[] = { |
| 2042 | { .compatible = "rockchip,gpio-bank" }, |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 2043 | { .compatible = "rockchip,rk3188-gpio-bank0" }, |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 2044 | {}, |
| 2045 | }; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2046 | |
| 2047 | static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info, |
| 2048 | struct device_node *np) |
| 2049 | { |
| 2050 | struct device_node *child; |
| 2051 | |
| 2052 | for_each_child_of_node(np, child) { |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 2053 | if (of_match_node(rockchip_bank_match, child)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2054 | continue; |
| 2055 | |
| 2056 | info->nfunctions++; |
| 2057 | info->ngroups += of_get_child_count(child); |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | static int rockchip_pinctrl_parse_groups(struct device_node *np, |
| 2062 | struct rockchip_pin_group *grp, |
| 2063 | struct rockchip_pinctrl *info, |
| 2064 | u32 index) |
| 2065 | { |
| 2066 | struct rockchip_pin_bank *bank; |
| 2067 | int size; |
| 2068 | const __be32 *list; |
| 2069 | int num; |
| 2070 | int i, j; |
| 2071 | int ret; |
| 2072 | |
| 2073 | dev_dbg(info->dev, "group(%d): %s\n", index, np->name); |
| 2074 | |
| 2075 | /* Initialise group */ |
| 2076 | grp->name = np->name; |
| 2077 | |
| 2078 | /* |
| 2079 | * the binding format is rockchip,pins = <bank pin mux CONFIG>, |
| 2080 | * do sanity check and calculate pins number |
| 2081 | */ |
| 2082 | list = of_get_property(np, "rockchip,pins", &size); |
| 2083 | /* we do not check return since it's safe node passed down */ |
| 2084 | size /= sizeof(*list); |
| 2085 | if (!size || size % 4) { |
| 2086 | dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n"); |
| 2087 | return -EINVAL; |
| 2088 | } |
| 2089 | |
| 2090 | grp->npins = size / 4; |
| 2091 | |
| 2092 | grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int), |
| 2093 | GFP_KERNEL); |
| 2094 | grp->data = devm_kzalloc(info->dev, grp->npins * |
| 2095 | sizeof(struct rockchip_pin_config), |
| 2096 | GFP_KERNEL); |
| 2097 | if (!grp->pins || !grp->data) |
| 2098 | return -ENOMEM; |
| 2099 | |
| 2100 | for (i = 0, j = 0; i < size; i += 4, j++) { |
| 2101 | const __be32 *phandle; |
| 2102 | struct device_node *np_config; |
| 2103 | |
| 2104 | num = be32_to_cpu(*list++); |
| 2105 | bank = bank_num_to_bank(info, num); |
| 2106 | if (IS_ERR(bank)) |
| 2107 | return PTR_ERR(bank); |
| 2108 | |
| 2109 | grp->pins[j] = bank->pin_base + be32_to_cpu(*list++); |
| 2110 | grp->data[j].func = be32_to_cpu(*list++); |
| 2111 | |
| 2112 | phandle = list++; |
| 2113 | if (!phandle) |
| 2114 | return -EINVAL; |
| 2115 | |
| 2116 | np_config = of_find_node_by_phandle(be32_to_cpup(phandle)); |
Soren Brinkmann | dd4d01f | 2015-01-09 07:43:46 -0800 | [diff] [blame] | 2117 | ret = pinconf_generic_parse_dt_config(np_config, NULL, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2118 | &grp->data[j].configs, &grp->data[j].nconfigs); |
| 2119 | if (ret) |
| 2120 | return ret; |
| 2121 | } |
| 2122 | |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
| 2126 | static int rockchip_pinctrl_parse_functions(struct device_node *np, |
| 2127 | struct rockchip_pinctrl *info, |
| 2128 | u32 index) |
| 2129 | { |
| 2130 | struct device_node *child; |
| 2131 | struct rockchip_pmx_func *func; |
| 2132 | struct rockchip_pin_group *grp; |
| 2133 | int ret; |
| 2134 | static u32 grp_index; |
| 2135 | u32 i = 0; |
| 2136 | |
| 2137 | dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name); |
| 2138 | |
| 2139 | func = &info->functions[index]; |
| 2140 | |
| 2141 | /* Initialise function */ |
| 2142 | func->name = np->name; |
| 2143 | func->ngroups = of_get_child_count(np); |
| 2144 | if (func->ngroups <= 0) |
| 2145 | return 0; |
| 2146 | |
| 2147 | func->groups = devm_kzalloc(info->dev, |
| 2148 | func->ngroups * sizeof(char *), GFP_KERNEL); |
| 2149 | if (!func->groups) |
| 2150 | return -ENOMEM; |
| 2151 | |
| 2152 | for_each_child_of_node(np, child) { |
| 2153 | func->groups[i] = child->name; |
| 2154 | grp = &info->groups[grp_index++]; |
| 2155 | ret = rockchip_pinctrl_parse_groups(child, grp, info, i++); |
Julia Lawall | f7a81b7 | 2015-12-21 17:39:47 +0100 | [diff] [blame] | 2156 | if (ret) { |
| 2157 | of_node_put(child); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2158 | return ret; |
Julia Lawall | f7a81b7 | 2015-12-21 17:39:47 +0100 | [diff] [blame] | 2159 | } |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | return 0; |
| 2163 | } |
| 2164 | |
| 2165 | static int rockchip_pinctrl_parse_dt(struct platform_device *pdev, |
| 2166 | struct rockchip_pinctrl *info) |
| 2167 | { |
| 2168 | struct device *dev = &pdev->dev; |
| 2169 | struct device_node *np = dev->of_node; |
| 2170 | struct device_node *child; |
| 2171 | int ret; |
| 2172 | int i; |
| 2173 | |
| 2174 | rockchip_pinctrl_child_count(info, np); |
| 2175 | |
| 2176 | dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions); |
| 2177 | dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups); |
| 2178 | |
| 2179 | info->functions = devm_kzalloc(dev, info->nfunctions * |
| 2180 | sizeof(struct rockchip_pmx_func), |
| 2181 | GFP_KERNEL); |
| 2182 | if (!info->functions) { |
| 2183 | dev_err(dev, "failed to allocate memory for function list\n"); |
| 2184 | return -EINVAL; |
| 2185 | } |
| 2186 | |
| 2187 | info->groups = devm_kzalloc(dev, info->ngroups * |
| 2188 | sizeof(struct rockchip_pin_group), |
| 2189 | GFP_KERNEL); |
| 2190 | if (!info->groups) { |
| 2191 | dev_err(dev, "failed allocate memory for ping group list\n"); |
| 2192 | return -EINVAL; |
| 2193 | } |
| 2194 | |
| 2195 | i = 0; |
| 2196 | |
| 2197 | for_each_child_of_node(np, child) { |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 2198 | if (of_match_node(rockchip_bank_match, child)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2199 | continue; |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 2200 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2201 | ret = rockchip_pinctrl_parse_functions(child, info, i++); |
| 2202 | if (ret) { |
| 2203 | dev_err(&pdev->dev, "failed to parse function\n"); |
Julia Lawall | f7a81b7 | 2015-12-21 17:39:47 +0100 | [diff] [blame] | 2204 | of_node_put(child); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2205 | return ret; |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | return 0; |
| 2210 | } |
| 2211 | |
| 2212 | static int rockchip_pinctrl_register(struct platform_device *pdev, |
| 2213 | struct rockchip_pinctrl *info) |
| 2214 | { |
| 2215 | struct pinctrl_desc *ctrldesc = &info->pctl; |
| 2216 | struct pinctrl_pin_desc *pindesc, *pdesc; |
| 2217 | struct rockchip_pin_bank *pin_bank; |
| 2218 | int pin, bank, ret; |
| 2219 | int k; |
| 2220 | |
| 2221 | ctrldesc->name = "rockchip-pinctrl"; |
| 2222 | ctrldesc->owner = THIS_MODULE; |
| 2223 | ctrldesc->pctlops = &rockchip_pctrl_ops; |
| 2224 | ctrldesc->pmxops = &rockchip_pmx_ops; |
| 2225 | ctrldesc->confops = &rockchip_pinconf_ops; |
| 2226 | |
| 2227 | pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) * |
| 2228 | info->ctrl->nr_pins, GFP_KERNEL); |
| 2229 | if (!pindesc) { |
| 2230 | dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n"); |
| 2231 | return -ENOMEM; |
| 2232 | } |
| 2233 | ctrldesc->pins = pindesc; |
| 2234 | ctrldesc->npins = info->ctrl->nr_pins; |
| 2235 | |
| 2236 | pdesc = pindesc; |
| 2237 | for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) { |
| 2238 | pin_bank = &info->ctrl->pin_banks[bank]; |
| 2239 | for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) { |
| 2240 | pdesc->number = k; |
| 2241 | pdesc->name = kasprintf(GFP_KERNEL, "%s-%d", |
| 2242 | pin_bank->name, pin); |
| 2243 | pdesc++; |
| 2244 | } |
| 2245 | } |
| 2246 | |
Doug Anderson | 0fb7dcb | 2014-10-21 10:47:34 -0700 | [diff] [blame] | 2247 | ret = rockchip_pinctrl_parse_dt(pdev, info); |
| 2248 | if (ret) |
| 2249 | return ret; |
| 2250 | |
Laxman Dewangan | 0085a2b | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 2251 | info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 2252 | if (IS_ERR(info->pctl_dev)) { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2253 | dev_err(&pdev->dev, "could not register pinctrl driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 2254 | return PTR_ERR(info->pctl_dev); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | for (bank = 0; bank < info->ctrl->nr_banks; ++bank) { |
| 2258 | pin_bank = &info->ctrl->pin_banks[bank]; |
| 2259 | pin_bank->grange.name = pin_bank->name; |
| 2260 | pin_bank->grange.id = bank; |
| 2261 | pin_bank->grange.pin_base = pin_bank->pin_base; |
| 2262 | pin_bank->grange.base = pin_bank->gpio_chip.base; |
| 2263 | pin_bank->grange.npins = pin_bank->gpio_chip.ngpio; |
| 2264 | pin_bank->grange.gc = &pin_bank->gpio_chip; |
| 2265 | pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange); |
| 2266 | } |
| 2267 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2268 | return 0; |
| 2269 | } |
| 2270 | |
| 2271 | /* |
| 2272 | * GPIO handling |
| 2273 | */ |
| 2274 | |
| 2275 | static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value) |
| 2276 | { |
Linus Walleij | 03bf81f | 2015-12-08 09:39:13 +0100 | [diff] [blame] | 2277 | struct rockchip_pin_bank *bank = gpiochip_get_data(gc); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2278 | void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR; |
| 2279 | unsigned long flags; |
| 2280 | u32 data; |
| 2281 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2282 | clk_enable(bank->clk); |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2283 | raw_spin_lock_irqsave(&bank->slock, flags); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2284 | |
| 2285 | data = readl(reg); |
| 2286 | data &= ~BIT(offset); |
| 2287 | if (value) |
| 2288 | data |= BIT(offset); |
| 2289 | writel(data, reg); |
| 2290 | |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2291 | raw_spin_unlock_irqrestore(&bank->slock, flags); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2292 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | /* |
| 2296 | * Returns the level of the pin for input direction and setting of the DR |
| 2297 | * register for output gpios. |
| 2298 | */ |
| 2299 | static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 2300 | { |
Linus Walleij | 03bf81f | 2015-12-08 09:39:13 +0100 | [diff] [blame] | 2301 | struct rockchip_pin_bank *bank = gpiochip_get_data(gc); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2302 | u32 data; |
| 2303 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2304 | clk_enable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2305 | data = readl(bank->reg_base + GPIO_EXT_PORT); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2306 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2307 | data >>= offset; |
| 2308 | data &= 1; |
| 2309 | return data; |
| 2310 | } |
| 2311 | |
| 2312 | /* |
| 2313 | * gpiolib gpio_direction_input callback function. The setting of the pin |
| 2314 | * mux function as 'gpio input' will be handled by the pinctrl susbsystem |
| 2315 | * interface. |
| 2316 | */ |
| 2317 | static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 2318 | { |
| 2319 | return pinctrl_gpio_direction_input(gc->base + offset); |
| 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * gpiolib gpio_direction_output callback function. The setting of the pin |
| 2324 | * mux function as 'gpio output' will be handled by the pinctrl susbsystem |
| 2325 | * interface. |
| 2326 | */ |
| 2327 | static int rockchip_gpio_direction_output(struct gpio_chip *gc, |
| 2328 | unsigned offset, int value) |
| 2329 | { |
| 2330 | rockchip_gpio_set(gc, offset, value); |
| 2331 | return pinctrl_gpio_direction_output(gc->base + offset); |
| 2332 | } |
| 2333 | |
| 2334 | /* |
| 2335 | * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin |
| 2336 | * and a virtual IRQ, if not already present. |
| 2337 | */ |
| 2338 | static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 2339 | { |
Linus Walleij | 03bf81f | 2015-12-08 09:39:13 +0100 | [diff] [blame] | 2340 | struct rockchip_pin_bank *bank = gpiochip_get_data(gc); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2341 | unsigned int virq; |
| 2342 | |
| 2343 | if (!bank->domain) |
| 2344 | return -ENXIO; |
| 2345 | |
| 2346 | virq = irq_create_mapping(bank->domain, offset); |
| 2347 | |
| 2348 | return (virq) ? : -ENXIO; |
| 2349 | } |
| 2350 | |
| 2351 | static const struct gpio_chip rockchip_gpiolib_chip = { |
Jonas Gorski | 98c85d5 | 2015-10-11 17:34:19 +0200 | [diff] [blame] | 2352 | .request = gpiochip_generic_request, |
| 2353 | .free = gpiochip_generic_free, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2354 | .set = rockchip_gpio_set, |
| 2355 | .get = rockchip_gpio_get, |
Caesar Wang | 6ba20a0 | 2016-03-15 15:55:45 +0800 | [diff] [blame] | 2356 | .get_direction = rockchip_gpio_get_direction, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2357 | .direction_input = rockchip_gpio_direction_input, |
| 2358 | .direction_output = rockchip_gpio_direction_output, |
| 2359 | .to_irq = rockchip_gpio_to_irq, |
| 2360 | .owner = THIS_MODULE, |
| 2361 | }; |
| 2362 | |
| 2363 | /* |
| 2364 | * Interrupt handling |
| 2365 | */ |
| 2366 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 2367 | static void rockchip_irq_demux(struct irq_desc *desc) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2368 | { |
Jiang Liu | 5663bb2 | 2015-06-04 12:13:16 +0800 | [diff] [blame] | 2369 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 2370 | struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2371 | u32 pend; |
| 2372 | |
| 2373 | dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name); |
| 2374 | |
| 2375 | chained_irq_enter(chip, desc); |
| 2376 | |
| 2377 | pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS); |
| 2378 | |
| 2379 | while (pend) { |
Thomas Gleixner | 415f748 | 2015-07-13 01:52:00 +0200 | [diff] [blame] | 2380 | unsigned int irq, virq; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2381 | |
| 2382 | irq = __ffs(pend); |
| 2383 | pend &= ~BIT(irq); |
| 2384 | virq = irq_linear_revmap(bank->domain, irq); |
| 2385 | |
| 2386 | if (!virq) { |
| 2387 | dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq); |
| 2388 | continue; |
| 2389 | } |
| 2390 | |
| 2391 | dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq); |
| 2392 | |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2393 | /* |
| 2394 | * Triggering IRQ on both rising and falling edge |
| 2395 | * needs manual intervention. |
| 2396 | */ |
| 2397 | if (bank->toggle_edge_mode & BIT(irq)) { |
Doug Anderson | 53b1bfc | 2014-12-22 10:47:29 -0800 | [diff] [blame] | 2398 | u32 data, data_old, polarity; |
| 2399 | unsigned long flags; |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2400 | |
Doug Anderson | 53b1bfc | 2014-12-22 10:47:29 -0800 | [diff] [blame] | 2401 | data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT); |
| 2402 | do { |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2403 | raw_spin_lock_irqsave(&bank->slock, flags); |
Doug Anderson | 53b1bfc | 2014-12-22 10:47:29 -0800 | [diff] [blame] | 2404 | |
| 2405 | polarity = readl_relaxed(bank->reg_base + |
| 2406 | GPIO_INT_POLARITY); |
| 2407 | if (data & BIT(irq)) |
| 2408 | polarity &= ~BIT(irq); |
| 2409 | else |
| 2410 | polarity |= BIT(irq); |
| 2411 | writel(polarity, |
| 2412 | bank->reg_base + GPIO_INT_POLARITY); |
| 2413 | |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2414 | raw_spin_unlock_irqrestore(&bank->slock, flags); |
Doug Anderson | 53b1bfc | 2014-12-22 10:47:29 -0800 | [diff] [blame] | 2415 | |
| 2416 | data_old = data; |
| 2417 | data = readl_relaxed(bank->reg_base + |
| 2418 | GPIO_EXT_PORT); |
| 2419 | } while ((data & BIT(irq)) != (data_old & BIT(irq))); |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2420 | } |
| 2421 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2422 | generic_handle_irq(virq); |
| 2423 | } |
| 2424 | |
| 2425 | chained_irq_exit(chip, desc); |
| 2426 | } |
| 2427 | |
| 2428 | static int rockchip_irq_set_type(struct irq_data *d, unsigned int type) |
| 2429 | { |
| 2430 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 2431 | struct rockchip_pin_bank *bank = gc->private; |
| 2432 | u32 mask = BIT(d->hwirq); |
| 2433 | u32 polarity; |
| 2434 | u32 level; |
| 2435 | u32 data; |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 2436 | unsigned long flags; |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 2437 | int ret; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2438 | |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2439 | /* make sure the pin is configured as gpio input */ |
John Keeping | 88bb942 | 2017-03-23 10:59:31 +0000 | [diff] [blame] | 2440 | ret = rockchip_verify_mux(bank, d->hwirq, RK_FUNC_GPIO); |
Heiko Stübner | 1479718 | 2014-03-26 00:57:00 +0100 | [diff] [blame] | 2441 | if (ret < 0) |
| 2442 | return ret; |
| 2443 | |
John Keeping | 88bb942 | 2017-03-23 10:59:31 +0000 | [diff] [blame] | 2444 | bank->new_irqs |= mask; |
| 2445 | |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2446 | raw_spin_lock_irqsave(&bank->slock, flags); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 2447 | |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2448 | data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); |
| 2449 | data &= ~mask; |
| 2450 | writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR); |
| 2451 | |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2452 | raw_spin_unlock_irqrestore(&bank->slock, flags); |
Doug Anderson | fab262f | 2014-10-21 10:47:35 -0700 | [diff] [blame] | 2453 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2454 | if (type & IRQ_TYPE_EDGE_BOTH) |
Thomas Gleixner | 2dbf1bc | 2015-06-23 15:52:50 +0200 | [diff] [blame] | 2455 | irq_set_handler_locked(d, handle_edge_irq); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2456 | else |
Thomas Gleixner | 2dbf1bc | 2015-06-23 15:52:50 +0200 | [diff] [blame] | 2457 | irq_set_handler_locked(d, handle_level_irq); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2458 | |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2459 | raw_spin_lock_irqsave(&bank->slock, flags); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2460 | irq_gc_lock(gc); |
| 2461 | |
| 2462 | level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL); |
| 2463 | polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY); |
| 2464 | |
| 2465 | switch (type) { |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2466 | case IRQ_TYPE_EDGE_BOTH: |
| 2467 | bank->toggle_edge_mode |= mask; |
| 2468 | level |= mask; |
| 2469 | |
| 2470 | /* |
| 2471 | * Determine gpio state. If 1 next interrupt should be falling |
| 2472 | * otherwise rising. |
| 2473 | */ |
| 2474 | data = readl(bank->reg_base + GPIO_EXT_PORT); |
| 2475 | if (data & mask) |
| 2476 | polarity &= ~mask; |
| 2477 | else |
| 2478 | polarity |= mask; |
| 2479 | break; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2480 | case IRQ_TYPE_EDGE_RISING: |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2481 | bank->toggle_edge_mode &= ~mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2482 | level |= mask; |
| 2483 | polarity |= mask; |
| 2484 | break; |
| 2485 | case IRQ_TYPE_EDGE_FALLING: |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2486 | bank->toggle_edge_mode &= ~mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2487 | level |= mask; |
| 2488 | polarity &= ~mask; |
| 2489 | break; |
| 2490 | case IRQ_TYPE_LEVEL_HIGH: |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2491 | bank->toggle_edge_mode &= ~mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2492 | level &= ~mask; |
| 2493 | polarity |= mask; |
| 2494 | break; |
| 2495 | case IRQ_TYPE_LEVEL_LOW: |
Heiko Stübner | 5a92750 | 2013-10-16 01:09:08 +0200 | [diff] [blame] | 2496 | bank->toggle_edge_mode &= ~mask; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2497 | level &= ~mask; |
| 2498 | polarity &= ~mask; |
| 2499 | break; |
| 2500 | default: |
Axel Lin | 7cc5f97 | 2013-06-23 08:48:34 +0800 | [diff] [blame] | 2501 | irq_gc_unlock(gc); |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2502 | raw_spin_unlock_irqrestore(&bank->slock, flags); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2503 | return -EINVAL; |
| 2504 | } |
| 2505 | |
| 2506 | writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL); |
| 2507 | writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY); |
| 2508 | |
| 2509 | irq_gc_unlock(gc); |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2510 | raw_spin_unlock_irqrestore(&bank->slock, flags); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2511 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2512 | return 0; |
| 2513 | } |
| 2514 | |
Doug Anderson | 68bda47 | 2014-11-19 14:51:32 -0800 | [diff] [blame] | 2515 | static void rockchip_irq_suspend(struct irq_data *d) |
| 2516 | { |
| 2517 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 2518 | struct rockchip_pin_bank *bank = gc->private; |
| 2519 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2520 | clk_enable(bank->clk); |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 2521 | bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK); |
| 2522 | irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2523 | clk_disable(bank->clk); |
Doug Anderson | 68bda47 | 2014-11-19 14:51:32 -0800 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | static void rockchip_irq_resume(struct irq_data *d) |
| 2527 | { |
| 2528 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 2529 | struct rockchip_pin_bank *bank = gc->private; |
| 2530 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2531 | clk_enable(bank->clk); |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 2532 | irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2533 | clk_disable(bank->clk); |
| 2534 | } |
| 2535 | |
Jeffy Chen | d468289 | 2017-03-02 13:56:52 +0800 | [diff] [blame] | 2536 | static void rockchip_irq_enable(struct irq_data *d) |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2537 | { |
| 2538 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 2539 | struct rockchip_pin_bank *bank = gc->private; |
| 2540 | |
| 2541 | clk_enable(bank->clk); |
| 2542 | irq_gc_mask_clr_bit(d); |
| 2543 | } |
| 2544 | |
Jeffy Chen | d468289 | 2017-03-02 13:56:52 +0800 | [diff] [blame] | 2545 | static void rockchip_irq_disable(struct irq_data *d) |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2546 | { |
| 2547 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 2548 | struct rockchip_pin_bank *bank = gc->private; |
| 2549 | |
| 2550 | irq_gc_mask_set_bit(d); |
| 2551 | clk_disable(bank->clk); |
Doug Anderson | f2dd028 | 2014-11-19 14:51:33 -0800 | [diff] [blame] | 2552 | } |
| 2553 | |
John Keeping | 88bb942 | 2017-03-23 10:59:31 +0000 | [diff] [blame] | 2554 | static void rockchip_irq_bus_lock(struct irq_data *d) |
| 2555 | { |
| 2556 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 2557 | struct rockchip_pin_bank *bank = gc->private; |
| 2558 | |
| 2559 | clk_enable(bank->clk); |
| 2560 | mutex_lock(&bank->irq_lock); |
| 2561 | } |
| 2562 | |
| 2563 | static void rockchip_irq_bus_sync_unlock(struct irq_data *d) |
| 2564 | { |
| 2565 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 2566 | struct rockchip_pin_bank *bank = gc->private; |
| 2567 | |
| 2568 | while (bank->new_irqs) { |
| 2569 | unsigned int irq = __ffs(bank->new_irqs); |
| 2570 | int ret; |
| 2571 | |
| 2572 | ret = rockchip_set_mux(bank, irq, RK_FUNC_GPIO); |
| 2573 | WARN_ON(ret < 0); |
| 2574 | |
| 2575 | bank->new_irqs &= ~BIT(irq); |
| 2576 | } |
| 2577 | |
| 2578 | mutex_unlock(&bank->irq_lock); |
| 2579 | clk_disable(bank->clk); |
| 2580 | } |
| 2581 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2582 | static int rockchip_interrupts_register(struct platform_device *pdev, |
| 2583 | struct rockchip_pinctrl *info) |
| 2584 | { |
| 2585 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 2586 | struct rockchip_pin_bank *bank = ctrl->pin_banks; |
| 2587 | unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN; |
| 2588 | struct irq_chip_generic *gc; |
| 2589 | int ret; |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2590 | int i, j; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2591 | |
| 2592 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
| 2593 | if (!bank->valid) { |
| 2594 | dev_warn(&pdev->dev, "bank %s is not valid\n", |
| 2595 | bank->name); |
| 2596 | continue; |
| 2597 | } |
| 2598 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2599 | ret = clk_enable(bank->clk); |
| 2600 | if (ret) { |
| 2601 | dev_err(&pdev->dev, "failed to enable clock for bank %s\n", |
| 2602 | bank->name); |
| 2603 | continue; |
| 2604 | } |
| 2605 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2606 | bank->domain = irq_domain_add_linear(bank->of_node, 32, |
| 2607 | &irq_generic_chip_ops, NULL); |
| 2608 | if (!bank->domain) { |
| 2609 | dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n", |
| 2610 | bank->name); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2611 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2612 | continue; |
| 2613 | } |
| 2614 | |
| 2615 | ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1, |
| 2616 | "rockchip_gpio_irq", handle_level_irq, |
| 2617 | clr, 0, IRQ_GC_INIT_MASK_CACHE); |
| 2618 | if (ret) { |
| 2619 | dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n", |
| 2620 | bank->name); |
| 2621 | irq_domain_remove(bank->domain); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2622 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2623 | continue; |
| 2624 | } |
| 2625 | |
Doug Anderson | 5ae0c7a | 2015-01-26 08:24:03 -0800 | [diff] [blame] | 2626 | /* |
| 2627 | * Linux assumes that all interrupts start out disabled/masked. |
| 2628 | * Our driver only uses the concept of masked and always keeps |
| 2629 | * things enabled, so for us that's all masked and all enabled. |
| 2630 | */ |
| 2631 | writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK); |
| 2632 | writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN); |
| 2633 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2634 | gc = irq_get_domain_generic_chip(bank->domain, 0); |
| 2635 | gc->reg_base = bank->reg_base; |
| 2636 | gc->private = bank; |
Doug Anderson | f2dd028 | 2014-11-19 14:51:33 -0800 | [diff] [blame] | 2637 | gc->chip_types[0].regs.mask = GPIO_INTMASK; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2638 | gc->chip_types[0].regs.ack = GPIO_PORTS_EOI; |
| 2639 | gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit; |
Jeffy Chen | d468289 | 2017-03-02 13:56:52 +0800 | [diff] [blame] | 2640 | gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit; |
| 2641 | gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit; |
| 2642 | gc->chip_types[0].chip.irq_enable = rockchip_irq_enable; |
| 2643 | gc->chip_types[0].chip.irq_disable = rockchip_irq_disable; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2644 | gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake; |
Doug Anderson | 68bda47 | 2014-11-19 14:51:32 -0800 | [diff] [blame] | 2645 | gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend; |
| 2646 | gc->chip_types[0].chip.irq_resume = rockchip_irq_resume; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2647 | gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type; |
John Keeping | 88bb942 | 2017-03-23 10:59:31 +0000 | [diff] [blame] | 2648 | gc->chip_types[0].chip.irq_bus_lock = rockchip_irq_bus_lock; |
| 2649 | gc->chip_types[0].chip.irq_bus_sync_unlock = |
| 2650 | rockchip_irq_bus_sync_unlock; |
Doug Anderson | 876d716 | 2014-10-21 10:47:32 -0700 | [diff] [blame] | 2651 | gc->wake_enabled = IRQ_MSK(bank->nr_pins); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2652 | |
Thomas Gleixner | 03051bc | 2015-06-21 21:11:06 +0200 | [diff] [blame] | 2653 | irq_set_chained_handler_and_data(bank->irq, |
| 2654 | rockchip_irq_demux, bank); |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2655 | |
| 2656 | /* map the gpio irqs here, when the clock is still running */ |
| 2657 | for (j = 0 ; j < 32 ; j++) |
| 2658 | irq_create_mapping(bank->domain, j); |
| 2659 | |
| 2660 | clk_disable(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | return 0; |
| 2664 | } |
| 2665 | |
| 2666 | static int rockchip_gpiolib_register(struct platform_device *pdev, |
| 2667 | struct rockchip_pinctrl *info) |
| 2668 | { |
| 2669 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 2670 | struct rockchip_pin_bank *bank = ctrl->pin_banks; |
| 2671 | struct gpio_chip *gc; |
| 2672 | int ret; |
| 2673 | int i; |
| 2674 | |
| 2675 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
| 2676 | if (!bank->valid) { |
| 2677 | dev_warn(&pdev->dev, "bank %s is not valid\n", |
| 2678 | bank->name); |
| 2679 | continue; |
| 2680 | } |
| 2681 | |
| 2682 | bank->gpio_chip = rockchip_gpiolib_chip; |
| 2683 | |
| 2684 | gc = &bank->gpio_chip; |
| 2685 | gc->base = bank->pin_base; |
| 2686 | gc->ngpio = bank->nr_pins; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 2687 | gc->parent = &pdev->dev; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2688 | gc->of_node = bank->of_node; |
| 2689 | gc->label = bank->name; |
| 2690 | |
Linus Walleij | 03bf81f | 2015-12-08 09:39:13 +0100 | [diff] [blame] | 2691 | ret = gpiochip_add_data(gc, bank); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2692 | if (ret) { |
| 2693 | dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n", |
| 2694 | gc->label, ret); |
| 2695 | goto fail; |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | rockchip_interrupts_register(pdev, info); |
| 2700 | |
| 2701 | return 0; |
| 2702 | |
| 2703 | fail: |
| 2704 | for (--i, --bank; i >= 0; --i, --bank) { |
| 2705 | if (!bank->valid) |
| 2706 | continue; |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 2707 | gpiochip_remove(&bank->gpio_chip); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2708 | } |
| 2709 | return ret; |
| 2710 | } |
| 2711 | |
| 2712 | static int rockchip_gpiolib_unregister(struct platform_device *pdev, |
| 2713 | struct rockchip_pinctrl *info) |
| 2714 | { |
| 2715 | struct rockchip_pin_ctrl *ctrl = info->ctrl; |
| 2716 | struct rockchip_pin_bank *bank = ctrl->pin_banks; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2717 | int i; |
| 2718 | |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 2719 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2720 | if (!bank->valid) |
| 2721 | continue; |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 2722 | gpiochip_remove(&bank->gpio_chip); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 2725 | return 0; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | static int rockchip_get_bank_data(struct rockchip_pin_bank *bank, |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 2729 | struct rockchip_pinctrl *info) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2730 | { |
| 2731 | struct resource res; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 2732 | void __iomem *base; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2733 | |
| 2734 | if (of_address_to_resource(bank->of_node, 0, &res)) { |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 2735 | dev_err(info->dev, "cannot find IO resource for bank\n"); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2736 | return -ENOENT; |
| 2737 | } |
| 2738 | |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 2739 | bank->reg_base = devm_ioremap_resource(info->dev, &res); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2740 | if (IS_ERR(bank->reg_base)) |
| 2741 | return PTR_ERR(bank->reg_base); |
| 2742 | |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 2743 | /* |
| 2744 | * special case, where parts of the pull setting-registers are |
| 2745 | * part of the PMU register space |
| 2746 | */ |
| 2747 | if (of_device_is_compatible(bank->of_node, |
| 2748 | "rockchip,rk3188-gpio-bank0")) { |
Heiko Stübner | a658efa | 2014-05-05 13:59:30 +0200 | [diff] [blame] | 2749 | struct device_node *node; |
Heiko Stübner | bfc7a42 | 2014-05-05 13:58:00 +0200 | [diff] [blame] | 2750 | |
Heiko Stübner | a658efa | 2014-05-05 13:59:30 +0200 | [diff] [blame] | 2751 | node = of_parse_phandle(bank->of_node->parent, |
| 2752 | "rockchip,pmu", 0); |
| 2753 | if (!node) { |
| 2754 | if (of_address_to_resource(bank->of_node, 1, &res)) { |
| 2755 | dev_err(info->dev, "cannot find IO resource for bank\n"); |
| 2756 | return -ENOENT; |
| 2757 | } |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 2758 | |
Heiko Stübner | a658efa | 2014-05-05 13:59:30 +0200 | [diff] [blame] | 2759 | base = devm_ioremap_resource(info->dev, &res); |
| 2760 | if (IS_ERR(base)) |
| 2761 | return PTR_ERR(base); |
| 2762 | rockchip_regmap_config.max_register = |
| 2763 | resource_size(&res) - 4; |
| 2764 | rockchip_regmap_config.name = |
| 2765 | "rockchip,rk3188-gpio-bank0-pull"; |
| 2766 | bank->regmap_pull = devm_regmap_init_mmio(info->dev, |
| 2767 | base, |
| 2768 | &rockchip_regmap_config); |
| 2769 | } |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 2770 | } |
Heiko Stübner | 65fca61 | 2013-10-16 01:07:49 +0200 | [diff] [blame] | 2771 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2772 | bank->irq = irq_of_parse_and_map(bank->of_node, 0); |
| 2773 | |
| 2774 | bank->clk = of_clk_get(bank->of_node, 0); |
| 2775 | if (IS_ERR(bank->clk)) |
| 2776 | return PTR_ERR(bank->clk); |
| 2777 | |
Lin Huang | 07a06ae | 2015-08-11 18:12:04 +0800 | [diff] [blame] | 2778 | return clk_prepare(bank->clk); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | static const struct of_device_id rockchip_pinctrl_dt_match[]; |
| 2782 | |
| 2783 | /* retrieve the soc specific data */ |
| 2784 | static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data( |
| 2785 | struct rockchip_pinctrl *d, |
| 2786 | struct platform_device *pdev) |
| 2787 | { |
| 2788 | const struct of_device_id *match; |
| 2789 | struct device_node *node = pdev->dev.of_node; |
| 2790 | struct device_node *np; |
| 2791 | struct rockchip_pin_ctrl *ctrl; |
| 2792 | struct rockchip_pin_bank *bank; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 2793 | int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2794 | |
| 2795 | match = of_match_node(rockchip_pinctrl_dt_match, node); |
| 2796 | ctrl = (struct rockchip_pin_ctrl *)match->data; |
| 2797 | |
| 2798 | for_each_child_of_node(node, np) { |
| 2799 | if (!of_find_property(np, "gpio-controller", NULL)) |
| 2800 | continue; |
| 2801 | |
| 2802 | bank = ctrl->pin_banks; |
| 2803 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
| 2804 | if (!strcmp(bank->name, np->name)) { |
| 2805 | bank->of_node = np; |
| 2806 | |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 2807 | if (!rockchip_get_bank_data(bank, d)) |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2808 | bank->valid = true; |
| 2809 | |
| 2810 | break; |
| 2811 | } |
| 2812 | } |
| 2813 | } |
| 2814 | |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 2815 | grf_offs = ctrl->grf_mux_offset; |
| 2816 | pmu_offs = ctrl->pmu_mux_offset; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 2817 | drv_pmu_offs = ctrl->pmu_drv_offset; |
| 2818 | drv_grf_offs = ctrl->grf_drv_offset; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2819 | bank = ctrl->pin_banks; |
| 2820 | for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2821 | int bank_pins = 0; |
| 2822 | |
John Keeping | 70b7aa7 | 2017-03-23 10:59:29 +0000 | [diff] [blame] | 2823 | raw_spin_lock_init(&bank->slock); |
John Keeping | 88bb942 | 2017-03-23 10:59:31 +0000 | [diff] [blame] | 2824 | mutex_init(&bank->irq_lock); |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2825 | bank->drvdata = d; |
| 2826 | bank->pin_base = ctrl->nr_pins; |
| 2827 | ctrl->nr_pins += bank->nr_pins; |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2828 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 2829 | /* calculate iomux and drv offsets */ |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2830 | for (j = 0; j < 4; j++) { |
| 2831 | struct rockchip_iomux *iom = &bank->iomux[j]; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 2832 | struct rockchip_drv *drv = &bank->drv[j]; |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 2833 | int inc; |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2834 | |
| 2835 | if (bank_pins >= bank->nr_pins) |
| 2836 | break; |
| 2837 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 2838 | /* preset iomux offset value, set new start value */ |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2839 | if (iom->offset >= 0) { |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 2840 | if (iom->type & IOMUX_SOURCE_PMU) |
| 2841 | pmu_offs = iom->offset; |
| 2842 | else |
| 2843 | grf_offs = iom->offset; |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 2844 | } else { /* set current iomux offset */ |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 2845 | iom->offset = (iom->type & IOMUX_SOURCE_PMU) ? |
| 2846 | pmu_offs : grf_offs; |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 2849 | /* preset drv offset value, set new start value */ |
| 2850 | if (drv->offset >= 0) { |
| 2851 | if (iom->type & IOMUX_SOURCE_PMU) |
| 2852 | drv_pmu_offs = drv->offset; |
| 2853 | else |
| 2854 | drv_grf_offs = drv->offset; |
| 2855 | } else { /* set current drv offset */ |
| 2856 | drv->offset = (iom->type & IOMUX_SOURCE_PMU) ? |
| 2857 | drv_pmu_offs : drv_grf_offs; |
| 2858 | } |
| 2859 | |
| 2860 | dev_dbg(d->dev, "bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n", |
| 2861 | i, j, iom->offset, drv->offset); |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2862 | |
| 2863 | /* |
| 2864 | * Increase offset according to iomux width. |
Heiko Stübner | 03716e1 | 2014-06-16 01:36:57 +0200 | [diff] [blame] | 2865 | * 4bit iomux'es are spread over two registers. |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2866 | */ |
david.wu | 8b6c6f9 | 2017-02-10 18:23:47 +0800 | [diff] [blame] | 2867 | inc = (iom->type & (IOMUX_WIDTH_4BIT | |
| 2868 | IOMUX_WIDTH_3BIT)) ? 8 : 4; |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 2869 | if (iom->type & IOMUX_SOURCE_PMU) |
| 2870 | pmu_offs += inc; |
| 2871 | else |
| 2872 | grf_offs += inc; |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2873 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 2874 | /* |
| 2875 | * Increase offset according to drv width. |
| 2876 | * 3bit drive-strenth'es are spread over two registers. |
| 2877 | */ |
| 2878 | if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) || |
| 2879 | (drv->drv_type == DRV_TYPE_IO_3V3_ONLY)) |
| 2880 | inc = 8; |
| 2881 | else |
| 2882 | inc = 4; |
| 2883 | |
| 2884 | if (iom->type & IOMUX_SOURCE_PMU) |
| 2885 | drv_pmu_offs += inc; |
| 2886 | else |
| 2887 | drv_grf_offs += inc; |
| 2888 | |
Heiko Stübner | 6bc0d12 | 2014-06-16 01:36:33 +0200 | [diff] [blame] | 2889 | bank_pins += 8; |
| 2890 | } |
David Wu | bd35b9b | 2017-05-26 15:20:20 +0800 | [diff] [blame] | 2891 | |
| 2892 | /* calculate the per-bank route_mask */ |
| 2893 | for (j = 0; j < ctrl->niomux_routes; j++) { |
| 2894 | int pin = 0; |
| 2895 | |
| 2896 | if (ctrl->iomux_routes[j].bank_num == bank->bank_num) { |
| 2897 | pin = ctrl->iomux_routes[j].pin; |
| 2898 | bank->route_mask |= BIT(pin); |
| 2899 | } |
| 2900 | } |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | return ctrl; |
| 2904 | } |
| 2905 | |
Chris Zhong | 8dca933 | 2014-10-29 19:52:00 +0800 | [diff] [blame] | 2906 | #define RK3288_GRF_GPIO6C_IOMUX 0x64 |
| 2907 | #define GPIO6C6_SEL_WRITE_ENABLE BIT(28) |
| 2908 | |
| 2909 | static u32 rk3288_grf_gpio6c_iomux; |
| 2910 | |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 2911 | static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev) |
| 2912 | { |
| 2913 | struct rockchip_pinctrl *info = dev_get_drvdata(dev); |
Chris Zhong | 8dca933 | 2014-10-29 19:52:00 +0800 | [diff] [blame] | 2914 | int ret = pinctrl_force_sleep(info->pctl_dev); |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 2915 | |
Chris Zhong | 8dca933 | 2014-10-29 19:52:00 +0800 | [diff] [blame] | 2916 | if (ret) |
| 2917 | return ret; |
| 2918 | |
| 2919 | /* |
| 2920 | * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save |
| 2921 | * the setting here, and restore it at resume. |
| 2922 | */ |
| 2923 | if (info->ctrl->type == RK3288) { |
| 2924 | ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX, |
| 2925 | &rk3288_grf_gpio6c_iomux); |
| 2926 | if (ret) { |
| 2927 | pinctrl_force_default(info->pctl_dev); |
| 2928 | return ret; |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | return 0; |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | static int __maybe_unused rockchip_pinctrl_resume(struct device *dev) |
| 2936 | { |
| 2937 | struct rockchip_pinctrl *info = dev_get_drvdata(dev); |
Chris Zhong | 8dca933 | 2014-10-29 19:52:00 +0800 | [diff] [blame] | 2938 | int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX, |
| 2939 | rk3288_grf_gpio6c_iomux | |
| 2940 | GPIO6C6_SEL_WRITE_ENABLE); |
| 2941 | |
| 2942 | if (ret) |
| 2943 | return ret; |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 2944 | |
| 2945 | return pinctrl_force_default(info->pctl_dev); |
| 2946 | } |
| 2947 | |
| 2948 | static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend, |
| 2949 | rockchip_pinctrl_resume); |
| 2950 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2951 | static int rockchip_pinctrl_probe(struct platform_device *pdev) |
| 2952 | { |
| 2953 | struct rockchip_pinctrl *info; |
| 2954 | struct device *dev = &pdev->dev; |
| 2955 | struct rockchip_pin_ctrl *ctrl; |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 2956 | struct device_node *np = pdev->dev.of_node, *node; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2957 | struct resource *res; |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 2958 | void __iomem *base; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2959 | int ret; |
| 2960 | |
| 2961 | if (!dev->of_node) { |
| 2962 | dev_err(dev, "device tree node not found\n"); |
| 2963 | return -ENODEV; |
| 2964 | } |
| 2965 | |
| 2966 | info = devm_kzalloc(dev, sizeof(struct rockchip_pinctrl), GFP_KERNEL); |
| 2967 | if (!info) |
| 2968 | return -ENOMEM; |
| 2969 | |
Heiko Stübner | 622f323 | 2014-05-05 13:58:46 +0200 | [diff] [blame] | 2970 | info->dev = dev; |
| 2971 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2972 | ctrl = rockchip_pinctrl_get_soc_data(info, pdev); |
| 2973 | if (!ctrl) { |
| 2974 | dev_err(dev, "driver data not available\n"); |
| 2975 | return -EINVAL; |
| 2976 | } |
| 2977 | info->ctrl = ctrl; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 2978 | |
Heiko Stübner | 1e747e5 | 2014-05-05 13:59:51 +0200 | [diff] [blame] | 2979 | node = of_parse_phandle(np, "rockchip,grf", 0); |
| 2980 | if (node) { |
| 2981 | info->regmap_base = syscon_node_to_regmap(node); |
| 2982 | if (IS_ERR(info->regmap_base)) |
| 2983 | return PTR_ERR(info->regmap_base); |
| 2984 | } else { |
| 2985 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Heiko Stübner | 751a99a | 2014-05-05 13:58:20 +0200 | [diff] [blame] | 2986 | base = devm_ioremap_resource(&pdev->dev, res); |
| 2987 | if (IS_ERR(base)) |
| 2988 | return PTR_ERR(base); |
| 2989 | |
| 2990 | rockchip_regmap_config.max_register = resource_size(res) - 4; |
Heiko Stübner | 1e747e5 | 2014-05-05 13:59:51 +0200 | [diff] [blame] | 2991 | rockchip_regmap_config.name = "rockchip,pinctrl"; |
| 2992 | info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base, |
| 2993 | &rockchip_regmap_config); |
| 2994 | |
| 2995 | /* to check for the old dt-bindings */ |
| 2996 | info->reg_size = resource_size(res); |
| 2997 | |
| 2998 | /* Honor the old binding, with pull registers as 2nd resource */ |
| 2999 | if (ctrl->type == RK3188 && info->reg_size < 0x200) { |
| 3000 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 3001 | base = devm_ioremap_resource(&pdev->dev, res); |
| 3002 | if (IS_ERR(base)) |
| 3003 | return PTR_ERR(base); |
| 3004 | |
| 3005 | rockchip_regmap_config.max_register = |
| 3006 | resource_size(res) - 4; |
| 3007 | rockchip_regmap_config.name = "rockchip,pinctrl-pull"; |
| 3008 | info->regmap_pull = devm_regmap_init_mmio(&pdev->dev, |
| 3009 | base, |
| 3010 | &rockchip_regmap_config); |
| 3011 | } |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 3012 | } |
| 3013 | |
Heiko Stübner | 14dee86 | 2014-05-05 13:59:09 +0200 | [diff] [blame] | 3014 | /* try to find the optional reference to the pmu syscon */ |
| 3015 | node = of_parse_phandle(np, "rockchip,pmu", 0); |
| 3016 | if (node) { |
| 3017 | info->regmap_pmu = syscon_node_to_regmap(node); |
| 3018 | if (IS_ERR(info->regmap_pmu)) |
| 3019 | return PTR_ERR(info->regmap_pmu); |
| 3020 | } |
| 3021 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3022 | ret = rockchip_gpiolib_register(pdev, info); |
| 3023 | if (ret) |
| 3024 | return ret; |
| 3025 | |
| 3026 | ret = rockchip_pinctrl_register(pdev, info); |
| 3027 | if (ret) { |
| 3028 | rockchip_gpiolib_unregister(pdev, info); |
| 3029 | return ret; |
| 3030 | } |
| 3031 | |
| 3032 | platform_set_drvdata(pdev, info); |
| 3033 | |
| 3034 | return 0; |
| 3035 | } |
| 3036 | |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 3037 | static struct rockchip_pin_bank rv1108_pin_banks[] = { |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 3038 | PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU, |
| 3039 | IOMUX_SOURCE_PMU, |
| 3040 | IOMUX_SOURCE_PMU, |
| 3041 | IOMUX_SOURCE_PMU), |
| 3042 | PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0), |
| 3043 | PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0), |
| 3044 | PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0), |
| 3045 | }; |
| 3046 | |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 3047 | static struct rockchip_pin_ctrl rv1108_pin_ctrl = { |
| 3048 | .pin_banks = rv1108_pin_banks, |
| 3049 | .nr_banks = ARRAY_SIZE(rv1108_pin_banks), |
| 3050 | .label = "RV1108-GPIO", |
| 3051 | .type = RV1108, |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 3052 | .grf_mux_offset = 0x10, |
| 3053 | .pmu_mux_offset = 0x0, |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 3054 | .pull_calc_reg = rv1108_calc_pull_reg_and_bit, |
| 3055 | .drv_calc_reg = rv1108_calc_drv_reg_and_bit, |
Andy Yan | 688daf2 | 2016-11-15 18:02:43 +0800 | [diff] [blame] | 3056 | }; |
| 3057 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3058 | static struct rockchip_pin_bank rk2928_pin_banks[] = { |
| 3059 | PIN_BANK(0, 32, "gpio0"), |
| 3060 | PIN_BANK(1, 32, "gpio1"), |
| 3061 | PIN_BANK(2, 32, "gpio2"), |
| 3062 | PIN_BANK(3, 32, "gpio3"), |
| 3063 | }; |
| 3064 | |
| 3065 | static struct rockchip_pin_ctrl rk2928_pin_ctrl = { |
| 3066 | .pin_banks = rk2928_pin_banks, |
| 3067 | .nr_banks = ARRAY_SIZE(rk2928_pin_banks), |
| 3068 | .label = "RK2928-GPIO", |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 3069 | .type = RK2928, |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 3070 | .grf_mux_offset = 0xa8, |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 3071 | .pull_calc_reg = rk2928_calc_pull_reg_and_bit, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3072 | }; |
| 3073 | |
Xing Zheng | c5ce767 | 2015-08-28 13:46:47 +0800 | [diff] [blame] | 3074 | static struct rockchip_pin_bank rk3036_pin_banks[] = { |
| 3075 | PIN_BANK(0, 32, "gpio0"), |
| 3076 | PIN_BANK(1, 32, "gpio1"), |
| 3077 | PIN_BANK(2, 32, "gpio2"), |
| 3078 | }; |
| 3079 | |
| 3080 | static struct rockchip_pin_ctrl rk3036_pin_ctrl = { |
| 3081 | .pin_banks = rk3036_pin_banks, |
| 3082 | .nr_banks = ARRAY_SIZE(rk3036_pin_banks), |
| 3083 | .label = "RK3036-GPIO", |
| 3084 | .type = RK2928, |
| 3085 | .grf_mux_offset = 0xa8, |
| 3086 | .pull_calc_reg = rk2928_calc_pull_reg_and_bit, |
| 3087 | }; |
| 3088 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3089 | static struct rockchip_pin_bank rk3066a_pin_banks[] = { |
| 3090 | PIN_BANK(0, 32, "gpio0"), |
| 3091 | PIN_BANK(1, 32, "gpio1"), |
| 3092 | PIN_BANK(2, 32, "gpio2"), |
| 3093 | PIN_BANK(3, 32, "gpio3"), |
| 3094 | PIN_BANK(4, 32, "gpio4"), |
| 3095 | PIN_BANK(6, 16, "gpio6"), |
| 3096 | }; |
| 3097 | |
| 3098 | static struct rockchip_pin_ctrl rk3066a_pin_ctrl = { |
| 3099 | .pin_banks = rk3066a_pin_banks, |
| 3100 | .nr_banks = ARRAY_SIZE(rk3066a_pin_banks), |
| 3101 | .label = "RK3066a-GPIO", |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 3102 | .type = RK2928, |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 3103 | .grf_mux_offset = 0xa8, |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 3104 | .pull_calc_reg = rk2928_calc_pull_reg_and_bit, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3105 | }; |
| 3106 | |
| 3107 | static struct rockchip_pin_bank rk3066b_pin_banks[] = { |
| 3108 | PIN_BANK(0, 32, "gpio0"), |
| 3109 | PIN_BANK(1, 32, "gpio1"), |
| 3110 | PIN_BANK(2, 32, "gpio2"), |
| 3111 | PIN_BANK(3, 32, "gpio3"), |
| 3112 | }; |
| 3113 | |
| 3114 | static struct rockchip_pin_ctrl rk3066b_pin_ctrl = { |
| 3115 | .pin_banks = rk3066b_pin_banks, |
| 3116 | .nr_banks = ARRAY_SIZE(rk3066b_pin_banks), |
| 3117 | .label = "RK3066b-GPIO", |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 3118 | .type = RK3066B, |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 3119 | .grf_mux_offset = 0x60, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3120 | }; |
| 3121 | |
| 3122 | static struct rockchip_pin_bank rk3188_pin_banks[] = { |
Heiko Stübner | fc72c92 | 2014-06-16 01:36:05 +0200 | [diff] [blame] | 3123 | 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] | 3124 | PIN_BANK(1, 32, "gpio1"), |
| 3125 | PIN_BANK(2, 32, "gpio2"), |
| 3126 | PIN_BANK(3, 32, "gpio3"), |
| 3127 | }; |
| 3128 | |
| 3129 | static struct rockchip_pin_ctrl rk3188_pin_ctrl = { |
| 3130 | .pin_banks = rk3188_pin_banks, |
| 3131 | .nr_banks = ARRAY_SIZE(rk3188_pin_banks), |
| 3132 | .label = "RK3188-GPIO", |
Heiko Stübner | a282926 | 2013-10-16 01:07:20 +0200 | [diff] [blame] | 3133 | .type = RK3188, |
Heiko Stübner | 95ec8ae | 2014-06-16 01:37:23 +0200 | [diff] [blame] | 3134 | .grf_mux_offset = 0x60, |
Heiko Stübner | 6ca5274 | 2013-10-16 01:08:42 +0200 | [diff] [blame] | 3135 | .pull_calc_reg = rk3188_calc_pull_reg_and_bit, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3136 | }; |
| 3137 | |
Jeffy Chen | fea0fe6 | 2015-12-09 17:04:06 +0800 | [diff] [blame] | 3138 | static struct rockchip_pin_bank rk3228_pin_banks[] = { |
| 3139 | PIN_BANK(0, 32, "gpio0"), |
| 3140 | PIN_BANK(1, 32, "gpio1"), |
| 3141 | PIN_BANK(2, 32, "gpio2"), |
| 3142 | PIN_BANK(3, 32, "gpio3"), |
| 3143 | }; |
| 3144 | |
| 3145 | static struct rockchip_pin_ctrl rk3228_pin_ctrl = { |
| 3146 | .pin_banks = rk3228_pin_banks, |
| 3147 | .nr_banks = ARRAY_SIZE(rk3228_pin_banks), |
| 3148 | .label = "RK3228-GPIO", |
| 3149 | .type = RK3288, |
| 3150 | .grf_mux_offset = 0x0, |
David Wu | d4970ee | 2017-05-26 15:20:21 +0800 | [diff] [blame] | 3151 | .iomux_routes = rk3228_mux_route_data, |
| 3152 | .niomux_routes = ARRAY_SIZE(rk3228_mux_route_data), |
Jeffy Chen | fea0fe6 | 2015-12-09 17:04:06 +0800 | [diff] [blame] | 3153 | .pull_calc_reg = rk3228_calc_pull_reg_and_bit, |
| 3154 | .drv_calc_reg = rk3228_calc_drv_reg_and_bit, |
| 3155 | }; |
| 3156 | |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 3157 | static struct rockchip_pin_bank rk3288_pin_banks[] = { |
| 3158 | PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU, |
| 3159 | IOMUX_SOURCE_PMU, |
| 3160 | IOMUX_SOURCE_PMU, |
| 3161 | IOMUX_UNROUTED |
| 3162 | ), |
| 3163 | PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED, |
| 3164 | IOMUX_UNROUTED, |
| 3165 | IOMUX_UNROUTED, |
| 3166 | 0 |
| 3167 | ), |
| 3168 | PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED), |
| 3169 | PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT), |
| 3170 | PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT, |
| 3171 | IOMUX_WIDTH_4BIT, |
| 3172 | 0, |
| 3173 | 0 |
| 3174 | ), |
| 3175 | PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED, |
| 3176 | 0, |
| 3177 | 0, |
| 3178 | IOMUX_UNROUTED |
| 3179 | ), |
| 3180 | PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED), |
| 3181 | PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0, |
| 3182 | 0, |
| 3183 | IOMUX_WIDTH_4BIT, |
| 3184 | IOMUX_UNROUTED |
| 3185 | ), |
| 3186 | PIN_BANK(8, 16, "gpio8"), |
| 3187 | }; |
| 3188 | |
| 3189 | static struct rockchip_pin_ctrl rk3288_pin_ctrl = { |
| 3190 | .pin_banks = rk3288_pin_banks, |
| 3191 | .nr_banks = ARRAY_SIZE(rk3288_pin_banks), |
| 3192 | .label = "RK3288-GPIO", |
Heiko Stübner | 66d750e | 2014-07-20 01:49:17 +0200 | [diff] [blame] | 3193 | .type = RK3288, |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 3194 | .grf_mux_offset = 0x0, |
| 3195 | .pmu_mux_offset = 0x84, |
| 3196 | .pull_calc_reg = rk3288_calc_pull_reg_and_bit, |
Heiko Stübner | ef17f69 | 2015-06-12 23:50:11 +0200 | [diff] [blame] | 3197 | .drv_calc_reg = rk3288_calc_drv_reg_and_bit, |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 3198 | }; |
| 3199 | |
david.wu | 3818e4a | 2017-02-10 18:23:49 +0800 | [diff] [blame] | 3200 | static struct rockchip_pin_bank rk3328_pin_banks[] = { |
| 3201 | PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0), |
| 3202 | PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0), |
| 3203 | PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, |
| 3204 | IOMUX_WIDTH_3BIT | IOMUX_RECALCED, |
| 3205 | IOMUX_WIDTH_3BIT | IOMUX_RECALCED, |
| 3206 | 0), |
| 3207 | PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", |
| 3208 | IOMUX_WIDTH_3BIT, |
| 3209 | IOMUX_WIDTH_3BIT | IOMUX_RECALCED, |
| 3210 | 0, |
| 3211 | 0), |
| 3212 | }; |
| 3213 | |
| 3214 | static struct rockchip_pin_ctrl rk3328_pin_ctrl = { |
| 3215 | .pin_banks = rk3328_pin_banks, |
| 3216 | .nr_banks = ARRAY_SIZE(rk3328_pin_banks), |
| 3217 | .label = "RK3328-GPIO", |
| 3218 | .type = RK3288, |
| 3219 | .grf_mux_offset = 0x0, |
David Wu | cedc964 | 2017-05-26 15:20:22 +0800 | [diff] [blame] | 3220 | .iomux_routes = rk3328_mux_route_data, |
| 3221 | .niomux_routes = ARRAY_SIZE(rk3328_mux_route_data), |
david.wu | 3818e4a | 2017-02-10 18:23:49 +0800 | [diff] [blame] | 3222 | .pull_calc_reg = rk3228_calc_pull_reg_and_bit, |
| 3223 | .drv_calc_reg = rk3228_calc_drv_reg_and_bit, |
| 3224 | .iomux_recalc = rk3328_recalc_mux, |
david.wu | 728d3f5 | 2017-03-02 15:11:24 +0800 | [diff] [blame] | 3225 | .schmitt_calc_reg = rk3328_calc_schmitt_reg_and_bit, |
david.wu | 3818e4a | 2017-02-10 18:23:49 +0800 | [diff] [blame] | 3226 | }; |
| 3227 | |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 3228 | static struct rockchip_pin_bank rk3368_pin_banks[] = { |
| 3229 | PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU, |
| 3230 | IOMUX_SOURCE_PMU, |
| 3231 | IOMUX_SOURCE_PMU, |
| 3232 | IOMUX_SOURCE_PMU |
| 3233 | ), |
| 3234 | PIN_BANK(1, 32, "gpio1"), |
| 3235 | PIN_BANK(2, 32, "gpio2"), |
| 3236 | PIN_BANK(3, 32, "gpio3"), |
| 3237 | }; |
| 3238 | |
| 3239 | static struct rockchip_pin_ctrl rk3368_pin_ctrl = { |
| 3240 | .pin_banks = rk3368_pin_banks, |
| 3241 | .nr_banks = ARRAY_SIZE(rk3368_pin_banks), |
| 3242 | .label = "RK3368-GPIO", |
| 3243 | .type = RK3368, |
| 3244 | .grf_mux_offset = 0x0, |
| 3245 | .pmu_mux_offset = 0x0, |
| 3246 | .pull_calc_reg = rk3368_calc_pull_reg_and_bit, |
| 3247 | .drv_calc_reg = rk3368_calc_drv_reg_and_bit, |
| 3248 | }; |
| 3249 | |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 3250 | static struct rockchip_pin_bank rk3399_pin_banks[] = { |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 3251 | PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0", |
| 3252 | IOMUX_SOURCE_PMU, |
| 3253 | IOMUX_SOURCE_PMU, |
| 3254 | IOMUX_SOURCE_PMU, |
| 3255 | IOMUX_SOURCE_PMU, |
| 3256 | DRV_TYPE_IO_1V8_ONLY, |
| 3257 | DRV_TYPE_IO_1V8_ONLY, |
| 3258 | DRV_TYPE_IO_DEFAULT, |
| 3259 | DRV_TYPE_IO_DEFAULT, |
| 3260 | 0x0, |
| 3261 | 0x8, |
| 3262 | -1, |
| 3263 | -1, |
| 3264 | PULL_TYPE_IO_1V8_ONLY, |
| 3265 | PULL_TYPE_IO_1V8_ONLY, |
| 3266 | PULL_TYPE_IO_DEFAULT, |
| 3267 | PULL_TYPE_IO_DEFAULT |
| 3268 | ), |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 3269 | PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU, |
| 3270 | IOMUX_SOURCE_PMU, |
| 3271 | IOMUX_SOURCE_PMU, |
| 3272 | IOMUX_SOURCE_PMU, |
| 3273 | DRV_TYPE_IO_1V8_OR_3V0, |
| 3274 | DRV_TYPE_IO_1V8_OR_3V0, |
| 3275 | DRV_TYPE_IO_1V8_OR_3V0, |
| 3276 | DRV_TYPE_IO_1V8_OR_3V0, |
| 3277 | 0x20, |
| 3278 | 0x28, |
| 3279 | 0x30, |
| 3280 | 0x38 |
| 3281 | ), |
David Wu | 3ba6767 | 2016-05-11 11:39:28 +0800 | [diff] [blame] | 3282 | PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0, |
| 3283 | DRV_TYPE_IO_1V8_OR_3V0, |
| 3284 | DRV_TYPE_IO_1V8_ONLY, |
| 3285 | DRV_TYPE_IO_1V8_ONLY, |
| 3286 | PULL_TYPE_IO_DEFAULT, |
| 3287 | PULL_TYPE_IO_DEFAULT, |
| 3288 | PULL_TYPE_IO_1V8_ONLY, |
| 3289 | PULL_TYPE_IO_1V8_ONLY |
| 3290 | ), |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 3291 | PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY, |
| 3292 | DRV_TYPE_IO_3V3_ONLY, |
| 3293 | DRV_TYPE_IO_3V3_ONLY, |
| 3294 | DRV_TYPE_IO_1V8_OR_3V0 |
| 3295 | ), |
| 3296 | PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0, |
| 3297 | DRV_TYPE_IO_1V8_3V0_AUTO, |
| 3298 | DRV_TYPE_IO_1V8_OR_3V0, |
| 3299 | DRV_TYPE_IO_1V8_OR_3V0 |
| 3300 | ), |
| 3301 | }; |
| 3302 | |
| 3303 | static struct rockchip_pin_ctrl rk3399_pin_ctrl = { |
| 3304 | .pin_banks = rk3399_pin_banks, |
| 3305 | .nr_banks = ARRAY_SIZE(rk3399_pin_banks), |
| 3306 | .label = "RK3399-GPIO", |
| 3307 | .type = RK3399, |
| 3308 | .grf_mux_offset = 0xe000, |
| 3309 | .pmu_mux_offset = 0x0, |
| 3310 | .grf_drv_offset = 0xe100, |
| 3311 | .pmu_drv_offset = 0x80, |
David Wu | accc1ce | 2017-05-26 15:20:23 +0800 | [diff] [blame^] | 3312 | .iomux_routes = rk3399_mux_route_data, |
| 3313 | .niomux_routes = ARRAY_SIZE(rk3399_mux_route_data), |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 3314 | .pull_calc_reg = rk3399_calc_pull_reg_and_bit, |
| 3315 | .drv_calc_reg = rk3399_calc_drv_reg_and_bit, |
| 3316 | }; |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 3317 | |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3318 | static const struct of_device_id rockchip_pinctrl_dt_match[] = { |
Andy Yan | b9c6dca | 2017-03-17 18:18:36 +0100 | [diff] [blame] | 3319 | { .compatible = "rockchip,rv1108-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3320 | .data = &rv1108_pin_ctrl }, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3321 | { .compatible = "rockchip,rk2928-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3322 | .data = &rk2928_pin_ctrl }, |
Xing Zheng | c5ce767 | 2015-08-28 13:46:47 +0800 | [diff] [blame] | 3323 | { .compatible = "rockchip,rk3036-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3324 | .data = &rk3036_pin_ctrl }, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3325 | { .compatible = "rockchip,rk3066a-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3326 | .data = &rk3066a_pin_ctrl }, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3327 | { .compatible = "rockchip,rk3066b-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3328 | .data = &rk3066b_pin_ctrl }, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3329 | { .compatible = "rockchip,rk3188-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3330 | .data = &rk3188_pin_ctrl }, |
Jeffy Chen | fea0fe6 | 2015-12-09 17:04:06 +0800 | [diff] [blame] | 3331 | { .compatible = "rockchip,rk3228-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3332 | .data = &rk3228_pin_ctrl }, |
Heiko Stübner | 304f077 | 2014-06-16 01:38:14 +0200 | [diff] [blame] | 3333 | { .compatible = "rockchip,rk3288-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3334 | .data = &rk3288_pin_ctrl }, |
david.wu | 3818e4a | 2017-02-10 18:23:49 +0800 | [diff] [blame] | 3335 | { .compatible = "rockchip,rk3328-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3336 | .data = &rk3328_pin_ctrl }, |
Heiko Stübner | daecdc6 | 2015-06-12 23:51:01 +0200 | [diff] [blame] | 3337 | { .compatible = "rockchip,rk3368-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3338 | .data = &rk3368_pin_ctrl }, |
David Wu | b6c2327 | 2016-02-01 10:58:21 +0800 | [diff] [blame] | 3339 | { .compatible = "rockchip,rk3399-pinctrl", |
Masahiro Yamada | cdbbd26 | 2017-04-28 21:50:35 +0900 | [diff] [blame] | 3340 | .data = &rk3399_pin_ctrl }, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3341 | {}, |
| 3342 | }; |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3343 | |
| 3344 | static struct platform_driver rockchip_pinctrl_driver = { |
| 3345 | .probe = rockchip_pinctrl_probe, |
| 3346 | .driver = { |
| 3347 | .name = "rockchip-pinctrl", |
Chris Zhong | 9198f50 | 2014-10-29 19:51:59 +0800 | [diff] [blame] | 3348 | .pm = &rockchip_pinctrl_dev_pm_ops, |
Axel Lin | 0be9e70 | 2013-08-23 14:27:53 +0800 | [diff] [blame] | 3349 | .of_match_table = rockchip_pinctrl_dt_match, |
Heiko Stübner | d3e5116 | 2013-06-10 22:16:22 +0200 | [diff] [blame] | 3350 | }, |
| 3351 | }; |
| 3352 | |
| 3353 | static int __init rockchip_pinctrl_drv_register(void) |
| 3354 | { |
| 3355 | return platform_driver_register(&rockchip_pinctrl_driver); |
| 3356 | } |
| 3357 | postcore_initcall(rockchip_pinctrl_drv_register); |