1 // SPDX-License-Identifier: GPL-2.0+
3 * Qualcomm pm8916 pmic gpio driver - part of Qualcomm PM8916 PMIC
5 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
10 #include <power/pmic.h>
11 #include <spmi/spmi.h>
14 #include <linux/bitops.h>
16 /* Register offset for each gpio */
17 #define REG_OFFSET(x) ((x) * 0x100)
21 /* Type and subtype are shared for all pm8916 peripherals */
23 #define REG_SUBTYPE 0x5
25 #define REG_STATUS 0x08
26 #define REG_STATUS_VAL_MASK 0x1
30 #define REG_CTL_MODE_MASK 0x70
31 #define REG_CTL_MODE_INPUT 0x00
32 #define REG_CTL_MODE_INOUT 0x20
33 #define REG_CTL_MODE_OUTPUT 0x10
34 #define REG_CTL_OUTPUT_MASK 0x0F
36 #define REG_DIG_VIN_CTL 0x41
37 #define REG_DIG_VIN_VIN0 0
39 #define REG_DIG_PULL_CTL 0x42
40 #define REG_DIG_PULL_NO_PU 0x5
42 #define REG_DIG_OUT_CTL 0x45
43 #define REG_DIG_OUT_CTL_CMOS (0x0 << 4)
44 #define REG_DIG_OUT_CTL_DRIVE_L 0x1
46 #define REG_EN_CTL 0x46
47 #define REG_EN_CTL_ENABLE (1 << 7)
49 struct pm8916_gpio_bank {
50 uint32_t pid; /* Peripheral ID on SPMI bus */
53 static int pm8916_gpio_set_direction(struct udevice *dev, unsigned offset,
54 bool input, int value)
56 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
57 uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
60 /* Disable the GPIO */
61 ret = pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL,
62 REG_EN_CTL_ENABLE, 0);
68 ret = pmic_reg_write(dev->parent, gpio_base + REG_CTL,
71 ret = pmic_reg_write(dev->parent, gpio_base + REG_CTL,
72 REG_CTL_MODE_INOUT | (value ? 1 : 0));
76 /* Set the right pull (no pull) */
77 ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_PULL_CTL,
82 /* Configure output pin drivers if needed */
84 /* Select the VIN - VIN0, pin is input so it doesn't matter */
85 ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_VIN_CTL,
90 /* Set the right dig out control */
91 ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_OUT_CTL,
92 REG_DIG_OUT_CTL_CMOS |
93 REG_DIG_OUT_CTL_DRIVE_L);
99 return pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL, 0,
103 static int pm8916_gpio_direction_input(struct udevice *dev, unsigned offset)
105 return pm8916_gpio_set_direction(dev, offset, true, 0);
108 static int pm8916_gpio_direction_output(struct udevice *dev, unsigned offset,
111 return pm8916_gpio_set_direction(dev, offset, false, value);
114 static int pm8916_gpio_get_function(struct udevice *dev, unsigned offset)
116 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
117 uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
120 /* Set the output value of the gpio */
121 reg = pmic_reg_read(dev->parent, gpio_base + REG_CTL);
125 switch (reg & REG_CTL_MODE_MASK) {
126 case REG_CTL_MODE_INPUT:
128 case REG_CTL_MODE_INOUT: /* Fallthrough */
129 case REG_CTL_MODE_OUTPUT:
132 return GPIOF_UNKNOWN;
136 static int pm8916_gpio_get_value(struct udevice *dev, unsigned offset)
138 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
139 uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
142 reg = pmic_reg_read(dev->parent, gpio_base + REG_STATUS);
146 return !!(reg & REG_STATUS_VAL_MASK);
149 static int pm8916_gpio_set_value(struct udevice *dev, unsigned offset,
152 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
153 uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
155 /* Set the output value of the gpio */
156 return pmic_clrsetbits(dev->parent, gpio_base + REG_CTL,
157 REG_CTL_OUTPUT_MASK, !!value);
160 static const struct dm_gpio_ops pm8916_gpio_ops = {
161 .direction_input = pm8916_gpio_direction_input,
162 .direction_output = pm8916_gpio_direction_output,
163 .get_value = pm8916_gpio_get_value,
164 .set_value = pm8916_gpio_set_value,
165 .get_function = pm8916_gpio_get_function,
168 static int pm8916_gpio_probe(struct udevice *dev)
170 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
173 priv->pid = dev_read_addr(dev);
174 if (priv->pid == FDT_ADDR_T_NONE)
177 /* Do a sanity check */
178 reg = pmic_reg_read(dev->parent, priv->pid + REG_TYPE);
182 reg = pmic_reg_read(dev->parent, priv->pid + REG_SUBTYPE);
183 if (reg != 0x5 && reg != 0x1)
189 static int pm8916_gpio_ofdata_to_platdata(struct udevice *dev)
191 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
193 uc_priv->gpio_count = dev_read_u32_default(dev, "gpio-count", 0);
194 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
195 if (uc_priv->bank_name == NULL)
196 uc_priv->bank_name = "pm8916";
201 static const struct udevice_id pm8916_gpio_ids[] = {
202 { .compatible = "qcom,pm8916-gpio" },
203 { .compatible = "qcom,pm8994-gpio" }, /* 22 GPIO's */
207 U_BOOT_DRIVER(gpio_pm8916) = {
208 .name = "gpio_pm8916",
210 .of_match = pm8916_gpio_ids,
211 .ofdata_to_platdata = pm8916_gpio_ofdata_to_platdata,
212 .probe = pm8916_gpio_probe,
213 .ops = &pm8916_gpio_ops,
214 .priv_auto_alloc_size = sizeof(struct pm8916_gpio_bank),
218 /* Add pmic buttons as GPIO as well - there is no generic way for now */
219 #define PON_INT_RT_STS 0x10
220 #define KPDPWR_ON_INT_BIT 0
221 #define RESIN_ON_INT_BIT 1
223 static int pm8941_pwrkey_get_function(struct udevice *dev, unsigned offset)
228 static int pm8941_pwrkey_get_value(struct udevice *dev, unsigned offset)
230 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
232 int reg = pmic_reg_read(dev->parent, priv->pid + PON_INT_RT_STS);
238 case 0: /* Power button */
239 return (reg & BIT(KPDPWR_ON_INT_BIT)) != 0;
241 case 1: /* Reset button */
243 return (reg & BIT(RESIN_ON_INT_BIT)) != 0;
248 static const struct dm_gpio_ops pm8941_pwrkey_ops = {
249 .get_value = pm8941_pwrkey_get_value,
250 .get_function = pm8941_pwrkey_get_function,
253 static int pm8941_pwrkey_probe(struct udevice *dev)
255 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
258 priv->pid = devfdt_get_addr(dev);
259 if (priv->pid == FDT_ADDR_T_NONE)
262 /* Do a sanity check */
263 reg = pmic_reg_read(dev->parent, priv->pid + REG_TYPE);
267 reg = pmic_reg_read(dev->parent, priv->pid + REG_SUBTYPE);
274 static int pm8941_pwrkey_ofdata_to_platdata(struct udevice *dev)
276 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
278 uc_priv->gpio_count = 2;
279 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
280 if (uc_priv->bank_name == NULL)
281 uc_priv->bank_name = "pm8916_key";
286 static const struct udevice_id pm8941_pwrkey_ids[] = {
287 { .compatible = "qcom,pm8916-pwrkey" },
288 { .compatible = "qcom,pm8994-pwrkey" },
292 U_BOOT_DRIVER(pwrkey_pm8941) = {
293 .name = "pwrkey_pm8916",
295 .of_match = pm8941_pwrkey_ids,
296 .ofdata_to_platdata = pm8941_pwrkey_ofdata_to_platdata,
297 .probe = pm8941_pwrkey_probe,
298 .ops = &pm8941_pwrkey_ops,
299 .priv_auto_alloc_size = sizeof(struct pm8916_gpio_bank),