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