.. | .. |
---|
3 | 3 | // Driver for the IMX SNVS ON/OFF Power Key |
---|
4 | 4 | // Copyright (C) 2015 Freescale Semiconductor, Inc. All Rights Reserved. |
---|
5 | 5 | |
---|
| 6 | +#include <linux/clk.h> |
---|
6 | 7 | #include <linux/device.h> |
---|
7 | 8 | #include <linux/err.h> |
---|
8 | 9 | #include <linux/init.h> |
---|
.. | .. |
---|
15 | 16 | #include <linux/of.h> |
---|
16 | 17 | #include <linux/of_address.h> |
---|
17 | 18 | #include <linux/platform_device.h> |
---|
| 19 | +#include <linux/pm_wakeirq.h> |
---|
18 | 20 | #include <linux/mfd/syscon.h> |
---|
19 | 21 | #include <linux/regmap.h> |
---|
20 | 22 | |
---|
21 | | -#define SNVS_LPSR_REG 0x4C /* LP Status Register */ |
---|
22 | | -#define SNVS_LPCR_REG 0x38 /* LP Control Register */ |
---|
23 | | -#define SNVS_HPSR_REG 0x14 |
---|
24 | | -#define SNVS_HPSR_BTN BIT(6) |
---|
25 | | -#define SNVS_LPSR_SPO BIT(18) |
---|
26 | | -#define SNVS_LPCR_DEP_EN BIT(5) |
---|
| 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) |
---|
27 | 30 | |
---|
28 | | -#define DEBOUNCE_TIME 30 |
---|
29 | | -#define REPEAT_INTERVAL 60 |
---|
| 31 | +#define DEBOUNCE_TIME 30 |
---|
| 32 | +#define REPEAT_INTERVAL 60 |
---|
30 | 33 | |
---|
31 | 34 | struct pwrkey_drv_data { |
---|
32 | 35 | struct regmap *snvs; |
---|
.. | .. |
---|
36 | 39 | int wakeup; |
---|
37 | 40 | struct timer_list check_timer; |
---|
38 | 41 | struct input_dev *input; |
---|
| 42 | + u8 minor_rev; |
---|
39 | 43 | }; |
---|
40 | 44 | |
---|
41 | 45 | static void imx_imx_snvs_check_for_events(struct timer_list *t) |
---|
.. | .. |
---|
66 | 70 | { |
---|
67 | 71 | struct platform_device *pdev = dev_id; |
---|
68 | 72 | struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); |
---|
| 73 | + struct input_dev *input = pdata->input; |
---|
69 | 74 | u32 lp_status; |
---|
70 | 75 | |
---|
71 | | - pm_wakeup_event(pdata->input->dev.parent, 0); |
---|
| 76 | + pm_wakeup_event(input->dev.parent, 0); |
---|
72 | 77 | |
---|
73 | 78 | regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status); |
---|
74 | | - if (lp_status & SNVS_LPSR_SPO) |
---|
75 | | - mod_timer(&pdata->check_timer, jiffies + msecs_to_jiffies(DEBOUNCE_TIME)); |
---|
| 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 | + } |
---|
76 | 96 | |
---|
77 | 97 | /* clear SPO status */ |
---|
78 | 98 | regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO); |
---|
79 | 99 | |
---|
80 | 100 | return IRQ_HANDLED; |
---|
| 101 | +} |
---|
| 102 | + |
---|
| 103 | +static void imx_snvs_pwrkey_disable_clk(void *data) |
---|
| 104 | +{ |
---|
| 105 | + clk_disable_unprepare(data); |
---|
81 | 106 | } |
---|
82 | 107 | |
---|
83 | 108 | static void imx_snvs_pwrkey_act(void *pdata) |
---|
.. | .. |
---|
89 | 114 | |
---|
90 | 115 | static int imx_snvs_pwrkey_probe(struct platform_device *pdev) |
---|
91 | 116 | { |
---|
92 | | - struct pwrkey_drv_data *pdata = NULL; |
---|
93 | | - struct input_dev *input = NULL; |
---|
| 117 | + struct pwrkey_drv_data *pdata; |
---|
| 118 | + struct input_dev *input; |
---|
94 | 119 | struct device_node *np; |
---|
| 120 | + struct clk *clk; |
---|
95 | 121 | int error; |
---|
| 122 | + u32 vid; |
---|
96 | 123 | |
---|
97 | 124 | /* Get SNVS register Page */ |
---|
98 | 125 | np = pdev->dev.of_node; |
---|
.. | .. |
---|
114 | 141 | dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n"); |
---|
115 | 142 | } |
---|
116 | 143 | |
---|
| 144 | + clk = devm_clk_get_optional(&pdev->dev, NULL); |
---|
| 145 | + if (IS_ERR(clk)) { |
---|
| 146 | + dev_err(&pdev->dev, "Failed to get snvs clock (%pe)\n", clk); |
---|
| 147 | + return PTR_ERR(clk); |
---|
| 148 | + } |
---|
| 149 | + |
---|
| 150 | + error = clk_prepare_enable(clk); |
---|
| 151 | + if (error) { |
---|
| 152 | + dev_err(&pdev->dev, "Failed to enable snvs clock (%pe)\n", |
---|
| 153 | + ERR_PTR(error)); |
---|
| 154 | + return error; |
---|
| 155 | + } |
---|
| 156 | + |
---|
| 157 | + error = devm_add_action_or_reset(&pdev->dev, |
---|
| 158 | + imx_snvs_pwrkey_disable_clk, clk); |
---|
| 159 | + if (error) { |
---|
| 160 | + dev_err(&pdev->dev, |
---|
| 161 | + "Failed to register clock cleanup handler (%pe)\n", |
---|
| 162 | + ERR_PTR(error)); |
---|
| 163 | + return error; |
---|
| 164 | + } |
---|
| 165 | + |
---|
117 | 166 | pdata->wakeup = of_property_read_bool(np, "wakeup-source"); |
---|
118 | 167 | |
---|
119 | 168 | pdata->irq = platform_get_irq(pdev, 0); |
---|
120 | | - if (pdata->irq < 0) { |
---|
121 | | - dev_err(&pdev->dev, "no irq defined in platform data\n"); |
---|
| 169 | + if (pdata->irq < 0) |
---|
122 | 170 | return -EINVAL; |
---|
123 | | - } |
---|
| 171 | + |
---|
| 172 | + regmap_read(pdata->snvs, SNVS_HPVIDR1_REG, &vid); |
---|
| 173 | + pdata->minor_rev = vid & 0xff; |
---|
124 | 174 | |
---|
125 | 175 | regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN); |
---|
126 | 176 | |
---|
.. | .. |
---|
167 | 217 | } |
---|
168 | 218 | |
---|
169 | 219 | device_init_wakeup(&pdev->dev, pdata->wakeup); |
---|
170 | | - |
---|
171 | | - return 0; |
---|
172 | | -} |
---|
173 | | - |
---|
174 | | -static int __maybe_unused imx_snvs_pwrkey_suspend(struct device *dev) |
---|
175 | | -{ |
---|
176 | | - struct platform_device *pdev = to_platform_device(dev); |
---|
177 | | - struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); |
---|
178 | | - |
---|
179 | | - if (device_may_wakeup(&pdev->dev)) |
---|
180 | | - enable_irq_wake(pdata->irq); |
---|
181 | | - |
---|
182 | | - return 0; |
---|
183 | | -} |
---|
184 | | - |
---|
185 | | -static int __maybe_unused imx_snvs_pwrkey_resume(struct device *dev) |
---|
186 | | -{ |
---|
187 | | - struct platform_device *pdev = to_platform_device(dev); |
---|
188 | | - struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); |
---|
189 | | - |
---|
190 | | - if (device_may_wakeup(&pdev->dev)) |
---|
191 | | - disable_irq_wake(pdata->irq); |
---|
| 220 | + error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq); |
---|
| 221 | + if (error) |
---|
| 222 | + dev_err(&pdev->dev, "irq wake enable failed.\n"); |
---|
192 | 223 | |
---|
193 | 224 | return 0; |
---|
194 | 225 | } |
---|
.. | .. |
---|
199 | 230 | }; |
---|
200 | 231 | MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids); |
---|
201 | 232 | |
---|
202 | | -static SIMPLE_DEV_PM_OPS(imx_snvs_pwrkey_pm_ops, imx_snvs_pwrkey_suspend, |
---|
203 | | - imx_snvs_pwrkey_resume); |
---|
204 | | - |
---|
205 | 233 | static struct platform_driver imx_snvs_pwrkey_driver = { |
---|
206 | 234 | .driver = { |
---|
207 | 235 | .name = "snvs_pwrkey", |
---|
208 | | - .pm = &imx_snvs_pwrkey_pm_ops, |
---|
209 | 236 | .of_match_table = imx_snvs_pwrkey_ids, |
---|
210 | 237 | }, |
---|
211 | 238 | .probe = imx_snvs_pwrkey_probe, |
---|