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