Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 1 | /* |
Fenglin Wu | 0e40737 | 2016-07-19 13:25:15 +0800 | [diff] [blame] | 2 | * Copyright (c) 2012-2014, 2016, The Linux Foundation. All rights reserved. |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/gpio.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
Stephen Boyd | ab4256c | 2015-11-18 11:33:17 -0800 | [diff] [blame] | 17 | #include <linux/of_irq.h> |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 18 | #include <linux/pinctrl/pinconf-generic.h> |
| 19 | #include <linux/pinctrl/pinconf.h> |
| 20 | #include <linux/pinctrl/pinmux.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/regmap.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/types.h> |
| 25 | |
| 26 | #include <dt-bindings/pinctrl/qcom,pmic-mpp.h> |
| 27 | |
| 28 | #include "../core.h" |
| 29 | #include "../pinctrl-utils.h" |
| 30 | |
| 31 | #define PMIC_MPP_ADDRESS_RANGE 0x100 |
| 32 | |
| 33 | /* |
| 34 | * Pull Up Values - it indicates whether a pull-up should be |
| 35 | * applied for bidirectional mode only. The hardware ignores the |
| 36 | * configuration when operating in other modes. |
| 37 | */ |
| 38 | #define PMIC_MPP_PULL_UP_0P6KOHM 0 |
| 39 | #define PMIC_MPP_PULL_UP_10KOHM 1 |
| 40 | #define PMIC_MPP_PULL_UP_30KOHM 2 |
| 41 | #define PMIC_MPP_PULL_UP_OPEN 3 |
| 42 | |
| 43 | /* type registers base address bases */ |
| 44 | #define PMIC_MPP_REG_TYPE 0x4 |
| 45 | #define PMIC_MPP_REG_SUBTYPE 0x5 |
| 46 | |
| 47 | /* mpp peripheral type and subtype values */ |
| 48 | #define PMIC_MPP_TYPE 0x11 |
| 49 | #define PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT 0x3 |
| 50 | #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT 0x4 |
| 51 | #define PMIC_MPP_SUBTYPE_4CH_NO_SINK 0x5 |
| 52 | #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK 0x6 |
| 53 | #define PMIC_MPP_SUBTYPE_4CH_FULL_FUNC 0x7 |
| 54 | #define PMIC_MPP_SUBTYPE_8CH_FULL_FUNC 0xf |
| 55 | |
| 56 | #define PMIC_MPP_REG_RT_STS 0x10 |
| 57 | #define PMIC_MPP_REG_RT_STS_VAL_MASK 0x1 |
| 58 | |
| 59 | /* control register base address bases */ |
| 60 | #define PMIC_MPP_REG_MODE_CTL 0x40 |
| 61 | #define PMIC_MPP_REG_DIG_VIN_CTL 0x41 |
| 62 | #define PMIC_MPP_REG_DIG_PULL_CTL 0x42 |
| 63 | #define PMIC_MPP_REG_DIG_IN_CTL 0x43 |
| 64 | #define PMIC_MPP_REG_EN_CTL 0x46 |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 65 | #define PMIC_MPP_REG_AOUT_CTL 0x48 |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 66 | #define PMIC_MPP_REG_AIN_CTL 0x4a |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 67 | #define PMIC_MPP_REG_SINK_CTL 0x4c |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 68 | |
| 69 | /* PMIC_MPP_REG_MODE_CTL */ |
| 70 | #define PMIC_MPP_REG_MODE_VALUE_MASK 0x1 |
| 71 | #define PMIC_MPP_REG_MODE_FUNCTION_SHIFT 1 |
| 72 | #define PMIC_MPP_REG_MODE_FUNCTION_MASK 0x7 |
| 73 | #define PMIC_MPP_REG_MODE_DIR_SHIFT 4 |
| 74 | #define PMIC_MPP_REG_MODE_DIR_MASK 0x7 |
| 75 | |
| 76 | /* PMIC_MPP_REG_DIG_VIN_CTL */ |
| 77 | #define PMIC_MPP_REG_VIN_SHIFT 0 |
| 78 | #define PMIC_MPP_REG_VIN_MASK 0x7 |
| 79 | |
| 80 | /* PMIC_MPP_REG_DIG_PULL_CTL */ |
| 81 | #define PMIC_MPP_REG_PULL_SHIFT 0 |
| 82 | #define PMIC_MPP_REG_PULL_MASK 0x7 |
| 83 | |
| 84 | /* PMIC_MPP_REG_EN_CTL */ |
| 85 | #define PMIC_MPP_REG_MASTER_EN_SHIFT 7 |
| 86 | |
| 87 | /* PMIC_MPP_REG_AIN_CTL */ |
| 88 | #define PMIC_MPP_REG_AIN_ROUTE_SHIFT 0 |
| 89 | #define PMIC_MPP_REG_AIN_ROUTE_MASK 0x7 |
| 90 | |
Fenglin Wu | 0e40737 | 2016-07-19 13:25:15 +0800 | [diff] [blame] | 91 | /* PMIC_MPP_REG_SINK_CTL */ |
| 92 | #define PMIC_MPP_REG_CURRENT_SINK_MASK 0x7 |
| 93 | #define MPP_CURRENT_SINK_MA_STEP_SIZE 5 |
| 94 | |
Bjorn Andersson | eaaf5dd | 2015-06-17 23:47:27 -0700 | [diff] [blame] | 95 | #define PMIC_MPP_MODE_DIGITAL_INPUT 0 |
| 96 | #define PMIC_MPP_MODE_DIGITAL_OUTPUT 1 |
| 97 | #define PMIC_MPP_MODE_DIGITAL_BIDIR 2 |
| 98 | #define PMIC_MPP_MODE_ANALOG_BIDIR 3 |
| 99 | #define PMIC_MPP_MODE_ANALOG_INPUT 4 |
| 100 | #define PMIC_MPP_MODE_ANALOG_OUTPUT 5 |
| 101 | #define PMIC_MPP_MODE_CURRENT_SINK 6 |
| 102 | |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 103 | #define PMIC_MPP_SELECTOR_NORMAL 0 |
| 104 | #define PMIC_MPP_SELECTOR_PAIRED 1 |
| 105 | #define PMIC_MPP_SELECTOR_DTEST_FIRST 4 |
| 106 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 107 | #define PMIC_MPP_PHYSICAL_OFFSET 1 |
| 108 | |
| 109 | /* Qualcomm specific pin configurations */ |
| 110 | #define PMIC_MPP_CONF_AMUX_ROUTE (PIN_CONFIG_END + 1) |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 111 | #define PMIC_MPP_CONF_ANALOG_LEVEL (PIN_CONFIG_END + 2) |
| 112 | #define PMIC_MPP_CONF_DTEST_SELECTOR (PIN_CONFIG_END + 3) |
| 113 | #define PMIC_MPP_CONF_PAIRED (PIN_CONFIG_END + 4) |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 114 | #define PMIC_MPP_CONF_DTEST_BUFFER (PIN_CONFIG_END + 5) |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * struct pmic_mpp_pad - keep current MPP settings |
| 118 | * @base: Address base in SPMI device. |
| 119 | * @irq: IRQ number which this MPP generate. |
| 120 | * @is_enabled: Set to false when MPP should be put in high Z state. |
| 121 | * @out_value: Cached pin output value. |
| 122 | * @output_enabled: Set to true if MPP output logic is enabled. |
| 123 | * @input_enabled: Set to true if MPP input buffer logic is enabled. |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 124 | * @paired: Pin operates in paired mode |
Stephen Boyd | ae6d54f | 2016-01-08 12:25:53 -0800 | [diff] [blame] | 125 | * @has_pullup: Pin has support to configure pullup |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 126 | * @num_sources: Number of power-sources supported by this MPP. |
| 127 | * @power_source: Current power-source used. |
| 128 | * @amux_input: Set the source for analog input. |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 129 | * @aout_level: Analog output level |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 130 | * @pullup: Pullup resistor value. Valid in Bidirectional mode only. |
| 131 | * @function: See pmic_mpp_functions[]. |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 132 | * @drive_strength: Amount of current in sink mode |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 133 | * @dtest: DTEST route selector |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 134 | * @dtest_buffer: the DTEST buffer selection for digital input mode |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 135 | */ |
| 136 | struct pmic_mpp_pad { |
| 137 | u16 base; |
| 138 | int irq; |
| 139 | bool is_enabled; |
| 140 | bool out_value; |
| 141 | bool output_enabled; |
| 142 | bool input_enabled; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 143 | bool paired; |
Stephen Boyd | ae6d54f | 2016-01-08 12:25:53 -0800 | [diff] [blame] | 144 | bool has_pullup; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 145 | unsigned int num_sources; |
| 146 | unsigned int power_source; |
| 147 | unsigned int amux_input; |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 148 | unsigned int aout_level; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 149 | unsigned int pullup; |
| 150 | unsigned int function; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 151 | unsigned int drive_strength; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 152 | unsigned int dtest; |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 153 | unsigned int dtest_buffer; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | struct pmic_mpp_state { |
| 157 | struct device *dev; |
| 158 | struct regmap *map; |
| 159 | struct pinctrl_dev *ctrl; |
| 160 | struct gpio_chip chip; |
| 161 | }; |
| 162 | |
Bjorn Andersson | ba5f94c | 2015-06-17 23:47:25 -0700 | [diff] [blame] | 163 | static const struct pinconf_generic_params pmic_mpp_bindings[] = { |
| 164 | {"qcom,amux-route", PMIC_MPP_CONF_AMUX_ROUTE, 0}, |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 165 | {"qcom,analog-level", PMIC_MPP_CONF_ANALOG_LEVEL, 0}, |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 166 | {"qcom,dtest", PMIC_MPP_CONF_DTEST_SELECTOR, 0}, |
| 167 | {"qcom,paired", PMIC_MPP_CONF_PAIRED, 0}, |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 168 | {"qcom,dtest-buffer", PMIC_MPP_CONF_DTEST_BUFFER, 0}, |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 169 | }; |
| 170 | |
Bjorn Andersson | ba5f94c | 2015-06-17 23:47:25 -0700 | [diff] [blame] | 171 | #ifdef CONFIG_DEBUG_FS |
| 172 | static const struct pin_config_item pmic_conf_items[] = { |
| 173 | PCONFDUMP(PMIC_MPP_CONF_AMUX_ROUTE, "analog mux", NULL, true), |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 174 | PCONFDUMP(PMIC_MPP_CONF_ANALOG_LEVEL, "analog level", NULL, true), |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 175 | PCONFDUMP(PMIC_MPP_CONF_DTEST_SELECTOR, "dtest", NULL, true), |
| 176 | PCONFDUMP(PMIC_MPP_CONF_PAIRED, "paired", NULL, false), |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 177 | PCONFDUMP(PMIC_MPP_CONF_DTEST_BUFFER, "dtest buffer", NULL, true), |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 178 | }; |
Bjorn Andersson | ba5f94c | 2015-06-17 23:47:25 -0700 | [diff] [blame] | 179 | #endif |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 180 | |
| 181 | static const char *const pmic_mpp_groups[] = { |
| 182 | "mpp1", "mpp2", "mpp3", "mpp4", "mpp5", "mpp6", "mpp7", "mpp8", |
| 183 | }; |
| 184 | |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 185 | #define PMIC_MPP_DIGITAL 0 |
| 186 | #define PMIC_MPP_ANALOG 1 |
| 187 | #define PMIC_MPP_SINK 2 |
| 188 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 189 | static const char *const pmic_mpp_functions[] = { |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 190 | "digital", "analog", "sink" |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 191 | }; |
| 192 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 193 | static int pmic_mpp_read(struct pmic_mpp_state *state, |
| 194 | struct pmic_mpp_pad *pad, unsigned int addr) |
| 195 | { |
| 196 | unsigned int val; |
| 197 | int ret; |
| 198 | |
| 199 | ret = regmap_read(state->map, pad->base + addr, &val); |
| 200 | if (ret < 0) |
| 201 | dev_err(state->dev, "read 0x%x failed\n", addr); |
| 202 | else |
| 203 | ret = val; |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static int pmic_mpp_write(struct pmic_mpp_state *state, |
| 209 | struct pmic_mpp_pad *pad, unsigned int addr, |
| 210 | unsigned int val) |
| 211 | { |
| 212 | int ret; |
| 213 | |
| 214 | ret = regmap_write(state->map, pad->base + addr, val); |
| 215 | if (ret < 0) |
| 216 | dev_err(state->dev, "write 0x%x failed\n", addr); |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | static int pmic_mpp_get_groups_count(struct pinctrl_dev *pctldev) |
| 222 | { |
| 223 | /* Every PIN is a group */ |
| 224 | return pctldev->desc->npins; |
| 225 | } |
| 226 | |
| 227 | static const char *pmic_mpp_get_group_name(struct pinctrl_dev *pctldev, |
| 228 | unsigned pin) |
| 229 | { |
| 230 | return pctldev->desc->pins[pin].name; |
| 231 | } |
| 232 | |
| 233 | static int pmic_mpp_get_group_pins(struct pinctrl_dev *pctldev, |
| 234 | unsigned pin, |
| 235 | const unsigned **pins, unsigned *num_pins) |
| 236 | { |
| 237 | *pins = &pctldev->desc->pins[pin].number; |
| 238 | *num_pins = 1; |
| 239 | return 0; |
| 240 | } |
| 241 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 242 | static const struct pinctrl_ops pmic_mpp_pinctrl_ops = { |
| 243 | .get_groups_count = pmic_mpp_get_groups_count, |
| 244 | .get_group_name = pmic_mpp_get_group_name, |
| 245 | .get_group_pins = pmic_mpp_get_group_pins, |
Bjorn Andersson | ba5f94c | 2015-06-17 23:47:25 -0700 | [diff] [blame] | 246 | .dt_node_to_map = pinconf_generic_dt_node_to_map_group, |
Irina Tirdea | d32f7fd | 2016-03-31 14:44:42 +0300 | [diff] [blame] | 247 | .dt_free_map = pinctrl_utils_free_map, |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static int pmic_mpp_get_functions_count(struct pinctrl_dev *pctldev) |
| 251 | { |
| 252 | return ARRAY_SIZE(pmic_mpp_functions); |
| 253 | } |
| 254 | |
| 255 | static const char *pmic_mpp_get_function_name(struct pinctrl_dev *pctldev, |
| 256 | unsigned function) |
| 257 | { |
| 258 | return pmic_mpp_functions[function]; |
| 259 | } |
| 260 | |
| 261 | static int pmic_mpp_get_function_groups(struct pinctrl_dev *pctldev, |
| 262 | unsigned function, |
| 263 | const char *const **groups, |
| 264 | unsigned *const num_qgroups) |
| 265 | { |
| 266 | *groups = pmic_mpp_groups; |
| 267 | *num_qgroups = pctldev->desc->npins; |
| 268 | return 0; |
| 269 | } |
| 270 | |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 271 | static int pmic_mpp_write_mode_ctl(struct pmic_mpp_state *state, |
| 272 | struct pmic_mpp_pad *pad) |
| 273 | { |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 274 | unsigned int mode; |
| 275 | unsigned int sel; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 276 | unsigned int val; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 277 | unsigned int en; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 278 | |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 279 | switch (pad->function) { |
| 280 | case PMIC_MPP_ANALOG: |
| 281 | if (pad->input_enabled && pad->output_enabled) |
| 282 | mode = PMIC_MPP_MODE_ANALOG_BIDIR; |
| 283 | else if (pad->input_enabled) |
| 284 | mode = PMIC_MPP_MODE_ANALOG_INPUT; |
| 285 | else |
| 286 | mode = PMIC_MPP_MODE_ANALOG_OUTPUT; |
| 287 | break; |
| 288 | case PMIC_MPP_DIGITAL: |
| 289 | if (pad->input_enabled && pad->output_enabled) |
| 290 | mode = PMIC_MPP_MODE_DIGITAL_BIDIR; |
| 291 | else if (pad->input_enabled) |
| 292 | mode = PMIC_MPP_MODE_DIGITAL_INPUT; |
| 293 | else |
| 294 | mode = PMIC_MPP_MODE_DIGITAL_OUTPUT; |
| 295 | break; |
| 296 | case PMIC_MPP_SINK: |
| 297 | default: |
| 298 | mode = PMIC_MPP_MODE_CURRENT_SINK; |
| 299 | break; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 302 | if (pad->dtest) |
| 303 | sel = PMIC_MPP_SELECTOR_DTEST_FIRST + pad->dtest - 1; |
| 304 | else if (pad->paired) |
| 305 | sel = PMIC_MPP_SELECTOR_PAIRED; |
| 306 | else |
| 307 | sel = PMIC_MPP_SELECTOR_NORMAL; |
| 308 | |
| 309 | en = !!pad->out_value; |
| 310 | |
| 311 | val = mode << PMIC_MPP_REG_MODE_DIR_SHIFT | |
| 312 | sel << PMIC_MPP_REG_MODE_FUNCTION_SHIFT | |
| 313 | en; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 314 | |
| 315 | return pmic_mpp_write(state, pad, PMIC_MPP_REG_MODE_CTL, val); |
| 316 | } |
| 317 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 318 | static int pmic_mpp_set_mux(struct pinctrl_dev *pctldev, unsigned function, |
| 319 | unsigned pin) |
| 320 | { |
| 321 | struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev); |
| 322 | struct pmic_mpp_pad *pad; |
| 323 | unsigned int val; |
| 324 | int ret; |
| 325 | |
| 326 | pad = pctldev->desc->pins[pin].drv_data; |
| 327 | |
| 328 | pad->function = function; |
| 329 | |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 330 | ret = pmic_mpp_write_mode_ctl(state, pad); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 331 | |
| 332 | val = pad->is_enabled << PMIC_MPP_REG_MASTER_EN_SHIFT; |
| 333 | |
| 334 | return pmic_mpp_write(state, pad, PMIC_MPP_REG_EN_CTL, val); |
| 335 | } |
| 336 | |
| 337 | static const struct pinmux_ops pmic_mpp_pinmux_ops = { |
| 338 | .get_functions_count = pmic_mpp_get_functions_count, |
| 339 | .get_function_name = pmic_mpp_get_function_name, |
| 340 | .get_function_groups = pmic_mpp_get_function_groups, |
| 341 | .set_mux = pmic_mpp_set_mux, |
| 342 | }; |
| 343 | |
| 344 | static int pmic_mpp_config_get(struct pinctrl_dev *pctldev, |
| 345 | unsigned int pin, unsigned long *config) |
| 346 | { |
| 347 | unsigned param = pinconf_to_config_param(*config); |
| 348 | struct pmic_mpp_pad *pad; |
| 349 | unsigned arg = 0; |
| 350 | |
| 351 | pad = pctldev->desc->pins[pin].drv_data; |
| 352 | |
| 353 | switch (param) { |
| 354 | case PIN_CONFIG_BIAS_DISABLE: |
| 355 | arg = pad->pullup == PMIC_MPP_PULL_UP_OPEN; |
| 356 | break; |
| 357 | case PIN_CONFIG_BIAS_PULL_UP: |
| 358 | switch (pad->pullup) { |
| 359 | case PMIC_MPP_PULL_UP_OPEN: |
| 360 | arg = 0; |
| 361 | break; |
| 362 | case PMIC_MPP_PULL_UP_0P6KOHM: |
| 363 | arg = 600; |
| 364 | break; |
| 365 | case PMIC_MPP_PULL_UP_10KOHM: |
| 366 | arg = 10000; |
| 367 | break; |
| 368 | case PMIC_MPP_PULL_UP_30KOHM: |
| 369 | arg = 30000; |
| 370 | break; |
| 371 | default: |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | break; |
| 375 | case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: |
| 376 | arg = !pad->is_enabled; |
| 377 | break; |
| 378 | case PIN_CONFIG_POWER_SOURCE: |
| 379 | arg = pad->power_source; |
| 380 | break; |
| 381 | case PIN_CONFIG_INPUT_ENABLE: |
| 382 | arg = pad->input_enabled; |
| 383 | break; |
| 384 | case PIN_CONFIG_OUTPUT: |
| 385 | arg = pad->out_value; |
| 386 | break; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 387 | case PMIC_MPP_CONF_DTEST_SELECTOR: |
| 388 | arg = pad->dtest; |
| 389 | break; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 390 | case PMIC_MPP_CONF_AMUX_ROUTE: |
| 391 | arg = pad->amux_input; |
| 392 | break; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 393 | case PMIC_MPP_CONF_PAIRED: |
| 394 | arg = pad->paired; |
| 395 | break; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 396 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 397 | arg = pad->drive_strength; |
| 398 | break; |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 399 | case PMIC_MPP_CONF_ANALOG_LEVEL: |
| 400 | arg = pad->aout_level; |
| 401 | break; |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 402 | case PMIC_MPP_CONF_DTEST_BUFFER: |
| 403 | arg = pad->dtest_buffer; |
| 404 | break; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 405 | default: |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | /* Convert register value to pinconf value */ |
| 410 | *config = pinconf_to_config_packed(param, arg); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int pmic_mpp_config_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 415 | unsigned long *configs, unsigned nconfs) |
| 416 | { |
| 417 | struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev); |
| 418 | struct pmic_mpp_pad *pad; |
| 419 | unsigned param, arg; |
| 420 | unsigned int val; |
| 421 | int i, ret; |
| 422 | |
| 423 | pad = pctldev->desc->pins[pin].drv_data; |
| 424 | |
Bjorn Andersson | 7682b37 | 2015-06-17 23:47:26 -0700 | [diff] [blame] | 425 | /* Make it possible to enable the pin, by not setting high impedance */ |
| 426 | pad->is_enabled = true; |
| 427 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 428 | for (i = 0; i < nconfs; i++) { |
| 429 | param = pinconf_to_config_param(configs[i]); |
| 430 | arg = pinconf_to_config_argument(configs[i]); |
| 431 | |
| 432 | switch (param) { |
| 433 | case PIN_CONFIG_BIAS_DISABLE: |
| 434 | pad->pullup = PMIC_MPP_PULL_UP_OPEN; |
| 435 | break; |
| 436 | case PIN_CONFIG_BIAS_PULL_UP: |
| 437 | switch (arg) { |
| 438 | case 600: |
| 439 | pad->pullup = PMIC_MPP_PULL_UP_0P6KOHM; |
| 440 | break; |
| 441 | case 10000: |
| 442 | pad->pullup = PMIC_MPP_PULL_UP_10KOHM; |
| 443 | break; |
| 444 | case 30000: |
| 445 | pad->pullup = PMIC_MPP_PULL_UP_30KOHM; |
| 446 | break; |
| 447 | default: |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | break; |
| 451 | case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: |
| 452 | pad->is_enabled = false; |
| 453 | break; |
| 454 | case PIN_CONFIG_POWER_SOURCE: |
| 455 | if (arg >= pad->num_sources) |
| 456 | return -EINVAL; |
| 457 | pad->power_source = arg; |
| 458 | break; |
| 459 | case PIN_CONFIG_INPUT_ENABLE: |
| 460 | pad->input_enabled = arg ? true : false; |
| 461 | break; |
| 462 | case PIN_CONFIG_OUTPUT: |
| 463 | pad->output_enabled = true; |
| 464 | pad->out_value = arg; |
| 465 | break; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 466 | case PMIC_MPP_CONF_DTEST_SELECTOR: |
| 467 | pad->dtest = arg; |
| 468 | break; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 469 | case PIN_CONFIG_DRIVE_STRENGTH: |
Fenglin Wu | 0e40737 | 2016-07-19 13:25:15 +0800 | [diff] [blame] | 470 | pad->drive_strength = arg; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 471 | break; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 472 | case PMIC_MPP_CONF_AMUX_ROUTE: |
| 473 | if (arg >= PMIC_MPP_AMUX_ROUTE_ABUS4) |
| 474 | return -EINVAL; |
| 475 | pad->amux_input = arg; |
| 476 | break; |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 477 | case PMIC_MPP_CONF_ANALOG_LEVEL: |
| 478 | pad->aout_level = arg; |
| 479 | break; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 480 | case PMIC_MPP_CONF_PAIRED: |
| 481 | pad->paired = !!arg; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 482 | break; |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 483 | case PMIC_MPP_CONF_DTEST_BUFFER: |
| 484 | /* |
| 485 | * 0xf is the max value which selects |
| 486 | * 4 dtest rails simultaneously |
| 487 | */ |
| 488 | if (arg > 0xf) |
| 489 | return -EINVAL; |
| 490 | pad->dtest_buffer = arg; |
| 491 | break; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 492 | default: |
| 493 | return -EINVAL; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | val = pad->power_source << PMIC_MPP_REG_VIN_SHIFT; |
| 498 | |
| 499 | ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_DIG_VIN_CTL, val); |
| 500 | if (ret < 0) |
| 501 | return ret; |
| 502 | |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 503 | val = pad->dtest_buffer; |
| 504 | ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_DIG_IN_CTL, val); |
| 505 | if (ret < 0) |
| 506 | return ret; |
| 507 | |
Stephen Boyd | ae6d54f | 2016-01-08 12:25:53 -0800 | [diff] [blame] | 508 | if (pad->has_pullup) { |
| 509 | val = pad->pullup << PMIC_MPP_REG_PULL_SHIFT; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 510 | |
Stephen Boyd | ae6d54f | 2016-01-08 12:25:53 -0800 | [diff] [blame] | 511 | ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_DIG_PULL_CTL, |
| 512 | val); |
| 513 | if (ret < 0) |
| 514 | return ret; |
| 515 | } |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 516 | |
| 517 | val = pad->amux_input & PMIC_MPP_REG_AIN_ROUTE_MASK; |
| 518 | |
| 519 | ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_AIN_CTL, val); |
| 520 | if (ret < 0) |
| 521 | return ret; |
| 522 | |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 523 | ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_AOUT_CTL, pad->aout_level); |
| 524 | if (ret < 0) |
| 525 | return ret; |
| 526 | |
Fenglin Wu | 0e40737 | 2016-07-19 13:25:15 +0800 | [diff] [blame] | 527 | val = 0; |
| 528 | if (pad->drive_strength >= MPP_CURRENT_SINK_MA_STEP_SIZE) |
| 529 | val = DIV_ROUND_UP(pad->drive_strength, |
| 530 | MPP_CURRENT_SINK_MA_STEP_SIZE) - 1; |
| 531 | |
| 532 | val &= PMIC_MPP_REG_CURRENT_SINK_MASK; |
| 533 | ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_SINK_CTL, val); |
| 534 | if (ret < 0) |
| 535 | return ret; |
| 536 | |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 537 | ret = pmic_mpp_write_mode_ctl(state, pad); |
Bjorn Andersson | 7682b37 | 2015-06-17 23:47:26 -0700 | [diff] [blame] | 538 | if (ret < 0) |
| 539 | return ret; |
| 540 | |
| 541 | val = pad->is_enabled << PMIC_MPP_REG_MASTER_EN_SHIFT; |
| 542 | |
| 543 | return pmic_mpp_write(state, pad, PMIC_MPP_REG_EN_CTL, val); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static void pmic_mpp_config_dbg_show(struct pinctrl_dev *pctldev, |
| 547 | struct seq_file *s, unsigned pin) |
| 548 | { |
| 549 | struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev); |
| 550 | struct pmic_mpp_pad *pad; |
Bjorn Andersson | 7682b37 | 2015-06-17 23:47:26 -0700 | [diff] [blame] | 551 | int ret; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 552 | |
| 553 | static const char *const biases[] = { |
| 554 | "0.6kOhm", "10kOhm", "30kOhm", "Disabled" |
| 555 | }; |
| 556 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 557 | pad = pctldev->desc->pins[pin].drv_data; |
| 558 | |
| 559 | seq_printf(s, " mpp%-2d:", pin + PMIC_MPP_PHYSICAL_OFFSET); |
| 560 | |
Bjorn Andersson | 7682b37 | 2015-06-17 23:47:26 -0700 | [diff] [blame] | 561 | if (!pad->is_enabled) { |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 562 | seq_puts(s, " ---"); |
| 563 | } else { |
| 564 | |
| 565 | if (pad->input_enabled) { |
| 566 | ret = pmic_mpp_read(state, pad, PMIC_MPP_REG_RT_STS); |
Ivan T. Ivanov | 4e637ac | 2015-04-09 18:18:37 +0300 | [diff] [blame] | 567 | if (ret < 0) |
| 568 | return; |
| 569 | |
| 570 | ret &= PMIC_MPP_REG_RT_STS_VAL_MASK; |
| 571 | pad->out_value = ret; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | seq_printf(s, " %-4s", pad->output_enabled ? "out" : "in"); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 575 | seq_printf(s, " %-7s", pmic_mpp_functions[pad->function]); |
| 576 | seq_printf(s, " vin-%d", pad->power_source); |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 577 | seq_printf(s, " %d", pad->aout_level); |
Stephen Boyd | ae6d54f | 2016-01-08 12:25:53 -0800 | [diff] [blame] | 578 | if (pad->has_pullup) |
| 579 | seq_printf(s, " %-8s", biases[pad->pullup]); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 580 | seq_printf(s, " %-4s", pad->out_value ? "high" : "low"); |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 581 | if (pad->dtest) |
| 582 | seq_printf(s, " dtest%d", pad->dtest); |
| 583 | if (pad->paired) |
| 584 | seq_puts(s, " paired"); |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 585 | if (pad->dtest_buffer) |
| 586 | seq_printf(s, " dtest buffer %d", pad->dtest_buffer); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | static const struct pinconf_ops pmic_mpp_pinconf_ops = { |
Bjorn Andersson | ba5f94c | 2015-06-17 23:47:25 -0700 | [diff] [blame] | 591 | .is_generic = true, |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 592 | .pin_config_group_get = pmic_mpp_config_get, |
| 593 | .pin_config_group_set = pmic_mpp_config_set, |
| 594 | .pin_config_group_dbg_show = pmic_mpp_config_dbg_show, |
| 595 | }; |
| 596 | |
| 597 | static int pmic_mpp_direction_input(struct gpio_chip *chip, unsigned pin) |
| 598 | { |
Linus Walleij | 064761d | 2015-12-08 09:53:39 +0100 | [diff] [blame] | 599 | struct pmic_mpp_state *state = gpiochip_get_data(chip); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 600 | unsigned long config; |
| 601 | |
| 602 | config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1); |
| 603 | |
| 604 | return pmic_mpp_config_set(state->ctrl, pin, &config, 1); |
| 605 | } |
| 606 | |
| 607 | static int pmic_mpp_direction_output(struct gpio_chip *chip, |
| 608 | unsigned pin, int val) |
| 609 | { |
Linus Walleij | 064761d | 2015-12-08 09:53:39 +0100 | [diff] [blame] | 610 | struct pmic_mpp_state *state = gpiochip_get_data(chip); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 611 | unsigned long config; |
| 612 | |
| 613 | config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val); |
| 614 | |
| 615 | return pmic_mpp_config_set(state->ctrl, pin, &config, 1); |
| 616 | } |
| 617 | |
| 618 | static int pmic_mpp_get(struct gpio_chip *chip, unsigned pin) |
| 619 | { |
Linus Walleij | 064761d | 2015-12-08 09:53:39 +0100 | [diff] [blame] | 620 | struct pmic_mpp_state *state = gpiochip_get_data(chip); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 621 | struct pmic_mpp_pad *pad; |
| 622 | int ret; |
| 623 | |
| 624 | pad = state->ctrl->desc->pins[pin].drv_data; |
| 625 | |
| 626 | if (pad->input_enabled) { |
| 627 | ret = pmic_mpp_read(state, pad, PMIC_MPP_REG_RT_STS); |
| 628 | if (ret < 0) |
| 629 | return ret; |
| 630 | |
| 631 | pad->out_value = ret & PMIC_MPP_REG_RT_STS_VAL_MASK; |
| 632 | } |
| 633 | |
Linus Walleij | 59663a4 | 2015-12-21 16:31:06 +0100 | [diff] [blame] | 634 | return !!pad->out_value; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | static void pmic_mpp_set(struct gpio_chip *chip, unsigned pin, int value) |
| 638 | { |
Linus Walleij | 064761d | 2015-12-08 09:53:39 +0100 | [diff] [blame] | 639 | struct pmic_mpp_state *state = gpiochip_get_data(chip); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 640 | unsigned long config; |
| 641 | |
| 642 | config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value); |
| 643 | |
| 644 | pmic_mpp_config_set(state->ctrl, pin, &config, 1); |
| 645 | } |
| 646 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 647 | static int pmic_mpp_of_xlate(struct gpio_chip *chip, |
| 648 | const struct of_phandle_args *gpio_desc, |
| 649 | u32 *flags) |
| 650 | { |
| 651 | if (chip->of_gpio_n_cells < 2) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | if (flags) |
| 655 | *flags = gpio_desc->args[1]; |
| 656 | |
| 657 | return gpio_desc->args[0] - PMIC_MPP_PHYSICAL_OFFSET; |
| 658 | } |
| 659 | |
| 660 | static int pmic_mpp_to_irq(struct gpio_chip *chip, unsigned pin) |
| 661 | { |
Linus Walleij | 064761d | 2015-12-08 09:53:39 +0100 | [diff] [blame] | 662 | struct pmic_mpp_state *state = gpiochip_get_data(chip); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 663 | struct pmic_mpp_pad *pad; |
| 664 | |
| 665 | pad = state->ctrl->desc->pins[pin].drv_data; |
| 666 | |
| 667 | return pad->irq; |
| 668 | } |
| 669 | |
| 670 | static void pmic_mpp_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 671 | { |
Linus Walleij | 064761d | 2015-12-08 09:53:39 +0100 | [diff] [blame] | 672 | struct pmic_mpp_state *state = gpiochip_get_data(chip); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 673 | unsigned i; |
| 674 | |
| 675 | for (i = 0; i < chip->ngpio; i++) { |
| 676 | pmic_mpp_config_dbg_show(state->ctrl, s, i); |
| 677 | seq_puts(s, "\n"); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | static const struct gpio_chip pmic_mpp_gpio_template = { |
| 682 | .direction_input = pmic_mpp_direction_input, |
| 683 | .direction_output = pmic_mpp_direction_output, |
| 684 | .get = pmic_mpp_get, |
| 685 | .set = pmic_mpp_set, |
Jonas Gorski | 98c85d5 | 2015-10-11 17:34:19 +0200 | [diff] [blame] | 686 | .request = gpiochip_generic_request, |
| 687 | .free = gpiochip_generic_free, |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 688 | .of_xlate = pmic_mpp_of_xlate, |
| 689 | .to_irq = pmic_mpp_to_irq, |
| 690 | .dbg_show = pmic_mpp_dbg_show, |
| 691 | }; |
| 692 | |
| 693 | static int pmic_mpp_populate(struct pmic_mpp_state *state, |
| 694 | struct pmic_mpp_pad *pad) |
| 695 | { |
| 696 | int type, subtype, val, dir; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 697 | unsigned int sel; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 698 | |
| 699 | type = pmic_mpp_read(state, pad, PMIC_MPP_REG_TYPE); |
| 700 | if (type < 0) |
| 701 | return type; |
| 702 | |
| 703 | if (type != PMIC_MPP_TYPE) { |
| 704 | dev_err(state->dev, "incorrect block type 0x%x at 0x%x\n", |
| 705 | type, pad->base); |
| 706 | return -ENODEV; |
| 707 | } |
| 708 | |
| 709 | subtype = pmic_mpp_read(state, pad, PMIC_MPP_REG_SUBTYPE); |
| 710 | if (subtype < 0) |
| 711 | return subtype; |
| 712 | |
| 713 | switch (subtype) { |
| 714 | case PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT: |
| 715 | case PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT: |
| 716 | case PMIC_MPP_SUBTYPE_4CH_NO_SINK: |
| 717 | case PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK: |
| 718 | case PMIC_MPP_SUBTYPE_4CH_FULL_FUNC: |
| 719 | pad->num_sources = 4; |
| 720 | break; |
| 721 | case PMIC_MPP_SUBTYPE_8CH_FULL_FUNC: |
| 722 | pad->num_sources = 8; |
| 723 | break; |
| 724 | default: |
| 725 | dev_err(state->dev, "unknown MPP type 0x%x at 0x%x\n", |
| 726 | subtype, pad->base); |
| 727 | return -ENODEV; |
| 728 | } |
| 729 | |
| 730 | val = pmic_mpp_read(state, pad, PMIC_MPP_REG_MODE_CTL); |
| 731 | if (val < 0) |
| 732 | return val; |
| 733 | |
| 734 | pad->out_value = val & PMIC_MPP_REG_MODE_VALUE_MASK; |
| 735 | |
| 736 | dir = val >> PMIC_MPP_REG_MODE_DIR_SHIFT; |
| 737 | dir &= PMIC_MPP_REG_MODE_DIR_MASK; |
| 738 | |
| 739 | switch (dir) { |
Bjorn Andersson | eaaf5dd | 2015-06-17 23:47:27 -0700 | [diff] [blame] | 740 | case PMIC_MPP_MODE_DIGITAL_INPUT: |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 741 | pad->input_enabled = true; |
| 742 | pad->output_enabled = false; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 743 | pad->function = PMIC_MPP_DIGITAL; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 744 | break; |
Bjorn Andersson | eaaf5dd | 2015-06-17 23:47:27 -0700 | [diff] [blame] | 745 | case PMIC_MPP_MODE_DIGITAL_OUTPUT: |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 746 | pad->input_enabled = false; |
| 747 | pad->output_enabled = true; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 748 | pad->function = PMIC_MPP_DIGITAL; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 749 | break; |
Bjorn Andersson | eaaf5dd | 2015-06-17 23:47:27 -0700 | [diff] [blame] | 750 | case PMIC_MPP_MODE_DIGITAL_BIDIR: |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 751 | pad->input_enabled = true; |
| 752 | pad->output_enabled = true; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 753 | pad->function = PMIC_MPP_DIGITAL; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 754 | break; |
Bjorn Andersson | eaaf5dd | 2015-06-17 23:47:27 -0700 | [diff] [blame] | 755 | case PMIC_MPP_MODE_ANALOG_BIDIR: |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 756 | pad->input_enabled = true; |
| 757 | pad->output_enabled = true; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 758 | pad->function = PMIC_MPP_ANALOG; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 759 | break; |
Bjorn Andersson | eaaf5dd | 2015-06-17 23:47:27 -0700 | [diff] [blame] | 760 | case PMIC_MPP_MODE_ANALOG_INPUT: |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 761 | pad->input_enabled = true; |
| 762 | pad->output_enabled = false; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 763 | pad->function = PMIC_MPP_ANALOG; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 764 | break; |
Bjorn Andersson | eaaf5dd | 2015-06-17 23:47:27 -0700 | [diff] [blame] | 765 | case PMIC_MPP_MODE_ANALOG_OUTPUT: |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 766 | pad->input_enabled = false; |
| 767 | pad->output_enabled = true; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 768 | pad->function = PMIC_MPP_ANALOG; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 769 | break; |
| 770 | case PMIC_MPP_MODE_CURRENT_SINK: |
| 771 | pad->input_enabled = false; |
| 772 | pad->output_enabled = true; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 773 | pad->function = PMIC_MPP_SINK; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 774 | break; |
| 775 | default: |
| 776 | dev_err(state->dev, "unknown MPP direction\n"); |
| 777 | return -ENODEV; |
| 778 | } |
| 779 | |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 780 | sel = val >> PMIC_MPP_REG_MODE_FUNCTION_SHIFT; |
| 781 | sel &= PMIC_MPP_REG_MODE_FUNCTION_MASK; |
| 782 | |
| 783 | if (sel >= PMIC_MPP_SELECTOR_DTEST_FIRST) |
Fenglin Wu | 9a508e8 | 2016-08-18 10:54:00 +0800 | [diff] [blame] | 784 | pad->dtest = sel - PMIC_MPP_SELECTOR_DTEST_FIRST + 1; |
Bjorn Andersson | eb5c144 | 2015-06-17 23:47:30 -0700 | [diff] [blame] | 785 | else if (sel == PMIC_MPP_SELECTOR_PAIRED) |
| 786 | pad->paired = true; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 787 | |
| 788 | val = pmic_mpp_read(state, pad, PMIC_MPP_REG_DIG_VIN_CTL); |
| 789 | if (val < 0) |
| 790 | return val; |
| 791 | |
| 792 | pad->power_source = val >> PMIC_MPP_REG_VIN_SHIFT; |
| 793 | pad->power_source &= PMIC_MPP_REG_VIN_MASK; |
| 794 | |
Fenglin Wu | 9651d64 | 2016-07-19 17:03:42 +0800 | [diff] [blame] | 795 | val = pmic_mpp_read(state, pad, PMIC_MPP_REG_DIG_IN_CTL); |
| 796 | if (val < 0) |
| 797 | return val; |
| 798 | |
| 799 | pad->dtest_buffer = val; |
| 800 | |
Stephen Boyd | ae6d54f | 2016-01-08 12:25:53 -0800 | [diff] [blame] | 801 | if (subtype != PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT && |
| 802 | subtype != PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK) { |
| 803 | val = pmic_mpp_read(state, pad, PMIC_MPP_REG_DIG_PULL_CTL); |
| 804 | if (val < 0) |
| 805 | return val; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 806 | |
Stephen Boyd | ae6d54f | 2016-01-08 12:25:53 -0800 | [diff] [blame] | 807 | pad->pullup = val >> PMIC_MPP_REG_PULL_SHIFT; |
| 808 | pad->pullup &= PMIC_MPP_REG_PULL_MASK; |
| 809 | pad->has_pullup = true; |
| 810 | } |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 811 | |
| 812 | val = pmic_mpp_read(state, pad, PMIC_MPP_REG_AIN_CTL); |
| 813 | if (val < 0) |
| 814 | return val; |
| 815 | |
| 816 | pad->amux_input = val >> PMIC_MPP_REG_AIN_ROUTE_SHIFT; |
| 817 | pad->amux_input &= PMIC_MPP_REG_AIN_ROUTE_MASK; |
| 818 | |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 819 | val = pmic_mpp_read(state, pad, PMIC_MPP_REG_SINK_CTL); |
| 820 | if (val < 0) |
| 821 | return val; |
| 822 | |
Fenglin Wu | 0e40737 | 2016-07-19 13:25:15 +0800 | [diff] [blame] | 823 | val &= PMIC_MPP_REG_CURRENT_SINK_MASK; |
| 824 | pad->drive_strength = (val + 1) * MPP_CURRENT_SINK_MA_STEP_SIZE; |
Bjorn Andersson | 0e94804 | 2015-06-17 23:47:28 -0700 | [diff] [blame] | 825 | |
Bjorn Andersson | 099f3e4 | 2015-07-14 23:40:33 -0700 | [diff] [blame] | 826 | val = pmic_mpp_read(state, pad, PMIC_MPP_REG_AOUT_CTL); |
| 827 | if (val < 0) |
| 828 | return val; |
| 829 | |
| 830 | pad->aout_level = val; |
| 831 | |
Bjorn Andersson | 7682b37 | 2015-06-17 23:47:26 -0700 | [diff] [blame] | 832 | val = pmic_mpp_read(state, pad, PMIC_MPP_REG_EN_CTL); |
| 833 | if (val < 0) |
| 834 | return val; |
| 835 | |
| 836 | pad->is_enabled = !!val; |
| 837 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | static int pmic_mpp_probe(struct platform_device *pdev) |
| 842 | { |
| 843 | struct device *dev = &pdev->dev; |
| 844 | struct pinctrl_pin_desc *pindesc; |
| 845 | struct pinctrl_desc *pctrldesc; |
| 846 | struct pmic_mpp_pad *pad, *pads; |
| 847 | struct pmic_mpp_state *state; |
| 848 | int ret, npins, i; |
Stephen Boyd | ab4256c | 2015-11-18 11:33:17 -0800 | [diff] [blame] | 849 | u32 reg; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 850 | |
Stephen Boyd | ab4256c | 2015-11-18 11:33:17 -0800 | [diff] [blame] | 851 | ret = of_property_read_u32(dev->of_node, "reg", ®); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 852 | if (ret < 0) { |
Stephen Boyd | ab4256c | 2015-11-18 11:33:17 -0800 | [diff] [blame] | 853 | dev_err(dev, "missing base address"); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 854 | return ret; |
| 855 | } |
| 856 | |
Stephen Boyd | a5ea13f | 2016-01-06 17:37:41 -0800 | [diff] [blame] | 857 | npins = platform_irq_count(pdev); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 858 | if (!npins) |
| 859 | return -EINVAL; |
Stephen Boyd | a5ea13f | 2016-01-06 17:37:41 -0800 | [diff] [blame] | 860 | if (npins < 0) |
| 861 | return npins; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 862 | |
| 863 | BUG_ON(npins > ARRAY_SIZE(pmic_mpp_groups)); |
| 864 | |
| 865 | state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL); |
| 866 | if (!state) |
| 867 | return -ENOMEM; |
| 868 | |
| 869 | platform_set_drvdata(pdev, state); |
| 870 | |
| 871 | state->dev = &pdev->dev; |
| 872 | state->map = dev_get_regmap(dev->parent, NULL); |
| 873 | |
| 874 | pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL); |
| 875 | if (!pindesc) |
| 876 | return -ENOMEM; |
| 877 | |
| 878 | pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL); |
| 879 | if (!pads) |
| 880 | return -ENOMEM; |
| 881 | |
| 882 | pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL); |
| 883 | if (!pctrldesc) |
| 884 | return -ENOMEM; |
| 885 | |
| 886 | pctrldesc->pctlops = &pmic_mpp_pinctrl_ops; |
| 887 | pctrldesc->pmxops = &pmic_mpp_pinmux_ops; |
| 888 | pctrldesc->confops = &pmic_mpp_pinconf_ops; |
| 889 | pctrldesc->owner = THIS_MODULE; |
| 890 | pctrldesc->name = dev_name(dev); |
| 891 | pctrldesc->pins = pindesc; |
| 892 | pctrldesc->npins = npins; |
| 893 | |
Bjorn Andersson | ba5f94c | 2015-06-17 23:47:25 -0700 | [diff] [blame] | 894 | pctrldesc->num_custom_params = ARRAY_SIZE(pmic_mpp_bindings); |
| 895 | pctrldesc->custom_params = pmic_mpp_bindings; |
| 896 | #ifdef CONFIG_DEBUG_FS |
| 897 | pctrldesc->custom_conf_items = pmic_conf_items; |
| 898 | #endif |
| 899 | |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 900 | for (i = 0; i < npins; i++, pindesc++) { |
| 901 | pad = &pads[i]; |
| 902 | pindesc->drv_data = pad; |
| 903 | pindesc->number = i; |
| 904 | pindesc->name = pmic_mpp_groups[i]; |
| 905 | |
| 906 | pad->irq = platform_get_irq(pdev, i); |
| 907 | if (pad->irq < 0) |
| 908 | return pad->irq; |
| 909 | |
Stephen Boyd | ab4256c | 2015-11-18 11:33:17 -0800 | [diff] [blame] | 910 | pad->base = reg + i * PMIC_MPP_ADDRESS_RANGE; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 911 | |
| 912 | ret = pmic_mpp_populate(state, pad); |
| 913 | if (ret < 0) |
| 914 | return ret; |
| 915 | } |
| 916 | |
| 917 | state->chip = pmic_mpp_gpio_template; |
Linus Walleij | 58383c7 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 918 | state->chip.parent = dev; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 919 | state->chip.base = -1; |
| 920 | state->chip.ngpio = npins; |
| 921 | state->chip.label = dev_name(dev); |
| 922 | state->chip.of_gpio_n_cells = 2; |
| 923 | state->chip.can_sleep = false; |
| 924 | |
Laxman Dewangan | ce18e59 | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 925 | state->ctrl = devm_pinctrl_register(dev, pctrldesc, state); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 926 | if (IS_ERR(state->ctrl)) |
| 927 | return PTR_ERR(state->ctrl); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 928 | |
Linus Walleij | 064761d | 2015-12-08 09:53:39 +0100 | [diff] [blame] | 929 | ret = gpiochip_add_data(&state->chip, state); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 930 | if (ret) { |
| 931 | dev_err(state->dev, "can't add gpio chip\n"); |
Laxman Dewangan | ce18e59 | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 932 | return ret; |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins); |
| 936 | if (ret) { |
| 937 | dev_err(dev, "failed to add pin range\n"); |
| 938 | goto err_range; |
| 939 | } |
| 940 | |
| 941 | return 0; |
| 942 | |
| 943 | err_range: |
| 944 | gpiochip_remove(&state->chip); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 945 | return ret; |
| 946 | } |
| 947 | |
| 948 | static int pmic_mpp_remove(struct platform_device *pdev) |
| 949 | { |
| 950 | struct pmic_mpp_state *state = platform_get_drvdata(pdev); |
| 951 | |
| 952 | gpiochip_remove(&state->chip); |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static const struct of_device_id pmic_mpp_of_match[] = { |
| 957 | { .compatible = "qcom,pm8841-mpp" }, /* 4 MPP's */ |
Ivan T. Ivanov | 7414b09 | 2015-03-31 12:37:18 +0300 | [diff] [blame] | 958 | { .compatible = "qcom,pm8916-mpp" }, /* 4 MPP's */ |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 959 | { .compatible = "qcom,pm8941-mpp" }, /* 8 MPP's */ |
Stephen Boyd | eb9610f | 2015-11-17 16:52:33 -0800 | [diff] [blame] | 960 | { .compatible = "qcom,pm8994-mpp" }, /* 8 MPP's */ |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 961 | { .compatible = "qcom,pma8084-mpp" }, /* 8 MPP's */ |
Stephen Boyd | 94fa667 | 2016-07-11 12:01:09 -0700 | [diff] [blame] | 962 | { .compatible = "qcom,spmi-mpp" }, /* Generic */ |
Ivan T. Ivanov | cfb24f6 | 2014-10-22 12:58:47 +0300 | [diff] [blame] | 963 | { }, |
| 964 | }; |
| 965 | |
| 966 | MODULE_DEVICE_TABLE(of, pmic_mpp_of_match); |
| 967 | |
| 968 | static struct platform_driver pmic_mpp_driver = { |
| 969 | .driver = { |
| 970 | .name = "qcom-spmi-mpp", |
| 971 | .of_match_table = pmic_mpp_of_match, |
| 972 | }, |
| 973 | .probe = pmic_mpp_probe, |
| 974 | .remove = pmic_mpp_remove, |
| 975 | }; |
| 976 | |
| 977 | module_platform_driver(pmic_mpp_driver); |
| 978 | |
| 979 | MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>"); |
| 980 | MODULE_DESCRIPTION("Qualcomm SPMI PMIC MPP pin control driver"); |
| 981 | MODULE_ALIAS("platform:qcom-spmi-mpp"); |
| 982 | MODULE_LICENSE("GPL v2"); |