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