21 #include <linux/bitmap.h>
24 #include <linux/device.h>
32 #include <linux/module.h>
43 #include <linux/slab.h>
45 #include <linux/types.h>
47 #define MODULE_NAME "pinctrl-bcm2835"
48 #define BCM2835_NUM_GPIOS 54
49 #define BCM2835_NUM_BANKS 2
51 #define BCM2835_PIN_BITMAP_SZ \
52 DIV_ROUND_UP(BCM2835_NUM_GPIOS, sizeof(unsigned long) * 8)
67 #define GPPUDCLK0 0x98
69 #define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
70 #define FSEL_SHIFT(p) (((p) % 10) * 3)
71 #define GPIO_REG_OFFSET(p) ((p) / 32)
72 #define GPIO_REG_SHIFT(p) ((p) % 32)
85 #define BCM2835_PINCONF_PACK(_param_, _arg_) ((_param_) << 16 | (_arg_))
86 #define BCM2835_PINCONF_UNPACK_PARAM(_conf_) ((_conf_) >> 16)
87 #define BCM2835_PINCONF_UNPACK_ARG(_conf_) ((_conf_) & 0xffff)
115 #define BCM2835_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
174 static const char *
const bcm2835_gpio_groups[] = {
255 static const char *
const irq_type_names[] = {
284 unsigned reg,
unsigned bit)
290 static inline enum bcm2835_fsel bcm2835_pinctrl_fsel_get(
296 dev_dbg(pc->
dev,
"get %08x (%u => %s)\n", val, pin,
297 bcm2835_functions[status]);
302 static inline void bcm2835_pinctrl_fsel_set(
309 dev_dbg(pc->
dev,
"read %08x (%u => %s)\n", val, pin,
310 bcm2835_functions[cur]);
320 dev_dbg(pc->
dev,
"trans %08x (%u <= %s)\n", val, pin,
322 bcm2835_gpio_wr(pc,
FSEL_REG(pin), val);
328 dev_dbg(pc->
dev,
"write %08x (%u <= %s)\n", val, pin,
329 bcm2835_functions[fsel]);
330 bcm2835_gpio_wr(pc,
FSEL_REG(pin), val);
333 static int bcm2835_gpio_request(
struct gpio_chip *
chip,
unsigned offset)
338 static void bcm2835_gpio_free(
struct gpio_chip *
chip,
unsigned offset)
343 static int bcm2835_gpio_direction_input(
struct gpio_chip *
chip,
unsigned offset)
348 static int bcm2835_gpio_get(
struct gpio_chip *
chip,
unsigned offset)
352 return bcm2835_gpio_get_bit(pc,
GPLEV0, offset);
355 static int bcm2835_gpio_direction_output(
struct gpio_chip *
chip,
365 bcm2835_gpio_set_bit(pc, value ?
GPSET0 :
GPCLR0, offset);
378 .request = bcm2835_gpio_request,
379 .free = bcm2835_gpio_free,
380 .direction_input = bcm2835_gpio_direction_input,
381 .direction_output = bcm2835_gpio_direction_output,
382 .get = bcm2835_gpio_get,
383 .set = bcm2835_gpio_set,
384 .to_irq = bcm2835_gpio_to_irq,
394 int bank = irqdata->
bank;
400 events = bcm2835_gpio_rd(pc,
GPEDS0 + bank * 4);
403 gpio = (32 * bank) + offset;
408 bcm2835_gpio_set_bit(pc,
GPEDS0, gpio);
413 if (type & IRQ_TYPE_LEVEL_MASK)
414 bcm2835_gpio_set_bit(pc,
GPEDS0, gpio);
419 static inline void __bcm2835_gpio_irq_config(
struct bcm2835_pinctrl *pc,
420 unsigned reg,
unsigned offset,
bool enable)
424 value = bcm2835_gpio_rd(pc, reg);
429 bcm2835_gpio_wr(pc, reg, value);
434 unsigned offset,
bool enable)
438 __bcm2835_gpio_irq_config(pc,
GPREN0, offset, enable);
442 __bcm2835_gpio_irq_config(pc,
GPFEN0, offset, enable);
446 __bcm2835_gpio_irq_config(pc,
GPREN0, offset, enable);
447 __bcm2835_gpio_irq_config(pc,
GPFEN0, offset, enable);
451 __bcm2835_gpio_irq_config(pc,
GPHEN0, offset, enable);
455 __bcm2835_gpio_irq_config(pc,
GPLEN0, offset, enable);
460 static void bcm2835_gpio_irq_enable(
struct irq_data *
data)
463 unsigned gpio = irqd_to_hwirq(data);
470 bcm2835_gpio_irq_config(pc, gpio,
true);
471 spin_unlock_irqrestore(&pc->
irq_lock[bank], flags);
474 static void bcm2835_gpio_irq_disable(
struct irq_data *
data)
477 unsigned gpio = irqd_to_hwirq(data);
483 bcm2835_gpio_irq_config(pc, gpio,
false);
485 spin_unlock_irqrestore(&pc->
irq_lock[bank], flags);
488 static int __bcm2835_gpio_irq_set_type_disabled(
struct bcm2835_pinctrl *pc,
489 unsigned offset,
unsigned int type)
508 static int __bcm2835_gpio_irq_set_type_enabled(
struct bcm2835_pinctrl *pc,
509 unsigned offset,
unsigned int type)
514 bcm2835_gpio_irq_config(pc, offset,
false);
523 bcm2835_gpio_irq_config(pc, offset,
false);
525 }
else if (pc->
irq_type[offset] != type) {
526 bcm2835_gpio_irq_config(pc, offset,
false);
528 bcm2835_gpio_irq_config(pc, offset,
true);
536 bcm2835_gpio_irq_config(pc, offset,
false);
538 }
else if (pc->
irq_type[offset] != type) {
539 bcm2835_gpio_irq_config(pc, offset,
false);
541 bcm2835_gpio_irq_config(pc, offset,
true);
549 bcm2835_gpio_irq_config(pc, offset,
true);
554 bcm2835_gpio_irq_config(pc, offset,
true);
556 }
else if (pc->
irq_type[offset] != type) {
557 bcm2835_gpio_irq_config(pc, offset,
false);
559 bcm2835_gpio_irq_config(pc, offset,
true);
566 bcm2835_gpio_irq_config(pc, offset,
false);
568 bcm2835_gpio_irq_config(pc, offset,
true);
578 static int bcm2835_gpio_irq_set_type(
struct irq_data *
data,
unsigned int type)
581 unsigned gpio = irqd_to_hwirq(data);
590 ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
592 ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
594 spin_unlock_irqrestore(&pc->
irq_lock[bank], flags);
599 static struct irq_chip bcm2835_gpio_irq_chip = {
601 .irq_enable = bcm2835_gpio_irq_enable,
602 .irq_disable = bcm2835_gpio_irq_disable,
603 .irq_set_type = bcm2835_gpio_irq_set_type,
606 static int bcm2835_pctl_get_groups_count(
struct pinctrl_dev *pctldev)
611 static const char *bcm2835_pctl_get_group_name(
struct pinctrl_dev *pctldev,
614 return bcm2835_gpio_groups[selector];
617 static int bcm2835_pctl_get_group_pins(
struct pinctrl_dev *pctldev,
619 const unsigned **
pins,
628 static void bcm2835_pctl_pin_dbg_show(
struct pinctrl_dev *pctldev,
633 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
634 const char *
fname = bcm2835_functions[
fsel];
635 int value = bcm2835_gpio_get_bit(pc,
GPLEV0, offset);
638 seq_printf(s,
"function %s in %s; irq %d (%s)",
639 fname, value ?
"hi" :
"lo",
640 irq, irq_type_names[pc->
irq_type[offset]]);
643 static void bcm2835_pctl_dt_free_map(
struct pinctrl_dev *pctldev,
648 for (i = 0; i < num_maps; i++)
650 kfree(maps[i].data.configs.configs);
655 static int bcm2835_pctl_dt_node_to_map_func(
struct bcm2835_pinctrl *pc,
662 dev_err(pc->
dev,
"%s: invalid brcm,function %d\n",
663 of_node_full_name(np), fnum);
668 map->
data.
mux.group = bcm2835_gpio_groups[
pin];
669 map->
data.
mux.function = bcm2835_functions[fnum];
675 static int bcm2835_pctl_dt_node_to_map_pull(
struct bcm2835_pinctrl *pc,
683 dev_err(pc->
dev,
"%s: invalid brcm,pull %d\n",
684 of_node_full_name(np), pull);
688 configs = kzalloc(
sizeof(*configs),
GFP_KERNEL);
707 static int bcm2835_pctl_dt_node_to_map(
struct pinctrl_dev *pctldev,
713 int num_pins,
num_funcs, num_pulls, maps_per_pin;
720 dev_err(pc->
dev,
"%s: missing brcm,pins property\n",
721 of_node_full_name(np));
728 if (!funcs && !pulls) {
730 "%s: neither brcm,function nor brcm,pull specified\n",
731 of_node_full_name(np));
735 num_pins = pins->
length / 4;
736 num_funcs = funcs ? (funcs->
length / 4) : 0;
737 num_pulls = pulls ? (pulls->
length / 4) : 0;
739 if (num_funcs > 1 && num_funcs != num_pins) {
741 "%s: brcm,function must have 1 or %d entries\n",
742 of_node_full_name(np), num_pins);
746 if (num_pulls > 1 && num_pulls != num_pins) {
748 "%s: brcm,pull must have 1 or %d entries\n",
749 of_node_full_name(np), num_pins);
758 cur_map = maps = kzalloc(num_pins * maps_per_pin *
sizeof(*maps),
763 for (i = 0; i < num_pins; i++) {
764 pin = prop_u32(pins, i);
766 dev_err(pc->
dev,
"%s: invalid brcm,pins value %d\n",
767 of_node_full_name(np), pin);
773 func = prop_u32(funcs, (num_funcs > 1) ? i : 0);
774 err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
780 pull = prop_u32(pulls, (num_pulls > 1) ? i : 0);
781 err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
789 *num_maps = num_pins * maps_per_pin;
798 static struct pinctrl_ops bcm2835_pctl_ops = {
799 .get_groups_count = bcm2835_pctl_get_groups_count,
800 .get_group_name = bcm2835_pctl_get_group_name,
801 .get_group_pins = bcm2835_pctl_get_group_pins,
802 .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
803 .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
804 .dt_free_map = bcm2835_pctl_dt_free_map,
807 static int bcm2835_pmx_get_functions_count(
struct pinctrl_dev *pctldev)
812 static const char *bcm2835_pmx_get_function_name(
struct pinctrl_dev *pctldev,
815 return bcm2835_functions[selector];
818 static int bcm2835_pmx_get_function_groups(
struct pinctrl_dev *pctldev,
820 const char *
const **groups,
821 unsigned *
const num_groups)
824 *groups = bcm2835_gpio_groups;
825 *num_groups =
ARRAY_SIZE(bcm2835_gpio_groups);
830 static int bcm2835_pmx_enable(
struct pinctrl_dev *pctldev,
831 unsigned func_selector,
832 unsigned group_selector)
836 bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
841 static void bcm2835_pmx_disable(
struct pinctrl_dev *pctldev,
842 unsigned func_selector,
843 unsigned group_selector)
851 static void bcm2835_pmx_gpio_disable_free(
struct pinctrl_dev *pctldev,
852 struct pinctrl_gpio_range *
range,
861 static int bcm2835_pmx_gpio_set_direction(
struct pinctrl_dev *pctldev,
862 struct pinctrl_gpio_range *
range,
870 bcm2835_pinctrl_fsel_set(pc, offset, fsel);
875 static struct pinmux_ops bcm2835_pmx_ops = {
876 .get_functions_count = bcm2835_pmx_get_functions_count,
877 .get_function_name = bcm2835_pmx_get_function_name,
878 .get_function_groups = bcm2835_pmx_get_function_groups,
879 .enable = bcm2835_pmx_enable,
880 .disable = bcm2835_pmx_disable,
881 .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
882 .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
885 static int bcm2835_pinconf_get(
struct pinctrl_dev *pctldev,
886 unsigned pin,
unsigned long *
config)
892 static int bcm2835_pinconf_set(
struct pinctrl_dev *pctldev,
893 unsigned pin,
unsigned long config)
906 bcm2835_gpio_wr(pc,
GPPUD, arg & 3);
914 bcm2835_gpio_wr(pc,
GPPUDCLK0 + (off * 4), 0);
920 .pin_config_get = bcm2835_pinconf_get,
921 .pin_config_set = bcm2835_pinconf_set,
924 static struct pinctrl_desc bcm2835_pinctrl_desc = {
928 .pctlops = &bcm2835_pctl_ops,
929 .pmxops = &bcm2835_pmx_ops,
934 static struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range __devinitconst = {
953 platform_set_drvdata(pdev, pc);
958 dev_err(dev,
"could not get IO memory\n");
973 dev_err(dev,
"could not create IRQ domain\n");
979 irq_set_lockdep_class(irq, &gpio_lock_class);
980 irq_set_chip_and_handler(irq, &bcm2835_gpio_irq_chip,
993 bcm2835_gpio_wr(pc,
GPREN0 + i * 4, 0);
994 bcm2835_gpio_wr(pc,
GPFEN0 + i * 4, 0);
995 bcm2835_gpio_wr(pc,
GPHEN0 + i * 4, 0);
996 bcm2835_gpio_wr(pc,
GPLEN0 + i * 4, 0);
997 bcm2835_gpio_wr(pc,
GPAREN0 + i * 4, 0);
998 bcm2835_gpio_wr(pc,
GPAFEN0 + i * 4, 0);
1001 events = bcm2835_gpio_rd(pc,
GPEDS0 + i * 4);
1003 bcm2835_gpio_wr(pc,
GPEDS0 + i * 4,
BIT(offset));
1010 len =
strlen(dev_name(pc->dev)) + 16;
1014 snprintf(name, len, "%
s:bank%
d", dev_name(pc->dev), i);
1016 err = devm_request_irq(dev, pc->irq[i],
1020 dev_err(dev,
"unable to request IRQ %d\n", pc->irq[i]);
1027 dev_err(dev,
"could not add GPIO chip\n");
1055 static struct of_device_id bcm2835_pinctrl_match[] __devinitconst = {
1062 .probe = bcm2835_pinctrl_probe,
1063 .remove = bcm2835_pinctrl_remove,
1067 .of_match_table = bcm2835_pinctrl_match,