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/kernel.h>
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/interrupt.h>
21 #include <linux/bitops.h>
22 #include <linux/mfd/abx500.h>
23 #include <linux/mfd/abx500/ab8500.h>
24 #include <linux/pinctrl/pinctrl.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/pinctrl/pinmux.h>
27 #include <linux/pinctrl/pinconf.h>
28 #include <linux/pinctrl/pinconf-generic.h>
29 #include <linux/pinctrl/machine.h>
31 #include "pinctrl-abx500.h"
33 #include "../pinconf.h"
34 #include "../pinctrl-utils.h"
37 * GPIO registers offset
40 #define AB8500_GPIO_SEL1_REG 0x00
41 #define AB8500_GPIO_SEL2_REG 0x01
42 #define AB8500_GPIO_SEL3_REG 0x02
43 #define AB8500_GPIO_SEL4_REG 0x03
44 #define AB8500_GPIO_SEL5_REG 0x04
45 #define AB8500_GPIO_SEL6_REG 0x05
47 #define AB8500_GPIO_DIR1_REG 0x10
48 #define AB8500_GPIO_DIR2_REG 0x11
49 #define AB8500_GPIO_DIR3_REG 0x12
50 #define AB8500_GPIO_DIR4_REG 0x13
51 #define AB8500_GPIO_DIR5_REG 0x14
52 #define AB8500_GPIO_DIR6_REG 0x15
54 #define AB8500_GPIO_OUT1_REG 0x20
55 #define AB8500_GPIO_OUT2_REG 0x21
56 #define AB8500_GPIO_OUT3_REG 0x22
57 #define AB8500_GPIO_OUT4_REG 0x23
58 #define AB8500_GPIO_OUT5_REG 0x24
59 #define AB8500_GPIO_OUT6_REG 0x25
61 #define AB8500_GPIO_PUD1_REG 0x30
62 #define AB8500_GPIO_PUD2_REG 0x31
63 #define AB8500_GPIO_PUD3_REG 0x32
64 #define AB8500_GPIO_PUD4_REG 0x33
65 #define AB8500_GPIO_PUD5_REG 0x34
66 #define AB8500_GPIO_PUD6_REG 0x35
68 #define AB8500_GPIO_IN1_REG 0x40
69 #define AB8500_GPIO_IN2_REG 0x41
70 #define AB8500_GPIO_IN3_REG 0x42
71 #define AB8500_GPIO_IN4_REG 0x43
72 #define AB8500_GPIO_IN5_REG 0x44
73 #define AB8500_GPIO_IN6_REG 0x45
74 #define AB8500_GPIO_ALTFUN_REG 0x50
76 #define ABX500_GPIO_INPUT 0
77 #define ABX500_GPIO_OUTPUT 1
79 struct abx500_pinctrl {
81 struct pinctrl_dev *pctldev;
82 struct abx500_pinctrl_soc_data *soc;
83 struct gpio_chip chip;
84 struct ab8500 *parent;
85 struct abx500_gpio_irq_cluster *irq_cluster;
89 static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
90 unsigned offset, bool *bit)
92 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
98 ret = abx500_get_register_interruptible(pct->dev,
99 AB8500_MISC, reg, &val);
102 "%s read reg =%x, offset=%x failed (%d)\n",
103 __func__, reg, offset, ret);
107 *bit = !!(val & BIT(pos));
112 static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
113 unsigned offset, int val)
115 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
120 ret = abx500_mask_and_set_register_interruptible(pct->dev,
121 AB8500_MISC, reg, BIT(pos), val << pos);
123 dev_err(pct->dev, "%s write reg, %x offset %x failed (%d)\n",
124 __func__, reg, offset, ret);
130 * abx500_gpio_get() - Get the particular GPIO value
132 * @offset: GPIO number to read
134 static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
136 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
139 u8 gpio_offset = offset - 1;
142 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
143 gpio_offset, &is_out);
148 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_OUT1_REG,
151 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
155 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
162 static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
164 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
167 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
169 dev_err(pct->dev, "%s write failed (%d)\n", __func__, ret);
172 static int abx500_gpio_direction_output(struct gpio_chip *chip,
176 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
179 /* set direction as output */
180 ret = abx500_gpio_set_bits(chip,
181 AB8500_GPIO_DIR1_REG,
187 /* disable pull down */
188 ret = abx500_gpio_set_bits(chip,
189 AB8500_GPIO_PUD1_REG,
191 ABX500_GPIO_PULL_NONE);
195 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
199 /* set the output as 1 or 0 */
200 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
203 static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
205 /* set the register as input */
206 return abx500_gpio_set_bits(chip,
207 AB8500_GPIO_DIR1_REG,
212 static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
214 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
215 /* The AB8500 GPIO numbers are off by one */
216 int gpio = offset + 1;
220 for (i = 0; i < pct->irq_cluster_size; i++) {
221 struct abx500_gpio_irq_cluster *cluster =
222 &pct->irq_cluster[i];
224 if (gpio >= cluster->start && gpio <= cluster->end) {
226 * The ABx500 GPIO's associated IRQs are clustered together
227 * throughout the interrupt numbers at irregular intervals.
228 * To solve this quandry, we have placed the read-in values
229 * into the cluster information table.
231 hwirq = gpio - cluster->start + cluster->to_irq;
232 return irq_create_mapping(pct->parent->domain, hwirq);
239 static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
240 unsigned gpio, int alt_setting)
242 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
243 struct alternate_functions af = pct->soc->alternate_functions[gpio];
248 const char *modes[] = {
249 [ABX500_DEFAULT] = "default",
250 [ABX500_ALT_A] = "altA",
251 [ABX500_ALT_B] = "altB",
252 [ABX500_ALT_C] = "altC",
256 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
257 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
258 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
259 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
264 /* on ABx5xx, there is no GPIO0, so adjust the offset */
267 switch (alt_setting) {
270 * for ABx5xx family, default mode is always selected by
271 * writing 0 to GPIOSELx register, except for pins which
272 * support at least ALT_B mode, default mode is selected
273 * by writing 1 to GPIOSELx register
276 if (af.alt_bit1 != UNUSED)
279 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
285 * for ABx5xx family, alt_a mode is always selected by
286 * writing 1 to GPIOSELx register, except for pins which
287 * support at least ALT_B mode, alt_a mode is selected
288 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
291 if (af.alt_bit1 != UNUSED) {
292 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
297 ret = abx500_gpio_set_bits(chip,
298 AB8500_GPIO_ALTFUN_REG,
300 !!(af.alta_val & BIT(0)));
304 if (af.alt_bit2 != UNUSED)
305 ret = abx500_gpio_set_bits(chip,
306 AB8500_GPIO_ALTFUN_REG,
308 !!(af.alta_val & BIT(1)));
310 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
315 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
320 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
321 af.alt_bit1, !!(af.altb_val & BIT(0)));
325 if (af.alt_bit2 != UNUSED)
326 ret = abx500_gpio_set_bits(chip,
327 AB8500_GPIO_ALTFUN_REG,
329 !!(af.altb_val & BIT(1)));
333 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
338 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
339 af.alt_bit2, !!(af.altc_val & BIT(0)));
343 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
344 af.alt_bit2, !!(af.altc_val & BIT(1)));
348 dev_dbg(pct->dev, "unknown alt_setting %d\n", alt_setting);
354 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
359 #ifdef CONFIG_DEBUG_FS
360 static int abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
367 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
368 struct alternate_functions af = pct->soc->alternate_functions[gpio];
369 /* on ABx5xx, there is no GPIO0, so adjust the offset */
370 unsigned offset = gpio - 1;
374 * if gpiosel_bit is set to unused,
375 * it means no GPIO or special case
377 if (af.gpiosel_bit == UNUSED)
378 return ABX500_DEFAULT;
380 /* read GpioSelx register */
381 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
382 af.gpiosel_bit, &bit_mode);
389 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
390 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
392 "alt_bitX value not in correct range (-1 to 7)\n");
396 /* if alt_bit2 is used, alt_bit1 must be used too */
397 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
399 "if alt_bit2 is used, alt_bit1 can't be unused\n");
403 /* check if pin use AlternateFunction register */
404 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
407 * if pin GPIOSEL bit is set and pin supports alternate function,
408 * it means DEFAULT mode
411 return ABX500_DEFAULT;
414 * pin use the AlternatFunction register
415 * read alt_bit1 value
417 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
418 af.alt_bit1, &alt_bit1);
422 if (af.alt_bit2 != UNUSED) {
423 /* read alt_bit2 value */
424 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
432 mode = (alt_bit2 << 1) + alt_bit1;
433 if (mode == af.alta_val)
435 else if (mode == af.altb_val)
441 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
445 #include <linux/seq_file.h>
447 static void abx500_gpio_dbg_show_one(struct seq_file *s,
448 struct pinctrl_dev *pctldev,
449 struct gpio_chip *chip,
450 unsigned offset, unsigned gpio)
452 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
453 const char *label = gpiochip_is_requested(chip, offset - 1);
454 u8 gpio_offset = offset - 1;
460 const char *modes[] = {
461 [ABX500_DEFAULT] = "default",
462 [ABX500_ALT_A] = "altA",
463 [ABX500_ALT_B] = "altB",
464 [ABX500_ALT_C] = "altC",
467 const char *pull_up_down[] = {
468 [ABX500_GPIO_PULL_DOWN] = "pull down",
469 [ABX500_GPIO_PULL_NONE] = "pull none",
470 [ABX500_GPIO_PULL_NONE + 1] = "pull none",
471 [ABX500_GPIO_PULL_UP] = "pull up",
474 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
475 gpio_offset, &is_out);
479 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
480 gpio, label ?: "(none)",
481 is_out ? "out" : "in ");
484 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
489 seq_printf(s, " %-9s", pull_up_down[pd]);
491 seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
493 mode = abx500_get_mode(pctldev, chip, offset);
495 seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
499 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
502 static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
505 unsigned gpio = chip->base;
506 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
507 struct pinctrl_dev *pctldev = pct->pctldev;
509 for (i = 0; i < chip->ngpio; i++, gpio++) {
510 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
511 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
517 static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
518 struct pinctrl_dev *pctldev,
519 struct gpio_chip *chip,
520 unsigned offset, unsigned gpio)
523 #define abx500_gpio_dbg_show NULL
526 static const struct gpio_chip abx500gpio_chip = {
527 .label = "abx500-gpio",
528 .owner = THIS_MODULE,
529 .request = gpiochip_generic_request,
530 .free = gpiochip_generic_free,
531 .direction_input = abx500_gpio_direction_input,
532 .get = abx500_gpio_get,
533 .direction_output = abx500_gpio_direction_output,
534 .set = abx500_gpio_set,
535 .to_irq = abx500_gpio_to_irq,
536 .dbg_show = abx500_gpio_dbg_show,
539 static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
541 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
543 return pct->soc->nfunctions;
546 static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
549 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
551 return pct->soc->functions[function].name;
554 static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
556 const char * const **groups,
557 unsigned * const num_groups)
559 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
561 *groups = pct->soc->functions[function].groups;
562 *num_groups = pct->soc->functions[function].ngroups;
567 static int abx500_pmx_set(struct pinctrl_dev *pctldev, unsigned function,
570 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
571 struct gpio_chip *chip = &pct->chip;
572 const struct abx500_pingroup *g;
576 g = &pct->soc->groups[group];
577 if (g->altsetting < 0)
580 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
582 for (i = 0; i < g->npins; i++) {
583 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
584 g->pins[i], g->altsetting);
586 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
590 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
595 static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
596 struct pinctrl_gpio_range *range,
599 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
600 const struct abx500_pinrange *p;
605 * Different ranges have different ways to enable GPIO function on a
606 * pin, so refer back to our local range type, where we handily define
607 * what altfunc enables GPIO for a certain pin.
609 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
610 p = &pct->soc->gpio_ranges[i];
611 if ((offset >= p->offset) &&
612 (offset < (p->offset + p->npins)))
616 if (i == pct->soc->gpio_num_ranges) {
617 dev_err(pct->dev, "%s failed to locate range\n", __func__);
621 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
624 ret = abx500_set_mode(pct->pctldev, &pct->chip,
627 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
632 static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
633 struct pinctrl_gpio_range *range,
638 static const struct pinmux_ops abx500_pinmux_ops = {
639 .get_functions_count = abx500_pmx_get_funcs_cnt,
640 .get_function_name = abx500_pmx_get_func_name,
641 .get_function_groups = abx500_pmx_get_func_groups,
642 .set_mux = abx500_pmx_set,
643 .gpio_request_enable = abx500_gpio_request_enable,
644 .gpio_disable_free = abx500_gpio_disable_free,
647 static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
649 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
651 return pct->soc->ngroups;
654 static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
657 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
659 return pct->soc->groups[selector].name;
662 static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
664 const unsigned **pins,
667 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
669 *pins = pct->soc->groups[selector].pins;
670 *num_pins = pct->soc->groups[selector].npins;
675 static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
676 struct seq_file *s, unsigned offset)
678 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
679 struct gpio_chip *chip = &pct->chip;
681 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
682 chip->base + offset - 1);
685 static int abx500_dt_add_map_mux(struct pinctrl_map **map,
686 unsigned *reserved_maps,
687 unsigned *num_maps, const char *group,
688 const char *function)
690 if (*num_maps == *reserved_maps)
693 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
694 (*map)[*num_maps].data.mux.group = group;
695 (*map)[*num_maps].data.mux.function = function;
701 static int abx500_dt_add_map_configs(struct pinctrl_map **map,
702 unsigned *reserved_maps,
703 unsigned *num_maps, const char *group,
704 unsigned long *configs, unsigned num_configs)
706 unsigned long *dup_configs;
708 if (*num_maps == *reserved_maps)
711 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
716 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
718 (*map)[*num_maps].data.configs.group_or_pin = group;
719 (*map)[*num_maps].data.configs.configs = dup_configs;
720 (*map)[*num_maps].data.configs.num_configs = num_configs;
726 static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
727 const char *pin_name)
730 struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
732 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
733 for (i = 0; i < npct->soc->npins; i++)
734 if (npct->soc->pins[i].number == pin_number)
735 return npct->soc->pins[i].name;
739 static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
740 struct device_node *np,
741 struct pinctrl_map **map,
742 unsigned *reserved_maps,
746 const char *function = NULL;
747 unsigned long *configs;
748 unsigned int nconfigs = 0;
749 struct property *prop;
751 ret = of_property_read_string(np, "function", &function);
755 ret = of_property_count_strings(np, "groups");
759 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
764 of_property_for_each_string(np, "groups", prop, group) {
765 ret = abx500_dt_add_map_mux(map, reserved_maps,
766 num_maps, group, function);
772 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &nconfigs);
774 const char *gpio_name;
777 ret = of_property_count_strings(np, "pins");
781 ret = pinctrl_utils_reserve_map(pctldev, map,
787 of_property_for_each_string(np, "pins", prop, pin) {
788 gpio_name = abx500_find_pin_name(pctldev, pin);
790 ret = abx500_dt_add_map_configs(map, reserved_maps,
791 num_maps, gpio_name, configs, 1);
801 static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
802 struct device_node *np_config,
803 struct pinctrl_map **map, unsigned *num_maps)
805 unsigned reserved_maps;
806 struct device_node *np;
813 for_each_child_of_node(np_config, np) {
814 ret = abx500_dt_subnode_to_map(pctldev, np, map,
815 &reserved_maps, num_maps);
817 pinctrl_utils_free_map(pctldev, *map, *num_maps);
826 static const struct pinctrl_ops abx500_pinctrl_ops = {
827 .get_groups_count = abx500_get_groups_cnt,
828 .get_group_name = abx500_get_group_name,
829 .get_group_pins = abx500_get_group_pins,
830 .pin_dbg_show = abx500_pin_dbg_show,
831 .dt_node_to_map = abx500_dt_node_to_map,
832 .dt_free_map = pinctrl_utils_free_map,
835 static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
837 unsigned long *config)
842 static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
844 unsigned long *configs,
845 unsigned num_configs)
847 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
848 struct gpio_chip *chip = &pct->chip;
852 enum pin_config_param param;
853 enum pin_config_param argument;
855 for (i = 0; i < num_configs; i++) {
856 param = pinconf_to_config_param(configs[i]);
857 argument = pinconf_to_config_argument(configs[i]);
859 dev_dbg(chip->parent, "pin %d [%#lx]: %s %s\n",
861 (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
862 (param == PIN_CONFIG_OUTPUT) ?
863 (argument ? "high" : "low") :
864 (argument ? "pull up" : "pull down"));
866 /* on ABx500, there is no GPIO0, so adjust the offset */
870 case PIN_CONFIG_BIAS_DISABLE:
871 ret = abx500_gpio_direction_input(chip, offset);
875 /* Chip only supports pull down */
876 ret = abx500_gpio_set_bits(chip,
877 AB8500_GPIO_PUD1_REG, offset,
878 ABX500_GPIO_PULL_NONE);
881 case PIN_CONFIG_BIAS_PULL_DOWN:
882 ret = abx500_gpio_direction_input(chip, offset);
886 * if argument = 1 set the pull down
887 * else clear the pull down
888 * Chip only supports pull down
890 ret = abx500_gpio_set_bits(chip,
891 AB8500_GPIO_PUD1_REG,
893 argument ? ABX500_GPIO_PULL_DOWN :
894 ABX500_GPIO_PULL_NONE);
897 case PIN_CONFIG_BIAS_PULL_UP:
898 ret = abx500_gpio_direction_input(chip, offset);
902 * if argument = 1 set the pull up
903 * else clear the pull up
905 ret = abx500_gpio_direction_input(chip, offset);
908 case PIN_CONFIG_OUTPUT:
909 ret = abx500_gpio_direction_output(chip, offset,
914 dev_err(chip->parent,
915 "illegal configuration requested\n");
917 } /* for each config */
920 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
925 static const struct pinconf_ops abx500_pinconf_ops = {
926 .pin_config_get = abx500_pin_config_get,
927 .pin_config_set = abx500_pin_config_set,
931 static struct pinctrl_desc abx500_pinctrl_desc = {
932 .name = "pinctrl-abx500",
933 .pctlops = &abx500_pinctrl_ops,
934 .pmxops = &abx500_pinmux_ops,
935 .confops = &abx500_pinconf_ops,
936 .owner = THIS_MODULE,
939 static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
941 unsigned int lowest = 0;
942 unsigned int highest = 0;
943 unsigned int npins = 0;
947 * Compute number of GPIOs from the last SoC gpio range descriptors
948 * These ranges may include "holes" but the GPIO number space shall
949 * still be homogeneous, so we need to detect and account for any
950 * such holes so that these are included in the number of GPIO pins.
952 for (i = 0; i < soc->gpio_num_ranges; i++) {
955 const struct abx500_pinrange *p;
957 p = &soc->gpio_ranges[i];
959 gend = p->offset + p->npins - 1;
962 /* First iteration, set start values */
972 /* this gives the absolute number of pins */
973 npins = highest - lowest + 1;
977 static const struct of_device_id abx500_gpio_match[] = {
978 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
979 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
983 static int abx500_gpio_probe(struct platform_device *pdev)
985 struct device_node *np = pdev->dev.of_node;
986 const struct of_device_id *match;
987 struct abx500_pinctrl *pct;
988 unsigned int id = -1;
993 dev_err(&pdev->dev, "gpio dt node missing\n");
997 pct = devm_kzalloc(&pdev->dev, sizeof(*pct), GFP_KERNEL);
1001 pct->dev = &pdev->dev;
1002 pct->parent = dev_get_drvdata(pdev->dev.parent);
1003 pct->chip = abx500gpio_chip;
1004 pct->chip.parent = &pdev->dev;
1005 pct->chip.base = -1; /* Dynamic allocation */
1007 match = of_match_device(abx500_gpio_match, &pdev->dev);
1009 dev_err(&pdev->dev, "gpio dt not matching\n");
1012 id = (unsigned long)match->data;
1014 /* Poke in other ASIC variants here */
1016 case PINCTRL_AB8500:
1017 abx500_pinctrl_ab8500_init(&pct->soc);
1019 case PINCTRL_AB8505:
1020 abx500_pinctrl_ab8505_init(&pct->soc);
1023 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
1028 dev_err(&pdev->dev, "Invalid SOC data\n");
1032 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1033 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1034 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
1036 ret = gpiochip_add_data(&pct->chip, pct);
1038 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
1041 dev_info(&pdev->dev, "added gpiochip\n");
1043 abx500_pinctrl_desc.pins = pct->soc->pins;
1044 abx500_pinctrl_desc.npins = pct->soc->npins;
1045 pct->pctldev = devm_pinctrl_register(&pdev->dev, &abx500_pinctrl_desc,
1047 if (IS_ERR(pct->pctldev)) {
1049 "could not register abx500 pinctrl driver\n");
1050 ret = PTR_ERR(pct->pctldev);
1053 dev_info(&pdev->dev, "registered pin controller\n");
1055 /* We will handle a range of GPIO pins */
1056 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1057 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1059 ret = gpiochip_add_pin_range(&pct->chip,
1060 dev_name(&pdev->dev),
1061 p->offset - 1, p->offset, p->npins);
1066 platform_set_drvdata(pdev, pct);
1067 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1072 gpiochip_remove(&pct->chip);
1077 * abx500_gpio_remove() - remove Ab8500-gpio driver
1078 * @pdev: Platform device registered
1080 static int abx500_gpio_remove(struct platform_device *pdev)
1082 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
1084 gpiochip_remove(&pct->chip);
1088 static struct platform_driver abx500_gpio_driver = {
1090 .name = "abx500-gpio",
1091 .of_match_table = abx500_gpio_match,
1093 .probe = abx500_gpio_probe,
1094 .remove = abx500_gpio_remove,
1097 static int __init abx500_gpio_init(void)
1099 return platform_driver_register(&abx500_gpio_driver);
1101 core_initcall(abx500_gpio_init);