Merge tag 'reset-for-v5.3' of git://git.pengutronix.de/git/pza/linux into arm/drivers
[sfrench/cifs-2.6.git] / drivers / input / keyboard / davinci_keyscan.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DaVinci Key Scan Driver for TI platforms
4  *
5  * Copyright (C) 2009 Texas Instruments, Inc
6  *
7  * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
8  *
9  * Initial Code: Sandeep Paulraj <s-paulraj@ti.com>
10  */
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>
21
22 #include <linux/platform_data/keyscan-davinci.h>
23
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
36
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
44
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
51
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[];
60 };
61
62 /* Initializing the kp Module */
63 static int __init davinci_ks_initialize(struct davinci_ks *davinci_ks)
64 {
65         struct device *dev = &davinci_ks->input->dev;
66         struct davinci_ks_platform_data *pdata = davinci_ks->pdata;
67         u32 matrix_ctrl;
68
69         /* Enable all interrupts */
70         __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
71                      davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
72
73         /* Clear interrupts if any */
74         __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
75                      davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
76
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);
84
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;
96         }
97
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);
101
102         return 0;
103 }
104
105 static irqreturn_t davinci_ks_interrupt(int irq, void *dev_id)
106 {
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;
115
116         /* Disable interrupt */
117         __raw_writel(0x0, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
118
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);
122
123         changed = prev_status ^ new_status;
124
125         if (changed) {
126                 /*
127                  * It goes through all bits in 'changed' to ensure
128                  * that no key changes are being missed
129                  */
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);
139                         }
140                 }
141                 /* Clearing interrupt */
142                 __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
143                              davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
144         }
145
146         /* Enable interrupts */
147         __raw_writel(0x1, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
148
149         return IRQ_HANDLED;
150 }
151
152 static int __init davinci_ks_probe(struct platform_device *pdev)
153 {
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;
160
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;
166                 }
167         }
168
169         if (!pdata->keymap) {
170                 dev_dbg(dev, "no keymap from pdata\n");
171                 return -EINVAL;
172         }
173
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;
179         }
180
181         memcpy(davinci_ks->keymap, pdata->keymap,
182                 sizeof(unsigned short) * pdata->keymapsize);
183
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;
189         }
190
191         davinci_ks->input = key_dev;
192
193         davinci_ks->irq = platform_get_irq(pdev, 0);
194         if (davinci_ks->irq < 0) {
195                 dev_err(dev, "no key scan irq\n");
196                 error = davinci_ks->irq;
197                 goto fail2;
198         }
199
200         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
201         if (!res) {
202                 dev_err(dev, "no mem resource\n");
203                 error = -EINVAL;
204                 goto fail2;
205         }
206
207         davinci_ks->pbase = res->start;
208         davinci_ks->base_size = resource_size(res);
209
210         mem = request_mem_region(davinci_ks->pbase, davinci_ks->base_size,
211                                  pdev->name);
212         if (!mem) {
213                 dev_err(dev, "key scan registers at %08x are not free\n",
214                         davinci_ks->pbase);
215                 error = -EBUSY;
216                 goto fail2;
217         }
218
219         davinci_ks->base = ioremap(davinci_ks->pbase, davinci_ks->base_size);
220         if (!davinci_ks->base) {
221                 dev_err(dev, "can't ioremap MEM resource.\n");
222                 error = -ENOMEM;
223                 goto fail3;
224         }
225
226         /* Enable auto repeat feature of Linux input subsystem */
227         if (pdata->rep)
228                 __set_bit(EV_REP, key_dev->evbit);
229
230         /* Setup input device */
231         __set_bit(EV_KEY, key_dev->evbit);
232
233         /* Setup the platform data */
234         davinci_ks->pdata = pdata;
235
236         for (i = 0; i < davinci_ks->pdata->keymapsize; i++)
237                 __set_bit(davinci_ks->pdata->keymap[i], key_dev->keybit);
238
239         key_dev->name = "davinci_keyscan";
240         key_dev->phys = "davinci_keyscan/input0";
241         key_dev->dev.parent = dev;
242         key_dev->id.bustype = BUS_HOST;
243         key_dev->id.vendor = 0x0001;
244         key_dev->id.product = 0x0001;
245         key_dev->id.version = 0x0001;
246         key_dev->keycode = davinci_ks->keymap;
247         key_dev->keycodesize = sizeof(davinci_ks->keymap[0]);
248         key_dev->keycodemax = davinci_ks->pdata->keymapsize;
249
250         error = input_register_device(davinci_ks->input);
251         if (error < 0) {
252                 dev_err(dev, "unable to register davinci key scan device\n");
253                 goto fail4;
254         }
255
256         error = request_irq(davinci_ks->irq, davinci_ks_interrupt,
257                           0, pdev->name, davinci_ks);
258         if (error < 0) {
259                 dev_err(dev, "unable to register davinci key scan interrupt\n");
260                 goto fail5;
261         }
262
263         error = davinci_ks_initialize(davinci_ks);
264         if (error < 0) {
265                 dev_err(dev, "unable to initialize davinci key scan device\n");
266                 goto fail6;
267         }
268
269         platform_set_drvdata(pdev, davinci_ks);
270         return 0;
271
272 fail6:
273         free_irq(davinci_ks->irq, davinci_ks);
274 fail5:
275         input_unregister_device(davinci_ks->input);
276         key_dev = NULL;
277 fail4:
278         iounmap(davinci_ks->base);
279 fail3:
280         release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
281 fail2:
282         input_free_device(key_dev);
283 fail1:
284         kfree(davinci_ks);
285
286         return error;
287 }
288
289 static int davinci_ks_remove(struct platform_device *pdev)
290 {
291         struct davinci_ks *davinci_ks = platform_get_drvdata(pdev);
292
293         free_irq(davinci_ks->irq, davinci_ks);
294
295         input_unregister_device(davinci_ks->input);
296
297         iounmap(davinci_ks->base);
298         release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
299
300         kfree(davinci_ks);
301
302         return 0;
303 }
304
305 static struct platform_driver davinci_ks_driver = {
306         .driver = {
307                 .name = "davinci_keyscan",
308         },
309         .remove = davinci_ks_remove,
310 };
311
312 module_platform_driver_probe(davinci_ks_driver, davinci_ks_probe);
313
314 MODULE_AUTHOR("Miguel Aguilar");
315 MODULE_DESCRIPTION("Texas Instruments DaVinci Key Scan Driver");
316 MODULE_LICENSE("GPL");