1 // SPDX-License-Identifier: GPL-2.0+
5 #include <linux/device.h>
7 #include <linux/init.h>
8 #include <linux/input.h>
9 #include <linux/interrupt.h>
11 #include <linux/jiffies.h>
12 #include <linux/kernel.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_wakeirq.h>
19 #include <linux/regmap.h>
21 #define BBNSM_CTRL 0x8
22 #define BBNSM_INT_EN 0x10
23 #define BBNSM_EVENTS 0x14
24 #define BBNSM_PAD_CTRL 0x24
26 #define BBNSM_BTN_PRESSED BIT(7)
27 #define BBNSM_PWR_ON BIT(6)
28 #define BBNSM_BTN_OFF BIT(5)
29 #define BBNSM_EMG_OFF BIT(4)
30 #define BBNSM_PWRKEY_EVENTS (BBNSM_PWR_ON | BBNSM_BTN_OFF | BBNSM_EMG_OFF)
31 #define BBNSM_DP_EN BIT(24)
33 #define DEBOUNCE_TIME 30
34 #define REPEAT_INTERVAL 60
37 struct regmap *regmap;
40 int keystate; /* 1:pressed */
41 struct timer_list check_timer;
42 struct input_dev *input;
45 static void bbnsm_pwrkey_check_for_events(struct timer_list *t)
47 struct bbnsm_pwrkey *bbnsm = from_timer(bbnsm, t, check_timer);
48 struct input_dev *input = bbnsm->input;
51 regmap_read(bbnsm->regmap, BBNSM_EVENTS, &state);
53 state = state & BBNSM_BTN_PRESSED ? 1 : 0;
55 /* only report new event if status changed */
56 if (state ^ bbnsm->keystate) {
57 bbnsm->keystate = state;
58 input_event(input, EV_KEY, bbnsm->keycode, state);
60 pm_relax(bbnsm->input->dev.parent);
63 /* repeat check if pressed long */
65 mod_timer(&bbnsm->check_timer,
66 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
69 static irqreturn_t bbnsm_pwrkey_interrupt(int irq, void *dev_id)
71 struct platform_device *pdev = dev_id;
72 struct bbnsm_pwrkey *bbnsm = platform_get_drvdata(pdev);
75 regmap_read(bbnsm->regmap, BBNSM_EVENTS, &event);
76 if (!(event & BBNSM_BTN_OFF))
79 pm_wakeup_event(bbnsm->input->dev.parent, 0);
81 mod_timer(&bbnsm->check_timer,
82 jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
85 regmap_write(bbnsm->regmap, BBNSM_EVENTS, BBNSM_BTN_OFF);
90 static void bbnsm_pwrkey_act(void *pdata)
92 struct bbnsm_pwrkey *bbnsm = pdata;
94 timer_shutdown_sync(&bbnsm->check_timer);
97 static int bbnsm_pwrkey_probe(struct platform_device *pdev)
99 struct bbnsm_pwrkey *bbnsm;
100 struct input_dev *input;
101 struct device_node *np = pdev->dev.of_node;
104 bbnsm = devm_kzalloc(&pdev->dev, sizeof(*bbnsm), GFP_KERNEL);
108 bbnsm->regmap = syscon_node_to_regmap(np->parent);
109 if (IS_ERR(bbnsm->regmap)) {
110 dev_err(&pdev->dev, "bbnsm pwerkey get regmap failed\n");
111 return PTR_ERR(bbnsm->regmap);
114 if (device_property_read_u32(&pdev->dev, "linux,code",
116 bbnsm->keycode = KEY_POWER;
117 dev_warn(&pdev->dev, "key code is not specified, using default KEY_POWER\n");
120 bbnsm->irq = platform_get_irq(pdev, 0);
124 /* config the BBNSM power related register */
125 regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, BBNSM_DP_EN, BBNSM_DP_EN);
127 /* clear the unexpected interrupt before driver ready */
128 regmap_write_bits(bbnsm->regmap, BBNSM_EVENTS, BBNSM_PWRKEY_EVENTS,
129 BBNSM_PWRKEY_EVENTS);
131 timer_setup(&bbnsm->check_timer, bbnsm_pwrkey_check_for_events, 0);
133 input = devm_input_allocate_device(&pdev->dev);
135 dev_err(&pdev->dev, "failed to allocate the input device\n");
139 input->name = pdev->name;
140 input->phys = "bbnsm-pwrkey/input0";
141 input->id.bustype = BUS_HOST;
143 input_set_capability(input, EV_KEY, bbnsm->keycode);
145 /* input customer action to cancel release timer */
146 error = devm_add_action(&pdev->dev, bbnsm_pwrkey_act, bbnsm);
148 dev_err(&pdev->dev, "failed to register remove action\n");
152 bbnsm->input = input;
153 platform_set_drvdata(pdev, bbnsm);
155 error = devm_request_irq(&pdev->dev, bbnsm->irq, bbnsm_pwrkey_interrupt,
156 IRQF_SHARED, pdev->name, pdev);
158 dev_err(&pdev->dev, "interrupt not available.\n");
162 error = input_register_device(input);
164 dev_err(&pdev->dev, "failed to register input device\n");
168 device_init_wakeup(&pdev->dev, true);
169 error = dev_pm_set_wake_irq(&pdev->dev, bbnsm->irq);
171 dev_warn(&pdev->dev, "irq wake enable failed.\n");
176 static const struct of_device_id bbnsm_pwrkey_ids[] = {
177 { .compatible = "nxp,imx93-bbnsm-pwrkey" },
180 MODULE_DEVICE_TABLE(of, bbnsm_pwrkey_ids);
182 static struct platform_driver bbnsm_pwrkey_driver = {
184 .name = "bbnsm_pwrkey",
185 .of_match_table = bbnsm_pwrkey_ids,
187 .probe = bbnsm_pwrkey_probe,
189 module_platform_driver(bbnsm_pwrkey_driver);
191 MODULE_AUTHOR("Jacky Bai <ping.bai@nxp.com>");
192 MODULE_DESCRIPTION("NXP bbnsm power key Driver");
193 MODULE_LICENSE("GPL");