1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2022 MediaTek Inc. 4 * Author Fengping Yu <fengping.yu@mediatek.com> 5 */ 6 #include <linux/bitops.h> 7 #include <linux/clk.h> 8 #include <linux/input/matrix_keypad.h> 9 #include <linux/interrupt.h> 10 #include <linux/module.h> 11 #include <linux/property.h> 12 #include <linux/platform_device.h> 13 #include <linux/regmap.h> 14 15 #define MTK_KPD_NAME "mt6779-keypad" 16 #define MTK_KPD_MEM 0x0004 17 #define MTK_KPD_DEBOUNCE 0x0018 18 #define MTK_KPD_DEBOUNCE_MASK GENMASK(13, 0) 19 #define MTK_KPD_DEBOUNCE_MAX_MS 256 20 #define MTK_KPD_SEL 0x0020 21 #define MTK_KPD_SEL_COL GENMASK(15, 10) 22 #define MTK_KPD_SEL_ROW GENMASK(9, 4) 23 #define MTK_KPD_SEL_COLMASK(c) GENMASK((c) + 9, 10) 24 #define MTK_KPD_SEL_ROWMASK(r) GENMASK((r) + 3, 4) 25 #define MTK_KPD_NUM_MEMS 5 26 #define MTK_KPD_NUM_BITS 136 /* 4*32+8 MEM5 only use 8 BITS */ 27 28 struct mt6779_keypad { 29 struct regmap *regmap; 30 struct input_dev *input_dev; 31 struct clk *clk; 32 u32 n_rows; 33 u32 n_cols; 34 DECLARE_BITMAP(keymap_state, MTK_KPD_NUM_BITS); 35 }; 36 37 static const struct regmap_config mt6779_keypad_regmap_cfg = { 38 .reg_bits = 32, 39 .val_bits = 32, 40 .reg_stride = sizeof(u32), 41 .max_register = 36, 42 }; 43 44 static irqreturn_t mt6779_keypad_irq_handler(int irq, void *dev_id) 45 { 46 struct mt6779_keypad *keypad = dev_id; 47 const unsigned short *keycode = keypad->input_dev->keycode; 48 DECLARE_BITMAP(new_state, MTK_KPD_NUM_BITS); 49 DECLARE_BITMAP(change, MTK_KPD_NUM_BITS); 50 unsigned int bit_nr, key; 51 unsigned int row, col; 52 unsigned int scancode; 53 unsigned int row_shift = get_count_order(keypad->n_cols); 54 bool pressed; 55 56 regmap_bulk_read(keypad->regmap, MTK_KPD_MEM, 57 new_state, MTK_KPD_NUM_MEMS); 58 59 bitmap_xor(change, new_state, keypad->keymap_state, MTK_KPD_NUM_BITS); 60 61 for_each_set_bit(bit_nr, change, MTK_KPD_NUM_BITS) { 62 /* 63 * Registers are 32bits, but only bits [15:0] are used to 64 * indicate key status. 65 */ 66 if (bit_nr % 32 >= 16) 67 continue; 68 69 key = bit_nr / 32 * 16 + bit_nr % 32; 70 row = key / 9; 71 col = key % 9; 72 73 scancode = MATRIX_SCAN_CODE(row, col, row_shift); 74 /* 1: not pressed, 0: pressed */ 75 pressed = !test_bit(bit_nr, new_state); 76 dev_dbg(&keypad->input_dev->dev, "%s", 77 pressed ? "pressed" : "released"); 78 79 input_event(keypad->input_dev, EV_MSC, MSC_SCAN, scancode); 80 input_report_key(keypad->input_dev, keycode[scancode], pressed); 81 input_sync(keypad->input_dev); 82 83 dev_dbg(&keypad->input_dev->dev, 84 "report Linux keycode = %d\n", keycode[scancode]); 85 } 86 87 bitmap_copy(keypad->keymap_state, new_state, MTK_KPD_NUM_BITS); 88 89 return IRQ_HANDLED; 90 } 91 92 static void mt6779_keypad_clk_disable(void *data) 93 { 94 clk_disable_unprepare(data); 95 } 96 97 static int mt6779_keypad_pdrv_probe(struct platform_device *pdev) 98 { 99 struct mt6779_keypad *keypad; 100 void __iomem *base; 101 int irq; 102 u32 debounce; 103 bool wakeup; 104 int error; 105 106 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL); 107 if (!keypad) 108 return -ENOMEM; 109 110 base = devm_platform_ioremap_resource(pdev, 0); 111 if (IS_ERR(base)) 112 return PTR_ERR(base); 113 114 keypad->regmap = devm_regmap_init_mmio(&pdev->dev, base, 115 &mt6779_keypad_regmap_cfg); 116 if (IS_ERR(keypad->regmap)) { 117 dev_err(&pdev->dev, 118 "regmap init failed:%pe\n", keypad->regmap); 119 return PTR_ERR(keypad->regmap); 120 } 121 122 bitmap_fill(keypad->keymap_state, MTK_KPD_NUM_BITS); 123 124 keypad->input_dev = devm_input_allocate_device(&pdev->dev); 125 if (!keypad->input_dev) { 126 dev_err(&pdev->dev, "Failed to allocate input dev\n"); 127 return -ENOMEM; 128 } 129 130 keypad->input_dev->name = MTK_KPD_NAME; 131 keypad->input_dev->id.bustype = BUS_HOST; 132 133 error = matrix_keypad_parse_properties(&pdev->dev, &keypad->n_rows, 134 &keypad->n_cols); 135 if (error) { 136 dev_err(&pdev->dev, "Failed to parse keypad params\n"); 137 return error; 138 } 139 140 if (device_property_read_u32(&pdev->dev, "debounce-delay-ms", 141 &debounce)) 142 debounce = 16; 143 144 if (debounce > MTK_KPD_DEBOUNCE_MAX_MS) { 145 dev_err(&pdev->dev, 146 "Debounce time exceeds the maximum allowed time %dms\n", 147 MTK_KPD_DEBOUNCE_MAX_MS); 148 return -EINVAL; 149 } 150 151 wakeup = device_property_read_bool(&pdev->dev, "wakeup-source"); 152 153 dev_dbg(&pdev->dev, "n_row=%d n_col=%d debounce=%d\n", 154 keypad->n_rows, keypad->n_cols, debounce); 155 156 error = matrix_keypad_build_keymap(NULL, NULL, 157 keypad->n_rows, keypad->n_cols, 158 NULL, keypad->input_dev); 159 if (error) { 160 dev_err(&pdev->dev, "Failed to build keymap\n"); 161 return error; 162 } 163 164 input_set_capability(keypad->input_dev, EV_MSC, MSC_SCAN); 165 166 regmap_write(keypad->regmap, MTK_KPD_DEBOUNCE, 167 (debounce * (1 << 5)) & MTK_KPD_DEBOUNCE_MASK); 168 169 regmap_update_bits(keypad->regmap, MTK_KPD_SEL, MTK_KPD_SEL_ROW, 170 MTK_KPD_SEL_ROWMASK(keypad->n_rows)); 171 regmap_update_bits(keypad->regmap, MTK_KPD_SEL, MTK_KPD_SEL_COL, 172 MTK_KPD_SEL_COLMASK(keypad->n_cols)); 173 174 keypad->clk = devm_clk_get(&pdev->dev, "kpd"); 175 if (IS_ERR(keypad->clk)) 176 return PTR_ERR(keypad->clk); 177 178 error = clk_prepare_enable(keypad->clk); 179 if (error) { 180 dev_err(&pdev->dev, "cannot prepare/enable keypad clock\n"); 181 return error; 182 } 183 184 error = devm_add_action_or_reset(&pdev->dev, mt6779_keypad_clk_disable, 185 keypad->clk); 186 if (error) 187 return error; 188 189 irq = platform_get_irq(pdev, 0); 190 if (irq < 0) 191 return irq; 192 193 error = devm_request_threaded_irq(&pdev->dev, irq, 194 NULL, mt6779_keypad_irq_handler, 195 IRQF_ONESHOT, MTK_KPD_NAME, keypad); 196 if (error) { 197 dev_err(&pdev->dev, "Failed to request IRQ#%d: %d\n", 198 irq, error); 199 return error; 200 } 201 202 error = input_register_device(keypad->input_dev); 203 if (error) { 204 dev_err(&pdev->dev, "Failed to register device\n"); 205 return error; 206 } 207 208 error = device_init_wakeup(&pdev->dev, wakeup); 209 if (error) 210 dev_warn(&pdev->dev, "device_init_wakeup() failed: %d\n", 211 error); 212 213 return 0; 214 } 215 216 static const struct of_device_id mt6779_keypad_of_match[] = { 217 { .compatible = "mediatek,mt6779-keypad" }, 218 { .compatible = "mediatek,mt6873-keypad" }, 219 { /* sentinel */ } 220 }; 221 222 static struct platform_driver mt6779_keypad_pdrv = { 223 .probe = mt6779_keypad_pdrv_probe, 224 .driver = { 225 .name = MTK_KPD_NAME, 226 .of_match_table = mt6779_keypad_of_match, 227 }, 228 }; 229 module_platform_driver(mt6779_keypad_pdrv); 230 231 MODULE_AUTHOR("Mediatek Corporation"); 232 MODULE_DESCRIPTION("MTK Keypad (KPD) Driver"); 233 MODULE_LICENSE("GPL"); 234