blob: b1d4ac8d36f88fca88c0371897a8d35fe552e2fd [file] [log] [blame]
Thomas Abraham30574f02012-09-07 06:07:19 +09001/*
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>
Tomasz Figaa19fe2d2012-10-11 10:11:20 +020029#include <linux/irqdomain.h>
Tomasz Figa19846952013-03-18 22:31:50 +010030#include <linux/spinlock.h>
Thomas Abraham30574f02012-09-07 06:07:19 +090031
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 */
Axel Lind5fd5da2012-12-10 09:54:03 +090041static struct pin_config {
Thomas Abraham30574f02012-09-07 06:07:19 +090042 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
Sachin Kamat6fb6f1ba2012-11-24 11:20:51 +090051static unsigned int pin_base;
Tomasz Figa40ba6222012-10-11 10:11:09 +020052
Tomasz Figad3a7b9e2012-10-11 10:11:17 +020053static 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
Thomas Abraham30574f02012-09-07 06:07:19 +090058/* 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 */
Laurent Pinchart022ab142013-02-16 10:25:07 +0100218static const struct pinctrl_ops samsung_pctrl_ops = {
Thomas Abraham30574f02012-09-07 06:07:19 +0900219 .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 */
Tomasz Figa62f14c02012-10-11 10:11:08 +0200262static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata,
263 unsigned pin, void __iomem **reg, u32 *offset,
Thomas Abraham30574f02012-09-07 06:07:19 +0900264 struct samsung_pin_bank **bank)
265{
Thomas Abraham30574f02012-09-07 06:07:19 +0900266 struct samsung_pin_bank *b;
267
Thomas Abraham30574f02012-09-07 06:07:19 +0900268 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;
278
279 /* some banks have two config registers in a single bank */
280 if (*offset * b->func_width > BITS_PER_LONG)
281 *reg += 4;
282}
283
284/* enable or disable a pinmux function */
285static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
286 unsigned group, bool enable)
287{
288 struct samsung_pinctrl_drv_data *drvdata;
289 const unsigned int *pins;
290 struct samsung_pin_bank *bank;
291 void __iomem *reg;
292 u32 mask, shift, data, pin_offset, cnt;
Tomasz Figa19846952013-03-18 22:31:50 +0100293 unsigned long flags;
Thomas Abraham30574f02012-09-07 06:07:19 +0900294
295 drvdata = pinctrl_dev_get_drvdata(pctldev);
296 pins = drvdata->pin_groups[group].pins;
297
298 /*
299 * for each pin in the pin group selected, program the correspoding pin
300 * pin function number in the config register.
301 */
302 for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) {
Tomasz Figa62f14c02012-10-11 10:11:08 +0200303 pin_to_reg_bank(drvdata, pins[cnt] - drvdata->ctrl->base,
Thomas Abraham30574f02012-09-07 06:07:19 +0900304 &reg, &pin_offset, &bank);
305 mask = (1 << bank->func_width) - 1;
306 shift = pin_offset * bank->func_width;
307
Tomasz Figa19846952013-03-18 22:31:50 +0100308 spin_lock_irqsave(&bank->slock, flags);
309
Thomas Abraham30574f02012-09-07 06:07:19 +0900310 data = readl(reg);
311 data &= ~(mask << shift);
312 if (enable)
313 data |= drvdata->pin_groups[group].func << shift;
314 writel(data, reg);
Tomasz Figa19846952013-03-18 22:31:50 +0100315
316 spin_unlock_irqrestore(&bank->slock, flags);
Thomas Abraham30574f02012-09-07 06:07:19 +0900317 }
318}
319
320/* enable a specified pinmux by writing to registers */
321static int samsung_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
322 unsigned group)
323{
324 samsung_pinmux_setup(pctldev, selector, group, true);
325 return 0;
326}
327
328/* disable a specified pinmux by writing to registers */
329static void samsung_pinmux_disable(struct pinctrl_dev *pctldev,
330 unsigned selector, unsigned group)
331{
332 samsung_pinmux_setup(pctldev, selector, group, false);
333}
334
335/*
336 * The calls to gpio_direction_output() and gpio_direction_input()
337 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
338 * function called from the gpiolib interface).
339 */
340static int samsung_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
341 struct pinctrl_gpio_range *range, unsigned offset, bool input)
342{
343 struct samsung_pin_bank *bank;
Tomasz Figa62f14c02012-10-11 10:11:08 +0200344 struct samsung_pinctrl_drv_data *drvdata;
Thomas Abraham30574f02012-09-07 06:07:19 +0900345 void __iomem *reg;
346 u32 data, pin_offset, mask, shift;
Tomasz Figa19846952013-03-18 22:31:50 +0100347 unsigned long flags;
Thomas Abraham30574f02012-09-07 06:07:19 +0900348
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200349 bank = gc_to_pin_bank(range->gc);
Tomasz Figa62f14c02012-10-11 10:11:08 +0200350 drvdata = pinctrl_dev_get_drvdata(pctldev);
351
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200352 pin_offset = offset - bank->pin_base;
353 reg = drvdata->virt_base + bank->pctl_offset;
354
Thomas Abraham30574f02012-09-07 06:07:19 +0900355 mask = (1 << bank->func_width) - 1;
356 shift = pin_offset * bank->func_width;
357
Tomasz Figa19846952013-03-18 22:31:50 +0100358 spin_lock_irqsave(&bank->slock, flags);
359
Thomas Abraham30574f02012-09-07 06:07:19 +0900360 data = readl(reg);
361 data &= ~(mask << shift);
362 if (!input)
363 data |= FUNC_OUTPUT << shift;
364 writel(data, reg);
Tomasz Figa19846952013-03-18 22:31:50 +0100365
366 spin_unlock_irqrestore(&bank->slock, flags);
367
Thomas Abraham30574f02012-09-07 06:07:19 +0900368 return 0;
369}
370
371/* list of pinmux callbacks for the pinmux vertical in pinctrl core */
Laurent Pinchart022ab142013-02-16 10:25:07 +0100372static const struct pinmux_ops samsung_pinmux_ops = {
Thomas Abraham30574f02012-09-07 06:07:19 +0900373 .get_functions_count = samsung_get_functions_count,
374 .get_function_name = samsung_pinmux_get_fname,
375 .get_function_groups = samsung_pinmux_get_groups,
376 .enable = samsung_pinmux_enable,
377 .disable = samsung_pinmux_disable,
378 .gpio_set_direction = samsung_pinmux_gpio_set_direction,
379};
380
381/* set or get the pin config settings for a specified pin */
382static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin,
383 unsigned long *config, bool set)
384{
385 struct samsung_pinctrl_drv_data *drvdata;
386 struct samsung_pin_bank *bank;
387 void __iomem *reg_base;
388 enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
389 u32 data, width, pin_offset, mask, shift;
390 u32 cfg_value, cfg_reg;
Tomasz Figa19846952013-03-18 22:31:50 +0100391 unsigned long flags;
Thomas Abraham30574f02012-09-07 06:07:19 +0900392
393 drvdata = pinctrl_dev_get_drvdata(pctldev);
Tomasz Figa62f14c02012-10-11 10:11:08 +0200394 pin_to_reg_bank(drvdata, pin - drvdata->ctrl->base, &reg_base,
Thomas Abraham30574f02012-09-07 06:07:19 +0900395 &pin_offset, &bank);
396
397 switch (cfg_type) {
398 case PINCFG_TYPE_PUD:
399 width = bank->pud_width;
400 cfg_reg = PUD_REG;
401 break;
402 case PINCFG_TYPE_DRV:
403 width = bank->drv_width;
404 cfg_reg = DRV_REG;
405 break;
406 case PINCFG_TYPE_CON_PDN:
407 width = bank->conpdn_width;
408 cfg_reg = CONPDN_REG;
409 break;
410 case PINCFG_TYPE_PUD_PDN:
411 width = bank->pudpdn_width;
412 cfg_reg = PUDPDN_REG;
413 break;
414 default:
415 WARN_ON(1);
416 return -EINVAL;
417 }
418
Tomasz Figa7c367d32012-10-11 10:11:07 +0200419 if (!width)
420 return -EINVAL;
421
Tomasz Figa19846952013-03-18 22:31:50 +0100422 spin_lock_irqsave(&bank->slock, flags);
423
Thomas Abraham30574f02012-09-07 06:07:19 +0900424 mask = (1 << width) - 1;
425 shift = pin_offset * width;
426 data = readl(reg_base + cfg_reg);
427
428 if (set) {
429 cfg_value = PINCFG_UNPACK_VALUE(*config);
430 data &= ~(mask << shift);
431 data |= (cfg_value << shift);
432 writel(data, reg_base + cfg_reg);
433 } else {
434 data >>= shift;
435 data &= mask;
436 *config = PINCFG_PACK(cfg_type, data);
437 }
Tomasz Figa19846952013-03-18 22:31:50 +0100438
439 spin_unlock_irqrestore(&bank->slock, flags);
440
Thomas Abraham30574f02012-09-07 06:07:19 +0900441 return 0;
442}
443
444/* set the pin config settings for a specified pin */
445static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
446 unsigned long config)
447{
448 return samsung_pinconf_rw(pctldev, pin, &config, true);
449}
450
451/* get the pin config settings for a specified pin */
452static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
453 unsigned long *config)
454{
455 return samsung_pinconf_rw(pctldev, pin, config, false);
456}
457
458/* set the pin config settings for a specified pin group */
459static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
460 unsigned group, unsigned long config)
461{
462 struct samsung_pinctrl_drv_data *drvdata;
463 const unsigned int *pins;
464 unsigned int cnt;
465
466 drvdata = pinctrl_dev_get_drvdata(pctldev);
467 pins = drvdata->pin_groups[group].pins;
468
469 for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
470 samsung_pinconf_set(pctldev, pins[cnt], config);
471
472 return 0;
473}
474
475/* get the pin config settings for a specified pin group */
476static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
477 unsigned int group, unsigned long *config)
478{
479 struct samsung_pinctrl_drv_data *drvdata;
480 const unsigned int *pins;
481
482 drvdata = pinctrl_dev_get_drvdata(pctldev);
483 pins = drvdata->pin_groups[group].pins;
484 samsung_pinconf_get(pctldev, pins[0], config);
485 return 0;
486}
487
488/* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
Laurent Pinchart022ab142013-02-16 10:25:07 +0100489static const struct pinconf_ops samsung_pinconf_ops = {
Thomas Abraham30574f02012-09-07 06:07:19 +0900490 .pin_config_get = samsung_pinconf_get,
491 .pin_config_set = samsung_pinconf_set,
492 .pin_config_group_get = samsung_pinconf_group_get,
493 .pin_config_group_set = samsung_pinconf_group_set,
494};
495
496/* gpiolib gpio_set callback function */
497static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
498{
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200499 struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
Tomasz Figa19846952013-03-18 22:31:50 +0100500 unsigned long flags;
Thomas Abraham30574f02012-09-07 06:07:19 +0900501 void __iomem *reg;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200502 u32 data;
Thomas Abraham30574f02012-09-07 06:07:19 +0900503
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200504 reg = bank->drvdata->virt_base + bank->pctl_offset;
Tomasz Figa62f14c02012-10-11 10:11:08 +0200505
Tomasz Figa19846952013-03-18 22:31:50 +0100506 spin_lock_irqsave(&bank->slock, flags);
507
Thomas Abraham30574f02012-09-07 06:07:19 +0900508 data = readl(reg + DAT_REG);
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200509 data &= ~(1 << offset);
Thomas Abraham30574f02012-09-07 06:07:19 +0900510 if (value)
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200511 data |= 1 << offset;
Thomas Abraham30574f02012-09-07 06:07:19 +0900512 writel(data, reg + DAT_REG);
Tomasz Figa19846952013-03-18 22:31:50 +0100513
514 spin_unlock_irqrestore(&bank->slock, flags);
Thomas Abraham30574f02012-09-07 06:07:19 +0900515}
516
517/* gpiolib gpio_get callback function */
518static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
519{
520 void __iomem *reg;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200521 u32 data;
522 struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
Thomas Abraham30574f02012-09-07 06:07:19 +0900523
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200524 reg = bank->drvdata->virt_base + bank->pctl_offset;
Tomasz Figa62f14c02012-10-11 10:11:08 +0200525
Thomas Abraham30574f02012-09-07 06:07:19 +0900526 data = readl(reg + DAT_REG);
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200527 data >>= offset;
Thomas Abraham30574f02012-09-07 06:07:19 +0900528 data &= 1;
529 return data;
530}
531
532/*
533 * gpiolib gpio_direction_input callback function. The setting of the pin
534 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
535 * interface.
536 */
537static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
538{
539 return pinctrl_gpio_direction_input(gc->base + offset);
540}
541
542/*
543 * gpiolib gpio_direction_output callback function. The setting of the pin
544 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
545 * interface.
546 */
547static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
548 int value)
549{
550 samsung_gpio_set(gc, offset, value);
551 return pinctrl_gpio_direction_output(gc->base + offset);
552}
553
554/*
Tomasz Figaa19fe2d2012-10-11 10:11:20 +0200555 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
556 * and a virtual IRQ, if not already present.
557 */
558static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
559{
560 struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
561 unsigned int virq;
562
563 if (!bank->irq_domain)
564 return -ENXIO;
565
566 virq = irq_create_mapping(bank->irq_domain, offset);
567
568 return (virq) ? : -ENXIO;
569}
570
571/*
Thomas Abraham30574f02012-09-07 06:07:19 +0900572 * Parse the pin names listed in the 'samsung,pins' property and convert it
573 * into a list of gpio numbers are create a pin group from it.
574 */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800575static int samsung_pinctrl_parse_dt_pins(struct platform_device *pdev,
576 struct device_node *cfg_np,
577 struct pinctrl_desc *pctl,
578 unsigned int **pin_list,
579 unsigned int *npins)
Thomas Abraham30574f02012-09-07 06:07:19 +0900580{
581 struct device *dev = &pdev->dev;
582 struct property *prop;
583 struct pinctrl_pin_desc const *pdesc = pctl->pins;
584 unsigned int idx = 0, cnt;
585 const char *pin_name;
586
587 *npins = of_property_count_strings(cfg_np, "samsung,pins");
Tushar Behera4a991b42012-11-19 09:45:19 +0900588 if (IS_ERR_VALUE(*npins)) {
Thomas Abraham30574f02012-09-07 06:07:19 +0900589 dev_err(dev, "invalid pin list in %s node", cfg_np->name);
590 return -EINVAL;
591 }
592
593 *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
594 if (!*pin_list) {
595 dev_err(dev, "failed to allocate memory for pin list\n");
596 return -ENOMEM;
597 }
598
599 of_property_for_each_string(cfg_np, "samsung,pins", prop, pin_name) {
600 for (cnt = 0; cnt < pctl->npins; cnt++) {
601 if (pdesc[cnt].name) {
602 if (!strcmp(pin_name, pdesc[cnt].name)) {
603 (*pin_list)[idx++] = pdesc[cnt].number;
604 break;
605 }
606 }
607 }
608 if (cnt == pctl->npins) {
609 dev_err(dev, "pin %s not valid in %s node\n",
610 pin_name, cfg_np->name);
611 devm_kfree(dev, *pin_list);
612 return -EINVAL;
613 }
614 }
615
616 return 0;
617}
618
619/*
620 * Parse the information about all the available pin groups and pin functions
621 * from device node of the pin-controller. A pin group is formed with all
622 * the pins listed in the "samsung,pins" property.
623 */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800624static int samsung_pinctrl_parse_dt(struct platform_device *pdev,
625 struct samsung_pinctrl_drv_data *drvdata)
Thomas Abraham30574f02012-09-07 06:07:19 +0900626{
627 struct device *dev = &pdev->dev;
628 struct device_node *dev_np = dev->of_node;
629 struct device_node *cfg_np;
630 struct samsung_pin_group *groups, *grp;
631 struct samsung_pmx_func *functions, *func;
632 unsigned *pin_list;
633 unsigned int npins, grp_cnt, func_idx = 0;
634 char *gname, *fname;
635 int ret;
636
637 grp_cnt = of_get_child_count(dev_np);
638 if (!grp_cnt)
639 return -EINVAL;
640
641 groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
642 if (!groups) {
643 dev_err(dev, "failed allocate memory for ping group list\n");
644 return -EINVAL;
645 }
646 grp = groups;
647
648 functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
649 if (!functions) {
650 dev_err(dev, "failed to allocate memory for function list\n");
651 return -EINVAL;
652 }
653 func = functions;
654
655 /*
656 * Iterate over all the child nodes of the pin controller node
657 * and create pin groups and pin function lists.
658 */
659 for_each_child_of_node(dev_np, cfg_np) {
660 u32 function;
Tomasz Figa724e56a2012-10-11 10:11:11 +0200661 if (!of_find_property(cfg_np, "samsung,pins", NULL))
Thomas Abraham30574f02012-09-07 06:07:19 +0900662 continue;
663
664 ret = samsung_pinctrl_parse_dt_pins(pdev, cfg_np,
665 &drvdata->pctl, &pin_list, &npins);
666 if (ret)
667 return ret;
668
669 /* derive pin group name from the node name */
670 gname = devm_kzalloc(dev, strlen(cfg_np->name) + GSUFFIX_LEN,
671 GFP_KERNEL);
672 if (!gname) {
673 dev_err(dev, "failed to alloc memory for group name\n");
674 return -ENOMEM;
675 }
676 sprintf(gname, "%s%s", cfg_np->name, GROUP_SUFFIX);
677
678 grp->name = gname;
679 grp->pins = pin_list;
680 grp->num_pins = npins;
681 of_property_read_u32(cfg_np, "samsung,pin-function", &function);
682 grp->func = function;
683 grp++;
684
685 if (!of_find_property(cfg_np, "samsung,pin-function", NULL))
686 continue;
687
688 /* derive function name from the node name */
689 fname = devm_kzalloc(dev, strlen(cfg_np->name) + FSUFFIX_LEN,
690 GFP_KERNEL);
691 if (!fname) {
692 dev_err(dev, "failed to alloc memory for func name\n");
693 return -ENOMEM;
694 }
695 sprintf(fname, "%s%s", cfg_np->name, FUNCTION_SUFFIX);
696
697 func->name = fname;
698 func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
699 if (!func->groups) {
700 dev_err(dev, "failed to alloc memory for group list "
701 "in pin function");
702 return -ENOMEM;
703 }
704 func->groups[0] = gname;
705 func->num_groups = 1;
706 func++;
707 func_idx++;
708 }
709
710 drvdata->pin_groups = groups;
711 drvdata->nr_groups = grp_cnt;
712 drvdata->pmx_functions = functions;
713 drvdata->nr_functions = func_idx;
714
715 return 0;
716}
717
718/* register the pinctrl interface with the pinctrl subsystem */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800719static int samsung_pinctrl_register(struct platform_device *pdev,
720 struct samsung_pinctrl_drv_data *drvdata)
Thomas Abraham30574f02012-09-07 06:07:19 +0900721{
722 struct pinctrl_desc *ctrldesc = &drvdata->pctl;
723 struct pinctrl_pin_desc *pindesc, *pdesc;
724 struct samsung_pin_bank *pin_bank;
725 char *pin_names;
726 int pin, bank, ret;
727
728 ctrldesc->name = "samsung-pinctrl";
729 ctrldesc->owner = THIS_MODULE;
730 ctrldesc->pctlops = &samsung_pctrl_ops;
731 ctrldesc->pmxops = &samsung_pinmux_ops;
732 ctrldesc->confops = &samsung_pinconf_ops;
733
734 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
735 drvdata->ctrl->nr_pins, GFP_KERNEL);
736 if (!pindesc) {
737 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
738 return -ENOMEM;
739 }
740 ctrldesc->pins = pindesc;
741 ctrldesc->npins = drvdata->ctrl->nr_pins;
Thomas Abraham30574f02012-09-07 06:07:19 +0900742
743 /* dynamically populate the pin number and pin name for pindesc */
744 for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
745 pdesc->number = pin + drvdata->ctrl->base;
746
747 /*
748 * allocate space for storing the dynamically generated names for all
749 * the pins which belong to this pin-controller.
750 */
751 pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
752 drvdata->ctrl->nr_pins, GFP_KERNEL);
753 if (!pin_names) {
754 dev_err(&pdev->dev, "mem alloc for pin names failed\n");
755 return -ENOMEM;
756 }
757
758 /* for each pin, the name of the pin is pin-bank name + pin number */
759 for (bank = 0; bank < drvdata->ctrl->nr_banks; bank++) {
760 pin_bank = &drvdata->ctrl->pin_banks[bank];
761 for (pin = 0; pin < pin_bank->nr_pins; pin++) {
762 sprintf(pin_names, "%s-%d", pin_bank->name, pin);
763 pdesc = pindesc + pin_bank->pin_base + pin;
764 pdesc->name = pin_names;
765 pin_names += PIN_NAME_LENGTH;
766 }
767 }
768
769 drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata);
770 if (!drvdata->pctl_dev) {
771 dev_err(&pdev->dev, "could not register pinctrl driver\n");
772 return -EINVAL;
773 }
774
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200775 for (bank = 0; bank < drvdata->ctrl->nr_banks; ++bank) {
776 pin_bank = &drvdata->ctrl->pin_banks[bank];
777 pin_bank->grange.name = pin_bank->name;
778 pin_bank->grange.id = bank;
779 pin_bank->grange.pin_base = pin_bank->pin_base;
780 pin_bank->grange.base = pin_bank->gpio_chip.base;
781 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
782 pin_bank->grange.gc = &pin_bank->gpio_chip;
783 pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange);
784 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900785
786 ret = samsung_pinctrl_parse_dt(pdev, drvdata);
787 if (ret) {
788 pinctrl_unregister(drvdata->pctl_dev);
789 return ret;
790 }
791
792 return 0;
793}
794
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200795static const struct gpio_chip samsung_gpiolib_chip = {
796 .set = samsung_gpio_set,
797 .get = samsung_gpio_get,
798 .direction_input = samsung_gpio_direction_input,
799 .direction_output = samsung_gpio_direction_output,
Tomasz Figaa19fe2d2012-10-11 10:11:20 +0200800 .to_irq = samsung_gpio_to_irq,
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200801 .owner = THIS_MODULE,
802};
803
Thomas Abraham30574f02012-09-07 06:07:19 +0900804/* register the gpiolib interface with the gpiolib subsystem */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800805static int samsung_gpiolib_register(struct platform_device *pdev,
806 struct samsung_pinctrl_drv_data *drvdata)
Thomas Abraham30574f02012-09-07 06:07:19 +0900807{
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200808 struct samsung_pin_ctrl *ctrl = drvdata->ctrl;
809 struct samsung_pin_bank *bank = ctrl->pin_banks;
Thomas Abraham30574f02012-09-07 06:07:19 +0900810 struct gpio_chip *gc;
811 int ret;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200812 int i;
Thomas Abraham30574f02012-09-07 06:07:19 +0900813
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200814 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
815 bank->gpio_chip = samsung_gpiolib_chip;
Thomas Abraham30574f02012-09-07 06:07:19 +0900816
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200817 gc = &bank->gpio_chip;
818 gc->base = ctrl->base + bank->pin_base;
819 gc->ngpio = bank->nr_pins;
820 gc->dev = &pdev->dev;
821 gc->of_node = bank->of_node;
822 gc->label = bank->name;
823
824 ret = gpiochip_add(gc);
825 if (ret) {
826 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
827 gc->label, ret);
828 goto fail;
829 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900830 }
831
832 return 0;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200833
834fail:
835 for (--i, --bank; i >= 0; --i, --bank)
836 if (gpiochip_remove(&bank->gpio_chip))
837 dev_err(&pdev->dev, "gpio chip %s remove failed\n",
838 bank->gpio_chip.label);
839 return ret;
Thomas Abraham30574f02012-09-07 06:07:19 +0900840}
841
842/* unregister the gpiolib interface with the gpiolib subsystem */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800843static int samsung_gpiolib_unregister(struct platform_device *pdev,
844 struct samsung_pinctrl_drv_data *drvdata)
Thomas Abraham30574f02012-09-07 06:07:19 +0900845{
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200846 struct samsung_pin_ctrl *ctrl = drvdata->ctrl;
847 struct samsung_pin_bank *bank = ctrl->pin_banks;
848 int ret = 0;
849 int i;
850
851 for (i = 0; !ret && i < ctrl->nr_banks; ++i, ++bank)
852 ret = gpiochip_remove(&bank->gpio_chip);
853
854 if (ret)
Thomas Abraham30574f02012-09-07 06:07:19 +0900855 dev_err(&pdev->dev, "gpio chip remove failed\n");
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200856
857 return ret;
Thomas Abraham30574f02012-09-07 06:07:19 +0900858}
859
860static const struct of_device_id samsung_pinctrl_dt_match[];
861
862/* retrieve the soc specific data */
Tomasz Figa2f0253f2012-09-21 07:34:04 +0900863static struct samsung_pin_ctrl *samsung_pinctrl_get_soc_data(
Tomasz Figa6defe9a2012-10-11 10:11:14 +0200864 struct samsung_pinctrl_drv_data *d,
Thomas Abraham30574f02012-09-07 06:07:19 +0900865 struct platform_device *pdev)
866{
867 int id;
868 const struct of_device_id *match;
Tomasz Figa6defe9a2012-10-11 10:11:14 +0200869 struct device_node *node = pdev->dev.of_node;
Tomasz Figaab663782012-10-11 10:11:13 +0200870 struct device_node *np;
Tomasz Figa40ba6222012-10-11 10:11:09 +0200871 struct samsung_pin_ctrl *ctrl;
872 struct samsung_pin_bank *bank;
873 int i;
Thomas Abraham30574f02012-09-07 06:07:19 +0900874
Tomasz Figa6defe9a2012-10-11 10:11:14 +0200875 id = of_alias_get_id(node, "pinctrl");
Thomas Abraham30574f02012-09-07 06:07:19 +0900876 if (id < 0) {
877 dev_err(&pdev->dev, "failed to get alias id\n");
878 return NULL;
879 }
880 match = of_match_node(samsung_pinctrl_dt_match, node);
Tomasz Figa40ba6222012-10-11 10:11:09 +0200881 ctrl = (struct samsung_pin_ctrl *)match->data + id;
882
883 bank = ctrl->pin_banks;
884 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
Tomasz Figa19846952013-03-18 22:31:50 +0100885 spin_lock_init(&bank->slock);
Tomasz Figa6defe9a2012-10-11 10:11:14 +0200886 bank->drvdata = d;
Tomasz Figa40ba6222012-10-11 10:11:09 +0200887 bank->pin_base = ctrl->nr_pins;
888 ctrl->nr_pins += bank->nr_pins;
Tomasz Figa40ba6222012-10-11 10:11:09 +0200889 }
890
Tomasz Figaab663782012-10-11 10:11:13 +0200891 for_each_child_of_node(node, np) {
892 if (!of_find_property(np, "gpio-controller", NULL))
893 continue;
894 bank = ctrl->pin_banks;
895 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
896 if (!strcmp(bank->name, np->name)) {
897 bank->of_node = np;
898 break;
899 }
900 }
901 }
902
Tomasz Figa40ba6222012-10-11 10:11:09 +0200903 ctrl->base = pin_base;
904 pin_base += ctrl->nr_pins;
905
906 return ctrl;
Thomas Abraham30574f02012-09-07 06:07:19 +0900907}
908
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800909static int samsung_pinctrl_probe(struct platform_device *pdev)
Thomas Abraham30574f02012-09-07 06:07:19 +0900910{
911 struct samsung_pinctrl_drv_data *drvdata;
912 struct device *dev = &pdev->dev;
913 struct samsung_pin_ctrl *ctrl;
914 struct resource *res;
915 int ret;
916
917 if (!dev->of_node) {
918 dev_err(dev, "device tree node not found\n");
919 return -ENODEV;
920 }
921
Thomas Abraham30574f02012-09-07 06:07:19 +0900922 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
923 if (!drvdata) {
924 dev_err(dev, "failed to allocate memory for driver's "
925 "private data\n");
926 return -ENOMEM;
927 }
Tomasz Figa6defe9a2012-10-11 10:11:14 +0200928
929 ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev);
930 if (!ctrl) {
931 dev_err(&pdev->dev, "driver data not available\n");
932 return -EINVAL;
933 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900934 drvdata->ctrl = ctrl;
935 drvdata->dev = dev;
936
937 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
938 if (!res) {
939 dev_err(dev, "cannot find IO resource\n");
940 return -ENOENT;
941 }
942
Thierry Reding9e0c1fb2013-01-21 11:09:14 +0100943 drvdata->virt_base = devm_ioremap_resource(&pdev->dev, res);
944 if (IS_ERR(drvdata->virt_base))
945 return PTR_ERR(drvdata->virt_base);
Thomas Abraham30574f02012-09-07 06:07:19 +0900946
947 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
948 if (res)
949 drvdata->irq = res->start;
950
951 ret = samsung_gpiolib_register(pdev, drvdata);
952 if (ret)
953 return ret;
954
955 ret = samsung_pinctrl_register(pdev, drvdata);
956 if (ret) {
957 samsung_gpiolib_unregister(pdev, drvdata);
958 return ret;
959 }
960
961 if (ctrl->eint_gpio_init)
962 ctrl->eint_gpio_init(drvdata);
963 if (ctrl->eint_wkup_init)
964 ctrl->eint_wkup_init(drvdata);
965
966 platform_set_drvdata(pdev, drvdata);
967 return 0;
968}
969
970static const struct of_device_id samsung_pinctrl_dt_match[] = {
Kukjin Kimb533c862013-01-02 16:05:42 -0800971 { .compatible = "samsung,exynos4210-pinctrl",
Thomas Abraham30574f02012-09-07 06:07:19 +0900972 .data = (void *)exynos4210_pin_ctrl },
Kukjin Kimb533c862013-01-02 16:05:42 -0800973 { .compatible = "samsung,exynos4x12-pinctrl",
Tomasz Figa6edc7942012-11-07 08:44:59 +0900974 .data = (void *)exynos4x12_pin_ctrl },
Thomas Abraham30574f02012-09-07 06:07:19 +0900975 {},
976};
977MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match);
978
979static struct platform_driver samsung_pinctrl_driver = {
980 .probe = samsung_pinctrl_probe,
981 .driver = {
982 .name = "samsung-pinctrl",
983 .owner = THIS_MODULE,
984 .of_match_table = of_match_ptr(samsung_pinctrl_dt_match),
985 },
986};
987
988static int __init samsung_pinctrl_drv_register(void)
989{
990 return platform_driver_register(&samsung_pinctrl_driver);
991}
992postcore_initcall(samsung_pinctrl_drv_register);
993
994static void __exit samsung_pinctrl_drv_unregister(void)
995{
996 platform_driver_unregister(&samsung_pinctrl_driver);
997}
998module_exit(samsung_pinctrl_drv_unregister);
999
1000MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1001MODULE_DESCRIPTION("Samsung pinctrl driver");
1002MODULE_LICENSE("GPL v2");