Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/spmi.h> |
| 18 | #include <linux/platform_device.h> |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 19 | #include <linux/debugfs.h> |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 20 | #include <linux/gpio.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_gpio.h> |
| 24 | #include <linux/of_irq.h> |
Michael Bohan | 0b24fb1 | 2012-06-01 10:30:12 -0700 | [diff] [blame] | 25 | #include <linux/export.h> |
| 26 | #include <linux/module.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 27 | #include <linux/export.h> |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 28 | #include <linux/qpnp/pin.h> |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 29 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 30 | #define Q_REG_ADDR(q_spec, reg_index) \ |
| 31 | ((q_spec)->offset + reg_index) |
| 32 | |
| 33 | #define Q_REG_STATUS1 0x8 |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 34 | #define Q_NUM_CTL_REGS 0xD |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 35 | |
| 36 | /* type registers base address offsets */ |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 37 | #define Q_REG_TYPE 0x4 |
| 38 | #define Q_REG_SUBTYPE 0x5 |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 39 | |
| 40 | /* gpio peripheral type and subtype values */ |
| 41 | #define Q_GPIO_TYPE 0x10 |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 42 | #define Q_GPIO_SUBTYPE_GPIO_4CH 0x0 |
| 43 | #define Q_GPIO_SUBTYPE_GPIOC_4CH 0x2 |
| 44 | #define Q_GPIO_SUBTYPE_GPIO_8CH 0x4 |
| 45 | #define Q_GPIO_SUBTYPE_GPIOC_8CH 0x6 |
| 46 | |
| 47 | /* mpp peripheral type and subtype values */ |
| 48 | #define Q_MPP_TYPE 0x11 |
| 49 | #define Q_MPP_SUBTYPE_4CH_NO_ANA_OUT 0x1 |
| 50 | #define Q_MPP_SUBTYPE_4CH_NO_SINK 0x2 |
| 51 | #define Q_MPP_SUBTYPE_4CH_FULL_FUNC 0x3 |
| 52 | #define Q_MPP_SUBTYPE_8CH_FULL_FUNC 0x7 |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 53 | |
| 54 | /* control register base address offsets */ |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 55 | #define Q_REG_MODE_CTL 0x40 |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 56 | #define Q_REG_DIG_VIN_CTL 0x41 |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 57 | #define Q_REG_DIG_PULL_CTL 0x42 |
| 58 | #define Q_REG_DIG_IN_CTL 0x43 |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 59 | #define Q_REG_DIG_OUT_CTL 0x45 |
| 60 | #define Q_REG_EN_CTL 0x46 |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 61 | #define Q_REG_AOUT_CTL 0x48 |
| 62 | #define Q_REG_AIN_CTL 0x4A |
| 63 | #define Q_REG_SINK_CTL 0x4C |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 64 | |
| 65 | /* control register regs array indices */ |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 66 | #define Q_REG_I_MODE_CTL 0 |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 67 | #define Q_REG_I_DIG_VIN_CTL 1 |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 68 | #define Q_REG_I_DIG_PULL_CTL 2 |
| 69 | #define Q_REG_I_DIG_IN_CTL 3 |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 70 | #define Q_REG_I_DIG_OUT_CTL 5 |
| 71 | #define Q_REG_I_EN_CTL 6 |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 72 | #define Q_REG_I_AOUT_CTL 8 |
| 73 | #define Q_REG_I_AIN_CTL 10 |
| 74 | #define Q_REG_I_SINK_CTL 12 |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 75 | |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 76 | /* control reg: mode */ |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 77 | #define Q_REG_OUT_INVERT_SHIFT 0 |
| 78 | #define Q_REG_OUT_INVERT_MASK 0x1 |
| 79 | #define Q_REG_SRC_SEL_SHIFT 1 |
| 80 | #define Q_REG_SRC_SEL_MASK 0xE |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 81 | #define Q_REG_MODE_SEL_SHIFT 4 |
| 82 | #define Q_REG_MODE_SEL_MASK 0x70 |
| 83 | |
| 84 | /* control reg: dig_vin */ |
| 85 | #define Q_REG_VIN_SHIFT 0 |
| 86 | #define Q_REG_VIN_MASK 0x7 |
| 87 | |
| 88 | /* control reg: dig_pull */ |
| 89 | #define Q_REG_PULL_SHIFT 0 |
| 90 | #define Q_REG_PULL_MASK 0x7 |
| 91 | |
| 92 | /* control reg: dig_out */ |
| 93 | #define Q_REG_OUT_STRENGTH_SHIFT 0 |
| 94 | #define Q_REG_OUT_STRENGTH_MASK 0x3 |
| 95 | #define Q_REG_OUT_TYPE_SHIFT 4 |
| 96 | #define Q_REG_OUT_TYPE_MASK 0x30 |
| 97 | |
| 98 | /* control reg: en */ |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 99 | #define Q_REG_MASTER_EN_SHIFT 7 |
| 100 | #define Q_REG_MASTER_EN_MASK 0x80 |
| 101 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 102 | /* control reg: ana_out */ |
| 103 | #define Q_REG_AOUT_REF_SHIFT 0 |
| 104 | #define Q_REG_AOUT_REF_MASK 0x7 |
| 105 | |
| 106 | /* control reg: ana_in */ |
| 107 | #define Q_REG_AIN_ROUTE_SHIFT 0 |
| 108 | #define Q_REG_AIN_ROUTE_MASK 0x7 |
| 109 | |
| 110 | /* control reg: sink */ |
| 111 | #define Q_REG_CS_OUT_SHIFT 0 |
| 112 | #define Q_REG_CS_OUT_MASK 0x7 |
| 113 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 114 | enum qpnp_pin_param_type { |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 115 | Q_PIN_CFG_MODE, |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 116 | Q_PIN_CFG_OUTPUT_TYPE, |
| 117 | Q_PIN_CFG_INVERT, |
| 118 | Q_PIN_CFG_PULL, |
| 119 | Q_PIN_CFG_VIN_SEL, |
| 120 | Q_PIN_CFG_OUT_STRENGTH, |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 121 | Q_PIN_CFG_SELECT, |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 122 | Q_PIN_CFG_MASTER_EN, |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 123 | Q_PIN_CFG_AOUT_REF, |
| 124 | Q_PIN_CFG_AIN_ROUTE, |
| 125 | Q_PIN_CFG_CS_OUT, |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 126 | Q_PIN_CFG_INVALID, |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 129 | #define Q_NUM_PARAMS Q_PIN_CFG_INVALID |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 130 | |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 131 | /* param error checking */ |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 132 | #define QPNP_PIN_MODE_INVALID 3 |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 133 | #define QPNP_PIN_INVERT_INVALID 2 |
| 134 | #define QPNP_PIN_OUT_BUF_INVALID 3 |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 135 | #define QPNP_PIN_VIN_4CH_INVALID 5 |
| 136 | #define QPNP_PIN_VIN_8CH_INVALID 8 |
| 137 | #define QPNP_PIN_GPIO_PULL_INVALID 6 |
| 138 | #define QPNP_PIN_MPP_PULL_INVALID 4 |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 139 | #define QPNP_PIN_OUT_STRENGTH_INVALID 4 |
| 140 | #define QPNP_PIN_SRC_INVALID 8 |
| 141 | #define QPNP_PIN_MASTER_INVALID 2 |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 142 | #define QPNP_PIN_AOUT_REF_INVALID 8 |
| 143 | #define QPNP_PIN_AIN_ROUTE_INVALID 8 |
| 144 | #define QPNP_PIN_CS_OUT_INVALID 8 |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 145 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 146 | struct qpnp_pin_spec { |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 147 | uint8_t slave; /* 0-15 */ |
| 148 | uint16_t offset; /* 0-255 */ |
| 149 | uint32_t gpio_chip_idx; /* offset from gpio_chip base */ |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 150 | uint32_t pmic_pin; /* PMIC pin number */ |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 151 | int irq; /* logical IRQ number */ |
| 152 | u8 regs[Q_NUM_CTL_REGS]; /* Control regs */ |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 153 | u8 num_ctl_regs; /* usable number on this pin */ |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 154 | u8 type; /* peripheral type */ |
| 155 | u8 subtype; /* peripheral subtype */ |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 156 | struct device_node *node; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 157 | enum qpnp_pin_param_type params[Q_NUM_PARAMS]; |
| 158 | struct qpnp_pin_chip *q_chip; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 161 | struct qpnp_pin_chip { |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 162 | struct gpio_chip gpio_chip; |
| 163 | struct spmi_device *spmi; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 164 | struct qpnp_pin_spec **pmic_pins; |
| 165 | struct qpnp_pin_spec **chip_gpios; |
| 166 | uint32_t pmic_pin_lowest; |
| 167 | uint32_t pmic_pin_highest; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 168 | struct device_node *int_ctrl; |
| 169 | struct list_head chip_list; |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 170 | struct dentry *dfs_dir; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 171 | }; |
| 172 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 173 | static LIST_HEAD(qpnp_pin_chips); |
| 174 | static DEFINE_MUTEX(qpnp_pin_chips_lock); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 175 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 176 | static inline void qpnp_pmic_pin_set_spec(struct qpnp_pin_chip *q_chip, |
| 177 | uint32_t pmic_pin, |
| 178 | struct qpnp_pin_spec *spec) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 179 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 180 | q_chip->pmic_pins[pmic_pin - q_chip->pmic_pin_lowest] = spec; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 183 | static inline struct qpnp_pin_spec *qpnp_pmic_pin_get_spec( |
| 184 | struct qpnp_pin_chip *q_chip, |
| 185 | uint32_t pmic_pin) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 186 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 187 | if (pmic_pin < q_chip->pmic_pin_lowest || |
| 188 | pmic_pin > q_chip->pmic_pin_highest) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 189 | return NULL; |
| 190 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 191 | return q_chip->pmic_pins[pmic_pin - q_chip->pmic_pin_lowest]; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 194 | static inline struct qpnp_pin_spec *qpnp_chip_gpio_get_spec( |
| 195 | struct qpnp_pin_chip *q_chip, |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 196 | uint32_t chip_gpio) |
| 197 | { |
| 198 | if (chip_gpio > q_chip->gpio_chip.ngpio) |
| 199 | return NULL; |
| 200 | |
| 201 | return q_chip->chip_gpios[chip_gpio]; |
| 202 | } |
| 203 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 204 | static inline void qpnp_chip_gpio_set_spec(struct qpnp_pin_chip *q_chip, |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 205 | uint32_t chip_gpio, |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 206 | struct qpnp_pin_spec *spec) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 207 | { |
| 208 | q_chip->chip_gpios[chip_gpio] = spec; |
| 209 | } |
| 210 | |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 211 | /* |
| 212 | * Determines whether a specified param's configuration is correct. |
| 213 | * This check is two tier. First a check is done whether the hardware |
| 214 | * supports this param and value requested. The second check validates |
| 215 | * that the configuration is correct, given the fact that the hardware |
| 216 | * supports it. |
| 217 | * |
| 218 | * Returns |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 219 | * -ENXIO is the hardware does not support this param. |
| 220 | * -EINVAL if the the hardware does support this param, but the |
| 221 | * requested value is outside the supported range. |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 222 | */ |
| 223 | static int qpnp_pin_check_config(enum qpnp_pin_param_type idx, |
| 224 | struct qpnp_pin_spec *q_spec, uint32_t val) |
| 225 | { |
| 226 | switch (idx) { |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 227 | case Q_PIN_CFG_MODE: |
| 228 | if (val >= QPNP_PIN_MODE_INVALID) |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 229 | return -EINVAL; |
| 230 | break; |
| 231 | case Q_PIN_CFG_OUTPUT_TYPE: |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 232 | if (q_spec->type != Q_GPIO_TYPE) |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 233 | return -ENXIO; |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 234 | if ((val == QPNP_PIN_OUT_BUF_OPEN_DRAIN_NMOS || |
| 235 | val == QPNP_PIN_OUT_BUF_OPEN_DRAIN_PMOS) && |
| 236 | (q_spec->subtype == Q_GPIO_SUBTYPE_GPIOC_4CH || |
| 237 | (q_spec->subtype == Q_GPIO_SUBTYPE_GPIOC_8CH))) |
| 238 | return -EINVAL; |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 239 | else if (val >= QPNP_PIN_OUT_BUF_INVALID) |
| 240 | return -EINVAL; |
| 241 | break; |
| 242 | case Q_PIN_CFG_INVERT: |
| 243 | if (val >= QPNP_PIN_INVERT_INVALID) |
| 244 | return -EINVAL; |
| 245 | break; |
| 246 | case Q_PIN_CFG_PULL: |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 247 | if (q_spec->type == Q_GPIO_TYPE && |
| 248 | val >= QPNP_PIN_GPIO_PULL_INVALID) |
| 249 | return -EINVAL; |
| 250 | if (q_spec->type == Q_MPP_TYPE && |
| 251 | val >= QPNP_PIN_MPP_PULL_INVALID) |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 252 | return -EINVAL; |
| 253 | break; |
| 254 | case Q_PIN_CFG_VIN_SEL: |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 255 | if (val >= QPNP_PIN_VIN_8CH_INVALID) |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 256 | return -EINVAL; |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 257 | else if (val >= QPNP_PIN_VIN_4CH_INVALID) { |
| 258 | if (q_spec->type == Q_GPIO_TYPE && |
| 259 | (q_spec->subtype == Q_GPIO_SUBTYPE_GPIO_4CH || |
| 260 | q_spec->subtype == Q_GPIO_SUBTYPE_GPIOC_4CH)) |
| 261 | return -EINVAL; |
| 262 | if (q_spec->type == Q_MPP_TYPE && |
| 263 | (q_spec->subtype == Q_MPP_SUBTYPE_4CH_NO_ANA_OUT || |
| 264 | q_spec->subtype == Q_MPP_SUBTYPE_4CH_NO_SINK || |
| 265 | q_spec->subtype == Q_MPP_SUBTYPE_4CH_FULL_FUNC)) |
| 266 | return -EINVAL; |
| 267 | } |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 268 | break; |
| 269 | case Q_PIN_CFG_OUT_STRENGTH: |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 270 | if (q_spec->type != Q_GPIO_TYPE) |
| 271 | return -ENXIO; |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 272 | if (val >= QPNP_PIN_OUT_STRENGTH_INVALID || |
| 273 | val == 0) |
| 274 | return -EINVAL; |
| 275 | break; |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 276 | case Q_PIN_CFG_SELECT: |
| 277 | if (q_spec->type == Q_MPP_TYPE && |
| 278 | (val == QPNP_PIN_SEL_FUNC_1 || |
| 279 | val == QPNP_PIN_SEL_FUNC_2)) |
| 280 | return -EINVAL; |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 281 | if (val >= QPNP_PIN_SRC_INVALID) |
| 282 | return -EINVAL; |
| 283 | break; |
| 284 | case Q_PIN_CFG_MASTER_EN: |
| 285 | if (val >= QPNP_PIN_MASTER_INVALID) |
| 286 | return -EINVAL; |
| 287 | break; |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 288 | case Q_PIN_CFG_AOUT_REF: |
| 289 | if (q_spec->type != Q_MPP_TYPE) |
| 290 | return -ENXIO; |
| 291 | if (q_spec->subtype == Q_MPP_SUBTYPE_4CH_NO_ANA_OUT) |
| 292 | return -ENXIO; |
| 293 | if (val >= QPNP_PIN_AOUT_REF_INVALID) |
| 294 | return -EINVAL; |
| 295 | break; |
| 296 | case Q_PIN_CFG_AIN_ROUTE: |
| 297 | if (q_spec->type != Q_MPP_TYPE) |
| 298 | return -ENXIO; |
| 299 | if (val >= QPNP_PIN_AIN_ROUTE_INVALID) |
| 300 | return -EINVAL; |
| 301 | break; |
| 302 | case Q_PIN_CFG_CS_OUT: |
| 303 | if (q_spec->type != Q_MPP_TYPE) |
| 304 | return -ENXIO; |
| 305 | if (q_spec->subtype == Q_MPP_SUBTYPE_4CH_NO_SINK) |
| 306 | return -ENXIO; |
| 307 | if (val >= QPNP_PIN_CS_OUT_INVALID) |
| 308 | return -EINVAL; |
| 309 | break; |
| 310 | |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 311 | default: |
| 312 | pr_err("invalid param type %u specified\n", idx); |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | #define Q_CHK_INVALID(idx, q_spec, val) \ |
| 319 | (qpnp_pin_check_config(idx, q_spec, val) == -EINVAL) |
| 320 | |
| 321 | static int qpnp_pin_check_constraints(struct qpnp_pin_spec *q_spec, |
| 322 | struct qpnp_pin_cfg *param) |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 323 | { |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 324 | int pin = q_spec->pmic_pin; |
| 325 | const char *name; |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 326 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 327 | name = (q_spec->type == Q_GPIO_TYPE) ? "gpio" : "mpp"; |
| 328 | |
| 329 | if (Q_CHK_INVALID(Q_PIN_CFG_MODE, q_spec, param->mode)) |
| 330 | pr_err("invalid direction for %s %d\n", name, pin); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 331 | else if (Q_CHK_INVALID(Q_PIN_CFG_INVERT, q_spec, param->invert)) |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 332 | pr_err("invalid invert polarity for %s %d\n", name, pin); |
| 333 | else if (Q_CHK_INVALID(Q_PIN_CFG_SELECT, q_spec, param->select)) |
| 334 | pr_err("invalid source select for %s %d\n", name, pin); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 335 | else if (Q_CHK_INVALID(Q_PIN_CFG_OUT_STRENGTH, |
| 336 | q_spec, param->out_strength)) |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 337 | pr_err("invalid out strength for %s %d\n", name, pin); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 338 | else if (Q_CHK_INVALID(Q_PIN_CFG_OUTPUT_TYPE, |
| 339 | q_spec, param->output_type)) |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 340 | pr_err("invalid out type for %s %d\n", name, pin); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 341 | else if (Q_CHK_INVALID(Q_PIN_CFG_VIN_SEL, q_spec, param->vin_sel)) |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 342 | pr_err("invalid vin select value for %s %d\n", name, pin); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 343 | else if (Q_CHK_INVALID(Q_PIN_CFG_PULL, q_spec, param->pull)) |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 344 | pr_err("invalid pull value for pin %s %d\n", name, pin); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 345 | else if (Q_CHK_INVALID(Q_PIN_CFG_MASTER_EN, q_spec, param->master_en)) |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 346 | pr_err("invalid master_en value for %s %d\n", name, pin); |
| 347 | else if (Q_CHK_INVALID(Q_PIN_CFG_AOUT_REF, q_spec, param->aout_ref)) |
| 348 | pr_err("invalid aout_reg value for %s %d\n", name, pin); |
| 349 | else if (Q_CHK_INVALID(Q_PIN_CFG_AIN_ROUTE, q_spec, param->ain_route)) |
| 350 | pr_err("invalid ain_route value for %s %d\n", name, pin); |
| 351 | else if (Q_CHK_INVALID(Q_PIN_CFG_CS_OUT, q_spec, param->cs_out)) |
| 352 | pr_err("invalid cs_out value for %s %d\n", name, pin); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 353 | else |
| 354 | return 0; |
| 355 | |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 359 | static inline u8 q_reg_get(u8 *reg, int shift, int mask) |
| 360 | { |
| 361 | return (*reg & mask) >> shift; |
| 362 | } |
| 363 | |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 364 | static inline void q_reg_set(u8 *reg, int shift, int mask, int value) |
| 365 | { |
| 366 | *reg |= (value << shift) & mask; |
| 367 | } |
| 368 | |
| 369 | static inline void q_reg_clr_set(u8 *reg, int shift, int mask, int value) |
| 370 | { |
| 371 | *reg &= ~mask; |
| 372 | *reg |= (value << shift) & mask; |
| 373 | } |
| 374 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 375 | /* |
| 376 | * Calculate the minimum number of registers that must be read / written |
| 377 | * in order to satisfy the full feature set of the given pin. |
| 378 | */ |
| 379 | static int qpnp_pin_ctl_regs_init(struct qpnp_pin_spec *q_spec) |
| 380 | { |
| 381 | if (q_spec->type == Q_GPIO_TYPE) |
| 382 | q_spec->num_ctl_regs = 7; |
| 383 | else if (q_spec->type == Q_MPP_TYPE) |
| 384 | switch (q_spec->subtype) { |
| 385 | case Q_MPP_SUBTYPE_4CH_NO_SINK: |
| 386 | q_spec->num_ctl_regs = 12; |
| 387 | break; |
| 388 | case Q_MPP_SUBTYPE_4CH_NO_ANA_OUT: |
| 389 | case Q_MPP_SUBTYPE_4CH_FULL_FUNC: |
| 390 | case Q_MPP_SUBTYPE_8CH_FULL_FUNC: |
| 391 | q_spec->num_ctl_regs = 13; |
| 392 | break; |
| 393 | default: |
| 394 | pr_err("Invalid MPP subtype 0x%x\n", q_spec->subtype); |
| 395 | return -EINVAL; |
| 396 | } |
| 397 | else { |
| 398 | pr_err("Invalid type 0x%x\n", q_spec->type); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int qpnp_pin_read_regs(struct qpnp_pin_chip *q_chip, |
| 405 | struct qpnp_pin_spec *q_spec, u16 addr, u8 *buf) |
| 406 | { |
| 407 | int bytes_left = q_spec->num_ctl_regs; |
| 408 | int rc; |
| 409 | char *reg_p = &q_spec->regs[0]; |
| 410 | |
| 411 | while (bytes_left > 0) { |
| 412 | rc = spmi_ext_register_readl(q_chip->spmi->ctrl, q_spec->slave, |
| 413 | Q_REG_ADDR(q_spec, Q_REG_MODE_CTL), |
| 414 | reg_p, bytes_left < 8 ? bytes_left : 8); |
| 415 | if (rc) |
| 416 | return rc; |
| 417 | bytes_left -= 8; |
| 418 | reg_p += 8; |
| 419 | } |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int qpnp_pin_write_regs(struct qpnp_pin_chip *q_chip, |
| 424 | struct qpnp_pin_spec *q_spec, u16 addr, u8 *buf) |
| 425 | { |
| 426 | int bytes_left = q_spec->num_ctl_regs; |
| 427 | int rc; |
| 428 | char *reg_p = &q_spec->regs[0]; |
| 429 | |
| 430 | while (bytes_left > 0) { |
| 431 | rc = spmi_ext_register_writel(q_chip->spmi->ctrl, q_spec->slave, |
| 432 | Q_REG_ADDR(q_spec, Q_REG_MODE_CTL), |
| 433 | reg_p, bytes_left < 8 ? bytes_left : 8); |
| 434 | if (rc) |
| 435 | return rc; |
| 436 | bytes_left -= 8; |
| 437 | reg_p += 8; |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 442 | static int qpnp_pin_cache_regs(struct qpnp_pin_chip *q_chip, |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 443 | struct qpnp_pin_spec *q_spec) |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 444 | { |
| 445 | int rc; |
| 446 | struct device *dev = &q_chip->spmi->dev; |
| 447 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 448 | rc = qpnp_pin_read_regs(q_chip, q_spec, |
| 449 | Q_REG_ADDR(q_spec, Q_REG_MODE_CTL), |
| 450 | &q_spec->regs[Q_REG_I_MODE_CTL]); |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 451 | if (rc) |
| 452 | dev_err(dev, "%s: unable to read control regs\n", __func__); |
| 453 | |
| 454 | return rc; |
| 455 | } |
| 456 | |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 457 | #define Q_HAVE_HW_SP(idx, q_spec, val) \ |
| 458 | (qpnp_pin_check_config(idx, q_spec, val) == 0) |
| 459 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 460 | static int _qpnp_pin_config(struct qpnp_pin_chip *q_chip, |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 461 | struct qpnp_pin_spec *q_spec, |
| 462 | struct qpnp_pin_cfg *param) |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 463 | { |
| 464 | struct device *dev = &q_chip->spmi->dev; |
| 465 | int rc; |
| 466 | |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 467 | rc = qpnp_pin_check_constraints(q_spec, param); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 468 | if (rc) |
| 469 | goto gpio_cfg; |
| 470 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 471 | /* set mode */ |
| 472 | if (Q_HAVE_HW_SP(Q_PIN_CFG_MODE, q_spec, param->mode)) |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 473 | q_reg_clr_set(&q_spec->regs[Q_REG_I_MODE_CTL], |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 474 | Q_REG_MODE_SEL_SHIFT, Q_REG_MODE_SEL_MASK, |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 475 | param->mode); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 476 | |
| 477 | /* output specific configuration */ |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 478 | if (Q_HAVE_HW_SP(Q_PIN_CFG_INVERT, q_spec, param->invert)) |
| 479 | q_reg_clr_set(&q_spec->regs[Q_REG_I_MODE_CTL], |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 480 | Q_REG_OUT_INVERT_SHIFT, Q_REG_OUT_INVERT_MASK, |
| 481 | param->invert); |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 482 | if (Q_HAVE_HW_SP(Q_PIN_CFG_SELECT, q_spec, param->select)) |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 483 | q_reg_clr_set(&q_spec->regs[Q_REG_I_MODE_CTL], |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 484 | Q_REG_SRC_SEL_SHIFT, Q_REG_SRC_SEL_MASK, |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 485 | param->select); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 486 | if (Q_HAVE_HW_SP(Q_PIN_CFG_OUT_STRENGTH, q_spec, param->out_strength)) |
| 487 | q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_OUT_CTL], |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 488 | Q_REG_OUT_STRENGTH_SHIFT, Q_REG_OUT_STRENGTH_MASK, |
| 489 | param->out_strength); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 490 | if (Q_HAVE_HW_SP(Q_PIN_CFG_OUTPUT_TYPE, q_spec, param->output_type)) |
| 491 | q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_OUT_CTL], |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 492 | Q_REG_OUT_TYPE_SHIFT, Q_REG_OUT_TYPE_MASK, |
| 493 | param->output_type); |
| 494 | |
| 495 | /* config applicable for both input / output */ |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 496 | if (Q_HAVE_HW_SP(Q_PIN_CFG_VIN_SEL, q_spec, param->vin_sel)) |
| 497 | q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_VIN_CTL], |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 498 | Q_REG_VIN_SHIFT, Q_REG_VIN_MASK, |
| 499 | param->vin_sel); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 500 | if (Q_HAVE_HW_SP(Q_PIN_CFG_PULL, q_spec, param->pull)) |
| 501 | q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_PULL_CTL], |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 502 | Q_REG_PULL_SHIFT, Q_REG_PULL_MASK, |
| 503 | param->pull); |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 504 | if (Q_HAVE_HW_SP(Q_PIN_CFG_MASTER_EN, q_spec, param->master_en)) |
| 505 | q_reg_clr_set(&q_spec->regs[Q_REG_I_EN_CTL], |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 506 | Q_REG_MASTER_EN_SHIFT, Q_REG_MASTER_EN_MASK, |
| 507 | param->master_en); |
| 508 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 509 | /* mpp specific config */ |
| 510 | if (Q_HAVE_HW_SP(Q_PIN_CFG_AOUT_REF, q_spec, param->aout_ref)) |
| 511 | q_reg_clr_set(&q_spec->regs[Q_REG_I_AOUT_CTL], |
| 512 | Q_REG_AOUT_REF_SHIFT, Q_REG_AOUT_REF_MASK, |
| 513 | param->aout_ref); |
| 514 | if (Q_HAVE_HW_SP(Q_PIN_CFG_AIN_ROUTE, q_spec, param->ain_route)) |
| 515 | q_reg_clr_set(&q_spec->regs[Q_REG_I_AIN_CTL], |
| 516 | Q_REG_AIN_ROUTE_SHIFT, Q_REG_AIN_ROUTE_MASK, |
| 517 | param->ain_route); |
| 518 | if (Q_HAVE_HW_SP(Q_PIN_CFG_CS_OUT, q_spec, param->cs_out)) |
| 519 | q_reg_clr_set(&q_spec->regs[Q_REG_I_SINK_CTL], |
| 520 | Q_REG_CS_OUT_SHIFT, Q_REG_CS_OUT_MASK, |
| 521 | param->cs_out); |
| 522 | |
| 523 | rc = qpnp_pin_write_regs(q_chip, q_spec, |
| 524 | Q_REG_ADDR(q_spec, Q_REG_MODE_CTL), |
| 525 | &q_spec->regs[Q_REG_I_MODE_CTL]); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 526 | if (rc) { |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 527 | dev_err(&q_chip->spmi->dev, "%s: unable to write master enable\n", |
| 528 | __func__); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 529 | goto gpio_cfg; |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | |
| 534 | gpio_cfg: |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 535 | dev_err(dev, "%s: unable to set default config for pmic gpio %d\n", |
| 536 | __func__, q_spec->pmic_pin); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 537 | |
| 538 | return rc; |
| 539 | } |
| 540 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 541 | int qpnp_pin_config(int gpio, struct qpnp_pin_cfg *param) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 542 | { |
| 543 | int rc, chip_offset; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 544 | struct qpnp_pin_chip *q_chip; |
| 545 | struct qpnp_pin_spec *q_spec = NULL; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 546 | struct gpio_chip *gpio_chip; |
| 547 | |
| 548 | if (param == NULL) |
| 549 | return -EINVAL; |
| 550 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 551 | mutex_lock(&qpnp_pin_chips_lock); |
| 552 | list_for_each_entry(q_chip, &qpnp_pin_chips, chip_list) { |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 553 | gpio_chip = &q_chip->gpio_chip; |
| 554 | if (gpio >= gpio_chip->base |
| 555 | && gpio < gpio_chip->base + gpio_chip->ngpio) { |
| 556 | chip_offset = gpio - gpio_chip->base; |
| 557 | q_spec = qpnp_chip_gpio_get_spec(q_chip, chip_offset); |
| 558 | if (WARN_ON(!q_spec)) { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 559 | mutex_unlock(&qpnp_pin_chips_lock); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 560 | return -ENODEV; |
| 561 | } |
| 562 | break; |
| 563 | } |
| 564 | } |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 565 | mutex_unlock(&qpnp_pin_chips_lock); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 566 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 567 | rc = _qpnp_pin_config(q_chip, q_spec, param); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 568 | |
| 569 | return rc; |
| 570 | } |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 571 | EXPORT_SYMBOL(qpnp_pin_config); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 572 | |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 573 | #define Q_MAX_CHIP_NAME 128 |
| 574 | int qpnp_pin_map(const char *name, uint32_t pmic_pin) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 575 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 576 | struct qpnp_pin_chip *q_chip; |
| 577 | struct qpnp_pin_spec *q_spec = NULL; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 578 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 579 | mutex_lock(&qpnp_pin_chips_lock); |
| 580 | list_for_each_entry(q_chip, &qpnp_pin_chips, chip_list) { |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 581 | if (strncmp(q_chip->gpio_chip.label, name, |
| 582 | Q_MAX_CHIP_NAME) != 0) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 583 | continue; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 584 | if (q_chip->pmic_pin_lowest <= pmic_pin && |
| 585 | q_chip->pmic_pin_highest >= pmic_pin) { |
| 586 | q_spec = qpnp_pmic_pin_get_spec(q_chip, pmic_pin); |
| 587 | mutex_unlock(&qpnp_pin_chips_lock); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 588 | if (WARN_ON(!q_spec)) |
| 589 | return -ENODEV; |
| 590 | return q_chip->gpio_chip.base + q_spec->gpio_chip_idx; |
| 591 | } |
| 592 | } |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 593 | mutex_unlock(&qpnp_pin_chips_lock); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 594 | return -EINVAL; |
| 595 | } |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 596 | EXPORT_SYMBOL(qpnp_pin_map); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 597 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 598 | static int qpnp_pin_to_irq(struct gpio_chip *gpio_chip, unsigned offset) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 599 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 600 | struct qpnp_pin_chip *q_chip = dev_get_drvdata(gpio_chip->dev); |
| 601 | struct qpnp_pin_spec *q_spec; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 602 | |
| 603 | q_spec = qpnp_chip_gpio_get_spec(q_chip, offset); |
| 604 | if (!q_spec) |
| 605 | return -EINVAL; |
| 606 | |
| 607 | return q_spec->irq; |
| 608 | } |
| 609 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 610 | static int qpnp_pin_get(struct gpio_chip *gpio_chip, unsigned offset) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 611 | { |
| 612 | int rc, ret_val; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 613 | struct qpnp_pin_chip *q_chip = dev_get_drvdata(gpio_chip->dev); |
| 614 | struct qpnp_pin_spec *q_spec = NULL; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 615 | u8 buf[1]; |
| 616 | |
| 617 | if (WARN_ON(!q_chip)) |
| 618 | return -ENODEV; |
| 619 | |
| 620 | q_spec = qpnp_chip_gpio_get_spec(q_chip, offset); |
| 621 | if (WARN_ON(!q_spec)) |
| 622 | return -ENODEV; |
| 623 | |
| 624 | /* gpio val is from RT status iff input is enabled */ |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 625 | if ((q_spec->regs[Q_REG_I_MODE_CTL] & Q_REG_MODE_SEL_MASK) |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 626 | == QPNP_PIN_MODE_DIG_IN) { |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 627 | /* INT_RT_STS */ |
| 628 | rc = spmi_ext_register_readl(q_chip->spmi->ctrl, q_spec->slave, |
| 629 | Q_REG_ADDR(q_spec, Q_REG_STATUS1), |
| 630 | &buf[0], 1); |
| 631 | return buf[0]; |
| 632 | |
| 633 | } else { |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 634 | ret_val = (q_spec->regs[Q_REG_I_MODE_CTL] & |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 635 | Q_REG_OUT_INVERT_MASK) >> Q_REG_OUT_INVERT_SHIFT; |
| 636 | return ret_val; |
| 637 | } |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 642 | static int __qpnp_pin_set(struct qpnp_pin_chip *q_chip, |
| 643 | struct qpnp_pin_spec *q_spec, int value) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 644 | { |
| 645 | int rc; |
| 646 | |
| 647 | if (!q_chip || !q_spec) |
| 648 | return -EINVAL; |
| 649 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 650 | if (value) |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 651 | q_reg_clr_set(&q_spec->regs[Q_REG_I_MODE_CTL], |
| 652 | Q_REG_OUT_INVERT_SHIFT, Q_REG_OUT_INVERT_MASK, 1); |
| 653 | else |
| 654 | q_reg_clr_set(&q_spec->regs[Q_REG_I_MODE_CTL], |
| 655 | Q_REG_OUT_INVERT_SHIFT, Q_REG_OUT_INVERT_MASK, 0); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 656 | |
| 657 | rc = spmi_ext_register_writel(q_chip->spmi->ctrl, q_spec->slave, |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 658 | Q_REG_ADDR(q_spec, Q_REG_I_MODE_CTL), |
| 659 | &q_spec->regs[Q_REG_I_MODE_CTL], 1); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 660 | if (rc) |
| 661 | dev_err(&q_chip->spmi->dev, "%s: spmi write failed\n", |
| 662 | __func__); |
| 663 | return rc; |
| 664 | } |
| 665 | |
| 666 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 667 | static void qpnp_pin_set(struct gpio_chip *gpio_chip, |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 668 | unsigned offset, int value) |
| 669 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 670 | struct qpnp_pin_chip *q_chip = dev_get_drvdata(gpio_chip->dev); |
| 671 | struct qpnp_pin_spec *q_spec; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 672 | |
| 673 | if (WARN_ON(!q_chip)) |
| 674 | return; |
| 675 | |
| 676 | q_spec = qpnp_chip_gpio_get_spec(q_chip, offset); |
| 677 | if (WARN_ON(!q_spec)) |
| 678 | return; |
| 679 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 680 | __qpnp_pin_set(q_chip, q_spec, value); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 683 | static int qpnp_pin_set_mode(struct qpnp_pin_chip *q_chip, |
| 684 | struct qpnp_pin_spec *q_spec, int mode) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 685 | { |
| 686 | int rc; |
| 687 | |
| 688 | if (!q_chip || !q_spec) |
| 689 | return -EINVAL; |
| 690 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 691 | if (mode >= QPNP_PIN_MODE_INVALID) { |
| 692 | pr_err("invalid mode specification %d\n", mode); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 693 | return -EINVAL; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 694 | } |
| 695 | |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 696 | q_reg_clr_set(&q_spec->regs[Q_REG_I_MODE_CTL], |
| 697 | Q_REG_MODE_SEL_SHIFT, |
| 698 | Q_REG_MODE_SEL_MASK, |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 699 | mode); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 700 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 701 | rc = spmi_ext_register_writel(q_chip->spmi->ctrl, q_spec->slave, |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 702 | Q_REG_ADDR(q_spec, Q_REG_I_MODE_CTL), |
| 703 | &q_spec->regs[Q_REG_I_MODE_CTL], 1); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 704 | return rc; |
| 705 | } |
| 706 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 707 | static int qpnp_pin_direction_input(struct gpio_chip *gpio_chip, |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 708 | unsigned offset) |
| 709 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 710 | struct qpnp_pin_chip *q_chip = dev_get_drvdata(gpio_chip->dev); |
| 711 | struct qpnp_pin_spec *q_spec; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 712 | |
| 713 | if (WARN_ON(!q_chip)) |
| 714 | return -ENODEV; |
| 715 | |
| 716 | q_spec = qpnp_chip_gpio_get_spec(q_chip, offset); |
| 717 | if (WARN_ON(!q_spec)) |
| 718 | return -ENODEV; |
| 719 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 720 | return qpnp_pin_set_mode(q_chip, q_spec, QPNP_PIN_MODE_DIG_IN); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 723 | static int qpnp_pin_direction_output(struct gpio_chip *gpio_chip, |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 724 | unsigned offset, |
| 725 | int val) |
| 726 | { |
| 727 | int rc; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 728 | struct qpnp_pin_chip *q_chip = dev_get_drvdata(gpio_chip->dev); |
| 729 | struct qpnp_pin_spec *q_spec; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 730 | |
| 731 | if (WARN_ON(!q_chip)) |
| 732 | return -ENODEV; |
| 733 | |
| 734 | q_spec = qpnp_chip_gpio_get_spec(q_chip, offset); |
| 735 | if (WARN_ON(!q_spec)) |
| 736 | return -ENODEV; |
| 737 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 738 | rc = __qpnp_pin_set(q_chip, q_spec, val); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 739 | if (rc) |
| 740 | return rc; |
| 741 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 742 | rc = qpnp_pin_set_mode(q_chip, q_spec, QPNP_PIN_MODE_DIG_OUT); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 743 | |
| 744 | return rc; |
| 745 | } |
| 746 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 747 | static int qpnp_pin_of_gpio_xlate(struct gpio_chip *gpio_chip, |
Michael Bohan | 0b24fb1 | 2012-06-01 10:30:12 -0700 | [diff] [blame] | 748 | const struct of_phandle_args *gpio_spec, |
| 749 | u32 *flags) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 750 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 751 | struct qpnp_pin_chip *q_chip = dev_get_drvdata(gpio_chip->dev); |
| 752 | struct qpnp_pin_spec *q_spec; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 753 | |
| 754 | if (WARN_ON(gpio_chip->of_gpio_n_cells < 2)) { |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 755 | pr_err("of_gpio_n_cells < 2\n"); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 756 | return -EINVAL; |
| 757 | } |
| 758 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 759 | q_spec = qpnp_pmic_pin_get_spec(q_chip, gpio_spec->args[0]); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 760 | if (!q_spec) { |
Michael Bohan | 0b24fb1 | 2012-06-01 10:30:12 -0700 | [diff] [blame] | 761 | pr_err("no such PMIC gpio %u in device topology\n", |
| 762 | gpio_spec->args[0]); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 763 | return -EINVAL; |
| 764 | } |
| 765 | |
| 766 | if (flags) |
Michael Bohan | 0b24fb1 | 2012-06-01 10:30:12 -0700 | [diff] [blame] | 767 | *flags = gpio_spec->args[1]; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 768 | |
| 769 | return q_spec->gpio_chip_idx; |
| 770 | } |
| 771 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 772 | static int qpnp_pin_apply_config(struct qpnp_pin_chip *q_chip, |
| 773 | struct qpnp_pin_spec *q_spec) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 774 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 775 | struct qpnp_pin_cfg param; |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 776 | struct device_node *node = q_spec->node; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 777 | int rc; |
| 778 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 779 | param.mode = q_reg_get(&q_spec->regs[Q_REG_I_MODE_CTL], |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 780 | Q_REG_MODE_SEL_SHIFT, |
| 781 | Q_REG_MODE_SEL_MASK); |
| 782 | param.output_type = q_reg_get(&q_spec->regs[Q_REG_I_DIG_OUT_CTL], |
| 783 | Q_REG_OUT_TYPE_SHIFT, |
| 784 | Q_REG_OUT_TYPE_MASK); |
| 785 | param.invert = q_reg_get(&q_spec->regs[Q_REG_I_MODE_CTL], |
| 786 | Q_REG_OUT_INVERT_MASK, |
| 787 | Q_REG_OUT_INVERT_MASK); |
| 788 | param.pull = q_reg_get(&q_spec->regs[Q_REG_I_MODE_CTL], |
| 789 | Q_REG_PULL_SHIFT, Q_REG_PULL_MASK); |
| 790 | param.vin_sel = q_reg_get(&q_spec->regs[Q_REG_I_DIG_VIN_CTL], |
| 791 | Q_REG_VIN_SHIFT, Q_REG_VIN_MASK); |
| 792 | param.out_strength = q_reg_get(&q_spec->regs[Q_REG_I_DIG_OUT_CTL], |
| 793 | Q_REG_OUT_STRENGTH_SHIFT, |
| 794 | Q_REG_OUT_STRENGTH_MASK); |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 795 | param.select = q_reg_get(&q_spec->regs[Q_REG_I_MODE_CTL], |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 796 | Q_REG_SRC_SEL_SHIFT, Q_REG_SRC_SEL_MASK); |
| 797 | param.master_en = q_reg_get(&q_spec->regs[Q_REG_I_EN_CTL], |
| 798 | Q_REG_MASTER_EN_SHIFT, |
| 799 | Q_REG_MASTER_EN_MASK); |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 800 | param.aout_ref = q_reg_get(&q_spec->regs[Q_REG_I_AOUT_CTL], |
| 801 | Q_REG_AOUT_REF_SHIFT, |
| 802 | Q_REG_AOUT_REF_MASK); |
| 803 | param.ain_route = q_reg_get(&q_spec->regs[Q_REG_I_AIN_CTL], |
| 804 | Q_REG_AIN_ROUTE_SHIFT, |
| 805 | Q_REG_AIN_ROUTE_MASK); |
| 806 | param.cs_out = q_reg_get(&q_spec->regs[Q_REG_I_SINK_CTL], |
| 807 | Q_REG_CS_OUT_SHIFT, |
| 808 | Q_REG_CS_OUT_MASK); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 809 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 810 | of_property_read_u32(node, "qcom,mode", |
| 811 | ¶m.mode); |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 812 | of_property_read_u32(node, "qcom,output-type", |
| 813 | ¶m.output_type); |
| 814 | of_property_read_u32(node, "qcom,invert", |
| 815 | ¶m.invert); |
| 816 | of_property_read_u32(node, "qcom,pull", |
| 817 | ¶m.pull); |
| 818 | of_property_read_u32(node, "qcom,vin-sel", |
| 819 | ¶m.vin_sel); |
| 820 | of_property_read_u32(node, "qcom,out-strength", |
| 821 | ¶m.out_strength); |
| 822 | of_property_read_u32(node, "qcom,src-select", |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 823 | ¶m.select); |
| 824 | of_property_read_u32(node, "qcom,master-en", |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 825 | ¶m.master_en); |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 826 | of_property_read_u32(node, "qcom,aout-ref", |
| 827 | ¶m.aout_ref); |
| 828 | of_property_read_u32(node, "qcom,ain-route", |
| 829 | ¶m.ain_route); |
| 830 | of_property_read_u32(node, "qcom,cs-out", |
| 831 | ¶m.cs_out); |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 832 | rc = _qpnp_pin_config(q_chip, q_spec, ¶m); |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 833 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 834 | return rc; |
| 835 | } |
| 836 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 837 | static int qpnp_pin_free_chip(struct qpnp_pin_chip *q_chip) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 838 | { |
| 839 | struct spmi_device *spmi = q_chip->spmi; |
| 840 | int rc, i; |
| 841 | |
| 842 | if (q_chip->chip_gpios) |
| 843 | for (i = 0; i < spmi->num_dev_node; i++) |
| 844 | kfree(q_chip->chip_gpios[i]); |
| 845 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 846 | mutex_lock(&qpnp_pin_chips_lock); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 847 | list_del(&q_chip->chip_list); |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 848 | mutex_unlock(&qpnp_pin_chips_lock); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 849 | rc = gpiochip_remove(&q_chip->gpio_chip); |
| 850 | if (rc) |
| 851 | dev_err(&q_chip->spmi->dev, "%s: unable to remove gpio\n", |
| 852 | __func__); |
| 853 | kfree(q_chip->chip_gpios); |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 854 | kfree(q_chip->pmic_pins); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 855 | kfree(q_chip); |
| 856 | return rc; |
| 857 | } |
| 858 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 859 | #ifdef CONFIG_GPIO_QPNP_PIN_DEBUG |
| 860 | struct qpnp_pin_reg { |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 861 | uint32_t addr; |
| 862 | uint32_t idx; |
| 863 | uint32_t shift; |
| 864 | uint32_t mask; |
| 865 | }; |
| 866 | |
| 867 | static struct dentry *driver_dfs_dir; |
| 868 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 869 | static int qpnp_pin_reg_attr(enum qpnp_pin_param_type type, |
| 870 | struct qpnp_pin_reg *cfg) |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 871 | { |
| 872 | switch (type) { |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 873 | case Q_PIN_CFG_MODE: |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 874 | cfg->addr = Q_REG_MODE_CTL; |
| 875 | cfg->idx = Q_REG_I_MODE_CTL; |
| 876 | cfg->shift = Q_REG_MODE_SEL_SHIFT; |
| 877 | cfg->mask = Q_REG_MODE_SEL_MASK; |
| 878 | break; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 879 | case Q_PIN_CFG_OUTPUT_TYPE: |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 880 | cfg->addr = Q_REG_DIG_OUT_CTL; |
| 881 | cfg->idx = Q_REG_I_DIG_OUT_CTL; |
| 882 | cfg->shift = Q_REG_OUT_TYPE_SHIFT; |
| 883 | cfg->mask = Q_REG_OUT_TYPE_MASK; |
| 884 | break; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 885 | case Q_PIN_CFG_INVERT: |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 886 | cfg->addr = Q_REG_MODE_CTL; |
| 887 | cfg->idx = Q_REG_I_MODE_CTL; |
| 888 | cfg->shift = Q_REG_OUT_INVERT_SHIFT; |
| 889 | cfg->mask = Q_REG_OUT_INVERT_MASK; |
| 890 | break; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 891 | case Q_PIN_CFG_PULL: |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 892 | cfg->addr = Q_REG_DIG_PULL_CTL; |
| 893 | cfg->idx = Q_REG_I_DIG_PULL_CTL; |
| 894 | cfg->shift = Q_REG_PULL_SHIFT; |
| 895 | cfg->mask = Q_REG_PULL_MASK; |
| 896 | break; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 897 | case Q_PIN_CFG_VIN_SEL: |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 898 | cfg->addr = Q_REG_DIG_VIN_CTL; |
| 899 | cfg->idx = Q_REG_I_DIG_VIN_CTL; |
| 900 | cfg->shift = Q_REG_VIN_SHIFT; |
| 901 | cfg->mask = Q_REG_VIN_MASK; |
| 902 | break; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 903 | case Q_PIN_CFG_OUT_STRENGTH: |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 904 | cfg->addr = Q_REG_DIG_OUT_CTL; |
| 905 | cfg->idx = Q_REG_I_DIG_OUT_CTL; |
| 906 | cfg->shift = Q_REG_OUT_STRENGTH_SHIFT; |
| 907 | cfg->mask = Q_REG_OUT_STRENGTH_MASK; |
| 908 | break; |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 909 | case Q_PIN_CFG_SELECT: |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 910 | cfg->addr = Q_REG_MODE_CTL; |
| 911 | cfg->idx = Q_REG_I_MODE_CTL; |
| 912 | cfg->shift = Q_REG_SRC_SEL_SHIFT; |
| 913 | cfg->mask = Q_REG_SRC_SEL_MASK; |
| 914 | break; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 915 | case Q_PIN_CFG_MASTER_EN: |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 916 | cfg->addr = Q_REG_EN_CTL; |
| 917 | cfg->idx = Q_REG_I_EN_CTL; |
| 918 | cfg->shift = Q_REG_MASTER_EN_SHIFT; |
| 919 | cfg->mask = Q_REG_MASTER_EN_MASK; |
| 920 | break; |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 921 | case Q_PIN_CFG_AOUT_REF: |
| 922 | cfg->addr = Q_REG_AOUT_CTL; |
| 923 | cfg->idx = Q_REG_I_AOUT_CTL; |
| 924 | cfg->shift = Q_REG_AOUT_REF_SHIFT; |
| 925 | cfg->mask = Q_REG_AOUT_REF_MASK; |
| 926 | break; |
| 927 | case Q_PIN_CFG_AIN_ROUTE: |
| 928 | cfg->addr = Q_REG_AIN_CTL; |
| 929 | cfg->idx = Q_REG_I_AIN_CTL; |
| 930 | cfg->shift = Q_REG_AIN_ROUTE_SHIFT; |
| 931 | cfg->mask = Q_REG_AIN_ROUTE_MASK; |
| 932 | break; |
| 933 | case Q_PIN_CFG_CS_OUT: |
| 934 | cfg->addr = Q_REG_SINK_CTL; |
| 935 | cfg->idx = Q_REG_I_SINK_CTL; |
| 936 | cfg->shift = Q_REG_CS_OUT_SHIFT; |
| 937 | cfg->mask = Q_REG_CS_OUT_MASK; |
| 938 | break; |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 939 | default: |
| 940 | return -EINVAL; |
| 941 | } |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 946 | static int qpnp_pin_debugfs_get(void *data, u64 *val) |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 947 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 948 | enum qpnp_pin_param_type *idx = data; |
| 949 | struct qpnp_pin_spec *q_spec; |
| 950 | struct qpnp_pin_reg cfg = {}; |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 951 | int rc; |
| 952 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 953 | rc = qpnp_pin_reg_attr(*idx, &cfg); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 954 | if (rc) |
| 955 | return rc; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 956 | q_spec = container_of(idx, struct qpnp_pin_spec, params[*idx]); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 957 | *val = q_reg_get(&q_spec->regs[cfg.idx], cfg.shift, cfg.mask); |
| 958 | return 0; |
| 959 | } |
| 960 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 961 | static int qpnp_pin_debugfs_set(void *data, u64 val) |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 962 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 963 | enum qpnp_pin_param_type *idx = data; |
| 964 | struct qpnp_pin_spec *q_spec; |
| 965 | struct qpnp_pin_chip *q_chip; |
| 966 | struct qpnp_pin_reg cfg = {}; |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 967 | int rc; |
| 968 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 969 | q_spec = container_of(idx, struct qpnp_pin_spec, params[*idx]); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 970 | q_chip = q_spec->q_chip; |
| 971 | |
Michael Bohan | 58d0780 | 2012-05-31 15:37:55 -0700 | [diff] [blame] | 972 | rc = qpnp_pin_check_config(*idx, q_spec, val); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 973 | if (rc) |
| 974 | return rc; |
| 975 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 976 | rc = qpnp_pin_reg_attr(*idx, &cfg); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 977 | if (rc) |
| 978 | return rc; |
| 979 | q_reg_clr_set(&q_spec->regs[cfg.idx], cfg.shift, cfg.mask, val); |
| 980 | rc = spmi_ext_register_writel(q_chip->spmi->ctrl, q_spec->slave, |
| 981 | Q_REG_ADDR(q_spec, cfg.addr), |
| 982 | &q_spec->regs[cfg.idx], 1); |
| 983 | |
| 984 | return rc; |
| 985 | } |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 986 | DEFINE_SIMPLE_ATTRIBUTE(qpnp_pin_fops, qpnp_pin_debugfs_get, |
| 987 | qpnp_pin_debugfs_set, "%llu\n"); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 988 | |
| 989 | #define DEBUGFS_BUF_SIZE 11 /* supports 2^32 in decimal */ |
| 990 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 991 | struct qpnp_pin_debugfs_args { |
| 992 | enum qpnp_pin_param_type type; |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 993 | const char *filename; |
| 994 | }; |
| 995 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 996 | static struct qpnp_pin_debugfs_args dfs_args[] = { |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 997 | { Q_PIN_CFG_MODE, "mode" }, |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 998 | { Q_PIN_CFG_OUTPUT_TYPE, "output_type" }, |
| 999 | { Q_PIN_CFG_INVERT, "invert" }, |
| 1000 | { Q_PIN_CFG_PULL, "pull" }, |
| 1001 | { Q_PIN_CFG_VIN_SEL, "vin_sel" }, |
| 1002 | { Q_PIN_CFG_OUT_STRENGTH, "out_strength" }, |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 1003 | { Q_PIN_CFG_SELECT, "select" }, |
| 1004 | { Q_PIN_CFG_MASTER_EN, "master_en" }, |
| 1005 | { Q_PIN_CFG_AOUT_REF, "aout_ref" }, |
| 1006 | { Q_PIN_CFG_AIN_ROUTE, "ain_route" }, |
| 1007 | { Q_PIN_CFG_CS_OUT, "cs_out" }, |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1008 | }; |
| 1009 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1010 | static int qpnp_pin_debugfs_create(struct qpnp_pin_chip *q_chip) |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1011 | { |
| 1012 | struct spmi_device *spmi = q_chip->spmi; |
| 1013 | struct device *dev = &spmi->dev; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1014 | struct qpnp_pin_spec *q_spec; |
| 1015 | enum qpnp_pin_param_type *params; |
| 1016 | enum qpnp_pin_param_type type; |
| 1017 | char pmic_pin[DEBUGFS_BUF_SIZE]; |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1018 | const char *filename; |
| 1019 | struct dentry *dfs, *dfs_io_dir; |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 1020 | int i, j, rc; |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1021 | |
| 1022 | BUG_ON(Q_NUM_PARAMS != ARRAY_SIZE(dfs_args)); |
| 1023 | |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 1024 | q_chip->dfs_dir = debugfs_create_dir(q_chip->gpio_chip.label, |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1025 | driver_dfs_dir); |
| 1026 | if (q_chip->dfs_dir == NULL) { |
| 1027 | dev_err(dev, "%s: cannot register chip debugfs directory %s\n", |
| 1028 | __func__, dev->of_node->name); |
| 1029 | return -ENODEV; |
| 1030 | } |
| 1031 | |
| 1032 | for (i = 0; i < spmi->num_dev_node; i++) { |
| 1033 | q_spec = qpnp_chip_gpio_get_spec(q_chip, i); |
| 1034 | params = q_spec->params; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1035 | snprintf(pmic_pin, DEBUGFS_BUF_SIZE, "%u", q_spec->pmic_pin); |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 1036 | dfs_io_dir = debugfs_create_dir(pmic_pin, q_chip->dfs_dir); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1037 | if (dfs_io_dir == NULL) |
| 1038 | goto dfs_err; |
| 1039 | |
| 1040 | for (j = 0; j < Q_NUM_PARAMS; j++) { |
| 1041 | type = dfs_args[j].type; |
| 1042 | filename = dfs_args[j].filename; |
| 1043 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 1044 | /* |
| 1045 | * Use a value of '0' to see if the pin has even basic |
| 1046 | * support for a function. Do not create a file if |
| 1047 | * it doesn't. |
| 1048 | */ |
| 1049 | rc = qpnp_pin_check_config(type, q_spec, 0); |
| 1050 | if (rc == -ENXIO) |
| 1051 | continue; |
| 1052 | |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1053 | params[type] = type; |
| 1054 | dfs = debugfs_create_file( |
| 1055 | filename, |
| 1056 | S_IRUGO | S_IWUSR, |
| 1057 | dfs_io_dir, |
| 1058 | &q_spec->params[type], |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1059 | &qpnp_pin_fops); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1060 | if (dfs == NULL) |
| 1061 | goto dfs_err; |
| 1062 | } |
| 1063 | } |
| 1064 | return 0; |
| 1065 | dfs_err: |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 1066 | dev_err(dev, "%s: cannot register debugfs for pmic gpio %u on chip %s\n", |
| 1067 | __func__, q_spec->pmic_pin, dev->of_node->name); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1068 | debugfs_remove_recursive(q_chip->dfs_dir); |
| 1069 | return -ENFILE; |
| 1070 | } |
| 1071 | #else |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1072 | static int qpnp_pin_debugfs_create(struct qpnp_pin_chip *q_chip) |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1073 | { |
| 1074 | return 0; |
| 1075 | } |
| 1076 | #endif |
| 1077 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1078 | static int qpnp_pin_probe(struct spmi_device *spmi) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1079 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1080 | struct qpnp_pin_chip *q_chip; |
| 1081 | struct qpnp_pin_spec *q_spec; |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 1082 | struct resource *res; |
| 1083 | struct spmi_resource *d_node; |
Michael Bohan | 94e397b | 2012-04-25 15:21:55 -0700 | [diff] [blame] | 1084 | int i, rc; |
| 1085 | int lowest_gpio = UINT_MAX, highest_gpio = 0; |
| 1086 | u32 intspec[3], gpio; |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 1087 | char buf[2]; |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 1088 | const char *dev_name; |
| 1089 | |
| 1090 | dev_name = spmi_get_primary_dev_name(spmi); |
| 1091 | if (!dev_name) { |
| 1092 | dev_err(&spmi->dev, "%s: label binding undefined for node %s\n", |
| 1093 | __func__, spmi->dev.of_node->full_name); |
| 1094 | return -EINVAL; |
| 1095 | } |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1096 | |
| 1097 | q_chip = kzalloc(sizeof(*q_chip), GFP_KERNEL); |
| 1098 | if (!q_chip) { |
| 1099 | dev_err(&spmi->dev, "%s: Can't allocate gpio_chip\n", |
| 1100 | __func__); |
| 1101 | return -ENOMEM; |
| 1102 | } |
| 1103 | q_chip->spmi = spmi; |
| 1104 | dev_set_drvdata(&spmi->dev, q_chip); |
| 1105 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1106 | mutex_lock(&qpnp_pin_chips_lock); |
| 1107 | list_add(&q_chip->chip_list, &qpnp_pin_chips); |
| 1108 | mutex_unlock(&qpnp_pin_chips_lock); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1109 | |
| 1110 | /* first scan through nodes to find the range required for allocation */ |
| 1111 | for (i = 0; i < spmi->num_dev_node; i++) { |
Michael Bohan | 94e397b | 2012-04-25 15:21:55 -0700 | [diff] [blame] | 1112 | rc = of_property_read_u32(spmi->dev_node[i].of_node, |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1113 | "qcom,pin-num", &gpio); |
Michael Bohan | 94e397b | 2012-04-25 15:21:55 -0700 | [diff] [blame] | 1114 | if (rc) { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1115 | dev_err(&spmi->dev, "%s: unable to get qcom,pin-num property\n", |
| 1116 | __func__); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1117 | goto err_probe; |
| 1118 | } |
| 1119 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1120 | if (gpio < lowest_gpio) |
| 1121 | lowest_gpio = gpio; |
| 1122 | if (gpio > highest_gpio) |
| 1123 | highest_gpio = gpio; |
| 1124 | } |
| 1125 | |
| 1126 | if (highest_gpio < lowest_gpio) { |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 1127 | dev_err(&spmi->dev, "%s: no device nodes specified in topology\n", |
| 1128 | __func__); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1129 | rc = -EINVAL; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1130 | goto err_probe; |
| 1131 | } else if (lowest_gpio == 0) { |
| 1132 | dev_err(&spmi->dev, "%s: 0 is not a valid PMIC GPIO\n", |
| 1133 | __func__); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1134 | rc = -EINVAL; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1135 | goto err_probe; |
| 1136 | } |
| 1137 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1138 | q_chip->pmic_pin_lowest = lowest_gpio; |
| 1139 | q_chip->pmic_pin_highest = highest_gpio; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1140 | |
| 1141 | /* allocate gpio lookup tables */ |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1142 | q_chip->pmic_pins = kzalloc(sizeof(struct qpnp_pin_spec *) * |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1143 | highest_gpio - lowest_gpio + 1, |
| 1144 | GFP_KERNEL); |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1145 | q_chip->chip_gpios = kzalloc(sizeof(struct qpnp_pin_spec *) * |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1146 | spmi->num_dev_node, GFP_KERNEL); |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1147 | if (!q_chip->pmic_pins || !q_chip->chip_gpios) { |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1148 | dev_err(&spmi->dev, "%s: unable to allocate memory\n", |
| 1149 | __func__); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1150 | rc = -ENOMEM; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1151 | goto err_probe; |
| 1152 | } |
| 1153 | |
| 1154 | /* get interrupt controller device_node */ |
| 1155 | q_chip->int_ctrl = of_irq_find_parent(spmi->dev.of_node); |
| 1156 | if (!q_chip->int_ctrl) { |
| 1157 | dev_err(&spmi->dev, "%s: Can't find interrupt parent\n", |
| 1158 | __func__); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1159 | rc = -EINVAL; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1160 | goto err_probe; |
| 1161 | } |
| 1162 | |
| 1163 | /* now scan through again and populate the lookup table */ |
| 1164 | for (i = 0; i < spmi->num_dev_node; i++) { |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 1165 | d_node = &spmi->dev_node[i]; |
| 1166 | res = spmi_get_resource(spmi, d_node, IORESOURCE_MEM, 0); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1167 | if (!res) { |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 1168 | dev_err(&spmi->dev, "%s: node %s is missing has no base address definition\n", |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 1169 | __func__, d_node->of_node->full_name); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 1172 | rc = of_property_read_u32(d_node->of_node, |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1173 | "qcom,pin-num", &gpio); |
Michael Bohan | 94e397b | 2012-04-25 15:21:55 -0700 | [diff] [blame] | 1174 | if (rc) { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1175 | dev_err(&spmi->dev, "%s: unable to get qcom,pin-num property\n", |
| 1176 | __func__); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1177 | goto err_probe; |
| 1178 | } |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1179 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1180 | q_spec = kzalloc(sizeof(struct qpnp_pin_spec), |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1181 | GFP_KERNEL); |
| 1182 | if (!q_spec) { |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 1183 | dev_err(&spmi->dev, "%s: unable to allocate memory\n", |
| 1184 | __func__); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1185 | rc = -ENOMEM; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1186 | goto err_probe; |
| 1187 | } |
| 1188 | |
| 1189 | q_spec->slave = spmi->sid; |
| 1190 | q_spec->offset = res->start; |
| 1191 | q_spec->gpio_chip_idx = i; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1192 | q_spec->pmic_pin = gpio; |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 1193 | q_spec->node = d_node->of_node; |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1194 | q_spec->q_chip = q_chip; |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 1195 | |
| 1196 | rc = spmi_ext_register_readl(spmi->ctrl, q_spec->slave, |
| 1197 | Q_REG_ADDR(q_spec, Q_REG_TYPE), &buf[0], 2); |
| 1198 | if (rc) { |
| 1199 | dev_err(&spmi->dev, "%s: unable to read type regs\n", |
| 1200 | __func__); |
Michael Bohan | e25e15f | 2012-04-12 17:28:26 -0700 | [diff] [blame] | 1201 | goto err_probe; |
| 1202 | } |
| 1203 | q_spec->type = buf[0]; |
| 1204 | q_spec->subtype = buf[1]; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1205 | |
Michael Bohan | 6b90157 | 2012-05-30 13:32:24 -0700 | [diff] [blame^] | 1206 | rc = qpnp_pin_ctl_regs_init(q_spec); |
| 1207 | if (rc) |
| 1208 | goto err_probe; |
| 1209 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1210 | /* call into irq_domain to get irq mapping */ |
| 1211 | intspec[0] = q_chip->spmi->sid; |
| 1212 | intspec[1] = (q_spec->offset >> 8) & 0xFF; |
| 1213 | intspec[2] = 0; |
| 1214 | q_spec->irq = irq_create_of_mapping(q_chip->int_ctrl, |
| 1215 | intspec, 3); |
| 1216 | if (!q_spec->irq) { |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 1217 | dev_err(&spmi->dev, "%s: invalid irq for gpio %u\n", |
| 1218 | __func__, gpio); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1219 | rc = -EINVAL; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1220 | goto err_probe; |
| 1221 | } |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1222 | /* initialize lookup table params */ |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1223 | qpnp_pmic_pin_set_spec(q_chip, gpio, q_spec); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1224 | qpnp_chip_gpio_set_spec(q_chip, i, q_spec); |
| 1225 | } |
| 1226 | |
| 1227 | q_chip->gpio_chip.base = -1; |
| 1228 | q_chip->gpio_chip.ngpio = spmi->num_dev_node; |
Michael Bohan | 6ea2cd2 | 2012-05-29 15:40:18 -0700 | [diff] [blame] | 1229 | q_chip->gpio_chip.label = dev_name; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1230 | q_chip->gpio_chip.direction_input = qpnp_pin_direction_input; |
| 1231 | q_chip->gpio_chip.direction_output = qpnp_pin_direction_output; |
| 1232 | q_chip->gpio_chip.to_irq = qpnp_pin_to_irq; |
| 1233 | q_chip->gpio_chip.get = qpnp_pin_get; |
| 1234 | q_chip->gpio_chip.set = qpnp_pin_set; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1235 | q_chip->gpio_chip.dev = &spmi->dev; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1236 | q_chip->gpio_chip.of_xlate = qpnp_pin_of_gpio_xlate; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1237 | q_chip->gpio_chip.of_gpio_n_cells = 2; |
| 1238 | q_chip->gpio_chip.can_sleep = 0; |
| 1239 | |
| 1240 | rc = gpiochip_add(&q_chip->gpio_chip); |
| 1241 | if (rc) { |
| 1242 | dev_err(&spmi->dev, "%s: Can't add gpio chip, rc = %d\n", |
| 1243 | __func__, rc); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1244 | goto err_probe; |
| 1245 | } |
| 1246 | |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 1247 | /* now configure gpio config defaults if they exist */ |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1248 | for (i = 0; i < spmi->num_dev_node; i++) { |
| 1249 | q_spec = qpnp_chip_gpio_get_spec(q_chip, i); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1250 | if (WARN_ON(!q_spec)) { |
| 1251 | rc = -ENODEV; |
| 1252 | goto err_probe; |
| 1253 | } |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1254 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1255 | rc = qpnp_pin_cache_regs(q_chip, q_spec); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1256 | if (rc) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1257 | goto err_probe; |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 1258 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1259 | rc = qpnp_pin_apply_config(q_chip, q_spec); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1260 | if (rc) |
Michael Bohan | de3942a | 2012-04-17 15:28:01 -0700 | [diff] [blame] | 1261 | goto err_probe; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | dev_dbg(&spmi->dev, "%s: gpio_chip registered between %d-%u\n", |
| 1265 | __func__, q_chip->gpio_chip.base, |
| 1266 | (q_chip->gpio_chip.base + q_chip->gpio_chip.ngpio) - 1); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1267 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1268 | rc = qpnp_pin_debugfs_create(q_chip); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1269 | if (rc) { |
| 1270 | dev_err(&spmi->dev, "%s: debugfs creation failed\n", __func__); |
| 1271 | goto err_probe; |
| 1272 | } |
| 1273 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1274 | return 0; |
| 1275 | |
| 1276 | err_probe: |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1277 | qpnp_pin_free_chip(q_chip); |
Michael Bohan | fdcbed2 | 2012-04-23 17:44:27 -0700 | [diff] [blame] | 1278 | return rc; |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1281 | static int qpnp_pin_remove(struct spmi_device *spmi) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1282 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1283 | struct qpnp_pin_chip *q_chip = dev_get_drvdata(&spmi->dev); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1284 | |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1285 | debugfs_remove_recursive(q_chip->dfs_dir); |
| 1286 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1287 | return qpnp_pin_free_chip(q_chip); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | static struct of_device_id spmi_match_table[] = { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1291 | { .compatible = "qcom,qpnp-pin", |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1292 | }, |
| 1293 | {} |
| 1294 | }; |
| 1295 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1296 | static const struct spmi_device_id qpnp_pin_id[] = { |
| 1297 | { "qcom,qpnp-pin", 0 }, |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1298 | { } |
| 1299 | }; |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1300 | MODULE_DEVICE_TABLE(spmi, qpnp_pin_id); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1301 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1302 | static struct spmi_driver qpnp_pin_driver = { |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1303 | .driver = { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1304 | .name = "qcom,qpnp-pin", |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1305 | .of_match_table = spmi_match_table, |
| 1306 | }, |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1307 | .probe = qpnp_pin_probe, |
| 1308 | .remove = qpnp_pin_remove, |
| 1309 | .id_table = qpnp_pin_id, |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1310 | }; |
| 1311 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1312 | static int __init qpnp_pin_init(void) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1313 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1314 | #ifdef CONFIG_GPIO_QPNP_PIN_DEBUG |
| 1315 | driver_dfs_dir = debugfs_create_dir("qpnp_pin", NULL); |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1316 | if (driver_dfs_dir == NULL) |
| 1317 | pr_err("Cannot register top level debugfs directory\n"); |
| 1318 | #endif |
| 1319 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1320 | return spmi_driver_register(&qpnp_pin_driver); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1323 | static void __exit qpnp_pin_exit(void) |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1324 | { |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1325 | #ifdef CONFIG_GPIO_QPNP_PIN_DEBUG |
Michael Bohan | a19dced | 2012-04-24 13:14:50 -0700 | [diff] [blame] | 1326 | debugfs_remove_recursive(driver_dfs_dir); |
| 1327 | #endif |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1328 | spmi_driver_unregister(&qpnp_pin_driver); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1331 | MODULE_DESCRIPTION("QPNP PMIC gpio driver"); |
Michael Bohan | 7f0cc9d | 2012-04-16 17:16:09 -0700 | [diff] [blame] | 1332 | MODULE_LICENSE("GPL v2"); |
Michael Bohan | 0ba63b8 | 2012-02-06 13:42:34 -0800 | [diff] [blame] | 1333 | |
Michael Bohan | a05f455 | 2012-05-24 15:58:11 -0700 | [diff] [blame] | 1334 | module_init(qpnp_pin_init); |
| 1335 | module_exit(qpnp_pin_exit); |