]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/input/keyboard/pxa930_rotary.c
Input: use dev_get_platdata()
[karo-tx-linux.git] / drivers / input / keyboard / pxa930_rotary.c
1 /*
2  * Driver for the enhanced rotary controller on pxa930 and pxa935
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/input.h>
14 #include <linux/platform_device.h>
15 #include <linux/io.h>
16 #include <linux/slab.h>
17
18 #include <linux/platform_data/keyboard-pxa930_rotary.h>
19
20 #define SBCR    (0x04)
21 #define ERCR    (0x0c)
22
23 #define SBCR_ERSB       (1 << 5)
24
25 struct pxa930_rotary {
26         struct input_dev        *input_dev;
27         void __iomem            *mmio_base;
28         int                     last_ercr;
29
30         struct pxa930_rotary_platform_data *pdata;
31 };
32
33 static void clear_sbcr(struct pxa930_rotary *r)
34 {
35         uint32_t sbcr = __raw_readl(r->mmio_base + SBCR);
36
37         __raw_writel(sbcr | SBCR_ERSB, r->mmio_base + SBCR);
38         __raw_writel(sbcr & ~SBCR_ERSB, r->mmio_base + SBCR);
39 }
40
41 static irqreturn_t rotary_irq(int irq, void *dev_id)
42 {
43         struct pxa930_rotary *r = dev_id;
44         struct pxa930_rotary_platform_data *pdata = r->pdata;
45         int ercr, delta, key;
46
47         ercr = __raw_readl(r->mmio_base + ERCR) & 0xf;
48         clear_sbcr(r);
49
50         delta = ercr - r->last_ercr;
51         if (delta == 0)
52                 return IRQ_HANDLED;
53
54         r->last_ercr = ercr;
55
56         if (pdata->up_key && pdata->down_key) {
57                 key = (delta > 0) ? pdata->up_key : pdata->down_key;
58                 input_report_key(r->input_dev, key, 1);
59                 input_sync(r->input_dev);
60                 input_report_key(r->input_dev, key, 0);
61         } else
62                 input_report_rel(r->input_dev, pdata->rel_code, delta);
63
64         input_sync(r->input_dev);
65
66         return IRQ_HANDLED;
67 }
68
69 static int pxa930_rotary_open(struct input_dev *dev)
70 {
71         struct pxa930_rotary *r = input_get_drvdata(dev);
72
73         clear_sbcr(r);
74
75         return 0;
76 }
77
78 static void pxa930_rotary_close(struct input_dev *dev)
79 {
80         struct pxa930_rotary *r = input_get_drvdata(dev);
81
82         clear_sbcr(r);
83 }
84
85 static int pxa930_rotary_probe(struct platform_device *pdev)
86 {
87         struct pxa930_rotary_platform_data *pdata =
88                         dev_get_platdata(&pdev->dev);
89         struct pxa930_rotary *r;
90         struct input_dev *input_dev;
91         struct resource *res;
92         int irq;
93         int err;
94
95         irq = platform_get_irq(pdev, 0);
96         if (irq < 0) {
97                 dev_err(&pdev->dev, "no irq for rotary controller\n");
98                 return -ENXIO;
99         }
100
101         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
102         if (!res) {
103                 dev_err(&pdev->dev, "no I/O memory defined\n");
104                 return -ENXIO;
105         }
106
107         if (!pdata) {
108                 dev_err(&pdev->dev, "no platform data defined\n");
109                 return -EINVAL;
110         }
111
112         r = kzalloc(sizeof(struct pxa930_rotary), GFP_KERNEL);
113         if (!r)
114                 return -ENOMEM;
115
116         r->mmio_base = ioremap_nocache(res->start, resource_size(res));
117         if (r->mmio_base == NULL) {
118                 dev_err(&pdev->dev, "failed to remap IO memory\n");
119                 err = -ENXIO;
120                 goto failed_free;
121         }
122
123         r->pdata = pdata;
124         platform_set_drvdata(pdev, r);
125
126         /* allocate and register the input device */
127         input_dev = input_allocate_device();
128         if (!input_dev) {
129                 dev_err(&pdev->dev, "failed to allocate input device\n");
130                 err = -ENOMEM;
131                 goto failed_free_io;
132         }
133
134         input_dev->name = pdev->name;
135         input_dev->id.bustype = BUS_HOST;
136         input_dev->open = pxa930_rotary_open;
137         input_dev->close = pxa930_rotary_close;
138         input_dev->dev.parent = &pdev->dev;
139
140         if (pdata->up_key && pdata->down_key) {
141                 __set_bit(pdata->up_key, input_dev->keybit);
142                 __set_bit(pdata->down_key, input_dev->keybit);
143                 __set_bit(EV_KEY, input_dev->evbit);
144         } else {
145                 __set_bit(pdata->rel_code, input_dev->relbit);
146                 __set_bit(EV_REL, input_dev->evbit);
147         }
148
149         r->input_dev = input_dev;
150         input_set_drvdata(input_dev, r);
151
152         err = request_irq(irq, rotary_irq, 0,
153                         "enhanced rotary", r);
154         if (err) {
155                 dev_err(&pdev->dev, "failed to request IRQ\n");
156                 goto failed_free_input;
157         }
158
159         err = input_register_device(input_dev);
160         if (err) {
161                 dev_err(&pdev->dev, "failed to register input device\n");
162                 goto failed_free_irq;
163         }
164
165         return 0;
166
167 failed_free_irq:
168         free_irq(irq, r);
169 failed_free_input:
170         input_free_device(input_dev);
171 failed_free_io:
172         iounmap(r->mmio_base);
173 failed_free:
174         kfree(r);
175         return err;
176 }
177
178 static int pxa930_rotary_remove(struct platform_device *pdev)
179 {
180         struct pxa930_rotary *r = platform_get_drvdata(pdev);
181
182         free_irq(platform_get_irq(pdev, 0), r);
183         input_unregister_device(r->input_dev);
184         iounmap(r->mmio_base);
185         kfree(r);
186
187         return 0;
188 }
189
190 static struct platform_driver pxa930_rotary_driver = {
191         .driver         = {
192                 .name   = "pxa930-rotary",
193                 .owner  = THIS_MODULE,
194         },
195         .probe          = pxa930_rotary_probe,
196         .remove         = pxa930_rotary_remove,
197 };
198 module_platform_driver(pxa930_rotary_driver);
199
200 MODULE_LICENSE("GPL");
201 MODULE_DESCRIPTION("Driver for PXA93x Enhanced Rotary Controller");
202 MODULE_AUTHOR("Yao Yong <yaoyong@marvell.com>");