pinctrl: exynos: Set pin function to EINT in irq_set_type of wake-up EINT
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / pinctrl / pinctrl-samsung.c
CommitLineData
30574f0d
TA
1/*
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/module.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
30#include "core.h"
31#include "pinctrl-samsung.h"
32
33#define GROUP_SUFFIX "-grp"
34#define GSUFFIX_LEN sizeof(GROUP_SUFFIX)
35#define FUNCTION_SUFFIX "-mux"
36#define FSUFFIX_LEN sizeof(FUNCTION_SUFFIX)
37
38/* list of all possible config options supported */
39struct pin_config {
40 char *prop_cfg;
41 unsigned int cfg_type;
42} pcfgs[] = {
43 { "samsung,pin-pud", PINCFG_TYPE_PUD },
44 { "samsung,pin-drv", PINCFG_TYPE_DRV },
45 { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN },
46 { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN },
47};
48
40ba6227
TF
49static unsigned int pin_base = 0;
50
d3a7b9e3
TF
51static inline struct samsung_pin_bank *gc_to_pin_bank(struct gpio_chip *gc)
52{
53 return container_of(gc, struct samsung_pin_bank, gpio_chip);
54}
55
30574f0d
TA
56/* check if the selector is a valid pin group selector */
57static int samsung_get_group_count(struct pinctrl_dev *pctldev)
58{
59 struct samsung_pinctrl_drv_data *drvdata;
60
61 drvdata = pinctrl_dev_get_drvdata(pctldev);
62 return drvdata->nr_groups;
63}
64
65/* return the name of the group selected by the group selector */
66static const char *samsung_get_group_name(struct pinctrl_dev *pctldev,
67 unsigned selector)
68{
69 struct samsung_pinctrl_drv_data *drvdata;
70
71 drvdata = pinctrl_dev_get_drvdata(pctldev);
72 return drvdata->pin_groups[selector].name;
73}
74
75/* return the pin numbers associated with the specified group */
76static int samsung_get_group_pins(struct pinctrl_dev *pctldev,
77 unsigned selector, const unsigned **pins, unsigned *num_pins)
78{
79 struct samsung_pinctrl_drv_data *drvdata;
80
81 drvdata = pinctrl_dev_get_drvdata(pctldev);
82 *pins = drvdata->pin_groups[selector].pins;
83 *num_pins = drvdata->pin_groups[selector].num_pins;
84 return 0;
85}
86
87/* create pinctrl_map entries by parsing device tree nodes */
88static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev,
89 struct device_node *np, struct pinctrl_map **maps,
90 unsigned *nmaps)
91{
92 struct device *dev = pctldev->dev;
93 struct pinctrl_map *map;
94 unsigned long *cfg = NULL;
95 char *gname, *fname;
96 int cfg_cnt = 0, map_cnt = 0, idx = 0;
97
98 /* count the number of config options specfied in the node */
99 for (idx = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
100 if (of_find_property(np, pcfgs[idx].prop_cfg, NULL))
101 cfg_cnt++;
102 }
103
104 /*
105 * Find out the number of map entries to create. All the config options
106 * can be accomadated into a single config map entry.
107 */
108 if (cfg_cnt)
109 map_cnt = 1;
110 if (of_find_property(np, "samsung,pin-function", NULL))
111 map_cnt++;
112 if (!map_cnt) {
113 dev_err(dev, "node %s does not have either config or function "
114 "configurations\n", np->name);
115 return -EINVAL;
116 }
117
118 /* Allocate memory for pin-map entries */
119 map = kzalloc(sizeof(*map) * map_cnt, GFP_KERNEL);
120 if (!map) {
121 dev_err(dev, "could not alloc memory for pin-maps\n");
122 return -ENOMEM;
123 }
124 *nmaps = 0;
125
126 /*
127 * Allocate memory for pin group name. The pin group name is derived
128 * from the node name from which these map entries are be created.
129 */
130 gname = kzalloc(strlen(np->name) + GSUFFIX_LEN, GFP_KERNEL);
131 if (!gname) {
132 dev_err(dev, "failed to alloc memory for group name\n");
133 goto free_map;
134 }
135 sprintf(gname, "%s%s", np->name, GROUP_SUFFIX);
136
137 /*
138 * don't have config options? then skip over to creating function
139 * map entries.
140 */
141 if (!cfg_cnt)
142 goto skip_cfgs;
143
144 /* Allocate memory for config entries */
145 cfg = kzalloc(sizeof(*cfg) * cfg_cnt, GFP_KERNEL);
146 if (!cfg) {
147 dev_err(dev, "failed to alloc memory for configs\n");
148 goto free_gname;
149 }
150
151 /* Prepare a list of config settings */
152 for (idx = 0, cfg_cnt = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
153 u32 value;
154 if (!of_property_read_u32(np, pcfgs[idx].prop_cfg, &value))
155 cfg[cfg_cnt++] =
156 PINCFG_PACK(pcfgs[idx].cfg_type, value);
157 }
158
159 /* create the config map entry */
160 map[*nmaps].data.configs.group_or_pin = gname;
161 map[*nmaps].data.configs.configs = cfg;
162 map[*nmaps].data.configs.num_configs = cfg_cnt;
163 map[*nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
164 *nmaps += 1;
165
166skip_cfgs:
167 /* create the function map entry */
168 if (of_find_property(np, "samsung,pin-function", NULL)) {
169 fname = kzalloc(strlen(np->name) + FSUFFIX_LEN, GFP_KERNEL);
170 if (!fname) {
171 dev_err(dev, "failed to alloc memory for func name\n");
172 goto free_cfg;
173 }
174 sprintf(fname, "%s%s", np->name, FUNCTION_SUFFIX);
175
176 map[*nmaps].data.mux.group = gname;
177 map[*nmaps].data.mux.function = fname;
178 map[*nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
179 *nmaps += 1;
180 }
181
182 *maps = map;
183 return 0;
184
185free_cfg:
186 kfree(cfg);
187free_gname:
188 kfree(gname);
189free_map:
190 kfree(map);
191 return -ENOMEM;
192}
193
194/* free the memory allocated to hold the pin-map table */
195static void samsung_dt_free_map(struct pinctrl_dev *pctldev,
196 struct pinctrl_map *map, unsigned num_maps)
197{
198 int idx;
199
200 for (idx = 0; idx < num_maps; idx++) {
201 if (map[idx].type == PIN_MAP_TYPE_MUX_GROUP) {
202 kfree(map[idx].data.mux.function);
203 if (!idx)
204 kfree(map[idx].data.mux.group);
205 } else if (map->type == PIN_MAP_TYPE_CONFIGS_GROUP) {
206 kfree(map[idx].data.configs.configs);
207 if (!idx)
208 kfree(map[idx].data.configs.group_or_pin);
209 }
210 };
211
212 kfree(map);
213}
214
215/* list of pinctrl callbacks for the pinctrl core */
216static struct pinctrl_ops samsung_pctrl_ops = {
217 .get_groups_count = samsung_get_group_count,
218 .get_group_name = samsung_get_group_name,
219 .get_group_pins = samsung_get_group_pins,
220 .dt_node_to_map = samsung_dt_node_to_map,
221 .dt_free_map = samsung_dt_free_map,
222};
223
224/* check if the selector is a valid pin function selector */
225static int samsung_get_functions_count(struct pinctrl_dev *pctldev)
226{
227 struct samsung_pinctrl_drv_data *drvdata;
228
229 drvdata = pinctrl_dev_get_drvdata(pctldev);
230 return drvdata->nr_functions;
231}
232
233/* return the name of the pin function specified */
234static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev,
235 unsigned selector)
236{
237 struct samsung_pinctrl_drv_data *drvdata;
238
239 drvdata = pinctrl_dev_get_drvdata(pctldev);
240 return drvdata->pmx_functions[selector].name;
241}
242
243/* return the groups associated for the specified function selector */
244static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev,
245 unsigned selector, const char * const **groups,
246 unsigned * const num_groups)
247{
248 struct samsung_pinctrl_drv_data *drvdata;
249
250 drvdata = pinctrl_dev_get_drvdata(pctldev);
251 *groups = drvdata->pmx_functions[selector].groups;
252 *num_groups = drvdata->pmx_functions[selector].num_groups;
253 return 0;
254}
255
256/*
257 * given a pin number that is local to a pin controller, find out the pin bank
258 * and the register base of the pin bank.
259 */
62f14c0e
TF
260static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata,
261 unsigned pin, void __iomem **reg, u32 *offset,
30574f0d
TA
262 struct samsung_pin_bank **bank)
263{
30574f0d
TA
264 struct samsung_pin_bank *b;
265
30574f0d
TA
266 b = drvdata->ctrl->pin_banks;
267
268 while ((pin >= b->pin_base) &&
269 ((b->pin_base + b->nr_pins - 1) < pin))
270 b++;
271
272 *reg = drvdata->virt_base + b->pctl_offset;
273 *offset = pin - b->pin_base;
274 if (bank)
275 *bank = b;
276
277 /* some banks have two config registers in a single bank */
278 if (*offset * b->func_width > BITS_PER_LONG)
279 *reg += 4;
280}
281
282/* enable or disable a pinmux function */
283static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
284 unsigned group, bool enable)
285{
286 struct samsung_pinctrl_drv_data *drvdata;
287 const unsigned int *pins;
288 struct samsung_pin_bank *bank;
289 void __iomem *reg;
290 u32 mask, shift, data, pin_offset, cnt;
291
292 drvdata = pinctrl_dev_get_drvdata(pctldev);
293 pins = drvdata->pin_groups[group].pins;
294
295 /*
296 * for each pin in the pin group selected, program the correspoding pin
297 * pin function number in the config register.
298 */
299 for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) {
62f14c0e 300 pin_to_reg_bank(drvdata, pins[cnt] - drvdata->ctrl->base,
30574f0d
TA
301 &reg, &pin_offset, &bank);
302 mask = (1 << bank->func_width) - 1;
303 shift = pin_offset * bank->func_width;
304
305 data = readl(reg);
306 data &= ~(mask << shift);
307 if (enable)
308 data |= drvdata->pin_groups[group].func << shift;
309 writel(data, reg);
310 }
311}
312
313/* enable a specified pinmux by writing to registers */
314static int samsung_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
315 unsigned group)
316{
317 samsung_pinmux_setup(pctldev, selector, group, true);
318 return 0;
319}
320
321/* disable a specified pinmux by writing to registers */
322static void samsung_pinmux_disable(struct pinctrl_dev *pctldev,
323 unsigned selector, unsigned group)
324{
325 samsung_pinmux_setup(pctldev, selector, group, false);
326}
327
328/*
329 * The calls to gpio_direction_output() and gpio_direction_input()
330 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
331 * function called from the gpiolib interface).
332 */
333static int samsung_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
334 struct pinctrl_gpio_range *range, unsigned offset, bool input)
335{
336 struct samsung_pin_bank *bank;
62f14c0e 337 struct samsung_pinctrl_drv_data *drvdata;
30574f0d
TA
338 void __iomem *reg;
339 u32 data, pin_offset, mask, shift;
340
d3a7b9e3 341 bank = gc_to_pin_bank(range->gc);
62f14c0e
TF
342 drvdata = pinctrl_dev_get_drvdata(pctldev);
343
d3a7b9e3
TF
344 pin_offset = offset - bank->pin_base;
345 reg = drvdata->virt_base + bank->pctl_offset;
346
30574f0d
TA
347 mask = (1 << bank->func_width) - 1;
348 shift = pin_offset * bank->func_width;
349
350 data = readl(reg);
351 data &= ~(mask << shift);
352 if (!input)
353 data |= FUNC_OUTPUT << shift;
354 writel(data, reg);
355 return 0;
356}
357
358/* list of pinmux callbacks for the pinmux vertical in pinctrl core */
359static struct pinmux_ops samsung_pinmux_ops = {
360 .get_functions_count = samsung_get_functions_count,
361 .get_function_name = samsung_pinmux_get_fname,
362 .get_function_groups = samsung_pinmux_get_groups,
363 .enable = samsung_pinmux_enable,
364 .disable = samsung_pinmux_disable,
365 .gpio_set_direction = samsung_pinmux_gpio_set_direction,
366};
367
368/* set or get the pin config settings for a specified pin */
369static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin,
370 unsigned long *config, bool set)
371{
372 struct samsung_pinctrl_drv_data *drvdata;
373 struct samsung_pin_bank *bank;
374 void __iomem *reg_base;
375 enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
376 u32 data, width, pin_offset, mask, shift;
377 u32 cfg_value, cfg_reg;
378
379 drvdata = pinctrl_dev_get_drvdata(pctldev);
62f14c0e 380 pin_to_reg_bank(drvdata, pin - drvdata->ctrl->base, &reg_base,
30574f0d
TA
381 &pin_offset, &bank);
382
383 switch (cfg_type) {
384 case PINCFG_TYPE_PUD:
385 width = bank->pud_width;
386 cfg_reg = PUD_REG;
387 break;
388 case PINCFG_TYPE_DRV:
389 width = bank->drv_width;
390 cfg_reg = DRV_REG;
391 break;
392 case PINCFG_TYPE_CON_PDN:
393 width = bank->conpdn_width;
394 cfg_reg = CONPDN_REG;
395 break;
396 case PINCFG_TYPE_PUD_PDN:
397 width = bank->pudpdn_width;
398 cfg_reg = PUDPDN_REG;
399 break;
400 default:
401 WARN_ON(1);
402 return -EINVAL;
403 }
404
7c367d3d
TF
405 if (!width)
406 return -EINVAL;
407
30574f0d
TA
408 mask = (1 << width) - 1;
409 shift = pin_offset * width;
410 data = readl(reg_base + cfg_reg);
411
412 if (set) {
413 cfg_value = PINCFG_UNPACK_VALUE(*config);
414 data &= ~(mask << shift);
415 data |= (cfg_value << shift);
416 writel(data, reg_base + cfg_reg);
417 } else {
418 data >>= shift;
419 data &= mask;
420 *config = PINCFG_PACK(cfg_type, data);
421 }
422 return 0;
423}
424
425/* set the pin config settings for a specified pin */
426static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
427 unsigned long config)
428{
429 return samsung_pinconf_rw(pctldev, pin, &config, true);
430}
431
432/* get the pin config settings for a specified pin */
433static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
434 unsigned long *config)
435{
436 return samsung_pinconf_rw(pctldev, pin, config, false);
437}
438
439/* set the pin config settings for a specified pin group */
440static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
441 unsigned group, unsigned long config)
442{
443 struct samsung_pinctrl_drv_data *drvdata;
444 const unsigned int *pins;
445 unsigned int cnt;
446
447 drvdata = pinctrl_dev_get_drvdata(pctldev);
448 pins = drvdata->pin_groups[group].pins;
449
450 for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
451 samsung_pinconf_set(pctldev, pins[cnt], config);
452
453 return 0;
454}
455
456/* get the pin config settings for a specified pin group */
457static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
458 unsigned int group, unsigned long *config)
459{
460 struct samsung_pinctrl_drv_data *drvdata;
461 const unsigned int *pins;
462
463 drvdata = pinctrl_dev_get_drvdata(pctldev);
464 pins = drvdata->pin_groups[group].pins;
465 samsung_pinconf_get(pctldev, pins[0], config);
466 return 0;
467}
468
469/* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
470static struct pinconf_ops samsung_pinconf_ops = {
471 .pin_config_get = samsung_pinconf_get,
472 .pin_config_set = samsung_pinconf_set,
473 .pin_config_group_get = samsung_pinconf_group_get,
474 .pin_config_group_set = samsung_pinconf_group_set,
475};
476
477/* gpiolib gpio_set callback function */
478static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
479{
d3a7b9e3 480 struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
30574f0d 481 void __iomem *reg;
d3a7b9e3 482 u32 data;
30574f0d 483
d3a7b9e3 484 reg = bank->drvdata->virt_base + bank->pctl_offset;
62f14c0e 485
30574f0d 486 data = readl(reg + DAT_REG);
d3a7b9e3 487 data &= ~(1 << offset);
30574f0d 488 if (value)
d3a7b9e3 489 data |= 1 << offset;
30574f0d
TA
490 writel(data, reg + DAT_REG);
491}
492
493/* gpiolib gpio_get callback function */
494static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
495{
496 void __iomem *reg;
d3a7b9e3
TF
497 u32 data;
498 struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
62f14c0e 499
d3a7b9e3 500 reg = bank->drvdata->virt_base + bank->pctl_offset;
30574f0d 501
30574f0d 502 data = readl(reg + DAT_REG);
d3a7b9e3 503 data >>= offset;
30574f0d
TA
504 data &= 1;
505 return data;
506}
507
508/*
509 * gpiolib gpio_direction_input callback function. The setting of the pin
510 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
511 * interface.
512 */
513static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
514{
515 return pinctrl_gpio_direction_input(gc->base + offset);
516}
517
518/*
519 * gpiolib gpio_direction_output callback function. The setting of the pin
520 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
521 * interface.
522 */
523static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
524 int value)
525{
526 samsung_gpio_set(gc, offset, value);
527 return pinctrl_gpio_direction_output(gc->base + offset);
528}
529
530/*
531 * Parse the pin names listed in the 'samsung,pins' property and convert it
532 * into a list of gpio numbers are create a pin group from it.
533 */
534static int __init samsung_pinctrl_parse_dt_pins(struct platform_device *pdev,
535 struct device_node *cfg_np, struct pinctrl_desc *pctl,
536 unsigned int **pin_list, unsigned int *npins)
537{
538 struct device *dev = &pdev->dev;
539 struct property *prop;
540 struct pinctrl_pin_desc const *pdesc = pctl->pins;
541 unsigned int idx = 0, cnt;
542 const char *pin_name;
543
544 *npins = of_property_count_strings(cfg_np, "samsung,pins");
545 if (*npins < 0) {
546 dev_err(dev, "invalid pin list in %s node", cfg_np->name);
547 return -EINVAL;
548 }
549
550 *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
551 if (!*pin_list) {
552 dev_err(dev, "failed to allocate memory for pin list\n");
553 return -ENOMEM;
554 }
555
556 of_property_for_each_string(cfg_np, "samsung,pins", prop, pin_name) {
557 for (cnt = 0; cnt < pctl->npins; cnt++) {
558 if (pdesc[cnt].name) {
559 if (!strcmp(pin_name, pdesc[cnt].name)) {
560 (*pin_list)[idx++] = pdesc[cnt].number;
561 break;
562 }
563 }
564 }
565 if (cnt == pctl->npins) {
566 dev_err(dev, "pin %s not valid in %s node\n",
567 pin_name, cfg_np->name);
568 devm_kfree(dev, *pin_list);
569 return -EINVAL;
570 }
571 }
572
573 return 0;
574}
575
576/*
577 * Parse the information about all the available pin groups and pin functions
578 * from device node of the pin-controller. A pin group is formed with all
579 * the pins listed in the "samsung,pins" property.
580 */
581static int __init samsung_pinctrl_parse_dt(struct platform_device *pdev,
582 struct samsung_pinctrl_drv_data *drvdata)
583{
584 struct device *dev = &pdev->dev;
585 struct device_node *dev_np = dev->of_node;
586 struct device_node *cfg_np;
587 struct samsung_pin_group *groups, *grp;
588 struct samsung_pmx_func *functions, *func;
589 unsigned *pin_list;
590 unsigned int npins, grp_cnt, func_idx = 0;
591 char *gname, *fname;
592 int ret;
593
594 grp_cnt = of_get_child_count(dev_np);
595 if (!grp_cnt)
596 return -EINVAL;
597
598 groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
599 if (!groups) {
600 dev_err(dev, "failed allocate memory for ping group list\n");
601 return -EINVAL;
602 }
603 grp = groups;
604
605 functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
606 if (!functions) {
607 dev_err(dev, "failed to allocate memory for function list\n");
608 return -EINVAL;
609 }
610 func = functions;
611
612 /*
613 * Iterate over all the child nodes of the pin controller node
614 * and create pin groups and pin function lists.
615 */
616 for_each_child_of_node(dev_np, cfg_np) {
617 u32 function;
724e56a4 618 if (!of_find_property(cfg_np, "samsung,pins", NULL))
30574f0d
TA
619 continue;
620
621 ret = samsung_pinctrl_parse_dt_pins(pdev, cfg_np,
622 &drvdata->pctl, &pin_list, &npins);
623 if (ret)
624 return ret;
625
626 /* derive pin group name from the node name */
627 gname = devm_kzalloc(dev, strlen(cfg_np->name) + GSUFFIX_LEN,
628 GFP_KERNEL);
629 if (!gname) {
630 dev_err(dev, "failed to alloc memory for group name\n");
631 return -ENOMEM;
632 }
633 sprintf(gname, "%s%s", cfg_np->name, GROUP_SUFFIX);
634
635 grp->name = gname;
636 grp->pins = pin_list;
637 grp->num_pins = npins;
638 of_property_read_u32(cfg_np, "samsung,pin-function", &function);
639 grp->func = function;
640 grp++;
641
642 if (!of_find_property(cfg_np, "samsung,pin-function", NULL))
643 continue;
644
645 /* derive function name from the node name */
646 fname = devm_kzalloc(dev, strlen(cfg_np->name) + FSUFFIX_LEN,
647 GFP_KERNEL);
648 if (!fname) {
649 dev_err(dev, "failed to alloc memory for func name\n");
650 return -ENOMEM;
651 }
652 sprintf(fname, "%s%s", cfg_np->name, FUNCTION_SUFFIX);
653
654 func->name = fname;
655 func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
656 if (!func->groups) {
657 dev_err(dev, "failed to alloc memory for group list "
658 "in pin function");
659 return -ENOMEM;
660 }
661 func->groups[0] = gname;
662 func->num_groups = 1;
663 func++;
664 func_idx++;
665 }
666
667 drvdata->pin_groups = groups;
668 drvdata->nr_groups = grp_cnt;
669 drvdata->pmx_functions = functions;
670 drvdata->nr_functions = func_idx;
671
672 return 0;
673}
674
675/* register the pinctrl interface with the pinctrl subsystem */
676static int __init samsung_pinctrl_register(struct platform_device *pdev,
677 struct samsung_pinctrl_drv_data *drvdata)
678{
679 struct pinctrl_desc *ctrldesc = &drvdata->pctl;
680 struct pinctrl_pin_desc *pindesc, *pdesc;
681 struct samsung_pin_bank *pin_bank;
682 char *pin_names;
683 int pin, bank, ret;
684
685 ctrldesc->name = "samsung-pinctrl";
686 ctrldesc->owner = THIS_MODULE;
687 ctrldesc->pctlops = &samsung_pctrl_ops;
688 ctrldesc->pmxops = &samsung_pinmux_ops;
689 ctrldesc->confops = &samsung_pinconf_ops;
690
691 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
692 drvdata->ctrl->nr_pins, GFP_KERNEL);
693 if (!pindesc) {
694 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
695 return -ENOMEM;
696 }
697 ctrldesc->pins = pindesc;
698 ctrldesc->npins = drvdata->ctrl->nr_pins;
699 ctrldesc->npins = drvdata->ctrl->nr_pins;
700
701 /* dynamically populate the pin number and pin name for pindesc */
702 for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
703 pdesc->number = pin + drvdata->ctrl->base;
704
705 /*
706 * allocate space for storing the dynamically generated names for all
707 * the pins which belong to this pin-controller.
708 */
709 pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
710 drvdata->ctrl->nr_pins, GFP_KERNEL);
711 if (!pin_names) {
712 dev_err(&pdev->dev, "mem alloc for pin names failed\n");
713 return -ENOMEM;
714 }
715
716 /* for each pin, the name of the pin is pin-bank name + pin number */
717 for (bank = 0; bank < drvdata->ctrl->nr_banks; bank++) {
718 pin_bank = &drvdata->ctrl->pin_banks[bank];
719 for (pin = 0; pin < pin_bank->nr_pins; pin++) {
720 sprintf(pin_names, "%s-%d", pin_bank->name, pin);
721 pdesc = pindesc + pin_bank->pin_base + pin;
722 pdesc->name = pin_names;
723 pin_names += PIN_NAME_LENGTH;
724 }
725 }
726
727 drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata);
728 if (!drvdata->pctl_dev) {
729 dev_err(&pdev->dev, "could not register pinctrl driver\n");
730 return -EINVAL;
731 }
732
d3a7b9e3
TF
733 for (bank = 0; bank < drvdata->ctrl->nr_banks; ++bank) {
734 pin_bank = &drvdata->ctrl->pin_banks[bank];
735 pin_bank->grange.name = pin_bank->name;
736 pin_bank->grange.id = bank;
737 pin_bank->grange.pin_base = pin_bank->pin_base;
738 pin_bank->grange.base = pin_bank->gpio_chip.base;
739 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
740 pin_bank->grange.gc = &pin_bank->gpio_chip;
741 pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange);
742 }
30574f0d
TA
743
744 ret = samsung_pinctrl_parse_dt(pdev, drvdata);
745 if (ret) {
746 pinctrl_unregister(drvdata->pctl_dev);
747 return ret;
748 }
749
750 return 0;
751}
752
d3a7b9e3
TF
753static const struct gpio_chip samsung_gpiolib_chip = {
754 .set = samsung_gpio_set,
755 .get = samsung_gpio_get,
756 .direction_input = samsung_gpio_direction_input,
757 .direction_output = samsung_gpio_direction_output,
758 .owner = THIS_MODULE,
759};
760
30574f0d
TA
761/* register the gpiolib interface with the gpiolib subsystem */
762static int __init samsung_gpiolib_register(struct platform_device *pdev,
763 struct samsung_pinctrl_drv_data *drvdata)
764{
d3a7b9e3
TF
765 struct samsung_pin_ctrl *ctrl = drvdata->ctrl;
766 struct samsung_pin_bank *bank = ctrl->pin_banks;
30574f0d
TA
767 struct gpio_chip *gc;
768 int ret;
d3a7b9e3
TF
769 int i;
770
771 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
772 bank->gpio_chip = samsung_gpiolib_chip;
773
774 gc = &bank->gpio_chip;
775 gc->base = ctrl->base + bank->pin_base;
776 gc->ngpio = bank->nr_pins;
777 gc->dev = &pdev->dev;
778 gc->of_node = bank->of_node;
779 gc->label = bank->name;
780
781 ret = gpiochip_add(gc);
782 if (ret) {
783 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
784 gc->label, ret);
785 goto fail;
786 }
30574f0d
TA
787 }
788
789 return 0;
d3a7b9e3
TF
790
791fail:
792 for (--i, --bank; i >= 0; --i, --bank)
793 if (gpiochip_remove(&bank->gpio_chip))
794 dev_err(&pdev->dev, "gpio chip %s remove failed\n",
795 bank->gpio_chip.label);
796 return ret;
30574f0d
TA
797}
798
799/* unregister the gpiolib interface with the gpiolib subsystem */
800static int __init samsung_gpiolib_unregister(struct platform_device *pdev,
801 struct samsung_pinctrl_drv_data *drvdata)
802{
d3a7b9e3
TF
803 struct samsung_pin_ctrl *ctrl = drvdata->ctrl;
804 struct samsung_pin_bank *bank = ctrl->pin_banks;
805 int ret = 0;
806 int i;
807
808 for (i = 0; !ret && i < ctrl->nr_banks; ++i, ++bank)
809 ret = gpiochip_remove(&bank->gpio_chip);
810
811 if (ret)
30574f0d 812 dev_err(&pdev->dev, "gpio chip remove failed\n");
d3a7b9e3
TF
813
814 return ret;
30574f0d
TA
815}
816
817static const struct of_device_id samsung_pinctrl_dt_match[];
818
819/* retrieve the soc specific data */
2f0253ff 820static struct samsung_pin_ctrl *samsung_pinctrl_get_soc_data(
6defe9a0 821 struct samsung_pinctrl_drv_data *d,
30574f0d
TA
822 struct platform_device *pdev)
823{
824 int id;
825 const struct of_device_id *match;
6defe9a0 826 struct device_node *node = pdev->dev.of_node;
ab663789 827 struct device_node *np;
40ba6227
TF
828 struct samsung_pin_ctrl *ctrl;
829 struct samsung_pin_bank *bank;
830 int i;
30574f0d 831
6defe9a0 832 id = of_alias_get_id(node, "pinctrl");
30574f0d
TA
833 if (id < 0) {
834 dev_err(&pdev->dev, "failed to get alias id\n");
835 return NULL;
836 }
837 match = of_match_node(samsung_pinctrl_dt_match, node);
40ba6227
TF
838 ctrl = (struct samsung_pin_ctrl *)match->data + id;
839
840 bank = ctrl->pin_banks;
841 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
6defe9a0 842 bank->drvdata = d;
40ba6227
TF
843 bank->pin_base = ctrl->nr_pins;
844 ctrl->nr_pins += bank->nr_pins;
40ba6227
TF
845 }
846
ab663789
TF
847 for_each_child_of_node(node, np) {
848 if (!of_find_property(np, "gpio-controller", NULL))
849 continue;
850 bank = ctrl->pin_banks;
851 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
852 if (!strcmp(bank->name, np->name)) {
853 bank->of_node = np;
854 break;
855 }
856 }
857 }
858
40ba6227
TF
859 ctrl->base = pin_base;
860 pin_base += ctrl->nr_pins;
861
862 return ctrl;
30574f0d
TA
863}
864
865static int __devinit samsung_pinctrl_probe(struct platform_device *pdev)
866{
867 struct samsung_pinctrl_drv_data *drvdata;
868 struct device *dev = &pdev->dev;
869 struct samsung_pin_ctrl *ctrl;
870 struct resource *res;
871 int ret;
872
873 if (!dev->of_node) {
874 dev_err(dev, "device tree node not found\n");
875 return -ENODEV;
876 }
877
30574f0d
TA
878 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
879 if (!drvdata) {
880 dev_err(dev, "failed to allocate memory for driver's "
881 "private data\n");
882 return -ENOMEM;
883 }
6defe9a0
TF
884
885 ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev);
886 if (!ctrl) {
887 dev_err(&pdev->dev, "driver data not available\n");
888 return -EINVAL;
889 }
30574f0d
TA
890 drvdata->ctrl = ctrl;
891 drvdata->dev = dev;
892
893 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
894 if (!res) {
895 dev_err(dev, "cannot find IO resource\n");
896 return -ENOENT;
897 }
898
899 drvdata->virt_base = devm_request_and_ioremap(&pdev->dev, res);
900 if (!drvdata->virt_base) {
901 dev_err(dev, "ioremap failed\n");
902 return -ENODEV;
903 }
904
905 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
906 if (res)
907 drvdata->irq = res->start;
908
909 ret = samsung_gpiolib_register(pdev, drvdata);
910 if (ret)
911 return ret;
912
913 ret = samsung_pinctrl_register(pdev, drvdata);
914 if (ret) {
915 samsung_gpiolib_unregister(pdev, drvdata);
916 return ret;
917 }
918
919 if (ctrl->eint_gpio_init)
920 ctrl->eint_gpio_init(drvdata);
921 if (ctrl->eint_wkup_init)
922 ctrl->eint_wkup_init(drvdata);
923
924 platform_set_drvdata(pdev, drvdata);
925 return 0;
926}
927
928static const struct of_device_id samsung_pinctrl_dt_match[] = {
929 { .compatible = "samsung,pinctrl-exynos4210",
930 .data = (void *)exynos4210_pin_ctrl },
931 {},
932};
933MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match);
934
935static struct platform_driver samsung_pinctrl_driver = {
936 .probe = samsung_pinctrl_probe,
937 .driver = {
938 .name = "samsung-pinctrl",
939 .owner = THIS_MODULE,
940 .of_match_table = of_match_ptr(samsung_pinctrl_dt_match),
941 },
942};
943
944static int __init samsung_pinctrl_drv_register(void)
945{
946 return platform_driver_register(&samsung_pinctrl_driver);
947}
948postcore_initcall(samsung_pinctrl_drv_register);
949
950static void __exit samsung_pinctrl_drv_unregister(void)
951{
952 platform_driver_unregister(&samsung_pinctrl_driver);
953}
954module_exit(samsung_pinctrl_drv_unregister);
955
956MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
957MODULE_DESCRIPTION("Samsung pinctrl driver");
958MODULE_LICENSE("GPL v2");