2 * Custom GPIO-based rotary driver
4 * Copyright (C) 2010 Claudio Mignanti <c.mignanti@gmail.com>
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.
10 * Strongly based on Custom GPIO-based I2C driver by:
11 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
13 * ---------------------------------------------------------------------------
15 * The behaviour of this driver can be altered by setting some parameters
16 * from the insmod command line.
18 * The following parameters are adjustable:
20 * bus0 These four arguments can be arrays of
21 * bus1 1-8 unsigned integers as follows:
23 * bus3 <id>,<steps>,<axis>,<gpioa>,<gpiob>,<inverted>
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:
30 * rotary-gpio-custom.bus0
31 * rotary-gpio-custom.bus1
32 * rotary-gpio-custom.bus2
33 * rotary-gpio-custom.bus3
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>
43 #define DRV_NAME "rotary-gpio-custom"
44 #define DRV_DESC "Custom GPIO-based rotary driver"
45 #define DRV_VERSION "0.1.0"
47 #define PFX DRV_NAME ": "
49 #define BUS_PARAM_REQUIRED 5
50 #define BUS_PARAM_COUNT 6
51 #define BUS_COUNT_MAX 4
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;
58 static unsigned int bus_nump[BUS_COUNT_MAX] __initdata;
60 #define BUS_PARM_DESC \
61 " config -> id,steps,axis,gpioa,gpiob[,inverted]"
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);
72 static struct platform_device *devices[BUS_COUNT_MAX];
73 static unsigned int nr_devices;
75 static void rotary_gpio_custom_cleanup(void)
79 for (i = 0; i < nr_devices; i++)
81 platform_device_put(devices[i]);
84 static int __init rotary_gpio_custom_add_one(unsigned int id, unsigned int *params)
86 struct platform_device *pdev;
87 struct rotary_encoder_platform_data pdata;
93 if (bus_nump[id] < BUS_PARAM_REQUIRED) {
94 printk(KERN_ERR PFX "not enough parameters for bus%d\n", id);
99 pdev = platform_device_alloc("rotary-gpio", params[0]);
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];
112 if (params[5] == 1) {
113 pdata.inverted_a = 1;
114 pdata.inverted_b = 1;
116 pdata.inverted_a = 0;
117 pdata.inverted_b = 0;
120 err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
124 err = platform_device_add(pdev);
128 devices[nr_devices++] = pdev;
132 platform_device_put(pdev);
137 static int __init rotary_gpio_custom_probe(void)
141 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
143 err = rotary_gpio_custom_add_one(0, bus0);
146 err = rotary_gpio_custom_add_one(1, bus1);
149 err = rotary_gpio_custom_add_one(2, bus2);
152 err = rotary_gpio_custom_add_one(3, bus3);
156 printk(KERN_ERR PFX "no bus parameter(s) specified\n");
164 rotary_gpio_custom_cleanup();
169 static int __init rotary_gpio_custom_init(void)
171 return rotary_gpio_custom_probe();
173 module_init(rotary_gpio_custom_init);
175 static void __exit rotary_gpio_custom_exit(void)
177 rotary_gpio_custom_cleanup();
179 module_exit(rotary_gpio_custom_exit);
181 subsys_initcall(rotary_gpio_custom_probe);
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);