Blame view
drivers/gpio/gpio-em.c
9.63 KB
0b7121833 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
a07e103ef gpio: Emma Mobile... |
2 3 4 5 |
/* * Emma Mobile GPIO Support - GIO * * Copyright (C) 2012 Magnus Damm |
a07e103ef gpio: Emma Mobile... |
6 7 8 9 10 11 12 13 14 15 16 17 |
*/ #include <linux/init.h> #include <linux/platform_device.h> #include <linux/spinlock.h> #include <linux/interrupt.h> #include <linux/ioport.h> #include <linux/io.h> #include <linux/irq.h> #include <linux/irqdomain.h> #include <linux/bitops.h> #include <linux/err.h> |
7275cb75b gpio: em: Use the... |
18 |
#include <linux/gpio/driver.h> |
a07e103ef gpio: Emma Mobile... |
19 20 |
#include <linux/slab.h> #include <linux/module.h> |
640efa08c gpio: em: Add pin... |
21 |
#include <linux/pinctrl/consumer.h> |
a07e103ef gpio: Emma Mobile... |
22 23 24 25 |
struct em_gio_priv { void __iomem *base0; void __iomem *base1; |
a07e103ef gpio: Emma Mobile... |
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
spinlock_t sense_lock; struct platform_device *pdev; struct gpio_chip gpio_chip; struct irq_chip irq_chip; struct irq_domain *irq_domain; }; #define GIO_E1 0x00 #define GIO_E0 0x04 #define GIO_EM 0x04 #define GIO_OL 0x08 #define GIO_OH 0x0c #define GIO_I 0x10 #define GIO_IIA 0x14 #define GIO_IEN 0x18 #define GIO_IDS 0x1c #define GIO_IIM 0x1c #define GIO_RAW 0x20 #define GIO_MST 0x24 #define GIO_IIR 0x28 #define GIO_IDT0 0x40 #define GIO_IDT1 0x44 #define GIO_IDT2 0x48 #define GIO_IDT3 0x4c #define GIO_RAWBL 0x50 #define GIO_RAWBH 0x54 #define GIO_IRBL 0x58 #define GIO_IRBH 0x5c #define GIO_IDT(n) (GIO_IDT0 + ((n) * 4)) static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs) { if (offs < GIO_IDT0) return ioread32(p->base0 + offs); else return ioread32(p->base1 + (offs - GIO_IDT0)); } static inline void em_gio_write(struct em_gio_priv *p, int offs, unsigned long value) { if (offs < GIO_IDT0) iowrite32(value, p->base0 + offs); else iowrite32(value, p->base1 + (offs - GIO_IDT0)); } |
a07e103ef gpio: Emma Mobile... |
74 75 |
static void em_gio_irq_disable(struct irq_data *d) { |
a9f77c93a gpio: em: Use irq... |
76 |
struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
a07e103ef gpio: Emma Mobile... |
77 78 79 80 81 82 |
em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d))); } static void em_gio_irq_enable(struct irq_data *d) { |
a9f77c93a gpio: em: Use irq... |
83 |
struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
a07e103ef gpio: Emma Mobile... |
84 85 86 |
em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d))); } |
57ef04288 gpio: switch driv... |
87 |
static int em_gio_irq_reqres(struct irq_data *d) |
0dc616236 gpio: em: lock IR... |
88 89 |
{ struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
41d69087f gpio: em: Don't s... |
90 |
int ret; |
0dc616236 gpio: em: lock IR... |
91 |
|
41d69087f gpio: em: Don't s... |
92 93 |
ret = gpiochip_lock_as_irq(&p->gpio_chip, irqd_to_hwirq(d)); if (ret) { |
58383c784 gpio: change memb... |
94 |
dev_err(p->gpio_chip.parent, |
0dc616236 gpio: em: lock IR... |
95 96 97 |
"unable to lock HW IRQ %lu for IRQ ", irqd_to_hwirq(d)); |
41d69087f gpio: em: Don't s... |
98 |
return ret; |
57ef04288 gpio: switch driv... |
99 |
} |
0dc616236 gpio: em: lock IR... |
100 101 |
return 0; } |
57ef04288 gpio: switch driv... |
102 |
static void em_gio_irq_relres(struct irq_data *d) |
0dc616236 gpio: em: lock IR... |
103 104 |
{ struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
e3a2e8789 gpio: rename gpio... |
105 |
gpiochip_unlock_as_irq(&p->gpio_chip, irqd_to_hwirq(d)); |
0dc616236 gpio: em: lock IR... |
106 |
} |
a07e103ef gpio: Emma Mobile... |
107 108 109 110 111 112 113 114 115 116 117 118 119 |
#define GIO_ASYNC(x) (x + 8) static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = { [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00), [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01), [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02), [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03), [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04), }; static int em_gio_irq_set_type(struct irq_data *d, unsigned int type) { unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK]; |
a9f77c93a gpio: em: Use irq... |
120 |
struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
a07e103ef gpio: Emma Mobile... |
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
unsigned int reg, offset, shift; unsigned long flags; unsigned long tmp; if (!value) return -EINVAL; offset = irqd_to_hwirq(d); pr_debug("gio: sense irq = %d, mode = %d ", offset, value); /* 8 x 4 bit fields in 4 IDT registers */ reg = GIO_IDT(offset >> 3); shift = (offset & 0x07) << 4; spin_lock_irqsave(&p->sense_lock, flags); /* disable the interrupt in IIA */ tmp = em_gio_read(p, GIO_IIA); tmp &= ~BIT(offset); em_gio_write(p, GIO_IIA, tmp); /* change the sense setting in IDT */ tmp = em_gio_read(p, reg); tmp &= ~(0xf << shift); tmp |= value << shift; em_gio_write(p, reg, tmp); /* clear pending interrupts */ em_gio_write(p, GIO_IIR, BIT(offset)); /* enable the interrupt in IIA */ tmp = em_gio_read(p, GIO_IIA); tmp |= BIT(offset); em_gio_write(p, GIO_IIA, tmp); spin_unlock_irqrestore(&p->sense_lock, flags); return 0; } static irqreturn_t em_gio_irq_handler(int irq, void *dev_id) { struct em_gio_priv *p = dev_id; unsigned long pending; unsigned int offset, irqs_handled = 0; while ((pending = em_gio_read(p, GIO_MST))) { offset = __ffs(pending); em_gio_write(p, GIO_IIR, BIT(offset)); generic_handle_irq(irq_find_mapping(p->irq_domain, offset)); irqs_handled++; } return irqs_handled ? IRQ_HANDLED : IRQ_NONE; } static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip) { |
6219e7bba gpio: em: use gpi... |
181 |
return gpiochip_get_data(chip); |
a07e103ef gpio: Emma Mobile... |
182 183 184 185 186 187 188 189 190 191 |
} static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset) { em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset)); return 0; } static int em_gio_get(struct gpio_chip *chip, unsigned offset) { |
8388f2909 gpio: em: Be sure... |
192 |
return !!(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset)); |
a07e103ef gpio: Emma Mobile... |
193 194 195 196 197 198 199 |
} static void __em_gio_set(struct gpio_chip *chip, unsigned int reg, unsigned shift, int value) { /* upper 16 bits contains mask and lower 16 actual value */ em_gio_write(gpio_to_priv(chip), reg, |
5f077644b gpio: em: use BIT... |
200 |
(BIT(shift + 16)) | (value << shift)); |
a07e103ef gpio: Emma Mobile... |
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
} static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value) { /* output is split into two registers */ if (offset < 16) __em_gio_set(chip, GIO_OL, offset, value); else __em_gio_set(chip, GIO_OH, offset - 16, value); } static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset, int value) { /* write GPIO value to output before selecting output mode of pin */ em_gio_set(chip, offset, value); em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset)); return 0; } static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset) { |
7385500a4 gpio/em: convert ... |
223 |
return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset); |
a07e103ef gpio: Emma Mobile... |
224 |
} |
640efa08c gpio: em: Add pin... |
225 226 |
static int em_gio_request(struct gpio_chip *chip, unsigned offset) { |
a9a1d2a78 pinctrl/gpio: Uni... |
227 |
return pinctrl_gpio_request(chip->base + offset); |
640efa08c gpio: em: Add pin... |
228 229 230 231 |
} static void em_gio_free(struct gpio_chip *chip, unsigned offset) { |
a9a1d2a78 pinctrl/gpio: Uni... |
232 |
pinctrl_gpio_free(chip->base + offset); |
640efa08c gpio: em: Add pin... |
233 234 235 236 237 238 |
/* Set the GPIO as an input to ensure that the next GPIO request won't * drive the GPIO pin as an output. */ em_gio_direction_input(chip, offset); } |
2d61e3e90 gpio: em: drop re... |
239 240 |
static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int irq, irq_hw_number_t hwirq) |
a07e103ef gpio: Emma Mobile... |
241 242 |
{ struct em_gio_priv *p = h->host_data; |
2d61e3e90 gpio: em: drop re... |
243 244 |
pr_debug("gio: map hw irq = %d, irq = %d ", (int)hwirq, irq); |
a07e103ef gpio: Emma Mobile... |
245 |
|
2d61e3e90 gpio: em: drop re... |
246 247 |
irq_set_chip_data(irq, h->host_data); irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq); |
a07e103ef gpio: Emma Mobile... |
248 249 |
return 0; } |
0b354dc43 gpio: Constify ir... |
250 |
static const struct irq_domain_ops em_gio_irq_domain_ops = { |
a07e103ef gpio: Emma Mobile... |
251 |
.map = em_gio_irq_domain_map, |
753c5983d gpio/em: Add Devi... |
252 |
.xlate = irq_domain_xlate_twocell, |
a07e103ef gpio: Emma Mobile... |
253 |
}; |
19ec11a22 gpio: em: remove ... |
254 255 256 257 258 259 |
static void em_gio_irq_domain_remove(void *data) { struct irq_domain *domain = data; irq_domain_remove(domain); } |
3836309d9 gpio: remove use ... |
260 |
static int em_gio_probe(struct platform_device *pdev) |
a07e103ef gpio: Emma Mobile... |
261 |
{ |
a07e103ef gpio: Emma Mobile... |
262 263 264 265 |
struct em_gio_priv *p; struct resource *io[2], *irq[2]; struct gpio_chip *gpio_chip; struct irq_chip *irq_chip; |
66ad66293 gpio: em: use a h... |
266 267 |
struct device *dev = &pdev->dev; const char *name = dev_name(dev); |
527b397a7 gpio: em: Remove ... |
268 |
unsigned int ngpios; |
a07e103ef gpio: Emma Mobile... |
269 |
int ret; |
66ad66293 gpio: em: use a h... |
270 |
p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL); |
715ed7280 gpio: em: Return ... |
271 272 |
if (!p) return -ENOMEM; |
a07e103ef gpio: Emma Mobile... |
273 274 275 276 277 278 279 280 281 |
p->pdev = pdev; platform_set_drvdata(pdev, p); spin_lock_init(&p->sense_lock); io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0); io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1); irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0); irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1); |
753c5983d gpio/em: Add Devi... |
282 |
if (!io[0] || !io[1] || !irq[0] || !irq[1]) { |
66ad66293 gpio: em: use a h... |
283 284 |
dev_err(dev, "missing IRQ or IOMEM "); |
715ed7280 gpio: em: Return ... |
285 |
return -EINVAL; |
a07e103ef gpio: Emma Mobile... |
286 |
} |
66ad66293 gpio: em: use a h... |
287 |
p->base0 = devm_ioremap_nocache(dev, io[0]->start, |
1cfe6f8cb gpio: em: Make us... |
288 |
resource_size(io[0])); |
715ed7280 gpio: em: Return ... |
289 290 |
if (!p->base0) return -ENOMEM; |
a07e103ef gpio: Emma Mobile... |
291 |
|
66ad66293 gpio: em: use a h... |
292 |
p->base1 = devm_ioremap_nocache(dev, io[1]->start, |
1cfe6f8cb gpio: em: Make us... |
293 |
resource_size(io[1])); |
715ed7280 gpio: em: Return ... |
294 295 |
if (!p->base1) return -ENOMEM; |
a07e103ef gpio: Emma Mobile... |
296 |
|
66ad66293 gpio: em: use a h... |
297 298 299 |
if (of_property_read_u32(dev->of_node, "ngpios", &ngpios)) { dev_err(dev, "Missing ngpios OF property "); |
715ed7280 gpio: em: Return ... |
300 |
return -EINVAL; |
753c5983d gpio/em: Add Devi... |
301 |
} |
a07e103ef gpio: Emma Mobile... |
302 |
gpio_chip = &p->gpio_chip; |
66ad66293 gpio: em: use a h... |
303 |
gpio_chip->of_node = dev->of_node; |
a07e103ef gpio: Emma Mobile... |
304 305 306 307 308 |
gpio_chip->direction_input = em_gio_direction_input; gpio_chip->get = em_gio_get; gpio_chip->direction_output = em_gio_direction_output; gpio_chip->set = em_gio_set; gpio_chip->to_irq = em_gio_to_irq; |
640efa08c gpio: em: Add pin... |
309 310 |
gpio_chip->request = em_gio_request; gpio_chip->free = em_gio_free; |
a07e103ef gpio: Emma Mobile... |
311 |
gpio_chip->label = name; |
66ad66293 gpio: em: use a h... |
312 |
gpio_chip->parent = dev; |
a07e103ef gpio: Emma Mobile... |
313 |
gpio_chip->owner = THIS_MODULE; |
527b397a7 gpio: em: Remove ... |
314 315 |
gpio_chip->base = -1; gpio_chip->ngpio = ngpios; |
a07e103ef gpio: Emma Mobile... |
316 317 318 319 320 |
irq_chip = &p->irq_chip; irq_chip->name = name; irq_chip->irq_mask = em_gio_irq_disable; irq_chip->irq_unmask = em_gio_irq_enable; |
a07e103ef gpio: Emma Mobile... |
321 |
irq_chip->irq_set_type = em_gio_irq_set_type; |
57ef04288 gpio: switch driv... |
322 323 |
irq_chip->irq_request_resources = em_gio_irq_reqres; irq_chip->irq_release_resources = em_gio_irq_relres; |
03621b605 gpio: em: Enable ... |
324 |
irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND; |
a07e103ef gpio: Emma Mobile... |
325 |
|
66ad66293 gpio: em: use a h... |
326 |
p->irq_domain = irq_domain_add_simple(dev->of_node, ngpios, 0, |
7385500a4 gpio/em: convert ... |
327 |
&em_gio_irq_domain_ops, p); |
163108199 gpio: em: Fix bui... |
328 |
if (!p->irq_domain) { |
66ad66293 gpio: em: use a h... |
329 330 |
dev_err(dev, "cannot initialize irq domain "); |
715ed7280 gpio: em: Return ... |
331 |
return -ENXIO; |
a07e103ef gpio: Emma Mobile... |
332 |
} |
66ad66293 gpio: em: use a h... |
333 |
ret = devm_add_action_or_reset(dev, em_gio_irq_domain_remove, |
19ec11a22 gpio: em: remove ... |
334 335 336 |
p->irq_domain); if (ret) return ret; |
66ad66293 gpio: em: use a h... |
337 |
if (devm_request_irq(dev, irq[0]->start, |
1cfe6f8cb gpio: em: Make us... |
338 |
em_gio_irq_handler, 0, name, p)) { |
66ad66293 gpio: em: use a h... |
339 340 |
dev_err(dev, "failed to request low IRQ "); |
19ec11a22 gpio: em: remove ... |
341 |
return -ENOENT; |
a07e103ef gpio: Emma Mobile... |
342 |
} |
66ad66293 gpio: em: use a h... |
343 |
if (devm_request_irq(dev, irq[1]->start, |
1cfe6f8cb gpio: em: Make us... |
344 |
em_gio_irq_handler, 0, name, p)) { |
66ad66293 gpio: em: use a h... |
345 346 |
dev_err(dev, "failed to request high IRQ "); |
19ec11a22 gpio: em: remove ... |
347 |
return -ENOENT; |
a07e103ef gpio: Emma Mobile... |
348 |
} |
66ad66293 gpio: em: use a h... |
349 |
ret = devm_gpiochip_add_data(dev, gpio_chip, p); |
a07e103ef gpio: Emma Mobile... |
350 |
if (ret) { |
66ad66293 gpio: em: use a h... |
351 352 |
dev_err(dev, "failed to add GPIO controller "); |
19ec11a22 gpio: em: remove ... |
353 |
return ret; |
a07e103ef gpio: Emma Mobile... |
354 |
} |
640efa08c gpio: em: Add pin... |
355 |
|
a07e103ef gpio: Emma Mobile... |
356 |
return 0; |
a07e103ef gpio: Emma Mobile... |
357 |
} |
753c5983d gpio/em: Add Devi... |
358 359 360 361 362 |
static const struct of_device_id em_gio_dt_ids[] = { { .compatible = "renesas,em-gio", }, {}, }; MODULE_DEVICE_TABLE(of, em_gio_dt_ids); |
a07e103ef gpio: Emma Mobile... |
363 364 |
static struct platform_driver em_gio_device_driver = { .probe = em_gio_probe, |
a07e103ef gpio: Emma Mobile... |
365 366 |
.driver = { .name = "em_gio", |
753c5983d gpio/em: Add Devi... |
367 |
.of_match_table = em_gio_dt_ids, |
a07e103ef gpio: Emma Mobile... |
368 369 |
} }; |
753c5983d gpio/em: Add Devi... |
370 371 372 373 374 375 376 377 378 379 380 |
static int __init em_gio_init(void) { return platform_driver_register(&em_gio_device_driver); } postcore_initcall(em_gio_init); static void __exit em_gio_exit(void) { platform_driver_unregister(&em_gio_device_driver); } module_exit(em_gio_exit); |
a07e103ef gpio: Emma Mobile... |
381 382 383 384 |
MODULE_AUTHOR("Magnus Damm"); MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver"); MODULE_LICENSE("GPL v2"); |