blob: 26a3fba194a236bc66ce32ae79b861513739865e [file] [log] [blame]
Ramon Friede43d8e72018-05-16 12:13:40 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * TLMM driver for Qualcomm APQ8016, APQ8096
4 *
5 * (C) Copyright 2018 Ramon Fried <ramon.fried@gmail.com>
6 *
7 */
8
Ramon Friede43d8e72018-05-16 12:13:40 +03009#include <dm.h>
10#include <errno.h>
11#include <asm/io.h>
Sumit Gargb7572e52022-07-27 13:52:04 +053012#include <dm/device_compat.h>
Caleb Connolly506eb532023-11-14 12:55:40 +000013#include <dm/device-internal.h>
Sumit Gargb7572e52022-07-27 13:52:04 +053014#include <dm/lists.h>
Caleb Connollyfabb8972023-11-14 12:55:42 +000015#include <asm/gpio.h>
Ramon Friede43d8e72018-05-16 12:13:40 +030016#include <dm/pinctrl.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Caleb Connolly190005c2024-02-26 17:26:17 +000018#include <linux/bug.h>
Caleb Connollyfabb8972023-11-14 12:55:42 +000019#include <mach/gpio.h>
20
Caleb Connolly506eb532023-11-14 12:55:40 +000021#include "pinctrl-qcom.h"
Ramon Friede43d8e72018-05-16 12:13:40 +030022
23struct msm_pinctrl_priv {
24 phys_addr_t base;
25 struct msm_pinctrl_data *data;
26};
27
Caleb Connollyfabb8972023-11-14 12:55:42 +000028#define GPIO_CONFIG_REG(priv, x) \
29 (qcom_pin_offset((priv)->data->pin_data.pin_offsets, x))
30
Sumit Gargadc3bcb2024-04-12 15:24:35 +053031#define GPIO_IN_OUT_REG(priv, x) \
32 (GPIO_CONFIG_REG(priv, x) + 0x4)
33
34#define TLMM_GPIO_PULL_MASK GENMASK(1, 0)
35#define TLMM_FUNC_SEL_MASK GENMASK(5, 2)
36#define TLMM_DRV_STRENGTH_MASK GENMASK(8, 6)
37#define TLMM_GPIO_OUTPUT_MASK BIT(1)
38#define TLMM_GPIO_OE_MASK BIT(9)
39
40/* GPIO register shifts. */
41#define GPIO_OUT_SHIFT 1
Ramon Friede43d8e72018-05-16 12:13:40 +030042
43static const struct pinconf_param msm_conf_params[] = {
Sumit Gargfd1ad932023-02-01 19:28:52 +053044 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 2 },
Ramon Friede43d8e72018-05-16 12:13:40 +030045 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
Sumit Gargfd1ad932023-02-01 19:28:52 +053046 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 3 },
Neil Armstrongd3eed4b2024-05-28 10:31:53 +020047 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_UP, 1 },
Sumit Gargadc3bcb2024-04-12 15:24:35 +053048 { "output-high", PIN_CONFIG_OUTPUT, 1, },
49 { "output-low", PIN_CONFIG_OUTPUT, 0, },
Ramon Friede43d8e72018-05-16 12:13:40 +030050};
51
52static int msm_get_functions_count(struct udevice *dev)
53{
54 struct msm_pinctrl_priv *priv = dev_get_priv(dev);
55
56 return priv->data->functions_count;
57}
58
59static int msm_get_pins_count(struct udevice *dev)
60{
61 struct msm_pinctrl_priv *priv = dev_get_priv(dev);
62
Caleb Connollyfabb8972023-11-14 12:55:42 +000063 return priv->data->pin_data.pin_count;
Ramon Friede43d8e72018-05-16 12:13:40 +030064}
65
66static const char *msm_get_function_name(struct udevice *dev,
67 unsigned int selector)
68{
69 struct msm_pinctrl_priv *priv = dev_get_priv(dev);
70
71 return priv->data->get_function_name(dev, selector);
72}
73
74static int msm_pinctrl_probe(struct udevice *dev)
75{
76 struct msm_pinctrl_priv *priv = dev_get_priv(dev);
77
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +090078 priv->base = dev_read_addr(dev);
Caleb Connollyfabb8972023-11-14 12:55:42 +000079 priv->data = (struct msm_pinctrl_data *)dev_get_driver_data(dev);
Ramon Friede43d8e72018-05-16 12:13:40 +030080
81 return priv->base == FDT_ADDR_T_NONE ? -EINVAL : 0;
82}
83
84static const char *msm_get_pin_name(struct udevice *dev, unsigned int selector)
85{
86 struct msm_pinctrl_priv *priv = dev_get_priv(dev);
87
88 return priv->data->get_pin_name(dev, selector);
89}
90
91static int msm_pinmux_set(struct udevice *dev, unsigned int pin_selector,
92 unsigned int func_selector)
93{
94 struct msm_pinctrl_priv *priv = dev_get_priv(dev);
Volodymyr Babchukc4cc9792024-03-11 21:33:46 +000095 u32 func = priv->data->get_function_mux(pin_selector, func_selector);
Ramon Friede43d8e72018-05-16 12:13:40 +030096
Caleb Connolly190005c2024-02-26 17:26:17 +000097 /* Always NOP for special pins, assume they're in the correct state */
98 if (qcom_is_special_pin(&priv->data->pin_data, pin_selector))
99 return 0;
100
Caleb Connollyfabb8972023-11-14 12:55:42 +0000101 clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector),
Sumit Gargadc3bcb2024-04-12 15:24:35 +0530102 TLMM_FUNC_SEL_MASK | TLMM_GPIO_OE_MASK, func << 2);
Ramon Friede43d8e72018-05-16 12:13:40 +0300103 return 0;
104}
105
Neil Armstrong0154d922024-05-28 10:31:55 +0200106static int msm_pinconf_set_special(struct msm_pinctrl_priv *priv, unsigned int pin_selector,
107 unsigned int param, unsigned int argument)
108{
109 unsigned int offset = pin_selector - priv->data->pin_data.special_pins_start;
110 const struct msm_special_pin_data *data;
111
112 if (!priv->data->pin_data.special_pins_data)
113 return 0;
114
115 data = &priv->data->pin_data.special_pins_data[offset];
116
117 switch (param) {
118 case PIN_CONFIG_DRIVE_STRENGTH:
119 argument = (argument / 2) - 1;
120 clrsetbits_le32(priv->base + data->ctl_reg,
121 GENMASK(2, 0) << data->drv_bit,
122 argument << data->drv_bit);
123 break;
124 case PIN_CONFIG_BIAS_DISABLE:
125 clrbits_le32(priv->base + data->ctl_reg,
126 TLMM_GPIO_PULL_MASK << data->pull_bit);
127 break;
128 case PIN_CONFIG_BIAS_PULL_UP:
129 clrsetbits_le32(priv->base + data->ctl_reg,
130 TLMM_GPIO_PULL_MASK << data->pull_bit,
131 argument << data->pull_bit);
132 break;
133 default:
134 return 0;
135 }
136
137 return 0;
138}
139
Ramon Friede43d8e72018-05-16 12:13:40 +0300140static int msm_pinconf_set(struct udevice *dev, unsigned int pin_selector,
141 unsigned int param, unsigned int argument)
142{
143 struct msm_pinctrl_priv *priv = dev_get_priv(dev);
144
Caleb Connolly190005c2024-02-26 17:26:17 +0000145 if (qcom_is_special_pin(&priv->data->pin_data, pin_selector))
Neil Armstrong0154d922024-05-28 10:31:55 +0200146 return msm_pinconf_set_special(priv, pin_selector, param, argument);
Caleb Connolly190005c2024-02-26 17:26:17 +0000147
Ramon Friede43d8e72018-05-16 12:13:40 +0300148 switch (param) {
149 case PIN_CONFIG_DRIVE_STRENGTH:
Sumit Gargfd1ad932023-02-01 19:28:52 +0530150 argument = (argument / 2) - 1;
Caleb Connollyfabb8972023-11-14 12:55:42 +0000151 clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector),
Ramon Friede43d8e72018-05-16 12:13:40 +0300152 TLMM_DRV_STRENGTH_MASK, argument << 6);
153 break;
154 case PIN_CONFIG_BIAS_DISABLE:
Caleb Connollyfabb8972023-11-14 12:55:42 +0000155 clrbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector),
Ramon Friede43d8e72018-05-16 12:13:40 +0300156 TLMM_GPIO_PULL_MASK);
157 break;
Sumit Gargfd1ad932023-02-01 19:28:52 +0530158 case PIN_CONFIG_BIAS_PULL_UP:
Caleb Connollyfabb8972023-11-14 12:55:42 +0000159 clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector),
Sumit Gargfd1ad932023-02-01 19:28:52 +0530160 TLMM_GPIO_PULL_MASK, argument);
161 break;
Sumit Gargadc3bcb2024-04-12 15:24:35 +0530162 case PIN_CONFIG_OUTPUT:
163 writel(argument << GPIO_OUT_SHIFT,
164 priv->base + GPIO_IN_OUT_REG(priv, pin_selector));
165 setbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector),
166 TLMM_GPIO_OE_MASK);
167 break;
Ramon Friede43d8e72018-05-16 12:13:40 +0300168 default:
169 return 0;
170 }
171
172 return 0;
173}
174
Caleb Connolly506eb532023-11-14 12:55:40 +0000175struct pinctrl_ops msm_pinctrl_ops = {
Ramon Friede43d8e72018-05-16 12:13:40 +0300176 .get_pins_count = msm_get_pins_count,
177 .get_pin_name = msm_get_pin_name,
178 .set_state = pinctrl_generic_set_state,
179 .pinmux_set = msm_pinmux_set,
180 .pinconf_num_params = ARRAY_SIZE(msm_conf_params),
181 .pinconf_params = msm_conf_params,
182 .pinconf_set = msm_pinconf_set,
183 .get_functions_count = msm_get_functions_count,
184 .get_function_name = msm_get_function_name,
185};
186
Caleb Connolly506eb532023-11-14 12:55:40 +0000187int msm_pinctrl_bind(struct udevice *dev)
Sumit Gargb7572e52022-07-27 13:52:04 +0530188{
189 ofnode node = dev_ofnode(dev);
Caleb Connolly506eb532023-11-14 12:55:40 +0000190 struct msm_pinctrl_data *data = (struct msm_pinctrl_data *)dev_get_driver_data(dev);
191 struct driver *drv;
192 struct udevice *pinctrl_dev;
Sumit Gargb7572e52022-07-27 13:52:04 +0530193 const char *name;
194 int ret;
195
Caleb Connolly190005c2024-02-26 17:26:17 +0000196 if (!data->pin_data.special_pins_start)
197 dev_warn(dev, "Special pins start index not defined!\n");
198
Caleb Connolly506eb532023-11-14 12:55:40 +0000199 drv = lists_driver_lookup_name("pinctrl_qcom");
200 if (!drv)
201 return -ENOENT;
202
203 ret = device_bind_with_driver_data(dev_get_parent(dev), drv, ofnode_get_name(node), (ulong)data,
204 dev_ofnode(dev), &pinctrl_dev);
205 if (ret)
206 return ret;
207
Sumit Gargb7572e52022-07-27 13:52:04 +0530208 ofnode_get_property(node, "gpio-controller", &ret);
209 if (ret < 0)
210 return 0;
211
212 /* Get the name of gpio node */
213 name = ofnode_get_name(node);
214 if (!name)
215 return -EINVAL;
216
Caleb Connollyfabb8972023-11-14 12:55:42 +0000217 drv = lists_driver_lookup_name("gpio_msm");
218 if (!drv) {
219 printf("Can't find gpio_msm driver\n");
220 return -ENODEV;
221 }
222
223 /* Bind gpio device as a child of the pinctrl device */
224 ret = device_bind_with_driver_data(pinctrl_dev, drv,
225 name, (ulong)&data->pin_data, node, NULL);
Caleb Connolly506eb532023-11-14 12:55:40 +0000226 if (ret) {
227 device_unbind(pinctrl_dev);
Sumit Gargb7572e52022-07-27 13:52:04 +0530228 return ret;
Caleb Connolly506eb532023-11-14 12:55:40 +0000229 }
Sumit Gargb7572e52022-07-27 13:52:04 +0530230
231 return 0;
232}
233
Caleb Connolly506eb532023-11-14 12:55:40 +0000234U_BOOT_DRIVER(pinctrl_qcom) = {
235 .name = "pinctrl_qcom",
Ramon Friede43d8e72018-05-16 12:13:40 +0300236 .id = UCLASS_PINCTRL,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700237 .priv_auto = sizeof(struct msm_pinctrl_priv),
Ramon Friede43d8e72018-05-16 12:13:40 +0300238 .ops = &msm_pinctrl_ops,
239 .probe = msm_pinctrl_probe,
Ramon Friede43d8e72018-05-16 12:13:40 +0300240};