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,
87 struct platform_device *pdev;
88 struct rotary_encoder_platform_data pdata;
94 if (bus_nump[id] < BUS_PARAM_REQUIRED) {
95 printk(KERN_ERR PFX "not enough parameters for bus%d\n", id);
100 pdev = platform_device_alloc("rotary-gpio", params[0]);
106 pdata.steps = params[1];
107 pdata.axis = params[2];
108 pdata.relative_axis = false;
109 pdata.rollover = false;
110 pdata.gpio_a = params[3];
111 pdata.gpio_b = params[4];
113 if (params[5] == 1) {
114 pdata.inverted_a = 1;
115 pdata.inverted_b = 1;
117 pdata.inverted_a = 0;
118 pdata.inverted_b = 0;
121 err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
125 err = platform_device_add(pdev);
129 devices[nr_devices++] = pdev;
133 platform_device_put(pdev);
138 static int __init rotary_gpio_custom_probe(void)
142 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
144 err = rotary_gpio_custom_add_one(0, bus0);
148 err = rotary_gpio_custom_add_one(1, bus1);
152 err = rotary_gpio_custom_add_one(2, bus2);
156 err = rotary_gpio_custom_add_one(3, bus3);
161 printk(KERN_ERR PFX "no bus parameter(s) specified\n");
169 rotary_gpio_custom_cleanup();
174 static int __init rotary_gpio_custom_init(void)
176 return rotary_gpio_custom_probe();
178 module_init(rotary_gpio_custom_init);
180 static void __exit rotary_gpio_custom_exit(void)
182 rotary_gpio_custom_cleanup();
184 module_exit(rotary_gpio_custom_exit);
186 subsys_initcall(rotary_gpio_custom_probe);
189 MODULE_LICENSE("GPL v2");
190 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org >");
191 MODULE_AUTHOR("Claudio Mignanti <c.mignanti@gmail.com>");
192 MODULE_DESCRIPTION(DRV_DESC);
193 MODULE_VERSION(DRV_VERSION);