1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Pinctrl for Cirrus Logic Madera codecs
4 *
5 * Copyright (C) 2016-2018 Cirrus Logic
6 */
7
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/regmap.h>
12 #include <linux/slab.h>
13 #include <linux/pinctrl/machine.h>
14 #include <linux/pinctrl/pinctrl.h>
15 #include <linux/pinctrl/pinmux.h>
16 #include <linux/pinctrl/pinconf.h>
17 #include <linux/pinctrl/pinconf-generic.h>
18
19 #include <linux/mfd/madera/core.h>
20 #include <linux/mfd/madera/registers.h>
21
22 #include "../pinctrl-utils.h"
23
24 #include "pinctrl-madera.h"
25
26 /*
27 * Use pin GPIO names for consistency
28 * NOTE: IDs are zero-indexed for coding convenience
29 */
30 static const struct pinctrl_pin_desc madera_pins[] = {
31 PINCTRL_PIN(0, "gpio1"),
32 PINCTRL_PIN(1, "gpio2"),
33 PINCTRL_PIN(2, "gpio3"),
34 PINCTRL_PIN(3, "gpio4"),
35 PINCTRL_PIN(4, "gpio5"),
36 PINCTRL_PIN(5, "gpio6"),
37 PINCTRL_PIN(6, "gpio7"),
38 PINCTRL_PIN(7, "gpio8"),
39 PINCTRL_PIN(8, "gpio9"),
40 PINCTRL_PIN(9, "gpio10"),
41 PINCTRL_PIN(10, "gpio11"),
42 PINCTRL_PIN(11, "gpio12"),
43 PINCTRL_PIN(12, "gpio13"),
44 PINCTRL_PIN(13, "gpio14"),
45 PINCTRL_PIN(14, "gpio15"),
46 PINCTRL_PIN(15, "gpio16"),
47 PINCTRL_PIN(16, "gpio17"),
48 PINCTRL_PIN(17, "gpio18"),
49 PINCTRL_PIN(18, "gpio19"),
50 PINCTRL_PIN(19, "gpio20"),
51 PINCTRL_PIN(20, "gpio21"),
52 PINCTRL_PIN(21, "gpio22"),
53 PINCTRL_PIN(22, "gpio23"),
54 PINCTRL_PIN(23, "gpio24"),
55 PINCTRL_PIN(24, "gpio25"),
56 PINCTRL_PIN(25, "gpio26"),
57 PINCTRL_PIN(26, "gpio27"),
58 PINCTRL_PIN(27, "gpio28"),
59 PINCTRL_PIN(28, "gpio29"),
60 PINCTRL_PIN(29, "gpio30"),
61 PINCTRL_PIN(30, "gpio31"),
62 PINCTRL_PIN(31, "gpio32"),
63 PINCTRL_PIN(32, "gpio33"),
64 PINCTRL_PIN(33, "gpio34"),
65 PINCTRL_PIN(34, "gpio35"),
66 PINCTRL_PIN(35, "gpio36"),
67 PINCTRL_PIN(36, "gpio37"),
68 PINCTRL_PIN(37, "gpio38"),
69 PINCTRL_PIN(38, "gpio39"),
70 PINCTRL_PIN(39, "gpio40"),
71 };
72
73 /*
74 * All single-pin functions can be mapped to any GPIO, however pinmux applies
75 * functions to pin groups and only those groups declared as supporting that
76 * function. To make this work we must put each pin in its own dummy group so
77 * that the functions can be described as applying to all pins.
78 * Since these do not correspond to anything in the actual hardware - they are
79 * merely an adaptation to pinctrl's view of the world - we use the same name
80 * as the pin to avoid confusion when comparing with datasheet instructions
81 */
82 static const char * const madera_pin_single_group_names[] = {
83 "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7",
84 "gpio8", "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14",
85 "gpio15", "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21",
86 "gpio22", "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28",
87 "gpio29", "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35",
88 "gpio36", "gpio37", "gpio38", "gpio39", "gpio40",
89 };
90
91 /* set of pin numbers for single-pin groups, zero-indexed */
92 static const unsigned int madera_pin_single_group_pins[] = {
93 0, 1, 2, 3, 4, 5, 6,
94 7, 8, 9, 10, 11, 12, 13,
95 14, 15, 16, 17, 18, 19, 20,
96 21, 22, 23, 24, 25, 26, 27,
97 28, 29, 30, 31, 32, 33, 34,
98 35, 36, 37, 38, 39,
99 };
100
101 static const char * const madera_aif1_group_names[] = { "aif1" };
102 static const char * const madera_aif2_group_names[] = { "aif2" };
103 static const char * const madera_aif3_group_names[] = { "aif3" };
104 static const char * const madera_aif4_group_names[] = { "aif4" };
105 static const char * const madera_mif1_group_names[] = { "mif1" };
106 static const char * const madera_mif2_group_names[] = { "mif2" };
107 static const char * const madera_mif3_group_names[] = { "mif3" };
108 static const char * const madera_dmic3_group_names[] = { "dmic3" };
109 static const char * const madera_dmic4_group_names[] = { "dmic4" };
110 static const char * const madera_dmic5_group_names[] = { "dmic5" };
111 static const char * const madera_dmic6_group_names[] = { "dmic6" };
112 static const char * const madera_spk1_group_names[] = { "pdmspk1" };
113 static const char * const madera_spk2_group_names[] = { "pdmspk2" };
114
115 /*
116 * alt-functions always apply to a single pin group, other functions always
117 * apply to all pins
118 */
119 static const struct {
120 const char *name;
121 const char * const *group_names;
122 u32 func;
123 } madera_mux_funcs[] = {
124 {
125 .name = "aif1",
126 .group_names = madera_aif1_group_names,
127 .func = 0x000
128 },
129 {
130 .name = "aif2",
131 .group_names = madera_aif2_group_names,
132 .func = 0x000
133 },
134 {
135 .name = "aif3",
136 .group_names = madera_aif3_group_names,
137 .func = 0x000
138 },
139 {
140 .name = "aif4",
141 .group_names = madera_aif4_group_names,
142 .func = 0x000
143 },
144 {
145 .name = "mif1",
146 .group_names = madera_mif1_group_names,
147 .func = 0x000
148 },
149 {
150 .name = "mif2",
151 .group_names = madera_mif2_group_names,
152 .func = 0x000
153 },
154 {
155 .name = "mif3",
156 .group_names = madera_mif3_group_names,
157 .func = 0x000
158 },
159 {
160 .name = "dmic3",
161 .group_names = madera_dmic3_group_names,
162 .func = 0x000
163 },
164 {
165 .name = "dmic4",
166 .group_names = madera_dmic4_group_names,
167 .func = 0x000
168 },
169 {
170 .name = "dmic5",
171 .group_names = madera_dmic5_group_names,
172 .func = 0x000
173 },
174 {
175 .name = "dmic6",
176 .group_names = madera_dmic6_group_names,
177 .func = 0x000
178 },
179 {
180 .name = "pdmspk1",
181 .group_names = madera_spk1_group_names,
182 .func = 0x000
183 },
184 {
185 .name = "pdmspk2",
186 .group_names = madera_spk2_group_names,
187 .func = 0x000
188 },
189 {
190 .name = "io",
191 .group_names = madera_pin_single_group_names,
192 .func = 0x001
193 },
194 {
195 .name = "dsp-gpio",
196 .group_names = madera_pin_single_group_names,
197 .func = 0x002
198 },
199 {
200 .name = "irq1",
201 .group_names = madera_pin_single_group_names,
202 .func = 0x003
203 },
204 {
205 .name = "irq2",
206 .group_names = madera_pin_single_group_names,
207 .func = 0x004
208 },
209 {
210 .name = "fll1-clk",
211 .group_names = madera_pin_single_group_names,
212 .func = 0x010
213 },
214 {
215 .name = "fll2-clk",
216 .group_names = madera_pin_single_group_names,
217 .func = 0x011
218 },
219 {
220 .name = "fll3-clk",
221 .group_names = madera_pin_single_group_names,
222 .func = 0x012
223 },
224 {
225 .name = "fllao-clk",
226 .group_names = madera_pin_single_group_names,
227 .func = 0x013
228 },
229 {
230 .name = "fll1-lock",
231 .group_names = madera_pin_single_group_names,
232 .func = 0x018
233 },
234 {
235 .name = "fll2-lock",
236 .group_names = madera_pin_single_group_names,
237 .func = 0x019
238 },
239 {
240 .name = "fll3-lock",
241 .group_names = madera_pin_single_group_names,
242 .func = 0x01a
243 },
244 {
245 .name = "fllao-lock",
246 .group_names = madera_pin_single_group_names,
247 .func = 0x01b
248 },
249 {
250 .name = "opclk",
251 .group_names = madera_pin_single_group_names,
252 .func = 0x040
253 },
254 {
255 .name = "opclk-async",
256 .group_names = madera_pin_single_group_names,
257 .func = 0x041
258 },
259 {
260 .name = "pwm1",
261 .group_names = madera_pin_single_group_names,
262 .func = 0x048
263 },
264 {
265 .name = "pwm2",
266 .group_names = madera_pin_single_group_names,
267 .func = 0x049
268 },
269 {
270 .name = "spdif",
271 .group_names = madera_pin_single_group_names,
272 .func = 0x04c
273 },
274 {
275 .name = "asrc1-in1-lock",
276 .group_names = madera_pin_single_group_names,
277 .func = 0x088
278 },
279 {
280 .name = "asrc1-in2-lock",
281 .group_names = madera_pin_single_group_names,
282 .func = 0x089
283 },
284 {
285 .name = "asrc2-in1-lock",
286 .group_names = madera_pin_single_group_names,
287 .func = 0x08a
288 },
289 {
290 .name = "asrc2-in2-lock",
291 .group_names = madera_pin_single_group_names,
292 .func = 0x08b
293 },
294 {
295 .name = "spkl-short-circuit",
296 .group_names = madera_pin_single_group_names,
297 .func = 0x0b6
298 },
299 {
300 .name = "spkr-short-circuit",
301 .group_names = madera_pin_single_group_names,
302 .func = 0x0b7
303 },
304 {
305 .name = "spk-shutdown",
306 .group_names = madera_pin_single_group_names,
307 .func = 0x0e0
308 },
309 {
310 .name = "spk-overheat-shutdown",
311 .group_names = madera_pin_single_group_names,
312 .func = 0x0e1
313 },
314 {
315 .name = "spk-overheat-warn",
316 .group_names = madera_pin_single_group_names,
317 .func = 0x0e2
318 },
319 {
320 .name = "timer1-sts",
321 .group_names = madera_pin_single_group_names,
322 .func = 0x140
323 },
324 {
325 .name = "timer2-sts",
326 .group_names = madera_pin_single_group_names,
327 .func = 0x141
328 },
329 {
330 .name = "timer3-sts",
331 .group_names = madera_pin_single_group_names,
332 .func = 0x142
333 },
334 {
335 .name = "timer4-sts",
336 .group_names = madera_pin_single_group_names,
337 .func = 0x143
338 },
339 {
340 .name = "timer5-sts",
341 .group_names = madera_pin_single_group_names,
342 .func = 0x144
343 },
344 {
345 .name = "timer6-sts",
346 .group_names = madera_pin_single_group_names,
347 .func = 0x145
348 },
349 {
350 .name = "timer7-sts",
351 .group_names = madera_pin_single_group_names,
352 .func = 0x146
353 },
354 {
355 .name = "timer8-sts",
356 .group_names = madera_pin_single_group_names,
357 .func = 0x147
358 },
359 {
360 .name = "log1-fifo-ne",
361 .group_names = madera_pin_single_group_names,
362 .func = 0x150
363 },
364 {
365 .name = "log2-fifo-ne",
366 .group_names = madera_pin_single_group_names,
367 .func = 0x151
368 },
369 {
370 .name = "log3-fifo-ne",
371 .group_names = madera_pin_single_group_names,
372 .func = 0x152
373 },
374 {
375 .name = "log4-fifo-ne",
376 .group_names = madera_pin_single_group_names,
377 .func = 0x153
378 },
379 {
380 .name = "log5-fifo-ne",
381 .group_names = madera_pin_single_group_names,
382 .func = 0x154
383 },
384 {
385 .name = "log6-fifo-ne",
386 .group_names = madera_pin_single_group_names,
387 .func = 0x155
388 },
389 {
390 .name = "log7-fifo-ne",
391 .group_names = madera_pin_single_group_names,
392 .func = 0x156
393 },
394 {
395 .name = "log8-fifo-ne",
396 .group_names = madera_pin_single_group_names,
397 .func = 0x157
398 },
399 {
400 .name = "aux-pdm-clk",
401 .group_names = madera_pin_single_group_names,
402 .func = 0x280
403 },
404 {
405 .name = "aux-pdm-dat",
406 .group_names = madera_pin_single_group_names,
407 .func = 0x281
408 },
409 };
410
madera_pin_make_drv_str(struct madera_pin_private * priv,unsigned int milliamps)411 static u16 madera_pin_make_drv_str(struct madera_pin_private *priv,
412 unsigned int milliamps)
413 {
414 switch (milliamps) {
415 case 4:
416 return 0;
417 case 8:
418 return 2 << MADERA_GP1_DRV_STR_SHIFT;
419 default:
420 break;
421 }
422
423 dev_warn(priv->dev, "%u mA not a valid drive strength", milliamps);
424
425 return 0;
426 }
427
madera_pin_unmake_drv_str(struct madera_pin_private * priv,u16 regval)428 static unsigned int madera_pin_unmake_drv_str(struct madera_pin_private *priv,
429 u16 regval)
430 {
431 regval = (regval & MADERA_GP1_DRV_STR_MASK) >> MADERA_GP1_DRV_STR_SHIFT;
432
433 switch (regval) {
434 case 0:
435 return 4;
436 case 2:
437 return 8;
438 default:
439 return 0;
440 }
441 }
442
madera_get_groups_count(struct pinctrl_dev * pctldev)443 static int madera_get_groups_count(struct pinctrl_dev *pctldev)
444 {
445 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
446
447 /* Number of alt function groups plus number of single-pin groups */
448 return priv->chip->n_pin_groups + priv->chip->n_pins;
449 }
450
madera_get_group_name(struct pinctrl_dev * pctldev,unsigned int selector)451 static const char *madera_get_group_name(struct pinctrl_dev *pctldev,
452 unsigned int selector)
453 {
454 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
455
456 if (selector < priv->chip->n_pin_groups)
457 return priv->chip->pin_groups[selector].name;
458
459 selector -= priv->chip->n_pin_groups;
460 return madera_pin_single_group_names[selector];
461 }
462
madera_get_group_pins(struct pinctrl_dev * pctldev,unsigned int selector,const unsigned int ** pins,unsigned int * num_pins)463 static int madera_get_group_pins(struct pinctrl_dev *pctldev,
464 unsigned int selector,
465 const unsigned int **pins,
466 unsigned int *num_pins)
467 {
468 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
469
470 if (selector < priv->chip->n_pin_groups) {
471 *pins = priv->chip->pin_groups[selector].pins;
472 *num_pins = priv->chip->pin_groups[selector].n_pins;
473 } else {
474 /* return the dummy group for a single pin */
475 selector -= priv->chip->n_pin_groups;
476 *pins = &madera_pin_single_group_pins[selector];
477 *num_pins = 1;
478 }
479 return 0;
480 }
481
madera_pin_dbg_show_fn(struct madera_pin_private * priv,struct seq_file * s,unsigned int pin,unsigned int fn)482 static void madera_pin_dbg_show_fn(struct madera_pin_private *priv,
483 struct seq_file *s,
484 unsigned int pin, unsigned int fn)
485 {
486 const struct madera_pin_chip *chip = priv->chip;
487 int i, g_pin;
488
489 if (fn != 0) {
490 for (i = 0; i < ARRAY_SIZE(madera_mux_funcs); ++i) {
491 if (madera_mux_funcs[i].func == fn) {
492 seq_printf(s, " FN=%s",
493 madera_mux_funcs[i].name);
494 return;
495 }
496 }
497 return; /* ignore unknown function values */
498 }
499
500 /* alt function */
501 for (i = 0; i < chip->n_pin_groups; ++i) {
502 for (g_pin = 0; g_pin < chip->pin_groups[i].n_pins; ++g_pin) {
503 if (chip->pin_groups[i].pins[g_pin] == pin) {
504 seq_printf(s, " FN=%s",
505 chip->pin_groups[i].name);
506 return;
507 }
508 }
509 }
510 }
511
madera_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned int pin)512 static void __maybe_unused madera_pin_dbg_show(struct pinctrl_dev *pctldev,
513 struct seq_file *s,
514 unsigned int pin)
515 {
516 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
517 unsigned int conf[2];
518 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * pin);
519 unsigned int fn;
520 int ret;
521
522 ret = regmap_read(priv->madera->regmap, reg, &conf[0]);
523 if (ret)
524 return;
525
526 ret = regmap_read(priv->madera->regmap, reg + 1, &conf[1]);
527 if (ret)
528 return;
529
530 seq_printf(s, "%04x:%04x", conf[0], conf[1]);
531
532 fn = (conf[0] & MADERA_GP1_FN_MASK) >> MADERA_GP1_FN_SHIFT;
533 madera_pin_dbg_show_fn(priv, s, pin, fn);
534
535 /* State of direction bit is only relevant if function==1 */
536 if (fn == 1) {
537 if (conf[1] & MADERA_GP1_DIR_MASK)
538 seq_puts(s, " IN");
539 else
540 seq_puts(s, " OUT");
541 }
542
543 if (conf[1] & MADERA_GP1_PU_MASK)
544 seq_puts(s, " PU");
545
546 if (conf[1] & MADERA_GP1_PD_MASK)
547 seq_puts(s, " PD");
548
549 if (conf[0] & MADERA_GP1_DB_MASK)
550 seq_puts(s, " DB");
551
552 if (conf[0] & MADERA_GP1_OP_CFG_MASK)
553 seq_puts(s, " OD");
554 else
555 seq_puts(s, " CMOS");
556
557 seq_printf(s, " DRV=%umA", madera_pin_unmake_drv_str(priv, conf[1]));
558
559 if (conf[0] & MADERA_GP1_IP_CFG_MASK)
560 seq_puts(s, " SCHMITT");
561 }
562
563
564 static const struct pinctrl_ops madera_pin_group_ops = {
565 .get_groups_count = madera_get_groups_count,
566 .get_group_name = madera_get_group_name,
567 .get_group_pins = madera_get_group_pins,
568 #if IS_ENABLED(CONFIG_OF)
569 .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
570 .dt_free_map = pinctrl_utils_free_map,
571 #endif
572 #if IS_ENABLED(CONFIG_DEBUG_FS)
573 .pin_dbg_show = madera_pin_dbg_show,
574 #endif
575 };
576
madera_mux_get_funcs_count(struct pinctrl_dev * pctldev)577 static int madera_mux_get_funcs_count(struct pinctrl_dev *pctldev)
578 {
579 return ARRAY_SIZE(madera_mux_funcs);
580 }
581
madera_mux_get_func_name(struct pinctrl_dev * pctldev,unsigned int selector)582 static const char *madera_mux_get_func_name(struct pinctrl_dev *pctldev,
583 unsigned int selector)
584 {
585 return madera_mux_funcs[selector].name;
586 }
587
madera_mux_get_groups(struct pinctrl_dev * pctldev,unsigned int selector,const char * const ** groups,unsigned int * const num_groups)588 static int madera_mux_get_groups(struct pinctrl_dev *pctldev,
589 unsigned int selector,
590 const char * const **groups,
591 unsigned int * const num_groups)
592 {
593 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
594
595 *groups = madera_mux_funcs[selector].group_names;
596
597 if (madera_mux_funcs[selector].func == 0) {
598 /* alt func always maps to a single group */
599 *num_groups = 1;
600 } else {
601 /* other funcs map to all available gpio pins */
602 *num_groups = priv->chip->n_pins;
603 }
604
605 return 0;
606 }
607
madera_mux_set_mux(struct pinctrl_dev * pctldev,unsigned int selector,unsigned int group)608 static int madera_mux_set_mux(struct pinctrl_dev *pctldev,
609 unsigned int selector,
610 unsigned int group)
611 {
612 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
613 struct madera *madera = priv->madera;
614 const struct madera_pin_groups *pin_group = priv->chip->pin_groups;
615 unsigned int n_chip_groups = priv->chip->n_pin_groups;
616 const char *func_name = madera_mux_funcs[selector].name;
617 unsigned int reg;
618 int i, ret = 0;
619
620 dev_dbg(priv->dev, "%s selecting %u (%s) for group %u (%s)\n",
621 __func__, selector, func_name, group,
622 madera_get_group_name(pctldev, group));
623
624 if (madera_mux_funcs[selector].func == 0) {
625 /* alt func pin assignments are codec-specific */
626 for (i = 0; i < n_chip_groups; ++i) {
627 if (strcmp(func_name, pin_group->name) == 0)
628 break;
629
630 ++pin_group;
631 }
632
633 if (i == n_chip_groups)
634 return -EINVAL;
635
636 for (i = 0; i < pin_group->n_pins; ++i) {
637 reg = MADERA_GPIO1_CTRL_1 + (2 * pin_group->pins[i]);
638
639 dev_dbg(priv->dev, "%s setting 0x%x func bits to 0\n",
640 __func__, reg);
641
642 ret = regmap_update_bits(madera->regmap, reg,
643 MADERA_GP1_FN_MASK, 0);
644 if (ret)
645 break;
646
647 }
648 } else {
649 /*
650 * for other funcs the group will be the gpio number and will
651 * be offset by the number of chip-specific functions at the
652 * start of the group list
653 */
654 group -= n_chip_groups;
655 reg = MADERA_GPIO1_CTRL_1 + (2 * group);
656
657 dev_dbg(priv->dev, "%s setting 0x%x func bits to 0x%x\n",
658 __func__, reg, madera_mux_funcs[selector].func);
659
660 ret = regmap_update_bits(madera->regmap,
661 reg,
662 MADERA_GP1_FN_MASK,
663 madera_mux_funcs[selector].func);
664 }
665
666 if (ret)
667 dev_err(priv->dev, "Failed to write to 0x%x (%d)\n", reg, ret);
668
669 return ret;
670 }
671
madera_gpio_set_direction(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int offset,bool input)672 static int madera_gpio_set_direction(struct pinctrl_dev *pctldev,
673 struct pinctrl_gpio_range *range,
674 unsigned int offset,
675 bool input)
676 {
677 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
678 struct madera *madera = priv->madera;
679 unsigned int reg = MADERA_GPIO1_CTRL_2 + (2 * offset);
680 unsigned int val;
681 int ret;
682
683 if (input)
684 val = MADERA_GP1_DIR;
685 else
686 val = 0;
687
688 ret = regmap_update_bits(madera->regmap, reg, MADERA_GP1_DIR_MASK, val);
689 if (ret)
690 dev_err(priv->dev, "Failed to write to 0x%x (%d)\n", reg, ret);
691
692 return ret;
693 }
694
madera_gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int offset)695 static int madera_gpio_request_enable(struct pinctrl_dev *pctldev,
696 struct pinctrl_gpio_range *range,
697 unsigned int offset)
698 {
699 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
700 struct madera *madera = priv->madera;
701 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * offset);
702 int ret;
703
704 /* put the pin into GPIO mode */
705 ret = regmap_update_bits(madera->regmap, reg, MADERA_GP1_FN_MASK, 1);
706 if (ret)
707 dev_err(priv->dev, "Failed to write to 0x%x (%d)\n", reg, ret);
708
709 return ret;
710 }
711
madera_gpio_disable_free(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int offset)712 static void madera_gpio_disable_free(struct pinctrl_dev *pctldev,
713 struct pinctrl_gpio_range *range,
714 unsigned int offset)
715 {
716 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
717 struct madera *madera = priv->madera;
718 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * offset);
719 int ret;
720
721 /* disable GPIO by setting to GPIO IN */
722 madera_gpio_set_direction(pctldev, range, offset, true);
723
724 ret = regmap_update_bits(madera->regmap, reg, MADERA_GP1_FN_MASK, 1);
725 if (ret)
726 dev_err(priv->dev, "Failed to write to 0x%x (%d)\n", reg, ret);
727 }
728
729 static const struct pinmux_ops madera_pin_mux_ops = {
730 .get_functions_count = madera_mux_get_funcs_count,
731 .get_function_name = madera_mux_get_func_name,
732 .get_function_groups = madera_mux_get_groups,
733 .set_mux = madera_mux_set_mux,
734 .gpio_request_enable = madera_gpio_request_enable,
735 .gpio_disable_free = madera_gpio_disable_free,
736 .gpio_set_direction = madera_gpio_set_direction,
737 .strict = true, /* GPIO and other functions are exclusive */
738 };
739
madera_pin_conf_get(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * config)740 static int madera_pin_conf_get(struct pinctrl_dev *pctldev, unsigned int pin,
741 unsigned long *config)
742 {
743 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
744 unsigned int param = pinconf_to_config_param(*config);
745 unsigned int result = 0;
746 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * pin);
747 unsigned int conf[2];
748 int ret;
749
750 ret = regmap_read(priv->madera->regmap, reg, &conf[0]);
751 if (!ret)
752 ret = regmap_read(priv->madera->regmap, reg + 1, &conf[1]);
753
754 if (ret) {
755 dev_err(priv->dev, "Failed to read GP%d conf (%d)\n",
756 pin + 1, ret);
757 return ret;
758 }
759
760 switch (param) {
761 case PIN_CONFIG_BIAS_BUS_HOLD:
762 conf[1] &= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK;
763 if (conf[1] == (MADERA_GP1_PU | MADERA_GP1_PD))
764 result = 1;
765 break;
766 case PIN_CONFIG_BIAS_DISABLE:
767 conf[1] &= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK;
768 if (!conf[1])
769 result = 1;
770 break;
771 case PIN_CONFIG_BIAS_PULL_DOWN:
772 conf[1] &= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK;
773 if (conf[1] == MADERA_GP1_PD_MASK)
774 result = 1;
775 break;
776 case PIN_CONFIG_BIAS_PULL_UP:
777 conf[1] &= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK;
778 if (conf[1] == MADERA_GP1_PU_MASK)
779 result = 1;
780 break;
781 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
782 if (conf[0] & MADERA_GP1_OP_CFG_MASK)
783 result = 1;
784 break;
785 case PIN_CONFIG_DRIVE_PUSH_PULL:
786 if (!(conf[0] & MADERA_GP1_OP_CFG_MASK))
787 result = 1;
788 break;
789 case PIN_CONFIG_DRIVE_STRENGTH:
790 result = madera_pin_unmake_drv_str(priv, conf[1]);
791 break;
792 case PIN_CONFIG_INPUT_DEBOUNCE:
793 if (conf[0] & MADERA_GP1_DB_MASK)
794 result = 1;
795 break;
796 case PIN_CONFIG_INPUT_ENABLE:
797 if (conf[0] & MADERA_GP1_DIR_MASK)
798 result = 1;
799 break;
800 case PIN_CONFIG_INPUT_SCHMITT:
801 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
802 if (conf[0] & MADERA_GP1_IP_CFG_MASK)
803 result = 1;
804 break;
805 case PIN_CONFIG_OUTPUT:
806 if ((conf[1] & MADERA_GP1_DIR_MASK) &&
807 (conf[0] & MADERA_GP1_LVL_MASK))
808 result = 1;
809 break;
810 default:
811 return -ENOTSUPP;
812 }
813
814 *config = pinconf_to_config_packed(param, result);
815
816 return 0;
817 }
818
madera_pin_conf_set(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * configs,unsigned int num_configs)819 static int madera_pin_conf_set(struct pinctrl_dev *pctldev, unsigned int pin,
820 unsigned long *configs, unsigned int num_configs)
821 {
822 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
823 u16 conf[2] = {0, 0};
824 u16 mask[2] = {0, 0};
825 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * pin);
826 unsigned int val;
827 int ret;
828
829 while (num_configs) {
830 dev_dbg(priv->dev, "%s config 0x%lx\n", __func__, *configs);
831
832 switch (pinconf_to_config_param(*configs)) {
833 case PIN_CONFIG_BIAS_BUS_HOLD:
834 mask[1] |= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK;
835 conf[1] |= MADERA_GP1_PU | MADERA_GP1_PD;
836 break;
837 case PIN_CONFIG_BIAS_DISABLE:
838 mask[1] |= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK;
839 conf[1] &= ~(MADERA_GP1_PU | MADERA_GP1_PD);
840 break;
841 case PIN_CONFIG_BIAS_PULL_DOWN:
842 mask[1] |= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK;
843 conf[1] |= MADERA_GP1_PD;
844 conf[1] &= ~MADERA_GP1_PU;
845 break;
846 case PIN_CONFIG_BIAS_PULL_UP:
847 mask[1] |= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK;
848 conf[1] |= MADERA_GP1_PU;
849 conf[1] &= ~MADERA_GP1_PD;
850 break;
851 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
852 mask[0] |= MADERA_GP1_OP_CFG_MASK;
853 conf[0] |= MADERA_GP1_OP_CFG;
854 break;
855 case PIN_CONFIG_DRIVE_PUSH_PULL:
856 mask[0] |= MADERA_GP1_OP_CFG_MASK;
857 conf[0] &= ~MADERA_GP1_OP_CFG;
858 break;
859 case PIN_CONFIG_DRIVE_STRENGTH:
860 val = pinconf_to_config_argument(*configs);
861 mask[1] |= MADERA_GP1_DRV_STR_MASK;
862 conf[1] &= ~MADERA_GP1_DRV_STR_MASK;
863 conf[1] |= madera_pin_make_drv_str(priv, val);
864 break;
865 case PIN_CONFIG_INPUT_DEBOUNCE:
866 mask[0] |= MADERA_GP1_DB_MASK;
867
868 /*
869 * we can't configure debounce time per-pin so value
870 * is just a flag
871 */
872 val = pinconf_to_config_argument(*configs);
873 if (val)
874 conf[0] |= MADERA_GP1_DB;
875 else
876 conf[0] &= ~MADERA_GP1_DB;
877 break;
878 case PIN_CONFIG_INPUT_ENABLE:
879 val = pinconf_to_config_argument(*configs);
880 mask[1] |= MADERA_GP1_DIR_MASK;
881 if (val)
882 conf[1] |= MADERA_GP1_DIR;
883 else
884 conf[1] &= ~MADERA_GP1_DIR;
885 break;
886 case PIN_CONFIG_INPUT_SCHMITT:
887 val = pinconf_to_config_argument(*configs);
888 mask[0] |= MADERA_GP1_IP_CFG;
889 if (val)
890 conf[0] |= MADERA_GP1_IP_CFG;
891 else
892 conf[0] &= ~MADERA_GP1_IP_CFG;
893
894 mask[1] |= MADERA_GP1_DIR_MASK;
895 conf[1] |= MADERA_GP1_DIR;
896 break;
897 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
898 mask[0] |= MADERA_GP1_IP_CFG;
899 conf[0] |= MADERA_GP1_IP_CFG;
900 mask[1] |= MADERA_GP1_DIR_MASK;
901 conf[1] |= MADERA_GP1_DIR;
902 break;
903 case PIN_CONFIG_OUTPUT:
904 val = pinconf_to_config_argument(*configs);
905 mask[0] |= MADERA_GP1_LVL_MASK;
906 if (val)
907 conf[0] |= MADERA_GP1_LVL;
908 else
909 conf[0] &= ~MADERA_GP1_LVL;
910
911 mask[1] |= MADERA_GP1_DIR_MASK;
912 conf[1] &= ~MADERA_GP1_DIR;
913 break;
914 default:
915 return -ENOTSUPP;
916 }
917
918 ++configs;
919 --num_configs;
920 }
921
922 dev_dbg(priv->dev,
923 "%s gpio%d 0x%x:0x%x 0x%x:0x%x\n",
924 __func__, pin + 1, reg, conf[0], reg + 1, conf[1]);
925
926 ret = regmap_update_bits(priv->madera->regmap, reg, mask[0], conf[0]);
927 if (ret)
928 goto err;
929
930 ++reg;
931 ret = regmap_update_bits(priv->madera->regmap, reg, mask[1], conf[1]);
932 if (ret)
933 goto err;
934
935 return 0;
936
937 err:
938 dev_err(priv->dev,
939 "Failed to write GPIO%d conf (%d) reg 0x%x\n",
940 pin + 1, ret, reg);
941
942 return ret;
943 }
944
madera_pin_conf_group_set(struct pinctrl_dev * pctldev,unsigned int selector,unsigned long * configs,unsigned int num_configs)945 static int madera_pin_conf_group_set(struct pinctrl_dev *pctldev,
946 unsigned int selector,
947 unsigned long *configs,
948 unsigned int num_configs)
949 {
950 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev);
951 const struct madera_pin_groups *pin_group;
952 unsigned int n_groups = priv->chip->n_pin_groups;
953 int i, ret;
954
955 dev_dbg(priv->dev, "%s setting group %s\n", __func__,
956 madera_get_group_name(pctldev, selector));
957
958 if (selector >= n_groups) {
959 /* group is a single pin, convert to pin number and set */
960 return madera_pin_conf_set(pctldev,
961 selector - n_groups,
962 configs,
963 num_configs);
964 } else {
965 pin_group = &priv->chip->pin_groups[selector];
966
967 for (i = 0; i < pin_group->n_pins; ++i) {
968 ret = madera_pin_conf_set(pctldev,
969 pin_group->pins[i],
970 configs,
971 num_configs);
972 if (ret)
973 return ret;
974 }
975 }
976
977 return 0;
978 }
979
980 static const struct pinconf_ops madera_pin_conf_ops = {
981 .is_generic = true,
982 .pin_config_get = madera_pin_conf_get,
983 .pin_config_set = madera_pin_conf_set,
984 .pin_config_group_set = madera_pin_conf_group_set,
985 };
986
987 static struct pinctrl_desc madera_pin_desc = {
988 .name = "madera-pinctrl",
989 .pins = madera_pins,
990 .pctlops = &madera_pin_group_ops,
991 .pmxops = &madera_pin_mux_ops,
992 .confops = &madera_pin_conf_ops,
993 .owner = THIS_MODULE,
994 };
995
madera_pin_probe(struct platform_device * pdev)996 static int madera_pin_probe(struct platform_device *pdev)
997 {
998 struct madera *madera = dev_get_drvdata(pdev->dev.parent);
999 const struct madera_pdata *pdata = &madera->pdata;
1000 struct madera_pin_private *priv;
1001 int ret;
1002
1003 BUILD_BUG_ON(ARRAY_SIZE(madera_pin_single_group_names) !=
1004 ARRAY_SIZE(madera_pin_single_group_pins));
1005
1006 dev_dbg(&pdev->dev, "%s\n", __func__);
1007
1008 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1009 if (!priv)
1010 return -ENOMEM;
1011
1012 priv->dev = &pdev->dev;
1013 priv->madera = madera;
1014 pdev->dev.of_node = madera->dev->of_node;
1015
1016 switch (madera->type) {
1017 case CS47L15:
1018 if (IS_ENABLED(CONFIG_PINCTRL_CS47L15))
1019 priv->chip = &cs47l15_pin_chip;
1020 break;
1021 case CS47L35:
1022 if (IS_ENABLED(CONFIG_PINCTRL_CS47L35))
1023 priv->chip = &cs47l35_pin_chip;
1024 break;
1025 case CS47L85:
1026 case WM1840:
1027 if (IS_ENABLED(CONFIG_PINCTRL_CS47L85))
1028 priv->chip = &cs47l85_pin_chip;
1029 break;
1030 case CS47L90:
1031 case CS47L91:
1032 if (IS_ENABLED(CONFIG_PINCTRL_CS47L90))
1033 priv->chip = &cs47l90_pin_chip;
1034 break;
1035 case CS42L92:
1036 case CS47L92:
1037 case CS47L93:
1038 if (IS_ENABLED(CONFIG_PINCTRL_CS47L92))
1039 priv->chip = &cs47l92_pin_chip;
1040 break;
1041 default:
1042 break;
1043 }
1044
1045 if (!priv->chip)
1046 return -ENODEV;
1047
1048 madera_pin_desc.npins = priv->chip->n_pins;
1049
1050 ret = devm_pinctrl_register_and_init(&pdev->dev,
1051 &madera_pin_desc,
1052 priv,
1053 &priv->pctl);
1054 if (ret) {
1055 dev_err(priv->dev, "Failed pinctrl register (%d)\n", ret);
1056 return ret;
1057 }
1058
1059 /* if the configuration is provided through pdata, apply it */
1060 if (pdata->gpio_configs) {
1061 ret = pinctrl_register_mappings(pdata->gpio_configs,
1062 pdata->n_gpio_configs);
1063 if (ret) {
1064 dev_err(priv->dev,
1065 "Failed to register pdata mappings (%d)\n",
1066 ret);
1067 return ret;
1068 }
1069 }
1070
1071 ret = pinctrl_enable(priv->pctl);
1072 if (ret) {
1073 dev_err(priv->dev, "Failed to enable pinctrl (%d)\n", ret);
1074 return ret;
1075 }
1076
1077 dev_dbg(priv->dev, "pinctrl probed ok\n");
1078
1079 return 0;
1080 }
1081
1082 static struct platform_driver madera_pin_driver = {
1083 .probe = madera_pin_probe,
1084 .driver = {
1085 .name = "madera-pinctrl",
1086 },
1087 };
1088
1089 module_platform_driver(madera_pin_driver);
1090
1091 MODULE_DESCRIPTION("Madera pinctrl driver");
1092 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
1093 MODULE_LICENSE("GPL v2");
1094