Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pinctrl: Add Cypress cy8c95x0 support

Add support for cypress I2C GPIO expanders cy8c9520, cy8c9540 and
cy8c9560. The GPIO expanders feature a PWM mode, thus add it as
pinctrl driver.

The chip features multiple drive modes for each pin when configured
as output and multiple bias settings when configured as input.

Tested all three components and verified that all functionality
is fully working.

Datasheet: https://www.cypress.com/file/37971/download
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
Link: https://lore.kernel.org/r/20220816054917.7893-3-patrick.rudolph@9elements.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Patrick Rudolph and committed by
Linus Walleij
e6cbbe42 8d39e55e

+1402
+6
MAINTAINERS
··· 5629 5629 T: git git://linuxtv.org/anttip/media_tree.git 5630 5630 F: drivers/media/common/cypress_firmware* 5631 5631 5632 + CYPRESS CY8C95X0 PINCTRL DRIVER 5633 + M: Patrick Rudolph <patrick.rudolph@9elements.com> 5634 + L: linux-gpio@vger.kernel.org 5635 + S: Maintained 5636 + F: drivers/pinctrl/pinctrl-cy8c95x0.c 5637 + 5632 5638 CYPRESS CY8CTMA140 TOUCHSCREEN DRIVER 5633 5639 M: Linus Walleij <linus.walleij@linaro.org> 5634 5640 L: linux-input@vger.kernel.org
+14
drivers/pinctrl/Kconfig
··· 135 135 help 136 136 Pinctrl driver for Bitmain BM1880 SoC. 137 137 138 + config PINCTRL_CY8C95X0 139 + tristate "Cypress CY8C95X0 I2C pinctrl and GPIO driver" 140 + depends on I2C && OF 141 + select GPIOLIB 142 + select GPIOLIB_IRQCHIP 143 + select PINMUX 144 + select PINCONF 145 + select GENERIC_PINCONF 146 + select REGMAP_I2C 147 + help 148 + Support for 20/40/60 pin Cypress Cy8C95x0 pinctrl/gpio I2C expander. 149 + This driver can also be built as a module. If so, the module will be 150 + called pinctrl-cy8c95x0. 151 + 138 152 config PINCTRL_DA850_PUPD 139 153 tristate "TI DA850/OMAP-L138/AM18XX pull-up and pull-down groups" 140 154 depends on OF && (ARCH_DAVINCI_DA850 || COMPILE_TEST)
+1
drivers/pinctrl/Makefile
··· 17 17 obj-$(CONFIG_PINCTRL_AT91PIO4) += pinctrl-at91-pio4.o 18 18 obj-$(CONFIG_PINCTRL_AXP209) += pinctrl-axp209.o 19 19 obj-$(CONFIG_PINCTRL_BM1880) += pinctrl-bm1880.o 20 + obj-$(CONFIG_PINCTRL_CY8C95X0) += pinctrl-cy8c95x0.o 20 21 obj-$(CONFIG_PINCTRL_DA850_PUPD) += pinctrl-da850-pupd.o 21 22 obj-$(CONFIG_PINCTRL_DA9062) += pinctrl-da9062.o 22 23 obj-$(CONFIG_PINCTRL_DIGICOLOR) += pinctrl-digicolor.o
+1381
drivers/pinctrl/pinctrl-cy8c95x0.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * CY8C95X0 20/40/60 pin I2C GPIO port expander with interrupt support 4 + * 5 + * Copyright (C) 2022 9elements GmbH 6 + * Author: Patrick Rudolph <patrick.rudolph@9elements.com> 7 + * Author: Naresh Solanki <Naresh.Solanki@9elements.com> 8 + */ 9 + 10 + #include <linux/bitmap.h> 11 + #include <linux/gpio/driver.h> 12 + #include <linux/gpio/consumer.h> 13 + #include <linux/i2c.h> 14 + #include <linux/init.h> 15 + #include <linux/interrupt.h> 16 + #include <linux/module.h> 17 + #include <linux/of.h> 18 + #include <linux/of_platform.h> 19 + #include <linux/pinctrl/pinctrl.h> 20 + #include <linux/pinctrl/pinconf.h> 21 + #include <linux/pinctrl/pinconf-generic.h> 22 + #include <linux/pinctrl/pinmux.h> 23 + #include <linux/regmap.h> 24 + #include <linux/regulator/consumer.h> 25 + 26 + /* Fast access registers */ 27 + #define CY8C95X0_INPUT 0x00 28 + #define CY8C95X0_OUTPUT 0x08 29 + #define CY8C95X0_INTSTATUS 0x10 30 + 31 + #define CY8C95X0_INPUT_(x) (CY8C95X0_INPUT + (x)) 32 + #define CY8C95X0_OUTPUT_(x) (CY8C95X0_OUTPUT + (x)) 33 + #define CY8C95X0_INTSTATUS_(x) (CY8C95X0_INTSTATUS + (x)) 34 + 35 + /* Port Select configures the port */ 36 + #define CY8C95X0_PORTSEL 0x18 37 + /* port settings, write PORTSEL first */ 38 + #define CY8C95X0_INTMASK 0x19 39 + #define CY8C95X0_PWMSEL 0x1A 40 + #define CY8C95X0_INVERT 0x1B 41 + #define CY8C95X0_DIRECTION 0x1C 42 + /* Drive mode register change state on writing '1' */ 43 + #define CY8C95X0_DRV_PU 0x1D 44 + #define CY8C95X0_DRV_PD 0x1E 45 + #define CY8C95X0_DRV_ODH 0x1F 46 + #define CY8C95X0_DRV_ODL 0x20 47 + #define CY8C95X0_DRV_PP_FAST 0x21 48 + #define CY8C95X0_DRV_PP_SLOW 0x22 49 + #define CY8C95X0_DRV_HIZ 0x23 50 + #define CY8C95X0_DEVID 0x2E 51 + #define CY8C95X0_WATCHDOG 0x2F 52 + #define CY8C95X0_COMMAND 0x30 53 + 54 + #define CY8C95X0_PIN_TO_OFFSET(x) (((x) >= 20) ? ((x) + 4) : (x)) 55 + 56 + static const struct i2c_device_id cy8c95x0_id[] = { 57 + { "cy8c9520", 20, }, 58 + { "cy8c9540", 40, }, 59 + { "cy8c9560", 60, }, 60 + { } 61 + }; 62 + MODULE_DEVICE_TABLE(i2c, cy8c95x0_id); 63 + 64 + #define OF_CY8C95X(__nrgpio) ((void *)(__nrgpio)) 65 + 66 + static const struct of_device_id cy8c95x0_dt_ids[] = { 67 + { .compatible = "cypress,cy8c9520", .data = OF_CY8C95X(20), }, 68 + { .compatible = "cypress,cy8c9540", .data = OF_CY8C95X(40), }, 69 + { .compatible = "cypress,cy8c9560", .data = OF_CY8C95X(60), }, 70 + { } 71 + }; 72 + 73 + MODULE_DEVICE_TABLE(of, cy8c95x0_dt_ids); 74 + 75 + #define MAX_BANK 8 76 + #define BANK_SZ 8 77 + #define MAX_LINE (MAX_BANK * BANK_SZ) 78 + 79 + #define CY8C95X0_GPIO_MASK GENMASK(7, 0) 80 + 81 + /** 82 + * struct cy8c95x0_pinctrl - driver data 83 + * @regmap: Device's regmap 84 + * @irq_lock: IRQ bus lock 85 + * @i2c_lock: Mutex for the device internal mux register 86 + * @irq_mask: I/O bits affected by interrupts 87 + * @irq_trig_raise: I/O bits affected by raising voltage level 88 + * @irq_trig_fall: I/O bits affected by falling voltage level 89 + * @irq_trig_low: I/O bits affected by a low voltage level 90 + * @irq_trig_high: I/O bits affected by a high voltage level 91 + * @push_pull: I/O bits configured as push pull driver 92 + * @shiftmask: Mask used to compensate for Gport2 width 93 + * @irq_chip: IRQ chip configuration 94 + * @nport: Number of Gports in this chip 95 + * @gpio_chip: gpiolib chip 96 + * @driver_data: private driver data 97 + * @regulator: Pointer to the regulator for the IC 98 + * @dev: struct device 99 + * @pctldev: pin controller device 100 + * @pinctrl_desc: pin controller description 101 + * @name: Chip controller name 102 + * @tpin: Total number of pins 103 + */ 104 + struct cy8c95x0_pinctrl { 105 + struct regmap *regmap; 106 + struct mutex irq_lock; 107 + struct mutex i2c_lock; 108 + DECLARE_BITMAP(irq_mask, MAX_LINE); 109 + DECLARE_BITMAP(irq_trig_raise, MAX_LINE); 110 + DECLARE_BITMAP(irq_trig_fall, MAX_LINE); 111 + DECLARE_BITMAP(irq_trig_low, MAX_LINE); 112 + DECLARE_BITMAP(irq_trig_high, MAX_LINE); 113 + DECLARE_BITMAP(push_pull, MAX_LINE); 114 + DECLARE_BITMAP(shiftmask, MAX_LINE); 115 + struct irq_chip irq_chip; 116 + int nport; 117 + struct gpio_chip gpio_chip; 118 + unsigned long driver_data; 119 + struct regulator *regulator; 120 + struct device *dev; 121 + struct pinctrl_dev *pctldev; 122 + struct pinctrl_desc pinctrl_desc; 123 + char name[32]; 124 + unsigned int tpin; 125 + }; 126 + 127 + static const struct pinctrl_pin_desc cy8c9560_pins[] = { 128 + PINCTRL_PIN(0, "gp00"), 129 + PINCTRL_PIN(1, "gp01"), 130 + PINCTRL_PIN(2, "gp02"), 131 + PINCTRL_PIN(3, "gp03"), 132 + PINCTRL_PIN(4, "gp04"), 133 + PINCTRL_PIN(5, "gp05"), 134 + PINCTRL_PIN(6, "gp06"), 135 + PINCTRL_PIN(7, "gp07"), 136 + 137 + PINCTRL_PIN(8, "gp10"), 138 + PINCTRL_PIN(9, "gp11"), 139 + PINCTRL_PIN(10, "gp12"), 140 + PINCTRL_PIN(11, "gp13"), 141 + PINCTRL_PIN(12, "gp14"), 142 + PINCTRL_PIN(13, "gp15"), 143 + PINCTRL_PIN(14, "gp16"), 144 + PINCTRL_PIN(15, "gp17"), 145 + 146 + PINCTRL_PIN(16, "gp20"), 147 + PINCTRL_PIN(17, "gp21"), 148 + PINCTRL_PIN(18, "gp22"), 149 + PINCTRL_PIN(19, "gp23"), 150 + 151 + PINCTRL_PIN(20, "gp30"), 152 + PINCTRL_PIN(21, "gp31"), 153 + PINCTRL_PIN(22, "gp32"), 154 + PINCTRL_PIN(23, "gp33"), 155 + PINCTRL_PIN(24, "gp34"), 156 + PINCTRL_PIN(25, "gp35"), 157 + PINCTRL_PIN(26, "gp36"), 158 + PINCTRL_PIN(27, "gp37"), 159 + 160 + PINCTRL_PIN(28, "gp40"), 161 + PINCTRL_PIN(29, "gp41"), 162 + PINCTRL_PIN(30, "gp42"), 163 + PINCTRL_PIN(31, "gp43"), 164 + PINCTRL_PIN(32, "gp44"), 165 + PINCTRL_PIN(33, "gp45"), 166 + PINCTRL_PIN(34, "gp46"), 167 + PINCTRL_PIN(35, "gp47"), 168 + 169 + PINCTRL_PIN(36, "gp50"), 170 + PINCTRL_PIN(37, "gp51"), 171 + PINCTRL_PIN(38, "gp52"), 172 + PINCTRL_PIN(39, "gp53"), 173 + PINCTRL_PIN(40, "gp54"), 174 + PINCTRL_PIN(41, "gp55"), 175 + PINCTRL_PIN(42, "gp56"), 176 + PINCTRL_PIN(43, "gp57"), 177 + 178 + PINCTRL_PIN(44, "gp60"), 179 + PINCTRL_PIN(45, "gp61"), 180 + PINCTRL_PIN(46, "gp62"), 181 + PINCTRL_PIN(47, "gp63"), 182 + PINCTRL_PIN(48, "gp64"), 183 + PINCTRL_PIN(49, "gp65"), 184 + PINCTRL_PIN(50, "gp66"), 185 + PINCTRL_PIN(51, "gp67"), 186 + 187 + PINCTRL_PIN(52, "gp70"), 188 + PINCTRL_PIN(53, "gp71"), 189 + PINCTRL_PIN(54, "gp72"), 190 + PINCTRL_PIN(55, "gp73"), 191 + PINCTRL_PIN(56, "gp74"), 192 + PINCTRL_PIN(57, "gp75"), 193 + PINCTRL_PIN(58, "gp76"), 194 + PINCTRL_PIN(59, "gp77"), 195 + }; 196 + 197 + static const char * const cy8c95x0_groups[] = { 198 + "gp00", 199 + "gp01", 200 + "gp02", 201 + "gp03", 202 + "gp04", 203 + "gp05", 204 + "gp06", 205 + "gp07", 206 + 207 + "gp10", 208 + "gp11", 209 + "gp12", 210 + "gp13", 211 + "gp14", 212 + "gp15", 213 + "gp16", 214 + "gp17", 215 + 216 + "gp20", 217 + "gp21", 218 + "gp22", 219 + "gp23", 220 + 221 + "gp30", 222 + "gp31", 223 + "gp32", 224 + "gp33", 225 + "gp34", 226 + "gp35", 227 + "gp36", 228 + "gp37", 229 + 230 + "gp40", 231 + "gp41", 232 + "gp42", 233 + "gp43", 234 + "gp44", 235 + "gp45", 236 + "gp46", 237 + "gp47", 238 + 239 + "gp50", 240 + "gp51", 241 + "gp52", 242 + "gp53", 243 + "gp54", 244 + "gp55", 245 + "gp56", 246 + "gp57", 247 + 248 + "gp60", 249 + "gp61", 250 + "gp62", 251 + "gp63", 252 + "gp64", 253 + "gp65", 254 + "gp66", 255 + "gp67", 256 + 257 + "gp70", 258 + "gp71", 259 + "gp72", 260 + "gp73", 261 + "gp74", 262 + "gp75", 263 + "gp76", 264 + "gp77", 265 + }; 266 + 267 + static inline u8 cypress_get_port(struct cy8c95x0_pinctrl *chip, unsigned int pin) 268 + { 269 + /* Account for GPORT2 which only has 4 bits */ 270 + return CY8C95X0_PIN_TO_OFFSET(pin) / BANK_SZ; 271 + } 272 + 273 + static int cypress_get_pin_mask(struct cy8c95x0_pinctrl *chip, unsigned int pin) 274 + { 275 + /* Account for GPORT2 which only has 4 bits */ 276 + return BIT(CY8C95X0_PIN_TO_OFFSET(pin) % BANK_SZ); 277 + } 278 + 279 + static bool cy8c95x0_readable_register(struct device *dev, unsigned int reg) 280 + { 281 + switch (reg) { 282 + case 0x24 ... 0x27: 283 + return false; 284 + } 285 + 286 + return true; 287 + } 288 + 289 + static bool cy8c95x0_writeable_register(struct device *dev, unsigned int reg) 290 + { 291 + switch (reg) { 292 + case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7): 293 + return false; 294 + case CY8C95X0_DEVID: 295 + return false; 296 + case 0x24 ... 0x27: 297 + return false; 298 + } 299 + 300 + return true; 301 + } 302 + 303 + static bool cy8c95x0_volatile_register(struct device *dev, unsigned int reg) 304 + { 305 + switch (reg) { 306 + case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7): 307 + case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7): 308 + case CY8C95X0_INTMASK: 309 + case CY8C95X0_INVERT: 310 + case CY8C95X0_PWMSEL: 311 + case CY8C95X0_DIRECTION: 312 + case CY8C95X0_DRV_PU: 313 + case CY8C95X0_DRV_PD: 314 + case CY8C95X0_DRV_ODH: 315 + case CY8C95X0_DRV_ODL: 316 + case CY8C95X0_DRV_PP_FAST: 317 + case CY8C95X0_DRV_PP_SLOW: 318 + case CY8C95X0_DRV_HIZ: 319 + return true; 320 + } 321 + 322 + return false; 323 + } 324 + 325 + static bool cy8c95x0_precious_register(struct device *dev, unsigned int reg) 326 + { 327 + switch (reg) { 328 + case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7): 329 + return true; 330 + } 331 + 332 + return false; 333 + } 334 + 335 + static const struct reg_default cy8c95x0_reg_defaults[] = { 336 + { CY8C95X0_OUTPUT_(0), 0xff }, 337 + { CY8C95X0_OUTPUT_(1), 0xff }, 338 + { CY8C95X0_OUTPUT_(2), 0xff }, 339 + { CY8C95X0_OUTPUT_(3), 0xff }, 340 + { CY8C95X0_OUTPUT_(4), 0xff }, 341 + { CY8C95X0_OUTPUT_(5), 0xff }, 342 + { CY8C95X0_OUTPUT_(6), 0xff }, 343 + { CY8C95X0_OUTPUT_(7), 0xff }, 344 + { CY8C95X0_PORTSEL, 0 }, 345 + { CY8C95X0_PWMSEL, 0 }, 346 + }; 347 + 348 + static const struct regmap_config cy8c95x0_i2c_regmap = { 349 + .reg_bits = 8, 350 + .val_bits = 8, 351 + 352 + .reg_defaults = cy8c95x0_reg_defaults, 353 + .num_reg_defaults = ARRAY_SIZE(cy8c95x0_reg_defaults), 354 + 355 + .readable_reg = cy8c95x0_readable_register, 356 + .writeable_reg = cy8c95x0_writeable_register, 357 + .volatile_reg = cy8c95x0_volatile_register, 358 + .precious_reg = cy8c95x0_precious_register, 359 + 360 + .cache_type = REGCACHE_FLAT, 361 + .max_register = CY8C95X0_COMMAND, 362 + }; 363 + 364 + static int cy8c95x0_write_regs_mask(struct cy8c95x0_pinctrl *chip, int reg, 365 + unsigned long *val, unsigned long *mask) 366 + { 367 + DECLARE_BITMAP(tmask, MAX_LINE); 368 + DECLARE_BITMAP(tval, MAX_LINE); 369 + int write_val; 370 + int ret = 0; 371 + int i, off = 0; 372 + u8 bits; 373 + 374 + /* Add the 4 bit gap of Gport2 */ 375 + bitmap_andnot(tmask, mask, chip->shiftmask, MAX_LINE); 376 + bitmap_shift_left(tmask, tmask, 4, MAX_LINE); 377 + bitmap_replace(tmask, tmask, mask, chip->shiftmask, BANK_SZ * 3); 378 + 379 + bitmap_andnot(tval, val, chip->shiftmask, MAX_LINE); 380 + bitmap_shift_left(tval, tval, 4, MAX_LINE); 381 + bitmap_replace(tval, tval, val, chip->shiftmask, BANK_SZ * 3); 382 + 383 + mutex_lock(&chip->i2c_lock); 384 + for (i = 0; i < chip->nport; i++) { 385 + /* Skip over unused banks */ 386 + bits = bitmap_get_value8(tmask, i * BANK_SZ); 387 + if (!bits) 388 + continue; 389 + 390 + switch (reg) { 391 + /* muxed registers */ 392 + case CY8C95X0_INTMASK: 393 + case CY8C95X0_PWMSEL: 394 + case CY8C95X0_INVERT: 395 + case CY8C95X0_DIRECTION: 396 + case CY8C95X0_DRV_PU: 397 + case CY8C95X0_DRV_PD: 398 + case CY8C95X0_DRV_ODH: 399 + case CY8C95X0_DRV_ODL: 400 + case CY8C95X0_DRV_PP_FAST: 401 + case CY8C95X0_DRV_PP_SLOW: 402 + case CY8C95X0_DRV_HIZ: 403 + ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, i); 404 + if (ret < 0) 405 + goto out; 406 + off = reg; 407 + break; 408 + /* direct access registers */ 409 + case CY8C95X0_INPUT: 410 + case CY8C95X0_OUTPUT: 411 + case CY8C95X0_INTSTATUS: 412 + off = reg + i; 413 + break; 414 + default: 415 + ret = -EINVAL; 416 + goto out; 417 + } 418 + 419 + write_val = bitmap_get_value8(tval, i * BANK_SZ); 420 + 421 + ret = regmap_update_bits(chip->regmap, off, bits, write_val); 422 + if (ret < 0) 423 + goto out; 424 + } 425 + out: 426 + mutex_unlock(&chip->i2c_lock); 427 + 428 + if (ret < 0) 429 + dev_err(chip->dev, "failed writing register %d: err %d\n", off, ret); 430 + 431 + return ret; 432 + } 433 + 434 + static int cy8c95x0_read_regs_mask(struct cy8c95x0_pinctrl *chip, int reg, 435 + unsigned long *val, unsigned long *mask) 436 + { 437 + DECLARE_BITMAP(tmask, MAX_LINE); 438 + DECLARE_BITMAP(tval, MAX_LINE); 439 + DECLARE_BITMAP(tmp, MAX_LINE); 440 + int read_val; 441 + int ret = 0; 442 + int i, off = 0; 443 + u8 bits; 444 + 445 + /* Add the 4 bit gap of Gport2 */ 446 + bitmap_andnot(tmask, mask, chip->shiftmask, MAX_LINE); 447 + bitmap_shift_left(tmask, tmask, 4, MAX_LINE); 448 + bitmap_replace(tmask, tmask, mask, chip->shiftmask, BANK_SZ * 3); 449 + 450 + bitmap_andnot(tval, val, chip->shiftmask, MAX_LINE); 451 + bitmap_shift_left(tval, tval, 4, MAX_LINE); 452 + bitmap_replace(tval, tval, val, chip->shiftmask, BANK_SZ * 3); 453 + 454 + mutex_lock(&chip->i2c_lock); 455 + for (i = 0; i < chip->nport; i++) { 456 + /* Skip over unused banks */ 457 + bits = bitmap_get_value8(tmask, i * BANK_SZ); 458 + if (!bits) 459 + continue; 460 + 461 + switch (reg) { 462 + /* muxed registers */ 463 + case CY8C95X0_INTMASK: 464 + case CY8C95X0_PWMSEL: 465 + case CY8C95X0_INVERT: 466 + case CY8C95X0_DIRECTION: 467 + case CY8C95X0_DRV_PU: 468 + case CY8C95X0_DRV_PD: 469 + case CY8C95X0_DRV_ODH: 470 + case CY8C95X0_DRV_ODL: 471 + case CY8C95X0_DRV_PP_FAST: 472 + case CY8C95X0_DRV_PP_SLOW: 473 + case CY8C95X0_DRV_HIZ: 474 + ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, i); 475 + if (ret < 0) 476 + goto out; 477 + off = reg; 478 + break; 479 + /* direct access registers */ 480 + case CY8C95X0_INPUT: 481 + case CY8C95X0_OUTPUT: 482 + case CY8C95X0_INTSTATUS: 483 + off = reg + i; 484 + break; 485 + default: 486 + ret = -EINVAL; 487 + goto out; 488 + } 489 + 490 + ret = regmap_read(chip->regmap, off, &read_val); 491 + if (ret < 0) 492 + goto out; 493 + 494 + read_val &= bits; 495 + read_val |= bitmap_get_value8(tval, i * BANK_SZ) & ~bits; 496 + bitmap_set_value8(tval, read_val, i * BANK_SZ); 497 + } 498 + 499 + /* Fill the 4 bit gap of Gport2 */ 500 + bitmap_shift_right(tmp, tval, 4, MAX_LINE); 501 + bitmap_replace(val, tmp, tval, chip->shiftmask, MAX_LINE); 502 + 503 + out: 504 + mutex_unlock(&chip->i2c_lock); 505 + 506 + if (ret < 0) 507 + dev_err(chip->dev, "failed reading register %d: err %d\n", off, ret); 508 + 509 + return ret; 510 + } 511 + 512 + static int cy8c95x0_gpio_direction_input(struct gpio_chip *gc, unsigned int off) 513 + { 514 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 515 + u8 port = cypress_get_port(chip, off); 516 + u8 bit = cypress_get_pin_mask(chip, off); 517 + int ret; 518 + 519 + mutex_lock(&chip->i2c_lock); 520 + ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port); 521 + if (ret) 522 + goto out; 523 + 524 + ret = regmap_write_bits(chip->regmap, CY8C95X0_DIRECTION, bit, bit); 525 + if (ret) 526 + goto out; 527 + 528 + if (test_bit(off, chip->push_pull)) { 529 + /* 530 + * Disable driving the pin by forcing it to HighZ. Only setting the 531 + * direction register isn't sufficient in Push-Pull mode. 532 + */ 533 + ret = regmap_write_bits(chip->regmap, CY8C95X0_DRV_HIZ, bit, bit); 534 + if (ret) 535 + goto out; 536 + clear_bit(off, chip->push_pull); 537 + } 538 + 539 + out: 540 + mutex_unlock(&chip->i2c_lock); 541 + 542 + return ret; 543 + } 544 + 545 + static int cy8c95x0_gpio_direction_output(struct gpio_chip *gc, 546 + unsigned int off, int val) 547 + { 548 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 549 + u8 port = cypress_get_port(chip, off); 550 + u8 outreg = CY8C95X0_OUTPUT_(port); 551 + u8 bit = cypress_get_pin_mask(chip, off); 552 + int ret; 553 + 554 + /* set output level */ 555 + ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0); 556 + if (ret) 557 + return ret; 558 + 559 + mutex_lock(&chip->i2c_lock); 560 + /* select port */ 561 + ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port); 562 + if (ret) 563 + goto out; 564 + 565 + /* then direction */ 566 + ret = regmap_write_bits(chip->regmap, CY8C95X0_DIRECTION, bit, 0); 567 + 568 + out: 569 + mutex_unlock(&chip->i2c_lock); 570 + 571 + return ret; 572 + } 573 + 574 + static int cy8c95x0_gpio_get_value(struct gpio_chip *gc, unsigned int off) 575 + { 576 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 577 + u8 inreg = CY8C95X0_INPUT_(cypress_get_port(chip, off)); 578 + u8 bit = cypress_get_pin_mask(chip, off); 579 + u32 reg_val; 580 + int ret; 581 + 582 + ret = regmap_read(chip->regmap, inreg, &reg_val); 583 + if (ret < 0) { 584 + /* 585 + * NOTE: 586 + * diagnostic already emitted; that's all we should 587 + * do unless gpio_*_value_cansleep() calls become different 588 + * from their nonsleeping siblings (and report faults). 589 + */ 590 + return 0; 591 + } 592 + 593 + return !!(reg_val & bit); 594 + } 595 + 596 + static void cy8c95x0_gpio_set_value(struct gpio_chip *gc, unsigned int off, 597 + int val) 598 + { 599 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 600 + u8 outreg = CY8C95X0_OUTPUT_(cypress_get_port(chip, off)); 601 + u8 bit = cypress_get_pin_mask(chip, off); 602 + 603 + regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0); 604 + } 605 + 606 + static int cy8c95x0_gpio_get_direction(struct gpio_chip *gc, unsigned int off) 607 + { 608 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 609 + u8 port = cypress_get_port(chip, off); 610 + u8 bit = cypress_get_pin_mask(chip, off); 611 + u32 reg_val; 612 + int ret; 613 + 614 + mutex_lock(&chip->i2c_lock); 615 + 616 + ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port); 617 + if (ret < 0) 618 + goto out; 619 + 620 + ret = regmap_read(chip->regmap, CY8C95X0_DIRECTION, &reg_val); 621 + if (ret < 0) 622 + goto out; 623 + 624 + mutex_unlock(&chip->i2c_lock); 625 + 626 + if (reg_val & bit) 627 + return GPIO_LINE_DIRECTION_IN; 628 + 629 + return GPIO_LINE_DIRECTION_OUT; 630 + out: 631 + mutex_unlock(&chip->i2c_lock); 632 + return ret; 633 + } 634 + 635 + static int cy8c95x0_gpio_get_pincfg(struct cy8c95x0_pinctrl *chip, 636 + unsigned int off, 637 + unsigned long *config) 638 + { 639 + enum pin_config_param param = pinconf_to_config_param(*config); 640 + u8 port = cypress_get_port(chip, off); 641 + u8 bit = cypress_get_pin_mask(chip, off); 642 + unsigned int reg; 643 + u32 reg_val; 644 + u16 arg = 0; 645 + int ret; 646 + 647 + mutex_lock(&chip->i2c_lock); 648 + 649 + /* select port */ 650 + ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port); 651 + if (ret < 0) 652 + goto out; 653 + 654 + switch (param) { 655 + case PIN_CONFIG_BIAS_PULL_UP: 656 + reg = CY8C95X0_DRV_PU; 657 + break; 658 + case PIN_CONFIG_BIAS_PULL_DOWN: 659 + reg = CY8C95X0_DRV_PD; 660 + break; 661 + case PIN_CONFIG_BIAS_DISABLE: 662 + reg = CY8C95X0_DRV_HIZ; 663 + break; 664 + case PIN_CONFIG_DRIVE_OPEN_DRAIN: 665 + reg = CY8C95X0_DRV_ODL; 666 + break; 667 + case PIN_CONFIG_DRIVE_OPEN_SOURCE: 668 + reg = CY8C95X0_DRV_ODH; 669 + break; 670 + case PIN_CONFIG_DRIVE_PUSH_PULL: 671 + reg = CY8C95X0_DRV_PP_FAST; 672 + break; 673 + case PIN_CONFIG_INPUT_ENABLE: 674 + reg = CY8C95X0_DIRECTION; 675 + break; 676 + case PIN_CONFIG_MODE_PWM: 677 + reg = CY8C95X0_PWMSEL; 678 + break; 679 + case PIN_CONFIG_OUTPUT: 680 + reg = CY8C95X0_OUTPUT_(port); 681 + break; 682 + case PIN_CONFIG_OUTPUT_ENABLE: 683 + reg = CY8C95X0_DIRECTION; 684 + break; 685 + 686 + case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: 687 + case PIN_CONFIG_BIAS_BUS_HOLD: 688 + case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: 689 + case PIN_CONFIG_DRIVE_STRENGTH: 690 + case PIN_CONFIG_DRIVE_STRENGTH_UA: 691 + case PIN_CONFIG_INPUT_DEBOUNCE: 692 + case PIN_CONFIG_INPUT_SCHMITT: 693 + case PIN_CONFIG_INPUT_SCHMITT_ENABLE: 694 + case PIN_CONFIG_MODE_LOW_POWER: 695 + case PIN_CONFIG_PERSIST_STATE: 696 + case PIN_CONFIG_POWER_SOURCE: 697 + case PIN_CONFIG_SKEW_DELAY: 698 + case PIN_CONFIG_SLEEP_HARDWARE_STATE: 699 + case PIN_CONFIG_SLEW_RATE: 700 + default: 701 + ret = -ENOTSUPP; 702 + goto out; 703 + } 704 + /* Writing 1 to one of the drive mode registers will automatically 705 + * clear conflicting set bits in the other drive mode registers. 706 + */ 707 + ret = regmap_read(chip->regmap, reg, &reg_val); 708 + if (reg_val & bit) 709 + arg = 1; 710 + 711 + *config = pinconf_to_config_packed(param, (u16)arg); 712 + out: 713 + mutex_unlock(&chip->i2c_lock); 714 + 715 + return ret; 716 + } 717 + 718 + static int cy8c95x0_gpio_set_pincfg(struct cy8c95x0_pinctrl *chip, 719 + unsigned int off, 720 + unsigned long config) 721 + { 722 + u8 port = cypress_get_port(chip, off); 723 + u8 bit = cypress_get_pin_mask(chip, off); 724 + unsigned long param = pinconf_to_config_param(config); 725 + unsigned int reg; 726 + int ret; 727 + 728 + mutex_lock(&chip->i2c_lock); 729 + 730 + /* select port */ 731 + ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port); 732 + if (ret < 0) 733 + goto out; 734 + 735 + switch (param) { 736 + case PIN_CONFIG_BIAS_PULL_UP: 737 + clear_bit(off, chip->push_pull); 738 + reg = CY8C95X0_DRV_PU; 739 + break; 740 + case PIN_CONFIG_BIAS_PULL_DOWN: 741 + clear_bit(off, chip->push_pull); 742 + reg = CY8C95X0_DRV_PD; 743 + break; 744 + case PIN_CONFIG_BIAS_DISABLE: 745 + clear_bit(off, chip->push_pull); 746 + reg = CY8C95X0_DRV_HIZ; 747 + break; 748 + case PIN_CONFIG_DRIVE_OPEN_DRAIN: 749 + clear_bit(off, chip->push_pull); 750 + reg = CY8C95X0_DRV_ODL; 751 + break; 752 + case PIN_CONFIG_DRIVE_OPEN_SOURCE: 753 + clear_bit(off, chip->push_pull); 754 + reg = CY8C95X0_DRV_ODH; 755 + break; 756 + case PIN_CONFIG_DRIVE_PUSH_PULL: 757 + set_bit(off, chip->push_pull); 758 + reg = CY8C95X0_DRV_PP_FAST; 759 + break; 760 + case PIN_CONFIG_MODE_PWM: 761 + reg = CY8C95X0_PWMSEL; 762 + break; 763 + default: 764 + ret = -ENOTSUPP; 765 + goto out; 766 + } 767 + /* Writing 1 to one of the drive mode registers will automatically 768 + * clear conflicting set bits in the other drive mode registers. 769 + */ 770 + ret = regmap_write_bits(chip->regmap, reg, bit, bit); 771 + 772 + out: 773 + mutex_unlock(&chip->i2c_lock); 774 + return ret; 775 + } 776 + 777 + static int cy8c95x0_gpio_set_config(struct gpio_chip *gc, unsigned int offset, 778 + unsigned long config) 779 + { 780 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 781 + unsigned long arg = pinconf_to_config_argument(config); 782 + 783 + switch (pinconf_to_config_param(config)) { 784 + case PIN_CONFIG_INPUT_ENABLE: 785 + return cy8c95x0_gpio_direction_input(gc, offset); 786 + case PIN_CONFIG_OUTPUT: 787 + return cy8c95x0_gpio_direction_output(gc, offset, arg); 788 + case PIN_CONFIG_MODE_PWM: 789 + case PIN_CONFIG_BIAS_PULL_UP: 790 + case PIN_CONFIG_BIAS_PULL_DOWN: 791 + case PIN_CONFIG_BIAS_DISABLE: 792 + case PIN_CONFIG_DRIVE_OPEN_DRAIN: 793 + case PIN_CONFIG_DRIVE_OPEN_SOURCE: 794 + case PIN_CONFIG_DRIVE_PUSH_PULL: 795 + return cy8c95x0_gpio_set_pincfg(chip, offset, config); 796 + default: 797 + return -ENOTSUPP; 798 + } 799 + } 800 + 801 + static int cy8c95x0_gpio_get_multiple(struct gpio_chip *gc, 802 + unsigned long *mask, unsigned long *bits) 803 + { 804 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 805 + 806 + return cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, bits, mask); 807 + } 808 + 809 + static void cy8c95x0_gpio_set_multiple(struct gpio_chip *gc, 810 + unsigned long *mask, unsigned long *bits) 811 + { 812 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 813 + 814 + cy8c95x0_write_regs_mask(chip, CY8C95X0_OUTPUT, bits, mask); 815 + } 816 + 817 + static int cy8c95x0_setup_gpiochip(struct cy8c95x0_pinctrl *chip, int ngpio) 818 + { 819 + struct gpio_chip *gc = &chip->gpio_chip; 820 + 821 + gc->direction_input = cy8c95x0_gpio_direction_input; 822 + gc->direction_output = cy8c95x0_gpio_direction_output; 823 + gc->get = cy8c95x0_gpio_get_value; 824 + gc->set = cy8c95x0_gpio_set_value; 825 + gc->get_direction = cy8c95x0_gpio_get_direction; 826 + gc->get_multiple = cy8c95x0_gpio_get_multiple; 827 + gc->set_multiple = cy8c95x0_gpio_set_multiple; 828 + gc->set_config = cy8c95x0_gpio_set_config; 829 + gc->can_sleep = true; 830 + 831 + gc->base = -1; 832 + gc->ngpio = ngpio; 833 + 834 + gc->parent = chip->dev; 835 + gc->owner = THIS_MODULE; 836 + gc->names = NULL; 837 + 838 + gc->label = dev_name(chip->dev); 839 + 840 + return devm_gpiochip_add_data(chip->dev, gc, chip); 841 + } 842 + 843 + static void cy8c95x0_irq_mask(struct irq_data *d) 844 + { 845 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 846 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 847 + 848 + set_bit(irqd_to_hwirq(d), chip->irq_mask); 849 + } 850 + 851 + static void cy8c95x0_irq_unmask(struct irq_data *d) 852 + { 853 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 854 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 855 + 856 + clear_bit(irqd_to_hwirq(d), chip->irq_mask); 857 + } 858 + 859 + static void cy8c95x0_irq_bus_lock(struct irq_data *d) 860 + { 861 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 862 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 863 + 864 + mutex_lock(&chip->irq_lock); 865 + } 866 + 867 + static void cy8c95x0_irq_bus_sync_unlock(struct irq_data *d) 868 + { 869 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 870 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 871 + DECLARE_BITMAP(ones, MAX_LINE); 872 + DECLARE_BITMAP(irq_mask, MAX_LINE); 873 + DECLARE_BITMAP(reg_direction, MAX_LINE); 874 + 875 + bitmap_fill(ones, MAX_LINE); 876 + 877 + cy8c95x0_write_regs_mask(chip, CY8C95X0_INTMASK, chip->irq_mask, ones); 878 + 879 + /* Switch direction to input if needed */ 880 + cy8c95x0_read_regs_mask(chip, CY8C95X0_DIRECTION, reg_direction, chip->irq_mask); 881 + bitmap_or(irq_mask, chip->irq_mask, reg_direction, MAX_LINE); 882 + bitmap_complement(irq_mask, irq_mask, MAX_LINE); 883 + 884 + /* Look for any newly setup interrupt */ 885 + cy8c95x0_write_regs_mask(chip, CY8C95X0_DIRECTION, ones, irq_mask); 886 + 887 + mutex_unlock(&chip->irq_lock); 888 + } 889 + 890 + static int cy8c95x0_irq_set_type(struct irq_data *d, unsigned int type) 891 + { 892 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 893 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 894 + irq_hw_number_t hwirq = irqd_to_hwirq(d); 895 + unsigned int trig_type; 896 + 897 + switch (type) { 898 + case IRQ_TYPE_EDGE_RISING: 899 + case IRQ_TYPE_EDGE_FALLING: 900 + case IRQ_TYPE_EDGE_BOTH: 901 + trig_type = type; 902 + break; 903 + case IRQ_TYPE_LEVEL_HIGH: 904 + trig_type = IRQ_TYPE_EDGE_RISING; 905 + break; 906 + case IRQ_TYPE_LEVEL_LOW: 907 + trig_type = IRQ_TYPE_EDGE_FALLING; 908 + break; 909 + default: 910 + dev_err(chip->dev, "irq %d: unsupported type %d\n", d->irq, type); 911 + return -EINVAL; 912 + } 913 + 914 + assign_bit(hwirq, chip->irq_trig_fall, trig_type & IRQ_TYPE_EDGE_FALLING); 915 + assign_bit(hwirq, chip->irq_trig_raise, trig_type & IRQ_TYPE_EDGE_RISING); 916 + assign_bit(hwirq, chip->irq_trig_low, type == IRQ_TYPE_LEVEL_LOW); 917 + assign_bit(hwirq, chip->irq_trig_high, type == IRQ_TYPE_LEVEL_HIGH); 918 + 919 + return 0; 920 + } 921 + 922 + static void cy8c95x0_irq_shutdown(struct irq_data *d) 923 + { 924 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 925 + struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc); 926 + irq_hw_number_t hwirq = irqd_to_hwirq(d); 927 + 928 + clear_bit(hwirq, chip->irq_trig_raise); 929 + clear_bit(hwirq, chip->irq_trig_fall); 930 + clear_bit(hwirq, chip->irq_trig_low); 931 + clear_bit(hwirq, chip->irq_trig_high); 932 + } 933 + 934 + static bool cy8c95x0_irq_pending(struct cy8c95x0_pinctrl *chip, unsigned long *pending) 935 + { 936 + DECLARE_BITMAP(ones, MAX_LINE); 937 + DECLARE_BITMAP(cur_stat, MAX_LINE); 938 + DECLARE_BITMAP(new_stat, MAX_LINE); 939 + DECLARE_BITMAP(trigger, MAX_LINE); 940 + 941 + bitmap_fill(ones, MAX_LINE); 942 + 943 + /* Read the current interrupt status from the device */ 944 + if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INTSTATUS, trigger, ones)) 945 + return false; 946 + 947 + /* Check latched inputs */ 948 + if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, cur_stat, trigger)) 949 + return false; 950 + 951 + /* Apply filter for rising/falling edge selection */ 952 + bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise, 953 + cur_stat, MAX_LINE); 954 + 955 + bitmap_and(pending, new_stat, trigger, MAX_LINE); 956 + 957 + return !bitmap_empty(pending, MAX_LINE); 958 + } 959 + 960 + static irqreturn_t cy8c95x0_irq_handler(int irq, void *devid) 961 + { 962 + struct cy8c95x0_pinctrl *chip = devid; 963 + struct gpio_chip *gc = &chip->gpio_chip; 964 + DECLARE_BITMAP(pending, MAX_LINE); 965 + int nested_irq, level; 966 + bool ret; 967 + 968 + ret = cy8c95x0_irq_pending(chip, pending); 969 + if (!ret) 970 + return IRQ_RETVAL(0); 971 + 972 + ret = 0; 973 + for_each_set_bit(level, pending, MAX_LINE) { 974 + /* Already accounted for 4bit gap in GPort2 */ 975 + nested_irq = irq_find_mapping(gc->irq.domain, level); 976 + 977 + if (unlikely(nested_irq <= 0)) { 978 + dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level); 979 + continue; 980 + } 981 + 982 + if (test_bit(level, chip->irq_trig_low)) 983 + while (!cy8c95x0_gpio_get_value(gc, level)) 984 + handle_nested_irq(nested_irq); 985 + else if (test_bit(level, chip->irq_trig_high)) 986 + while (cy8c95x0_gpio_get_value(gc, level)) 987 + handle_nested_irq(nested_irq); 988 + else 989 + handle_nested_irq(nested_irq); 990 + 991 + ret = 1; 992 + } 993 + 994 + return IRQ_RETVAL(ret); 995 + } 996 + 997 + static int cy8c95x0_pinctrl_get_groups_count(struct pinctrl_dev *pctldev) 998 + { 999 + struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev); 1000 + 1001 + return chip->tpin; 1002 + } 1003 + 1004 + static const char *cy8c95x0_pinctrl_get_group_name(struct pinctrl_dev *pctldev, 1005 + unsigned int group) 1006 + { 1007 + return cy8c95x0_groups[group]; 1008 + } 1009 + 1010 + static int cy8c95x0_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, 1011 + unsigned int group, 1012 + const unsigned int **pins, 1013 + unsigned int *num_pins) 1014 + { 1015 + struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev); 1016 + 1017 + if (group >= chip->tpin) { 1018 + *pins = NULL; 1019 + *num_pins = 0; 1020 + return 0; 1021 + } 1022 + 1023 + *pins = &cy8c9560_pins[group].number; 1024 + *num_pins = 1; 1025 + return 0; 1026 + } 1027 + 1028 + static const struct pinctrl_ops cy8c95x0_pinctrl_ops = { 1029 + .get_groups_count = cy8c95x0_pinctrl_get_groups_count, 1030 + .get_group_name = cy8c95x0_pinctrl_get_group_name, 1031 + .get_group_pins = cy8c95x0_pinctrl_get_group_pins, 1032 + .dt_node_to_map = pinconf_generic_dt_node_to_map_pin, 1033 + .dt_free_map = pinconf_generic_dt_free_map, 1034 + }; 1035 + 1036 + static int cy8c95x0_get_functions_count(struct pinctrl_dev *pctldev) 1037 + { 1038 + return 2; 1039 + } 1040 + 1041 + static const char *cy8c95x0_get_fname(struct pinctrl_dev *pctldev, unsigned int selector) 1042 + { 1043 + if (selector == 0) 1044 + return "gpio"; 1045 + else 1046 + return "pwm"; 1047 + } 1048 + 1049 + static int cy8c95x0_get_groups(struct pinctrl_dev *pctldev, unsigned int selector, 1050 + const char * const **groups, 1051 + unsigned int * const num_groups) 1052 + { 1053 + struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev); 1054 + 1055 + *groups = cy8c95x0_groups; 1056 + *num_groups = chip->tpin; 1057 + return 0; 1058 + } 1059 + 1060 + static int cy8c95x0_pinmux_cfg(struct cy8c95x0_pinctrl *chip, 1061 + unsigned int val, 1062 + unsigned long off) 1063 + { 1064 + u8 port = cypress_get_port(chip, off); 1065 + u8 bit = cypress_get_pin_mask(chip, off); 1066 + int ret; 1067 + 1068 + /* select port */ 1069 + ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port); 1070 + if (ret < 0) 1071 + return ret; 1072 + 1073 + ret = regmap_write_bits(chip->regmap, CY8C95X0_PWMSEL, bit, val ? bit : 0); 1074 + if (ret < 0) 1075 + return ret; 1076 + 1077 + /* Set direction to output & set output to 1 so that PWM can work */ 1078 + ret = regmap_write_bits(chip->regmap, CY8C95X0_DIRECTION, bit, bit); 1079 + if (ret < 0) 1080 + return ret; 1081 + 1082 + return regmap_write_bits(chip->regmap, CY8C95X0_OUTPUT_(port), bit, bit); 1083 + } 1084 + 1085 + static int cy8c95x0_set_mux(struct pinctrl_dev *pctldev, unsigned int selector, 1086 + unsigned int group) 1087 + { 1088 + struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev); 1089 + 1090 + if (group >= chip->tpin) 1091 + return -EINVAL; 1092 + 1093 + return cy8c95x0_pinmux_cfg(chip, selector, group); 1094 + } 1095 + 1096 + static const struct pinmux_ops cy8c95x0_pmxops = { 1097 + .get_functions_count = cy8c95x0_get_functions_count, 1098 + .get_function_name = cy8c95x0_get_fname, 1099 + .get_function_groups = cy8c95x0_get_groups, 1100 + .set_mux = cy8c95x0_set_mux, 1101 + .strict = true, 1102 + }; 1103 + 1104 + static int cy8c95x0_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, 1105 + unsigned long *config) 1106 + { 1107 + struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev); 1108 + 1109 + return cy8c95x0_gpio_get_pincfg(chip, pin, config); 1110 + } 1111 + 1112 + static int cy8c95x0_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, 1113 + unsigned long *configs, unsigned int num_configs) 1114 + { 1115 + struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev); 1116 + int ret = 0; 1117 + int i; 1118 + 1119 + if (WARN_ON(pin >= chip->tpin)) 1120 + return -EINVAL; 1121 + 1122 + for (i = 0; i < num_configs; i++) { 1123 + ret = cy8c95x0_gpio_set_pincfg(chip, pin, configs[i]); 1124 + if (ret) 1125 + return ret; 1126 + } 1127 + 1128 + return ret; 1129 + } 1130 + 1131 + static const struct pinconf_ops cy8c95x0_pinconf_ops = { 1132 + .pin_config_get = cy8c95x0_pinconf_get, 1133 + .pin_config_set = cy8c95x0_pinconf_set, 1134 + .is_generic = true, 1135 + }; 1136 + 1137 + static int cy8c95x0_irq_setup(struct cy8c95x0_pinctrl *chip, int irq) 1138 + { 1139 + struct irq_chip *irq_chip = &chip->irq_chip; 1140 + struct gpio_irq_chip *girq = &chip->gpio_chip.irq; 1141 + DECLARE_BITMAP(pending_irqs, MAX_LINE); 1142 + int ret; 1143 + 1144 + mutex_init(&chip->irq_lock); 1145 + 1146 + bitmap_zero(pending_irqs, MAX_LINE); 1147 + 1148 + /* Read IRQ status register to clear all pending interrupts */ 1149 + ret = cy8c95x0_irq_pending(chip, pending_irqs); 1150 + if (ret) { 1151 + dev_err(chip->dev, "failed to clear irq status register\n"); 1152 + return ret; 1153 + } 1154 + 1155 + /* Mask all interrupts */ 1156 + bitmap_fill(chip->irq_mask, MAX_LINE); 1157 + 1158 + irq_chip->name = devm_kasprintf(chip->dev, GFP_KERNEL, "%s-irq", chip->name); 1159 + irq_chip->irq_mask = cy8c95x0_irq_mask; 1160 + irq_chip->irq_unmask = cy8c95x0_irq_unmask; 1161 + irq_chip->irq_bus_lock = cy8c95x0_irq_bus_lock; 1162 + irq_chip->irq_bus_sync_unlock = cy8c95x0_irq_bus_sync_unlock; 1163 + irq_chip->irq_set_type = cy8c95x0_irq_set_type; 1164 + irq_chip->irq_shutdown = cy8c95x0_irq_shutdown; 1165 + 1166 + girq->chip = irq_chip; 1167 + /* This will let us handle the parent IRQ in the driver */ 1168 + girq->parent_handler = NULL; 1169 + girq->num_parents = 0; 1170 + girq->parents = NULL; 1171 + girq->default_type = IRQ_TYPE_NONE; 1172 + girq->handler = handle_simple_irq; 1173 + girq->threaded = true; 1174 + girq->first = 0; 1175 + 1176 + ret = devm_request_threaded_irq(chip->dev, irq, 1177 + NULL, cy8c95x0_irq_handler, 1178 + IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_HIGH, 1179 + dev_name(chip->dev), chip); 1180 + if (ret) { 1181 + dev_err(chip->dev, "failed to request irq %d\n", irq); 1182 + return ret; 1183 + } 1184 + dev_info(chip->dev, "Registered threaded IRQ\n"); 1185 + 1186 + return 0; 1187 + } 1188 + 1189 + static int cy8c95x0_setup_pinctrl(struct cy8c95x0_pinctrl *chip) 1190 + { 1191 + struct pinctrl_desc *pd = &chip->pinctrl_desc; 1192 + 1193 + pd->pctlops = &cy8c95x0_pinctrl_ops; 1194 + pd->confops = &cy8c95x0_pinconf_ops; 1195 + pd->pmxops = &cy8c95x0_pmxops; 1196 + pd->npins = chip->gpio_chip.ngpio; 1197 + pd->name = devm_kasprintf(chip->dev, GFP_KERNEL, "pinctrl-%s", 1198 + chip->name); 1199 + pd->pins = cy8c9560_pins; 1200 + pd->npins = chip->tpin; 1201 + pd->owner = THIS_MODULE; 1202 + chip->pctldev = devm_pinctrl_register(chip->dev, pd, chip); 1203 + 1204 + if (IS_ERR(chip->pctldev)) 1205 + return dev_err_probe(chip->dev, PTR_ERR(chip->pctldev), 1206 + "can't register controller\n"); 1207 + return 0; 1208 + } 1209 + 1210 + static int device_cy8c95x0_init(struct cy8c95x0_pinctrl *chip) 1211 + { 1212 + DECLARE_BITMAP(ones, MAX_LINE); 1213 + DECLARE_BITMAP(zeros, MAX_LINE); 1214 + int ret; 1215 + 1216 + /* Set all pins to input. This is the POR default. */ 1217 + bitmap_fill(ones, MAX_LINE); 1218 + ret = cy8c95x0_write_regs_mask(chip, CY8C95X0_DIRECTION, ones, ones); 1219 + if (ret) { 1220 + dev_err(chip->dev, "Failed to set pins to input\n"); 1221 + return ret; 1222 + } 1223 + 1224 + bitmap_zero(zeros, MAX_LINE); 1225 + ret = cy8c95x0_write_regs_mask(chip, CY8C95X0_INVERT, zeros, ones); 1226 + if (ret) { 1227 + dev_err(chip->dev, "Failed to set polarity inversion\n"); 1228 + return ret; 1229 + } 1230 + 1231 + return 0; 1232 + } 1233 + 1234 + static int cy8c95x0_detect(struct i2c_client *client, 1235 + struct i2c_board_info *info) 1236 + { 1237 + struct i2c_adapter *adapter = client->adapter; 1238 + int ret; 1239 + const char *name; 1240 + 1241 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 1242 + return -ENODEV; 1243 + 1244 + ret = i2c_smbus_read_byte_data(client, CY8C95X0_DEVID); 1245 + if (ret < 0) 1246 + return ret; 1247 + switch (ret & 0xf0) { 1248 + case 0x20: 1249 + name = cy8c95x0_id[0].name; 1250 + break; 1251 + case 0x40: 1252 + name = cy8c95x0_id[1].name; 1253 + break; 1254 + case 0x60: 1255 + name = cy8c95x0_id[2].name; 1256 + break; 1257 + default: 1258 + return -ENODEV; 1259 + } 1260 + 1261 + dev_info(&client->dev, "Found a %s chip at 0x%02x.\n", name, client->addr); 1262 + strscpy(info->type, name, I2C_NAME_SIZE); 1263 + 1264 + return -ENODEV; 1265 + } 1266 + 1267 + static int cy8c95x0_probe(struct i2c_client *client) 1268 + { 1269 + struct cy8c95x0_pinctrl *chip; 1270 + struct regulator *reg; 1271 + int ret; 1272 + 1273 + chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); 1274 + if (!chip) 1275 + return -ENOMEM; 1276 + 1277 + chip->dev = &client->dev; 1278 + 1279 + /* Set the device type */ 1280 + if (client->dev.of_node) 1281 + chip->driver_data = (unsigned long)of_device_get_match_data(&client->dev); 1282 + else 1283 + chip->driver_data = i2c_match_id(cy8c95x0_id, client)->driver_data; 1284 + 1285 + if (!chip->driver_data) 1286 + return -ENODEV; 1287 + 1288 + i2c_set_clientdata(client, chip); 1289 + 1290 + chip->tpin = chip->driver_data & CY8C95X0_GPIO_MASK; 1291 + chip->nport = DIV_ROUND_UP(CY8C95X0_PIN_TO_OFFSET(chip->tpin), BANK_SZ); 1292 + 1293 + switch (chip->tpin) { 1294 + case 20: 1295 + strscpy(chip->name, cy8c95x0_id[0].name, I2C_NAME_SIZE); 1296 + break; 1297 + case 40: 1298 + strscpy(chip->name, cy8c95x0_id[1].name, I2C_NAME_SIZE); 1299 + break; 1300 + case 60: 1301 + strscpy(chip->name, cy8c95x0_id[2].name, I2C_NAME_SIZE); 1302 + break; 1303 + } 1304 + 1305 + reg = devm_regulator_get(&client->dev, "vdd"); 1306 + if (IS_ERR(reg)) { 1307 + if (PTR_ERR(reg) == -EPROBE_DEFER) 1308 + return -EPROBE_DEFER; 1309 + } else { 1310 + ret = regulator_enable(reg); 1311 + if (ret) { 1312 + dev_err(&client->dev, "failed to enable regulator vdd: %d\n", ret); 1313 + return ret; 1314 + } 1315 + chip->regulator = reg; 1316 + } 1317 + 1318 + chip->regmap = devm_regmap_init_i2c(client, &cy8c95x0_i2c_regmap); 1319 + if (IS_ERR(chip->regmap)) { 1320 + ret = PTR_ERR(chip->regmap); 1321 + goto err_exit; 1322 + } 1323 + 1324 + bitmap_zero(chip->push_pull, MAX_LINE); 1325 + bitmap_zero(chip->shiftmask, MAX_LINE); 1326 + bitmap_set(chip->shiftmask, 0, 20); 1327 + mutex_init(&chip->i2c_lock); 1328 + 1329 + ret = device_cy8c95x0_init(chip); 1330 + if (ret) 1331 + goto err_exit; 1332 + 1333 + if (client->irq) { 1334 + ret = cy8c95x0_irq_setup(chip, client->irq); 1335 + if (ret) 1336 + goto err_exit; 1337 + } 1338 + 1339 + ret = cy8c95x0_setup_gpiochip(chip, chip->tpin); 1340 + if (ret) 1341 + goto err_exit; 1342 + 1343 + ret = cy8c95x0_setup_pinctrl(chip); 1344 + if (ret) 1345 + goto err_exit; 1346 + 1347 + return 0; 1348 + 1349 + err_exit: 1350 + if (!IS_ERR_OR_NULL(chip->regulator)) 1351 + regulator_disable(chip->regulator); 1352 + return ret; 1353 + } 1354 + 1355 + static int cy8c95x0_remove(struct i2c_client *client) 1356 + { 1357 + struct cy8c95x0_pinctrl *chip = i2c_get_clientdata(client); 1358 + 1359 + if (!IS_ERR_OR_NULL(chip->regulator)) 1360 + regulator_disable(chip->regulator); 1361 + 1362 + return 0; 1363 + } 1364 + 1365 + static struct i2c_driver cy8c95x0_driver = { 1366 + .driver = { 1367 + .name = "cy8c95x0-pinctrl", 1368 + .of_match_table = cy8c95x0_dt_ids, 1369 + }, 1370 + .probe_new = cy8c95x0_probe, 1371 + .remove = cy8c95x0_remove, 1372 + .id_table = cy8c95x0_id, 1373 + .detect = cy8c95x0_detect, 1374 + }; 1375 + 1376 + module_i2c_driver(cy8c95x0_driver); 1377 + 1378 + MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>"); 1379 + MODULE_AUTHOR("Naresh Solanki <naresh.solanki@9elements.com>"); 1380 + MODULE_DESCRIPTION("Pinctrl driver for CY8C95X0"); 1381 + MODULE_LICENSE("GPL");