19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 23e9f0b3eSChen Zhong /* 33e9f0b3eSChen Zhong * Copyright (C) 2017 MediaTek, Inc. 43e9f0b3eSChen Zhong * 53e9f0b3eSChen Zhong * Author: Chen Zhong <chen.zhong@mediatek.com> 63e9f0b3eSChen Zhong */ 73e9f0b3eSChen Zhong 83e9f0b3eSChen Zhong #include <linux/input.h> 93e9f0b3eSChen Zhong #include <linux/interrupt.h> 103e9f0b3eSChen Zhong #include <linux/kernel.h> 113e9f0b3eSChen Zhong #include <linux/mfd/mt6323/registers.h> 12cd01569bSAngeloGioacchino Del Regno #include <linux/mfd/mt6331/registers.h> 13994673dcSMattijs Korpershoek #include <linux/mfd/mt6358/registers.h> 143e9f0b3eSChen Zhong #include <linux/mfd/mt6397/core.h> 151eb7ea26SYueHaibing #include <linux/mfd/mt6397/registers.h> 161eb7ea26SYueHaibing #include <linux/module.h> 171eb7ea26SYueHaibing #include <linux/of_device.h> 181eb7ea26SYueHaibing #include <linux/of.h> 191eb7ea26SYueHaibing #include <linux/platform_device.h> 201eb7ea26SYueHaibing #include <linux/regmap.h> 213e9f0b3eSChen Zhong 222a0a5828SDmitry Torokhov #define MTK_PMIC_RST_DU_MASK GENMASK(9, 8) 232a0a5828SDmitry Torokhov #define MTK_PMIC_PWRKEY_RST BIT(6) 242a0a5828SDmitry Torokhov #define MTK_PMIC_HOMEKEY_RST BIT(5) 253e9f0b3eSChen Zhong 26cd01569bSAngeloGioacchino Del Regno #define MTK_PMIC_MT6331_RST_DU_MASK GENMASK(13, 12) 27cd01569bSAngeloGioacchino Del Regno #define MTK_PMIC_MT6331_PWRKEY_RST BIT(9) 28cd01569bSAngeloGioacchino Del Regno #define MTK_PMIC_MT6331_HOMEKEY_RST BIT(8) 29cd01569bSAngeloGioacchino Del Regno 303e9f0b3eSChen Zhong #define MTK_PMIC_PWRKEY_INDEX 0 313e9f0b3eSChen Zhong #define MTK_PMIC_HOMEKEY_INDEX 1 323e9f0b3eSChen Zhong #define MTK_PMIC_MAX_KEY_COUNT 2 333e9f0b3eSChen Zhong 343e9f0b3eSChen Zhong struct mtk_pmic_keys_regs { 353e9f0b3eSChen Zhong u32 deb_reg; 363e9f0b3eSChen Zhong u32 deb_mask; 373e9f0b3eSChen Zhong u32 intsel_reg; 383e9f0b3eSChen Zhong u32 intsel_mask; 39b581acb4SAngeloGioacchino Del Regno u32 rst_en_mask; 403e9f0b3eSChen Zhong }; 413e9f0b3eSChen Zhong 423e9f0b3eSChen Zhong #define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask, \ 43b581acb4SAngeloGioacchino Del Regno _intsel_reg, _intsel_mask, _rst_mask) \ 443e9f0b3eSChen Zhong { \ 453e9f0b3eSChen Zhong .deb_reg = _deb_reg, \ 463e9f0b3eSChen Zhong .deb_mask = _deb_mask, \ 473e9f0b3eSChen Zhong .intsel_reg = _intsel_reg, \ 483e9f0b3eSChen Zhong .intsel_mask = _intsel_mask, \ 49b581acb4SAngeloGioacchino Del Regno .rst_en_mask = _rst_mask, \ 503e9f0b3eSChen Zhong } 513e9f0b3eSChen Zhong 523e9f0b3eSChen Zhong struct mtk_pmic_regs { 533e9f0b3eSChen Zhong const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT]; 543e9f0b3eSChen Zhong u32 pmic_rst_reg; 5569cf890dSAngeloGioacchino Del Regno u32 rst_lprst_mask; /* Long-press reset timeout bitmask */ 563e9f0b3eSChen Zhong }; 573e9f0b3eSChen Zhong 583e9f0b3eSChen Zhong static const struct mtk_pmic_regs mt6397_regs = { 593e9f0b3eSChen Zhong .keys_regs[MTK_PMIC_PWRKEY_INDEX] = 603e9f0b3eSChen Zhong MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS, 61b581acb4SAngeloGioacchino Del Regno 0x8, MT6397_INT_RSV, 0x10, MTK_PMIC_PWRKEY_RST), 623e9f0b3eSChen Zhong .keys_regs[MTK_PMIC_HOMEKEY_INDEX] = 633e9f0b3eSChen Zhong MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2, 64b581acb4SAngeloGioacchino Del Regno 0x10, MT6397_INT_RSV, 0x8, MTK_PMIC_HOMEKEY_RST), 653e9f0b3eSChen Zhong .pmic_rst_reg = MT6397_TOP_RST_MISC, 6669cf890dSAngeloGioacchino Del Regno .rst_lprst_mask = MTK_PMIC_RST_DU_MASK, 673e9f0b3eSChen Zhong }; 683e9f0b3eSChen Zhong 693e9f0b3eSChen Zhong static const struct mtk_pmic_regs mt6323_regs = { 703e9f0b3eSChen Zhong .keys_regs[MTK_PMIC_PWRKEY_INDEX] = 713e9f0b3eSChen Zhong MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS, 72b581acb4SAngeloGioacchino Del Regno 0x2, MT6323_INT_MISC_CON, 0x10, MTK_PMIC_PWRKEY_RST), 733e9f0b3eSChen Zhong .keys_regs[MTK_PMIC_HOMEKEY_INDEX] = 743e9f0b3eSChen Zhong MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS, 75b581acb4SAngeloGioacchino Del Regno 0x4, MT6323_INT_MISC_CON, 0x8, MTK_PMIC_HOMEKEY_RST), 763e9f0b3eSChen Zhong .pmic_rst_reg = MT6323_TOP_RST_MISC, 7769cf890dSAngeloGioacchino Del Regno .rst_lprst_mask = MTK_PMIC_RST_DU_MASK, 783e9f0b3eSChen Zhong }; 793e9f0b3eSChen Zhong 80cd01569bSAngeloGioacchino Del Regno static const struct mtk_pmic_regs mt6331_regs = { 81cd01569bSAngeloGioacchino Del Regno .keys_regs[MTK_PMIC_PWRKEY_INDEX] = 82cd01569bSAngeloGioacchino Del Regno MTK_PMIC_KEYS_REGS(MT6331_TOPSTATUS, 0x2, 83cd01569bSAngeloGioacchino Del Regno MT6331_INT_MISC_CON, 0x4, 84cd01569bSAngeloGioacchino Del Regno MTK_PMIC_MT6331_PWRKEY_RST), 85cd01569bSAngeloGioacchino Del Regno .keys_regs[MTK_PMIC_HOMEKEY_INDEX] = 86cd01569bSAngeloGioacchino Del Regno MTK_PMIC_KEYS_REGS(MT6331_TOPSTATUS, 0x4, 87cd01569bSAngeloGioacchino Del Regno MT6331_INT_MISC_CON, 0x2, 88cd01569bSAngeloGioacchino Del Regno MTK_PMIC_MT6331_HOMEKEY_RST), 89cd01569bSAngeloGioacchino Del Regno .pmic_rst_reg = MT6331_TOP_RST_MISC, 90cd01569bSAngeloGioacchino Del Regno .rst_lprst_mask = MTK_PMIC_MT6331_RST_DU_MASK, 91cd01569bSAngeloGioacchino Del Regno }; 92cd01569bSAngeloGioacchino Del Regno 93994673dcSMattijs Korpershoek static const struct mtk_pmic_regs mt6358_regs = { 94994673dcSMattijs Korpershoek .keys_regs[MTK_PMIC_PWRKEY_INDEX] = 95994673dcSMattijs Korpershoek MTK_PMIC_KEYS_REGS(MT6358_TOPSTATUS, 96b581acb4SAngeloGioacchino Del Regno 0x2, MT6358_PSC_TOP_INT_CON0, 0x5, 97b581acb4SAngeloGioacchino Del Regno MTK_PMIC_PWRKEY_RST), 98994673dcSMattijs Korpershoek .keys_regs[MTK_PMIC_HOMEKEY_INDEX] = 99994673dcSMattijs Korpershoek MTK_PMIC_KEYS_REGS(MT6358_TOPSTATUS, 100b581acb4SAngeloGioacchino Del Regno 0x8, MT6358_PSC_TOP_INT_CON0, 0xa, 101b581acb4SAngeloGioacchino Del Regno MTK_PMIC_HOMEKEY_RST), 102994673dcSMattijs Korpershoek .pmic_rst_reg = MT6358_TOP_RST_MISC, 10369cf890dSAngeloGioacchino Del Regno .rst_lprst_mask = MTK_PMIC_RST_DU_MASK, 104994673dcSMattijs Korpershoek }; 105994673dcSMattijs Korpershoek 1063e9f0b3eSChen Zhong struct mtk_pmic_keys_info { 1073e9f0b3eSChen Zhong struct mtk_pmic_keys *keys; 1083e9f0b3eSChen Zhong const struct mtk_pmic_keys_regs *regs; 1093e9f0b3eSChen Zhong unsigned int keycode; 1103e9f0b3eSChen Zhong int irq; 111994673dcSMattijs Korpershoek int irq_r; /* optional: release irq if different */ 1123e9f0b3eSChen Zhong bool wakeup:1; 1133e9f0b3eSChen Zhong }; 1143e9f0b3eSChen Zhong 1153e9f0b3eSChen Zhong struct mtk_pmic_keys { 1163e9f0b3eSChen Zhong struct input_dev *input_dev; 1173e9f0b3eSChen Zhong struct device *dev; 1183e9f0b3eSChen Zhong struct regmap *regmap; 1193e9f0b3eSChen Zhong struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT]; 1203e9f0b3eSChen Zhong }; 1213e9f0b3eSChen Zhong 1223e9f0b3eSChen Zhong enum mtk_pmic_keys_lp_mode { 1233e9f0b3eSChen Zhong LP_DISABLE, 1243e9f0b3eSChen Zhong LP_ONEKEY, 1253e9f0b3eSChen Zhong LP_TWOKEY, 1263e9f0b3eSChen Zhong }; 1273e9f0b3eSChen Zhong 1283e9f0b3eSChen Zhong static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys, 12969cf890dSAngeloGioacchino Del Regno const struct mtk_pmic_regs *regs) 1303e9f0b3eSChen Zhong { 131b581acb4SAngeloGioacchino Del Regno const struct mtk_pmic_keys_regs *kregs_home, *kregs_pwr; 1323e9f0b3eSChen Zhong u32 long_press_mode, long_press_debounce; 1332a0a5828SDmitry Torokhov u32 value, mask; 1342a0a5828SDmitry Torokhov int error; 1353e9f0b3eSChen Zhong 136b581acb4SAngeloGioacchino Del Regno kregs_home = keys->keys[MTK_PMIC_HOMEKEY_INDEX].regs; 137b581acb4SAngeloGioacchino Del Regno kregs_pwr = keys->keys[MTK_PMIC_PWRKEY_INDEX].regs; 138b581acb4SAngeloGioacchino Del Regno 1392a0a5828SDmitry Torokhov error = of_property_read_u32(keys->dev->of_node, "power-off-time-sec", 1402a0a5828SDmitry Torokhov &long_press_debounce); 1412a0a5828SDmitry Torokhov if (error) 1423e9f0b3eSChen Zhong long_press_debounce = 0; 1433e9f0b3eSChen Zhong 14469cf890dSAngeloGioacchino Del Regno mask = regs->rst_lprst_mask; 14569cf890dSAngeloGioacchino Del Regno value = long_press_debounce << (ffs(regs->rst_lprst_mask) - 1); 1463e9f0b3eSChen Zhong 1472a0a5828SDmitry Torokhov error = of_property_read_u32(keys->dev->of_node, 1482a0a5828SDmitry Torokhov "mediatek,long-press-mode", 1492a0a5828SDmitry Torokhov &long_press_mode); 1502a0a5828SDmitry Torokhov if (error) 1513e9f0b3eSChen Zhong long_press_mode = LP_DISABLE; 1523e9f0b3eSChen Zhong 1533e9f0b3eSChen Zhong switch (long_press_mode) { 1543e9f0b3eSChen Zhong case LP_TWOKEY: 155b581acb4SAngeloGioacchino Del Regno value |= kregs_home->rst_en_mask; 1562a0a5828SDmitry Torokhov fallthrough; 1572a0a5828SDmitry Torokhov 1582a0a5828SDmitry Torokhov case LP_ONEKEY: 159b581acb4SAngeloGioacchino Del Regno value |= kregs_pwr->rst_en_mask; 1602a0a5828SDmitry Torokhov fallthrough; 1612a0a5828SDmitry Torokhov 1623e9f0b3eSChen Zhong case LP_DISABLE: 163b581acb4SAngeloGioacchino Del Regno mask |= kregs_home->rst_en_mask; 164b581acb4SAngeloGioacchino Del Regno mask |= kregs_pwr->rst_en_mask; 1653e9f0b3eSChen Zhong break; 1662a0a5828SDmitry Torokhov 1673e9f0b3eSChen Zhong default: 1683e9f0b3eSChen Zhong break; 1693e9f0b3eSChen Zhong } 1702a0a5828SDmitry Torokhov 17169cf890dSAngeloGioacchino Del Regno regmap_update_bits(keys->regmap, regs->pmic_rst_reg, mask, value); 1723e9f0b3eSChen Zhong } 1733e9f0b3eSChen Zhong 1743e9f0b3eSChen Zhong static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data) 1753e9f0b3eSChen Zhong { 1763e9f0b3eSChen Zhong struct mtk_pmic_keys_info *info = data; 1773e9f0b3eSChen Zhong u32 key_deb, pressed; 1783e9f0b3eSChen Zhong 1793e9f0b3eSChen Zhong regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb); 1803e9f0b3eSChen Zhong 1813e9f0b3eSChen Zhong key_deb &= info->regs->deb_mask; 1823e9f0b3eSChen Zhong 1833e9f0b3eSChen Zhong pressed = !key_deb; 1843e9f0b3eSChen Zhong 1853e9f0b3eSChen Zhong input_report_key(info->keys->input_dev, info->keycode, pressed); 1863e9f0b3eSChen Zhong input_sync(info->keys->input_dev); 1873e9f0b3eSChen Zhong 1883e9f0b3eSChen Zhong dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n", 1893e9f0b3eSChen Zhong pressed ? "pressed" : "released", info->keycode); 1903e9f0b3eSChen Zhong 1913e9f0b3eSChen Zhong return IRQ_HANDLED; 1923e9f0b3eSChen Zhong } 1933e9f0b3eSChen Zhong 1943e9f0b3eSChen Zhong static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys, 1953e9f0b3eSChen Zhong struct mtk_pmic_keys_info *info) 1963e9f0b3eSChen Zhong { 1973e9f0b3eSChen Zhong int ret; 1983e9f0b3eSChen Zhong 1993e9f0b3eSChen Zhong info->keys = keys; 2003e9f0b3eSChen Zhong 2013e9f0b3eSChen Zhong ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg, 2023e9f0b3eSChen Zhong info->regs->intsel_mask, 2033e9f0b3eSChen Zhong info->regs->intsel_mask); 2043e9f0b3eSChen Zhong if (ret < 0) 2053e9f0b3eSChen Zhong return ret; 2063e9f0b3eSChen Zhong 2073e9f0b3eSChen Zhong ret = devm_request_threaded_irq(keys->dev, info->irq, NULL, 2083e9f0b3eSChen Zhong mtk_pmic_keys_irq_handler_thread, 2093e9f0b3eSChen Zhong IRQF_ONESHOT | IRQF_TRIGGER_HIGH, 2103e9f0b3eSChen Zhong "mtk-pmic-keys", info); 2113e9f0b3eSChen Zhong if (ret) { 2123e9f0b3eSChen Zhong dev_err(keys->dev, "Failed to request IRQ: %d: %d\n", 2133e9f0b3eSChen Zhong info->irq, ret); 2143e9f0b3eSChen Zhong return ret; 2153e9f0b3eSChen Zhong } 2163e9f0b3eSChen Zhong 217994673dcSMattijs Korpershoek if (info->irq_r > 0) { 218994673dcSMattijs Korpershoek ret = devm_request_threaded_irq(keys->dev, info->irq_r, NULL, 219994673dcSMattijs Korpershoek mtk_pmic_keys_irq_handler_thread, 220994673dcSMattijs Korpershoek IRQF_ONESHOT | IRQF_TRIGGER_HIGH, 221994673dcSMattijs Korpershoek "mtk-pmic-keys", info); 222994673dcSMattijs Korpershoek if (ret) { 223994673dcSMattijs Korpershoek dev_err(keys->dev, "Failed to request IRQ_r: %d: %d\n", 224994673dcSMattijs Korpershoek info->irq, ret); 225994673dcSMattijs Korpershoek return ret; 226994673dcSMattijs Korpershoek } 227994673dcSMattijs Korpershoek } 228994673dcSMattijs Korpershoek 2293e9f0b3eSChen Zhong input_set_capability(keys->input_dev, EV_KEY, info->keycode); 2303e9f0b3eSChen Zhong 2313e9f0b3eSChen Zhong return 0; 2323e9f0b3eSChen Zhong } 2333e9f0b3eSChen Zhong 234*b3462009SJonathan Cameron static int mtk_pmic_keys_suspend(struct device *dev) 2353e9f0b3eSChen Zhong { 2363e9f0b3eSChen Zhong struct mtk_pmic_keys *keys = dev_get_drvdata(dev); 2373e9f0b3eSChen Zhong int index; 2383e9f0b3eSChen Zhong 2393e9f0b3eSChen Zhong for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) { 240994673dcSMattijs Korpershoek if (keys->keys[index].wakeup) { 2413e9f0b3eSChen Zhong enable_irq_wake(keys->keys[index].irq); 242994673dcSMattijs Korpershoek if (keys->keys[index].irq_r > 0) 243994673dcSMattijs Korpershoek enable_irq_wake(keys->keys[index].irq_r); 244994673dcSMattijs Korpershoek } 2453e9f0b3eSChen Zhong } 2463e9f0b3eSChen Zhong 2473e9f0b3eSChen Zhong return 0; 2483e9f0b3eSChen Zhong } 2493e9f0b3eSChen Zhong 250*b3462009SJonathan Cameron static int mtk_pmic_keys_resume(struct device *dev) 2513e9f0b3eSChen Zhong { 2523e9f0b3eSChen Zhong struct mtk_pmic_keys *keys = dev_get_drvdata(dev); 2533e9f0b3eSChen Zhong int index; 2543e9f0b3eSChen Zhong 2553e9f0b3eSChen Zhong for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) { 256994673dcSMattijs Korpershoek if (keys->keys[index].wakeup) { 2573e9f0b3eSChen Zhong disable_irq_wake(keys->keys[index].irq); 258994673dcSMattijs Korpershoek if (keys->keys[index].irq_r > 0) 259994673dcSMattijs Korpershoek disable_irq_wake(keys->keys[index].irq_r); 260994673dcSMattijs Korpershoek } 2613e9f0b3eSChen Zhong } 2623e9f0b3eSChen Zhong 2633e9f0b3eSChen Zhong return 0; 2643e9f0b3eSChen Zhong } 2653e9f0b3eSChen Zhong 266*b3462009SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend, 2673e9f0b3eSChen Zhong mtk_pmic_keys_resume); 2683e9f0b3eSChen Zhong 2693e9f0b3eSChen Zhong static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = { 2703e9f0b3eSChen Zhong { 2713e9f0b3eSChen Zhong .compatible = "mediatek,mt6397-keys", 2723e9f0b3eSChen Zhong .data = &mt6397_regs, 2733e9f0b3eSChen Zhong }, { 2743e9f0b3eSChen Zhong .compatible = "mediatek,mt6323-keys", 2753e9f0b3eSChen Zhong .data = &mt6323_regs, 2763e9f0b3eSChen Zhong }, { 277cd01569bSAngeloGioacchino Del Regno .compatible = "mediatek,mt6331-keys", 278cd01569bSAngeloGioacchino Del Regno .data = &mt6331_regs, 279cd01569bSAngeloGioacchino Del Regno }, { 280994673dcSMattijs Korpershoek .compatible = "mediatek,mt6358-keys", 281994673dcSMattijs Korpershoek .data = &mt6358_regs, 282994673dcSMattijs Korpershoek }, { 2833e9f0b3eSChen Zhong /* sentinel */ 2843e9f0b3eSChen Zhong } 2853e9f0b3eSChen Zhong }; 2863e9f0b3eSChen Zhong MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl); 2873e9f0b3eSChen Zhong 2883e9f0b3eSChen Zhong static int mtk_pmic_keys_probe(struct platform_device *pdev) 2893e9f0b3eSChen Zhong { 2903e9f0b3eSChen Zhong int error, index = 0; 2913e9f0b3eSChen Zhong unsigned int keycount; 2923e9f0b3eSChen Zhong struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent); 2933e9f0b3eSChen Zhong struct device_node *node = pdev->dev.of_node, *child; 294f4e09e8bSMattijs Korpershoek static const char *const irqnames[] = { "powerkey", "homekey" }; 295994673dcSMattijs Korpershoek static const char *const irqnames_r[] = { "powerkey_r", "homekey_r" }; 2963e9f0b3eSChen Zhong struct mtk_pmic_keys *keys; 2973e9f0b3eSChen Zhong const struct mtk_pmic_regs *mtk_pmic_regs; 2983e9f0b3eSChen Zhong struct input_dev *input_dev; 2993e9f0b3eSChen Zhong const struct of_device_id *of_id = 3003e9f0b3eSChen Zhong of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev); 3013e9f0b3eSChen Zhong 3023e9f0b3eSChen Zhong keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL); 3033e9f0b3eSChen Zhong if (!keys) 3043e9f0b3eSChen Zhong return -ENOMEM; 3053e9f0b3eSChen Zhong 3063e9f0b3eSChen Zhong keys->dev = &pdev->dev; 3073e9f0b3eSChen Zhong keys->regmap = pmic_chip->regmap; 3083e9f0b3eSChen Zhong mtk_pmic_regs = of_id->data; 3093e9f0b3eSChen Zhong 3103e9f0b3eSChen Zhong keys->input_dev = input_dev = devm_input_allocate_device(keys->dev); 3113e9f0b3eSChen Zhong if (!input_dev) { 3123e9f0b3eSChen Zhong dev_err(keys->dev, "input allocate device fail.\n"); 3133e9f0b3eSChen Zhong return -ENOMEM; 3143e9f0b3eSChen Zhong } 3153e9f0b3eSChen Zhong 3163e9f0b3eSChen Zhong input_dev->name = "mtk-pmic-keys"; 3173e9f0b3eSChen Zhong input_dev->id.bustype = BUS_HOST; 3183e9f0b3eSChen Zhong input_dev->id.vendor = 0x0001; 3193e9f0b3eSChen Zhong input_dev->id.product = 0x0001; 3203e9f0b3eSChen Zhong input_dev->id.version = 0x0001; 3213e9f0b3eSChen Zhong 3223e9f0b3eSChen Zhong keycount = of_get_available_child_count(node); 323f4e09e8bSMattijs Korpershoek if (keycount > MTK_PMIC_MAX_KEY_COUNT || 324f4e09e8bSMattijs Korpershoek keycount > ARRAY_SIZE(irqnames)) { 3253e9f0b3eSChen Zhong dev_err(keys->dev, "too many keys defined (%d)\n", keycount); 3263e9f0b3eSChen Zhong return -EINVAL; 3273e9f0b3eSChen Zhong } 3283e9f0b3eSChen Zhong 3293e9f0b3eSChen Zhong for_each_child_of_node(node, child) { 3303e9f0b3eSChen Zhong keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index]; 3313e9f0b3eSChen Zhong 332f4e09e8bSMattijs Korpershoek keys->keys[index].irq = 333f4e09e8bSMattijs Korpershoek platform_get_irq_byname(pdev, irqnames[index]); 334cc95527cSNishka Dasgupta if (keys->keys[index].irq < 0) { 335cc95527cSNishka Dasgupta of_node_put(child); 3363e9f0b3eSChen Zhong return keys->keys[index].irq; 337cc95527cSNishka Dasgupta } 3383e9f0b3eSChen Zhong 339994673dcSMattijs Korpershoek if (of_device_is_compatible(node, "mediatek,mt6358-keys")) { 340994673dcSMattijs Korpershoek keys->keys[index].irq_r = platform_get_irq_byname(pdev, 341994673dcSMattijs Korpershoek irqnames_r[index]); 342994673dcSMattijs Korpershoek 343994673dcSMattijs Korpershoek if (keys->keys[index].irq_r < 0) { 344994673dcSMattijs Korpershoek of_node_put(child); 345994673dcSMattijs Korpershoek return keys->keys[index].irq_r; 346994673dcSMattijs Korpershoek } 347994673dcSMattijs Korpershoek } 348994673dcSMattijs Korpershoek 3493e9f0b3eSChen Zhong error = of_property_read_u32(child, 3503e9f0b3eSChen Zhong "linux,keycodes", &keys->keys[index].keycode); 3513e9f0b3eSChen Zhong if (error) { 3523e9f0b3eSChen Zhong dev_err(keys->dev, 3533e9f0b3eSChen Zhong "failed to read key:%d linux,keycode property: %d\n", 3543e9f0b3eSChen Zhong index, error); 355cc95527cSNishka Dasgupta of_node_put(child); 3563e9f0b3eSChen Zhong return error; 3573e9f0b3eSChen Zhong } 3583e9f0b3eSChen Zhong 3593e9f0b3eSChen Zhong if (of_property_read_bool(child, "wakeup-source")) 3603e9f0b3eSChen Zhong keys->keys[index].wakeup = true; 3613e9f0b3eSChen Zhong 3623e9f0b3eSChen Zhong error = mtk_pmic_key_setup(keys, &keys->keys[index]); 363cc95527cSNishka Dasgupta if (error) { 364cc95527cSNishka Dasgupta of_node_put(child); 3653e9f0b3eSChen Zhong return error; 366cc95527cSNishka Dasgupta } 3673e9f0b3eSChen Zhong 3683e9f0b3eSChen Zhong index++; 3693e9f0b3eSChen Zhong } 3703e9f0b3eSChen Zhong 3713e9f0b3eSChen Zhong error = input_register_device(input_dev); 3723e9f0b3eSChen Zhong if (error) { 3733e9f0b3eSChen Zhong dev_err(&pdev->dev, 3743e9f0b3eSChen Zhong "register input device failed (%d)\n", error); 3753e9f0b3eSChen Zhong return error; 3763e9f0b3eSChen Zhong } 3773e9f0b3eSChen Zhong 37869cf890dSAngeloGioacchino Del Regno mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs); 3793e9f0b3eSChen Zhong 3803e9f0b3eSChen Zhong platform_set_drvdata(pdev, keys); 3813e9f0b3eSChen Zhong 3823e9f0b3eSChen Zhong return 0; 3833e9f0b3eSChen Zhong } 3843e9f0b3eSChen Zhong 3853e9f0b3eSChen Zhong static struct platform_driver pmic_keys_pdrv = { 3863e9f0b3eSChen Zhong .probe = mtk_pmic_keys_probe, 3873e9f0b3eSChen Zhong .driver = { 3883e9f0b3eSChen Zhong .name = "mtk-pmic-keys", 3893e9f0b3eSChen Zhong .of_match_table = of_mtk_pmic_keys_match_tbl, 390*b3462009SJonathan Cameron .pm = pm_sleep_ptr(&mtk_pmic_keys_pm_ops), 3913e9f0b3eSChen Zhong }, 3923e9f0b3eSChen Zhong }; 3933e9f0b3eSChen Zhong 3943e9f0b3eSChen Zhong module_platform_driver(pmic_keys_pdrv); 3953e9f0b3eSChen Zhong 3963e9f0b3eSChen Zhong MODULE_LICENSE("GPL v2"); 3973e9f0b3eSChen Zhong MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>"); 3983e9f0b3eSChen Zhong MODULE_DESCRIPTION("MTK pmic-keys driver v0.1"); 399