Abraham Arce | a17f795 | 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> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/io.h> |
| 30 | #include <linux/input.h> |
| 31 | #include <linux/slab.h> |
| 32 | |
| 33 | #include <plat/omap4-keypad.h> |
| 34 | |
| 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 */ |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 54 | #define OMAP4_DEF_IRQENABLE_EVENTEN (1 << 0) |
| 55 | #define OMAP4_DEF_IRQENABLE_LONGKEY (1 << 1) |
| 56 | #define OMAP4_DEF_IRQENABLE_TIMEOUTEN (1 << 2) |
| 57 | #define OMAP4_DEF_CTRL_NOSOFTMODE (1 << 1) |
| 58 | #define OMAP4_DEF_CTRLPTVVALUE (1 << 2) |
| 59 | #define OMAP4_DEF_CTRLPTV (1 << 1) |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 60 | |
| 61 | /* OMAP4 values */ |
Abraham Arce | 05362f4 | 2010-09-29 23:36:48 -0700 | [diff] [blame^] | 62 | #define OMAP4_VAL_IRQDISABLE 0x00 |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 63 | #define OMAP4_VAL_DEBOUNCINGTIME 0x07 |
| 64 | #define OMAP4_VAL_FUNCTIONALCFG 0x1E |
| 65 | |
| 66 | #define OMAP4_MASK_IRQSTATUSDISABLE 0xFFFF |
| 67 | |
| 68 | struct omap4_keypad { |
| 69 | struct input_dev *input; |
| 70 | |
| 71 | void __iomem *base; |
| 72 | int irq; |
| 73 | |
| 74 | unsigned int rows; |
| 75 | unsigned int cols; |
| 76 | unsigned int row_shift; |
| 77 | unsigned char key_state[8]; |
| 78 | unsigned short keymap[]; |
| 79 | }; |
| 80 | |
| 81 | static void __devinit omap4_keypad_config(struct omap4_keypad *keypad_data) |
| 82 | { |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 83 | __raw_writel(OMAP4_VAL_FUNCTIONALCFG, |
| 84 | keypad_data->base + OMAP4_KBD_CTRL); |
| 85 | __raw_writel(OMAP4_VAL_DEBOUNCINGTIME, |
| 86 | keypad_data->base + OMAP4_KBD_DEBOUNCINGTIME); |
Abraham Arce | 05362f4 | 2010-09-29 23:36:48 -0700 | [diff] [blame^] | 87 | __raw_writel(OMAP4_VAL_IRQDISABLE, |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 88 | keypad_data->base + OMAP4_KBD_IRQSTATUS); |
| 89 | __raw_writel(OMAP4_DEF_IRQENABLE_EVENTEN | OMAP4_DEF_IRQENABLE_LONGKEY, |
| 90 | keypad_data->base + OMAP4_KBD_IRQENABLE); |
| 91 | } |
| 92 | |
| 93 | /* Interrupt handler */ |
| 94 | static irqreturn_t omap4_keypad_interrupt(int irq, void *dev_id) |
| 95 | { |
| 96 | struct omap4_keypad *keypad_data = dev_id; |
| 97 | struct input_dev *input_dev = keypad_data->input; |
| 98 | unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)]; |
| 99 | unsigned int col, row, code, changed; |
| 100 | u32 *new_state = (u32 *) key_state; |
| 101 | |
| 102 | /* Disable interrupts */ |
Abraham Arce | 05362f4 | 2010-09-29 23:36:48 -0700 | [diff] [blame^] | 103 | __raw_writel(OMAP4_VAL_IRQDISABLE, |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 104 | keypad_data->base + OMAP4_KBD_IRQENABLE); |
| 105 | |
| 106 | *new_state = __raw_readl(keypad_data->base + OMAP4_KBD_FULLCODE31_0); |
Abraham Arce | 05362f4 | 2010-09-29 23:36:48 -0700 | [diff] [blame^] | 107 | *(new_state + 1) = __raw_readl(keypad_data->base |
| 108 | + OMAP4_KBD_FULLCODE63_32); |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 109 | |
| 110 | for (row = 0; row < keypad_data->rows; row++) { |
| 111 | changed = key_state[row] ^ keypad_data->key_state[row]; |
| 112 | if (!changed) |
| 113 | continue; |
| 114 | |
| 115 | for (col = 0; col < keypad_data->cols; col++) { |
| 116 | if (changed & (1 << col)) { |
| 117 | code = MATRIX_SCAN_CODE(row, col, |
| 118 | keypad_data->row_shift); |
| 119 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
| 120 | input_report_key(input_dev, |
| 121 | keypad_data->keymap[code], |
| 122 | key_state[row] & (1 << col)); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | input_sync(input_dev); |
| 128 | |
| 129 | memcpy(keypad_data->key_state, key_state, |
| 130 | sizeof(keypad_data->key_state)); |
| 131 | |
| 132 | /* clear pending interrupts */ |
| 133 | __raw_writel(__raw_readl(keypad_data->base + OMAP4_KBD_IRQSTATUS), |
| 134 | keypad_data->base + OMAP4_KBD_IRQSTATUS); |
| 135 | |
| 136 | /* enable interrupts */ |
| 137 | __raw_writel(OMAP4_DEF_IRQENABLE_EVENTEN | OMAP4_DEF_IRQENABLE_LONGKEY, |
| 138 | keypad_data->base + OMAP4_KBD_IRQENABLE); |
| 139 | |
| 140 | return IRQ_HANDLED; |
| 141 | } |
| 142 | |
| 143 | static int __devinit omap4_keypad_probe(struct platform_device *pdev) |
| 144 | { |
| 145 | const struct omap4_keypad_platform_data *pdata; |
| 146 | struct omap4_keypad *keypad_data; |
| 147 | struct input_dev *input_dev; |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 148 | struct resource *res; |
| 149 | resource_size_t size; |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 150 | unsigned int row_shift, max_keys; |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 151 | int irq; |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 152 | int error; |
| 153 | |
| 154 | /* platform data */ |
| 155 | pdata = pdev->dev.platform_data; |
| 156 | if (!pdata) { |
| 157 | dev_err(&pdev->dev, "no platform data defined\n"); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 161 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 162 | if (!res) { |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 163 | dev_err(&pdev->dev, "no base address specified\n"); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 167 | irq = platform_get_irq(pdev, 0); |
| 168 | if (!irq) { |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 169 | dev_err(&pdev->dev, "no keyboard irq assigned\n"); |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| 173 | if (!pdata->keymap_data) { |
| 174 | dev_err(&pdev->dev, "no keymap data defined\n"); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | |
| 178 | row_shift = get_count_order(pdata->cols); |
| 179 | max_keys = pdata->rows << row_shift; |
| 180 | |
| 181 | keypad_data = kzalloc(sizeof(struct omap4_keypad) + |
| 182 | max_keys * sizeof(keypad_data->keymap[0]), |
| 183 | GFP_KERNEL); |
| 184 | if (!keypad_data) { |
| 185 | dev_err(&pdev->dev, "keypad_data memory allocation failed\n"); |
| 186 | return -ENOMEM; |
| 187 | } |
| 188 | |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 189 | size = resource_size(res); |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 190 | |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 191 | res = request_mem_region(res->start, size, pdev->name); |
| 192 | if (!res) { |
| 193 | dev_err(&pdev->dev, "can't request mem region\n"); |
| 194 | error = -EBUSY; |
| 195 | goto err_free_keypad; |
| 196 | } |
| 197 | |
| 198 | keypad_data->base = ioremap(res->start, resource_size(res)); |
| 199 | if (!keypad_data->base) { |
| 200 | dev_err(&pdev->dev, "can't ioremap mem resource\n"); |
| 201 | error = -ENOMEM; |
| 202 | goto err_release_mem; |
| 203 | } |
| 204 | |
| 205 | keypad_data->irq = irq; |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 206 | keypad_data->row_shift = row_shift; |
| 207 | keypad_data->rows = pdata->rows; |
| 208 | keypad_data->cols = pdata->cols; |
| 209 | |
| 210 | /* input device allocation */ |
| 211 | keypad_data->input = input_dev = input_allocate_device(); |
| 212 | if (!input_dev) { |
| 213 | error = -ENOMEM; |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 214 | goto err_unmap; |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | input_dev->name = pdev->name; |
| 218 | input_dev->dev.parent = &pdev->dev; |
| 219 | input_dev->id.bustype = BUS_HOST; |
| 220 | input_dev->id.vendor = 0x0001; |
| 221 | input_dev->id.product = 0x0001; |
| 222 | input_dev->id.version = 0x0001; |
| 223 | |
| 224 | input_dev->keycode = keypad_data->keymap; |
| 225 | input_dev->keycodesize = sizeof(keypad_data->keymap[0]); |
| 226 | input_dev->keycodemax = max_keys; |
| 227 | |
| 228 | __set_bit(EV_KEY, input_dev->evbit); |
| 229 | __set_bit(EV_REP, input_dev->evbit); |
| 230 | |
| 231 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
| 232 | |
| 233 | input_set_drvdata(input_dev, keypad_data); |
| 234 | |
| 235 | matrix_keypad_build_keymap(pdata->keymap_data, row_shift, |
| 236 | input_dev->keycode, input_dev->keybit); |
| 237 | |
| 238 | omap4_keypad_config(keypad_data); |
| 239 | |
| 240 | error = request_irq(keypad_data->irq, omap4_keypad_interrupt, |
| 241 | IRQF_TRIGGER_FALLING, |
| 242 | "omap4-keypad", keypad_data); |
| 243 | if (error) { |
| 244 | dev_err(&pdev->dev, "failed to register interrupt\n"); |
| 245 | goto err_free_input; |
| 246 | } |
| 247 | |
| 248 | error = input_register_device(keypad_data->input); |
| 249 | if (error < 0) { |
| 250 | dev_err(&pdev->dev, "failed to register input device\n"); |
| 251 | goto err_free_irq; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | platform_set_drvdata(pdev, keypad_data); |
| 256 | return 0; |
| 257 | |
| 258 | err_free_irq: |
| 259 | free_irq(keypad_data->irq, keypad_data); |
| 260 | err_free_input: |
| 261 | input_free_device(input_dev); |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 262 | err_unmap: |
| 263 | iounmap(keypad_data->base); |
| 264 | err_release_mem: |
| 265 | release_mem_region(res->start, size); |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 266 | err_free_keypad: |
| 267 | kfree(keypad_data); |
| 268 | return error; |
| 269 | } |
| 270 | |
| 271 | static int __devexit omap4_keypad_remove(struct platform_device *pdev) |
| 272 | { |
| 273 | struct omap4_keypad *keypad_data = platform_get_drvdata(pdev); |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 274 | struct resource *res; |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 275 | |
| 276 | free_irq(keypad_data->irq, keypad_data); |
| 277 | input_unregister_device(keypad_data->input); |
Abraham Arce | f3a1ba6 | 2010-09-29 23:35:57 -0700 | [diff] [blame] | 278 | |
| 279 | iounmap(keypad_data->base); |
| 280 | |
| 281 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 282 | release_mem_region(res->start, resource_size(res)); |
| 283 | |
Abraham Arce | a17f795 | 2010-08-31 17:05:27 -0700 | [diff] [blame] | 284 | kfree(keypad_data); |
| 285 | platform_set_drvdata(pdev, NULL); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static struct platform_driver omap4_keypad_driver = { |
| 291 | .probe = omap4_keypad_probe, |
| 292 | .remove = __devexit_p(omap4_keypad_remove), |
| 293 | .driver = { |
| 294 | .name = "omap4-keypad", |
| 295 | .owner = THIS_MODULE, |
| 296 | }, |
| 297 | }; |
| 298 | |
| 299 | static int __init omap4_keypad_init(void) |
| 300 | { |
| 301 | return platform_driver_register(&omap4_keypad_driver); |
| 302 | } |
| 303 | module_init(omap4_keypad_init); |
| 304 | |
| 305 | static void __exit omap4_keypad_exit(void) |
| 306 | { |
| 307 | platform_driver_unregister(&omap4_keypad_driver); |
| 308 | } |
| 309 | module_exit(omap4_keypad_exit); |
| 310 | |
| 311 | MODULE_AUTHOR("Texas Instruments"); |
| 312 | MODULE_DESCRIPTION("OMAP4 Keypad Driver"); |
| 313 | MODULE_LICENSE("GPL"); |
| 314 | MODULE_ALIAS("platform:omap4-keypad"); |