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 * Authors: Patrick Rudolph <patrick.rudolph@9elements.com>
7 * Naresh Solanki <Naresh.Solanki@9elements.com>
8 */
9
10 #include <linux/acpi.h>
11 #include <linux/bitmap.h>
12 #include <linux/cleanup.h>
13 #include <linux/dmi.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/property.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/seq_file.h>
25
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pinctrl/pinconf.h>
28 #include <linux/pinctrl/pinconf-generic.h>
29 #include <linux/pinctrl/pinctrl.h>
30 #include <linux/pinctrl/pinmux.h>
31
32 /* Fast access registers */
33 #define CY8C95X0_INPUT 0x00
34 #define CY8C95X0_OUTPUT 0x08
35 #define CY8C95X0_INTSTATUS 0x10
36
37 #define CY8C95X0_INPUT_(x) (CY8C95X0_INPUT + (x))
38 #define CY8C95X0_OUTPUT_(x) (CY8C95X0_OUTPUT + (x))
39 #define CY8C95X0_INTSTATUS_(x) (CY8C95X0_INTSTATUS + (x))
40
41 /* Port Select configures the port */
42 #define CY8C95X0_PORTSEL 0x18
43
44 /* Port settings, write PORTSEL first */
45 #define CY8C95X0_INTMASK 0x19
46 #define CY8C95X0_SELPWM 0x1A
47 #define CY8C95X0_INVERT 0x1B
48 #define CY8C95X0_DIRECTION 0x1C
49 /* Drive mode register change state on writing '1' */
50 #define CY8C95X0_DRV_PU 0x1D
51 #define CY8C95X0_DRV_PD 0x1E
52 #define CY8C95X0_DRV_ODH 0x1F
53 #define CY8C95X0_DRV_ODL 0x20
54 #define CY8C95X0_DRV_PP_FAST 0x21
55 #define CY8C95X0_DRV_PP_SLOW 0x22
56 #define CY8C95X0_DRV_HIZ 0x23
57
58 /* Internal device configuration */
59 #define CY8C95X0_ENABLE_WDE 0x2D
60 #define CY8C95X0_DEVID 0x2E
61 #define CY8C95X0_WATCHDOG 0x2F
62 #define CY8C95X0_COMMAND 0x30
63
64 #define CY8C95X0_PIN_TO_OFFSET(x) (((x) >= 20) ? ((x) + 4) : (x))
65
66 #define MAX_BANK 8
67 #define BANK_SZ 8
68 #define MAX_LINE (MAX_BANK * BANK_SZ)
69 #define MUXED_STRIDE 16
70 #define CY8C95X0_GPIO_MASK GENMASK(7, 0)
71 #define CY8C95X0_VIRTUAL 0x40
72 #define CY8C95X0_MUX_REGMAP_TO_OFFSET(x, p) \
73 (CY8C95X0_VIRTUAL + (x) - CY8C95X0_PORTSEL + (p) * MUXED_STRIDE)
74
75 static const struct acpi_gpio_params cy8c95x0_irq_gpios = { 0, 0, true };
76
77 static const struct acpi_gpio_mapping cy8c95x0_acpi_irq_gpios[] = {
78 { "irq-gpios", &cy8c95x0_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER },
79 { }
80 };
81
cy8c95x0_acpi_get_irq(struct device * dev)82 static int cy8c95x0_acpi_get_irq(struct device *dev)
83 {
84 int ret;
85
86 ret = devm_acpi_dev_add_driver_gpios(dev, cy8c95x0_acpi_irq_gpios);
87 if (ret)
88 dev_warn(dev, "can't add GPIO ACPI mapping\n");
89
90 ret = acpi_dev_gpio_irq_get_by(ACPI_COMPANION(dev), "irq", 0);
91 if (ret < 0)
92 return ret;
93
94 dev_info(dev, "ACPI interrupt quirk (IRQ %d)\n", ret);
95 return ret;
96 }
97
98 static const struct dmi_system_id cy8c95x0_dmi_acpi_irq_info[] = {
99 {
100 /*
101 * On Intel Galileo Gen 1 board the IRQ pin is provided
102 * as an absolute number instead of being relative.
103 * Since first controller (gpio-sch.c) and second
104 * (gpio-dwapb.c) are at the fixed bases, we may safely
105 * refer to the number in the global space to get an IRQ
106 * out of it.
107 */
108 .matches = {
109 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Galileo"),
110 },
111 },
112 {}
113 };
114
115 /**
116 * struct cy8c95x0_pinctrl - driver data
117 * @regmap: Device's regmap. Only direct access registers.
118 * @irq_lock: IRQ bus lock
119 * @i2c_lock: Mutex to hold while using the regmap
120 * @irq_mask: I/O bits affected by interrupts
121 * @irq_trig_raise: I/O bits affected by raising voltage level
122 * @irq_trig_fall: I/O bits affected by falling voltage level
123 * @irq_trig_low: I/O bits affected by a low voltage level
124 * @irq_trig_high: I/O bits affected by a high voltage level
125 * @push_pull: I/O bits configured as push pull driver
126 * @map: Mask used to compensate for Gport2 width
127 * @nport: Number of Gports in this chip
128 * @gpio_chip: gpiolib chip
129 * @dev: struct device
130 * @pctldev: pin controller device
131 * @pinctrl_desc: pin controller description
132 * @tpin: Total number of pins
133 * @gpio_reset: GPIO line handler that can reset the IC
134 */
135 struct cy8c95x0_pinctrl {
136 struct regmap *regmap;
137 struct mutex irq_lock;
138 struct mutex i2c_lock;
139 DECLARE_BITMAP(irq_mask, MAX_LINE);
140 DECLARE_BITMAP(irq_trig_raise, MAX_LINE);
141 DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
142 DECLARE_BITMAP(irq_trig_low, MAX_LINE);
143 DECLARE_BITMAP(irq_trig_high, MAX_LINE);
144 DECLARE_BITMAP(push_pull, MAX_LINE);
145 DECLARE_BITMAP(map, MAX_LINE);
146 unsigned int nport;
147 struct gpio_chip gpio_chip;
148 struct device *dev;
149 struct pinctrl_dev *pctldev;
150 struct pinctrl_desc pinctrl_desc;
151 unsigned int tpin;
152 struct gpio_desc *gpio_reset;
153 };
154
155 static const struct pinctrl_pin_desc cy8c9560_pins[] = {
156 PINCTRL_PIN(0, "gp00"),
157 PINCTRL_PIN(1, "gp01"),
158 PINCTRL_PIN(2, "gp02"),
159 PINCTRL_PIN(3, "gp03"),
160 PINCTRL_PIN(4, "gp04"),
161 PINCTRL_PIN(5, "gp05"),
162 PINCTRL_PIN(6, "gp06"),
163 PINCTRL_PIN(7, "gp07"),
164
165 PINCTRL_PIN(8, "gp10"),
166 PINCTRL_PIN(9, "gp11"),
167 PINCTRL_PIN(10, "gp12"),
168 PINCTRL_PIN(11, "gp13"),
169 PINCTRL_PIN(12, "gp14"),
170 PINCTRL_PIN(13, "gp15"),
171 PINCTRL_PIN(14, "gp16"),
172 PINCTRL_PIN(15, "gp17"),
173
174 PINCTRL_PIN(16, "gp20"),
175 PINCTRL_PIN(17, "gp21"),
176 PINCTRL_PIN(18, "gp22"),
177 PINCTRL_PIN(19, "gp23"),
178
179 PINCTRL_PIN(20, "gp30"),
180 PINCTRL_PIN(21, "gp31"),
181 PINCTRL_PIN(22, "gp32"),
182 PINCTRL_PIN(23, "gp33"),
183 PINCTRL_PIN(24, "gp34"),
184 PINCTRL_PIN(25, "gp35"),
185 PINCTRL_PIN(26, "gp36"),
186 PINCTRL_PIN(27, "gp37"),
187
188 PINCTRL_PIN(28, "gp40"),
189 PINCTRL_PIN(29, "gp41"),
190 PINCTRL_PIN(30, "gp42"),
191 PINCTRL_PIN(31, "gp43"),
192 PINCTRL_PIN(32, "gp44"),
193 PINCTRL_PIN(33, "gp45"),
194 PINCTRL_PIN(34, "gp46"),
195 PINCTRL_PIN(35, "gp47"),
196
197 PINCTRL_PIN(36, "gp50"),
198 PINCTRL_PIN(37, "gp51"),
199 PINCTRL_PIN(38, "gp52"),
200 PINCTRL_PIN(39, "gp53"),
201 PINCTRL_PIN(40, "gp54"),
202 PINCTRL_PIN(41, "gp55"),
203 PINCTRL_PIN(42, "gp56"),
204 PINCTRL_PIN(43, "gp57"),
205
206 PINCTRL_PIN(44, "gp60"),
207 PINCTRL_PIN(45, "gp61"),
208 PINCTRL_PIN(46, "gp62"),
209 PINCTRL_PIN(47, "gp63"),
210 PINCTRL_PIN(48, "gp64"),
211 PINCTRL_PIN(49, "gp65"),
212 PINCTRL_PIN(50, "gp66"),
213 PINCTRL_PIN(51, "gp67"),
214
215 PINCTRL_PIN(52, "gp70"),
216 PINCTRL_PIN(53, "gp71"),
217 PINCTRL_PIN(54, "gp72"),
218 PINCTRL_PIN(55, "gp73"),
219 PINCTRL_PIN(56, "gp74"),
220 PINCTRL_PIN(57, "gp75"),
221 PINCTRL_PIN(58, "gp76"),
222 PINCTRL_PIN(59, "gp77"),
223 };
224
225 static const char * const cy8c95x0_groups[] = {
226 "gp00",
227 "gp01",
228 "gp02",
229 "gp03",
230 "gp04",
231 "gp05",
232 "gp06",
233 "gp07",
234
235 "gp10",
236 "gp11",
237 "gp12",
238 "gp13",
239 "gp14",
240 "gp15",
241 "gp16",
242 "gp17",
243
244 "gp20",
245 "gp21",
246 "gp22",
247 "gp23",
248
249 "gp30",
250 "gp31",
251 "gp32",
252 "gp33",
253 "gp34",
254 "gp35",
255 "gp36",
256 "gp37",
257
258 "gp40",
259 "gp41",
260 "gp42",
261 "gp43",
262 "gp44",
263 "gp45",
264 "gp46",
265 "gp47",
266
267 "gp50",
268 "gp51",
269 "gp52",
270 "gp53",
271 "gp54",
272 "gp55",
273 "gp56",
274 "gp57",
275
276 "gp60",
277 "gp61",
278 "gp62",
279 "gp63",
280 "gp64",
281 "gp65",
282 "gp66",
283 "gp67",
284
285 "gp70",
286 "gp71",
287 "gp72",
288 "gp73",
289 "gp74",
290 "gp75",
291 "gp76",
292 "gp77",
293 };
294
cypress_get_port(struct cy8c95x0_pinctrl * chip,unsigned int pin)295 static inline u8 cypress_get_port(struct cy8c95x0_pinctrl *chip, unsigned int pin)
296 {
297 /* Account for GPORT2 which only has 4 bits */
298 return CY8C95X0_PIN_TO_OFFSET(pin) / BANK_SZ;
299 }
300
cypress_get_pin_mask(struct cy8c95x0_pinctrl * chip,unsigned int pin)301 static int cypress_get_pin_mask(struct cy8c95x0_pinctrl *chip, unsigned int pin)
302 {
303 /* Account for GPORT2 which only has 4 bits */
304 return BIT(CY8C95X0_PIN_TO_OFFSET(pin) % BANK_SZ);
305 }
306
cy8c95x0_readable_register(struct device * dev,unsigned int reg)307 static bool cy8c95x0_readable_register(struct device *dev, unsigned int reg)
308 {
309 /*
310 * Only 12 registers are present per port (see Table 6 in the datasheet).
311 */
312 if (reg >= CY8C95X0_VIRTUAL && (reg % MUXED_STRIDE) >= 12)
313 return false;
314
315 switch (reg) {
316 case 0x24 ... 0x27:
317 case 0x31 ... 0x3f:
318 return false;
319 default:
320 return true;
321 }
322 }
323
cy8c95x0_writeable_register(struct device * dev,unsigned int reg)324 static bool cy8c95x0_writeable_register(struct device *dev, unsigned int reg)
325 {
326 /*
327 * Only 12 registers are present per port (see Table 6 in the datasheet).
328 */
329 if (reg >= CY8C95X0_VIRTUAL && (reg % MUXED_STRIDE) >= 12)
330 return false;
331
332 switch (reg) {
333 case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
334 return false;
335 case CY8C95X0_DEVID:
336 return false;
337 case 0x24 ... 0x27:
338 case 0x31 ... 0x3f:
339 return false;
340 default:
341 return true;
342 }
343 }
344
cy8c95x0_volatile_register(struct device * dev,unsigned int reg)345 static bool cy8c95x0_volatile_register(struct device *dev, unsigned int reg)
346 {
347 switch (reg) {
348 case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
349 case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
350 case CY8C95X0_INTMASK:
351 case CY8C95X0_SELPWM:
352 case CY8C95X0_INVERT:
353 case CY8C95X0_DIRECTION:
354 case CY8C95X0_DRV_PU:
355 case CY8C95X0_DRV_PD:
356 case CY8C95X0_DRV_ODH:
357 case CY8C95X0_DRV_ODL:
358 case CY8C95X0_DRV_PP_FAST:
359 case CY8C95X0_DRV_PP_SLOW:
360 case CY8C95X0_DRV_HIZ:
361 return true;
362 default:
363 return false;
364 }
365 }
366
cy8c95x0_precious_register(struct device * dev,unsigned int reg)367 static bool cy8c95x0_precious_register(struct device *dev, unsigned int reg)
368 {
369 switch (reg) {
370 case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
371 return true;
372 default:
373 return false;
374 }
375 }
376
cy8c95x0_muxed_register(unsigned int reg)377 static bool cy8c95x0_muxed_register(unsigned int reg)
378 {
379 switch (reg) {
380 case CY8C95X0_INTMASK:
381 case CY8C95X0_SELPWM:
382 case CY8C95X0_INVERT:
383 case CY8C95X0_DIRECTION:
384 case CY8C95X0_DRV_PU:
385 case CY8C95X0_DRV_PD:
386 case CY8C95X0_DRV_ODH:
387 case CY8C95X0_DRV_ODL:
388 case CY8C95X0_DRV_PP_FAST:
389 case CY8C95X0_DRV_PP_SLOW:
390 case CY8C95X0_DRV_HIZ:
391 return true;
392 default:
393 return false;
394 }
395 }
396
cy8c95x0_wc_register(unsigned int reg)397 static bool cy8c95x0_wc_register(unsigned int reg)
398 {
399 switch (reg) {
400 case CY8C95X0_DRV_PU:
401 case CY8C95X0_DRV_PD:
402 case CY8C95X0_DRV_ODH:
403 case CY8C95X0_DRV_ODL:
404 case CY8C95X0_DRV_PP_FAST:
405 case CY8C95X0_DRV_PP_SLOW:
406 case CY8C95X0_DRV_HIZ:
407 return true;
408 default:
409 return false;
410 }
411 }
412
cy8c95x0_quick_path_register(unsigned int reg)413 static bool cy8c95x0_quick_path_register(unsigned int reg)
414 {
415 switch (reg) {
416 case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
417 case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
418 case CY8C95X0_OUTPUT_(0) ... CY8C95X0_OUTPUT_(7):
419 return true;
420 default:
421 return false;
422 }
423 }
424
425 static const struct regmap_range_cfg cy8c95x0_ranges[] = {
426 {
427 .range_min = CY8C95X0_VIRTUAL,
428 .range_max = 0, /* Updated at runtime */
429 .selector_reg = CY8C95X0_PORTSEL,
430 .selector_mask = 0x07,
431 .selector_shift = 0x0,
432 .window_start = CY8C95X0_PORTSEL,
433 .window_len = MUXED_STRIDE,
434 }
435 };
436
437 static const struct regmap_config cy8c9520_i2c_regmap = {
438 .reg_bits = 8,
439 .val_bits = 8,
440
441 .readable_reg = cy8c95x0_readable_register,
442 .writeable_reg = cy8c95x0_writeable_register,
443 .volatile_reg = cy8c95x0_volatile_register,
444 .precious_reg = cy8c95x0_precious_register,
445
446 .cache_type = REGCACHE_MAPLE,
447 .ranges = NULL, /* Updated at runtime */
448 .num_ranges = 1,
449 .max_register = 0, /* Updated at runtime */
450 .num_reg_defaults_raw = 0, /* Updated at runtime */
451 .use_single_read = true, /* Workaround for regcache bug */
452 #if IS_ENABLED(CONFIG_DEBUG_PINCTRL)
453 .disable_locking = false,
454 #else
455 .disable_locking = true,
456 #endif
457 };
458
459 /* Caller should never modify PORTSEL directly */
cy8c95x0_regmap_update_bits_base(struct cy8c95x0_pinctrl * chip,unsigned int reg,unsigned int port,unsigned int mask,unsigned int val,bool * change,bool async,bool force)460 static int cy8c95x0_regmap_update_bits_base(struct cy8c95x0_pinctrl *chip,
461 unsigned int reg, unsigned int port,
462 unsigned int mask, unsigned int val,
463 bool *change, bool async, bool force)
464 {
465 int ret, off, i;
466
467 /* Registers behind the PORTSEL mux have their own range in regmap */
468 if (cy8c95x0_muxed_register(reg)) {
469 off = CY8C95X0_MUX_REGMAP_TO_OFFSET(reg, port);
470 } else {
471 /* Quick path direct access registers honor the port argument */
472 if (cy8c95x0_quick_path_register(reg))
473 off = reg + port;
474 else
475 off = reg;
476 }
477 guard(mutex)(&chip->i2c_lock);
478
479 ret = regmap_update_bits_base(chip->regmap, off, mask, val, change, async, force);
480 if (ret < 0)
481 return ret;
482
483 /*
484 * Mimic what hardware does and update the cache when a WC bit is written.
485 * Allows to mark the registers as non-volatile and reduces I/O cycles.
486 */
487 if (cy8c95x0_wc_register(reg) && (mask & val)) {
488 /* Writing a 1 clears set bits in the other drive mode registers */
489 regcache_cache_only(chip->regmap, true);
490 for (i = CY8C95X0_DRV_PU; i <= CY8C95X0_DRV_HIZ; i++) {
491 if (i == reg)
492 continue;
493
494 off = CY8C95X0_MUX_REGMAP_TO_OFFSET(i, port);
495 regmap_clear_bits(chip->regmap, off, mask & val);
496 }
497 regcache_cache_only(chip->regmap, false);
498 }
499
500 return 0;
501 }
502
503 /**
504 * cy8c95x0_regmap_write_bits() - writes a register using the regmap cache
505 * @chip: The pinctrl to work on
506 * @reg: The register to write to. Can be direct access or muxed register.
507 * MUST NOT be the PORTSEL register.
508 * @port: The port to be used for muxed registers or quick path direct access
509 * registers. Otherwise unused.
510 * @mask: Bitmask to change
511 * @val: New value for bitmask
512 *
513 * This function handles the register writes to the direct access registers and
514 * the muxed registers while caching all register accesses, internally handling
515 * the correct state of the PORTSEL register and protecting the access to muxed
516 * registers.
517 * The caller must only use this function to change registers behind the PORTSEL mux.
518 *
519 * Return: 0 for successful request, else a corresponding error value
520 */
cy8c95x0_regmap_write_bits(struct cy8c95x0_pinctrl * chip,unsigned int reg,unsigned int port,unsigned int mask,unsigned int val)521 static int cy8c95x0_regmap_write_bits(struct cy8c95x0_pinctrl *chip, unsigned int reg,
522 unsigned int port, unsigned int mask, unsigned int val)
523 {
524 return cy8c95x0_regmap_update_bits_base(chip, reg, port, mask, val, NULL, false, true);
525 }
526
527 /**
528 * cy8c95x0_regmap_update_bits() - updates a register using the regmap cache
529 * @chip: The pinctrl to work on
530 * @reg: The register to write to. Can be direct access or muxed register.
531 * MUST NOT be the PORTSEL register.
532 * @port: The port to be used for muxed registers or quick path direct access
533 * registers. Otherwise unused.
534 * @mask: Bitmask to change
535 * @val: New value for bitmask
536 *
537 * This function handles the register updates to the direct access registers and
538 * the muxed registers while caching all register accesses, internally handling
539 * the correct state of the PORTSEL register and protecting the access to muxed
540 * registers.
541 * The caller must only use this function to change registers behind the PORTSEL mux.
542 *
543 * Return: 0 for successful request, else a corresponding error value
544 */
cy8c95x0_regmap_update_bits(struct cy8c95x0_pinctrl * chip,unsigned int reg,unsigned int port,unsigned int mask,unsigned int val)545 static int cy8c95x0_regmap_update_bits(struct cy8c95x0_pinctrl *chip, unsigned int reg,
546 unsigned int port, unsigned int mask, unsigned int val)
547 {
548 return cy8c95x0_regmap_update_bits_base(chip, reg, port, mask, val, NULL, false, false);
549 }
550
551 /**
552 * cy8c95x0_regmap_read_bits() - reads a register using the regmap cache
553 * @chip: The pinctrl to work on
554 * @reg: The register to read from. Can be direct access or muxed register.
555 * @port: The port to be used for muxed registers or quick path direct access
556 * registers. Otherwise unused.
557 * @mask: Bitmask to apply
558 * @val: Value read from hardware or cache
559 *
560 * This function handles the register reads from the direct access registers and
561 * the muxed registers while caching all register accesses, internally handling
562 * the correct state of the PORTSEL register and protecting the access to muxed
563 * registers.
564 * The caller must only use this function to read registers behind the PORTSEL mux.
565 *
566 * Return: 0 for successful request, else a corresponding error value
567 */
cy8c95x0_regmap_read_bits(struct cy8c95x0_pinctrl * chip,unsigned int reg,unsigned int port,unsigned int mask,unsigned int * val)568 static int cy8c95x0_regmap_read_bits(struct cy8c95x0_pinctrl *chip, unsigned int reg,
569 unsigned int port, unsigned int mask, unsigned int *val)
570 {
571 unsigned int off;
572 unsigned int tmp;
573 int ret;
574
575 /* Registers behind the PORTSEL mux have their own range in regmap */
576 if (cy8c95x0_muxed_register(reg)) {
577 off = CY8C95X0_MUX_REGMAP_TO_OFFSET(reg, port);
578 } else {
579 /* Quick path direct access registers honor the port argument */
580 if (cy8c95x0_quick_path_register(reg))
581 off = reg + port;
582 else
583 off = reg;
584 }
585
586 scoped_guard(mutex, &chip->i2c_lock)
587 ret = regmap_read(chip->regmap, off, &tmp);
588 if (ret)
589 return ret;
590
591 *val = tmp & mask;
592 return 0;
593 }
594
cy8c95x0_write_regs_mask(struct cy8c95x0_pinctrl * chip,int reg,unsigned long * val,unsigned long * mask)595 static int cy8c95x0_write_regs_mask(struct cy8c95x0_pinctrl *chip, int reg,
596 unsigned long *val, unsigned long *mask)
597 {
598 DECLARE_BITMAP(tmask, MAX_LINE);
599 DECLARE_BITMAP(tval, MAX_LINE);
600 unsigned long bits, offset;
601 int write_val;
602 int ret;
603
604 /* Add the 4 bit gap of Gport2 */
605 bitmap_scatter(tmask, mask, chip->map, MAX_LINE);
606 bitmap_scatter(tval, val, chip->map, MAX_LINE);
607
608 for_each_set_clump8(offset, bits, tmask, chip->nport * BANK_SZ) {
609 unsigned int i = offset / 8;
610
611 write_val = bitmap_get_value8(tval, offset);
612
613 ret = cy8c95x0_regmap_update_bits(chip, reg, i, bits, write_val);
614 if (ret < 0) {
615 dev_err(chip->dev, "failed writing register %d, port %u: err %d\n", reg, i, ret);
616 return ret;
617 }
618 }
619
620 return 0;
621 }
622
cy8c95x0_read_regs_mask(struct cy8c95x0_pinctrl * chip,int reg,unsigned long * val,unsigned long * mask)623 static int cy8c95x0_read_regs_mask(struct cy8c95x0_pinctrl *chip, int reg,
624 unsigned long *val, unsigned long *mask)
625 {
626 DECLARE_BITMAP(tmask, MAX_LINE);
627 DECLARE_BITMAP(tval, MAX_LINE);
628 unsigned long bits, offset;
629 unsigned int read_val;
630 int ret;
631
632 /* Add the 4 bit gap of Gport2 */
633 bitmap_scatter(tmask, mask, chip->map, MAX_LINE);
634 bitmap_scatter(tval, val, chip->map, MAX_LINE);
635
636 for_each_set_clump8(offset, bits, tmask, chip->nport * BANK_SZ) {
637 unsigned int i = offset / 8;
638
639 ret = cy8c95x0_regmap_read_bits(chip, reg, i, bits, &read_val);
640 if (ret < 0) {
641 dev_err(chip->dev, "failed reading register %d, port %u: err %d\n", reg, i, ret);
642 return ret;
643 }
644
645 read_val |= bitmap_get_value8(tval, offset) & ~bits;
646 bitmap_set_value8(tval, read_val, offset);
647 }
648
649 /* Fill the 4 bit gap of Gport2 */
650 bitmap_gather(val, tval, chip->map, MAX_LINE);
651
652 return 0;
653 }
654
cy8c95x0_pinmux_direction(struct cy8c95x0_pinctrl * chip,unsigned int pin,bool input)655 static int cy8c95x0_pinmux_direction(struct cy8c95x0_pinctrl *chip, unsigned int pin, bool input)
656 {
657 u8 port = cypress_get_port(chip, pin);
658 u8 bit = cypress_get_pin_mask(chip, pin);
659 int ret;
660
661 ret = cy8c95x0_regmap_write_bits(chip, CY8C95X0_DIRECTION, port, bit, input ? bit : 0);
662 if (ret)
663 return ret;
664
665 /*
666 * Disable driving the pin by forcing it to HighZ. Only setting
667 * the direction register isn't sufficient in Push-Pull mode.
668 */
669 if (input && test_bit(pin, chip->push_pull)) {
670 ret = cy8c95x0_regmap_write_bits(chip, CY8C95X0_DRV_HIZ, port, bit, bit);
671 if (ret)
672 return ret;
673
674 __clear_bit(pin, chip->push_pull);
675 }
676
677 return 0;
678 }
679
cy8c95x0_gpio_direction_input(struct gpio_chip * gc,unsigned int off)680 static int cy8c95x0_gpio_direction_input(struct gpio_chip *gc, unsigned int off)
681 {
682 return pinctrl_gpio_direction_input(gc, off);
683 }
684
cy8c95x0_gpio_direction_output(struct gpio_chip * gc,unsigned int off,int val)685 static int cy8c95x0_gpio_direction_output(struct gpio_chip *gc,
686 unsigned int off, int val)
687 {
688 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
689 u8 port = cypress_get_port(chip, off);
690 u8 bit = cypress_get_pin_mask(chip, off);
691 int ret;
692
693 /* Set output level */
694 ret = cy8c95x0_regmap_write_bits(chip, CY8C95X0_OUTPUT, port, bit, val ? bit : 0);
695 if (ret)
696 return ret;
697
698 return pinctrl_gpio_direction_output(gc, off);
699 }
700
cy8c95x0_gpio_get_value(struct gpio_chip * gc,unsigned int off)701 static int cy8c95x0_gpio_get_value(struct gpio_chip *gc, unsigned int off)
702 {
703 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
704 u8 port = cypress_get_port(chip, off);
705 u8 bit = cypress_get_pin_mask(chip, off);
706 unsigned int reg_val;
707 int ret;
708
709 ret = cy8c95x0_regmap_read_bits(chip, CY8C95X0_INPUT, port, bit, ®_val);
710 if (ret < 0) {
711 /*
712 * NOTE:
713 * Diagnostic already emitted; that's all we should
714 * do unless gpio_*_value_cansleep() calls become different
715 * from their nonsleeping siblings (and report faults).
716 */
717 return 0;
718 }
719
720 return reg_val ? 1 : 0;
721 }
722
cy8c95x0_gpio_set_value(struct gpio_chip * gc,unsigned int off,int val)723 static int cy8c95x0_gpio_set_value(struct gpio_chip *gc, unsigned int off,
724 int val)
725 {
726 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
727 u8 port = cypress_get_port(chip, off);
728 u8 bit = cypress_get_pin_mask(chip, off);
729
730 return cy8c95x0_regmap_write_bits(chip, CY8C95X0_OUTPUT, port, bit,
731 val ? bit : 0);
732 }
733
cy8c95x0_gpio_get_direction(struct gpio_chip * gc,unsigned int off)734 static int cy8c95x0_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
735 {
736 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
737 u8 port = cypress_get_port(chip, off);
738 u8 bit = cypress_get_pin_mask(chip, off);
739 unsigned int reg_val;
740 int ret;
741
742 ret = cy8c95x0_regmap_read_bits(chip, CY8C95X0_DIRECTION, port, bit, ®_val);
743 if (ret < 0)
744 return ret;
745
746 if (reg_val)
747 return GPIO_LINE_DIRECTION_IN;
748
749 return GPIO_LINE_DIRECTION_OUT;
750 }
751
cy8c95x0_gpio_get_pincfg(struct cy8c95x0_pinctrl * chip,unsigned int off,unsigned long * config)752 static int cy8c95x0_gpio_get_pincfg(struct cy8c95x0_pinctrl *chip,
753 unsigned int off,
754 unsigned long *config)
755 {
756 enum pin_config_param param = pinconf_to_config_param(*config);
757 u8 port = cypress_get_port(chip, off);
758 u8 bit = cypress_get_pin_mask(chip, off);
759 unsigned int reg_val;
760 unsigned int reg;
761 u16 arg = 0;
762 int ret;
763
764 switch (param) {
765 case PIN_CONFIG_BIAS_PULL_UP:
766 reg = CY8C95X0_DRV_PU;
767 break;
768 case PIN_CONFIG_BIAS_PULL_DOWN:
769 reg = CY8C95X0_DRV_PD;
770 break;
771 case PIN_CONFIG_BIAS_DISABLE:
772 reg = CY8C95X0_DRV_HIZ;
773 break;
774 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
775 reg = CY8C95X0_DRV_ODL;
776 break;
777 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
778 reg = CY8C95X0_DRV_ODH;
779 break;
780 case PIN_CONFIG_DRIVE_PUSH_PULL:
781 reg = CY8C95X0_DRV_PP_FAST;
782 break;
783 case PIN_CONFIG_INPUT_ENABLE:
784 reg = CY8C95X0_DIRECTION;
785 break;
786 case PIN_CONFIG_MODE_PWM:
787 reg = CY8C95X0_SELPWM;
788 break;
789 case PIN_CONFIG_LEVEL:
790 reg = CY8C95X0_OUTPUT;
791 break;
792 case PIN_CONFIG_OUTPUT_ENABLE:
793 reg = CY8C95X0_DIRECTION;
794 break;
795
796 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
797 case PIN_CONFIG_BIAS_BUS_HOLD:
798 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
799 case PIN_CONFIG_DRIVE_STRENGTH:
800 case PIN_CONFIG_DRIVE_STRENGTH_UA:
801 case PIN_CONFIG_INPUT_DEBOUNCE:
802 case PIN_CONFIG_INPUT_SCHMITT:
803 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
804 case PIN_CONFIG_MODE_LOW_POWER:
805 case PIN_CONFIG_PERSIST_STATE:
806 case PIN_CONFIG_POWER_SOURCE:
807 case PIN_CONFIG_SKEW_DELAY:
808 case PIN_CONFIG_SLEEP_HARDWARE_STATE:
809 case PIN_CONFIG_SLEW_RATE:
810 default:
811 return -ENOTSUPP;
812 }
813 /*
814 * Writing 1 to one of the drive mode registers will automatically
815 * clear conflicting set bits in the other drive mode registers.
816 */
817 ret = cy8c95x0_regmap_read_bits(chip, reg, port, bit, ®_val);
818 if (ret < 0)
819 return ret;
820
821 if (reg_val)
822 arg = 1;
823 if (param == PIN_CONFIG_OUTPUT_ENABLE)
824 arg = !arg;
825
826 *config = pinconf_to_config_packed(param, arg);
827 return 0;
828 }
829
cy8c95x0_gpio_set_pincfg(struct cy8c95x0_pinctrl * chip,unsigned int off,unsigned long config)830 static int cy8c95x0_gpio_set_pincfg(struct cy8c95x0_pinctrl *chip,
831 unsigned int off,
832 unsigned long config)
833 {
834 u8 port = cypress_get_port(chip, off);
835 u8 bit = cypress_get_pin_mask(chip, off);
836 unsigned long param = pinconf_to_config_param(config);
837 unsigned long arg = pinconf_to_config_argument(config);
838 unsigned int reg;
839
840 switch (param) {
841 case PIN_CONFIG_BIAS_PULL_UP:
842 __clear_bit(off, chip->push_pull);
843 reg = CY8C95X0_DRV_PU;
844 break;
845 case PIN_CONFIG_BIAS_PULL_DOWN:
846 __clear_bit(off, chip->push_pull);
847 reg = CY8C95X0_DRV_PD;
848 break;
849 case PIN_CONFIG_BIAS_DISABLE:
850 __clear_bit(off, chip->push_pull);
851 reg = CY8C95X0_DRV_HIZ;
852 break;
853 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
854 __clear_bit(off, chip->push_pull);
855 reg = CY8C95X0_DRV_ODL;
856 break;
857 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
858 __clear_bit(off, chip->push_pull);
859 reg = CY8C95X0_DRV_ODH;
860 break;
861 case PIN_CONFIG_DRIVE_PUSH_PULL:
862 __set_bit(off, chip->push_pull);
863 reg = CY8C95X0_DRV_PP_FAST;
864 break;
865 case PIN_CONFIG_MODE_PWM:
866 reg = CY8C95X0_SELPWM;
867 break;
868 case PIN_CONFIG_OUTPUT_ENABLE:
869 return cy8c95x0_pinmux_direction(chip, off, !arg);
870 case PIN_CONFIG_INPUT_ENABLE:
871 return cy8c95x0_pinmux_direction(chip, off, arg);
872 default:
873 return -ENOTSUPP;
874 }
875 /*
876 * Writing 1 to one of the drive mode registers will automatically
877 * clear conflicting set bits in the other drive mode registers.
878 */
879 return cy8c95x0_regmap_write_bits(chip, reg, port, bit, bit);
880 }
881
cy8c95x0_gpio_get_multiple(struct gpio_chip * gc,unsigned long * mask,unsigned long * bits)882 static int cy8c95x0_gpio_get_multiple(struct gpio_chip *gc,
883 unsigned long *mask, unsigned long *bits)
884 {
885 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
886
887 return cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, bits, mask);
888 }
889
cy8c95x0_gpio_set_multiple(struct gpio_chip * gc,unsigned long * mask,unsigned long * bits)890 static int cy8c95x0_gpio_set_multiple(struct gpio_chip *gc,
891 unsigned long *mask, unsigned long *bits)
892 {
893 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
894
895 return cy8c95x0_write_regs_mask(chip, CY8C95X0_OUTPUT, bits, mask);
896 }
897
cy8c95x0_add_pin_ranges(struct gpio_chip * gc)898 static int cy8c95x0_add_pin_ranges(struct gpio_chip *gc)
899 {
900 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
901 struct device *dev = chip->dev;
902 int ret;
903
904 ret = gpiochip_add_pin_range(gc, dev_name(dev), 0, 0, chip->tpin);
905 if (ret)
906 dev_err(dev, "failed to add GPIO pin range\n");
907
908 return ret;
909 }
910
cy8c95x0_setup_gpiochip(struct cy8c95x0_pinctrl * chip)911 static int cy8c95x0_setup_gpiochip(struct cy8c95x0_pinctrl *chip)
912 {
913 struct gpio_chip *gc = &chip->gpio_chip;
914
915 gc->request = gpiochip_generic_request;
916 gc->free = gpiochip_generic_free;
917 gc->direction_input = cy8c95x0_gpio_direction_input;
918 gc->direction_output = cy8c95x0_gpio_direction_output;
919 gc->get = cy8c95x0_gpio_get_value;
920 gc->set = cy8c95x0_gpio_set_value;
921 gc->get_direction = cy8c95x0_gpio_get_direction;
922 gc->get_multiple = cy8c95x0_gpio_get_multiple;
923 gc->set_multiple = cy8c95x0_gpio_set_multiple;
924 gc->set_config = gpiochip_generic_config;
925 gc->can_sleep = true;
926 gc->add_pin_ranges = cy8c95x0_add_pin_ranges;
927
928 gc->base = -1;
929 gc->ngpio = chip->tpin;
930
931 gc->parent = chip->dev;
932 gc->owner = THIS_MODULE;
933 gc->names = NULL;
934
935 gc->label = dev_name(chip->dev);
936
937 return devm_gpiochip_add_data(chip->dev, gc, chip);
938 }
939
cy8c95x0_irq_mask(struct irq_data * d)940 static void cy8c95x0_irq_mask(struct irq_data *d)
941 {
942 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
943 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
944 irq_hw_number_t hwirq = irqd_to_hwirq(d);
945
946 set_bit(hwirq, chip->irq_mask);
947 gpiochip_disable_irq(gc, hwirq);
948 }
949
cy8c95x0_irq_unmask(struct irq_data * d)950 static void cy8c95x0_irq_unmask(struct irq_data *d)
951 {
952 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
953 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
954 irq_hw_number_t hwirq = irqd_to_hwirq(d);
955
956 gpiochip_enable_irq(gc, hwirq);
957 clear_bit(hwirq, chip->irq_mask);
958 }
959
cy8c95x0_irq_bus_lock(struct irq_data * d)960 static void cy8c95x0_irq_bus_lock(struct irq_data *d)
961 {
962 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
963 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
964
965 mutex_lock(&chip->irq_lock);
966 }
967
cy8c95x0_irq_bus_sync_unlock(struct irq_data * d)968 static void cy8c95x0_irq_bus_sync_unlock(struct irq_data *d)
969 {
970 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
971 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
972 DECLARE_BITMAP(ones, MAX_LINE);
973 DECLARE_BITMAP(irq_mask, MAX_LINE);
974 DECLARE_BITMAP(reg_direction, MAX_LINE);
975
976 bitmap_fill(ones, MAX_LINE);
977
978 cy8c95x0_write_regs_mask(chip, CY8C95X0_INTMASK, chip->irq_mask, ones);
979
980 /* Switch direction to input if needed */
981 cy8c95x0_read_regs_mask(chip, CY8C95X0_DIRECTION, reg_direction, chip->irq_mask);
982 bitmap_or(irq_mask, chip->irq_mask, reg_direction, MAX_LINE);
983 bitmap_complement(irq_mask, irq_mask, MAX_LINE);
984
985 /* Look for any newly setup interrupt */
986 cy8c95x0_write_regs_mask(chip, CY8C95X0_DIRECTION, ones, irq_mask);
987
988 mutex_unlock(&chip->irq_lock);
989 }
990
cy8c95x0_irq_set_type(struct irq_data * d,unsigned int type)991 static int cy8c95x0_irq_set_type(struct irq_data *d, unsigned int type)
992 {
993 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
994 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
995 irq_hw_number_t hwirq = irqd_to_hwirq(d);
996 unsigned int trig_type;
997
998 switch (type) {
999 case IRQ_TYPE_EDGE_RISING:
1000 case IRQ_TYPE_EDGE_FALLING:
1001 case IRQ_TYPE_EDGE_BOTH:
1002 trig_type = type;
1003 break;
1004 case IRQ_TYPE_LEVEL_HIGH:
1005 trig_type = IRQ_TYPE_EDGE_RISING;
1006 break;
1007 case IRQ_TYPE_LEVEL_LOW:
1008 trig_type = IRQ_TYPE_EDGE_FALLING;
1009 break;
1010 default:
1011 dev_err(chip->dev, "irq %d: unsupported type %d\n", d->irq, type);
1012 return -EINVAL;
1013 }
1014
1015 assign_bit(hwirq, chip->irq_trig_fall, trig_type & IRQ_TYPE_EDGE_FALLING);
1016 assign_bit(hwirq, chip->irq_trig_raise, trig_type & IRQ_TYPE_EDGE_RISING);
1017 assign_bit(hwirq, chip->irq_trig_low, type == IRQ_TYPE_LEVEL_LOW);
1018 assign_bit(hwirq, chip->irq_trig_high, type == IRQ_TYPE_LEVEL_HIGH);
1019
1020 return 0;
1021 }
1022
cy8c95x0_irq_shutdown(struct irq_data * d)1023 static void cy8c95x0_irq_shutdown(struct irq_data *d)
1024 {
1025 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1026 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
1027 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1028
1029 clear_bit(hwirq, chip->irq_trig_raise);
1030 clear_bit(hwirq, chip->irq_trig_fall);
1031 clear_bit(hwirq, chip->irq_trig_low);
1032 clear_bit(hwirq, chip->irq_trig_high);
1033 }
1034
1035 static const struct irq_chip cy8c95x0_irqchip = {
1036 .name = "cy8c95x0-irq",
1037 .irq_mask = cy8c95x0_irq_mask,
1038 .irq_unmask = cy8c95x0_irq_unmask,
1039 .irq_bus_lock = cy8c95x0_irq_bus_lock,
1040 .irq_bus_sync_unlock = cy8c95x0_irq_bus_sync_unlock,
1041 .irq_set_type = cy8c95x0_irq_set_type,
1042 .irq_shutdown = cy8c95x0_irq_shutdown,
1043 .flags = IRQCHIP_IMMUTABLE,
1044 GPIOCHIP_IRQ_RESOURCE_HELPERS,
1045 };
1046
cy8c95x0_irq_pending(struct cy8c95x0_pinctrl * chip,unsigned long * pending)1047 static bool cy8c95x0_irq_pending(struct cy8c95x0_pinctrl *chip, unsigned long *pending)
1048 {
1049 DECLARE_BITMAP(ones, MAX_LINE);
1050 DECLARE_BITMAP(cur_stat, MAX_LINE);
1051 DECLARE_BITMAP(new_stat, MAX_LINE);
1052 DECLARE_BITMAP(trigger, MAX_LINE);
1053
1054 bitmap_fill(ones, MAX_LINE);
1055
1056 /* Read the current interrupt status from the device */
1057 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INTSTATUS, trigger, ones))
1058 return false;
1059
1060 /* Check latched inputs */
1061 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, cur_stat, trigger))
1062 return false;
1063
1064 /* Apply filter for rising/falling edge selection */
1065 bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise,
1066 cur_stat, MAX_LINE);
1067
1068 bitmap_and(pending, new_stat, trigger, MAX_LINE);
1069
1070 return !bitmap_empty(pending, MAX_LINE);
1071 }
1072
cy8c95x0_irq_handler(int irq,void * devid)1073 static irqreturn_t cy8c95x0_irq_handler(int irq, void *devid)
1074 {
1075 struct cy8c95x0_pinctrl *chip = devid;
1076 struct gpio_chip *gc = &chip->gpio_chip;
1077 DECLARE_BITMAP(pending, MAX_LINE);
1078 int nested_irq, level;
1079 bool ret;
1080
1081 ret = cy8c95x0_irq_pending(chip, pending);
1082 if (!ret)
1083 return IRQ_RETVAL(0);
1084
1085 ret = false;
1086 for_each_set_bit(level, pending, MAX_LINE) {
1087 /* Already accounted for 4bit gap in GPort2 */
1088 nested_irq = irq_find_mapping(gc->irq.domain, level);
1089
1090 if (unlikely(nested_irq <= 0)) {
1091 dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level);
1092 continue;
1093 }
1094
1095 if (test_bit(level, chip->irq_trig_low))
1096 while (!cy8c95x0_gpio_get_value(gc, level))
1097 handle_nested_irq(nested_irq);
1098 else if (test_bit(level, chip->irq_trig_high))
1099 while (cy8c95x0_gpio_get_value(gc, level))
1100 handle_nested_irq(nested_irq);
1101 else
1102 handle_nested_irq(nested_irq);
1103
1104 ret = true;
1105 }
1106
1107 return IRQ_RETVAL(ret);
1108 }
1109
cy8c95x0_pinctrl_get_groups_count(struct pinctrl_dev * pctldev)1110 static int cy8c95x0_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
1111 {
1112 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1113
1114 return chip->tpin;
1115 }
1116
cy8c95x0_pinctrl_get_group_name(struct pinctrl_dev * pctldev,unsigned int group)1117 static const char *cy8c95x0_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
1118 unsigned int group)
1119 {
1120 return cy8c95x0_groups[group];
1121 }
1122
cy8c95x0_pinctrl_get_group_pins(struct pinctrl_dev * pctldev,unsigned int group,const unsigned int ** pins,unsigned int * num_pins)1123 static int cy8c95x0_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
1124 unsigned int group,
1125 const unsigned int **pins,
1126 unsigned int *num_pins)
1127 {
1128 *pins = &cy8c9560_pins[group].number;
1129 *num_pins = 1;
1130 return 0;
1131 }
1132
cy8c95x0_get_fname(unsigned int selector)1133 static const char *cy8c95x0_get_fname(unsigned int selector)
1134 {
1135 if (selector == 0)
1136 return "gpio";
1137 else
1138 return "pwm";
1139 }
1140
cy8c95x0_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned int pin)1141 static void cy8c95x0_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1142 unsigned int pin)
1143 {
1144 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1145 DECLARE_BITMAP(mask, MAX_LINE);
1146 DECLARE_BITMAP(pwm, MAX_LINE);
1147
1148 bitmap_zero(mask, MAX_LINE);
1149 __set_bit(pin, mask);
1150
1151 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_SELPWM, pwm, mask)) {
1152 seq_puts(s, "not available");
1153 return;
1154 }
1155
1156 seq_printf(s, "MODE:%s", cy8c95x0_get_fname(test_bit(pin, pwm)));
1157 }
1158
1159 static const struct pinctrl_ops cy8c95x0_pinctrl_ops = {
1160 .get_groups_count = cy8c95x0_pinctrl_get_groups_count,
1161 .get_group_name = cy8c95x0_pinctrl_get_group_name,
1162 .get_group_pins = cy8c95x0_pinctrl_get_group_pins,
1163 #ifdef CONFIG_OF
1164 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1165 .dt_free_map = pinconf_generic_dt_free_map,
1166 #endif
1167 .pin_dbg_show = cy8c95x0_pin_dbg_show,
1168 };
1169
cy8c95x0_get_function_name(struct pinctrl_dev * pctldev,unsigned int selector)1170 static const char *cy8c95x0_get_function_name(struct pinctrl_dev *pctldev, unsigned int selector)
1171 {
1172 return cy8c95x0_get_fname(selector);
1173 }
1174
cy8c95x0_get_functions_count(struct pinctrl_dev * pctldev)1175 static int cy8c95x0_get_functions_count(struct pinctrl_dev *pctldev)
1176 {
1177 return 2;
1178 }
1179
cy8c95x0_get_function_groups(struct pinctrl_dev * pctldev,unsigned int selector,const char * const ** groups,unsigned int * const num_groups)1180 static int cy8c95x0_get_function_groups(struct pinctrl_dev *pctldev, unsigned int selector,
1181 const char * const **groups,
1182 unsigned int * const num_groups)
1183 {
1184 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1185
1186 *groups = cy8c95x0_groups;
1187 *num_groups = chip->tpin;
1188 return 0;
1189 }
1190
cy8c95x0_set_mode(struct cy8c95x0_pinctrl * chip,unsigned int off,bool mode)1191 static int cy8c95x0_set_mode(struct cy8c95x0_pinctrl *chip, unsigned int off, bool mode)
1192 {
1193 u8 port = cypress_get_port(chip, off);
1194 u8 bit = cypress_get_pin_mask(chip, off);
1195
1196 return cy8c95x0_regmap_write_bits(chip, CY8C95X0_SELPWM, port, bit, mode ? bit : 0);
1197 }
1198
cy8c95x0_pinmux_mode(struct cy8c95x0_pinctrl * chip,unsigned int selector,unsigned int group)1199 static int cy8c95x0_pinmux_mode(struct cy8c95x0_pinctrl *chip,
1200 unsigned int selector, unsigned int group)
1201 {
1202 u8 port = cypress_get_port(chip, group);
1203 u8 bit = cypress_get_pin_mask(chip, group);
1204 int ret;
1205
1206 ret = cy8c95x0_set_mode(chip, group, selector);
1207 if (ret < 0)
1208 return ret;
1209
1210 if (selector == 0)
1211 return 0;
1212
1213 /* Set direction to output & set output to 1 so that PWM can work */
1214 ret = cy8c95x0_regmap_write_bits(chip, CY8C95X0_DIRECTION, port, bit, bit);
1215 if (ret < 0)
1216 return ret;
1217
1218 return cy8c95x0_regmap_write_bits(chip, CY8C95X0_OUTPUT, port, bit, bit);
1219 }
1220
cy8c95x0_set_mux(struct pinctrl_dev * pctldev,unsigned int selector,unsigned int group)1221 static int cy8c95x0_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
1222 unsigned int group)
1223 {
1224 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1225
1226 return cy8c95x0_pinmux_mode(chip, selector, group);
1227 }
1228
cy8c95x0_gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin)1229 static int cy8c95x0_gpio_request_enable(struct pinctrl_dev *pctldev,
1230 struct pinctrl_gpio_range *range,
1231 unsigned int pin)
1232 {
1233 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1234
1235 return cy8c95x0_set_mode(chip, pin, false);
1236 }
1237
cy8c95x0_gpio_set_direction(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin,bool input)1238 static int cy8c95x0_gpio_set_direction(struct pinctrl_dev *pctldev,
1239 struct pinctrl_gpio_range *range,
1240 unsigned int pin, bool input)
1241 {
1242 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1243
1244 return cy8c95x0_pinmux_direction(chip, pin, input);
1245 }
1246
1247 static const struct pinmux_ops cy8c95x0_pmxops = {
1248 .get_functions_count = cy8c95x0_get_functions_count,
1249 .get_function_name = cy8c95x0_get_function_name,
1250 .get_function_groups = cy8c95x0_get_function_groups,
1251 .set_mux = cy8c95x0_set_mux,
1252 .gpio_request_enable = cy8c95x0_gpio_request_enable,
1253 .gpio_set_direction = cy8c95x0_gpio_set_direction,
1254 .strict = true,
1255 };
1256
cy8c95x0_pinconf_get(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * config)1257 static int cy8c95x0_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
1258 unsigned long *config)
1259 {
1260 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1261
1262 return cy8c95x0_gpio_get_pincfg(chip, pin, config);
1263 }
1264
cy8c95x0_pinconf_set(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * configs,unsigned int num_configs)1265 static int cy8c95x0_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1266 unsigned long *configs, unsigned int num_configs)
1267 {
1268 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1269 int ret;
1270 int i;
1271
1272 for (i = 0; i < num_configs; i++) {
1273 ret = cy8c95x0_gpio_set_pincfg(chip, pin, configs[i]);
1274 if (ret)
1275 return ret;
1276 }
1277
1278 return 0;
1279 }
1280
1281 static const struct pinconf_ops cy8c95x0_pinconf_ops = {
1282 .pin_config_get = cy8c95x0_pinconf_get,
1283 .pin_config_set = cy8c95x0_pinconf_set,
1284 .is_generic = true,
1285 };
1286
cy8c95x0_irq_setup(struct cy8c95x0_pinctrl * chip,int irq)1287 static int cy8c95x0_irq_setup(struct cy8c95x0_pinctrl *chip, int irq)
1288 {
1289 struct gpio_irq_chip *girq = &chip->gpio_chip.irq;
1290 DECLARE_BITMAP(pending_irqs, MAX_LINE);
1291 struct device *dev = chip->dev;
1292 int ret;
1293
1294 ret = devm_mutex_init(chip->dev, &chip->irq_lock);
1295 if (ret)
1296 return ret;
1297
1298 bitmap_zero(pending_irqs, MAX_LINE);
1299
1300 /* Read IRQ status register to clear all pending interrupts */
1301 ret = cy8c95x0_irq_pending(chip, pending_irqs);
1302 if (ret)
1303 return dev_err_probe(dev, -EBUSY, "failed to clear irq status register\n");
1304
1305 /* Mask all interrupts */
1306 bitmap_fill(chip->irq_mask, MAX_LINE);
1307
1308 gpio_irq_chip_set_chip(girq, &cy8c95x0_irqchip);
1309
1310 /* This will let us handle the parent IRQ in the driver */
1311 girq->parent_handler = NULL;
1312 girq->num_parents = 0;
1313 girq->parents = NULL;
1314 girq->default_type = IRQ_TYPE_NONE;
1315 girq->handler = handle_simple_irq;
1316 girq->threaded = true;
1317
1318 return devm_request_threaded_irq(dev, irq, NULL, cy8c95x0_irq_handler,
1319 IRQF_ONESHOT | IRQF_SHARED,
1320 dev_name(chip->dev), chip);
1321 }
1322
cy8c95x0_setup_pinctrl(struct cy8c95x0_pinctrl * chip)1323 static int cy8c95x0_setup_pinctrl(struct cy8c95x0_pinctrl *chip)
1324 {
1325 struct pinctrl_desc *pd = &chip->pinctrl_desc;
1326
1327 pd->pctlops = &cy8c95x0_pinctrl_ops;
1328 pd->confops = &cy8c95x0_pinconf_ops;
1329 pd->pmxops = &cy8c95x0_pmxops;
1330 pd->name = dev_name(chip->dev);
1331 pd->pins = cy8c9560_pins;
1332 pd->npins = chip->tpin;
1333 pd->owner = THIS_MODULE;
1334
1335 chip->pctldev = devm_pinctrl_register(chip->dev, pd, chip);
1336 return PTR_ERR_OR_ZERO(chip->pctldev);
1337 }
1338
cy8c95x0_detect(struct i2c_client * client,struct i2c_board_info * info)1339 static int cy8c95x0_detect(struct i2c_client *client,
1340 struct i2c_board_info *info)
1341 {
1342 struct i2c_adapter *adapter = client->adapter;
1343 int ret;
1344 const char *name;
1345
1346 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1347 return -ENODEV;
1348
1349 ret = i2c_smbus_read_byte_data(client, CY8C95X0_DEVID);
1350 if (ret < 0)
1351 return ret;
1352 switch (ret & GENMASK(7, 4)) {
1353 case 0x20:
1354 name = "cy8c9520";
1355 break;
1356 case 0x40:
1357 name = "cy8c9540";
1358 break;
1359 case 0x60:
1360 name = "cy8c9560";
1361 break;
1362 default:
1363 return -ENODEV;
1364 }
1365
1366 dev_info(&client->dev, "Found a %s chip at 0x%02x.\n", name, client->addr);
1367 strscpy(info->type, name);
1368
1369 return 0;
1370 }
1371
cy8c95x0_probe(struct i2c_client * client)1372 static int cy8c95x0_probe(struct i2c_client *client)
1373 {
1374 struct device *dev = &client->dev;
1375 struct cy8c95x0_pinctrl *chip;
1376 struct regmap_config regmap_conf;
1377 struct regmap_range_cfg regmap_range_conf;
1378 unsigned long driver_data;
1379 int ret;
1380
1381 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
1382 if (!chip)
1383 return -ENOMEM;
1384
1385 chip->dev = dev;
1386
1387 /* Set the device type */
1388 driver_data = (unsigned long)i2c_get_match_data(client);
1389
1390 chip->tpin = driver_data & CY8C95X0_GPIO_MASK;
1391 chip->nport = DIV_ROUND_UP(CY8C95X0_PIN_TO_OFFSET(chip->tpin), BANK_SZ);
1392
1393 memcpy(®map_range_conf, &cy8c95x0_ranges[0], sizeof(regmap_range_conf));
1394
1395 switch (chip->tpin) {
1396 case 20:
1397 regmap_range_conf.range_max = CY8C95X0_VIRTUAL + 3 * MUXED_STRIDE - 1;
1398 break;
1399 case 40:
1400 regmap_range_conf.range_max = CY8C95X0_VIRTUAL + 6 * MUXED_STRIDE - 1;
1401 break;
1402 case 60:
1403 regmap_range_conf.range_max = CY8C95X0_VIRTUAL + 8 * MUXED_STRIDE - 1;
1404 break;
1405 default:
1406 return -ENODEV;
1407 }
1408
1409 ret = devm_regulator_get_enable(dev, "vdd");
1410 if (ret)
1411 return dev_err_probe(dev, ret, "failed to enable regulator vdd\n");
1412
1413 /* bring the chip out of reset if reset pin is provided */
1414 chip->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1415 if (IS_ERR(chip->gpio_reset))
1416 return dev_err_probe(dev, PTR_ERR(chip->gpio_reset), "Failed to get GPIO 'reset'\n");
1417 gpiod_set_consumer_name(chip->gpio_reset, "CY8C95X0 RESET");
1418 if (chip->gpio_reset) {
1419 fsleep(1000);
1420 gpiod_set_value_cansleep(chip->gpio_reset, 0);
1421 fsleep(250000);
1422 }
1423
1424 /* Regmap for direct and paged registers */
1425 memcpy(®map_conf, &cy8c9520_i2c_regmap, sizeof(regmap_conf));
1426 regmap_conf.ranges = ®map_range_conf;
1427 regmap_conf.max_register = regmap_range_conf.range_max;
1428 regmap_conf.num_reg_defaults_raw = regmap_range_conf.range_max;
1429
1430 chip->regmap = devm_regmap_init_i2c(client, ®map_conf);
1431 if (IS_ERR(chip->regmap))
1432 return PTR_ERR(chip->regmap);
1433
1434 bitmap_zero(chip->push_pull, MAX_LINE);
1435
1436 /* Setup HW pins mapping */
1437 bitmap_fill(chip->map, MAX_LINE);
1438 bitmap_clear(chip->map, 20, 4);
1439
1440 ret = devm_mutex_init(dev, &chip->i2c_lock);
1441 if (ret)
1442 return ret;
1443
1444 if (dmi_first_match(cy8c95x0_dmi_acpi_irq_info)) {
1445 ret = cy8c95x0_acpi_get_irq(&client->dev);
1446 if (ret > 0)
1447 client->irq = ret;
1448 }
1449
1450 if (client->irq) {
1451 ret = cy8c95x0_irq_setup(chip, client->irq);
1452 if (ret)
1453 return ret;
1454 }
1455
1456 ret = cy8c95x0_setup_pinctrl(chip);
1457 if (ret)
1458 return ret;
1459
1460 return cy8c95x0_setup_gpiochip(chip);
1461 }
1462
1463 static const struct i2c_device_id cy8c95x0_id[] = {
1464 { "cy8c9520", 20 },
1465 { "cy8c9540", 40 },
1466 { "cy8c9560", 60 },
1467 { }
1468 };
1469 MODULE_DEVICE_TABLE(i2c, cy8c95x0_id);
1470
1471 #define OF_CY8C95X(__nrgpio) ((void *)(__nrgpio))
1472
1473 static const struct of_device_id cy8c95x0_dt_ids[] = {
1474 { .compatible = "cypress,cy8c9520", .data = OF_CY8C95X(20) },
1475 { .compatible = "cypress,cy8c9540", .data = OF_CY8C95X(40) },
1476 { .compatible = "cypress,cy8c9560", .data = OF_CY8C95X(60) },
1477 { }
1478 };
1479 MODULE_DEVICE_TABLE(of, cy8c95x0_dt_ids);
1480
1481 static const struct acpi_device_id cy8c95x0_acpi_ids[] = {
1482 { "INT3490", 40 },
1483 { }
1484 };
1485 MODULE_DEVICE_TABLE(acpi, cy8c95x0_acpi_ids);
1486
1487 static struct i2c_driver cy8c95x0_driver = {
1488 .driver = {
1489 .name = "cy8c95x0-pinctrl",
1490 .of_match_table = cy8c95x0_dt_ids,
1491 .acpi_match_table = cy8c95x0_acpi_ids,
1492 },
1493 .probe = cy8c95x0_probe,
1494 .id_table = cy8c95x0_id,
1495 .detect = cy8c95x0_detect,
1496 };
1497 module_i2c_driver(cy8c95x0_driver);
1498
1499 MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>");
1500 MODULE_AUTHOR("Naresh Solanki <naresh.solanki@9elements.com>");
1501 MODULE_DESCRIPTION("Pinctrl driver for CY8C95X0");
1502 MODULE_LICENSE("GPL");
1503