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