GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / pinctrl / samsung / pinctrl-exynos5440.c
1 /*
2  * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's EXYNOS5440 SoC.
3  *
4  * Author: Thomas Abraham <thomas.ab@samsung.com>
5  *
6  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
7  *              http://www.samsung.com
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  */
14
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/io.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/device.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/interrupt.h>
26 #include <linux/irqdomain.h>
27 #include <linux/of_irq.h>
28 #include "../core.h"
29
30 /* EXYNOS5440 GPIO and Pinctrl register offsets */
31 #define GPIO_MUX                0x00
32 #define GPIO_IE                 0x04
33 #define GPIO_INT                0x08
34 #define GPIO_TYPE               0x0C
35 #define GPIO_VAL                0x10
36 #define GPIO_OE                 0x14
37 #define GPIO_IN                 0x18
38 #define GPIO_PE                 0x1C
39 #define GPIO_PS                 0x20
40 #define GPIO_SR                 0x24
41 #define GPIO_DS0                0x28
42 #define GPIO_DS1                0x2C
43
44 #define EXYNOS5440_MAX_PINS             23
45 #define EXYNOS5440_MAX_GPIO_INT 8
46 #define PIN_NAME_LENGTH         10
47
48 #define GROUP_SUFFIX            "-grp"
49 #define FUNCTION_SUFFIX         "-mux"
50
51 /*
52  * pin configuration type and its value are packed together into a 16-bits.
53  * The upper 8-bits represent the configuration type and the lower 8-bits
54  * hold the value of the configuration type.
55  */
56 #define PINCFG_TYPE_MASK                0xFF
57 #define PINCFG_VALUE_SHIFT              8
58 #define PINCFG_VALUE_MASK               (0xFF << PINCFG_VALUE_SHIFT)
59 #define PINCFG_PACK(type, value)        (((value) << PINCFG_VALUE_SHIFT) | type)
60 #define PINCFG_UNPACK_TYPE(cfg)         ((cfg) & PINCFG_TYPE_MASK)
61 #define PINCFG_UNPACK_VALUE(cfg)        (((cfg) & PINCFG_VALUE_MASK) >> \
62                                                 PINCFG_VALUE_SHIFT)
63
64 /**
65  * enum pincfg_type - possible pin configuration types supported.
66  * @PINCFG_TYPE_PUD: Pull up/down configuration.
67  * @PINCFG_TYPE_DRV: Drive strength configuration.
68  * @PINCFG_TYPE_SKEW_RATE: Skew rate configuration.
69  * @PINCFG_TYPE_INPUT_TYPE: Pin input type configuration.
70  */
71 enum pincfg_type {
72         PINCFG_TYPE_PUD,
73         PINCFG_TYPE_DRV,
74         PINCFG_TYPE_SKEW_RATE,
75         PINCFG_TYPE_INPUT_TYPE
76 };
77
78 /**
79  * struct exynos5440_pin_group: represent group of pins for pincfg setting.
80  * @name: name of the pin group, used to lookup the group.
81  * @pins: the pins included in this group.
82  * @num_pins: number of pins included in this group.
83  */
84 struct exynos5440_pin_group {
85         const char              *name;
86         const unsigned int      *pins;
87         u8                      num_pins;
88 };
89
90 /**
91  * struct exynos5440_pmx_func: represent a pin function.
92  * @name: name of the pin function, used to lookup the function.
93  * @groups: one or more names of pin groups that provide this function.
94  * @num_groups: number of groups included in @groups.
95  * @function: the function number to be programmed when selected.
96  */
97 struct exynos5440_pmx_func {
98         const char              *name;
99         const char              **groups;
100         u8                      num_groups;
101         unsigned long           function;
102 };
103
104 /**
105  * struct exynos5440_pinctrl_priv_data: driver's private runtime data.
106  * @reg_base: ioremapped based address of the register space.
107  * @gc: gpio chip registered with gpiolib.
108  * @pin_groups: list of pin groups parsed from device tree.
109  * @nr_groups: number of pin groups available.
110  * @pmx_functions: list of pin functions parsed from device tree.
111  * @nr_functions: number of pin functions available.
112  * @range: gpio range to register with pinctrl
113  */
114 struct exynos5440_pinctrl_priv_data {
115         void __iomem                    *reg_base;
116         struct gpio_chip                *gc;
117         struct irq_domain               *irq_domain;
118
119         const struct exynos5440_pin_group       *pin_groups;
120         unsigned int                    nr_groups;
121         const struct exynos5440_pmx_func        *pmx_functions;
122         unsigned int                    nr_functions;
123         struct pinctrl_gpio_range       range;
124 };
125
126 /**
127  * struct exynos5440_gpio_intr_data: private data for gpio interrupts.
128  * @priv: driver's private runtime data.
129  * @gpio_int: gpio interrupt number.
130  */
131 struct exynos5440_gpio_intr_data {
132         struct exynos5440_pinctrl_priv_data     *priv;
133         unsigned int                            gpio_int;
134 };
135
136 /* list of all possible config options supported */
137 static struct pin_config {
138         char            *prop_cfg;
139         unsigned int    cfg_type;
140 } pcfgs[] = {
141         { "samsung,exynos5440-pin-pud", PINCFG_TYPE_PUD },
142         { "samsung,exynos5440-pin-drv", PINCFG_TYPE_DRV },
143         { "samsung,exynos5440-pin-skew-rate", PINCFG_TYPE_SKEW_RATE },
144         { "samsung,exynos5440-pin-input-type", PINCFG_TYPE_INPUT_TYPE },
145 };
146
147 /* check if the selector is a valid pin group selector */
148 static int exynos5440_get_group_count(struct pinctrl_dev *pctldev)
149 {
150         struct exynos5440_pinctrl_priv_data *priv;
151
152         priv = pinctrl_dev_get_drvdata(pctldev);
153         return priv->nr_groups;
154 }
155
156 /* return the name of the group selected by the group selector */
157 static const char *exynos5440_get_group_name(struct pinctrl_dev *pctldev,
158                                                 unsigned selector)
159 {
160         struct exynos5440_pinctrl_priv_data *priv;
161
162         priv = pinctrl_dev_get_drvdata(pctldev);
163         return priv->pin_groups[selector].name;
164 }
165
166 /* return the pin numbers associated with the specified group */
167 static int exynos5440_get_group_pins(struct pinctrl_dev *pctldev,
168                 unsigned selector, const unsigned **pins, unsigned *num_pins)
169 {
170         struct exynos5440_pinctrl_priv_data *priv;
171
172         priv = pinctrl_dev_get_drvdata(pctldev);
173         *pins = priv->pin_groups[selector].pins;
174         *num_pins = priv->pin_groups[selector].num_pins;
175         return 0;
176 }
177
178 /* create pinctrl_map entries by parsing device tree nodes */
179 static int exynos5440_dt_node_to_map(struct pinctrl_dev *pctldev,
180                         struct device_node *np, struct pinctrl_map **maps,
181                         unsigned *nmaps)
182 {
183         struct device *dev = pctldev->dev;
184         struct pinctrl_map *map;
185         unsigned long *cfg = NULL;
186         char *gname, *fname;
187         int cfg_cnt = 0, map_cnt = 0, idx = 0;
188
189         /* count the number of config options specfied in the node */
190         for (idx = 0; idx < ARRAY_SIZE(pcfgs); idx++)
191                 if (of_find_property(np, pcfgs[idx].prop_cfg, NULL))
192                         cfg_cnt++;
193
194         /*
195          * Find out the number of map entries to create. All the config options
196          * can be accomadated into a single config map entry.
197          */
198         if (cfg_cnt)
199                 map_cnt = 1;
200         if (of_find_property(np, "samsung,exynos5440-pin-function", NULL))
201                 map_cnt++;
202         if (!map_cnt) {
203                 dev_err(dev, "node %s does not have either config or function "
204                                 "configurations\n", np->name);
205                 return -EINVAL;
206         }
207
208         /* Allocate memory for pin-map entries */
209         map = kzalloc(sizeof(*map) * map_cnt, GFP_KERNEL);
210         if (!map)
211                 return -ENOMEM;
212         *nmaps = 0;
213
214         /*
215          * Allocate memory for pin group name. The pin group name is derived
216          * from the node name from which these map entries are be created.
217          */
218         gname = kasprintf(GFP_KERNEL, "%s%s", np->name, GROUP_SUFFIX);
219         if (!gname)
220                 goto free_map;
221
222         /*
223          * don't have config options? then skip over to creating function
224          * map entries.
225          */
226         if (!cfg_cnt)
227                 goto skip_cfgs;
228
229         /* Allocate memory for config entries */
230         cfg = kzalloc(sizeof(*cfg) * cfg_cnt, GFP_KERNEL);
231         if (!cfg)
232                 goto free_gname;
233
234         /* Prepare a list of config settings */
235         for (idx = 0, cfg_cnt = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
236                 u32 value;
237                 if (!of_property_read_u32(np, pcfgs[idx].prop_cfg, &value))
238                         cfg[cfg_cnt++] =
239                                 PINCFG_PACK(pcfgs[idx].cfg_type, value);
240         }
241
242         /* create the config map entry */
243         map[*nmaps].data.configs.group_or_pin = gname;
244         map[*nmaps].data.configs.configs = cfg;
245         map[*nmaps].data.configs.num_configs = cfg_cnt;
246         map[*nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
247         *nmaps += 1;
248
249 skip_cfgs:
250         /* create the function map entry */
251         if (of_find_property(np, "samsung,exynos5440-pin-function", NULL)) {
252                 fname = kasprintf(GFP_KERNEL,
253                                   "%s%s", np->name, FUNCTION_SUFFIX);
254                 if (!fname)
255                         goto free_cfg;
256
257                 map[*nmaps].data.mux.group = gname;
258                 map[*nmaps].data.mux.function = fname;
259                 map[*nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
260                 *nmaps += 1;
261         }
262
263         *maps = map;
264         return 0;
265
266 free_cfg:
267         kfree(cfg);
268 free_gname:
269         kfree(gname);
270 free_map:
271         kfree(map);
272         return -ENOMEM;
273 }
274
275 /* free the memory allocated to hold the pin-map table */
276 static void exynos5440_dt_free_map(struct pinctrl_dev *pctldev,
277                              struct pinctrl_map *map, unsigned num_maps)
278 {
279         int idx;
280
281         for (idx = 0; idx < num_maps; idx++) {
282                 if (map[idx].type == PIN_MAP_TYPE_MUX_GROUP) {
283                         kfree(map[idx].data.mux.function);
284                         if (!idx)
285                                 kfree(map[idx].data.mux.group);
286                 } else if (map->type == PIN_MAP_TYPE_CONFIGS_GROUP) {
287                         kfree(map[idx].data.configs.configs);
288                         if (!idx)
289                                 kfree(map[idx].data.configs.group_or_pin);
290                 }
291         }
292
293         kfree(map);
294 }
295
296 /* list of pinctrl callbacks for the pinctrl core */
297 static const struct pinctrl_ops exynos5440_pctrl_ops = {
298         .get_groups_count       = exynos5440_get_group_count,
299         .get_group_name         = exynos5440_get_group_name,
300         .get_group_pins         = exynos5440_get_group_pins,
301         .dt_node_to_map         = exynos5440_dt_node_to_map,
302         .dt_free_map            = exynos5440_dt_free_map,
303 };
304
305 /* check if the selector is a valid pin function selector */
306 static int exynos5440_get_functions_count(struct pinctrl_dev *pctldev)
307 {
308         struct exynos5440_pinctrl_priv_data *priv;
309
310         priv = pinctrl_dev_get_drvdata(pctldev);
311         return priv->nr_functions;
312 }
313
314 /* return the name of the pin function specified */
315 static const char *exynos5440_pinmux_get_fname(struct pinctrl_dev *pctldev,
316                                                 unsigned selector)
317 {
318         struct exynos5440_pinctrl_priv_data *priv;
319
320         priv = pinctrl_dev_get_drvdata(pctldev);
321         return priv->pmx_functions[selector].name;
322 }
323
324 /* return the groups associated for the specified function selector */
325 static int exynos5440_pinmux_get_groups(struct pinctrl_dev *pctldev,
326                 unsigned selector, const char * const **groups,
327                 unsigned * const num_groups)
328 {
329         struct exynos5440_pinctrl_priv_data *priv;
330
331         priv = pinctrl_dev_get_drvdata(pctldev);
332         *groups = priv->pmx_functions[selector].groups;
333         *num_groups = priv->pmx_functions[selector].num_groups;
334         return 0;
335 }
336
337 /* enable or disable a pinmux function */
338 static void exynos5440_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
339                                         unsigned group, bool enable)
340 {
341         struct exynos5440_pinctrl_priv_data *priv;
342         void __iomem *base;
343         u32 function;
344         u32 data;
345
346         priv = pinctrl_dev_get_drvdata(pctldev);
347         base = priv->reg_base;
348         function = priv->pmx_functions[selector].function;
349
350         data = readl(base + GPIO_MUX);
351         if (enable)
352                 data |= (1 << function);
353         else
354                 data &= ~(1 << function);
355         writel(data, base + GPIO_MUX);
356 }
357
358 /* enable a specified pinmux by writing to registers */
359 static int exynos5440_pinmux_set_mux(struct pinctrl_dev *pctldev,
360                                      unsigned selector,
361                                      unsigned group)
362 {
363         exynos5440_pinmux_setup(pctldev, selector, group, true);
364         return 0;
365 }
366
367 /*
368  * The calls to gpio_direction_output() and gpio_direction_input()
369  * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
370  * function called from the gpiolib interface).
371  */
372 static int exynos5440_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
373                 struct pinctrl_gpio_range *range, unsigned offset, bool input)
374 {
375         return 0;
376 }
377
378 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
379 static const struct pinmux_ops exynos5440_pinmux_ops = {
380         .get_functions_count    = exynos5440_get_functions_count,
381         .get_function_name      = exynos5440_pinmux_get_fname,
382         .get_function_groups    = exynos5440_pinmux_get_groups,
383         .set_mux                = exynos5440_pinmux_set_mux,
384         .gpio_set_direction     = exynos5440_pinmux_gpio_set_direction,
385 };
386
387 /* set the pin config settings for a specified pin */
388 static int exynos5440_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
389                                 unsigned long *configs,
390                                 unsigned num_configs)
391 {
392         struct exynos5440_pinctrl_priv_data *priv;
393         void __iomem *base;
394         enum pincfg_type cfg_type;
395         u32 cfg_value;
396         u32 data;
397         int i;
398
399         priv = pinctrl_dev_get_drvdata(pctldev);
400         base = priv->reg_base;
401
402         for (i = 0; i < num_configs; i++) {
403                 cfg_type = PINCFG_UNPACK_TYPE(configs[i]);
404                 cfg_value = PINCFG_UNPACK_VALUE(configs[i]);
405
406                 switch (cfg_type) {
407                 case PINCFG_TYPE_PUD:
408                         /* first set pull enable/disable bit */
409                         data = readl(base + GPIO_PE);
410                         data &= ~(1 << pin);
411                         if (cfg_value)
412                                 data |= (1 << pin);
413                         writel(data, base + GPIO_PE);
414
415                         /* then set pull up/down bit */
416                         data = readl(base + GPIO_PS);
417                         data &= ~(1 << pin);
418                         if (cfg_value == 2)
419                                 data |= (1 << pin);
420                         writel(data, base + GPIO_PS);
421                         break;
422
423                 case PINCFG_TYPE_DRV:
424                         /* set the first bit of the drive strength */
425                         data = readl(base + GPIO_DS0);
426                         data &= ~(1 << pin);
427                         data |= ((cfg_value & 1) << pin);
428                         writel(data, base + GPIO_DS0);
429                         cfg_value >>= 1;
430
431                         /* set the second bit of the driver strength */
432                         data = readl(base + GPIO_DS1);
433                         data &= ~(1 << pin);
434                         data |= ((cfg_value & 1) << pin);
435                         writel(data, base + GPIO_DS1);
436                         break;
437                 case PINCFG_TYPE_SKEW_RATE:
438                         data = readl(base + GPIO_SR);
439                         data &= ~(1 << pin);
440                         data |= ((cfg_value & 1) << pin);
441                         writel(data, base + GPIO_SR);
442                         break;
443                 case PINCFG_TYPE_INPUT_TYPE:
444                         data = readl(base + GPIO_TYPE);
445                         data &= ~(1 << pin);
446                         data |= ((cfg_value & 1) << pin);
447                         writel(data, base + GPIO_TYPE);
448                         break;
449                 default:
450                         WARN_ON(1);
451                         return -EINVAL;
452                 }
453         } /* for each config */
454
455         return 0;
456 }
457
458 /* get the pin config settings for a specified pin */
459 static int exynos5440_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
460                                         unsigned long *config)
461 {
462         struct exynos5440_pinctrl_priv_data *priv;
463         void __iomem *base;
464         enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
465         u32 data;
466
467         priv = pinctrl_dev_get_drvdata(pctldev);
468         base = priv->reg_base;
469
470         switch (cfg_type) {
471         case PINCFG_TYPE_PUD:
472                 data = readl(base + GPIO_PE);
473                 data = (data >> pin) & 1;
474                 if (!data)
475                         *config = 0;
476                 else
477                         *config = ((readl(base + GPIO_PS) >> pin) & 1) + 1;
478                 break;
479         case PINCFG_TYPE_DRV:
480                 data = readl(base + GPIO_DS0);
481                 data = (data >> pin) & 1;
482                 *config = data;
483                 data = readl(base + GPIO_DS1);
484                 data = (data >> pin) & 1;
485                 *config |= (data << 1);
486                 break;
487         case PINCFG_TYPE_SKEW_RATE:
488                 data = readl(base + GPIO_SR);
489                 *config = (data >> pin) & 1;
490                 break;
491         case PINCFG_TYPE_INPUT_TYPE:
492                 data = readl(base + GPIO_TYPE);
493                 *config = (data >> pin) & 1;
494                 break;
495         default:
496                 WARN_ON(1);
497                 return -EINVAL;
498         }
499
500         return 0;
501 }
502
503 /* set the pin config settings for a specified pin group */
504 static int exynos5440_pinconf_group_set(struct pinctrl_dev *pctldev,
505                         unsigned group, unsigned long *configs,
506                         unsigned num_configs)
507 {
508         struct exynos5440_pinctrl_priv_data *priv;
509         const unsigned int *pins;
510         unsigned int cnt;
511
512         priv = pinctrl_dev_get_drvdata(pctldev);
513         pins = priv->pin_groups[group].pins;
514
515         for (cnt = 0; cnt < priv->pin_groups[group].num_pins; cnt++)
516                 exynos5440_pinconf_set(pctldev, pins[cnt], configs,
517                                        num_configs);
518
519         return 0;
520 }
521
522 /* get the pin config settings for a specified pin group */
523 static int exynos5440_pinconf_group_get(struct pinctrl_dev *pctldev,
524                                 unsigned int group, unsigned long *config)
525 {
526         struct exynos5440_pinctrl_priv_data *priv;
527         const unsigned int *pins;
528
529         priv = pinctrl_dev_get_drvdata(pctldev);
530         pins = priv->pin_groups[group].pins;
531         exynos5440_pinconf_get(pctldev, pins[0], config);
532         return 0;
533 }
534
535 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
536 static const struct pinconf_ops exynos5440_pinconf_ops = {
537         .pin_config_get         = exynos5440_pinconf_get,
538         .pin_config_set         = exynos5440_pinconf_set,
539         .pin_config_group_get   = exynos5440_pinconf_group_get,
540         .pin_config_group_set   = exynos5440_pinconf_group_set,
541 };
542
543 /* gpiolib gpio_set callback function */
544 static void exynos5440_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
545 {
546         struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
547         void __iomem *base = priv->reg_base;
548         u32 data;
549
550         data = readl(base + GPIO_VAL);
551         data &= ~(1 << offset);
552         if (value)
553                 data |= 1 << offset;
554         writel(data, base + GPIO_VAL);
555 }
556
557 /* gpiolib gpio_get callback function */
558 static int exynos5440_gpio_get(struct gpio_chip *gc, unsigned offset)
559 {
560         struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
561         void __iomem *base = priv->reg_base;
562         u32 data;
563
564         data = readl(base + GPIO_IN);
565         data >>= offset;
566         data &= 1;
567         return data;
568 }
569
570 /* gpiolib gpio_direction_input callback function */
571 static int exynos5440_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
572 {
573         struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
574         void __iomem *base = priv->reg_base;
575         u32 data;
576
577         /* first disable the data output enable on this pin */
578         data = readl(base + GPIO_OE);
579         data &= ~(1 << offset);
580         writel(data, base + GPIO_OE);
581
582         /* now enable input on this pin */
583         data =  readl(base + GPIO_IE);
584         data |= 1 << offset;
585         writel(data, base + GPIO_IE);
586         return 0;
587 }
588
589 /* gpiolib gpio_direction_output callback function */
590 static int exynos5440_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
591                                                         int value)
592 {
593         struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
594         void __iomem *base = priv->reg_base;
595         u32 data;
596
597         exynos5440_gpio_set(gc, offset, value);
598
599         /* first disable the data input enable on this pin */
600         data = readl(base + GPIO_IE);
601         data &= ~(1 << offset);
602         writel(data, base + GPIO_IE);
603
604         /* now enable output on this pin */
605         data =  readl(base + GPIO_OE);
606         data |= 1 << offset;
607         writel(data, base + GPIO_OE);
608         return 0;
609 }
610
611 /* gpiolib gpio_to_irq callback function */
612 static int exynos5440_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
613 {
614         struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
615         unsigned int virq;
616
617         if (offset < 16 || offset > 23)
618                 return -ENXIO;
619
620         if (!priv->irq_domain)
621                 return -ENXIO;
622
623         virq = irq_create_mapping(priv->irq_domain, offset - 16);
624         return virq ? : -ENXIO;
625 }
626
627 /* parse the pin numbers listed in the 'samsung,exynos5440-pins' property */
628 static int exynos5440_pinctrl_parse_dt_pins(struct platform_device *pdev,
629                         struct device_node *cfg_np, unsigned int **pin_list,
630                         unsigned int *npins)
631 {
632         struct device *dev = &pdev->dev;
633         struct property *prop;
634
635         prop = of_find_property(cfg_np, "samsung,exynos5440-pins", NULL);
636         if (!prop)
637                 return -ENOENT;
638
639         *npins = prop->length / sizeof(unsigned long);
640         if (!*npins) {
641                 dev_err(dev, "invalid pin list in %s node", cfg_np->name);
642                 return -EINVAL;
643         }
644
645         *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
646         if (!*pin_list)
647                 return -ENOMEM;
648
649         return of_property_read_u32_array(cfg_np, "samsung,exynos5440-pins",
650                         *pin_list, *npins);
651 }
652
653 /*
654  * Parse the information about all the available pin groups and pin functions
655  * from device node of the pin-controller.
656  */
657 static int exynos5440_pinctrl_parse_dt(struct platform_device *pdev,
658                                 struct exynos5440_pinctrl_priv_data *priv)
659 {
660         struct device *dev = &pdev->dev;
661         struct device_node *dev_np = dev->of_node;
662         struct device_node *cfg_np;
663         struct exynos5440_pin_group *groups, *grp;
664         struct exynos5440_pmx_func *functions, *func;
665         unsigned *pin_list;
666         unsigned int npins, grp_cnt, func_idx = 0;
667         char *gname, *fname;
668         int ret;
669
670         grp_cnt = of_get_child_count(dev_np);
671         if (!grp_cnt)
672                 return -EINVAL;
673
674         groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
675         if (!groups)
676                 return -EINVAL;
677
678         grp = groups;
679
680         functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
681         if (!functions)
682                 return -EINVAL;
683
684         func = functions;
685
686         /*
687          * Iterate over all the child nodes of the pin controller node
688          * and create pin groups and pin function lists.
689          */
690         for_each_child_of_node(dev_np, cfg_np) {
691                 u32 function;
692
693                 ret = exynos5440_pinctrl_parse_dt_pins(pdev, cfg_np,
694                                         &pin_list, &npins);
695                 if (ret) {
696                         gname = NULL;
697                         goto skip_to_pin_function;
698                 }
699
700                 /* derive pin group name from the node name */
701                 gname = devm_kasprintf(dev, GFP_KERNEL,
702                                        "%s%s", cfg_np->name, GROUP_SUFFIX);
703                 if (!gname)
704                         return -ENOMEM;
705
706                 grp->name = gname;
707                 grp->pins = pin_list;
708                 grp->num_pins = npins;
709                 grp++;
710
711 skip_to_pin_function:
712                 ret = of_property_read_u32(cfg_np, "samsung,exynos5440-pin-function",
713                                                 &function);
714                 if (ret)
715                         continue;
716
717                 /* derive function name from the node name */
718                 fname = devm_kasprintf(dev, GFP_KERNEL,
719                                        "%s%s", cfg_np->name, FUNCTION_SUFFIX);
720                 if (!fname)
721                         return -ENOMEM;
722
723                 func->name = fname;
724                 func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
725                 if (!func->groups)
726                         return -ENOMEM;
727                 func->groups[0] = gname;
728                 func->num_groups = gname ? 1 : 0;
729                 func->function = function;
730                 func++;
731                 func_idx++;
732         }
733
734         priv->pin_groups = groups;
735         priv->nr_groups = grp_cnt;
736         priv->pmx_functions = functions;
737         priv->nr_functions = func_idx;
738         return 0;
739 }
740
741 /* register the pinctrl interface with the pinctrl subsystem */
742 static int exynos5440_pinctrl_register(struct platform_device *pdev,
743                                 struct exynos5440_pinctrl_priv_data *priv)
744 {
745         struct device *dev = &pdev->dev;
746         struct pinctrl_desc *ctrldesc;
747         struct pinctrl_dev *pctl_dev;
748         struct pinctrl_pin_desc *pindesc, *pdesc;
749         char *pin_names;
750         int pin, ret;
751
752         ctrldesc = devm_kzalloc(dev, sizeof(*ctrldesc), GFP_KERNEL);
753         if (!ctrldesc)
754                 return -ENOMEM;
755
756         ctrldesc->name = "exynos5440-pinctrl";
757         ctrldesc->owner = THIS_MODULE;
758         ctrldesc->pctlops = &exynos5440_pctrl_ops;
759         ctrldesc->pmxops = &exynos5440_pinmux_ops;
760         ctrldesc->confops = &exynos5440_pinconf_ops;
761
762         pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
763                                 EXYNOS5440_MAX_PINS, GFP_KERNEL);
764         if (!pindesc)
765                 return -ENOMEM;
766         ctrldesc->pins = pindesc;
767         ctrldesc->npins = EXYNOS5440_MAX_PINS;
768
769         /* dynamically populate the pin number and pin name for pindesc */
770         for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
771                 pdesc->number = pin;
772
773         /*
774          * allocate space for storing the dynamically generated names for all
775          * the pins which belong to this pin-controller.
776          */
777         pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
778                                         ctrldesc->npins, GFP_KERNEL);
779         if (!pin_names)
780                 return -ENOMEM;
781
782         /* for each pin, set the name of the pin */
783         for (pin = 0; pin < ctrldesc->npins; pin++) {
784                 snprintf(pin_names, 6, "gpio%02d", pin);
785                 pdesc = pindesc + pin;
786                 pdesc->name = pin_names;
787                 pin_names += PIN_NAME_LENGTH;
788         }
789
790         ret = exynos5440_pinctrl_parse_dt(pdev, priv);
791         if (ret)
792                 return ret;
793
794         pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, priv);
795         if (IS_ERR(pctl_dev)) {
796                 dev_err(&pdev->dev, "could not register pinctrl driver\n");
797                 return PTR_ERR(pctl_dev);
798         }
799
800         priv->range.name = "exynos5440-pctrl-gpio-range";
801         priv->range.id = 0;
802         priv->range.base = 0;
803         priv->range.npins = EXYNOS5440_MAX_PINS;
804         priv->range.gc = priv->gc;
805         pinctrl_add_gpio_range(pctl_dev, &priv->range);
806         return 0;
807 }
808
809 /* register the gpiolib interface with the gpiolib subsystem */
810 static int exynos5440_gpiolib_register(struct platform_device *pdev,
811                                 struct exynos5440_pinctrl_priv_data *priv)
812 {
813         struct gpio_chip *gc;
814         int ret;
815
816         gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
817         if (!gc)
818                 return -ENOMEM;
819
820         priv->gc = gc;
821         gc->base = 0;
822         gc->ngpio = EXYNOS5440_MAX_PINS;
823         gc->parent = &pdev->dev;
824         gc->set = exynos5440_gpio_set;
825         gc->get = exynos5440_gpio_get;
826         gc->direction_input = exynos5440_gpio_direction_input;
827         gc->direction_output = exynos5440_gpio_direction_output;
828         gc->to_irq = exynos5440_gpio_to_irq;
829         gc->label = "gpiolib-exynos5440";
830         gc->owner = THIS_MODULE;
831         ret = gpiochip_add_data(gc, priv);
832         if (ret) {
833                 dev_err(&pdev->dev, "failed to register gpio_chip %s, error "
834                                         "code: %d\n", gc->label, ret);
835                 return ret;
836         }
837
838         return 0;
839 }
840
841 /* unregister the gpiolib interface with the gpiolib subsystem */
842 static int exynos5440_gpiolib_unregister(struct platform_device *pdev,
843                                 struct exynos5440_pinctrl_priv_data *priv)
844 {
845         gpiochip_remove(priv->gc);
846         return 0;
847 }
848
849 static void exynos5440_gpio_irq_unmask(struct irq_data *irqd)
850 {
851         struct exynos5440_pinctrl_priv_data *d;
852         unsigned long gpio_int;
853
854         d = irq_data_get_irq_chip_data(irqd);
855         gpio_int = readl(d->reg_base + GPIO_INT);
856         gpio_int |= 1 << irqd->hwirq;
857         writel(gpio_int, d->reg_base + GPIO_INT);
858 }
859
860 static void exynos5440_gpio_irq_mask(struct irq_data *irqd)
861 {
862         struct exynos5440_pinctrl_priv_data *d;
863         unsigned long gpio_int;
864
865         d = irq_data_get_irq_chip_data(irqd);
866         gpio_int = readl(d->reg_base + GPIO_INT);
867         gpio_int &= ~(1 << irqd->hwirq);
868         writel(gpio_int, d->reg_base + GPIO_INT);
869 }
870
871 /* irq_chip for gpio interrupts */
872 static struct irq_chip exynos5440_gpio_irq_chip = {
873         .name           = "exynos5440_gpio_irq_chip",
874         .irq_unmask     = exynos5440_gpio_irq_unmask,
875         .irq_mask       = exynos5440_gpio_irq_mask,
876 };
877
878 /* interrupt handler for GPIO interrupts 0..7 */
879 static irqreturn_t exynos5440_gpio_irq(int irq, void *data)
880 {
881         struct exynos5440_gpio_intr_data *intd = data;
882         struct exynos5440_pinctrl_priv_data *d = intd->priv;
883         int virq;
884
885         virq = irq_linear_revmap(d->irq_domain, intd->gpio_int);
886         if (!virq)
887                 return IRQ_NONE;
888         generic_handle_irq(virq);
889         return IRQ_HANDLED;
890 }
891
892 static int exynos5440_gpio_irq_map(struct irq_domain *h, unsigned int virq,
893                                         irq_hw_number_t hw)
894 {
895         struct exynos5440_pinctrl_priv_data *d = h->host_data;
896
897         irq_set_chip_data(virq, d);
898         irq_set_chip_and_handler(virq, &exynos5440_gpio_irq_chip,
899                                         handle_level_irq);
900         return 0;
901 }
902
903 /* irq domain callbacks for gpio interrupt controller */
904 static const struct irq_domain_ops exynos5440_gpio_irqd_ops = {
905         .map    = exynos5440_gpio_irq_map,
906         .xlate  = irq_domain_xlate_twocell,
907 };
908
909 /* setup handling of gpio interrupts */
910 static int exynos5440_gpio_irq_init(struct platform_device *pdev,
911                                 struct exynos5440_pinctrl_priv_data *priv)
912 {
913         struct device *dev = &pdev->dev;
914         struct exynos5440_gpio_intr_data *intd;
915         int i, irq, ret;
916
917         intd = devm_kzalloc(dev, sizeof(*intd) * EXYNOS5440_MAX_GPIO_INT,
918                                         GFP_KERNEL);
919         if (!intd)
920                 return -ENOMEM;
921
922         for (i = 0; i < EXYNOS5440_MAX_GPIO_INT; i++) {
923                 irq = irq_of_parse_and_map(dev->of_node, i);
924                 if (irq <= 0) {
925                         dev_err(dev, "irq parsing failed\n");
926                         return -EINVAL;
927                 }
928
929                 intd->gpio_int = i;
930                 intd->priv = priv;
931                 ret = devm_request_irq(dev, irq, exynos5440_gpio_irq,
932                                         0, dev_name(dev), intd++);
933                 if (ret) {
934                         dev_err(dev, "irq request failed\n");
935                         return -ENXIO;
936                 }
937         }
938
939         priv->irq_domain = irq_domain_add_linear(dev->of_node,
940                                 EXYNOS5440_MAX_GPIO_INT,
941                                 &exynos5440_gpio_irqd_ops, priv);
942         if (!priv->irq_domain) {
943                 dev_err(dev, "failed to create irq domain\n");
944                 return -ENXIO;
945         }
946
947         return 0;
948 }
949
950 static int exynos5440_pinctrl_probe(struct platform_device *pdev)
951 {
952         struct device *dev = &pdev->dev;
953         struct exynos5440_pinctrl_priv_data *priv;
954         struct resource *res;
955         int ret;
956
957         if (!dev->of_node) {
958                 dev_err(dev, "device tree node not found\n");
959                 return -ENODEV;
960         }
961
962         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
963         if (!priv)
964                 return -ENOMEM;
965
966         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
967         priv->reg_base = devm_ioremap_resource(&pdev->dev, res);
968         if (IS_ERR(priv->reg_base))
969                 return PTR_ERR(priv->reg_base);
970
971         ret = exynos5440_gpiolib_register(pdev, priv);
972         if (ret)
973                 return ret;
974
975         ret = exynos5440_pinctrl_register(pdev, priv);
976         if (ret) {
977                 exynos5440_gpiolib_unregister(pdev, priv);
978                 return ret;
979         }
980
981         ret = exynos5440_gpio_irq_init(pdev, priv);
982         if (ret) {
983                 dev_err(dev, "failed to setup gpio interrupts\n");
984                 return ret;
985         }
986
987         platform_set_drvdata(pdev, priv);
988         dev_info(dev, "EXYNOS5440 pinctrl driver registered\n");
989         return 0;
990 }
991
992 static const struct of_device_id exynos5440_pinctrl_dt_match[] = {
993         { .compatible = "samsung,exynos5440-pinctrl" },
994         {},
995 };
996
997 static struct platform_driver exynos5440_pinctrl_driver = {
998         .probe          = exynos5440_pinctrl_probe,
999         .driver = {
1000                 .name   = "exynos5440-pinctrl",
1001                 .of_match_table = exynos5440_pinctrl_dt_match,
1002                 .suppress_bind_attrs = true,
1003         },
1004 };
1005
1006 static int __init exynos5440_pinctrl_drv_register(void)
1007 {
1008         return platform_driver_register(&exynos5440_pinctrl_driver);
1009 }
1010 postcore_initcall(exynos5440_pinctrl_drv_register);