samba36: enable parallel build
[openwrt.git] / package / w1-gpio-custom / src / w1-gpio-custom.c
1 /*
2  *  Custom GPIO-based W1 driver
3  *
4  *  Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
5  *  Copyright (C) 2008 Bifferos <bifferos at yahoo.co.uk>
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 of
19  *      bus1    3 unsigned integers as follows:
20  *      bus2
21  *      bus3    <id>,<pin>,<od>
22  *
23  *  where:
24  *
25  *  <id>        ID to used as device_id for the corresponding bus (required)
26  *  <sda>       GPIO pin ID of data pin (required)
27  *  <od>        Pin is configured as open drain.
28  *
29  *  See include/w1-gpio.h for more information about the parameters.
30  *
31  *  If this driver is built into the kernel, you can use the following kernel
32  *  command line parameters, with the same values as the corresponding module
33  *  parameters listed above:
34  *
35  *      w1-gpio-custom.bus0
36  *      w1-gpio-custom.bus1
37  *      w1-gpio-custom.bus2
38  *      w1-gpio-custom.bus3
39  */
40
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/platform_device.h>
45
46 #include <linux/w1-gpio.h>
47
48 #define DRV_NAME        "w1-gpio-custom"
49 #define DRV_DESC        "Custom GPIO-based W1 driver"
50 #define DRV_VERSION     "0.1.1"
51
52 #define PFX             DRV_NAME ": "
53
54 #define BUS_PARAM_ID            0
55 #define BUS_PARAM_PIN           1
56 #define BUS_PARAM_OD            2
57
58 #define BUS_PARAM_REQUIRED      3
59 #define BUS_PARAM_COUNT         3
60 #define BUS_COUNT_MAX           4
61
62 static unsigned int bus0[BUS_PARAM_COUNT] __initdata;
63 static unsigned int bus1[BUS_PARAM_COUNT] __initdata;
64 static unsigned int bus2[BUS_PARAM_COUNT] __initdata;
65 static unsigned int bus3[BUS_PARAM_COUNT] __initdata;
66
67 static unsigned int bus_nump[BUS_COUNT_MAX] __initdata;
68
69 #define BUS_PARM_DESC " config -> id,pin,od"
70
71 module_param_array(bus0, uint, &bus_nump[0], 0);
72 MODULE_PARM_DESC(bus0, "bus0" BUS_PARM_DESC);
73 module_param_array(bus1, uint, &bus_nump[1], 0);
74 MODULE_PARM_DESC(bus1, "bus1" BUS_PARM_DESC);
75 module_param_array(bus2, uint, &bus_nump[2], 0);
76 MODULE_PARM_DESC(bus2, "bus2" BUS_PARM_DESC);
77 module_param_array(bus3, uint, &bus_nump[3], 0);
78 MODULE_PARM_DESC(bus3, "bus3" BUS_PARM_DESC);
79
80 static struct platform_device *devices[BUS_COUNT_MAX];
81 static unsigned int nr_devices;
82
83 static void w1_gpio_custom_cleanup(void)
84 {
85         int i;
86
87         for (i = 0; i < nr_devices; i++)
88                 if (devices[i])
89                         platform_device_put(devices[i]);
90 }
91
92 static int __init w1_gpio_custom_add_one(unsigned int id, unsigned int *params)
93 {
94         struct platform_device *pdev;
95         struct w1_gpio_platform_data pdata;
96         int err;
97
98         if (!bus_nump[id])
99                 return 0;
100
101         if (bus_nump[id] < BUS_PARAM_REQUIRED) {
102                 printk(KERN_ERR PFX "not enough parameters for bus%d\n", id);
103                 err = -EINVAL;
104                 goto err;
105         }
106
107         pdev = platform_device_alloc("w1-gpio", params[BUS_PARAM_ID]);
108         if (!pdev) {
109                 err = -ENOMEM;
110                 goto err;
111         }
112
113         pdata.pin = params[BUS_PARAM_PIN];
114         pdata.is_open_drain = params[BUS_PARAM_OD] ? 1:0;
115         pdata.enable_external_pullup = NULL;
116
117         err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
118         if (err)
119                 goto err_put;
120
121         err = platform_device_add(pdev);
122         if (err)
123                 goto err_put;
124
125         devices[nr_devices++] = pdev;
126         return 0;
127
128  err_put:
129         platform_device_put(pdev);
130  err:
131         return err;
132 }
133
134 static int __init w1_gpio_custom_probe(void)
135 {
136         int err;
137
138         nr_devices = 0;
139         printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
140
141         err = w1_gpio_custom_add_one(0, bus0);
142         if (err) goto err;
143
144         err = w1_gpio_custom_add_one(1, bus1);
145         if (err) goto err;
146
147         err = w1_gpio_custom_add_one(2, bus2);
148         if (err) goto err;
149
150         err = w1_gpio_custom_add_one(3, bus3);
151         if (err) goto err;
152
153         if (!nr_devices) {
154                 printk(KERN_ERR PFX "no bus parameter(s) specified\n");
155                 err = -ENODEV;
156                 goto err;
157         }
158
159         return 0;
160
161 err:
162         w1_gpio_custom_cleanup();
163         return err;
164 }
165
166 #ifdef MODULE
167 static int __init w1_gpio_custom_init(void)
168 {
169         return w1_gpio_custom_probe();
170 }
171 module_init(w1_gpio_custom_init);
172
173 static void __exit w1_gpio_custom_exit(void)
174 {
175         w1_gpio_custom_cleanup();
176 }
177 module_exit(w1_gpio_custom_exit);
178 #else
179 subsys_initcall(w1_gpio_custom_probe);
180 #endif /* MODULE*/
181
182 MODULE_LICENSE("GPL v2");
183 MODULE_AUTHOR("Bifferos <bifferos at yahoo.co.uk >");
184 MODULE_DESCRIPTION(DRV_DESC);
185 MODULE_VERSION(DRV_VERSION);