blob: 2af5587ec47a3e635c1d1218176f138af0b898f9 [file] [log] [blame]
Gregory CLEMENT2a626432018-12-08 09:59:01 +01001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/*
3 * Microsemi SoCs pinctrl driver
4 *
5 * Author: <alexandre.belloni@free-electrons.com>
6 * Author: <gregory.clement@bootlin.com>
7 * License: Dual MIT/GPL
8 * Copyright (c) 2017 Microsemi Corporation
9 */
10
11#include <asm/gpio.h>
12#include <asm/system.h>
Gregory CLEMENT2a626432018-12-08 09:59:01 +010013#include <config.h>
14#include <dm.h>
15#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <dm/devres.h>
Gregory CLEMENT2a626432018-12-08 09:59:01 +010018#include <dm/lists.h>
19#include <dm/pinctrl.h>
20#include <dm/root.h>
21#include <errno.h>
22#include <fdtdec.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060023#include <linux/bitops.h>
Gregory CLEMENT2a626432018-12-08 09:59:01 +010024#include <linux/io.h>
25#include "mscc-common.h"
26
Horatiu Vultur2d4fb842019-01-12 18:56:55 +010027static void mscc_writel(unsigned int offset, void *addr)
28{
29 if (offset < 32)
30 writel(BIT(offset), addr);
31 else
32 writel(BIT(offset % 32), addr + 4);
33}
34
35static unsigned int mscc_readl(unsigned int offset, void *addr)
36{
37 if (offset < 32)
38 return readl(addr);
39 else
40 return readl(addr + 4);
41}
42
43static void mscc_setbits(unsigned int offset, void *addr)
44{
45 if (offset < 32)
46 writel(readl(addr) | BIT(offset), addr);
47 else
48 writel(readl(addr + 4) | BIT(offset % 32), addr + 4);
49}
50
51static void mscc_clrbits(unsigned int offset, void *addr)
52{
53 if (offset < 32)
54 writel(readl(addr) & ~BIT(offset), addr);
55 else
56 writel(readl(addr + 4) & ~BIT(offset % 32), addr + 4);
57}
Gregory CLEMENT2a626432018-12-08 09:59:01 +010058
59static int mscc_get_functions_count(struct udevice *dev)
60{
61 struct mscc_pinctrl *info = dev_get_priv(dev);
62
63 return info->num_func;
64}
65
66static const char *mscc_get_function_name(struct udevice *dev,
67 unsigned int function)
68{
69 struct mscc_pinctrl *info = dev_get_priv(dev);
70
71 return info->function_names[function];
72}
73
74static int mscc_pin_function_idx(unsigned int pin, unsigned int function,
75 const struct mscc_pin_data *mscc_pins)
76{
77 struct mscc_pin_caps *p = mscc_pins[pin].drv_data;
78 int i;
79
80 for (i = 0; i < MSCC_FUNC_PER_PIN; i++) {
81 if (function == p->functions[i])
82 return i;
83 }
84
85 return -1;
86}
87
88static int mscc_pinmux_set_mux(struct udevice *dev,
89 unsigned int pin_selector, unsigned int selector)
90{
91 struct mscc_pinctrl *info = dev_get_priv(dev);
92 struct mscc_pin_caps *pin = info->mscc_pins[pin_selector].drv_data;
Horatiu Vultur2d4fb842019-01-12 18:56:55 +010093 int f, offset, regoff;
Gregory CLEMENT2a626432018-12-08 09:59:01 +010094
95 f = mscc_pin_function_idx(pin_selector, selector, info->mscc_pins);
96 if (f < 0)
97 return -EINVAL;
98 /*
99 * f is encoded on two bits.
100 * bit 0 of f goes in BIT(pin) of ALT0, bit 1 of f goes in BIT(pin) of
101 * ALT1
102 * This is racy because both registers can't be updated at the same time
103 * but it doesn't matter much for now.
104 */
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100105 offset = pin->pin;
106 regoff = info->mscc_gpios[MSCC_GPIO_ALT0];
107 if (offset >= 32) {
108 offset = offset % 32;
109 regoff = info->mscc_gpios[MSCC_GPIO_ALT1];
110 }
111
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100112 if (f & BIT(0))
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100113 mscc_setbits(offset, info->regs + regoff);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100114 else
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100115 mscc_clrbits(offset, info->regs + regoff);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100116
117 if (f & BIT(1))
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100118 mscc_setbits(offset, info->regs + regoff + 4);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100119 else
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100120 mscc_clrbits(offset, info->regs + regoff + 4);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100121
122 return 0;
123}
124
125static int mscc_pctl_get_groups_count(struct udevice *dev)
126{
127 struct mscc_pinctrl *info = dev_get_priv(dev);
128
129 return info->num_pins;
130}
131
132static const char *mscc_pctl_get_group_name(struct udevice *dev,
133 unsigned int group)
134{
135 struct mscc_pinctrl *info = dev_get_priv(dev);
136
137 return info->mscc_pins[group].name;
138}
139
140static int mscc_create_group_func_map(struct udevice *dev,
141 struct mscc_pinctrl *info)
142{
143 u16 pins[info->num_pins];
144 int f, npins, i;
145
146 for (f = 0; f < info->num_func; f++) {
147 for (npins = 0, i = 0; i < info->num_pins; i++) {
148 if (mscc_pin_function_idx(i, f, info->mscc_pins) >= 0)
149 pins[npins++] = i;
150 }
151
152 info->func[f].ngroups = npins;
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100153 info->func[f].groups = devm_kzalloc(dev, npins * sizeof(char *),
154 GFP_KERNEL);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100155 if (!info->func[f].groups)
156 return -ENOMEM;
157
158 for (i = 0; i < npins; i++)
159 info->func[f].groups[i] = info->mscc_pins[pins[i]].name;
160 }
161
162 return 0;
163}
164
165static int mscc_pinctrl_register(struct udevice *dev, struct mscc_pinctrl *info)
166{
167 int ret;
168
169 ret = mscc_create_group_func_map(dev, info);
170 if (ret) {
171 dev_err(dev, "Unable to create group func map.\n");
172 return ret;
173 }
174
175 return 0;
176}
177
178static int mscc_gpio_get(struct udevice *dev, unsigned int offset)
179{
180 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
181 unsigned int val;
182
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100183 if (mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]) &
184 BIT(offset % 32))
185 val = mscc_readl(offset,
186 info->regs + info->mscc_gpios[MSCC_GPIO_OUT]);
187 else
188 val = mscc_readl(offset,
189 info->regs + info->mscc_gpios[MSCC_GPIO_IN]);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100190
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100191 return !!(val & BIT(offset % 32));
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100192}
193
194static int mscc_gpio_set(struct udevice *dev, unsigned int offset, int value)
195{
196 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
197
198 if (value)
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100199 mscc_writel(offset,
200 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_SET]);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100201 else
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100202 mscc_writel(offset,
203 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_CLR]);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100204
205 return 0;
206}
207
208static int mscc_gpio_get_direction(struct udevice *dev, unsigned int offset)
209{
210 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
211 unsigned int val;
212
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100213 val = mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100214
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100215 return (val & BIT(offset % 32)) ? GPIOF_OUTPUT : GPIOF_INPUT;
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100216}
217
218static int mscc_gpio_direction_input(struct udevice *dev, unsigned int offset)
219{
220 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
221
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100222 mscc_clrbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100223
224 return 0;
225}
226
227static int mscc_gpio_direction_output(struct udevice *dev,
228 unsigned int offset, int value)
229{
230 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
231
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100232 mscc_setbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100233
234 return mscc_gpio_set(dev, offset, value);
235}
236
237const struct dm_gpio_ops mscc_gpio_ops = {
238 .set_value = mscc_gpio_set,
239 .get_value = mscc_gpio_get,
240 .get_function = mscc_gpio_get_direction,
241 .direction_input = mscc_gpio_direction_input,
242 .direction_output = mscc_gpio_direction_output,
243};
244
245const struct pinctrl_ops mscc_pinctrl_ops = {
246 .get_pins_count = mscc_pctl_get_groups_count,
247 .get_pin_name = mscc_pctl_get_group_name,
248 .get_functions_count = mscc_get_functions_count,
249 .get_function_name = mscc_get_function_name,
250 .pinmux_set = mscc_pinmux_set_mux,
251 .set_state = pinctrl_generic_set_state,
252};
253
254int mscc_pinctrl_probe(struct udevice *dev, int num_func,
255 const struct mscc_pin_data *mscc_pins, int num_pins,
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100256 char * const *function_names,
257 const unsigned long *mscc_gpios)
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100258{
259 struct mscc_pinctrl *priv = dev_get_priv(dev);
260 int ret;
261
262 priv->regs = dev_remap_addr(dev);
263 if (!priv->regs)
264 return -EINVAL;
265
266 priv->func = devm_kzalloc(dev, num_func * sizeof(struct mscc_pmx_func),
267 GFP_KERNEL);
268 priv->num_func = num_func;
269 priv->mscc_pins = mscc_pins;
270 priv->num_pins = num_pins;
271 priv->function_names = function_names;
Horatiu Vultur2d4fb842019-01-12 18:56:55 +0100272 priv->mscc_gpios = mscc_gpios;
Gregory CLEMENT2a626432018-12-08 09:59:01 +0100273 ret = mscc_pinctrl_register(dev, priv);
274
275 return ret;
276}