Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * SPEAr Keyboard Driver |
| 3 | * Based on omap-keypad driver |
| 4 | * |
| 5 | * Copyright (C) 2010 ST Microelectronics |
| 6 | * Rajeev Kumar<rajeev-dlh.kumar@st.com> |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public |
| 9 | * License version 2. This program is licensed "as is" without any |
| 10 | * warranty of any kind, whether express or implied. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/errno.h> |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/input.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/irq.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 21 | #include <linux/of.h> |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/pm_wakeup.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/types.h> |
Arnd Bergmann | 6884b68 | 2012-08-24 15:22:23 +0200 | [diff] [blame] | 26 | #include <linux/platform_data/keyboard-spear.h> |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 27 | |
| 28 | /* Keyboard Registers */ |
Shiraz Hashim | f6f2efa | 2012-07-07 18:00:54 -0700 | [diff] [blame] | 29 | #define MODE_CTL_REG 0x00 |
Shiraz Hashim | e99191f | 2012-07-07 18:00:36 -0700 | [diff] [blame] | 30 | #define STATUS_REG 0x0C |
| 31 | #define DATA_REG 0x10 |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 32 | #define INTR_MASK 0x54 |
| 33 | |
| 34 | /* Register Values */ |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 35 | #define NUM_ROWS 16 |
| 36 | #define NUM_COLS 16 |
Shiraz Hashim | 98e4d4d | 2012-07-07 18:01:07 -0700 | [diff] [blame] | 37 | #define MODE_CTL_PCLK_FREQ_SHIFT 9 |
| 38 | #define MODE_CTL_PCLK_FREQ_MSK 0x7F |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 39 | |
Shiraz Hashim | f6f2efa | 2012-07-07 18:00:54 -0700 | [diff] [blame] | 40 | #define MODE_CTL_KEYBOARD (0x2 << 0) |
| 41 | #define MODE_CTL_SCAN_RATE_10 (0x0 << 2) |
| 42 | #define MODE_CTL_SCAN_RATE_20 (0x1 << 2) |
| 43 | #define MODE_CTL_SCAN_RATE_40 (0x2 << 2) |
| 44 | #define MODE_CTL_SCAN_RATE_80 (0x3 << 2) |
| 45 | #define MODE_CTL_KEYNUM_SHIFT 6 |
| 46 | #define MODE_CTL_START_SCAN (0x1 << 8) |
| 47 | |
| 48 | #define STATUS_DATA_AVAIL (0x1 << 1) |
| 49 | |
| 50 | #define DATA_ROW_MASK 0xF0 |
| 51 | #define DATA_COLUMN_MASK 0x0F |
| 52 | |
| 53 | #define ROW_SHIFT 4 |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 54 | |
| 55 | struct spear_kbd { |
| 56 | struct input_dev *input; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 57 | void __iomem *io_base; |
| 58 | struct clk *clk; |
| 59 | unsigned int irq; |
Rajeev Kumar | f8354c6 | 2012-02-24 00:51:40 -0800 | [diff] [blame] | 60 | unsigned int mode; |
Deepak Sikri | 1eee4af | 2012-11-08 16:35:27 -0800 | [diff] [blame] | 61 | unsigned int suspended_rate; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 62 | unsigned short last_key; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 63 | unsigned short keycodes[NUM_ROWS * NUM_COLS]; |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 64 | bool rep; |
Deepak Sikri | 1eee4af | 2012-11-08 16:35:27 -0800 | [diff] [blame] | 65 | bool irq_wake_enabled; |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 66 | u32 mode_ctl_reg; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static irqreturn_t spear_kbd_interrupt(int irq, void *dev_id) |
| 70 | { |
| 71 | struct spear_kbd *kbd = dev_id; |
| 72 | struct input_dev *input = kbd->input; |
| 73 | unsigned int key; |
Shiraz Hashim | e99191f | 2012-07-07 18:00:36 -0700 | [diff] [blame] | 74 | u32 sts, val; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 75 | |
Shiraz Hashim | e99191f | 2012-07-07 18:00:36 -0700 | [diff] [blame] | 76 | sts = readl_relaxed(kbd->io_base + STATUS_REG); |
Shiraz Hashim | f6f2efa | 2012-07-07 18:00:54 -0700 | [diff] [blame] | 77 | if (!(sts & STATUS_DATA_AVAIL)) |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 78 | return IRQ_NONE; |
| 79 | |
| 80 | if (kbd->last_key != KEY_RESERVED) { |
| 81 | input_report_key(input, kbd->last_key, 0); |
| 82 | kbd->last_key = KEY_RESERVED; |
| 83 | } |
| 84 | |
| 85 | /* following reads active (row, col) pair */ |
Shiraz Hashim | f6f2efa | 2012-07-07 18:00:54 -0700 | [diff] [blame] | 86 | val = readl_relaxed(kbd->io_base + DATA_REG) & |
| 87 | (DATA_ROW_MASK | DATA_COLUMN_MASK); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 88 | key = kbd->keycodes[val]; |
| 89 | |
| 90 | input_event(input, EV_MSC, MSC_SCAN, val); |
| 91 | input_report_key(input, key, 1); |
| 92 | input_sync(input); |
| 93 | |
| 94 | kbd->last_key = key; |
| 95 | |
| 96 | /* clear interrupt */ |
Shiraz Hashim | e99191f | 2012-07-07 18:00:36 -0700 | [diff] [blame] | 97 | writel_relaxed(0, kbd->io_base + STATUS_REG); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 98 | |
| 99 | return IRQ_HANDLED; |
| 100 | } |
| 101 | |
| 102 | static int spear_kbd_open(struct input_dev *dev) |
| 103 | { |
| 104 | struct spear_kbd *kbd = input_get_drvdata(dev); |
| 105 | int error; |
Shiraz Hashim | e99191f | 2012-07-07 18:00:36 -0700 | [diff] [blame] | 106 | u32 val; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 107 | |
| 108 | kbd->last_key = KEY_RESERVED; |
| 109 | |
| 110 | error = clk_enable(kbd->clk); |
| 111 | if (error) |
| 112 | return error; |
| 113 | |
Shiraz Hashim | 98e4d4d | 2012-07-07 18:01:07 -0700 | [diff] [blame] | 114 | /* keyboard rate to be programmed is input clock (in MHz) - 1 */ |
| 115 | val = clk_get_rate(kbd->clk) / 1000000 - 1; |
| 116 | val = (val & MODE_CTL_PCLK_FREQ_MSK) << MODE_CTL_PCLK_FREQ_SHIFT; |
| 117 | |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 118 | /* program keyboard */ |
Shiraz Hashim | 98e4d4d | 2012-07-07 18:01:07 -0700 | [diff] [blame] | 119 | val = MODE_CTL_SCAN_RATE_80 | MODE_CTL_KEYBOARD | val | |
Shiraz Hashim | f6f2efa | 2012-07-07 18:00:54 -0700 | [diff] [blame] | 120 | (kbd->mode << MODE_CTL_KEYNUM_SHIFT); |
| 121 | writel_relaxed(val, kbd->io_base + MODE_CTL_REG); |
Shiraz Hashim | e99191f | 2012-07-07 18:00:36 -0700 | [diff] [blame] | 122 | writel_relaxed(1, kbd->io_base + STATUS_REG); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 123 | |
| 124 | /* start key scan */ |
Shiraz Hashim | f6f2efa | 2012-07-07 18:00:54 -0700 | [diff] [blame] | 125 | val = readl_relaxed(kbd->io_base + MODE_CTL_REG); |
| 126 | val |= MODE_CTL_START_SCAN; |
| 127 | writel_relaxed(val, kbd->io_base + MODE_CTL_REG); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static void spear_kbd_close(struct input_dev *dev) |
| 133 | { |
| 134 | struct spear_kbd *kbd = input_get_drvdata(dev); |
Shiraz Hashim | e99191f | 2012-07-07 18:00:36 -0700 | [diff] [blame] | 135 | u32 val; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 136 | |
| 137 | /* stop key scan */ |
Shiraz Hashim | f6f2efa | 2012-07-07 18:00:54 -0700 | [diff] [blame] | 138 | val = readl_relaxed(kbd->io_base + MODE_CTL_REG); |
| 139 | val &= ~MODE_CTL_START_SCAN; |
| 140 | writel_relaxed(val, kbd->io_base + MODE_CTL_REG); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 141 | |
| 142 | clk_disable(kbd->clk); |
| 143 | |
| 144 | kbd->last_key = KEY_RESERVED; |
| 145 | } |
| 146 | |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 147 | #ifdef CONFIG_OF |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 148 | static int spear_kbd_parse_dt(struct platform_device *pdev, |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 149 | struct spear_kbd *kbd) |
| 150 | { |
| 151 | struct device_node *np = pdev->dev.of_node; |
| 152 | int error; |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 153 | u32 val, suspended_rate; |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 154 | |
| 155 | if (!np) { |
| 156 | dev_err(&pdev->dev, "Missing DT data\n"); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | if (of_property_read_bool(np, "autorepeat")) |
| 161 | kbd->rep = true; |
| 162 | |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 163 | if (of_property_read_u32(np, "suspended_rate", &suspended_rate)) |
| 164 | kbd->suspended_rate = suspended_rate; |
| 165 | |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 166 | error = of_property_read_u32(np, "st,mode", &val); |
| 167 | if (error) { |
| 168 | dev_err(&pdev->dev, "DT: Invalid or missing mode\n"); |
| 169 | return error; |
| 170 | } |
| 171 | |
| 172 | kbd->mode = val; |
| 173 | return 0; |
| 174 | } |
| 175 | #else |
| 176 | static inline int spear_kbd_parse_dt(struct platform_device *pdev, |
| 177 | struct spear_kbd *kbd) |
| 178 | { |
| 179 | return -ENOSYS; |
| 180 | } |
| 181 | #endif |
| 182 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 183 | static int spear_kbd_probe(struct platform_device *pdev) |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 184 | { |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 185 | struct kbd_platform_data *pdata = dev_get_platdata(&pdev->dev); |
| 186 | const struct matrix_keymap_data *keymap = pdata ? pdata->keymap : NULL; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 187 | struct spear_kbd *kbd; |
| 188 | struct input_dev *input_dev; |
| 189 | struct resource *res; |
| 190 | int irq; |
| 191 | int error; |
| 192 | |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 193 | irq = platform_get_irq(pdev, 0); |
| 194 | if (irq < 0) { |
| 195 | dev_err(&pdev->dev, "not able to get irq for the device\n"); |
| 196 | return irq; |
| 197 | } |
| 198 | |
Viresh Kumar | 6102752 | 2012-11-08 21:41:24 -0800 | [diff] [blame] | 199 | kbd = devm_kzalloc(&pdev->dev, sizeof(*kbd), GFP_KERNEL); |
| 200 | if (!kbd) { |
| 201 | dev_err(&pdev->dev, "not enough memory for driver data\n"); |
| 202 | return -ENOMEM; |
| 203 | } |
| 204 | |
| 205 | input_dev = devm_input_allocate_device(&pdev->dev); |
| 206 | if (!input_dev) { |
| 207 | dev_err(&pdev->dev, "unable to allocate input device\n"); |
| 208 | return -ENOMEM; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | kbd->input = input_dev; |
| 212 | kbd->irq = irq; |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 213 | |
| 214 | if (!pdata) { |
| 215 | error = spear_kbd_parse_dt(pdev, kbd); |
| 216 | if (error) |
Viresh Kumar | 6102752 | 2012-11-08 21:41:24 -0800 | [diff] [blame] | 217 | return error; |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 218 | } else { |
| 219 | kbd->mode = pdata->mode; |
| 220 | kbd->rep = pdata->rep; |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 221 | kbd->suspended_rate = pdata->suspended_rate; |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 222 | } |
Rajeev Kumar | f8354c6 | 2012-02-24 00:51:40 -0800 | [diff] [blame] | 223 | |
Julia Lawall | ef0aca7 | 2013-08-15 00:08:17 -0700 | [diff] [blame] | 224 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 8d1cbc9 | 2013-01-21 11:09:05 +0100 | [diff] [blame] | 225 | kbd->io_base = devm_ioremap_resource(&pdev->dev, res); |
| 226 | if (IS_ERR(kbd->io_base)) |
| 227 | return PTR_ERR(kbd->io_base); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 228 | |
Viresh Kumar | 6102752 | 2012-11-08 21:41:24 -0800 | [diff] [blame] | 229 | kbd->clk = devm_clk_get(&pdev->dev, NULL); |
| 230 | if (IS_ERR(kbd->clk)) |
| 231 | return PTR_ERR(kbd->clk); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 232 | |
| 233 | input_dev->name = "Spear Keyboard"; |
| 234 | input_dev->phys = "keyboard/input0"; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 235 | input_dev->id.bustype = BUS_HOST; |
| 236 | input_dev->id.vendor = 0x0001; |
| 237 | input_dev->id.product = 0x0001; |
| 238 | input_dev->id.version = 0x0100; |
| 239 | input_dev->open = spear_kbd_open; |
| 240 | input_dev->close = spear_kbd_close; |
| 241 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 242 | error = matrix_keypad_build_keymap(keymap, NULL, NUM_ROWS, NUM_COLS, |
| 243 | kbd->keycodes, input_dev); |
| 244 | if (error) { |
| 245 | dev_err(&pdev->dev, "Failed to build keymap\n"); |
Viresh Kumar | 6102752 | 2012-11-08 21:41:24 -0800 | [diff] [blame] | 246 | return error; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 249 | if (kbd->rep) |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 250 | __set_bit(EV_REP, input_dev->evbit); |
| 251 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
| 252 | |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 253 | input_set_drvdata(input_dev, kbd); |
| 254 | |
Viresh Kumar | 6102752 | 2012-11-08 21:41:24 -0800 | [diff] [blame] | 255 | error = devm_request_irq(&pdev->dev, irq, spear_kbd_interrupt, 0, |
| 256 | "keyboard", kbd); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 257 | if (error) { |
Viresh Kumar | 6102752 | 2012-11-08 21:41:24 -0800 | [diff] [blame] | 258 | dev_err(&pdev->dev, "request_irq failed\n"); |
| 259 | return error; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Vipul Kumar Samar | 9336648 | 2012-11-26 08:50:08 -0800 | [diff] [blame] | 262 | error = clk_prepare(kbd->clk); |
| 263 | if (error) |
| 264 | return error; |
| 265 | |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 266 | error = input_register_device(input_dev); |
| 267 | if (error) { |
| 268 | dev_err(&pdev->dev, "Unable to register keyboard device\n"); |
Vipul Kumar Samar | 9336648 | 2012-11-26 08:50:08 -0800 | [diff] [blame] | 269 | clk_unprepare(kbd->clk); |
Viresh Kumar | 6102752 | 2012-11-08 21:41:24 -0800 | [diff] [blame] | 270 | return error; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | device_init_wakeup(&pdev->dev, 1); |
| 274 | platform_set_drvdata(pdev, kbd); |
| 275 | |
| 276 | return 0; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 279 | static int spear_kbd_remove(struct platform_device *pdev) |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 280 | { |
Vipul Kumar Samar | 9336648 | 2012-11-26 08:50:08 -0800 | [diff] [blame] | 281 | struct spear_kbd *kbd = platform_get_drvdata(pdev); |
| 282 | |
| 283 | input_unregister_device(kbd->input); |
| 284 | clk_unprepare(kbd->clk); |
| 285 | |
Vipul Kumar Samar | 5bdea83 | 2012-07-07 18:00:10 -0700 | [diff] [blame] | 286 | device_init_wakeup(&pdev->dev, 0); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | #ifdef CONFIG_PM |
| 292 | static int spear_kbd_suspend(struct device *dev) |
| 293 | { |
| 294 | struct platform_device *pdev = to_platform_device(dev); |
| 295 | struct spear_kbd *kbd = platform_get_drvdata(pdev); |
| 296 | struct input_dev *input_dev = kbd->input; |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 297 | unsigned int rate = 0, mode_ctl_reg, val; |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 298 | |
| 299 | mutex_lock(&input_dev->mutex); |
| 300 | |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 301 | /* explicitly enable clock as we may program device */ |
| 302 | clk_enable(kbd->clk); |
| 303 | |
| 304 | mode_ctl_reg = readl_relaxed(kbd->io_base + MODE_CTL_REG); |
| 305 | |
Shiraz Hashim | 53fe6285 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 306 | if (device_may_wakeup(&pdev->dev)) { |
Deepak Sikri | 1eee4af | 2012-11-08 16:35:27 -0800 | [diff] [blame] | 307 | if (!enable_irq_wake(kbd->irq)) |
| 308 | kbd->irq_wake_enabled = true; |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * reprogram the keyboard operating frequency as on some |
| 312 | * platform it may change during system suspended |
| 313 | */ |
| 314 | if (kbd->suspended_rate) |
| 315 | rate = kbd->suspended_rate / 1000000 - 1; |
| 316 | else |
| 317 | rate = clk_get_rate(kbd->clk) / 1000000 - 1; |
| 318 | |
| 319 | val = mode_ctl_reg & |
| 320 | ~(MODE_CTL_PCLK_FREQ_MSK << MODE_CTL_PCLK_FREQ_SHIFT); |
| 321 | val |= (rate & MODE_CTL_PCLK_FREQ_MSK) |
| 322 | << MODE_CTL_PCLK_FREQ_SHIFT; |
| 323 | writel_relaxed(val, kbd->io_base + MODE_CTL_REG); |
| 324 | |
Shiraz Hashim | 53fe6285 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 325 | } else { |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 326 | if (input_dev->users) { |
| 327 | writel_relaxed(mode_ctl_reg & ~MODE_CTL_START_SCAN, |
| 328 | kbd->io_base + MODE_CTL_REG); |
Shiraz Hashim | 53fe6285 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 329 | clk_disable(kbd->clk); |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 330 | } |
Shiraz Hashim | 53fe6285 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 331 | } |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 332 | |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 333 | /* store current configuration */ |
| 334 | if (input_dev->users) |
| 335 | kbd->mode_ctl_reg = mode_ctl_reg; |
| 336 | |
| 337 | /* restore previous clk state */ |
| 338 | clk_disable(kbd->clk); |
| 339 | |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 340 | mutex_unlock(&input_dev->mutex); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int spear_kbd_resume(struct device *dev) |
| 346 | { |
| 347 | struct platform_device *pdev = to_platform_device(dev); |
| 348 | struct spear_kbd *kbd = platform_get_drvdata(pdev); |
| 349 | struct input_dev *input_dev = kbd->input; |
| 350 | |
| 351 | mutex_lock(&input_dev->mutex); |
| 352 | |
Shiraz Hashim | 53fe6285 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 353 | if (device_may_wakeup(&pdev->dev)) { |
Deepak Sikri | 1eee4af | 2012-11-08 16:35:27 -0800 | [diff] [blame] | 354 | if (kbd->irq_wake_enabled) { |
| 355 | kbd->irq_wake_enabled = false; |
| 356 | disable_irq_wake(kbd->irq); |
| 357 | } |
Shiraz Hashim | 53fe6285 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 358 | } else { |
| 359 | if (input_dev->users) |
| 360 | clk_enable(kbd->clk); |
| 361 | } |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 362 | |
Shiraz Hashim | 8314f53 | 2012-07-13 00:11:10 -0700 | [diff] [blame] | 363 | /* restore current configuration */ |
| 364 | if (input_dev->users) |
| 365 | writel_relaxed(kbd->mode_ctl_reg, kbd->io_base + MODE_CTL_REG); |
| 366 | |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 367 | mutex_unlock(&input_dev->mutex); |
| 368 | |
| 369 | return 0; |
| 370 | } |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 371 | #endif |
| 372 | |
Viresh Kumar | f79e30a | 2012-02-24 00:51:37 -0800 | [diff] [blame] | 373 | static SIMPLE_DEV_PM_OPS(spear_kbd_pm_ops, spear_kbd_suspend, spear_kbd_resume); |
| 374 | |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 375 | #ifdef CONFIG_OF |
| 376 | static const struct of_device_id spear_kbd_id_table[] = { |
| 377 | { .compatible = "st,spear300-kbd" }, |
| 378 | {} |
| 379 | }; |
| 380 | MODULE_DEVICE_TABLE(of, spear_kbd_id_table); |
| 381 | #endif |
| 382 | |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 383 | static struct platform_driver spear_kbd_driver = { |
| 384 | .probe = spear_kbd_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 385 | .remove = spear_kbd_remove, |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 386 | .driver = { |
| 387 | .name = "keyboard", |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 388 | .pm = &spear_kbd_pm_ops, |
Viresh Kumar | 829c4f9 | 2012-05-10 22:37:16 -0700 | [diff] [blame] | 389 | .of_match_table = of_match_ptr(spear_kbd_id_table), |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 390 | }, |
| 391 | }; |
JJ Ding | 5146c84 | 2011-11-29 11:08:39 -0800 | [diff] [blame] | 392 | module_platform_driver(spear_kbd_driver); |
Rajeev Kumar | bc95df7 | 2010-11-19 12:41:19 -0800 | [diff] [blame] | 393 | |
| 394 | MODULE_AUTHOR("Rajeev Kumar"); |
| 395 | MODULE_DESCRIPTION("SPEAr Keyboard Driver"); |
| 396 | MODULE_LICENSE("GPL"); |