1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2013, Sony Mobile Communications AB.
4 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 */
6
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/log2.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm.h>
17 #include <linux/firmware/qcom/qcom_scm.h>
18 #include <linux/reboot.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22
23 #include <linux/pinctrl/machine.h>
24 #include <linux/pinctrl/pinconf-generic.h>
25 #include <linux/pinctrl/pinconf.h>
26 #include <linux/pinctrl/pinmux.h>
27
28 #include <linux/soc/qcom/irq.h>
29
30 #include "../core.h"
31 #include "../pinconf.h"
32 #include "../pinctrl-utils.h"
33
34 #include "pinctrl-msm.h"
35
36 #define MAX_NR_GPIO 300
37 #define MAX_NR_TILES 4
38 #define PS_HOLD_OFFSET 0x820
39
40 /**
41 * struct msm_pinctrl - state for a pinctrl-msm device
42 * @dev: device handle.
43 * @pctrl: pinctrl handle.
44 * @chip: gpiochip handle.
45 * @desc: pin controller descriptor
46 * @irq: parent irq for the TLMM irq_chip.
47 * @intr_target_use_scm: route irq to application cpu using scm calls
48 * @lock: Spinlock to protect register resources as well
49 * as msm_pinctrl data structures.
50 * @enabled_irqs: Bitmap of currently enabled irqs.
51 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
52 * detection.
53 * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
54 * @disabled_for_mux: These IRQs were disabled because we muxed away.
55 * @ever_gpio: This bit is set the first time we mux a pin to gpio_func.
56 * @soc: Reference to soc_data of platform specific data.
57 * @regs: Base addresses for the TLMM tiles.
58 * @phys_base: Physical base address
59 */
60 struct msm_pinctrl {
61 struct device *dev;
62 struct pinctrl_dev *pctrl;
63 struct gpio_chip chip;
64 struct pinctrl_desc desc;
65
66 int irq;
67
68 bool intr_target_use_scm;
69
70 raw_spinlock_t lock;
71
72 DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
73 DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
74 DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO);
75 DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO);
76 DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
77
78 const struct msm_pinctrl_soc_data *soc;
79 void __iomem *regs[MAX_NR_TILES];
80 u32 phys_base[MAX_NR_TILES];
81 };
82
83 #define MSM_ACCESSOR(name) \
84 static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
85 const struct msm_pingroup *g) \
86 { \
87 return readl(pctrl->regs[g->tile] + g->name##_reg); \
88 } \
89 static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
90 const struct msm_pingroup *g) \
91 { \
92 writel(val, pctrl->regs[g->tile] + g->name##_reg); \
93 }
94
95 MSM_ACCESSOR(ctl)
MSM_ACCESSOR(io)96 MSM_ACCESSOR(io)
97 MSM_ACCESSOR(intr_cfg)
98 MSM_ACCESSOR(intr_status)
99 MSM_ACCESSOR(intr_target)
100
101 static void msm_ack_intr_status(struct msm_pinctrl *pctrl,
102 const struct msm_pingroup *g)
103 {
104 u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0;
105
106 msm_writel_intr_status(val, pctrl, g);
107 }
108
msm_get_groups_count(struct pinctrl_dev * pctldev)109 static int msm_get_groups_count(struct pinctrl_dev *pctldev)
110 {
111 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
112
113 return pctrl->soc->ngroups;
114 }
115
msm_get_group_name(struct pinctrl_dev * pctldev,unsigned group)116 static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
117 unsigned group)
118 {
119 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
120
121 return pctrl->soc->groups[group].grp.name;
122 }
123
msm_get_group_pins(struct pinctrl_dev * pctldev,unsigned group,const unsigned ** pins,unsigned * num_pins)124 static int msm_get_group_pins(struct pinctrl_dev *pctldev,
125 unsigned group,
126 const unsigned **pins,
127 unsigned *num_pins)
128 {
129 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
130
131 *pins = pctrl->soc->groups[group].grp.pins;
132 *num_pins = pctrl->soc->groups[group].grp.npins;
133 return 0;
134 }
135
136 static const struct pinctrl_ops msm_pinctrl_ops = {
137 .get_groups_count = msm_get_groups_count,
138 .get_group_name = msm_get_group_name,
139 .get_group_pins = msm_get_group_pins,
140 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
141 .dt_free_map = pinctrl_utils_free_map,
142 };
143
msm_pinmux_request(struct pinctrl_dev * pctldev,unsigned offset)144 static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
145 {
146 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
147 struct gpio_chip *chip = &pctrl->chip;
148
149 return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
150 }
151
msm_get_functions_count(struct pinctrl_dev * pctldev)152 static int msm_get_functions_count(struct pinctrl_dev *pctldev)
153 {
154 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
155
156 return pctrl->soc->nfunctions;
157 }
158
msm_get_function_name(struct pinctrl_dev * pctldev,unsigned function)159 static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
160 unsigned function)
161 {
162 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
163
164 return pctrl->soc->functions[function].name;
165 }
166
msm_get_function_groups(struct pinctrl_dev * pctldev,unsigned function,const char * const ** groups,unsigned * const num_groups)167 static int msm_get_function_groups(struct pinctrl_dev *pctldev,
168 unsigned function,
169 const char * const **groups,
170 unsigned * const num_groups)
171 {
172 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
173
174 *groups = pctrl->soc->functions[function].groups;
175 *num_groups = pctrl->soc->functions[function].ngroups;
176 return 0;
177 }
178
msm_pinmux_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)179 static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
180 unsigned function,
181 unsigned group)
182 {
183 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
184 struct gpio_chip *gc = &pctrl->chip;
185 unsigned int irq = irq_find_mapping(gc->irq.domain, group);
186 struct irq_data *d = irq_get_irq_data(irq);
187 unsigned int gpio_func = pctrl->soc->gpio_func;
188 unsigned int egpio_func = pctrl->soc->egpio_func;
189 const struct msm_pingroup *g;
190 unsigned long flags;
191 u32 val, mask;
192 int i;
193
194 g = &pctrl->soc->groups[group];
195 mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
196
197 for (i = 0; i < g->nfuncs; i++) {
198 if (g->funcs[i] == function)
199 break;
200 }
201
202 if (WARN_ON(i == g->nfuncs))
203 return -EINVAL;
204
205 /*
206 * If an GPIO interrupt is setup on this pin then we need special
207 * handling. Specifically interrupt detection logic will still see
208 * the pin twiddle even when we're muxed away.
209 *
210 * When we see a pin with an interrupt setup on it then we'll disable
211 * (mask) interrupts on it when we mux away until we mux back. Note
212 * that disable_irq() refcounts and interrupts are disabled as long as
213 * at least one disable_irq() has been called.
214 */
215 if (d && i != gpio_func &&
216 !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
217 disable_irq(irq);
218
219 raw_spin_lock_irqsave(&pctrl->lock, flags);
220
221 val = msm_readl_ctl(pctrl, g);
222
223 /*
224 * If this is the first time muxing to GPIO and the direction is
225 * output, make sure that we're not going to be glitching the pin
226 * by reading the current state of the pin and setting it as the
227 * output.
228 */
229 if (i == gpio_func && (val & BIT(g->oe_bit)) &&
230 !test_and_set_bit(group, pctrl->ever_gpio)) {
231 u32 io_val = msm_readl_io(pctrl, g);
232
233 if (io_val & BIT(g->in_bit)) {
234 if (!(io_val & BIT(g->out_bit)))
235 msm_writel_io(io_val | BIT(g->out_bit), pctrl, g);
236 } else {
237 if (io_val & BIT(g->out_bit))
238 msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g);
239 }
240 }
241
242 if (egpio_func && i == egpio_func) {
243 if (val & BIT(g->egpio_present))
244 val &= ~BIT(g->egpio_enable);
245 } else {
246 val &= ~mask;
247 val |= i << g->mux_bit;
248 /* Claim ownership of pin if egpio capable */
249 if (egpio_func && val & BIT(g->egpio_present))
250 val |= BIT(g->egpio_enable);
251 }
252
253 msm_writel_ctl(val, pctrl, g);
254
255 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
256
257 if (d && i == gpio_func &&
258 test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) {
259 /*
260 * Clear interrupts detected while not GPIO since we only
261 * masked things.
262 */
263 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
264 irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
265 else
266 msm_ack_intr_status(pctrl, g);
267
268 enable_irq(irq);
269 }
270
271 return 0;
272 }
273
msm_pinmux_request_gpio(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)274 static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
275 struct pinctrl_gpio_range *range,
276 unsigned offset)
277 {
278 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
279 const struct msm_pingroup *g = &pctrl->soc->groups[offset];
280
281 /* No funcs? Probably ACPI so can't do anything here */
282 if (!g->nfuncs)
283 return 0;
284
285 return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset);
286 }
287
288 static const struct pinmux_ops msm_pinmux_ops = {
289 .request = msm_pinmux_request,
290 .get_functions_count = msm_get_functions_count,
291 .get_function_name = msm_get_function_name,
292 .get_function_groups = msm_get_function_groups,
293 .gpio_request_enable = msm_pinmux_request_gpio,
294 .set_mux = msm_pinmux_set_mux,
295 };
296
msm_config_reg(struct msm_pinctrl * pctrl,const struct msm_pingroup * g,unsigned param,unsigned * mask,unsigned * bit)297 static int msm_config_reg(struct msm_pinctrl *pctrl,
298 const struct msm_pingroup *g,
299 unsigned param,
300 unsigned *mask,
301 unsigned *bit)
302 {
303 switch (param) {
304 case PIN_CONFIG_BIAS_DISABLE:
305 case PIN_CONFIG_BIAS_PULL_DOWN:
306 case PIN_CONFIG_BIAS_BUS_HOLD:
307 case PIN_CONFIG_BIAS_PULL_UP:
308 *bit = g->pull_bit;
309 *mask = 3;
310 if (g->i2c_pull_bit)
311 *mask |= BIT(g->i2c_pull_bit) >> *bit;
312 break;
313 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
314 *bit = g->od_bit;
315 *mask = 1;
316 break;
317 case PIN_CONFIG_DRIVE_STRENGTH:
318 *bit = g->drv_bit;
319 *mask = 7;
320 break;
321 case PIN_CONFIG_OUTPUT:
322 case PIN_CONFIG_INPUT_ENABLE:
323 case PIN_CONFIG_OUTPUT_ENABLE:
324 *bit = g->oe_bit;
325 *mask = 1;
326 break;
327 default:
328 return -ENOTSUPP;
329 }
330
331 return 0;
332 }
333
334 #define MSM_NO_PULL 0
335 #define MSM_PULL_DOWN 1
336 #define MSM_KEEPER 2
337 #define MSM_PULL_UP_NO_KEEPER 2
338 #define MSM_PULL_UP 3
339 #define MSM_I2C_STRONG_PULL_UP 2200
340
msm_regval_to_drive(u32 val)341 static unsigned msm_regval_to_drive(u32 val)
342 {
343 return (val + 1) * 2;
344 }
345
msm_config_group_get(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * config)346 static int msm_config_group_get(struct pinctrl_dev *pctldev,
347 unsigned int group,
348 unsigned long *config)
349 {
350 const struct msm_pingroup *g;
351 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
352 unsigned param = pinconf_to_config_param(*config);
353 unsigned mask;
354 unsigned arg;
355 unsigned bit;
356 int ret;
357 u32 val;
358
359 g = &pctrl->soc->groups[group];
360
361 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
362 if (ret < 0)
363 return ret;
364
365 val = msm_readl_ctl(pctrl, g);
366 arg = (val >> bit) & mask;
367
368 /* Convert register value to pinconf value */
369 switch (param) {
370 case PIN_CONFIG_BIAS_DISABLE:
371 if (arg != MSM_NO_PULL)
372 return -EINVAL;
373 arg = 1;
374 break;
375 case PIN_CONFIG_BIAS_PULL_DOWN:
376 if (arg != MSM_PULL_DOWN)
377 return -EINVAL;
378 arg = 1;
379 break;
380 case PIN_CONFIG_BIAS_BUS_HOLD:
381 if (pctrl->soc->pull_no_keeper)
382 return -ENOTSUPP;
383
384 if (arg != MSM_KEEPER)
385 return -EINVAL;
386 arg = 1;
387 break;
388 case PIN_CONFIG_BIAS_PULL_UP:
389 if (pctrl->soc->pull_no_keeper)
390 arg = arg == MSM_PULL_UP_NO_KEEPER;
391 else if (arg & BIT(g->i2c_pull_bit))
392 arg = MSM_I2C_STRONG_PULL_UP;
393 else
394 arg = arg == MSM_PULL_UP;
395 if (!arg)
396 return -EINVAL;
397 break;
398 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
399 /* Pin is not open-drain */
400 if (!arg)
401 return -EINVAL;
402 arg = 1;
403 break;
404 case PIN_CONFIG_DRIVE_STRENGTH:
405 arg = msm_regval_to_drive(arg);
406 break;
407 case PIN_CONFIG_OUTPUT:
408 /* Pin is not output */
409 if (!arg)
410 return -EINVAL;
411
412 val = msm_readl_io(pctrl, g);
413 arg = !!(val & BIT(g->in_bit));
414 break;
415 case PIN_CONFIG_OUTPUT_ENABLE:
416 if (!arg)
417 return -EINVAL;
418 break;
419 default:
420 return -ENOTSUPP;
421 }
422
423 *config = pinconf_to_config_packed(param, arg);
424
425 return 0;
426 }
427
msm_config_group_set(struct pinctrl_dev * pctldev,unsigned group,unsigned long * configs,unsigned num_configs)428 static int msm_config_group_set(struct pinctrl_dev *pctldev,
429 unsigned group,
430 unsigned long *configs,
431 unsigned num_configs)
432 {
433 const struct msm_pingroup *g;
434 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
435 unsigned long flags;
436 unsigned param;
437 unsigned mask;
438 unsigned arg;
439 unsigned bit;
440 int ret;
441 u32 val;
442 int i;
443
444 g = &pctrl->soc->groups[group];
445
446 for (i = 0; i < num_configs; i++) {
447 param = pinconf_to_config_param(configs[i]);
448 arg = pinconf_to_config_argument(configs[i]);
449
450 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
451 if (ret < 0)
452 return ret;
453
454 /* Convert pinconf values to register values */
455 switch (param) {
456 case PIN_CONFIG_BIAS_DISABLE:
457 arg = MSM_NO_PULL;
458 break;
459 case PIN_CONFIG_BIAS_PULL_DOWN:
460 arg = MSM_PULL_DOWN;
461 break;
462 case PIN_CONFIG_BIAS_BUS_HOLD:
463 if (pctrl->soc->pull_no_keeper)
464 return -ENOTSUPP;
465
466 arg = MSM_KEEPER;
467 break;
468 case PIN_CONFIG_BIAS_PULL_UP:
469 if (pctrl->soc->pull_no_keeper)
470 arg = MSM_PULL_UP_NO_KEEPER;
471 else if (g->i2c_pull_bit && arg == MSM_I2C_STRONG_PULL_UP)
472 arg = BIT(g->i2c_pull_bit) | MSM_PULL_UP;
473 else
474 arg = MSM_PULL_UP;
475 break;
476 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
477 arg = 1;
478 break;
479 case PIN_CONFIG_DRIVE_STRENGTH:
480 /* Check for invalid values */
481 if (arg > 16 || arg < 2 || (arg % 2) != 0)
482 arg = -1;
483 else
484 arg = (arg / 2) - 1;
485 break;
486 case PIN_CONFIG_OUTPUT:
487 /* set output value */
488 raw_spin_lock_irqsave(&pctrl->lock, flags);
489 val = msm_readl_io(pctrl, g);
490 if (arg)
491 val |= BIT(g->out_bit);
492 else
493 val &= ~BIT(g->out_bit);
494 msm_writel_io(val, pctrl, g);
495 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
496
497 /* enable output */
498 arg = 1;
499 break;
500 case PIN_CONFIG_INPUT_ENABLE:
501 /*
502 * According to pinctrl documentation this should
503 * actually be a no-op.
504 *
505 * The docs are explicit that "this does not affect
506 * the pin's ability to drive output" but what we do
507 * here is to modify the output enable bit. Thus, to
508 * follow the docs we should remove that.
509 *
510 * The docs say that we should enable any relevant
511 * input buffer, but TLMM there is no input buffer that
512 * can be enabled/disabled. It's always on.
513 *
514 * The points above, explain why this _should_ be a
515 * no-op. However, for historical reasons and to
516 * support old device trees, we'll violate the docs
517 * and still affect the output.
518 *
519 * It should further be noted that this old historical
520 * behavior actually overrides arg to 0. That means
521 * that "input-enable" and "input-disable" in a device
522 * tree would _both_ disable the output. We'll
523 * continue to preserve this behavior as well since
524 * we have no other use for this attribute.
525 */
526 arg = 0;
527 break;
528 case PIN_CONFIG_OUTPUT_ENABLE:
529 arg = !!arg;
530 break;
531 default:
532 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
533 param);
534 return -EINVAL;
535 }
536
537 /* Range-check user-supplied value */
538 if (arg & ~mask) {
539 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
540 return -EINVAL;
541 }
542
543 raw_spin_lock_irqsave(&pctrl->lock, flags);
544 val = msm_readl_ctl(pctrl, g);
545 val &= ~(mask << bit);
546 val |= arg << bit;
547 msm_writel_ctl(val, pctrl, g);
548 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
549 }
550
551 return 0;
552 }
553
554 static const struct pinconf_ops msm_pinconf_ops = {
555 .is_generic = true,
556 .pin_config_group_get = msm_config_group_get,
557 .pin_config_group_set = msm_config_group_set,
558 };
559
msm_gpio_direction_input(struct gpio_chip * chip,unsigned offset)560 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
561 {
562 const struct msm_pingroup *g;
563 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
564 unsigned long flags;
565 u32 val;
566
567 g = &pctrl->soc->groups[offset];
568
569 raw_spin_lock_irqsave(&pctrl->lock, flags);
570
571 val = msm_readl_ctl(pctrl, g);
572 val &= ~BIT(g->oe_bit);
573 msm_writel_ctl(val, pctrl, g);
574
575 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
576
577 return 0;
578 }
579
msm_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int value)580 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
581 {
582 const struct msm_pingroup *g;
583 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
584 unsigned long flags;
585 u32 val;
586
587 g = &pctrl->soc->groups[offset];
588
589 raw_spin_lock_irqsave(&pctrl->lock, flags);
590
591 val = msm_readl_io(pctrl, g);
592 if (value)
593 val |= BIT(g->out_bit);
594 else
595 val &= ~BIT(g->out_bit);
596 msm_writel_io(val, pctrl, g);
597
598 val = msm_readl_ctl(pctrl, g);
599 val |= BIT(g->oe_bit);
600 msm_writel_ctl(val, pctrl, g);
601
602 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
603
604 return 0;
605 }
606
msm_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)607 static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
608 {
609 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
610 const struct msm_pingroup *g;
611 u32 val;
612
613 g = &pctrl->soc->groups[offset];
614
615 val = msm_readl_ctl(pctrl, g);
616
617 return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
618 GPIO_LINE_DIRECTION_IN;
619 }
620
msm_gpio_get(struct gpio_chip * chip,unsigned offset)621 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
622 {
623 const struct msm_pingroup *g;
624 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
625 u32 val;
626
627 g = &pctrl->soc->groups[offset];
628
629 val = msm_readl_io(pctrl, g);
630 return !!(val & BIT(g->in_bit));
631 }
632
msm_gpio_set(struct gpio_chip * chip,unsigned offset,int value)633 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
634 {
635 const struct msm_pingroup *g;
636 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
637 unsigned long flags;
638 u32 val;
639
640 g = &pctrl->soc->groups[offset];
641
642 raw_spin_lock_irqsave(&pctrl->lock, flags);
643
644 val = msm_readl_io(pctrl, g);
645 if (value)
646 val |= BIT(g->out_bit);
647 else
648 val &= ~BIT(g->out_bit);
649 msm_writel_io(val, pctrl, g);
650
651 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
652 }
653
654 #ifdef CONFIG_DEBUG_FS
655
msm_gpio_dbg_show_one(struct seq_file * s,struct pinctrl_dev * pctldev,struct gpio_chip * chip,unsigned offset,unsigned gpio)656 static void msm_gpio_dbg_show_one(struct seq_file *s,
657 struct pinctrl_dev *pctldev,
658 struct gpio_chip *chip,
659 unsigned offset,
660 unsigned gpio)
661 {
662 const struct msm_pingroup *g;
663 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
664 unsigned func;
665 int is_out;
666 int drive;
667 int pull;
668 int val;
669 int egpio_enable;
670 u32 ctl_reg, io_reg;
671
672 static const char * const pulls_keeper[] = {
673 "no pull",
674 "pull down",
675 "keeper",
676 "pull up"
677 };
678
679 static const char * const pulls_no_keeper[] = {
680 "no pull",
681 "pull down",
682 "pull up",
683 };
684
685 if (!gpiochip_line_is_valid(chip, offset))
686 return;
687
688 g = &pctrl->soc->groups[offset];
689 ctl_reg = msm_readl_ctl(pctrl, g);
690 io_reg = msm_readl_io(pctrl, g);
691
692 is_out = !!(ctl_reg & BIT(g->oe_bit));
693 func = (ctl_reg >> g->mux_bit) & 7;
694 drive = (ctl_reg >> g->drv_bit) & 7;
695 pull = (ctl_reg >> g->pull_bit) & 3;
696 egpio_enable = 0;
697 if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present))
698 egpio_enable = !(ctl_reg & BIT(g->egpio_enable));
699
700 if (is_out)
701 val = !!(io_reg & BIT(g->out_bit));
702 else
703 val = !!(io_reg & BIT(g->in_bit));
704
705 if (egpio_enable) {
706 seq_printf(s, " %-8s: egpio\n", g->grp.name);
707 return;
708 }
709
710 seq_printf(s, " %-8s: %-3s", g->grp.name, is_out ? "out" : "in");
711 seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
712 seq_printf(s, " %dmA", msm_regval_to_drive(drive));
713 if (pctrl->soc->pull_no_keeper)
714 seq_printf(s, " %s", pulls_no_keeper[pull]);
715 else
716 seq_printf(s, " %s", pulls_keeper[pull]);
717 seq_puts(s, "\n");
718 }
719
msm_gpio_dbg_show(struct seq_file * s,struct gpio_chip * chip)720 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
721 {
722 unsigned gpio = chip->base;
723 unsigned i;
724
725 for (i = 0; i < chip->ngpio; i++, gpio++)
726 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
727 }
728
729 #else
730 #define msm_gpio_dbg_show NULL
731 #endif
732
msm_gpio_init_valid_mask(struct gpio_chip * gc,unsigned long * valid_mask,unsigned int ngpios)733 static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
734 unsigned long *valid_mask,
735 unsigned int ngpios)
736 {
737 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
738 int ret;
739 unsigned int len, i;
740 const int *reserved = pctrl->soc->reserved_gpios;
741 u16 *tmp;
742
743 /* Remove driver-provided reserved GPIOs from valid_mask */
744 if (reserved) {
745 for (i = 0; reserved[i] >= 0; i++) {
746 if (i >= ngpios || reserved[i] >= ngpios) {
747 dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
748 return -EINVAL;
749 }
750 clear_bit(reserved[i], valid_mask);
751 }
752
753 return 0;
754 }
755
756 /* The number of GPIOs in the ACPI tables */
757 len = ret = device_property_count_u16(pctrl->dev, "gpios");
758 if (ret < 0)
759 return 0;
760
761 if (ret > ngpios)
762 return -EINVAL;
763
764 tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
765 if (!tmp)
766 return -ENOMEM;
767
768 ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
769 if (ret < 0) {
770 dev_err(pctrl->dev, "could not read list of GPIOs\n");
771 goto out;
772 }
773
774 bitmap_zero(valid_mask, ngpios);
775 for (i = 0; i < len; i++)
776 set_bit(tmp[i], valid_mask);
777
778 out:
779 kfree(tmp);
780 return ret;
781 }
782
783 static const struct gpio_chip msm_gpio_template = {
784 .direction_input = msm_gpio_direction_input,
785 .direction_output = msm_gpio_direction_output,
786 .get_direction = msm_gpio_get_direction,
787 .get = msm_gpio_get,
788 .set = msm_gpio_set,
789 .request = gpiochip_generic_request,
790 .free = gpiochip_generic_free,
791 .dbg_show = msm_gpio_dbg_show,
792 };
793
794 /* For dual-edge interrupts in software, since some hardware has no
795 * such support:
796 *
797 * At appropriate moments, this function may be called to flip the polarity
798 * settings of both-edge irq lines to try and catch the next edge.
799 *
800 * The attempt is considered successful if:
801 * - the status bit goes high, indicating that an edge was caught, or
802 * - the input value of the gpio doesn't change during the attempt.
803 * If the value changes twice during the process, that would cause the first
804 * test to fail but would force the second, as two opposite
805 * transitions would cause a detection no matter the polarity setting.
806 *
807 * The do-loop tries to sledge-hammer closed the timing hole between
808 * the initial value-read and the polarity-write - if the line value changes
809 * during that window, an interrupt is lost, the new polarity setting is
810 * incorrect, and the first success test will fail, causing a retry.
811 *
812 * Algorithm comes from Google's msmgpio driver.
813 */
msm_gpio_update_dual_edge_pos(struct msm_pinctrl * pctrl,const struct msm_pingroup * g,struct irq_data * d)814 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
815 const struct msm_pingroup *g,
816 struct irq_data *d)
817 {
818 int loop_limit = 100;
819 unsigned val, val2, intstat;
820 unsigned pol;
821
822 do {
823 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
824
825 pol = msm_readl_intr_cfg(pctrl, g);
826 pol ^= BIT(g->intr_polarity_bit);
827 msm_writel_intr_cfg(pol, pctrl, g);
828
829 val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
830 intstat = msm_readl_intr_status(pctrl, g);
831 if (intstat || (val == val2))
832 return;
833 } while (loop_limit-- > 0);
834 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
835 val, val2);
836 }
837
msm_gpio_irq_mask(struct irq_data * d)838 static void msm_gpio_irq_mask(struct irq_data *d)
839 {
840 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
841 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
842 const struct msm_pingroup *g;
843 unsigned long flags;
844 u32 val;
845
846 if (d->parent_data)
847 irq_chip_mask_parent(d);
848
849 if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
850 return;
851
852 g = &pctrl->soc->groups[d->hwirq];
853
854 raw_spin_lock_irqsave(&pctrl->lock, flags);
855
856 val = msm_readl_intr_cfg(pctrl, g);
857 /*
858 * There are two bits that control interrupt forwarding to the CPU. The
859 * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
860 * latched into the interrupt status register when the hardware detects
861 * an irq that it's configured for (either edge for edge type or level
862 * for level type irq). The 'non-raw' status enable bit causes the
863 * hardware to assert the summary interrupt to the CPU if the latched
864 * status bit is set. There's a bug though, the edge detection logic
865 * seems to have a problem where toggling the RAW_STATUS_EN bit may
866 * cause the status bit to latch spuriously when there isn't any edge
867 * so we can't touch that bit for edge type irqs and we have to keep
868 * the bit set anyway so that edges are latched while the line is masked.
869 *
870 * To make matters more complicated, leaving the RAW_STATUS_EN bit
871 * enabled all the time causes level interrupts to re-latch into the
872 * status register because the level is still present on the line after
873 * we ack it. We clear the raw status enable bit during mask here and
874 * set the bit on unmask so the interrupt can't latch into the hardware
875 * while it's masked.
876 */
877 if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
878 val &= ~BIT(g->intr_raw_status_bit);
879
880 val &= ~BIT(g->intr_enable_bit);
881 msm_writel_intr_cfg(val, pctrl, g);
882
883 clear_bit(d->hwirq, pctrl->enabled_irqs);
884
885 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
886 }
887
msm_gpio_irq_unmask(struct irq_data * d)888 static void msm_gpio_irq_unmask(struct irq_data *d)
889 {
890 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
891 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
892 const struct msm_pingroup *g;
893 unsigned long flags;
894 u32 val;
895
896 if (d->parent_data)
897 irq_chip_unmask_parent(d);
898
899 if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
900 return;
901
902 g = &pctrl->soc->groups[d->hwirq];
903
904 raw_spin_lock_irqsave(&pctrl->lock, flags);
905
906 val = msm_readl_intr_cfg(pctrl, g);
907 val |= BIT(g->intr_raw_status_bit);
908 val |= BIT(g->intr_enable_bit);
909 msm_writel_intr_cfg(val, pctrl, g);
910
911 set_bit(d->hwirq, pctrl->enabled_irqs);
912
913 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
914 }
915
msm_gpio_irq_enable(struct irq_data * d)916 static void msm_gpio_irq_enable(struct irq_data *d)
917 {
918 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
919 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
920
921 gpiochip_enable_irq(gc, d->hwirq);
922
923 if (d->parent_data)
924 irq_chip_enable_parent(d);
925
926 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
927 msm_gpio_irq_unmask(d);
928 }
929
msm_gpio_irq_disable(struct irq_data * d)930 static void msm_gpio_irq_disable(struct irq_data *d)
931 {
932 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
933 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
934
935 if (d->parent_data)
936 irq_chip_disable_parent(d);
937
938 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
939 msm_gpio_irq_mask(d);
940
941 gpiochip_disable_irq(gc, d->hwirq);
942 }
943
944 /**
945 * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
946 * @d: The irq dta.
947 *
948 * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
949 * normally handled by the parent irqchip. The logic here is slightly
950 * different due to what's easy to do with our parent, but in principle it's
951 * the same.
952 */
msm_gpio_update_dual_edge_parent(struct irq_data * d)953 static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
954 {
955 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
956 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
957 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
958 int loop_limit = 100;
959 unsigned int val;
960 unsigned int type;
961
962 /* Read the value and make a guess about what edge we need to catch */
963 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
964 type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
965
966 do {
967 /* Set the parent to catch the next edge */
968 irq_chip_set_type_parent(d, type);
969
970 /*
971 * Possibly the line changed between when we last read "val"
972 * (and decided what edge we needed) and when set the edge.
973 * If the value didn't change (or changed and then changed
974 * back) then we're done.
975 */
976 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
977 if (type == IRQ_TYPE_EDGE_RISING) {
978 if (!val)
979 return;
980 type = IRQ_TYPE_EDGE_FALLING;
981 } else if (type == IRQ_TYPE_EDGE_FALLING) {
982 if (val)
983 return;
984 type = IRQ_TYPE_EDGE_RISING;
985 }
986 } while (loop_limit-- > 0);
987 dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
988 }
989
msm_gpio_irq_ack(struct irq_data * d)990 static void msm_gpio_irq_ack(struct irq_data *d)
991 {
992 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
993 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
994 const struct msm_pingroup *g;
995 unsigned long flags;
996
997 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
998 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
999 msm_gpio_update_dual_edge_parent(d);
1000 return;
1001 }
1002
1003 g = &pctrl->soc->groups[d->hwirq];
1004
1005 raw_spin_lock_irqsave(&pctrl->lock, flags);
1006
1007 msm_ack_intr_status(pctrl, g);
1008
1009 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1010 msm_gpio_update_dual_edge_pos(pctrl, g, d);
1011
1012 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1013 }
1014
msm_gpio_irq_eoi(struct irq_data * d)1015 static void msm_gpio_irq_eoi(struct irq_data *d)
1016 {
1017 d = d->parent_data;
1018
1019 if (d)
1020 d->chip->irq_eoi(d);
1021 }
1022
msm_gpio_needs_dual_edge_parent_workaround(struct irq_data * d,unsigned int type)1023 static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
1024 unsigned int type)
1025 {
1026 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1027 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1028
1029 return type == IRQ_TYPE_EDGE_BOTH &&
1030 pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
1031 test_bit(d->hwirq, pctrl->skip_wake_irqs);
1032 }
1033
msm_gpio_irq_set_type(struct irq_data * d,unsigned int type)1034 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
1035 {
1036 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1037 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1038 const struct msm_pingroup *g;
1039 u32 intr_target_mask = GENMASK(2, 0);
1040 unsigned long flags;
1041 u32 val, oldval;
1042
1043 if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
1044 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1045 irq_set_handler_locked(d, handle_fasteoi_ack_irq);
1046 msm_gpio_update_dual_edge_parent(d);
1047 return 0;
1048 }
1049
1050 if (d->parent_data)
1051 irq_chip_set_type_parent(d, type);
1052
1053 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
1054 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1055 irq_set_handler_locked(d, handle_fasteoi_irq);
1056 return 0;
1057 }
1058
1059 g = &pctrl->soc->groups[d->hwirq];
1060
1061 raw_spin_lock_irqsave(&pctrl->lock, flags);
1062
1063 /*
1064 * For hw without possibility of detecting both edges
1065 */
1066 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
1067 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1068 else
1069 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1070
1071 /* Route interrupts to application cpu.
1072 * With intr_target_use_scm interrupts are routed to
1073 * application cpu using scm calls.
1074 */
1075 if (g->intr_target_width)
1076 intr_target_mask = GENMASK(g->intr_target_width - 1, 0);
1077
1078 if (pctrl->intr_target_use_scm) {
1079 u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
1080 int ret;
1081
1082 qcom_scm_io_readl(addr, &val);
1083 val &= ~(intr_target_mask << g->intr_target_bit);
1084 val |= g->intr_target_kpss_val << g->intr_target_bit;
1085
1086 ret = qcom_scm_io_writel(addr, val);
1087 if (ret)
1088 dev_err(pctrl->dev,
1089 "Failed routing %lu interrupt to Apps proc",
1090 d->hwirq);
1091 } else {
1092 val = msm_readl_intr_target(pctrl, g);
1093 val &= ~(intr_target_mask << g->intr_target_bit);
1094 val |= g->intr_target_kpss_val << g->intr_target_bit;
1095 msm_writel_intr_target(val, pctrl, g);
1096 }
1097
1098 /* Update configuration for gpio.
1099 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
1100 * internal circuitry of TLMM, toggling the RAW_STATUS
1101 * could cause the INTR_STATUS to be set for EDGE interrupts.
1102 */
1103 val = oldval = msm_readl_intr_cfg(pctrl, g);
1104 val |= BIT(g->intr_raw_status_bit);
1105 if (g->intr_detection_width == 2) {
1106 val &= ~(3 << g->intr_detection_bit);
1107 val &= ~(1 << g->intr_polarity_bit);
1108 switch (type) {
1109 case IRQ_TYPE_EDGE_RISING:
1110 val |= 1 << g->intr_detection_bit;
1111 val |= BIT(g->intr_polarity_bit);
1112 break;
1113 case IRQ_TYPE_EDGE_FALLING:
1114 val |= 2 << g->intr_detection_bit;
1115 val |= BIT(g->intr_polarity_bit);
1116 break;
1117 case IRQ_TYPE_EDGE_BOTH:
1118 val |= 3 << g->intr_detection_bit;
1119 val |= BIT(g->intr_polarity_bit);
1120 break;
1121 case IRQ_TYPE_LEVEL_LOW:
1122 break;
1123 case IRQ_TYPE_LEVEL_HIGH:
1124 val |= BIT(g->intr_polarity_bit);
1125 break;
1126 }
1127 } else if (g->intr_detection_width == 1) {
1128 val &= ~(1 << g->intr_detection_bit);
1129 val &= ~(1 << g->intr_polarity_bit);
1130 switch (type) {
1131 case IRQ_TYPE_EDGE_RISING:
1132 val |= BIT(g->intr_detection_bit);
1133 val |= BIT(g->intr_polarity_bit);
1134 break;
1135 case IRQ_TYPE_EDGE_FALLING:
1136 val |= BIT(g->intr_detection_bit);
1137 break;
1138 case IRQ_TYPE_EDGE_BOTH:
1139 val |= BIT(g->intr_detection_bit);
1140 val |= BIT(g->intr_polarity_bit);
1141 break;
1142 case IRQ_TYPE_LEVEL_LOW:
1143 break;
1144 case IRQ_TYPE_LEVEL_HIGH:
1145 val |= BIT(g->intr_polarity_bit);
1146 break;
1147 }
1148 } else {
1149 BUG();
1150 }
1151 msm_writel_intr_cfg(val, pctrl, g);
1152
1153 /*
1154 * The first time we set RAW_STATUS_EN it could trigger an interrupt.
1155 * Clear the interrupt. This is safe because we have
1156 * IRQCHIP_SET_TYPE_MASKED. When changing the interrupt type, we could
1157 * also still have a non-matching interrupt latched, so clear whenever
1158 * making changes to the interrupt configuration.
1159 */
1160 if (val != oldval)
1161 msm_ack_intr_status(pctrl, g);
1162
1163 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1164 msm_gpio_update_dual_edge_pos(pctrl, g, d);
1165
1166 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1167
1168 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
1169 irq_set_handler_locked(d, handle_level_irq);
1170 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
1171 irq_set_handler_locked(d, handle_edge_irq);
1172
1173 return 0;
1174 }
1175
msm_gpio_irq_set_wake(struct irq_data * d,unsigned int on)1176 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
1177 {
1178 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1179 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1180
1181 /*
1182 * While they may not wake up when the TLMM is powered off,
1183 * some GPIOs would like to wakeup the system from suspend
1184 * when TLMM is powered on. To allow that, enable the GPIO
1185 * summary line to be wakeup capable at GIC.
1186 */
1187 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1188 return irq_chip_set_wake_parent(d, on);
1189
1190 return irq_set_irq_wake(pctrl->irq, on);
1191 }
1192
msm_gpio_irq_reqres(struct irq_data * d)1193 static int msm_gpio_irq_reqres(struct irq_data *d)
1194 {
1195 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1196 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1197 int ret;
1198
1199 if (!try_module_get(gc->owner))
1200 return -ENODEV;
1201
1202 ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
1203 if (ret)
1204 goto out;
1205 msm_gpio_direction_input(gc, d->hwirq);
1206
1207 if (gpiochip_lock_as_irq(gc, d->hwirq)) {
1208 dev_err(gc->parent,
1209 "unable to lock HW IRQ %lu for IRQ\n",
1210 d->hwirq);
1211 ret = -EINVAL;
1212 goto out;
1213 }
1214
1215 /*
1216 * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
1217 * only works if disable is not lazy since we only clear any bogus
1218 * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
1219 */
1220 irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
1221
1222 return 0;
1223 out:
1224 module_put(gc->owner);
1225 return ret;
1226 }
1227
msm_gpio_irq_relres(struct irq_data * d)1228 static void msm_gpio_irq_relres(struct irq_data *d)
1229 {
1230 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1231
1232 gpiochip_unlock_as_irq(gc, d->hwirq);
1233 module_put(gc->owner);
1234 }
1235
msm_gpio_irq_set_affinity(struct irq_data * d,const struct cpumask * dest,bool force)1236 static int msm_gpio_irq_set_affinity(struct irq_data *d,
1237 const struct cpumask *dest, bool force)
1238 {
1239 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1240 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1241
1242 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1243 return irq_chip_set_affinity_parent(d, dest, force);
1244
1245 return -EINVAL;
1246 }
1247
msm_gpio_irq_set_vcpu_affinity(struct irq_data * d,void * vcpu_info)1248 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1249 {
1250 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1251 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1252
1253 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1254 return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
1255
1256 return -EINVAL;
1257 }
1258
msm_gpio_irq_handler(struct irq_desc * desc)1259 static void msm_gpio_irq_handler(struct irq_desc *desc)
1260 {
1261 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1262 const struct msm_pingroup *g;
1263 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1264 struct irq_chip *chip = irq_desc_get_chip(desc);
1265 int handled = 0;
1266 u32 val;
1267 int i;
1268
1269 chained_irq_enter(chip, desc);
1270
1271 /*
1272 * Each pin has it's own IRQ status register, so use
1273 * enabled_irq bitmap to limit the number of reads.
1274 */
1275 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1276 g = &pctrl->soc->groups[i];
1277 val = msm_readl_intr_status(pctrl, g);
1278 if (val & BIT(g->intr_status_bit)) {
1279 generic_handle_domain_irq(gc->irq.domain, i);
1280 handled++;
1281 }
1282 }
1283
1284 /* No interrupts were flagged */
1285 if (handled == 0)
1286 handle_bad_irq(desc);
1287
1288 chained_irq_exit(chip, desc);
1289 }
1290
msm_gpio_wakeirq(struct gpio_chip * gc,unsigned int child,unsigned int child_type,unsigned int * parent,unsigned int * parent_type)1291 static int msm_gpio_wakeirq(struct gpio_chip *gc,
1292 unsigned int child,
1293 unsigned int child_type,
1294 unsigned int *parent,
1295 unsigned int *parent_type)
1296 {
1297 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1298 const struct msm_gpio_wakeirq_map *map;
1299 int i;
1300
1301 *parent = GPIO_NO_WAKE_IRQ;
1302 *parent_type = IRQ_TYPE_EDGE_RISING;
1303
1304 for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
1305 map = &pctrl->soc->wakeirq_map[i];
1306 if (map->gpio == child) {
1307 *parent = map->wakeirq;
1308 break;
1309 }
1310 }
1311
1312 return 0;
1313 }
1314
msm_gpio_needs_valid_mask(struct msm_pinctrl * pctrl)1315 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
1316 {
1317 if (pctrl->soc->reserved_gpios)
1318 return true;
1319
1320 return device_property_count_u16(pctrl->dev, "gpios") > 0;
1321 }
1322
1323 static const struct irq_chip msm_gpio_irq_chip = {
1324 .name = "msmgpio",
1325 .irq_enable = msm_gpio_irq_enable,
1326 .irq_disable = msm_gpio_irq_disable,
1327 .irq_mask = msm_gpio_irq_mask,
1328 .irq_unmask = msm_gpio_irq_unmask,
1329 .irq_ack = msm_gpio_irq_ack,
1330 .irq_eoi = msm_gpio_irq_eoi,
1331 .irq_set_type = msm_gpio_irq_set_type,
1332 .irq_set_wake = msm_gpio_irq_set_wake,
1333 .irq_request_resources = msm_gpio_irq_reqres,
1334 .irq_release_resources = msm_gpio_irq_relres,
1335 .irq_set_affinity = msm_gpio_irq_set_affinity,
1336 .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity,
1337 .flags = (IRQCHIP_MASK_ON_SUSPEND |
1338 IRQCHIP_SET_TYPE_MASKED |
1339 IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
1340 IRQCHIP_IMMUTABLE),
1341 };
1342
msm_gpio_init(struct msm_pinctrl * pctrl)1343 static int msm_gpio_init(struct msm_pinctrl *pctrl)
1344 {
1345 struct gpio_chip *chip;
1346 struct gpio_irq_chip *girq;
1347 int i, ret;
1348 unsigned gpio, ngpio = pctrl->soc->ngpios;
1349 struct device_node *np;
1350 bool skip;
1351
1352 if (WARN_ON(ngpio > MAX_NR_GPIO))
1353 return -EINVAL;
1354
1355 chip = &pctrl->chip;
1356 chip->base = -1;
1357 chip->ngpio = ngpio;
1358 chip->label = dev_name(pctrl->dev);
1359 chip->parent = pctrl->dev;
1360 chip->owner = THIS_MODULE;
1361 if (msm_gpio_needs_valid_mask(pctrl))
1362 chip->init_valid_mask = msm_gpio_init_valid_mask;
1363
1364 np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
1365 if (np) {
1366 chip->irq.parent_domain = irq_find_matching_host(np,
1367 DOMAIN_BUS_WAKEUP);
1368 of_node_put(np);
1369 if (!chip->irq.parent_domain)
1370 return -EPROBE_DEFER;
1371 chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
1372 /*
1373 * Let's skip handling the GPIOs, if the parent irqchip
1374 * is handling the direct connect IRQ of the GPIO.
1375 */
1376 skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
1377 for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
1378 gpio = pctrl->soc->wakeirq_map[i].gpio;
1379 set_bit(gpio, pctrl->skip_wake_irqs);
1380 }
1381 }
1382
1383 girq = &chip->irq;
1384 gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
1385 girq->parent_handler = msm_gpio_irq_handler;
1386 girq->fwnode = dev_fwnode(pctrl->dev);
1387 girq->num_parents = 1;
1388 girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
1389 GFP_KERNEL);
1390 if (!girq->parents)
1391 return -ENOMEM;
1392 girq->default_type = IRQ_TYPE_NONE;
1393 girq->handler = handle_bad_irq;
1394 girq->parents[0] = pctrl->irq;
1395
1396 ret = gpiochip_add_data(&pctrl->chip, pctrl);
1397 if (ret) {
1398 dev_err(pctrl->dev, "Failed register gpiochip\n");
1399 return ret;
1400 }
1401
1402 /*
1403 * For DeviceTree-supported systems, the gpio core checks the
1404 * pinctrl's device node for the "gpio-ranges" property.
1405 * If it is present, it takes care of adding the pin ranges
1406 * for the driver. In this case the driver can skip ahead.
1407 *
1408 * In order to remain compatible with older, existing DeviceTree
1409 * files which don't set the "gpio-ranges" property or systems that
1410 * utilize ACPI the driver has to call gpiochip_add_pin_range().
1411 */
1412 if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
1413 ret = gpiochip_add_pin_range(&pctrl->chip,
1414 dev_name(pctrl->dev), 0, 0, chip->ngpio);
1415 if (ret) {
1416 dev_err(pctrl->dev, "Failed to add pin range\n");
1417 gpiochip_remove(&pctrl->chip);
1418 return ret;
1419 }
1420 }
1421
1422 return 0;
1423 }
1424
msm_ps_hold_restart(struct sys_off_data * data)1425 static int msm_ps_hold_restart(struct sys_off_data *data)
1426 {
1427 struct msm_pinctrl *pctrl = data->cb_data;
1428
1429 writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
1430 mdelay(1000);
1431 return NOTIFY_DONE;
1432 }
1433
1434 static struct msm_pinctrl *poweroff_pctrl;
1435
msm_ps_hold_poweroff(void)1436 static void msm_ps_hold_poweroff(void)
1437 {
1438 struct sys_off_data data = {
1439 .cb_data = poweroff_pctrl,
1440 };
1441
1442 msm_ps_hold_restart(&data);
1443 }
1444
msm_pinctrl_setup_pm_reset(struct msm_pinctrl * pctrl)1445 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
1446 {
1447 int i;
1448 const struct pinfunction *func = pctrl->soc->functions;
1449
1450 for (i = 0; i < pctrl->soc->nfunctions; i++)
1451 if (!strcmp(func[i].name, "ps_hold")) {
1452 if (devm_register_sys_off_handler(pctrl->dev,
1453 SYS_OFF_MODE_RESTART,
1454 128,
1455 msm_ps_hold_restart,
1456 pctrl))
1457 dev_err(pctrl->dev,
1458 "failed to setup restart handler.\n");
1459 poweroff_pctrl = pctrl;
1460 pm_power_off = msm_ps_hold_poweroff;
1461 break;
1462 }
1463 }
1464
msm_pinctrl_suspend(struct device * dev)1465 static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
1466 {
1467 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1468
1469 return pinctrl_force_sleep(pctrl->pctrl);
1470 }
1471
msm_pinctrl_resume(struct device * dev)1472 static __maybe_unused int msm_pinctrl_resume(struct device *dev)
1473 {
1474 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1475
1476 return pinctrl_force_default(pctrl->pctrl);
1477 }
1478
1479 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
1480 msm_pinctrl_resume);
1481
1482 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
1483
msm_pinctrl_probe(struct platform_device * pdev,const struct msm_pinctrl_soc_data * soc_data)1484 int msm_pinctrl_probe(struct platform_device *pdev,
1485 const struct msm_pinctrl_soc_data *soc_data)
1486 {
1487 struct msm_pinctrl *pctrl;
1488 struct resource *res;
1489 int ret;
1490 int i;
1491
1492 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1493 if (!pctrl)
1494 return -ENOMEM;
1495
1496 pctrl->dev = &pdev->dev;
1497 pctrl->soc = soc_data;
1498 pctrl->chip = msm_gpio_template;
1499 pctrl->intr_target_use_scm = of_device_is_compatible(
1500 pctrl->dev->of_node,
1501 "qcom,ipq8064-pinctrl");
1502
1503 raw_spin_lock_init(&pctrl->lock);
1504
1505 if (soc_data->tiles) {
1506 for (i = 0; i < soc_data->ntiles; i++) {
1507 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1508 soc_data->tiles[i]);
1509 pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1510 if (IS_ERR(pctrl->regs[i]))
1511 return PTR_ERR(pctrl->regs[i]);
1512 }
1513 } else {
1514 pctrl->regs[0] = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1515 if (IS_ERR(pctrl->regs[0]))
1516 return PTR_ERR(pctrl->regs[0]);
1517
1518 pctrl->phys_base[0] = res->start;
1519 }
1520
1521 msm_pinctrl_setup_pm_reset(pctrl);
1522
1523 pctrl->irq = platform_get_irq(pdev, 0);
1524 if (pctrl->irq < 0)
1525 return pctrl->irq;
1526
1527 pctrl->desc.owner = THIS_MODULE;
1528 pctrl->desc.pctlops = &msm_pinctrl_ops;
1529 pctrl->desc.pmxops = &msm_pinmux_ops;
1530 pctrl->desc.confops = &msm_pinconf_ops;
1531 pctrl->desc.name = dev_name(&pdev->dev);
1532 pctrl->desc.pins = pctrl->soc->pins;
1533 pctrl->desc.npins = pctrl->soc->npins;
1534
1535 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
1536 if (IS_ERR(pctrl->pctrl)) {
1537 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1538 return PTR_ERR(pctrl->pctrl);
1539 }
1540
1541 ret = msm_gpio_init(pctrl);
1542 if (ret)
1543 return ret;
1544
1545 platform_set_drvdata(pdev, pctrl);
1546
1547 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1548
1549 return 0;
1550 }
1551 EXPORT_SYMBOL(msm_pinctrl_probe);
1552
msm_pinctrl_remove(struct platform_device * pdev)1553 void msm_pinctrl_remove(struct platform_device *pdev)
1554 {
1555 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1556
1557 gpiochip_remove(&pctrl->chip);
1558 }
1559 EXPORT_SYMBOL(msm_pinctrl_remove);
1560
1561 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver");
1562 MODULE_LICENSE("GPL v2");
1563