blob: 7c0f5d4e89f3ace973c5e1c4573119215cdd9037 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's SoC's.
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 * Copyright (c) 2012 Linaro Ltd
7 * http://www.linaro.org
8 *
9 * Author: Thomas Abraham <thomas.ab@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This driver implements the Samsung pinctrl driver. It supports setting up of
17 * pinmux and pinconf configurations. The gpiolib interface is also included.
18 * External interrupt (gpio and wakeup) support are not included in this driver
19 * but provides extensions to which platform specific implementation of the gpio
20 * and wakeup interrupts can be hooked to.
21 */
22
23#include <linux/init.h>
24#include <linux/platform_device.h>
25#include <linux/io.h>
26#include <linux/slab.h>
27#include <linux/err.h>
28#include <linux/gpio.h>
29#include <linux/irqdomain.h>
30#include <linux/of_device.h>
31#include <linux/spinlock.h>
32
33#include <dt-bindings/pinctrl/samsung.h>
34
35#include "../core.h"
36#include "pinctrl-samsung.h"
37
38/* maximum number of the memory resources */
39#define SAMSUNG_PINCTRL_NUM_RESOURCES 2
40
41/* list of all possible config options supported */
42static struct pin_config {
43 const char *property;
44 enum pincfg_type param;
45} cfg_params[] = {
46 { "samsung,pin-pud", PINCFG_TYPE_PUD },
47 { "samsung,pin-drv", PINCFG_TYPE_DRV },
48 { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN },
49 { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN },
50 { "samsung,pin-val", PINCFG_TYPE_DAT },
51};
52
53static unsigned int pin_base;
54
55static int samsung_get_group_count(struct pinctrl_dev *pctldev)
56{
57 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
58
59 return pmx->nr_groups;
60}
61
62static const char *samsung_get_group_name(struct pinctrl_dev *pctldev,
63 unsigned group)
64{
65 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
66
67 return pmx->pin_groups[group].name;
68}
69
70static int samsung_get_group_pins(struct pinctrl_dev *pctldev,
71 unsigned group,
72 const unsigned **pins,
73 unsigned *num_pins)
74{
75 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
76
77 *pins = pmx->pin_groups[group].pins;
78 *num_pins = pmx->pin_groups[group].num_pins;
79
80 return 0;
81}
82
83static int reserve_map(struct device *dev, struct pinctrl_map **map,
84 unsigned *reserved_maps, unsigned *num_maps,
85 unsigned reserve)
86{
87 unsigned old_num = *reserved_maps;
88 unsigned new_num = *num_maps + reserve;
89 struct pinctrl_map *new_map;
90
91 if (old_num >= new_num)
92 return 0;
93
94 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
95 if (!new_map)
96 return -ENOMEM;
97
98 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
99
100 *map = new_map;
101 *reserved_maps = new_num;
102
103 return 0;
104}
105
106static int add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
107 unsigned *num_maps, const char *group,
108 const char *function)
109{
110 if (WARN_ON(*num_maps == *reserved_maps))
111 return -ENOSPC;
112
113 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
114 (*map)[*num_maps].data.mux.group = group;
115 (*map)[*num_maps].data.mux.function = function;
116 (*num_maps)++;
117
118 return 0;
119}
120
121static int add_map_configs(struct device *dev, struct pinctrl_map **map,
122 unsigned *reserved_maps, unsigned *num_maps,
123 const char *group, unsigned long *configs,
124 unsigned num_configs)
125{
126 unsigned long *dup_configs;
127
128 if (WARN_ON(*num_maps == *reserved_maps))
129 return -ENOSPC;
130
131 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
132 GFP_KERNEL);
133 if (!dup_configs)
134 return -ENOMEM;
135
136 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
137 (*map)[*num_maps].data.configs.group_or_pin = group;
138 (*map)[*num_maps].data.configs.configs = dup_configs;
139 (*map)[*num_maps].data.configs.num_configs = num_configs;
140 (*num_maps)++;
141
142 return 0;
143}
144
145static int add_config(struct device *dev, unsigned long **configs,
146 unsigned *num_configs, unsigned long config)
147{
148 unsigned old_num = *num_configs;
149 unsigned new_num = old_num + 1;
150 unsigned long *new_configs;
151
152 new_configs = krealloc(*configs, sizeof(*new_configs) * new_num,
153 GFP_KERNEL);
154 if (!new_configs)
155 return -ENOMEM;
156
157 new_configs[old_num] = config;
158
159 *configs = new_configs;
160 *num_configs = new_num;
161
162 return 0;
163}
164
165static void samsung_dt_free_map(struct pinctrl_dev *pctldev,
166 struct pinctrl_map *map,
167 unsigned num_maps)
168{
169 int i;
170
171 for (i = 0; i < num_maps; i++)
172 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
173 kfree(map[i].data.configs.configs);
174
175 kfree(map);
176}
177
178static int samsung_dt_subnode_to_map(struct samsung_pinctrl_drv_data *drvdata,
179 struct device *dev,
180 struct device_node *np,
181 struct pinctrl_map **map,
182 unsigned *reserved_maps,
183 unsigned *num_maps)
184{
185 int ret, i;
186 u32 val;
187 unsigned long config;
188 unsigned long *configs = NULL;
189 unsigned num_configs = 0;
190 unsigned reserve;
191 struct property *prop;
192 const char *group;
193 bool has_func = false;
194
195 ret = of_property_read_u32(np, "samsung,pin-function", &val);
196 if (!ret)
197 has_func = true;
198
199 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
200 ret = of_property_read_u32(np, cfg_params[i].property, &val);
201 if (!ret) {
202 config = PINCFG_PACK(cfg_params[i].param, val);
203 ret = add_config(dev, &configs, &num_configs, config);
204 if (ret < 0)
205 goto exit;
206 /* EINVAL=missing, which is fine since it's optional */
207 } else if (ret != -EINVAL) {
208 dev_err(dev, "could not parse property %s\n",
209 cfg_params[i].property);
210 }
211 }
212
213 reserve = 0;
214 if (has_func)
215 reserve++;
216 if (num_configs)
217 reserve++;
218 ret = of_property_count_strings(np, "samsung,pins");
219 if (ret < 0) {
220 dev_err(dev, "could not parse property samsung,pins\n");
221 goto exit;
222 }
223 reserve *= ret;
224
225 ret = reserve_map(dev, map, reserved_maps, num_maps, reserve);
226 if (ret < 0)
227 goto exit;
228
229 of_property_for_each_string(np, "samsung,pins", prop, group) {
230 if (has_func) {
231 ret = add_map_mux(map, reserved_maps,
232 num_maps, group, np->full_name);
233 if (ret < 0)
234 goto exit;
235 }
236
237 if (num_configs) {
238 ret = add_map_configs(dev, map, reserved_maps,
239 num_maps, group, configs,
240 num_configs);
241 if (ret < 0)
242 goto exit;
243 }
244 }
245
246 ret = 0;
247
248exit:
249 kfree(configs);
250 return ret;
251}
252
253static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev,
254 struct device_node *np_config,
255 struct pinctrl_map **map,
256 unsigned *num_maps)
257{
258 struct samsung_pinctrl_drv_data *drvdata;
259 unsigned reserved_maps;
260 struct device_node *np;
261 int ret;
262
263 drvdata = pinctrl_dev_get_drvdata(pctldev);
264
265 reserved_maps = 0;
266 *map = NULL;
267 *num_maps = 0;
268
269 if (!of_get_child_count(np_config))
270 return samsung_dt_subnode_to_map(drvdata, pctldev->dev,
271 np_config, map,
272 &reserved_maps,
273 num_maps);
274
275 for_each_child_of_node(np_config, np) {
276 ret = samsung_dt_subnode_to_map(drvdata, pctldev->dev, np, map,
277 &reserved_maps, num_maps);
278 if (ret < 0) {
279 samsung_dt_free_map(pctldev, *map, *num_maps);
280 of_node_put(np);
281 return ret;
282 }
283 }
284
285 return 0;
286}
287
288/* list of pinctrl callbacks for the pinctrl core */
289static const struct pinctrl_ops samsung_pctrl_ops = {
290 .get_groups_count = samsung_get_group_count,
291 .get_group_name = samsung_get_group_name,
292 .get_group_pins = samsung_get_group_pins,
293 .dt_node_to_map = samsung_dt_node_to_map,
294 .dt_free_map = samsung_dt_free_map,
295};
296
297/* check if the selector is a valid pin function selector */
298static int samsung_get_functions_count(struct pinctrl_dev *pctldev)
299{
300 struct samsung_pinctrl_drv_data *drvdata;
301
302 drvdata = pinctrl_dev_get_drvdata(pctldev);
303 return drvdata->nr_functions;
304}
305
306/* return the name of the pin function specified */
307static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev,
308 unsigned selector)
309{
310 struct samsung_pinctrl_drv_data *drvdata;
311
312 drvdata = pinctrl_dev_get_drvdata(pctldev);
313 return drvdata->pmx_functions[selector].name;
314}
315
316/* return the groups associated for the specified function selector */
317static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev,
318 unsigned selector, const char * const **groups,
319 unsigned * const num_groups)
320{
321 struct samsung_pinctrl_drv_data *drvdata;
322
323 drvdata = pinctrl_dev_get_drvdata(pctldev);
324 *groups = drvdata->pmx_functions[selector].groups;
325 *num_groups = drvdata->pmx_functions[selector].num_groups;
326 return 0;
327}
328
329/*
330 * given a pin number that is local to a pin controller, find out the pin bank
331 * and the register base of the pin bank.
332 */
333static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata,
334 unsigned pin, void __iomem **reg, u32 *offset,
335 struct samsung_pin_bank **bank)
336{
337 struct samsung_pin_bank *b;
338
339 b = drvdata->pin_banks;
340
341 while ((pin >= b->pin_base) &&
342 ((b->pin_base + b->nr_pins - 1) < pin))
343 b++;
344
345 *reg = b->pctl_base + b->pctl_offset;
346 *offset = pin - b->pin_base;
347 if (bank)
348 *bank = b;
349}
350
351/* enable or disable a pinmux function */
352static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
353 unsigned group)
354{
355 struct samsung_pinctrl_drv_data *drvdata;
356 const struct samsung_pin_bank_type *type;
357 struct samsung_pin_bank *bank;
358 void __iomem *reg;
359 u32 mask, shift, data, pin_offset;
360 unsigned long flags;
361 const struct samsung_pmx_func *func;
362 const struct samsung_pin_group *grp;
363
364 drvdata = pinctrl_dev_get_drvdata(pctldev);
365 func = &drvdata->pmx_functions[selector];
366 grp = &drvdata->pin_groups[group];
367
368 pin_to_reg_bank(drvdata, grp->pins[0] - drvdata->pin_base,
369 &reg, &pin_offset, &bank);
370 type = bank->type;
371 mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1;
372 shift = pin_offset * type->fld_width[PINCFG_TYPE_FUNC];
373 if (shift >= 32) {
374 /* Some banks have two config registers */
375 shift -= 32;
376 reg += 4;
377 }
378
379 spin_lock_irqsave(&bank->slock, flags);
380
381 data = readl(reg + type->reg_offset[PINCFG_TYPE_FUNC]);
382 data &= ~(mask << shift);
383 data |= func->val << shift;
384 writel(data, reg + type->reg_offset[PINCFG_TYPE_FUNC]);
385
386 spin_unlock_irqrestore(&bank->slock, flags);
387}
388
389/* enable a specified pinmux by writing to registers */
390static int samsung_pinmux_set_mux(struct pinctrl_dev *pctldev,
391 unsigned selector,
392 unsigned group)
393{
394 samsung_pinmux_setup(pctldev, selector, group);
395 return 0;
396}
397
398/* list of pinmux callbacks for the pinmux vertical in pinctrl core */
399static const struct pinmux_ops samsung_pinmux_ops = {
400 .get_functions_count = samsung_get_functions_count,
401 .get_function_name = samsung_pinmux_get_fname,
402 .get_function_groups = samsung_pinmux_get_groups,
403 .set_mux = samsung_pinmux_set_mux,
404};
405
406/* set or get the pin config settings for a specified pin */
407static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin,
408 unsigned long *config, bool set)
409{
410 struct samsung_pinctrl_drv_data *drvdata;
411 const struct samsung_pin_bank_type *type;
412 struct samsung_pin_bank *bank;
413 void __iomem *reg_base;
414 enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
415 u32 data, width, pin_offset, mask, shift;
416 u32 cfg_value, cfg_reg;
417 unsigned long flags;
418
419 drvdata = pinctrl_dev_get_drvdata(pctldev);
420 pin_to_reg_bank(drvdata, pin - drvdata->pin_base, &reg_base,
421 &pin_offset, &bank);
422 type = bank->type;
423
424 if (cfg_type >= PINCFG_TYPE_NUM || !type->fld_width[cfg_type])
425 return -EINVAL;
426
427 width = type->fld_width[cfg_type];
428 cfg_reg = type->reg_offset[cfg_type];
429
430 spin_lock_irqsave(&bank->slock, flags);
431
432 mask = (1 << width) - 1;
433 shift = pin_offset * width;
434 data = readl(reg_base + cfg_reg);
435
436 if (set) {
437 cfg_value = PINCFG_UNPACK_VALUE(*config);
438 data &= ~(mask << shift);
439 data |= (cfg_value << shift);
440 writel(data, reg_base + cfg_reg);
441 } else {
442 data >>= shift;
443 data &= mask;
444 *config = PINCFG_PACK(cfg_type, data);
445 }
446
447 spin_unlock_irqrestore(&bank->slock, flags);
448
449 return 0;
450}
451
452/* set the pin config settings for a specified pin */
453static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
454 unsigned long *configs, unsigned num_configs)
455{
456 int i, ret;
457
458 for (i = 0; i < num_configs; i++) {
459 ret = samsung_pinconf_rw(pctldev, pin, &configs[i], true);
460 if (ret < 0)
461 return ret;
462 } /* for each config */
463
464 return 0;
465}
466
467/* get the pin config settings for a specified pin */
468static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
469 unsigned long *config)
470{
471 return samsung_pinconf_rw(pctldev, pin, config, false);
472}
473
474/* set the pin config settings for a specified pin group */
475static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
476 unsigned group, unsigned long *configs,
477 unsigned num_configs)
478{
479 struct samsung_pinctrl_drv_data *drvdata;
480 const unsigned int *pins;
481 unsigned int cnt;
482
483 drvdata = pinctrl_dev_get_drvdata(pctldev);
484 pins = drvdata->pin_groups[group].pins;
485
486 for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
487 samsung_pinconf_set(pctldev, pins[cnt], configs, num_configs);
488
489 return 0;
490}
491
492/* get the pin config settings for a specified pin group */
493static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
494 unsigned int group, unsigned long *config)
495{
496 struct samsung_pinctrl_drv_data *drvdata;
497 const unsigned int *pins;
498
499 drvdata = pinctrl_dev_get_drvdata(pctldev);
500 pins = drvdata->pin_groups[group].pins;
501 samsung_pinconf_get(pctldev, pins[0], config);
502 return 0;
503}
504
505/* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
506static const struct pinconf_ops samsung_pinconf_ops = {
507 .pin_config_get = samsung_pinconf_get,
508 .pin_config_set = samsung_pinconf_set,
509 .pin_config_group_get = samsung_pinconf_group_get,
510 .pin_config_group_set = samsung_pinconf_group_set,
511};
512
513/*
514 * The samsung_gpio_set_vlaue() should be called with "bank->slock" held
515 * to avoid race condition.
516 */
517static void samsung_gpio_set_value(struct gpio_chip *gc,
518 unsigned offset, int value)
519{
520 struct samsung_pin_bank *bank = gpiochip_get_data(gc);
521 const struct samsung_pin_bank_type *type = bank->type;
522 void __iomem *reg;
523 u32 data;
524
525 reg = bank->pctl_base + bank->pctl_offset;
526
527 data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]);
528 data &= ~(1 << offset);
529 if (value)
530 data |= 1 << offset;
531 writel(data, reg + type->reg_offset[PINCFG_TYPE_DAT]);
532}
533
534/* gpiolib gpio_set callback function */
535static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
536{
537 struct samsung_pin_bank *bank = gpiochip_get_data(gc);
538 unsigned long flags;
539
540 spin_lock_irqsave(&bank->slock, flags);
541 samsung_gpio_set_value(gc, offset, value);
542 spin_unlock_irqrestore(&bank->slock, flags);
543}
544
545/* gpiolib gpio_get callback function */
546static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
547{
548 void __iomem *reg;
549 u32 data;
550 struct samsung_pin_bank *bank = gpiochip_get_data(gc);
551 const struct samsung_pin_bank_type *type = bank->type;
552
553 reg = bank->pctl_base + bank->pctl_offset;
554
555 data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]);
556 data >>= offset;
557 data &= 1;
558 return data;
559}
560
561/*
562 * The samsung_gpio_set_direction() should be called with "bank->slock" held
563 * to avoid race condition.
564 * The calls to gpio_direction_output() and gpio_direction_input()
565 * leads to this function call.
566 */
567static int samsung_gpio_set_direction(struct gpio_chip *gc,
568 unsigned offset, bool input)
569{
570 const struct samsung_pin_bank_type *type;
571 struct samsung_pin_bank *bank;
572 void __iomem *reg;
573 u32 data, mask, shift;
574
575 bank = gpiochip_get_data(gc);
576 type = bank->type;
577
578 reg = bank->pctl_base + bank->pctl_offset
579 + type->reg_offset[PINCFG_TYPE_FUNC];
580
581 mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1;
582 shift = offset * type->fld_width[PINCFG_TYPE_FUNC];
583 if (shift >= 32) {
584 /* Some banks have two config registers */
585 shift -= 32;
586 reg += 4;
587 }
588
589 data = readl(reg);
590 data &= ~(mask << shift);
591 if (!input)
592 data |= EXYNOS_PIN_FUNC_OUTPUT << shift;
593 writel(data, reg);
594
595 return 0;
596}
597
598/* gpiolib gpio_direction_input callback function. */
599static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
600{
601 struct samsung_pin_bank *bank = gpiochip_get_data(gc);
602 unsigned long flags;
603 int ret;
604
605 spin_lock_irqsave(&bank->slock, flags);
606 ret = samsung_gpio_set_direction(gc, offset, true);
607 spin_unlock_irqrestore(&bank->slock, flags);
608 return ret;
609}
610
611/* gpiolib gpio_direction_output callback function. */
612static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
613 int value)
614{
615 struct samsung_pin_bank *bank = gpiochip_get_data(gc);
616 unsigned long flags;
617 int ret;
618
619 spin_lock_irqsave(&bank->slock, flags);
620 samsung_gpio_set_value(gc, offset, value);
621 ret = samsung_gpio_set_direction(gc, offset, false);
622 spin_unlock_irqrestore(&bank->slock, flags);
623
624 return ret;
625}
626
627/*
628 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
629 * and a virtual IRQ, if not already present.
630 */
631static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
632{
633 struct samsung_pin_bank *bank = gpiochip_get_data(gc);
634 unsigned int virq;
635
636 if (!bank->irq_domain)
637 return -ENXIO;
638
639 virq = irq_create_mapping(bank->irq_domain, offset);
640
641 return (virq) ? : -ENXIO;
642}
643
644static struct samsung_pin_group *samsung_pinctrl_create_groups(
645 struct device *dev,
646 struct samsung_pinctrl_drv_data *drvdata,
647 unsigned int *cnt)
648{
649 struct pinctrl_desc *ctrldesc = &drvdata->pctl;
650 struct samsung_pin_group *groups, *grp;
651 const struct pinctrl_pin_desc *pdesc;
652 int i;
653
654 groups = devm_kzalloc(dev, ctrldesc->npins * sizeof(*groups),
655 GFP_KERNEL);
656 if (!groups)
657 return ERR_PTR(-EINVAL);
658 grp = groups;
659
660 pdesc = ctrldesc->pins;
661 for (i = 0; i < ctrldesc->npins; ++i, ++pdesc, ++grp) {
662 grp->name = pdesc->name;
663 grp->pins = &pdesc->number;
664 grp->num_pins = 1;
665 }
666
667 *cnt = ctrldesc->npins;
668 return groups;
669}
670
671static int samsung_pinctrl_create_function(struct device *dev,
672 struct samsung_pinctrl_drv_data *drvdata,
673 struct device_node *func_np,
674 struct samsung_pmx_func *func)
675{
676 int npins;
677 int ret;
678 int i;
679
680 if (of_property_read_u32(func_np, "samsung,pin-function", &func->val))
681 return 0;
682
683 npins = of_property_count_strings(func_np, "samsung,pins");
684 if (npins < 1) {
685 dev_err(dev, "invalid pin list in %pOFn node", func_np);
686 return -EINVAL;
687 }
688
689 func->name = func_np->full_name;
690
691 func->groups = devm_kzalloc(dev, npins * sizeof(char *), GFP_KERNEL);
692 if (!func->groups)
693 return -ENOMEM;
694
695 for (i = 0; i < npins; ++i) {
696 const char *gname;
697
698 ret = of_property_read_string_index(func_np, "samsung,pins",
699 i, &gname);
700 if (ret) {
701 dev_err(dev,
702 "failed to read pin name %d from %pOFn node\n",
703 i, func_np);
704 return ret;
705 }
706
707 func->groups[i] = gname;
708 }
709
710 func->num_groups = npins;
711 return 1;
712}
713
714static struct samsung_pmx_func *samsung_pinctrl_create_functions(
715 struct device *dev,
716 struct samsung_pinctrl_drv_data *drvdata,
717 unsigned int *cnt)
718{
719 struct samsung_pmx_func *functions, *func;
720 struct device_node *dev_np = dev->of_node;
721 struct device_node *cfg_np;
722 unsigned int func_cnt = 0;
723 int ret;
724
725 /*
726 * Iterate over all the child nodes of the pin controller node
727 * and create pin groups and pin function lists.
728 */
729 for_each_child_of_node(dev_np, cfg_np) {
730 struct device_node *func_np;
731
732 if (!of_get_child_count(cfg_np)) {
733 if (!of_find_property(cfg_np,
734 "samsung,pin-function", NULL))
735 continue;
736 ++func_cnt;
737 continue;
738 }
739
740 for_each_child_of_node(cfg_np, func_np) {
741 if (!of_find_property(func_np,
742 "samsung,pin-function", NULL))
743 continue;
744 ++func_cnt;
745 }
746 }
747
748 functions = devm_kzalloc(dev, func_cnt * sizeof(*functions),
749 GFP_KERNEL);
750 if (!functions)
751 return ERR_PTR(-ENOMEM);
752 func = functions;
753
754 /*
755 * Iterate over all the child nodes of the pin controller node
756 * and create pin groups and pin function lists.
757 */
758 func_cnt = 0;
759 for_each_child_of_node(dev_np, cfg_np) {
760 struct device_node *func_np;
761
762 if (!of_get_child_count(cfg_np)) {
763 ret = samsung_pinctrl_create_function(dev, drvdata,
764 cfg_np, func);
765 if (ret < 0) {
766 of_node_put(cfg_np);
767 return ERR_PTR(ret);
768 }
769 if (ret > 0) {
770 ++func;
771 ++func_cnt;
772 }
773 continue;
774 }
775
776 for_each_child_of_node(cfg_np, func_np) {
777 ret = samsung_pinctrl_create_function(dev, drvdata,
778 func_np, func);
779 if (ret < 0) {
780 of_node_put(func_np);
781 of_node_put(cfg_np);
782 return ERR_PTR(ret);
783 }
784 if (ret > 0) {
785 ++func;
786 ++func_cnt;
787 }
788 }
789 }
790
791 *cnt = func_cnt;
792 return functions;
793}
794
795/*
796 * Parse the information about all the available pin groups and pin functions
797 * from device node of the pin-controller. A pin group is formed with all
798 * the pins listed in the "samsung,pins" property.
799 */
800
801static int samsung_pinctrl_parse_dt(struct platform_device *pdev,
802 struct samsung_pinctrl_drv_data *drvdata)
803{
804 struct device *dev = &pdev->dev;
805 struct samsung_pin_group *groups;
806 struct samsung_pmx_func *functions;
807 unsigned int grp_cnt = 0, func_cnt = 0;
808
809 groups = samsung_pinctrl_create_groups(dev, drvdata, &grp_cnt);
810 if (IS_ERR(groups)) {
811 dev_err(dev, "failed to parse pin groups\n");
812 return PTR_ERR(groups);
813 }
814
815 functions = samsung_pinctrl_create_functions(dev, drvdata, &func_cnt);
816 if (IS_ERR(functions)) {
817 dev_err(dev, "failed to parse pin functions\n");
818 return PTR_ERR(functions);
819 }
820
821 drvdata->pin_groups = groups;
822 drvdata->nr_groups = grp_cnt;
823 drvdata->pmx_functions = functions;
824 drvdata->nr_functions = func_cnt;
825
826 return 0;
827}
828
829/* register the pinctrl interface with the pinctrl subsystem */
830static int samsung_pinctrl_register(struct platform_device *pdev,
831 struct samsung_pinctrl_drv_data *drvdata)
832{
833 struct pinctrl_desc *ctrldesc = &drvdata->pctl;
834 struct pinctrl_pin_desc *pindesc, *pdesc;
835 struct samsung_pin_bank *pin_bank;
836 char *pin_names;
837 int pin, bank, ret;
838
839 ctrldesc->name = "samsung-pinctrl";
840 ctrldesc->owner = THIS_MODULE;
841 ctrldesc->pctlops = &samsung_pctrl_ops;
842 ctrldesc->pmxops = &samsung_pinmux_ops;
843 ctrldesc->confops = &samsung_pinconf_ops;
844
845 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
846 drvdata->nr_pins, GFP_KERNEL);
847 if (!pindesc)
848 return -ENOMEM;
849 ctrldesc->pins = pindesc;
850 ctrldesc->npins = drvdata->nr_pins;
851
852 /* dynamically populate the pin number and pin name for pindesc */
853 for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
854 pdesc->number = pin + drvdata->pin_base;
855
856 /*
857 * allocate space for storing the dynamically generated names for all
858 * the pins which belong to this pin-controller.
859 */
860 pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
861 drvdata->nr_pins, GFP_KERNEL);
862 if (!pin_names)
863 return -ENOMEM;
864
865 /* for each pin, the name of the pin is pin-bank name + pin number */
866 for (bank = 0; bank < drvdata->nr_banks; bank++) {
867 pin_bank = &drvdata->pin_banks[bank];
868 for (pin = 0; pin < pin_bank->nr_pins; pin++) {
869 sprintf(pin_names, "%s-%d", pin_bank->name, pin);
870 pdesc = pindesc + pin_bank->pin_base + pin;
871 pdesc->name = pin_names;
872 pin_names += PIN_NAME_LENGTH;
873 }
874 }
875
876 ret = samsung_pinctrl_parse_dt(pdev, drvdata);
877 if (ret)
878 return ret;
879
880 drvdata->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc,
881 drvdata);
882 if (IS_ERR(drvdata->pctl_dev)) {
883 dev_err(&pdev->dev, "could not register pinctrl driver\n");
884 return PTR_ERR(drvdata->pctl_dev);
885 }
886
887 for (bank = 0; bank < drvdata->nr_banks; ++bank) {
888 pin_bank = &drvdata->pin_banks[bank];
889 pin_bank->grange.name = pin_bank->name;
890 pin_bank->grange.id = bank;
891 pin_bank->grange.pin_base = drvdata->pin_base
892 + pin_bank->pin_base;
893 pin_bank->grange.base = pin_bank->grange.pin_base;
894 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
895 pin_bank->grange.gc = &pin_bank->gpio_chip;
896 pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange);
897 }
898
899 return 0;
900}
901
902/* unregister the pinctrl interface with the pinctrl subsystem */
903static int samsung_pinctrl_unregister(struct platform_device *pdev,
904 struct samsung_pinctrl_drv_data *drvdata)
905{
906 struct samsung_pin_bank *bank = drvdata->pin_banks;
907 int i;
908
909 for (i = 0; i < drvdata->nr_banks; ++i, ++bank)
910 pinctrl_remove_gpio_range(drvdata->pctl_dev, &bank->grange);
911
912 return 0;
913}
914
915static const struct gpio_chip samsung_gpiolib_chip = {
916 .request = gpiochip_generic_request,
917 .free = gpiochip_generic_free,
918 .set = samsung_gpio_set,
919 .get = samsung_gpio_get,
920 .direction_input = samsung_gpio_direction_input,
921 .direction_output = samsung_gpio_direction_output,
922 .to_irq = samsung_gpio_to_irq,
923 .owner = THIS_MODULE,
924};
925
926/* register the gpiolib interface with the gpiolib subsystem */
927static int samsung_gpiolib_register(struct platform_device *pdev,
928 struct samsung_pinctrl_drv_data *drvdata)
929{
930 struct samsung_pin_bank *bank = drvdata->pin_banks;
931 struct gpio_chip *gc;
932 int ret;
933 int i;
934
935 for (i = 0; i < drvdata->nr_banks; ++i, ++bank) {
936 bank->gpio_chip = samsung_gpiolib_chip;
937
938 gc = &bank->gpio_chip;
939 gc->base = bank->grange.base;
940 gc->ngpio = bank->nr_pins;
941 gc->parent = &pdev->dev;
942 gc->of_node = bank->of_node;
943 gc->label = bank->name;
944
945 ret = devm_gpiochip_add_data(&pdev->dev, gc, bank);
946 if (ret) {
947 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
948 gc->label, ret);
949 return ret;
950 }
951 }
952
953 return 0;
954}
955
956static const struct samsung_pin_ctrl *
957samsung_pinctrl_get_soc_data_for_of_alias(struct platform_device *pdev)
958{
959 struct device_node *node = pdev->dev.of_node;
960 const struct samsung_pinctrl_of_match_data *of_data;
961 int id;
962
963 id = of_alias_get_id(node, "pinctrl");
964 if (id < 0) {
965 dev_err(&pdev->dev, "failed to get alias id\n");
966 return NULL;
967 }
968
969 of_data = of_device_get_match_data(&pdev->dev);
970 if (id >= of_data->num_ctrl) {
971 dev_err(&pdev->dev, "invalid alias id %d\n", id);
972 return NULL;
973 }
974
975 return &(of_data->ctrl[id]);
976}
977
978/* retrieve the soc specific data */
979static const struct samsung_pin_ctrl *
980samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data *d,
981 struct platform_device *pdev)
982{
983 struct device_node *node = pdev->dev.of_node;
984 struct device_node *np;
985 const struct samsung_pin_bank_data *bdata;
986 const struct samsung_pin_ctrl *ctrl;
987 struct samsung_pin_bank *bank;
988 struct resource *res;
989 void __iomem *virt_base[SAMSUNG_PINCTRL_NUM_RESOURCES];
990 unsigned int i;
991
992 ctrl = samsung_pinctrl_get_soc_data_for_of_alias(pdev);
993 if (!ctrl)
994 return ERR_PTR(-ENOENT);
995
996 d->suspend = ctrl->suspend;
997 d->resume = ctrl->resume;
998 d->nr_banks = ctrl->nr_banks;
999 d->pin_banks = devm_kcalloc(&pdev->dev, d->nr_banks,
1000 sizeof(*d->pin_banks), GFP_KERNEL);
1001 if (!d->pin_banks)
1002 return ERR_PTR(-ENOMEM);
1003
1004 if (ctrl->nr_ext_resources + 1 > SAMSUNG_PINCTRL_NUM_RESOURCES)
1005 return ERR_PTR(-EINVAL);
1006
1007 for (i = 0; i < ctrl->nr_ext_resources + 1; i++) {
1008 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1009 if (!res) {
1010 dev_err(&pdev->dev, "failed to get mem%d resource\n", i);
1011 return ERR_PTR(-EINVAL);
1012 }
1013 virt_base[i] = devm_ioremap(&pdev->dev, res->start,
1014 resource_size(res));
1015 if (!virt_base[i]) {
1016 dev_err(&pdev->dev, "failed to ioremap %pR\n", res);
1017 return ERR_PTR(-EIO);
1018 }
1019 }
1020
1021 bank = d->pin_banks;
1022 bdata = ctrl->pin_banks;
1023 for (i = 0; i < ctrl->nr_banks; ++i, ++bdata, ++bank) {
1024 bank->type = bdata->type;
1025 bank->pctl_offset = bdata->pctl_offset;
1026 bank->nr_pins = bdata->nr_pins;
1027 bank->eint_func = bdata->eint_func;
1028 bank->eint_type = bdata->eint_type;
1029 bank->eint_mask = bdata->eint_mask;
1030 bank->eint_offset = bdata->eint_offset;
1031 bank->name = bdata->name;
1032
1033 spin_lock_init(&bank->slock);
1034 bank->drvdata = d;
1035 bank->pin_base = d->nr_pins;
1036 d->nr_pins += bank->nr_pins;
1037
1038 bank->eint_base = virt_base[0];
1039 bank->pctl_base = virt_base[bdata->pctl_res_idx];
1040 }
1041 /*
1042 * Legacy platforms should provide only one resource with IO memory.
1043 * Store it as virt_base because legacy driver needs to access it
1044 * through samsung_pinctrl_drv_data.
1045 */
1046 d->virt_base = virt_base[0];
1047
1048 for_each_child_of_node(node, np) {
1049 if (!of_find_property(np, "gpio-controller", NULL))
1050 continue;
1051 bank = d->pin_banks;
1052 for (i = 0; i < d->nr_banks; ++i, ++bank) {
1053 if (!strcmp(bank->name, np->name)) {
1054 bank->of_node = np;
1055 break;
1056 }
1057 }
1058 }
1059
1060 d->pin_base = pin_base;
1061 pin_base += d->nr_pins;
1062
1063 return ctrl;
1064}
1065
1066static int samsung_pinctrl_probe(struct platform_device *pdev)
1067{
1068 struct samsung_pinctrl_drv_data *drvdata;
1069 const struct samsung_pin_ctrl *ctrl;
1070 struct device *dev = &pdev->dev;
1071 struct resource *res;
1072 int ret;
1073
1074 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1075 if (!drvdata)
1076 return -ENOMEM;
1077
1078 ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev);
1079 if (IS_ERR(ctrl)) {
1080 dev_err(&pdev->dev, "driver data not available\n");
1081 return PTR_ERR(ctrl);
1082 }
1083 drvdata->dev = dev;
1084
1085 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1086 if (res)
1087 drvdata->irq = res->start;
1088
1089 if (ctrl->retention_data) {
1090 drvdata->retention_ctrl = ctrl->retention_data->init(drvdata,
1091 ctrl->retention_data);
1092 if (IS_ERR(drvdata->retention_ctrl))
1093 return PTR_ERR(drvdata->retention_ctrl);
1094 }
1095
1096 ret = samsung_pinctrl_register(pdev, drvdata);
1097 if (ret)
1098 return ret;
1099
1100 ret = samsung_gpiolib_register(pdev, drvdata);
1101 if (ret) {
1102 samsung_pinctrl_unregister(pdev, drvdata);
1103 return ret;
1104 }
1105
1106 if (ctrl->eint_gpio_init)
1107 ctrl->eint_gpio_init(drvdata);
1108 if (ctrl->eint_wkup_init)
1109 ctrl->eint_wkup_init(drvdata);
1110
1111 platform_set_drvdata(pdev, drvdata);
1112
1113 return 0;
1114}
1115
1116/**
1117 * samsung_pinctrl_suspend - save pinctrl state for suspend
1118 *
1119 * Save data for all banks handled by this device.
1120 */
1121static int __maybe_unused samsung_pinctrl_suspend(struct device *dev)
1122{
1123 struct samsung_pinctrl_drv_data *drvdata = dev_get_drvdata(dev);
1124 int i;
1125
1126 for (i = 0; i < drvdata->nr_banks; i++) {
1127 struct samsung_pin_bank *bank = &drvdata->pin_banks[i];
1128 void __iomem *reg = bank->pctl_base + bank->pctl_offset;
1129 const u8 *offs = bank->type->reg_offset;
1130 const u8 *widths = bank->type->fld_width;
1131 enum pincfg_type type;
1132
1133 /* Registers without a powerdown config aren't lost */
1134 if (!widths[PINCFG_TYPE_CON_PDN])
1135 continue;
1136
1137 for (type = 0; type < PINCFG_TYPE_NUM; type++)
1138 if (widths[type])
1139 bank->pm_save[type] = readl(reg + offs[type]);
1140
1141 if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) {
1142 /* Some banks have two config registers */
1143 bank->pm_save[PINCFG_TYPE_NUM] =
1144 readl(reg + offs[PINCFG_TYPE_FUNC] + 4);
1145 pr_debug("Save %s @ %p (con %#010x %08x)\n",
1146 bank->name, reg,
1147 bank->pm_save[PINCFG_TYPE_FUNC],
1148 bank->pm_save[PINCFG_TYPE_NUM]);
1149 } else {
1150 pr_debug("Save %s @ %p (con %#010x)\n", bank->name,
1151 reg, bank->pm_save[PINCFG_TYPE_FUNC]);
1152 }
1153 }
1154
1155 if (drvdata->suspend)
1156 drvdata->suspend(drvdata);
1157 if (drvdata->retention_ctrl && drvdata->retention_ctrl->enable)
1158 drvdata->retention_ctrl->enable(drvdata);
1159
1160 return 0;
1161}
1162
1163/**
1164 * samsung_pinctrl_resume - restore pinctrl state from suspend
1165 *
1166 * Restore one of the banks that was saved during suspend.
1167 *
1168 * We don't bother doing anything complicated to avoid glitching lines since
1169 * we're called before pad retention is turned off.
1170 */
1171static int __maybe_unused samsung_pinctrl_resume(struct device *dev)
1172{
1173 struct samsung_pinctrl_drv_data *drvdata = dev_get_drvdata(dev);
1174 int i;
1175
1176 if (drvdata->resume)
1177 drvdata->resume(drvdata);
1178
1179 for (i = 0; i < drvdata->nr_banks; i++) {
1180 struct samsung_pin_bank *bank = &drvdata->pin_banks[i];
1181 void __iomem *reg = bank->pctl_base + bank->pctl_offset;
1182 const u8 *offs = bank->type->reg_offset;
1183 const u8 *widths = bank->type->fld_width;
1184 enum pincfg_type type;
1185
1186 /* Registers without a powerdown config aren't lost */
1187 if (!widths[PINCFG_TYPE_CON_PDN])
1188 continue;
1189
1190 if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) {
1191 /* Some banks have two config registers */
1192 pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n",
1193 bank->name, reg,
1194 readl(reg + offs[PINCFG_TYPE_FUNC]),
1195 readl(reg + offs[PINCFG_TYPE_FUNC] + 4),
1196 bank->pm_save[PINCFG_TYPE_FUNC],
1197 bank->pm_save[PINCFG_TYPE_NUM]);
1198 writel(bank->pm_save[PINCFG_TYPE_NUM],
1199 reg + offs[PINCFG_TYPE_FUNC] + 4);
1200 } else {
1201 pr_debug("%s @ %p (con %#010x => %#010x)\n", bank->name,
1202 reg, readl(reg + offs[PINCFG_TYPE_FUNC]),
1203 bank->pm_save[PINCFG_TYPE_FUNC]);
1204 }
1205 for (type = 0; type < PINCFG_TYPE_NUM; type++)
1206 if (widths[type])
1207 writel(bank->pm_save[type], reg + offs[type]);
1208 }
1209
1210 if (drvdata->retention_ctrl && drvdata->retention_ctrl->disable)
1211 drvdata->retention_ctrl->disable(drvdata);
1212
1213 return 0;
1214}
1215
1216static const struct of_device_id samsung_pinctrl_dt_match[] = {
1217#ifdef CONFIG_PINCTRL_EXYNOS_ARM
1218 { .compatible = "samsung,exynos3250-pinctrl",
1219 .data = &exynos3250_of_data },
1220 { .compatible = "samsung,exynos4210-pinctrl",
1221 .data = &exynos4210_of_data },
1222 { .compatible = "samsung,exynos4x12-pinctrl",
1223 .data = &exynos4x12_of_data },
1224 { .compatible = "samsung,exynos5250-pinctrl",
1225 .data = &exynos5250_of_data },
1226 { .compatible = "samsung,exynos5260-pinctrl",
1227 .data = &exynos5260_of_data },
1228 { .compatible = "samsung,exynos5410-pinctrl",
1229 .data = &exynos5410_of_data },
1230 { .compatible = "samsung,exynos5420-pinctrl",
1231 .data = &exynos5420_of_data },
1232 { .compatible = "samsung,s5pv210-pinctrl",
1233 .data = &s5pv210_of_data },
1234#endif
1235#ifdef CONFIG_PINCTRL_EXYNOS_ARM64
1236 { .compatible = "samsung,exynos5433-pinctrl",
1237 .data = &exynos5433_of_data },
1238 { .compatible = "samsung,exynos7-pinctrl",
1239 .data = &exynos7_of_data },
1240#endif
1241#ifdef CONFIG_PINCTRL_S3C64XX
1242 { .compatible = "samsung,s3c64xx-pinctrl",
1243 .data = &s3c64xx_of_data },
1244#endif
1245#ifdef CONFIG_PINCTRL_S3C24XX
1246 { .compatible = "samsung,s3c2412-pinctrl",
1247 .data = &s3c2412_of_data },
1248 { .compatible = "samsung,s3c2416-pinctrl",
1249 .data = &s3c2416_of_data },
1250 { .compatible = "samsung,s3c2440-pinctrl",
1251 .data = &s3c2440_of_data },
1252 { .compatible = "samsung,s3c2450-pinctrl",
1253 .data = &s3c2450_of_data },
1254#endif
1255 {},
1256};
1257
1258static const struct dev_pm_ops samsung_pinctrl_pm_ops = {
1259 SET_LATE_SYSTEM_SLEEP_PM_OPS(samsung_pinctrl_suspend,
1260 samsung_pinctrl_resume)
1261};
1262
1263static struct platform_driver samsung_pinctrl_driver = {
1264 .probe = samsung_pinctrl_probe,
1265 .driver = {
1266 .name = "samsung-pinctrl",
1267 .of_match_table = samsung_pinctrl_dt_match,
1268 .suppress_bind_attrs = true,
1269 .pm = &samsung_pinctrl_pm_ops,
1270 },
1271};
1272
1273static int __init samsung_pinctrl_drv_register(void)
1274{
1275 return platform_driver_register(&samsung_pinctrl_driver);
1276}
1277postcore_initcall(samsung_pinctrl_drv_register);