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