0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/platform_device.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/spinlock.h>
0015 #include <linux/io.h>
0016 #include <linux/delay.h>
0017 #include <linux/input.h>
0018 #include <linux/slab.h>
0019 #include <linux/clk.h>
0020 #include <linux/module.h>
0021
0022 #include <linux/platform_data/keypad-nomadik-ske.h>
0023
0024
0025 #define SKE_KPMLT (0x1 << 6)
0026 #define SKE_KPCN (0x7 << 3)
0027 #define SKE_KPASEN (0x1 << 2)
0028 #define SKE_KPASON (0x1 << 7)
0029
0030
0031 #define SKE_KPIMA (0x1 << 2)
0032
0033
0034 #define SKE_KPICS (0x1 << 3)
0035 #define SKE_KPICA (0x1 << 2)
0036
0037
0038 #define SKE_KPRISA (0x1 << 2)
0039
0040 #define SKE_KEYPAD_ROW_SHIFT 3
0041 #define SKE_KPD_NUM_ROWS 8
0042 #define SKE_KPD_NUM_COLS 8
0043
0044
0045 #define SKE_ASR0 0x20
0046 #define SKE_ASR1 0x24
0047 #define SKE_ASR2 0x28
0048 #define SKE_ASR3 0x2C
0049
0050 #define SKE_NUM_ASRX_REGISTERS (4)
0051 #define KEY_PRESSED_DELAY 10
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 struct ske_keypad {
0065 int irq;
0066 void __iomem *reg_base;
0067 struct input_dev *input;
0068 const struct ske_keypad_platform_data *board;
0069 unsigned short keymap[SKE_KPD_NUM_ROWS * SKE_KPD_NUM_COLS];
0070 struct clk *clk;
0071 struct clk *pclk;
0072 spinlock_t ske_keypad_lock;
0073 };
0074
0075 static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr,
0076 u8 mask, u8 data)
0077 {
0078 u32 ret;
0079
0080 spin_lock(&keypad->ske_keypad_lock);
0081
0082 ret = readl(keypad->reg_base + addr);
0083 ret &= ~mask;
0084 ret |= data;
0085 writel(ret, keypad->reg_base + addr);
0086
0087 spin_unlock(&keypad->ske_keypad_lock);
0088 }
0089
0090
0091
0092
0093
0094
0095 static int __init ske_keypad_chip_init(struct ske_keypad *keypad)
0096 {
0097 u32 value;
0098 int timeout = keypad->board->debounce_ms;
0099
0100
0101 while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--)
0102 cpu_relax();
0103
0104 if (timeout == -1)
0105 return -EINVAL;
0106
0107
0108
0109
0110
0111
0112 spin_lock(&keypad->ske_keypad_lock);
0113 value = readl(keypad->reg_base + SKE_DBCR);
0114 value = value & 0xff;
0115 value |= ((keypad->board->debounce_ms * 32000)/32768) << 8;
0116 writel(value, keypad->reg_base + SKE_DBCR);
0117 spin_unlock(&keypad->ske_keypad_lock);
0118
0119
0120 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT);
0121
0122
0123
0124
0125
0126 value = (keypad->board->kcol - 1) << 3;
0127 ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value);
0128
0129
0130 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS);
0131
0132
0133 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
0134
0135
0136 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN);
0137
0138 return 0;
0139 }
0140
0141 static void ske_keypad_report(struct ske_keypad *keypad, u8 status, int col)
0142 {
0143 int row = 0, code, pos;
0144 struct input_dev *input = keypad->input;
0145 u32 ske_ris;
0146 int key_pressed;
0147 int num_of_rows;
0148
0149
0150 num_of_rows = hweight8(status);
0151 do {
0152 pos = __ffs(status);
0153 row = pos;
0154 status &= ~(1 << pos);
0155
0156 code = MATRIX_SCAN_CODE(row, col, SKE_KEYPAD_ROW_SHIFT);
0157 ske_ris = readl(keypad->reg_base + SKE_RIS);
0158 key_pressed = ske_ris & SKE_KPRISA;
0159
0160 input_event(input, EV_MSC, MSC_SCAN, code);
0161 input_report_key(input, keypad->keymap[code], key_pressed);
0162 input_sync(input);
0163 num_of_rows--;
0164 } while (num_of_rows);
0165 }
0166
0167 static void ske_keypad_read_data(struct ske_keypad *keypad)
0168 {
0169 u8 status;
0170 int col = 0;
0171 int ske_asr, i;
0172
0173
0174
0175
0176
0177
0178
0179
0180 for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) {
0181 ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i));
0182 if (!ske_asr)
0183 continue;
0184
0185
0186 status = ske_asr & 0xff;
0187 if (status) {
0188 col = i * 2;
0189 ske_keypad_report(keypad, status, col);
0190 }
0191 status = (ske_asr & 0xff00) >> 8;
0192 if (status) {
0193 col = (i * 2) + 1;
0194 ske_keypad_report(keypad, status, col);
0195 }
0196 }
0197 }
0198
0199 static irqreturn_t ske_keypad_irq(int irq, void *dev_id)
0200 {
0201 struct ske_keypad *keypad = dev_id;
0202 int timeout = keypad->board->debounce_ms;
0203
0204
0205 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
0206 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA);
0207
0208 while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --timeout)
0209 cpu_relax();
0210
0211
0212 ske_keypad_read_data(keypad);
0213
0214
0215 while ((readl(keypad->reg_base + SKE_RIS)) && --timeout)
0216 msleep(KEY_PRESSED_DELAY);
0217
0218
0219 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
0220
0221 return IRQ_HANDLED;
0222 }
0223
0224 static int __init ske_keypad_probe(struct platform_device *pdev)
0225 {
0226 const struct ske_keypad_platform_data *plat =
0227 dev_get_platdata(&pdev->dev);
0228 struct ske_keypad *keypad;
0229 struct input_dev *input;
0230 struct resource *res;
0231 int irq;
0232 int error;
0233
0234 if (!plat) {
0235 dev_err(&pdev->dev, "invalid keypad platform data\n");
0236 return -EINVAL;
0237 }
0238
0239 irq = platform_get_irq(pdev, 0);
0240 if (irq < 0)
0241 return -EINVAL;
0242
0243 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0244 if (!res) {
0245 dev_err(&pdev->dev, "missing platform resources\n");
0246 return -EINVAL;
0247 }
0248
0249 keypad = kzalloc(sizeof(struct ske_keypad), GFP_KERNEL);
0250 input = input_allocate_device();
0251 if (!keypad || !input) {
0252 dev_err(&pdev->dev, "failed to allocate keypad memory\n");
0253 error = -ENOMEM;
0254 goto err_free_mem;
0255 }
0256
0257 keypad->irq = irq;
0258 keypad->board = plat;
0259 keypad->input = input;
0260 spin_lock_init(&keypad->ske_keypad_lock);
0261
0262 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
0263 dev_err(&pdev->dev, "failed to request I/O memory\n");
0264 error = -EBUSY;
0265 goto err_free_mem;
0266 }
0267
0268 keypad->reg_base = ioremap(res->start, resource_size(res));
0269 if (!keypad->reg_base) {
0270 dev_err(&pdev->dev, "failed to remap I/O memory\n");
0271 error = -ENXIO;
0272 goto err_free_mem_region;
0273 }
0274
0275 keypad->pclk = clk_get(&pdev->dev, "apb_pclk");
0276 if (IS_ERR(keypad->pclk)) {
0277 dev_err(&pdev->dev, "failed to get pclk\n");
0278 error = PTR_ERR(keypad->pclk);
0279 goto err_iounmap;
0280 }
0281
0282 keypad->clk = clk_get(&pdev->dev, NULL);
0283 if (IS_ERR(keypad->clk)) {
0284 dev_err(&pdev->dev, "failed to get clk\n");
0285 error = PTR_ERR(keypad->clk);
0286 goto err_pclk;
0287 }
0288
0289 input->id.bustype = BUS_HOST;
0290 input->name = "ux500-ske-keypad";
0291 input->dev.parent = &pdev->dev;
0292
0293 error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
0294 SKE_KPD_NUM_ROWS, SKE_KPD_NUM_COLS,
0295 keypad->keymap, input);
0296 if (error) {
0297 dev_err(&pdev->dev, "Failed to build keymap\n");
0298 goto err_clk;
0299 }
0300
0301 input_set_capability(input, EV_MSC, MSC_SCAN);
0302 if (!plat->no_autorepeat)
0303 __set_bit(EV_REP, input->evbit);
0304
0305 error = clk_prepare_enable(keypad->pclk);
0306 if (error) {
0307 dev_err(&pdev->dev, "Failed to prepare/enable pclk\n");
0308 goto err_clk;
0309 }
0310
0311 error = clk_prepare_enable(keypad->clk);
0312 if (error) {
0313 dev_err(&pdev->dev, "Failed to prepare/enable clk\n");
0314 goto err_pclk_disable;
0315 }
0316
0317
0318
0319 if (keypad->board->init)
0320 keypad->board->init();
0321
0322 error = ske_keypad_chip_init(keypad);
0323 if (error) {
0324 dev_err(&pdev->dev, "unable to init keypad hardware\n");
0325 goto err_clk_disable;
0326 }
0327
0328 error = request_threaded_irq(keypad->irq, NULL, ske_keypad_irq,
0329 IRQF_ONESHOT, "ske-keypad", keypad);
0330 if (error) {
0331 dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq);
0332 goto err_clk_disable;
0333 }
0334
0335 error = input_register_device(input);
0336 if (error) {
0337 dev_err(&pdev->dev,
0338 "unable to register input device: %d\n", error);
0339 goto err_free_irq;
0340 }
0341
0342 if (plat->wakeup_enable)
0343 device_init_wakeup(&pdev->dev, true);
0344
0345 platform_set_drvdata(pdev, keypad);
0346
0347 return 0;
0348
0349 err_free_irq:
0350 free_irq(keypad->irq, keypad);
0351 err_clk_disable:
0352 clk_disable_unprepare(keypad->clk);
0353 err_pclk_disable:
0354 clk_disable_unprepare(keypad->pclk);
0355 err_clk:
0356 clk_put(keypad->clk);
0357 err_pclk:
0358 clk_put(keypad->pclk);
0359 err_iounmap:
0360 iounmap(keypad->reg_base);
0361 err_free_mem_region:
0362 release_mem_region(res->start, resource_size(res));
0363 err_free_mem:
0364 input_free_device(input);
0365 kfree(keypad);
0366 return error;
0367 }
0368
0369 static int ske_keypad_remove(struct platform_device *pdev)
0370 {
0371 struct ske_keypad *keypad = platform_get_drvdata(pdev);
0372 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0373
0374 free_irq(keypad->irq, keypad);
0375
0376 input_unregister_device(keypad->input);
0377
0378 clk_disable_unprepare(keypad->clk);
0379 clk_put(keypad->clk);
0380
0381 if (keypad->board->exit)
0382 keypad->board->exit();
0383
0384 iounmap(keypad->reg_base);
0385 release_mem_region(res->start, resource_size(res));
0386 kfree(keypad);
0387
0388 return 0;
0389 }
0390
0391 #ifdef CONFIG_PM_SLEEP
0392 static int ske_keypad_suspend(struct device *dev)
0393 {
0394 struct platform_device *pdev = to_platform_device(dev);
0395 struct ske_keypad *keypad = platform_get_drvdata(pdev);
0396 int irq = platform_get_irq(pdev, 0);
0397
0398 if (device_may_wakeup(dev))
0399 enable_irq_wake(irq);
0400 else
0401 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
0402
0403 return 0;
0404 }
0405
0406 static int ske_keypad_resume(struct device *dev)
0407 {
0408 struct platform_device *pdev = to_platform_device(dev);
0409 struct ske_keypad *keypad = platform_get_drvdata(pdev);
0410 int irq = platform_get_irq(pdev, 0);
0411
0412 if (device_may_wakeup(dev))
0413 disable_irq_wake(irq);
0414 else
0415 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
0416
0417 return 0;
0418 }
0419 #endif
0420
0421 static SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops,
0422 ske_keypad_suspend, ske_keypad_resume);
0423
0424 static struct platform_driver ske_keypad_driver = {
0425 .driver = {
0426 .name = "nmk-ske-keypad",
0427 .pm = &ske_keypad_dev_pm_ops,
0428 },
0429 .remove = ske_keypad_remove,
0430 };
0431
0432 module_platform_driver_probe(ske_keypad_driver, ske_keypad_probe);
0433
0434 MODULE_LICENSE("GPL v2");
0435 MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
0436 MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
0437 MODULE_ALIAS("platform:nomadik-ske-keypad");