1a1a503a8SSean Wang // SPDX-License-Identifier: GPL-2.0 2a1a503a8SSean Wang /* 3a1a503a8SSean Wang * Copyright (C) 2018 MediaTek Inc. 4a1a503a8SSean Wang * 5a1a503a8SSean Wang * Author: Sean Wang <sean.wang@mediatek.com> 6a1a503a8SSean Wang * 7a1a503a8SSean Wang */ 8a1a503a8SSean Wang 9a1a503a8SSean Wang #include <linux/device.h> 10a1a503a8SSean Wang #include <linux/err.h> 11a1a503a8SSean Wang #include <linux/gpio.h> 12a1a503a8SSean Wang #include <linux/io.h> 13a1a503a8SSean Wang 14a1a503a8SSean Wang #include "pinctrl-mtk-common-v2.h" 15a1a503a8SSean Wang 16*c2832197SSean Wang /** 17*c2832197SSean Wang * struct mtk_drive_desc - the structure that holds the information 18*c2832197SSean Wang * of the driving current 19*c2832197SSean Wang * @min: the minimum current of this group 20*c2832197SSean Wang * @max: the maximum current of this group 21*c2832197SSean Wang * @step: the step current of this group 22*c2832197SSean Wang * @scal: the weight factor 23*c2832197SSean Wang * 24*c2832197SSean Wang * formula: output = ((input) / step - 1) * scal 25*c2832197SSean Wang */ 26*c2832197SSean Wang struct mtk_drive_desc { 27*c2832197SSean Wang u8 min; 28*c2832197SSean Wang u8 max; 29*c2832197SSean Wang u8 step; 30*c2832197SSean Wang u8 scal; 31*c2832197SSean Wang }; 32*c2832197SSean Wang 33*c2832197SSean Wang /* The groups of drive strength */ 34*c2832197SSean Wang const struct mtk_drive_desc mtk_drive[] = { 35*c2832197SSean Wang [DRV_GRP0] = { 4, 16, 4, 1 }, 36*c2832197SSean Wang [DRV_GRP1] = { 4, 16, 4, 2 }, 37*c2832197SSean Wang [DRV_GRP2] = { 2, 8, 2, 1 }, 38*c2832197SSean Wang [DRV_GRP3] = { 2, 8, 2, 2 }, 39*c2832197SSean Wang [DRV_GRP4] = { 2, 16, 2, 1 }, 40*c2832197SSean Wang }; 41*c2832197SSean Wang 42a1a503a8SSean Wang static void mtk_w32(struct mtk_pinctrl *pctl, u32 reg, u32 val) 43a1a503a8SSean Wang { 44a1a503a8SSean Wang writel_relaxed(val, pctl->base + reg); 45a1a503a8SSean Wang } 46a1a503a8SSean Wang 47a1a503a8SSean Wang static u32 mtk_r32(struct mtk_pinctrl *pctl, u32 reg) 48a1a503a8SSean Wang { 49a1a503a8SSean Wang return readl_relaxed(pctl->base + reg); 50a1a503a8SSean Wang } 51a1a503a8SSean Wang 52a1a503a8SSean Wang void mtk_rmw(struct mtk_pinctrl *pctl, u32 reg, u32 mask, u32 set) 53a1a503a8SSean Wang { 54a1a503a8SSean Wang u32 val; 55a1a503a8SSean Wang 56a1a503a8SSean Wang val = mtk_r32(pctl, reg); 57a1a503a8SSean Wang val &= ~mask; 58a1a503a8SSean Wang val |= set; 59a1a503a8SSean Wang mtk_w32(pctl, reg, val); 60a1a503a8SSean Wang } 61a1a503a8SSean Wang 62a1a503a8SSean Wang static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw, int pin, 63a1a503a8SSean Wang const struct mtk_pin_reg_calc *rc, 64a1a503a8SSean Wang struct mtk_pin_field *pfd) 65a1a503a8SSean Wang { 66a1a503a8SSean Wang const struct mtk_pin_field_calc *c, *e; 67a1a503a8SSean Wang u32 bits; 68a1a503a8SSean Wang 69a1a503a8SSean Wang c = rc->range; 70a1a503a8SSean Wang e = c + rc->nranges; 71a1a503a8SSean Wang 72a1a503a8SSean Wang while (c < e) { 73a1a503a8SSean Wang if (pin >= c->s_pin && pin <= c->e_pin) 74a1a503a8SSean Wang break; 75a1a503a8SSean Wang c++; 76a1a503a8SSean Wang } 77a1a503a8SSean Wang 78a1a503a8SSean Wang if (c >= e) { 79a1a503a8SSean Wang dev_err(hw->dev, "Out of range for pin = %d\n", pin); 80a1a503a8SSean Wang return -EINVAL; 81a1a503a8SSean Wang } 82a1a503a8SSean Wang 83b906faf7SSean Wang /* Calculated bits as the overall offset the pin is located at, 84b906faf7SSean Wang * if c->fixed is held, that determines the all the pins in the 85b906faf7SSean Wang * range use the same field with the s_pin. 86b906faf7SSean Wang */ 87b906faf7SSean Wang bits = c->fixed ? c->s_bit : c->s_bit + (pin - c->s_pin) * (c->x_bits); 88a1a503a8SSean Wang 89b906faf7SSean Wang /* Fill pfd from bits. For example 32-bit register applied is assumed 90b906faf7SSean Wang * when c->sz_reg is equal to 32. 91b906faf7SSean Wang */ 92b906faf7SSean Wang pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg); 93b906faf7SSean Wang pfd->bitpos = bits % c->sz_reg; 94a1a503a8SSean Wang pfd->mask = (1 << c->x_bits) - 1; 95a1a503a8SSean Wang 96a1a503a8SSean Wang /* pfd->next is used for indicating that bit wrapping-around happens 97a1a503a8SSean Wang * which requires the manipulation for bit 0 starting in the next 98a1a503a8SSean Wang * register to form the complete field read/write. 99a1a503a8SSean Wang */ 100b906faf7SSean Wang pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0; 101a1a503a8SSean Wang 102a1a503a8SSean Wang return 0; 103a1a503a8SSean Wang } 104a1a503a8SSean Wang 105a1a503a8SSean Wang static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw, int pin, 106a1a503a8SSean Wang int field, struct mtk_pin_field *pfd) 107a1a503a8SSean Wang { 108a1a503a8SSean Wang const struct mtk_pin_reg_calc *rc; 109a1a503a8SSean Wang 110a1a503a8SSean Wang if (field < 0 || field >= PINCTRL_PIN_REG_MAX) { 111a1a503a8SSean Wang dev_err(hw->dev, "Invalid Field %d\n", field); 112a1a503a8SSean Wang return -EINVAL; 113a1a503a8SSean Wang } 114a1a503a8SSean Wang 115a1a503a8SSean Wang if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) { 116a1a503a8SSean Wang rc = &hw->soc->reg_cal[field]; 117a1a503a8SSean Wang } else { 118a1a503a8SSean Wang dev_err(hw->dev, "Undefined range for field %d\n", field); 119a1a503a8SSean Wang return -EINVAL; 120a1a503a8SSean Wang } 121a1a503a8SSean Wang 122a1a503a8SSean Wang return mtk_hw_pin_field_lookup(hw, pin, rc, pfd); 123a1a503a8SSean Wang } 124a1a503a8SSean Wang 125a1a503a8SSean Wang static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l) 126a1a503a8SSean Wang { 127a1a503a8SSean Wang *l = 32 - pf->bitpos; 128a1a503a8SSean Wang *h = get_count_order(pf->mask) - *l; 129a1a503a8SSean Wang } 130a1a503a8SSean Wang 131a1a503a8SSean Wang static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw, 132a1a503a8SSean Wang struct mtk_pin_field *pf, int value) 133a1a503a8SSean Wang { 134a1a503a8SSean Wang int nbits_l, nbits_h; 135a1a503a8SSean Wang 136a1a503a8SSean Wang mtk_hw_bits_part(pf, &nbits_h, &nbits_l); 137a1a503a8SSean Wang 138a1a503a8SSean Wang mtk_rmw(hw, pf->offset, pf->mask << pf->bitpos, 139a1a503a8SSean Wang (value & pf->mask) << pf->bitpos); 140a1a503a8SSean Wang 141a1a503a8SSean Wang mtk_rmw(hw, pf->offset + pf->next, BIT(nbits_h) - 1, 142a1a503a8SSean Wang (value & pf->mask) >> nbits_l); 143a1a503a8SSean Wang } 144a1a503a8SSean Wang 145a1a503a8SSean Wang static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw, 146a1a503a8SSean Wang struct mtk_pin_field *pf, int *value) 147a1a503a8SSean Wang { 148a1a503a8SSean Wang int nbits_l, nbits_h, h, l; 149a1a503a8SSean Wang 150a1a503a8SSean Wang mtk_hw_bits_part(pf, &nbits_h, &nbits_l); 151a1a503a8SSean Wang 152a1a503a8SSean Wang l = (mtk_r32(hw, pf->offset) >> pf->bitpos) & (BIT(nbits_l) - 1); 153a1a503a8SSean Wang h = (mtk_r32(hw, pf->offset + pf->next)) & (BIT(nbits_h) - 1); 154a1a503a8SSean Wang 155a1a503a8SSean Wang *value = (h << nbits_l) | l; 156a1a503a8SSean Wang } 157a1a503a8SSean Wang 158a1a503a8SSean Wang int mtk_hw_set_value(struct mtk_pinctrl *hw, int pin, int field, int value) 159a1a503a8SSean Wang { 160a1a503a8SSean Wang struct mtk_pin_field pf; 161a1a503a8SSean Wang int err; 162a1a503a8SSean Wang 163a1a503a8SSean Wang err = mtk_hw_pin_field_get(hw, pin, field, &pf); 164a1a503a8SSean Wang if (err) 165a1a503a8SSean Wang return err; 166a1a503a8SSean Wang 167a1a503a8SSean Wang if (!pf.next) 168a1a503a8SSean Wang mtk_rmw(hw, pf.offset, pf.mask << pf.bitpos, 169a1a503a8SSean Wang (value & pf.mask) << pf.bitpos); 170a1a503a8SSean Wang else 171a1a503a8SSean Wang mtk_hw_write_cross_field(hw, &pf, value); 172a1a503a8SSean Wang 173a1a503a8SSean Wang return 0; 174a1a503a8SSean Wang } 175a1a503a8SSean Wang 176a1a503a8SSean Wang int mtk_hw_get_value(struct mtk_pinctrl *hw, int pin, int field, int *value) 177a1a503a8SSean Wang { 178a1a503a8SSean Wang struct mtk_pin_field pf; 179a1a503a8SSean Wang int err; 180a1a503a8SSean Wang 181a1a503a8SSean Wang err = mtk_hw_pin_field_get(hw, pin, field, &pf); 182a1a503a8SSean Wang if (err) 183a1a503a8SSean Wang return err; 184a1a503a8SSean Wang 185a1a503a8SSean Wang if (!pf.next) 186a1a503a8SSean Wang *value = (mtk_r32(hw, pf.offset) >> pf.bitpos) & pf.mask; 187a1a503a8SSean Wang else 188a1a503a8SSean Wang mtk_hw_read_cross_field(hw, &pf, value); 189a1a503a8SSean Wang 190a1a503a8SSean Wang return 0; 191a1a503a8SSean Wang } 192*c2832197SSean Wang 193*c2832197SSean Wang /* Revision 0 */ 194*c2832197SSean Wang int mtk_pinconf_drive_set(struct mtk_pinctrl *hw, 195*c2832197SSean Wang const struct mtk_pin_desc *desc, u32 arg) 196*c2832197SSean Wang { 197*c2832197SSean Wang const struct mtk_drive_desc *tb; 198*c2832197SSean Wang int err = -ENOTSUPP; 199*c2832197SSean Wang 200*c2832197SSean Wang tb = &mtk_drive[desc->drv_n]; 201*c2832197SSean Wang /* 4mA when (e8, e4) = (0, 0) 202*c2832197SSean Wang * 8mA when (e8, e4) = (0, 1) 203*c2832197SSean Wang * 12mA when (e8, e4) = (1, 0) 204*c2832197SSean Wang * 16mA when (e8, e4) = (1, 1) 205*c2832197SSean Wang */ 206*c2832197SSean Wang if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) { 207*c2832197SSean Wang arg = (arg / tb->step - 1) * tb->scal; 208*c2832197SSean Wang err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_E4, 209*c2832197SSean Wang arg & 0x1); 210*c2832197SSean Wang if (err) 211*c2832197SSean Wang return err; 212*c2832197SSean Wang 213*c2832197SSean Wang err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_E8, 214*c2832197SSean Wang (arg & 0x2) >> 1); 215*c2832197SSean Wang if (err) 216*c2832197SSean Wang return err; 217*c2832197SSean Wang } 218*c2832197SSean Wang 219*c2832197SSean Wang return err; 220*c2832197SSean Wang } 221*c2832197SSean Wang 222*c2832197SSean Wang int mtk_pinconf_drive_get(struct mtk_pinctrl *hw, 223*c2832197SSean Wang const struct mtk_pin_desc *desc, int *val) 224*c2832197SSean Wang { 225*c2832197SSean Wang const struct mtk_drive_desc *tb; 226*c2832197SSean Wang int err, val1, val2; 227*c2832197SSean Wang 228*c2832197SSean Wang tb = &mtk_drive[desc->drv_n]; 229*c2832197SSean Wang 230*c2832197SSean Wang err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_E4, &val1); 231*c2832197SSean Wang if (err) 232*c2832197SSean Wang return err; 233*c2832197SSean Wang 234*c2832197SSean Wang err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_E8, &val2); 235*c2832197SSean Wang if (err) 236*c2832197SSean Wang return err; 237*c2832197SSean Wang 238*c2832197SSean Wang /* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1) 239*c2832197SSean Wang * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1) 240*c2832197SSean Wang */ 241*c2832197SSean Wang *val = (((val2 << 1) + val1) / tb->scal + 1) * tb->step; 242*c2832197SSean Wang 243*c2832197SSean Wang return 0; 244*c2832197SSean Wang } 245