Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, BayLibre, SAS. All rights reserved. |
| 3 | * Author: Neil Armstrong <narmstrong@baylibre.com> |
| 4 | * |
| 5 | * Copyright (c) 2010, Code Aurora Forum. All rights reserved. |
| 6 | * |
| 7 | * Driver for Semtech SX150X I2C GPIO Expanders |
| 8 | * |
| 9 | * Author: Gregory Bean <gbean@codeaurora.org> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 and |
| 13 | * only version 2 as published by the Free Software Foundation. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | */ |
| 20 | |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 21 | #include <linux/regmap.h> |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 22 | #include <linux/i2c.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/irq.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/of.h> |
Andrey Smirnov | e3ba812 | 2016-11-07 08:53:09 -0800 | [diff] [blame] | 29 | #include <linux/of_device.h> |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 30 | #include <linux/gpio.h> |
| 31 | #include <linux/pinctrl/machine.h> |
| 32 | #include <linux/pinctrl/pinconf.h> |
| 33 | #include <linux/pinctrl/pinctrl.h> |
| 34 | #include <linux/pinctrl/pinmux.h> |
| 35 | #include <linux/pinctrl/pinconf-generic.h> |
| 36 | |
| 37 | #include "core.h" |
| 38 | #include "pinconf.h" |
| 39 | #include "pinctrl-utils.h" |
| 40 | |
| 41 | /* The chip models of sx150x */ |
| 42 | enum { |
| 43 | SX150X_123 = 0, |
| 44 | SX150X_456, |
| 45 | SX150X_789, |
| 46 | }; |
Andrey Smirnov | 7d68a79 | 2016-11-07 08:53:12 -0800 | [diff] [blame] | 47 | enum { |
| 48 | SX150X_789_REG_MISC_AUTOCLEAR_OFF = 1 << 0, |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 49 | SX150X_MAX_REGISTER = 0xad, |
Andrey Smirnov | fd931f2 | 2016-11-07 08:53:22 -0800 | [diff] [blame^] | 50 | SX150X_IRQ_TYPE_EDGE_RISING = 0x1, |
| 51 | SX150X_IRQ_TYPE_EDGE_FALLING = 0x2, |
Andrey Smirnov | 7d68a79 | 2016-11-07 08:53:12 -0800 | [diff] [blame] | 52 | }; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 53 | |
| 54 | struct sx150x_123_pri { |
| 55 | u8 reg_pld_mode; |
| 56 | u8 reg_pld_table0; |
| 57 | u8 reg_pld_table1; |
| 58 | u8 reg_pld_table2; |
| 59 | u8 reg_pld_table3; |
| 60 | u8 reg_pld_table4; |
| 61 | u8 reg_advance; |
| 62 | }; |
| 63 | |
| 64 | struct sx150x_456_pri { |
| 65 | u8 reg_pld_mode; |
| 66 | u8 reg_pld_table0; |
| 67 | u8 reg_pld_table1; |
| 68 | u8 reg_pld_table2; |
| 69 | u8 reg_pld_table3; |
| 70 | u8 reg_pld_table4; |
| 71 | u8 reg_advance; |
| 72 | }; |
| 73 | |
| 74 | struct sx150x_789_pri { |
| 75 | u8 reg_drain; |
| 76 | u8 reg_polarity; |
| 77 | u8 reg_clock; |
| 78 | u8 reg_misc; |
| 79 | u8 reg_reset; |
| 80 | u8 ngpios; |
| 81 | }; |
| 82 | |
| 83 | struct sx150x_device_data { |
| 84 | u8 model; |
| 85 | u8 reg_pullup; |
| 86 | u8 reg_pulldn; |
| 87 | u8 reg_dir; |
| 88 | u8 reg_data; |
| 89 | u8 reg_irq_mask; |
| 90 | u8 reg_irq_src; |
| 91 | u8 reg_sense; |
| 92 | u8 ngpios; |
| 93 | union { |
| 94 | struct sx150x_123_pri x123; |
| 95 | struct sx150x_456_pri x456; |
| 96 | struct sx150x_789_pri x789; |
| 97 | } pri; |
| 98 | const struct pinctrl_pin_desc *pins; |
| 99 | unsigned int npins; |
| 100 | }; |
| 101 | |
| 102 | struct sx150x_pinctrl { |
| 103 | struct device *dev; |
| 104 | struct i2c_client *client; |
| 105 | struct pinctrl_dev *pctldev; |
| 106 | struct pinctrl_desc pinctrl_desc; |
| 107 | struct gpio_chip gpio; |
| 108 | struct irq_chip irq_chip; |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 109 | struct regmap *regmap; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 110 | struct { |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 111 | u32 sense; |
| 112 | u32 masked; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 113 | } irq; |
| 114 | struct mutex lock; |
| 115 | const struct sx150x_device_data *data; |
| 116 | }; |
| 117 | |
| 118 | static const struct pinctrl_pin_desc sx150x_8_pins[] = { |
| 119 | PINCTRL_PIN(0, "gpio0"), |
| 120 | PINCTRL_PIN(1, "gpio1"), |
| 121 | PINCTRL_PIN(2, "gpio2"), |
| 122 | PINCTRL_PIN(3, "gpio3"), |
| 123 | PINCTRL_PIN(4, "gpio4"), |
| 124 | PINCTRL_PIN(5, "gpio5"), |
| 125 | PINCTRL_PIN(6, "gpio6"), |
| 126 | PINCTRL_PIN(7, "gpio7"), |
| 127 | PINCTRL_PIN(8, "oscio"), |
| 128 | }; |
| 129 | |
| 130 | static const struct pinctrl_pin_desc sx150x_16_pins[] = { |
| 131 | PINCTRL_PIN(0, "gpio0"), |
| 132 | PINCTRL_PIN(1, "gpio1"), |
| 133 | PINCTRL_PIN(2, "gpio2"), |
| 134 | PINCTRL_PIN(3, "gpio3"), |
| 135 | PINCTRL_PIN(4, "gpio4"), |
| 136 | PINCTRL_PIN(5, "gpio5"), |
| 137 | PINCTRL_PIN(6, "gpio6"), |
| 138 | PINCTRL_PIN(7, "gpio7"), |
| 139 | PINCTRL_PIN(8, "gpio8"), |
| 140 | PINCTRL_PIN(9, "gpio9"), |
| 141 | PINCTRL_PIN(10, "gpio10"), |
| 142 | PINCTRL_PIN(11, "gpio11"), |
| 143 | PINCTRL_PIN(12, "gpio12"), |
| 144 | PINCTRL_PIN(13, "gpio13"), |
| 145 | PINCTRL_PIN(14, "gpio14"), |
| 146 | PINCTRL_PIN(15, "gpio15"), |
| 147 | PINCTRL_PIN(16, "oscio"), |
| 148 | }; |
| 149 | |
| 150 | static const struct sx150x_device_data sx1508q_device_data = { |
| 151 | .model = SX150X_789, |
| 152 | .reg_pullup = 0x03, |
| 153 | .reg_pulldn = 0x04, |
| 154 | .reg_dir = 0x07, |
| 155 | .reg_data = 0x08, |
| 156 | .reg_irq_mask = 0x09, |
| 157 | .reg_irq_src = 0x0c, |
| 158 | .reg_sense = 0x0b, |
| 159 | .pri.x789 = { |
| 160 | .reg_drain = 0x05, |
| 161 | .reg_polarity = 0x06, |
| 162 | .reg_clock = 0x0f, |
| 163 | .reg_misc = 0x10, |
| 164 | .reg_reset = 0x7d, |
| 165 | }, |
| 166 | .ngpios = 8, |
| 167 | .pins = sx150x_8_pins, |
| 168 | .npins = ARRAY_SIZE(sx150x_8_pins), |
| 169 | }; |
| 170 | |
| 171 | static const struct sx150x_device_data sx1509q_device_data = { |
| 172 | .model = SX150X_789, |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 173 | .reg_pullup = 0x06, |
| 174 | .reg_pulldn = 0x08, |
| 175 | .reg_dir = 0x0e, |
| 176 | .reg_data = 0x10, |
| 177 | .reg_irq_mask = 0x12, |
| 178 | .reg_irq_src = 0x18, |
| 179 | .reg_sense = 0x14, |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 180 | .pri.x789 = { |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 181 | .reg_drain = 0x0a, |
| 182 | .reg_polarity = 0x0c, |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 183 | .reg_clock = 0x1e, |
| 184 | .reg_misc = 0x1f, |
| 185 | .reg_reset = 0x7d, |
| 186 | }, |
| 187 | .ngpios = 16, |
| 188 | .pins = sx150x_16_pins, |
| 189 | .npins = ARRAY_SIZE(sx150x_16_pins), |
| 190 | }; |
| 191 | |
| 192 | static const struct sx150x_device_data sx1506q_device_data = { |
| 193 | .model = SX150X_456, |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 194 | .reg_pullup = 0x04, |
| 195 | .reg_pulldn = 0x06, |
| 196 | .reg_dir = 0x02, |
| 197 | .reg_data = 0x00, |
| 198 | .reg_irq_mask = 0x08, |
| 199 | .reg_irq_src = 0x0e, |
| 200 | .reg_sense = 0x0a, |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 201 | .pri.x456 = { |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 202 | .reg_pld_mode = 0x20, |
| 203 | .reg_pld_table0 = 0x22, |
| 204 | .reg_pld_table1 = 0x24, |
| 205 | .reg_pld_table2 = 0x26, |
| 206 | .reg_pld_table3 = 0x28, |
| 207 | .reg_pld_table4 = 0x2a, |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 208 | .reg_advance = 0xad, |
| 209 | }, |
| 210 | .ngpios = 16, |
| 211 | .pins = sx150x_16_pins, |
| 212 | .npins = 16, /* oscio not available */ |
| 213 | }; |
| 214 | |
| 215 | static const struct sx150x_device_data sx1502q_device_data = { |
| 216 | .model = SX150X_123, |
| 217 | .reg_pullup = 0x02, |
| 218 | .reg_pulldn = 0x03, |
| 219 | .reg_dir = 0x01, |
| 220 | .reg_data = 0x00, |
| 221 | .reg_irq_mask = 0x05, |
| 222 | .reg_irq_src = 0x08, |
| 223 | .reg_sense = 0x07, |
| 224 | .pri.x123 = { |
| 225 | .reg_pld_mode = 0x10, |
| 226 | .reg_pld_table0 = 0x11, |
| 227 | .reg_pld_table1 = 0x12, |
| 228 | .reg_pld_table2 = 0x13, |
| 229 | .reg_pld_table3 = 0x14, |
| 230 | .reg_pld_table4 = 0x15, |
| 231 | .reg_advance = 0xad, |
| 232 | }, |
| 233 | .ngpios = 8, |
| 234 | .pins = sx150x_8_pins, |
| 235 | .npins = 8, /* oscio not available */ |
| 236 | }; |
| 237 | |
Andrey Smirnov | 6697546 | 2016-11-07 08:53:10 -0800 | [diff] [blame] | 238 | static const struct sx150x_device_data sx1503q_device_data = { |
| 239 | .model = SX150X_123, |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 240 | .reg_pullup = 0x04, |
| 241 | .reg_pulldn = 0x06, |
| 242 | .reg_dir = 0x02, |
| 243 | .reg_data = 0x00, |
| 244 | .reg_irq_mask = 0x08, |
| 245 | .reg_irq_src = 0x0e, |
| 246 | .reg_sense = 0x0a, |
Andrey Smirnov | 6697546 | 2016-11-07 08:53:10 -0800 | [diff] [blame] | 247 | .pri.x123 = { |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 248 | .reg_pld_mode = 0x20, |
| 249 | .reg_pld_table0 = 0x22, |
| 250 | .reg_pld_table1 = 0x24, |
| 251 | .reg_pld_table2 = 0x26, |
| 252 | .reg_pld_table3 = 0x28, |
| 253 | .reg_pld_table4 = 0x2a, |
Andrey Smirnov | 6697546 | 2016-11-07 08:53:10 -0800 | [diff] [blame] | 254 | .reg_advance = 0xad, |
| 255 | }, |
| 256 | .ngpios = 16, |
| 257 | .pins = sx150x_16_pins, |
| 258 | .npins = 16, /* oscio not available */ |
| 259 | }; |
| 260 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 261 | static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev *pctldev) |
| 262 | { |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev *pctldev, |
| 267 | unsigned int group) |
| 268 | { |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, |
| 273 | unsigned int group, |
| 274 | const unsigned int **pins, |
| 275 | unsigned int *num_pins) |
| 276 | { |
| 277 | return -ENOTSUPP; |
| 278 | } |
| 279 | |
| 280 | static const struct pinctrl_ops sx150x_pinctrl_ops = { |
| 281 | .get_groups_count = sx150x_pinctrl_get_groups_count, |
| 282 | .get_group_name = sx150x_pinctrl_get_group_name, |
| 283 | .get_group_pins = sx150x_pinctrl_get_group_pins, |
| 284 | #ifdef CONFIG_OF |
| 285 | .dt_node_to_map = pinconf_generic_dt_node_to_map_pin, |
| 286 | .dt_free_map = pinctrl_utils_free_map, |
| 287 | #endif |
| 288 | }; |
| 289 | |
| 290 | static bool sx150x_pin_is_oscio(struct sx150x_pinctrl *pctl, unsigned int pin) |
| 291 | { |
| 292 | if (pin >= pctl->data->npins) |
| 293 | return false; |
| 294 | |
| 295 | /* OSCIO pin is only present in 789 devices */ |
| 296 | if (pctl->data->model != SX150X_789) |
| 297 | return false; |
| 298 | |
| 299 | return !strcmp(pctl->data->pins[pin].name, "oscio"); |
| 300 | } |
| 301 | |
| 302 | static int sx150x_gpio_get_direction(struct gpio_chip *chip, |
| 303 | unsigned int offset) |
| 304 | { |
| 305 | struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 306 | unsigned int value; |
| 307 | int ret; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 308 | |
| 309 | if (sx150x_pin_is_oscio(pctl, offset)) |
| 310 | return false; |
| 311 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 312 | ret = regmap_read(pctl->regmap, pctl->data->reg_dir, &value); |
| 313 | if (ret < 0) |
| 314 | return ret; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 315 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 316 | return !!(value & BIT(offset)); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static int sx150x_gpio_get(struct gpio_chip *chip, unsigned int offset) |
| 320 | { |
| 321 | struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 322 | unsigned int value; |
| 323 | int ret; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 324 | |
| 325 | if (sx150x_pin_is_oscio(pctl, offset)) |
| 326 | return -EINVAL; |
| 327 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 328 | ret = regmap_read(pctl->regmap, pctl->data->reg_data, &value); |
| 329 | if (ret < 0) |
| 330 | return ret; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 331 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 332 | return !!(value & BIT(offset)); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int sx150x_gpio_set_single_ended(struct gpio_chip *chip, |
| 336 | unsigned int offset, |
| 337 | enum single_ended_mode mode) |
| 338 | { |
| 339 | struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); |
| 340 | int ret; |
| 341 | |
| 342 | switch (mode) { |
| 343 | case LINE_MODE_PUSH_PULL: |
| 344 | if (pctl->data->model != SX150X_789 || |
| 345 | sx150x_pin_is_oscio(pctl, offset)) |
| 346 | return 0; |
| 347 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 348 | ret = regmap_write_bits(pctl->regmap, |
| 349 | pctl->data->pri.x789.reg_drain, |
| 350 | BIT(offset), 0); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | case LINE_MODE_OPEN_DRAIN: |
| 354 | if (pctl->data->model != SX150X_789 || |
| 355 | sx150x_pin_is_oscio(pctl, offset)) |
| 356 | return -ENOTSUPP; |
| 357 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 358 | ret = regmap_write_bits(pctl->regmap, |
| 359 | pctl->data->pri.x789.reg_drain, |
| 360 | BIT(offset), BIT(offset)); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 361 | break; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 362 | default: |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 363 | ret = -ENOTSUPP; |
| 364 | break; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 367 | return ret; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 370 | static int __sx150x_gpio_set(struct sx150x_pinctrl *pctl, unsigned int offset, |
| 371 | int value) |
| 372 | { |
| 373 | return regmap_write_bits(pctl->regmap, pctl->data->reg_data, |
| 374 | BIT(offset), value ? BIT(offset) : 0); |
| 375 | } |
| 376 | |
Andrey Smirnov | ab5bd03 | 2016-11-07 08:53:19 -0800 | [diff] [blame] | 377 | static int sx150x_gpio_oscio_set(struct sx150x_pinctrl *pctl, |
| 378 | int value) |
| 379 | { |
| 380 | return regmap_write(pctl->regmap, |
| 381 | pctl->data->pri.x789.reg_clock, |
| 382 | (value ? 0x1f : 0x10)); |
| 383 | } |
| 384 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 385 | static void sx150x_gpio_set(struct gpio_chip *chip, unsigned int offset, |
| 386 | int value) |
| 387 | { |
| 388 | struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); |
| 389 | |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 390 | if (sx150x_pin_is_oscio(pctl, offset)) |
Andrey Smirnov | ab5bd03 | 2016-11-07 08:53:19 -0800 | [diff] [blame] | 391 | sx150x_gpio_oscio_set(pctl, value); |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 392 | else |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 393 | __sx150x_gpio_set(pctl, offset, value); |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 394 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int sx150x_gpio_direction_input(struct gpio_chip *chip, |
| 398 | unsigned int offset) |
| 399 | { |
| 400 | struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 401 | |
| 402 | if (sx150x_pin_is_oscio(pctl, offset)) |
| 403 | return -EINVAL; |
| 404 | |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 405 | return regmap_write_bits(pctl->regmap, |
| 406 | pctl->data->reg_dir, |
| 407 | BIT(offset), BIT(offset)); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static int sx150x_gpio_direction_output(struct gpio_chip *chip, |
| 411 | unsigned int offset, int value) |
| 412 | { |
| 413 | struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 414 | int ret; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 415 | |
Andrey Smirnov | ab5bd03 | 2016-11-07 08:53:19 -0800 | [diff] [blame] | 416 | if (sx150x_pin_is_oscio(pctl, offset)) |
| 417 | return sx150x_gpio_oscio_set(pctl, value); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 418 | |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 419 | ret = __sx150x_gpio_set(pctl, offset, value); |
| 420 | if (ret < 0) |
| 421 | return ret; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 422 | |
Andrey Smirnov | d977a87 | 2016-11-07 08:53:18 -0800 | [diff] [blame] | 423 | return regmap_write_bits(pctl->regmap, |
| 424 | pctl->data->reg_dir, |
| 425 | BIT(offset), 0); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static void sx150x_irq_mask(struct irq_data *d) |
| 429 | { |
| 430 | struct sx150x_pinctrl *pctl = |
| 431 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
| 432 | unsigned int n = d->hwirq; |
| 433 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 434 | pctl->irq.masked |= BIT(n); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void sx150x_irq_unmask(struct irq_data *d) |
| 438 | { |
| 439 | struct sx150x_pinctrl *pctl = |
| 440 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
| 441 | unsigned int n = d->hwirq; |
| 442 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 443 | pctl->irq.masked &= ~BIT(n); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Andrey Smirnov | fd931f2 | 2016-11-07 08:53:22 -0800 | [diff] [blame^] | 446 | static void sx150x_irq_set_sense(struct sx150x_pinctrl *pctl, |
| 447 | unsigned int line, unsigned int sense) |
| 448 | { |
| 449 | /* |
| 450 | * Every interrupt line is represented by two bits shifted |
| 451 | * proportionally to the line number |
| 452 | */ |
| 453 | const unsigned int n = line * 2; |
| 454 | const unsigned int mask = ~((SX150X_IRQ_TYPE_EDGE_RISING | |
| 455 | SX150X_IRQ_TYPE_EDGE_FALLING) << n); |
| 456 | |
| 457 | pctl->irq.sense &= mask; |
| 458 | pctl->irq.sense |= sense << n; |
| 459 | } |
| 460 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 461 | static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type) |
| 462 | { |
| 463 | struct sx150x_pinctrl *pctl = |
| 464 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
| 465 | unsigned int n, val = 0; |
| 466 | |
| 467 | if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) |
| 468 | return -EINVAL; |
| 469 | |
| 470 | n = d->hwirq; |
| 471 | |
| 472 | if (flow_type & IRQ_TYPE_EDGE_RISING) |
Andrey Smirnov | fd931f2 | 2016-11-07 08:53:22 -0800 | [diff] [blame^] | 473 | val |= SX150X_IRQ_TYPE_EDGE_RISING; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 474 | if (flow_type & IRQ_TYPE_EDGE_FALLING) |
Andrey Smirnov | fd931f2 | 2016-11-07 08:53:22 -0800 | [diff] [blame^] | 475 | val |= SX150X_IRQ_TYPE_EDGE_FALLING; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 476 | |
Andrey Smirnov | fd931f2 | 2016-11-07 08:53:22 -0800 | [diff] [blame^] | 477 | sx150x_irq_set_sense(pctl, n, val); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id) |
| 482 | { |
| 483 | struct sx150x_pinctrl *pctl = (struct sx150x_pinctrl *)dev_id; |
Andrey Smirnov | 05a90cc | 2016-11-07 08:53:20 -0800 | [diff] [blame] | 484 | unsigned long n, status; |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 485 | unsigned int val; |
Andrey Smirnov | 05a90cc | 2016-11-07 08:53:20 -0800 | [diff] [blame] | 486 | int err; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 487 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 488 | err = regmap_read(pctl->regmap, pctl->data->reg_irq_src, &val); |
| 489 | if (err < 0) |
| 490 | return IRQ_NONE; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 491 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 492 | err = regmap_write(pctl->regmap, pctl->data->reg_irq_src, val); |
| 493 | if (err < 0) |
| 494 | return IRQ_NONE; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 495 | |
Andrey Smirnov | 05a90cc | 2016-11-07 08:53:20 -0800 | [diff] [blame] | 496 | status = val; |
| 497 | for_each_set_bit(n, &status, pctl->data->ngpios) |
| 498 | handle_nested_irq(irq_find_mapping(pctl->gpio.irqdomain, n)); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 499 | |
Andrey Smirnov | 05a90cc | 2016-11-07 08:53:20 -0800 | [diff] [blame] | 500 | return IRQ_HANDLED; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static void sx150x_irq_bus_lock(struct irq_data *d) |
| 504 | { |
| 505 | struct sx150x_pinctrl *pctl = |
| 506 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
| 507 | |
| 508 | mutex_lock(&pctl->lock); |
| 509 | } |
| 510 | |
| 511 | static void sx150x_irq_bus_sync_unlock(struct irq_data *d) |
| 512 | { |
| 513 | struct sx150x_pinctrl *pctl = |
| 514 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 515 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 516 | regmap_write(pctl->regmap, pctl->data->reg_irq_mask, pctl->irq.masked); |
| 517 | regmap_write(pctl->regmap, pctl->data->reg_sense, pctl->irq.sense); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 518 | mutex_unlock(&pctl->lock); |
| 519 | } |
| 520 | |
| 521 | static int sx150x_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, |
| 522 | unsigned long *config) |
| 523 | { |
| 524 | struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 525 | unsigned int param = pinconf_to_config_param(*config); |
| 526 | int ret; |
| 527 | u32 arg; |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 528 | unsigned int data; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 529 | |
| 530 | if (sx150x_pin_is_oscio(pctl, pin)) { |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 531 | switch (param) { |
| 532 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
| 533 | case PIN_CONFIG_OUTPUT: |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 534 | ret = regmap_read(pctl->regmap, |
| 535 | pctl->data->pri.x789.reg_clock, |
| 536 | &data); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 537 | if (ret < 0) |
| 538 | return ret; |
| 539 | |
| 540 | if (param == PIN_CONFIG_DRIVE_PUSH_PULL) |
| 541 | arg = (data & 0x1f) ? 1 : 0; |
| 542 | else { |
| 543 | if ((data & 0x1f) == 0x1f) |
| 544 | arg = 1; |
| 545 | else if ((data & 0x1f) == 0x10) |
| 546 | arg = 0; |
| 547 | else |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | |
| 551 | break; |
| 552 | default: |
| 553 | return -ENOTSUPP; |
| 554 | } |
| 555 | |
| 556 | goto out; |
| 557 | } |
| 558 | |
| 559 | switch (param) { |
| 560 | case PIN_CONFIG_BIAS_PULL_DOWN: |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 561 | ret = regmap_read(pctl->regmap, |
| 562 | pctl->data->reg_pulldn, |
| 563 | &data); |
| 564 | data &= BIT(pin); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 565 | |
| 566 | if (ret < 0) |
| 567 | return ret; |
| 568 | |
| 569 | if (!ret) |
| 570 | return -EINVAL; |
| 571 | |
| 572 | arg = 1; |
| 573 | break; |
| 574 | |
| 575 | case PIN_CONFIG_BIAS_PULL_UP: |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 576 | ret = regmap_read(pctl->regmap, |
| 577 | pctl->data->reg_pullup, |
| 578 | &data); |
| 579 | data &= BIT(pin); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 580 | |
| 581 | if (ret < 0) |
| 582 | return ret; |
| 583 | |
| 584 | if (!ret) |
| 585 | return -EINVAL; |
| 586 | |
| 587 | arg = 1; |
| 588 | break; |
| 589 | |
| 590 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: |
| 591 | if (pctl->data->model != SX150X_789) |
| 592 | return -ENOTSUPP; |
| 593 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 594 | ret = regmap_read(pctl->regmap, |
| 595 | pctl->data->pri.x789.reg_drain, |
| 596 | &data); |
| 597 | data &= BIT(pin); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 598 | |
| 599 | if (ret < 0) |
| 600 | return ret; |
| 601 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 602 | if (!data) |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 603 | return -EINVAL; |
| 604 | |
| 605 | arg = 1; |
| 606 | break; |
| 607 | |
| 608 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
| 609 | if (pctl->data->model != SX150X_789) |
| 610 | arg = true; |
| 611 | else { |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 612 | ret = regmap_read(pctl->regmap, |
| 613 | pctl->data->pri.x789.reg_drain, |
| 614 | &data); |
| 615 | data &= BIT(pin); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 616 | |
| 617 | if (ret < 0) |
| 618 | return ret; |
| 619 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 620 | if (data) |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 621 | return -EINVAL; |
| 622 | |
| 623 | arg = 1; |
| 624 | } |
| 625 | break; |
| 626 | |
| 627 | case PIN_CONFIG_OUTPUT: |
| 628 | ret = sx150x_gpio_get_direction(&pctl->gpio, pin); |
| 629 | if (ret < 0) |
| 630 | return ret; |
| 631 | |
| 632 | if (ret) |
| 633 | return -EINVAL; |
| 634 | |
| 635 | ret = sx150x_gpio_get(&pctl->gpio, pin); |
| 636 | if (ret < 0) |
| 637 | return ret; |
| 638 | |
| 639 | arg = ret; |
| 640 | break; |
| 641 | |
| 642 | default: |
| 643 | return -ENOTSUPP; |
| 644 | } |
| 645 | |
| 646 | out: |
| 647 | *config = pinconf_to_config_packed(param, arg); |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static int sx150x_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 653 | unsigned long *configs, unsigned int num_configs) |
| 654 | { |
| 655 | struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 656 | enum pin_config_param param; |
| 657 | u32 arg; |
| 658 | int i; |
| 659 | int ret; |
| 660 | |
| 661 | for (i = 0; i < num_configs; i++) { |
| 662 | param = pinconf_to_config_param(configs[i]); |
| 663 | arg = pinconf_to_config_argument(configs[i]); |
| 664 | |
| 665 | if (sx150x_pin_is_oscio(pctl, pin)) { |
| 666 | if (param == PIN_CONFIG_OUTPUT) { |
| 667 | ret = sx150x_gpio_direction_output(&pctl->gpio, |
| 668 | pin, arg); |
| 669 | if (ret < 0) |
| 670 | return ret; |
| 671 | |
| 672 | continue; |
| 673 | } else |
| 674 | return -ENOTSUPP; |
| 675 | } |
| 676 | |
| 677 | switch (param) { |
| 678 | case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: |
| 679 | case PIN_CONFIG_BIAS_DISABLE: |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 680 | ret = regmap_write_bits(pctl->regmap, |
| 681 | pctl->data->reg_pulldn, |
| 682 | BIT(pin), 0); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 683 | if (ret < 0) |
| 684 | return ret; |
| 685 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 686 | ret = regmap_write_bits(pctl->regmap, |
| 687 | pctl->data->reg_pullup, |
| 688 | BIT(pin), 0); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 689 | if (ret < 0) |
| 690 | return ret; |
| 691 | |
| 692 | break; |
| 693 | |
| 694 | case PIN_CONFIG_BIAS_PULL_UP: |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 695 | ret = regmap_write_bits(pctl->regmap, |
| 696 | pctl->data->reg_pullup, |
| 697 | BIT(pin), BIT(pin)); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 698 | if (ret < 0) |
| 699 | return ret; |
| 700 | |
| 701 | break; |
| 702 | |
| 703 | case PIN_CONFIG_BIAS_PULL_DOWN: |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 704 | ret = regmap_write_bits(pctl->regmap, |
| 705 | pctl->data->reg_pulldn, |
| 706 | BIT(pin), BIT(pin)); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 707 | if (ret < 0) |
| 708 | return ret; |
| 709 | |
| 710 | break; |
| 711 | |
| 712 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: |
| 713 | ret = sx150x_gpio_set_single_ended(&pctl->gpio, |
| 714 | pin, LINE_MODE_OPEN_DRAIN); |
| 715 | if (ret < 0) |
| 716 | return ret; |
| 717 | |
| 718 | break; |
| 719 | |
| 720 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
| 721 | ret = sx150x_gpio_set_single_ended(&pctl->gpio, |
| 722 | pin, LINE_MODE_PUSH_PULL); |
| 723 | if (ret < 0) |
| 724 | return ret; |
| 725 | |
| 726 | break; |
| 727 | |
| 728 | case PIN_CONFIG_OUTPUT: |
| 729 | ret = sx150x_gpio_direction_output(&pctl->gpio, |
| 730 | pin, arg); |
| 731 | if (ret < 0) |
| 732 | return ret; |
| 733 | |
| 734 | break; |
| 735 | |
| 736 | default: |
| 737 | return -ENOTSUPP; |
| 738 | } |
| 739 | } /* for each config */ |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static const struct pinconf_ops sx150x_pinconf_ops = { |
| 745 | .pin_config_get = sx150x_pinconf_get, |
| 746 | .pin_config_set = sx150x_pinconf_set, |
| 747 | .is_generic = true, |
| 748 | }; |
| 749 | |
| 750 | static const struct i2c_device_id sx150x_id[] = { |
| 751 | {"sx1508q", (kernel_ulong_t) &sx1508q_device_data }, |
| 752 | {"sx1509q", (kernel_ulong_t) &sx1509q_device_data }, |
| 753 | {"sx1506q", (kernel_ulong_t) &sx1506q_device_data }, |
| 754 | {"sx1502q", (kernel_ulong_t) &sx1502q_device_data }, |
Andrey Smirnov | 6697546 | 2016-11-07 08:53:10 -0800 | [diff] [blame] | 755 | {"sx1503q", (kernel_ulong_t) &sx1503q_device_data }, |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 756 | {} |
| 757 | }; |
| 758 | |
| 759 | static const struct of_device_id sx150x_of_match[] = { |
Andrey Smirnov | e3ba812 | 2016-11-07 08:53:09 -0800 | [diff] [blame] | 760 | { .compatible = "semtech,sx1508q", .data = &sx1508q_device_data }, |
| 761 | { .compatible = "semtech,sx1509q", .data = &sx1509q_device_data }, |
| 762 | { .compatible = "semtech,sx1506q", .data = &sx1506q_device_data }, |
| 763 | { .compatible = "semtech,sx1502q", .data = &sx1502q_device_data }, |
Andrey Smirnov | 6697546 | 2016-11-07 08:53:10 -0800 | [diff] [blame] | 764 | { .compatible = "semtech,sx1503q", .data = &sx1503q_device_data }, |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 765 | {}, |
| 766 | }; |
| 767 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 768 | static int sx150x_reset(struct sx150x_pinctrl *pctl) |
| 769 | { |
| 770 | int err; |
| 771 | |
| 772 | err = i2c_smbus_write_byte_data(pctl->client, |
| 773 | pctl->data->pri.x789.reg_reset, |
| 774 | 0x12); |
| 775 | if (err < 0) |
| 776 | return err; |
| 777 | |
| 778 | err = i2c_smbus_write_byte_data(pctl->client, |
| 779 | pctl->data->pri.x789.reg_reset, |
| 780 | 0x34); |
| 781 | return err; |
| 782 | } |
| 783 | |
Andrey Smirnov | 310cdfa | 2016-11-07 08:53:14 -0800 | [diff] [blame] | 784 | static int sx150x_init_misc(struct sx150x_pinctrl *pctl) |
| 785 | { |
| 786 | u8 reg, value; |
| 787 | |
| 788 | switch (pctl->data->model) { |
| 789 | case SX150X_789: |
| 790 | reg = pctl->data->pri.x789.reg_misc; |
| 791 | value = SX150X_789_REG_MISC_AUTOCLEAR_OFF; |
| 792 | break; |
| 793 | case SX150X_456: |
| 794 | reg = pctl->data->pri.x456.reg_advance; |
| 795 | value = 0x00; |
Andrey Smirnov | b30d31e | 2016-11-07 08:53:15 -0800 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * Only SX1506 has RegAdvanced, SX1504/5 are expected |
| 799 | * to initialize this offset to zero |
| 800 | */ |
| 801 | if (!reg) |
| 802 | return 0; |
Andrey Smirnov | 310cdfa | 2016-11-07 08:53:14 -0800 | [diff] [blame] | 803 | break; |
| 804 | case SX150X_123: |
| 805 | reg = pctl->data->pri.x123.reg_advance; |
| 806 | value = 0x00; |
| 807 | break; |
| 808 | default: |
| 809 | WARN(1, "Unknown chip model %d\n", pctl->data->model); |
| 810 | return -EINVAL; |
| 811 | } |
| 812 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 813 | return regmap_write(pctl->regmap, reg, value); |
Andrey Smirnov | 310cdfa | 2016-11-07 08:53:14 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 816 | static int sx150x_init_hw(struct sx150x_pinctrl *pctl) |
| 817 | { |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 818 | const u8 reg[] = { |
| 819 | [SX150X_789] = pctl->data->pri.x789.reg_polarity, |
| 820 | [SX150X_456] = pctl->data->pri.x456.reg_pld_mode, |
| 821 | [SX150X_123] = pctl->data->pri.x123.reg_pld_mode, |
| 822 | }; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 823 | int err; |
| 824 | |
| 825 | if (pctl->data->model == SX150X_789 && |
| 826 | of_property_read_bool(pctl->dev->of_node, "semtech,probe-reset")) { |
| 827 | err = sx150x_reset(pctl); |
| 828 | if (err < 0) |
| 829 | return err; |
| 830 | } |
| 831 | |
Andrey Smirnov | 310cdfa | 2016-11-07 08:53:14 -0800 | [diff] [blame] | 832 | err = sx150x_init_misc(pctl); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 833 | if (err < 0) |
| 834 | return err; |
| 835 | |
| 836 | /* Set all pins to work in normal mode */ |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 837 | return regmap_write(pctl->regmap, reg[pctl->data->model], 0); |
| 838 | } |
| 839 | |
| 840 | static int sx150x_regmap_reg_width(struct sx150x_pinctrl *pctl, |
| 841 | unsigned int reg) |
| 842 | { |
| 843 | const struct sx150x_device_data *data = pctl->data; |
| 844 | |
| 845 | if (reg == data->reg_sense) { |
| 846 | /* |
| 847 | * RegSense packs two bits of configuration per GPIO, |
| 848 | * so we'd need to read twice as many bits as there |
| 849 | * are GPIO in our chip |
| 850 | */ |
| 851 | return 2 * data->ngpios; |
| 852 | } else if ((data->model == SX150X_789 && |
| 853 | (reg == data->pri.x789.reg_misc || |
| 854 | reg == data->pri.x789.reg_clock || |
| 855 | reg == data->pri.x789.reg_reset)) |
| 856 | || |
| 857 | (data->model == SX150X_123 && |
| 858 | reg == data->pri.x123.reg_advance) |
| 859 | || |
| 860 | (data->model == SX150X_456 && |
| 861 | reg == data->pri.x456.reg_advance)) { |
| 862 | return 8; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 863 | } else { |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 864 | return data->ngpios; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 865 | } |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl *pctl, |
| 869 | unsigned int reg, unsigned int val) |
| 870 | { |
| 871 | unsigned int a, b; |
| 872 | const struct sx150x_device_data *data = pctl->data; |
| 873 | |
| 874 | /* |
| 875 | * Whereas SX1509 presents RegSense in a simple layout as such: |
| 876 | * reg [ f f e e d d c c ] |
| 877 | * reg + 1 [ b b a a 9 9 8 8 ] |
| 878 | * reg + 2 [ 7 7 6 6 5 5 4 4 ] |
| 879 | * reg + 3 [ 3 3 2 2 1 1 0 0 ] |
| 880 | * |
| 881 | * SX1503 and SX1506 deviate from that data layout, instead storing |
| 882 | * thier contents as follows: |
| 883 | * |
| 884 | * reg [ f f e e d d c c ] |
| 885 | * reg + 1 [ 7 7 6 6 5 5 4 4 ] |
| 886 | * reg + 2 [ b b a a 9 9 8 8 ] |
| 887 | * reg + 3 [ 3 3 2 2 1 1 0 0 ] |
| 888 | * |
| 889 | * so, taking that into account, we swap two |
| 890 | * inner bytes of a 4-byte result |
| 891 | */ |
| 892 | |
| 893 | if (reg == data->reg_sense && |
| 894 | data->ngpios == 16 && |
| 895 | (data->model == SX150X_123 || |
| 896 | data->model == SX150X_456)) { |
| 897 | a = val & 0x00ff0000; |
| 898 | b = val & 0x0000ff00; |
| 899 | |
| 900 | val &= 0xff0000ff; |
| 901 | val |= b << 8; |
| 902 | val |= a >> 8; |
| 903 | } |
| 904 | |
| 905 | return val; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * In order to mask the differences between 16 and 8 bit expander |
| 910 | * devices we set up a sligthly ficticious regmap that pretends to be |
| 911 | * a set of 32-bit (to accomodate RegSenseLow/RegSenseHigh |
| 912 | * pair/quartet) registers and transparently reconstructs those |
| 913 | * registers via multiple I2C/SMBus reads |
| 914 | * |
| 915 | * This way the rest of the driver code, interfacing with the chip via |
| 916 | * regmap API, can work assuming that each GPIO pin is represented by |
| 917 | * a group of bits at an offset proportioan to GPIO number within a |
| 918 | * given register. |
| 919 | * |
| 920 | */ |
| 921 | static int sx150x_regmap_reg_read(void *context, unsigned int reg, |
| 922 | unsigned int *result) |
| 923 | { |
| 924 | int ret, n; |
| 925 | struct sx150x_pinctrl *pctl = context; |
| 926 | struct i2c_client *i2c = pctl->client; |
| 927 | const int width = sx150x_regmap_reg_width(pctl, reg); |
| 928 | unsigned int idx, val; |
| 929 | |
| 930 | /* |
| 931 | * There are four potential cases coverd by this function: |
| 932 | * |
| 933 | * 1) 8-pin chip, single configuration bit register |
| 934 | * |
| 935 | * This is trivial the code below just needs to read: |
| 936 | * reg [ 7 6 5 4 3 2 1 0 ] |
| 937 | * |
| 938 | * 2) 8-pin chip, double configuration bit register (RegSense) |
| 939 | * |
| 940 | * The read will be done as follows: |
| 941 | * reg [ 7 7 6 6 5 5 4 4 ] |
| 942 | * reg + 1 [ 3 3 2 2 1 1 0 0 ] |
| 943 | * |
| 944 | * 3) 16-pin chip, single configuration bit register |
| 945 | * |
| 946 | * The read will be done as follows: |
| 947 | * reg [ f e d c b a 9 8 ] |
| 948 | * reg + 1 [ 7 6 5 4 3 2 1 0 ] |
| 949 | * |
| 950 | * 4) 16-pin chip, double configuration bit register (RegSense) |
| 951 | * |
| 952 | * The read will be done as follows: |
| 953 | * reg [ f f e e d d c c ] |
| 954 | * reg + 1 [ b b a a 9 9 8 8 ] |
| 955 | * reg + 2 [ 7 7 6 6 5 5 4 4 ] |
| 956 | * reg + 3 [ 3 3 2 2 1 1 0 0 ] |
| 957 | */ |
| 958 | |
| 959 | for (n = width, val = 0, idx = reg; n > 0; n -= 8, idx++) { |
| 960 | val <<= 8; |
| 961 | |
| 962 | ret = i2c_smbus_read_byte_data(i2c, idx); |
| 963 | if (ret < 0) |
| 964 | return ret; |
| 965 | |
| 966 | val |= ret; |
| 967 | } |
| 968 | |
| 969 | *result = sx150x_maybe_swizzle(pctl, reg, val); |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static int sx150x_regmap_reg_write(void *context, unsigned int reg, |
| 975 | unsigned int val) |
| 976 | { |
| 977 | int ret, n; |
| 978 | struct sx150x_pinctrl *pctl = context; |
| 979 | struct i2c_client *i2c = pctl->client; |
| 980 | const int width = sx150x_regmap_reg_width(pctl, reg); |
| 981 | |
| 982 | val = sx150x_maybe_swizzle(pctl, reg, val); |
| 983 | |
| 984 | n = width - 8; |
| 985 | do { |
| 986 | const u8 byte = (val >> n) & 0xff; |
| 987 | |
| 988 | ret = i2c_smbus_write_byte_data(i2c, reg, byte); |
| 989 | if (ret < 0) |
| 990 | return ret; |
| 991 | |
| 992 | reg++; |
| 993 | n -= 8; |
| 994 | } while (n >= 0); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 999 | static bool sx150x_reg_volatile(struct device *dev, unsigned int reg) |
| 1000 | { |
| 1001 | struct sx150x_pinctrl *pctl = i2c_get_clientdata(to_i2c_client(dev)); |
| 1002 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 1003 | return reg == pctl->data->reg_irq_src || reg == pctl->data->reg_data; |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | const struct regmap_config sx150x_regmap_config = { |
| 1007 | .reg_bits = 8, |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 1008 | .val_bits = 32, |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 1009 | |
| 1010 | .cache_type = REGCACHE_RBTREE, |
| 1011 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 1012 | .reg_read = sx150x_regmap_reg_read, |
| 1013 | .reg_write = sx150x_regmap_reg_write, |
| 1014 | |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 1015 | .max_register = SX150X_MAX_REGISTER, |
| 1016 | .volatile_reg = sx150x_reg_volatile, |
| 1017 | }; |
| 1018 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1019 | static int sx150x_probe(struct i2c_client *client, |
| 1020 | const struct i2c_device_id *id) |
| 1021 | { |
| 1022 | static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA | |
| 1023 | I2C_FUNC_SMBUS_WRITE_WORD_DATA; |
| 1024 | struct device *dev = &client->dev; |
| 1025 | struct sx150x_pinctrl *pctl; |
| 1026 | int ret; |
| 1027 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1028 | if (!i2c_check_functionality(client->adapter, i2c_funcs)) |
| 1029 | return -ENOSYS; |
| 1030 | |
| 1031 | pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL); |
| 1032 | if (!pctl) |
| 1033 | return -ENOMEM; |
| 1034 | |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 1035 | i2c_set_clientdata(client, pctl); |
| 1036 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1037 | pctl->dev = dev; |
| 1038 | pctl->client = client; |
Andrey Smirnov | e3ba812 | 2016-11-07 08:53:09 -0800 | [diff] [blame] | 1039 | |
| 1040 | if (dev->of_node) |
| 1041 | pctl->data = of_device_get_match_data(dev); |
| 1042 | else |
| 1043 | pctl->data = (struct sx150x_device_data *)id->driver_data; |
| 1044 | |
| 1045 | if (!pctl->data) |
| 1046 | return -EINVAL; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1047 | |
Andrey Smirnov | 6489677 | 2016-11-07 08:53:17 -0800 | [diff] [blame] | 1048 | pctl->regmap = devm_regmap_init(dev, NULL, pctl, |
| 1049 | &sx150x_regmap_config); |
Andrey Smirnov | 0db0f26 | 2016-11-07 08:53:16 -0800 | [diff] [blame] | 1050 | if (IS_ERR(pctl->regmap)) { |
| 1051 | ret = PTR_ERR(pctl->regmap); |
| 1052 | dev_err(dev, "Failed to allocate register map: %d\n", |
| 1053 | ret); |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1057 | mutex_init(&pctl->lock); |
| 1058 | |
| 1059 | ret = sx150x_init_hw(pctl); |
| 1060 | if (ret) |
| 1061 | return ret; |
| 1062 | |
| 1063 | /* Register GPIO controller */ |
| 1064 | pctl->gpio.label = devm_kstrdup(dev, client->name, GFP_KERNEL); |
| 1065 | pctl->gpio.base = -1; |
| 1066 | pctl->gpio.ngpio = pctl->data->npins; |
| 1067 | pctl->gpio.get_direction = sx150x_gpio_get_direction; |
| 1068 | pctl->gpio.direction_input = sx150x_gpio_direction_input; |
| 1069 | pctl->gpio.direction_output = sx150x_gpio_direction_output; |
| 1070 | pctl->gpio.get = sx150x_gpio_get; |
| 1071 | pctl->gpio.set = sx150x_gpio_set; |
| 1072 | pctl->gpio.set_single_ended = sx150x_gpio_set_single_ended; |
| 1073 | pctl->gpio.parent = dev; |
| 1074 | #ifdef CONFIG_OF_GPIO |
| 1075 | pctl->gpio.of_node = dev->of_node; |
| 1076 | #endif |
| 1077 | pctl->gpio.can_sleep = true; |
| 1078 | |
| 1079 | ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl); |
| 1080 | if (ret) |
| 1081 | return ret; |
| 1082 | |
| 1083 | /* Add Interrupt support if an irq is specified */ |
| 1084 | if (client->irq > 0) { |
| 1085 | pctl->irq_chip.name = devm_kstrdup(dev, client->name, |
| 1086 | GFP_KERNEL); |
| 1087 | pctl->irq_chip.irq_mask = sx150x_irq_mask; |
| 1088 | pctl->irq_chip.irq_unmask = sx150x_irq_unmask; |
| 1089 | pctl->irq_chip.irq_set_type = sx150x_irq_set_type; |
| 1090 | pctl->irq_chip.irq_bus_lock = sx150x_irq_bus_lock; |
| 1091 | pctl->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock; |
| 1092 | |
| 1093 | pctl->irq.masked = ~0; |
| 1094 | pctl->irq.sense = 0; |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1095 | |
Andrey Smirnov | 080c489 | 2016-11-07 08:53:21 -0800 | [diff] [blame] | 1096 | /* |
| 1097 | * Because sx150x_irq_threaded_fn invokes all of the |
| 1098 | * nested interrrupt handlers via handle_nested_irq, |
| 1099 | * any "handler" passed to gpiochip_irqchip_add() |
| 1100 | * below is going to be ignored, so the choice of the |
| 1101 | * function does not matter that much. |
| 1102 | * |
| 1103 | * We set it to handle_bad_irq to avoid confusion, |
| 1104 | * plus it will be instantly noticeable if it is ever |
| 1105 | * called (should not happen) |
| 1106 | */ |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1107 | ret = gpiochip_irqchip_add(&pctl->gpio, |
| 1108 | &pctl->irq_chip, 0, |
Andrey Smirnov | 080c489 | 2016-11-07 08:53:21 -0800 | [diff] [blame] | 1109 | handle_bad_irq, IRQ_TYPE_NONE); |
Neil Armstrong | 9e80f90 | 2016-10-21 11:09:58 +0200 | [diff] [blame] | 1110 | if (ret) { |
| 1111 | dev_err(dev, "could not connect irqchip to gpiochip\n"); |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
| 1115 | ret = devm_request_threaded_irq(dev, client->irq, NULL, |
| 1116 | sx150x_irq_thread_fn, |
| 1117 | IRQF_ONESHOT | IRQF_SHARED | |
| 1118 | IRQF_TRIGGER_FALLING, |
| 1119 | pctl->irq_chip.name, pctl); |
| 1120 | if (ret < 0) |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | /* Pinctrl_desc */ |
| 1125 | pctl->pinctrl_desc.name = "sx150x-pinctrl"; |
| 1126 | pctl->pinctrl_desc.pctlops = &sx150x_pinctrl_ops; |
| 1127 | pctl->pinctrl_desc.confops = &sx150x_pinconf_ops; |
| 1128 | pctl->pinctrl_desc.pins = pctl->data->pins; |
| 1129 | pctl->pinctrl_desc.npins = pctl->data->npins; |
| 1130 | pctl->pinctrl_desc.owner = THIS_MODULE; |
| 1131 | |
| 1132 | pctl->pctldev = pinctrl_register(&pctl->pinctrl_desc, dev, pctl); |
| 1133 | if (IS_ERR(pctl->pctldev)) { |
| 1134 | dev_err(dev, "Failed to register pinctrl device\n"); |
| 1135 | return PTR_ERR(pctl->pctldev); |
| 1136 | } |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | static struct i2c_driver sx150x_driver = { |
| 1142 | .driver = { |
| 1143 | .name = "sx150x-pinctrl", |
| 1144 | .of_match_table = of_match_ptr(sx150x_of_match), |
| 1145 | }, |
| 1146 | .probe = sx150x_probe, |
| 1147 | .id_table = sx150x_id, |
| 1148 | }; |
| 1149 | |
| 1150 | static int __init sx150x_init(void) |
| 1151 | { |
| 1152 | return i2c_add_driver(&sx150x_driver); |
| 1153 | } |
| 1154 | subsys_initcall(sx150x_init); |