WIP FPC-III support
[linux/fpc-iii.git] / drivers / input / keyboard / davinci_keyscan.c
blobf489cd585b33f6aa4fbf3af61b07680badb6abf5
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * DaVinci Key Scan Driver for TI platforms
5 * Copyright (C) 2009 Texas Instruments, Inc
7 * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
9 * Initial Code: Sandeep Paulraj <s-paulraj@ti.com>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/types.h>
15 #include <linux/input.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
22 #include <linux/platform_data/keyscan-davinci.h>
24 /* Key scan registers */
25 #define DAVINCI_KEYSCAN_KEYCTRL 0x0000
26 #define DAVINCI_KEYSCAN_INTENA 0x0004
27 #define DAVINCI_KEYSCAN_INTFLAG 0x0008
28 #define DAVINCI_KEYSCAN_INTCLR 0x000c
29 #define DAVINCI_KEYSCAN_STRBWIDTH 0x0010
30 #define DAVINCI_KEYSCAN_INTERVAL 0x0014
31 #define DAVINCI_KEYSCAN_CONTTIME 0x0018
32 #define DAVINCI_KEYSCAN_CURRENTST 0x001c
33 #define DAVINCI_KEYSCAN_PREVSTATE 0x0020
34 #define DAVINCI_KEYSCAN_EMUCTRL 0x0024
35 #define DAVINCI_KEYSCAN_IODFTCTRL 0x002c
37 /* Key Control Register (KEYCTRL) */
38 #define DAVINCI_KEYSCAN_KEYEN 0x00000001
39 #define DAVINCI_KEYSCAN_PREVMODE 0x00000002
40 #define DAVINCI_KEYSCAN_CHATOFF 0x00000004
41 #define DAVINCI_KEYSCAN_AUTODET 0x00000008
42 #define DAVINCI_KEYSCAN_SCANMODE 0x00000010
43 #define DAVINCI_KEYSCAN_OUTTYPE 0x00000020
45 /* Masks for the interrupts */
46 #define DAVINCI_KEYSCAN_INT_CONT 0x00000008
47 #define DAVINCI_KEYSCAN_INT_OFF 0x00000004
48 #define DAVINCI_KEYSCAN_INT_ON 0x00000002
49 #define DAVINCI_KEYSCAN_INT_CHANGE 0x00000001
50 #define DAVINCI_KEYSCAN_INT_ALL 0x0000000f
52 struct davinci_ks {
53 struct input_dev *input;
54 struct davinci_ks_platform_data *pdata;
55 int irq;
56 void __iomem *base;
57 resource_size_t pbase;
58 size_t base_size;
59 unsigned short keymap[];
62 /* Initializing the kp Module */
63 static int __init davinci_ks_initialize(struct davinci_ks *davinci_ks)
65 struct device *dev = &davinci_ks->input->dev;
66 struct davinci_ks_platform_data *pdata = davinci_ks->pdata;
67 u32 matrix_ctrl;
69 /* Enable all interrupts */
70 __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
71 davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
73 /* Clear interrupts if any */
74 __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
75 davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
77 /* Setup the scan period = strobe + interval */
78 __raw_writel(pdata->strobe,
79 davinci_ks->base + DAVINCI_KEYSCAN_STRBWIDTH);
80 __raw_writel(pdata->interval,
81 davinci_ks->base + DAVINCI_KEYSCAN_INTERVAL);
82 __raw_writel(0x01,
83 davinci_ks->base + DAVINCI_KEYSCAN_CONTTIME);
85 /* Define matrix type */
86 switch (pdata->matrix_type) {
87 case DAVINCI_KEYSCAN_MATRIX_4X4:
88 matrix_ctrl = 0;
89 break;
90 case DAVINCI_KEYSCAN_MATRIX_5X3:
91 matrix_ctrl = (1 << 6);
92 break;
93 default:
94 dev_err(dev->parent, "wrong matrix type\n");
95 return -EINVAL;
98 /* Enable key scan module and set matrix type */
99 __raw_writel(DAVINCI_KEYSCAN_AUTODET | DAVINCI_KEYSCAN_KEYEN |
100 matrix_ctrl, davinci_ks->base + DAVINCI_KEYSCAN_KEYCTRL);
102 return 0;
105 static irqreturn_t davinci_ks_interrupt(int irq, void *dev_id)
107 struct davinci_ks *davinci_ks = dev_id;
108 struct device *dev = &davinci_ks->input->dev;
109 unsigned short *keymap = davinci_ks->keymap;
110 int keymapsize = davinci_ks->pdata->keymapsize;
111 u32 prev_status, new_status, changed;
112 bool release;
113 int keycode = KEY_UNKNOWN;
114 int i;
116 /* Disable interrupt */
117 __raw_writel(0x0, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
119 /* Reading previous and new status of the key scan */
120 prev_status = __raw_readl(davinci_ks->base + DAVINCI_KEYSCAN_PREVSTATE);
121 new_status = __raw_readl(davinci_ks->base + DAVINCI_KEYSCAN_CURRENTST);
123 changed = prev_status ^ new_status;
125 if (changed) {
127 * It goes through all bits in 'changed' to ensure
128 * that no key changes are being missed
130 for (i = 0 ; i < keymapsize; i++) {
131 if ((changed>>i) & 0x1) {
132 keycode = keymap[i];
133 release = (new_status >> i) & 0x1;
134 dev_dbg(dev->parent, "key %d %s\n", keycode,
135 release ? "released" : "pressed");
136 input_report_key(davinci_ks->input, keycode,
137 !release);
138 input_sync(davinci_ks->input);
141 /* Clearing interrupt */
142 __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
143 davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
146 /* Enable interrupts */
147 __raw_writel(0x1, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
149 return IRQ_HANDLED;
152 static int __init davinci_ks_probe(struct platform_device *pdev)
154 struct davinci_ks *davinci_ks;
155 struct input_dev *key_dev;
156 struct resource *res, *mem;
157 struct device *dev = &pdev->dev;
158 struct davinci_ks_platform_data *pdata = dev_get_platdata(dev);
159 int error, i;
161 if (pdata->device_enable) {
162 error = pdata->device_enable(dev);
163 if (error < 0) {
164 dev_dbg(dev, "device enable function failed\n");
165 return error;
169 if (!pdata->keymap) {
170 dev_dbg(dev, "no keymap from pdata\n");
171 return -EINVAL;
174 davinci_ks = kzalloc(sizeof(struct davinci_ks) +
175 sizeof(unsigned short) * pdata->keymapsize, GFP_KERNEL);
176 if (!davinci_ks) {
177 dev_dbg(dev, "could not allocate memory for private data\n");
178 return -ENOMEM;
181 memcpy(davinci_ks->keymap, pdata->keymap,
182 sizeof(unsigned short) * pdata->keymapsize);
184 key_dev = input_allocate_device();
185 if (!key_dev) {
186 dev_dbg(dev, "could not allocate input device\n");
187 error = -ENOMEM;
188 goto fail1;
191 davinci_ks->input = key_dev;
193 davinci_ks->irq = platform_get_irq(pdev, 0);
194 if (davinci_ks->irq < 0) {
195 error = davinci_ks->irq;
196 goto fail2;
199 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 if (!res) {
201 dev_err(dev, "no mem resource\n");
202 error = -EINVAL;
203 goto fail2;
206 davinci_ks->pbase = res->start;
207 davinci_ks->base_size = resource_size(res);
209 mem = request_mem_region(davinci_ks->pbase, davinci_ks->base_size,
210 pdev->name);
211 if (!mem) {
212 dev_err(dev, "key scan registers at %08x are not free\n",
213 davinci_ks->pbase);
214 error = -EBUSY;
215 goto fail2;
218 davinci_ks->base = ioremap(davinci_ks->pbase, davinci_ks->base_size);
219 if (!davinci_ks->base) {
220 dev_err(dev, "can't ioremap MEM resource.\n");
221 error = -ENOMEM;
222 goto fail3;
225 /* Enable auto repeat feature of Linux input subsystem */
226 if (pdata->rep)
227 __set_bit(EV_REP, key_dev->evbit);
229 /* Setup input device */
230 __set_bit(EV_KEY, key_dev->evbit);
232 /* Setup the platform data */
233 davinci_ks->pdata = pdata;
235 for (i = 0; i < davinci_ks->pdata->keymapsize; i++)
236 __set_bit(davinci_ks->pdata->keymap[i], key_dev->keybit);
238 key_dev->name = "davinci_keyscan";
239 key_dev->phys = "davinci_keyscan/input0";
240 key_dev->dev.parent = dev;
241 key_dev->id.bustype = BUS_HOST;
242 key_dev->id.vendor = 0x0001;
243 key_dev->id.product = 0x0001;
244 key_dev->id.version = 0x0001;
245 key_dev->keycode = davinci_ks->keymap;
246 key_dev->keycodesize = sizeof(davinci_ks->keymap[0]);
247 key_dev->keycodemax = davinci_ks->pdata->keymapsize;
249 error = input_register_device(davinci_ks->input);
250 if (error < 0) {
251 dev_err(dev, "unable to register davinci key scan device\n");
252 goto fail4;
255 error = request_irq(davinci_ks->irq, davinci_ks_interrupt,
256 0, pdev->name, davinci_ks);
257 if (error < 0) {
258 dev_err(dev, "unable to register davinci key scan interrupt\n");
259 goto fail5;
262 error = davinci_ks_initialize(davinci_ks);
263 if (error < 0) {
264 dev_err(dev, "unable to initialize davinci key scan device\n");
265 goto fail6;
268 platform_set_drvdata(pdev, davinci_ks);
269 return 0;
271 fail6:
272 free_irq(davinci_ks->irq, davinci_ks);
273 fail5:
274 input_unregister_device(davinci_ks->input);
275 key_dev = NULL;
276 fail4:
277 iounmap(davinci_ks->base);
278 fail3:
279 release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
280 fail2:
281 input_free_device(key_dev);
282 fail1:
283 kfree(davinci_ks);
285 return error;
288 static int davinci_ks_remove(struct platform_device *pdev)
290 struct davinci_ks *davinci_ks = platform_get_drvdata(pdev);
292 free_irq(davinci_ks->irq, davinci_ks);
294 input_unregister_device(davinci_ks->input);
296 iounmap(davinci_ks->base);
297 release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
299 kfree(davinci_ks);
301 return 0;
304 static struct platform_driver davinci_ks_driver = {
305 .driver = {
306 .name = "davinci_keyscan",
308 .remove = davinci_ks_remove,
311 module_platform_driver_probe(davinci_ks_driver, davinci_ks_probe);
313 MODULE_AUTHOR("Miguel Aguilar");
314 MODULE_DESCRIPTION("Texas Instruments DaVinci Key Scan Driver");
315 MODULE_LICENSE("GPL");