|
| 1 | +/* |
| 2 | + * Texas Instruments TNETV107X Keypad Driver |
| 3 | + * |
| 4 | + * Copyright (C) 2010 Texas Instruments |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU General Public License as |
| 8 | + * published by the Free Software Foundation version 2. |
| 9 | + * |
| 10 | + * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 11 | + * kind, whether express or implied; without even the implied warranty |
| 12 | + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + */ |
| 15 | + |
| 16 | +#include <linux/kernel.h> |
| 17 | +#include <linux/errno.h> |
| 18 | +#include <linux/input.h> |
| 19 | +#include <linux/platform_device.h> |
| 20 | +#include <linux/interrupt.h> |
| 21 | +#include <linux/slab.h> |
| 22 | +#include <linux/delay.h> |
| 23 | +#include <linux/io.h> |
| 24 | +#include <linux/clk.h> |
| 25 | +#include <linux/input/matrix_keypad.h> |
| 26 | + |
| 27 | +#define BITS(x) (BIT(x) - 1) |
| 28 | + |
| 29 | +#define KEYPAD_ROWS 9 |
| 30 | +#define KEYPAD_COLS 9 |
| 31 | + |
| 32 | +#define DEBOUNCE_MIN 0x400ul |
| 33 | +#define DEBOUNCE_MAX 0x3ffffffful |
| 34 | + |
| 35 | +struct keypad_regs { |
| 36 | + u32 rev; |
| 37 | + u32 mode; |
| 38 | + u32 mask; |
| 39 | + u32 pol; |
| 40 | + u32 dclock; |
| 41 | + u32 rclock; |
| 42 | + u32 stable_cnt; |
| 43 | + u32 in_en; |
| 44 | + u32 out; |
| 45 | + u32 out_en; |
| 46 | + u32 in; |
| 47 | + u32 lock; |
| 48 | + u32 pres[3]; |
| 49 | +}; |
| 50 | + |
| 51 | +#define keypad_read(kp, reg) __raw_readl(&(kp)->regs->reg) |
| 52 | +#define keypad_write(kp, reg, val) __raw_writel(val, &(kp)->regs->reg) |
| 53 | + |
| 54 | +struct keypad_data { |
| 55 | + struct input_dev *input_dev; |
| 56 | + struct resource *res; |
| 57 | + struct keypad_regs __iomem *regs; |
| 58 | + struct clk *clk; |
| 59 | + struct device *dev; |
| 60 | + spinlock_t lock; |
| 61 | + u32 irq_press; |
| 62 | + u32 irq_release; |
| 63 | + int rows, cols, row_shift; |
| 64 | + int debounce_ms, active_low; |
| 65 | + u32 prev_keys[3]; |
| 66 | + unsigned short keycodes[]; |
| 67 | +}; |
| 68 | + |
| 69 | +static irqreturn_t keypad_irq(int irq, void *data) |
| 70 | +{ |
| 71 | + struct keypad_data *kp = data; |
| 72 | + int i, bit, val, row, col, code; |
| 73 | + unsigned long flags; |
| 74 | + u32 curr_keys[3]; |
| 75 | + u32 change; |
| 76 | + |
| 77 | + spin_lock_irqsave(&kp->lock, flags); |
| 78 | + |
| 79 | + memset(curr_keys, 0, sizeof(curr_keys)); |
| 80 | + if (irq == kp->irq_press) |
| 81 | + for (i = 0; i < 3; i++) |
| 82 | + curr_keys[i] = keypad_read(kp, pres[i]); |
| 83 | + |
| 84 | + for (i = 0; i < 3; i++) { |
| 85 | + change = curr_keys[i] ^ kp->prev_keys[i]; |
| 86 | + |
| 87 | + while (change) { |
| 88 | + bit = fls(change) - 1; |
| 89 | + change ^= BIT(bit); |
| 90 | + val = curr_keys[i] & BIT(bit); |
| 91 | + bit += i * 32; |
| 92 | + row = bit / KEYPAD_COLS; |
| 93 | + col = bit % KEYPAD_COLS; |
| 94 | + |
| 95 | + code = MATRIX_SCAN_CODE(row, col, kp->row_shift); |
| 96 | + input_event(kp->input_dev, EV_MSC, MSC_SCAN, code); |
| 97 | + input_report_key(kp->input_dev, kp->keycodes[code], |
| 98 | + val); |
| 99 | + } |
| 100 | + } |
| 101 | + input_sync(kp->input_dev); |
| 102 | + memcpy(kp->prev_keys, curr_keys, sizeof(curr_keys)); |
| 103 | + |
| 104 | + if (irq == kp->irq_press) |
| 105 | + keypad_write(kp, lock, 0); /* Allow hardware updates */ |
| 106 | + |
| 107 | + spin_unlock_irqrestore(&kp->lock, flags); |
| 108 | + |
| 109 | + return IRQ_HANDLED; |
| 110 | +} |
| 111 | + |
| 112 | +static int keypad_start(struct input_dev *dev) |
| 113 | +{ |
| 114 | + struct keypad_data *kp = input_get_drvdata(dev); |
| 115 | + unsigned long mask, debounce, clk_rate_khz; |
| 116 | + unsigned long flags; |
| 117 | + |
| 118 | + clk_enable(kp->clk); |
| 119 | + clk_rate_khz = clk_get_rate(kp->clk) / 1000; |
| 120 | + |
| 121 | + spin_lock_irqsave(&kp->lock, flags); |
| 122 | + |
| 123 | + /* Initialize device registers */ |
| 124 | + keypad_write(kp, mode, 0); |
| 125 | + |
| 126 | + mask = BITS(kp->rows) << KEYPAD_COLS; |
| 127 | + mask |= BITS(kp->cols); |
| 128 | + keypad_write(kp, mask, ~mask); |
| 129 | + |
| 130 | + keypad_write(kp, pol, kp->active_low ? 0 : 0x3ffff); |
| 131 | + keypad_write(kp, stable_cnt, 3); |
| 132 | + |
| 133 | + debounce = kp->debounce_ms * clk_rate_khz; |
| 134 | + debounce = clamp(debounce, DEBOUNCE_MIN, DEBOUNCE_MAX); |
| 135 | + keypad_write(kp, dclock, debounce); |
| 136 | + keypad_write(kp, rclock, 4 * debounce); |
| 137 | + |
| 138 | + keypad_write(kp, in_en, 1); |
| 139 | + |
| 140 | + spin_unlock_irqrestore(&kp->lock, flags); |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
| 145 | +static void keypad_stop(struct input_dev *dev) |
| 146 | +{ |
| 147 | + struct keypad_data *kp = input_get_drvdata(dev); |
| 148 | + |
| 149 | + synchronize_irq(kp->irq_press); |
| 150 | + synchronize_irq(kp->irq_release); |
| 151 | + clk_disable(kp->clk); |
| 152 | +} |
| 153 | + |
| 154 | +static int __devinit keypad_probe(struct platform_device *pdev) |
| 155 | +{ |
| 156 | + const struct matrix_keypad_platform_data *pdata; |
| 157 | + const struct matrix_keymap_data *keymap_data; |
| 158 | + struct device *dev = &pdev->dev; |
| 159 | + struct keypad_data *kp; |
| 160 | + int error = 0, sz, row_shift; |
| 161 | + u32 rev = 0; |
| 162 | + |
| 163 | + pdata = pdev->dev.platform_data; |
| 164 | + if (!pdata) { |
| 165 | + dev_err(dev, "cannot find device data\n"); |
| 166 | + return -EINVAL; |
| 167 | + } |
| 168 | + |
| 169 | + keymap_data = pdata->keymap_data; |
| 170 | + if (!keymap_data) { |
| 171 | + dev_err(dev, "cannot find keymap data\n"); |
| 172 | + return -EINVAL; |
| 173 | + } |
| 174 | + |
| 175 | + row_shift = get_count_order(pdata->num_col_gpios); |
| 176 | + sz = offsetof(struct keypad_data, keycodes); |
| 177 | + sz += (pdata->num_row_gpios << row_shift) * sizeof(kp->keycodes[0]); |
| 178 | + kp = kzalloc(sz, GFP_KERNEL); |
| 179 | + if (!kp) { |
| 180 | + dev_err(dev, "cannot allocate device info\n"); |
| 181 | + return -ENOMEM; |
| 182 | + } |
| 183 | + |
| 184 | + kp->dev = dev; |
| 185 | + kp->rows = pdata->num_row_gpios; |
| 186 | + kp->cols = pdata->num_col_gpios; |
| 187 | + kp->row_shift = row_shift; |
| 188 | + platform_set_drvdata(pdev, kp); |
| 189 | + spin_lock_init(&kp->lock); |
| 190 | + |
| 191 | + kp->irq_press = platform_get_irq_byname(pdev, "press"); |
| 192 | + kp->irq_release = platform_get_irq_byname(pdev, "release"); |
| 193 | + if (kp->irq_press < 0 || kp->irq_release < 0) { |
| 194 | + dev_err(dev, "cannot determine device interrupts\n"); |
| 195 | + error = -ENODEV; |
| 196 | + goto error_res; |
| 197 | + } |
| 198 | + |
| 199 | + kp->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 200 | + if (!kp->res) { |
| 201 | + dev_err(dev, "cannot determine register area\n"); |
| 202 | + error = -ENODEV; |
| 203 | + goto error_res; |
| 204 | + } |
| 205 | + |
| 206 | + if (!request_mem_region(kp->res->start, resource_size(kp->res), |
| 207 | + pdev->name)) { |
| 208 | + dev_err(dev, "cannot claim register memory\n"); |
| 209 | + kp->res = NULL; |
| 210 | + error = -EINVAL; |
| 211 | + goto error_res; |
| 212 | + } |
| 213 | + |
| 214 | + kp->regs = ioremap(kp->res->start, resource_size(kp->res)); |
| 215 | + if (!kp->regs) { |
| 216 | + dev_err(dev, "cannot map register memory\n"); |
| 217 | + error = -ENOMEM; |
| 218 | + goto error_map; |
| 219 | + } |
| 220 | + |
| 221 | + kp->clk = clk_get(dev, NULL); |
| 222 | + if (!kp->clk) { |
| 223 | + dev_err(dev, "cannot claim device clock\n"); |
| 224 | + error = -EINVAL; |
| 225 | + goto error_clk; |
| 226 | + } |
| 227 | + |
| 228 | + error = request_threaded_irq(kp->irq_press, NULL, keypad_irq, 0, |
| 229 | + dev_name(dev), kp); |
| 230 | + if (error < 0) { |
| 231 | + dev_err(kp->dev, "Could not allocate keypad press key irq\n"); |
| 232 | + goto error_irq_press; |
| 233 | + } |
| 234 | + |
| 235 | + error = request_threaded_irq(kp->irq_release, NULL, keypad_irq, 0, |
| 236 | + dev_name(dev), kp); |
| 237 | + if (error < 0) { |
| 238 | + dev_err(kp->dev, "Could not allocate keypad release key irq\n"); |
| 239 | + goto error_irq_release; |
| 240 | + } |
| 241 | + |
| 242 | + kp->input_dev = input_allocate_device(); |
| 243 | + if (!kp->input_dev) { |
| 244 | + dev_err(dev, "cannot allocate input device\n"); |
| 245 | + error = -ENOMEM; |
| 246 | + goto error_input; |
| 247 | + } |
| 248 | + input_set_drvdata(kp->input_dev, kp); |
| 249 | + |
| 250 | + kp->input_dev->name = pdev->name; |
| 251 | + kp->input_dev->dev.parent = &pdev->dev; |
| 252 | + kp->input_dev->open = keypad_start; |
| 253 | + kp->input_dev->close = keypad_stop; |
| 254 | + kp->input_dev->evbit[0] = BIT_MASK(EV_KEY); |
| 255 | + if (!pdata->no_autorepeat) |
| 256 | + kp->input_dev->evbit[0] |= BIT_MASK(EV_REP); |
| 257 | + |
| 258 | + clk_enable(kp->clk); |
| 259 | + rev = keypad_read(kp, rev); |
| 260 | + kp->input_dev->id.bustype = BUS_HOST; |
| 261 | + kp->input_dev->id.product = ((rev >> 8) & 0x07); |
| 262 | + kp->input_dev->id.version = ((rev >> 16) & 0xfff); |
| 263 | + clk_disable(kp->clk); |
| 264 | + |
| 265 | + kp->input_dev->keycode = kp->keycodes; |
| 266 | + kp->input_dev->keycodesize = sizeof(kp->keycodes[0]); |
| 267 | + kp->input_dev->keycodemax = kp->rows << kp->row_shift; |
| 268 | + |
| 269 | + matrix_keypad_build_keymap(keymap_data, kp->row_shift, kp->keycodes, |
| 270 | + kp->input_dev->keybit); |
| 271 | + |
| 272 | + input_set_capability(kp->input_dev, EV_MSC, MSC_SCAN); |
| 273 | + |
| 274 | + error = input_register_device(kp->input_dev); |
| 275 | + if (error < 0) { |
| 276 | + dev_err(dev, "Could not register input device\n"); |
| 277 | + goto error_reg; |
| 278 | + } |
| 279 | + |
| 280 | + return 0; |
| 281 | + |
| 282 | + |
| 283 | +error_reg: |
| 284 | + input_free_device(kp->input_dev); |
| 285 | +error_input: |
| 286 | + free_irq(kp->irq_release, kp); |
| 287 | +error_irq_release: |
| 288 | + free_irq(kp->irq_press, kp); |
| 289 | +error_irq_press: |
| 290 | + clk_put(kp->clk); |
| 291 | +error_clk: |
| 292 | + iounmap(kp->regs); |
| 293 | +error_map: |
| 294 | + release_mem_region(kp->res->start, resource_size(kp->res)); |
| 295 | +error_res: |
| 296 | + platform_set_drvdata(pdev, NULL); |
| 297 | + kfree(kp); |
| 298 | + return error; |
| 299 | +} |
| 300 | + |
| 301 | +static int __devexit keypad_remove(struct platform_device *pdev) |
| 302 | +{ |
| 303 | + struct keypad_data *kp = platform_get_drvdata(pdev); |
| 304 | + |
| 305 | + free_irq(kp->irq_press, kp); |
| 306 | + free_irq(kp->irq_release, kp); |
| 307 | + input_unregister_device(kp->input_dev); |
| 308 | + clk_put(kp->clk); |
| 309 | + iounmap(kp->regs); |
| 310 | + release_mem_region(kp->res->start, resource_size(kp->res)); |
| 311 | + platform_set_drvdata(pdev, NULL); |
| 312 | + kfree(kp); |
| 313 | + |
| 314 | + return 0; |
| 315 | +} |
| 316 | + |
| 317 | +static struct platform_driver keypad_driver = { |
| 318 | + .probe = keypad_probe, |
| 319 | + .remove = __devexit_p(keypad_remove), |
| 320 | + .driver.name = "tnetv107x-keypad", |
| 321 | + .driver.owner = THIS_MODULE, |
| 322 | +}; |
| 323 | + |
| 324 | +static int __init keypad_init(void) |
| 325 | +{ |
| 326 | + return platform_driver_register(&keypad_driver); |
| 327 | +} |
| 328 | + |
| 329 | +static void __exit keypad_exit(void) |
| 330 | +{ |
| 331 | + platform_driver_unregister(&keypad_driver); |
| 332 | +} |
| 333 | + |
| 334 | +module_init(keypad_init); |
| 335 | +module_exit(keypad_exit); |
| 336 | + |
| 337 | +MODULE_AUTHOR("Cyril Chemparathy"); |
| 338 | +MODULE_DESCRIPTION("TNETV107X Keypad Driver"); |
| 339 | +MODULE_ALIAS("platform: tnetv107x-keypad"); |
| 340 | +MODULE_LICENSE("GPL"); |
0 commit comments