Loading...
1/*
2 * Driver for the IMX SNVS ON/OFF Power Key
3 * Copyright (C) 2015 Freescale Semiconductor, Inc. All Rights Reserved.
4 *
5 * The code contained herein is licensed under the GNU General Public
6 * License. You may obtain a copy of the GNU General Public License
7 * Version 2 or later at the following locations:
8 *
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
11 */
12
13#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/input.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/jiffies.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/of.h>
23#include <linux/of_address.h>
24#include <linux/platform_device.h>
25#include <linux/mfd/syscon.h>
26#include <linux/regmap.h>
27
28#define SNVS_LPSR_REG 0x4C /* LP Status Register */
29#define SNVS_LPCR_REG 0x38 /* LP Control Register */
30#define SNVS_HPSR_REG 0x14
31#define SNVS_HPSR_BTN BIT(6)
32#define SNVS_LPSR_SPO BIT(18)
33#define SNVS_LPCR_DEP_EN BIT(5)
34
35#define DEBOUNCE_TIME 30
36#define REPEAT_INTERVAL 60
37
38struct pwrkey_drv_data {
39 struct regmap *snvs;
40 int irq;
41 int keycode;
42 int keystate; /* 1:pressed */
43 int wakeup;
44 struct timer_list check_timer;
45 struct input_dev *input;
46};
47
48static void imx_imx_snvs_check_for_events(unsigned long data)
49{
50 struct pwrkey_drv_data *pdata = (struct pwrkey_drv_data *) data;
51 struct input_dev *input = pdata->input;
52 u32 state;
53
54 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
55 state = state & SNVS_HPSR_BTN ? 1 : 0;
56
57 /* only report new event if status changed */
58 if (state ^ pdata->keystate) {
59 pdata->keystate = state;
60 input_event(input, EV_KEY, pdata->keycode, state);
61 input_sync(input);
62 pm_relax(pdata->input->dev.parent);
63 }
64
65 /* repeat check if pressed long */
66 if (state) {
67 mod_timer(&pdata->check_timer,
68 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
69 }
70}
71
72static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
73{
74 struct platform_device *pdev = dev_id;
75 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
76 u32 lp_status;
77
78 pm_wakeup_event(pdata->input->dev.parent, 0);
79
80 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
81 if (lp_status & SNVS_LPSR_SPO)
82 mod_timer(&pdata->check_timer, jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
83
84 /* clear SPO status */
85 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
86
87 return IRQ_HANDLED;
88}
89
90static void imx_snvs_pwrkey_act(void *pdata)
91{
92 struct pwrkey_drv_data *pd = pdata;
93
94 del_timer_sync(&pd->check_timer);
95}
96
97static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
98{
99 struct pwrkey_drv_data *pdata = NULL;
100 struct input_dev *input = NULL;
101 struct device_node *np;
102 int error;
103
104 /* Get SNVS register Page */
105 np = pdev->dev.of_node;
106 if (!np)
107 return -ENODEV;
108
109 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
110 if (!pdata)
111 return -ENOMEM;
112
113 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
114 if (IS_ERR(pdata->snvs)) {
115 dev_err(&pdev->dev, "Can't get snvs syscon\n");
116 return PTR_ERR(pdata->snvs);
117 }
118
119 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
120 pdata->keycode = KEY_POWER;
121 dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
122 }
123
124 pdata->wakeup = of_property_read_bool(np, "wakeup-source");
125
126 pdata->irq = platform_get_irq(pdev, 0);
127 if (pdata->irq < 0) {
128 dev_err(&pdev->dev, "no irq defined in platform data\n");
129 return -EINVAL;
130 }
131
132 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
133
134 /* clear the unexpected interrupt before driver ready */
135 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
136
137 setup_timer(&pdata->check_timer,
138 imx_imx_snvs_check_for_events, (unsigned long) pdata);
139
140 input = devm_input_allocate_device(&pdev->dev);
141 if (!input) {
142 dev_err(&pdev->dev, "failed to allocate the input device\n");
143 return -ENOMEM;
144 }
145
146 input->name = pdev->name;
147 input->phys = "snvs-pwrkey/input0";
148 input->id.bustype = BUS_HOST;
149
150 input_set_capability(input, EV_KEY, pdata->keycode);
151
152 /* input customer action to cancel release timer */
153 error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
154 if (error) {
155 dev_err(&pdev->dev, "failed to register remove action\n");
156 return error;
157 }
158
159 error = devm_request_irq(&pdev->dev, pdata->irq,
160 imx_snvs_pwrkey_interrupt,
161 0, pdev->name, pdev);
162
163 if (error) {
164 dev_err(&pdev->dev, "interrupt not available.\n");
165 return error;
166 }
167
168 error = input_register_device(input);
169 if (error < 0) {
170 dev_err(&pdev->dev, "failed to register input device\n");
171 input_free_device(input);
172 return error;
173 }
174
175 pdata->input = input;
176 platform_set_drvdata(pdev, pdata);
177
178 device_init_wakeup(&pdev->dev, pdata->wakeup);
179
180 return 0;
181}
182
183static int __maybe_unused imx_snvs_pwrkey_suspend(struct device *dev)
184{
185 struct platform_device *pdev = to_platform_device(dev);
186 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
187
188 if (device_may_wakeup(&pdev->dev))
189 enable_irq_wake(pdata->irq);
190
191 return 0;
192}
193
194static int __maybe_unused imx_snvs_pwrkey_resume(struct device *dev)
195{
196 struct platform_device *pdev = to_platform_device(dev);
197 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
198
199 if (device_may_wakeup(&pdev->dev))
200 disable_irq_wake(pdata->irq);
201
202 return 0;
203}
204
205static const struct of_device_id imx_snvs_pwrkey_ids[] = {
206 { .compatible = "fsl,sec-v4.0-pwrkey" },
207 { /* sentinel */ }
208};
209MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
210
211static SIMPLE_DEV_PM_OPS(imx_snvs_pwrkey_pm_ops, imx_snvs_pwrkey_suspend,
212 imx_snvs_pwrkey_resume);
213
214static struct platform_driver imx_snvs_pwrkey_driver = {
215 .driver = {
216 .name = "snvs_pwrkey",
217 .pm = &imx_snvs_pwrkey_pm_ops,
218 .of_match_table = imx_snvs_pwrkey_ids,
219 },
220 .probe = imx_snvs_pwrkey_probe,
221};
222module_platform_driver(imx_snvs_pwrkey_driver);
223
224MODULE_AUTHOR("Freescale Semiconductor");
225MODULE_DESCRIPTION("i.MX snvs power key Driver");
226MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0+
2//
3// Driver for the IMX SNVS ON/OFF Power Key
4// Copyright (C) 2015 Freescale Semiconductor, Inc. All Rights Reserved.
5
6#include <linux/clk.h>
7#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/init.h>
10#include <linux/input.h>
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/jiffies.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_address.h>
18#include <linux/platform_device.h>
19#include <linux/pm_wakeirq.h>
20#include <linux/mfd/syscon.h>
21#include <linux/regmap.h>
22
23#define SNVS_HPVIDR1_REG 0xBF8
24#define SNVS_LPSR_REG 0x4C /* LP Status Register */
25#define SNVS_LPCR_REG 0x38 /* LP Control Register */
26#define SNVS_HPSR_REG 0x14
27#define SNVS_HPSR_BTN BIT(6)
28#define SNVS_LPSR_SPO BIT(18)
29#define SNVS_LPCR_DEP_EN BIT(5)
30
31#define DEBOUNCE_TIME 30
32#define REPEAT_INTERVAL 60
33
34struct pwrkey_drv_data {
35 struct regmap *snvs;
36 int irq;
37 int keycode;
38 int keystate; /* 1:pressed */
39 int wakeup;
40 struct timer_list check_timer;
41 struct input_dev *input;
42 u8 minor_rev;
43};
44
45static void imx_imx_snvs_check_for_events(struct timer_list *t)
46{
47 struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer);
48 struct input_dev *input = pdata->input;
49 u32 state;
50
51 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
52 state = state & SNVS_HPSR_BTN ? 1 : 0;
53
54 /* only report new event if status changed */
55 if (state ^ pdata->keystate) {
56 pdata->keystate = state;
57 input_event(input, EV_KEY, pdata->keycode, state);
58 input_sync(input);
59 pm_relax(pdata->input->dev.parent);
60 }
61
62 /* repeat check if pressed long */
63 if (state) {
64 mod_timer(&pdata->check_timer,
65 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
66 }
67}
68
69static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
70{
71 struct platform_device *pdev = dev_id;
72 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
73 struct input_dev *input = pdata->input;
74 u32 lp_status;
75
76 pm_wakeup_event(input->dev.parent, 0);
77
78 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
79 if (lp_status & SNVS_LPSR_SPO) {
80 if (pdata->minor_rev == 0) {
81 /*
82 * The first generation i.MX6 SoCs only sends an
83 * interrupt on button release. To mimic power-key
84 * usage, we'll prepend a press event.
85 */
86 input_report_key(input, pdata->keycode, 1);
87 input_sync(input);
88 input_report_key(input, pdata->keycode, 0);
89 input_sync(input);
90 pm_relax(input->dev.parent);
91 } else {
92 mod_timer(&pdata->check_timer,
93 jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
94 }
95 }
96
97 /* clear SPO status */
98 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
99
100 return IRQ_HANDLED;
101}
102
103static void imx_snvs_pwrkey_act(void *pdata)
104{
105 struct pwrkey_drv_data *pd = pdata;
106
107 del_timer_sync(&pd->check_timer);
108}
109
110static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
111{
112 struct pwrkey_drv_data *pdata;
113 struct input_dev *input;
114 struct device_node *np;
115 struct clk *clk;
116 int error;
117 u32 vid;
118
119 /* Get SNVS register Page */
120 np = pdev->dev.of_node;
121 if (!np)
122 return -ENODEV;
123
124 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
125 if (!pdata)
126 return -ENOMEM;
127
128 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
129 if (IS_ERR(pdata->snvs)) {
130 dev_err(&pdev->dev, "Can't get snvs syscon\n");
131 return PTR_ERR(pdata->snvs);
132 }
133
134 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
135 pdata->keycode = KEY_POWER;
136 dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
137 }
138
139 clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
140 if (IS_ERR(clk)) {
141 dev_err(&pdev->dev, "Failed to get snvs clock (%pe)\n", clk);
142 return PTR_ERR(clk);
143 }
144
145 pdata->wakeup = of_property_read_bool(np, "wakeup-source");
146
147 pdata->irq = platform_get_irq(pdev, 0);
148 if (pdata->irq < 0)
149 return -EINVAL;
150
151 regmap_read(pdata->snvs, SNVS_HPVIDR1_REG, &vid);
152 pdata->minor_rev = vid & 0xff;
153
154 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
155
156 /* clear the unexpected interrupt before driver ready */
157 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
158
159 timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
160
161 input = devm_input_allocate_device(&pdev->dev);
162 if (!input) {
163 dev_err(&pdev->dev, "failed to allocate the input device\n");
164 return -ENOMEM;
165 }
166
167 input->name = pdev->name;
168 input->phys = "snvs-pwrkey/input0";
169 input->id.bustype = BUS_HOST;
170
171 input_set_capability(input, EV_KEY, pdata->keycode);
172
173 /* input customer action to cancel release timer */
174 error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
175 if (error) {
176 dev_err(&pdev->dev, "failed to register remove action\n");
177 return error;
178 }
179
180 pdata->input = input;
181 platform_set_drvdata(pdev, pdata);
182
183 error = devm_request_irq(&pdev->dev, pdata->irq,
184 imx_snvs_pwrkey_interrupt,
185 0, pdev->name, pdev);
186 if (error) {
187 dev_err(&pdev->dev, "interrupt not available.\n");
188 return error;
189 }
190
191 error = input_register_device(input);
192 if (error < 0) {
193 dev_err(&pdev->dev, "failed to register input device\n");
194 return error;
195 }
196
197 device_init_wakeup(&pdev->dev, pdata->wakeup);
198 error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
199 if (error)
200 dev_err(&pdev->dev, "irq wake enable failed.\n");
201
202 return 0;
203}
204
205static const struct of_device_id imx_snvs_pwrkey_ids[] = {
206 { .compatible = "fsl,sec-v4.0-pwrkey" },
207 { /* sentinel */ }
208};
209MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
210
211static struct platform_driver imx_snvs_pwrkey_driver = {
212 .driver = {
213 .name = "snvs_pwrkey",
214 .of_match_table = imx_snvs_pwrkey_ids,
215 },
216 .probe = imx_snvs_pwrkey_probe,
217};
218module_platform_driver(imx_snvs_pwrkey_driver);
219
220MODULE_AUTHOR("Freescale Semiconductor");
221MODULE_DESCRIPTION("i.MX snvs power key Driver");
222MODULE_LICENSE("GPL");