Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2018 MediaTek Inc.
0004  *
0005  * Author: Sean Wang <sean.wang@mediatek.com>
0006  *
0007  */
0008 
0009 #ifndef __PINCTRL_MTK_COMMON_V2_H
0010 #define __PINCTRL_MTK_COMMON_V2_H
0011 
0012 #include <linux/gpio/driver.h>
0013 
0014 #define MTK_INPUT      0
0015 #define MTK_OUTPUT     1
0016 #define MTK_DISABLE    0
0017 #define MTK_ENABLE     1
0018 #define MTK_PULLDOWN   0
0019 #define MTK_PULLUP     1
0020 #define MTK_PULL_PU_PD_TYPE     BIT(0)
0021 #define MTK_PULL_PULLSEL_TYPE       BIT(1)
0022 #define MTK_PULL_PUPD_R1R0_TYPE     BIT(2)
0023 /* MTK_PULL_RSEL_TYPE can select resistance and can be
0024  * turned on/off itself. But it can't be selected pull up/down
0025  */
0026 #define MTK_PULL_RSEL_TYPE      BIT(3)
0027 /* MTK_PULL_PU_PD_RSEL_TYPE is a type which is controlled by
0028  * MTK_PULL_PU_PD_TYPE and MTK_PULL_RSEL_TYPE.
0029  */
0030 #define MTK_PULL_PU_PD_RSEL_TYPE    (MTK_PULL_PU_PD_TYPE \
0031                     | MTK_PULL_RSEL_TYPE)
0032 #define MTK_PULL_TYPE_MASK  (MTK_PULL_PU_PD_TYPE |\
0033                  MTK_PULL_PULLSEL_TYPE |\
0034                  MTK_PULL_PUPD_R1R0_TYPE |\
0035                  MTK_PULL_RSEL_TYPE)
0036 
0037 #define EINT_NA U16_MAX
0038 #define NO_EINT_SUPPORT EINT_NA
0039 
0040 #define PIN_FIELD_CALC(_s_pin, _e_pin, _i_base, _s_addr, _x_addrs,      \
0041                _s_bit, _x_bits, _sz_reg, _fixed) {      \
0042         .s_pin = _s_pin,                    \
0043         .e_pin = _e_pin,                    \
0044         .i_base = _i_base,                  \
0045         .s_addr = _s_addr,                  \
0046         .x_addrs = _x_addrs,                    \
0047         .s_bit = _s_bit,                    \
0048         .x_bits = _x_bits,                  \
0049         .sz_reg = _sz_reg,                  \
0050         .fixed = _fixed,                    \
0051     }
0052 
0053 #define PIN_FIELD(_s_pin, _e_pin, _s_addr, _x_addrs, _s_bit, _x_bits)   \
0054     PIN_FIELD_CALC(_s_pin, _e_pin, 0, _s_addr, _x_addrs, _s_bit,    \
0055                _x_bits, 32, 0)
0056 
0057 #define PINS_FIELD(_s_pin, _e_pin, _s_addr, _x_addrs, _s_bit, _x_bits)  \
0058     PIN_FIELD_CALC(_s_pin, _e_pin, 0, _s_addr, _x_addrs, _s_bit,    \
0059                _x_bits, 32, 1)
0060 
0061 #define PIN_RSEL(_s_pin, _e_pin, _rsel_index, _up_resl, _down_rsel) {   \
0062         .s_pin = _s_pin,                    \
0063         .e_pin = _e_pin,                    \
0064         .rsel_index = _rsel_index,              \
0065         .up_rsel = _up_resl,                    \
0066         .down_rsel = _down_rsel,                \
0067     }
0068 
0069 /* List these attributes which could be modified for the pin */
0070 enum {
0071     PINCTRL_PIN_REG_MODE,
0072     PINCTRL_PIN_REG_DIR,
0073     PINCTRL_PIN_REG_DI,
0074     PINCTRL_PIN_REG_DO,
0075     PINCTRL_PIN_REG_SR,
0076     PINCTRL_PIN_REG_SMT,
0077     PINCTRL_PIN_REG_PD,
0078     PINCTRL_PIN_REG_PU,
0079     PINCTRL_PIN_REG_E4,
0080     PINCTRL_PIN_REG_E8,
0081     PINCTRL_PIN_REG_TDSEL,
0082     PINCTRL_PIN_REG_RDSEL,
0083     PINCTRL_PIN_REG_DRV,
0084     PINCTRL_PIN_REG_PUPD,
0085     PINCTRL_PIN_REG_R0,
0086     PINCTRL_PIN_REG_R1,
0087     PINCTRL_PIN_REG_IES,
0088     PINCTRL_PIN_REG_PULLEN,
0089     PINCTRL_PIN_REG_PULLSEL,
0090     PINCTRL_PIN_REG_DRV_EN,
0091     PINCTRL_PIN_REG_DRV_E0,
0092     PINCTRL_PIN_REG_DRV_E1,
0093     PINCTRL_PIN_REG_DRV_ADV,
0094     PINCTRL_PIN_REG_RSEL,
0095     PINCTRL_PIN_REG_MAX,
0096 };
0097 
0098 /* Group the pins by the driving current */
0099 enum {
0100     DRV_FIXED,
0101     DRV_GRP0,
0102     DRV_GRP1,
0103     DRV_GRP2,
0104     DRV_GRP3,
0105     DRV_GRP4,
0106     DRV_GRP_MAX,
0107 };
0108 
0109 static const char * const mtk_default_register_base_names[] __maybe_unused = {
0110     "base",
0111 };
0112 
0113 /* struct mtk_pin_field - the structure that holds the information of the field
0114  *            used to describe the attribute for the pin
0115  * @base:       the index pointing to the entry in base address list
0116  * @offset:     the register offset relative to the base address
0117  * @mask:       the mask used to filter out the field from the register
0118  * @bitpos:     the start bit relative to the register
0119  * @next:       the indication that the field would be extended to the
0120             next register
0121  */
0122 struct mtk_pin_field {
0123     u8  index;
0124     u32 offset;
0125     u32 mask;
0126     u8  bitpos;
0127     u8  next;
0128 };
0129 
0130 /* struct mtk_pin_field_calc - the structure that holds the range providing
0131  *                 the guide used to look up the relevant field
0132  * @s_pin:      the start pin within the range
0133  * @e_pin:      the end pin within the range
0134  * @i_base:     the index pointing to the entry in base address list
0135  * @s_addr:     the start address for the range
0136  * @x_addrs:        the address distance between two consecutive registers
0137  *          within the range
0138  * @s_bit:      the start bit for the first register within the range
0139  * @x_bits:     the bit distance between two consecutive pins within
0140  *          the range
0141  * @sz_reg:     the size of bits in a register
0142  * @fixed:      the consecutive pins share the same bits with the 1st
0143  *          pin
0144  */
0145 struct mtk_pin_field_calc {
0146     u16 s_pin;
0147     u16 e_pin;
0148     u8  i_base;
0149     u32 s_addr;
0150     u8  x_addrs;
0151     u8  s_bit;
0152     u8  x_bits;
0153     u8  sz_reg;
0154     u8  fixed;
0155 };
0156 
0157 /**
0158  * struct mtk_pin_rsel - the structure that provides bias resistance selection.
0159  * @s_pin:      the start pin within the rsel range
0160  * @e_pin:      the end pin within the rsel range
0161  * @rsel_index: the rsel bias resistance index
0162  * @up_rsel:    the pullup rsel bias resistance value
0163  * @down_rsel:  the pulldown rsel bias resistance value
0164  */
0165 struct mtk_pin_rsel {
0166     u16 s_pin;
0167     u16 e_pin;
0168     u16 rsel_index;
0169     u32 up_rsel;
0170     u32 down_rsel;
0171 };
0172 
0173 /* struct mtk_pin_reg_calc - the structure that holds all ranges used to
0174  *               determine which register the pin would make use of
0175  *               for certain pin attribute.
0176  * @range:           the start address for the range
0177  * @nranges:             the number of items in the range
0178  */
0179 struct mtk_pin_reg_calc {
0180     const struct mtk_pin_field_calc *range;
0181     unsigned int nranges;
0182 };
0183 
0184 /**
0185  * struct mtk_func_desc - the structure that providing information
0186  *            all the funcs for this pin
0187  * @name:       the name of function
0188  * @muxval:     the mux to the function
0189  */
0190 struct mtk_func_desc {
0191     const char *name;
0192     u8 muxval;
0193 };
0194 
0195 /**
0196  * struct mtk_eint_desc - the structure that providing information
0197  *                 for eint data per pin
0198  * @eint_m:     the eint mux for this pin
0199  * @eitn_n:     the eint number for this pin
0200  */
0201 struct mtk_eint_desc {
0202     u16 eint_m;
0203     u16 eint_n;
0204 };
0205 
0206 /**
0207  * struct mtk_pin_desc - the structure that providing information
0208  *                 for each pin of chips
0209  * @number:     unique pin number from the global pin number space
0210  * @name:       name for this pin
0211  * @eint:       the eint data for this pin
0212  * @drv_n:      the index with the driving group
0213  * @funcs:      all available functions for this pins (only used in
0214  *          those drivers compatible to pinctrl-mtk-common.c-like
0215  *          ones)
0216  */
0217 struct mtk_pin_desc {
0218     unsigned int number;
0219     const char *name;
0220     struct mtk_eint_desc eint;
0221     u8 drv_n;
0222     struct mtk_func_desc *funcs;
0223 };
0224 
0225 struct mtk_pinctrl_group {
0226     const char  *name;
0227     unsigned long   config;
0228     unsigned    pin;
0229 };
0230 
0231 struct mtk_pinctrl;
0232 
0233 /* struct mtk_pin_soc - the structure that holds SoC-specific data */
0234 struct mtk_pin_soc {
0235     const struct mtk_pin_reg_calc   *reg_cal;
0236     const struct mtk_pin_desc   *pins;
0237     unsigned int            npins;
0238     const struct group_desc     *grps;
0239     unsigned int            ngrps;
0240     const struct function_desc  *funcs;
0241     unsigned int            nfuncs;
0242     const struct mtk_eint_regs  *eint_regs;
0243     const struct mtk_eint_hw    *eint_hw;
0244 
0245     /* Specific parameters per SoC */
0246     u8              gpio_m;
0247     bool                ies_present;
0248     const char * const      *base_names;
0249     unsigned int            nbase_names;
0250     const unsigned int      *pull_type;
0251     const struct mtk_pin_rsel   *pin_rsel;
0252     unsigned int            npin_rsel;
0253 
0254     /* Specific pinconfig operations */
0255     int (*bias_disable_set)(struct mtk_pinctrl *hw,
0256                 const struct mtk_pin_desc *desc);
0257     int (*bias_disable_get)(struct mtk_pinctrl *hw,
0258                 const struct mtk_pin_desc *desc, int *res);
0259     int (*bias_set)(struct mtk_pinctrl *hw,
0260             const struct mtk_pin_desc *desc, bool pullup);
0261     int (*bias_get)(struct mtk_pinctrl *hw,
0262             const struct mtk_pin_desc *desc, bool pullup, int *res);
0263 
0264     int (*bias_set_combo)(struct mtk_pinctrl *hw,
0265             const struct mtk_pin_desc *desc, u32 pullup, u32 arg);
0266     int (*bias_get_combo)(struct mtk_pinctrl *hw,
0267             const struct mtk_pin_desc *desc, u32 *pullup, u32 *arg);
0268 
0269     int (*drive_set)(struct mtk_pinctrl *hw,
0270              const struct mtk_pin_desc *desc, u32 arg);
0271     int (*drive_get)(struct mtk_pinctrl *hw,
0272              const struct mtk_pin_desc *desc, int *val);
0273 
0274     int (*adv_pull_set)(struct mtk_pinctrl *hw,
0275                 const struct mtk_pin_desc *desc, bool pullup,
0276                 u32 arg);
0277     int (*adv_pull_get)(struct mtk_pinctrl *hw,
0278                 const struct mtk_pin_desc *desc, bool pullup,
0279                 u32 *val);
0280     int (*adv_drive_set)(struct mtk_pinctrl *hw,
0281                  const struct mtk_pin_desc *desc, u32 arg);
0282     int (*adv_drive_get)(struct mtk_pinctrl *hw,
0283                  const struct mtk_pin_desc *desc, u32 *val);
0284 
0285     /* Specific driver data */
0286     void                *driver_data;
0287 };
0288 
0289 struct mtk_pinctrl {
0290     struct pinctrl_dev      *pctrl;
0291     void __iomem            **base;
0292     u8              nbase;
0293     struct device           *dev;
0294     struct gpio_chip        chip;
0295     const struct mtk_pin_soc        *soc;
0296     struct mtk_eint         *eint;
0297     struct mtk_pinctrl_group    *groups;
0298     const char          **grp_names;
0299     /* lock pin's register resource to avoid multiple threads issue*/
0300     spinlock_t lock;
0301     /* identify rsel setting by si unit or rsel define in dts node */
0302     bool rsel_si_unit;
0303 };
0304 
0305 void mtk_rmw(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 mask, u32 set);
0306 
0307 int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
0308              int field, int value);
0309 int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
0310              int field, int *value);
0311 
0312 int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev);
0313 
0314 int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
0315                  const struct mtk_pin_desc *desc);
0316 int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
0317                  const struct mtk_pin_desc *desc, int *res);
0318 int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
0319              const struct mtk_pin_desc *desc, bool pullup);
0320 int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
0321              const struct mtk_pin_desc *desc, bool pullup,
0322              int *res);
0323 
0324 int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
0325                       const struct mtk_pin_desc *desc);
0326 int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
0327                       const struct mtk_pin_desc *desc,
0328                       int *res);
0329 int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
0330                   const struct mtk_pin_desc *desc, bool pullup);
0331 int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
0332                   const struct mtk_pin_desc *desc, bool pullup,
0333                   int *res);
0334 int mtk_pinconf_bias_set_combo(struct mtk_pinctrl *hw,
0335                 const struct mtk_pin_desc *desc,
0336                 u32 pullup, u32 enable);
0337 int mtk_pinconf_bias_get_combo(struct mtk_pinctrl *hw,
0338                   const struct mtk_pin_desc *desc,
0339                   u32 *pullup, u32 *enable);
0340 
0341 int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
0342               const struct mtk_pin_desc *desc, u32 arg);
0343 int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
0344               const struct mtk_pin_desc *desc, int *val);
0345 
0346 int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
0347                    const struct mtk_pin_desc *desc, u32 arg);
0348 int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
0349                    const struct mtk_pin_desc *desc, int *val);
0350 
0351 int mtk_pinconf_drive_set_raw(struct mtk_pinctrl *hw,
0352                    const struct mtk_pin_desc *desc, u32 arg);
0353 int mtk_pinconf_drive_get_raw(struct mtk_pinctrl *hw,
0354                    const struct mtk_pin_desc *desc, int *val);
0355 
0356 int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
0357                  const struct mtk_pin_desc *desc, bool pullup,
0358                  u32 arg);
0359 int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
0360                  const struct mtk_pin_desc *desc, bool pullup,
0361                  u32 *val);
0362 int mtk_pinconf_adv_drive_set(struct mtk_pinctrl *hw,
0363                   const struct mtk_pin_desc *desc, u32 arg);
0364 int mtk_pinconf_adv_drive_get(struct mtk_pinctrl *hw,
0365                   const struct mtk_pin_desc *desc, u32 *val);
0366 int mtk_pinconf_adv_drive_set_raw(struct mtk_pinctrl *hw,
0367                   const struct mtk_pin_desc *desc, u32 arg);
0368 int mtk_pinconf_adv_drive_get_raw(struct mtk_pinctrl *hw,
0369                   const struct mtk_pin_desc *desc, u32 *val);
0370 
0371 bool mtk_is_virt_gpio(struct mtk_pinctrl *hw, unsigned int gpio_n);
0372 #endif /* __PINCTRL_MTK_COMMON_V2_H */