1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (C) 2014 Broadcom Corporation 3 4 #include <linux/bitops.h> 5 #include <linux/clk.h> 6 #include <linux/gfp.h> 7 #include <linux/io.h> 8 #include <linux/input.h> 9 #include <linux/input/matrix_keypad.h> 10 #include <linux/interrupt.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/stddef.h> 15 #include <linux/types.h> 16 17 #define DEFAULT_CLK_HZ 31250 18 #define MAX_ROWS 8 19 #define MAX_COLS 8 20 21 /* Register/field definitions */ 22 #define KPCR_OFFSET 0x00000080 23 #define KPCR_MODE 0x00000002 24 #define KPCR_MODE_SHIFT 1 25 #define KPCR_MODE_MASK 1 26 #define KPCR_ENABLE 0x00000001 27 #define KPCR_STATUSFILTERENABLE 0x00008000 28 #define KPCR_STATUSFILTERTYPE_SHIFT 12 29 #define KPCR_COLFILTERENABLE 0x00000800 30 #define KPCR_COLFILTERTYPE_SHIFT 8 31 #define KPCR_ROWWIDTH_SHIFT 20 32 #define KPCR_COLUMNWIDTH_SHIFT 16 33 34 #define KPIOR_OFFSET 0x00000084 35 #define KPIOR_ROWOCONTRL_SHIFT 24 36 #define KPIOR_ROWOCONTRL_MASK 0xFF000000 37 #define KPIOR_COLUMNOCONTRL_SHIFT 16 38 #define KPIOR_COLUMNOCONTRL_MASK 0x00FF0000 39 #define KPIOR_COLUMN_IO_DATA_SHIFT 0 40 41 #define KPEMR0_OFFSET 0x00000090 42 #define KPEMR1_OFFSET 0x00000094 43 #define KPEMR2_OFFSET 0x00000098 44 #define KPEMR3_OFFSET 0x0000009C 45 #define KPEMR_EDGETYPE_BOTH 3 46 47 #define KPSSR0_OFFSET 0x000000A0 48 #define KPSSR1_OFFSET 0x000000A4 49 #define KPSSRN_OFFSET(reg_n) (KPSSR0_OFFSET + 4 * (reg_n)) 50 #define KPIMR0_OFFSET 0x000000B0 51 #define KPIMR1_OFFSET 0x000000B4 52 #define KPICR0_OFFSET 0x000000B8 53 #define KPICR1_OFFSET 0x000000BC 54 #define KPICRN_OFFSET(reg_n) (KPICR0_OFFSET + 4 * (reg_n)) 55 #define KPISR0_OFFSET 0x000000C0 56 #define KPISR1_OFFSET 0x000000C4 57 58 #define KPCR_STATUSFILTERTYPE_MAX 7 59 #define KPCR_COLFILTERTYPE_MAX 7 60 61 /* Macros to determine the row/column from a bit that is set in SSR0/1. */ 62 #define BIT_TO_ROW_SSRN(bit_nr, reg_n) (((bit_nr) >> 3) + 4 * (reg_n)) 63 #define BIT_TO_COL(bit_nr) ((bit_nr) % 8) 64 65 /* Structure representing various run-time entities */ 66 struct bcm_kp { 67 void __iomem *base; 68 int irq; 69 struct clk *clk; 70 struct input_dev *input_dev; 71 unsigned long last_state[2]; 72 unsigned int n_rows; 73 unsigned int n_cols; 74 u32 kpcr; 75 u32 kpior; 76 u32 kpemr; 77 u32 imr0_val; 78 u32 imr1_val; 79 }; 80 81 /* 82 * Returns the keycode from the input device keymap given the row and 83 * column. 84 */ 85 static int bcm_kp_get_keycode(struct bcm_kp *kp, int row, int col) 86 { 87 unsigned int row_shift = get_count_order(kp->n_cols); 88 unsigned short *keymap = kp->input_dev->keycode; 89 90 return keymap[MATRIX_SCAN_CODE(row, col, row_shift)]; 91 } 92 93 static void bcm_kp_report_keys(struct bcm_kp *kp, int reg_num, int pull_mode) 94 { 95 unsigned long state, change; 96 int bit_nr; 97 int key_press; 98 int row, col; 99 unsigned int keycode; 100 101 /* Clear interrupts */ 102 writel(0xFFFFFFFF, kp->base + KPICRN_OFFSET(reg_num)); 103 104 state = readl(kp->base + KPSSRN_OFFSET(reg_num)); 105 change = kp->last_state[reg_num] ^ state; 106 kp->last_state[reg_num] = state; 107 108 for_each_set_bit(bit_nr, &change, BITS_PER_LONG) { 109 key_press = state & BIT(bit_nr); 110 /* The meaning of SSR register depends on pull mode. */ 111 key_press = pull_mode ? !key_press : key_press; 112 row = BIT_TO_ROW_SSRN(bit_nr, reg_num); 113 col = BIT_TO_COL(bit_nr); 114 keycode = bcm_kp_get_keycode(kp, row, col); 115 input_report_key(kp->input_dev, keycode, key_press); 116 } 117 } 118 119 static irqreturn_t bcm_kp_isr_thread(int irq, void *dev_id) 120 { 121 struct bcm_kp *kp = dev_id; 122 int pull_mode = (kp->kpcr >> KPCR_MODE_SHIFT) & KPCR_MODE_MASK; 123 int reg_num; 124 125 for (reg_num = 0; reg_num <= 1; reg_num++) 126 bcm_kp_report_keys(kp, reg_num, pull_mode); 127 128 input_sync(kp->input_dev); 129 130 return IRQ_HANDLED; 131 } 132 133 static int bcm_kp_start(struct bcm_kp *kp) 134 { 135 int error; 136 137 if (kp->clk) { 138 error = clk_prepare_enable(kp->clk); 139 if (error) 140 return error; 141 } 142 143 writel(kp->kpior, kp->base + KPIOR_OFFSET); 144 145 writel(kp->imr0_val, kp->base + KPIMR0_OFFSET); 146 writel(kp->imr1_val, kp->base + KPIMR1_OFFSET); 147 148 writel(kp->kpemr, kp->base + KPEMR0_OFFSET); 149 writel(kp->kpemr, kp->base + KPEMR1_OFFSET); 150 writel(kp->kpemr, kp->base + KPEMR2_OFFSET); 151 writel(kp->kpemr, kp->base + KPEMR3_OFFSET); 152 153 writel(0xFFFFFFFF, kp->base + KPICR0_OFFSET); 154 writel(0xFFFFFFFF, kp->base + KPICR1_OFFSET); 155 156 kp->last_state[0] = readl(kp->base + KPSSR0_OFFSET); 157 kp->last_state[0] = readl(kp->base + KPSSR1_OFFSET); 158 159 writel(kp->kpcr | KPCR_ENABLE, kp->base + KPCR_OFFSET); 160 161 return 0; 162 } 163 164 static void bcm_kp_stop(const struct bcm_kp *kp) 165 { 166 u32 val; 167 168 val = readl(kp->base + KPCR_OFFSET); 169 val &= ~KPCR_ENABLE; 170 writel(0, kp->base + KPCR_OFFSET); 171 writel(0, kp->base + KPIMR0_OFFSET); 172 writel(0, kp->base + KPIMR1_OFFSET); 173 writel(0xFFFFFFFF, kp->base + KPICR0_OFFSET); 174 writel(0xFFFFFFFF, kp->base + KPICR1_OFFSET); 175 176 clk_disable_unprepare(kp->clk); 177 } 178 179 static int bcm_kp_open(struct input_dev *dev) 180 { 181 struct bcm_kp *kp = input_get_drvdata(dev); 182 183 return bcm_kp_start(kp); 184 } 185 186 static void bcm_kp_close(struct input_dev *dev) 187 { 188 struct bcm_kp *kp = input_get_drvdata(dev); 189 190 bcm_kp_stop(kp); 191 } 192 193 static int bcm_kp_matrix_key_parse_dt(struct bcm_kp *kp) 194 { 195 struct device *dev = kp->input_dev->dev.parent; 196 struct device_node *np = dev->of_node; 197 int error; 198 unsigned int dt_val; 199 unsigned int i; 200 unsigned int num_rows, col_mask, rows_set; 201 202 /* Initialize the KPCR Keypad Configuration Register */ 203 kp->kpcr = KPCR_STATUSFILTERENABLE | KPCR_COLFILTERENABLE; 204 205 error = matrix_keypad_parse_properties(dev, &kp->n_rows, &kp->n_cols); 206 if (error) { 207 dev_err(dev, "failed to parse kp params\n"); 208 return error; 209 } 210 211 /* Set row width for the ASIC block. */ 212 kp->kpcr |= (kp->n_rows - 1) << KPCR_ROWWIDTH_SHIFT; 213 214 /* Set column width for the ASIC block. */ 215 kp->kpcr |= (kp->n_cols - 1) << KPCR_COLUMNWIDTH_SHIFT; 216 217 /* Configure the IMR registers */ 218 219 /* 220 * IMR registers contain interrupt enable bits for 8x8 matrix 221 * IMR0 register format: <row3> <row2> <row1> <row0> 222 * IMR1 register format: <row7> <row6> <row5> <row4> 223 */ 224 col_mask = (1 << (kp->n_cols)) - 1; 225 num_rows = kp->n_rows; 226 227 /* Set column bits in rows 0 to 3 in IMR0 */ 228 kp->imr0_val = col_mask; 229 230 rows_set = 1; 231 while (--num_rows && rows_set++ < 4) 232 kp->imr0_val |= kp->imr0_val << MAX_COLS; 233 234 /* Set column bits in rows 4 to 7 in IMR1 */ 235 kp->imr1_val = 0; 236 if (num_rows) { 237 kp->imr1_val = col_mask; 238 while (--num_rows) 239 kp->imr1_val |= kp->imr1_val << MAX_COLS; 240 } 241 242 /* Initialize the KPEMR Keypress Edge Mode Registers */ 243 /* Trigger on both edges */ 244 kp->kpemr = 0; 245 for (i = 0; i <= 30; i += 2) 246 kp->kpemr |= (KPEMR_EDGETYPE_BOTH << i); 247 248 /* 249 * Obtain the Status filter debounce value and verify against the 250 * possible values specified in the DT binding. 251 */ 252 of_property_read_u32(np, "status-debounce-filter-period", &dt_val); 253 254 if (dt_val > KPCR_STATUSFILTERTYPE_MAX) { 255 dev_err(dev, "Invalid Status filter debounce value %d\n", 256 dt_val); 257 return -EINVAL; 258 } 259 260 kp->kpcr |= dt_val << KPCR_STATUSFILTERTYPE_SHIFT; 261 262 /* 263 * Obtain the Column filter debounce value and verify against the 264 * possible values specified in the DT binding. 265 */ 266 of_property_read_u32(np, "col-debounce-filter-period", &dt_val); 267 268 if (dt_val > KPCR_COLFILTERTYPE_MAX) { 269 dev_err(dev, "Invalid Column filter debounce value %d\n", 270 dt_val); 271 return -EINVAL; 272 } 273 274 kp->kpcr |= dt_val << KPCR_COLFILTERTYPE_SHIFT; 275 276 /* 277 * Determine between the row and column, 278 * which should be configured as output. 279 */ 280 if (of_property_read_bool(np, "row-output-enabled")) { 281 /* 282 * Set RowOContrl or ColumnOContrl in KPIOR 283 * to the number of pins to drive as outputs 284 */ 285 kp->kpior = ((1 << kp->n_rows) - 1) << 286 KPIOR_ROWOCONTRL_SHIFT; 287 } else { 288 kp->kpior = ((1 << kp->n_cols) - 1) << 289 KPIOR_COLUMNOCONTRL_SHIFT; 290 } 291 292 /* 293 * Determine if the scan pull up needs to be enabled 294 */ 295 if (of_property_read_bool(np, "pull-up-enabled")) 296 kp->kpcr |= KPCR_MODE; 297 298 dev_dbg(dev, "n_rows=%d n_col=%d kpcr=%x kpior=%x kpemr=%x\n", 299 kp->n_rows, kp->n_cols, 300 kp->kpcr, kp->kpior, kp->kpemr); 301 302 return 0; 303 } 304 305 306 static int bcm_kp_probe(struct platform_device *pdev) 307 { 308 struct bcm_kp *kp; 309 struct input_dev *input_dev; 310 struct resource *res; 311 int error; 312 313 kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL); 314 if (!kp) 315 return -ENOMEM; 316 317 input_dev = devm_input_allocate_device(&pdev->dev); 318 if (!input_dev) { 319 dev_err(&pdev->dev, "failed to allocate the input device\n"); 320 return -ENOMEM; 321 } 322 323 __set_bit(EV_KEY, input_dev->evbit); 324 325 /* Enable auto repeat feature of Linux input subsystem */ 326 if (of_property_read_bool(pdev->dev.of_node, "autorepeat")) 327 __set_bit(EV_REP, input_dev->evbit); 328 329 input_dev->name = pdev->name; 330 input_dev->phys = "keypad/input0"; 331 input_dev->dev.parent = &pdev->dev; 332 input_dev->open = bcm_kp_open; 333 input_dev->close = bcm_kp_close; 334 335 input_dev->id.bustype = BUS_HOST; 336 input_dev->id.vendor = 0x0001; 337 input_dev->id.product = 0x0001; 338 input_dev->id.version = 0x0100; 339 340 input_set_drvdata(input_dev, kp); 341 342 kp->input_dev = input_dev; 343 344 error = bcm_kp_matrix_key_parse_dt(kp); 345 if (error) 346 return error; 347 348 error = matrix_keypad_build_keymap(NULL, NULL, 349 kp->n_rows, kp->n_cols, 350 NULL, input_dev); 351 if (error) { 352 dev_err(&pdev->dev, "failed to build keymap\n"); 353 return error; 354 } 355 356 /* Get the KEYPAD base address */ 357 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 358 if (!res) { 359 dev_err(&pdev->dev, "Missing keypad base address resource\n"); 360 return -ENODEV; 361 } 362 363 kp->base = devm_ioremap_resource(&pdev->dev, res); 364 if (IS_ERR(kp->base)) 365 return PTR_ERR(kp->base); 366 367 /* Enable clock */ 368 kp->clk = devm_clk_get(&pdev->dev, "peri_clk"); 369 if (IS_ERR(kp->clk)) { 370 error = PTR_ERR(kp->clk); 371 if (error != -ENOENT) { 372 if (error != -EPROBE_DEFER) 373 dev_err(&pdev->dev, "Failed to get clock\n"); 374 return error; 375 } 376 dev_dbg(&pdev->dev, 377 "No clock specified. Assuming it's enabled\n"); 378 kp->clk = NULL; 379 } else { 380 unsigned int desired_rate; 381 long actual_rate; 382 383 error = of_property_read_u32(pdev->dev.of_node, 384 "clock-frequency", &desired_rate); 385 if (error < 0) 386 desired_rate = DEFAULT_CLK_HZ; 387 388 actual_rate = clk_round_rate(kp->clk, desired_rate); 389 if (actual_rate <= 0) 390 return -EINVAL; 391 392 error = clk_set_rate(kp->clk, actual_rate); 393 if (error) 394 return error; 395 396 error = clk_prepare_enable(kp->clk); 397 if (error) 398 return error; 399 } 400 401 /* Put the kp into a known sane state */ 402 bcm_kp_stop(kp); 403 404 kp->irq = platform_get_irq(pdev, 0); 405 if (kp->irq < 0) 406 return -EINVAL; 407 408 error = devm_request_threaded_irq(&pdev->dev, kp->irq, 409 NULL, bcm_kp_isr_thread, 410 IRQF_ONESHOT, pdev->name, kp); 411 if (error) { 412 dev_err(&pdev->dev, "failed to request IRQ\n"); 413 return error; 414 } 415 416 error = input_register_device(input_dev); 417 if (error) { 418 dev_err(&pdev->dev, "failed to register input device\n"); 419 return error; 420 } 421 422 return 0; 423 } 424 425 static const struct of_device_id bcm_kp_of_match[] = { 426 { .compatible = "brcm,bcm-keypad" }, 427 { }, 428 }; 429 MODULE_DEVICE_TABLE(of, bcm_kp_of_match); 430 431 static struct platform_driver bcm_kp_device_driver = { 432 .probe = bcm_kp_probe, 433 .driver = { 434 .name = "bcm-keypad", 435 .of_match_table = of_match_ptr(bcm_kp_of_match), 436 } 437 }; 438 439 module_platform_driver(bcm_kp_device_driver); 440 441 MODULE_AUTHOR("Broadcom Corporation"); 442 MODULE_DESCRIPTION("BCM Keypad Driver"); 443 MODULE_LICENSE("GPL v2"); 444