| 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 | |
| 61 | u32 matrix_sz; /* Size of matrix in XxY, ie. 3 = 3x3 */ |
| 62 | u32 deb_clks; /* Debounce clocks (based on 32KHz clock) */ |
| 63 | u32 scan_delay; /* Scan delay (based on 32KHz clock) */ |
| 64 | |
| 65 | unsigned int row_shift; |
| 66 | unsigned short *keymap; /* Pointer to key map for the scan matrix */ |
| 67 | |
| 68 | u8 lastkeystates[8]; |
| 69 | }; |
| 70 | |
| 71 | static void lpc32xx_mod_states(struct lpc32xx_kscan_drv *kscandat, int col) |
| 72 | { |
| 73 | struct input_dev *input = kscandat->input; |
| 74 | unsigned row, changed, scancode, keycode; |
| 75 | u8 key; |
| 76 | |
| 77 | key = readl(LPC32XX_KS_DATA(kscandat->kscan_base, col)); |
| 78 | changed = key ^ kscandat->lastkeystates[col]; |
| 79 | kscandat->lastkeystates[col] = key; |
| 80 | |
| 81 | for (row = 0; changed; row++, changed >>= 1) { |
| 82 | if (changed & 1) { |
| 83 | /* Key state changed, signal an event */ |
| 84 | scancode = MATRIX_SCAN_CODE(row, col, |
| 85 | kscandat->row_shift); |
| 86 | keycode = kscandat->keymap[scancode]; |
| 87 | input_event(dev: input, EV_MSC, MSC_SCAN, value: scancode); |
| 88 | input_report_key(dev: input, code: keycode, value: key & (1 << row)); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static irqreturn_t lpc32xx_kscan_irq(int irq, void *dev_id) |
| 94 | { |
| 95 | struct lpc32xx_kscan_drv *kscandat = dev_id; |
| 96 | int i; |
| 97 | |
| 98 | for (i = 0; i < kscandat->matrix_sz; i++) |
| 99 | lpc32xx_mod_states(kscandat, col: i); |
| 100 | |
| 101 | writel(val: 1, LPC32XX_KS_IRQ(kscandat->kscan_base)); |
| 102 | |
| 103 | input_sync(dev: kscandat->input); |
| 104 | |
| 105 | return IRQ_HANDLED; |
| 106 | } |
| 107 | |
| 108 | static int lpc32xx_kscan_open(struct input_dev *dev) |
| 109 | { |
| 110 | struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev); |
| 111 | int error; |
| 112 | |
| 113 | error = clk_prepare_enable(clk: kscandat->clk); |
| 114 | if (error) |
| 115 | return error; |
| 116 | |
| 117 | writel(val: 1, LPC32XX_KS_IRQ(kscandat->kscan_base)); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static void lpc32xx_kscan_close(struct input_dev *dev) |
| 123 | { |
| 124 | struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev); |
| 125 | |
| 126 | writel(val: 1, LPC32XX_KS_IRQ(kscandat->kscan_base)); |
| 127 | clk_disable_unprepare(clk: kscandat->clk); |
| 128 | } |
| 129 | |
| 130 | static int lpc32xx_parse_dt(struct device *dev, |
| 131 | struct lpc32xx_kscan_drv *kscandat) |
| 132 | { |
| 133 | struct device_node *np = dev->of_node; |
| 134 | u32 rows = 0, columns = 0; |
| 135 | int err; |
| 136 | |
| 137 | err = matrix_keypad_parse_properties(dev, rows: &rows, cols: &columns); |
| 138 | if (err) |
| 139 | return err; |
| 140 | if (rows != columns) { |
| 141 | dev_err(dev, "rows and columns must be equal!\n" ); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | kscandat->matrix_sz = rows; |
| 146 | kscandat->row_shift = get_count_order(count: columns); |
| 147 | |
| 148 | of_property_read_u32(np, propname: "nxp,debounce-delay-ms" , out_value: &kscandat->deb_clks); |
| 149 | of_property_read_u32(np, propname: "nxp,scan-delay-ms" , out_value: &kscandat->scan_delay); |
| 150 | if (!kscandat->deb_clks || !kscandat->scan_delay) { |
| 151 | dev_err(dev, "debounce or scan delay not specified\n" ); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int lpc32xx_kscan_probe(struct platform_device *pdev) |
| 159 | { |
| 160 | struct lpc32xx_kscan_drv *kscandat; |
| 161 | struct input_dev *input; |
| 162 | size_t keymap_size; |
| 163 | int error; |
| 164 | int irq; |
| 165 | |
| 166 | irq = platform_get_irq(pdev, 0); |
| 167 | if (irq < 0) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | kscandat = devm_kzalloc(dev: &pdev->dev, size: sizeof(*kscandat), |
| 171 | GFP_KERNEL); |
| 172 | if (!kscandat) |
| 173 | return -ENOMEM; |
| 174 | |
| 175 | error = lpc32xx_parse_dt(dev: &pdev->dev, kscandat); |
| 176 | if (error) { |
| 177 | dev_err(&pdev->dev, "failed to parse device tree\n" ); |
| 178 | return error; |
| 179 | } |
| 180 | |
| 181 | keymap_size = sizeof(kscandat->keymap[0]) * |
| 182 | (kscandat->matrix_sz << kscandat->row_shift); |
| 183 | kscandat->keymap = devm_kzalloc(dev: &pdev->dev, size: keymap_size, GFP_KERNEL); |
| 184 | if (!kscandat->keymap) |
| 185 | return -ENOMEM; |
| 186 | |
| 187 | kscandat->input = input = devm_input_allocate_device(&pdev->dev); |
| 188 | if (!input) { |
| 189 | dev_err(&pdev->dev, "failed to allocate input device\n" ); |
| 190 | return -ENOMEM; |
| 191 | } |
| 192 | |
| 193 | /* Setup key input */ |
| 194 | input->name = pdev->name; |
| 195 | input->phys = "lpc32xx/input0" ; |
| 196 | input->id.vendor = 0x0001; |
| 197 | input->id.product = 0x0001; |
| 198 | input->id.version = 0x0100; |
| 199 | input->open = lpc32xx_kscan_open; |
| 200 | input->close = lpc32xx_kscan_close; |
| 201 | input->dev.parent = &pdev->dev; |
| 202 | |
| 203 | input_set_capability(dev: input, EV_MSC, MSC_SCAN); |
| 204 | |
| 205 | error = matrix_keypad_build_keymap(NULL, NULL, |
| 206 | rows: kscandat->matrix_sz, |
| 207 | cols: kscandat->matrix_sz, |
| 208 | keymap: kscandat->keymap, input_dev: kscandat->input); |
| 209 | if (error) { |
| 210 | dev_err(&pdev->dev, "failed to build keymap\n" ); |
| 211 | return error; |
| 212 | } |
| 213 | |
| 214 | input_set_drvdata(dev: kscandat->input, data: kscandat); |
| 215 | |
| 216 | kscandat->kscan_base = devm_platform_ioremap_resource(pdev, index: 0); |
| 217 | if (IS_ERR(ptr: kscandat->kscan_base)) |
| 218 | return PTR_ERR(ptr: kscandat->kscan_base); |
| 219 | |
| 220 | /* Get the key scanner clock */ |
| 221 | kscandat->clk = devm_clk_get(dev: &pdev->dev, NULL); |
| 222 | if (IS_ERR(ptr: kscandat->clk)) { |
| 223 | dev_err(&pdev->dev, "failed to get clock\n" ); |
| 224 | return PTR_ERR(ptr: kscandat->clk); |
| 225 | } |
| 226 | |
| 227 | /* Configure the key scanner */ |
| 228 | error = clk_prepare_enable(clk: kscandat->clk); |
| 229 | if (error) |
| 230 | return error; |
| 231 | |
| 232 | writel(val: kscandat->deb_clks, LPC32XX_KS_DEB(kscandat->kscan_base)); |
| 233 | writel(val: kscandat->scan_delay, LPC32XX_KS_SCAN_CTL(kscandat->kscan_base)); |
| 234 | writel(LPC32XX_KSCAN_FTST_USE32K_CLK, |
| 235 | LPC32XX_KS_FAST_TST(kscandat->kscan_base)); |
| 236 | writel(val: kscandat->matrix_sz, |
| 237 | LPC32XX_KS_MATRIX_DIM(kscandat->kscan_base)); |
| 238 | writel(val: 1, LPC32XX_KS_IRQ(kscandat->kscan_base)); |
| 239 | clk_disable_unprepare(clk: kscandat->clk); |
| 240 | |
| 241 | error = devm_request_irq(dev: &pdev->dev, irq, handler: lpc32xx_kscan_irq, irqflags: 0, |
| 242 | devname: pdev->name, dev_id: kscandat); |
| 243 | if (error) { |
| 244 | dev_err(&pdev->dev, "failed to request irq\n" ); |
| 245 | return error; |
| 246 | } |
| 247 | |
| 248 | error = input_register_device(kscandat->input); |
| 249 | if (error) { |
| 250 | dev_err(&pdev->dev, "failed to register input device\n" ); |
| 251 | return error; |
| 252 | } |
| 253 | |
| 254 | platform_set_drvdata(pdev, data: kscandat); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int lpc32xx_kscan_suspend(struct device *dev) |
| 260 | { |
| 261 | struct platform_device *pdev = to_platform_device(dev); |
| 262 | struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev); |
| 263 | struct input_dev *input = kscandat->input; |
| 264 | |
| 265 | guard(mutex)(T: &input->mutex); |
| 266 | |
| 267 | if (input_device_enabled(dev: input)) { |
| 268 | /* Clear IRQ and disable clock */ |
| 269 | writel(val: 1, LPC32XX_KS_IRQ(kscandat->kscan_base)); |
| 270 | clk_disable_unprepare(clk: kscandat->clk); |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int lpc32xx_kscan_resume(struct device *dev) |
| 277 | { |
| 278 | struct platform_device *pdev = to_platform_device(dev); |
| 279 | struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev); |
| 280 | struct input_dev *input = kscandat->input; |
| 281 | int error; |
| 282 | |
| 283 | guard(mutex)(T: &input->mutex); |
| 284 | |
| 285 | if (input_device_enabled(dev: input)) { |
| 286 | /* Enable clock and clear IRQ */ |
| 287 | error = clk_prepare_enable(clk: kscandat->clk); |
| 288 | if (error) |
| 289 | return error; |
| 290 | |
| 291 | writel(val: 1, LPC32XX_KS_IRQ(kscandat->kscan_base)); |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static DEFINE_SIMPLE_DEV_PM_OPS(lpc32xx_kscan_pm_ops, lpc32xx_kscan_suspend, |
| 298 | lpc32xx_kscan_resume); |
| 299 | |
| 300 | static const struct of_device_id lpc32xx_kscan_match[] = { |
| 301 | { .compatible = "nxp,lpc3220-key" }, |
| 302 | {}, |
| 303 | }; |
| 304 | MODULE_DEVICE_TABLE(of, lpc32xx_kscan_match); |
| 305 | |
| 306 | static struct platform_driver lpc32xx_kscan_driver = { |
| 307 | .probe = lpc32xx_kscan_probe, |
| 308 | .driver = { |
| 309 | .name = DRV_NAME, |
| 310 | .pm = pm_sleep_ptr(&lpc32xx_kscan_pm_ops), |
| 311 | .of_match_table = lpc32xx_kscan_match, |
| 312 | } |
| 313 | }; |
| 314 | |
| 315 | module_platform_driver(lpc32xx_kscan_driver); |
| 316 | |
| 317 | MODULE_LICENSE("GPL" ); |
| 318 | MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>" ); |
| 319 | MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>" ); |
| 320 | MODULE_DESCRIPTION("Key scanner driver for LPC32XX devices" ); |
| 321 | |