blob: 35ead69521a09a421ce42f5a499190d7b0c0d27d [file] [log] [blame]
Michael Hennerich48329582009-07-22 21:51:34 -07001/*
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 Hennerich48329582009-07-22 21:51:34 -07009#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 Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Sonic Zhang1ea74012015-02-03 17:12:17 -080015#include <linux/platform_data/bfin_rotary.h>
Michael Hennerich48329582009-07-22 21:51:34 -070016
17#include <asm/portmux.h>
Michael Hennerich48329582009-07-22 21:51:34 -070018
19static const u16 per_cnt[] = {
20 P_CNT_CUD,
21 P_CNT_CDG,
22 P_CNT_CZM,
23 0
24};
25
26struct 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
38static 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
47static 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
60static irqreturn_t bfin_rotary_isr(int irq, void *dev_id)
61{
Dmitry Torokhov7694f442015-02-10 17:47:04 -080062 struct bfin_rot *rotary = dev_id;
Michael Hennerich48329582009-07-22 21:51:34 -070063 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 Pemberton5298cc42012-11-23 21:38:25 -080091static int bfin_rotary_probe(struct platform_device *pdev)
Michael Hennerich48329582009-07-22 21:51:34 -070092{
Jingoo Hanc838cb32013-12-05 19:21:10 -080093 struct bfin_rotary_platform_data *pdata = dev_get_platdata(&pdev->dev);
Michael Hennerich48329582009-07-22 21:51:34 -070094 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 Torokhov7694f442015-02-10 17:47:04 -0800154 0, dev_name(&pdev->dev), rotary);
Michael Hennerich48329582009-07-22 21:51:34 -0700155 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
187out2:
Dmitry Torokhov7694f442015-02-10 17:47:04 -0800188 free_irq(rotary->irq, rotary);
Michael Hennerich48329582009-07-22 21:51:34 -0700189out1:
190 input_free_device(input);
191 kfree(rotary);
192 peripheral_free_list(per_cnt);
193
194 return error;
195}
196
Bill Pembertone2619cf2012-11-23 21:50:47 -0800197static int bfin_rotary_remove(struct platform_device *pdev)
Michael Hennerich48329582009-07-22 21:51:34 -0700198{
199 struct bfin_rot *rotary = platform_get_drvdata(pdev);
200
201 bfin_write_CNT_CONFIG(0);
202 bfin_write_CNT_IMASK(0);
203
Dmitry Torokhov7694f442015-02-10 17:47:04 -0800204 free_irq(rotary->irq, rotary);
Michael Hennerich48329582009-07-22 21:51:34 -0700205 input_unregister_device(rotary->input);
206 peripheral_free_list(per_cnt);
207
208 kfree(rotary);
Michael Hennerich48329582009-07-22 21:51:34 -0700209
210 return 0;
211}
212
Dmitry Torokhov5ec662e2015-02-05 22:05:51 -0800213static int __maybe_unused bfin_rotary_suspend(struct device *dev)
Michael Hennerich48329582009-07-22 21:51:34 -0700214{
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 Torokhov5ec662e2015-02-05 22:05:51 -0800228static int __maybe_unused bfin_rotary_resume(struct device *dev)
Michael Hennerich48329582009-07-22 21:51:34 -0700229{
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 Torokhov5ec662e2015-02-05 22:05:51 -0800246static SIMPLE_DEV_PM_OPS(bfin_rotary_pm_ops,
247 bfin_rotary_suspend, bfin_rotary_resume);
Michael Hennerich48329582009-07-22 21:51:34 -0700248
249static struct platform_driver bfin_rotary_device_driver = {
250 .probe = bfin_rotary_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800251 .remove = bfin_rotary_remove,
Michael Hennerich48329582009-07-22 21:51:34 -0700252 .driver = {
253 .name = "bfin-rotary",
Michael Hennerich48329582009-07-22 21:51:34 -0700254 .pm = &bfin_rotary_pm_ops,
Michael Hennerich48329582009-07-22 21:51:34 -0700255 },
256};
JJ Ding840a7462011-11-29 11:08:40 -0800257module_platform_driver(bfin_rotary_device_driver);
Michael Hennerich48329582009-07-22 21:51:34 -0700258
259MODULE_LICENSE("GPL");
260MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
261MODULE_DESCRIPTION("Rotary Counter driver for Blackfin Processors");
262MODULE_ALIAS("platform:bfin-rotary");