1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2017 MediaTek, Inc. 4 * 5 * Author: Chen Zhong <chen.zhong@mediatek.com> 6 */ 7 8 #include <linux/input.h> 9 #include <linux/interrupt.h> 10 #include <linux/kernel.h> 11 #include <linux/mfd/mt6323/registers.h> 12 #include <linux/mfd/mt6358/registers.h> 13 #include <linux/mfd/mt6397/core.h> 14 #include <linux/mfd/mt6397/registers.h> 15 #include <linux/module.h> 16 #include <linux/of_device.h> 17 #include <linux/of.h> 18 #include <linux/platform_device.h> 19 #include <linux/regmap.h> 20 21 #define MTK_PMIC_RST_DU_MASK GENMASK(9, 8) 22 #define MTK_PMIC_PWRKEY_RST BIT(6) 23 #define MTK_PMIC_HOMEKEY_RST BIT(5) 24 25 #define MTK_PMIC_PWRKEY_INDEX 0 26 #define MTK_PMIC_HOMEKEY_INDEX 1 27 #define MTK_PMIC_MAX_KEY_COUNT 2 28 29 struct mtk_pmic_keys_regs { 30 u32 deb_reg; 31 u32 deb_mask; 32 u32 intsel_reg; 33 u32 intsel_mask; 34 u32 rst_en_mask; 35 }; 36 37 #define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask, \ 38 _intsel_reg, _intsel_mask, _rst_mask) \ 39 { \ 40 .deb_reg = _deb_reg, \ 41 .deb_mask = _deb_mask, \ 42 .intsel_reg = _intsel_reg, \ 43 .intsel_mask = _intsel_mask, \ 44 .rst_en_mask = _rst_mask, \ 45 } 46 47 struct mtk_pmic_regs { 48 const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT]; 49 u32 pmic_rst_reg; 50 u32 rst_lprst_mask; /* Long-press reset timeout bitmask */ 51 }; 52 53 static const struct mtk_pmic_regs mt6397_regs = { 54 .keys_regs[MTK_PMIC_PWRKEY_INDEX] = 55 MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS, 56 0x8, MT6397_INT_RSV, 0x10, MTK_PMIC_PWRKEY_RST), 57 .keys_regs[MTK_PMIC_HOMEKEY_INDEX] = 58 MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2, 59 0x10, MT6397_INT_RSV, 0x8, MTK_PMIC_HOMEKEY_RST), 60 .pmic_rst_reg = MT6397_TOP_RST_MISC, 61 .rst_lprst_mask = MTK_PMIC_RST_DU_MASK, 62 }; 63 64 static const struct mtk_pmic_regs mt6323_regs = { 65 .keys_regs[MTK_PMIC_PWRKEY_INDEX] = 66 MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS, 67 0x2, MT6323_INT_MISC_CON, 0x10, MTK_PMIC_PWRKEY_RST), 68 .keys_regs[MTK_PMIC_HOMEKEY_INDEX] = 69 MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS, 70 0x4, MT6323_INT_MISC_CON, 0x8, MTK_PMIC_HOMEKEY_RST), 71 .pmic_rst_reg = MT6323_TOP_RST_MISC, 72 .rst_lprst_mask = MTK_PMIC_RST_DU_MASK, 73 }; 74 75 static const struct mtk_pmic_regs mt6358_regs = { 76 .keys_regs[MTK_PMIC_PWRKEY_INDEX] = 77 MTK_PMIC_KEYS_REGS(MT6358_TOPSTATUS, 78 0x2, MT6358_PSC_TOP_INT_CON0, 0x5, 79 MTK_PMIC_PWRKEY_RST), 80 .keys_regs[MTK_PMIC_HOMEKEY_INDEX] = 81 MTK_PMIC_KEYS_REGS(MT6358_TOPSTATUS, 82 0x8, MT6358_PSC_TOP_INT_CON0, 0xa, 83 MTK_PMIC_HOMEKEY_RST), 84 .pmic_rst_reg = MT6358_TOP_RST_MISC, 85 .rst_lprst_mask = MTK_PMIC_RST_DU_MASK, 86 }; 87 88 struct mtk_pmic_keys_info { 89 struct mtk_pmic_keys *keys; 90 const struct mtk_pmic_keys_regs *regs; 91 unsigned int keycode; 92 int irq; 93 int irq_r; /* optional: release irq if different */ 94 bool wakeup:1; 95 }; 96 97 struct mtk_pmic_keys { 98 struct input_dev *input_dev; 99 struct device *dev; 100 struct regmap *regmap; 101 struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT]; 102 }; 103 104 enum mtk_pmic_keys_lp_mode { 105 LP_DISABLE, 106 LP_ONEKEY, 107 LP_TWOKEY, 108 }; 109 110 static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys, 111 const struct mtk_pmic_regs *regs) 112 { 113 const struct mtk_pmic_keys_regs *kregs_home, *kregs_pwr; 114 u32 long_press_mode, long_press_debounce; 115 u32 value, mask; 116 int error; 117 118 kregs_home = keys->keys[MTK_PMIC_HOMEKEY_INDEX].regs; 119 kregs_pwr = keys->keys[MTK_PMIC_PWRKEY_INDEX].regs; 120 121 error = of_property_read_u32(keys->dev->of_node, "power-off-time-sec", 122 &long_press_debounce); 123 if (error) 124 long_press_debounce = 0; 125 126 mask = regs->rst_lprst_mask; 127 value = long_press_debounce << (ffs(regs->rst_lprst_mask) - 1); 128 129 error = of_property_read_u32(keys->dev->of_node, 130 "mediatek,long-press-mode", 131 &long_press_mode); 132 if (error) 133 long_press_mode = LP_DISABLE; 134 135 switch (long_press_mode) { 136 case LP_TWOKEY: 137 value |= kregs_home->rst_en_mask; 138 fallthrough; 139 140 case LP_ONEKEY: 141 value |= kregs_pwr->rst_en_mask; 142 fallthrough; 143 144 case LP_DISABLE: 145 mask |= kregs_home->rst_en_mask; 146 mask |= kregs_pwr->rst_en_mask; 147 break; 148 149 default: 150 break; 151 } 152 153 regmap_update_bits(keys->regmap, regs->pmic_rst_reg, mask, value); 154 } 155 156 static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data) 157 { 158 struct mtk_pmic_keys_info *info = data; 159 u32 key_deb, pressed; 160 161 regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb); 162 163 key_deb &= info->regs->deb_mask; 164 165 pressed = !key_deb; 166 167 input_report_key(info->keys->input_dev, info->keycode, pressed); 168 input_sync(info->keys->input_dev); 169 170 dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n", 171 pressed ? "pressed" : "released", info->keycode); 172 173 return IRQ_HANDLED; 174 } 175 176 static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys, 177 struct mtk_pmic_keys_info *info) 178 { 179 int ret; 180 181 info->keys = keys; 182 183 ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg, 184 info->regs->intsel_mask, 185 info->regs->intsel_mask); 186 if (ret < 0) 187 return ret; 188 189 ret = devm_request_threaded_irq(keys->dev, info->irq, NULL, 190 mtk_pmic_keys_irq_handler_thread, 191 IRQF_ONESHOT | IRQF_TRIGGER_HIGH, 192 "mtk-pmic-keys", info); 193 if (ret) { 194 dev_err(keys->dev, "Failed to request IRQ: %d: %d\n", 195 info->irq, ret); 196 return ret; 197 } 198 199 if (info->irq_r > 0) { 200 ret = devm_request_threaded_irq(keys->dev, info->irq_r, NULL, 201 mtk_pmic_keys_irq_handler_thread, 202 IRQF_ONESHOT | IRQF_TRIGGER_HIGH, 203 "mtk-pmic-keys", info); 204 if (ret) { 205 dev_err(keys->dev, "Failed to request IRQ_r: %d: %d\n", 206 info->irq, ret); 207 return ret; 208 } 209 } 210 211 input_set_capability(keys->input_dev, EV_KEY, info->keycode); 212 213 return 0; 214 } 215 216 static int __maybe_unused mtk_pmic_keys_suspend(struct device *dev) 217 { 218 struct mtk_pmic_keys *keys = dev_get_drvdata(dev); 219 int index; 220 221 for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) { 222 if (keys->keys[index].wakeup) { 223 enable_irq_wake(keys->keys[index].irq); 224 if (keys->keys[index].irq_r > 0) 225 enable_irq_wake(keys->keys[index].irq_r); 226 } 227 } 228 229 return 0; 230 } 231 232 static int __maybe_unused mtk_pmic_keys_resume(struct device *dev) 233 { 234 struct mtk_pmic_keys *keys = dev_get_drvdata(dev); 235 int index; 236 237 for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) { 238 if (keys->keys[index].wakeup) { 239 disable_irq_wake(keys->keys[index].irq); 240 if (keys->keys[index].irq_r > 0) 241 disable_irq_wake(keys->keys[index].irq_r); 242 } 243 } 244 245 return 0; 246 } 247 248 static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend, 249 mtk_pmic_keys_resume); 250 251 static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = { 252 { 253 .compatible = "mediatek,mt6397-keys", 254 .data = &mt6397_regs, 255 }, { 256 .compatible = "mediatek,mt6323-keys", 257 .data = &mt6323_regs, 258 }, { 259 .compatible = "mediatek,mt6358-keys", 260 .data = &mt6358_regs, 261 }, { 262 /* sentinel */ 263 } 264 }; 265 MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl); 266 267 static int mtk_pmic_keys_probe(struct platform_device *pdev) 268 { 269 int error, index = 0; 270 unsigned int keycount; 271 struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent); 272 struct device_node *node = pdev->dev.of_node, *child; 273 static const char *const irqnames[] = { "powerkey", "homekey" }; 274 static const char *const irqnames_r[] = { "powerkey_r", "homekey_r" }; 275 struct mtk_pmic_keys *keys; 276 const struct mtk_pmic_regs *mtk_pmic_regs; 277 struct input_dev *input_dev; 278 const struct of_device_id *of_id = 279 of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev); 280 281 keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL); 282 if (!keys) 283 return -ENOMEM; 284 285 keys->dev = &pdev->dev; 286 keys->regmap = pmic_chip->regmap; 287 mtk_pmic_regs = of_id->data; 288 289 keys->input_dev = input_dev = devm_input_allocate_device(keys->dev); 290 if (!input_dev) { 291 dev_err(keys->dev, "input allocate device fail.\n"); 292 return -ENOMEM; 293 } 294 295 input_dev->name = "mtk-pmic-keys"; 296 input_dev->id.bustype = BUS_HOST; 297 input_dev->id.vendor = 0x0001; 298 input_dev->id.product = 0x0001; 299 input_dev->id.version = 0x0001; 300 301 keycount = of_get_available_child_count(node); 302 if (keycount > MTK_PMIC_MAX_KEY_COUNT || 303 keycount > ARRAY_SIZE(irqnames)) { 304 dev_err(keys->dev, "too many keys defined (%d)\n", keycount); 305 return -EINVAL; 306 } 307 308 for_each_child_of_node(node, child) { 309 keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index]; 310 311 keys->keys[index].irq = 312 platform_get_irq_byname(pdev, irqnames[index]); 313 if (keys->keys[index].irq < 0) { 314 of_node_put(child); 315 return keys->keys[index].irq; 316 } 317 318 if (of_device_is_compatible(node, "mediatek,mt6358-keys")) { 319 keys->keys[index].irq_r = platform_get_irq_byname(pdev, 320 irqnames_r[index]); 321 322 if (keys->keys[index].irq_r < 0) { 323 of_node_put(child); 324 return keys->keys[index].irq_r; 325 } 326 } 327 328 error = of_property_read_u32(child, 329 "linux,keycodes", &keys->keys[index].keycode); 330 if (error) { 331 dev_err(keys->dev, 332 "failed to read key:%d linux,keycode property: %d\n", 333 index, error); 334 of_node_put(child); 335 return error; 336 } 337 338 if (of_property_read_bool(child, "wakeup-source")) 339 keys->keys[index].wakeup = true; 340 341 error = mtk_pmic_key_setup(keys, &keys->keys[index]); 342 if (error) { 343 of_node_put(child); 344 return error; 345 } 346 347 index++; 348 } 349 350 error = input_register_device(input_dev); 351 if (error) { 352 dev_err(&pdev->dev, 353 "register input device failed (%d)\n", error); 354 return error; 355 } 356 357 mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs); 358 359 platform_set_drvdata(pdev, keys); 360 361 return 0; 362 } 363 364 static struct platform_driver pmic_keys_pdrv = { 365 .probe = mtk_pmic_keys_probe, 366 .driver = { 367 .name = "mtk-pmic-keys", 368 .of_match_table = of_mtk_pmic_keys_match_tbl, 369 .pm = &mtk_pmic_keys_pm_ops, 370 }, 371 }; 372 373 module_platform_driver(pmic_keys_pdrv); 374 375 MODULE_LICENSE("GPL v2"); 376 MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>"); 377 MODULE_DESCRIPTION("MTK pmic-keys driver v0.1"); 378