| 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 | #include <linux/workqueue.h> |
| 43 | |
| 44 | #include <linux/pwm/pwm.h> |
| 45 | |
| 46 | #define DRV_NAME "pwm-gpio-custom" |
| 47 | #define DRV_DESC "Custom GPIO-based pwm driver" |
| 48 | #define DRV_VERSION "0.1.0" |
| 49 | |
| 50 | #define PFX DRV_NAME ": " |
| 51 | |
| 52 | #define BUS_PARAM_ID 0 |
| 53 | #define BUS_PARAM_PIN 1 |
| 54 | |
| 55 | #define BUS_PARAM_REQUIRED 2 |
| 56 | #define BUS_PARAM_COUNT 2 |
| 57 | #define BUS_COUNT_MAX 10 |
| 58 | |
| 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; |
| 69 | |
| 70 | static unsigned int bus_nump[BUS_COUNT_MAX] __initdata; |
| 71 | |
| 72 | #define BUS_PARM_DESC " config -> id,pin" |
| 73 | |
| 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); |
| 94 | |
| 95 | static struct platform_device *devices[BUS_COUNT_MAX]; |
| 96 | static unsigned int nr_devices; |
| 97 | |
| 98 | static void pwm_gpio_custom_cleanup(void) |
| 99 | { |
| 100 | int i; |
| 101 | |
| 102 | for (i = 0; i < nr_devices; i++) |
| 103 | if (devices[i]) |
| 104 | platform_device_put(devices[i]); |
| 105 | } |
| 106 | |
| 107 | static int __init pwm_gpio_custom_add_one(unsigned int id, unsigned int *params) |
| 108 | { |
| 109 | struct platform_device *pdev; |
| 110 | struct gpio_pwm_platform_data pdata; |
| 111 | int err; |
| 112 | |
| 113 | if (!bus_nump[id]) |
| 114 | return 0; |
| 115 | |
| 116 | if (bus_nump[id] < BUS_PARAM_REQUIRED) { |
| 117 | printk(KERN_ERR PFX "not enough parameters for bus%d\n", id); |
| 118 | err = -EINVAL; |
| 119 | goto err; |
| 120 | } |
| 121 | |
| 122 | pdev = platform_device_alloc("gpio_pwm", params[BUS_PARAM_ID]); |
| 123 | if (!pdev) { |
| 124 | err = -ENOMEM; |
| 125 | goto err; |
| 126 | } |
| 127 | |
| 128 | pdata.gpio = params[BUS_PARAM_PIN]; |
| 129 | |
| 130 | err = platform_device_add_data(pdev, &pdata, sizeof(pdata)); |
| 131 | if (err) |
| 132 | goto err_put; |
| 133 | |
| 134 | err = platform_device_add(pdev); |
| 135 | if (err) |
| 136 | goto err_put; |
| 137 | |
| 138 | devices[nr_devices++] = pdev; |
| 139 | return 0; |
| 140 | |
| 141 | err_put: |
| 142 | platform_device_put(pdev); |
| 143 | err: |
| 144 | return err; |
| 145 | } |
| 146 | |
| 147 | static int __init pwm_gpio_custom_probe(void) |
| 148 | { |
| 149 | int err; |
| 150 | |
| 151 | nr_devices = 0; |
| 152 | printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n"); |
| 153 | |
| 154 | err = pwm_gpio_custom_add_one(0, bus0); |
| 155 | if (err) goto err; |
| 156 | |
| 157 | err = pwm_gpio_custom_add_one(1, bus1); |
| 158 | if (err) goto err; |
| 159 | |
| 160 | err = pwm_gpio_custom_add_one(2, bus2); |
| 161 | if (err) goto err; |
| 162 | |
| 163 | err = pwm_gpio_custom_add_one(3, bus3); |
| 164 | if (err) goto err; |
| 165 | |
| 166 | err = pwm_gpio_custom_add_one(4, bus4); |
| 167 | if (err) goto err; |
| 168 | |
| 169 | err = pwm_gpio_custom_add_one(5, bus5); |
| 170 | if (err) goto err; |
| 171 | |
| 172 | err = pwm_gpio_custom_add_one(6, bus6); |
| 173 | if (err) goto err; |
| 174 | |
| 175 | err = pwm_gpio_custom_add_one(7, bus7); |
| 176 | if (err) goto err; |
| 177 | |
| 178 | err = pwm_gpio_custom_add_one(8, bus8); |
| 179 | if (err) goto err; |
| 180 | |
| 181 | err = pwm_gpio_custom_add_one(9, bus9); |
| 182 | if (err) goto err; |
| 183 | |
| 184 | if (!nr_devices) { |
| 185 | printk(KERN_ERR PFX "no bus parameter(s) specified\n"); |
| 186 | err = -ENODEV; |
| 187 | goto err; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | |
| 192 | err: |
| 193 | pwm_gpio_custom_cleanup(); |
| 194 | return err; |
| 195 | } |
| 196 | |
| 197 | #ifdef MODULE |
| 198 | static int __init pwm_gpio_custom_init(void) |
| 199 | { |
| 200 | return pwm_gpio_custom_probe(); |
| 201 | } |
| 202 | module_init(pwm_gpio_custom_init); |
| 203 | |
| 204 | static void __exit pwm_gpio_custom_exit(void) |
| 205 | { |
| 206 | pwm_gpio_custom_cleanup(); |
| 207 | } |
| 208 | module_exit(pwm_gpio_custom_exit); |
| 209 | #else |
| 210 | subsys_initcall(pwm_gpio_custom_probe); |
| 211 | #endif /* MODULE*/ |
| 212 | |
| 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); |
| 218 | |
| 219 | |