i2c-gpio-custom: Adapt to moved include file
[openwrt/openwrt.git] / package / kernel / i2c-gpio-custom / src / i2c-gpio-custom.c
1 /*
2 * Custom GPIO-based I2C driver
3 *
4 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ---------------------------------------------------------------------------
11 *
12 * The behaviour of this driver can be altered by setting some parameters
13 * from the insmod command line.
14 *
15 * The following parameters are adjustable:
16 *
17 * bus0 These four arguments can be arrays of
18 * bus1 1-8 unsigned integers as follows:
19 * bus2
20 * bus3 <id>,<sda>,<scl>,<udelay>,<timeout>,<sda_od>,<scl_od>,<scl_oo>
21 *
22 * where:
23 *
24 * <id> ID to used as device_id for the corresponding bus (required)
25 * <sda> GPIO pin ID to used for SDA (required)
26 * <scl> GPIO pin ID to used for SCL (required)
27 * <udelay> signal toggle delay.
28 * <timeout> clock stretching timeout.
29 * <sda_od> SDA is configured as open drain.
30 * <scl_od> SCL is configured as open drain.
31 * <scl_oo> SCL output drivers cannot be turned off.
32 *
33 * See include/i2c-gpio.h for more information about the parameters.
34 *
35 * If this driver is built into the kernel, you can use the following kernel
36 * command line parameters, with the same values as the corresponding module
37 * parameters listed above:
38 *
39 * i2c-gpio-custom.bus0
40 * i2c-gpio-custom.bus1
41 * i2c-gpio-custom.bus2
42 * i2c-gpio-custom.bus3
43 */
44
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/platform_device.h>
49
50 #include <linux/version.h>
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
52 #include <linux/i2c-gpio.h>
53 #else
54 #include <linux/platform_data/i2c-gpio.h>
55 #endif
56
57 #define DRV_NAME "i2c-gpio-custom"
58 #define DRV_DESC "Custom GPIO-based I2C driver"
59 #define DRV_VERSION "0.1.1"
60
61 #define PFX DRV_NAME ": "
62
63 #define BUS_PARAM_ID 0
64 #define BUS_PARAM_SDA 1
65 #define BUS_PARAM_SCL 2
66 #define BUS_PARAM_UDELAY 3
67 #define BUS_PARAM_TIMEOUT 4
68 #define BUS_PARAM_SDA_OD 5
69 #define BUS_PARAM_SCL_OD 6
70 #define BUS_PARAM_SCL_OO 7
71
72 #define BUS_PARAM_REQUIRED 3
73 #define BUS_PARAM_COUNT 8
74 #define BUS_COUNT_MAX 4
75
76 static unsigned int bus0[BUS_PARAM_COUNT] __initdata;
77 static unsigned int bus1[BUS_PARAM_COUNT] __initdata;
78 static unsigned int bus2[BUS_PARAM_COUNT] __initdata;
79 static unsigned int bus3[BUS_PARAM_COUNT] __initdata;
80
81 static unsigned int bus_nump[BUS_COUNT_MAX] __initdata;
82
83 #define BUS_PARM_DESC \
84 " config -> id,sda,scl[,udelay,timeout,sda_od,scl_od,scl_oo]"
85
86 module_param_array(bus0, uint, &bus_nump[0], 0);
87 MODULE_PARM_DESC(bus0, "bus0" BUS_PARM_DESC);
88 module_param_array(bus1, uint, &bus_nump[1], 0);
89 MODULE_PARM_DESC(bus1, "bus1" BUS_PARM_DESC);
90 module_param_array(bus2, uint, &bus_nump[2], 0);
91 MODULE_PARM_DESC(bus2, "bus2" BUS_PARM_DESC);
92 module_param_array(bus3, uint, &bus_nump[3], 0);
93 MODULE_PARM_DESC(bus3, "bus3" BUS_PARM_DESC);
94
95 static struct platform_device *devices[BUS_COUNT_MAX];
96 static unsigned int nr_devices;
97
98 static void i2c_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 i2c_gpio_custom_add_one(unsigned int id, unsigned int *params)
108 {
109 struct platform_device *pdev;
110 struct i2c_gpio_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("i2c-gpio", params[BUS_PARAM_ID]);
123 if (!pdev) {
124 err = -ENOMEM;
125 goto err;
126 }
127
128 pdata.sda_pin = params[BUS_PARAM_SDA];
129 pdata.scl_pin = params[BUS_PARAM_SCL];
130 pdata.udelay = params[BUS_PARAM_UDELAY];
131 pdata.timeout = params[BUS_PARAM_TIMEOUT];
132 pdata.sda_is_open_drain = params[BUS_PARAM_SDA_OD] != 0;
133 pdata.scl_is_open_drain = params[BUS_PARAM_SCL_OD] != 0;
134 pdata.scl_is_output_only = params[BUS_PARAM_SCL_OO] != 0;
135
136 err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
137 if (err)
138 goto err_put;
139
140 err = platform_device_add(pdev);
141 if (err)
142 goto err_put;
143
144 devices[nr_devices++] = pdev;
145 return 0;
146
147 err_put:
148 platform_device_put(pdev);
149 err:
150 return err;
151 }
152
153 static int __init i2c_gpio_custom_probe(void)
154 {
155 int err;
156
157 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
158
159 err = i2c_gpio_custom_add_one(0, bus0);
160 if (err)
161 goto err;
162
163 err = i2c_gpio_custom_add_one(1, bus1);
164 if (err)
165 goto err;
166
167 err = i2c_gpio_custom_add_one(2, bus2);
168 if (err)
169 goto err;
170
171 err = i2c_gpio_custom_add_one(3, bus3);
172 if (err)
173 goto err;
174
175 if (!nr_devices) {
176 printk(KERN_ERR PFX "no bus parameter(s) specified\n");
177 err = -ENODEV;
178 goto err;
179 }
180
181 return 0;
182
183 err:
184 i2c_gpio_custom_cleanup();
185 return err;
186 }
187
188 #ifdef MODULE
189 static int __init i2c_gpio_custom_init(void)
190 {
191 return i2c_gpio_custom_probe();
192 }
193 module_init(i2c_gpio_custom_init);
194
195 static void __exit i2c_gpio_custom_exit(void)
196 {
197 i2c_gpio_custom_cleanup();
198 }
199 module_exit(i2c_gpio_custom_exit);
200 #else
201 subsys_initcall(i2c_gpio_custom_probe);
202 #endif /* MODULE*/
203
204 MODULE_LICENSE("GPL v2");
205 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org >");
206 MODULE_DESCRIPTION(DRV_DESC);
207 MODULE_VERSION(DRV_VERSION);