Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / input / keyboard / lpc32xx-keys.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * NXP LPC32xx SoC Key Scan Interface
4  *
5  * Authors:
6  *    Kevin Wells <kevin.wells@nxp.com>
7  *    Roland Stigge <stigge@antcom.de>
8  *
9  * Copyright (C) 2010 NXP Semiconductors
10  * Copyright (C) 2012 Roland Stigge
11  *
12  * This controller supports square key matrices from 1x1 up to 8x8
13  */
14
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/irq.h>
19 #include <linux/pm.h>
20 #include <linux/platform_device.h>
21 #include <linux/input.h>
22 #include <linux/clk.h>
23 #include <linux/io.h>
24 #include <linux/of.h>
25 #include <linux/input/matrix_keypad.h>
26
27 #define DRV_NAME                                "lpc32xx_keys"
28
29 /*
30  * Key scanner register offsets
31  */
32 #define LPC32XX_KS_DEB(x)                       ((x) + 0x00)
33 #define LPC32XX_KS_STATE_COND(x)                ((x) + 0x04)
34 #define LPC32XX_KS_IRQ(x)                       ((x) + 0x08)
35 #define LPC32XX_KS_SCAN_CTL(x)                  ((x) + 0x0C)
36 #define LPC32XX_KS_FAST_TST(x)                  ((x) + 0x10)
37 #define LPC32XX_KS_MATRIX_DIM(x)                ((x) + 0x14) /* 1..8 */
38 #define LPC32XX_KS_DATA(x, y)                   ((x) + 0x40 + ((y) << 2))
39
40 #define LPC32XX_KSCAN_DEB_NUM_DEB_PASS(n)       ((n) & 0xFF)
41
42 #define LPC32XX_KSCAN_SCOND_IN_IDLE             0x0
43 #define LPC32XX_KSCAN_SCOND_IN_SCANONCE         0x1
44 #define LPC32XX_KSCAN_SCOND_IN_IRQGEN           0x2
45 #define LPC32XX_KSCAN_SCOND_IN_SCAN_MATRIX      0x3
46
47 #define LPC32XX_KSCAN_IRQ_PENDING_CLR           0x1
48
49 #define LPC32XX_KSCAN_SCTRL_SCAN_DELAY(n)       ((n) & 0xFF)
50
51 #define LPC32XX_KSCAN_FTST_FORCESCANONCE        0x1
52 #define LPC32XX_KSCAN_FTST_USE32K_CLK           0x2
53
54 #define LPC32XX_KSCAN_MSEL_SELECT(n)            ((n) & 0xF)
55
56 struct lpc32xx_kscan_drv {
57         struct input_dev *input;
58         struct clk *clk;
59         void __iomem *kscan_base;
60         unsigned int irq;
61
62         u32 matrix_sz;          /* Size of matrix in XxY, ie. 3 = 3x3 */
63         u32 deb_clks;           /* Debounce clocks (based on 32KHz clock) */
64         u32 scan_delay;         /* Scan delay (based on 32KHz clock) */
65
66         unsigned short *keymap; /* Pointer to key map for the scan matrix */
67         unsigned int row_shift;
68
69         u8 lastkeystates[8];
70 };
71
72 static void lpc32xx_mod_states(struct lpc32xx_kscan_drv *kscandat, int col)
73 {
74         struct input_dev *input = kscandat->input;
75         unsigned row, changed, scancode, keycode;
76         u8 key;
77
78         key = readl(LPC32XX_KS_DATA(kscandat->kscan_base, col));
79         changed = key ^ kscandat->lastkeystates[col];
80         kscandat->lastkeystates[col] = key;
81
82         for (row = 0; changed; row++, changed >>= 1) {
83                 if (changed & 1) {
84                         /* Key state changed, signal an event */
85                         scancode = MATRIX_SCAN_CODE(row, col,
86                                                     kscandat->row_shift);
87                         keycode = kscandat->keymap[scancode];
88                         input_event(input, EV_MSC, MSC_SCAN, scancode);
89                         input_report_key(input, keycode, key & (1 << row));
90                 }
91         }
92 }
93
94 static irqreturn_t lpc32xx_kscan_irq(int irq, void *dev_id)
95 {
96         struct lpc32xx_kscan_drv *kscandat = dev_id;
97         int i;
98
99         for (i = 0; i < kscandat->matrix_sz; i++)
100                 lpc32xx_mod_states(kscandat, i);
101
102         writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
103
104         input_sync(kscandat->input);
105
106         return IRQ_HANDLED;
107 }
108
109 static int lpc32xx_kscan_open(struct input_dev *dev)
110 {
111         struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
112         int error;
113
114         error = clk_prepare_enable(kscandat->clk);
115         if (error)
116                 return error;
117
118         writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
119
120         return 0;
121 }
122
123 static void lpc32xx_kscan_close(struct input_dev *dev)
124 {
125         struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
126
127         writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
128         clk_disable_unprepare(kscandat->clk);
129 }
130
131 static int lpc32xx_parse_dt(struct device *dev,
132                                       struct lpc32xx_kscan_drv *kscandat)
133 {
134         struct device_node *np = dev->of_node;
135         u32 rows = 0, columns = 0;
136         int err;
137
138         err = matrix_keypad_parse_properties(dev, &rows, &columns);
139         if (err)
140                 return err;
141         if (rows != columns) {
142                 dev_err(dev, "rows and columns must be equal!\n");
143                 return -EINVAL;
144         }
145
146         kscandat->matrix_sz = rows;
147         kscandat->row_shift = get_count_order(columns);
148
149         of_property_read_u32(np, "nxp,debounce-delay-ms", &kscandat->deb_clks);
150         of_property_read_u32(np, "nxp,scan-delay-ms", &kscandat->scan_delay);
151         if (!kscandat->deb_clks || !kscandat->scan_delay) {
152                 dev_err(dev, "debounce or scan delay not specified\n");
153                 return -EINVAL;
154         }
155
156         return 0;
157 }
158
159 static int lpc32xx_kscan_probe(struct platform_device *pdev)
160 {
161         struct lpc32xx_kscan_drv *kscandat;
162         struct input_dev *input;
163         struct resource *res;
164         size_t keymap_size;
165         int error;
166         int irq;
167
168         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
169         if (!res) {
170                 dev_err(&pdev->dev, "failed to get platform I/O memory\n");
171                 return -EINVAL;
172         }
173
174         irq = platform_get_irq(pdev, 0);
175         if (irq < 0) {
176                 dev_err(&pdev->dev, "failed to get platform irq\n");
177                 return -EINVAL;
178         }
179
180         kscandat = devm_kzalloc(&pdev->dev, sizeof(*kscandat),
181                                 GFP_KERNEL);
182         if (!kscandat)
183                 return -ENOMEM;
184
185         error = lpc32xx_parse_dt(&pdev->dev, kscandat);
186         if (error) {
187                 dev_err(&pdev->dev, "failed to parse device tree\n");
188                 return error;
189         }
190
191         keymap_size = sizeof(kscandat->keymap[0]) *
192                                 (kscandat->matrix_sz << kscandat->row_shift);
193         kscandat->keymap = devm_kzalloc(&pdev->dev, keymap_size, GFP_KERNEL);
194         if (!kscandat->keymap)
195                 return -ENOMEM;
196
197         kscandat->input = input = devm_input_allocate_device(&pdev->dev);
198         if (!input) {
199                 dev_err(&pdev->dev, "failed to allocate input device\n");
200                 return -ENOMEM;
201         }
202
203         /* Setup key input */
204         input->name             = pdev->name;
205         input->phys             = "lpc32xx/input0";
206         input->id.vendor        = 0x0001;
207         input->id.product       = 0x0001;
208         input->id.version       = 0x0100;
209         input->open             = lpc32xx_kscan_open;
210         input->close            = lpc32xx_kscan_close;
211         input->dev.parent       = &pdev->dev;
212
213         input_set_capability(input, EV_MSC, MSC_SCAN);
214
215         error = matrix_keypad_build_keymap(NULL, NULL,
216                                            kscandat->matrix_sz,
217                                            kscandat->matrix_sz,
218                                            kscandat->keymap, kscandat->input);
219         if (error) {
220                 dev_err(&pdev->dev, "failed to build keymap\n");
221                 return error;
222         }
223
224         input_set_drvdata(kscandat->input, kscandat);
225
226         kscandat->kscan_base = devm_ioremap_resource(&pdev->dev, res);
227         if (IS_ERR(kscandat->kscan_base))
228                 return PTR_ERR(kscandat->kscan_base);
229
230         /* Get the key scanner clock */
231         kscandat->clk = devm_clk_get(&pdev->dev, NULL);
232         if (IS_ERR(kscandat->clk)) {
233                 dev_err(&pdev->dev, "failed to get clock\n");
234                 return PTR_ERR(kscandat->clk);
235         }
236
237         /* Configure the key scanner */
238         error = clk_prepare_enable(kscandat->clk);
239         if (error)
240                 return error;
241
242         writel(kscandat->deb_clks, LPC32XX_KS_DEB(kscandat->kscan_base));
243         writel(kscandat->scan_delay, LPC32XX_KS_SCAN_CTL(kscandat->kscan_base));
244         writel(LPC32XX_KSCAN_FTST_USE32K_CLK,
245                LPC32XX_KS_FAST_TST(kscandat->kscan_base));
246         writel(kscandat->matrix_sz,
247                LPC32XX_KS_MATRIX_DIM(kscandat->kscan_base));
248         writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
249         clk_disable_unprepare(kscandat->clk);
250
251         error = devm_request_irq(&pdev->dev, irq, lpc32xx_kscan_irq, 0,
252                                  pdev->name, kscandat);
253         if (error) {
254                 dev_err(&pdev->dev, "failed to request irq\n");
255                 return error;
256         }
257
258         error = input_register_device(kscandat->input);
259         if (error) {
260                 dev_err(&pdev->dev, "failed to register input device\n");
261                 return error;
262         }
263
264         platform_set_drvdata(pdev, kscandat);
265
266         return 0;
267 }
268
269 #ifdef CONFIG_PM_SLEEP
270 static int lpc32xx_kscan_suspend(struct device *dev)
271 {
272         struct platform_device *pdev = to_platform_device(dev);
273         struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
274         struct input_dev *input = kscandat->input;
275
276         mutex_lock(&input->mutex);
277
278         if (input->users) {
279                 /* Clear IRQ and disable clock */
280                 writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
281                 clk_disable_unprepare(kscandat->clk);
282         }
283
284         mutex_unlock(&input->mutex);
285         return 0;
286 }
287
288 static int lpc32xx_kscan_resume(struct device *dev)
289 {
290         struct platform_device *pdev = to_platform_device(dev);
291         struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
292         struct input_dev *input = kscandat->input;
293         int retval = 0;
294
295         mutex_lock(&input->mutex);
296
297         if (input->users) {
298                 /* Enable clock and clear IRQ */
299                 retval = clk_prepare_enable(kscandat->clk);
300                 if (retval == 0)
301                         writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
302         }
303
304         mutex_unlock(&input->mutex);
305         return retval;
306 }
307 #endif
308
309 static SIMPLE_DEV_PM_OPS(lpc32xx_kscan_pm_ops, lpc32xx_kscan_suspend,
310                          lpc32xx_kscan_resume);
311
312 static const struct of_device_id lpc32xx_kscan_match[] = {
313         { .compatible = "nxp,lpc3220-key" },
314         {},
315 };
316 MODULE_DEVICE_TABLE(of, lpc32xx_kscan_match);
317
318 static struct platform_driver lpc32xx_kscan_driver = {
319         .probe          = lpc32xx_kscan_probe,
320         .driver         = {
321                 .name   = DRV_NAME,
322                 .pm     = &lpc32xx_kscan_pm_ops,
323                 .of_match_table = lpc32xx_kscan_match,
324         }
325 };
326
327 module_platform_driver(lpc32xx_kscan_driver);
328
329 MODULE_LICENSE("GPL");
330 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
331 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
332 MODULE_DESCRIPTION("Key scanner driver for LPC32XX devices");