Merge 3.0-rc2 into tty-linus
[linux/fpc-iii.git] / drivers / input / misc / bfin_rotary.c
blob4f72bdd694102eb626011956d1b3b642d6d1ae63
1 /*
2 * Rotary counter driver for Analog Devices Blackfin Processors
4 * Copyright 2008-2009 Analog Devices Inc.
5 * Licensed under the GPL-2 or later.
6 */
8 #include <linux/module.h>
9 #include <linux/version.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/pm.h>
14 #include <linux/platform_device.h>
15 #include <linux/input.h>
16 #include <linux/slab.h>
18 #include <asm/portmux.h>
19 #include <asm/bfin_rotary.h>
21 static const u16 per_cnt[] = {
22 P_CNT_CUD,
23 P_CNT_CDG,
24 P_CNT_CZM,
28 struct bfin_rot {
29 struct input_dev *input;
30 int irq;
31 unsigned int up_key;
32 unsigned int down_key;
33 unsigned int button_key;
34 unsigned int rel_code;
35 unsigned short cnt_config;
36 unsigned short cnt_imask;
37 unsigned short cnt_debounce;
40 static void report_key_event(struct input_dev *input, int keycode)
42 /* simulate a press-n-release */
43 input_report_key(input, keycode, 1);
44 input_sync(input);
45 input_report_key(input, keycode, 0);
46 input_sync(input);
49 static void report_rotary_event(struct bfin_rot *rotary, int delta)
51 struct input_dev *input = rotary->input;
53 if (rotary->up_key) {
54 report_key_event(input,
55 delta > 0 ? rotary->up_key : rotary->down_key);
56 } else {
57 input_report_rel(input, rotary->rel_code, delta);
58 input_sync(input);
62 static irqreturn_t bfin_rotary_isr(int irq, void *dev_id)
64 struct platform_device *pdev = dev_id;
65 struct bfin_rot *rotary = platform_get_drvdata(pdev);
66 int delta;
68 switch (bfin_read_CNT_STATUS()) {
70 case ICII:
71 break;
73 case UCII:
74 case DCII:
75 delta = bfin_read_CNT_COUNTER();
76 if (delta)
77 report_rotary_event(rotary, delta);
78 break;
80 case CZMII:
81 report_key_event(rotary->input, rotary->button_key);
82 break;
84 default:
85 break;
88 bfin_write_CNT_COMMAND(W1LCNT_ZERO); /* Clear COUNTER */
89 bfin_write_CNT_STATUS(-1); /* Clear STATUS */
91 return IRQ_HANDLED;
94 static int __devinit bfin_rotary_probe(struct platform_device *pdev)
96 struct bfin_rotary_platform_data *pdata = pdev->dev.platform_data;
97 struct bfin_rot *rotary;
98 struct input_dev *input;
99 int error;
101 /* Basic validation */
102 if ((pdata->rotary_up_key && !pdata->rotary_down_key) ||
103 (!pdata->rotary_up_key && pdata->rotary_down_key)) {
104 return -EINVAL;
107 error = peripheral_request_list(per_cnt, dev_name(&pdev->dev));
108 if (error) {
109 dev_err(&pdev->dev, "requesting peripherals failed\n");
110 return error;
113 rotary = kzalloc(sizeof(struct bfin_rot), GFP_KERNEL);
114 input = input_allocate_device();
115 if (!rotary || !input) {
116 error = -ENOMEM;
117 goto out1;
120 rotary->input = input;
122 rotary->up_key = pdata->rotary_up_key;
123 rotary->down_key = pdata->rotary_down_key;
124 rotary->button_key = pdata->rotary_button_key;
125 rotary->rel_code = pdata->rotary_rel_code;
127 error = rotary->irq = platform_get_irq(pdev, 0);
128 if (error < 0)
129 goto out1;
131 input->name = pdev->name;
132 input->phys = "bfin-rotary/input0";
133 input->dev.parent = &pdev->dev;
135 input_set_drvdata(input, rotary);
137 input->id.bustype = BUS_HOST;
138 input->id.vendor = 0x0001;
139 input->id.product = 0x0001;
140 input->id.version = 0x0100;
142 if (rotary->up_key) {
143 __set_bit(EV_KEY, input->evbit);
144 __set_bit(rotary->up_key, input->keybit);
145 __set_bit(rotary->down_key, input->keybit);
146 } else {
147 __set_bit(EV_REL, input->evbit);
148 __set_bit(rotary->rel_code, input->relbit);
151 if (rotary->button_key) {
152 __set_bit(EV_KEY, input->evbit);
153 __set_bit(rotary->button_key, input->keybit);
156 error = request_irq(rotary->irq, bfin_rotary_isr,
157 0, dev_name(&pdev->dev), pdev);
158 if (error) {
159 dev_err(&pdev->dev,
160 "unable to claim irq %d; error %d\n",
161 rotary->irq, error);
162 goto out1;
165 error = input_register_device(input);
166 if (error) {
167 dev_err(&pdev->dev,
168 "unable to register input device (%d)\n", error);
169 goto out2;
172 if (pdata->rotary_button_key)
173 bfin_write_CNT_IMASK(CZMIE);
175 if (pdata->mode & ROT_DEBE)
176 bfin_write_CNT_DEBOUNCE(pdata->debounce & DPRESCALE);
178 if (pdata->mode)
179 bfin_write_CNT_CONFIG(bfin_read_CNT_CONFIG() |
180 (pdata->mode & ~CNTE));
182 bfin_write_CNT_IMASK(bfin_read_CNT_IMASK() | UCIE | DCIE);
183 bfin_write_CNT_CONFIG(bfin_read_CNT_CONFIG() | CNTE);
185 platform_set_drvdata(pdev, rotary);
186 device_init_wakeup(&pdev->dev, 1);
188 return 0;
190 out2:
191 free_irq(rotary->irq, pdev);
192 out1:
193 input_free_device(input);
194 kfree(rotary);
195 peripheral_free_list(per_cnt);
197 return error;
200 static int __devexit bfin_rotary_remove(struct platform_device *pdev)
202 struct bfin_rot *rotary = platform_get_drvdata(pdev);
204 bfin_write_CNT_CONFIG(0);
205 bfin_write_CNT_IMASK(0);
207 free_irq(rotary->irq, pdev);
208 input_unregister_device(rotary->input);
209 peripheral_free_list(per_cnt);
211 kfree(rotary);
212 platform_set_drvdata(pdev, NULL);
214 return 0;
217 #ifdef CONFIG_PM
218 static int bfin_rotary_suspend(struct device *dev)
220 struct platform_device *pdev = to_platform_device(dev);
221 struct bfin_rot *rotary = platform_get_drvdata(pdev);
223 rotary->cnt_config = bfin_read_CNT_CONFIG();
224 rotary->cnt_imask = bfin_read_CNT_IMASK();
225 rotary->cnt_debounce = bfin_read_CNT_DEBOUNCE();
227 if (device_may_wakeup(&pdev->dev))
228 enable_irq_wake(rotary->irq);
230 return 0;
233 static int bfin_rotary_resume(struct device *dev)
235 struct platform_device *pdev = to_platform_device(dev);
236 struct bfin_rot *rotary = platform_get_drvdata(pdev);
238 bfin_write_CNT_DEBOUNCE(rotary->cnt_debounce);
239 bfin_write_CNT_IMASK(rotary->cnt_imask);
240 bfin_write_CNT_CONFIG(rotary->cnt_config & ~CNTE);
242 if (device_may_wakeup(&pdev->dev))
243 disable_irq_wake(rotary->irq);
245 if (rotary->cnt_config & CNTE)
246 bfin_write_CNT_CONFIG(rotary->cnt_config);
248 return 0;
251 static const struct dev_pm_ops bfin_rotary_pm_ops = {
252 .suspend = bfin_rotary_suspend,
253 .resume = bfin_rotary_resume,
255 #endif
257 static struct platform_driver bfin_rotary_device_driver = {
258 .probe = bfin_rotary_probe,
259 .remove = __devexit_p(bfin_rotary_remove),
260 .driver = {
261 .name = "bfin-rotary",
262 .owner = THIS_MODULE,
263 #ifdef CONFIG_PM
264 .pm = &bfin_rotary_pm_ops,
265 #endif
269 static int __init bfin_rotary_init(void)
271 return platform_driver_register(&bfin_rotary_device_driver);
273 module_init(bfin_rotary_init);
275 static void __exit bfin_rotary_exit(void)
277 platform_driver_unregister(&bfin_rotary_device_driver);
279 module_exit(bfin_rotary_exit);
281 MODULE_LICENSE("GPL");
282 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
283 MODULE_DESCRIPTION("Rotary Counter driver for Blackfin Processors");
284 MODULE_ALIAS("platform:bfin-rotary");