[kernel] move lots of kernel related packages to the new system/ folder
[openwrt/svn-archive/archive.git] / package / system / rotary-gpio-custom / src / rotary-gpio-custom.c
1 /*
2 * Custom GPIO-based rotary driver
3 *
4 * Copyright (C) 2010 Claudio Mignanti <c.mignanti@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Strongly based on Custom GPIO-based I2C driver by:
11 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
12 *
13 * ---------------------------------------------------------------------------
14 *
15 * The behaviour of this driver can be altered by setting some parameters
16 * from the insmod command line.
17 *
18 * The following parameters are adjustable:
19 *
20 * bus0 These four arguments can be arrays of
21 * bus1 1-8 unsigned integers as follows:
22 * bus2
23 * bus3 <id>,<steps>,<axis>,<gpioa>,<gpiob>,<inverted>
24 *
25 *
26 * If this driver is built into the kernel, you can use the following kernel
27 * command line parameters, with the same values as the corresponding module
28 * parameters listed above:
29 *
30 * rotary-gpio-custom.bus0
31 * rotary-gpio-custom.bus1
32 * rotary-gpio-custom.bus2
33 * rotary-gpio-custom.bus3
34 */
35
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/input.h>
40 #include <linux/platform_device.h>
41 #include <linux/rotary_encoder.h>
42
43 #define DRV_NAME "rotary-gpio-custom"
44 #define DRV_DESC "Custom GPIO-based rotary driver"
45 #define DRV_VERSION "0.1.0"
46
47 #define PFX DRV_NAME ": "
48
49 #define BUS_PARAM_REQUIRED 5
50 #define BUS_PARAM_COUNT 6
51 #define BUS_COUNT_MAX 4
52
53 static unsigned int bus0[BUS_PARAM_COUNT] __initdata;
54 static unsigned int bus1[BUS_PARAM_COUNT] __initdata;
55 static unsigned int bus2[BUS_PARAM_COUNT] __initdata;
56 static unsigned int bus3[BUS_PARAM_COUNT] __initdata;
57
58 static unsigned int bus_nump[BUS_COUNT_MAX] __initdata;
59
60 #define BUS_PARM_DESC \
61 " config -> id,steps,axis,gpioa,gpiob[,inverted]"
62
63 module_param_array(bus0, uint, &bus_nump[0], 0);
64 MODULE_PARM_DESC(bus0, "bus0" BUS_PARM_DESC);
65 module_param_array(bus1, uint, &bus_nump[1], 0);
66 MODULE_PARM_DESC(bus1, "bus1" BUS_PARM_DESC);
67 module_param_array(bus2, uint, &bus_nump[2], 0);
68 MODULE_PARM_DESC(bus2, "bus2" BUS_PARM_DESC);
69 module_param_array(bus3, uint, &bus_nump[3], 0);
70 MODULE_PARM_DESC(bus3, "bus3" BUS_PARM_DESC);
71
72 static struct platform_device *devices[BUS_COUNT_MAX];
73 static unsigned int nr_devices;
74
75 static void rotary_gpio_custom_cleanup(void)
76 {
77 int i;
78
79 for (i = 0; i < nr_devices; i++)
80 if (devices[i])
81 platform_device_put(devices[i]);
82 }
83
84 static int __init rotary_gpio_custom_add_one(unsigned int id, unsigned int *params)
85 {
86 struct platform_device *pdev;
87 struct rotary_encoder_platform_data pdata;
88 int err;
89
90 if (!bus_nump[id])
91 return 0;
92
93 if (bus_nump[id] < BUS_PARAM_REQUIRED) {
94 printk(KERN_ERR PFX "not enough parameters for bus%d\n", id);
95 err = -EINVAL;
96 goto err;
97 }
98
99 pdev = platform_device_alloc("rotary-gpio", params[0]);
100 if (!pdev) {
101 err = -ENOMEM;
102 goto err;
103 }
104
105 pdata.steps = params[1];
106 pdata.axis = params[2];
107 pdata.relative_axis = false;
108 pdata.rollover = false;
109 pdata.gpio_a = params[3];
110 pdata.gpio_b = params[4];
111
112 if (params[5] == 1) {
113 pdata.inverted_a = 1;
114 pdata.inverted_b = 1;
115 } else {
116 pdata.inverted_a = 0;
117 pdata.inverted_b = 0;
118 }
119
120 err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
121 if (err)
122 goto err_put;
123
124 err = platform_device_add(pdev);
125 if (err)
126 goto err_put;
127
128 devices[nr_devices++] = pdev;
129 return 0;
130
131 err_put:
132 platform_device_put(pdev);
133 err:
134 return err;
135 }
136
137 static int __init rotary_gpio_custom_probe(void)
138 {
139 int err;
140
141 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
142
143 err = rotary_gpio_custom_add_one(0, bus0);
144 if (err) goto err;
145
146 err = rotary_gpio_custom_add_one(1, bus1);
147 if (err) goto err;
148
149 err = rotary_gpio_custom_add_one(2, bus2);
150 if (err) goto err;
151
152 err = rotary_gpio_custom_add_one(3, bus3);
153 if (err) goto err;
154
155 if (!nr_devices) {
156 printk(KERN_ERR PFX "no bus parameter(s) specified\n");
157 err = -ENODEV;
158 goto err;
159 }
160
161 return 0;
162
163 err:
164 rotary_gpio_custom_cleanup();
165 return err;
166 }
167
168 #ifdef MODULE
169 static int __init rotary_gpio_custom_init(void)
170 {
171 return rotary_gpio_custom_probe();
172 }
173 module_init(rotary_gpio_custom_init);
174
175 static void __exit rotary_gpio_custom_exit(void)
176 {
177 rotary_gpio_custom_cleanup();
178 }
179 module_exit(rotary_gpio_custom_exit);
180 #else
181 subsys_initcall(rotary_gpio_custom_probe);
182 #endif /* MODULE*/
183
184 MODULE_LICENSE("GPL v2");
185 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org >");
186 MODULE_AUTHOR("Claudio Mignanti <c.mignanti@gmail.com>");
187 MODULE_DESCRIPTION(DRV_DESC);
188 MODULE_VERSION(DRV_VERSION);