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>
11 #include <power/pmic.h>
12 #include <spmi/spmi.h>
15 #include <linux/bitops.h>
17 /* Register offset for each gpio */
18 #define REG_OFFSET(x) ((x) * 0x100)
22 /* Type and subtype are shared for all pm8916 peripherals */
24 #define REG_SUBTYPE 0x5
26 #define REG_STATUS 0x08
27 #define REG_STATUS_VAL_MASK 0x1
31 #define REG_CTL_MODE_MASK 0x70
32 #define REG_CTL_MODE_INPUT 0x00
33 #define REG_CTL_MODE_INOUT 0x20
34 #define REG_CTL_MODE_OUTPUT 0x10
35 #define REG_CTL_OUTPUT_MASK 0x0F
37 #define REG_DIG_VIN_CTL 0x41
38 #define REG_DIG_VIN_VIN0 0
40 #define REG_DIG_PULL_CTL 0x42
41 #define REG_DIG_PULL_NO_PU 0x5
43 #define REG_DIG_OUT_CTL 0x45
44 #define REG_DIG_OUT_CTL_CMOS (0x0 << 4)
45 #define REG_DIG_OUT_CTL_DRIVE_L 0x1
47 #define REG_EN_CTL 0x46
48 #define REG_EN_CTL_ENABLE (1 << 7)
50 struct pm8916_gpio_bank {
51 uint32_t pid; /* Peripheral ID on SPMI bus */
54 static int pm8916_gpio_set_direction(struct udevice *dev, unsigned offset,
55 bool input, int value)
57 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
58 uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
61 /* Disable the GPIO */
62 ret = pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL,
63 REG_EN_CTL_ENABLE, 0);
69 ret = pmic_reg_write(dev->parent, gpio_base + REG_CTL,
72 ret = pmic_reg_write(dev->parent, gpio_base + REG_CTL,
73 REG_CTL_MODE_INOUT | (value ? 1 : 0));
77 /* Set the right pull (no pull) */
78 ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_PULL_CTL,
83 /* Configure output pin drivers if needed */
85 /* Select the VIN - VIN0, pin is input so it doesn't matter */
86 ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_VIN_CTL,
91 /* Set the right dig out control */
92 ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_OUT_CTL,
93 REG_DIG_OUT_CTL_CMOS |
94 REG_DIG_OUT_CTL_DRIVE_L);
100 return pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL, 0,
104 static int pm8916_gpio_direction_input(struct udevice *dev, unsigned offset)
106 return pm8916_gpio_set_direction(dev, offset, true, 0);
109 static int pm8916_gpio_direction_output(struct udevice *dev, unsigned offset,
112 return pm8916_gpio_set_direction(dev, offset, false, value);
115 static int pm8916_gpio_get_function(struct udevice *dev, unsigned offset)
117 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
118 uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
121 /* Set the output value of the gpio */
122 reg = pmic_reg_read(dev->parent, gpio_base + REG_CTL);
126 switch (reg & REG_CTL_MODE_MASK) {
127 case REG_CTL_MODE_INPUT:
129 case REG_CTL_MODE_INOUT: /* Fallthrough */
130 case REG_CTL_MODE_OUTPUT:
133 return GPIOF_UNKNOWN;
137 static int pm8916_gpio_get_value(struct udevice *dev, unsigned offset)
139 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
140 uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
143 reg = pmic_reg_read(dev->parent, gpio_base + REG_STATUS);
147 return !!(reg & REG_STATUS_VAL_MASK);
150 static int pm8916_gpio_set_value(struct udevice *dev, unsigned offset,
153 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
154 uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
156 /* Set the output value of the gpio */
157 return pmic_clrsetbits(dev->parent, gpio_base + REG_CTL,
158 REG_CTL_OUTPUT_MASK, !!value);
161 static const struct dm_gpio_ops pm8916_gpio_ops = {
162 .direction_input = pm8916_gpio_direction_input,
163 .direction_output = pm8916_gpio_direction_output,
164 .get_value = pm8916_gpio_get_value,
165 .set_value = pm8916_gpio_set_value,
166 .get_function = pm8916_gpio_get_function,
169 static int pm8916_gpio_probe(struct udevice *dev)
171 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
174 priv->pid = dev_read_addr(dev);
175 if (priv->pid == FDT_ADDR_T_NONE)
176 return log_msg_ret("bad address", -EINVAL);
178 /* Do a sanity check */
179 reg = pmic_reg_read(dev->parent, priv->pid + REG_TYPE);
181 return log_msg_ret("bad type", -ENXIO);
183 reg = pmic_reg_read(dev->parent, priv->pid + REG_SUBTYPE);
184 if (reg != 0x5 && reg != 0x1)
185 return log_msg_ret("bad subtype", -ENXIO);
190 static int pm8916_gpio_of_to_plat(struct udevice *dev)
192 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
194 uc_priv->gpio_count = dev_read_u32_default(dev, "gpio-count", 0);
195 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
196 if (uc_priv->bank_name == NULL)
197 uc_priv->bank_name = "pm8916";
202 static const struct udevice_id pm8916_gpio_ids[] = {
203 { .compatible = "qcom,pm8916-gpio" },
204 { .compatible = "qcom,pm8994-gpio" }, /* 22 GPIO's */
205 { .compatible = "qcom,pm8998-gpio" },
209 U_BOOT_DRIVER(gpio_pm8916) = {
210 .name = "gpio_pm8916",
212 .of_match = pm8916_gpio_ids,
213 .of_to_plat = pm8916_gpio_of_to_plat,
214 .probe = pm8916_gpio_probe,
215 .ops = &pm8916_gpio_ops,
216 .priv_auto = sizeof(struct pm8916_gpio_bank),
220 /* Add pmic buttons as GPIO as well - there is no generic way for now */
221 #define PON_INT_RT_STS 0x10
222 #define KPDPWR_ON_INT_BIT 0
223 #define RESIN_ON_INT_BIT 1
225 static int pm8941_pwrkey_get_function(struct udevice *dev, unsigned offset)
230 static int pm8941_pwrkey_get_value(struct udevice *dev, unsigned offset)
232 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
234 int reg = pmic_reg_read(dev->parent, priv->pid + PON_INT_RT_STS);
240 case 0: /* Power button */
241 return (reg & BIT(KPDPWR_ON_INT_BIT)) != 0;
243 case 1: /* Reset button */
245 return (reg & BIT(RESIN_ON_INT_BIT)) != 0;
250 static const struct dm_gpio_ops pm8941_pwrkey_ops = {
251 .get_value = pm8941_pwrkey_get_value,
252 .get_function = pm8941_pwrkey_get_function,
255 static int pm8941_pwrkey_probe(struct udevice *dev)
257 struct pm8916_gpio_bank *priv = dev_get_priv(dev);
260 priv->pid = dev_read_addr(dev);
261 if (priv->pid == FDT_ADDR_T_NONE)
262 return log_msg_ret("bad address", -EINVAL);
264 /* Do a sanity check */
265 reg = pmic_reg_read(dev->parent, priv->pid + REG_TYPE);
267 return log_msg_ret("bad type", -ENXIO);
269 reg = pmic_reg_read(dev->parent, priv->pid + REG_SUBTYPE);
270 if ((reg & 0x5) == 0)
271 return log_msg_ret("bad subtype", -ENXIO);
276 static int pm8941_pwrkey_of_to_plat(struct udevice *dev)
278 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
280 uc_priv->gpio_count = 2;
281 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
282 if (uc_priv->bank_name == NULL)
283 uc_priv->bank_name = "pm8916_key";
288 static const struct udevice_id pm8941_pwrkey_ids[] = {
289 { .compatible = "qcom,pm8916-pwrkey" },
290 { .compatible = "qcom,pm8994-pwrkey" },
291 { .compatible = "qcom,pm8998-pwrkey" },
295 U_BOOT_DRIVER(pwrkey_pm89xx) = {
296 .name = "pwrkey_pm89xx",
298 .of_match = pm8941_pwrkey_ids,
299 .of_to_plat = pm8941_pwrkey_of_to_plat,
300 .probe = pm8941_pwrkey_probe,
301 .ops = &pm8941_pwrkey_ops,
302 .priv_auto = sizeof(struct pm8916_gpio_bank),