409bc9582d31e75ea38309f3c6e0875c8a93627c
[openwrt.git] / package / pwm-gpio-custom / src / pwm-gpio-custom.c
1 /*
2  *  Custom GPIO-based PWM driver
3  *
4  *  Based on w1-gpio-custom by Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
5  *  Copyright (C) 2010 Claudio Mignanti <c.mignanti@gmail.com >
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  * ---------------------------------------------------------------------------
12  *
13  *  The behaviour of this driver can be altered by setting some parameters
14  *  from the insmod command line.
15  *
16  *  The following parameters are adjustable:
17  *
18  *      bus0    These four arguments must be arrays
19  *      bus1
20  *      .....
21  *      bus10   <id>,<pin>,<od>
22  *
23  *  where:
24  *
25  *  <id>        ID to used as device_id for the corresponding bus (required)
26  *  <pin>       GPIO pin ID for PWM channel (required)
27  * *
28  *  If this driver is built into the kernel, you can use the following kernel
29  *  command line parameters, with the same values as the corresponding module
30  *  parameters listed above:
31  *
32  *      pwm-gpio-custom.bus0
33  *      pwm-gpio-custom.bus1
34  *      pwm-gpio-custom.bunN
35  *      pwm-gpio-custom.bus10
36  */
37
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/platform_device.h>
42
43 #include <linux/pwm/pwm.h>
44
45 #define DRV_NAME        "pwm-gpio-custom"
46 #define DRV_DESC        "Custom GPIO-based pwm driver"
47 #define DRV_VERSION     "0.1.0"
48
49 #define PFX             DRV_NAME ": "
50
51 #define BUS_PARAM_ID            0
52 #define BUS_PARAM_PIN           1
53
54 #define BUS_PARAM_REQUIRED      2
55 #define BUS_PARAM_COUNT         2
56 #define BUS_COUNT_MAX           10
57
58 static unsigned int bus0[BUS_PARAM_COUNT] __initdata;
59 static unsigned int bus1[BUS_PARAM_COUNT] __initdata;
60 static unsigned int bus2[BUS_PARAM_COUNT] __initdata;
61 static unsigned int bus3[BUS_PARAM_COUNT] __initdata;
62 static unsigned int bus4[BUS_PARAM_COUNT] __initdata;
63 static unsigned int bus5[BUS_PARAM_COUNT] __initdata;
64 static unsigned int bus6[BUS_PARAM_COUNT] __initdata;
65 static unsigned int bus7[BUS_PARAM_COUNT] __initdata;
66 static unsigned int bus8[BUS_PARAM_COUNT] __initdata;
67 static unsigned int bus9[BUS_PARAM_COUNT] __initdata;
68
69 static unsigned int bus_nump[BUS_COUNT_MAX] __initdata;
70
71 #define BUS_PARM_DESC " config -> id,pin"
72
73 module_param_array(bus0, uint, &bus_nump[0], 0);
74 MODULE_PARM_DESC(bus0, "bus0" BUS_PARM_DESC);
75 module_param_array(bus1, uint, &bus_nump[1], 0);
76 MODULE_PARM_DESC(bus1, "bus1" BUS_PARM_DESC);
77 module_param_array(bus2, uint, &bus_nump[2], 0);
78 MODULE_PARM_DESC(bus2, "bus2" BUS_PARM_DESC);
79 module_param_array(bus3, uint, &bus_nump[3], 0);
80 MODULE_PARM_DESC(bus3, "bus3" BUS_PARM_DESC);
81 module_param_array(bus4, uint, &bus_nump[4], 0);
82 MODULE_PARM_DESC(bus4, "bus4" BUS_PARM_DESC);
83 module_param_array(bus5, uint, &bus_nump[5], 0);
84 MODULE_PARM_DESC(bus5, "bus5" BUS_PARM_DESC);
85 module_param_array(bus6, uint, &bus_nump[6], 0);
86 MODULE_PARM_DESC(bus6, "bus6" BUS_PARM_DESC);
87 module_param_array(bus7, uint, &bus_nump[7], 0);
88 MODULE_PARM_DESC(bus7, "bus7" BUS_PARM_DESC);
89 module_param_array(bus8, uint, &bus_nump[8], 0);
90 MODULE_PARM_DESC(bus8, "bus8" BUS_PARM_DESC);
91 module_param_array(bus9, uint, &bus_nump[9], 0);
92 MODULE_PARM_DESC(bus9, "bus9" BUS_PARM_DESC);
93
94 static struct platform_device *devices[BUS_COUNT_MAX];
95 static unsigned int nr_devices;
96
97 static void pwm_gpio_custom_cleanup(void)
98 {
99         int i;
100
101         for (i = 0; i < nr_devices; i++)
102                 if (devices[i])
103                         platform_device_put(devices[i]);
104 }
105
106 static int __init pwm_gpio_custom_add_one(unsigned int id, unsigned int *params)
107 {
108         struct platform_device *pdev;
109         struct gpio_pwm_platform_data pdata;
110         int err;
111
112         if (!bus_nump[id])
113                 return 0;
114
115         if (bus_nump[id] < BUS_PARAM_REQUIRED) {
116                 printk(KERN_ERR PFX "not enough parameters for bus%d\n", id);
117                 err = -EINVAL;
118                 goto err;
119         }
120
121         pdev = platform_device_alloc("gpio_pwm", params[BUS_PARAM_ID]);
122         if (!pdev) {
123                 err = -ENOMEM;
124                 goto err;
125         }
126
127         pdata.gpio = params[BUS_PARAM_PIN];
128
129         err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
130         if (err)
131                 goto err_put;
132
133         err = platform_device_add(pdev);
134         if (err)
135                 goto err_put;
136
137         devices[nr_devices++] = pdev;
138         return 0;
139
140  err_put:
141         platform_device_put(pdev);
142  err:
143         return err;
144 }
145
146 static int __init pwm_gpio_custom_probe(void)
147 {
148         int err;
149
150         nr_devices = 0;
151         printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
152
153         err = pwm_gpio_custom_add_one(0, bus0);
154         if (err) goto err;
155
156         err = pwm_gpio_custom_add_one(1, bus1);
157         if (err) goto err;
158
159         err = pwm_gpio_custom_add_one(2, bus2);
160         if (err) goto err;
161
162         err = pwm_gpio_custom_add_one(3, bus3);
163         if (err) goto err;
164
165         err = pwm_gpio_custom_add_one(4, bus4);
166         if (err) goto err;
167
168         err = pwm_gpio_custom_add_one(5, bus5);
169         if (err) goto err;
170
171         err = pwm_gpio_custom_add_one(6, bus6);
172         if (err) goto err;
173
174         err = pwm_gpio_custom_add_one(7, bus7);
175         if (err) goto err;
176
177         err = pwm_gpio_custom_add_one(8, bus8);
178         if (err) goto err;
179
180         err = pwm_gpio_custom_add_one(9, bus9);
181         if (err) goto err;
182
183         if (!nr_devices) {
184                 printk(KERN_ERR PFX "no bus parameter(s) specified\n");
185                 err = -ENODEV;
186                 goto err;
187         }
188
189         return 0;
190
191 err:
192         pwm_gpio_custom_cleanup();
193         return err;
194 }
195
196 #ifdef MODULE
197 static int __init pwm_gpio_custom_init(void)
198 {
199         return pwm_gpio_custom_probe();
200 }
201 module_init(pwm_gpio_custom_init);
202
203 static void __exit pwm_gpio_custom_exit(void)
204 {
205         pwm_gpio_custom_cleanup();
206 }
207 module_exit(pwm_gpio_custom_exit);
208 #else
209 subsys_initcall(pwm_gpio_custom_probe);
210 #endif /* MODULE*/
211
212 MODULE_LICENSE("GPL v2");
213 MODULE_AUTHOR("Bifferos <bifferos at yahoo.co.uk >");
214 MODULE_AUTHOR("Claudio Mignanti <c.mignanti@gmail.com >");
215 MODULE_DESCRIPTION(DRV_DESC);
216 MODULE_VERSION(DRV_VERSION);
217