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 */
davinci_ks_initialize(struct davinci_ks * davinci_ks)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
davinci_ks_interrupt(int irq,void * dev_id)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
davinci_ks_probe(struct platform_device * pdev)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 error = davinci_ks->irq;
196 goto fail2;
197 }
198
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;
204 }
205
206 davinci_ks->pbase = res->start;
207 davinci_ks->base_size = resource_size(res);
208
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;
216 }
217
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;
223 }
224
225 /* Enable auto repeat feature of Linux input subsystem */
226 if (pdata->rep)
227 __set_bit(EV_REP, key_dev->evbit);
228
229 /* Setup input device */
230 __set_bit(EV_KEY, key_dev->evbit);
231
232 /* Setup the platform data */
233 davinci_ks->pdata = pdata;
234
235 for (i = 0; i < davinci_ks->pdata->keymapsize; i++)
236 __set_bit(davinci_ks->pdata->keymap[i], key_dev->keybit);
237
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;
248
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;
253 }
254
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;
260 }
261
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;
266 }
267
268 platform_set_drvdata(pdev, davinci_ks);
269 return 0;
270
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);
284
285 return error;
286 }
287
davinci_ks_remove(struct platform_device * pdev)288 static int davinci_ks_remove(struct platform_device *pdev)
289 {
290 struct davinci_ks *davinci_ks = platform_get_drvdata(pdev);
291
292 free_irq(davinci_ks->irq, davinci_ks);
293
294 input_unregister_device(davinci_ks->input);
295
296 iounmap(davinci_ks->base);
297 release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
298
299 kfree(davinci_ks);
300
301 return 0;
302 }
303
304 static struct platform_driver davinci_ks_driver = {
305 .driver = {
306 .name = "davinci_keyscan",
307 },
308 .remove = davinci_ks_remove,
309 };
310
311 module_platform_driver_probe(davinci_ks_driver, davinci_ks_probe);
312
313 MODULE_AUTHOR("Miguel Aguilar");
314 MODULE_DESCRIPTION("Texas Instruments DaVinci Key Scan Driver");
315 MODULE_LICENSE("GPL");
316