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