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_init_valid_mask(struct gpio_chip * gc,unsigned long * valid_mask,unsigned int ngpios)1034 static void msm_gpio_irq_init_valid_mask(struct gpio_chip *gc,
1035 unsigned long *valid_mask,
1036 unsigned int ngpios)
1037 {
1038 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1039 const struct msm_pingroup *g;
1040 int i;
1041
1042 bitmap_fill(valid_mask, ngpios);
1043
1044 for (i = 0; i < ngpios; i++) {
1045 g = &pctrl->soc->groups[i];
1046
1047 if (g->intr_detection_width != 1 &&
1048 g->intr_detection_width != 2)
1049 clear_bit(i, valid_mask);
1050 }
1051 }
1052
msm_gpio_irq_set_type(struct irq_data * d,unsigned int type)1053 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
1054 {
1055 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1056 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1057 const struct msm_pingroup *g;
1058 u32 intr_target_mask = GENMASK(2, 0);
1059 unsigned long flags;
1060 u32 val, oldval;
1061
1062 if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
1063 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1064 irq_set_handler_locked(d, handle_fasteoi_ack_irq);
1065 msm_gpio_update_dual_edge_parent(d);
1066 return 0;
1067 }
1068
1069 if (d->parent_data)
1070 irq_chip_set_type_parent(d, type);
1071
1072 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
1073 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1074 irq_set_handler_locked(d, handle_fasteoi_irq);
1075 return 0;
1076 }
1077
1078 g = &pctrl->soc->groups[d->hwirq];
1079
1080 raw_spin_lock_irqsave(&pctrl->lock, flags);
1081
1082 /*
1083 * For hw without possibility of detecting both edges
1084 */
1085 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
1086 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1087 else
1088 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1089
1090 /* Route interrupts to application cpu.
1091 * With intr_target_use_scm interrupts are routed to
1092 * application cpu using scm calls.
1093 */
1094 if (g->intr_target_width)
1095 intr_target_mask = GENMASK(g->intr_target_width - 1, 0);
1096
1097 if (pctrl->intr_target_use_scm) {
1098 u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
1099 int ret;
1100
1101 qcom_scm_io_readl(addr, &val);
1102 val &= ~(intr_target_mask << g->intr_target_bit);
1103 val |= g->intr_target_kpss_val << g->intr_target_bit;
1104
1105 ret = qcom_scm_io_writel(addr, val);
1106 if (ret)
1107 dev_err(pctrl->dev,
1108 "Failed routing %lu interrupt to Apps proc",
1109 d->hwirq);
1110 } else {
1111 val = msm_readl_intr_target(pctrl, g);
1112 val &= ~(intr_target_mask << g->intr_target_bit);
1113 val |= g->intr_target_kpss_val << g->intr_target_bit;
1114 msm_writel_intr_target(val, pctrl, g);
1115 }
1116
1117 /* Update configuration for gpio.
1118 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
1119 * internal circuitry of TLMM, toggling the RAW_STATUS
1120 * could cause the INTR_STATUS to be set for EDGE interrupts.
1121 */
1122 val = oldval = msm_readl_intr_cfg(pctrl, g);
1123 val |= BIT(g->intr_raw_status_bit);
1124 if (g->intr_detection_width == 2) {
1125 val &= ~(3 << g->intr_detection_bit);
1126 val &= ~(1 << g->intr_polarity_bit);
1127 switch (type) {
1128 case IRQ_TYPE_EDGE_RISING:
1129 val |= 1 << g->intr_detection_bit;
1130 val |= BIT(g->intr_polarity_bit);
1131 break;
1132 case IRQ_TYPE_EDGE_FALLING:
1133 val |= 2 << g->intr_detection_bit;
1134 val |= BIT(g->intr_polarity_bit);
1135 break;
1136 case IRQ_TYPE_EDGE_BOTH:
1137 val |= 3 << g->intr_detection_bit;
1138 val |= BIT(g->intr_polarity_bit);
1139 break;
1140 case IRQ_TYPE_LEVEL_LOW:
1141 break;
1142 case IRQ_TYPE_LEVEL_HIGH:
1143 val |= BIT(g->intr_polarity_bit);
1144 break;
1145 }
1146 } else if (g->intr_detection_width == 1) {
1147 val &= ~(1 << g->intr_detection_bit);
1148 val &= ~(1 << g->intr_polarity_bit);
1149 switch (type) {
1150 case IRQ_TYPE_EDGE_RISING:
1151 val |= BIT(g->intr_detection_bit);
1152 val |= BIT(g->intr_polarity_bit);
1153 break;
1154 case IRQ_TYPE_EDGE_FALLING:
1155 val |= BIT(g->intr_detection_bit);
1156 break;
1157 case IRQ_TYPE_EDGE_BOTH:
1158 val |= BIT(g->intr_detection_bit);
1159 val |= BIT(g->intr_polarity_bit);
1160 break;
1161 case IRQ_TYPE_LEVEL_LOW:
1162 break;
1163 case IRQ_TYPE_LEVEL_HIGH:
1164 val |= BIT(g->intr_polarity_bit);
1165 break;
1166 }
1167 } else {
1168 BUG();
1169 }
1170 msm_writel_intr_cfg(val, pctrl, g);
1171
1172 /*
1173 * The first time we set RAW_STATUS_EN it could trigger an interrupt.
1174 * Clear the interrupt. This is safe because we have
1175 * IRQCHIP_SET_TYPE_MASKED. When changing the interrupt type, we could
1176 * also still have a non-matching interrupt latched, so clear whenever
1177 * making changes to the interrupt configuration.
1178 */
1179 if (val != oldval)
1180 msm_ack_intr_status(pctrl, g);
1181
1182 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1183 msm_gpio_update_dual_edge_pos(pctrl, g, d);
1184
1185 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1186
1187 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
1188 irq_set_handler_locked(d, handle_level_irq);
1189 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
1190 irq_set_handler_locked(d, handle_edge_irq);
1191
1192 return 0;
1193 }
1194
msm_gpio_irq_set_wake(struct irq_data * d,unsigned int on)1195 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
1196 {
1197 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1198 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1199
1200 /*
1201 * While they may not wake up when the TLMM is powered off,
1202 * some GPIOs would like to wakeup the system from suspend
1203 * when TLMM is powered on. To allow that, enable the GPIO
1204 * summary line to be wakeup capable at GIC.
1205 */
1206 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1207 return irq_chip_set_wake_parent(d, on);
1208
1209 return irq_set_irq_wake(pctrl->irq, on);
1210 }
1211
msm_gpio_irq_reqres(struct irq_data * d)1212 static int msm_gpio_irq_reqres(struct irq_data *d)
1213 {
1214 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1215 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1216 int ret;
1217
1218 if (!try_module_get(gc->owner))
1219 return -ENODEV;
1220
1221 ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
1222 if (ret)
1223 goto out;
1224 msm_gpio_direction_input(gc, d->hwirq);
1225
1226 if (gpiochip_lock_as_irq(gc, d->hwirq)) {
1227 dev_err(gc->parent,
1228 "unable to lock HW IRQ %lu for IRQ\n",
1229 d->hwirq);
1230 ret = -EINVAL;
1231 goto out;
1232 }
1233
1234 /*
1235 * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
1236 * only works if disable is not lazy since we only clear any bogus
1237 * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
1238 */
1239 irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
1240
1241 return 0;
1242 out:
1243 module_put(gc->owner);
1244 return ret;
1245 }
1246
msm_gpio_irq_relres(struct irq_data * d)1247 static void msm_gpio_irq_relres(struct irq_data *d)
1248 {
1249 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1250
1251 gpiochip_unlock_as_irq(gc, d->hwirq);
1252 module_put(gc->owner);
1253 }
1254
msm_gpio_irq_set_affinity(struct irq_data * d,const struct cpumask * dest,bool force)1255 static int msm_gpio_irq_set_affinity(struct irq_data *d,
1256 const struct cpumask *dest, bool force)
1257 {
1258 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1259 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1260
1261 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1262 return irq_chip_set_affinity_parent(d, dest, force);
1263
1264 return -EINVAL;
1265 }
1266
msm_gpio_irq_set_vcpu_affinity(struct irq_data * d,void * vcpu_info)1267 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1268 {
1269 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1270 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1271
1272 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1273 return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
1274
1275 return -EINVAL;
1276 }
1277
msm_gpio_irq_handler(struct irq_desc * desc)1278 static void msm_gpio_irq_handler(struct irq_desc *desc)
1279 {
1280 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1281 const struct msm_pingroup *g;
1282 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1283 struct irq_chip *chip = irq_desc_get_chip(desc);
1284 int handled = 0;
1285 u32 val;
1286 int i;
1287
1288 chained_irq_enter(chip, desc);
1289
1290 /*
1291 * Each pin has it's own IRQ status register, so use
1292 * enabled_irq bitmap to limit the number of reads.
1293 */
1294 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1295 g = &pctrl->soc->groups[i];
1296 val = msm_readl_intr_status(pctrl, g);
1297 if (val & BIT(g->intr_status_bit)) {
1298 generic_handle_domain_irq(gc->irq.domain, i);
1299 handled++;
1300 }
1301 }
1302
1303 /* No interrupts were flagged */
1304 if (handled == 0)
1305 handle_bad_irq(desc);
1306
1307 chained_irq_exit(chip, desc);
1308 }
1309
msm_gpio_wakeirq(struct gpio_chip * gc,unsigned int child,unsigned int child_type,unsigned int * parent,unsigned int * parent_type)1310 static int msm_gpio_wakeirq(struct gpio_chip *gc,
1311 unsigned int child,
1312 unsigned int child_type,
1313 unsigned int *parent,
1314 unsigned int *parent_type)
1315 {
1316 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1317 const struct msm_gpio_wakeirq_map *map;
1318 int i;
1319
1320 *parent = GPIO_NO_WAKE_IRQ;
1321 *parent_type = IRQ_TYPE_EDGE_RISING;
1322
1323 for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
1324 map = &pctrl->soc->wakeirq_map[i];
1325 if (map->gpio == child) {
1326 *parent = map->wakeirq;
1327 break;
1328 }
1329 }
1330
1331 return 0;
1332 }
1333
msm_gpio_needs_valid_mask(struct msm_pinctrl * pctrl)1334 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
1335 {
1336 if (pctrl->soc->reserved_gpios)
1337 return true;
1338
1339 return device_property_count_u16(pctrl->dev, "gpios") > 0;
1340 }
1341
1342 static const struct irq_chip msm_gpio_irq_chip = {
1343 .name = "msmgpio",
1344 .irq_enable = msm_gpio_irq_enable,
1345 .irq_disable = msm_gpio_irq_disable,
1346 .irq_mask = msm_gpio_irq_mask,
1347 .irq_unmask = msm_gpio_irq_unmask,
1348 .irq_ack = msm_gpio_irq_ack,
1349 .irq_eoi = msm_gpio_irq_eoi,
1350 .irq_set_type = msm_gpio_irq_set_type,
1351 .irq_set_wake = msm_gpio_irq_set_wake,
1352 .irq_request_resources = msm_gpio_irq_reqres,
1353 .irq_release_resources = msm_gpio_irq_relres,
1354 .irq_set_affinity = msm_gpio_irq_set_affinity,
1355 .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity,
1356 .flags = (IRQCHIP_MASK_ON_SUSPEND |
1357 IRQCHIP_SET_TYPE_MASKED |
1358 IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
1359 IRQCHIP_IMMUTABLE),
1360 };
1361
msm_gpio_init(struct msm_pinctrl * pctrl)1362 static int msm_gpio_init(struct msm_pinctrl *pctrl)
1363 {
1364 struct gpio_chip *chip;
1365 struct gpio_irq_chip *girq;
1366 int i, ret;
1367 unsigned gpio, ngpio = pctrl->soc->ngpios;
1368 struct device_node *np;
1369 bool skip;
1370
1371 if (WARN_ON(ngpio > MAX_NR_GPIO))
1372 return -EINVAL;
1373
1374 chip = &pctrl->chip;
1375 chip->base = -1;
1376 chip->ngpio = ngpio;
1377 chip->label = dev_name(pctrl->dev);
1378 chip->parent = pctrl->dev;
1379 chip->owner = THIS_MODULE;
1380 if (msm_gpio_needs_valid_mask(pctrl))
1381 chip->init_valid_mask = msm_gpio_init_valid_mask;
1382
1383 np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
1384 if (np) {
1385 chip->irq.parent_domain = irq_find_matching_host(np,
1386 DOMAIN_BUS_WAKEUP);
1387 of_node_put(np);
1388 if (!chip->irq.parent_domain)
1389 return -EPROBE_DEFER;
1390 chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
1391 /*
1392 * Let's skip handling the GPIOs, if the parent irqchip
1393 * is handling the direct connect IRQ of the GPIO.
1394 */
1395 skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
1396 for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
1397 gpio = pctrl->soc->wakeirq_map[i].gpio;
1398 set_bit(gpio, pctrl->skip_wake_irqs);
1399 }
1400 }
1401
1402 girq = &chip->irq;
1403 gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
1404 girq->parent_handler = msm_gpio_irq_handler;
1405 girq->fwnode = dev_fwnode(pctrl->dev);
1406 girq->num_parents = 1;
1407 girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
1408 GFP_KERNEL);
1409 if (!girq->parents)
1410 return -ENOMEM;
1411 girq->default_type = IRQ_TYPE_NONE;
1412 girq->handler = handle_bad_irq;
1413 girq->parents[0] = pctrl->irq;
1414 girq->init_valid_mask = msm_gpio_irq_init_valid_mask;
1415
1416 ret = gpiochip_add_data(&pctrl->chip, pctrl);
1417 if (ret) {
1418 dev_err(pctrl->dev, "Failed register gpiochip\n");
1419 return ret;
1420 }
1421
1422 /*
1423 * For DeviceTree-supported systems, the gpio core checks the
1424 * pinctrl's device node for the "gpio-ranges" property.
1425 * If it is present, it takes care of adding the pin ranges
1426 * for the driver. In this case the driver can skip ahead.
1427 *
1428 * In order to remain compatible with older, existing DeviceTree
1429 * files which don't set the "gpio-ranges" property or systems that
1430 * utilize ACPI the driver has to call gpiochip_add_pin_range().
1431 */
1432 if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
1433 ret = gpiochip_add_pin_range(&pctrl->chip,
1434 dev_name(pctrl->dev), 0, 0, chip->ngpio);
1435 if (ret) {
1436 dev_err(pctrl->dev, "Failed to add pin range\n");
1437 gpiochip_remove(&pctrl->chip);
1438 return ret;
1439 }
1440 }
1441
1442 return 0;
1443 }
1444
msm_ps_hold_restart(struct sys_off_data * data)1445 static int msm_ps_hold_restart(struct sys_off_data *data)
1446 {
1447 struct msm_pinctrl *pctrl = data->cb_data;
1448
1449 writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
1450 mdelay(1000);
1451 return NOTIFY_DONE;
1452 }
1453
1454 static struct msm_pinctrl *poweroff_pctrl;
1455
msm_ps_hold_poweroff(void)1456 static void msm_ps_hold_poweroff(void)
1457 {
1458 struct sys_off_data data = {
1459 .cb_data = poweroff_pctrl,
1460 };
1461
1462 msm_ps_hold_restart(&data);
1463 }
1464
msm_pinctrl_setup_pm_reset(struct msm_pinctrl * pctrl)1465 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
1466 {
1467 int i;
1468 const struct pinfunction *func = pctrl->soc->functions;
1469
1470 for (i = 0; i < pctrl->soc->nfunctions; i++)
1471 if (!strcmp(func[i].name, "ps_hold")) {
1472 if (devm_register_sys_off_handler(pctrl->dev,
1473 SYS_OFF_MODE_RESTART,
1474 128,
1475 msm_ps_hold_restart,
1476 pctrl))
1477 dev_err(pctrl->dev,
1478 "failed to setup restart handler.\n");
1479 poweroff_pctrl = pctrl;
1480 pm_power_off = msm_ps_hold_poweroff;
1481 break;
1482 }
1483 }
1484
msm_pinctrl_suspend(struct device * dev)1485 static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
1486 {
1487 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1488
1489 return pinctrl_force_sleep(pctrl->pctrl);
1490 }
1491
msm_pinctrl_resume(struct device * dev)1492 static __maybe_unused int msm_pinctrl_resume(struct device *dev)
1493 {
1494 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1495
1496 return pinctrl_force_default(pctrl->pctrl);
1497 }
1498
1499 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
1500 msm_pinctrl_resume);
1501
1502 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
1503
msm_pinctrl_probe(struct platform_device * pdev,const struct msm_pinctrl_soc_data * soc_data)1504 int msm_pinctrl_probe(struct platform_device *pdev,
1505 const struct msm_pinctrl_soc_data *soc_data)
1506 {
1507 struct msm_pinctrl *pctrl;
1508 struct resource *res;
1509 int ret;
1510 int i;
1511
1512 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1513 if (!pctrl)
1514 return -ENOMEM;
1515
1516 pctrl->dev = &pdev->dev;
1517 pctrl->soc = soc_data;
1518 pctrl->chip = msm_gpio_template;
1519 pctrl->intr_target_use_scm = of_device_is_compatible(
1520 pctrl->dev->of_node,
1521 "qcom,ipq8064-pinctrl");
1522
1523 raw_spin_lock_init(&pctrl->lock);
1524
1525 if (soc_data->tiles) {
1526 for (i = 0; i < soc_data->ntiles; i++) {
1527 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1528 soc_data->tiles[i]);
1529 pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1530 if (IS_ERR(pctrl->regs[i]))
1531 return PTR_ERR(pctrl->regs[i]);
1532 }
1533 } else {
1534 pctrl->regs[0] = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1535 if (IS_ERR(pctrl->regs[0]))
1536 return PTR_ERR(pctrl->regs[0]);
1537
1538 pctrl->phys_base[0] = res->start;
1539 }
1540
1541 msm_pinctrl_setup_pm_reset(pctrl);
1542
1543 pctrl->irq = platform_get_irq(pdev, 0);
1544 if (pctrl->irq < 0)
1545 return pctrl->irq;
1546
1547 pctrl->desc.owner = THIS_MODULE;
1548 pctrl->desc.pctlops = &msm_pinctrl_ops;
1549 pctrl->desc.pmxops = &msm_pinmux_ops;
1550 pctrl->desc.confops = &msm_pinconf_ops;
1551 pctrl->desc.name = dev_name(&pdev->dev);
1552 pctrl->desc.pins = pctrl->soc->pins;
1553 pctrl->desc.npins = pctrl->soc->npins;
1554
1555 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
1556 if (IS_ERR(pctrl->pctrl)) {
1557 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1558 return PTR_ERR(pctrl->pctrl);
1559 }
1560
1561 ret = msm_gpio_init(pctrl);
1562 if (ret)
1563 return ret;
1564
1565 platform_set_drvdata(pdev, pctrl);
1566
1567 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1568
1569 return 0;
1570 }
1571 EXPORT_SYMBOL(msm_pinctrl_probe);
1572
msm_pinctrl_remove(struct platform_device * pdev)1573 void msm_pinctrl_remove(struct platform_device *pdev)
1574 {
1575 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1576
1577 gpiochip_remove(&pctrl->chip);
1578 }
1579 EXPORT_SYMBOL(msm_pinctrl_remove);
1580
1581 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver");
1582 MODULE_LICENSE("GPL v2");
1583