183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
2910df4d0SMarek Vasut /*
3910df4d0SMarek Vasut * Pin Control driver for SuperH Pin Function Controller.
4910df4d0SMarek Vasut *
5910df4d0SMarek Vasut * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
6910df4d0SMarek Vasut *
7910df4d0SMarek Vasut * Copyright (C) 2008 Magnus Damm
8910df4d0SMarek Vasut * Copyright (C) 2009 - 2012 Paul Mundt
9910df4d0SMarek Vasut * Copyright (C) 2017 Marek Vasut
10910df4d0SMarek Vasut */
11910df4d0SMarek Vasut
12910df4d0SMarek Vasut #define DRV_NAME "sh-pfc"
13910df4d0SMarek Vasut
14910df4d0SMarek Vasut #include <common.h>
15910df4d0SMarek Vasut #include <dm.h>
16910df4d0SMarek Vasut #include <errno.h>
17910df4d0SMarek Vasut #include <dm/pinctrl.h>
18910df4d0SMarek Vasut #include <linux/io.h>
19910df4d0SMarek Vasut #include <linux/sizes.h>
20910df4d0SMarek Vasut
21910df4d0SMarek Vasut #include "sh_pfc.h"
22910df4d0SMarek Vasut
23910df4d0SMarek Vasut enum sh_pfc_model {
247547ad4cSMarek Vasut SH_PFC_R8A7790 = 0,
25427c75dfSMarek Vasut SH_PFC_R8A7791,
26ab2d09b4SMarek Vasut SH_PFC_R8A7792,
27427c75dfSMarek Vasut SH_PFC_R8A7793,
2834e93605SMarek Vasut SH_PFC_R8A7794,
297547ad4cSMarek Vasut SH_PFC_R8A7795,
30910df4d0SMarek Vasut SH_PFC_R8A7796,
31c106bb53SMarek Vasut SH_PFC_R8A77970,
32cb13e46aSMarek Vasut SH_PFC_R8A77990,
33a59e6976SMarek Vasut SH_PFC_R8A77995,
34910df4d0SMarek Vasut };
35910df4d0SMarek Vasut
36910df4d0SMarek Vasut struct sh_pfc_pin_config {
37910df4d0SMarek Vasut u32 type;
38910df4d0SMarek Vasut };
39910df4d0SMarek Vasut
40910df4d0SMarek Vasut struct sh_pfc_pinctrl {
41910df4d0SMarek Vasut struct sh_pfc *pfc;
42910df4d0SMarek Vasut
43910df4d0SMarek Vasut struct sh_pfc_pin_config *configs;
44910df4d0SMarek Vasut
45910df4d0SMarek Vasut const char *func_prop_name;
46910df4d0SMarek Vasut const char *groups_prop_name;
47910df4d0SMarek Vasut const char *pins_prop_name;
48910df4d0SMarek Vasut };
49910df4d0SMarek Vasut
50910df4d0SMarek Vasut struct sh_pfc_pin_range {
51910df4d0SMarek Vasut u16 start;
52910df4d0SMarek Vasut u16 end;
53910df4d0SMarek Vasut };
54910df4d0SMarek Vasut
55910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv {
56910df4d0SMarek Vasut struct sh_pfc pfc;
57910df4d0SMarek Vasut struct sh_pfc_pinctrl pmx;
58910df4d0SMarek Vasut };
59910df4d0SMarek Vasut
sh_pfc_get_pin_index(struct sh_pfc * pfc,unsigned int pin)60910df4d0SMarek Vasut int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
61910df4d0SMarek Vasut {
62910df4d0SMarek Vasut unsigned int offset;
63910df4d0SMarek Vasut unsigned int i;
64910df4d0SMarek Vasut
65910df4d0SMarek Vasut for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
66910df4d0SMarek Vasut const struct sh_pfc_pin_range *range = &pfc->ranges[i];
67910df4d0SMarek Vasut
68910df4d0SMarek Vasut if (pin <= range->end)
69910df4d0SMarek Vasut return pin >= range->start
70910df4d0SMarek Vasut ? offset + pin - range->start : -1;
71910df4d0SMarek Vasut
72910df4d0SMarek Vasut offset += range->end - range->start + 1;
73910df4d0SMarek Vasut }
74910df4d0SMarek Vasut
75910df4d0SMarek Vasut return -EINVAL;
76910df4d0SMarek Vasut }
77910df4d0SMarek Vasut
sh_pfc_enum_in_range(u16 enum_id,const struct pinmux_range * r)78910df4d0SMarek Vasut static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
79910df4d0SMarek Vasut {
80910df4d0SMarek Vasut if (enum_id < r->begin)
81910df4d0SMarek Vasut return 0;
82910df4d0SMarek Vasut
83910df4d0SMarek Vasut if (enum_id > r->end)
84910df4d0SMarek Vasut return 0;
85910df4d0SMarek Vasut
86910df4d0SMarek Vasut return 1;
87910df4d0SMarek Vasut }
88910df4d0SMarek Vasut
sh_pfc_read_raw_reg(void __iomem * mapped_reg,unsigned int reg_width)89910df4d0SMarek Vasut u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
90910df4d0SMarek Vasut {
91910df4d0SMarek Vasut switch (reg_width) {
92910df4d0SMarek Vasut case 8:
93910df4d0SMarek Vasut return readb(mapped_reg);
94910df4d0SMarek Vasut case 16:
95910df4d0SMarek Vasut return readw(mapped_reg);
96910df4d0SMarek Vasut case 32:
97910df4d0SMarek Vasut return readl(mapped_reg);
98910df4d0SMarek Vasut }
99910df4d0SMarek Vasut
100910df4d0SMarek Vasut BUG();
101910df4d0SMarek Vasut return 0;
102910df4d0SMarek Vasut }
103910df4d0SMarek Vasut
sh_pfc_write_raw_reg(void __iomem * mapped_reg,unsigned int reg_width,u32 data)104910df4d0SMarek Vasut void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
105910df4d0SMarek Vasut u32 data)
106910df4d0SMarek Vasut {
107910df4d0SMarek Vasut switch (reg_width) {
108910df4d0SMarek Vasut case 8:
109910df4d0SMarek Vasut writeb(data, mapped_reg);
110910df4d0SMarek Vasut return;
111910df4d0SMarek Vasut case 16:
112910df4d0SMarek Vasut writew(data, mapped_reg);
113910df4d0SMarek Vasut return;
114910df4d0SMarek Vasut case 32:
115910df4d0SMarek Vasut writel(data, mapped_reg);
116910df4d0SMarek Vasut return;
117910df4d0SMarek Vasut }
118910df4d0SMarek Vasut
119910df4d0SMarek Vasut BUG();
120910df4d0SMarek Vasut }
121910df4d0SMarek Vasut
sh_pfc_read(struct sh_pfc * pfc,u32 reg)122bf8d2dabSMarek Vasut u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
123910df4d0SMarek Vasut {
1245af65419SMarek Vasut return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32);
125910df4d0SMarek Vasut }
126910df4d0SMarek Vasut
sh_pfc_write(struct sh_pfc * pfc,u32 reg,u32 data)127bf8d2dabSMarek Vasut void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
128910df4d0SMarek Vasut {
129910df4d0SMarek Vasut void __iomem *unlock_reg =
130910df4d0SMarek Vasut (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
131910df4d0SMarek Vasut
132910df4d0SMarek Vasut if (pfc->info->unlock_reg)
133910df4d0SMarek Vasut sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
134910df4d0SMarek Vasut
1355af65419SMarek Vasut sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)reg, 32, data);
136910df4d0SMarek Vasut }
137910df4d0SMarek Vasut
sh_pfc_config_reg_helper(struct sh_pfc * pfc,const struct pinmux_cfg_reg * crp,unsigned int in_pos,void __iomem ** mapped_regp,u32 * maskp,unsigned int * posp)138910df4d0SMarek Vasut static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
139910df4d0SMarek Vasut const struct pinmux_cfg_reg *crp,
140910df4d0SMarek Vasut unsigned int in_pos,
141910df4d0SMarek Vasut void __iomem **mapped_regp, u32 *maskp,
142910df4d0SMarek Vasut unsigned int *posp)
143910df4d0SMarek Vasut {
144910df4d0SMarek Vasut unsigned int k;
145910df4d0SMarek Vasut
146910df4d0SMarek Vasut *mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
147910df4d0SMarek Vasut
148910df4d0SMarek Vasut if (crp->field_width) {
149910df4d0SMarek Vasut *maskp = (1 << crp->field_width) - 1;
150910df4d0SMarek Vasut *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
151910df4d0SMarek Vasut } else {
152910df4d0SMarek Vasut *maskp = (1 << crp->var_field_width[in_pos]) - 1;
153910df4d0SMarek Vasut *posp = crp->reg_width;
154910df4d0SMarek Vasut for (k = 0; k <= in_pos; k++)
155910df4d0SMarek Vasut *posp -= crp->var_field_width[k];
156910df4d0SMarek Vasut }
157910df4d0SMarek Vasut }
158910df4d0SMarek Vasut
sh_pfc_write_config_reg(struct sh_pfc * pfc,const struct pinmux_cfg_reg * crp,unsigned int field,u32 value)159910df4d0SMarek Vasut static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
160910df4d0SMarek Vasut const struct pinmux_cfg_reg *crp,
161910df4d0SMarek Vasut unsigned int field, u32 value)
162910df4d0SMarek Vasut {
163910df4d0SMarek Vasut void __iomem *mapped_reg;
164910df4d0SMarek Vasut void __iomem *unlock_reg =
165910df4d0SMarek Vasut (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
166910df4d0SMarek Vasut unsigned int pos;
167910df4d0SMarek Vasut u32 mask, data;
168910df4d0SMarek Vasut
169910df4d0SMarek Vasut sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
170910df4d0SMarek Vasut
171910df4d0SMarek Vasut dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
172910df4d0SMarek Vasut "r_width = %u, f_width = %u\n",
173910df4d0SMarek Vasut crp->reg, value, field, crp->reg_width, crp->field_width);
174910df4d0SMarek Vasut
175910df4d0SMarek Vasut mask = ~(mask << pos);
176910df4d0SMarek Vasut value = value << pos;
177910df4d0SMarek Vasut
178910df4d0SMarek Vasut data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
179910df4d0SMarek Vasut data &= mask;
180910df4d0SMarek Vasut data |= value;
181910df4d0SMarek Vasut
182910df4d0SMarek Vasut if (pfc->info->unlock_reg)
183910df4d0SMarek Vasut sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
184910df4d0SMarek Vasut
185910df4d0SMarek Vasut sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
186910df4d0SMarek Vasut }
187910df4d0SMarek Vasut
sh_pfc_get_config_reg(struct sh_pfc * pfc,u16 enum_id,const struct pinmux_cfg_reg ** crp,unsigned int * fieldp,u32 * valuep)188910df4d0SMarek Vasut static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
189910df4d0SMarek Vasut const struct pinmux_cfg_reg **crp,
190910df4d0SMarek Vasut unsigned int *fieldp, u32 *valuep)
191910df4d0SMarek Vasut {
192910df4d0SMarek Vasut unsigned int k = 0;
193910df4d0SMarek Vasut
194910df4d0SMarek Vasut while (1) {
195910df4d0SMarek Vasut const struct pinmux_cfg_reg *config_reg =
196910df4d0SMarek Vasut pfc->info->cfg_regs + k;
197910df4d0SMarek Vasut unsigned int r_width = config_reg->reg_width;
198910df4d0SMarek Vasut unsigned int f_width = config_reg->field_width;
199910df4d0SMarek Vasut unsigned int curr_width;
200910df4d0SMarek Vasut unsigned int bit_pos;
201910df4d0SMarek Vasut unsigned int pos = 0;
202910df4d0SMarek Vasut unsigned int m = 0;
203910df4d0SMarek Vasut
204910df4d0SMarek Vasut if (!r_width)
205910df4d0SMarek Vasut break;
206910df4d0SMarek Vasut
207910df4d0SMarek Vasut for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
208910df4d0SMarek Vasut u32 ncomb;
209910df4d0SMarek Vasut u32 n;
210910df4d0SMarek Vasut
211910df4d0SMarek Vasut if (f_width)
212910df4d0SMarek Vasut curr_width = f_width;
213910df4d0SMarek Vasut else
214910df4d0SMarek Vasut curr_width = config_reg->var_field_width[m];
215910df4d0SMarek Vasut
216910df4d0SMarek Vasut ncomb = 1 << curr_width;
217910df4d0SMarek Vasut for (n = 0; n < ncomb; n++) {
218910df4d0SMarek Vasut if (config_reg->enum_ids[pos + n] == enum_id) {
219910df4d0SMarek Vasut *crp = config_reg;
220910df4d0SMarek Vasut *fieldp = m;
221910df4d0SMarek Vasut *valuep = n;
222910df4d0SMarek Vasut return 0;
223910df4d0SMarek Vasut }
224910df4d0SMarek Vasut }
225910df4d0SMarek Vasut pos += ncomb;
226910df4d0SMarek Vasut m++;
227910df4d0SMarek Vasut }
228910df4d0SMarek Vasut k++;
229910df4d0SMarek Vasut }
230910df4d0SMarek Vasut
231910df4d0SMarek Vasut return -EINVAL;
232910df4d0SMarek Vasut }
233910df4d0SMarek Vasut
sh_pfc_mark_to_enum(struct sh_pfc * pfc,u16 mark,int pos,u16 * enum_idp)234910df4d0SMarek Vasut static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
235910df4d0SMarek Vasut u16 *enum_idp)
236910df4d0SMarek Vasut {
237910df4d0SMarek Vasut const u16 *data = pfc->info->pinmux_data;
238910df4d0SMarek Vasut unsigned int k;
239910df4d0SMarek Vasut
240910df4d0SMarek Vasut if (pos) {
241910df4d0SMarek Vasut *enum_idp = data[pos + 1];
242910df4d0SMarek Vasut return pos + 1;
243910df4d0SMarek Vasut }
244910df4d0SMarek Vasut
245910df4d0SMarek Vasut for (k = 0; k < pfc->info->pinmux_data_size; k++) {
246910df4d0SMarek Vasut if (data[k] == mark) {
247910df4d0SMarek Vasut *enum_idp = data[k + 1];
248910df4d0SMarek Vasut return k + 1;
249910df4d0SMarek Vasut }
250910df4d0SMarek Vasut }
251910df4d0SMarek Vasut
252910df4d0SMarek Vasut dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
253910df4d0SMarek Vasut mark);
254910df4d0SMarek Vasut return -EINVAL;
255910df4d0SMarek Vasut }
256910df4d0SMarek Vasut
sh_pfc_config_mux(struct sh_pfc * pfc,unsigned mark,int pinmux_type)257910df4d0SMarek Vasut int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
258910df4d0SMarek Vasut {
259910df4d0SMarek Vasut const struct pinmux_range *range;
260910df4d0SMarek Vasut int pos = 0;
261910df4d0SMarek Vasut
262910df4d0SMarek Vasut switch (pinmux_type) {
263910df4d0SMarek Vasut case PINMUX_TYPE_GPIO:
264910df4d0SMarek Vasut case PINMUX_TYPE_FUNCTION:
265910df4d0SMarek Vasut range = NULL;
266910df4d0SMarek Vasut break;
267910df4d0SMarek Vasut
268910df4d0SMarek Vasut case PINMUX_TYPE_OUTPUT:
269910df4d0SMarek Vasut range = &pfc->info->output;
270910df4d0SMarek Vasut break;
271910df4d0SMarek Vasut
272910df4d0SMarek Vasut case PINMUX_TYPE_INPUT:
273910df4d0SMarek Vasut range = &pfc->info->input;
274910df4d0SMarek Vasut break;
275910df4d0SMarek Vasut
276910df4d0SMarek Vasut default:
277910df4d0SMarek Vasut return -EINVAL;
278910df4d0SMarek Vasut }
279910df4d0SMarek Vasut
280910df4d0SMarek Vasut /* Iterate over all the configuration fields we need to update. */
281910df4d0SMarek Vasut while (1) {
282910df4d0SMarek Vasut const struct pinmux_cfg_reg *cr;
283910df4d0SMarek Vasut unsigned int field;
284910df4d0SMarek Vasut u16 enum_id;
285910df4d0SMarek Vasut u32 value;
286910df4d0SMarek Vasut int in_range;
287910df4d0SMarek Vasut int ret;
288910df4d0SMarek Vasut
289910df4d0SMarek Vasut pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
290910df4d0SMarek Vasut if (pos < 0)
291910df4d0SMarek Vasut return pos;
292910df4d0SMarek Vasut
293910df4d0SMarek Vasut if (!enum_id)
294910df4d0SMarek Vasut break;
295910df4d0SMarek Vasut
296910df4d0SMarek Vasut /* Check if the configuration field selects a function. If it
297910df4d0SMarek Vasut * doesn't, skip the field if it's not applicable to the
298910df4d0SMarek Vasut * requested pinmux type.
299910df4d0SMarek Vasut */
300910df4d0SMarek Vasut in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
301910df4d0SMarek Vasut if (!in_range) {
302910df4d0SMarek Vasut if (pinmux_type == PINMUX_TYPE_FUNCTION) {
303910df4d0SMarek Vasut /* Functions are allowed to modify all
304910df4d0SMarek Vasut * fields.
305910df4d0SMarek Vasut */
306910df4d0SMarek Vasut in_range = 1;
307910df4d0SMarek Vasut } else if (pinmux_type != PINMUX_TYPE_GPIO) {
308910df4d0SMarek Vasut /* Input/output types can only modify fields
309910df4d0SMarek Vasut * that correspond to their respective ranges.
310910df4d0SMarek Vasut */
311910df4d0SMarek Vasut in_range = sh_pfc_enum_in_range(enum_id, range);
312910df4d0SMarek Vasut
313910df4d0SMarek Vasut /*
314910df4d0SMarek Vasut * special case pass through for fixed
315910df4d0SMarek Vasut * input-only or output-only pins without
316910df4d0SMarek Vasut * function enum register association.
317910df4d0SMarek Vasut */
318910df4d0SMarek Vasut if (in_range && enum_id == range->force)
319910df4d0SMarek Vasut continue;
320910df4d0SMarek Vasut }
321910df4d0SMarek Vasut /* GPIOs are only allowed to modify function fields. */
322910df4d0SMarek Vasut }
323910df4d0SMarek Vasut
324910df4d0SMarek Vasut if (!in_range)
325910df4d0SMarek Vasut continue;
326910df4d0SMarek Vasut
327910df4d0SMarek Vasut ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
328910df4d0SMarek Vasut if (ret < 0)
329910df4d0SMarek Vasut return ret;
330910df4d0SMarek Vasut
331910df4d0SMarek Vasut sh_pfc_write_config_reg(pfc, cr, field, value);
332910df4d0SMarek Vasut }
333910df4d0SMarek Vasut
334910df4d0SMarek Vasut return 0;
335910df4d0SMarek Vasut }
336910df4d0SMarek Vasut
337bf8d2dabSMarek Vasut const struct pinmux_bias_reg *
sh_pfc_pin_to_bias_reg(const struct sh_pfc * pfc,unsigned int pin,unsigned int * bit)338bf8d2dabSMarek Vasut sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin,
339bf8d2dabSMarek Vasut unsigned int *bit)
340910df4d0SMarek Vasut {
341bf8d2dabSMarek Vasut unsigned int i, j;
342910df4d0SMarek Vasut
343bf8d2dabSMarek Vasut for (i = 0; pfc->info->bias_regs[i].puen; i++) {
344bf8d2dabSMarek Vasut for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) {
345bf8d2dabSMarek Vasut if (pfc->info->bias_regs[i].pins[j] == pin) {
346bf8d2dabSMarek Vasut *bit = j;
347bf8d2dabSMarek Vasut return &pfc->info->bias_regs[i];
348bf8d2dabSMarek Vasut }
349bf8d2dabSMarek Vasut }
350bf8d2dabSMarek Vasut }
351910df4d0SMarek Vasut
352bf8d2dabSMarek Vasut WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
353910df4d0SMarek Vasut
354910df4d0SMarek Vasut return NULL;
355910df4d0SMarek Vasut }
356910df4d0SMarek Vasut
sh_pfc_init_ranges(struct sh_pfc * pfc)357910df4d0SMarek Vasut static int sh_pfc_init_ranges(struct sh_pfc *pfc)
358910df4d0SMarek Vasut {
359910df4d0SMarek Vasut struct sh_pfc_pin_range *range;
360910df4d0SMarek Vasut unsigned int nr_ranges;
361910df4d0SMarek Vasut unsigned int i;
362910df4d0SMarek Vasut
363910df4d0SMarek Vasut if (pfc->info->pins[0].pin == (u16)-1) {
364910df4d0SMarek Vasut /* Pin number -1 denotes that the SoC doesn't report pin numbers
365910df4d0SMarek Vasut * in its pin arrays yet. Consider the pin numbers range as
366910df4d0SMarek Vasut * continuous and allocate a single range.
367910df4d0SMarek Vasut */
368910df4d0SMarek Vasut pfc->nr_ranges = 1;
369910df4d0SMarek Vasut pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
370910df4d0SMarek Vasut if (pfc->ranges == NULL)
371910df4d0SMarek Vasut return -ENOMEM;
372910df4d0SMarek Vasut
373910df4d0SMarek Vasut pfc->ranges->start = 0;
374910df4d0SMarek Vasut pfc->ranges->end = pfc->info->nr_pins - 1;
375910df4d0SMarek Vasut pfc->nr_gpio_pins = pfc->info->nr_pins;
376910df4d0SMarek Vasut
377910df4d0SMarek Vasut return 0;
378910df4d0SMarek Vasut }
379910df4d0SMarek Vasut
380910df4d0SMarek Vasut /* Count, allocate and fill the ranges. The PFC SoC data pins array must
381910df4d0SMarek Vasut * be sorted by pin numbers, and pins without a GPIO port must come
382910df4d0SMarek Vasut * last.
383910df4d0SMarek Vasut */
384910df4d0SMarek Vasut for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
385910df4d0SMarek Vasut if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
386910df4d0SMarek Vasut nr_ranges++;
387910df4d0SMarek Vasut }
388910df4d0SMarek Vasut
389910df4d0SMarek Vasut pfc->nr_ranges = nr_ranges;
390910df4d0SMarek Vasut pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
391910df4d0SMarek Vasut if (pfc->ranges == NULL)
392910df4d0SMarek Vasut return -ENOMEM;
393910df4d0SMarek Vasut
394910df4d0SMarek Vasut range = pfc->ranges;
395910df4d0SMarek Vasut range->start = pfc->info->pins[0].pin;
396910df4d0SMarek Vasut
397910df4d0SMarek Vasut for (i = 1; i < pfc->info->nr_pins; ++i) {
398910df4d0SMarek Vasut if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
399910df4d0SMarek Vasut continue;
400910df4d0SMarek Vasut
401910df4d0SMarek Vasut range->end = pfc->info->pins[i-1].pin;
402910df4d0SMarek Vasut if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
403910df4d0SMarek Vasut pfc->nr_gpio_pins = range->end + 1;
404910df4d0SMarek Vasut
405910df4d0SMarek Vasut range++;
406910df4d0SMarek Vasut range->start = pfc->info->pins[i].pin;
407910df4d0SMarek Vasut }
408910df4d0SMarek Vasut
409910df4d0SMarek Vasut range->end = pfc->info->pins[i-1].pin;
410910df4d0SMarek Vasut if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
411910df4d0SMarek Vasut pfc->nr_gpio_pins = range->end + 1;
412910df4d0SMarek Vasut
413910df4d0SMarek Vasut return 0;
414910df4d0SMarek Vasut }
415910df4d0SMarek Vasut
sh_pfc_pinctrl_get_pins_count(struct udevice * dev)416910df4d0SMarek Vasut static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
417910df4d0SMarek Vasut {
418910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
419910df4d0SMarek Vasut
420910df4d0SMarek Vasut return priv->pfc.info->nr_pins;
421910df4d0SMarek Vasut }
422910df4d0SMarek Vasut
sh_pfc_pinctrl_get_pin_name(struct udevice * dev,unsigned selector)423910df4d0SMarek Vasut static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
424910df4d0SMarek Vasut unsigned selector)
425910df4d0SMarek Vasut {
426910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
427910df4d0SMarek Vasut
428910df4d0SMarek Vasut return priv->pfc.info->pins[selector].name;
429910df4d0SMarek Vasut }
430910df4d0SMarek Vasut
sh_pfc_pinctrl_get_groups_count(struct udevice * dev)431910df4d0SMarek Vasut static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
432910df4d0SMarek Vasut {
433910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
434910df4d0SMarek Vasut
435910df4d0SMarek Vasut return priv->pfc.info->nr_groups;
436910df4d0SMarek Vasut }
437910df4d0SMarek Vasut
sh_pfc_pinctrl_get_group_name(struct udevice * dev,unsigned selector)438910df4d0SMarek Vasut static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
439910df4d0SMarek Vasut unsigned selector)
440910df4d0SMarek Vasut {
441910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
442910df4d0SMarek Vasut
443910df4d0SMarek Vasut return priv->pfc.info->groups[selector].name;
444910df4d0SMarek Vasut }
445910df4d0SMarek Vasut
sh_pfc_pinctrl_get_functions_count(struct udevice * dev)446910df4d0SMarek Vasut static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
447910df4d0SMarek Vasut {
448910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
449910df4d0SMarek Vasut
450910df4d0SMarek Vasut return priv->pfc.info->nr_functions;
451910df4d0SMarek Vasut }
452910df4d0SMarek Vasut
sh_pfc_pinctrl_get_function_name(struct udevice * dev,unsigned selector)453910df4d0SMarek Vasut static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
454910df4d0SMarek Vasut unsigned selector)
455910df4d0SMarek Vasut {
456910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
457910df4d0SMarek Vasut
458910df4d0SMarek Vasut return priv->pfc.info->functions[selector].name;
459910df4d0SMarek Vasut }
460910df4d0SMarek Vasut
sh_pfc_config_mux_for_gpio(struct udevice * dev,unsigned pin_selector)461f6e545a7SMarek Vasut int sh_pfc_config_mux_for_gpio(struct udevice *dev, unsigned pin_selector)
462f6e545a7SMarek Vasut {
463f6e545a7SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
464f6e545a7SMarek Vasut struct sh_pfc_pinctrl *pmx = &priv->pmx;
465f6e545a7SMarek Vasut struct sh_pfc *pfc = &priv->pfc;
466f6e545a7SMarek Vasut struct sh_pfc_pin_config *cfg;
467f6e545a7SMarek Vasut const struct sh_pfc_pin *pin = NULL;
468f6e545a7SMarek Vasut int i, idx;
469f6e545a7SMarek Vasut
470f6e545a7SMarek Vasut for (i = 1; i < pfc->info->nr_pins; i++) {
471f6e545a7SMarek Vasut if (priv->pfc.info->pins[i].pin != pin_selector)
472f6e545a7SMarek Vasut continue;
473f6e545a7SMarek Vasut
474f6e545a7SMarek Vasut pin = &priv->pfc.info->pins[i];
475f6e545a7SMarek Vasut break;
476f6e545a7SMarek Vasut }
477f6e545a7SMarek Vasut
478f6e545a7SMarek Vasut if (!pin)
479f6e545a7SMarek Vasut return -EINVAL;
480f6e545a7SMarek Vasut
481f6e545a7SMarek Vasut idx = sh_pfc_get_pin_index(pfc, pin->pin);
482f6e545a7SMarek Vasut cfg = &pmx->configs[idx];
483f6e545a7SMarek Vasut
484f6e545a7SMarek Vasut if (cfg->type != PINMUX_TYPE_NONE)
485f6e545a7SMarek Vasut return -EBUSY;
486f6e545a7SMarek Vasut
487f6e545a7SMarek Vasut return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
488f6e545a7SMarek Vasut }
489f6e545a7SMarek Vasut
sh_pfc_pinctrl_pin_set(struct udevice * dev,unsigned pin_selector,unsigned func_selector)4902489bb54SMarek Vasut static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
4912489bb54SMarek Vasut unsigned func_selector)
4922489bb54SMarek Vasut {
4932489bb54SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
4942489bb54SMarek Vasut struct sh_pfc_pinctrl *pmx = &priv->pmx;
4952489bb54SMarek Vasut struct sh_pfc *pfc = &priv->pfc;
4962489bb54SMarek Vasut const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
4972489bb54SMarek Vasut int idx = sh_pfc_get_pin_index(pfc, pin->pin);
4982489bb54SMarek Vasut struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
4992489bb54SMarek Vasut
5002489bb54SMarek Vasut if (cfg->type != PINMUX_TYPE_NONE)
5012489bb54SMarek Vasut return -EBUSY;
5022489bb54SMarek Vasut
5032489bb54SMarek Vasut return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
5042489bb54SMarek Vasut }
5052489bb54SMarek Vasut
sh_pfc_pinctrl_group_set(struct udevice * dev,unsigned group_selector,unsigned func_selector)506910df4d0SMarek Vasut static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
507910df4d0SMarek Vasut unsigned func_selector)
508910df4d0SMarek Vasut {
509910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
510910df4d0SMarek Vasut struct sh_pfc_pinctrl *pmx = &priv->pmx;
511910df4d0SMarek Vasut struct sh_pfc *pfc = &priv->pfc;
512910df4d0SMarek Vasut const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
513910df4d0SMarek Vasut unsigned int i;
514910df4d0SMarek Vasut int ret = 0;
515910df4d0SMarek Vasut
516910df4d0SMarek Vasut for (i = 0; i < grp->nr_pins; ++i) {
517910df4d0SMarek Vasut int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
518910df4d0SMarek Vasut struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
519910df4d0SMarek Vasut
520910df4d0SMarek Vasut if (cfg->type != PINMUX_TYPE_NONE) {
521910df4d0SMarek Vasut ret = -EBUSY;
522910df4d0SMarek Vasut goto done;
523910df4d0SMarek Vasut }
524910df4d0SMarek Vasut }
525910df4d0SMarek Vasut
526910df4d0SMarek Vasut for (i = 0; i < grp->nr_pins; ++i) {
527910df4d0SMarek Vasut ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
528910df4d0SMarek Vasut if (ret < 0)
529910df4d0SMarek Vasut break;
530910df4d0SMarek Vasut }
531910df4d0SMarek Vasut
532910df4d0SMarek Vasut done:
533910df4d0SMarek Vasut return ret;
534910df4d0SMarek Vasut }
535d52132c3SMarek Vasut #if CONFIG_IS_ENABLED(PINCONF)
536d52132c3SMarek Vasut static const struct pinconf_param sh_pfc_pinconf_params[] = {
537d52132c3SMarek Vasut { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
538d52132c3SMarek Vasut { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
539d52132c3SMarek Vasut { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
540d52132c3SMarek Vasut { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
541d52132c3SMarek Vasut { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
542d52132c3SMarek Vasut };
543d52132c3SMarek Vasut
544d52132c3SMarek Vasut static void __iomem *
sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc * pfc,unsigned int pin,unsigned int * offset,unsigned int * size)545d52132c3SMarek Vasut sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
546d52132c3SMarek Vasut unsigned int *offset, unsigned int *size)
547d52132c3SMarek Vasut {
548d52132c3SMarek Vasut const struct pinmux_drive_reg_field *field;
549d52132c3SMarek Vasut const struct pinmux_drive_reg *reg;
550d52132c3SMarek Vasut unsigned int i;
551d52132c3SMarek Vasut
552d52132c3SMarek Vasut for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
553d52132c3SMarek Vasut for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
554d52132c3SMarek Vasut field = ®->fields[i];
555d52132c3SMarek Vasut
556d52132c3SMarek Vasut if (field->size && field->pin == pin) {
557d52132c3SMarek Vasut *offset = field->offset;
558d52132c3SMarek Vasut *size = field->size;
559d52132c3SMarek Vasut
560d52132c3SMarek Vasut return (void __iomem *)(uintptr_t)reg->reg;
561d52132c3SMarek Vasut }
562d52132c3SMarek Vasut }
563d52132c3SMarek Vasut }
564d52132c3SMarek Vasut
565d52132c3SMarek Vasut return NULL;
566d52132c3SMarek Vasut }
567d52132c3SMarek Vasut
sh_pfc_pinconf_set_drive_strength(struct sh_pfc * pfc,unsigned int pin,u16 strength)568d52132c3SMarek Vasut static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
569d52132c3SMarek Vasut unsigned int pin, u16 strength)
570d52132c3SMarek Vasut {
571d52132c3SMarek Vasut unsigned int offset;
572d52132c3SMarek Vasut unsigned int size;
573d52132c3SMarek Vasut unsigned int step;
574d52132c3SMarek Vasut void __iomem *reg;
575d52132c3SMarek Vasut void __iomem *unlock_reg =
576d52132c3SMarek Vasut (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
577d52132c3SMarek Vasut u32 val;
578d52132c3SMarek Vasut
579d52132c3SMarek Vasut reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
580d52132c3SMarek Vasut if (!reg)
581d52132c3SMarek Vasut return -EINVAL;
582d52132c3SMarek Vasut
583d52132c3SMarek Vasut step = size == 2 ? 6 : 3;
584d52132c3SMarek Vasut
585d52132c3SMarek Vasut if (strength < step || strength > 24)
586d52132c3SMarek Vasut return -EINVAL;
587d52132c3SMarek Vasut
588d52132c3SMarek Vasut /* Convert the value from mA based on a full drive strength value of
589d52132c3SMarek Vasut * 24mA. We can make the full value configurable later if needed.
590d52132c3SMarek Vasut */
591d52132c3SMarek Vasut strength = strength / step - 1;
592d52132c3SMarek Vasut
593d52132c3SMarek Vasut val = sh_pfc_read_raw_reg(reg, 32);
594*0ff9e480SMarek Vasut val &= ~GENMASK(offset + 4 - 1, offset);
595d52132c3SMarek Vasut val |= strength << offset;
596d52132c3SMarek Vasut
597d52132c3SMarek Vasut if (unlock_reg)
598d52132c3SMarek Vasut sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
599d52132c3SMarek Vasut
600d52132c3SMarek Vasut sh_pfc_write_raw_reg(reg, 32, val);
601d52132c3SMarek Vasut
602d52132c3SMarek Vasut return 0;
603d52132c3SMarek Vasut }
604d52132c3SMarek Vasut
605d52132c3SMarek Vasut /* Check whether the requested parameter is supported for a pin. */
sh_pfc_pinconf_validate(struct sh_pfc * pfc,unsigned int _pin,unsigned int param)606d52132c3SMarek Vasut static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
607d52132c3SMarek Vasut unsigned int param)
608d52132c3SMarek Vasut {
609d52132c3SMarek Vasut int idx = sh_pfc_get_pin_index(pfc, _pin);
610d52132c3SMarek Vasut const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
611d52132c3SMarek Vasut
612d52132c3SMarek Vasut switch (param) {
613d52132c3SMarek Vasut case PIN_CONFIG_BIAS_DISABLE:
614d52132c3SMarek Vasut return pin->configs &
615d52132c3SMarek Vasut (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
616d52132c3SMarek Vasut
617d52132c3SMarek Vasut case PIN_CONFIG_BIAS_PULL_UP:
618d52132c3SMarek Vasut return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
619d52132c3SMarek Vasut
620d52132c3SMarek Vasut case PIN_CONFIG_BIAS_PULL_DOWN:
621d52132c3SMarek Vasut return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
622d52132c3SMarek Vasut
623d52132c3SMarek Vasut case PIN_CONFIG_DRIVE_STRENGTH:
624d52132c3SMarek Vasut return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
625d52132c3SMarek Vasut
626d52132c3SMarek Vasut case PIN_CONFIG_POWER_SOURCE:
627d52132c3SMarek Vasut return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
628d52132c3SMarek Vasut
629d52132c3SMarek Vasut default:
630d52132c3SMarek Vasut return false;
631d52132c3SMarek Vasut }
632d52132c3SMarek Vasut }
633d52132c3SMarek Vasut
sh_pfc_pinconf_set(struct sh_pfc_pinctrl * pmx,unsigned _pin,unsigned int param,unsigned int arg)634d52132c3SMarek Vasut static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
635d52132c3SMarek Vasut unsigned int param, unsigned int arg)
636d52132c3SMarek Vasut {
637d52132c3SMarek Vasut struct sh_pfc *pfc = pmx->pfc;
638d52132c3SMarek Vasut void __iomem *pocctrl;
639d52132c3SMarek Vasut void __iomem *unlock_reg =
640d52132c3SMarek Vasut (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
641d52132c3SMarek Vasut u32 addr, val;
642d52132c3SMarek Vasut int bit, ret;
643d52132c3SMarek Vasut
644d52132c3SMarek Vasut if (!sh_pfc_pinconf_validate(pfc, _pin, param))
645d52132c3SMarek Vasut return -ENOTSUPP;
646d52132c3SMarek Vasut
647d52132c3SMarek Vasut switch (param) {
648d52132c3SMarek Vasut case PIN_CONFIG_BIAS_PULL_UP:
649d52132c3SMarek Vasut case PIN_CONFIG_BIAS_PULL_DOWN:
650d52132c3SMarek Vasut case PIN_CONFIG_BIAS_DISABLE:
651d52132c3SMarek Vasut if (!pfc->info->ops || !pfc->info->ops->set_bias)
652d52132c3SMarek Vasut return -ENOTSUPP;
653d52132c3SMarek Vasut
654d52132c3SMarek Vasut pfc->info->ops->set_bias(pfc, _pin, param);
655d52132c3SMarek Vasut
656d52132c3SMarek Vasut break;
657d52132c3SMarek Vasut
658d52132c3SMarek Vasut case PIN_CONFIG_DRIVE_STRENGTH:
659d52132c3SMarek Vasut ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
660d52132c3SMarek Vasut if (ret < 0)
661d52132c3SMarek Vasut return ret;
662d52132c3SMarek Vasut
663d52132c3SMarek Vasut break;
664d52132c3SMarek Vasut
665d52132c3SMarek Vasut case PIN_CONFIG_POWER_SOURCE:
666d52132c3SMarek Vasut if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
667d52132c3SMarek Vasut return -ENOTSUPP;
668d52132c3SMarek Vasut
669d52132c3SMarek Vasut bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
670d52132c3SMarek Vasut if (bit < 0) {
671d52132c3SMarek Vasut printf("invalid pin %#x", _pin);
672d52132c3SMarek Vasut return bit;
673d52132c3SMarek Vasut }
674d52132c3SMarek Vasut
675d52132c3SMarek Vasut if (arg != 1800 && arg != 3300)
676d52132c3SMarek Vasut return -EINVAL;
677d52132c3SMarek Vasut
678d52132c3SMarek Vasut pocctrl = (void __iomem *)(uintptr_t)addr;
679d52132c3SMarek Vasut
680d52132c3SMarek Vasut val = sh_pfc_read_raw_reg(pocctrl, 32);
681d52132c3SMarek Vasut if (arg == 3300)
682d52132c3SMarek Vasut val |= BIT(bit);
683d52132c3SMarek Vasut else
684d52132c3SMarek Vasut val &= ~BIT(bit);
685d52132c3SMarek Vasut
686d52132c3SMarek Vasut if (unlock_reg)
687d52132c3SMarek Vasut sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
688d52132c3SMarek Vasut
689d52132c3SMarek Vasut sh_pfc_write_raw_reg(pocctrl, 32, val);
690d52132c3SMarek Vasut
691d52132c3SMarek Vasut break;
692d52132c3SMarek Vasut
693d52132c3SMarek Vasut default:
694d52132c3SMarek Vasut return -ENOTSUPP;
695d52132c3SMarek Vasut }
696d52132c3SMarek Vasut
697d52132c3SMarek Vasut return 0;
698d52132c3SMarek Vasut }
699d52132c3SMarek Vasut
sh_pfc_pinconf_pin_set(struct udevice * dev,unsigned int pin_selector,unsigned int param,unsigned int arg)7002489bb54SMarek Vasut static int sh_pfc_pinconf_pin_set(struct udevice *dev,
7012489bb54SMarek Vasut unsigned int pin_selector,
7022489bb54SMarek Vasut unsigned int param, unsigned int arg)
7032489bb54SMarek Vasut {
7042489bb54SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
7052489bb54SMarek Vasut struct sh_pfc_pinctrl *pmx = &priv->pmx;
7062489bb54SMarek Vasut struct sh_pfc *pfc = &priv->pfc;
7072489bb54SMarek Vasut const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
7082489bb54SMarek Vasut
7092489bb54SMarek Vasut sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
7102489bb54SMarek Vasut
7112489bb54SMarek Vasut return 0;
7122489bb54SMarek Vasut }
713d52132c3SMarek Vasut
sh_pfc_pinconf_group_set(struct udevice * dev,unsigned int group_selector,unsigned int param,unsigned int arg)714d52132c3SMarek Vasut static int sh_pfc_pinconf_group_set(struct udevice *dev,
715d52132c3SMarek Vasut unsigned int group_selector,
716d52132c3SMarek Vasut unsigned int param, unsigned int arg)
717d52132c3SMarek Vasut {
718d52132c3SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
719d52132c3SMarek Vasut struct sh_pfc_pinctrl *pmx = &priv->pmx;
720d52132c3SMarek Vasut struct sh_pfc *pfc = &priv->pfc;
721d52132c3SMarek Vasut const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
722d52132c3SMarek Vasut unsigned int i;
723d52132c3SMarek Vasut
724d52132c3SMarek Vasut for (i = 0; i < grp->nr_pins; i++)
725d52132c3SMarek Vasut sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
726d52132c3SMarek Vasut
727d52132c3SMarek Vasut return 0;
728d52132c3SMarek Vasut }
729d52132c3SMarek Vasut #endif
730910df4d0SMarek Vasut
731910df4d0SMarek Vasut static struct pinctrl_ops sh_pfc_pinctrl_ops = {
732910df4d0SMarek Vasut .get_pins_count = sh_pfc_pinctrl_get_pins_count,
733910df4d0SMarek Vasut .get_pin_name = sh_pfc_pinctrl_get_pin_name,
734910df4d0SMarek Vasut .get_groups_count = sh_pfc_pinctrl_get_groups_count,
735910df4d0SMarek Vasut .get_group_name = sh_pfc_pinctrl_get_group_name,
736910df4d0SMarek Vasut .get_functions_count = sh_pfc_pinctrl_get_functions_count,
737910df4d0SMarek Vasut .get_function_name = sh_pfc_pinctrl_get_function_name,
738910df4d0SMarek Vasut
739d52132c3SMarek Vasut #if CONFIG_IS_ENABLED(PINCONF)
740d52132c3SMarek Vasut .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
741d52132c3SMarek Vasut .pinconf_params = sh_pfc_pinconf_params,
7422489bb54SMarek Vasut .pinconf_set = sh_pfc_pinconf_pin_set,
743d52132c3SMarek Vasut .pinconf_group_set = sh_pfc_pinconf_group_set,
744d52132c3SMarek Vasut #endif
7452489bb54SMarek Vasut .pinmux_set = sh_pfc_pinctrl_pin_set,
746910df4d0SMarek Vasut .pinmux_group_set = sh_pfc_pinctrl_group_set,
747910df4d0SMarek Vasut .set_state = pinctrl_generic_set_state,
748910df4d0SMarek Vasut };
749910df4d0SMarek Vasut
sh_pfc_map_pins(struct sh_pfc * pfc,struct sh_pfc_pinctrl * pmx)750910df4d0SMarek Vasut static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
751910df4d0SMarek Vasut {
752910df4d0SMarek Vasut unsigned int i;
753910df4d0SMarek Vasut
754910df4d0SMarek Vasut /* Allocate and initialize the pins and configs arrays. */
755910df4d0SMarek Vasut pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
756910df4d0SMarek Vasut GFP_KERNEL);
757910df4d0SMarek Vasut if (unlikely(!pmx->configs))
758910df4d0SMarek Vasut return -ENOMEM;
759910df4d0SMarek Vasut
760910df4d0SMarek Vasut for (i = 0; i < pfc->info->nr_pins; ++i) {
761910df4d0SMarek Vasut struct sh_pfc_pin_config *cfg = &pmx->configs[i];
762910df4d0SMarek Vasut cfg->type = PINMUX_TYPE_NONE;
763910df4d0SMarek Vasut }
764910df4d0SMarek Vasut
765910df4d0SMarek Vasut return 0;
766910df4d0SMarek Vasut }
767910df4d0SMarek Vasut
768910df4d0SMarek Vasut
sh_pfc_pinctrl_probe(struct udevice * dev)769910df4d0SMarek Vasut static int sh_pfc_pinctrl_probe(struct udevice *dev)
770910df4d0SMarek Vasut {
771910df4d0SMarek Vasut struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
772910df4d0SMarek Vasut enum sh_pfc_model model = dev_get_driver_data(dev);
773910df4d0SMarek Vasut fdt_addr_t base;
774910df4d0SMarek Vasut
775910df4d0SMarek Vasut base = devfdt_get_addr(dev);
776910df4d0SMarek Vasut if (base == FDT_ADDR_T_NONE)
777910df4d0SMarek Vasut return -EINVAL;
778910df4d0SMarek Vasut
779910df4d0SMarek Vasut priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
780910df4d0SMarek Vasut if (!priv->pfc.regs)
781910df4d0SMarek Vasut return -ENOMEM;
782910df4d0SMarek Vasut
7837547ad4cSMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7790
7847547ad4cSMarek Vasut if (model == SH_PFC_R8A7790)
7857547ad4cSMarek Vasut priv->pfc.info = &r8a7790_pinmux_info;
7867547ad4cSMarek Vasut #endif
787427c75dfSMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7791
788427c75dfSMarek Vasut if (model == SH_PFC_R8A7791)
789427c75dfSMarek Vasut priv->pfc.info = &r8a7791_pinmux_info;
790427c75dfSMarek Vasut #endif
791ab2d09b4SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7792
792ab2d09b4SMarek Vasut if (model == SH_PFC_R8A7792)
793ab2d09b4SMarek Vasut priv->pfc.info = &r8a7792_pinmux_info;
794ab2d09b4SMarek Vasut #endif
795427c75dfSMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7793
796427c75dfSMarek Vasut if (model == SH_PFC_R8A7793)
797427c75dfSMarek Vasut priv->pfc.info = &r8a7793_pinmux_info;
798427c75dfSMarek Vasut #endif
79934e93605SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7794
80034e93605SMarek Vasut if (model == SH_PFC_R8A7794)
80134e93605SMarek Vasut priv->pfc.info = &r8a7794_pinmux_info;
80234e93605SMarek Vasut #endif
803910df4d0SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7795
804910df4d0SMarek Vasut if (model == SH_PFC_R8A7795)
805910df4d0SMarek Vasut priv->pfc.info = &r8a7795_pinmux_info;
806910df4d0SMarek Vasut #endif
807910df4d0SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7796
808910df4d0SMarek Vasut if (model == SH_PFC_R8A7796)
809910df4d0SMarek Vasut priv->pfc.info = &r8a7796_pinmux_info;
810910df4d0SMarek Vasut #endif
811c106bb53SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A77970
812c106bb53SMarek Vasut if (model == SH_PFC_R8A77970)
813c106bb53SMarek Vasut priv->pfc.info = &r8a77970_pinmux_info;
814c106bb53SMarek Vasut #endif
815cb13e46aSMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A77990
816cb13e46aSMarek Vasut if (model == SH_PFC_R8A77990)
817cb13e46aSMarek Vasut priv->pfc.info = &r8a77990_pinmux_info;
818cb13e46aSMarek Vasut #endif
819a59e6976SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A77995
820a59e6976SMarek Vasut if (model == SH_PFC_R8A77995)
821a59e6976SMarek Vasut priv->pfc.info = &r8a77995_pinmux_info;
822a59e6976SMarek Vasut #endif
823910df4d0SMarek Vasut
824910df4d0SMarek Vasut priv->pmx.pfc = &priv->pfc;
825910df4d0SMarek Vasut sh_pfc_init_ranges(&priv->pfc);
826910df4d0SMarek Vasut sh_pfc_map_pins(&priv->pfc, &priv->pmx);
827910df4d0SMarek Vasut
828910df4d0SMarek Vasut return 0;
829910df4d0SMarek Vasut }
830910df4d0SMarek Vasut
831910df4d0SMarek Vasut static const struct udevice_id sh_pfc_pinctrl_ids[] = {
8327547ad4cSMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7790
8337547ad4cSMarek Vasut {
8347547ad4cSMarek Vasut .compatible = "renesas,pfc-r8a7790",
8357547ad4cSMarek Vasut .data = SH_PFC_R8A7790,
8367547ad4cSMarek Vasut },
8377547ad4cSMarek Vasut #endif
838427c75dfSMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7791
839427c75dfSMarek Vasut {
840427c75dfSMarek Vasut .compatible = "renesas,pfc-r8a7791",
841427c75dfSMarek Vasut .data = SH_PFC_R8A7791,
842427c75dfSMarek Vasut },
843427c75dfSMarek Vasut #endif
844ab2d09b4SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7792
845ab2d09b4SMarek Vasut {
846ab2d09b4SMarek Vasut .compatible = "renesas,pfc-r8a7792",
847ab2d09b4SMarek Vasut .data = SH_PFC_R8A7792,
848ab2d09b4SMarek Vasut },
849ab2d09b4SMarek Vasut #endif
850427c75dfSMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7793
851427c75dfSMarek Vasut {
852427c75dfSMarek Vasut .compatible = "renesas,pfc-r8a7793",
853427c75dfSMarek Vasut .data = SH_PFC_R8A7793,
854427c75dfSMarek Vasut },
855427c75dfSMarek Vasut #endif
85634e93605SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7794
85734e93605SMarek Vasut {
85834e93605SMarek Vasut .compatible = "renesas,pfc-r8a7794",
85934e93605SMarek Vasut .data = SH_PFC_R8A7794,
86034e93605SMarek Vasut },
86134e93605SMarek Vasut #endif
862910df4d0SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7795
863910df4d0SMarek Vasut {
864910df4d0SMarek Vasut .compatible = "renesas,pfc-r8a7795",
865910df4d0SMarek Vasut .data = SH_PFC_R8A7795,
866910df4d0SMarek Vasut },
867910df4d0SMarek Vasut #endif
868910df4d0SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A7796
869910df4d0SMarek Vasut {
870910df4d0SMarek Vasut .compatible = "renesas,pfc-r8a7796",
871910df4d0SMarek Vasut .data = SH_PFC_R8A7796,
872d1fe3182SMarek Vasut }, {
873d1fe3182SMarek Vasut .compatible = "renesas,pfc-r8a77965",
874d1fe3182SMarek Vasut .data = SH_PFC_R8A7796,
875910df4d0SMarek Vasut },
876910df4d0SMarek Vasut #endif
877c106bb53SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A77970
878c106bb53SMarek Vasut {
879c106bb53SMarek Vasut .compatible = "renesas,pfc-r8a77970",
880c106bb53SMarek Vasut .data = SH_PFC_R8A77970,
881c106bb53SMarek Vasut },
882c106bb53SMarek Vasut #endif
883cb13e46aSMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A77990
884cb13e46aSMarek Vasut {
885cb13e46aSMarek Vasut .compatible = "renesas,pfc-r8a77990",
886cb13e46aSMarek Vasut .data = SH_PFC_R8A77990,
887cb13e46aSMarek Vasut },
888cb13e46aSMarek Vasut #endif
889a59e6976SMarek Vasut #ifdef CONFIG_PINCTRL_PFC_R8A77995
890a59e6976SMarek Vasut {
891a59e6976SMarek Vasut .compatible = "renesas,pfc-r8a77995",
892a59e6976SMarek Vasut .data = SH_PFC_R8A77995,
893a59e6976SMarek Vasut },
894a59e6976SMarek Vasut #endif
895910df4d0SMarek Vasut { },
896910df4d0SMarek Vasut };
897910df4d0SMarek Vasut
898910df4d0SMarek Vasut U_BOOT_DRIVER(pinctrl_sh_pfc) = {
899910df4d0SMarek Vasut .name = "sh_pfc_pinctrl",
900910df4d0SMarek Vasut .id = UCLASS_PINCTRL,
901910df4d0SMarek Vasut .of_match = sh_pfc_pinctrl_ids,
902910df4d0SMarek Vasut .priv_auto_alloc_size = sizeof(struct sh_pfc_pinctrl_priv),
903910df4d0SMarek Vasut .ops = &sh_pfc_pinctrl_ops,
904910df4d0SMarek Vasut .probe = sh_pfc_pinctrl_probe,
905910df4d0SMarek Vasut };
906