1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 MediaTek, Inc.
5 * Author: Chen Zhong <chen.zhong@mediatek.com>
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/mt6397/core.h>
13 #include <linux/mfd/mt6397/registers.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
20 #define MTK_PMIC_PWRKEY_RST_EN_MASK 0x1
21 #define MTK_PMIC_PWRKEY_RST_EN_SHIFT 6
22 #define MTK_PMIC_HOMEKEY_RST_EN_MASK 0x1
23 #define MTK_PMIC_HOMEKEY_RST_EN_SHIFT 5
24 #define MTK_PMIC_RST_DU_MASK 0x3
25 #define MTK_PMIC_RST_DU_SHIFT 8
27 #define MTK_PMIC_PWRKEY_RST \
28 (MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
29 #define MTK_PMIC_HOMEKEY_RST \
30 (MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
32 #define MTK_PMIC_PWRKEY_INDEX 0
33 #define MTK_PMIC_HOMEKEY_INDEX 1
34 #define MTK_PMIC_MAX_KEY_COUNT 2
36 struct mtk_pmic_keys_regs
{
43 #define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask, \
44 _intsel_reg, _intsel_mask) \
46 .deb_reg = _deb_reg, \
47 .deb_mask = _deb_mask, \
48 .intsel_reg = _intsel_reg, \
49 .intsel_mask = _intsel_mask, \
52 struct mtk_pmic_regs
{
53 const struct mtk_pmic_keys_regs keys_regs
[MTK_PMIC_MAX_KEY_COUNT
];
57 static const struct mtk_pmic_regs mt6397_regs
= {
58 .keys_regs
[MTK_PMIC_PWRKEY_INDEX
] =
59 MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS
,
60 0x8, MT6397_INT_RSV
, 0x10),
61 .keys_regs
[MTK_PMIC_HOMEKEY_INDEX
] =
62 MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2
,
63 0x10, MT6397_INT_RSV
, 0x8),
64 .pmic_rst_reg
= MT6397_TOP_RST_MISC
,
67 static const struct mtk_pmic_regs mt6323_regs
= {
68 .keys_regs
[MTK_PMIC_PWRKEY_INDEX
] =
69 MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS
,
70 0x2, MT6323_INT_MISC_CON
, 0x10),
71 .keys_regs
[MTK_PMIC_HOMEKEY_INDEX
] =
72 MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS
,
73 0x4, MT6323_INT_MISC_CON
, 0x8),
74 .pmic_rst_reg
= MT6323_TOP_RST_MISC
,
77 struct mtk_pmic_keys_info
{
78 struct mtk_pmic_keys
*keys
;
79 const struct mtk_pmic_keys_regs
*regs
;
85 struct mtk_pmic_keys
{
86 struct input_dev
*input_dev
;
88 struct regmap
*regmap
;
89 struct mtk_pmic_keys_info keys
[MTK_PMIC_MAX_KEY_COUNT
];
92 enum mtk_pmic_keys_lp_mode
{
98 static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys
*keys
,
102 u32 long_press_mode
, long_press_debounce
;
104 ret
= of_property_read_u32(keys
->dev
->of_node
,
105 "power-off-time-sec", &long_press_debounce
);
107 long_press_debounce
= 0;
109 regmap_update_bits(keys
->regmap
, pmic_rst_reg
,
110 MTK_PMIC_RST_DU_MASK
<< MTK_PMIC_RST_DU_SHIFT
,
111 long_press_debounce
<< MTK_PMIC_RST_DU_SHIFT
);
113 ret
= of_property_read_u32(keys
->dev
->of_node
,
114 "mediatek,long-press-mode", &long_press_mode
);
116 long_press_mode
= LP_DISABLE
;
118 switch (long_press_mode
) {
120 regmap_update_bits(keys
->regmap
, pmic_rst_reg
,
122 MTK_PMIC_PWRKEY_RST
);
123 regmap_update_bits(keys
->regmap
, pmic_rst_reg
,
124 MTK_PMIC_HOMEKEY_RST
,
128 regmap_update_bits(keys
->regmap
, pmic_rst_reg
,
130 MTK_PMIC_PWRKEY_RST
);
131 regmap_update_bits(keys
->regmap
, pmic_rst_reg
,
132 MTK_PMIC_HOMEKEY_RST
,
133 MTK_PMIC_HOMEKEY_RST
);
136 regmap_update_bits(keys
->regmap
, pmic_rst_reg
,
139 regmap_update_bits(keys
->regmap
, pmic_rst_reg
,
140 MTK_PMIC_HOMEKEY_RST
,
148 static irqreturn_t
mtk_pmic_keys_irq_handler_thread(int irq
, void *data
)
150 struct mtk_pmic_keys_info
*info
= data
;
151 u32 key_deb
, pressed
;
153 regmap_read(info
->keys
->regmap
, info
->regs
->deb_reg
, &key_deb
);
155 key_deb
&= info
->regs
->deb_mask
;
159 input_report_key(info
->keys
->input_dev
, info
->keycode
, pressed
);
160 input_sync(info
->keys
->input_dev
);
162 dev_dbg(info
->keys
->dev
, "(%s) key =%d using PMIC\n",
163 pressed
? "pressed" : "released", info
->keycode
);
168 static int mtk_pmic_key_setup(struct mtk_pmic_keys
*keys
,
169 struct mtk_pmic_keys_info
*info
)
175 ret
= regmap_update_bits(keys
->regmap
, info
->regs
->intsel_reg
,
176 info
->regs
->intsel_mask
,
177 info
->regs
->intsel_mask
);
181 ret
= devm_request_threaded_irq(keys
->dev
, info
->irq
, NULL
,
182 mtk_pmic_keys_irq_handler_thread
,
183 IRQF_ONESHOT
| IRQF_TRIGGER_HIGH
,
184 "mtk-pmic-keys", info
);
186 dev_err(keys
->dev
, "Failed to request IRQ: %d: %d\n",
191 input_set_capability(keys
->input_dev
, EV_KEY
, info
->keycode
);
196 static int __maybe_unused
mtk_pmic_keys_suspend(struct device
*dev
)
198 struct mtk_pmic_keys
*keys
= dev_get_drvdata(dev
);
201 for (index
= 0; index
< MTK_PMIC_MAX_KEY_COUNT
; index
++) {
202 if (keys
->keys
[index
].wakeup
)
203 enable_irq_wake(keys
->keys
[index
].irq
);
209 static int __maybe_unused
mtk_pmic_keys_resume(struct device
*dev
)
211 struct mtk_pmic_keys
*keys
= dev_get_drvdata(dev
);
214 for (index
= 0; index
< MTK_PMIC_MAX_KEY_COUNT
; index
++) {
215 if (keys
->keys
[index
].wakeup
)
216 disable_irq_wake(keys
->keys
[index
].irq
);
222 static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops
, mtk_pmic_keys_suspend
,
223 mtk_pmic_keys_resume
);
225 static const struct of_device_id of_mtk_pmic_keys_match_tbl
[] = {
227 .compatible
= "mediatek,mt6397-keys",
228 .data
= &mt6397_regs
,
230 .compatible
= "mediatek,mt6323-keys",
231 .data
= &mt6323_regs
,
236 MODULE_DEVICE_TABLE(of
, of_mtk_pmic_keys_match_tbl
);
238 static int mtk_pmic_keys_probe(struct platform_device
*pdev
)
240 int error
, index
= 0;
241 unsigned int keycount
;
242 struct mt6397_chip
*pmic_chip
= dev_get_drvdata(pdev
->dev
.parent
);
243 struct device_node
*node
= pdev
->dev
.of_node
, *child
;
244 struct mtk_pmic_keys
*keys
;
245 const struct mtk_pmic_regs
*mtk_pmic_regs
;
246 struct input_dev
*input_dev
;
247 const struct of_device_id
*of_id
=
248 of_match_device(of_mtk_pmic_keys_match_tbl
, &pdev
->dev
);
250 keys
= devm_kzalloc(&pdev
->dev
, sizeof(*keys
), GFP_KERNEL
);
254 keys
->dev
= &pdev
->dev
;
255 keys
->regmap
= pmic_chip
->regmap
;
256 mtk_pmic_regs
= of_id
->data
;
258 keys
->input_dev
= input_dev
= devm_input_allocate_device(keys
->dev
);
260 dev_err(keys
->dev
, "input allocate device fail.\n");
264 input_dev
->name
= "mtk-pmic-keys";
265 input_dev
->id
.bustype
= BUS_HOST
;
266 input_dev
->id
.vendor
= 0x0001;
267 input_dev
->id
.product
= 0x0001;
268 input_dev
->id
.version
= 0x0001;
270 keycount
= of_get_available_child_count(node
);
271 if (keycount
> MTK_PMIC_MAX_KEY_COUNT
) {
272 dev_err(keys
->dev
, "too many keys defined (%d)\n", keycount
);
276 for_each_child_of_node(node
, child
) {
277 keys
->keys
[index
].regs
= &mtk_pmic_regs
->keys_regs
[index
];
279 keys
->keys
[index
].irq
= platform_get_irq(pdev
, index
);
280 if (keys
->keys
[index
].irq
< 0) {
282 return keys
->keys
[index
].irq
;
285 error
= of_property_read_u32(child
,
286 "linux,keycodes", &keys
->keys
[index
].keycode
);
289 "failed to read key:%d linux,keycode property: %d\n",
295 if (of_property_read_bool(child
, "wakeup-source"))
296 keys
->keys
[index
].wakeup
= true;
298 error
= mtk_pmic_key_setup(keys
, &keys
->keys
[index
]);
307 error
= input_register_device(input_dev
);
310 "register input device failed (%d)\n", error
);
314 mtk_pmic_keys_lp_reset_setup(keys
, mtk_pmic_regs
->pmic_rst_reg
);
316 platform_set_drvdata(pdev
, keys
);
321 static struct platform_driver pmic_keys_pdrv
= {
322 .probe
= mtk_pmic_keys_probe
,
324 .name
= "mtk-pmic-keys",
325 .of_match_table
= of_mtk_pmic_keys_match_tbl
,
326 .pm
= &mtk_pmic_keys_pm_ops
,
330 module_platform_driver(pmic_keys_pdrv
);
332 MODULE_LICENSE("GPL v2");
333 MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
334 MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");