[package] import w1-gpio-custom driver from the Squidge (http://squidge.sourceforge...
[openwrt/svn-archive/archive.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.0"
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_unregister(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 devices[nr_devices++] = pdev;
114
115 pdata.pin = params[BUS_PARAM_PIN];
116 pdata.is_open_drain = params[BUS_PARAM_OD] ? 1:0;
117
118 err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
119 if (err)
120 goto err;
121
122 err = platform_device_register(pdev);
123 if (err)
124 goto err;
125
126 return 0;
127
128 err:
129 return err;
130 }
131
132 static int __init w1_gpio_custom_probe(void)
133 {
134 int err;
135
136 nr_devices = 0;
137 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
138
139 err = w1_gpio_custom_add_one(0, bus0);
140 if (err) goto err;
141
142 err = w1_gpio_custom_add_one(1, bus1);
143 if (err) goto err;
144
145 err = w1_gpio_custom_add_one(2, bus2);
146 if (err) goto err;
147
148 err = w1_gpio_custom_add_one(3, bus3);
149 if (err) goto err;
150
151 if (!nr_devices) {
152 printk(KERN_ERR PFX "no bus parameter(s) specified\n");
153 err = -ENODEV;
154 goto err;
155 }
156
157 return 0;
158
159 err:
160 w1_gpio_custom_cleanup();
161 return err;
162 }
163
164 #ifdef MODULE
165 static int __init w1_gpio_custom_init(void)
166 {
167 return w1_gpio_custom_probe();
168 }
169 module_init(w1_gpio_custom_init);
170
171 static void __exit w1_gpio_custom_exit(void)
172 {
173 w1_gpio_custom_cleanup();
174 }
175 module_exit(w1_gpio_custom_exit);
176 #else
177 subsys_initcall(w1_gpio_custom_probe);
178 #endif /* MODULE*/
179
180 MODULE_LICENSE("GPL v2");
181 MODULE_AUTHOR("Bifferos <bifferos at yahoo.co.uk >");
182 MODULE_DESCRIPTION(DRV_DESC);
183 MODULE_VERSION(DRV_VERSION);