23 #include <linux/module.h>
26 #include <linux/slab.h>
33 #define GROUP_SUFFIX "-grp"
34 #define GSUFFIX_LEN sizeof(GROUP_SUFFIX)
35 #define FUNCTION_SUFFIX "-mux"
36 #define FSUFFIX_LEN sizeof(FUNCTION_SUFFIX)
50 static int samsung_get_group_count(
struct pinctrl_dev *pctldev)
59 static const char *samsung_get_group_name(
struct pinctrl_dev *pctldev,
69 static int samsung_get_group_pins(
struct pinctrl_dev *pctldev,
70 unsigned selector,
const unsigned **
pins,
unsigned *num_pins)
76 *num_pins = drvdata->
pin_groups[selector].num_pins;
81 static int samsung_dt_node_to_map(
struct pinctrl_dev *pctldev,
89 int cfg_cnt = 0, map_cnt = 0,
idx = 0;
106 dev_err(dev,
"node %s does not have either config or function "
107 "configurations\n", np->
name);
112 map = kzalloc(
sizeof(*map) * map_cnt,
GFP_KERNEL);
114 dev_err(dev,
"could not alloc memory for pin-maps\n");
125 dev_err(dev,
"failed to alloc memory for group name\n");
138 cfg = kzalloc(
sizeof(*cfg) * cfg_cnt,
GFP_KERNEL);
140 dev_err(dev,
"failed to alloc memory for configs\n");
147 if (!of_property_read_u32(np,
pcfgs[
idx].prop_cfg, &value))
164 dev_err(dev,
"failed to alloc memory for func name\n");
169 map[*nmaps].
data.
mux.group = gname;
188 static void samsung_dt_free_map(
struct pinctrl_dev *pctldev,
193 for (idx = 0; idx < num_maps; idx++) {
201 kfree(map[idx].
data.configs.group_or_pin);
209 static 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,
218 static int samsung_get_functions_count(
struct pinctrl_dev *pctldev)
227 static const char *samsung_pinmux_get_fname(
struct pinctrl_dev *pctldev,
237 static int samsung_pinmux_get_groups(
struct pinctrl_dev *pctldev,
238 unsigned selector,
const char *
const **groups,
239 unsigned *
const num_groups)
253 static void pin_to_reg_bank(
struct gpio_chip *
gc,
unsigned pin,
261 b = drvdata->
ctrl->pin_banks;
282 const unsigned int *
pins;
295 pin_to_reg_bank(drvdata->
gc, pins[cnt] - drvdata->
ctrl->base,
296 ®, &pin_offset, &bank);
301 data &= ~(mask << shift);
309 static int samsung_pinmux_enable(
struct pinctrl_dev *pctldev,
unsigned selector,
312 samsung_pinmux_setup(pctldev, selector, group,
true);
317 static void samsung_pinmux_disable(
struct pinctrl_dev *pctldev,
318 unsigned selector,
unsigned group)
320 samsung_pinmux_setup(pctldev, selector, group,
false);
328 static int samsung_pinmux_gpio_set_direction(
struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *
range,
unsigned offset,
bool input)
335 pin_to_reg_bank(range->gc, offset, ®, &pin_offset, &bank);
340 data &= ~(mask << shift);
348 static 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,
358 static int samsung_pinconf_rw(
struct pinctrl_dev *pctldev,
unsigned int pin,
359 unsigned long *
config,
bool set)
366 u32 cfg_value, cfg_reg;
369 pin_to_reg_bank(drvdata->
gc, pin - drvdata->
ctrl->base, ®_base,
394 mask = (1 <<
width) - 1;
395 shift = pin_offset *
width;
396 data =
readl(reg_base + cfg_reg);
400 data &= ~(mask << shift);
401 data |= (cfg_value << shift);
402 writel(data, reg_base + cfg_reg);
412 static int samsung_pinconf_set(
struct pinctrl_dev *pctldev,
unsigned int pin,
413 unsigned long config)
415 return samsung_pinconf_rw(pctldev, pin, &config,
true);
419 static int samsung_pinconf_get(
struct pinctrl_dev *pctldev,
unsigned int pin,
420 unsigned long *config)
422 return samsung_pinconf_rw(pctldev, pin, config,
false);
426 static int samsung_pinconf_group_set(
struct pinctrl_dev *pctldev,
427 unsigned group,
unsigned long config)
430 const unsigned int *
pins;
437 samsung_pinconf_set(pctldev, pins[cnt], config);
443 static int samsung_pinconf_group_get(
struct pinctrl_dev *pctldev,
444 unsigned int group,
unsigned long *config)
447 const unsigned int *
pins;
451 samsung_pinconf_get(pctldev, pins[0], config);
456 static struct pinconf_ops samsung_pinconf_ops = {
457 .pin_config_get = samsung_pinconf_get,
458 .pin_config_set = samsung_pinconf_set,
459 .pin_config_group_get = samsung_pinconf_group_get,
460 .pin_config_group_set = samsung_pinconf_group_set,
464 static void samsung_gpio_set(
struct gpio_chip *gc,
unsigned offset,
int value)
469 pin_to_reg_bank(gc, offset, ®, &pin_offset,
NULL);
471 data &= ~(1 << pin_offset);
473 data |= 1 << pin_offset;
478 static int samsung_gpio_get(
struct gpio_chip *gc,
unsigned offset)
483 pin_to_reg_bank(gc, offset, ®, &pin_offset,
NULL);
495 static int samsung_gpio_direction_input(
struct gpio_chip *gc,
unsigned offset)
505 static int samsung_gpio_direction_output(
struct gpio_chip *gc,
unsigned offset,
508 samsung_gpio_set(gc, offset, value);
517 struct device_node *cfg_np,
struct pinctrl_desc *pctl,
518 unsigned int **pin_list,
unsigned int *npins)
522 struct pinctrl_pin_desc
const *
pdesc = pctl->pins;
523 unsigned int idx = 0,
cnt;
524 const char *pin_name;
528 dev_err(dev,
"invalid pin list in %s node", cfg_np->
name);
534 dev_err(dev,
"failed to allocate memory for pin list\n");
539 for (cnt = 0; cnt < pctl->npins; cnt++) {
540 if (pdesc[cnt].
name) {
541 if (!
strcmp(pin_name, pdesc[cnt].name)) {
542 (*pin_list)[idx++] = pdesc[
cnt].number;
547 if (cnt == pctl->npins) {
548 dev_err(dev,
"pin %s not valid in %s node\n",
549 pin_name, cfg_np->
name);
572 unsigned int npins, grp_cnt, func_idx = 0;
576 grp_cnt = of_get_child_count(dev_np);
582 dev_err(dev,
"failed allocate memory for ping group list\n");
589 dev_err(dev,
"failed to allocate memory for function list\n");
603 ret = samsung_pinctrl_parse_dt_pins(pdev, cfg_np,
604 &drvdata->
pctl, &pin_list, &npins);
612 dev_err(dev,
"failed to alloc memory for group name\n");
618 grp->
pins = pin_list;
620 of_property_read_u32(cfg_np,
"samsung,pin-function", &
function);
621 grp->
func =
function;
631 dev_err(dev,
"failed to alloc memory for func name\n");
639 dev_err(dev,
"failed to alloc memory for group list "
661 struct pinctrl_desc *ctrldesc = &drvdata->
pctl;
662 struct pinctrl_pin_desc *pindesc, *pdesc;
667 ctrldesc->name =
"samsung-pinctrl";
669 ctrldesc->pctlops = &samsung_pctrl_ops;
670 ctrldesc->pmxops = &samsung_pinmux_ops;
671 ctrldesc->confops = &samsung_pinconf_ops;
676 dev_err(&pdev->
dev,
"mem alloc for pin descriptors failed\n");
679 ctrldesc->pins = pindesc;
680 ctrldesc->npins = drvdata->
ctrl->nr_pins;
681 ctrldesc->npins = drvdata->
ctrl->nr_pins;
684 for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
685 pdesc->number = pin + drvdata->
ctrl->base;
694 dev_err(&pdev->
dev,
"mem alloc for pin names failed\n");
699 for (bank = 0; bank < drvdata->
ctrl->nr_banks; bank++) {
700 pin_bank = &drvdata->
ctrl->pin_banks[bank];
701 for (pin = 0; pin < pin_bank->
nr_pins; pin++) {
704 pdesc->name = pin_names;
711 dev_err(&pdev->
dev,
"could not register pinctrl driver\n");
715 drvdata->
grange.name =
"samsung-pctrl-gpio-range";
718 drvdata->
grange.npins = drvdata->
ctrl->nr_pins;
722 ret = samsung_pinctrl_parse_dt(pdev, drvdata);
735 struct gpio_chip *gc;
740 dev_err(&pdev->
dev,
"mem alloc for gpio_chip failed\n");
745 gc->base = drvdata->
ctrl->base;
746 gc->ngpio = drvdata->
ctrl->nr_pins;
747 gc->dev = &pdev->
dev;
748 gc->set = samsung_gpio_set;
749 gc->get = samsung_gpio_get;
750 gc->direction_input = samsung_gpio_direction_input;
751 gc->direction_output = samsung_gpio_direction_output;
752 gc->label = drvdata->
ctrl->label;
756 dev_err(&pdev->
dev,
"failed to register gpio_chip %s, error "
757 "code: %d\n", gc->label, ret);
770 dev_err(&pdev->
dev,
"gpio chip remove failed\n");
776 static const struct of_device_id samsung_pinctrl_dt_match[];
788 dev_err(&pdev->
dev,
"failed to get alias id\n");
804 dev_err(dev,
"device tree node not found\n");
808 ctrl = samsung_pinctrl_get_soc_data(pdev);
810 dev_err(&pdev->
dev,
"driver data not available\n");
816 dev_err(dev,
"failed to allocate memory for driver's "
825 dev_err(dev,
"cannot find IO resource\n");
831 dev_err(dev,
"ioremap failed\n");
839 ret = samsung_gpiolib_register(pdev, drvdata);
843 ret = samsung_pinctrl_register(pdev, drvdata);
845 samsung_gpiolib_unregister(pdev, drvdata);
854 platform_set_drvdata(pdev, drvdata);
858 static const struct of_device_id samsung_pinctrl_dt_match[] = {
859 { .compatible =
"samsung,pinctrl-exynos4210",
866 .probe = samsung_pinctrl_probe,
868 .name =
"samsung-pinctrl",
870 .of_match_table =
of_match_ptr(samsung_pinctrl_dt_match),
874 static int __init samsung_pinctrl_drv_register(
void)
880 static void __exit samsung_pinctrl_drv_unregister(
void)