Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * OMAP4 Keypad Driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Texas Instruments |
| 5 | * |
| 6 | * Author: Abraham Arce <x0066660@ti.com> |
| 7 | * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/io.h> |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 29 | #include <linux/of.h> |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 30 | #include <linux/input.h> |
Joachim Eastwood | 61721c8 | 2014-05-29 00:30:02 -0700 | [diff] [blame] | 31 | #include <linux/input/matrix_keypad.h> |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 32 | #include <linux/slab.h> |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 33 | #include <linux/pm_runtime.h> |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 34 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 35 | /* OMAP4 registers */ |
| 36 | #define OMAP4_KBD_REVISION 0x00 |
| 37 | #define OMAP4_KBD_SYSCONFIG 0x10 |
| 38 | #define OMAP4_KBD_SYSSTATUS 0x14 |
| 39 | #define OMAP4_KBD_IRQSTATUS 0x18 |
| 40 | #define OMAP4_KBD_IRQENABLE 0x1C |
| 41 | #define OMAP4_KBD_WAKEUPENABLE 0x20 |
| 42 | #define OMAP4_KBD_PENDING 0x24 |
| 43 | #define OMAP4_KBD_CTRL 0x28 |
| 44 | #define OMAP4_KBD_DEBOUNCINGTIME 0x2C |
| 45 | #define OMAP4_KBD_LONGKEYTIME 0x30 |
| 46 | #define OMAP4_KBD_TIMEOUT 0x34 |
| 47 | #define OMAP4_KBD_STATEMACHINE 0x38 |
| 48 | #define OMAP4_KBD_ROWINPUTS 0x3C |
| 49 | #define OMAP4_KBD_COLUMNOUTPUTS 0x40 |
| 50 | #define OMAP4_KBD_FULLCODE31_0 0x44 |
| 51 | #define OMAP4_KBD_FULLCODE63_32 0x48 |
| 52 | |
| 53 | /* OMAP4 bit definitions */ |
Illia Smyrnov | 875ad69 | 2013-08-25 23:25:18 -0700 | [diff] [blame] | 54 | #define OMAP4_DEF_IRQENABLE_EVENTEN BIT(0) |
| 55 | #define OMAP4_DEF_IRQENABLE_LONGKEY BIT(1) |
| 56 | #define OMAP4_DEF_WUP_EVENT_ENA BIT(0) |
| 57 | #define OMAP4_DEF_WUP_LONG_KEY_ENA BIT(1) |
| 58 | #define OMAP4_DEF_CTRL_NOSOFTMODE BIT(1) |
| 59 | #define OMAP4_DEF_CTRL_PTV_SHIFT 2 |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 60 | |
| 61 | /* OMAP4 values */ |
Illia Smyrnov | 875ad69 | 2013-08-25 23:25:18 -0700 | [diff] [blame] | 62 | #define OMAP4_VAL_IRQDISABLE 0x0 |
| 63 | #define OMAP4_VAL_DEBOUNCINGTIME 0x7 |
| 64 | #define OMAP4_VAL_PVT 0x7 |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 65 | |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 66 | enum { |
| 67 | KBD_REVISION_OMAP4 = 0, |
| 68 | KBD_REVISION_OMAP5, |
| 69 | }; |
| 70 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 71 | struct omap4_keypad { |
| 72 | struct input_dev *input; |
| 73 | |
| 74 | void __iomem *base; |
Illia Smyrnov | 78608a0 | 2013-08-25 23:29:30 -0700 | [diff] [blame] | 75 | bool irq_wake_enabled; |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 76 | unsigned int irq; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 77 | |
| 78 | unsigned int rows; |
| 79 | unsigned int cols; |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 80 | u32 reg_offset; |
| 81 | u32 irqreg_offset; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 82 | unsigned int row_shift; |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 83 | bool no_autorepeat; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 84 | unsigned char key_state[8]; |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 85 | unsigned short *keymap; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 88 | static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset) |
| 89 | { |
| 90 | return __raw_readl(keypad_data->base + |
| 91 | keypad_data->reg_offset + offset); |
| 92 | } |
| 93 | |
| 94 | static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value) |
| 95 | { |
| 96 | __raw_writel(value, |
| 97 | keypad_data->base + keypad_data->reg_offset + offset); |
| 98 | } |
| 99 | |
| 100 | static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset) |
| 101 | { |
| 102 | return __raw_readl(keypad_data->base + |
| 103 | keypad_data->irqreg_offset + offset); |
| 104 | } |
| 105 | |
| 106 | static void kbd_write_irqreg(struct omap4_keypad *keypad_data, |
| 107 | u32 offset, u32 value) |
| 108 | { |
| 109 | __raw_writel(value, |
| 110 | keypad_data->base + keypad_data->irqreg_offset + offset); |
| 111 | } |
| 112 | |
| 113 | |
Illia Smyrnov | c683da3 | 2013-08-25 23:25:50 -0700 | [diff] [blame] | 114 | /* Interrupt handlers */ |
| 115 | static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id) |
| 116 | { |
| 117 | struct omap4_keypad *keypad_data = dev_id; |
| 118 | |
| 119 | if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)) { |
| 120 | /* Disable interrupts */ |
| 121 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, |
| 122 | OMAP4_VAL_IRQDISABLE); |
| 123 | return IRQ_WAKE_THREAD; |
| 124 | } |
| 125 | |
| 126 | return IRQ_NONE; |
| 127 | } |
| 128 | |
| 129 | static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id) |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 130 | { |
| 131 | struct omap4_keypad *keypad_data = dev_id; |
| 132 | struct input_dev *input_dev = keypad_data->input; |
| 133 | unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)]; |
| 134 | unsigned int col, row, code, changed; |
| 135 | u32 *new_state = (u32 *) key_state; |
| 136 | |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 137 | *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0); |
| 138 | *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 139 | |
| 140 | for (row = 0; row < keypad_data->rows; row++) { |
| 141 | changed = key_state[row] ^ keypad_data->key_state[row]; |
| 142 | if (!changed) |
| 143 | continue; |
| 144 | |
| 145 | for (col = 0; col < keypad_data->cols; col++) { |
| 146 | if (changed & (1 << col)) { |
| 147 | code = MATRIX_SCAN_CODE(row, col, |
| 148 | keypad_data->row_shift); |
| 149 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
| 150 | input_report_key(input_dev, |
| 151 | keypad_data->keymap[code], |
| 152 | key_state[row] & (1 << col)); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | input_sync(input_dev); |
| 158 | |
| 159 | memcpy(keypad_data->key_state, key_state, |
| 160 | sizeof(keypad_data->key_state)); |
| 161 | |
| 162 | /* clear pending interrupts */ |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 163 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, |
| 164 | kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 165 | |
| 166 | /* enable interrupts */ |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 167 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, |
| 168 | OMAP4_DEF_IRQENABLE_EVENTEN | |
| 169 | OMAP4_DEF_IRQENABLE_LONGKEY); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 170 | |
| 171 | return IRQ_HANDLED; |
| 172 | } |
| 173 | |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 174 | static int omap4_keypad_open(struct input_dev *input) |
| 175 | { |
| 176 | struct omap4_keypad *keypad_data = input_get_drvdata(input); |
| 177 | |
| 178 | pm_runtime_get_sync(input->dev.parent); |
| 179 | |
| 180 | disable_irq(keypad_data->irq); |
| 181 | |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 182 | kbd_writel(keypad_data, OMAP4_KBD_CTRL, |
Illia Smyrnov | 875ad69 | 2013-08-25 23:25:18 -0700 | [diff] [blame] | 183 | OMAP4_DEF_CTRL_NOSOFTMODE | |
| 184 | (OMAP4_VAL_PVT << OMAP4_DEF_CTRL_PTV_SHIFT)); |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 185 | kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME, |
| 186 | OMAP4_VAL_DEBOUNCINGTIME); |
Illia Smyrnov | afbac60b | 2013-08-25 23:26:16 -0700 | [diff] [blame] | 187 | /* clear pending interrupts */ |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 188 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, |
Illia Smyrnov | afbac60b | 2013-08-25 23:26:16 -0700 | [diff] [blame] | 189 | kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 190 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, |
| 191 | OMAP4_DEF_IRQENABLE_EVENTEN | |
| 192 | OMAP4_DEF_IRQENABLE_LONGKEY); |
| 193 | kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, |
| 194 | OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA); |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 195 | |
| 196 | enable_irq(keypad_data->irq); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static void omap4_keypad_close(struct input_dev *input) |
| 202 | { |
| 203 | struct omap4_keypad *keypad_data = input_get_drvdata(input); |
| 204 | |
| 205 | disable_irq(keypad_data->irq); |
| 206 | |
| 207 | /* Disable interrupts */ |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 208 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, |
| 209 | OMAP4_VAL_IRQDISABLE); |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 210 | |
| 211 | /* clear pending interrupts */ |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 212 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, |
| 213 | kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 214 | |
| 215 | enable_irq(keypad_data->irq); |
| 216 | |
| 217 | pm_runtime_put_sync(input->dev.parent); |
| 218 | } |
| 219 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 220 | static int omap4_keypad_parse_dt(struct device *dev, |
| 221 | struct omap4_keypad *keypad_data) |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 222 | { |
| 223 | struct device_node *np = dev->of_node; |
Simon Glass | 4384041 | 2013-02-25 14:08:40 -0800 | [diff] [blame] | 224 | int err; |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 225 | |
Simon Glass | 4384041 | 2013-02-25 14:08:40 -0800 | [diff] [blame] | 226 | err = matrix_keypad_parse_of_params(dev, &keypad_data->rows, |
| 227 | &keypad_data->cols); |
| 228 | if (err) |
| 229 | return err; |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 230 | |
| 231 | if (of_get_property(np, "linux,input-no-autorepeat", NULL)) |
| 232 | keypad_data->no_autorepeat = true; |
| 233 | |
| 234 | return 0; |
| 235 | } |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 236 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 237 | static int omap4_keypad_probe(struct platform_device *pdev) |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 238 | { |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 239 | struct omap4_keypad *keypad_data; |
| 240 | struct input_dev *input_dev; |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 241 | struct resource *res; |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 242 | unsigned int max_keys; |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 243 | int rev; |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 244 | int irq; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 245 | int error; |
| 246 | |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 247 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 248 | if (!res) { |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 249 | dev_err(&pdev->dev, "no base address specified\n"); |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 253 | irq = platform_get_irq(pdev, 0); |
| 254 | if (!irq) { |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 255 | dev_err(&pdev->dev, "no keyboard irq assigned\n"); |
| 256 | return -EINVAL; |
| 257 | } |
| 258 | |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 259 | keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 260 | if (!keypad_data) { |
| 261 | dev_err(&pdev->dev, "keypad_data memory allocation failed\n"); |
| 262 | return -ENOMEM; |
| 263 | } |
| 264 | |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 265 | keypad_data->irq = irq; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 266 | |
Joachim Eastwood | 61721c8 | 2014-05-29 00:30:02 -0700 | [diff] [blame] | 267 | error = omap4_keypad_parse_dt(&pdev->dev, keypad_data); |
| 268 | if (error) |
| 269 | return error; |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 270 | |
| 271 | res = request_mem_region(res->start, resource_size(res), pdev->name); |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 272 | if (!res) { |
| 273 | dev_err(&pdev->dev, "can't request mem region\n"); |
| 274 | error = -EBUSY; |
| 275 | goto err_free_keypad; |
| 276 | } |
| 277 | |
| 278 | keypad_data->base = ioremap(res->start, resource_size(res)); |
| 279 | if (!keypad_data->base) { |
| 280 | dev_err(&pdev->dev, "can't ioremap mem resource\n"); |
| 281 | error = -ENOMEM; |
| 282 | goto err_release_mem; |
| 283 | } |
| 284 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 285 | |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 286 | /* |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 287 | * Enable clocks for the keypad module so that we can read |
| 288 | * revision register. |
| 289 | */ |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 290 | pm_runtime_enable(&pdev->dev); |
| 291 | error = pm_runtime_get_sync(&pdev->dev); |
| 292 | if (error) { |
| 293 | dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n"); |
| 294 | goto err_unmap; |
| 295 | } |
| 296 | rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION); |
| 297 | rev &= 0x03 << 30; |
| 298 | rev >>= 30; |
| 299 | switch (rev) { |
| 300 | case KBD_REVISION_OMAP4: |
| 301 | keypad_data->reg_offset = 0x00; |
| 302 | keypad_data->irqreg_offset = 0x00; |
| 303 | break; |
| 304 | case KBD_REVISION_OMAP5: |
| 305 | keypad_data->reg_offset = 0x10; |
| 306 | keypad_data->irqreg_offset = 0x0c; |
| 307 | break; |
| 308 | default: |
| 309 | dev_err(&pdev->dev, |
| 310 | "Keypad reports unsupported revision %d", rev); |
| 311 | error = -EINVAL; |
| 312 | goto err_pm_put_sync; |
| 313 | } |
| 314 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 315 | /* input device allocation */ |
| 316 | keypad_data->input = input_dev = input_allocate_device(); |
| 317 | if (!input_dev) { |
| 318 | error = -ENOMEM; |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 319 | goto err_pm_put_sync; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | input_dev->name = pdev->name; |
| 323 | input_dev->dev.parent = &pdev->dev; |
| 324 | input_dev->id.bustype = BUS_HOST; |
| 325 | input_dev->id.vendor = 0x0001; |
| 326 | input_dev->id.product = 0x0001; |
| 327 | input_dev->id.version = 0x0001; |
| 328 | |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 329 | input_dev->open = omap4_keypad_open; |
| 330 | input_dev->close = omap4_keypad_close; |
| 331 | |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 332 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
| 333 | if (!keypad_data->no_autorepeat) |
| 334 | __set_bit(EV_REP, input_dev->evbit); |
| 335 | |
| 336 | input_set_drvdata(input_dev, keypad_data); |
| 337 | |
| 338 | keypad_data->row_shift = get_count_order(keypad_data->cols); |
| 339 | max_keys = keypad_data->rows << keypad_data->row_shift; |
| 340 | keypad_data->keymap = kzalloc(max_keys * sizeof(keypad_data->keymap[0]), |
| 341 | GFP_KERNEL); |
| 342 | if (!keypad_data->keymap) { |
| 343 | dev_err(&pdev->dev, "Not enough memory for keymap\n"); |
| 344 | error = -ENOMEM; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 345 | goto err_free_input; |
| 346 | } |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 347 | |
Joachim Eastwood | 61721c8 | 2014-05-29 00:30:02 -0700 | [diff] [blame] | 348 | error = matrix_keypad_build_keymap(NULL, NULL, |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 349 | keypad_data->rows, keypad_data->cols, |
| 350 | keypad_data->keymap, input_dev); |
| 351 | if (error) { |
| 352 | dev_err(&pdev->dev, "failed to build keymap\n"); |
| 353 | goto err_free_keymap; |
| 354 | } |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 355 | |
Illia Smyrnov | c683da3 | 2013-08-25 23:25:50 -0700 | [diff] [blame] | 356 | error = request_threaded_irq(keypad_data->irq, omap4_keypad_irq_handler, |
Illia Smyrnov | 50f3c16 | 2013-08-25 23:32:45 -0700 | [diff] [blame] | 357 | omap4_keypad_irq_thread_fn, 0, |
Illia Smyrnov | c683da3 | 2013-08-25 23:25:50 -0700 | [diff] [blame] | 358 | "omap4-keypad", keypad_data); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 359 | if (error) { |
| 360 | dev_err(&pdev->dev, "failed to register interrupt\n"); |
| 361 | goto err_free_input; |
| 362 | } |
| 363 | |
Illia Smyrnov | 78608a0 | 2013-08-25 23:29:30 -0700 | [diff] [blame] | 364 | device_init_wakeup(&pdev->dev, true); |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 365 | pm_runtime_put_sync(&pdev->dev); |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 366 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 367 | error = input_register_device(keypad_data->input); |
| 368 | if (error < 0) { |
| 369 | dev_err(&pdev->dev, "failed to register input device\n"); |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 370 | goto err_pm_disable; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 373 | platform_set_drvdata(pdev, keypad_data); |
| 374 | return 0; |
| 375 | |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 376 | err_pm_disable: |
| 377 | pm_runtime_disable(&pdev->dev); |
Illia Smyrnov | 78608a0 | 2013-08-25 23:29:30 -0700 | [diff] [blame] | 378 | device_init_wakeup(&pdev->dev, false); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 379 | free_irq(keypad_data->irq, keypad_data); |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 380 | err_free_keymap: |
| 381 | kfree(keypad_data->keymap); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 382 | err_free_input: |
| 383 | input_free_device(input_dev); |
Poddar, Sourav | f77621c | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 384 | err_pm_put_sync: |
| 385 | pm_runtime_put_sync(&pdev->dev); |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 386 | err_unmap: |
| 387 | iounmap(keypad_data->base); |
| 388 | err_release_mem: |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 389 | release_mem_region(res->start, resource_size(res)); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 390 | err_free_keypad: |
| 391 | kfree(keypad_data); |
| 392 | return error; |
| 393 | } |
| 394 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 395 | static int omap4_keypad_remove(struct platform_device *pdev) |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 396 | { |
| 397 | struct omap4_keypad *keypad_data = platform_get_drvdata(pdev); |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 398 | struct resource *res; |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 399 | |
| 400 | free_irq(keypad_data->irq, keypad_data); |
Abraham Arce | 5ad567f | 2011-02-22 22:25:59 -0800 | [diff] [blame] | 401 | |
| 402 | pm_runtime_disable(&pdev->dev); |
| 403 | |
Illia Smyrnov | 78608a0 | 2013-08-25 23:29:30 -0700 | [diff] [blame] | 404 | device_init_wakeup(&pdev->dev, false); |
| 405 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 406 | input_unregister_device(keypad_data->input); |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 407 | |
| 408 | iounmap(keypad_data->base); |
| 409 | |
| 410 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 411 | release_mem_region(res->start, resource_size(res)); |
| 412 | |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 413 | kfree(keypad_data->keymap); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 414 | kfree(keypad_data); |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 415 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 419 | static const struct of_device_id omap_keypad_dt_match[] = { |
| 420 | { .compatible = "ti,omap4-keypad" }, |
| 421 | {}, |
| 422 | }; |
| 423 | MODULE_DEVICE_TABLE(of, omap_keypad_dt_match); |
Sourav Poddar | 1398743 | 2012-07-13 00:10:47 -0700 | [diff] [blame] | 424 | |
Illia Smyrnov | 78608a0 | 2013-08-25 23:29:30 -0700 | [diff] [blame] | 425 | #ifdef CONFIG_PM_SLEEP |
| 426 | static int omap4_keypad_suspend(struct device *dev) |
| 427 | { |
| 428 | struct platform_device *pdev = to_platform_device(dev); |
| 429 | struct omap4_keypad *keypad_data = platform_get_drvdata(pdev); |
| 430 | int error; |
| 431 | |
| 432 | if (device_may_wakeup(&pdev->dev)) { |
| 433 | error = enable_irq_wake(keypad_data->irq); |
| 434 | if (!error) |
| 435 | keypad_data->irq_wake_enabled = true; |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static int omap4_keypad_resume(struct device *dev) |
| 442 | { |
| 443 | struct platform_device *pdev = to_platform_device(dev); |
| 444 | struct omap4_keypad *keypad_data = platform_get_drvdata(pdev); |
| 445 | |
| 446 | if (device_may_wakeup(&pdev->dev) && keypad_data->irq_wake_enabled) { |
| 447 | disable_irq_wake(keypad_data->irq); |
| 448 | keypad_data->irq_wake_enabled = false; |
| 449 | } |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | #endif |
| 454 | |
| 455 | static SIMPLE_DEV_PM_OPS(omap4_keypad_pm_ops, |
| 456 | omap4_keypad_suspend, omap4_keypad_resume); |
| 457 | |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 458 | static struct platform_driver omap4_keypad_driver = { |
| 459 | .probe = omap4_keypad_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 460 | .remove = omap4_keypad_remove, |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 461 | .driver = { |
| 462 | .name = "omap4-keypad", |
Illia Smyrnov | 78608a0 | 2013-08-25 23:29:30 -0700 | [diff] [blame] | 463 | .pm = &omap4_keypad_pm_ops, |
Joachim Eastwood | 61721c8 | 2014-05-29 00:30:02 -0700 | [diff] [blame] | 464 | .of_match_table = omap_keypad_dt_match, |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 465 | }, |
| 466 | }; |
JJ Ding | 5146c84 | 2011-11-29 11:08:39 -0800 | [diff] [blame] | 467 | module_platform_driver(omap4_keypad_driver); |
Abraham Arce | a17f7955 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 468 | |
| 469 | MODULE_AUTHOR("Texas Instruments"); |
| 470 | MODULE_DESCRIPTION("OMAP4 Keypad Driver"); |
| 471 | MODULE_LICENSE("GPL"); |
| 472 | MODULE_ALIAS("platform:omap4-keypad"); |