hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
....@@ -64,17 +64,16 @@
6464 * @gc: GPIO chip
6565 * @pctl: pointer to pinctrl_dev
6666 * @pctldesc: pinctrl descriptor
67
- * @irq_domain: pointer to irq domain
6867 * @lock: lock to protect access to I/O registers
6968 */
7069 struct nsp_gpio {
7170 struct device *dev;
7271 void __iomem *base;
7372 void __iomem *io_ctrl;
73
+ struct irq_chip irqchip;
7474 struct gpio_chip gc;
7575 struct pinctrl_dev *pctl;
7676 struct pinctrl_desc pctldesc;
77
- struct irq_domain *irq_domain;
7877 raw_spinlock_t lock;
7978 };
8079
....@@ -136,8 +135,8 @@
136135
137136 static irqreturn_t nsp_gpio_irq_handler(int irq, void *data)
138137 {
139
- struct nsp_gpio *chip = (struct nsp_gpio *)data;
140
- struct gpio_chip gc = chip->gc;
138
+ struct gpio_chip *gc = (struct gpio_chip *)data;
139
+ struct nsp_gpio *chip = gpiochip_get_data(gc);
141140 int bit;
142141 unsigned long int_bits = 0;
143142 u32 int_status;
....@@ -155,15 +154,9 @@
155154 level &= readl(chip->base + NSP_GPIO_INT_MASK);
156155 int_bits = level | event;
157156
158
- for_each_set_bit(bit, &int_bits, gc.ngpio) {
159
- /*
160
- * Clear the interrupt before invoking the
161
- * handler, so we do not leave any window
162
- */
163
- writel(BIT(bit), chip->base + NSP_GPIO_EVENT);
157
+ for_each_set_bit(bit, &int_bits, gc->ngpio)
164158 generic_handle_irq(
165
- irq_linear_revmap(chip->irq_domain, bit));
166
- }
159
+ irq_linear_revmap(gc->irq.domain, bit));
167160 }
168161
169162 return int_bits ? IRQ_HANDLED : IRQ_NONE;
....@@ -171,14 +164,15 @@
171164
172165 static void nsp_gpio_irq_ack(struct irq_data *d)
173166 {
174
- struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
167
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
168
+ struct nsp_gpio *chip = gpiochip_get_data(gc);
175169 unsigned gpio = d->hwirq;
176170 u32 val = BIT(gpio);
177171 u32 trigger_type;
178172
179173 trigger_type = irq_get_trigger_type(d->irq);
180174 if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
181
- nsp_set_bit(chip, REG, NSP_GPIO_EVENT, gpio, val);
175
+ writel(val, chip->base + NSP_GPIO_EVENT);
182176 }
183177
184178 /*
....@@ -189,7 +183,8 @@
189183 */
190184 static void nsp_gpio_irq_set_mask(struct irq_data *d, bool unmask)
191185 {
192
- struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
186
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
187
+ struct nsp_gpio *chip = gpiochip_get_data(gc);
193188 unsigned gpio = d->hwirq;
194189 u32 trigger_type;
195190
....@@ -202,7 +197,8 @@
202197
203198 static void nsp_gpio_irq_mask(struct irq_data *d)
204199 {
205
- struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
200
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
201
+ struct nsp_gpio *chip = gpiochip_get_data(gc);
206202 unsigned long flags;
207203
208204 raw_spin_lock_irqsave(&chip->lock, flags);
....@@ -212,7 +208,8 @@
212208
213209 static void nsp_gpio_irq_unmask(struct irq_data *d)
214210 {
215
- struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
211
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
212
+ struct nsp_gpio *chip = gpiochip_get_data(gc);
216213 unsigned long flags;
217214
218215 raw_spin_lock_irqsave(&chip->lock, flags);
....@@ -222,7 +219,8 @@
222219
223220 static int nsp_gpio_irq_set_type(struct irq_data *d, unsigned int type)
224221 {
225
- struct nsp_gpio *chip = irq_data_get_irq_chip_data(d);
222
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
223
+ struct nsp_gpio *chip = gpiochip_get_data(gc);
226224 unsigned gpio = d->hwirq;
227225 bool level_low;
228226 bool falling;
....@@ -258,22 +256,18 @@
258256
259257 nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio, falling);
260258 nsp_set_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio, level_low);
259
+
260
+ if (type & IRQ_TYPE_EDGE_BOTH)
261
+ irq_set_handler_locked(d, handle_edge_irq);
262
+ else
263
+ irq_set_handler_locked(d, handle_level_irq);
264
+
261265 raw_spin_unlock_irqrestore(&chip->lock, flags);
262266
263267 dev_dbg(chip->dev, "gpio:%u level_low:%s falling:%s\n", gpio,
264268 level_low ? "true" : "false", falling ? "true" : "false");
265269 return 0;
266270 }
267
-
268
-static struct irq_chip nsp_gpio_irq_chip = {
269
- .name = "gpio-a",
270
- .irq_enable = nsp_gpio_irq_unmask,
271
- .irq_disable = nsp_gpio_irq_mask,
272
- .irq_ack = nsp_gpio_irq_ack,
273
- .irq_mask = nsp_gpio_irq_mask,
274
- .irq_unmask = nsp_gpio_irq_unmask,
275
- .irq_set_type = nsp_gpio_irq_set_type,
276
-};
277271
278272 static int nsp_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
279273 {
....@@ -303,6 +297,19 @@
303297 return 0;
304298 }
305299
300
+static int nsp_gpio_get_direction(struct gpio_chip *gc, unsigned gpio)
301
+{
302
+ struct nsp_gpio *chip = gpiochip_get_data(gc);
303
+ unsigned long flags;
304
+ int val;
305
+
306
+ raw_spin_lock_irqsave(&chip->lock, flags);
307
+ val = nsp_get_bit(chip, REG, NSP_GPIO_OUT_EN, gpio);
308
+ raw_spin_unlock_irqrestore(&chip->lock, flags);
309
+
310
+ return !val;
311
+}
312
+
306313 static void nsp_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
307314 {
308315 struct nsp_gpio *chip = gpiochip_get_data(gc);
....@@ -320,13 +327,6 @@
320327 struct nsp_gpio *chip = gpiochip_get_data(gc);
321328
322329 return !!(readl(chip->base + NSP_GPIO_DATA_IN) & BIT(gpio));
323
-}
324
-
325
-static int nsp_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
326
-{
327
- struct nsp_gpio *chip = gpiochip_get_data(gc);
328
-
329
- return irq_linear_revmap(chip->irq_domain, offset);
330330 }
331331
332332 static int nsp_get_groups_count(struct pinctrl_dev *pctldev)
....@@ -613,10 +613,9 @@
613613 static int nsp_gpio_probe(struct platform_device *pdev)
614614 {
615615 struct device *dev = &pdev->dev;
616
- struct resource *res;
617616 struct nsp_gpio *chip;
618617 struct gpio_chip *gc;
619
- u32 val, count;
618
+ u32 val;
620619 int irq, ret;
621620
622621 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &val)) {
....@@ -631,15 +630,13 @@
631630 chip->dev = dev;
632631 platform_set_drvdata(pdev, chip);
633632
634
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
635
- chip->base = devm_ioremap_resource(dev, res);
633
+ chip->base = devm_platform_ioremap_resource(pdev, 0);
636634 if (IS_ERR(chip->base)) {
637635 dev_err(dev, "unable to map I/O memory\n");
638636 return PTR_ERR(chip->base);
639637 }
640638
641
- res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
642
- chip->io_ctrl = devm_ioremap_resource(dev, res);
639
+ chip->io_ctrl = devm_platform_ioremap_resource(pdev, 1);
643640 if (IS_ERR(chip->io_ctrl)) {
644641 dev_err(dev, "unable to map I/O memory\n");
645642 return PTR_ERR(chip->io_ctrl);
....@@ -657,46 +654,47 @@
657654 gc->free = gpiochip_generic_free;
658655 gc->direction_input = nsp_gpio_direction_input;
659656 gc->direction_output = nsp_gpio_direction_output;
657
+ gc->get_direction = nsp_gpio_get_direction;
660658 gc->set = nsp_gpio_set;
661659 gc->get = nsp_gpio_get;
662
- gc->to_irq = nsp_gpio_to_irq;
663660
664661 /* optional GPIO interrupt support */
665662 irq = platform_get_irq(pdev, 0);
666663 if (irq > 0) {
667
- /* Create irq domain so that each pin can be assigned an IRQ.*/
668
- chip->irq_domain = irq_domain_add_linear(gc->of_node, gc->ngpio,
669
- &irq_domain_simple_ops,
670
- chip);
671
- if (!chip->irq_domain) {
672
- dev_err(&pdev->dev, "Couldn't allocate IRQ domain\n");
673
- return -ENXIO;
674
- }
664
+ struct gpio_irq_chip *girq;
665
+ struct irq_chip *irqc;
675666
676
- /* Map each gpio to an IRQ and set the handler for gpiolib. */
677
- for (count = 0; count < gc->ngpio; count++) {
678
- int irq = irq_create_mapping(chip->irq_domain, count);
679
-
680
- irq_set_chip_and_handler(irq, &nsp_gpio_irq_chip,
681
- handle_simple_irq);
682
- irq_set_chip_data(irq, chip);
683
- }
684
-
685
- /* Install ISR for this GPIO controller. */
686
- ret = devm_request_irq(&pdev->dev, irq, nsp_gpio_irq_handler,
687
- IRQF_SHARED, "gpio-a", chip);
688
- if (ret) {
689
- dev_err(&pdev->dev, "Unable to request IRQ%d: %d\n",
690
- irq, ret);
691
- goto err_rm_gpiochip;
692
- }
667
+ irqc = &chip->irqchip;
668
+ irqc->name = "gpio-a";
669
+ irqc->irq_ack = nsp_gpio_irq_ack;
670
+ irqc->irq_mask = nsp_gpio_irq_mask;
671
+ irqc->irq_unmask = nsp_gpio_irq_unmask;
672
+ irqc->irq_set_type = nsp_gpio_irq_set_type;
693673
694674 val = readl(chip->base + NSP_CHIP_A_INT_MASK);
695675 val = val | NSP_CHIP_A_GPIO_INT_BIT;
696676 writel(val, (chip->base + NSP_CHIP_A_INT_MASK));
677
+
678
+ /* Install ISR for this GPIO controller. */
679
+ ret = devm_request_irq(dev, irq, nsp_gpio_irq_handler,
680
+ IRQF_SHARED, "gpio-a", &chip->gc);
681
+ if (ret) {
682
+ dev_err(&pdev->dev, "Unable to request IRQ%d: %d\n",
683
+ irq, ret);
684
+ return ret;
685
+ }
686
+
687
+ girq = &chip->gc.irq;
688
+ girq->chip = irqc;
689
+ /* This will let us handle the parent IRQ in the driver */
690
+ girq->parent_handler = NULL;
691
+ girq->num_parents = 0;
692
+ girq->parents = NULL;
693
+ girq->default_type = IRQ_TYPE_NONE;
694
+ girq->handler = handle_bad_irq;
697695 }
698696
699
- ret = gpiochip_add_data(gc, chip);
697
+ ret = devm_gpiochip_add_data(dev, gc, chip);
700698 if (ret < 0) {
701699 dev_err(dev, "unable to add GPIO chip\n");
702700 return ret;
....@@ -705,15 +703,10 @@
705703 ret = nsp_gpio_register_pinconf(chip);
706704 if (ret) {
707705 dev_err(dev, "unable to register pinconf\n");
708
- goto err_rm_gpiochip;
706
+ return ret;
709707 }
710708
711709 return 0;
712
-
713
-err_rm_gpiochip:
714
- gpiochip_remove(gc);
715
-
716
- return ret;
717710 }
718711
719712 static struct platform_driver nsp_gpio_driver = {