2 * Custom GPIO-based PWM driver
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 >
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.
11 * ---------------------------------------------------------------------------
13 * The behaviour of this driver can be altered by setting some parameters
14 * from the insmod command line.
16 * The following parameters are adjustable:
18 * bus0 These four arguments must be arrays
21 * bus10 <id>,<pin>,<od>
25 * <id> ID to used as device_id for the corresponding bus (required)
26 * <pin> GPIO pin ID for PWM channel (required)
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:
32 * pwm-gpio-custom.bus0
33 * pwm-gpio-custom.bus1
34 * pwm-gpio-custom.bunN
35 * pwm-gpio-custom.bus10
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/platform_device.h>
42 #include <linux/workqueue.h>
44 #include <linux/pwm/pwm.h>
46 #define DRV_NAME "pwm-gpio-custom"
47 #define DRV_DESC "Custom GPIO-based pwm driver"
48 #define DRV_VERSION "0.1.0"
50 #define PFX DRV_NAME ": "
52 #define BUS_PARAM_ID 0
53 #define BUS_PARAM_PIN 1
55 #define BUS_PARAM_REQUIRED 2
56 #define BUS_PARAM_COUNT 2
57 #define BUS_COUNT_MAX 10
59 static unsigned int bus0[BUS_PARAM_COUNT] __initdata;
60 static unsigned int bus1[BUS_PARAM_COUNT] __initdata;
61 static unsigned int bus2[BUS_PARAM_COUNT] __initdata;
62 static unsigned int bus3[BUS_PARAM_COUNT] __initdata;
63 static unsigned int bus4[BUS_PARAM_COUNT] __initdata;
64 static unsigned int bus5[BUS_PARAM_COUNT] __initdata;
65 static unsigned int bus6[BUS_PARAM_COUNT] __initdata;
66 static unsigned int bus7[BUS_PARAM_COUNT] __initdata;
67 static unsigned int bus8[BUS_PARAM_COUNT] __initdata;
68 static unsigned int bus9[BUS_PARAM_COUNT] __initdata;
70 static unsigned int bus_nump[BUS_COUNT_MAX] __initdata;
72 #define BUS_PARM_DESC " config -> id,pin"
74 module_param_array(bus0, uint, &bus_nump[0], 0);
75 MODULE_PARM_DESC(bus0, "bus0" BUS_PARM_DESC);
76 module_param_array(bus1, uint, &bus_nump[1], 0);
77 MODULE_PARM_DESC(bus1, "bus1" BUS_PARM_DESC);
78 module_param_array(bus2, uint, &bus_nump[2], 0);
79 MODULE_PARM_DESC(bus2, "bus2" BUS_PARM_DESC);
80 module_param_array(bus3, uint, &bus_nump[3], 0);
81 MODULE_PARM_DESC(bus3, "bus3" BUS_PARM_DESC);
82 module_param_array(bus4, uint, &bus_nump[4], 0);
83 MODULE_PARM_DESC(bus4, "bus4" BUS_PARM_DESC);
84 module_param_array(bus5, uint, &bus_nump[5], 0);
85 MODULE_PARM_DESC(bus5, "bus5" BUS_PARM_DESC);
86 module_param_array(bus6, uint, &bus_nump[6], 0);
87 MODULE_PARM_DESC(bus6, "bus6" BUS_PARM_DESC);
88 module_param_array(bus7, uint, &bus_nump[7], 0);
89 MODULE_PARM_DESC(bus7, "bus7" BUS_PARM_DESC);
90 module_param_array(bus8, uint, &bus_nump[8], 0);
91 MODULE_PARM_DESC(bus8, "bus8" BUS_PARM_DESC);
92 module_param_array(bus9, uint, &bus_nump[9], 0);
93 MODULE_PARM_DESC(bus9, "bus9" BUS_PARM_DESC);
95 static struct platform_device *devices[BUS_COUNT_MAX];
96 static unsigned int nr_devices;
98 static void pwm_gpio_custom_cleanup(void)
102 for (i = 0; i < nr_devices; i++)
104 platform_device_put(devices[i]);
107 static int __init pwm_gpio_custom_add_one(unsigned int id, unsigned int *params)
109 struct platform_device *pdev;
110 struct gpio_pwm_platform_data pdata;
116 if (bus_nump[id] < BUS_PARAM_REQUIRED) {
117 printk(KERN_ERR PFX "not enough parameters for bus%d\n", id);
122 pdev = platform_device_alloc("gpio_pwm", params[BUS_PARAM_ID]);
128 pdata.gpio = params[BUS_PARAM_PIN];
130 err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
134 err = platform_device_add(pdev);
138 devices[nr_devices++] = pdev;
142 platform_device_put(pdev);
147 static int __init pwm_gpio_custom_probe(void)
152 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
154 err = pwm_gpio_custom_add_one(0, bus0);
157 err = pwm_gpio_custom_add_one(1, bus1);
160 err = pwm_gpio_custom_add_one(2, bus2);
163 err = pwm_gpio_custom_add_one(3, bus3);
166 err = pwm_gpio_custom_add_one(4, bus4);
169 err = pwm_gpio_custom_add_one(5, bus5);
172 err = pwm_gpio_custom_add_one(6, bus6);
175 err = pwm_gpio_custom_add_one(7, bus7);
178 err = pwm_gpio_custom_add_one(8, bus8);
181 err = pwm_gpio_custom_add_one(9, bus9);
185 printk(KERN_ERR PFX "no bus parameter(s) specified\n");
193 pwm_gpio_custom_cleanup();
198 static int __init pwm_gpio_custom_init(void)
200 return pwm_gpio_custom_probe();
202 module_init(pwm_gpio_custom_init);
204 static void __exit pwm_gpio_custom_exit(void)
206 pwm_gpio_custom_cleanup();
208 module_exit(pwm_gpio_custom_exit);
210 subsys_initcall(pwm_gpio_custom_probe);
213 MODULE_LICENSE("GPL v2");
214 MODULE_AUTHOR("Bifferos <bifferos at yahoo.co.uk >");
215 MODULE_AUTHOR("Claudio Mignanti <c.mignanti@gmail.com >");
216 MODULE_DESCRIPTION(DRV_DESC);
217 MODULE_VERSION(DRV_VERSION);