Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Power key driver for SiRF PrimaII |
| 3 | * |
| 4 | * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company. |
| 5 | * |
| 6 | * Licensed under GPLv2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/input.h> |
| 14 | #include <linux/rtc/sirfsoc_rtciobrg.h> |
| 15 | #include <linux/of.h> |
| 16 | |
| 17 | struct sirfsoc_pwrc_drvdata { |
| 18 | u32 pwrc_base; |
| 19 | struct input_dev *input; |
| 20 | }; |
| 21 | |
| 22 | #define PWRC_ON_KEY_BIT (1 << 0) |
| 23 | |
| 24 | #define PWRC_INT_STATUS 0xc |
| 25 | #define PWRC_INT_MASK 0x10 |
| 26 | |
| 27 | static irqreturn_t sirfsoc_pwrc_isr(int irq, void *dev_id) |
| 28 | { |
| 29 | struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_id; |
| 30 | u32 int_status; |
| 31 | |
| 32 | int_status = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base + |
| 33 | PWRC_INT_STATUS); |
| 34 | sirfsoc_rtc_iobrg_writel(int_status & ~PWRC_ON_KEY_BIT, |
| 35 | pwrcdrv->pwrc_base + PWRC_INT_STATUS); |
| 36 | |
| 37 | /* |
| 38 | * For a typical Linux system, we report KEY_SUSPEND to trigger apm-power.c |
| 39 | * to queue a SUSPEND APM event |
| 40 | */ |
| 41 | input_event(pwrcdrv->input, EV_PWR, KEY_SUSPEND, 1); |
| 42 | input_sync(pwrcdrv->input); |
| 43 | |
| 44 | /* |
| 45 | * Todo: report KEY_POWER event for Android platforms, Android PowerManager |
| 46 | * will handle the suspend and powerdown/hibernation |
| 47 | */ |
| 48 | |
| 49 | return IRQ_HANDLED; |
| 50 | } |
| 51 | |
Dmitry Torokhov | 864d3c0 | 2014-02-13 23:38:43 -0800 | [diff] [blame] | 52 | static void sirfsoc_pwrc_toggle_interrupts(struct sirfsoc_pwrc_drvdata *pwrcdrv, |
| 53 | bool enable) |
| 54 | { |
| 55 | u32 int_mask; |
| 56 | |
| 57 | int_mask = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base + PWRC_INT_MASK); |
| 58 | if (enable) |
| 59 | int_mask |= PWRC_ON_KEY_BIT; |
| 60 | else |
| 61 | int_mask &= ~PWRC_ON_KEY_BIT; |
| 62 | sirfsoc_rtc_iobrg_writel(int_mask, pwrcdrv->pwrc_base + PWRC_INT_MASK); |
| 63 | } |
| 64 | |
| 65 | static int sirfsoc_pwrc_open(struct input_dev *input) |
| 66 | { |
| 67 | struct sirfsoc_pwrc_drvdata *pwrcdrv = input_get_drvdata(input); |
| 68 | |
| 69 | sirfsoc_pwrc_toggle_interrupts(pwrcdrv, true); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static void sirfsoc_pwrc_close(struct input_dev *input) |
| 75 | { |
| 76 | struct sirfsoc_pwrc_drvdata *pwrcdrv = input_get_drvdata(input); |
| 77 | |
| 78 | sirfsoc_pwrc_toggle_interrupts(pwrcdrv, false); |
| 79 | } |
| 80 | |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 81 | static const struct of_device_id sirfsoc_pwrc_of_match[] = { |
| 82 | { .compatible = "sirf,prima2-pwrc" }, |
| 83 | {}, |
| 84 | } |
| 85 | MODULE_DEVICE_TABLE(of, sirfsoc_pwrc_of_match); |
| 86 | |
| 87 | static int sirfsoc_pwrc_probe(struct platform_device *pdev) |
| 88 | { |
| 89 | struct device_node *np = pdev->dev.of_node; |
| 90 | struct sirfsoc_pwrc_drvdata *pwrcdrv; |
| 91 | int irq; |
| 92 | int error; |
| 93 | |
| 94 | pwrcdrv = devm_kzalloc(&pdev->dev, sizeof(struct sirfsoc_pwrc_drvdata), |
| 95 | GFP_KERNEL); |
| 96 | if (!pwrcdrv) { |
| 97 | dev_info(&pdev->dev, "Not enough memory for the device data\n"); |
| 98 | return -ENOMEM; |
| 99 | } |
| 100 | |
| 101 | /* |
Dmitry Torokhov | 864d3c0 | 2014-02-13 23:38:43 -0800 | [diff] [blame] | 102 | * We can't use of_iomap because pwrc is not mapped in memory, |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 103 | * the so-called base address is only offset in rtciobrg |
| 104 | */ |
| 105 | error = of_property_read_u32(np, "reg", &pwrcdrv->pwrc_base); |
| 106 | if (error) { |
| 107 | dev_err(&pdev->dev, |
| 108 | "unable to find base address of pwrc node in dtb\n"); |
| 109 | return error; |
| 110 | } |
| 111 | |
| 112 | pwrcdrv->input = devm_input_allocate_device(&pdev->dev); |
| 113 | if (!pwrcdrv->input) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | pwrcdrv->input->name = "sirfsoc pwrckey"; |
| 117 | pwrcdrv->input->phys = "pwrc/input0"; |
| 118 | pwrcdrv->input->evbit[0] = BIT_MASK(EV_PWR); |
| 119 | |
Dmitry Torokhov | 864d3c0 | 2014-02-13 23:38:43 -0800 | [diff] [blame] | 120 | pwrcdrv->input->open = sirfsoc_pwrc_open; |
| 121 | pwrcdrv->input->close = sirfsoc_pwrc_close; |
| 122 | |
| 123 | input_set_drvdata(pwrcdrv->input, pwrcdrv); |
| 124 | |
| 125 | /* Make sure the device is quiesced */ |
| 126 | sirfsoc_pwrc_toggle_interrupts(pwrcdrv, false); |
| 127 | |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 128 | irq = platform_get_irq(pdev, 0); |
| 129 | error = devm_request_irq(&pdev->dev, irq, |
Barry Song | 5099817 | 2014-02-14 08:43:05 -0800 | [diff] [blame] | 130 | sirfsoc_pwrc_isr, 0, |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 131 | "sirfsoc_pwrc_int", pwrcdrv); |
| 132 | if (error) { |
| 133 | dev_err(&pdev->dev, "unable to claim irq %d, error: %d\n", |
| 134 | irq, error); |
| 135 | return error; |
| 136 | } |
| 137 | |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 138 | error = input_register_device(pwrcdrv->input); |
| 139 | if (error) { |
| 140 | dev_err(&pdev->dev, |
| 141 | "unable to register input device, error: %d\n", |
| 142 | error); |
| 143 | return error; |
| 144 | } |
| 145 | |
Xianglong Du | a5e4664 | 2014-02-14 08:45:56 -0800 | [diff] [blame^] | 146 | dev_set_drvdata(&pdev->dev, pwrcdrv); |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 147 | device_init_wakeup(&pdev->dev, 1); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int sirfsoc_pwrc_remove(struct platform_device *pdev) |
| 153 | { |
| 154 | device_init_wakeup(&pdev->dev, 0); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | #ifdef CONFIG_PM_SLEEP |
Xianglong Du | 3a80035 | 2014-02-14 08:45:13 -0800 | [diff] [blame] | 160 | static int sirfsoc_pwrc_resume(struct device *dev) |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 161 | { |
Xianglong Du | a5e4664 | 2014-02-14 08:45:56 -0800 | [diff] [blame^] | 162 | struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_get_drvdata(dev); |
Dmitry Torokhov | 864d3c0 | 2014-02-13 23:38:43 -0800 | [diff] [blame] | 163 | struct input_dev *input = pwrcdrv->input; |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * Do not mask pwrc interrupt as we want pwrc work as a wakeup source |
| 167 | * if users touch X_ONKEY_B, see arch/arm/mach-prima2/pm.c |
| 168 | */ |
Dmitry Torokhov | 864d3c0 | 2014-02-13 23:38:43 -0800 | [diff] [blame] | 169 | mutex_lock(&input->mutex); |
| 170 | if (input->users) |
| 171 | sirfsoc_pwrc_toggle_interrupts(pwrcdrv, true); |
| 172 | mutex_unlock(&input->mutex); |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | #endif |
| 177 | |
Xianglong Du | 3a80035 | 2014-02-14 08:45:13 -0800 | [diff] [blame] | 178 | static SIMPLE_DEV_PM_OPS(sirfsoc_pwrc_pm_ops, NULL, sirfsoc_pwrc_resume); |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 179 | |
| 180 | static struct platform_driver sirfsoc_pwrc_driver = { |
| 181 | .probe = sirfsoc_pwrc_probe, |
| 182 | .remove = sirfsoc_pwrc_remove, |
| 183 | .driver = { |
| 184 | .name = "sirfsoc-pwrc", |
| 185 | .owner = THIS_MODULE, |
| 186 | .pm = &sirfsoc_pwrc_pm_ops, |
Sachin Kamat | dee964c | 2013-10-06 00:51:28 -0700 | [diff] [blame] | 187 | .of_match_table = sirfsoc_pwrc_of_match, |
Binghua Duan | 9b5f953 | 2013-06-02 23:38:46 -0700 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | |
| 191 | module_platform_driver(sirfsoc_pwrc_driver); |
| 192 | |
| 193 | MODULE_LICENSE("GPLv2"); |
| 194 | MODULE_AUTHOR("Binghua Duan <Binghua.Duan@csr.com>, Xianglong Du <Xianglong.Du@csr.com>"); |
| 195 | MODULE_DESCRIPTION("CSR Prima2 PWRC Driver"); |
| 196 | MODULE_ALIAS("platform:sirfsoc-pwrc"); |