1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2013
5 * Author: Patrice Chotard <patrice.chotard@st.com>
7 * Driver allows to use AxB5xx unused pins to be used as GPIO
9 #include <linux/bitops.h>
10 #include <linux/err.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/irqdomain.h>
16 #include <linux/kernel.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/seq_file.h>
21 #include <linux/slab.h>
22 #include <linux/types.h>
24 #include <linux/mfd/abx500.h>
25 #include <linux/mfd/abx500/ab8500.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/pinctrl/machine.h>
29 #include <linux/pinctrl/pinconf-generic.h>
30 #include <linux/pinctrl/pinconf.h>
31 #include <linux/pinctrl/pinctrl.h>
32 #include <linux/pinctrl/pinmux.h>
35 #include "../pinconf.h"
36 #include "../pinctrl-utils.h"
38 #include "pinctrl-abx500.h"
41 * GPIO registers offset
44 #define AB8500_GPIO_SEL1_REG 0x00
45 #define AB8500_GPIO_SEL2_REG 0x01
46 #define AB8500_GPIO_SEL3_REG 0x02
47 #define AB8500_GPIO_SEL4_REG 0x03
48 #define AB8500_GPIO_SEL5_REG 0x04
49 #define AB8500_GPIO_SEL6_REG 0x05
51 #define AB8500_GPIO_DIR1_REG 0x10
52 #define AB8500_GPIO_DIR2_REG 0x11
53 #define AB8500_GPIO_DIR3_REG 0x12
54 #define AB8500_GPIO_DIR4_REG 0x13
55 #define AB8500_GPIO_DIR5_REG 0x14
56 #define AB8500_GPIO_DIR6_REG 0x15
58 #define AB8500_GPIO_OUT1_REG 0x20
59 #define AB8500_GPIO_OUT2_REG 0x21
60 #define AB8500_GPIO_OUT3_REG 0x22
61 #define AB8500_GPIO_OUT4_REG 0x23
62 #define AB8500_GPIO_OUT5_REG 0x24
63 #define AB8500_GPIO_OUT6_REG 0x25
65 #define AB8500_GPIO_PUD1_REG 0x30
66 #define AB8500_GPIO_PUD2_REG 0x31
67 #define AB8500_GPIO_PUD3_REG 0x32
68 #define AB8500_GPIO_PUD4_REG 0x33
69 #define AB8500_GPIO_PUD5_REG 0x34
70 #define AB8500_GPIO_PUD6_REG 0x35
72 #define AB8500_GPIO_IN1_REG 0x40
73 #define AB8500_GPIO_IN2_REG 0x41
74 #define AB8500_GPIO_IN3_REG 0x42
75 #define AB8500_GPIO_IN4_REG 0x43
76 #define AB8500_GPIO_IN5_REG 0x44
77 #define AB8500_GPIO_IN6_REG 0x45
78 #define AB8500_GPIO_ALTFUN_REG 0x50
80 #define ABX500_GPIO_INPUT 0
81 #define ABX500_GPIO_OUTPUT 1
83 struct abx500_pinctrl {
85 struct pinctrl_dev *pctldev;
86 struct abx500_pinctrl_soc_data *soc;
87 struct gpio_chip chip;
88 struct ab8500 *parent;
89 struct abx500_gpio_irq_cluster *irq_cluster;
93 static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
94 unsigned offset, bool *bit)
96 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
102 ret = abx500_get_register_interruptible(pct->dev,
103 AB8500_MISC, reg, &val);
106 "%s read reg =%x, offset=%x failed (%d)\n",
107 __func__, reg, offset, ret);
111 *bit = !!(val & BIT(pos));
116 static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
117 unsigned offset, int val)
119 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
124 ret = abx500_mask_and_set_register_interruptible(pct->dev,
125 AB8500_MISC, reg, BIT(pos), val << pos);
127 dev_err(pct->dev, "%s write reg, %x offset %x failed (%d)\n",
128 __func__, reg, offset, ret);
134 * abx500_gpio_get() - Get the particular GPIO value
136 * @offset: GPIO number to read
138 static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
140 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
143 u8 gpio_offset = offset - 1;
146 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
147 gpio_offset, &is_out);
152 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_OUT1_REG,
155 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
159 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
166 static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
168 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
171 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
173 dev_err(pct->dev, "%s write failed (%d)\n", __func__, ret);
176 static int abx500_gpio_direction_output(struct gpio_chip *chip,
180 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
183 /* set direction as output */
184 ret = abx500_gpio_set_bits(chip,
185 AB8500_GPIO_DIR1_REG,
191 /* disable pull down */
192 ret = abx500_gpio_set_bits(chip,
193 AB8500_GPIO_PUD1_REG,
195 ABX500_GPIO_PULL_NONE);
199 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
203 /* set the output as 1 or 0 */
204 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
207 static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
209 /* set the register as input */
210 return abx500_gpio_set_bits(chip,
211 AB8500_GPIO_DIR1_REG,
216 static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
218 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
219 /* The AB8500 GPIO numbers are off by one */
220 int gpio = offset + 1;
224 for (i = 0; i < pct->irq_cluster_size; i++) {
225 struct abx500_gpio_irq_cluster *cluster =
226 &pct->irq_cluster[i];
228 if (gpio >= cluster->start && gpio <= cluster->end) {
230 * The ABx500 GPIO's associated IRQs are clustered together
231 * throughout the interrupt numbers at irregular intervals.
232 * To solve this quandry, we have placed the read-in values
233 * into the cluster information table.
235 hwirq = gpio - cluster->start + cluster->to_irq;
236 return irq_create_mapping(pct->parent->domain, hwirq);
243 static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
244 unsigned gpio, int alt_setting)
246 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
247 struct alternate_functions af = pct->soc->alternate_functions[gpio];
252 const char *modes[] = {
253 [ABX500_DEFAULT] = "default",
254 [ABX500_ALT_A] = "altA",
255 [ABX500_ALT_B] = "altB",
256 [ABX500_ALT_C] = "altC",
260 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
261 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
262 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
263 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
268 /* on ABx5xx, there is no GPIO0, so adjust the offset */
271 switch (alt_setting) {
274 * for ABx5xx family, default mode is always selected by
275 * writing 0 to GPIOSELx register, except for pins which
276 * support at least ALT_B mode, default mode is selected
277 * by writing 1 to GPIOSELx register
280 if (af.alt_bit1 != UNUSED)
283 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
289 * for ABx5xx family, alt_a mode is always selected by
290 * writing 1 to GPIOSELx register, except for pins which
291 * support at least ALT_B mode, alt_a mode is selected
292 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
295 if (af.alt_bit1 != UNUSED) {
296 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
301 ret = abx500_gpio_set_bits(chip,
302 AB8500_GPIO_ALTFUN_REG,
304 !!(af.alta_val & BIT(0)));
308 if (af.alt_bit2 != UNUSED)
309 ret = abx500_gpio_set_bits(chip,
310 AB8500_GPIO_ALTFUN_REG,
312 !!(af.alta_val & BIT(1)));
314 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
319 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
324 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
325 af.alt_bit1, !!(af.altb_val & BIT(0)));
329 if (af.alt_bit2 != UNUSED)
330 ret = abx500_gpio_set_bits(chip,
331 AB8500_GPIO_ALTFUN_REG,
333 !!(af.altb_val & BIT(1)));
337 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
342 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
343 af.alt_bit2, !!(af.altc_val & BIT(0)));
347 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
348 af.alt_bit2, !!(af.altc_val & BIT(1)));
352 dev_dbg(pct->dev, "unknown alt_setting %d\n", alt_setting);
358 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
363 #ifdef CONFIG_DEBUG_FS
364 static int abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
371 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
372 struct alternate_functions af = pct->soc->alternate_functions[gpio];
373 /* on ABx5xx, there is no GPIO0, so adjust the offset */
374 unsigned offset = gpio - 1;
378 * if gpiosel_bit is set to unused,
379 * it means no GPIO or special case
381 if (af.gpiosel_bit == UNUSED)
382 return ABX500_DEFAULT;
384 /* read GpioSelx register */
385 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
386 af.gpiosel_bit, &bit_mode);
393 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
394 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
396 "alt_bitX value not in correct range (-1 to 7)\n");
400 /* if alt_bit2 is used, alt_bit1 must be used too */
401 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
403 "if alt_bit2 is used, alt_bit1 can't be unused\n");
407 /* check if pin use AlternateFunction register */
408 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
411 * if pin GPIOSEL bit is set and pin supports alternate function,
412 * it means DEFAULT mode
415 return ABX500_DEFAULT;
418 * pin use the AlternatFunction register
419 * read alt_bit1 value
421 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
422 af.alt_bit1, &alt_bit1);
426 if (af.alt_bit2 != UNUSED) {
427 /* read alt_bit2 value */
428 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
436 mode = (alt_bit2 << 1) + alt_bit1;
437 if (mode == af.alta_val)
439 else if (mode == af.altb_val)
445 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
449 static void abx500_gpio_dbg_show_one(struct seq_file *s,
450 struct pinctrl_dev *pctldev,
451 struct gpio_chip *chip,
452 unsigned offset, unsigned gpio)
454 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
455 const char *label = gpiochip_is_requested(chip, offset - 1);
456 u8 gpio_offset = offset - 1;
462 const char *modes[] = {
463 [ABX500_DEFAULT] = "default",
464 [ABX500_ALT_A] = "altA",
465 [ABX500_ALT_B] = "altB",
466 [ABX500_ALT_C] = "altC",
469 const char *pull_up_down[] = {
470 [ABX500_GPIO_PULL_DOWN] = "pull down",
471 [ABX500_GPIO_PULL_NONE] = "pull none",
472 [ABX500_GPIO_PULL_NONE + 1] = "pull none",
473 [ABX500_GPIO_PULL_UP] = "pull up",
476 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
477 gpio_offset, &is_out);
481 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
482 gpio, label ?: "(none)",
483 is_out ? "out" : "in ");
486 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
491 seq_printf(s, " %-9s", pull_up_down[pd]);
493 seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
495 mode = abx500_get_mode(pctldev, chip, offset);
497 seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
501 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
504 static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
507 unsigned gpio = chip->base;
508 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
509 struct pinctrl_dev *pctldev = pct->pctldev;
511 for (i = 0; i < chip->ngpio; i++, gpio++) {
512 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
513 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
519 static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
520 struct pinctrl_dev *pctldev,
521 struct gpio_chip *chip,
522 unsigned offset, unsigned gpio)
525 #define abx500_gpio_dbg_show NULL
528 static const struct gpio_chip abx500gpio_chip = {
529 .label = "abx500-gpio",
530 .owner = THIS_MODULE,
531 .request = gpiochip_generic_request,
532 .free = gpiochip_generic_free,
533 .direction_input = abx500_gpio_direction_input,
534 .get = abx500_gpio_get,
535 .direction_output = abx500_gpio_direction_output,
536 .set = abx500_gpio_set,
537 .to_irq = abx500_gpio_to_irq,
538 .dbg_show = abx500_gpio_dbg_show,
541 static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
543 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
545 return pct->soc->nfunctions;
548 static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
551 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
553 return pct->soc->functions[function].name;
556 static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
558 const char * const **groups,
559 unsigned * const num_groups)
561 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
563 *groups = pct->soc->functions[function].groups;
564 *num_groups = pct->soc->functions[function].ngroups;
569 static int abx500_pmx_set(struct pinctrl_dev *pctldev, unsigned function,
572 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
573 struct gpio_chip *chip = &pct->chip;
574 const struct abx500_pingroup *g;
578 g = &pct->soc->groups[group];
579 if (g->altsetting < 0)
582 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
584 for (i = 0; i < g->npins; i++) {
585 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
586 g->pins[i], g->altsetting);
588 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
592 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
597 static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
598 struct pinctrl_gpio_range *range,
601 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
602 const struct abx500_pinrange *p;
607 * Different ranges have different ways to enable GPIO function on a
608 * pin, so refer back to our local range type, where we handily define
609 * what altfunc enables GPIO for a certain pin.
611 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
612 p = &pct->soc->gpio_ranges[i];
613 if ((offset >= p->offset) &&
614 (offset < (p->offset + p->npins)))
618 if (i == pct->soc->gpio_num_ranges) {
619 dev_err(pct->dev, "%s failed to locate range\n", __func__);
623 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
626 ret = abx500_set_mode(pct->pctldev, &pct->chip,
629 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
634 static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
635 struct pinctrl_gpio_range *range,
640 static const struct pinmux_ops abx500_pinmux_ops = {
641 .get_functions_count = abx500_pmx_get_funcs_cnt,
642 .get_function_name = abx500_pmx_get_func_name,
643 .get_function_groups = abx500_pmx_get_func_groups,
644 .set_mux = abx500_pmx_set,
645 .gpio_request_enable = abx500_gpio_request_enable,
646 .gpio_disable_free = abx500_gpio_disable_free,
649 static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
651 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
653 return pct->soc->ngroups;
656 static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
659 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
661 return pct->soc->groups[selector].name;
664 static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
666 const unsigned **pins,
669 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
671 *pins = pct->soc->groups[selector].pins;
672 *num_pins = pct->soc->groups[selector].npins;
677 static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
678 struct seq_file *s, unsigned offset)
680 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
681 struct gpio_chip *chip = &pct->chip;
683 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
684 chip->base + offset - 1);
687 static int abx500_dt_add_map_mux(struct pinctrl_map **map,
688 unsigned *reserved_maps,
689 unsigned *num_maps, const char *group,
690 const char *function)
692 if (*num_maps == *reserved_maps)
695 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
696 (*map)[*num_maps].data.mux.group = group;
697 (*map)[*num_maps].data.mux.function = function;
703 static int abx500_dt_add_map_configs(struct pinctrl_map **map,
704 unsigned *reserved_maps,
705 unsigned *num_maps, const char *group,
706 unsigned long *configs, unsigned num_configs)
708 unsigned long *dup_configs;
710 if (*num_maps == *reserved_maps)
713 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
718 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
720 (*map)[*num_maps].data.configs.group_or_pin = group;
721 (*map)[*num_maps].data.configs.configs = dup_configs;
722 (*map)[*num_maps].data.configs.num_configs = num_configs;
728 static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
729 const char *pin_name)
732 struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
734 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
735 for (i = 0; i < npct->soc->npins; i++)
736 if (npct->soc->pins[i].number == pin_number)
737 return npct->soc->pins[i].name;
741 static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
742 struct device_node *np,
743 struct pinctrl_map **map,
744 unsigned *reserved_maps,
748 const char *function = NULL;
749 unsigned long *configs;
750 unsigned int nconfigs = 0;
751 struct property *prop;
753 ret = of_property_read_string(np, "function", &function);
757 ret = of_property_count_strings(np, "groups");
761 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
766 of_property_for_each_string(np, "groups", prop, group) {
767 ret = abx500_dt_add_map_mux(map, reserved_maps,
768 num_maps, group, function);
774 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &nconfigs);
776 const char *gpio_name;
779 ret = of_property_count_strings(np, "pins");
783 ret = pinctrl_utils_reserve_map(pctldev, map,
789 of_property_for_each_string(np, "pins", prop, pin) {
790 gpio_name = abx500_find_pin_name(pctldev, pin);
792 ret = abx500_dt_add_map_configs(map, reserved_maps,
793 num_maps, gpio_name, configs, 1);
803 static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
804 struct device_node *np_config,
805 struct pinctrl_map **map, unsigned *num_maps)
807 unsigned reserved_maps;
808 struct device_node *np;
815 for_each_child_of_node(np_config, np) {
816 ret = abx500_dt_subnode_to_map(pctldev, np, map,
817 &reserved_maps, num_maps);
819 pinctrl_utils_free_map(pctldev, *map, *num_maps);
828 static const struct pinctrl_ops abx500_pinctrl_ops = {
829 .get_groups_count = abx500_get_groups_cnt,
830 .get_group_name = abx500_get_group_name,
831 .get_group_pins = abx500_get_group_pins,
832 .pin_dbg_show = abx500_pin_dbg_show,
833 .dt_node_to_map = abx500_dt_node_to_map,
834 .dt_free_map = pinctrl_utils_free_map,
837 static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
839 unsigned long *config)
844 static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
846 unsigned long *configs,
847 unsigned num_configs)
849 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
850 struct gpio_chip *chip = &pct->chip;
854 enum pin_config_param param;
855 enum pin_config_param argument;
857 for (i = 0; i < num_configs; i++) {
858 param = pinconf_to_config_param(configs[i]);
859 argument = pinconf_to_config_argument(configs[i]);
861 dev_dbg(chip->parent, "pin %d [%#lx]: %s %s\n",
863 (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
864 (param == PIN_CONFIG_OUTPUT) ?
865 (argument ? "high" : "low") :
866 (argument ? "pull up" : "pull down"));
868 /* on ABx500, there is no GPIO0, so adjust the offset */
872 case PIN_CONFIG_BIAS_DISABLE:
873 ret = abx500_gpio_direction_input(chip, offset);
877 /* Chip only supports pull down */
878 ret = abx500_gpio_set_bits(chip,
879 AB8500_GPIO_PUD1_REG, offset,
880 ABX500_GPIO_PULL_NONE);
883 case PIN_CONFIG_BIAS_PULL_DOWN:
884 ret = abx500_gpio_direction_input(chip, offset);
888 * if argument = 1 set the pull down
889 * else clear the pull down
890 * Chip only supports pull down
892 ret = abx500_gpio_set_bits(chip,
893 AB8500_GPIO_PUD1_REG,
895 argument ? ABX500_GPIO_PULL_DOWN :
896 ABX500_GPIO_PULL_NONE);
899 case PIN_CONFIG_BIAS_PULL_UP:
900 ret = abx500_gpio_direction_input(chip, offset);
904 * if argument = 1 set the pull up
905 * else clear the pull up
907 ret = abx500_gpio_direction_input(chip, offset);
910 case PIN_CONFIG_OUTPUT:
911 ret = abx500_gpio_direction_output(chip, offset,
916 dev_err(chip->parent,
917 "illegal configuration requested\n");
919 } /* for each config */
922 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
927 static const struct pinconf_ops abx500_pinconf_ops = {
928 .pin_config_get = abx500_pin_config_get,
929 .pin_config_set = abx500_pin_config_set,
933 static struct pinctrl_desc abx500_pinctrl_desc = {
934 .name = "pinctrl-abx500",
935 .pctlops = &abx500_pinctrl_ops,
936 .pmxops = &abx500_pinmux_ops,
937 .confops = &abx500_pinconf_ops,
938 .owner = THIS_MODULE,
941 static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
943 unsigned int lowest = 0;
944 unsigned int highest = 0;
945 unsigned int npins = 0;
949 * Compute number of GPIOs from the last SoC gpio range descriptors
950 * These ranges may include "holes" but the GPIO number space shall
951 * still be homogeneous, so we need to detect and account for any
952 * such holes so that these are included in the number of GPIO pins.
954 for (i = 0; i < soc->gpio_num_ranges; i++) {
957 const struct abx500_pinrange *p;
959 p = &soc->gpio_ranges[i];
961 gend = p->offset + p->npins - 1;
964 /* First iteration, set start values */
974 /* this gives the absolute number of pins */
975 npins = highest - lowest + 1;
979 static const struct of_device_id abx500_gpio_match[] = {
980 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
981 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
985 static int abx500_gpio_probe(struct platform_device *pdev)
987 struct device_node *np = pdev->dev.of_node;
988 const struct of_device_id *match;
989 struct abx500_pinctrl *pct;
990 unsigned int id = -1;
995 dev_err(&pdev->dev, "gpio dt node missing\n");
999 pct = devm_kzalloc(&pdev->dev, sizeof(*pct), GFP_KERNEL);
1003 pct->dev = &pdev->dev;
1004 pct->parent = dev_get_drvdata(pdev->dev.parent);
1005 pct->chip = abx500gpio_chip;
1006 pct->chip.parent = &pdev->dev;
1007 pct->chip.base = -1; /* Dynamic allocation */
1009 match = of_match_device(abx500_gpio_match, &pdev->dev);
1011 dev_err(&pdev->dev, "gpio dt not matching\n");
1014 id = (unsigned long)match->data;
1016 /* Poke in other ASIC variants here */
1018 case PINCTRL_AB8500:
1019 abx500_pinctrl_ab8500_init(&pct->soc);
1021 case PINCTRL_AB8505:
1022 abx500_pinctrl_ab8505_init(&pct->soc);
1025 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
1030 dev_err(&pdev->dev, "Invalid SOC data\n");
1034 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1035 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1036 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
1038 ret = gpiochip_add_data(&pct->chip, pct);
1040 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
1043 dev_info(&pdev->dev, "added gpiochip\n");
1045 abx500_pinctrl_desc.pins = pct->soc->pins;
1046 abx500_pinctrl_desc.npins = pct->soc->npins;
1047 pct->pctldev = devm_pinctrl_register(&pdev->dev, &abx500_pinctrl_desc,
1049 if (IS_ERR(pct->pctldev)) {
1051 "could not register abx500 pinctrl driver\n");
1052 ret = PTR_ERR(pct->pctldev);
1055 dev_info(&pdev->dev, "registered pin controller\n");
1057 /* We will handle a range of GPIO pins */
1058 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1059 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1061 ret = gpiochip_add_pin_range(&pct->chip,
1062 dev_name(&pdev->dev),
1063 p->offset - 1, p->offset, p->npins);
1068 platform_set_drvdata(pdev, pct);
1069 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1074 gpiochip_remove(&pct->chip);
1079 * abx500_gpio_remove() - remove Ab8500-gpio driver
1080 * @pdev: Platform device registered
1082 static int abx500_gpio_remove(struct platform_device *pdev)
1084 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
1086 gpiochip_remove(&pct->chip);
1090 static struct platform_driver abx500_gpio_driver = {
1092 .name = "abx500-gpio",
1093 .of_match_table = abx500_gpio_match,
1095 .probe = abx500_gpio_probe,
1096 .remove = abx500_gpio_remove,
1099 static int __init abx500_gpio_init(void)
1101 return platform_driver_register(&abx500_gpio_driver);
1103 core_initcall(abx500_gpio_init);