Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Rotary counter driver for Analog Devices Blackfin Processors |
| 3 | * |
| 4 | * Copyright 2008-2009 Analog Devices Inc. |
| 5 | * Licensed under the GPL-2 or later. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/irq.h> |
| 11 | #include <linux/pm.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/input.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Sonic Zhang | 1ea7401 | 2015-02-03 17:12:17 -0800 | [diff] [blame^] | 15 | #include <linux/platform_data/bfin_rotary.h> |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 16 | |
| 17 | #include <asm/portmux.h> |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 18 | |
| 19 | static const u16 per_cnt[] = { |
| 20 | P_CNT_CUD, |
| 21 | P_CNT_CDG, |
| 22 | P_CNT_CZM, |
| 23 | 0 |
| 24 | }; |
| 25 | |
| 26 | struct bfin_rot { |
| 27 | struct input_dev *input; |
| 28 | int irq; |
| 29 | unsigned int up_key; |
| 30 | unsigned int down_key; |
| 31 | unsigned int button_key; |
| 32 | unsigned int rel_code; |
| 33 | unsigned short cnt_config; |
| 34 | unsigned short cnt_imask; |
| 35 | unsigned short cnt_debounce; |
| 36 | }; |
| 37 | |
| 38 | static void report_key_event(struct input_dev *input, int keycode) |
| 39 | { |
| 40 | /* simulate a press-n-release */ |
| 41 | input_report_key(input, keycode, 1); |
| 42 | input_sync(input); |
| 43 | input_report_key(input, keycode, 0); |
| 44 | input_sync(input); |
| 45 | } |
| 46 | |
| 47 | static void report_rotary_event(struct bfin_rot *rotary, int delta) |
| 48 | { |
| 49 | struct input_dev *input = rotary->input; |
| 50 | |
| 51 | if (rotary->up_key) { |
| 52 | report_key_event(input, |
| 53 | delta > 0 ? rotary->up_key : rotary->down_key); |
| 54 | } else { |
| 55 | input_report_rel(input, rotary->rel_code, delta); |
| 56 | input_sync(input); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static irqreturn_t bfin_rotary_isr(int irq, void *dev_id) |
| 61 | { |
Dmitry Torokhov | 7694f44 | 2015-02-10 17:47:04 -0800 | [diff] [blame] | 62 | struct bfin_rot *rotary = dev_id; |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 63 | int delta; |
| 64 | |
| 65 | switch (bfin_read_CNT_STATUS()) { |
| 66 | |
| 67 | case ICII: |
| 68 | break; |
| 69 | |
| 70 | case UCII: |
| 71 | case DCII: |
| 72 | delta = bfin_read_CNT_COUNTER(); |
| 73 | if (delta) |
| 74 | report_rotary_event(rotary, delta); |
| 75 | break; |
| 76 | |
| 77 | case CZMII: |
| 78 | report_key_event(rotary->input, rotary->button_key); |
| 79 | break; |
| 80 | |
| 81 | default: |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | bfin_write_CNT_COMMAND(W1LCNT_ZERO); /* Clear COUNTER */ |
| 86 | bfin_write_CNT_STATUS(-1); /* Clear STATUS */ |
| 87 | |
| 88 | return IRQ_HANDLED; |
| 89 | } |
| 90 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 91 | static int bfin_rotary_probe(struct platform_device *pdev) |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 92 | { |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 93 | struct bfin_rotary_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 94 | struct bfin_rot *rotary; |
| 95 | struct input_dev *input; |
| 96 | int error; |
| 97 | |
| 98 | /* Basic validation */ |
| 99 | if ((pdata->rotary_up_key && !pdata->rotary_down_key) || |
| 100 | (!pdata->rotary_up_key && pdata->rotary_down_key)) { |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | error = peripheral_request_list(per_cnt, dev_name(&pdev->dev)); |
| 105 | if (error) { |
| 106 | dev_err(&pdev->dev, "requesting peripherals failed\n"); |
| 107 | return error; |
| 108 | } |
| 109 | |
| 110 | rotary = kzalloc(sizeof(struct bfin_rot), GFP_KERNEL); |
| 111 | input = input_allocate_device(); |
| 112 | if (!rotary || !input) { |
| 113 | error = -ENOMEM; |
| 114 | goto out1; |
| 115 | } |
| 116 | |
| 117 | rotary->input = input; |
| 118 | |
| 119 | rotary->up_key = pdata->rotary_up_key; |
| 120 | rotary->down_key = pdata->rotary_down_key; |
| 121 | rotary->button_key = pdata->rotary_button_key; |
| 122 | rotary->rel_code = pdata->rotary_rel_code; |
| 123 | |
| 124 | error = rotary->irq = platform_get_irq(pdev, 0); |
| 125 | if (error < 0) |
| 126 | goto out1; |
| 127 | |
| 128 | input->name = pdev->name; |
| 129 | input->phys = "bfin-rotary/input0"; |
| 130 | input->dev.parent = &pdev->dev; |
| 131 | |
| 132 | input_set_drvdata(input, rotary); |
| 133 | |
| 134 | input->id.bustype = BUS_HOST; |
| 135 | input->id.vendor = 0x0001; |
| 136 | input->id.product = 0x0001; |
| 137 | input->id.version = 0x0100; |
| 138 | |
| 139 | if (rotary->up_key) { |
| 140 | __set_bit(EV_KEY, input->evbit); |
| 141 | __set_bit(rotary->up_key, input->keybit); |
| 142 | __set_bit(rotary->down_key, input->keybit); |
| 143 | } else { |
| 144 | __set_bit(EV_REL, input->evbit); |
| 145 | __set_bit(rotary->rel_code, input->relbit); |
| 146 | } |
| 147 | |
| 148 | if (rotary->button_key) { |
| 149 | __set_bit(EV_KEY, input->evbit); |
| 150 | __set_bit(rotary->button_key, input->keybit); |
| 151 | } |
| 152 | |
| 153 | error = request_irq(rotary->irq, bfin_rotary_isr, |
Dmitry Torokhov | 7694f44 | 2015-02-10 17:47:04 -0800 | [diff] [blame] | 154 | 0, dev_name(&pdev->dev), rotary); |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 155 | if (error) { |
| 156 | dev_err(&pdev->dev, |
| 157 | "unable to claim irq %d; error %d\n", |
| 158 | rotary->irq, error); |
| 159 | goto out1; |
| 160 | } |
| 161 | |
| 162 | error = input_register_device(input); |
| 163 | if (error) { |
| 164 | dev_err(&pdev->dev, |
| 165 | "unable to register input device (%d)\n", error); |
| 166 | goto out2; |
| 167 | } |
| 168 | |
| 169 | if (pdata->rotary_button_key) |
| 170 | bfin_write_CNT_IMASK(CZMIE); |
| 171 | |
| 172 | if (pdata->mode & ROT_DEBE) |
| 173 | bfin_write_CNT_DEBOUNCE(pdata->debounce & DPRESCALE); |
| 174 | |
| 175 | if (pdata->mode) |
| 176 | bfin_write_CNT_CONFIG(bfin_read_CNT_CONFIG() | |
| 177 | (pdata->mode & ~CNTE)); |
| 178 | |
| 179 | bfin_write_CNT_IMASK(bfin_read_CNT_IMASK() | UCIE | DCIE); |
| 180 | bfin_write_CNT_CONFIG(bfin_read_CNT_CONFIG() | CNTE); |
| 181 | |
| 182 | platform_set_drvdata(pdev, rotary); |
| 183 | device_init_wakeup(&pdev->dev, 1); |
| 184 | |
| 185 | return 0; |
| 186 | |
| 187 | out2: |
Dmitry Torokhov | 7694f44 | 2015-02-10 17:47:04 -0800 | [diff] [blame] | 188 | free_irq(rotary->irq, rotary); |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 189 | out1: |
| 190 | input_free_device(input); |
| 191 | kfree(rotary); |
| 192 | peripheral_free_list(per_cnt); |
| 193 | |
| 194 | return error; |
| 195 | } |
| 196 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 197 | static int bfin_rotary_remove(struct platform_device *pdev) |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 198 | { |
| 199 | struct bfin_rot *rotary = platform_get_drvdata(pdev); |
| 200 | |
| 201 | bfin_write_CNT_CONFIG(0); |
| 202 | bfin_write_CNT_IMASK(0); |
| 203 | |
Dmitry Torokhov | 7694f44 | 2015-02-10 17:47:04 -0800 | [diff] [blame] | 204 | free_irq(rotary->irq, rotary); |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 205 | input_unregister_device(rotary->input); |
| 206 | peripheral_free_list(per_cnt); |
| 207 | |
| 208 | kfree(rotary); |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Dmitry Torokhov | 5ec662e | 2015-02-05 22:05:51 -0800 | [diff] [blame] | 213 | static int __maybe_unused bfin_rotary_suspend(struct device *dev) |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 214 | { |
| 215 | struct platform_device *pdev = to_platform_device(dev); |
| 216 | struct bfin_rot *rotary = platform_get_drvdata(pdev); |
| 217 | |
| 218 | rotary->cnt_config = bfin_read_CNT_CONFIG(); |
| 219 | rotary->cnt_imask = bfin_read_CNT_IMASK(); |
| 220 | rotary->cnt_debounce = bfin_read_CNT_DEBOUNCE(); |
| 221 | |
| 222 | if (device_may_wakeup(&pdev->dev)) |
| 223 | enable_irq_wake(rotary->irq); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Dmitry Torokhov | 5ec662e | 2015-02-05 22:05:51 -0800 | [diff] [blame] | 228 | static int __maybe_unused bfin_rotary_resume(struct device *dev) |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 229 | { |
| 230 | struct platform_device *pdev = to_platform_device(dev); |
| 231 | struct bfin_rot *rotary = platform_get_drvdata(pdev); |
| 232 | |
| 233 | bfin_write_CNT_DEBOUNCE(rotary->cnt_debounce); |
| 234 | bfin_write_CNT_IMASK(rotary->cnt_imask); |
| 235 | bfin_write_CNT_CONFIG(rotary->cnt_config & ~CNTE); |
| 236 | |
| 237 | if (device_may_wakeup(&pdev->dev)) |
| 238 | disable_irq_wake(rotary->irq); |
| 239 | |
| 240 | if (rotary->cnt_config & CNTE) |
| 241 | bfin_write_CNT_CONFIG(rotary->cnt_config); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Dmitry Torokhov | 5ec662e | 2015-02-05 22:05:51 -0800 | [diff] [blame] | 246 | static SIMPLE_DEV_PM_OPS(bfin_rotary_pm_ops, |
| 247 | bfin_rotary_suspend, bfin_rotary_resume); |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 248 | |
| 249 | static struct platform_driver bfin_rotary_device_driver = { |
| 250 | .probe = bfin_rotary_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 251 | .remove = bfin_rotary_remove, |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 252 | .driver = { |
| 253 | .name = "bfin-rotary", |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 254 | .pm = &bfin_rotary_pm_ops, |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 255 | }, |
| 256 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 257 | module_platform_driver(bfin_rotary_device_driver); |
Michael Hennerich | 4832958 | 2009-07-22 21:51:34 -0700 | [diff] [blame] | 258 | |
| 259 | MODULE_LICENSE("GPL"); |
| 260 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 261 | MODULE_DESCRIPTION("Rotary Counter driver for Blackfin Processors"); |
| 262 | MODULE_ALIAS("platform:bfin-rotary"); |