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