Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008-2009 Nuvoton technology corporation. |
| 3 | * |
| 4 | * Wan ZongShun <mcuos.com@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation;version 2 of the License. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/input.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/clk.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 23 | |
| 24 | #include <mach/w90p910_keypad.h> |
| 25 | |
| 26 | /* Keypad Interface Control Registers */ |
| 27 | #define KPI_CONF 0x00 |
| 28 | #define KPI_3KCONF 0x04 |
| 29 | #define KPI_LPCONF 0x08 |
| 30 | #define KPI_STATUS 0x0C |
| 31 | |
| 32 | #define IS1KEY (0x01 << 16) |
| 33 | #define INTTR (0x01 << 21) |
| 34 | #define KEY0R (0x0f << 3) |
| 35 | #define KEY0C 0x07 |
| 36 | #define DEBOUNCE_BIT 0x08 |
| 37 | #define KSIZE0 (0x01 << 16) |
| 38 | #define KSIZE1 (0x01 << 17) |
| 39 | #define KPSEL (0x01 << 19) |
| 40 | #define ENKP (0x01 << 18) |
| 41 | |
| 42 | #define KGET_RAW(n) (((n) & KEY0R) >> 3) |
| 43 | #define KGET_COLUMN(n) ((n) & KEY0C) |
| 44 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 45 | #define W90P910_NUM_ROWS 8 |
| 46 | #define W90P910_NUM_COLS 8 |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 47 | #define W90P910_ROW_SHIFT 3 |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 48 | |
| 49 | struct w90p910_keypad { |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 50 | const struct w90p910_keypad_platform_data *pdata; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 51 | struct clk *clk; |
| 52 | struct input_dev *input_dev; |
| 53 | void __iomem *mmio_base; |
| 54 | int irq; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 55 | unsigned short keymap[W90P910_NUM_ROWS * W90P910_NUM_COLS]; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 58 | static void w90p910_keypad_scan_matrix(struct w90p910_keypad *keypad, |
| 59 | unsigned int status) |
| 60 | { |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 61 | struct input_dev *input_dev = keypad->input_dev; |
| 62 | unsigned int row = KGET_RAW(status); |
| 63 | unsigned int col = KGET_COLUMN(status); |
| 64 | unsigned int code = MATRIX_SCAN_CODE(row, col, W90P910_ROW_SHIFT); |
| 65 | unsigned int key = keypad->keymap[code]; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 66 | |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 67 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
| 68 | input_report_key(input_dev, key, 1); |
| 69 | input_sync(input_dev); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 70 | |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 71 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
| 72 | input_report_key(input_dev, key, 0); |
| 73 | input_sync(input_dev); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static irqreturn_t w90p910_keypad_irq_handler(int irq, void *dev_id) |
| 77 | { |
| 78 | struct w90p910_keypad *keypad = dev_id; |
| 79 | unsigned int kstatus, val; |
| 80 | |
| 81 | kstatus = __raw_readl(keypad->mmio_base + KPI_STATUS); |
| 82 | |
| 83 | val = INTTR | IS1KEY; |
| 84 | |
| 85 | if (kstatus & val) |
| 86 | w90p910_keypad_scan_matrix(keypad, kstatus); |
| 87 | |
| 88 | return IRQ_HANDLED; |
| 89 | } |
| 90 | |
| 91 | static int w90p910_keypad_open(struct input_dev *dev) |
| 92 | { |
| 93 | struct w90p910_keypad *keypad = input_get_drvdata(dev); |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 94 | const struct w90p910_keypad_platform_data *pdata = keypad->pdata; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 95 | unsigned int val, config; |
| 96 | |
| 97 | /* Enable unit clock */ |
| 98 | clk_enable(keypad->clk); |
| 99 | |
| 100 | val = __raw_readl(keypad->mmio_base + KPI_CONF); |
| 101 | val |= (KPSEL | ENKP); |
| 102 | val &= ~(KSIZE0 | KSIZE1); |
| 103 | |
| 104 | config = pdata->prescale | (pdata->debounce << DEBOUNCE_BIT); |
| 105 | |
| 106 | val |= config; |
| 107 | |
| 108 | __raw_writel(val, keypad->mmio_base + KPI_CONF); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static void w90p910_keypad_close(struct input_dev *dev) |
| 114 | { |
| 115 | struct w90p910_keypad *keypad = input_get_drvdata(dev); |
| 116 | |
| 117 | /* Disable clock unit */ |
| 118 | clk_disable(keypad->clk); |
| 119 | } |
| 120 | |
| 121 | static int __devinit w90p910_keypad_probe(struct platform_device *pdev) |
| 122 | { |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 123 | const struct w90p910_keypad_platform_data *pdata = |
| 124 | pdev->dev.platform_data; |
Julia Lawall | 903b912 | 2009-08-30 11:30:48 -0700 | [diff] [blame] | 125 | const struct matrix_keymap_data *keymap_data; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 126 | struct w90p910_keypad *keypad; |
| 127 | struct input_dev *input_dev; |
| 128 | struct resource *res; |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 129 | int irq; |
| 130 | int error; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 131 | |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 132 | if (!pdata) { |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 133 | dev_err(&pdev->dev, "no platform data defined\n"); |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 134 | return -EINVAL; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Julia Lawall | 903b912 | 2009-08-30 11:30:48 -0700 | [diff] [blame] | 137 | keymap_data = pdata->keymap_data; |
| 138 | |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 139 | irq = platform_get_irq(pdev, 0); |
| 140 | if (irq < 0) { |
| 141 | dev_err(&pdev->dev, "failed to get keypad irq\n"); |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 142 | return -ENXIO; |
| 143 | } |
| 144 | |
| 145 | keypad = kzalloc(sizeof(struct w90p910_keypad), GFP_KERNEL); |
| 146 | input_dev = input_allocate_device(); |
| 147 | if (!keypad || !input_dev) { |
| 148 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
| 149 | error = -ENOMEM; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 150 | goto failed_free; |
| 151 | } |
| 152 | |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 153 | keypad->pdata = pdata; |
| 154 | keypad->input_dev = input_dev; |
| 155 | keypad->irq = irq; |
| 156 | |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 157 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 158 | if (res == NULL) { |
| 159 | dev_err(&pdev->dev, "failed to get I/O memory\n"); |
| 160 | error = -ENXIO; |
| 161 | goto failed_free; |
| 162 | } |
| 163 | |
| 164 | res = request_mem_region(res->start, resource_size(res), pdev->name); |
| 165 | if (res == NULL) { |
| 166 | dev_err(&pdev->dev, "failed to request I/O memory\n"); |
| 167 | error = -EBUSY; |
| 168 | goto failed_free; |
| 169 | } |
| 170 | |
| 171 | keypad->mmio_base = ioremap(res->start, resource_size(res)); |
| 172 | if (keypad->mmio_base == NULL) { |
| 173 | dev_err(&pdev->dev, "failed to remap I/O memory\n"); |
| 174 | error = -ENXIO; |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 175 | goto failed_free_res; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | keypad->clk = clk_get(&pdev->dev, NULL); |
| 179 | if (IS_ERR(keypad->clk)) { |
| 180 | dev_err(&pdev->dev, "failed to get keypad clock\n"); |
| 181 | error = PTR_ERR(keypad->clk); |
| 182 | goto failed_free_io; |
| 183 | } |
| 184 | |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 185 | /* set multi-function pin for w90p910 kpi. */ |
| 186 | mfp_set_groupi(&pdev->dev); |
| 187 | |
| 188 | input_dev->name = pdev->name; |
| 189 | input_dev->id.bustype = BUS_HOST; |
| 190 | input_dev->open = w90p910_keypad_open; |
| 191 | input_dev->close = w90p910_keypad_close; |
| 192 | input_dev->dev.parent = &pdev->dev; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 193 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 194 | error = matrix_keypad_build_keymap(keymap_data, NULL, |
| 195 | W90P910_NUM_ROWS, W90P910_NUM_COLS, |
| 196 | keypad->keymap, input_dev); |
| 197 | if (error) { |
| 198 | dev_err(&pdev->dev, "failed to build keymap\n"); |
| 199 | goto failed_put_clk; |
| 200 | } |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 201 | |
| 202 | error = request_irq(keypad->irq, w90p910_keypad_irq_handler, |
Yong Zhang | ec4665c | 2011-09-07 14:04:16 -0700 | [diff] [blame] | 203 | 0, pdev->name, keypad); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 204 | if (error) { |
| 205 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 206 | goto failed_put_clk; |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 209 | __set_bit(EV_REP, input_dev->evbit); |
| 210 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
| 211 | input_set_drvdata(input_dev, keypad); |
| 212 | |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 213 | /* Register the input device */ |
| 214 | error = input_register_device(input_dev); |
| 215 | if (error) { |
| 216 | dev_err(&pdev->dev, "failed to register input device\n"); |
| 217 | goto failed_free_irq; |
| 218 | } |
| 219 | |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 220 | platform_set_drvdata(pdev, keypad); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | |
| 223 | failed_free_irq: |
| 224 | free_irq(irq, pdev); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 225 | failed_put_clk: |
| 226 | clk_put(keypad->clk); |
| 227 | failed_free_io: |
| 228 | iounmap(keypad->mmio_base); |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 229 | failed_free_res: |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 230 | release_mem_region(res->start, resource_size(res)); |
| 231 | failed_free: |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 232 | input_free_device(input_dev); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 233 | kfree(keypad); |
| 234 | return error; |
| 235 | } |
| 236 | |
| 237 | static int __devexit w90p910_keypad_remove(struct platform_device *pdev) |
| 238 | { |
| 239 | struct w90p910_keypad *keypad = platform_get_drvdata(pdev); |
| 240 | struct resource *res; |
| 241 | |
| 242 | free_irq(keypad->irq, pdev); |
| 243 | |
| 244 | clk_put(keypad->clk); |
| 245 | |
| 246 | input_unregister_device(keypad->input_dev); |
| 247 | |
| 248 | iounmap(keypad->mmio_base); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 249 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 250 | release_mem_region(res->start, resource_size(res)); |
| 251 | |
| 252 | platform_set_drvdata(pdev, NULL); |
| 253 | kfree(keypad); |
Dmitry Torokhov | 09113ae | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 254 | |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static struct platform_driver w90p910_keypad_driver = { |
| 259 | .probe = w90p910_keypad_probe, |
| 260 | .remove = __devexit_p(w90p910_keypad_remove), |
| 261 | .driver = { |
Wan ZongShun | 1afaab9 | 2010-07-18 22:23:19 -0700 | [diff] [blame] | 262 | .name = "nuc900-kpi", |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 263 | .owner = THIS_MODULE, |
| 264 | }, |
| 265 | }; |
JJ Ding | 5146c84 | 2011-11-29 11:08:39 -0800 | [diff] [blame] | 266 | module_platform_driver(w90p910_keypad_driver); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 267 | |
| 268 | MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>"); |
Wan ZongShun | fb962e1 | 2009-08-20 21:41:03 -0700 | [diff] [blame] | 269 | MODULE_DESCRIPTION("w90p910 keypad driver"); |
Wan ZongShun | 4a15235 | 2009-08-09 21:22:22 -0700 | [diff] [blame] | 270 | MODULE_LICENSE("GPL"); |
Wan ZongShun | fb962e1 | 2009-08-20 21:41:03 -0700 | [diff] [blame] | 271 | MODULE_ALIAS("platform:nuc900-keypad"); |