Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2010 |
| 3 | * |
| 4 | * License Terms: GNU General Public License v2 |
| 5 | * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson |
| 6 | * |
| 7 | * AB8500 Power-On Key handler |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/input.h> |
| 14 | #include <linux/interrupt.h> |
Linus Walleij | ee66e65 | 2011-12-02 14:16:33 +0100 | [diff] [blame] | 15 | #include <linux/mfd/abx500/ab8500.h> |
Lee Jones | 03ecd22 | 2012-06-12 00:21:47 -0700 | [diff] [blame] | 16 | #include <linux/of.h> |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | |
| 19 | /** |
| 20 | * struct ab8500_ponkey - ab8500 ponkey information |
| 21 | * @input_dev: pointer to input device |
| 22 | * @ab8500: ab8500 parent |
| 23 | * @irq_dbf: irq number for falling transition |
| 24 | * @irq_dbr: irq number for rising transition |
| 25 | */ |
| 26 | struct ab8500_ponkey { |
| 27 | struct input_dev *idev; |
| 28 | struct ab8500 *ab8500; |
| 29 | int irq_dbf; |
| 30 | int irq_dbr; |
| 31 | }; |
| 32 | |
| 33 | /* AB8500 gives us an interrupt when ONKEY is held */ |
| 34 | static irqreturn_t ab8500_ponkey_handler(int irq, void *data) |
| 35 | { |
| 36 | struct ab8500_ponkey *ponkey = data; |
| 37 | |
| 38 | if (irq == ponkey->irq_dbf) |
| 39 | input_report_key(ponkey->idev, KEY_POWER, true); |
| 40 | else if (irq == ponkey->irq_dbr) |
| 41 | input_report_key(ponkey->idev, KEY_POWER, false); |
| 42 | |
| 43 | input_sync(ponkey->idev); |
| 44 | |
| 45 | return IRQ_HANDLED; |
| 46 | } |
| 47 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 48 | static int ab8500_ponkey_probe(struct platform_device *pdev) |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 49 | { |
| 50 | struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent); |
| 51 | struct ab8500_ponkey *ponkey; |
| 52 | struct input_dev *input; |
| 53 | int irq_dbf, irq_dbr; |
| 54 | int error; |
| 55 | |
| 56 | irq_dbf = platform_get_irq_byname(pdev, "ONKEY_DBF"); |
| 57 | if (irq_dbf < 0) { |
| 58 | dev_err(&pdev->dev, "No IRQ for ONKEY_DBF, error=%d\n", irq_dbf); |
| 59 | return irq_dbf; |
| 60 | } |
| 61 | |
| 62 | irq_dbr = platform_get_irq_byname(pdev, "ONKEY_DBR"); |
| 63 | if (irq_dbr < 0) { |
| 64 | dev_err(&pdev->dev, "No IRQ for ONKEY_DBR, error=%d\n", irq_dbr); |
| 65 | return irq_dbr; |
| 66 | } |
| 67 | |
| 68 | ponkey = kzalloc(sizeof(struct ab8500_ponkey), GFP_KERNEL); |
| 69 | input = input_allocate_device(); |
| 70 | if (!ponkey || !input) { |
| 71 | error = -ENOMEM; |
| 72 | goto err_free_mem; |
| 73 | } |
| 74 | |
| 75 | ponkey->idev = input; |
| 76 | ponkey->ab8500 = ab8500; |
Dmitry Torokhov | 527df5f | 2012-09-19 10:23:18 -0700 | [diff] [blame] | 77 | ponkey->irq_dbf = irq_dbf; |
| 78 | ponkey->irq_dbr = irq_dbr; |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 79 | |
| 80 | input->name = "AB8500 POn(PowerOn) Key"; |
| 81 | input->dev.parent = &pdev->dev; |
| 82 | |
| 83 | input_set_capability(input, EV_KEY, KEY_POWER); |
| 84 | |
| 85 | error = request_any_context_irq(ponkey->irq_dbf, ab8500_ponkey_handler, |
| 86 | 0, "ab8500-ponkey-dbf", ponkey); |
| 87 | if (error < 0) { |
| 88 | dev_err(ab8500->dev, "Failed to request dbf IRQ#%d: %d\n", |
| 89 | ponkey->irq_dbf, error); |
| 90 | goto err_free_mem; |
| 91 | } |
| 92 | |
| 93 | error = request_any_context_irq(ponkey->irq_dbr, ab8500_ponkey_handler, |
| 94 | 0, "ab8500-ponkey-dbr", ponkey); |
| 95 | if (error < 0) { |
| 96 | dev_err(ab8500->dev, "Failed to request dbr IRQ#%d: %d\n", |
| 97 | ponkey->irq_dbr, error); |
| 98 | goto err_free_dbf_irq; |
| 99 | } |
| 100 | |
| 101 | error = input_register_device(ponkey->idev); |
| 102 | if (error) { |
| 103 | dev_err(ab8500->dev, "Can't register input device: %d\n", error); |
| 104 | goto err_free_dbr_irq; |
| 105 | } |
| 106 | |
| 107 | platform_set_drvdata(pdev, ponkey); |
| 108 | return 0; |
| 109 | |
| 110 | err_free_dbr_irq: |
Nicolas Kaiser | c5fb514 | 2010-10-24 21:48:11 -0700 | [diff] [blame] | 111 | free_irq(ponkey->irq_dbr, ponkey); |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 112 | err_free_dbf_irq: |
| 113 | free_irq(ponkey->irq_dbf, ponkey); |
| 114 | err_free_mem: |
| 115 | input_free_device(input); |
| 116 | kfree(ponkey); |
| 117 | |
| 118 | return error; |
| 119 | } |
| 120 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 121 | static int ab8500_ponkey_remove(struct platform_device *pdev) |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 122 | { |
| 123 | struct ab8500_ponkey *ponkey = platform_get_drvdata(pdev); |
| 124 | |
| 125 | free_irq(ponkey->irq_dbf, ponkey); |
| 126 | free_irq(ponkey->irq_dbr, ponkey); |
| 127 | input_unregister_device(ponkey->idev); |
| 128 | kfree(ponkey); |
| 129 | |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Lee Jones | 03ecd22 | 2012-06-12 00:21:47 -0700 | [diff] [blame] | 133 | #ifdef CONFIG_OF |
| 134 | static const struct of_device_id ab8500_ponkey_match[] = { |
| 135 | { .compatible = "stericsson,ab8500-ponkey", }, |
| 136 | {} |
| 137 | }; |
| 138 | #endif |
| 139 | |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 140 | static struct platform_driver ab8500_ponkey_driver = { |
| 141 | .driver = { |
| 142 | .name = "ab8500-poweron-key", |
| 143 | .owner = THIS_MODULE, |
Lee Jones | 03ecd22 | 2012-06-12 00:21:47 -0700 | [diff] [blame] | 144 | .of_match_table = of_match_ptr(ab8500_ponkey_match), |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 145 | }, |
| 146 | .probe = ab8500_ponkey_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 147 | .remove = ab8500_ponkey_remove, |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 148 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 149 | module_platform_driver(ab8500_ponkey_driver); |
Sundar R Iyer | 7768651 | 2010-09-05 12:18:47 -0700 | [diff] [blame] | 150 | |
| 151 | MODULE_LICENSE("GPL v2"); |
| 152 | MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>"); |
| 153 | MODULE_DESCRIPTION("ST-Ericsson AB8500 Power-ON(Pon) Key driver"); |