1d3e51161SHeiko Stübner /* 2d3e51161SHeiko Stübner * Pinctrl driver for Rockchip SoCs 3d3e51161SHeiko Stübner * 4d3e51161SHeiko Stübner * Copyright (c) 2013 MundoReader S.L. 5d3e51161SHeiko Stübner * Author: Heiko Stuebner <heiko@sntech.de> 6d3e51161SHeiko Stübner * 7d3e51161SHeiko Stübner * With some ideas taken from pinctrl-samsung: 8d3e51161SHeiko Stübner * Copyright (c) 2012 Samsung Electronics Co., Ltd. 9d3e51161SHeiko Stübner * http://www.samsung.com 10d3e51161SHeiko Stübner * Copyright (c) 2012 Linaro Ltd 11d3e51161SHeiko Stübner * http://www.linaro.org 12d3e51161SHeiko Stübner * 13d3e51161SHeiko Stübner * and pinctrl-at91: 14d3e51161SHeiko Stübner * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> 15d3e51161SHeiko Stübner * 16d3e51161SHeiko Stübner * This program is free software; you can redistribute it and/or modify 17d3e51161SHeiko Stübner * it under the terms of the GNU General Public License version 2 as published 18d3e51161SHeiko Stübner * by the Free Software Foundation. 19d3e51161SHeiko Stübner * 20d3e51161SHeiko Stübner * This program is distributed in the hope that it will be useful, 21d3e51161SHeiko Stübner * but WITHOUT ANY WARRANTY; without even the implied warranty of 22d3e51161SHeiko Stübner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23d3e51161SHeiko Stübner * GNU General Public License for more details. 24d3e51161SHeiko Stübner */ 25d3e51161SHeiko Stübner 262f436204SPaul Gortmaker #include <linux/init.h> 27d3e51161SHeiko Stübner #include <linux/platform_device.h> 28d3e51161SHeiko Stübner #include <linux/io.h> 29d3e51161SHeiko Stübner #include <linux/bitops.h> 30d3e51161SHeiko Stübner #include <linux/gpio.h> 31d3e51161SHeiko Stübner #include <linux/of_address.h> 32d3e51161SHeiko Stübner #include <linux/of_irq.h> 33d3e51161SHeiko Stübner #include <linux/pinctrl/machine.h> 34d3e51161SHeiko Stübner #include <linux/pinctrl/pinconf.h> 35d3e51161SHeiko Stübner #include <linux/pinctrl/pinctrl.h> 36d3e51161SHeiko Stübner #include <linux/pinctrl/pinmux.h> 37d3e51161SHeiko Stübner #include <linux/pinctrl/pinconf-generic.h> 38d3e51161SHeiko Stübner #include <linux/irqchip/chained_irq.h> 397e865abbSHeiko Stübner #include <linux/clk.h> 40751a99abSHeiko Stübner #include <linux/regmap.h> 4114dee867SHeiko Stübner #include <linux/mfd/syscon.h> 42d3e51161SHeiko Stübner #include <dt-bindings/pinctrl/rockchip.h> 43d3e51161SHeiko Stübner 44d3e51161SHeiko Stübner #include "core.h" 45d3e51161SHeiko Stübner #include "pinconf.h" 46d3e51161SHeiko Stübner 47d3e51161SHeiko Stübner /* GPIO control registers */ 48d3e51161SHeiko Stübner #define GPIO_SWPORT_DR 0x00 49d3e51161SHeiko Stübner #define GPIO_SWPORT_DDR 0x04 50d3e51161SHeiko Stübner #define GPIO_INTEN 0x30 51d3e51161SHeiko Stübner #define GPIO_INTMASK 0x34 52d3e51161SHeiko Stübner #define GPIO_INTTYPE_LEVEL 0x38 53d3e51161SHeiko Stübner #define GPIO_INT_POLARITY 0x3c 54d3e51161SHeiko Stübner #define GPIO_INT_STATUS 0x40 55d3e51161SHeiko Stübner #define GPIO_INT_RAWSTATUS 0x44 56d3e51161SHeiko Stübner #define GPIO_DEBOUNCE 0x48 57d3e51161SHeiko Stübner #define GPIO_PORTS_EOI 0x4c 58d3e51161SHeiko Stübner #define GPIO_EXT_PORT 0x50 59d3e51161SHeiko Stübner #define GPIO_LS_SYNC 0x60 60d3e51161SHeiko Stübner 61a282926dSHeiko Stübner enum rockchip_pinctrl_type { 62b9c6dcabSAndy Yan RV1108, 63a282926dSHeiko Stübner RK2928, 64a282926dSHeiko Stübner RK3066B, 65a282926dSHeiko Stübner RK3188, 6666d750e1SHeiko Stübner RK3288, 67daecdc66SHeiko Stübner RK3368, 68b6c23275SDavid Wu RK3399, 69a282926dSHeiko Stübner }; 70a282926dSHeiko Stübner 71fc72c923SHeiko Stübner /** 72fc72c923SHeiko Stübner * Encode variants of iomux registers into a type variable 73fc72c923SHeiko Stübner */ 74fc72c923SHeiko Stübner #define IOMUX_GPIO_ONLY BIT(0) 7503716e1dSHeiko Stübner #define IOMUX_WIDTH_4BIT BIT(1) 7695ec8ae4SHeiko Stübner #define IOMUX_SOURCE_PMU BIT(2) 7762f49226SHeiko Stübner #define IOMUX_UNROUTED BIT(3) 788b6c6f93Sdavid.wu #define IOMUX_WIDTH_3BIT BIT(4) 79ea262ad6Sdavid.wu #define IOMUX_RECALCED BIT(5) 80fc72c923SHeiko Stübner 81fc72c923SHeiko Stübner /** 82fc72c923SHeiko Stübner * @type: iomux variant using IOMUX_* constants 836bc0d121SHeiko Stübner * @offset: if initialized to -1 it will be autocalculated, by specifying 846bc0d121SHeiko Stübner * an initial offset value the relevant source offset can be reset 856bc0d121SHeiko Stübner * to a new value for autocalculating the following iomux registers. 86fc72c923SHeiko Stübner */ 87fc72c923SHeiko Stübner struct rockchip_iomux { 88fc72c923SHeiko Stübner int type; 896bc0d121SHeiko Stübner int offset; 9065fca613SHeiko Stübner }; 9165fca613SHeiko Stübner 92d3e51161SHeiko Stübner /** 93b6c23275SDavid Wu * enum type index corresponding to rockchip_perpin_drv_list arrays index. 94b6c23275SDavid Wu */ 95b6c23275SDavid Wu enum rockchip_pin_drv_type { 96b6c23275SDavid Wu DRV_TYPE_IO_DEFAULT = 0, 97b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0, 98b6c23275SDavid Wu DRV_TYPE_IO_1V8_ONLY, 99b6c23275SDavid Wu DRV_TYPE_IO_1V8_3V0_AUTO, 100b6c23275SDavid Wu DRV_TYPE_IO_3V3_ONLY, 101b6c23275SDavid Wu DRV_TYPE_MAX 102b6c23275SDavid Wu }; 103b6c23275SDavid Wu 104b6c23275SDavid Wu /** 1053ba6767aSDavid Wu * enum type index corresponding to rockchip_pull_list arrays index. 1063ba6767aSDavid Wu */ 1073ba6767aSDavid Wu enum rockchip_pin_pull_type { 1083ba6767aSDavid Wu PULL_TYPE_IO_DEFAULT = 0, 1093ba6767aSDavid Wu PULL_TYPE_IO_1V8_ONLY, 1103ba6767aSDavid Wu PULL_TYPE_MAX 1113ba6767aSDavid Wu }; 1123ba6767aSDavid Wu 1133ba6767aSDavid Wu /** 114b6c23275SDavid Wu * @drv_type: drive strength variant using rockchip_perpin_drv_type 115b6c23275SDavid Wu * @offset: if initialized to -1 it will be autocalculated, by specifying 116b6c23275SDavid Wu * an initial offset value the relevant source offset can be reset 117b6c23275SDavid Wu * to a new value for autocalculating the following drive strength 118b6c23275SDavid Wu * registers. if used chips own cal_drv func instead to calculate 119b6c23275SDavid Wu * registers offset, the variant could be ignored. 120b6c23275SDavid Wu */ 121b6c23275SDavid Wu struct rockchip_drv { 122b6c23275SDavid Wu enum rockchip_pin_drv_type drv_type; 123b6c23275SDavid Wu int offset; 124b6c23275SDavid Wu }; 125b6c23275SDavid Wu 126b6c23275SDavid Wu /** 127d3e51161SHeiko Stübner * @reg_base: register base of the gpio bank 1286ca5274dSHeiko Stübner * @reg_pull: optional separate register for additional pull settings 129d3e51161SHeiko Stübner * @clk: clock of the gpio bank 130d3e51161SHeiko Stübner * @irq: interrupt of the gpio bank 1315ae0c7adSDoug Anderson * @saved_masks: Saved content of GPIO_INTEN at suspend time. 132d3e51161SHeiko Stübner * @pin_base: first pin number 133d3e51161SHeiko Stübner * @nr_pins: number of pins in this bank 134d3e51161SHeiko Stübner * @name: name of the bank 135d3e51161SHeiko Stübner * @bank_num: number of the bank, to account for holes 136fc72c923SHeiko Stübner * @iomux: array describing the 4 iomux sources of the bank 137b6c23275SDavid Wu * @drv: array describing the 4 drive strength sources of the bank 1383ba6767aSDavid Wu * @pull_type: array describing the 4 pull type sources of the bank 139d3e51161SHeiko Stübner * @valid: are all necessary informations present 140d3e51161SHeiko Stübner * @of_node: dt node of this bank 141d3e51161SHeiko Stübner * @drvdata: common pinctrl basedata 142d3e51161SHeiko Stübner * @domain: irqdomain of the gpio bank 143d3e51161SHeiko Stübner * @gpio_chip: gpiolib chip 144d3e51161SHeiko Stübner * @grange: gpio range 145d3e51161SHeiko Stübner * @slock: spinlock for the gpio bank 14688bb9421SJohn Keeping * @irq_lock: bus lock for irq chip 14788bb9421SJohn Keeping * @new_irqs: newly configured irqs which must be muxed as GPIOs in 14888bb9421SJohn Keeping * irq_bus_sync_unlock() 149d3e51161SHeiko Stübner */ 150d3e51161SHeiko Stübner struct rockchip_pin_bank { 151d3e51161SHeiko Stübner void __iomem *reg_base; 152751a99abSHeiko Stübner struct regmap *regmap_pull; 153d3e51161SHeiko Stübner struct clk *clk; 154d3e51161SHeiko Stübner int irq; 1555ae0c7adSDoug Anderson u32 saved_masks; 156d3e51161SHeiko Stübner u32 pin_base; 157d3e51161SHeiko Stübner u8 nr_pins; 158d3e51161SHeiko Stübner char *name; 159d3e51161SHeiko Stübner u8 bank_num; 160fc72c923SHeiko Stübner struct rockchip_iomux iomux[4]; 161b6c23275SDavid Wu struct rockchip_drv drv[4]; 1623ba6767aSDavid Wu enum rockchip_pin_pull_type pull_type[4]; 163d3e51161SHeiko Stübner bool valid; 164d3e51161SHeiko Stübner struct device_node *of_node; 165d3e51161SHeiko Stübner struct rockchip_pinctrl *drvdata; 166d3e51161SHeiko Stübner struct irq_domain *domain; 167d3e51161SHeiko Stübner struct gpio_chip gpio_chip; 168d3e51161SHeiko Stübner struct pinctrl_gpio_range grange; 16970b7aa7aSJohn Keeping raw_spinlock_t slock; 1705a927501SHeiko Stübner u32 toggle_edge_mode; 17188bb9421SJohn Keeping struct mutex irq_lock; 17288bb9421SJohn Keeping u32 new_irqs; 173d3e51161SHeiko Stübner }; 174d3e51161SHeiko Stübner 175d3e51161SHeiko Stübner #define PIN_BANK(id, pins, label) \ 176d3e51161SHeiko Stübner { \ 177d3e51161SHeiko Stübner .bank_num = id, \ 178d3e51161SHeiko Stübner .nr_pins = pins, \ 179d3e51161SHeiko Stübner .name = label, \ 1806bc0d121SHeiko Stübner .iomux = { \ 1816bc0d121SHeiko Stübner { .offset = -1 }, \ 1826bc0d121SHeiko Stübner { .offset = -1 }, \ 1836bc0d121SHeiko Stübner { .offset = -1 }, \ 1846bc0d121SHeiko Stübner { .offset = -1 }, \ 1856bc0d121SHeiko Stübner }, \ 186d3e51161SHeiko Stübner } 187d3e51161SHeiko Stübner 188fc72c923SHeiko Stübner #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \ 189fc72c923SHeiko Stübner { \ 190fc72c923SHeiko Stübner .bank_num = id, \ 191fc72c923SHeiko Stübner .nr_pins = pins, \ 192fc72c923SHeiko Stübner .name = label, \ 193fc72c923SHeiko Stübner .iomux = { \ 1946bc0d121SHeiko Stübner { .type = iom0, .offset = -1 }, \ 1956bc0d121SHeiko Stübner { .type = iom1, .offset = -1 }, \ 1966bc0d121SHeiko Stübner { .type = iom2, .offset = -1 }, \ 1976bc0d121SHeiko Stübner { .type = iom3, .offset = -1 }, \ 198fc72c923SHeiko Stübner }, \ 199fc72c923SHeiko Stübner } 200fc72c923SHeiko Stübner 201b6c23275SDavid Wu #define PIN_BANK_DRV_FLAGS(id, pins, label, type0, type1, type2, type3) \ 202b6c23275SDavid Wu { \ 203b6c23275SDavid Wu .bank_num = id, \ 204b6c23275SDavid Wu .nr_pins = pins, \ 205b6c23275SDavid Wu .name = label, \ 206b6c23275SDavid Wu .iomux = { \ 207b6c23275SDavid Wu { .offset = -1 }, \ 208b6c23275SDavid Wu { .offset = -1 }, \ 209b6c23275SDavid Wu { .offset = -1 }, \ 210b6c23275SDavid Wu { .offset = -1 }, \ 211b6c23275SDavid Wu }, \ 212b6c23275SDavid Wu .drv = { \ 213b6c23275SDavid Wu { .drv_type = type0, .offset = -1 }, \ 214b6c23275SDavid Wu { .drv_type = type1, .offset = -1 }, \ 215b6c23275SDavid Wu { .drv_type = type2, .offset = -1 }, \ 216b6c23275SDavid Wu { .drv_type = type3, .offset = -1 }, \ 217b6c23275SDavid Wu }, \ 218b6c23275SDavid Wu } 219b6c23275SDavid Wu 2203ba6767aSDavid Wu #define PIN_BANK_DRV_FLAGS_PULL_FLAGS(id, pins, label, drv0, drv1, \ 2213ba6767aSDavid Wu drv2, drv3, pull0, pull1, \ 2223ba6767aSDavid Wu pull2, pull3) \ 2233ba6767aSDavid Wu { \ 2243ba6767aSDavid Wu .bank_num = id, \ 2253ba6767aSDavid Wu .nr_pins = pins, \ 2263ba6767aSDavid Wu .name = label, \ 2273ba6767aSDavid Wu .iomux = { \ 2283ba6767aSDavid Wu { .offset = -1 }, \ 2293ba6767aSDavid Wu { .offset = -1 }, \ 2303ba6767aSDavid Wu { .offset = -1 }, \ 2313ba6767aSDavid Wu { .offset = -1 }, \ 2323ba6767aSDavid Wu }, \ 2333ba6767aSDavid Wu .drv = { \ 2343ba6767aSDavid Wu { .drv_type = drv0, .offset = -1 }, \ 2353ba6767aSDavid Wu { .drv_type = drv1, .offset = -1 }, \ 2363ba6767aSDavid Wu { .drv_type = drv2, .offset = -1 }, \ 2373ba6767aSDavid Wu { .drv_type = drv3, .offset = -1 }, \ 2383ba6767aSDavid Wu }, \ 2393ba6767aSDavid Wu .pull_type[0] = pull0, \ 2403ba6767aSDavid Wu .pull_type[1] = pull1, \ 2413ba6767aSDavid Wu .pull_type[2] = pull2, \ 2423ba6767aSDavid Wu .pull_type[3] = pull3, \ 2433ba6767aSDavid Wu } 2443ba6767aSDavid Wu 245b6c23275SDavid Wu #define PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(id, pins, label, iom0, iom1, \ 246b6c23275SDavid Wu iom2, iom3, drv0, drv1, drv2, \ 247b6c23275SDavid Wu drv3, offset0, offset1, \ 248b6c23275SDavid Wu offset2, offset3) \ 249b6c23275SDavid Wu { \ 250b6c23275SDavid Wu .bank_num = id, \ 251b6c23275SDavid Wu .nr_pins = pins, \ 252b6c23275SDavid Wu .name = label, \ 253b6c23275SDavid Wu .iomux = { \ 254b6c23275SDavid Wu { .type = iom0, .offset = -1 }, \ 255b6c23275SDavid Wu { .type = iom1, .offset = -1 }, \ 256b6c23275SDavid Wu { .type = iom2, .offset = -1 }, \ 257b6c23275SDavid Wu { .type = iom3, .offset = -1 }, \ 258b6c23275SDavid Wu }, \ 259b6c23275SDavid Wu .drv = { \ 260b6c23275SDavid Wu { .drv_type = drv0, .offset = offset0 }, \ 261b6c23275SDavid Wu { .drv_type = drv1, .offset = offset1 }, \ 262b6c23275SDavid Wu { .drv_type = drv2, .offset = offset2 }, \ 263b6c23275SDavid Wu { .drv_type = drv3, .offset = offset3 }, \ 264b6c23275SDavid Wu }, \ 265b6c23275SDavid Wu } 266b6c23275SDavid Wu 2673ba6767aSDavid Wu #define PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(id, pins, \ 2683ba6767aSDavid Wu label, iom0, iom1, iom2, \ 2693ba6767aSDavid Wu iom3, drv0, drv1, drv2, \ 2703ba6767aSDavid Wu drv3, offset0, offset1, \ 2713ba6767aSDavid Wu offset2, offset3, pull0, \ 2723ba6767aSDavid Wu pull1, pull2, pull3) \ 2733ba6767aSDavid Wu { \ 2743ba6767aSDavid Wu .bank_num = id, \ 2753ba6767aSDavid Wu .nr_pins = pins, \ 2763ba6767aSDavid Wu .name = label, \ 2773ba6767aSDavid Wu .iomux = { \ 2783ba6767aSDavid Wu { .type = iom0, .offset = -1 }, \ 2793ba6767aSDavid Wu { .type = iom1, .offset = -1 }, \ 2803ba6767aSDavid Wu { .type = iom2, .offset = -1 }, \ 2813ba6767aSDavid Wu { .type = iom3, .offset = -1 }, \ 2823ba6767aSDavid Wu }, \ 2833ba6767aSDavid Wu .drv = { \ 2843ba6767aSDavid Wu { .drv_type = drv0, .offset = offset0 }, \ 2853ba6767aSDavid Wu { .drv_type = drv1, .offset = offset1 }, \ 2863ba6767aSDavid Wu { .drv_type = drv2, .offset = offset2 }, \ 2873ba6767aSDavid Wu { .drv_type = drv3, .offset = offset3 }, \ 2883ba6767aSDavid Wu }, \ 2893ba6767aSDavid Wu .pull_type[0] = pull0, \ 2903ba6767aSDavid Wu .pull_type[1] = pull1, \ 2913ba6767aSDavid Wu .pull_type[2] = pull2, \ 2923ba6767aSDavid Wu .pull_type[3] = pull3, \ 2933ba6767aSDavid Wu } 2943ba6767aSDavid Wu 295d3e51161SHeiko Stübner /** 296d3e51161SHeiko Stübner */ 297d3e51161SHeiko Stübner struct rockchip_pin_ctrl { 298d3e51161SHeiko Stübner struct rockchip_pin_bank *pin_banks; 299d3e51161SHeiko Stübner u32 nr_banks; 300d3e51161SHeiko Stübner u32 nr_pins; 301d3e51161SHeiko Stübner char *label; 302a282926dSHeiko Stübner enum rockchip_pinctrl_type type; 30395ec8ae4SHeiko Stübner int grf_mux_offset; 30495ec8ae4SHeiko Stübner int pmu_mux_offset; 305b6c23275SDavid Wu int grf_drv_offset; 306b6c23275SDavid Wu int pmu_drv_offset; 307b6c23275SDavid Wu 308751a99abSHeiko Stübner void (*pull_calc_reg)(struct rockchip_pin_bank *bank, 309751a99abSHeiko Stübner int pin_num, struct regmap **regmap, 310751a99abSHeiko Stübner int *reg, u8 *bit); 311ef17f69fSHeiko Stübner void (*drv_calc_reg)(struct rockchip_pin_bank *bank, 312ef17f69fSHeiko Stübner int pin_num, struct regmap **regmap, 313ef17f69fSHeiko Stübner int *reg, u8 *bit); 314ea262ad6Sdavid.wu void (*iomux_recalc)(u8 bank_num, int pin, int *reg, 315ea262ad6Sdavid.wu u8 *bit, int *mask); 316e3b357d7Sdavid.wu int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank, 317e3b357d7Sdavid.wu int pin_num, struct regmap **regmap, 318e3b357d7Sdavid.wu int *reg, u8 *bit); 319d3e51161SHeiko Stübner }; 320d3e51161SHeiko Stübner 321d3e51161SHeiko Stübner struct rockchip_pin_config { 322d3e51161SHeiko Stübner unsigned int func; 323d3e51161SHeiko Stübner unsigned long *configs; 324d3e51161SHeiko Stübner unsigned int nconfigs; 325d3e51161SHeiko Stübner }; 326d3e51161SHeiko Stübner 327d3e51161SHeiko Stübner /** 328d3e51161SHeiko Stübner * struct rockchip_pin_group: represent group of pins of a pinmux function. 329d3e51161SHeiko Stübner * @name: name of the pin group, used to lookup the group. 330d3e51161SHeiko Stübner * @pins: the pins included in this group. 331d3e51161SHeiko Stübner * @npins: number of pins included in this group. 332d3e51161SHeiko Stübner * @func: the mux function number to be programmed when selected. 333d3e51161SHeiko Stübner * @configs: the config values to be set for each pin 334d3e51161SHeiko Stübner * @nconfigs: number of configs for each pin 335d3e51161SHeiko Stübner */ 336d3e51161SHeiko Stübner struct rockchip_pin_group { 337d3e51161SHeiko Stübner const char *name; 338d3e51161SHeiko Stübner unsigned int npins; 339d3e51161SHeiko Stübner unsigned int *pins; 340d3e51161SHeiko Stübner struct rockchip_pin_config *data; 341d3e51161SHeiko Stübner }; 342d3e51161SHeiko Stübner 343d3e51161SHeiko Stübner /** 344d3e51161SHeiko Stübner * struct rockchip_pmx_func: represent a pin function. 345d3e51161SHeiko Stübner * @name: name of the pin function, used to lookup the function. 346d3e51161SHeiko Stübner * @groups: one or more names of pin groups that provide this function. 347d3e51161SHeiko Stübner * @num_groups: number of groups included in @groups. 348d3e51161SHeiko Stübner */ 349d3e51161SHeiko Stübner struct rockchip_pmx_func { 350d3e51161SHeiko Stübner const char *name; 351d3e51161SHeiko Stübner const char **groups; 352d3e51161SHeiko Stübner u8 ngroups; 353d3e51161SHeiko Stübner }; 354d3e51161SHeiko Stübner 355d3e51161SHeiko Stübner struct rockchip_pinctrl { 356751a99abSHeiko Stübner struct regmap *regmap_base; 357bfc7a42aSHeiko Stübner int reg_size; 358751a99abSHeiko Stübner struct regmap *regmap_pull; 35914dee867SHeiko Stübner struct regmap *regmap_pmu; 360d3e51161SHeiko Stübner struct device *dev; 361d3e51161SHeiko Stübner struct rockchip_pin_ctrl *ctrl; 362d3e51161SHeiko Stübner struct pinctrl_desc pctl; 363d3e51161SHeiko Stübner struct pinctrl_dev *pctl_dev; 364d3e51161SHeiko Stübner struct rockchip_pin_group *groups; 365d3e51161SHeiko Stübner unsigned int ngroups; 366d3e51161SHeiko Stübner struct rockchip_pmx_func *functions; 367d3e51161SHeiko Stübner unsigned int nfunctions; 368d3e51161SHeiko Stübner }; 369d3e51161SHeiko Stübner 370ea262ad6Sdavid.wu /** 371ea262ad6Sdavid.wu * struct rockchip_mux_recalced_data: represent a pin iomux data. 372ea262ad6Sdavid.wu * @num: bank number. 373ea262ad6Sdavid.wu * @pin: pin number. 374ea262ad6Sdavid.wu * @bit: index at register. 375ea262ad6Sdavid.wu * @reg: register offset. 376ea262ad6Sdavid.wu * @mask: mask bit 377ea262ad6Sdavid.wu */ 378ea262ad6Sdavid.wu struct rockchip_mux_recalced_data { 379ea262ad6Sdavid.wu u8 num; 380ea262ad6Sdavid.wu u8 pin; 381ea262ad6Sdavid.wu u8 reg; 382ea262ad6Sdavid.wu u8 bit; 383ea262ad6Sdavid.wu u8 mask; 384ea262ad6Sdavid.wu }; 385ea262ad6Sdavid.wu 386751a99abSHeiko Stübner static struct regmap_config rockchip_regmap_config = { 387751a99abSHeiko Stübner .reg_bits = 32, 388751a99abSHeiko Stübner .val_bits = 32, 389751a99abSHeiko Stübner .reg_stride = 4, 390751a99abSHeiko Stübner }; 391751a99abSHeiko Stübner 39256411f3cSArnd Bergmann static inline const struct rockchip_pin_group *pinctrl_name_to_group( 393d3e51161SHeiko Stübner const struct rockchip_pinctrl *info, 394d3e51161SHeiko Stübner const char *name) 395d3e51161SHeiko Stübner { 396d3e51161SHeiko Stübner int i; 397d3e51161SHeiko Stübner 398d3e51161SHeiko Stübner for (i = 0; i < info->ngroups; i++) { 3991cb95395SAxel Lin if (!strcmp(info->groups[i].name, name)) 4001cb95395SAxel Lin return &info->groups[i]; 401d3e51161SHeiko Stübner } 402d3e51161SHeiko Stübner 4031cb95395SAxel Lin return NULL; 404d3e51161SHeiko Stübner } 405d3e51161SHeiko Stübner 406d3e51161SHeiko Stübner /* 407d3e51161SHeiko Stübner * given a pin number that is local to a pin controller, find out the pin bank 408d3e51161SHeiko Stübner * and the register base of the pin bank. 409d3e51161SHeiko Stübner */ 410d3e51161SHeiko Stübner static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info, 411d3e51161SHeiko Stübner unsigned pin) 412d3e51161SHeiko Stübner { 413d3e51161SHeiko Stübner struct rockchip_pin_bank *b = info->ctrl->pin_banks; 414d3e51161SHeiko Stübner 41551578b9bSAxel Lin while (pin >= (b->pin_base + b->nr_pins)) 416d3e51161SHeiko Stübner b++; 417d3e51161SHeiko Stübner 418d3e51161SHeiko Stübner return b; 419d3e51161SHeiko Stübner } 420d3e51161SHeiko Stübner 421d3e51161SHeiko Stübner static struct rockchip_pin_bank *bank_num_to_bank( 422d3e51161SHeiko Stübner struct rockchip_pinctrl *info, 423d3e51161SHeiko Stübner unsigned num) 424d3e51161SHeiko Stübner { 425d3e51161SHeiko Stübner struct rockchip_pin_bank *b = info->ctrl->pin_banks; 426d3e51161SHeiko Stübner int i; 427d3e51161SHeiko Stübner 4281cb95395SAxel Lin for (i = 0; i < info->ctrl->nr_banks; i++, b++) { 429d3e51161SHeiko Stübner if (b->bank_num == num) 4301cb95395SAxel Lin return b; 431d3e51161SHeiko Stübner } 432d3e51161SHeiko Stübner 433d3e51161SHeiko Stübner return ERR_PTR(-EINVAL); 434d3e51161SHeiko Stübner } 435d3e51161SHeiko Stübner 436d3e51161SHeiko Stübner /* 437d3e51161SHeiko Stübner * Pinctrl_ops handling 438d3e51161SHeiko Stübner */ 439d3e51161SHeiko Stübner 440d3e51161SHeiko Stübner static int rockchip_get_groups_count(struct pinctrl_dev *pctldev) 441d3e51161SHeiko Stübner { 442d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 443d3e51161SHeiko Stübner 444d3e51161SHeiko Stübner return info->ngroups; 445d3e51161SHeiko Stübner } 446d3e51161SHeiko Stübner 447d3e51161SHeiko Stübner static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev, 448d3e51161SHeiko Stübner unsigned selector) 449d3e51161SHeiko Stübner { 450d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 451d3e51161SHeiko Stübner 452d3e51161SHeiko Stübner return info->groups[selector].name; 453d3e51161SHeiko Stübner } 454d3e51161SHeiko Stübner 455d3e51161SHeiko Stübner static int rockchip_get_group_pins(struct pinctrl_dev *pctldev, 456d3e51161SHeiko Stübner unsigned selector, const unsigned **pins, 457d3e51161SHeiko Stübner unsigned *npins) 458d3e51161SHeiko Stübner { 459d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 460d3e51161SHeiko Stübner 461d3e51161SHeiko Stübner if (selector >= info->ngroups) 462d3e51161SHeiko Stübner return -EINVAL; 463d3e51161SHeiko Stübner 464d3e51161SHeiko Stübner *pins = info->groups[selector].pins; 465d3e51161SHeiko Stübner *npins = info->groups[selector].npins; 466d3e51161SHeiko Stübner 467d3e51161SHeiko Stübner return 0; 468d3e51161SHeiko Stübner } 469d3e51161SHeiko Stübner 470d3e51161SHeiko Stübner static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev, 471d3e51161SHeiko Stübner struct device_node *np, 472d3e51161SHeiko Stübner struct pinctrl_map **map, unsigned *num_maps) 473d3e51161SHeiko Stübner { 474d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 475d3e51161SHeiko Stübner const struct rockchip_pin_group *grp; 476d3e51161SHeiko Stübner struct pinctrl_map *new_map; 477d3e51161SHeiko Stübner struct device_node *parent; 478d3e51161SHeiko Stübner int map_num = 1; 479d3e51161SHeiko Stübner int i; 480d3e51161SHeiko Stübner 481d3e51161SHeiko Stübner /* 482d3e51161SHeiko Stübner * first find the group of this node and check if we need to create 483d3e51161SHeiko Stübner * config maps for pins 484d3e51161SHeiko Stübner */ 485d3e51161SHeiko Stübner grp = pinctrl_name_to_group(info, np->name); 486d3e51161SHeiko Stübner if (!grp) { 487d3e51161SHeiko Stübner dev_err(info->dev, "unable to find group for node %s\n", 488d3e51161SHeiko Stübner np->name); 489d3e51161SHeiko Stübner return -EINVAL; 490d3e51161SHeiko Stübner } 491d3e51161SHeiko Stübner 492d3e51161SHeiko Stübner map_num += grp->npins; 493d3e51161SHeiko Stübner new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, 494d3e51161SHeiko Stübner GFP_KERNEL); 495d3e51161SHeiko Stübner if (!new_map) 496d3e51161SHeiko Stübner return -ENOMEM; 497d3e51161SHeiko Stübner 498d3e51161SHeiko Stübner *map = new_map; 499d3e51161SHeiko Stübner *num_maps = map_num; 500d3e51161SHeiko Stübner 501d3e51161SHeiko Stübner /* create mux map */ 502d3e51161SHeiko Stübner parent = of_get_parent(np); 503d3e51161SHeiko Stübner if (!parent) { 504d3e51161SHeiko Stübner devm_kfree(pctldev->dev, new_map); 505d3e51161SHeiko Stübner return -EINVAL; 506d3e51161SHeiko Stübner } 507d3e51161SHeiko Stübner new_map[0].type = PIN_MAP_TYPE_MUX_GROUP; 508d3e51161SHeiko Stübner new_map[0].data.mux.function = parent->name; 509d3e51161SHeiko Stübner new_map[0].data.mux.group = np->name; 510d3e51161SHeiko Stübner of_node_put(parent); 511d3e51161SHeiko Stübner 512d3e51161SHeiko Stübner /* create config map */ 513d3e51161SHeiko Stübner new_map++; 514d3e51161SHeiko Stübner for (i = 0; i < grp->npins; i++) { 515d3e51161SHeiko Stübner new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN; 516d3e51161SHeiko Stübner new_map[i].data.configs.group_or_pin = 517d3e51161SHeiko Stübner pin_get_name(pctldev, grp->pins[i]); 518d3e51161SHeiko Stübner new_map[i].data.configs.configs = grp->data[i].configs; 519d3e51161SHeiko Stübner new_map[i].data.configs.num_configs = grp->data[i].nconfigs; 520d3e51161SHeiko Stübner } 521d3e51161SHeiko Stübner 522d3e51161SHeiko Stübner dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n", 523d3e51161SHeiko Stübner (*map)->data.mux.function, (*map)->data.mux.group, map_num); 524d3e51161SHeiko Stübner 525d3e51161SHeiko Stübner return 0; 526d3e51161SHeiko Stübner } 527d3e51161SHeiko Stübner 528d3e51161SHeiko Stübner static void rockchip_dt_free_map(struct pinctrl_dev *pctldev, 529d3e51161SHeiko Stübner struct pinctrl_map *map, unsigned num_maps) 530d3e51161SHeiko Stübner { 531d3e51161SHeiko Stübner } 532d3e51161SHeiko Stübner 533d3e51161SHeiko Stübner static const struct pinctrl_ops rockchip_pctrl_ops = { 534d3e51161SHeiko Stübner .get_groups_count = rockchip_get_groups_count, 535d3e51161SHeiko Stübner .get_group_name = rockchip_get_group_name, 536d3e51161SHeiko Stübner .get_group_pins = rockchip_get_group_pins, 537d3e51161SHeiko Stübner .dt_node_to_map = rockchip_dt_node_to_map, 538d3e51161SHeiko Stübner .dt_free_map = rockchip_dt_free_map, 539d3e51161SHeiko Stübner }; 540d3e51161SHeiko Stübner 541d3e51161SHeiko Stübner /* 542d3e51161SHeiko Stübner * Hardware access 543d3e51161SHeiko Stübner */ 544d3e51161SHeiko Stübner 5453818e4a7Sdavid.wu static const struct rockchip_mux_recalced_data rk3328_mux_recalced_data[] = { 5463818e4a7Sdavid.wu { 5473818e4a7Sdavid.wu .num = 2, 5483818e4a7Sdavid.wu .pin = 12, 5493818e4a7Sdavid.wu .reg = 0x24, 5503818e4a7Sdavid.wu .bit = 8, 5513818e4a7Sdavid.wu .mask = 0x3 5523818e4a7Sdavid.wu }, { 5533818e4a7Sdavid.wu .num = 2, 5543818e4a7Sdavid.wu .pin = 15, 5553818e4a7Sdavid.wu .reg = 0x28, 5563818e4a7Sdavid.wu .bit = 0, 5573818e4a7Sdavid.wu .mask = 0x7 5583818e4a7Sdavid.wu }, { 5593818e4a7Sdavid.wu .num = 2, 5603818e4a7Sdavid.wu .pin = 23, 5613818e4a7Sdavid.wu .reg = 0x30, 5623818e4a7Sdavid.wu .bit = 14, 5633818e4a7Sdavid.wu .mask = 0x3 5643818e4a7Sdavid.wu }, 5653818e4a7Sdavid.wu }; 5663818e4a7Sdavid.wu 5673818e4a7Sdavid.wu static void rk3328_recalc_mux(u8 bank_num, int pin, int *reg, 5683818e4a7Sdavid.wu u8 *bit, int *mask) 5693818e4a7Sdavid.wu { 5703818e4a7Sdavid.wu const struct rockchip_mux_recalced_data *data = NULL; 5713818e4a7Sdavid.wu int i; 5723818e4a7Sdavid.wu 5733818e4a7Sdavid.wu for (i = 0; i < ARRAY_SIZE(rk3328_mux_recalced_data); i++) 5743818e4a7Sdavid.wu if (rk3328_mux_recalced_data[i].num == bank_num && 5753818e4a7Sdavid.wu rk3328_mux_recalced_data[i].pin == pin) { 5763818e4a7Sdavid.wu data = &rk3328_mux_recalced_data[i]; 5773818e4a7Sdavid.wu break; 5783818e4a7Sdavid.wu } 5793818e4a7Sdavid.wu 5803818e4a7Sdavid.wu if (!data) 5813818e4a7Sdavid.wu return; 5823818e4a7Sdavid.wu 5833818e4a7Sdavid.wu *reg = data->reg; 5843818e4a7Sdavid.wu *mask = data->mask; 5853818e4a7Sdavid.wu *bit = data->bit; 5863818e4a7Sdavid.wu } 5873818e4a7Sdavid.wu 588a076e2edSHeiko Stübner static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin) 589a076e2edSHeiko Stübner { 590a076e2edSHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 591ea262ad6Sdavid.wu struct rockchip_pin_ctrl *ctrl = info->ctrl; 592fc72c923SHeiko Stübner int iomux_num = (pin / 8); 59395ec8ae4SHeiko Stübner struct regmap *regmap; 594751a99abSHeiko Stübner unsigned int val; 595ea262ad6Sdavid.wu int reg, ret, mask, mux_type; 596a076e2edSHeiko Stübner u8 bit; 597a076e2edSHeiko Stübner 598fc72c923SHeiko Stübner if (iomux_num > 3) 599fc72c923SHeiko Stübner return -EINVAL; 600fc72c923SHeiko Stübner 60162f49226SHeiko Stübner if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) { 60262f49226SHeiko Stübner dev_err(info->dev, "pin %d is unrouted\n", pin); 60362f49226SHeiko Stübner return -EINVAL; 60462f49226SHeiko Stübner } 60562f49226SHeiko Stübner 606fc72c923SHeiko Stübner if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) 607a076e2edSHeiko Stübner return RK_FUNC_GPIO; 608a076e2edSHeiko Stübner 60995ec8ae4SHeiko Stübner regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) 61095ec8ae4SHeiko Stübner ? info->regmap_pmu : info->regmap_base; 61195ec8ae4SHeiko Stübner 612a076e2edSHeiko Stübner /* get basic quadrupel of mux registers and the correct reg inside */ 613ea262ad6Sdavid.wu mux_type = bank->iomux[iomux_num].type; 6146bc0d121SHeiko Stübner reg = bank->iomux[iomux_num].offset; 615ea262ad6Sdavid.wu if (mux_type & IOMUX_WIDTH_4BIT) { 61603716e1dSHeiko Stübner if ((pin % 8) >= 4) 61703716e1dSHeiko Stübner reg += 0x4; 61803716e1dSHeiko Stübner bit = (pin % 4) * 4; 6198b6c6f93Sdavid.wu mask = 0xf; 620ea262ad6Sdavid.wu } else if (mux_type & IOMUX_WIDTH_3BIT) { 6218b6c6f93Sdavid.wu if ((pin % 8) >= 5) 6228b6c6f93Sdavid.wu reg += 0x4; 6238b6c6f93Sdavid.wu bit = (pin % 8 % 5) * 3; 6248b6c6f93Sdavid.wu mask = 0x7; 62503716e1dSHeiko Stübner } else { 626a076e2edSHeiko Stübner bit = (pin % 8) * 2; 6278b6c6f93Sdavid.wu mask = 0x3; 62803716e1dSHeiko Stübner } 629a076e2edSHeiko Stübner 630ea262ad6Sdavid.wu if (ctrl->iomux_recalc && (mux_type & IOMUX_RECALCED)) 631ea262ad6Sdavid.wu ctrl->iomux_recalc(bank->bank_num, pin, ®, &bit, &mask); 632ea262ad6Sdavid.wu 63395ec8ae4SHeiko Stübner ret = regmap_read(regmap, reg, &val); 634751a99abSHeiko Stübner if (ret) 635751a99abSHeiko Stübner return ret; 636751a99abSHeiko Stübner 63703716e1dSHeiko Stübner return ((val >> bit) & mask); 638a076e2edSHeiko Stübner } 639a076e2edSHeiko Stübner 64005709c3eSJohn Keeping static int rockchip_verify_mux(struct rockchip_pin_bank *bank, 64105709c3eSJohn Keeping int pin, int mux) 64205709c3eSJohn Keeping { 64305709c3eSJohn Keeping struct rockchip_pinctrl *info = bank->drvdata; 64405709c3eSJohn Keeping int iomux_num = (pin / 8); 64505709c3eSJohn Keeping 64605709c3eSJohn Keeping if (iomux_num > 3) 64705709c3eSJohn Keeping return -EINVAL; 64805709c3eSJohn Keeping 64905709c3eSJohn Keeping if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) { 65005709c3eSJohn Keeping dev_err(info->dev, "pin %d is unrouted\n", pin); 65105709c3eSJohn Keeping return -EINVAL; 65205709c3eSJohn Keeping } 65305709c3eSJohn Keeping 65405709c3eSJohn Keeping if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) { 65505709c3eSJohn Keeping if (mux != RK_FUNC_GPIO) { 65605709c3eSJohn Keeping dev_err(info->dev, 65705709c3eSJohn Keeping "pin %d only supports a gpio mux\n", pin); 65805709c3eSJohn Keeping return -ENOTSUPP; 65905709c3eSJohn Keeping } 66005709c3eSJohn Keeping } 66105709c3eSJohn Keeping 66205709c3eSJohn Keeping return 0; 66305709c3eSJohn Keeping } 66405709c3eSJohn Keeping 665d3e51161SHeiko Stübner /* 666d3e51161SHeiko Stübner * Set a new mux function for a pin. 667d3e51161SHeiko Stübner * 668d3e51161SHeiko Stübner * The register is divided into the upper and lower 16 bit. When changing 669d3e51161SHeiko Stübner * a value, the previous register value is not read and changed. Instead 670d3e51161SHeiko Stübner * it seems the changed bits are marked in the upper 16 bit, while the 671d3e51161SHeiko Stübner * changed value gets set in the same offset in the lower 16 bit. 672d3e51161SHeiko Stübner * All pin settings seem to be 2 bit wide in both the upper and lower 673d3e51161SHeiko Stübner * parts. 674d3e51161SHeiko Stübner * @bank: pin bank to change 675d3e51161SHeiko Stübner * @pin: pin to change 676d3e51161SHeiko Stübner * @mux: new mux function to set 677d3e51161SHeiko Stübner */ 67814797189SHeiko Stübner static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux) 679d3e51161SHeiko Stübner { 680d3e51161SHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 681ea262ad6Sdavid.wu struct rockchip_pin_ctrl *ctrl = info->ctrl; 682fc72c923SHeiko Stübner int iomux_num = (pin / 8); 68395ec8ae4SHeiko Stübner struct regmap *regmap; 684ea262ad6Sdavid.wu int reg, ret, mask, mux_type; 685d3e51161SHeiko Stübner u8 bit; 68699e872d9SSonny Rao u32 data, rmask; 687d3e51161SHeiko Stübner 68805709c3eSJohn Keeping ret = rockchip_verify_mux(bank, pin, mux); 68905709c3eSJohn Keeping if (ret < 0) 69005709c3eSJohn Keeping return ret; 691fc72c923SHeiko Stübner 69205709c3eSJohn Keeping if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) 693c4a532deSHeiko Stübner return 0; 694c4a532deSHeiko Stübner 695d3e51161SHeiko Stübner dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n", 696d3e51161SHeiko Stübner bank->bank_num, pin, mux); 697d3e51161SHeiko Stübner 69895ec8ae4SHeiko Stübner regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) 69995ec8ae4SHeiko Stübner ? info->regmap_pmu : info->regmap_base; 70095ec8ae4SHeiko Stübner 701d3e51161SHeiko Stübner /* get basic quadrupel of mux registers and the correct reg inside */ 702ea262ad6Sdavid.wu mux_type = bank->iomux[iomux_num].type; 7036bc0d121SHeiko Stübner reg = bank->iomux[iomux_num].offset; 704ea262ad6Sdavid.wu if (mux_type & IOMUX_WIDTH_4BIT) { 70503716e1dSHeiko Stübner if ((pin % 8) >= 4) 70603716e1dSHeiko Stübner reg += 0x4; 70703716e1dSHeiko Stübner bit = (pin % 4) * 4; 7088b6c6f93Sdavid.wu mask = 0xf; 709ea262ad6Sdavid.wu } else if (mux_type & IOMUX_WIDTH_3BIT) { 7108b6c6f93Sdavid.wu if ((pin % 8) >= 5) 7118b6c6f93Sdavid.wu reg += 0x4; 7128b6c6f93Sdavid.wu bit = (pin % 8 % 5) * 3; 7138b6c6f93Sdavid.wu mask = 0x7; 71403716e1dSHeiko Stübner } else { 715d3e51161SHeiko Stübner bit = (pin % 8) * 2; 7168b6c6f93Sdavid.wu mask = 0x3; 71703716e1dSHeiko Stübner } 718d3e51161SHeiko Stübner 719ea262ad6Sdavid.wu if (ctrl->iomux_recalc && (mux_type & IOMUX_RECALCED)) 720ea262ad6Sdavid.wu ctrl->iomux_recalc(bank->bank_num, pin, ®, &bit, &mask); 721ea262ad6Sdavid.wu 72203716e1dSHeiko Stübner data = (mask << (bit + 16)); 72399e872d9SSonny Rao rmask = data | (data >> 16); 72403716e1dSHeiko Stübner data |= (mux & mask) << bit; 72599e872d9SSonny Rao ret = regmap_update_bits(regmap, reg, rmask, data); 726d3e51161SHeiko Stübner 727751a99abSHeiko Stübner return ret; 728d3e51161SHeiko Stübner } 729d3e51161SHeiko Stübner 730b9c6dcabSAndy Yan #define RV1108_PULL_PMU_OFFSET 0x10 731b9c6dcabSAndy Yan #define RV1108_PULL_OFFSET 0x110 732b9c6dcabSAndy Yan #define RV1108_PULL_PINS_PER_REG 8 733b9c6dcabSAndy Yan #define RV1108_PULL_BITS_PER_PIN 2 734b9c6dcabSAndy Yan #define RV1108_PULL_BANK_STRIDE 16 735688daf23SAndy Yan 736b9c6dcabSAndy Yan static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, 737688daf23SAndy Yan int pin_num, struct regmap **regmap, 738688daf23SAndy Yan int *reg, u8 *bit) 739688daf23SAndy Yan { 740688daf23SAndy Yan struct rockchip_pinctrl *info = bank->drvdata; 741688daf23SAndy Yan 742688daf23SAndy Yan /* The first 24 pins of the first bank are located in PMU */ 743688daf23SAndy Yan if (bank->bank_num == 0) { 744688daf23SAndy Yan *regmap = info->regmap_pmu; 745b9c6dcabSAndy Yan *reg = RV1108_PULL_PMU_OFFSET; 746688daf23SAndy Yan } else { 747b9c6dcabSAndy Yan *reg = RV1108_PULL_OFFSET; 748688daf23SAndy Yan *regmap = info->regmap_base; 749688daf23SAndy Yan /* correct the offset, as we're starting with the 2nd bank */ 750688daf23SAndy Yan *reg -= 0x10; 751b9c6dcabSAndy Yan *reg += bank->bank_num * RV1108_PULL_BANK_STRIDE; 752688daf23SAndy Yan } 753688daf23SAndy Yan 754b9c6dcabSAndy Yan *reg += ((pin_num / RV1108_PULL_PINS_PER_REG) * 4); 755b9c6dcabSAndy Yan *bit = (pin_num % RV1108_PULL_PINS_PER_REG); 756b9c6dcabSAndy Yan *bit *= RV1108_PULL_BITS_PER_PIN; 757688daf23SAndy Yan } 758688daf23SAndy Yan 759b9c6dcabSAndy Yan #define RV1108_DRV_PMU_OFFSET 0x20 760b9c6dcabSAndy Yan #define RV1108_DRV_GRF_OFFSET 0x210 761b9c6dcabSAndy Yan #define RV1108_DRV_BITS_PER_PIN 2 762b9c6dcabSAndy Yan #define RV1108_DRV_PINS_PER_REG 8 763b9c6dcabSAndy Yan #define RV1108_DRV_BANK_STRIDE 16 764688daf23SAndy Yan 765b9c6dcabSAndy Yan static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, 766688daf23SAndy Yan int pin_num, struct regmap **regmap, 767688daf23SAndy Yan int *reg, u8 *bit) 768688daf23SAndy Yan { 769688daf23SAndy Yan struct rockchip_pinctrl *info = bank->drvdata; 770688daf23SAndy Yan 771688daf23SAndy Yan /* The first 24 pins of the first bank are located in PMU */ 772688daf23SAndy Yan if (bank->bank_num == 0) { 773688daf23SAndy Yan *regmap = info->regmap_pmu; 774b9c6dcabSAndy Yan *reg = RV1108_DRV_PMU_OFFSET; 775688daf23SAndy Yan } else { 776688daf23SAndy Yan *regmap = info->regmap_base; 777b9c6dcabSAndy Yan *reg = RV1108_DRV_GRF_OFFSET; 778688daf23SAndy Yan 779688daf23SAndy Yan /* correct the offset, as we're starting with the 2nd bank */ 780688daf23SAndy Yan *reg -= 0x10; 781b9c6dcabSAndy Yan *reg += bank->bank_num * RV1108_DRV_BANK_STRIDE; 782688daf23SAndy Yan } 783688daf23SAndy Yan 784b9c6dcabSAndy Yan *reg += ((pin_num / RV1108_DRV_PINS_PER_REG) * 4); 785b9c6dcabSAndy Yan *bit = pin_num % RV1108_DRV_PINS_PER_REG; 786b9c6dcabSAndy Yan *bit *= RV1108_DRV_BITS_PER_PIN; 787688daf23SAndy Yan } 788688daf23SAndy Yan 789a282926dSHeiko Stübner #define RK2928_PULL_OFFSET 0x118 790a282926dSHeiko Stübner #define RK2928_PULL_PINS_PER_REG 16 791a282926dSHeiko Stübner #define RK2928_PULL_BANK_STRIDE 8 792a282926dSHeiko Stübner 793a282926dSHeiko Stübner static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, 794751a99abSHeiko Stübner int pin_num, struct regmap **regmap, 795751a99abSHeiko Stübner int *reg, u8 *bit) 796a282926dSHeiko Stübner { 797a282926dSHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 798a282926dSHeiko Stübner 799751a99abSHeiko Stübner *regmap = info->regmap_base; 800751a99abSHeiko Stübner *reg = RK2928_PULL_OFFSET; 801a282926dSHeiko Stübner *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE; 802a282926dSHeiko Stübner *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4; 803a282926dSHeiko Stübner 804a282926dSHeiko Stübner *bit = pin_num % RK2928_PULL_PINS_PER_REG; 805a282926dSHeiko Stübner }; 806a282926dSHeiko Stübner 807bfc7a42aSHeiko Stübner #define RK3188_PULL_OFFSET 0x164 8086ca5274dSHeiko Stübner #define RK3188_PULL_BITS_PER_PIN 2 8096ca5274dSHeiko Stübner #define RK3188_PULL_PINS_PER_REG 8 8106ca5274dSHeiko Stübner #define RK3188_PULL_BANK_STRIDE 16 81114dee867SHeiko Stübner #define RK3188_PULL_PMU_OFFSET 0x64 8126ca5274dSHeiko Stübner 8136ca5274dSHeiko Stübner static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, 814751a99abSHeiko Stübner int pin_num, struct regmap **regmap, 815751a99abSHeiko Stübner int *reg, u8 *bit) 8166ca5274dSHeiko Stübner { 8176ca5274dSHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 8186ca5274dSHeiko Stübner 8196ca5274dSHeiko Stübner /* The first 12 pins of the first bank are located elsewhere */ 820fc72c923SHeiko Stübner if (bank->bank_num == 0 && pin_num < 12) { 82114dee867SHeiko Stübner *regmap = info->regmap_pmu ? info->regmap_pmu 82214dee867SHeiko Stübner : bank->regmap_pull; 82314dee867SHeiko Stübner *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0; 824751a99abSHeiko Stübner *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 8256ca5274dSHeiko Stübner *bit = pin_num % RK3188_PULL_PINS_PER_REG; 8266ca5274dSHeiko Stübner *bit *= RK3188_PULL_BITS_PER_PIN; 8276ca5274dSHeiko Stübner } else { 828751a99abSHeiko Stübner *regmap = info->regmap_pull ? info->regmap_pull 829751a99abSHeiko Stübner : info->regmap_base; 830751a99abSHeiko Stübner *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET; 831751a99abSHeiko Stübner 832bfc7a42aSHeiko Stübner /* correct the offset, as it is the 2nd pull register */ 833bfc7a42aSHeiko Stübner *reg -= 4; 8346ca5274dSHeiko Stübner *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; 8356ca5274dSHeiko Stübner *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 8366ca5274dSHeiko Stübner 8376ca5274dSHeiko Stübner /* 8386ca5274dSHeiko Stübner * The bits in these registers have an inverse ordering 8396ca5274dSHeiko Stübner * with the lowest pin being in bits 15:14 and the highest 8406ca5274dSHeiko Stübner * pin in bits 1:0 8416ca5274dSHeiko Stübner */ 8426ca5274dSHeiko Stübner *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG); 8436ca5274dSHeiko Stübner *bit *= RK3188_PULL_BITS_PER_PIN; 8446ca5274dSHeiko Stübner } 8456ca5274dSHeiko Stübner } 8466ca5274dSHeiko Stübner 847304f077dSHeiko Stübner #define RK3288_PULL_OFFSET 0x140 848304f077dSHeiko Stübner static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, 849304f077dSHeiko Stübner int pin_num, struct regmap **regmap, 850304f077dSHeiko Stübner int *reg, u8 *bit) 851304f077dSHeiko Stübner { 852304f077dSHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 853304f077dSHeiko Stübner 854304f077dSHeiko Stübner /* The first 24 pins of the first bank are located in PMU */ 855304f077dSHeiko Stübner if (bank->bank_num == 0) { 856304f077dSHeiko Stübner *regmap = info->regmap_pmu; 857304f077dSHeiko Stübner *reg = RK3188_PULL_PMU_OFFSET; 858304f077dSHeiko Stübner 859304f077dSHeiko Stübner *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 860304f077dSHeiko Stübner *bit = pin_num % RK3188_PULL_PINS_PER_REG; 861304f077dSHeiko Stübner *bit *= RK3188_PULL_BITS_PER_PIN; 862304f077dSHeiko Stübner } else { 863304f077dSHeiko Stübner *regmap = info->regmap_base; 864304f077dSHeiko Stübner *reg = RK3288_PULL_OFFSET; 865304f077dSHeiko Stübner 866304f077dSHeiko Stübner /* correct the offset, as we're starting with the 2nd bank */ 867304f077dSHeiko Stübner *reg -= 0x10; 868304f077dSHeiko Stübner *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; 869304f077dSHeiko Stübner *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 870304f077dSHeiko Stübner 871304f077dSHeiko Stübner *bit = (pin_num % RK3188_PULL_PINS_PER_REG); 872304f077dSHeiko Stübner *bit *= RK3188_PULL_BITS_PER_PIN; 873304f077dSHeiko Stübner } 874304f077dSHeiko Stübner } 875304f077dSHeiko Stübner 876b547c800SHeiko Stübner #define RK3288_DRV_PMU_OFFSET 0x70 877b547c800SHeiko Stübner #define RK3288_DRV_GRF_OFFSET 0x1c0 878b547c800SHeiko Stübner #define RK3288_DRV_BITS_PER_PIN 2 879b547c800SHeiko Stübner #define RK3288_DRV_PINS_PER_REG 8 880b547c800SHeiko Stübner #define RK3288_DRV_BANK_STRIDE 16 881b547c800SHeiko Stübner 882b547c800SHeiko Stübner static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, 883b547c800SHeiko Stübner int pin_num, struct regmap **regmap, 884b547c800SHeiko Stübner int *reg, u8 *bit) 885b547c800SHeiko Stübner { 886b547c800SHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 887b547c800SHeiko Stübner 888b547c800SHeiko Stübner /* The first 24 pins of the first bank are located in PMU */ 889b547c800SHeiko Stübner if (bank->bank_num == 0) { 890b547c800SHeiko Stübner *regmap = info->regmap_pmu; 891b547c800SHeiko Stübner *reg = RK3288_DRV_PMU_OFFSET; 892b547c800SHeiko Stübner 893b547c800SHeiko Stübner *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); 894b547c800SHeiko Stübner *bit = pin_num % RK3288_DRV_PINS_PER_REG; 895b547c800SHeiko Stübner *bit *= RK3288_DRV_BITS_PER_PIN; 896b547c800SHeiko Stübner } else { 897b547c800SHeiko Stübner *regmap = info->regmap_base; 898b547c800SHeiko Stübner *reg = RK3288_DRV_GRF_OFFSET; 899b547c800SHeiko Stübner 900b547c800SHeiko Stübner /* correct the offset, as we're starting with the 2nd bank */ 901b547c800SHeiko Stübner *reg -= 0x10; 902b547c800SHeiko Stübner *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE; 903b547c800SHeiko Stübner *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); 904b547c800SHeiko Stübner 905b547c800SHeiko Stübner *bit = (pin_num % RK3288_DRV_PINS_PER_REG); 906b547c800SHeiko Stübner *bit *= RK3288_DRV_BITS_PER_PIN; 907b547c800SHeiko Stübner } 908b547c800SHeiko Stübner } 909b547c800SHeiko Stübner 910fea0fe60SJeffy Chen #define RK3228_PULL_OFFSET 0x100 911fea0fe60SJeffy Chen 912fea0fe60SJeffy Chen static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, 913fea0fe60SJeffy Chen int pin_num, struct regmap **regmap, 914fea0fe60SJeffy Chen int *reg, u8 *bit) 915fea0fe60SJeffy Chen { 916fea0fe60SJeffy Chen struct rockchip_pinctrl *info = bank->drvdata; 917fea0fe60SJeffy Chen 918fea0fe60SJeffy Chen *regmap = info->regmap_base; 919fea0fe60SJeffy Chen *reg = RK3228_PULL_OFFSET; 920fea0fe60SJeffy Chen *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; 921fea0fe60SJeffy Chen *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 922fea0fe60SJeffy Chen 923fea0fe60SJeffy Chen *bit = (pin_num % RK3188_PULL_PINS_PER_REG); 924fea0fe60SJeffy Chen *bit *= RK3188_PULL_BITS_PER_PIN; 925fea0fe60SJeffy Chen } 926fea0fe60SJeffy Chen 927fea0fe60SJeffy Chen #define RK3228_DRV_GRF_OFFSET 0x200 928fea0fe60SJeffy Chen 929fea0fe60SJeffy Chen static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, 930fea0fe60SJeffy Chen int pin_num, struct regmap **regmap, 931fea0fe60SJeffy Chen int *reg, u8 *bit) 932fea0fe60SJeffy Chen { 933fea0fe60SJeffy Chen struct rockchip_pinctrl *info = bank->drvdata; 934fea0fe60SJeffy Chen 935fea0fe60SJeffy Chen *regmap = info->regmap_base; 936fea0fe60SJeffy Chen *reg = RK3228_DRV_GRF_OFFSET; 937fea0fe60SJeffy Chen *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE; 938fea0fe60SJeffy Chen *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); 939fea0fe60SJeffy Chen 940fea0fe60SJeffy Chen *bit = (pin_num % RK3288_DRV_PINS_PER_REG); 941fea0fe60SJeffy Chen *bit *= RK3288_DRV_BITS_PER_PIN; 942fea0fe60SJeffy Chen } 943fea0fe60SJeffy Chen 944daecdc66SHeiko Stübner #define RK3368_PULL_GRF_OFFSET 0x100 945daecdc66SHeiko Stübner #define RK3368_PULL_PMU_OFFSET 0x10 946daecdc66SHeiko Stübner 947daecdc66SHeiko Stübner static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, 948daecdc66SHeiko Stübner int pin_num, struct regmap **regmap, 949daecdc66SHeiko Stübner int *reg, u8 *bit) 950daecdc66SHeiko Stübner { 951daecdc66SHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 952daecdc66SHeiko Stübner 953daecdc66SHeiko Stübner /* The first 32 pins of the first bank are located in PMU */ 954daecdc66SHeiko Stübner if (bank->bank_num == 0) { 955daecdc66SHeiko Stübner *regmap = info->regmap_pmu; 956daecdc66SHeiko Stübner *reg = RK3368_PULL_PMU_OFFSET; 957daecdc66SHeiko Stübner 958daecdc66SHeiko Stübner *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 959daecdc66SHeiko Stübner *bit = pin_num % RK3188_PULL_PINS_PER_REG; 960daecdc66SHeiko Stübner *bit *= RK3188_PULL_BITS_PER_PIN; 961daecdc66SHeiko Stübner } else { 962daecdc66SHeiko Stübner *regmap = info->regmap_base; 963daecdc66SHeiko Stübner *reg = RK3368_PULL_GRF_OFFSET; 964daecdc66SHeiko Stübner 965daecdc66SHeiko Stübner /* correct the offset, as we're starting with the 2nd bank */ 966daecdc66SHeiko Stübner *reg -= 0x10; 967daecdc66SHeiko Stübner *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; 968daecdc66SHeiko Stübner *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 969daecdc66SHeiko Stübner 970daecdc66SHeiko Stübner *bit = (pin_num % RK3188_PULL_PINS_PER_REG); 971daecdc66SHeiko Stübner *bit *= RK3188_PULL_BITS_PER_PIN; 972daecdc66SHeiko Stübner } 973daecdc66SHeiko Stübner } 974daecdc66SHeiko Stübner 975daecdc66SHeiko Stübner #define RK3368_DRV_PMU_OFFSET 0x20 976daecdc66SHeiko Stübner #define RK3368_DRV_GRF_OFFSET 0x200 977daecdc66SHeiko Stübner 978daecdc66SHeiko Stübner static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, 979daecdc66SHeiko Stübner int pin_num, struct regmap **regmap, 980daecdc66SHeiko Stübner int *reg, u8 *bit) 981daecdc66SHeiko Stübner { 982daecdc66SHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 983daecdc66SHeiko Stübner 984daecdc66SHeiko Stübner /* The first 32 pins of the first bank are located in PMU */ 985daecdc66SHeiko Stübner if (bank->bank_num == 0) { 986daecdc66SHeiko Stübner *regmap = info->regmap_pmu; 987daecdc66SHeiko Stübner *reg = RK3368_DRV_PMU_OFFSET; 988daecdc66SHeiko Stübner 989daecdc66SHeiko Stübner *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); 990daecdc66SHeiko Stübner *bit = pin_num % RK3288_DRV_PINS_PER_REG; 991daecdc66SHeiko Stübner *bit *= RK3288_DRV_BITS_PER_PIN; 992daecdc66SHeiko Stübner } else { 993daecdc66SHeiko Stübner *regmap = info->regmap_base; 994daecdc66SHeiko Stübner *reg = RK3368_DRV_GRF_OFFSET; 995daecdc66SHeiko Stübner 996daecdc66SHeiko Stübner /* correct the offset, as we're starting with the 2nd bank */ 997daecdc66SHeiko Stübner *reg -= 0x10; 998daecdc66SHeiko Stübner *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE; 999daecdc66SHeiko Stübner *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4); 1000daecdc66SHeiko Stübner 1001daecdc66SHeiko Stübner *bit = (pin_num % RK3288_DRV_PINS_PER_REG); 1002daecdc66SHeiko Stübner *bit *= RK3288_DRV_BITS_PER_PIN; 1003daecdc66SHeiko Stübner } 1004daecdc66SHeiko Stübner } 1005daecdc66SHeiko Stübner 1006b6c23275SDavid Wu #define RK3399_PULL_GRF_OFFSET 0xe040 1007b6c23275SDavid Wu #define RK3399_PULL_PMU_OFFSET 0x40 1008b6c23275SDavid Wu #define RK3399_DRV_3BITS_PER_PIN 3 1009b6c23275SDavid Wu 1010b6c23275SDavid Wu static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, 1011b6c23275SDavid Wu int pin_num, struct regmap **regmap, 1012b6c23275SDavid Wu int *reg, u8 *bit) 1013b6c23275SDavid Wu { 1014b6c23275SDavid Wu struct rockchip_pinctrl *info = bank->drvdata; 1015b6c23275SDavid Wu 1016b6c23275SDavid Wu /* The bank0:16 and bank1:32 pins are located in PMU */ 1017b6c23275SDavid Wu if ((bank->bank_num == 0) || (bank->bank_num == 1)) { 1018b6c23275SDavid Wu *regmap = info->regmap_pmu; 1019b6c23275SDavid Wu *reg = RK3399_PULL_PMU_OFFSET; 1020b6c23275SDavid Wu 1021b6c23275SDavid Wu *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; 1022b6c23275SDavid Wu 1023b6c23275SDavid Wu *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 1024b6c23275SDavid Wu *bit = pin_num % RK3188_PULL_PINS_PER_REG; 1025b6c23275SDavid Wu *bit *= RK3188_PULL_BITS_PER_PIN; 1026b6c23275SDavid Wu } else { 1027b6c23275SDavid Wu *regmap = info->regmap_base; 1028b6c23275SDavid Wu *reg = RK3399_PULL_GRF_OFFSET; 1029b6c23275SDavid Wu 1030b6c23275SDavid Wu /* correct the offset, as we're starting with the 3rd bank */ 1031b6c23275SDavid Wu *reg -= 0x20; 1032b6c23275SDavid Wu *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE; 1033b6c23275SDavid Wu *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4); 1034b6c23275SDavid Wu 1035b6c23275SDavid Wu *bit = (pin_num % RK3188_PULL_PINS_PER_REG); 1036b6c23275SDavid Wu *bit *= RK3188_PULL_BITS_PER_PIN; 1037b6c23275SDavid Wu } 1038b6c23275SDavid Wu } 1039b6c23275SDavid Wu 1040b6c23275SDavid Wu static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, 1041b6c23275SDavid Wu int pin_num, struct regmap **regmap, 1042b6c23275SDavid Wu int *reg, u8 *bit) 1043b6c23275SDavid Wu { 1044b6c23275SDavid Wu struct rockchip_pinctrl *info = bank->drvdata; 1045b6c23275SDavid Wu int drv_num = (pin_num / 8); 1046b6c23275SDavid Wu 1047b6c23275SDavid Wu /* The bank0:16 and bank1:32 pins are located in PMU */ 1048b6c23275SDavid Wu if ((bank->bank_num == 0) || (bank->bank_num == 1)) 1049b6c23275SDavid Wu *regmap = info->regmap_pmu; 1050b6c23275SDavid Wu else 1051b6c23275SDavid Wu *regmap = info->regmap_base; 1052b6c23275SDavid Wu 1053b6c23275SDavid Wu *reg = bank->drv[drv_num].offset; 1054b6c23275SDavid Wu if ((bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) || 1055b6c23275SDavid Wu (bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY)) 1056b6c23275SDavid Wu *bit = (pin_num % 8) * 3; 1057b6c23275SDavid Wu else 1058b6c23275SDavid Wu *bit = (pin_num % 8) * 2; 1059b6c23275SDavid Wu } 1060b6c23275SDavid Wu 1061b6c23275SDavid Wu static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = { 1062b6c23275SDavid Wu { 2, 4, 8, 12, -1, -1, -1, -1 }, 1063b6c23275SDavid Wu { 3, 6, 9, 12, -1, -1, -1, -1 }, 1064b6c23275SDavid Wu { 5, 10, 15, 20, -1, -1, -1, -1 }, 1065b6c23275SDavid Wu { 4, 6, 8, 10, 12, 14, 16, 18 }, 1066b6c23275SDavid Wu { 4, 7, 10, 13, 16, 19, 22, 26 } 1067b6c23275SDavid Wu }; 1068ef17f69fSHeiko Stübner 1069ef17f69fSHeiko Stübner static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank, 1070ef17f69fSHeiko Stübner int pin_num) 1071b547c800SHeiko Stübner { 1072ef17f69fSHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 1073ef17f69fSHeiko Stübner struct rockchip_pin_ctrl *ctrl = info->ctrl; 1074b547c800SHeiko Stübner struct regmap *regmap; 1075b547c800SHeiko Stübner int reg, ret; 1076b6c23275SDavid Wu u32 data, temp, rmask_bits; 1077b547c800SHeiko Stübner u8 bit; 1078b6c23275SDavid Wu int drv_type = bank->drv[pin_num / 8].drv_type; 1079b547c800SHeiko Stübner 1080ef17f69fSHeiko Stübner ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit); 1081b547c800SHeiko Stübner 1082b6c23275SDavid Wu switch (drv_type) { 1083b6c23275SDavid Wu case DRV_TYPE_IO_1V8_3V0_AUTO: 1084b6c23275SDavid Wu case DRV_TYPE_IO_3V3_ONLY: 1085b6c23275SDavid Wu rmask_bits = RK3399_DRV_3BITS_PER_PIN; 1086b6c23275SDavid Wu switch (bit) { 1087b6c23275SDavid Wu case 0 ... 12: 1088b6c23275SDavid Wu /* regular case, nothing to do */ 1089b6c23275SDavid Wu break; 1090b6c23275SDavid Wu case 15: 1091b6c23275SDavid Wu /* 1092b6c23275SDavid Wu * drive-strength offset is special, as it is 1093b6c23275SDavid Wu * spread over 2 registers 1094b6c23275SDavid Wu */ 1095b6c23275SDavid Wu ret = regmap_read(regmap, reg, &data); 1096b6c23275SDavid Wu if (ret) 1097b6c23275SDavid Wu return ret; 1098b6c23275SDavid Wu 1099b6c23275SDavid Wu ret = regmap_read(regmap, reg + 0x4, &temp); 1100b6c23275SDavid Wu if (ret) 1101b6c23275SDavid Wu return ret; 1102b6c23275SDavid Wu 1103b6c23275SDavid Wu /* 1104b6c23275SDavid Wu * the bit data[15] contains bit 0 of the value 1105b6c23275SDavid Wu * while temp[1:0] contains bits 2 and 1 1106b6c23275SDavid Wu */ 1107b6c23275SDavid Wu data >>= 15; 1108b6c23275SDavid Wu temp &= 0x3; 1109b6c23275SDavid Wu temp <<= 1; 1110b6c23275SDavid Wu data |= temp; 1111b6c23275SDavid Wu 1112b6c23275SDavid Wu return rockchip_perpin_drv_list[drv_type][data]; 1113b6c23275SDavid Wu case 18 ... 21: 1114b6c23275SDavid Wu /* setting fully enclosed in the second register */ 1115b6c23275SDavid Wu reg += 4; 1116b6c23275SDavid Wu bit -= 16; 1117b6c23275SDavid Wu break; 1118b6c23275SDavid Wu default: 1119b6c23275SDavid Wu dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n", 1120b6c23275SDavid Wu bit, drv_type); 1121b6c23275SDavid Wu return -EINVAL; 1122b6c23275SDavid Wu } 1123b6c23275SDavid Wu 1124b6c23275SDavid Wu break; 1125b6c23275SDavid Wu case DRV_TYPE_IO_DEFAULT: 1126b6c23275SDavid Wu case DRV_TYPE_IO_1V8_OR_3V0: 1127b6c23275SDavid Wu case DRV_TYPE_IO_1V8_ONLY: 1128b6c23275SDavid Wu rmask_bits = RK3288_DRV_BITS_PER_PIN; 1129b6c23275SDavid Wu break; 1130b6c23275SDavid Wu default: 1131b6c23275SDavid Wu dev_err(info->dev, "unsupported pinctrl drive type: %d\n", 1132b6c23275SDavid Wu drv_type); 1133b6c23275SDavid Wu return -EINVAL; 1134b6c23275SDavid Wu } 1135b6c23275SDavid Wu 1136b547c800SHeiko Stübner ret = regmap_read(regmap, reg, &data); 1137b547c800SHeiko Stübner if (ret) 1138b547c800SHeiko Stübner return ret; 1139b547c800SHeiko Stübner 1140b547c800SHeiko Stübner data >>= bit; 1141b6c23275SDavid Wu data &= (1 << rmask_bits) - 1; 1142b547c800SHeiko Stübner 1143b6c23275SDavid Wu return rockchip_perpin_drv_list[drv_type][data]; 1144b547c800SHeiko Stübner } 1145b547c800SHeiko Stübner 1146ef17f69fSHeiko Stübner static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank, 1147ef17f69fSHeiko Stübner int pin_num, int strength) 1148b547c800SHeiko Stübner { 1149b547c800SHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 1150ef17f69fSHeiko Stübner struct rockchip_pin_ctrl *ctrl = info->ctrl; 1151b547c800SHeiko Stübner struct regmap *regmap; 1152b547c800SHeiko Stübner int reg, ret, i; 1153b6c23275SDavid Wu u32 data, rmask, rmask_bits, temp; 1154b547c800SHeiko Stübner u8 bit; 1155b6c23275SDavid Wu int drv_type = bank->drv[pin_num / 8].drv_type; 1156b6c23275SDavid Wu 1157b6c23275SDavid Wu dev_dbg(info->dev, "setting drive of GPIO%d-%d to %d\n", 1158b6c23275SDavid Wu bank->bank_num, pin_num, strength); 1159b547c800SHeiko Stübner 1160ef17f69fSHeiko Stübner ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit); 1161b547c800SHeiko Stübner 1162b547c800SHeiko Stübner ret = -EINVAL; 1163b6c23275SDavid Wu for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) { 1164b6c23275SDavid Wu if (rockchip_perpin_drv_list[drv_type][i] == strength) { 1165b547c800SHeiko Stübner ret = i; 1166b547c800SHeiko Stübner break; 1167b6c23275SDavid Wu } else if (rockchip_perpin_drv_list[drv_type][i] < 0) { 1168b6c23275SDavid Wu ret = rockchip_perpin_drv_list[drv_type][i]; 1169b6c23275SDavid Wu break; 1170b547c800SHeiko Stübner } 1171b547c800SHeiko Stübner } 1172b547c800SHeiko Stübner 1173b547c800SHeiko Stübner if (ret < 0) { 1174b547c800SHeiko Stübner dev_err(info->dev, "unsupported driver strength %d\n", 1175b547c800SHeiko Stübner strength); 1176b547c800SHeiko Stübner return ret; 1177b547c800SHeiko Stübner } 1178b547c800SHeiko Stübner 1179b6c23275SDavid Wu switch (drv_type) { 1180b6c23275SDavid Wu case DRV_TYPE_IO_1V8_3V0_AUTO: 1181b6c23275SDavid Wu case DRV_TYPE_IO_3V3_ONLY: 1182b6c23275SDavid Wu rmask_bits = RK3399_DRV_3BITS_PER_PIN; 1183b6c23275SDavid Wu switch (bit) { 1184b6c23275SDavid Wu case 0 ... 12: 1185b6c23275SDavid Wu /* regular case, nothing to do */ 1186b6c23275SDavid Wu break; 1187b6c23275SDavid Wu case 15: 1188b6c23275SDavid Wu /* 1189b6c23275SDavid Wu * drive-strength offset is special, as it is spread 1190b6c23275SDavid Wu * over 2 registers, the bit data[15] contains bit 0 1191b6c23275SDavid Wu * of the value while temp[1:0] contains bits 2 and 1 1192b6c23275SDavid Wu */ 1193b6c23275SDavid Wu data = (ret & 0x1) << 15; 1194b6c23275SDavid Wu temp = (ret >> 0x1) & 0x3; 1195b6c23275SDavid Wu 1196b6c23275SDavid Wu rmask = BIT(15) | BIT(31); 1197b6c23275SDavid Wu data |= BIT(31); 1198b6c23275SDavid Wu ret = regmap_update_bits(regmap, reg, rmask, data); 1199f07bedc3SJohn Keeping if (ret) 1200b6c23275SDavid Wu return ret; 1201b6c23275SDavid Wu 1202b6c23275SDavid Wu rmask = 0x3 | (0x3 << 16); 1203b6c23275SDavid Wu temp |= (0x3 << 16); 1204b6c23275SDavid Wu reg += 0x4; 1205b6c23275SDavid Wu ret = regmap_update_bits(regmap, reg, rmask, temp); 1206b6c23275SDavid Wu 1207b6c23275SDavid Wu return ret; 1208b6c23275SDavid Wu case 18 ... 21: 1209b6c23275SDavid Wu /* setting fully enclosed in the second register */ 1210b6c23275SDavid Wu reg += 4; 1211b6c23275SDavid Wu bit -= 16; 1212b6c23275SDavid Wu break; 1213b6c23275SDavid Wu default: 1214b6c23275SDavid Wu dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n", 1215b6c23275SDavid Wu bit, drv_type); 1216b6c23275SDavid Wu return -EINVAL; 1217b6c23275SDavid Wu } 1218b6c23275SDavid Wu break; 1219b6c23275SDavid Wu case DRV_TYPE_IO_DEFAULT: 1220b6c23275SDavid Wu case DRV_TYPE_IO_1V8_OR_3V0: 1221b6c23275SDavid Wu case DRV_TYPE_IO_1V8_ONLY: 1222b6c23275SDavid Wu rmask_bits = RK3288_DRV_BITS_PER_PIN; 1223b6c23275SDavid Wu break; 1224b6c23275SDavid Wu default: 1225b6c23275SDavid Wu dev_err(info->dev, "unsupported pinctrl drive type: %d\n", 1226b6c23275SDavid Wu drv_type); 1227b6c23275SDavid Wu return -EINVAL; 1228b6c23275SDavid Wu } 1229b6c23275SDavid Wu 1230b547c800SHeiko Stübner /* enable the write to the equivalent lower bits */ 1231b6c23275SDavid Wu data = ((1 << rmask_bits) - 1) << (bit + 16); 123299e872d9SSonny Rao rmask = data | (data >> 16); 1233b547c800SHeiko Stübner data |= (ret << bit); 1234b547c800SHeiko Stübner 123599e872d9SSonny Rao ret = regmap_update_bits(regmap, reg, rmask, data); 1236b547c800SHeiko Stübner 1237b547c800SHeiko Stübner return ret; 1238b547c800SHeiko Stübner } 1239b547c800SHeiko Stübner 12403ba6767aSDavid Wu static int rockchip_pull_list[PULL_TYPE_MAX][4] = { 12413ba6767aSDavid Wu { 12423ba6767aSDavid Wu PIN_CONFIG_BIAS_DISABLE, 12433ba6767aSDavid Wu PIN_CONFIG_BIAS_PULL_UP, 12443ba6767aSDavid Wu PIN_CONFIG_BIAS_PULL_DOWN, 12453ba6767aSDavid Wu PIN_CONFIG_BIAS_BUS_HOLD 12463ba6767aSDavid Wu }, 12473ba6767aSDavid Wu { 12483ba6767aSDavid Wu PIN_CONFIG_BIAS_DISABLE, 12493ba6767aSDavid Wu PIN_CONFIG_BIAS_PULL_DOWN, 12503ba6767aSDavid Wu PIN_CONFIG_BIAS_DISABLE, 12513ba6767aSDavid Wu PIN_CONFIG_BIAS_PULL_UP 12523ba6767aSDavid Wu }, 12533ba6767aSDavid Wu }; 12543ba6767aSDavid Wu 1255d3e51161SHeiko Stübner static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num) 1256d3e51161SHeiko Stübner { 1257d3e51161SHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 1258d3e51161SHeiko Stübner struct rockchip_pin_ctrl *ctrl = info->ctrl; 1259751a99abSHeiko Stübner struct regmap *regmap; 12603ba6767aSDavid Wu int reg, ret, pull_type; 1261d3e51161SHeiko Stübner u8 bit; 12626ca5274dSHeiko Stübner u32 data; 1263d3e51161SHeiko Stübner 1264d3e51161SHeiko Stübner /* rk3066b does support any pulls */ 1265a282926dSHeiko Stübner if (ctrl->type == RK3066B) 1266d3e51161SHeiko Stübner return PIN_CONFIG_BIAS_DISABLE; 1267d3e51161SHeiko Stübner 1268751a99abSHeiko Stübner ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit); 1269751a99abSHeiko Stübner 1270751a99abSHeiko Stübner ret = regmap_read(regmap, reg, &data); 1271751a99abSHeiko Stübner if (ret) 1272751a99abSHeiko Stübner return ret; 12736ca5274dSHeiko Stübner 1274a282926dSHeiko Stübner switch (ctrl->type) { 1275a282926dSHeiko Stübner case RK2928: 1276751a99abSHeiko Stübner return !(data & BIT(bit)) 1277d3e51161SHeiko Stübner ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT 1278d3e51161SHeiko Stübner : PIN_CONFIG_BIAS_DISABLE; 1279b9c6dcabSAndy Yan case RV1108: 1280a282926dSHeiko Stübner case RK3188: 128166d750e1SHeiko Stübner case RK3288: 1282daecdc66SHeiko Stübner case RK3368: 1283b6c23275SDavid Wu case RK3399: 12843ba6767aSDavid Wu pull_type = bank->pull_type[pin_num / 8]; 1285751a99abSHeiko Stübner data >>= bit; 12866ca5274dSHeiko Stübner data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1; 12876ca5274dSHeiko Stübner 12883ba6767aSDavid Wu return rockchip_pull_list[pull_type][data]; 1289a282926dSHeiko Stübner default: 1290a282926dSHeiko Stübner dev_err(info->dev, "unsupported pinctrl type\n"); 1291a282926dSHeiko Stübner return -EINVAL; 1292a282926dSHeiko Stübner }; 1293d3e51161SHeiko Stübner } 1294d3e51161SHeiko Stübner 1295d3e51161SHeiko Stübner static int rockchip_set_pull(struct rockchip_pin_bank *bank, 1296d3e51161SHeiko Stübner int pin_num, int pull) 1297d3e51161SHeiko Stübner { 1298d3e51161SHeiko Stübner struct rockchip_pinctrl *info = bank->drvdata; 1299d3e51161SHeiko Stübner struct rockchip_pin_ctrl *ctrl = info->ctrl; 1300751a99abSHeiko Stübner struct regmap *regmap; 13013ba6767aSDavid Wu int reg, ret, i, pull_type; 1302d3e51161SHeiko Stübner u8 bit; 130399e872d9SSonny Rao u32 data, rmask; 1304d3e51161SHeiko Stübner 1305d3e51161SHeiko Stübner dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n", 1306d3e51161SHeiko Stübner bank->bank_num, pin_num, pull); 1307d3e51161SHeiko Stübner 1308d3e51161SHeiko Stübner /* rk3066b does support any pulls */ 1309a282926dSHeiko Stübner if (ctrl->type == RK3066B) 1310d3e51161SHeiko Stübner return pull ? -EINVAL : 0; 1311d3e51161SHeiko Stübner 1312751a99abSHeiko Stübner ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit); 1313d3e51161SHeiko Stübner 13146ca5274dSHeiko Stübner switch (ctrl->type) { 13156ca5274dSHeiko Stübner case RK2928: 1316d3e51161SHeiko Stübner data = BIT(bit + 16); 1317d3e51161SHeiko Stübner if (pull == PIN_CONFIG_BIAS_DISABLE) 1318d3e51161SHeiko Stübner data |= BIT(bit); 1319751a99abSHeiko Stübner ret = regmap_write(regmap, reg, data); 1320a282926dSHeiko Stübner break; 1321b9c6dcabSAndy Yan case RV1108: 1322a282926dSHeiko Stübner case RK3188: 132366d750e1SHeiko Stübner case RK3288: 1324daecdc66SHeiko Stübner case RK3368: 1325b6c23275SDavid Wu case RK3399: 13263ba6767aSDavid Wu pull_type = bank->pull_type[pin_num / 8]; 13273ba6767aSDavid Wu ret = -EINVAL; 13283ba6767aSDavid Wu for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]); 13293ba6767aSDavid Wu i++) { 13303ba6767aSDavid Wu if (rockchip_pull_list[pull_type][i] == pull) { 13313ba6767aSDavid Wu ret = i; 13323ba6767aSDavid Wu break; 13333ba6767aSDavid Wu } 13343ba6767aSDavid Wu } 13353ba6767aSDavid Wu 13363ba6767aSDavid Wu if (ret < 0) { 13373ba6767aSDavid Wu dev_err(info->dev, "unsupported pull setting %d\n", 13383ba6767aSDavid Wu pull); 13393ba6767aSDavid Wu return ret; 13403ba6767aSDavid Wu } 13413ba6767aSDavid Wu 13426ca5274dSHeiko Stübner /* enable the write to the equivalent lower bits */ 13436ca5274dSHeiko Stübner data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16); 134499e872d9SSonny Rao rmask = data | (data >> 16); 13453ba6767aSDavid Wu data |= (ret << bit); 13466ca5274dSHeiko Stübner 134799e872d9SSonny Rao ret = regmap_update_bits(regmap, reg, rmask, data); 13486ca5274dSHeiko Stübner break; 1349a282926dSHeiko Stübner default: 1350a282926dSHeiko Stübner dev_err(info->dev, "unsupported pinctrl type\n"); 1351a282926dSHeiko Stübner return -EINVAL; 1352d3e51161SHeiko Stübner } 1353d3e51161SHeiko Stübner 1354751a99abSHeiko Stübner return ret; 1355d3e51161SHeiko Stübner } 1356d3e51161SHeiko Stübner 1357728d3f5aSdavid.wu #define RK3328_SCHMITT_BITS_PER_PIN 1 1358728d3f5aSdavid.wu #define RK3328_SCHMITT_PINS_PER_REG 16 1359728d3f5aSdavid.wu #define RK3328_SCHMITT_BANK_STRIDE 8 1360728d3f5aSdavid.wu #define RK3328_SCHMITT_GRF_OFFSET 0x380 1361728d3f5aSdavid.wu 1362728d3f5aSdavid.wu static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank, 1363728d3f5aSdavid.wu int pin_num, 1364728d3f5aSdavid.wu struct regmap **regmap, 1365728d3f5aSdavid.wu int *reg, u8 *bit) 1366728d3f5aSdavid.wu { 1367728d3f5aSdavid.wu struct rockchip_pinctrl *info = bank->drvdata; 1368728d3f5aSdavid.wu 1369728d3f5aSdavid.wu *regmap = info->regmap_base; 1370728d3f5aSdavid.wu *reg = RK3328_SCHMITT_GRF_OFFSET; 1371728d3f5aSdavid.wu 1372728d3f5aSdavid.wu *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE; 1373728d3f5aSdavid.wu *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4); 1374728d3f5aSdavid.wu *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG; 1375728d3f5aSdavid.wu 1376728d3f5aSdavid.wu return 0; 1377728d3f5aSdavid.wu } 1378728d3f5aSdavid.wu 1379e3b357d7Sdavid.wu static int rockchip_get_schmitt(struct rockchip_pin_bank *bank, int pin_num) 1380e3b357d7Sdavid.wu { 1381e3b357d7Sdavid.wu struct rockchip_pinctrl *info = bank->drvdata; 1382e3b357d7Sdavid.wu struct rockchip_pin_ctrl *ctrl = info->ctrl; 1383e3b357d7Sdavid.wu struct regmap *regmap; 1384e3b357d7Sdavid.wu int reg, ret; 1385e3b357d7Sdavid.wu u8 bit; 1386e3b357d7Sdavid.wu u32 data; 1387e3b357d7Sdavid.wu 1388e3b357d7Sdavid.wu ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit); 1389e3b357d7Sdavid.wu if (ret) 1390e3b357d7Sdavid.wu return ret; 1391e3b357d7Sdavid.wu 1392e3b357d7Sdavid.wu ret = regmap_read(regmap, reg, &data); 1393e3b357d7Sdavid.wu if (ret) 1394e3b357d7Sdavid.wu return ret; 1395e3b357d7Sdavid.wu 1396e3b357d7Sdavid.wu data >>= bit; 1397e3b357d7Sdavid.wu return data & 0x1; 1398e3b357d7Sdavid.wu } 1399e3b357d7Sdavid.wu 1400e3b357d7Sdavid.wu static int rockchip_set_schmitt(struct rockchip_pin_bank *bank, 1401e3b357d7Sdavid.wu int pin_num, int enable) 1402e3b357d7Sdavid.wu { 1403e3b357d7Sdavid.wu struct rockchip_pinctrl *info = bank->drvdata; 1404e3b357d7Sdavid.wu struct rockchip_pin_ctrl *ctrl = info->ctrl; 1405e3b357d7Sdavid.wu struct regmap *regmap; 1406e3b357d7Sdavid.wu int reg, ret; 1407e3b357d7Sdavid.wu u8 bit; 1408e3b357d7Sdavid.wu u32 data, rmask; 1409e3b357d7Sdavid.wu 1410e3b357d7Sdavid.wu dev_dbg(info->dev, "setting input schmitt of GPIO%d-%d to %d\n", 1411e3b357d7Sdavid.wu bank->bank_num, pin_num, enable); 1412e3b357d7Sdavid.wu 1413e3b357d7Sdavid.wu ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit); 1414e3b357d7Sdavid.wu if (ret) 1415e3b357d7Sdavid.wu return ret; 1416e3b357d7Sdavid.wu 1417e3b357d7Sdavid.wu /* enable the write to the equivalent lower bits */ 1418e3b357d7Sdavid.wu data = BIT(bit + 16) | (enable << bit); 1419e3b357d7Sdavid.wu rmask = BIT(bit + 16) | BIT(bit); 1420e3b357d7Sdavid.wu 1421f07bedc3SJohn Keeping return regmap_update_bits(regmap, reg, rmask, data); 1422e3b357d7Sdavid.wu } 1423e3b357d7Sdavid.wu 1424d3e51161SHeiko Stübner /* 1425d3e51161SHeiko Stübner * Pinmux_ops handling 1426d3e51161SHeiko Stübner */ 1427d3e51161SHeiko Stübner 1428d3e51161SHeiko Stübner static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev) 1429d3e51161SHeiko Stübner { 1430d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 1431d3e51161SHeiko Stübner 1432d3e51161SHeiko Stübner return info->nfunctions; 1433d3e51161SHeiko Stübner } 1434d3e51161SHeiko Stübner 1435d3e51161SHeiko Stübner static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev, 1436d3e51161SHeiko Stübner unsigned selector) 1437d3e51161SHeiko Stübner { 1438d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 1439d3e51161SHeiko Stübner 1440d3e51161SHeiko Stübner return info->functions[selector].name; 1441d3e51161SHeiko Stübner } 1442d3e51161SHeiko Stübner 1443d3e51161SHeiko Stübner static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev, 1444d3e51161SHeiko Stübner unsigned selector, const char * const **groups, 1445d3e51161SHeiko Stübner unsigned * const num_groups) 1446d3e51161SHeiko Stübner { 1447d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 1448d3e51161SHeiko Stübner 1449d3e51161SHeiko Stübner *groups = info->functions[selector].groups; 1450d3e51161SHeiko Stübner *num_groups = info->functions[selector].ngroups; 1451d3e51161SHeiko Stübner 1452d3e51161SHeiko Stübner return 0; 1453d3e51161SHeiko Stübner } 1454d3e51161SHeiko Stübner 145503e9f0caSLinus Walleij static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector, 1456d3e51161SHeiko Stübner unsigned group) 1457d3e51161SHeiko Stübner { 1458d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 1459d3e51161SHeiko Stübner const unsigned int *pins = info->groups[group].pins; 1460d3e51161SHeiko Stübner const struct rockchip_pin_config *data = info->groups[group].data; 1461d3e51161SHeiko Stübner struct rockchip_pin_bank *bank; 146214797189SHeiko Stübner int cnt, ret = 0; 1463d3e51161SHeiko Stübner 1464d3e51161SHeiko Stübner dev_dbg(info->dev, "enable function %s group %s\n", 1465d3e51161SHeiko Stübner info->functions[selector].name, info->groups[group].name); 1466d3e51161SHeiko Stübner 1467d3e51161SHeiko Stübner /* 1468d3e51161SHeiko Stübner * for each pin in the pin group selected, program the correspoding pin 1469d3e51161SHeiko Stübner * pin function number in the config register. 1470d3e51161SHeiko Stübner */ 1471d3e51161SHeiko Stübner for (cnt = 0; cnt < info->groups[group].npins; cnt++) { 1472d3e51161SHeiko Stübner bank = pin_to_bank(info, pins[cnt]); 147314797189SHeiko Stübner ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 1474d3e51161SHeiko Stübner data[cnt].func); 147514797189SHeiko Stübner if (ret) 147614797189SHeiko Stübner break; 147714797189SHeiko Stübner } 147814797189SHeiko Stübner 147914797189SHeiko Stübner if (ret) { 148014797189SHeiko Stübner /* revert the already done pin settings */ 148114797189SHeiko Stübner for (cnt--; cnt >= 0; cnt--) 148214797189SHeiko Stübner rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0); 148314797189SHeiko Stübner 148414797189SHeiko Stübner return ret; 1485d3e51161SHeiko Stübner } 1486d3e51161SHeiko Stübner 1487d3e51161SHeiko Stübner return 0; 1488d3e51161SHeiko Stübner } 1489d3e51161SHeiko Stübner 14906ba20a00SCaesar Wang static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset) 14916ba20a00SCaesar Wang { 14926ba20a00SCaesar Wang struct rockchip_pin_bank *bank = gpiochip_get_data(chip); 14936ba20a00SCaesar Wang u32 data; 14946ba20a00SCaesar Wang 14956ba20a00SCaesar Wang data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); 14966ba20a00SCaesar Wang 14976ba20a00SCaesar Wang return !(data & BIT(offset)); 14986ba20a00SCaesar Wang } 14996ba20a00SCaesar Wang 1500d3e51161SHeiko Stübner /* 1501d3e51161SHeiko Stübner * The calls to gpio_direction_output() and gpio_direction_input() 1502d3e51161SHeiko Stübner * leads to this function call (via the pinctrl_gpio_direction_{input|output}() 1503d3e51161SHeiko Stübner * function called from the gpiolib interface). 1504d3e51161SHeiko Stübner */ 1505e5c2c9dbSDoug Anderson static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip, 1506e5c2c9dbSDoug Anderson int pin, bool input) 1507d3e51161SHeiko Stübner { 1508d3e51161SHeiko Stübner struct rockchip_pin_bank *bank; 1509e5c2c9dbSDoug Anderson int ret; 1510fab262f5SDoug Anderson unsigned long flags; 1511d3e51161SHeiko Stübner u32 data; 1512d3e51161SHeiko Stübner 151303bf81f1SLinus Walleij bank = gpiochip_get_data(chip); 1514d3e51161SHeiko Stübner 151514797189SHeiko Stübner ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO); 151614797189SHeiko Stübner if (ret < 0) 151714797189SHeiko Stübner return ret; 1518d3e51161SHeiko Stübner 151907a06ae9SLin Huang clk_enable(bank->clk); 152070b7aa7aSJohn Keeping raw_spin_lock_irqsave(&bank->slock, flags); 1521fab262f5SDoug Anderson 1522d3e51161SHeiko Stübner data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); 1523d3e51161SHeiko Stübner /* set bit to 1 for output, 0 for input */ 1524d3e51161SHeiko Stübner if (!input) 1525d3e51161SHeiko Stübner data |= BIT(pin); 1526d3e51161SHeiko Stübner else 1527d3e51161SHeiko Stübner data &= ~BIT(pin); 1528d3e51161SHeiko Stübner writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR); 1529d3e51161SHeiko Stübner 153070b7aa7aSJohn Keeping raw_spin_unlock_irqrestore(&bank->slock, flags); 153107a06ae9SLin Huang clk_disable(bank->clk); 1532fab262f5SDoug Anderson 1533d3e51161SHeiko Stübner return 0; 1534d3e51161SHeiko Stübner } 1535d3e51161SHeiko Stübner 1536e5c2c9dbSDoug Anderson static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev, 1537e5c2c9dbSDoug Anderson struct pinctrl_gpio_range *range, 1538e5c2c9dbSDoug Anderson unsigned offset, bool input) 1539e5c2c9dbSDoug Anderson { 1540e5c2c9dbSDoug Anderson struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 1541e5c2c9dbSDoug Anderson struct gpio_chip *chip; 1542e5c2c9dbSDoug Anderson int pin; 1543e5c2c9dbSDoug Anderson 1544e5c2c9dbSDoug Anderson chip = range->gc; 1545e5c2c9dbSDoug Anderson pin = offset - chip->base; 1546e5c2c9dbSDoug Anderson dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n", 1547e5c2c9dbSDoug Anderson offset, range->name, pin, input ? "input" : "output"); 1548e5c2c9dbSDoug Anderson 1549e5c2c9dbSDoug Anderson return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base, 1550e5c2c9dbSDoug Anderson input); 1551e5c2c9dbSDoug Anderson } 1552e5c2c9dbSDoug Anderson 1553d3e51161SHeiko Stübner static const struct pinmux_ops rockchip_pmx_ops = { 1554d3e51161SHeiko Stübner .get_functions_count = rockchip_pmx_get_funcs_count, 1555d3e51161SHeiko Stübner .get_function_name = rockchip_pmx_get_func_name, 1556d3e51161SHeiko Stübner .get_function_groups = rockchip_pmx_get_groups, 155703e9f0caSLinus Walleij .set_mux = rockchip_pmx_set, 1558d3e51161SHeiko Stübner .gpio_set_direction = rockchip_pmx_gpio_set_direction, 1559d3e51161SHeiko Stübner }; 1560d3e51161SHeiko Stübner 1561d3e51161SHeiko Stübner /* 1562d3e51161SHeiko Stübner * Pinconf_ops handling 1563d3e51161SHeiko Stübner */ 1564d3e51161SHeiko Stübner 156544b6d930SHeiko Stübner static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl, 156644b6d930SHeiko Stübner enum pin_config_param pull) 156744b6d930SHeiko Stübner { 1568a282926dSHeiko Stübner switch (ctrl->type) { 1569a282926dSHeiko Stübner case RK2928: 1570a282926dSHeiko Stübner return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT || 1571a282926dSHeiko Stübner pull == PIN_CONFIG_BIAS_DISABLE); 1572a282926dSHeiko Stübner case RK3066B: 157344b6d930SHeiko Stübner return pull ? false : true; 1574b9c6dcabSAndy Yan case RV1108: 1575a282926dSHeiko Stübner case RK3188: 157666d750e1SHeiko Stübner case RK3288: 1577daecdc66SHeiko Stübner case RK3368: 1578b6c23275SDavid Wu case RK3399: 1579a282926dSHeiko Stübner return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT); 158044b6d930SHeiko Stübner } 158144b6d930SHeiko Stübner 1582a282926dSHeiko Stübner return false; 158344b6d930SHeiko Stübner } 158444b6d930SHeiko Stübner 1585e5c2c9dbSDoug Anderson static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value); 1586a076e2edSHeiko Stübner static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset); 1587a076e2edSHeiko Stübner 1588d3e51161SHeiko Stübner /* set the pin config settings for a specified pin */ 1589d3e51161SHeiko Stübner static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, 159003b054e9SSherman Yin unsigned long *configs, unsigned num_configs) 1591d3e51161SHeiko Stübner { 1592d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 1593d3e51161SHeiko Stübner struct rockchip_pin_bank *bank = pin_to_bank(info, pin); 159403b054e9SSherman Yin enum pin_config_param param; 159558957d2eSMika Westerberg u32 arg; 159603b054e9SSherman Yin int i; 159703b054e9SSherman Yin int rc; 159803b054e9SSherman Yin 159903b054e9SSherman Yin for (i = 0; i < num_configs; i++) { 160003b054e9SSherman Yin param = pinconf_to_config_param(configs[i]); 160103b054e9SSherman Yin arg = pinconf_to_config_argument(configs[i]); 1602d3e51161SHeiko Stübner 1603d3e51161SHeiko Stübner switch (param) { 1604d3e51161SHeiko Stübner case PIN_CONFIG_BIAS_DISABLE: 160503b054e9SSherman Yin rc = rockchip_set_pull(bank, pin - bank->pin_base, 160603b054e9SSherman Yin param); 160703b054e9SSherman Yin if (rc) 160803b054e9SSherman Yin return rc; 160944b6d930SHeiko Stübner break; 1610d3e51161SHeiko Stübner case PIN_CONFIG_BIAS_PULL_UP: 1611d3e51161SHeiko Stübner case PIN_CONFIG_BIAS_PULL_DOWN: 1612d3e51161SHeiko Stübner case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: 16136ca5274dSHeiko Stübner case PIN_CONFIG_BIAS_BUS_HOLD: 161444b6d930SHeiko Stübner if (!rockchip_pinconf_pull_valid(info->ctrl, param)) 161544b6d930SHeiko Stübner return -ENOTSUPP; 161644b6d930SHeiko Stübner 161744b6d930SHeiko Stübner if (!arg) 161844b6d930SHeiko Stübner return -EINVAL; 161944b6d930SHeiko Stübner 162003b054e9SSherman Yin rc = rockchip_set_pull(bank, pin - bank->pin_base, 162103b054e9SSherman Yin param); 162203b054e9SSherman Yin if (rc) 162303b054e9SSherman Yin return rc; 1624d3e51161SHeiko Stübner break; 1625a076e2edSHeiko Stübner case PIN_CONFIG_OUTPUT: 1626e5c2c9dbSDoug Anderson rockchip_gpio_set(&bank->gpio_chip, 1627e5c2c9dbSDoug Anderson pin - bank->pin_base, arg); 1628e5c2c9dbSDoug Anderson rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip, 1629e5c2c9dbSDoug Anderson pin - bank->pin_base, false); 1630a076e2edSHeiko Stübner if (rc) 1631a076e2edSHeiko Stübner return rc; 1632a076e2edSHeiko Stübner break; 1633b547c800SHeiko Stübner case PIN_CONFIG_DRIVE_STRENGTH: 1634b547c800SHeiko Stübner /* rk3288 is the first with per-pin drive-strength */ 1635ef17f69fSHeiko Stübner if (!info->ctrl->drv_calc_reg) 1636b547c800SHeiko Stübner return -ENOTSUPP; 1637b547c800SHeiko Stübner 1638ef17f69fSHeiko Stübner rc = rockchip_set_drive_perpin(bank, 1639ef17f69fSHeiko Stübner pin - bank->pin_base, arg); 1640b547c800SHeiko Stübner if (rc < 0) 1641b547c800SHeiko Stübner return rc; 1642b547c800SHeiko Stübner break; 1643e3b357d7Sdavid.wu case PIN_CONFIG_INPUT_SCHMITT_ENABLE: 1644e3b357d7Sdavid.wu if (!info->ctrl->schmitt_calc_reg) 1645e3b357d7Sdavid.wu return -ENOTSUPP; 1646e3b357d7Sdavid.wu 1647e3b357d7Sdavid.wu rc = rockchip_set_schmitt(bank, 1648e3b357d7Sdavid.wu pin - bank->pin_base, arg); 1649e3b357d7Sdavid.wu if (rc < 0) 1650e3b357d7Sdavid.wu return rc; 1651e3b357d7Sdavid.wu break; 1652d3e51161SHeiko Stübner default: 1653d3e51161SHeiko Stübner return -ENOTSUPP; 1654d3e51161SHeiko Stübner break; 1655d3e51161SHeiko Stübner } 165603b054e9SSherman Yin } /* for each config */ 1657d3e51161SHeiko Stübner 1658d3e51161SHeiko Stübner return 0; 1659d3e51161SHeiko Stübner } 1660d3e51161SHeiko Stübner 1661d3e51161SHeiko Stübner /* get the pin config settings for a specified pin */ 1662d3e51161SHeiko Stübner static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, 1663d3e51161SHeiko Stübner unsigned long *config) 1664d3e51161SHeiko Stübner { 1665d3e51161SHeiko Stübner struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 1666d3e51161SHeiko Stübner struct rockchip_pin_bank *bank = pin_to_bank(info, pin); 1667d3e51161SHeiko Stübner enum pin_config_param param = pinconf_to_config_param(*config); 1668dab3eba7SHeiko Stübner u16 arg; 1669a076e2edSHeiko Stübner int rc; 1670d3e51161SHeiko Stübner 1671d3e51161SHeiko Stübner switch (param) { 1672d3e51161SHeiko Stübner case PIN_CONFIG_BIAS_DISABLE: 167344b6d930SHeiko Stübner if (rockchip_get_pull(bank, pin - bank->pin_base) != param) 1674d3e51161SHeiko Stübner return -EINVAL; 1675d3e51161SHeiko Stübner 1676dab3eba7SHeiko Stübner arg = 0; 1677d3e51161SHeiko Stübner break; 167844b6d930SHeiko Stübner case PIN_CONFIG_BIAS_PULL_UP: 167944b6d930SHeiko Stübner case PIN_CONFIG_BIAS_PULL_DOWN: 168044b6d930SHeiko Stübner case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: 16816ca5274dSHeiko Stübner case PIN_CONFIG_BIAS_BUS_HOLD: 168244b6d930SHeiko Stübner if (!rockchip_pinconf_pull_valid(info->ctrl, param)) 168344b6d930SHeiko Stübner return -ENOTSUPP; 168444b6d930SHeiko Stübner 168544b6d930SHeiko Stübner if (rockchip_get_pull(bank, pin - bank->pin_base) != param) 168644b6d930SHeiko Stübner return -EINVAL; 168744b6d930SHeiko Stübner 1688dab3eba7SHeiko Stübner arg = 1; 168944b6d930SHeiko Stübner break; 1690a076e2edSHeiko Stübner case PIN_CONFIG_OUTPUT: 1691a076e2edSHeiko Stübner rc = rockchip_get_mux(bank, pin - bank->pin_base); 1692a076e2edSHeiko Stübner if (rc != RK_FUNC_GPIO) 1693a076e2edSHeiko Stübner return -EINVAL; 1694a076e2edSHeiko Stübner 1695a076e2edSHeiko Stübner rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base); 1696a076e2edSHeiko Stübner if (rc < 0) 1697a076e2edSHeiko Stübner return rc; 1698a076e2edSHeiko Stübner 1699a076e2edSHeiko Stübner arg = rc ? 1 : 0; 1700a076e2edSHeiko Stübner break; 1701b547c800SHeiko Stübner case PIN_CONFIG_DRIVE_STRENGTH: 1702b547c800SHeiko Stübner /* rk3288 is the first with per-pin drive-strength */ 1703ef17f69fSHeiko Stübner if (!info->ctrl->drv_calc_reg) 1704b547c800SHeiko Stübner return -ENOTSUPP; 1705b547c800SHeiko Stübner 1706ef17f69fSHeiko Stübner rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base); 1707b547c800SHeiko Stübner if (rc < 0) 1708b547c800SHeiko Stübner return rc; 1709b547c800SHeiko Stübner 1710b547c800SHeiko Stübner arg = rc; 1711b547c800SHeiko Stübner break; 1712e3b357d7Sdavid.wu case PIN_CONFIG_INPUT_SCHMITT_ENABLE: 1713e3b357d7Sdavid.wu if (!info->ctrl->schmitt_calc_reg) 1714e3b357d7Sdavid.wu return -ENOTSUPP; 1715e3b357d7Sdavid.wu 1716e3b357d7Sdavid.wu rc = rockchip_get_schmitt(bank, pin - bank->pin_base); 1717e3b357d7Sdavid.wu if (rc < 0) 1718e3b357d7Sdavid.wu return rc; 1719e3b357d7Sdavid.wu 1720e3b357d7Sdavid.wu arg = rc; 1721e3b357d7Sdavid.wu break; 1722d3e51161SHeiko Stübner default: 1723d3e51161SHeiko Stübner return -ENOTSUPP; 1724d3e51161SHeiko Stübner break; 1725d3e51161SHeiko Stübner } 1726d3e51161SHeiko Stübner 1727dab3eba7SHeiko Stübner *config = pinconf_to_config_packed(param, arg); 1728dab3eba7SHeiko Stübner 1729d3e51161SHeiko Stübner return 0; 1730d3e51161SHeiko Stübner } 1731d3e51161SHeiko Stübner 1732d3e51161SHeiko Stübner static const struct pinconf_ops rockchip_pinconf_ops = { 1733d3e51161SHeiko Stübner .pin_config_get = rockchip_pinconf_get, 1734d3e51161SHeiko Stübner .pin_config_set = rockchip_pinconf_set, 1735ed62f2f2SHeiko Stübner .is_generic = true, 1736d3e51161SHeiko Stübner }; 1737d3e51161SHeiko Stübner 173865fca613SHeiko Stübner static const struct of_device_id rockchip_bank_match[] = { 173965fca613SHeiko Stübner { .compatible = "rockchip,gpio-bank" }, 17406ca5274dSHeiko Stübner { .compatible = "rockchip,rk3188-gpio-bank0" }, 174165fca613SHeiko Stübner {}, 174265fca613SHeiko Stübner }; 1743d3e51161SHeiko Stübner 1744d3e51161SHeiko Stübner static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info, 1745d3e51161SHeiko Stübner struct device_node *np) 1746d3e51161SHeiko Stübner { 1747d3e51161SHeiko Stübner struct device_node *child; 1748d3e51161SHeiko Stübner 1749d3e51161SHeiko Stübner for_each_child_of_node(np, child) { 175065fca613SHeiko Stübner if (of_match_node(rockchip_bank_match, child)) 1751d3e51161SHeiko Stübner continue; 1752d3e51161SHeiko Stübner 1753d3e51161SHeiko Stübner info->nfunctions++; 1754d3e51161SHeiko Stübner info->ngroups += of_get_child_count(child); 1755d3e51161SHeiko Stübner } 1756d3e51161SHeiko Stübner } 1757d3e51161SHeiko Stübner 1758d3e51161SHeiko Stübner static int rockchip_pinctrl_parse_groups(struct device_node *np, 1759d3e51161SHeiko Stübner struct rockchip_pin_group *grp, 1760d3e51161SHeiko Stübner struct rockchip_pinctrl *info, 1761d3e51161SHeiko Stübner u32 index) 1762d3e51161SHeiko Stübner { 1763d3e51161SHeiko Stübner struct rockchip_pin_bank *bank; 1764d3e51161SHeiko Stübner int size; 1765d3e51161SHeiko Stübner const __be32 *list; 1766d3e51161SHeiko Stübner int num; 1767d3e51161SHeiko Stübner int i, j; 1768d3e51161SHeiko Stübner int ret; 1769d3e51161SHeiko Stübner 1770d3e51161SHeiko Stübner dev_dbg(info->dev, "group(%d): %s\n", index, np->name); 1771d3e51161SHeiko Stübner 1772d3e51161SHeiko Stübner /* Initialise group */ 1773d3e51161SHeiko Stübner grp->name = np->name; 1774d3e51161SHeiko Stübner 1775d3e51161SHeiko Stübner /* 1776d3e51161SHeiko Stübner * the binding format is rockchip,pins = <bank pin mux CONFIG>, 1777d3e51161SHeiko Stübner * do sanity check and calculate pins number 1778d3e51161SHeiko Stübner */ 1779d3e51161SHeiko Stübner list = of_get_property(np, "rockchip,pins", &size); 1780d3e51161SHeiko Stübner /* we do not check return since it's safe node passed down */ 1781d3e51161SHeiko Stübner size /= sizeof(*list); 1782d3e51161SHeiko Stübner if (!size || size % 4) { 1783d3e51161SHeiko Stübner dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n"); 1784d3e51161SHeiko Stübner return -EINVAL; 1785d3e51161SHeiko Stübner } 1786d3e51161SHeiko Stübner 1787d3e51161SHeiko Stübner grp->npins = size / 4; 1788d3e51161SHeiko Stübner 1789d3e51161SHeiko Stübner grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int), 1790d3e51161SHeiko Stübner GFP_KERNEL); 1791d3e51161SHeiko Stübner grp->data = devm_kzalloc(info->dev, grp->npins * 1792d3e51161SHeiko Stübner sizeof(struct rockchip_pin_config), 1793d3e51161SHeiko Stübner GFP_KERNEL); 1794d3e51161SHeiko Stübner if (!grp->pins || !grp->data) 1795d3e51161SHeiko Stübner return -ENOMEM; 1796d3e51161SHeiko Stübner 1797d3e51161SHeiko Stübner for (i = 0, j = 0; i < size; i += 4, j++) { 1798d3e51161SHeiko Stübner const __be32 *phandle; 1799d3e51161SHeiko Stübner struct device_node *np_config; 1800d3e51161SHeiko Stübner 1801d3e51161SHeiko Stübner num = be32_to_cpu(*list++); 1802d3e51161SHeiko Stübner bank = bank_num_to_bank(info, num); 1803d3e51161SHeiko Stübner if (IS_ERR(bank)) 1804d3e51161SHeiko Stübner return PTR_ERR(bank); 1805d3e51161SHeiko Stübner 1806d3e51161SHeiko Stübner grp->pins[j] = bank->pin_base + be32_to_cpu(*list++); 1807d3e51161SHeiko Stübner grp->data[j].func = be32_to_cpu(*list++); 1808d3e51161SHeiko Stübner 1809d3e51161SHeiko Stübner phandle = list++; 1810d3e51161SHeiko Stübner if (!phandle) 1811d3e51161SHeiko Stübner return -EINVAL; 1812d3e51161SHeiko Stübner 1813d3e51161SHeiko Stübner np_config = of_find_node_by_phandle(be32_to_cpup(phandle)); 1814dd4d01f7SSoren Brinkmann ret = pinconf_generic_parse_dt_config(np_config, NULL, 1815d3e51161SHeiko Stübner &grp->data[j].configs, &grp->data[j].nconfigs); 1816d3e51161SHeiko Stübner if (ret) 1817d3e51161SHeiko Stübner return ret; 1818d3e51161SHeiko Stübner } 1819d3e51161SHeiko Stübner 1820d3e51161SHeiko Stübner return 0; 1821d3e51161SHeiko Stübner } 1822d3e51161SHeiko Stübner 1823d3e51161SHeiko Stübner static int rockchip_pinctrl_parse_functions(struct device_node *np, 1824d3e51161SHeiko Stübner struct rockchip_pinctrl *info, 1825d3e51161SHeiko Stübner u32 index) 1826d3e51161SHeiko Stübner { 1827d3e51161SHeiko Stübner struct device_node *child; 1828d3e51161SHeiko Stübner struct rockchip_pmx_func *func; 1829d3e51161SHeiko Stübner struct rockchip_pin_group *grp; 1830d3e51161SHeiko Stübner int ret; 1831d3e51161SHeiko Stübner static u32 grp_index; 1832d3e51161SHeiko Stübner u32 i = 0; 1833d3e51161SHeiko Stübner 1834d3e51161SHeiko Stübner dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name); 1835d3e51161SHeiko Stübner 1836d3e51161SHeiko Stübner func = &info->functions[index]; 1837d3e51161SHeiko Stübner 1838d3e51161SHeiko Stübner /* Initialise function */ 1839d3e51161SHeiko Stübner func->name = np->name; 1840d3e51161SHeiko Stübner func->ngroups = of_get_child_count(np); 1841d3e51161SHeiko Stübner if (func->ngroups <= 0) 1842d3e51161SHeiko Stübner return 0; 1843d3e51161SHeiko Stübner 1844d3e51161SHeiko Stübner func->groups = devm_kzalloc(info->dev, 1845d3e51161SHeiko Stübner func->ngroups * sizeof(char *), GFP_KERNEL); 1846d3e51161SHeiko Stübner if (!func->groups) 1847d3e51161SHeiko Stübner return -ENOMEM; 1848d3e51161SHeiko Stübner 1849d3e51161SHeiko Stübner for_each_child_of_node(np, child) { 1850d3e51161SHeiko Stübner func->groups[i] = child->name; 1851d3e51161SHeiko Stübner grp = &info->groups[grp_index++]; 1852d3e51161SHeiko Stübner ret = rockchip_pinctrl_parse_groups(child, grp, info, i++); 1853f7a81b7fSJulia Lawall if (ret) { 1854f7a81b7fSJulia Lawall of_node_put(child); 1855d3e51161SHeiko Stübner return ret; 1856d3e51161SHeiko Stübner } 1857f7a81b7fSJulia Lawall } 1858d3e51161SHeiko Stübner 1859d3e51161SHeiko Stübner return 0; 1860d3e51161SHeiko Stübner } 1861d3e51161SHeiko Stübner 1862d3e51161SHeiko Stübner static int rockchip_pinctrl_parse_dt(struct platform_device *pdev, 1863d3e51161SHeiko Stübner struct rockchip_pinctrl *info) 1864d3e51161SHeiko Stübner { 1865d3e51161SHeiko Stübner struct device *dev = &pdev->dev; 1866d3e51161SHeiko Stübner struct device_node *np = dev->of_node; 1867d3e51161SHeiko Stübner struct device_node *child; 1868d3e51161SHeiko Stübner int ret; 1869d3e51161SHeiko Stübner int i; 1870d3e51161SHeiko Stübner 1871d3e51161SHeiko Stübner rockchip_pinctrl_child_count(info, np); 1872d3e51161SHeiko Stübner 1873d3e51161SHeiko Stübner dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions); 1874d3e51161SHeiko Stübner dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups); 1875d3e51161SHeiko Stübner 1876d3e51161SHeiko Stübner info->functions = devm_kzalloc(dev, info->nfunctions * 1877d3e51161SHeiko Stübner sizeof(struct rockchip_pmx_func), 1878d3e51161SHeiko Stübner GFP_KERNEL); 1879d3e51161SHeiko Stübner if (!info->functions) { 1880d3e51161SHeiko Stübner dev_err(dev, "failed to allocate memory for function list\n"); 1881d3e51161SHeiko Stübner return -EINVAL; 1882d3e51161SHeiko Stübner } 1883d3e51161SHeiko Stübner 1884d3e51161SHeiko Stübner info->groups = devm_kzalloc(dev, info->ngroups * 1885d3e51161SHeiko Stübner sizeof(struct rockchip_pin_group), 1886d3e51161SHeiko Stübner GFP_KERNEL); 1887d3e51161SHeiko Stübner if (!info->groups) { 1888d3e51161SHeiko Stübner dev_err(dev, "failed allocate memory for ping group list\n"); 1889d3e51161SHeiko Stübner return -EINVAL; 1890d3e51161SHeiko Stübner } 1891d3e51161SHeiko Stübner 1892d3e51161SHeiko Stübner i = 0; 1893d3e51161SHeiko Stübner 1894d3e51161SHeiko Stübner for_each_child_of_node(np, child) { 189565fca613SHeiko Stübner if (of_match_node(rockchip_bank_match, child)) 1896d3e51161SHeiko Stübner continue; 189765fca613SHeiko Stübner 1898d3e51161SHeiko Stübner ret = rockchip_pinctrl_parse_functions(child, info, i++); 1899d3e51161SHeiko Stübner if (ret) { 1900d3e51161SHeiko Stübner dev_err(&pdev->dev, "failed to parse function\n"); 1901f7a81b7fSJulia Lawall of_node_put(child); 1902d3e51161SHeiko Stübner return ret; 1903d3e51161SHeiko Stübner } 1904d3e51161SHeiko Stübner } 1905d3e51161SHeiko Stübner 1906d3e51161SHeiko Stübner return 0; 1907d3e51161SHeiko Stübner } 1908d3e51161SHeiko Stübner 1909d3e51161SHeiko Stübner static int rockchip_pinctrl_register(struct platform_device *pdev, 1910d3e51161SHeiko Stübner struct rockchip_pinctrl *info) 1911d3e51161SHeiko Stübner { 1912d3e51161SHeiko Stübner struct pinctrl_desc *ctrldesc = &info->pctl; 1913d3e51161SHeiko Stübner struct pinctrl_pin_desc *pindesc, *pdesc; 1914d3e51161SHeiko Stübner struct rockchip_pin_bank *pin_bank; 1915d3e51161SHeiko Stübner int pin, bank, ret; 1916d3e51161SHeiko Stübner int k; 1917d3e51161SHeiko Stübner 1918d3e51161SHeiko Stübner ctrldesc->name = "rockchip-pinctrl"; 1919d3e51161SHeiko Stübner ctrldesc->owner = THIS_MODULE; 1920d3e51161SHeiko Stübner ctrldesc->pctlops = &rockchip_pctrl_ops; 1921d3e51161SHeiko Stübner ctrldesc->pmxops = &rockchip_pmx_ops; 1922d3e51161SHeiko Stübner ctrldesc->confops = &rockchip_pinconf_ops; 1923d3e51161SHeiko Stübner 1924d3e51161SHeiko Stübner pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) * 1925d3e51161SHeiko Stübner info->ctrl->nr_pins, GFP_KERNEL); 1926d3e51161SHeiko Stübner if (!pindesc) { 1927d3e51161SHeiko Stübner dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n"); 1928d3e51161SHeiko Stübner return -ENOMEM; 1929d3e51161SHeiko Stübner } 1930d3e51161SHeiko Stübner ctrldesc->pins = pindesc; 1931d3e51161SHeiko Stübner ctrldesc->npins = info->ctrl->nr_pins; 1932d3e51161SHeiko Stübner 1933d3e51161SHeiko Stübner pdesc = pindesc; 1934d3e51161SHeiko Stübner for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) { 1935d3e51161SHeiko Stübner pin_bank = &info->ctrl->pin_banks[bank]; 1936d3e51161SHeiko Stübner for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) { 1937d3e51161SHeiko Stübner pdesc->number = k; 1938d3e51161SHeiko Stübner pdesc->name = kasprintf(GFP_KERNEL, "%s-%d", 1939d3e51161SHeiko Stübner pin_bank->name, pin); 1940d3e51161SHeiko Stübner pdesc++; 1941d3e51161SHeiko Stübner } 1942d3e51161SHeiko Stübner } 1943d3e51161SHeiko Stübner 19440fb7dcb1SDoug Anderson ret = rockchip_pinctrl_parse_dt(pdev, info); 19450fb7dcb1SDoug Anderson if (ret) 19460fb7dcb1SDoug Anderson return ret; 19470fb7dcb1SDoug Anderson 19480085a2b4SLaxman Dewangan info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info); 1949323de9efSMasahiro Yamada if (IS_ERR(info->pctl_dev)) { 1950d3e51161SHeiko Stübner dev_err(&pdev->dev, "could not register pinctrl driver\n"); 1951323de9efSMasahiro Yamada return PTR_ERR(info->pctl_dev); 1952d3e51161SHeiko Stübner } 1953d3e51161SHeiko Stübner 1954d3e51161SHeiko Stübner for (bank = 0; bank < info->ctrl->nr_banks; ++bank) { 1955d3e51161SHeiko Stübner pin_bank = &info->ctrl->pin_banks[bank]; 1956d3e51161SHeiko Stübner pin_bank->grange.name = pin_bank->name; 1957d3e51161SHeiko Stübner pin_bank->grange.id = bank; 1958d3e51161SHeiko Stübner pin_bank->grange.pin_base = pin_bank->pin_base; 1959d3e51161SHeiko Stübner pin_bank->grange.base = pin_bank->gpio_chip.base; 1960d3e51161SHeiko Stübner pin_bank->grange.npins = pin_bank->gpio_chip.ngpio; 1961d3e51161SHeiko Stübner pin_bank->grange.gc = &pin_bank->gpio_chip; 1962d3e51161SHeiko Stübner pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange); 1963d3e51161SHeiko Stübner } 1964d3e51161SHeiko Stübner 1965d3e51161SHeiko Stübner return 0; 1966d3e51161SHeiko Stübner } 1967d3e51161SHeiko Stübner 1968d3e51161SHeiko Stübner /* 1969d3e51161SHeiko Stübner * GPIO handling 1970d3e51161SHeiko Stübner */ 1971d3e51161SHeiko Stübner 1972d3e51161SHeiko Stübner static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value) 1973d3e51161SHeiko Stübner { 197403bf81f1SLinus Walleij struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 1975d3e51161SHeiko Stübner void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR; 1976d3e51161SHeiko Stübner unsigned long flags; 1977d3e51161SHeiko Stübner u32 data; 1978d3e51161SHeiko Stübner 197907a06ae9SLin Huang clk_enable(bank->clk); 198070b7aa7aSJohn Keeping raw_spin_lock_irqsave(&bank->slock, flags); 1981d3e51161SHeiko Stübner 1982d3e51161SHeiko Stübner data = readl(reg); 1983d3e51161SHeiko Stübner data &= ~BIT(offset); 1984d3e51161SHeiko Stübner if (value) 1985d3e51161SHeiko Stübner data |= BIT(offset); 1986d3e51161SHeiko Stübner writel(data, reg); 1987d3e51161SHeiko Stübner 198870b7aa7aSJohn Keeping raw_spin_unlock_irqrestore(&bank->slock, flags); 198907a06ae9SLin Huang clk_disable(bank->clk); 1990d3e51161SHeiko Stübner } 1991d3e51161SHeiko Stübner 1992d3e51161SHeiko Stübner /* 1993d3e51161SHeiko Stübner * Returns the level of the pin for input direction and setting of the DR 1994d3e51161SHeiko Stübner * register for output gpios. 1995d3e51161SHeiko Stübner */ 1996d3e51161SHeiko Stübner static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset) 1997d3e51161SHeiko Stübner { 199803bf81f1SLinus Walleij struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 1999d3e51161SHeiko Stübner u32 data; 2000d3e51161SHeiko Stübner 200107a06ae9SLin Huang clk_enable(bank->clk); 2002d3e51161SHeiko Stübner data = readl(bank->reg_base + GPIO_EXT_PORT); 200307a06ae9SLin Huang clk_disable(bank->clk); 2004d3e51161SHeiko Stübner data >>= offset; 2005d3e51161SHeiko Stübner data &= 1; 2006d3e51161SHeiko Stübner return data; 2007d3e51161SHeiko Stübner } 2008d3e51161SHeiko Stübner 2009d3e51161SHeiko Stübner /* 2010d3e51161SHeiko Stübner * gpiolib gpio_direction_input callback function. The setting of the pin 2011d3e51161SHeiko Stübner * mux function as 'gpio input' will be handled by the pinctrl susbsystem 2012d3e51161SHeiko Stübner * interface. 2013d3e51161SHeiko Stübner */ 2014d3e51161SHeiko Stübner static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset) 2015d3e51161SHeiko Stübner { 2016d3e51161SHeiko Stübner return pinctrl_gpio_direction_input(gc->base + offset); 2017d3e51161SHeiko Stübner } 2018d3e51161SHeiko Stübner 2019d3e51161SHeiko Stübner /* 2020d3e51161SHeiko Stübner * gpiolib gpio_direction_output callback function. The setting of the pin 2021d3e51161SHeiko Stübner * mux function as 'gpio output' will be handled by the pinctrl susbsystem 2022d3e51161SHeiko Stübner * interface. 2023d3e51161SHeiko Stübner */ 2024d3e51161SHeiko Stübner static int rockchip_gpio_direction_output(struct gpio_chip *gc, 2025d3e51161SHeiko Stübner unsigned offset, int value) 2026d3e51161SHeiko Stübner { 2027d3e51161SHeiko Stübner rockchip_gpio_set(gc, offset, value); 2028d3e51161SHeiko Stübner return pinctrl_gpio_direction_output(gc->base + offset); 2029d3e51161SHeiko Stübner } 2030d3e51161SHeiko Stübner 2031d3e51161SHeiko Stübner /* 2032d3e51161SHeiko Stübner * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin 2033d3e51161SHeiko Stübner * and a virtual IRQ, if not already present. 2034d3e51161SHeiko Stübner */ 2035d3e51161SHeiko Stübner static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset) 2036d3e51161SHeiko Stübner { 203703bf81f1SLinus Walleij struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 2038d3e51161SHeiko Stübner unsigned int virq; 2039d3e51161SHeiko Stübner 2040d3e51161SHeiko Stübner if (!bank->domain) 2041d3e51161SHeiko Stübner return -ENXIO; 2042d3e51161SHeiko Stübner 2043d3e51161SHeiko Stübner virq = irq_create_mapping(bank->domain, offset); 2044d3e51161SHeiko Stübner 2045d3e51161SHeiko Stübner return (virq) ? : -ENXIO; 2046d3e51161SHeiko Stübner } 2047d3e51161SHeiko Stübner 2048d3e51161SHeiko Stübner static const struct gpio_chip rockchip_gpiolib_chip = { 204998c85d58SJonas Gorski .request = gpiochip_generic_request, 205098c85d58SJonas Gorski .free = gpiochip_generic_free, 2051d3e51161SHeiko Stübner .set = rockchip_gpio_set, 2052d3e51161SHeiko Stübner .get = rockchip_gpio_get, 20536ba20a00SCaesar Wang .get_direction = rockchip_gpio_get_direction, 2054d3e51161SHeiko Stübner .direction_input = rockchip_gpio_direction_input, 2055d3e51161SHeiko Stübner .direction_output = rockchip_gpio_direction_output, 2056d3e51161SHeiko Stübner .to_irq = rockchip_gpio_to_irq, 2057d3e51161SHeiko Stübner .owner = THIS_MODULE, 2058d3e51161SHeiko Stübner }; 2059d3e51161SHeiko Stübner 2060d3e51161SHeiko Stübner /* 2061d3e51161SHeiko Stübner * Interrupt handling 2062d3e51161SHeiko Stübner */ 2063d3e51161SHeiko Stübner 2064bd0b9ac4SThomas Gleixner static void rockchip_irq_demux(struct irq_desc *desc) 2065d3e51161SHeiko Stübner { 20665663bb27SJiang Liu struct irq_chip *chip = irq_desc_get_chip(desc); 20675663bb27SJiang Liu struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc); 2068d3e51161SHeiko Stübner u32 pend; 2069d3e51161SHeiko Stübner 2070d3e51161SHeiko Stübner dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name); 2071d3e51161SHeiko Stübner 2072d3e51161SHeiko Stübner chained_irq_enter(chip, desc); 2073d3e51161SHeiko Stübner 2074d3e51161SHeiko Stübner pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS); 2075d3e51161SHeiko Stübner 2076d3e51161SHeiko Stübner while (pend) { 2077415f748cSThomas Gleixner unsigned int irq, virq; 2078d3e51161SHeiko Stübner 2079d3e51161SHeiko Stübner irq = __ffs(pend); 2080d3e51161SHeiko Stübner pend &= ~BIT(irq); 2081d3e51161SHeiko Stübner virq = irq_linear_revmap(bank->domain, irq); 2082d3e51161SHeiko Stübner 2083d3e51161SHeiko Stübner if (!virq) { 2084d3e51161SHeiko Stübner dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq); 2085d3e51161SHeiko Stübner continue; 2086d3e51161SHeiko Stübner } 2087d3e51161SHeiko Stübner 2088d3e51161SHeiko Stübner dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq); 2089d3e51161SHeiko Stübner 20905a927501SHeiko Stübner /* 20915a927501SHeiko Stübner * Triggering IRQ on both rising and falling edge 20925a927501SHeiko Stübner * needs manual intervention. 20935a927501SHeiko Stübner */ 20945a927501SHeiko Stübner if (bank->toggle_edge_mode & BIT(irq)) { 209553b1bfc7SDoug Anderson u32 data, data_old, polarity; 209653b1bfc7SDoug Anderson unsigned long flags; 209753b1bfc7SDoug Anderson 209853b1bfc7SDoug Anderson data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT); 209953b1bfc7SDoug Anderson do { 210070b7aa7aSJohn Keeping raw_spin_lock_irqsave(&bank->slock, flags); 210153b1bfc7SDoug Anderson 210253b1bfc7SDoug Anderson polarity = readl_relaxed(bank->reg_base + 210353b1bfc7SDoug Anderson GPIO_INT_POLARITY); 21045a927501SHeiko Stübner if (data & BIT(irq)) 21055a927501SHeiko Stübner polarity &= ~BIT(irq); 21065a927501SHeiko Stübner else 21075a927501SHeiko Stübner polarity |= BIT(irq); 210853b1bfc7SDoug Anderson writel(polarity, 210953b1bfc7SDoug Anderson bank->reg_base + GPIO_INT_POLARITY); 21105a927501SHeiko Stübner 211170b7aa7aSJohn Keeping raw_spin_unlock_irqrestore(&bank->slock, flags); 211253b1bfc7SDoug Anderson 211353b1bfc7SDoug Anderson data_old = data; 211453b1bfc7SDoug Anderson data = readl_relaxed(bank->reg_base + 211553b1bfc7SDoug Anderson GPIO_EXT_PORT); 211653b1bfc7SDoug Anderson } while ((data & BIT(irq)) != (data_old & BIT(irq))); 21175a927501SHeiko Stübner } 21185a927501SHeiko Stübner 2119d3e51161SHeiko Stübner generic_handle_irq(virq); 2120d3e51161SHeiko Stübner } 2121d3e51161SHeiko Stübner 2122d3e51161SHeiko Stübner chained_irq_exit(chip, desc); 2123d3e51161SHeiko Stübner } 2124d3e51161SHeiko Stübner 2125d3e51161SHeiko Stübner static int rockchip_irq_set_type(struct irq_data *d, unsigned int type) 2126d3e51161SHeiko Stübner { 2127d3e51161SHeiko Stübner struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 2128d3e51161SHeiko Stübner struct rockchip_pin_bank *bank = gc->private; 2129d3e51161SHeiko Stübner u32 mask = BIT(d->hwirq); 2130d3e51161SHeiko Stübner u32 polarity; 2131d3e51161SHeiko Stübner u32 level; 2132d3e51161SHeiko Stübner u32 data; 2133fab262f5SDoug Anderson unsigned long flags; 213414797189SHeiko Stübner int ret; 2135d3e51161SHeiko Stübner 21365a927501SHeiko Stübner /* make sure the pin is configured as gpio input */ 213788bb9421SJohn Keeping ret = rockchip_verify_mux(bank, d->hwirq, RK_FUNC_GPIO); 213814797189SHeiko Stübner if (ret < 0) 213914797189SHeiko Stübner return ret; 214014797189SHeiko Stübner 214188bb9421SJohn Keeping bank->new_irqs |= mask; 214288bb9421SJohn Keeping 214370b7aa7aSJohn Keeping raw_spin_lock_irqsave(&bank->slock, flags); 2144fab262f5SDoug Anderson 21455a927501SHeiko Stübner data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); 21465a927501SHeiko Stübner data &= ~mask; 21475a927501SHeiko Stübner writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR); 21485a927501SHeiko Stübner 214970b7aa7aSJohn Keeping raw_spin_unlock_irqrestore(&bank->slock, flags); 2150fab262f5SDoug Anderson 2151d3e51161SHeiko Stübner if (type & IRQ_TYPE_EDGE_BOTH) 21522dbf1bc5SThomas Gleixner irq_set_handler_locked(d, handle_edge_irq); 2153d3e51161SHeiko Stübner else 21542dbf1bc5SThomas Gleixner irq_set_handler_locked(d, handle_level_irq); 2155d3e51161SHeiko Stübner 215670b7aa7aSJohn Keeping raw_spin_lock_irqsave(&bank->slock, flags); 2157d3e51161SHeiko Stübner irq_gc_lock(gc); 2158d3e51161SHeiko Stübner 2159d3e51161SHeiko Stübner level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL); 2160d3e51161SHeiko Stübner polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY); 2161d3e51161SHeiko Stübner 2162d3e51161SHeiko Stübner switch (type) { 21635a927501SHeiko Stübner case IRQ_TYPE_EDGE_BOTH: 21645a927501SHeiko Stübner bank->toggle_edge_mode |= mask; 21655a927501SHeiko Stübner level |= mask; 21665a927501SHeiko Stübner 21675a927501SHeiko Stübner /* 21685a927501SHeiko Stübner * Determine gpio state. If 1 next interrupt should be falling 21695a927501SHeiko Stübner * otherwise rising. 21705a927501SHeiko Stübner */ 21715a927501SHeiko Stübner data = readl(bank->reg_base + GPIO_EXT_PORT); 21725a927501SHeiko Stübner if (data & mask) 21735a927501SHeiko Stübner polarity &= ~mask; 21745a927501SHeiko Stübner else 21755a927501SHeiko Stübner polarity |= mask; 21765a927501SHeiko Stübner break; 2177d3e51161SHeiko Stübner case IRQ_TYPE_EDGE_RISING: 21785a927501SHeiko Stübner bank->toggle_edge_mode &= ~mask; 2179d3e51161SHeiko Stübner level |= mask; 2180d3e51161SHeiko Stübner polarity |= mask; 2181d3e51161SHeiko Stübner break; 2182d3e51161SHeiko Stübner case IRQ_TYPE_EDGE_FALLING: 21835a927501SHeiko Stübner bank->toggle_edge_mode &= ~mask; 2184d3e51161SHeiko Stübner level |= mask; 2185d3e51161SHeiko Stübner polarity &= ~mask; 2186d3e51161SHeiko Stübner break; 2187d3e51161SHeiko Stübner case IRQ_TYPE_LEVEL_HIGH: 21885a927501SHeiko Stübner bank->toggle_edge_mode &= ~mask; 2189d3e51161SHeiko Stübner level &= ~mask; 2190d3e51161SHeiko Stübner polarity |= mask; 2191d3e51161SHeiko Stübner break; 2192d3e51161SHeiko Stübner case IRQ_TYPE_LEVEL_LOW: 21935a927501SHeiko Stübner bank->toggle_edge_mode &= ~mask; 2194d3e51161SHeiko Stübner level &= ~mask; 2195d3e51161SHeiko Stübner polarity &= ~mask; 2196d3e51161SHeiko Stübner break; 2197d3e51161SHeiko Stübner default: 21987cc5f970SAxel Lin irq_gc_unlock(gc); 219970b7aa7aSJohn Keeping raw_spin_unlock_irqrestore(&bank->slock, flags); 2200d3e51161SHeiko Stübner return -EINVAL; 2201d3e51161SHeiko Stübner } 2202d3e51161SHeiko Stübner 2203d3e51161SHeiko Stübner writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL); 2204d3e51161SHeiko Stübner writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY); 2205d3e51161SHeiko Stübner 2206d3e51161SHeiko Stübner irq_gc_unlock(gc); 220770b7aa7aSJohn Keeping raw_spin_unlock_irqrestore(&bank->slock, flags); 2208d3e51161SHeiko Stübner 2209d3e51161SHeiko Stübner return 0; 2210d3e51161SHeiko Stübner } 2211d3e51161SHeiko Stübner 221268bda47cSDoug Anderson static void rockchip_irq_suspend(struct irq_data *d) 221368bda47cSDoug Anderson { 221468bda47cSDoug Anderson struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 221568bda47cSDoug Anderson struct rockchip_pin_bank *bank = gc->private; 221668bda47cSDoug Anderson 221707a06ae9SLin Huang clk_enable(bank->clk); 22185ae0c7adSDoug Anderson bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK); 22195ae0c7adSDoug Anderson irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK); 222007a06ae9SLin Huang clk_disable(bank->clk); 222168bda47cSDoug Anderson } 222268bda47cSDoug Anderson 222368bda47cSDoug Anderson static void rockchip_irq_resume(struct irq_data *d) 222468bda47cSDoug Anderson { 222568bda47cSDoug Anderson struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 222668bda47cSDoug Anderson struct rockchip_pin_bank *bank = gc->private; 222768bda47cSDoug Anderson 222807a06ae9SLin Huang clk_enable(bank->clk); 22295ae0c7adSDoug Anderson irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK); 223007a06ae9SLin Huang clk_disable(bank->clk); 223107a06ae9SLin Huang } 223207a06ae9SLin Huang 2233d468289aSJeffy Chen static void rockchip_irq_enable(struct irq_data *d) 223407a06ae9SLin Huang { 223507a06ae9SLin Huang struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 223607a06ae9SLin Huang struct rockchip_pin_bank *bank = gc->private; 223707a06ae9SLin Huang 223807a06ae9SLin Huang clk_enable(bank->clk); 223907a06ae9SLin Huang irq_gc_mask_clr_bit(d); 224007a06ae9SLin Huang } 224107a06ae9SLin Huang 2242d468289aSJeffy Chen static void rockchip_irq_disable(struct irq_data *d) 224307a06ae9SLin Huang { 224407a06ae9SLin Huang struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 224507a06ae9SLin Huang struct rockchip_pin_bank *bank = gc->private; 224607a06ae9SLin Huang 224707a06ae9SLin Huang irq_gc_mask_set_bit(d); 224807a06ae9SLin Huang clk_disable(bank->clk); 2249f2dd028cSDoug Anderson } 2250f2dd028cSDoug Anderson 225188bb9421SJohn Keeping static void rockchip_irq_bus_lock(struct irq_data *d) 225288bb9421SJohn Keeping { 225388bb9421SJohn Keeping struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 225488bb9421SJohn Keeping struct rockchip_pin_bank *bank = gc->private; 225588bb9421SJohn Keeping 225688bb9421SJohn Keeping clk_enable(bank->clk); 225788bb9421SJohn Keeping mutex_lock(&bank->irq_lock); 225888bb9421SJohn Keeping } 225988bb9421SJohn Keeping 226088bb9421SJohn Keeping static void rockchip_irq_bus_sync_unlock(struct irq_data *d) 226188bb9421SJohn Keeping { 226288bb9421SJohn Keeping struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 226388bb9421SJohn Keeping struct rockchip_pin_bank *bank = gc->private; 226488bb9421SJohn Keeping 226588bb9421SJohn Keeping while (bank->new_irqs) { 226688bb9421SJohn Keeping unsigned int irq = __ffs(bank->new_irqs); 226788bb9421SJohn Keeping int ret; 226888bb9421SJohn Keeping 226988bb9421SJohn Keeping ret = rockchip_set_mux(bank, irq, RK_FUNC_GPIO); 227088bb9421SJohn Keeping WARN_ON(ret < 0); 227188bb9421SJohn Keeping 227288bb9421SJohn Keeping bank->new_irqs &= ~BIT(irq); 227388bb9421SJohn Keeping } 227488bb9421SJohn Keeping 227588bb9421SJohn Keeping mutex_unlock(&bank->irq_lock); 227688bb9421SJohn Keeping clk_disable(bank->clk); 227788bb9421SJohn Keeping } 227888bb9421SJohn Keeping 2279d3e51161SHeiko Stübner static int rockchip_interrupts_register(struct platform_device *pdev, 2280d3e51161SHeiko Stübner struct rockchip_pinctrl *info) 2281d3e51161SHeiko Stübner { 2282d3e51161SHeiko Stübner struct rockchip_pin_ctrl *ctrl = info->ctrl; 2283d3e51161SHeiko Stübner struct rockchip_pin_bank *bank = ctrl->pin_banks; 2284d3e51161SHeiko Stübner unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN; 2285d3e51161SHeiko Stübner struct irq_chip_generic *gc; 2286d3e51161SHeiko Stübner int ret; 228707a06ae9SLin Huang int i, j; 2288d3e51161SHeiko Stübner 2289d3e51161SHeiko Stübner for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 2290d3e51161SHeiko Stübner if (!bank->valid) { 2291d3e51161SHeiko Stübner dev_warn(&pdev->dev, "bank %s is not valid\n", 2292d3e51161SHeiko Stübner bank->name); 2293d3e51161SHeiko Stübner continue; 2294d3e51161SHeiko Stübner } 2295d3e51161SHeiko Stübner 229607a06ae9SLin Huang ret = clk_enable(bank->clk); 229707a06ae9SLin Huang if (ret) { 229807a06ae9SLin Huang dev_err(&pdev->dev, "failed to enable clock for bank %s\n", 229907a06ae9SLin Huang bank->name); 230007a06ae9SLin Huang continue; 230107a06ae9SLin Huang } 230207a06ae9SLin Huang 2303d3e51161SHeiko Stübner bank->domain = irq_domain_add_linear(bank->of_node, 32, 2304d3e51161SHeiko Stübner &irq_generic_chip_ops, NULL); 2305d3e51161SHeiko Stübner if (!bank->domain) { 2306d3e51161SHeiko Stübner dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n", 2307d3e51161SHeiko Stübner bank->name); 230807a06ae9SLin Huang clk_disable(bank->clk); 2309d3e51161SHeiko Stübner continue; 2310d3e51161SHeiko Stübner } 2311d3e51161SHeiko Stübner 2312d3e51161SHeiko Stübner ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1, 2313d3e51161SHeiko Stübner "rockchip_gpio_irq", handle_level_irq, 2314d3e51161SHeiko Stübner clr, 0, IRQ_GC_INIT_MASK_CACHE); 2315d3e51161SHeiko Stübner if (ret) { 2316d3e51161SHeiko Stübner dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n", 2317d3e51161SHeiko Stübner bank->name); 2318d3e51161SHeiko Stübner irq_domain_remove(bank->domain); 231907a06ae9SLin Huang clk_disable(bank->clk); 2320d3e51161SHeiko Stübner continue; 2321d3e51161SHeiko Stübner } 2322d3e51161SHeiko Stübner 23235ae0c7adSDoug Anderson /* 23245ae0c7adSDoug Anderson * Linux assumes that all interrupts start out disabled/masked. 23255ae0c7adSDoug Anderson * Our driver only uses the concept of masked and always keeps 23265ae0c7adSDoug Anderson * things enabled, so for us that's all masked and all enabled. 23275ae0c7adSDoug Anderson */ 23285ae0c7adSDoug Anderson writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK); 23295ae0c7adSDoug Anderson writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN); 23305ae0c7adSDoug Anderson 2331d3e51161SHeiko Stübner gc = irq_get_domain_generic_chip(bank->domain, 0); 2332d3e51161SHeiko Stübner gc->reg_base = bank->reg_base; 2333d3e51161SHeiko Stübner gc->private = bank; 2334f2dd028cSDoug Anderson gc->chip_types[0].regs.mask = GPIO_INTMASK; 2335d3e51161SHeiko Stübner gc->chip_types[0].regs.ack = GPIO_PORTS_EOI; 2336d3e51161SHeiko Stübner gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit; 2337d468289aSJeffy Chen gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit; 2338d468289aSJeffy Chen gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit; 2339d468289aSJeffy Chen gc->chip_types[0].chip.irq_enable = rockchip_irq_enable; 2340d468289aSJeffy Chen gc->chip_types[0].chip.irq_disable = rockchip_irq_disable; 2341d3e51161SHeiko Stübner gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake; 234268bda47cSDoug Anderson gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend; 234368bda47cSDoug Anderson gc->chip_types[0].chip.irq_resume = rockchip_irq_resume; 2344d3e51161SHeiko Stübner gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type; 234588bb9421SJohn Keeping gc->chip_types[0].chip.irq_bus_lock = rockchip_irq_bus_lock; 234688bb9421SJohn Keeping gc->chip_types[0].chip.irq_bus_sync_unlock = 234788bb9421SJohn Keeping rockchip_irq_bus_sync_unlock; 2348876d716bSDoug Anderson gc->wake_enabled = IRQ_MSK(bank->nr_pins); 2349d3e51161SHeiko Stübner 235003051bc2SThomas Gleixner irq_set_chained_handler_and_data(bank->irq, 235103051bc2SThomas Gleixner rockchip_irq_demux, bank); 235207a06ae9SLin Huang 235307a06ae9SLin Huang /* map the gpio irqs here, when the clock is still running */ 235407a06ae9SLin Huang for (j = 0 ; j < 32 ; j++) 235507a06ae9SLin Huang irq_create_mapping(bank->domain, j); 235607a06ae9SLin Huang 235707a06ae9SLin Huang clk_disable(bank->clk); 2358d3e51161SHeiko Stübner } 2359d3e51161SHeiko Stübner 2360d3e51161SHeiko Stübner return 0; 2361d3e51161SHeiko Stübner } 2362d3e51161SHeiko Stübner 2363d3e51161SHeiko Stübner static int rockchip_gpiolib_register(struct platform_device *pdev, 2364d3e51161SHeiko Stübner struct rockchip_pinctrl *info) 2365d3e51161SHeiko Stübner { 2366d3e51161SHeiko Stübner struct rockchip_pin_ctrl *ctrl = info->ctrl; 2367d3e51161SHeiko Stübner struct rockchip_pin_bank *bank = ctrl->pin_banks; 2368d3e51161SHeiko Stübner struct gpio_chip *gc; 2369d3e51161SHeiko Stübner int ret; 2370d3e51161SHeiko Stübner int i; 2371d3e51161SHeiko Stübner 2372d3e51161SHeiko Stübner for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 2373d3e51161SHeiko Stübner if (!bank->valid) { 2374d3e51161SHeiko Stübner dev_warn(&pdev->dev, "bank %s is not valid\n", 2375d3e51161SHeiko Stübner bank->name); 2376d3e51161SHeiko Stübner continue; 2377d3e51161SHeiko Stübner } 2378d3e51161SHeiko Stübner 2379d3e51161SHeiko Stübner bank->gpio_chip = rockchip_gpiolib_chip; 2380d3e51161SHeiko Stübner 2381d3e51161SHeiko Stübner gc = &bank->gpio_chip; 2382d3e51161SHeiko Stübner gc->base = bank->pin_base; 2383d3e51161SHeiko Stübner gc->ngpio = bank->nr_pins; 238458383c78SLinus Walleij gc->parent = &pdev->dev; 2385d3e51161SHeiko Stübner gc->of_node = bank->of_node; 2386d3e51161SHeiko Stübner gc->label = bank->name; 2387d3e51161SHeiko Stübner 238803bf81f1SLinus Walleij ret = gpiochip_add_data(gc, bank); 2389d3e51161SHeiko Stübner if (ret) { 2390d3e51161SHeiko Stübner dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n", 2391d3e51161SHeiko Stübner gc->label, ret); 2392d3e51161SHeiko Stübner goto fail; 2393d3e51161SHeiko Stübner } 2394d3e51161SHeiko Stübner } 2395d3e51161SHeiko Stübner 2396d3e51161SHeiko Stübner rockchip_interrupts_register(pdev, info); 2397d3e51161SHeiko Stübner 2398d3e51161SHeiko Stübner return 0; 2399d3e51161SHeiko Stübner 2400d3e51161SHeiko Stübner fail: 2401d3e51161SHeiko Stübner for (--i, --bank; i >= 0; --i, --bank) { 2402d3e51161SHeiko Stübner if (!bank->valid) 2403d3e51161SHeiko Stübner continue; 2404b4e7c55dSabdoulaye berthe gpiochip_remove(&bank->gpio_chip); 2405d3e51161SHeiko Stübner } 2406d3e51161SHeiko Stübner return ret; 2407d3e51161SHeiko Stübner } 2408d3e51161SHeiko Stübner 2409d3e51161SHeiko Stübner static int rockchip_gpiolib_unregister(struct platform_device *pdev, 2410d3e51161SHeiko Stübner struct rockchip_pinctrl *info) 2411d3e51161SHeiko Stübner { 2412d3e51161SHeiko Stübner struct rockchip_pin_ctrl *ctrl = info->ctrl; 2413d3e51161SHeiko Stübner struct rockchip_pin_bank *bank = ctrl->pin_banks; 2414d3e51161SHeiko Stübner int i; 2415d3e51161SHeiko Stübner 2416b4e7c55dSabdoulaye berthe for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 2417d3e51161SHeiko Stübner if (!bank->valid) 2418d3e51161SHeiko Stübner continue; 2419b4e7c55dSabdoulaye berthe gpiochip_remove(&bank->gpio_chip); 2420d3e51161SHeiko Stübner } 2421d3e51161SHeiko Stübner 2422b4e7c55dSabdoulaye berthe return 0; 2423d3e51161SHeiko Stübner } 2424d3e51161SHeiko Stübner 2425d3e51161SHeiko Stübner static int rockchip_get_bank_data(struct rockchip_pin_bank *bank, 2426622f3237SHeiko Stübner struct rockchip_pinctrl *info) 2427d3e51161SHeiko Stübner { 2428d3e51161SHeiko Stübner struct resource res; 2429751a99abSHeiko Stübner void __iomem *base; 2430d3e51161SHeiko Stübner 2431d3e51161SHeiko Stübner if (of_address_to_resource(bank->of_node, 0, &res)) { 2432622f3237SHeiko Stübner dev_err(info->dev, "cannot find IO resource for bank\n"); 2433d3e51161SHeiko Stübner return -ENOENT; 2434d3e51161SHeiko Stübner } 2435d3e51161SHeiko Stübner 2436622f3237SHeiko Stübner bank->reg_base = devm_ioremap_resource(info->dev, &res); 2437d3e51161SHeiko Stübner if (IS_ERR(bank->reg_base)) 2438d3e51161SHeiko Stübner return PTR_ERR(bank->reg_base); 2439d3e51161SHeiko Stübner 24406ca5274dSHeiko Stübner /* 24416ca5274dSHeiko Stübner * special case, where parts of the pull setting-registers are 24426ca5274dSHeiko Stübner * part of the PMU register space 24436ca5274dSHeiko Stübner */ 24446ca5274dSHeiko Stübner if (of_device_is_compatible(bank->of_node, 24456ca5274dSHeiko Stübner "rockchip,rk3188-gpio-bank0")) { 2446a658efaaSHeiko Stübner struct device_node *node; 2447bfc7a42aSHeiko Stübner 2448a658efaaSHeiko Stübner node = of_parse_phandle(bank->of_node->parent, 2449a658efaaSHeiko Stübner "rockchip,pmu", 0); 2450a658efaaSHeiko Stübner if (!node) { 24516ca5274dSHeiko Stübner if (of_address_to_resource(bank->of_node, 1, &res)) { 2452622f3237SHeiko Stübner dev_err(info->dev, "cannot find IO resource for bank\n"); 24536ca5274dSHeiko Stübner return -ENOENT; 24546ca5274dSHeiko Stübner } 24556ca5274dSHeiko Stübner 2456622f3237SHeiko Stübner base = devm_ioremap_resource(info->dev, &res); 2457751a99abSHeiko Stübner if (IS_ERR(base)) 2458751a99abSHeiko Stübner return PTR_ERR(base); 2459a658efaaSHeiko Stübner rockchip_regmap_config.max_register = 2460a658efaaSHeiko Stübner resource_size(&res) - 4; 2461a658efaaSHeiko Stübner rockchip_regmap_config.name = 2462a658efaaSHeiko Stübner "rockchip,rk3188-gpio-bank0-pull"; 2463a658efaaSHeiko Stübner bank->regmap_pull = devm_regmap_init_mmio(info->dev, 2464a658efaaSHeiko Stübner base, 2465751a99abSHeiko Stübner &rockchip_regmap_config); 2466a658efaaSHeiko Stübner } 24676ca5274dSHeiko Stübner } 246865fca613SHeiko Stübner 2469d3e51161SHeiko Stübner bank->irq = irq_of_parse_and_map(bank->of_node, 0); 2470d3e51161SHeiko Stübner 2471d3e51161SHeiko Stübner bank->clk = of_clk_get(bank->of_node, 0); 2472d3e51161SHeiko Stübner if (IS_ERR(bank->clk)) 2473d3e51161SHeiko Stübner return PTR_ERR(bank->clk); 2474d3e51161SHeiko Stübner 247507a06ae9SLin Huang return clk_prepare(bank->clk); 2476d3e51161SHeiko Stübner } 2477d3e51161SHeiko Stübner 2478d3e51161SHeiko Stübner static const struct of_device_id rockchip_pinctrl_dt_match[]; 2479d3e51161SHeiko Stübner 2480d3e51161SHeiko Stübner /* retrieve the soc specific data */ 2481d3e51161SHeiko Stübner static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data( 2482d3e51161SHeiko Stübner struct rockchip_pinctrl *d, 2483d3e51161SHeiko Stübner struct platform_device *pdev) 2484d3e51161SHeiko Stübner { 2485d3e51161SHeiko Stübner const struct of_device_id *match; 2486d3e51161SHeiko Stübner struct device_node *node = pdev->dev.of_node; 2487d3e51161SHeiko Stübner struct device_node *np; 2488d3e51161SHeiko Stübner struct rockchip_pin_ctrl *ctrl; 2489d3e51161SHeiko Stübner struct rockchip_pin_bank *bank; 2490b6c23275SDavid Wu int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j; 2491d3e51161SHeiko Stübner 2492d3e51161SHeiko Stübner match = of_match_node(rockchip_pinctrl_dt_match, node); 2493d3e51161SHeiko Stübner ctrl = (struct rockchip_pin_ctrl *)match->data; 2494d3e51161SHeiko Stübner 2495d3e51161SHeiko Stübner for_each_child_of_node(node, np) { 2496d3e51161SHeiko Stübner if (!of_find_property(np, "gpio-controller", NULL)) 2497d3e51161SHeiko Stübner continue; 2498d3e51161SHeiko Stübner 2499d3e51161SHeiko Stübner bank = ctrl->pin_banks; 2500d3e51161SHeiko Stübner for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 2501d3e51161SHeiko Stübner if (!strcmp(bank->name, np->name)) { 2502d3e51161SHeiko Stübner bank->of_node = np; 2503d3e51161SHeiko Stübner 2504622f3237SHeiko Stübner if (!rockchip_get_bank_data(bank, d)) 2505d3e51161SHeiko Stübner bank->valid = true; 2506d3e51161SHeiko Stübner 2507d3e51161SHeiko Stübner break; 2508d3e51161SHeiko Stübner } 2509d3e51161SHeiko Stübner } 2510d3e51161SHeiko Stübner } 2511d3e51161SHeiko Stübner 251295ec8ae4SHeiko Stübner grf_offs = ctrl->grf_mux_offset; 251395ec8ae4SHeiko Stübner pmu_offs = ctrl->pmu_mux_offset; 2514b6c23275SDavid Wu drv_pmu_offs = ctrl->pmu_drv_offset; 2515b6c23275SDavid Wu drv_grf_offs = ctrl->grf_drv_offset; 2516d3e51161SHeiko Stübner bank = ctrl->pin_banks; 2517d3e51161SHeiko Stübner for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 25186bc0d121SHeiko Stübner int bank_pins = 0; 25196bc0d121SHeiko Stübner 252070b7aa7aSJohn Keeping raw_spin_lock_init(&bank->slock); 252188bb9421SJohn Keeping mutex_init(&bank->irq_lock); 2522d3e51161SHeiko Stübner bank->drvdata = d; 2523d3e51161SHeiko Stübner bank->pin_base = ctrl->nr_pins; 2524d3e51161SHeiko Stübner ctrl->nr_pins += bank->nr_pins; 25256bc0d121SHeiko Stübner 2526b6c23275SDavid Wu /* calculate iomux and drv offsets */ 25276bc0d121SHeiko Stübner for (j = 0; j < 4; j++) { 25286bc0d121SHeiko Stübner struct rockchip_iomux *iom = &bank->iomux[j]; 2529b6c23275SDavid Wu struct rockchip_drv *drv = &bank->drv[j]; 253003716e1dSHeiko Stübner int inc; 25316bc0d121SHeiko Stübner 25326bc0d121SHeiko Stübner if (bank_pins >= bank->nr_pins) 25336bc0d121SHeiko Stübner break; 25346bc0d121SHeiko Stübner 2535b6c23275SDavid Wu /* preset iomux offset value, set new start value */ 25366bc0d121SHeiko Stübner if (iom->offset >= 0) { 253795ec8ae4SHeiko Stübner if (iom->type & IOMUX_SOURCE_PMU) 253895ec8ae4SHeiko Stübner pmu_offs = iom->offset; 253995ec8ae4SHeiko Stübner else 25406bc0d121SHeiko Stübner grf_offs = iom->offset; 2541b6c23275SDavid Wu } else { /* set current iomux offset */ 254295ec8ae4SHeiko Stübner iom->offset = (iom->type & IOMUX_SOURCE_PMU) ? 254395ec8ae4SHeiko Stübner pmu_offs : grf_offs; 25446bc0d121SHeiko Stübner } 25456bc0d121SHeiko Stübner 2546b6c23275SDavid Wu /* preset drv offset value, set new start value */ 2547b6c23275SDavid Wu if (drv->offset >= 0) { 2548b6c23275SDavid Wu if (iom->type & IOMUX_SOURCE_PMU) 2549b6c23275SDavid Wu drv_pmu_offs = drv->offset; 2550b6c23275SDavid Wu else 2551b6c23275SDavid Wu drv_grf_offs = drv->offset; 2552b6c23275SDavid Wu } else { /* set current drv offset */ 2553b6c23275SDavid Wu drv->offset = (iom->type & IOMUX_SOURCE_PMU) ? 2554b6c23275SDavid Wu drv_pmu_offs : drv_grf_offs; 2555b6c23275SDavid Wu } 2556b6c23275SDavid Wu 2557b6c23275SDavid Wu dev_dbg(d->dev, "bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n", 2558b6c23275SDavid Wu i, j, iom->offset, drv->offset); 25596bc0d121SHeiko Stübner 25606bc0d121SHeiko Stübner /* 25616bc0d121SHeiko Stübner * Increase offset according to iomux width. 256203716e1dSHeiko Stübner * 4bit iomux'es are spread over two registers. 25636bc0d121SHeiko Stübner */ 25648b6c6f93Sdavid.wu inc = (iom->type & (IOMUX_WIDTH_4BIT | 25658b6c6f93Sdavid.wu IOMUX_WIDTH_3BIT)) ? 8 : 4; 256695ec8ae4SHeiko Stübner if (iom->type & IOMUX_SOURCE_PMU) 256795ec8ae4SHeiko Stübner pmu_offs += inc; 256895ec8ae4SHeiko Stübner else 256903716e1dSHeiko Stübner grf_offs += inc; 25706bc0d121SHeiko Stübner 2571b6c23275SDavid Wu /* 2572b6c23275SDavid Wu * Increase offset according to drv width. 2573b6c23275SDavid Wu * 3bit drive-strenth'es are spread over two registers. 2574b6c23275SDavid Wu */ 2575b6c23275SDavid Wu if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) || 2576b6c23275SDavid Wu (drv->drv_type == DRV_TYPE_IO_3V3_ONLY)) 2577b6c23275SDavid Wu inc = 8; 2578b6c23275SDavid Wu else 2579b6c23275SDavid Wu inc = 4; 2580b6c23275SDavid Wu 2581b6c23275SDavid Wu if (iom->type & IOMUX_SOURCE_PMU) 2582b6c23275SDavid Wu drv_pmu_offs += inc; 2583b6c23275SDavid Wu else 2584b6c23275SDavid Wu drv_grf_offs += inc; 2585b6c23275SDavid Wu 25866bc0d121SHeiko Stübner bank_pins += 8; 25876bc0d121SHeiko Stübner } 2588d3e51161SHeiko Stübner } 2589d3e51161SHeiko Stübner 2590d3e51161SHeiko Stübner return ctrl; 2591d3e51161SHeiko Stübner } 2592d3e51161SHeiko Stübner 25938dca9331SChris Zhong #define RK3288_GRF_GPIO6C_IOMUX 0x64 25948dca9331SChris Zhong #define GPIO6C6_SEL_WRITE_ENABLE BIT(28) 25958dca9331SChris Zhong 25968dca9331SChris Zhong static u32 rk3288_grf_gpio6c_iomux; 25978dca9331SChris Zhong 25989198f509SChris Zhong static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev) 25999198f509SChris Zhong { 26009198f509SChris Zhong struct rockchip_pinctrl *info = dev_get_drvdata(dev); 26018dca9331SChris Zhong int ret = pinctrl_force_sleep(info->pctl_dev); 26029198f509SChris Zhong 26038dca9331SChris Zhong if (ret) 26048dca9331SChris Zhong return ret; 26058dca9331SChris Zhong 26068dca9331SChris Zhong /* 26078dca9331SChris Zhong * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save 26088dca9331SChris Zhong * the setting here, and restore it at resume. 26098dca9331SChris Zhong */ 26108dca9331SChris Zhong if (info->ctrl->type == RK3288) { 26118dca9331SChris Zhong ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX, 26128dca9331SChris Zhong &rk3288_grf_gpio6c_iomux); 26138dca9331SChris Zhong if (ret) { 26148dca9331SChris Zhong pinctrl_force_default(info->pctl_dev); 26158dca9331SChris Zhong return ret; 26168dca9331SChris Zhong } 26178dca9331SChris Zhong } 26188dca9331SChris Zhong 26198dca9331SChris Zhong return 0; 26209198f509SChris Zhong } 26219198f509SChris Zhong 26229198f509SChris Zhong static int __maybe_unused rockchip_pinctrl_resume(struct device *dev) 26239198f509SChris Zhong { 26249198f509SChris Zhong struct rockchip_pinctrl *info = dev_get_drvdata(dev); 26258dca9331SChris Zhong int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX, 26268dca9331SChris Zhong rk3288_grf_gpio6c_iomux | 26278dca9331SChris Zhong GPIO6C6_SEL_WRITE_ENABLE); 26288dca9331SChris Zhong 26298dca9331SChris Zhong if (ret) 26308dca9331SChris Zhong return ret; 26319198f509SChris Zhong 26329198f509SChris Zhong return pinctrl_force_default(info->pctl_dev); 26339198f509SChris Zhong } 26349198f509SChris Zhong 26359198f509SChris Zhong static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend, 26369198f509SChris Zhong rockchip_pinctrl_resume); 26379198f509SChris Zhong 2638d3e51161SHeiko Stübner static int rockchip_pinctrl_probe(struct platform_device *pdev) 2639d3e51161SHeiko Stübner { 2640d3e51161SHeiko Stübner struct rockchip_pinctrl *info; 2641d3e51161SHeiko Stübner struct device *dev = &pdev->dev; 2642d3e51161SHeiko Stübner struct rockchip_pin_ctrl *ctrl; 264314dee867SHeiko Stübner struct device_node *np = pdev->dev.of_node, *node; 2644d3e51161SHeiko Stübner struct resource *res; 2645751a99abSHeiko Stübner void __iomem *base; 2646d3e51161SHeiko Stübner int ret; 2647d3e51161SHeiko Stübner 2648d3e51161SHeiko Stübner if (!dev->of_node) { 2649d3e51161SHeiko Stübner dev_err(dev, "device tree node not found\n"); 2650d3e51161SHeiko Stübner return -ENODEV; 2651d3e51161SHeiko Stübner } 2652d3e51161SHeiko Stübner 2653d3e51161SHeiko Stübner info = devm_kzalloc(dev, sizeof(struct rockchip_pinctrl), GFP_KERNEL); 2654d3e51161SHeiko Stübner if (!info) 2655d3e51161SHeiko Stübner return -ENOMEM; 2656d3e51161SHeiko Stübner 2657622f3237SHeiko Stübner info->dev = dev; 2658622f3237SHeiko Stübner 2659d3e51161SHeiko Stübner ctrl = rockchip_pinctrl_get_soc_data(info, pdev); 2660d3e51161SHeiko Stübner if (!ctrl) { 2661d3e51161SHeiko Stübner dev_err(dev, "driver data not available\n"); 2662d3e51161SHeiko Stübner return -EINVAL; 2663d3e51161SHeiko Stübner } 2664d3e51161SHeiko Stübner info->ctrl = ctrl; 2665d3e51161SHeiko Stübner 26661e747e59SHeiko Stübner node = of_parse_phandle(np, "rockchip,grf", 0); 26671e747e59SHeiko Stübner if (node) { 26681e747e59SHeiko Stübner info->regmap_base = syscon_node_to_regmap(node); 26691e747e59SHeiko Stübner if (IS_ERR(info->regmap_base)) 26701e747e59SHeiko Stübner return PTR_ERR(info->regmap_base); 26711e747e59SHeiko Stübner } else { 2672d3e51161SHeiko Stübner res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2673751a99abSHeiko Stübner base = devm_ioremap_resource(&pdev->dev, res); 2674751a99abSHeiko Stübner if (IS_ERR(base)) 2675751a99abSHeiko Stübner return PTR_ERR(base); 2676751a99abSHeiko Stübner 2677751a99abSHeiko Stübner rockchip_regmap_config.max_register = resource_size(res) - 4; 2678751a99abSHeiko Stübner rockchip_regmap_config.name = "rockchip,pinctrl"; 2679751a99abSHeiko Stübner info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base, 2680751a99abSHeiko Stübner &rockchip_regmap_config); 2681d3e51161SHeiko Stübner 2682bfc7a42aSHeiko Stübner /* to check for the old dt-bindings */ 2683bfc7a42aSHeiko Stübner info->reg_size = resource_size(res); 2684bfc7a42aSHeiko Stübner 2685bfc7a42aSHeiko Stübner /* Honor the old binding, with pull registers as 2nd resource */ 2686bfc7a42aSHeiko Stübner if (ctrl->type == RK3188 && info->reg_size < 0x200) { 26876ca5274dSHeiko Stübner res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 2688751a99abSHeiko Stübner base = devm_ioremap_resource(&pdev->dev, res); 2689751a99abSHeiko Stübner if (IS_ERR(base)) 2690751a99abSHeiko Stübner return PTR_ERR(base); 2691751a99abSHeiko Stübner 26921e747e59SHeiko Stübner rockchip_regmap_config.max_register = 26931e747e59SHeiko Stübner resource_size(res) - 4; 2694751a99abSHeiko Stübner rockchip_regmap_config.name = "rockchip,pinctrl-pull"; 26951e747e59SHeiko Stübner info->regmap_pull = devm_regmap_init_mmio(&pdev->dev, 26961e747e59SHeiko Stübner base, 2697751a99abSHeiko Stübner &rockchip_regmap_config); 26986ca5274dSHeiko Stübner } 26991e747e59SHeiko Stübner } 27006ca5274dSHeiko Stübner 270114dee867SHeiko Stübner /* try to find the optional reference to the pmu syscon */ 270214dee867SHeiko Stübner node = of_parse_phandle(np, "rockchip,pmu", 0); 270314dee867SHeiko Stübner if (node) { 270414dee867SHeiko Stübner info->regmap_pmu = syscon_node_to_regmap(node); 270514dee867SHeiko Stübner if (IS_ERR(info->regmap_pmu)) 270614dee867SHeiko Stübner return PTR_ERR(info->regmap_pmu); 270714dee867SHeiko Stübner } 270814dee867SHeiko Stübner 2709d3e51161SHeiko Stübner ret = rockchip_gpiolib_register(pdev, info); 2710d3e51161SHeiko Stübner if (ret) 2711d3e51161SHeiko Stübner return ret; 2712d3e51161SHeiko Stübner 2713d3e51161SHeiko Stübner ret = rockchip_pinctrl_register(pdev, info); 2714d3e51161SHeiko Stübner if (ret) { 2715d3e51161SHeiko Stübner rockchip_gpiolib_unregister(pdev, info); 2716d3e51161SHeiko Stübner return ret; 2717d3e51161SHeiko Stübner } 2718d3e51161SHeiko Stübner 2719d3e51161SHeiko Stübner platform_set_drvdata(pdev, info); 2720d3e51161SHeiko Stübner 2721d3e51161SHeiko Stübner return 0; 2722d3e51161SHeiko Stübner } 2723d3e51161SHeiko Stübner 2724b9c6dcabSAndy Yan static struct rockchip_pin_bank rv1108_pin_banks[] = { 2725688daf23SAndy Yan PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU, 2726688daf23SAndy Yan IOMUX_SOURCE_PMU, 2727688daf23SAndy Yan IOMUX_SOURCE_PMU, 2728688daf23SAndy Yan IOMUX_SOURCE_PMU), 2729688daf23SAndy Yan PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0), 2730688daf23SAndy Yan PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0), 2731688daf23SAndy Yan PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0), 2732688daf23SAndy Yan }; 2733688daf23SAndy Yan 2734b9c6dcabSAndy Yan static struct rockchip_pin_ctrl rv1108_pin_ctrl = { 2735b9c6dcabSAndy Yan .pin_banks = rv1108_pin_banks, 2736b9c6dcabSAndy Yan .nr_banks = ARRAY_SIZE(rv1108_pin_banks), 2737b9c6dcabSAndy Yan .label = "RV1108-GPIO", 2738b9c6dcabSAndy Yan .type = RV1108, 2739688daf23SAndy Yan .grf_mux_offset = 0x10, 2740688daf23SAndy Yan .pmu_mux_offset = 0x0, 2741b9c6dcabSAndy Yan .pull_calc_reg = rv1108_calc_pull_reg_and_bit, 2742b9c6dcabSAndy Yan .drv_calc_reg = rv1108_calc_drv_reg_and_bit, 2743688daf23SAndy Yan }; 2744688daf23SAndy Yan 2745d3e51161SHeiko Stübner static struct rockchip_pin_bank rk2928_pin_banks[] = { 2746d3e51161SHeiko Stübner PIN_BANK(0, 32, "gpio0"), 2747d3e51161SHeiko Stübner PIN_BANK(1, 32, "gpio1"), 2748d3e51161SHeiko Stübner PIN_BANK(2, 32, "gpio2"), 2749d3e51161SHeiko Stübner PIN_BANK(3, 32, "gpio3"), 2750d3e51161SHeiko Stübner }; 2751d3e51161SHeiko Stübner 2752d3e51161SHeiko Stübner static struct rockchip_pin_ctrl rk2928_pin_ctrl = { 2753d3e51161SHeiko Stübner .pin_banks = rk2928_pin_banks, 2754d3e51161SHeiko Stübner .nr_banks = ARRAY_SIZE(rk2928_pin_banks), 2755d3e51161SHeiko Stübner .label = "RK2928-GPIO", 2756a282926dSHeiko Stübner .type = RK2928, 275795ec8ae4SHeiko Stübner .grf_mux_offset = 0xa8, 2758a282926dSHeiko Stübner .pull_calc_reg = rk2928_calc_pull_reg_and_bit, 2759d3e51161SHeiko Stübner }; 2760d3e51161SHeiko Stübner 2761c5ce7670SXing Zheng static struct rockchip_pin_bank rk3036_pin_banks[] = { 2762c5ce7670SXing Zheng PIN_BANK(0, 32, "gpio0"), 2763c5ce7670SXing Zheng PIN_BANK(1, 32, "gpio1"), 2764c5ce7670SXing Zheng PIN_BANK(2, 32, "gpio2"), 2765c5ce7670SXing Zheng }; 2766c5ce7670SXing Zheng 2767c5ce7670SXing Zheng static struct rockchip_pin_ctrl rk3036_pin_ctrl = { 2768c5ce7670SXing Zheng .pin_banks = rk3036_pin_banks, 2769c5ce7670SXing Zheng .nr_banks = ARRAY_SIZE(rk3036_pin_banks), 2770c5ce7670SXing Zheng .label = "RK3036-GPIO", 2771c5ce7670SXing Zheng .type = RK2928, 2772c5ce7670SXing Zheng .grf_mux_offset = 0xa8, 2773c5ce7670SXing Zheng .pull_calc_reg = rk2928_calc_pull_reg_and_bit, 2774c5ce7670SXing Zheng }; 2775c5ce7670SXing Zheng 2776d3e51161SHeiko Stübner static struct rockchip_pin_bank rk3066a_pin_banks[] = { 2777d3e51161SHeiko Stübner PIN_BANK(0, 32, "gpio0"), 2778d3e51161SHeiko Stübner PIN_BANK(1, 32, "gpio1"), 2779d3e51161SHeiko Stübner PIN_BANK(2, 32, "gpio2"), 2780d3e51161SHeiko Stübner PIN_BANK(3, 32, "gpio3"), 2781d3e51161SHeiko Stübner PIN_BANK(4, 32, "gpio4"), 2782d3e51161SHeiko Stübner PIN_BANK(6, 16, "gpio6"), 2783d3e51161SHeiko Stübner }; 2784d3e51161SHeiko Stübner 2785d3e51161SHeiko Stübner static struct rockchip_pin_ctrl rk3066a_pin_ctrl = { 2786d3e51161SHeiko Stübner .pin_banks = rk3066a_pin_banks, 2787d3e51161SHeiko Stübner .nr_banks = ARRAY_SIZE(rk3066a_pin_banks), 2788d3e51161SHeiko Stübner .label = "RK3066a-GPIO", 2789a282926dSHeiko Stübner .type = RK2928, 279095ec8ae4SHeiko Stübner .grf_mux_offset = 0xa8, 2791a282926dSHeiko Stübner .pull_calc_reg = rk2928_calc_pull_reg_and_bit, 2792d3e51161SHeiko Stübner }; 2793d3e51161SHeiko Stübner 2794d3e51161SHeiko Stübner static struct rockchip_pin_bank rk3066b_pin_banks[] = { 2795d3e51161SHeiko Stübner PIN_BANK(0, 32, "gpio0"), 2796d3e51161SHeiko Stübner PIN_BANK(1, 32, "gpio1"), 2797d3e51161SHeiko Stübner PIN_BANK(2, 32, "gpio2"), 2798d3e51161SHeiko Stübner PIN_BANK(3, 32, "gpio3"), 2799d3e51161SHeiko Stübner }; 2800d3e51161SHeiko Stübner 2801d3e51161SHeiko Stübner static struct rockchip_pin_ctrl rk3066b_pin_ctrl = { 2802d3e51161SHeiko Stübner .pin_banks = rk3066b_pin_banks, 2803d3e51161SHeiko Stübner .nr_banks = ARRAY_SIZE(rk3066b_pin_banks), 2804d3e51161SHeiko Stübner .label = "RK3066b-GPIO", 2805a282926dSHeiko Stübner .type = RK3066B, 280695ec8ae4SHeiko Stübner .grf_mux_offset = 0x60, 2807d3e51161SHeiko Stübner }; 2808d3e51161SHeiko Stübner 2809d3e51161SHeiko Stübner static struct rockchip_pin_bank rk3188_pin_banks[] = { 2810fc72c923SHeiko Stübner PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0), 2811d3e51161SHeiko Stübner PIN_BANK(1, 32, "gpio1"), 2812d3e51161SHeiko Stübner PIN_BANK(2, 32, "gpio2"), 2813d3e51161SHeiko Stübner PIN_BANK(3, 32, "gpio3"), 2814d3e51161SHeiko Stübner }; 2815d3e51161SHeiko Stübner 2816d3e51161SHeiko Stübner static struct rockchip_pin_ctrl rk3188_pin_ctrl = { 2817d3e51161SHeiko Stübner .pin_banks = rk3188_pin_banks, 2818d3e51161SHeiko Stübner .nr_banks = ARRAY_SIZE(rk3188_pin_banks), 2819d3e51161SHeiko Stübner .label = "RK3188-GPIO", 2820a282926dSHeiko Stübner .type = RK3188, 282195ec8ae4SHeiko Stübner .grf_mux_offset = 0x60, 28226ca5274dSHeiko Stübner .pull_calc_reg = rk3188_calc_pull_reg_and_bit, 2823d3e51161SHeiko Stübner }; 2824d3e51161SHeiko Stübner 2825fea0fe60SJeffy Chen static struct rockchip_pin_bank rk3228_pin_banks[] = { 2826fea0fe60SJeffy Chen PIN_BANK(0, 32, "gpio0"), 2827fea0fe60SJeffy Chen PIN_BANK(1, 32, "gpio1"), 2828fea0fe60SJeffy Chen PIN_BANK(2, 32, "gpio2"), 2829fea0fe60SJeffy Chen PIN_BANK(3, 32, "gpio3"), 2830fea0fe60SJeffy Chen }; 2831fea0fe60SJeffy Chen 2832fea0fe60SJeffy Chen static struct rockchip_pin_ctrl rk3228_pin_ctrl = { 2833fea0fe60SJeffy Chen .pin_banks = rk3228_pin_banks, 2834fea0fe60SJeffy Chen .nr_banks = ARRAY_SIZE(rk3228_pin_banks), 2835fea0fe60SJeffy Chen .label = "RK3228-GPIO", 2836fea0fe60SJeffy Chen .type = RK3288, 2837fea0fe60SJeffy Chen .grf_mux_offset = 0x0, 2838fea0fe60SJeffy Chen .pull_calc_reg = rk3228_calc_pull_reg_and_bit, 2839fea0fe60SJeffy Chen .drv_calc_reg = rk3228_calc_drv_reg_and_bit, 2840fea0fe60SJeffy Chen }; 2841fea0fe60SJeffy Chen 2842304f077dSHeiko Stübner static struct rockchip_pin_bank rk3288_pin_banks[] = { 2843304f077dSHeiko Stübner PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU, 2844304f077dSHeiko Stübner IOMUX_SOURCE_PMU, 2845304f077dSHeiko Stübner IOMUX_SOURCE_PMU, 2846304f077dSHeiko Stübner IOMUX_UNROUTED 2847304f077dSHeiko Stübner ), 2848304f077dSHeiko Stübner PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED, 2849304f077dSHeiko Stübner IOMUX_UNROUTED, 2850304f077dSHeiko Stübner IOMUX_UNROUTED, 2851304f077dSHeiko Stübner 0 2852304f077dSHeiko Stübner ), 2853304f077dSHeiko Stübner PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED), 2854304f077dSHeiko Stübner PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT), 2855304f077dSHeiko Stübner PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT, 2856304f077dSHeiko Stübner IOMUX_WIDTH_4BIT, 2857304f077dSHeiko Stübner 0, 2858304f077dSHeiko Stübner 0 2859304f077dSHeiko Stübner ), 2860304f077dSHeiko Stübner PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED, 2861304f077dSHeiko Stübner 0, 2862304f077dSHeiko Stübner 0, 2863304f077dSHeiko Stübner IOMUX_UNROUTED 2864304f077dSHeiko Stübner ), 2865304f077dSHeiko Stübner PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED), 2866304f077dSHeiko Stübner PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0, 2867304f077dSHeiko Stübner 0, 2868304f077dSHeiko Stübner IOMUX_WIDTH_4BIT, 2869304f077dSHeiko Stübner IOMUX_UNROUTED 2870304f077dSHeiko Stübner ), 2871304f077dSHeiko Stübner PIN_BANK(8, 16, "gpio8"), 2872304f077dSHeiko Stübner }; 2873304f077dSHeiko Stübner 2874304f077dSHeiko Stübner static struct rockchip_pin_ctrl rk3288_pin_ctrl = { 2875304f077dSHeiko Stübner .pin_banks = rk3288_pin_banks, 2876304f077dSHeiko Stübner .nr_banks = ARRAY_SIZE(rk3288_pin_banks), 2877304f077dSHeiko Stübner .label = "RK3288-GPIO", 287866d750e1SHeiko Stübner .type = RK3288, 2879304f077dSHeiko Stübner .grf_mux_offset = 0x0, 2880304f077dSHeiko Stübner .pmu_mux_offset = 0x84, 2881304f077dSHeiko Stübner .pull_calc_reg = rk3288_calc_pull_reg_and_bit, 2882ef17f69fSHeiko Stübner .drv_calc_reg = rk3288_calc_drv_reg_and_bit, 2883304f077dSHeiko Stübner }; 2884304f077dSHeiko Stübner 28853818e4a7Sdavid.wu static struct rockchip_pin_bank rk3328_pin_banks[] = { 28863818e4a7Sdavid.wu PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0), 28873818e4a7Sdavid.wu PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0), 28883818e4a7Sdavid.wu PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 28893818e4a7Sdavid.wu IOMUX_WIDTH_3BIT | IOMUX_RECALCED, 28903818e4a7Sdavid.wu IOMUX_WIDTH_3BIT | IOMUX_RECALCED, 28913818e4a7Sdavid.wu 0), 28923818e4a7Sdavid.wu PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 28933818e4a7Sdavid.wu IOMUX_WIDTH_3BIT, 28943818e4a7Sdavid.wu IOMUX_WIDTH_3BIT | IOMUX_RECALCED, 28953818e4a7Sdavid.wu 0, 28963818e4a7Sdavid.wu 0), 28973818e4a7Sdavid.wu }; 28983818e4a7Sdavid.wu 28993818e4a7Sdavid.wu static struct rockchip_pin_ctrl rk3328_pin_ctrl = { 29003818e4a7Sdavid.wu .pin_banks = rk3328_pin_banks, 29013818e4a7Sdavid.wu .nr_banks = ARRAY_SIZE(rk3328_pin_banks), 29023818e4a7Sdavid.wu .label = "RK3328-GPIO", 29033818e4a7Sdavid.wu .type = RK3288, 29043818e4a7Sdavid.wu .grf_mux_offset = 0x0, 29053818e4a7Sdavid.wu .pull_calc_reg = rk3228_calc_pull_reg_and_bit, 29063818e4a7Sdavid.wu .drv_calc_reg = rk3228_calc_drv_reg_and_bit, 29073818e4a7Sdavid.wu .iomux_recalc = rk3328_recalc_mux, 2908728d3f5aSdavid.wu .schmitt_calc_reg = rk3328_calc_schmitt_reg_and_bit, 29093818e4a7Sdavid.wu }; 29103818e4a7Sdavid.wu 2911daecdc66SHeiko Stübner static struct rockchip_pin_bank rk3368_pin_banks[] = { 2912daecdc66SHeiko Stübner PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU, 2913daecdc66SHeiko Stübner IOMUX_SOURCE_PMU, 2914daecdc66SHeiko Stübner IOMUX_SOURCE_PMU, 2915daecdc66SHeiko Stübner IOMUX_SOURCE_PMU 2916daecdc66SHeiko Stübner ), 2917daecdc66SHeiko Stübner PIN_BANK(1, 32, "gpio1"), 2918daecdc66SHeiko Stübner PIN_BANK(2, 32, "gpio2"), 2919daecdc66SHeiko Stübner PIN_BANK(3, 32, "gpio3"), 2920daecdc66SHeiko Stübner }; 2921daecdc66SHeiko Stübner 2922daecdc66SHeiko Stübner static struct rockchip_pin_ctrl rk3368_pin_ctrl = { 2923daecdc66SHeiko Stübner .pin_banks = rk3368_pin_banks, 2924daecdc66SHeiko Stübner .nr_banks = ARRAY_SIZE(rk3368_pin_banks), 2925daecdc66SHeiko Stübner .label = "RK3368-GPIO", 2926daecdc66SHeiko Stübner .type = RK3368, 2927daecdc66SHeiko Stübner .grf_mux_offset = 0x0, 2928daecdc66SHeiko Stübner .pmu_mux_offset = 0x0, 2929daecdc66SHeiko Stübner .pull_calc_reg = rk3368_calc_pull_reg_and_bit, 2930daecdc66SHeiko Stübner .drv_calc_reg = rk3368_calc_drv_reg_and_bit, 2931daecdc66SHeiko Stübner }; 2932daecdc66SHeiko Stübner 2933b6c23275SDavid Wu static struct rockchip_pin_bank rk3399_pin_banks[] = { 29343ba6767aSDavid Wu PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0", 29353ba6767aSDavid Wu IOMUX_SOURCE_PMU, 2936b6c23275SDavid Wu IOMUX_SOURCE_PMU, 2937b6c23275SDavid Wu IOMUX_SOURCE_PMU, 2938b6c23275SDavid Wu IOMUX_SOURCE_PMU, 2939b6c23275SDavid Wu DRV_TYPE_IO_1V8_ONLY, 2940b6c23275SDavid Wu DRV_TYPE_IO_1V8_ONLY, 2941b6c23275SDavid Wu DRV_TYPE_IO_DEFAULT, 2942b6c23275SDavid Wu DRV_TYPE_IO_DEFAULT, 2943b6c23275SDavid Wu 0x0, 2944b6c23275SDavid Wu 0x8, 2945b6c23275SDavid Wu -1, 29463ba6767aSDavid Wu -1, 29473ba6767aSDavid Wu PULL_TYPE_IO_1V8_ONLY, 29483ba6767aSDavid Wu PULL_TYPE_IO_1V8_ONLY, 29493ba6767aSDavid Wu PULL_TYPE_IO_DEFAULT, 29503ba6767aSDavid Wu PULL_TYPE_IO_DEFAULT 2951b6c23275SDavid Wu ), 2952b6c23275SDavid Wu PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU, 2953b6c23275SDavid Wu IOMUX_SOURCE_PMU, 2954b6c23275SDavid Wu IOMUX_SOURCE_PMU, 2955b6c23275SDavid Wu IOMUX_SOURCE_PMU, 2956b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0, 2957b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0, 2958b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0, 2959b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0, 2960b6c23275SDavid Wu 0x20, 2961b6c23275SDavid Wu 0x28, 2962b6c23275SDavid Wu 0x30, 2963b6c23275SDavid Wu 0x38 2964b6c23275SDavid Wu ), 29653ba6767aSDavid Wu PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0, 2966b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0, 2967b6c23275SDavid Wu DRV_TYPE_IO_1V8_ONLY, 29683ba6767aSDavid Wu DRV_TYPE_IO_1V8_ONLY, 29693ba6767aSDavid Wu PULL_TYPE_IO_DEFAULT, 29703ba6767aSDavid Wu PULL_TYPE_IO_DEFAULT, 29713ba6767aSDavid Wu PULL_TYPE_IO_1V8_ONLY, 29723ba6767aSDavid Wu PULL_TYPE_IO_1V8_ONLY 2973b6c23275SDavid Wu ), 2974b6c23275SDavid Wu PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY, 2975b6c23275SDavid Wu DRV_TYPE_IO_3V3_ONLY, 2976b6c23275SDavid Wu DRV_TYPE_IO_3V3_ONLY, 2977b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0 2978b6c23275SDavid Wu ), 2979b6c23275SDavid Wu PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0, 2980b6c23275SDavid Wu DRV_TYPE_IO_1V8_3V0_AUTO, 2981b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0, 2982b6c23275SDavid Wu DRV_TYPE_IO_1V8_OR_3V0 2983b6c23275SDavid Wu ), 2984b6c23275SDavid Wu }; 2985b6c23275SDavid Wu 2986b6c23275SDavid Wu static struct rockchip_pin_ctrl rk3399_pin_ctrl = { 2987b6c23275SDavid Wu .pin_banks = rk3399_pin_banks, 2988b6c23275SDavid Wu .nr_banks = ARRAY_SIZE(rk3399_pin_banks), 2989b6c23275SDavid Wu .label = "RK3399-GPIO", 2990b6c23275SDavid Wu .type = RK3399, 2991b6c23275SDavid Wu .grf_mux_offset = 0xe000, 2992b6c23275SDavid Wu .pmu_mux_offset = 0x0, 2993b6c23275SDavid Wu .grf_drv_offset = 0xe100, 2994b6c23275SDavid Wu .pmu_drv_offset = 0x80, 2995b6c23275SDavid Wu .pull_calc_reg = rk3399_calc_pull_reg_and_bit, 2996b6c23275SDavid Wu .drv_calc_reg = rk3399_calc_drv_reg_and_bit, 2997b6c23275SDavid Wu }; 2998daecdc66SHeiko Stübner 2999d3e51161SHeiko Stübner static const struct of_device_id rockchip_pinctrl_dt_match[] = { 3000b9c6dcabSAndy Yan { .compatible = "rockchip,rv1108-pinctrl", 3001*cdbbd26fSMasahiro Yamada .data = &rv1108_pin_ctrl }, 3002d3e51161SHeiko Stübner { .compatible = "rockchip,rk2928-pinctrl", 3003*cdbbd26fSMasahiro Yamada .data = &rk2928_pin_ctrl }, 3004c5ce7670SXing Zheng { .compatible = "rockchip,rk3036-pinctrl", 3005*cdbbd26fSMasahiro Yamada .data = &rk3036_pin_ctrl }, 3006d3e51161SHeiko Stübner { .compatible = "rockchip,rk3066a-pinctrl", 3007*cdbbd26fSMasahiro Yamada .data = &rk3066a_pin_ctrl }, 3008d3e51161SHeiko Stübner { .compatible = "rockchip,rk3066b-pinctrl", 3009*cdbbd26fSMasahiro Yamada .data = &rk3066b_pin_ctrl }, 3010d3e51161SHeiko Stübner { .compatible = "rockchip,rk3188-pinctrl", 3011*cdbbd26fSMasahiro Yamada .data = &rk3188_pin_ctrl }, 3012fea0fe60SJeffy Chen { .compatible = "rockchip,rk3228-pinctrl", 3013*cdbbd26fSMasahiro Yamada .data = &rk3228_pin_ctrl }, 3014304f077dSHeiko Stübner { .compatible = "rockchip,rk3288-pinctrl", 3015*cdbbd26fSMasahiro Yamada .data = &rk3288_pin_ctrl }, 30163818e4a7Sdavid.wu { .compatible = "rockchip,rk3328-pinctrl", 3017*cdbbd26fSMasahiro Yamada .data = &rk3328_pin_ctrl }, 3018daecdc66SHeiko Stübner { .compatible = "rockchip,rk3368-pinctrl", 3019*cdbbd26fSMasahiro Yamada .data = &rk3368_pin_ctrl }, 3020b6c23275SDavid Wu { .compatible = "rockchip,rk3399-pinctrl", 3021*cdbbd26fSMasahiro Yamada .data = &rk3399_pin_ctrl }, 3022d3e51161SHeiko Stübner {}, 3023d3e51161SHeiko Stübner }; 3024d3e51161SHeiko Stübner 3025d3e51161SHeiko Stübner static struct platform_driver rockchip_pinctrl_driver = { 3026d3e51161SHeiko Stübner .probe = rockchip_pinctrl_probe, 3027d3e51161SHeiko Stübner .driver = { 3028d3e51161SHeiko Stübner .name = "rockchip-pinctrl", 30299198f509SChris Zhong .pm = &rockchip_pinctrl_dev_pm_ops, 30300be9e70dSAxel Lin .of_match_table = rockchip_pinctrl_dt_match, 3031d3e51161SHeiko Stübner }, 3032d3e51161SHeiko Stübner }; 3033d3e51161SHeiko Stübner 3034d3e51161SHeiko Stübner static int __init rockchip_pinctrl_drv_register(void) 3035d3e51161SHeiko Stübner { 3036d3e51161SHeiko Stübner return platform_driver_register(&rockchip_pinctrl_driver); 3037d3e51161SHeiko Stübner } 3038d3e51161SHeiko Stübner postcore_initcall(rockchip_pinctrl_drv_register); 3039