Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 1 | /* |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 2 | * AXP20x pinctrl and GPIO driver |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2016 Maxime Ripard <maxime.ripard@free-electrons.com> |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 5 | * Copyright (C) 2017 Quentin Schulz <quentin.schulz@free-electrons.com> |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/bitops.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/gpio/driver.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/mfd/axp20x.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 22 | #include <linux/pinctrl/pinconf-generic.h> |
| 23 | #include <linux/pinctrl/pinctrl.h> |
| 24 | #include <linux/pinctrl/pinmux.h> |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/regmap.h> |
| 27 | #include <linux/slab.h> |
| 28 | |
| 29 | #define AXP20X_GPIO_FUNCTIONS 0x7 |
| 30 | #define AXP20X_GPIO_FUNCTION_OUT_LOW 0 |
| 31 | #define AXP20X_GPIO_FUNCTION_OUT_HIGH 1 |
| 32 | #define AXP20X_GPIO_FUNCTION_INPUT 2 |
| 33 | |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 34 | #define AXP20X_FUNC_GPIO_OUT 0 |
| 35 | #define AXP20X_FUNC_GPIO_IN 1 |
| 36 | #define AXP20X_FUNC_LDO 2 |
| 37 | #define AXP20X_FUNC_ADC 3 |
| 38 | #define AXP20X_FUNCS_NB 4 |
| 39 | |
| 40 | #define AXP20X_MUX_GPIO_OUT 0 |
| 41 | #define AXP20X_MUX_GPIO_IN BIT(1) |
| 42 | #define AXP20X_MUX_ADC BIT(2) |
| 43 | |
| 44 | struct axp20x_pctrl_desc { |
| 45 | const struct pinctrl_pin_desc *pins; |
| 46 | unsigned int npins; |
| 47 | /* Stores the pins supporting LDO function. Bit offset is pin number. */ |
| 48 | u8 ldo_mask; |
| 49 | /* Stores the pins supporting ADC function. Bit offset is pin number. */ |
| 50 | u8 adc_mask; |
| 51 | }; |
| 52 | |
| 53 | struct axp20x_pinctrl_function { |
| 54 | const char *name; |
| 55 | unsigned int muxval; |
| 56 | const char **groups; |
| 57 | unsigned int ngroups; |
| 58 | }; |
| 59 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 60 | struct axp20x_pctl { |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 61 | struct gpio_chip chip; |
| 62 | struct regmap *regmap; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 63 | struct pinctrl_dev *pctl_dev; |
| 64 | struct device *dev; |
| 65 | const struct axp20x_pctrl_desc *desc; |
| 66 | struct axp20x_pinctrl_function funcs[AXP20X_FUNCS_NB]; |
| 67 | }; |
| 68 | |
| 69 | static const struct pinctrl_pin_desc axp209_pins[] = { |
| 70 | PINCTRL_PIN(0, "GPIO0"), |
| 71 | PINCTRL_PIN(1, "GPIO1"), |
| 72 | PINCTRL_PIN(2, "GPIO2"), |
| 73 | }; |
| 74 | |
| 75 | static const struct axp20x_pctrl_desc axp20x_data = { |
| 76 | .pins = axp209_pins, |
| 77 | .npins = ARRAY_SIZE(axp209_pins), |
| 78 | .ldo_mask = BIT(0) | BIT(1), |
| 79 | .adc_mask = BIT(0) | BIT(1), |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
Quentin Schulz | 3cac991 | 2017-12-05 15:46:39 +0100 | [diff] [blame] | 82 | static int axp20x_gpio_get_reg(unsigned int offset) |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 83 | { |
| 84 | switch (offset) { |
| 85 | case 0: |
| 86 | return AXP20X_GPIO0_CTRL; |
| 87 | case 1: |
| 88 | return AXP20X_GPIO1_CTRL; |
| 89 | case 2: |
| 90 | return AXP20X_GPIO2_CTRL; |
| 91 | } |
| 92 | |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
Quentin Schulz | 3cac991 | 2017-12-05 15:46:39 +0100 | [diff] [blame] | 96 | static int axp20x_gpio_input(struct gpio_chip *chip, unsigned int offset) |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 97 | { |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 98 | return pinctrl_gpio_direction_input(chip->base + offset); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Quentin Schulz | 3cac991 | 2017-12-05 15:46:39 +0100 | [diff] [blame] | 101 | static int axp20x_gpio_get(struct gpio_chip *chip, unsigned int offset) |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 102 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 103 | struct axp20x_pctl *pctl = gpiochip_get_data(chip); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 104 | unsigned int val; |
Quentin Schulz | 1d2b2ac | 2016-11-23 15:11:50 +0100 | [diff] [blame] | 105 | int ret; |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 106 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 107 | ret = regmap_read(pctl->regmap, AXP20X_GPIO20_SS, &val); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 108 | if (ret) |
| 109 | return ret; |
| 110 | |
| 111 | return !!(val & BIT(offset + 4)); |
| 112 | } |
| 113 | |
Quentin Schulz | 3cac991 | 2017-12-05 15:46:39 +0100 | [diff] [blame] | 114 | static int axp20x_gpio_get_direction(struct gpio_chip *chip, |
| 115 | unsigned int offset) |
Maxime Ripard | 81d3753 | 2016-09-21 23:51:22 +0300 | [diff] [blame] | 116 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 117 | struct axp20x_pctl *pctl = gpiochip_get_data(chip); |
Maxime Ripard | 81d3753 | 2016-09-21 23:51:22 +0300 | [diff] [blame] | 118 | unsigned int val; |
| 119 | int reg, ret; |
| 120 | |
| 121 | reg = axp20x_gpio_get_reg(offset); |
| 122 | if (reg < 0) |
| 123 | return reg; |
| 124 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 125 | ret = regmap_read(pctl->regmap, reg, &val); |
Maxime Ripard | 81d3753 | 2016-09-21 23:51:22 +0300 | [diff] [blame] | 126 | if (ret) |
| 127 | return ret; |
| 128 | |
| 129 | /* |
| 130 | * This shouldn't really happen if the pin is in use already, |
| 131 | * or if it's not in use yet, it doesn't matter since we're |
| 132 | * going to change the value soon anyway. Default to output. |
| 133 | */ |
| 134 | if ((val & AXP20X_GPIO_FUNCTIONS) > 2) |
| 135 | return 0; |
| 136 | |
| 137 | /* |
| 138 | * The GPIO directions are the three lowest values. |
| 139 | * 2 is input, 0 and 1 are output |
| 140 | */ |
| 141 | return val & 2; |
| 142 | } |
| 143 | |
Quentin Schulz | 3cac991 | 2017-12-05 15:46:39 +0100 | [diff] [blame] | 144 | static int axp20x_gpio_output(struct gpio_chip *chip, unsigned int offset, |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 145 | int value) |
| 146 | { |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 147 | chip->set(chip, offset, value); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static void axp20x_gpio_set(struct gpio_chip *chip, unsigned int offset, |
| 153 | int value) |
| 154 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 155 | struct axp20x_pctl *pctl = gpiochip_get_data(chip); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 156 | int reg; |
| 157 | |
| 158 | reg = axp20x_gpio_get_reg(offset); |
| 159 | if (reg < 0) |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 160 | return; |
| 161 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 162 | regmap_update_bits(pctl->regmap, reg, |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 163 | AXP20X_GPIO_FUNCTIONS, |
| 164 | value ? AXP20X_GPIO_FUNCTION_OUT_HIGH : |
| 165 | AXP20X_GPIO_FUNCTION_OUT_LOW); |
| 166 | } |
| 167 | |
| 168 | static int axp20x_pmx_set(struct pinctrl_dev *pctldev, unsigned int offset, |
| 169 | u8 config) |
| 170 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 171 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 172 | int reg; |
| 173 | |
| 174 | reg = axp20x_gpio_get_reg(offset); |
| 175 | if (reg < 0) |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 176 | return reg; |
| 177 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 178 | return regmap_update_bits(pctl->regmap, reg, AXP20X_GPIO_FUNCTIONS, |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 179 | config); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 182 | static int axp20x_pmx_func_cnt(struct pinctrl_dev *pctldev) |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 183 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 184 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 185 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 186 | return ARRAY_SIZE(pctl->funcs); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static const char *axp20x_pmx_func_name(struct pinctrl_dev *pctldev, |
| 190 | unsigned int selector) |
| 191 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 192 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 193 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 194 | return pctl->funcs[selector].name; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static int axp20x_pmx_func_groups(struct pinctrl_dev *pctldev, |
| 198 | unsigned int selector, |
| 199 | const char * const **groups, |
| 200 | unsigned int *num_groups) |
| 201 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 202 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 203 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 204 | *groups = pctl->funcs[selector].groups; |
| 205 | *num_groups = pctl->funcs[selector].ngroups; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int axp20x_pmx_set_mux(struct pinctrl_dev *pctldev, |
| 211 | unsigned int function, unsigned int group) |
| 212 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 213 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 214 | unsigned int mask; |
| 215 | |
| 216 | /* Every pin supports GPIO_OUT and GPIO_IN functions */ |
| 217 | if (function <= AXP20X_FUNC_GPIO_IN) |
| 218 | return axp20x_pmx_set(pctldev, group, |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 219 | pctl->funcs[function].muxval); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 220 | |
| 221 | if (function == AXP20X_FUNC_LDO) |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 222 | mask = pctl->desc->ldo_mask; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 223 | else |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 224 | mask = pctl->desc->adc_mask; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 225 | |
| 226 | if (!(BIT(group) & mask)) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | /* |
| 230 | * We let the regulator framework handle the LDO muxing as muxing bits |
| 231 | * are basically also regulators on/off bits. It's better not to enforce |
| 232 | * any state of the regulator when selecting LDO mux so that we don't |
| 233 | * interfere with the regulator driver. |
| 234 | */ |
| 235 | if (function == AXP20X_FUNC_LDO) |
| 236 | return 0; |
| 237 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 238 | return axp20x_pmx_set(pctldev, group, pctl->funcs[function].muxval); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static int axp20x_pmx_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 242 | struct pinctrl_gpio_range *range, |
| 243 | unsigned int offset, bool input) |
| 244 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 245 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 246 | |
| 247 | if (input) |
| 248 | return axp20x_pmx_set(pctldev, offset, |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 249 | pctl->funcs[AXP20X_FUNC_GPIO_IN].muxval); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 250 | |
| 251 | return axp20x_pmx_set(pctldev, offset, |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 252 | pctl->funcs[AXP20X_FUNC_GPIO_OUT].muxval); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static const struct pinmux_ops axp20x_pmx_ops = { |
| 256 | .get_functions_count = axp20x_pmx_func_cnt, |
| 257 | .get_function_name = axp20x_pmx_func_name, |
| 258 | .get_function_groups = axp20x_pmx_func_groups, |
| 259 | .set_mux = axp20x_pmx_set_mux, |
| 260 | .gpio_set_direction = axp20x_pmx_gpio_set_direction, |
| 261 | .strict = true, |
| 262 | }; |
| 263 | |
| 264 | static int axp20x_groups_cnt(struct pinctrl_dev *pctldev) |
| 265 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 266 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 267 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 268 | return pctl->desc->npins; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int axp20x_group_pins(struct pinctrl_dev *pctldev, unsigned int selector, |
| 272 | const unsigned int **pins, unsigned int *num_pins) |
| 273 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 274 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 275 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 276 | *pins = (unsigned int *)&pctl->desc->pins[selector]; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 277 | *num_pins = 1; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static const char *axp20x_group_name(struct pinctrl_dev *pctldev, |
| 283 | unsigned int selector) |
| 284 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 285 | struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 286 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 287 | return pctl->desc->pins[selector].name; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static const struct pinctrl_ops axp20x_pctrl_ops = { |
| 291 | .dt_node_to_map = pinconf_generic_dt_node_to_map_group, |
| 292 | .dt_free_map = pinconf_generic_dt_free_map, |
| 293 | .get_groups_count = axp20x_groups_cnt, |
| 294 | .get_group_name = axp20x_group_name, |
| 295 | .get_group_pins = axp20x_group_pins, |
| 296 | }; |
| 297 | |
| 298 | static void axp20x_funcs_groups_from_mask(struct device *dev, unsigned int mask, |
| 299 | unsigned int mask_len, |
| 300 | struct axp20x_pinctrl_function *func, |
| 301 | const struct pinctrl_pin_desc *pins) |
| 302 | { |
| 303 | unsigned long int mask_cpy = mask; |
| 304 | const char **group; |
| 305 | unsigned int ngroups = hweight8(mask); |
| 306 | int bit; |
| 307 | |
| 308 | func->ngroups = ngroups; |
| 309 | if (func->ngroups > 0) { |
| 310 | func->groups = devm_kzalloc(dev, ngroups * sizeof(const char *), |
| 311 | GFP_KERNEL); |
| 312 | group = func->groups; |
| 313 | for_each_set_bit(bit, &mask_cpy, mask_len) { |
| 314 | *group = pins[bit].name; |
| 315 | group++; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | static void axp20x_build_funcs_groups(struct platform_device *pdev) |
| 321 | { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 322 | struct axp20x_pctl *pctl = platform_get_drvdata(pdev); |
| 323 | int i, pin, npins = pctl->desc->npins; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 324 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 325 | pctl->funcs[AXP20X_FUNC_GPIO_OUT].name = "gpio_out"; |
| 326 | pctl->funcs[AXP20X_FUNC_GPIO_OUT].muxval = AXP20X_MUX_GPIO_OUT; |
| 327 | pctl->funcs[AXP20X_FUNC_GPIO_IN].name = "gpio_in"; |
| 328 | pctl->funcs[AXP20X_FUNC_GPIO_IN].muxval = AXP20X_MUX_GPIO_IN; |
| 329 | pctl->funcs[AXP20X_FUNC_LDO].name = "ldo"; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 330 | /* |
| 331 | * Muxval for LDO is useless as we won't use it. |
| 332 | * See comment in axp20x_pmx_set_mux. |
| 333 | */ |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 334 | pctl->funcs[AXP20X_FUNC_ADC].name = "adc"; |
| 335 | pctl->funcs[AXP20X_FUNC_ADC].muxval = AXP20X_MUX_ADC; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 336 | |
| 337 | /* Every pin supports GPIO_OUT and GPIO_IN functions */ |
| 338 | for (i = 0; i <= AXP20X_FUNC_GPIO_IN; i++) { |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 339 | pctl->funcs[i].ngroups = npins; |
| 340 | pctl->funcs[i].groups = devm_kzalloc(&pdev->dev, |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 341 | npins * sizeof(char *), |
| 342 | GFP_KERNEL); |
| 343 | for (pin = 0; pin < npins; pin++) |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 344 | pctl->funcs[i].groups[pin] = pctl->desc->pins[pin].name; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 347 | axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->ldo_mask, |
| 348 | npins, &pctl->funcs[AXP20X_FUNC_LDO], |
| 349 | pctl->desc->pins); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 350 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 351 | axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->adc_mask, |
| 352 | npins, &pctl->funcs[AXP20X_FUNC_ADC], |
| 353 | pctl->desc->pins); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 356 | static int axp20x_pctl_probe(struct platform_device *pdev) |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 357 | { |
| 358 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 359 | struct axp20x_pctl *pctl; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 360 | struct pinctrl_desc *pctrl_desc; |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 361 | int ret; |
| 362 | |
| 363 | if (!of_device_is_available(pdev->dev.of_node)) |
| 364 | return -ENODEV; |
| 365 | |
| 366 | if (!axp20x) { |
| 367 | dev_err(&pdev->dev, "Parent drvdata not set\n"); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 371 | pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL); |
| 372 | if (!pctl) |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 373 | return -ENOMEM; |
| 374 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 375 | pctl->chip.base = -1; |
| 376 | pctl->chip.can_sleep = true; |
| 377 | pctl->chip.request = gpiochip_generic_request; |
| 378 | pctl->chip.free = gpiochip_generic_free; |
| 379 | pctl->chip.parent = &pdev->dev; |
| 380 | pctl->chip.label = dev_name(&pdev->dev); |
| 381 | pctl->chip.owner = THIS_MODULE; |
| 382 | pctl->chip.get = axp20x_gpio_get; |
| 383 | pctl->chip.get_direction = axp20x_gpio_get_direction; |
| 384 | pctl->chip.set = axp20x_gpio_set; |
| 385 | pctl->chip.direction_input = axp20x_gpio_input; |
| 386 | pctl->chip.direction_output = axp20x_gpio_output; |
| 387 | pctl->chip.ngpio = 3; |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 388 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 389 | pctl->desc = &axp20x_data; |
| 390 | pctl->regmap = axp20x->regmap; |
| 391 | pctl->dev = &pdev->dev; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 392 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 393 | platform_set_drvdata(pdev, pctl); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 394 | |
| 395 | axp20x_build_funcs_groups(pdev); |
| 396 | |
| 397 | pctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctrl_desc), GFP_KERNEL); |
| 398 | if (!pctrl_desc) |
| 399 | return -ENOMEM; |
| 400 | |
| 401 | pctrl_desc->name = dev_name(&pdev->dev); |
| 402 | pctrl_desc->owner = THIS_MODULE; |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 403 | pctrl_desc->pins = pctl->desc->pins; |
| 404 | pctrl_desc->npins = pctl->desc->npins; |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 405 | pctrl_desc->pctlops = &axp20x_pctrl_ops; |
| 406 | pctrl_desc->pmxops = &axp20x_pmx_ops; |
| 407 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 408 | pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl); |
| 409 | if (IS_ERR(pctl->pctl_dev)) { |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 410 | dev_err(&pdev->dev, "couldn't register pinctrl driver\n"); |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 411 | return PTR_ERR(pctl->pctl_dev); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 412 | } |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 413 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 414 | ret = devm_gpiochip_add_data(&pdev->dev, &pctl->chip, pctl); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 415 | if (ret) { |
| 416 | dev_err(&pdev->dev, "Failed to register GPIO chip\n"); |
| 417 | return ret; |
| 418 | } |
| 419 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 420 | ret = gpiochip_add_pin_range(&pctl->chip, dev_name(&pdev->dev), |
| 421 | pctl->desc->pins->number, |
| 422 | pctl->desc->pins->number, |
| 423 | pctl->desc->npins); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 424 | if (ret) { |
| 425 | dev_err(&pdev->dev, "failed to add pin range\n"); |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | dev_info(&pdev->dev, "AXP209 pinctrl and GPIO driver loaded\n"); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 434 | static const struct of_device_id axp20x_pctl_match[] = { |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 435 | { .compatible = "x-powers,axp209-gpio" }, |
| 436 | { } |
| 437 | }; |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 438 | MODULE_DEVICE_TABLE(of, axp20x_pctl_match); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 439 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 440 | static struct platform_driver axp20x_pctl_driver = { |
| 441 | .probe = axp20x_pctl_probe, |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 442 | .driver = { |
| 443 | .name = "axp20x-gpio", |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 444 | .of_match_table = axp20x_pctl_match, |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 445 | }, |
| 446 | }; |
| 447 | |
Quentin Schulz | d242e60 | 2017-12-05 15:46:43 +0100 | [diff] [blame^] | 448 | module_platform_driver(axp20x_pctl_driver); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 449 | |
| 450 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
Quentin Schulz | 23f75d7 | 2017-12-05 15:46:41 +0100 | [diff] [blame] | 451 | MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>"); |
| 452 | MODULE_DESCRIPTION("AXP20x PMIC pinctrl and GPIO driver"); |
Maxime Ripard | f72f4b4 | 2016-07-20 16:11:36 +0200 | [diff] [blame] | 453 | MODULE_LICENSE("GPL"); |