Loading...
1// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright 2022 NXP.
4
5#include <linux/device.h>
6#include <linux/err.h>
7#include <linux/init.h>
8#include <linux/input.h>
9#include <linux/interrupt.h>
10#include <linux/io.h>
11#include <linux/jiffies.h>
12#include <linux/kernel.h>
13#include <linux/mfd/syscon.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_address.h>
17#include <linux/platform_device.h>
18#include <linux/pm_wakeirq.h>
19#include <linux/regmap.h>
20
21#define BBNSM_CTRL 0x8
22#define BBNSM_INT_EN 0x10
23#define BBNSM_EVENTS 0x14
24#define BBNSM_PAD_CTRL 0x24
25
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)
32
33#define DEBOUNCE_TIME 30
34#define REPEAT_INTERVAL 60
35
36struct bbnsm_pwrkey {
37 struct regmap *regmap;
38 int irq;
39 int keycode;
40 int keystate; /* 1:pressed */
41 bool suspended;
42 struct timer_list check_timer;
43 struct input_dev *input;
44};
45
46static void bbnsm_pwrkey_check_for_events(struct timer_list *t)
47{
48 struct bbnsm_pwrkey *bbnsm = from_timer(bbnsm, t, check_timer);
49 struct input_dev *input = bbnsm->input;
50 u32 state;
51
52 regmap_read(bbnsm->regmap, BBNSM_EVENTS, &state);
53
54 state = state & BBNSM_BTN_PRESSED ? 1 : 0;
55
56 /* only report new event if status changed */
57 if (state ^ bbnsm->keystate) {
58 bbnsm->keystate = state;
59 input_event(input, EV_KEY, bbnsm->keycode, state);
60 input_sync(input);
61 pm_relax(bbnsm->input->dev.parent);
62 }
63
64 /* repeat check if pressed long */
65 if (state)
66 mod_timer(&bbnsm->check_timer,
67 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
68}
69
70static irqreturn_t bbnsm_pwrkey_interrupt(int irq, void *dev_id)
71{
72 struct platform_device *pdev = dev_id;
73 struct bbnsm_pwrkey *bbnsm = platform_get_drvdata(pdev);
74 struct input_dev *input = bbnsm->input;
75 u32 event;
76
77 regmap_read(bbnsm->regmap, BBNSM_EVENTS, &event);
78 if (!(event & BBNSM_BTN_OFF))
79 return IRQ_NONE;
80
81 pm_wakeup_event(bbnsm->input->dev.parent, 0);
82
83 /*
84 * Directly report key event after resume to make sure key press
85 * event is never missed.
86 */
87 if (bbnsm->suspended) {
88 bbnsm->keystate = 1;
89 input_event(input, EV_KEY, bbnsm->keycode, 1);
90 input_sync(input);
91 /* Fire at most once per suspend/resume cycle */
92 bbnsm->suspended = false;
93 }
94
95 mod_timer(&bbnsm->check_timer,
96 jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
97
98 /* clear PWR OFF */
99 regmap_write(bbnsm->regmap, BBNSM_EVENTS, BBNSM_BTN_OFF);
100
101 return IRQ_HANDLED;
102}
103
104static void bbnsm_pwrkey_act(void *pdata)
105{
106 struct bbnsm_pwrkey *bbnsm = pdata;
107
108 timer_shutdown_sync(&bbnsm->check_timer);
109}
110
111static int bbnsm_pwrkey_probe(struct platform_device *pdev)
112{
113 struct bbnsm_pwrkey *bbnsm;
114 struct input_dev *input;
115 struct device_node *np = pdev->dev.of_node;
116 int error;
117
118 bbnsm = devm_kzalloc(&pdev->dev, sizeof(*bbnsm), GFP_KERNEL);
119 if (!bbnsm)
120 return -ENOMEM;
121
122 bbnsm->regmap = syscon_node_to_regmap(np->parent);
123 if (IS_ERR(bbnsm->regmap)) {
124 dev_err(&pdev->dev, "bbnsm pwerkey get regmap failed\n");
125 return PTR_ERR(bbnsm->regmap);
126 }
127
128 if (device_property_read_u32(&pdev->dev, "linux,code",
129 &bbnsm->keycode)) {
130 bbnsm->keycode = KEY_POWER;
131 dev_warn(&pdev->dev, "key code is not specified, using default KEY_POWER\n");
132 }
133
134 bbnsm->irq = platform_get_irq(pdev, 0);
135 if (bbnsm->irq < 0)
136 return -EINVAL;
137
138 /* config the BBNSM power related register */
139 regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, BBNSM_DP_EN, BBNSM_DP_EN);
140
141 /* clear the unexpected interrupt before driver ready */
142 regmap_write_bits(bbnsm->regmap, BBNSM_EVENTS, BBNSM_PWRKEY_EVENTS,
143 BBNSM_PWRKEY_EVENTS);
144
145 timer_setup(&bbnsm->check_timer, bbnsm_pwrkey_check_for_events, 0);
146
147 input = devm_input_allocate_device(&pdev->dev);
148 if (!input) {
149 dev_err(&pdev->dev, "failed to allocate the input device\n");
150 return -ENOMEM;
151 }
152
153 input->name = pdev->name;
154 input->phys = "bbnsm-pwrkey/input0";
155 input->id.bustype = BUS_HOST;
156
157 input_set_capability(input, EV_KEY, bbnsm->keycode);
158
159 /* input customer action to cancel release timer */
160 error = devm_add_action(&pdev->dev, bbnsm_pwrkey_act, bbnsm);
161 if (error) {
162 dev_err(&pdev->dev, "failed to register remove action\n");
163 return error;
164 }
165
166 bbnsm->input = input;
167 platform_set_drvdata(pdev, bbnsm);
168
169 error = devm_request_irq(&pdev->dev, bbnsm->irq, bbnsm_pwrkey_interrupt,
170 IRQF_SHARED, pdev->name, pdev);
171 if (error) {
172 dev_err(&pdev->dev, "interrupt not available.\n");
173 return error;
174 }
175
176 error = input_register_device(input);
177 if (error) {
178 dev_err(&pdev->dev, "failed to register input device\n");
179 return error;
180 }
181
182 device_init_wakeup(&pdev->dev, true);
183 error = dev_pm_set_wake_irq(&pdev->dev, bbnsm->irq);
184 if (error)
185 dev_warn(&pdev->dev, "irq wake enable failed.\n");
186
187 return 0;
188}
189
190static void bbnsm_pwrkey_remove(struct platform_device *pdev)
191{
192 dev_pm_clear_wake_irq(&pdev->dev);
193 device_init_wakeup(&pdev->dev, false);
194}
195
196static int __maybe_unused bbnsm_pwrkey_suspend(struct device *dev)
197{
198 struct platform_device *pdev = to_platform_device(dev);
199 struct bbnsm_pwrkey *bbnsm = platform_get_drvdata(pdev);
200
201 bbnsm->suspended = true;
202
203 return 0;
204}
205
206static int __maybe_unused bbnsm_pwrkey_resume(struct device *dev)
207{
208 struct platform_device *pdev = to_platform_device(dev);
209 struct bbnsm_pwrkey *bbnsm = platform_get_drvdata(pdev);
210
211 bbnsm->suspended = false;
212
213 return 0;
214}
215
216static SIMPLE_DEV_PM_OPS(bbnsm_pwrkey_pm_ops, bbnsm_pwrkey_suspend,
217 bbnsm_pwrkey_resume);
218
219static const struct of_device_id bbnsm_pwrkey_ids[] = {
220 { .compatible = "nxp,imx93-bbnsm-pwrkey" },
221 { /* sentinel */ }
222};
223MODULE_DEVICE_TABLE(of, bbnsm_pwrkey_ids);
224
225static struct platform_driver bbnsm_pwrkey_driver = {
226 .driver = {
227 .name = "bbnsm_pwrkey",
228 .pm = &bbnsm_pwrkey_pm_ops,
229 .of_match_table = bbnsm_pwrkey_ids,
230 },
231 .probe = bbnsm_pwrkey_probe,
232 .remove = bbnsm_pwrkey_remove,
233
234};
235module_platform_driver(bbnsm_pwrkey_driver);
236
237MODULE_AUTHOR("Jacky Bai <ping.bai@nxp.com>");
238MODULE_DESCRIPTION("NXP bbnsm power key Driver");
239MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright 2022 NXP.
4
5#include <linux/device.h>
6#include <linux/err.h>
7#include <linux/init.h>
8#include <linux/input.h>
9#include <linux/interrupt.h>
10#include <linux/io.h>
11#include <linux/jiffies.h>
12#include <linux/kernel.h>
13#include <linux/mfd/syscon.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_address.h>
17#include <linux/platform_device.h>
18#include <linux/pm_wakeirq.h>
19#include <linux/regmap.h>
20
21#define BBNSM_CTRL 0x8
22#define BBNSM_INT_EN 0x10
23#define BBNSM_EVENTS 0x14
24#define BBNSM_PAD_CTRL 0x24
25
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)
32
33#define DEBOUNCE_TIME 30
34#define REPEAT_INTERVAL 60
35
36struct bbnsm_pwrkey {
37 struct regmap *regmap;
38 int irq;
39 int keycode;
40 int keystate; /* 1:pressed */
41 struct timer_list check_timer;
42 struct input_dev *input;
43};
44
45static void bbnsm_pwrkey_check_for_events(struct timer_list *t)
46{
47 struct bbnsm_pwrkey *bbnsm = from_timer(bbnsm, t, check_timer);
48 struct input_dev *input = bbnsm->input;
49 u32 state;
50
51 regmap_read(bbnsm->regmap, BBNSM_EVENTS, &state);
52
53 state = state & BBNSM_BTN_PRESSED ? 1 : 0;
54
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);
59 input_sync(input);
60 pm_relax(bbnsm->input->dev.parent);
61 }
62
63 /* repeat check if pressed long */
64 if (state)
65 mod_timer(&bbnsm->check_timer,
66 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
67}
68
69static irqreturn_t bbnsm_pwrkey_interrupt(int irq, void *dev_id)
70{
71 struct platform_device *pdev = dev_id;
72 struct bbnsm_pwrkey *bbnsm = platform_get_drvdata(pdev);
73 u32 event;
74
75 regmap_read(bbnsm->regmap, BBNSM_EVENTS, &event);
76 if (!(event & BBNSM_BTN_OFF))
77 return IRQ_NONE;
78
79 pm_wakeup_event(bbnsm->input->dev.parent, 0);
80
81 mod_timer(&bbnsm->check_timer,
82 jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
83
84 /* clear PWR OFF */
85 regmap_write(bbnsm->regmap, BBNSM_EVENTS, BBNSM_BTN_OFF);
86
87 return IRQ_HANDLED;
88}
89
90static void bbnsm_pwrkey_act(void *pdata)
91{
92 struct bbnsm_pwrkey *bbnsm = pdata;
93
94 timer_shutdown_sync(&bbnsm->check_timer);
95}
96
97static int bbnsm_pwrkey_probe(struct platform_device *pdev)
98{
99 struct bbnsm_pwrkey *bbnsm;
100 struct input_dev *input;
101 struct device_node *np = pdev->dev.of_node;
102 int error;
103
104 bbnsm = devm_kzalloc(&pdev->dev, sizeof(*bbnsm), GFP_KERNEL);
105 if (!bbnsm)
106 return -ENOMEM;
107
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);
112 }
113
114 if (device_property_read_u32(&pdev->dev, "linux,code",
115 &bbnsm->keycode)) {
116 bbnsm->keycode = KEY_POWER;
117 dev_warn(&pdev->dev, "key code is not specified, using default KEY_POWER\n");
118 }
119
120 bbnsm->irq = platform_get_irq(pdev, 0);
121 if (bbnsm->irq < 0)
122 return -EINVAL;
123
124 /* config the BBNSM power related register */
125 regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, BBNSM_DP_EN, BBNSM_DP_EN);
126
127 /* clear the unexpected interrupt before driver ready */
128 regmap_write_bits(bbnsm->regmap, BBNSM_EVENTS, BBNSM_PWRKEY_EVENTS,
129 BBNSM_PWRKEY_EVENTS);
130
131 timer_setup(&bbnsm->check_timer, bbnsm_pwrkey_check_for_events, 0);
132
133 input = devm_input_allocate_device(&pdev->dev);
134 if (!input) {
135 dev_err(&pdev->dev, "failed to allocate the input device\n");
136 return -ENOMEM;
137 }
138
139 input->name = pdev->name;
140 input->phys = "bbnsm-pwrkey/input0";
141 input->id.bustype = BUS_HOST;
142
143 input_set_capability(input, EV_KEY, bbnsm->keycode);
144
145 /* input customer action to cancel release timer */
146 error = devm_add_action(&pdev->dev, bbnsm_pwrkey_act, bbnsm);
147 if (error) {
148 dev_err(&pdev->dev, "failed to register remove action\n");
149 return error;
150 }
151
152 bbnsm->input = input;
153 platform_set_drvdata(pdev, bbnsm);
154
155 error = devm_request_irq(&pdev->dev, bbnsm->irq, bbnsm_pwrkey_interrupt,
156 IRQF_SHARED, pdev->name, pdev);
157 if (error) {
158 dev_err(&pdev->dev, "interrupt not available.\n");
159 return error;
160 }
161
162 error = input_register_device(input);
163 if (error) {
164 dev_err(&pdev->dev, "failed to register input device\n");
165 return error;
166 }
167
168 device_init_wakeup(&pdev->dev, true);
169 error = dev_pm_set_wake_irq(&pdev->dev, bbnsm->irq);
170 if (error)
171 dev_warn(&pdev->dev, "irq wake enable failed.\n");
172
173 return 0;
174}
175
176static const struct of_device_id bbnsm_pwrkey_ids[] = {
177 { .compatible = "nxp,imx93-bbnsm-pwrkey" },
178 { /* sentinel */ }
179};
180MODULE_DEVICE_TABLE(of, bbnsm_pwrkey_ids);
181
182static struct platform_driver bbnsm_pwrkey_driver = {
183 .driver = {
184 .name = "bbnsm_pwrkey",
185 .of_match_table = bbnsm_pwrkey_ids,
186 },
187 .probe = bbnsm_pwrkey_probe,
188};
189module_platform_driver(bbnsm_pwrkey_driver);
190
191MODULE_AUTHOR("Jacky Bai <ping.bai@nxp.com>");
192MODULE_DESCRIPTION("NXP bbnsm power key Driver");
193MODULE_LICENSE("GPL");