aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpio-dln2.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpio/gpio-dln2.c')
-rw-r--r--drivers/gpio/gpio-dln2.c156
1 files changed, 67 insertions, 89 deletions
diff --git a/drivers/gpio/gpio-dln2.c b/drivers/gpio/gpio-dln2.c
index 978b51eae2ec..ce3c1558cb0a 100644
--- a/drivers/gpio/gpio-dln2.c
+++ b/drivers/gpio/gpio-dln2.c
@@ -47,13 +47,6 @@
47 47
48#define DLN2_GPIO_MAX_PINS 32 48#define DLN2_GPIO_MAX_PINS 32
49 49
50struct dln2_irq_work {
51 struct work_struct work;
52 struct dln2_gpio *dln2;
53 int pin;
54 int type;
55};
56
57struct dln2_gpio { 50struct dln2_gpio {
58 struct platform_device *pdev; 51 struct platform_device *pdev;
59 struct gpio_chip gpio; 52 struct gpio_chip gpio;
@@ -64,10 +57,12 @@ struct dln2_gpio {
64 */ 57 */
65 DECLARE_BITMAP(output_enabled, DLN2_GPIO_MAX_PINS); 58 DECLARE_BITMAP(output_enabled, DLN2_GPIO_MAX_PINS);
66 59
67 DECLARE_BITMAP(irqs_masked, DLN2_GPIO_MAX_PINS); 60 /* active IRQs - not synced to hardware */
68 DECLARE_BITMAP(irqs_enabled, DLN2_GPIO_MAX_PINS); 61 DECLARE_BITMAP(unmasked_irqs, DLN2_GPIO_MAX_PINS);
69 DECLARE_BITMAP(irqs_pending, DLN2_GPIO_MAX_PINS); 62 /* active IRQS - synced to hardware */
70 struct dln2_irq_work *irq_work; 63 DECLARE_BITMAP(enabled_irqs, DLN2_GPIO_MAX_PINS);
64 int irq_type[DLN2_GPIO_MAX_PINS];
65 struct mutex irq_lock;
71}; 66};
72 67
73struct dln2_gpio_pin { 68struct dln2_gpio_pin {
@@ -141,16 +136,16 @@ static int dln2_gpio_pin_get_out_val(struct dln2_gpio *dln2, unsigned int pin)
141 return !!ret; 136 return !!ret;
142} 137}
143 138
144static void dln2_gpio_pin_set_out_val(struct dln2_gpio *dln2, 139static int dln2_gpio_pin_set_out_val(struct dln2_gpio *dln2,
145 unsigned int pin, int value) 140 unsigned int pin, int value)
146{ 141{
147 struct dln2_gpio_pin_val req = { 142 struct dln2_gpio_pin_val req = {
148 .pin = cpu_to_le16(pin), 143 .pin = cpu_to_le16(pin),
149 .value = value, 144 .value = value,
150 }; 145 };
151 146
152 dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_OUT_VAL, &req, 147 return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_OUT_VAL, &req,
153 sizeof(req)); 148 sizeof(req));
154} 149}
155 150
156#define DLN2_GPIO_DIRECTION_IN 0 151#define DLN2_GPIO_DIRECTION_IN 0
@@ -267,6 +262,13 @@ static int dln2_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
267static int dln2_gpio_direction_output(struct gpio_chip *chip, unsigned offset, 262static int dln2_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
268 int value) 263 int value)
269{ 264{
265 struct dln2_gpio *dln2 = container_of(chip, struct dln2_gpio, gpio);
266 int ret;
267
268 ret = dln2_gpio_pin_set_out_val(dln2, offset, value);
269 if (ret < 0)
270 return ret;
271
270 return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_OUT); 272 return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_OUT);
271} 273}
272 274
@@ -297,36 +299,13 @@ static int dln2_gpio_set_event_cfg(struct dln2_gpio *dln2, unsigned pin,
297 &req, sizeof(req)); 299 &req, sizeof(req));
298} 300}
299 301
300static void dln2_irq_work(struct work_struct *w) 302static void dln2_irq_unmask(struct irq_data *irqd)
301{
302 struct dln2_irq_work *iw = container_of(w, struct dln2_irq_work, work);
303 struct dln2_gpio *dln2 = iw->dln2;
304 u8 type = iw->type & DLN2_GPIO_EVENT_MASK;
305
306 if (test_bit(iw->pin, dln2->irqs_enabled))
307 dln2_gpio_set_event_cfg(dln2, iw->pin, type, 0);
308 else
309 dln2_gpio_set_event_cfg(dln2, iw->pin, DLN2_GPIO_EVENT_NONE, 0);
310}
311
312static void dln2_irq_enable(struct irq_data *irqd)
313{
314 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
315 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
316 int pin = irqd_to_hwirq(irqd);
317
318 set_bit(pin, dln2->irqs_enabled);
319 schedule_work(&dln2->irq_work[pin].work);
320}
321
322static void dln2_irq_disable(struct irq_data *irqd)
323{ 303{
324 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); 304 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
325 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio); 305 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
326 int pin = irqd_to_hwirq(irqd); 306 int pin = irqd_to_hwirq(irqd);
327 307
328 clear_bit(pin, dln2->irqs_enabled); 308 set_bit(pin, dln2->unmasked_irqs);
329 schedule_work(&dln2->irq_work[pin].work);
330} 309}
331 310
332static void dln2_irq_mask(struct irq_data *irqd) 311static void dln2_irq_mask(struct irq_data *irqd)
@@ -335,27 +314,7 @@ static void dln2_irq_mask(struct irq_data *irqd)
335 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio); 314 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
336 int pin = irqd_to_hwirq(irqd); 315 int pin = irqd_to_hwirq(irqd);
337 316
338 set_bit(pin, dln2->irqs_masked); 317 clear_bit(pin, dln2->unmasked_irqs);
339}
340
341static void dln2_irq_unmask(struct irq_data *irqd)
342{
343 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
344 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
345 struct device *dev = dln2->gpio.dev;
346 int pin = irqd_to_hwirq(irqd);
347
348 if (test_and_clear_bit(pin, dln2->irqs_pending)) {
349 int irq;
350
351 irq = irq_find_mapping(dln2->gpio.irqdomain, pin);
352 if (!irq) {
353 dev_err(dev, "pin %d not mapped to IRQ\n", pin);
354 return;
355 }
356
357 generic_handle_irq(irq);
358 }
359} 318}
360 319
361static int dln2_irq_set_type(struct irq_data *irqd, unsigned type) 320static int dln2_irq_set_type(struct irq_data *irqd, unsigned type)
@@ -366,19 +325,19 @@ static int dln2_irq_set_type(struct irq_data *irqd, unsigned type)
366 325
367 switch (type) { 326 switch (type) {
368 case IRQ_TYPE_LEVEL_HIGH: 327 case IRQ_TYPE_LEVEL_HIGH:
369 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_LVL_HIGH; 328 dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_HIGH;
370 break; 329 break;
371 case IRQ_TYPE_LEVEL_LOW: 330 case IRQ_TYPE_LEVEL_LOW:
372 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_LVL_LOW; 331 dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_LOW;
373 break; 332 break;
374 case IRQ_TYPE_EDGE_BOTH: 333 case IRQ_TYPE_EDGE_BOTH:
375 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE; 334 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE;
376 break; 335 break;
377 case IRQ_TYPE_EDGE_RISING: 336 case IRQ_TYPE_EDGE_RISING:
378 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE_RISING; 337 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_RISING;
379 break; 338 break;
380 case IRQ_TYPE_EDGE_FALLING: 339 case IRQ_TYPE_EDGE_FALLING:
381 dln2->irq_work[pin].type = DLN2_GPIO_EVENT_CHANGE_FALLING; 340 dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_FALLING;
382 break; 341 break;
383 default: 342 default:
384 return -EINVAL; 343 return -EINVAL;
@@ -387,13 +346,50 @@ static int dln2_irq_set_type(struct irq_data *irqd, unsigned type)
387 return 0; 346 return 0;
388} 347}
389 348
349static void dln2_irq_bus_lock(struct irq_data *irqd)
350{
351 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
352 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
353
354 mutex_lock(&dln2->irq_lock);
355}
356
357static void dln2_irq_bus_unlock(struct irq_data *irqd)
358{
359 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
360 struct dln2_gpio *dln2 = container_of(gc, struct dln2_gpio, gpio);
361 int pin = irqd_to_hwirq(irqd);
362 int enabled, unmasked;
363 unsigned type;
364 int ret;
365
366 enabled = test_bit(pin, dln2->enabled_irqs);
367 unmasked = test_bit(pin, dln2->unmasked_irqs);
368
369 if (enabled != unmasked) {
370 if (unmasked) {
371 type = dln2->irq_type[pin] & DLN2_GPIO_EVENT_MASK;
372 set_bit(pin, dln2->enabled_irqs);
373 } else {
374 type = DLN2_GPIO_EVENT_NONE;
375 clear_bit(pin, dln2->enabled_irqs);
376 }
377
378 ret = dln2_gpio_set_event_cfg(dln2, pin, type, 0);
379 if (ret)
380 dev_err(dln2->gpio.dev, "failed to set event\n");
381 }
382
383 mutex_unlock(&dln2->irq_lock);
384}
385
390static struct irq_chip dln2_gpio_irqchip = { 386static struct irq_chip dln2_gpio_irqchip = {
391 .name = "dln2-irq", 387 .name = "dln2-irq",
392 .irq_enable = dln2_irq_enable,
393 .irq_disable = dln2_irq_disable,
394 .irq_mask = dln2_irq_mask, 388 .irq_mask = dln2_irq_mask,
395 .irq_unmask = dln2_irq_unmask, 389 .irq_unmask = dln2_irq_unmask,
396 .irq_set_type = dln2_irq_set_type, 390 .irq_set_type = dln2_irq_set_type,
391 .irq_bus_lock = dln2_irq_bus_lock,
392 .irq_bus_sync_unlock = dln2_irq_bus_unlock,
397}; 393};
398 394
399static void dln2_gpio_event(struct platform_device *pdev, u16 echo, 395static void dln2_gpio_event(struct platform_device *pdev, u16 echo,
@@ -425,14 +421,7 @@ static void dln2_gpio_event(struct platform_device *pdev, u16 echo,
425 return; 421 return;
426 } 422 }
427 423
428 if (!test_bit(pin, dln2->irqs_enabled)) 424 switch (dln2->irq_type[pin]) {
429 return;
430 if (test_bit(pin, dln2->irqs_masked)) {
431 set_bit(pin, dln2->irqs_pending);
432 return;
433 }
434
435 switch (dln2->irq_work[pin].type) {
436 case DLN2_GPIO_EVENT_CHANGE_RISING: 425 case DLN2_GPIO_EVENT_CHANGE_RISING:
437 if (event->value) 426 if (event->value)
438 generic_handle_irq(irq); 427 generic_handle_irq(irq);
@@ -451,7 +440,7 @@ static int dln2_gpio_probe(struct platform_device *pdev)
451 struct dln2_gpio *dln2; 440 struct dln2_gpio *dln2;
452 struct device *dev = &pdev->dev; 441 struct device *dev = &pdev->dev;
453 int pins; 442 int pins;
454 int i, ret; 443 int ret;
455 444
456 pins = dln2_gpio_get_pin_count(pdev); 445 pins = dln2_gpio_get_pin_count(pdev);
457 if (pins < 0) { 446 if (pins < 0) {
@@ -467,15 +456,7 @@ static int dln2_gpio_probe(struct platform_device *pdev)
467 if (!dln2) 456 if (!dln2)
468 return -ENOMEM; 457 return -ENOMEM;
469 458
470 dln2->irq_work = devm_kcalloc(&pdev->dev, pins, 459 mutex_init(&dln2->irq_lock);
471 sizeof(struct dln2_irq_work), GFP_KERNEL);
472 if (!dln2->irq_work)
473 return -ENOMEM;
474 for (i = 0; i < pins; i++) {
475 INIT_WORK(&dln2->irq_work[i].work, dln2_irq_work);
476 dln2->irq_work[i].pin = i;
477 dln2->irq_work[i].dln2 = dln2;
478 }
479 460
480 dln2->pdev = pdev; 461 dln2->pdev = pdev;
481 462
@@ -529,11 +510,8 @@ out:
529static int dln2_gpio_remove(struct platform_device *pdev) 510static int dln2_gpio_remove(struct platform_device *pdev)
530{ 511{
531 struct dln2_gpio *dln2 = platform_get_drvdata(pdev); 512 struct dln2_gpio *dln2 = platform_get_drvdata(pdev);
532 int i;
533 513
534 dln2_unregister_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV); 514 dln2_unregister_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV);
535 for (i = 0; i < dln2->gpio.ngpio; i++)
536 flush_work(&dln2->irq_work[i].work);
537 gpiochip_remove(&dln2->gpio); 515 gpiochip_remove(&dln2->gpio);
538 516
539 return 0; 517 return 0;