0001
0002
0003
0004
0005
0006 #include <linux/clk.h>
0007 #include <linux/device.h>
0008 #include <linux/err.h>
0009 #include <linux/init.h>
0010 #include <linux/input.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/io.h>
0013 #include <linux/jiffies.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/of_address.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/pm_wakeirq.h>
0020 #include <linux/mfd/syscon.h>
0021 #include <linux/regmap.h>
0022
0023 #define SNVS_HPVIDR1_REG 0xBF8
0024 #define SNVS_LPSR_REG 0x4C
0025 #define SNVS_LPCR_REG 0x38
0026 #define SNVS_HPSR_REG 0x14
0027 #define SNVS_HPSR_BTN BIT(6)
0028 #define SNVS_LPSR_SPO BIT(18)
0029 #define SNVS_LPCR_DEP_EN BIT(5)
0030
0031 #define DEBOUNCE_TIME 30
0032 #define REPEAT_INTERVAL 60
0033
0034 struct pwrkey_drv_data {
0035 struct regmap *snvs;
0036 int irq;
0037 int keycode;
0038 int keystate;
0039 int wakeup;
0040 struct timer_list check_timer;
0041 struct input_dev *input;
0042 u8 minor_rev;
0043 };
0044
0045 static void imx_imx_snvs_check_for_events(struct timer_list *t)
0046 {
0047 struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer);
0048 struct input_dev *input = pdata->input;
0049 u32 state;
0050
0051 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
0052 state = state & SNVS_HPSR_BTN ? 1 : 0;
0053
0054
0055 if (state ^ pdata->keystate) {
0056 pdata->keystate = state;
0057 input_event(input, EV_KEY, pdata->keycode, state);
0058 input_sync(input);
0059 pm_relax(pdata->input->dev.parent);
0060 }
0061
0062
0063 if (state) {
0064 mod_timer(&pdata->check_timer,
0065 jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
0066 }
0067 }
0068
0069 static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
0070 {
0071 struct platform_device *pdev = dev_id;
0072 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
0073 struct input_dev *input = pdata->input;
0074 u32 lp_status;
0075
0076 pm_wakeup_event(input->dev.parent, 0);
0077
0078 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
0079 if (lp_status & SNVS_LPSR_SPO) {
0080 if (pdata->minor_rev == 0) {
0081
0082
0083
0084
0085
0086 input_report_key(input, pdata->keycode, 1);
0087 input_sync(input);
0088 input_report_key(input, pdata->keycode, 0);
0089 input_sync(input);
0090 pm_relax(input->dev.parent);
0091 } else {
0092 mod_timer(&pdata->check_timer,
0093 jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
0094 }
0095 }
0096
0097
0098 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
0099
0100 return IRQ_HANDLED;
0101 }
0102
0103 static void imx_snvs_pwrkey_disable_clk(void *data)
0104 {
0105 clk_disable_unprepare(data);
0106 }
0107
0108 static void imx_snvs_pwrkey_act(void *pdata)
0109 {
0110 struct pwrkey_drv_data *pd = pdata;
0111
0112 del_timer_sync(&pd->check_timer);
0113 }
0114
0115 static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
0116 {
0117 struct pwrkey_drv_data *pdata;
0118 struct input_dev *input;
0119 struct device_node *np;
0120 struct clk *clk;
0121 int error;
0122 u32 vid;
0123
0124
0125 np = pdev->dev.of_node;
0126 if (!np)
0127 return -ENODEV;
0128
0129 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
0130 if (!pdata)
0131 return -ENOMEM;
0132
0133 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
0134 if (IS_ERR(pdata->snvs)) {
0135 dev_err(&pdev->dev, "Can't get snvs syscon\n");
0136 return PTR_ERR(pdata->snvs);
0137 }
0138
0139 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
0140 pdata->keycode = KEY_POWER;
0141 dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
0142 }
0143
0144 clk = devm_clk_get_optional(&pdev->dev, NULL);
0145 if (IS_ERR(clk)) {
0146 dev_err(&pdev->dev, "Failed to get snvs clock (%pe)\n", clk);
0147 return PTR_ERR(clk);
0148 }
0149
0150 error = clk_prepare_enable(clk);
0151 if (error) {
0152 dev_err(&pdev->dev, "Failed to enable snvs clock (%pe)\n",
0153 ERR_PTR(error));
0154 return error;
0155 }
0156
0157 error = devm_add_action_or_reset(&pdev->dev,
0158 imx_snvs_pwrkey_disable_clk, clk);
0159 if (error) {
0160 dev_err(&pdev->dev,
0161 "Failed to register clock cleanup handler (%pe)\n",
0162 ERR_PTR(error));
0163 return error;
0164 }
0165
0166 pdata->wakeup = of_property_read_bool(np, "wakeup-source");
0167
0168 pdata->irq = platform_get_irq(pdev, 0);
0169 if (pdata->irq < 0)
0170 return -EINVAL;
0171
0172 regmap_read(pdata->snvs, SNVS_HPVIDR1_REG, &vid);
0173 pdata->minor_rev = vid & 0xff;
0174
0175 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
0176
0177
0178 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
0179
0180 timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
0181
0182 input = devm_input_allocate_device(&pdev->dev);
0183 if (!input) {
0184 dev_err(&pdev->dev, "failed to allocate the input device\n");
0185 return -ENOMEM;
0186 }
0187
0188 input->name = pdev->name;
0189 input->phys = "snvs-pwrkey/input0";
0190 input->id.bustype = BUS_HOST;
0191
0192 input_set_capability(input, EV_KEY, pdata->keycode);
0193
0194
0195 error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
0196 if (error) {
0197 dev_err(&pdev->dev, "failed to register remove action\n");
0198 return error;
0199 }
0200
0201 pdata->input = input;
0202 platform_set_drvdata(pdev, pdata);
0203
0204 error = devm_request_irq(&pdev->dev, pdata->irq,
0205 imx_snvs_pwrkey_interrupt,
0206 0, pdev->name, pdev);
0207
0208 if (error) {
0209 dev_err(&pdev->dev, "interrupt not available.\n");
0210 return error;
0211 }
0212
0213 error = input_register_device(input);
0214 if (error < 0) {
0215 dev_err(&pdev->dev, "failed to register input device\n");
0216 return error;
0217 }
0218
0219 device_init_wakeup(&pdev->dev, pdata->wakeup);
0220 error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
0221 if (error)
0222 dev_err(&pdev->dev, "irq wake enable failed.\n");
0223
0224 return 0;
0225 }
0226
0227 static const struct of_device_id imx_snvs_pwrkey_ids[] = {
0228 { .compatible = "fsl,sec-v4.0-pwrkey" },
0229 { }
0230 };
0231 MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
0232
0233 static struct platform_driver imx_snvs_pwrkey_driver = {
0234 .driver = {
0235 .name = "snvs_pwrkey",
0236 .of_match_table = imx_snvs_pwrkey_ids,
0237 },
0238 .probe = imx_snvs_pwrkey_probe,
0239 };
0240 module_platform_driver(imx_snvs_pwrkey_driver);
0241
0242 MODULE_AUTHOR("Freescale Semiconductor");
0243 MODULE_DESCRIPTION("i.MX snvs power key Driver");
0244 MODULE_LICENSE("GPL");