16ac73095SBeniamino Galvani /* 26ac73095SBeniamino Galvani * Pin controller and GPIO driver for Amlogic Meson SoCs 36ac73095SBeniamino Galvani * 46ac73095SBeniamino Galvani * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com> 56ac73095SBeniamino Galvani * 66ac73095SBeniamino Galvani * This program is free software; you can redistribute it and/or 76ac73095SBeniamino Galvani * modify it under the terms of the GNU General Public License 86ac73095SBeniamino Galvani * version 2 as published by the Free Software Foundation. 96ac73095SBeniamino Galvani * 106ac73095SBeniamino Galvani * You should have received a copy of the GNU General Public License 116ac73095SBeniamino Galvani * along with this program. If not, see <http://www.gnu.org/licenses/>. 126ac73095SBeniamino Galvani */ 136ac73095SBeniamino Galvani 146ac73095SBeniamino Galvani /* 156ac73095SBeniamino Galvani * The available pins are organized in banks (A,B,C,D,E,X,Y,Z,AO, 16faa246deSCarlo Caione * BOOT,CARD for meson6, X,Y,DV,H,Z,AO,BOOT,CARD for meson8 and 17faa246deSCarlo Caione * X,Y,DV,H,AO,BOOT,CARD,DIF for meson8b) and each bank has a 18faa246deSCarlo Caione * variable number of pins. 196ac73095SBeniamino Galvani * 206ac73095SBeniamino Galvani * The AO bank is special because it belongs to the Always-On power 216ac73095SBeniamino Galvani * domain which can't be powered off; the bank also uses a set of 226ac73095SBeniamino Galvani * registers different from the other banks. 236ac73095SBeniamino Galvani * 24db80f0e1SBeniamino Galvani * For each pin controller there are 4 different register ranges that 25db80f0e1SBeniamino Galvani * control the following properties of the pins: 266ac73095SBeniamino Galvani * 1) pin muxing 276ac73095SBeniamino Galvani * 2) pull enable/disable 286ac73095SBeniamino Galvani * 3) pull up/down 296ac73095SBeniamino Galvani * 4) GPIO direction, output value, input value 306ac73095SBeniamino Galvani * 316ac73095SBeniamino Galvani * In some cases the register ranges for pull enable and pull 326ac73095SBeniamino Galvani * direction are the same and thus there are only 3 register ranges. 336ac73095SBeniamino Galvani * 34e66dd48eSXingyu Chen * Since Meson G12A SoC, the ao register ranges for gpio, pull enable 35e66dd48eSXingyu Chen * and pull direction are the same, so there are only 2 register ranges. 36e66dd48eSXingyu Chen * 376ac73095SBeniamino Galvani * For the pull and GPIO configuration every bank uses a contiguous 386ac73095SBeniamino Galvani * set of bits in the register sets described above; the same register 396ac73095SBeniamino Galvani * can be shared by more banks with different offsets. 406ac73095SBeniamino Galvani * 416ac73095SBeniamino Galvani * In addition to this there are some registers shared between all 426ac73095SBeniamino Galvani * banks that control the IRQ functionality. This feature is not 436ac73095SBeniamino Galvani * supported at the moment by the driver. 446ac73095SBeniamino Galvani */ 456ac73095SBeniamino Galvani 466ac73095SBeniamino Galvani #include <linux/device.h> 471c5fb66aSLinus Walleij #include <linux/gpio/driver.h> 486ac73095SBeniamino Galvani #include <linux/init.h> 496ac73095SBeniamino Galvani #include <linux/io.h> 506ac73095SBeniamino Galvani #include <linux/of.h> 516ac73095SBeniamino Galvani #include <linux/of_address.h> 52277d14ebSJerome Brunet #include <linux/of_device.h> 536ac73095SBeniamino Galvani #include <linux/pinctrl/pinconf-generic.h> 546ac73095SBeniamino Galvani #include <linux/pinctrl/pinconf.h> 556ac73095SBeniamino Galvani #include <linux/pinctrl/pinctrl.h> 566ac73095SBeniamino Galvani #include <linux/pinctrl/pinmux.h> 576ac73095SBeniamino Galvani #include <linux/platform_device.h> 586ac73095SBeniamino Galvani #include <linux/regmap.h> 596ac73095SBeniamino Galvani #include <linux/seq_file.h> 606ac73095SBeniamino Galvani 616ac73095SBeniamino Galvani #include "../core.h" 626ac73095SBeniamino Galvani #include "../pinctrl-utils.h" 636ac73095SBeniamino Galvani #include "pinctrl-meson.h" 646ac73095SBeniamino Galvani 656ac73095SBeniamino Galvani /** 666ac73095SBeniamino Galvani * meson_get_bank() - find the bank containing a given pin 676ac73095SBeniamino Galvani * 68db80f0e1SBeniamino Galvani * @pc: the pinctrl instance 696ac73095SBeniamino Galvani * @pin: the pin number 706ac73095SBeniamino Galvani * @bank: the found bank 716ac73095SBeniamino Galvani * 726ac73095SBeniamino Galvani * Return: 0 on success, a negative value on error 736ac73095SBeniamino Galvani */ 74db80f0e1SBeniamino Galvani static int meson_get_bank(struct meson_pinctrl *pc, unsigned int pin, 756ac73095SBeniamino Galvani struct meson_bank **bank) 766ac73095SBeniamino Galvani { 776ac73095SBeniamino Galvani int i; 786ac73095SBeniamino Galvani 79db80f0e1SBeniamino Galvani for (i = 0; i < pc->data->num_banks; i++) { 80db80f0e1SBeniamino Galvani if (pin >= pc->data->banks[i].first && 81db80f0e1SBeniamino Galvani pin <= pc->data->banks[i].last) { 82db80f0e1SBeniamino Galvani *bank = &pc->data->banks[i]; 836ac73095SBeniamino Galvani return 0; 846ac73095SBeniamino Galvani } 856ac73095SBeniamino Galvani } 866ac73095SBeniamino Galvani 876ac73095SBeniamino Galvani return -EINVAL; 886ac73095SBeniamino Galvani } 896ac73095SBeniamino Galvani 906ac73095SBeniamino Galvani /** 916ac73095SBeniamino Galvani * meson_calc_reg_and_bit() - calculate register and bit for a pin 926ac73095SBeniamino Galvani * 936ac73095SBeniamino Galvani * @bank: the bank containing the pin 946ac73095SBeniamino Galvani * @pin: the pin number 956ac73095SBeniamino Galvani * @reg_type: the type of register needed (pull-enable, pull, etc...) 966ac73095SBeniamino Galvani * @reg: the computed register offset 976ac73095SBeniamino Galvani * @bit: the computed bit 986ac73095SBeniamino Galvani */ 996ac73095SBeniamino Galvani static void meson_calc_reg_and_bit(struct meson_bank *bank, unsigned int pin, 1006ac73095SBeniamino Galvani enum meson_reg_type reg_type, 1016ac73095SBeniamino Galvani unsigned int *reg, unsigned int *bit) 1026ac73095SBeniamino Galvani { 1036ac73095SBeniamino Galvani struct meson_reg_desc *desc = &bank->regs[reg_type]; 1046ac73095SBeniamino Galvani 1056ac73095SBeniamino Galvani *reg = desc->reg * 4; 1066ac73095SBeniamino Galvani *bit = desc->bit + pin - bank->first; 1076ac73095SBeniamino Galvani } 1086ac73095SBeniamino Galvani 1096ac73095SBeniamino Galvani static int meson_get_groups_count(struct pinctrl_dev *pcdev) 1106ac73095SBeniamino Galvani { 1116ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 1126ac73095SBeniamino Galvani 1136ac73095SBeniamino Galvani return pc->data->num_groups; 1146ac73095SBeniamino Galvani } 1156ac73095SBeniamino Galvani 1166ac73095SBeniamino Galvani static const char *meson_get_group_name(struct pinctrl_dev *pcdev, 1176ac73095SBeniamino Galvani unsigned selector) 1186ac73095SBeniamino Galvani { 1196ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 1206ac73095SBeniamino Galvani 1216ac73095SBeniamino Galvani return pc->data->groups[selector].name; 1226ac73095SBeniamino Galvani } 1236ac73095SBeniamino Galvani 1246ac73095SBeniamino Galvani static int meson_get_group_pins(struct pinctrl_dev *pcdev, unsigned selector, 1256ac73095SBeniamino Galvani const unsigned **pins, unsigned *num_pins) 1266ac73095SBeniamino Galvani { 1276ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 1286ac73095SBeniamino Galvani 1296ac73095SBeniamino Galvani *pins = pc->data->groups[selector].pins; 1306ac73095SBeniamino Galvani *num_pins = pc->data->groups[selector].num_pins; 1316ac73095SBeniamino Galvani 1326ac73095SBeniamino Galvani return 0; 1336ac73095SBeniamino Galvani } 1346ac73095SBeniamino Galvani 1356ac73095SBeniamino Galvani static void meson_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s, 1366ac73095SBeniamino Galvani unsigned offset) 1376ac73095SBeniamino Galvani { 1386ac73095SBeniamino Galvani seq_printf(s, " %s", dev_name(pcdev->dev)); 1396ac73095SBeniamino Galvani } 1406ac73095SBeniamino Galvani 1416ac73095SBeniamino Galvani static const struct pinctrl_ops meson_pctrl_ops = { 1426ac73095SBeniamino Galvani .get_groups_count = meson_get_groups_count, 1436ac73095SBeniamino Galvani .get_group_name = meson_get_group_name, 1446ac73095SBeniamino Galvani .get_group_pins = meson_get_group_pins, 1456ac73095SBeniamino Galvani .dt_node_to_map = pinconf_generic_dt_node_to_map_all, 146d32f7fd3SIrina Tirdea .dt_free_map = pinctrl_utils_free_map, 1476ac73095SBeniamino Galvani .pin_dbg_show = meson_pin_dbg_show, 1486ac73095SBeniamino Galvani }; 1496ac73095SBeniamino Galvani 150ce385aa2SJerome Brunet int meson_pmx_get_funcs_count(struct pinctrl_dev *pcdev) 1516ac73095SBeniamino Galvani { 1526ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 1536ac73095SBeniamino Galvani 1546ac73095SBeniamino Galvani return pc->data->num_funcs; 1556ac73095SBeniamino Galvani } 1566ac73095SBeniamino Galvani 157ce385aa2SJerome Brunet const char *meson_pmx_get_func_name(struct pinctrl_dev *pcdev, 1586ac73095SBeniamino Galvani unsigned selector) 1596ac73095SBeniamino Galvani { 1606ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 1616ac73095SBeniamino Galvani 1626ac73095SBeniamino Galvani return pc->data->funcs[selector].name; 1636ac73095SBeniamino Galvani } 1646ac73095SBeniamino Galvani 165ce385aa2SJerome Brunet int meson_pmx_get_groups(struct pinctrl_dev *pcdev, unsigned selector, 1666ac73095SBeniamino Galvani const char * const **groups, 1676ac73095SBeniamino Galvani unsigned * const num_groups) 1686ac73095SBeniamino Galvani { 1696ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 1706ac73095SBeniamino Galvani 1716ac73095SBeniamino Galvani *groups = pc->data->funcs[selector].groups; 1726ac73095SBeniamino Galvani *num_groups = pc->data->funcs[selector].num_groups; 1736ac73095SBeniamino Galvani 1746ac73095SBeniamino Galvani return 0; 1756ac73095SBeniamino Galvani } 1766ac73095SBeniamino Galvani 1779959d9a7SGuillaume La Roque static int meson_pinconf_disable_bias(struct meson_pinctrl *pc, 1789959d9a7SGuillaume La Roque unsigned int pin) 1796ac73095SBeniamino Galvani { 1806ac73095SBeniamino Galvani struct meson_bank *bank; 1819959d9a7SGuillaume La Roque unsigned int reg, bit = 0; 1829959d9a7SGuillaume La Roque int ret; 1836ac73095SBeniamino Galvani 184db80f0e1SBeniamino Galvani ret = meson_get_bank(pc, pin, &bank); 1856ac73095SBeniamino Galvani if (ret) 1866ac73095SBeniamino Galvani return ret; 1876ac73095SBeniamino Galvani 1889959d9a7SGuillaume La Roque meson_calc_reg_and_bit(bank, pin, REG_PULLEN, ®, &bit); 1899959d9a7SGuillaume La Roque ret = regmap_update_bits(pc->reg_pullen, reg, BIT(bit), 0); 1909959d9a7SGuillaume La Roque if (ret) 1919959d9a7SGuillaume La Roque return ret; 1929959d9a7SGuillaume La Roque 1939959d9a7SGuillaume La Roque return 0; 1949959d9a7SGuillaume La Roque } 1959959d9a7SGuillaume La Roque 1969959d9a7SGuillaume La Roque static int meson_pinconf_enable_bias(struct meson_pinctrl *pc, unsigned int pin, 1979959d9a7SGuillaume La Roque bool pull_up) 1989959d9a7SGuillaume La Roque { 1999959d9a7SGuillaume La Roque struct meson_bank *bank; 2009959d9a7SGuillaume La Roque unsigned int reg, bit, val = 0; 2019959d9a7SGuillaume La Roque int ret; 2029959d9a7SGuillaume La Roque 2039959d9a7SGuillaume La Roque ret = meson_get_bank(pc, pin, &bank); 2049959d9a7SGuillaume La Roque if (ret) 2059959d9a7SGuillaume La Roque return ret; 2069959d9a7SGuillaume La Roque 2079959d9a7SGuillaume La Roque meson_calc_reg_and_bit(bank, pin, REG_PULL, ®, &bit); 2089959d9a7SGuillaume La Roque if (pull_up) 2099959d9a7SGuillaume La Roque val = BIT(bit); 2109959d9a7SGuillaume La Roque 2119959d9a7SGuillaume La Roque ret = regmap_update_bits(pc->reg_pull, reg, BIT(bit), val); 2129959d9a7SGuillaume La Roque if (ret) 2139959d9a7SGuillaume La Roque return ret; 2149959d9a7SGuillaume La Roque 2159959d9a7SGuillaume La Roque meson_calc_reg_and_bit(bank, pin, REG_PULLEN, ®, &bit); 2169959d9a7SGuillaume La Roque ret = regmap_update_bits(pc->reg_pullen, reg, BIT(bit), BIT(bit)); 2179959d9a7SGuillaume La Roque if (ret) 2189959d9a7SGuillaume La Roque return ret; 2199959d9a7SGuillaume La Roque 2209959d9a7SGuillaume La Roque return 0; 2219959d9a7SGuillaume La Roque } 2229959d9a7SGuillaume La Roque 223*6ea3e3bbSGuillaume La Roque static int meson_pinconf_set_drive_strength(struct meson_pinctrl *pc, 224*6ea3e3bbSGuillaume La Roque unsigned int pin, 225*6ea3e3bbSGuillaume La Roque u16 drive_strength_ua) 226*6ea3e3bbSGuillaume La Roque { 227*6ea3e3bbSGuillaume La Roque struct meson_bank *bank; 228*6ea3e3bbSGuillaume La Roque unsigned int reg, bit, ds_val; 229*6ea3e3bbSGuillaume La Roque int ret; 230*6ea3e3bbSGuillaume La Roque 231*6ea3e3bbSGuillaume La Roque if (!pc->reg_ds) { 232*6ea3e3bbSGuillaume La Roque dev_err(pc->dev, "drive-strength not supported\n"); 233*6ea3e3bbSGuillaume La Roque return -ENOTSUPP; 234*6ea3e3bbSGuillaume La Roque } 235*6ea3e3bbSGuillaume La Roque 236*6ea3e3bbSGuillaume La Roque ret = meson_get_bank(pc, pin, &bank); 237*6ea3e3bbSGuillaume La Roque if (ret) 238*6ea3e3bbSGuillaume La Roque return ret; 239*6ea3e3bbSGuillaume La Roque 240*6ea3e3bbSGuillaume La Roque meson_calc_reg_and_bit(bank, pin, REG_DS, ®, &bit); 241*6ea3e3bbSGuillaume La Roque bit = bit << 1; 242*6ea3e3bbSGuillaume La Roque 243*6ea3e3bbSGuillaume La Roque if (drive_strength_ua <= 500) { 244*6ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_500UA; 245*6ea3e3bbSGuillaume La Roque } else if (drive_strength_ua <= 2500) { 246*6ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_2500UA; 247*6ea3e3bbSGuillaume La Roque } else if (drive_strength_ua <= 3000) { 248*6ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_3000UA; 249*6ea3e3bbSGuillaume La Roque } else if (drive_strength_ua <= 4000) { 250*6ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_4000UA; 251*6ea3e3bbSGuillaume La Roque } else { 252*6ea3e3bbSGuillaume La Roque dev_warn_once(pc->dev, 253*6ea3e3bbSGuillaume La Roque "pin %u: invalid drive-strength : %d , default to 4mA\n", 254*6ea3e3bbSGuillaume La Roque pin, drive_strength_ua); 255*6ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_4000UA; 256*6ea3e3bbSGuillaume La Roque } 257*6ea3e3bbSGuillaume La Roque 258*6ea3e3bbSGuillaume La Roque ret = regmap_update_bits(pc->reg_ds, reg, 0x3 << bit, ds_val << bit); 259*6ea3e3bbSGuillaume La Roque if (ret) 260*6ea3e3bbSGuillaume La Roque return ret; 261*6ea3e3bbSGuillaume La Roque 262*6ea3e3bbSGuillaume La Roque return 0; 263*6ea3e3bbSGuillaume La Roque } 264*6ea3e3bbSGuillaume La Roque 2659959d9a7SGuillaume La Roque static int meson_pinconf_set(struct pinctrl_dev *pcdev, unsigned int pin, 2669959d9a7SGuillaume La Roque unsigned long *configs, unsigned num_configs) 2679959d9a7SGuillaume La Roque { 2689959d9a7SGuillaume La Roque struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 2699959d9a7SGuillaume La Roque enum pin_config_param param; 270*6ea3e3bbSGuillaume La Roque unsigned int drive_strength_ua; 2719959d9a7SGuillaume La Roque int i, ret; 2729959d9a7SGuillaume La Roque 2736ac73095SBeniamino Galvani for (i = 0; i < num_configs; i++) { 2746ac73095SBeniamino Galvani param = pinconf_to_config_param(configs[i]); 2756ac73095SBeniamino Galvani 2766ac73095SBeniamino Galvani switch (param) { 2776ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_DISABLE: 2789959d9a7SGuillaume La Roque ret = meson_pinconf_disable_bias(pc, pin); 2796ac73095SBeniamino Galvani if (ret) 2806ac73095SBeniamino Galvani return ret; 2816ac73095SBeniamino Galvani break; 2826ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_PULL_UP: 2839959d9a7SGuillaume La Roque ret = meson_pinconf_enable_bias(pc, pin, true); 2846ac73095SBeniamino Galvani if (ret) 2856ac73095SBeniamino Galvani return ret; 2866ac73095SBeniamino Galvani break; 2876ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_PULL_DOWN: 2889959d9a7SGuillaume La Roque ret = meson_pinconf_enable_bias(pc, pin, false); 2896ac73095SBeniamino Galvani if (ret) 2906ac73095SBeniamino Galvani return ret; 2916ac73095SBeniamino Galvani break; 292*6ea3e3bbSGuillaume La Roque case PIN_CONFIG_DRIVE_STRENGTH_UA: 293*6ea3e3bbSGuillaume La Roque drive_strength_ua = 294*6ea3e3bbSGuillaume La Roque pinconf_to_config_argument(configs[i]); 295*6ea3e3bbSGuillaume La Roque ret = meson_pinconf_set_drive_strength 296*6ea3e3bbSGuillaume La Roque (pc, pin, drive_strength_ua); 297*6ea3e3bbSGuillaume La Roque if (ret) 298*6ea3e3bbSGuillaume La Roque return ret; 299*6ea3e3bbSGuillaume La Roque break; 3006ac73095SBeniamino Galvani default: 3016ac73095SBeniamino Galvani return -ENOTSUPP; 3026ac73095SBeniamino Galvani } 3036ac73095SBeniamino Galvani } 3046ac73095SBeniamino Galvani 3056ac73095SBeniamino Galvani return 0; 3066ac73095SBeniamino Galvani } 3076ac73095SBeniamino Galvani 3086ac73095SBeniamino Galvani static int meson_pinconf_get_pull(struct meson_pinctrl *pc, unsigned int pin) 3096ac73095SBeniamino Galvani { 3106ac73095SBeniamino Galvani struct meson_bank *bank; 3116ac73095SBeniamino Galvani unsigned int reg, bit, val; 3126ac73095SBeniamino Galvani int ret, conf; 3136ac73095SBeniamino Galvani 314db80f0e1SBeniamino Galvani ret = meson_get_bank(pc, pin, &bank); 3156ac73095SBeniamino Galvani if (ret) 3166ac73095SBeniamino Galvani return ret; 3176ac73095SBeniamino Galvani 3186ac73095SBeniamino Galvani meson_calc_reg_and_bit(bank, pin, REG_PULLEN, ®, &bit); 3196ac73095SBeniamino Galvani 320db80f0e1SBeniamino Galvani ret = regmap_read(pc->reg_pullen, reg, &val); 3216ac73095SBeniamino Galvani if (ret) 3226ac73095SBeniamino Galvani return ret; 3236ac73095SBeniamino Galvani 3246ac73095SBeniamino Galvani if (!(val & BIT(bit))) { 3256ac73095SBeniamino Galvani conf = PIN_CONFIG_BIAS_DISABLE; 3266ac73095SBeniamino Galvani } else { 3276ac73095SBeniamino Galvani meson_calc_reg_and_bit(bank, pin, REG_PULL, ®, &bit); 3286ac73095SBeniamino Galvani 329db80f0e1SBeniamino Galvani ret = regmap_read(pc->reg_pull, reg, &val); 3306ac73095SBeniamino Galvani if (ret) 3316ac73095SBeniamino Galvani return ret; 3326ac73095SBeniamino Galvani 3336ac73095SBeniamino Galvani if (val & BIT(bit)) 3346ac73095SBeniamino Galvani conf = PIN_CONFIG_BIAS_PULL_UP; 3356ac73095SBeniamino Galvani else 3366ac73095SBeniamino Galvani conf = PIN_CONFIG_BIAS_PULL_DOWN; 3376ac73095SBeniamino Galvani } 3386ac73095SBeniamino Galvani 3396ac73095SBeniamino Galvani return conf; 3406ac73095SBeniamino Galvani } 3416ac73095SBeniamino Galvani 342*6ea3e3bbSGuillaume La Roque static int meson_pinconf_get_drive_strength(struct meson_pinctrl *pc, 343*6ea3e3bbSGuillaume La Roque unsigned int pin, 344*6ea3e3bbSGuillaume La Roque u16 *drive_strength_ua) 345*6ea3e3bbSGuillaume La Roque { 346*6ea3e3bbSGuillaume La Roque struct meson_bank *bank; 347*6ea3e3bbSGuillaume La Roque unsigned int reg, bit; 348*6ea3e3bbSGuillaume La Roque unsigned int val; 349*6ea3e3bbSGuillaume La Roque int ret; 350*6ea3e3bbSGuillaume La Roque 351*6ea3e3bbSGuillaume La Roque if (!pc->reg_ds) 352*6ea3e3bbSGuillaume La Roque return -ENOTSUPP; 353*6ea3e3bbSGuillaume La Roque 354*6ea3e3bbSGuillaume La Roque ret = meson_get_bank(pc, pin, &bank); 355*6ea3e3bbSGuillaume La Roque if (ret) 356*6ea3e3bbSGuillaume La Roque return ret; 357*6ea3e3bbSGuillaume La Roque 358*6ea3e3bbSGuillaume La Roque meson_calc_reg_and_bit(bank, pin, REG_DS, ®, &bit); 359*6ea3e3bbSGuillaume La Roque 360*6ea3e3bbSGuillaume La Roque ret = regmap_read(pc->reg_ds, reg, &val); 361*6ea3e3bbSGuillaume La Roque if (ret) 362*6ea3e3bbSGuillaume La Roque return ret; 363*6ea3e3bbSGuillaume La Roque 364*6ea3e3bbSGuillaume La Roque switch ((val >> bit) & 0x3) { 365*6ea3e3bbSGuillaume La Roque case MESON_PINCONF_DRV_500UA: 366*6ea3e3bbSGuillaume La Roque *drive_strength_ua = 500; 367*6ea3e3bbSGuillaume La Roque break; 368*6ea3e3bbSGuillaume La Roque case MESON_PINCONF_DRV_2500UA: 369*6ea3e3bbSGuillaume La Roque *drive_strength_ua = 2500; 370*6ea3e3bbSGuillaume La Roque break; 371*6ea3e3bbSGuillaume La Roque case MESON_PINCONF_DRV_3000UA: 372*6ea3e3bbSGuillaume La Roque *drive_strength_ua = 3000; 373*6ea3e3bbSGuillaume La Roque break; 374*6ea3e3bbSGuillaume La Roque case MESON_PINCONF_DRV_4000UA: 375*6ea3e3bbSGuillaume La Roque *drive_strength_ua = 4000; 376*6ea3e3bbSGuillaume La Roque break; 377*6ea3e3bbSGuillaume La Roque default: 378*6ea3e3bbSGuillaume La Roque return -EINVAL; 379*6ea3e3bbSGuillaume La Roque } 380*6ea3e3bbSGuillaume La Roque 381*6ea3e3bbSGuillaume La Roque return 0; 382*6ea3e3bbSGuillaume La Roque } 383*6ea3e3bbSGuillaume La Roque 3846ac73095SBeniamino Galvani static int meson_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin, 3856ac73095SBeniamino Galvani unsigned long *config) 3866ac73095SBeniamino Galvani { 3876ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 3886ac73095SBeniamino Galvani enum pin_config_param param = pinconf_to_config_param(*config); 3896ac73095SBeniamino Galvani u16 arg; 390*6ea3e3bbSGuillaume La Roque int ret; 3916ac73095SBeniamino Galvani 3926ac73095SBeniamino Galvani switch (param) { 3936ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_DISABLE: 3946ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_PULL_DOWN: 3956ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_PULL_UP: 3966ac73095SBeniamino Galvani if (meson_pinconf_get_pull(pc, pin) == param) 3976ac73095SBeniamino Galvani arg = 1; 3986ac73095SBeniamino Galvani else 3996ac73095SBeniamino Galvani return -EINVAL; 4006ac73095SBeniamino Galvani break; 401*6ea3e3bbSGuillaume La Roque case PIN_CONFIG_DRIVE_STRENGTH_UA: 402*6ea3e3bbSGuillaume La Roque ret = meson_pinconf_get_drive_strength(pc, pin, &arg); 403*6ea3e3bbSGuillaume La Roque if (ret) 404*6ea3e3bbSGuillaume La Roque return ret; 405*6ea3e3bbSGuillaume La Roque break; 4066ac73095SBeniamino Galvani default: 4076ac73095SBeniamino Galvani return -ENOTSUPP; 4086ac73095SBeniamino Galvani } 4096ac73095SBeniamino Galvani 4106ac73095SBeniamino Galvani *config = pinconf_to_config_packed(param, arg); 4116ac73095SBeniamino Galvani dev_dbg(pc->dev, "pinconf for pin %u is %lu\n", pin, *config); 4126ac73095SBeniamino Galvani 4136ac73095SBeniamino Galvani return 0; 4146ac73095SBeniamino Galvani } 4156ac73095SBeniamino Galvani 4166ac73095SBeniamino Galvani static int meson_pinconf_group_set(struct pinctrl_dev *pcdev, 4176ac73095SBeniamino Galvani unsigned int num_group, 4186ac73095SBeniamino Galvani unsigned long *configs, unsigned num_configs) 4196ac73095SBeniamino Galvani { 4206ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev); 4216ac73095SBeniamino Galvani struct meson_pmx_group *group = &pc->data->groups[num_group]; 4226ac73095SBeniamino Galvani int i; 4236ac73095SBeniamino Galvani 4246ac73095SBeniamino Galvani dev_dbg(pc->dev, "set pinconf for group %s\n", group->name); 4256ac73095SBeniamino Galvani 4266ac73095SBeniamino Galvani for (i = 0; i < group->num_pins; i++) { 4276ac73095SBeniamino Galvani meson_pinconf_set(pcdev, group->pins[i], configs, 4286ac73095SBeniamino Galvani num_configs); 4296ac73095SBeniamino Galvani } 4306ac73095SBeniamino Galvani 4316ac73095SBeniamino Galvani return 0; 4326ac73095SBeniamino Galvani } 4336ac73095SBeniamino Galvani 4346ac73095SBeniamino Galvani static int meson_pinconf_group_get(struct pinctrl_dev *pcdev, 4356ac73095SBeniamino Galvani unsigned int group, unsigned long *config) 4366ac73095SBeniamino Galvani { 4371ffbf50bSJerome Brunet return -ENOTSUPP; 4386ac73095SBeniamino Galvani } 4396ac73095SBeniamino Galvani 4406ac73095SBeniamino Galvani static const struct pinconf_ops meson_pinconf_ops = { 4416ac73095SBeniamino Galvani .pin_config_get = meson_pinconf_get, 4426ac73095SBeniamino Galvani .pin_config_set = meson_pinconf_set, 4436ac73095SBeniamino Galvani .pin_config_group_get = meson_pinconf_group_get, 4446ac73095SBeniamino Galvani .pin_config_group_set = meson_pinconf_group_set, 4456ac73095SBeniamino Galvani .is_generic = true, 4466ac73095SBeniamino Galvani }; 4476ac73095SBeniamino Galvani 4486ac73095SBeniamino Galvani static int meson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) 4496ac73095SBeniamino Galvani { 450db80f0e1SBeniamino Galvani struct meson_pinctrl *pc = gpiochip_get_data(chip); 45170e5ecb1SJerome Brunet unsigned int reg, bit; 4526ac73095SBeniamino Galvani struct meson_bank *bank; 4536ac73095SBeniamino Galvani int ret; 4546ac73095SBeniamino Galvani 45570e5ecb1SJerome Brunet ret = meson_get_bank(pc, gpio, &bank); 4566ac73095SBeniamino Galvani if (ret) 4576ac73095SBeniamino Galvani return ret; 4586ac73095SBeniamino Galvani 45970e5ecb1SJerome Brunet meson_calc_reg_and_bit(bank, gpio, REG_DIR, ®, &bit); 4606ac73095SBeniamino Galvani 461db80f0e1SBeniamino Galvani return regmap_update_bits(pc->reg_gpio, reg, BIT(bit), BIT(bit)); 4626ac73095SBeniamino Galvani } 4636ac73095SBeniamino Galvani 4646ac73095SBeniamino Galvani static int meson_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, 4656ac73095SBeniamino Galvani int value) 4666ac73095SBeniamino Galvani { 467db80f0e1SBeniamino Galvani struct meson_pinctrl *pc = gpiochip_get_data(chip); 46870e5ecb1SJerome Brunet unsigned int reg, bit; 4696ac73095SBeniamino Galvani struct meson_bank *bank; 4706ac73095SBeniamino Galvani int ret; 4716ac73095SBeniamino Galvani 47270e5ecb1SJerome Brunet ret = meson_get_bank(pc, gpio, &bank); 4736ac73095SBeniamino Galvani if (ret) 4746ac73095SBeniamino Galvani return ret; 4756ac73095SBeniamino Galvani 47670e5ecb1SJerome Brunet meson_calc_reg_and_bit(bank, gpio, REG_DIR, ®, &bit); 477db80f0e1SBeniamino Galvani ret = regmap_update_bits(pc->reg_gpio, reg, BIT(bit), 0); 4786ac73095SBeniamino Galvani if (ret) 4796ac73095SBeniamino Galvani return ret; 4806ac73095SBeniamino Galvani 48170e5ecb1SJerome Brunet meson_calc_reg_and_bit(bank, gpio, REG_OUT, ®, &bit); 482db80f0e1SBeniamino Galvani return regmap_update_bits(pc->reg_gpio, reg, BIT(bit), 4836ac73095SBeniamino Galvani value ? BIT(bit) : 0); 4846ac73095SBeniamino Galvani } 4856ac73095SBeniamino Galvani 4866ac73095SBeniamino Galvani static void meson_gpio_set(struct gpio_chip *chip, unsigned gpio, int value) 4876ac73095SBeniamino Galvani { 488db80f0e1SBeniamino Galvani struct meson_pinctrl *pc = gpiochip_get_data(chip); 48970e5ecb1SJerome Brunet unsigned int reg, bit; 4906ac73095SBeniamino Galvani struct meson_bank *bank; 4916ac73095SBeniamino Galvani int ret; 4926ac73095SBeniamino Galvani 49370e5ecb1SJerome Brunet ret = meson_get_bank(pc, gpio, &bank); 4946ac73095SBeniamino Galvani if (ret) 4956ac73095SBeniamino Galvani return; 4966ac73095SBeniamino Galvani 49770e5ecb1SJerome Brunet meson_calc_reg_and_bit(bank, gpio, REG_OUT, ®, &bit); 498db80f0e1SBeniamino Galvani regmap_update_bits(pc->reg_gpio, reg, BIT(bit), 4996ac73095SBeniamino Galvani value ? BIT(bit) : 0); 5006ac73095SBeniamino Galvani } 5016ac73095SBeniamino Galvani 5026ac73095SBeniamino Galvani static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio) 5036ac73095SBeniamino Galvani { 504db80f0e1SBeniamino Galvani struct meson_pinctrl *pc = gpiochip_get_data(chip); 50570e5ecb1SJerome Brunet unsigned int reg, bit, val; 5066ac73095SBeniamino Galvani struct meson_bank *bank; 5076ac73095SBeniamino Galvani int ret; 5086ac73095SBeniamino Galvani 50970e5ecb1SJerome Brunet ret = meson_get_bank(pc, gpio, &bank); 5106ac73095SBeniamino Galvani if (ret) 5116ac73095SBeniamino Galvani return ret; 5126ac73095SBeniamino Galvani 51370e5ecb1SJerome Brunet meson_calc_reg_and_bit(bank, gpio, REG_IN, ®, &bit); 514db80f0e1SBeniamino Galvani regmap_read(pc->reg_gpio, reg, &val); 5156ac73095SBeniamino Galvani 5166ac73095SBeniamino Galvani return !!(val & BIT(bit)); 5176ac73095SBeniamino Galvani } 5186ac73095SBeniamino Galvani 5196ac73095SBeniamino Galvani static int meson_gpiolib_register(struct meson_pinctrl *pc) 5206ac73095SBeniamino Galvani { 5219dab1868SCarlo Caione int ret; 5226ac73095SBeniamino Galvani 523db80f0e1SBeniamino Galvani pc->chip.label = pc->data->name; 524db80f0e1SBeniamino Galvani pc->chip.parent = pc->dev; 525634e40b0SJerome Brunet pc->chip.request = gpiochip_generic_request; 526634e40b0SJerome Brunet pc->chip.free = gpiochip_generic_free; 527db80f0e1SBeniamino Galvani pc->chip.direction_input = meson_gpio_direction_input; 528db80f0e1SBeniamino Galvani pc->chip.direction_output = meson_gpio_direction_output; 529db80f0e1SBeniamino Galvani pc->chip.get = meson_gpio_get; 530db80f0e1SBeniamino Galvani pc->chip.set = meson_gpio_set; 531634e40b0SJerome Brunet pc->chip.base = -1; 532db80f0e1SBeniamino Galvani pc->chip.ngpio = pc->data->num_pins; 533db80f0e1SBeniamino Galvani pc->chip.can_sleep = false; 534db80f0e1SBeniamino Galvani pc->chip.of_node = pc->of_node; 535db80f0e1SBeniamino Galvani pc->chip.of_gpio_n_cells = 2; 5366ac73095SBeniamino Galvani 537db80f0e1SBeniamino Galvani ret = gpiochip_add_data(&pc->chip, pc); 5386ac73095SBeniamino Galvani if (ret) { 5396ac73095SBeniamino Galvani dev_err(pc->dev, "can't add gpio chip %s\n", 540db80f0e1SBeniamino Galvani pc->data->name); 541c7fc5fbaSNeil Armstrong return ret; 5426ac73095SBeniamino Galvani } 5436ac73095SBeniamino Galvani 5446ac73095SBeniamino Galvani return 0; 5456ac73095SBeniamino Galvani } 5466ac73095SBeniamino Galvani 5476ac73095SBeniamino Galvani static struct regmap_config meson_regmap_config = { 5486ac73095SBeniamino Galvani .reg_bits = 32, 5496ac73095SBeniamino Galvani .val_bits = 32, 5506ac73095SBeniamino Galvani .reg_stride = 4, 5516ac73095SBeniamino Galvani }; 5526ac73095SBeniamino Galvani 5536ac73095SBeniamino Galvani static struct regmap *meson_map_resource(struct meson_pinctrl *pc, 5546ac73095SBeniamino Galvani struct device_node *node, char *name) 5556ac73095SBeniamino Galvani { 5566ac73095SBeniamino Galvani struct resource res; 5576ac73095SBeniamino Galvani void __iomem *base; 5586ac73095SBeniamino Galvani int i; 5596ac73095SBeniamino Galvani 5606ac73095SBeniamino Galvani i = of_property_match_string(node, "reg-names", name); 5616ac73095SBeniamino Galvani if (of_address_to_resource(node, i, &res)) 5626ac73095SBeniamino Galvani return ERR_PTR(-ENOENT); 5636ac73095SBeniamino Galvani 5646ac73095SBeniamino Galvani base = devm_ioremap_resource(pc->dev, &res); 5656ac73095SBeniamino Galvani if (IS_ERR(base)) 5666ac73095SBeniamino Galvani return ERR_CAST(base); 5676ac73095SBeniamino Galvani 5686ac73095SBeniamino Galvani meson_regmap_config.max_register = resource_size(&res) - 4; 5696ac73095SBeniamino Galvani meson_regmap_config.name = devm_kasprintf(pc->dev, GFP_KERNEL, 57094f4e54cSRob Herring "%pOFn-%s", node, 5716ac73095SBeniamino Galvani name); 5726ac73095SBeniamino Galvani if (!meson_regmap_config.name) 5736ac73095SBeniamino Galvani return ERR_PTR(-ENOMEM); 5746ac73095SBeniamino Galvani 5756ac73095SBeniamino Galvani return devm_regmap_init_mmio(pc->dev, base, &meson_regmap_config); 5766ac73095SBeniamino Galvani } 5776ac73095SBeniamino Galvani 5786ac73095SBeniamino Galvani static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc, 5796ac73095SBeniamino Galvani struct device_node *node) 5806ac73095SBeniamino Galvani { 581db80f0e1SBeniamino Galvani struct device_node *np, *gpio_np = NULL; 5826ac73095SBeniamino Galvani 5836ac73095SBeniamino Galvani for_each_child_of_node(node, np) { 5846ac73095SBeniamino Galvani if (!of_find_property(np, "gpio-controller", NULL)) 5856ac73095SBeniamino Galvani continue; 586db80f0e1SBeniamino Galvani if (gpio_np) { 587db80f0e1SBeniamino Galvani dev_err(pc->dev, "multiple gpio nodes\n"); 588db80f0e1SBeniamino Galvani return -EINVAL; 589db80f0e1SBeniamino Galvani } 590db80f0e1SBeniamino Galvani gpio_np = np; 5916ac73095SBeniamino Galvani } 5926ac73095SBeniamino Galvani 593db80f0e1SBeniamino Galvani if (!gpio_np) { 594db80f0e1SBeniamino Galvani dev_err(pc->dev, "no gpio node found\n"); 5956ac73095SBeniamino Galvani return -EINVAL; 5966ac73095SBeniamino Galvani } 5976ac73095SBeniamino Galvani 598db80f0e1SBeniamino Galvani pc->of_node = gpio_np; 5996ac73095SBeniamino Galvani 600db80f0e1SBeniamino Galvani pc->reg_mux = meson_map_resource(pc, gpio_np, "mux"); 601db80f0e1SBeniamino Galvani if (IS_ERR(pc->reg_mux)) { 6026ac73095SBeniamino Galvani dev_err(pc->dev, "mux registers not found\n"); 603db80f0e1SBeniamino Galvani return PTR_ERR(pc->reg_mux); 6046ac73095SBeniamino Galvani } 6056ac73095SBeniamino Galvani 606db80f0e1SBeniamino Galvani pc->reg_gpio = meson_map_resource(pc, gpio_np, "gpio"); 607db80f0e1SBeniamino Galvani if (IS_ERR(pc->reg_gpio)) { 6086ac73095SBeniamino Galvani dev_err(pc->dev, "gpio registers not found\n"); 609db80f0e1SBeniamino Galvani return PTR_ERR(pc->reg_gpio); 6106ac73095SBeniamino Galvani } 6116ac73095SBeniamino Galvani 612e66dd48eSXingyu Chen pc->reg_pull = meson_map_resource(pc, gpio_np, "pull"); 613e66dd48eSXingyu Chen /* Use gpio region if pull one is not present */ 614e66dd48eSXingyu Chen if (IS_ERR(pc->reg_pull)) 615e66dd48eSXingyu Chen pc->reg_pull = pc->reg_gpio; 616e66dd48eSXingyu Chen 617e66dd48eSXingyu Chen pc->reg_pullen = meson_map_resource(pc, gpio_np, "pull-enable"); 618e66dd48eSXingyu Chen /* Use pull region if pull-enable one is not present */ 619e66dd48eSXingyu Chen if (IS_ERR(pc->reg_pullen)) 620e66dd48eSXingyu Chen pc->reg_pullen = pc->reg_pull; 621e66dd48eSXingyu Chen 62264856974SJerome Brunet pc->reg_ds = meson_map_resource(pc, gpio_np, "ds"); 62364856974SJerome Brunet if (IS_ERR(pc->reg_ds)) { 62464856974SJerome Brunet dev_dbg(pc->dev, "ds registers not found - skipping\n"); 62564856974SJerome Brunet pc->reg_ds = NULL; 62664856974SJerome Brunet } 62764856974SJerome Brunet 6286ac73095SBeniamino Galvani return 0; 6296ac73095SBeniamino Galvani } 6306ac73095SBeniamino Galvani 631277d14ebSJerome Brunet int meson_pinctrl_probe(struct platform_device *pdev) 6326ac73095SBeniamino Galvani { 6336ac73095SBeniamino Galvani struct device *dev = &pdev->dev; 6346ac73095SBeniamino Galvani struct meson_pinctrl *pc; 6356ac73095SBeniamino Galvani int ret; 6366ac73095SBeniamino Galvani 6376ac73095SBeniamino Galvani pc = devm_kzalloc(dev, sizeof(struct meson_pinctrl), GFP_KERNEL); 6386ac73095SBeniamino Galvani if (!pc) 6396ac73095SBeniamino Galvani return -ENOMEM; 6406ac73095SBeniamino Galvani 6416ac73095SBeniamino Galvani pc->dev = dev; 642277d14ebSJerome Brunet pc->data = (struct meson_pinctrl_data *) of_device_get_match_data(dev); 6436ac73095SBeniamino Galvani 644277d14ebSJerome Brunet ret = meson_pinctrl_parse_dt(pc, dev->of_node); 6456ac73095SBeniamino Galvani if (ret) 6466ac73095SBeniamino Galvani return ret; 6476ac73095SBeniamino Galvani 6486ac73095SBeniamino Galvani pc->desc.name = "pinctrl-meson"; 6496ac73095SBeniamino Galvani pc->desc.owner = THIS_MODULE; 6506ac73095SBeniamino Galvani pc->desc.pctlops = &meson_pctrl_ops; 651ce385aa2SJerome Brunet pc->desc.pmxops = pc->data->pmx_ops; 6526ac73095SBeniamino Galvani pc->desc.confops = &meson_pinconf_ops; 6536ac73095SBeniamino Galvani pc->desc.pins = pc->data->pins; 6546ac73095SBeniamino Galvani pc->desc.npins = pc->data->num_pins; 6556ac73095SBeniamino Galvani 656e649f7ecSLaxman Dewangan pc->pcdev = devm_pinctrl_register(pc->dev, &pc->desc, pc); 657323de9efSMasahiro Yamada if (IS_ERR(pc->pcdev)) { 6586ac73095SBeniamino Galvani dev_err(pc->dev, "can't register pinctrl device"); 659323de9efSMasahiro Yamada return PTR_ERR(pc->pcdev); 6606ac73095SBeniamino Galvani } 6616ac73095SBeniamino Galvani 6625b236d0fSWei Yongjun return meson_gpiolib_register(pc); 6636ac73095SBeniamino Galvani } 664