blob: ee362d8464fc0a569e8b51ca1b08f88856dd3442 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Beniamino Galvani677b5352016-08-16 11:49:49 +02002/*
3 * (C) Copyright 2016 - Beniamino Galvani <b.galvani@gmail.com>
Beniamino Galvani677b5352016-08-16 11:49:49 +02004 */
5
6#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -06007#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Beniamino Galvani2009a8d2017-07-10 00:30:04 +020011#include <dm/device-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <dm/device_compat.h>
Beniamino Galvani2009a8d2017-07-10 00:30:04 +020013#include <dm/lists.h>
Beniamino Galvani677b5352016-08-16 11:49:49 +020014#include <dm/pinctrl.h>
15#include <fdt_support.h>
Simon Glasscd93d622020-05-10 11:40:13 -060016#include <linux/bitops.h>
Beniamino Galvani677b5352016-08-16 11:49:49 +020017#include <linux/err.h>
18#include <linux/io.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060019#include <linux/libfdt.h>
Beniamino Galvani677b5352016-08-16 11:49:49 +020020#include <linux/sizes.h>
Beniamino Galvani2009a8d2017-07-10 00:30:04 +020021#include <asm/gpio.h>
Beniamino Galvani677b5352016-08-16 11:49:49 +020022
23#include "pinctrl-meson.h"
24
25DECLARE_GLOBAL_DATA_PTR;
26
27static const char *meson_pinctrl_dummy_name = "_dummy";
28
Neil Armstrong88fa32b2019-06-04 11:04:52 +020029static char pin_name[PINNAME_SIZE];
30
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +020031int meson_pinctrl_get_groups_count(struct udevice *dev)
Beniamino Galvani677b5352016-08-16 11:49:49 +020032{
33 struct meson_pinctrl *priv = dev_get_priv(dev);
34
35 return priv->data->num_groups;
36}
37
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +020038const char *meson_pinctrl_get_group_name(struct udevice *dev,
39 unsigned int selector)
Beniamino Galvani677b5352016-08-16 11:49:49 +020040{
41 struct meson_pinctrl *priv = dev_get_priv(dev);
42
43 if (!priv->data->groups[selector].name)
44 return meson_pinctrl_dummy_name;
45
46 return priv->data->groups[selector].name;
47}
48
Neil Armstrong88fa32b2019-06-04 11:04:52 +020049int meson_pinctrl_get_pins_count(struct udevice *dev)
50{
51 struct meson_pinctrl *priv = dev_get_priv(dev);
52
53 return priv->data->num_pins;
54}
55
56const char *meson_pinctrl_get_pin_name(struct udevice *dev,
57 unsigned int selector)
58{
59 struct meson_pinctrl *priv = dev_get_priv(dev);
60
61 if (selector > priv->data->num_pins ||
62 selector > priv->data->funcs[0].num_groups)
63 snprintf(pin_name, PINNAME_SIZE, "Error");
64 else
65 snprintf(pin_name, PINNAME_SIZE, "%s",
66 priv->data->funcs[0].groups[selector]);
67
68 return pin_name;
69}
70
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +020071int meson_pinmux_get_functions_count(struct udevice *dev)
Beniamino Galvani677b5352016-08-16 11:49:49 +020072{
73 struct meson_pinctrl *priv = dev_get_priv(dev);
74
75 return priv->data->num_funcs;
76}
77
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +020078const char *meson_pinmux_get_function_name(struct udevice *dev,
79 unsigned int selector)
Beniamino Galvani677b5352016-08-16 11:49:49 +020080{
81 struct meson_pinctrl *priv = dev_get_priv(dev);
82
83 return priv->data->funcs[selector].name;
84}
85
Beniamino Galvani2009a8d2017-07-10 00:30:04 +020086static int meson_gpio_calc_reg_and_bit(struct udevice *dev, unsigned int offset,
87 enum meson_reg_type reg_type,
88 unsigned int *reg, unsigned int *bit)
89{
Jerome Brunetc4c726c2019-01-04 15:44:34 +010090 struct meson_pinctrl *priv = dev_get_priv(dev);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +020091 struct meson_bank *bank = NULL;
92 struct meson_reg_desc *desc;
93 unsigned int pin;
94 int i;
95
96 pin = priv->data->pin_base + offset;
97
98 for (i = 0; i < priv->data->num_banks; i++) {
99 if (pin >= priv->data->banks[i].first &&
100 pin <= priv->data->banks[i].last) {
101 bank = &priv->data->banks[i];
102 break;
103 }
104 }
105
106 if (!bank)
107 return -EINVAL;
108
109 desc = &bank->regs[reg_type];
110 *reg = desc->reg * 4;
111 *bit = desc->bit + pin - bank->first;
112
113 return 0;
114}
115
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +0200116int meson_gpio_get(struct udevice *dev, unsigned int offset)
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200117{
118 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
119 unsigned int reg, bit;
120 int ret;
121
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100122 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_IN, &reg,
123 &bit);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200124 if (ret)
125 return ret;
126
127 return !!(readl(priv->reg_gpio + reg) & BIT(bit));
128}
129
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +0200130int meson_gpio_set(struct udevice *dev, unsigned int offset, int value)
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200131{
132 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
133 unsigned int reg, bit;
134 int ret;
135
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100136 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_OUT, &reg,
137 &bit);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200138 if (ret)
139 return ret;
140
141 clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), value ? BIT(bit) : 0);
142
143 return 0;
144}
145
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +0200146int meson_gpio_get_direction(struct udevice *dev, unsigned int offset)
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200147{
148 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
149 unsigned int reg, bit, val;
150 int ret;
151
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100152 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_DIR, &reg,
153 &bit);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200154 if (ret)
155 return ret;
156
157 val = readl(priv->reg_gpio + reg);
158
159 return (val & BIT(bit)) ? GPIOF_INPUT : GPIOF_OUTPUT;
160}
161
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +0200162int meson_gpio_direction_input(struct udevice *dev, unsigned int offset)
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200163{
164 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
165 unsigned int reg, bit;
166 int ret;
167
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100168 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_DIR, &reg,
169 &bit);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200170 if (ret)
171 return ret;
172
Carlo Caionefb19c7b2018-12-03 18:00:42 +0000173 setbits_le32(priv->reg_gpio + reg, BIT(bit));
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200174
175 return 0;
176}
177
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +0200178int meson_gpio_direction_output(struct udevice *dev,
179 unsigned int offset, int value)
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200180{
181 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
182 unsigned int reg, bit;
183 int ret;
184
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100185 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_DIR, &reg,
186 &bit);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200187 if (ret)
188 return ret;
189
Carlo Caionefb19c7b2018-12-03 18:00:42 +0000190 clrbits_le32(priv->reg_gpio + reg, BIT(bit));
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200191
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100192 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_OUT, &reg,
193 &bit);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200194 if (ret)
195 return ret;
196
197 clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), value ? BIT(bit) : 0);
198
199 return 0;
200}
201
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100202static int meson_pinconf_bias_set(struct udevice *dev, unsigned int pin,
203 unsigned int param)
204{
205 struct meson_pinctrl *priv = dev_get_priv(dev);
206 unsigned int offset = pin - priv->data->pin_base;
207 unsigned int reg, bit;
208 int ret;
209
210 ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_PULLEN, &reg, &bit);
211 if (ret)
212 return ret;
213
214 if (param == PIN_CONFIG_BIAS_DISABLE) {
215 clrsetbits_le32(priv->reg_pullen + reg, BIT(bit), 0);
216 return 0;
217 }
218
219 /* othewise, enable the bias and select level */
Neil Armstrong5ccd5d22020-12-14 19:39:07 +0100220 clrsetbits_le32(priv->reg_pullen + reg, BIT(bit), BIT(bit));
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100221 ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_PULL, &reg, &bit);
222 if (ret)
223 return ret;
224
225 clrsetbits_le32(priv->reg_pull + reg, BIT(bit),
Neil Armstrong5ccd5d22020-12-14 19:39:07 +0100226 (param == PIN_CONFIG_BIAS_PULL_UP ? BIT(bit) : 0));
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100227
228 return 0;
229}
230
Guillaume La Roque478c5632019-06-04 13:53:07 +0200231static int meson_pinconf_drive_strength_set(struct udevice *dev,
232 unsigned int pin,
233 unsigned int drive_strength_ua)
234{
235 struct meson_pinctrl *priv = dev_get_priv(dev);
236 unsigned int offset = pin - priv->data->pin_base;
237 unsigned int reg, bit;
238 unsigned int ds_val;
239 int ret;
240
241 if (!priv->reg_ds) {
242 dev_err(dev, "drive-strength-microamp not supported\n");
243 return -ENOTSUPP;
244 }
245
246 ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_DS, &reg, &bit);
247 if (ret)
248 return ret;
249
250 bit = bit << 1;
251
252 if (drive_strength_ua <= 500) {
253 ds_val = MESON_PINCONF_DRV_500UA;
254 } else if (drive_strength_ua <= 2500) {
255 ds_val = MESON_PINCONF_DRV_2500UA;
256 } else if (drive_strength_ua <= 3000) {
257 ds_val = MESON_PINCONF_DRV_3000UA;
258 } else if (drive_strength_ua <= 4000) {
259 ds_val = MESON_PINCONF_DRV_4000UA;
260 } else {
261 dev_warn(dev,
262 "pin %u: invalid drive-strength-microamp : %d , default to 4mA\n",
263 pin, drive_strength_ua);
264 ds_val = MESON_PINCONF_DRV_4000UA;
265 }
266
267 clrsetbits_le32(priv->reg_ds + reg, 0x3 << bit, ds_val << bit);
268
269 return 0;
270}
271
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100272int meson_pinconf_set(struct udevice *dev, unsigned int pin,
273 unsigned int param, unsigned int arg)
274{
275 int ret;
276
277 switch (param) {
278 case PIN_CONFIG_BIAS_DISABLE:
279 case PIN_CONFIG_BIAS_PULL_UP:
280 case PIN_CONFIG_BIAS_PULL_DOWN:
281 ret = meson_pinconf_bias_set(dev, pin, param);
282 break;
Guillaume La Roque478c5632019-06-04 13:53:07 +0200283 case PIN_CONFIG_DRIVE_STRENGTH_UA:
284 ret = meson_pinconf_drive_strength_set(dev, pin, arg);
285 break;
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100286 default:
287 dev_err(dev, "unsupported configuration parameter %u\n", param);
288 return -EINVAL;
289 }
290
291 return ret;
292}
293
294int meson_pinconf_group_set(struct udevice *dev,
295 unsigned int group_selector,
296 unsigned int param, unsigned int arg)
297{
298 struct meson_pinctrl *priv = dev_get_priv(dev);
299 struct meson_pmx_group *grp = &priv->data->groups[group_selector];
300 int i, ret;
301
302 for (i = 0; i < grp->num_pins; i++) {
303 ret = meson_pinconf_set(dev, grp->pins[i], param, arg);
304 if (ret)
305 return ret;
306 }
307
308 return 0;
309}
310
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +0200311int meson_gpio_probe(struct udevice *dev)
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200312{
313 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
314 struct gpio_dev_priv *uc_priv;
315
316 uc_priv = dev_get_uclass_priv(dev);
317 uc_priv->bank_name = priv->data->name;
318 uc_priv->gpio_count = priv->data->num_pins;
319
320 return 0;
321}
322
Beniamino Galvani677b5352016-08-16 11:49:49 +0200323static fdt_addr_t parse_address(int offset, const char *name, int na, int ns)
324{
325 int index, len = 0;
326 const fdt32_t *reg;
327
Simon Glassb02e4042016-10-02 17:59:28 -0600328 index = fdt_stringlist_search(gd->fdt_blob, offset, "reg-names", name);
Beniamino Galvani677b5352016-08-16 11:49:49 +0200329 if (index < 0)
330 return FDT_ADDR_T_NONE;
331
332 reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
333 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns))))
334 return FDT_ADDR_T_NONE;
335
336 reg += index * (na + ns);
337
338 return fdt_translate_address((void *)gd->fdt_blob, offset, reg);
339}
340
341int meson_pinctrl_probe(struct udevice *dev)
342{
343 struct meson_pinctrl *priv = dev_get_priv(dev);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200344 struct uclass_driver *drv;
345 struct udevice *gpio_dev;
Beniamino Galvani677b5352016-08-16 11:49:49 +0200346 fdt_addr_t addr;
347 int node, gpio = -1, len;
348 int na, ns;
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200349 char *name;
Beniamino Galvani677b5352016-08-16 11:49:49 +0200350
Simon Glassa2703ce2020-11-28 17:50:03 -0700351 /* FIXME: Should use livetree */
Simon Glasse160f7d2017-01-17 16:52:55 -0700352 na = fdt_address_cells(gd->fdt_blob, dev_of_offset(dev->parent));
Beniamino Galvani677b5352016-08-16 11:49:49 +0200353 if (na < 1) {
354 debug("bad #address-cells\n");
355 return -EINVAL;
356 }
357
Simon Glasse160f7d2017-01-17 16:52:55 -0700358 ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
Beniamino Galvani677b5352016-08-16 11:49:49 +0200359 if (ns < 1) {
360 debug("bad #size-cells\n");
361 return -EINVAL;
362 }
363
Simon Glasse160f7d2017-01-17 16:52:55 -0700364 fdt_for_each_subnode(node, gd->fdt_blob, dev_of_offset(dev)) {
Beniamino Galvani677b5352016-08-16 11:49:49 +0200365 if (fdt_getprop(gd->fdt_blob, node, "gpio-controller", &len)) {
366 gpio = node;
367 break;
368 }
369 }
370
371 if (!gpio) {
372 debug("gpio node not found\n");
373 return -EINVAL;
374 }
375
376 addr = parse_address(gpio, "mux", na, ns);
377 if (addr == FDT_ADDR_T_NONE) {
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200378 debug("mux address not found\n");
Beniamino Galvani677b5352016-08-16 11:49:49 +0200379 return -EINVAL;
380 }
Beniamino Galvani677b5352016-08-16 11:49:49 +0200381 priv->reg_mux = (void __iomem *)addr;
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200382
383 addr = parse_address(gpio, "gpio", na, ns);
384 if (addr == FDT_ADDR_T_NONE) {
385 debug("gpio address not found\n");
386 return -EINVAL;
387 }
388 priv->reg_gpio = (void __iomem *)addr;
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100389
390 addr = parse_address(gpio, "pull", na, ns);
Jerome Brunetf91121c2019-02-08 17:40:57 +0100391 /* Use gpio region if pull one is not present */
392 if (addr == FDT_ADDR_T_NONE)
393 priv->reg_pull = priv->reg_gpio;
394 else
395 priv->reg_pull = (void __iomem *)addr;
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100396
397 addr = parse_address(gpio, "pull-enable", na, ns);
398 /* Use pull region if pull-enable one is not present */
399 if (addr == FDT_ADDR_T_NONE)
400 priv->reg_pullen = priv->reg_pull;
401 else
402 priv->reg_pullen = (void __iomem *)addr;
403
Jerome Brunetf91121c2019-02-08 17:40:57 +0100404 addr = parse_address(gpio, "ds", na, ns);
405 /* Drive strength region is optional */
406 if (addr == FDT_ADDR_T_NONE)
407 priv->reg_ds = NULL;
408 else
409 priv->reg_ds = (void __iomem *)addr;
410
Beniamino Galvani677b5352016-08-16 11:49:49 +0200411 priv->data = (struct meson_pinctrl_data *)dev_get_driver_data(dev);
412
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200413 /* Lookup GPIO driver */
414 drv = lists_uclass_lookup(UCLASS_GPIO);
415 if (!drv) {
416 puts("Cannot find GPIO driver\n");
417 return -ENOENT;
418 }
419
420 name = calloc(1, 32);
421 sprintf(name, "meson-gpio");
422
423 /* Create child device UCLASS_GPIO and bind it */
Simon Glassa2703ce2020-11-28 17:50:03 -0700424 device_bind(dev, priv->data->gpio_driver, name, NULL,
425 offset_to_ofnode(gpio), &gpio_dev);
Beniamino Galvani2009a8d2017-07-10 00:30:04 +0200426
Beniamino Galvani677b5352016-08-16 11:49:49 +0200427 return 0;
428}