diff options
author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
---|---|---|
committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/i2c/muxes | |
parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) |
Diffstat (limited to 'drivers/i2c/muxes')
-rw-r--r-- | drivers/i2c/muxes/gpio-i2cmux.c | 184 | ||||
-rw-r--r-- | drivers/i2c/muxes/pca9541.c | 411 | ||||
-rw-r--r-- | drivers/i2c/muxes/pca954x.c | 404 |
3 files changed, 999 insertions, 0 deletions
diff --git a/drivers/i2c/muxes/gpio-i2cmux.c b/drivers/i2c/muxes/gpio-i2cmux.c new file mode 100644 index 00000000000..7b6ce624cd6 --- /dev/null +++ b/drivers/i2c/muxes/gpio-i2cmux.c | |||
@@ -0,0 +1,184 @@ | |||
1 | /* | ||
2 | * I2C multiplexer using GPIO API | ||
3 | * | ||
4 | * Peter Korsgaard <peter.korsgaard@barco.com> | ||
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 | #include <linux/i2c.h> | ||
12 | #include <linux/i2c-mux.h> | ||
13 | #include <linux/gpio-i2cmux.h> | ||
14 | #include <linux/platform_device.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/module.h> | ||
17 | #include <linux/slab.h> | ||
18 | #include <linux/gpio.h> | ||
19 | |||
20 | struct gpiomux { | ||
21 | struct i2c_adapter *parent; | ||
22 | struct i2c_adapter **adap; /* child busses */ | ||
23 | struct gpio_i2cmux_platform_data data; | ||
24 | }; | ||
25 | |||
26 | static void gpiomux_set(const struct gpiomux *mux, unsigned val) | ||
27 | { | ||
28 | int i; | ||
29 | |||
30 | for (i = 0; i < mux->data.n_gpios; i++) | ||
31 | gpio_set_value(mux->data.gpios[i], val & (1 << i)); | ||
32 | } | ||
33 | |||
34 | static int gpiomux_select(struct i2c_adapter *adap, void *data, u32 chan) | ||
35 | { | ||
36 | struct gpiomux *mux = data; | ||
37 | |||
38 | gpiomux_set(mux, mux->data.values[chan]); | ||
39 | |||
40 | return 0; | ||
41 | } | ||
42 | |||
43 | static int gpiomux_deselect(struct i2c_adapter *adap, void *data, u32 chan) | ||
44 | { | ||
45 | struct gpiomux *mux = data; | ||
46 | |||
47 | gpiomux_set(mux, mux->data.idle); | ||
48 | |||
49 | return 0; | ||
50 | } | ||
51 | |||
52 | static int __devinit gpiomux_probe(struct platform_device *pdev) | ||
53 | { | ||
54 | struct gpiomux *mux; | ||
55 | struct gpio_i2cmux_platform_data *pdata; | ||
56 | struct i2c_adapter *parent; | ||
57 | int (*deselect) (struct i2c_adapter *, void *, u32); | ||
58 | unsigned initial_state; | ||
59 | int i, ret; | ||
60 | |||
61 | pdata = pdev->dev.platform_data; | ||
62 | if (!pdata) { | ||
63 | dev_err(&pdev->dev, "Missing platform data\n"); | ||
64 | return -ENODEV; | ||
65 | } | ||
66 | |||
67 | parent = i2c_get_adapter(pdata->parent); | ||
68 | if (!parent) { | ||
69 | dev_err(&pdev->dev, "Parent adapter (%d) not found\n", | ||
70 | pdata->parent); | ||
71 | return -ENODEV; | ||
72 | } | ||
73 | |||
74 | mux = kzalloc(sizeof(*mux), GFP_KERNEL); | ||
75 | if (!mux) { | ||
76 | ret = -ENOMEM; | ||
77 | goto alloc_failed; | ||
78 | } | ||
79 | |||
80 | mux->parent = parent; | ||
81 | mux->data = *pdata; | ||
82 | mux->adap = kzalloc(sizeof(struct i2c_adapter *) * pdata->n_values, | ||
83 | GFP_KERNEL); | ||
84 | if (!mux->adap) { | ||
85 | ret = -ENOMEM; | ||
86 | goto alloc_failed2; | ||
87 | } | ||
88 | |||
89 | if (pdata->idle != GPIO_I2CMUX_NO_IDLE) { | ||
90 | initial_state = pdata->idle; | ||
91 | deselect = gpiomux_deselect; | ||
92 | } else { | ||
93 | initial_state = pdata->values[0]; | ||
94 | deselect = NULL; | ||
95 | } | ||
96 | |||
97 | for (i = 0; i < pdata->n_gpios; i++) { | ||
98 | ret = gpio_request(pdata->gpios[i], "gpio-i2cmux"); | ||
99 | if (ret) | ||
100 | goto err_request_gpio; | ||
101 | gpio_direction_output(pdata->gpios[i], | ||
102 | initial_state & (1 << i)); | ||
103 | } | ||
104 | |||
105 | for (i = 0; i < pdata->n_values; i++) { | ||
106 | u32 nr = pdata->base_nr ? (pdata->base_nr + i) : 0; | ||
107 | |||
108 | mux->adap[i] = i2c_add_mux_adapter(parent, mux, nr, i, | ||
109 | gpiomux_select, deselect); | ||
110 | if (!mux->adap[i]) { | ||
111 | ret = -ENODEV; | ||
112 | dev_err(&pdev->dev, "Failed to add adapter %d\n", i); | ||
113 | goto add_adapter_failed; | ||
114 | } | ||
115 | } | ||
116 | |||
117 | dev_info(&pdev->dev, "%d port mux on %s adapter\n", | ||
118 | pdata->n_values, parent->name); | ||
119 | |||
120 | platform_set_drvdata(pdev, mux); | ||
121 | |||
122 | return 0; | ||
123 | |||
124 | add_adapter_failed: | ||
125 | for (; i > 0; i--) | ||
126 | i2c_del_mux_adapter(mux->adap[i - 1]); | ||
127 | i = pdata->n_gpios; | ||
128 | err_request_gpio: | ||
129 | for (; i > 0; i--) | ||
130 | gpio_free(pdata->gpios[i - 1]); | ||
131 | kfree(mux->adap); | ||
132 | alloc_failed2: | ||
133 | kfree(mux); | ||
134 | alloc_failed: | ||
135 | i2c_put_adapter(parent); | ||
136 | |||
137 | return ret; | ||
138 | } | ||
139 | |||
140 | static int __devexit gpiomux_remove(struct platform_device *pdev) | ||
141 | { | ||
142 | struct gpiomux *mux = platform_get_drvdata(pdev); | ||
143 | int i; | ||
144 | |||
145 | for (i = 0; i < mux->data.n_values; i++) | ||
146 | i2c_del_mux_adapter(mux->adap[i]); | ||
147 | |||
148 | for (i = 0; i < mux->data.n_gpios; i++) | ||
149 | gpio_free(mux->data.gpios[i]); | ||
150 | |||
151 | platform_set_drvdata(pdev, NULL); | ||
152 | i2c_put_adapter(mux->parent); | ||
153 | kfree(mux->adap); | ||
154 | kfree(mux); | ||
155 | |||
156 | return 0; | ||
157 | } | ||
158 | |||
159 | static struct platform_driver gpiomux_driver = { | ||
160 | .probe = gpiomux_probe, | ||
161 | .remove = __devexit_p(gpiomux_remove), | ||
162 | .driver = { | ||
163 | .owner = THIS_MODULE, | ||
164 | .name = "gpio-i2cmux", | ||
165 | }, | ||
166 | }; | ||
167 | |||
168 | static int __init gpiomux_init(void) | ||
169 | { | ||
170 | return platform_driver_register(&gpiomux_driver); | ||
171 | } | ||
172 | |||
173 | static void __exit gpiomux_exit(void) | ||
174 | { | ||
175 | platform_driver_unregister(&gpiomux_driver); | ||
176 | } | ||
177 | |||
178 | module_init(gpiomux_init); | ||
179 | module_exit(gpiomux_exit); | ||
180 | |||
181 | MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver"); | ||
182 | MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>"); | ||
183 | MODULE_LICENSE("GPL"); | ||
184 | MODULE_ALIAS("platform:gpio-i2cmux"); | ||
diff --git a/drivers/i2c/muxes/pca9541.c b/drivers/i2c/muxes/pca9541.c new file mode 100644 index 00000000000..ed699c5aa79 --- /dev/null +++ b/drivers/i2c/muxes/pca9541.c | |||
@@ -0,0 +1,411 @@ | |||
1 | /* | ||
2 | * I2C multiplexer driver for PCA9541 bus master selector | ||
3 | * | ||
4 | * Copyright (c) 2010 Ericsson AB. | ||
5 | * | ||
6 | * Author: Guenter Roeck <guenter.roeck@ericsson.com> | ||
7 | * | ||
8 | * Derived from: | ||
9 | * pca954x.c | ||
10 | * | ||
11 | * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it> | ||
12 | * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it> | ||
13 | * | ||
14 | * This file is licensed under the terms of the GNU General Public | ||
15 | * License version 2. This program is licensed "as is" without any | ||
16 | * warranty of any kind, whether express or implied. | ||
17 | */ | ||
18 | |||
19 | #include <linux/module.h> | ||
20 | #include <linux/init.h> | ||
21 | #include <linux/jiffies.h> | ||
22 | #include <linux/delay.h> | ||
23 | #include <linux/slab.h> | ||
24 | #include <linux/device.h> | ||
25 | #include <linux/i2c.h> | ||
26 | #include <linux/i2c-mux.h> | ||
27 | |||
28 | #include <linux/i2c/pca954x.h> | ||
29 | |||
30 | /* | ||
31 | * The PCA9541 is a bus master selector. It supports two I2C masters connected | ||
32 | * to a single slave bus. | ||
33 | * | ||
34 | * Before each bus transaction, a master has to acquire bus ownership. After the | ||
35 | * transaction is complete, bus ownership has to be released. This fits well | ||
36 | * into the I2C multiplexer framework, which provides select and release | ||
37 | * functions for this purpose. For this reason, this driver is modeled as | ||
38 | * single-channel I2C bus multiplexer. | ||
39 | * | ||
40 | * This driver assumes that the two bus masters are controlled by two different | ||
41 | * hosts. If a single host controls both masters, platform code has to ensure | ||
42 | * that only one of the masters is instantiated at any given time. | ||
43 | */ | ||
44 | |||
45 | #define PCA9541_CONTROL 0x01 | ||
46 | #define PCA9541_ISTAT 0x02 | ||
47 | |||
48 | #define PCA9541_CTL_MYBUS (1 << 0) | ||
49 | #define PCA9541_CTL_NMYBUS (1 << 1) | ||
50 | #define PCA9541_CTL_BUSON (1 << 2) | ||
51 | #define PCA9541_CTL_NBUSON (1 << 3) | ||
52 | #define PCA9541_CTL_BUSINIT (1 << 4) | ||
53 | #define PCA9541_CTL_TESTON (1 << 6) | ||
54 | #define PCA9541_CTL_NTESTON (1 << 7) | ||
55 | |||
56 | #define PCA9541_ISTAT_INTIN (1 << 0) | ||
57 | #define PCA9541_ISTAT_BUSINIT (1 << 1) | ||
58 | #define PCA9541_ISTAT_BUSOK (1 << 2) | ||
59 | #define PCA9541_ISTAT_BUSLOST (1 << 3) | ||
60 | #define PCA9541_ISTAT_MYTEST (1 << 6) | ||
61 | #define PCA9541_ISTAT_NMYTEST (1 << 7) | ||
62 | |||
63 | #define BUSON (PCA9541_CTL_BUSON | PCA9541_CTL_NBUSON) | ||
64 | #define MYBUS (PCA9541_CTL_MYBUS | PCA9541_CTL_NMYBUS) | ||
65 | #define mybus(x) (!((x) & MYBUS) || ((x) & MYBUS) == MYBUS) | ||
66 | #define busoff(x) (!((x) & BUSON) || ((x) & BUSON) == BUSON) | ||
67 | |||
68 | /* arbitration timeouts, in jiffies */ | ||
69 | #define ARB_TIMEOUT (HZ / 8) /* 125 ms until forcing bus ownership */ | ||
70 | #define ARB2_TIMEOUT (HZ / 4) /* 250 ms until acquisition failure */ | ||
71 | |||
72 | /* arbitration retry delays, in us */ | ||
73 | #define SELECT_DELAY_SHORT 50 | ||
74 | #define SELECT_DELAY_LONG 1000 | ||
75 | |||
76 | struct pca9541 { | ||
77 | struct i2c_adapter *mux_adap; | ||
78 | unsigned long select_timeout; | ||
79 | unsigned long arb_timeout; | ||
80 | }; | ||
81 | |||
82 | static const struct i2c_device_id pca9541_id[] = { | ||
83 | {"pca9541", 0}, | ||
84 | {} | ||
85 | }; | ||
86 | |||
87 | MODULE_DEVICE_TABLE(i2c, pca9541_id); | ||
88 | |||
89 | /* | ||
90 | * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer() | ||
91 | * as they will try to lock the adapter a second time. | ||
92 | */ | ||
93 | static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val) | ||
94 | { | ||
95 | struct i2c_adapter *adap = client->adapter; | ||
96 | int ret; | ||
97 | |||
98 | if (adap->algo->master_xfer) { | ||
99 | struct i2c_msg msg; | ||
100 | char buf[2]; | ||
101 | |||
102 | msg.addr = client->addr; | ||
103 | msg.flags = 0; | ||
104 | msg.len = 2; | ||
105 | buf[0] = command; | ||
106 | buf[1] = val; | ||
107 | msg.buf = buf; | ||
108 | ret = adap->algo->master_xfer(adap, &msg, 1); | ||
109 | } else { | ||
110 | union i2c_smbus_data data; | ||
111 | |||
112 | data.byte = val; | ||
113 | ret = adap->algo->smbus_xfer(adap, client->addr, | ||
114 | client->flags, | ||
115 | I2C_SMBUS_WRITE, | ||
116 | command, | ||
117 | I2C_SMBUS_BYTE_DATA, &data); | ||
118 | } | ||
119 | |||
120 | return ret; | ||
121 | } | ||
122 | |||
123 | /* | ||
124 | * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer() | ||
125 | * as they will try to lock adapter a second time. | ||
126 | */ | ||
127 | static int pca9541_reg_read(struct i2c_client *client, u8 command) | ||
128 | { | ||
129 | struct i2c_adapter *adap = client->adapter; | ||
130 | int ret; | ||
131 | u8 val; | ||
132 | |||
133 | if (adap->algo->master_xfer) { | ||
134 | struct i2c_msg msg[2] = { | ||
135 | { | ||
136 | .addr = client->addr, | ||
137 | .flags = 0, | ||
138 | .len = 1, | ||
139 | .buf = &command | ||
140 | }, | ||
141 | { | ||
142 | .addr = client->addr, | ||
143 | .flags = I2C_M_RD, | ||
144 | .len = 1, | ||
145 | .buf = &val | ||
146 | } | ||
147 | }; | ||
148 | ret = adap->algo->master_xfer(adap, msg, 2); | ||
149 | if (ret == 2) | ||
150 | ret = val; | ||
151 | else if (ret >= 0) | ||
152 | ret = -EIO; | ||
153 | } else { | ||
154 | union i2c_smbus_data data; | ||
155 | |||
156 | ret = adap->algo->smbus_xfer(adap, client->addr, | ||
157 | client->flags, | ||
158 | I2C_SMBUS_READ, | ||
159 | command, | ||
160 | I2C_SMBUS_BYTE_DATA, &data); | ||
161 | if (!ret) | ||
162 | ret = data.byte; | ||
163 | } | ||
164 | return ret; | ||
165 | } | ||
166 | |||
167 | /* | ||
168 | * Arbitration management functions | ||
169 | */ | ||
170 | |||
171 | /* Release bus. Also reset NTESTON and BUSINIT if it was set. */ | ||
172 | static void pca9541_release_bus(struct i2c_client *client) | ||
173 | { | ||
174 | int reg; | ||
175 | |||
176 | reg = pca9541_reg_read(client, PCA9541_CONTROL); | ||
177 | if (reg >= 0 && !busoff(reg) && mybus(reg)) | ||
178 | pca9541_reg_write(client, PCA9541_CONTROL, | ||
179 | (reg & PCA9541_CTL_NBUSON) >> 1); | ||
180 | } | ||
181 | |||
182 | /* | ||
183 | * Arbitration is defined as a two-step process. A bus master can only activate | ||
184 | * the slave bus if it owns it; otherwise it has to request ownership first. | ||
185 | * This multi-step process ensures that access contention is resolved | ||
186 | * gracefully. | ||
187 | * | ||
188 | * Bus Ownership Other master Action | ||
189 | * state requested access | ||
190 | * ---------------------------------------------------- | ||
191 | * off - yes wait for arbitration timeout or | ||
192 | * for other master to drop request | ||
193 | * off no no take ownership | ||
194 | * off yes no turn on bus | ||
195 | * on yes - done | ||
196 | * on no - wait for arbitration timeout or | ||
197 | * for other master to release bus | ||
198 | * | ||
199 | * The main contention point occurs if the slave bus is off and both masters | ||
200 | * request ownership at the same time. In this case, one master will turn on | ||
201 | * the slave bus, believing that it owns it. The other master will request | ||
202 | * bus ownership. Result is that the bus is turned on, and master which did | ||
203 | * _not_ own the slave bus before ends up owning it. | ||
204 | */ | ||
205 | |||
206 | /* Control commands per PCA9541 datasheet */ | ||
207 | static const u8 pca9541_control[16] = { | ||
208 | 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1 | ||
209 | }; | ||
210 | |||
211 | /* | ||
212 | * Channel arbitration | ||
213 | * | ||
214 | * Return values: | ||
215 | * <0: error | ||
216 | * 0 : bus not acquired | ||
217 | * 1 : bus acquired | ||
218 | */ | ||
219 | static int pca9541_arbitrate(struct i2c_client *client) | ||
220 | { | ||
221 | struct pca9541 *data = i2c_get_clientdata(client); | ||
222 | int reg; | ||
223 | |||
224 | reg = pca9541_reg_read(client, PCA9541_CONTROL); | ||
225 | if (reg < 0) | ||
226 | return reg; | ||
227 | |||
228 | if (busoff(reg)) { | ||
229 | int istat; | ||
230 | /* | ||
231 | * Bus is off. Request ownership or turn it on unless | ||
232 | * other master requested ownership. | ||
233 | */ | ||
234 | istat = pca9541_reg_read(client, PCA9541_ISTAT); | ||
235 | if (!(istat & PCA9541_ISTAT_NMYTEST) | ||
236 | || time_is_before_eq_jiffies(data->arb_timeout)) { | ||
237 | /* | ||
238 | * Other master did not request ownership, | ||
239 | * or arbitration timeout expired. Take the bus. | ||
240 | */ | ||
241 | pca9541_reg_write(client, | ||
242 | PCA9541_CONTROL, | ||
243 | pca9541_control[reg & 0x0f] | ||
244 | | PCA9541_CTL_NTESTON); | ||
245 | data->select_timeout = SELECT_DELAY_SHORT; | ||
246 | } else { | ||
247 | /* | ||
248 | * Other master requested ownership. | ||
249 | * Set extra long timeout to give it time to acquire it. | ||
250 | */ | ||
251 | data->select_timeout = SELECT_DELAY_LONG * 2; | ||
252 | } | ||
253 | } else if (mybus(reg)) { | ||
254 | /* | ||
255 | * Bus is on, and we own it. We are done with acquisition. | ||
256 | * Reset NTESTON and BUSINIT, then return success. | ||
257 | */ | ||
258 | if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT)) | ||
259 | pca9541_reg_write(client, | ||
260 | PCA9541_CONTROL, | ||
261 | reg & ~(PCA9541_CTL_NTESTON | ||
262 | | PCA9541_CTL_BUSINIT)); | ||
263 | return 1; | ||
264 | } else { | ||
265 | /* | ||
266 | * Other master owns the bus. | ||
267 | * If arbitration timeout has expired, force ownership. | ||
268 | * Otherwise request it. | ||
269 | */ | ||
270 | data->select_timeout = SELECT_DELAY_LONG; | ||
271 | if (time_is_before_eq_jiffies(data->arb_timeout)) { | ||
272 | /* Time is up, take the bus and reset it. */ | ||
273 | pca9541_reg_write(client, | ||
274 | PCA9541_CONTROL, | ||
275 | pca9541_control[reg & 0x0f] | ||
276 | | PCA9541_CTL_BUSINIT | ||
277 | | PCA9541_CTL_NTESTON); | ||
278 | } else { | ||
279 | /* Request bus ownership if needed */ | ||
280 | if (!(reg & PCA9541_CTL_NTESTON)) | ||
281 | pca9541_reg_write(client, | ||
282 | PCA9541_CONTROL, | ||
283 | reg | PCA9541_CTL_NTESTON); | ||
284 | } | ||
285 | } | ||
286 | return 0; | ||
287 | } | ||
288 | |||
289 | static int pca9541_select_chan(struct i2c_adapter *adap, void *client, u32 chan) | ||
290 | { | ||
291 | struct pca9541 *data = i2c_get_clientdata(client); | ||
292 | int ret; | ||
293 | unsigned long timeout = jiffies + ARB2_TIMEOUT; | ||
294 | /* give up after this time */ | ||
295 | |||
296 | data->arb_timeout = jiffies + ARB_TIMEOUT; | ||
297 | /* force bus ownership after this time */ | ||
298 | |||
299 | do { | ||
300 | ret = pca9541_arbitrate(client); | ||
301 | if (ret) | ||
302 | return ret < 0 ? ret : 0; | ||
303 | |||
304 | if (data->select_timeout == SELECT_DELAY_SHORT) | ||
305 | udelay(data->select_timeout); | ||
306 | else | ||
307 | msleep(data->select_timeout / 1000); | ||
308 | } while (time_is_after_eq_jiffies(timeout)); | ||
309 | |||
310 | return -ETIMEDOUT; | ||
311 | } | ||
312 | |||
313 | static int pca9541_release_chan(struct i2c_adapter *adap, | ||
314 | void *client, u32 chan) | ||
315 | { | ||
316 | pca9541_release_bus(client); | ||
317 | return 0; | ||
318 | } | ||
319 | |||
320 | /* | ||
321 | * I2C init/probing/exit functions | ||
322 | */ | ||
323 | static int pca9541_probe(struct i2c_client *client, | ||
324 | const struct i2c_device_id *id) | ||
325 | { | ||
326 | struct i2c_adapter *adap = client->adapter; | ||
327 | struct pca954x_platform_data *pdata = client->dev.platform_data; | ||
328 | struct pca9541 *data; | ||
329 | int force; | ||
330 | int ret = -ENODEV; | ||
331 | |||
332 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA)) | ||
333 | goto err; | ||
334 | |||
335 | data = kzalloc(sizeof(struct pca9541), GFP_KERNEL); | ||
336 | if (!data) { | ||
337 | ret = -ENOMEM; | ||
338 | goto err; | ||
339 | } | ||
340 | |||
341 | i2c_set_clientdata(client, data); | ||
342 | |||
343 | /* | ||
344 | * I2C accesses are unprotected here. | ||
345 | * We have to lock the adapter before releasing the bus. | ||
346 | */ | ||
347 | i2c_lock_adapter(adap); | ||
348 | pca9541_release_bus(client); | ||
349 | i2c_unlock_adapter(adap); | ||
350 | |||
351 | /* Create mux adapter */ | ||
352 | |||
353 | force = 0; | ||
354 | if (pdata) | ||
355 | force = pdata->modes[0].adap_id; | ||
356 | data->mux_adap = i2c_add_mux_adapter(adap, client, force, 0, | ||
357 | pca9541_select_chan, | ||
358 | pca9541_release_chan); | ||
359 | |||
360 | if (data->mux_adap == NULL) { | ||
361 | dev_err(&client->dev, "failed to register master selector\n"); | ||
362 | goto exit_free; | ||
363 | } | ||
364 | |||
365 | dev_info(&client->dev, "registered master selector for I2C %s\n", | ||
366 | client->name); | ||
367 | |||
368 | return 0; | ||
369 | |||
370 | exit_free: | ||
371 | kfree(data); | ||
372 | err: | ||
373 | return ret; | ||
374 | } | ||
375 | |||
376 | static int pca9541_remove(struct i2c_client *client) | ||
377 | { | ||
378 | struct pca9541 *data = i2c_get_clientdata(client); | ||
379 | |||
380 | i2c_del_mux_adapter(data->mux_adap); | ||
381 | |||
382 | kfree(data); | ||
383 | return 0; | ||
384 | } | ||
385 | |||
386 | static struct i2c_driver pca9541_driver = { | ||
387 | .driver = { | ||
388 | .name = "pca9541", | ||
389 | .owner = THIS_MODULE, | ||
390 | }, | ||
391 | .probe = pca9541_probe, | ||
392 | .remove = pca9541_remove, | ||
393 | .id_table = pca9541_id, | ||
394 | }; | ||
395 | |||
396 | static int __init pca9541_init(void) | ||
397 | { | ||
398 | return i2c_add_driver(&pca9541_driver); | ||
399 | } | ||
400 | |||
401 | static void __exit pca9541_exit(void) | ||
402 | { | ||
403 | i2c_del_driver(&pca9541_driver); | ||
404 | } | ||
405 | |||
406 | module_init(pca9541_init); | ||
407 | module_exit(pca9541_exit); | ||
408 | |||
409 | MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>"); | ||
410 | MODULE_DESCRIPTION("PCA9541 I2C master selector driver"); | ||
411 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/i2c/muxes/pca954x.c b/drivers/i2c/muxes/pca954x.c new file mode 100644 index 00000000000..dd14ae38d3e --- /dev/null +++ b/drivers/i2c/muxes/pca954x.c | |||
@@ -0,0 +1,404 @@ | |||
1 | /* | ||
2 | * I2C multiplexer | ||
3 | * | ||
4 | * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it> | ||
5 | * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it> | ||
6 | * | ||
7 | * This module supports the PCA954x series of I2C multiplexer/switch chips | ||
8 | * made by Philips Semiconductors. | ||
9 | * This includes the: | ||
10 | * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547 | ||
11 | * and PCA9548. | ||
12 | * | ||
13 | * These chips are all controlled via the I2C bus itself, and all have a | ||
14 | * single 8-bit register. The upstream "parent" bus fans out to two, | ||
15 | * four, or eight downstream busses or channels; which of these | ||
16 | * are selected is determined by the chip type and register contents. A | ||
17 | * mux can select only one sub-bus at a time; a switch can select any | ||
18 | * combination simultaneously. | ||
19 | * | ||
20 | * Based on: | ||
21 | * pca954x.c from Kumar Gala <galak@kernel.crashing.org> | ||
22 | * Copyright (C) 2006 | ||
23 | * | ||
24 | * Based on: | ||
25 | * pca954x.c from Ken Harrenstien | ||
26 | * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) | ||
27 | * | ||
28 | * Based on: | ||
29 | * i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com> | ||
30 | * and | ||
31 | * pca9540.c from Jean Delvare <khali@linux-fr.org>. | ||
32 | * | ||
33 | * This file is licensed under the terms of the GNU General Public | ||
34 | * License version 2. This program is licensed "as is" without any | ||
35 | * warranty of any kind, whether express or implied. | ||
36 | */ | ||
37 | |||
38 | #include <linux/module.h> | ||
39 | #include <linux/init.h> | ||
40 | #include <linux/slab.h> | ||
41 | #include <linux/device.h> | ||
42 | #include <linux/i2c.h> | ||
43 | #include <linux/i2c-mux.h> | ||
44 | #include <linux/regulator/consumer.h> | ||
45 | #include <linux/err.h> | ||
46 | #include <linux/delay.h> | ||
47 | |||
48 | #include <linux/i2c/pca954x.h> | ||
49 | |||
50 | #define PCA954X_MAX_NCHANS 8 | ||
51 | |||
52 | enum pca_type { | ||
53 | pca_9540, | ||
54 | pca_9542, | ||
55 | pca_9543, | ||
56 | pca_9544, | ||
57 | pca_9545, | ||
58 | pca_9546, | ||
59 | pca_9547, | ||
60 | pca_9548, | ||
61 | }; | ||
62 | |||
63 | struct pca954x { | ||
64 | enum pca_type type; | ||
65 | struct i2c_adapter *virt_adaps[PCA954X_MAX_NCHANS]; | ||
66 | |||
67 | u8 last_chan; /* last register value */ | ||
68 | struct regulator *vcc_reg; | ||
69 | struct regulator *i2c_reg; | ||
70 | }; | ||
71 | |||
72 | struct chip_desc { | ||
73 | u8 nchans; | ||
74 | u8 enable; /* used for muxes only */ | ||
75 | enum muxtype { | ||
76 | pca954x_ismux = 0, | ||
77 | pca954x_isswi | ||
78 | } muxtype; | ||
79 | }; | ||
80 | |||
81 | /* Provide specs for the PCA954x types we know about */ | ||
82 | static const struct chip_desc chips[] = { | ||
83 | [pca_9540] = { | ||
84 | .nchans = 2, | ||
85 | .enable = 0x4, | ||
86 | .muxtype = pca954x_ismux, | ||
87 | }, | ||
88 | [pca_9543] = { | ||
89 | .nchans = 2, | ||
90 | .muxtype = pca954x_isswi, | ||
91 | }, | ||
92 | [pca_9544] = { | ||
93 | .nchans = 4, | ||
94 | .enable = 0x4, | ||
95 | .muxtype = pca954x_ismux, | ||
96 | }, | ||
97 | [pca_9545] = { | ||
98 | .nchans = 4, | ||
99 | .muxtype = pca954x_isswi, | ||
100 | }, | ||
101 | [pca_9547] = { | ||
102 | .nchans = 8, | ||
103 | .enable = 0x8, | ||
104 | .muxtype = pca954x_ismux, | ||
105 | }, | ||
106 | [pca_9548] = { | ||
107 | .nchans = 8, | ||
108 | .muxtype = pca954x_isswi, | ||
109 | }, | ||
110 | }; | ||
111 | |||
112 | static const struct i2c_device_id pca954x_id[] = { | ||
113 | { "pca9540", pca_9540 }, | ||
114 | { "pca9542", pca_9540 }, | ||
115 | { "pca9543", pca_9543 }, | ||
116 | { "pca9544", pca_9544 }, | ||
117 | { "pca9545", pca_9545 }, | ||
118 | { "pca9546", pca_9545 }, | ||
119 | { "pca9547", pca_9547 }, | ||
120 | { "pca9548", pca_9548 }, | ||
121 | { } | ||
122 | }; | ||
123 | MODULE_DEVICE_TABLE(i2c, pca954x_id); | ||
124 | |||
125 | /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer() | ||
126 | for this as they will try to lock adapter a second time */ | ||
127 | static int pca954x_reg_write(struct i2c_adapter *adap, | ||
128 | struct i2c_client *client, u8 val) | ||
129 | { | ||
130 | int ret = -ENODEV; | ||
131 | struct pca954x *data = i2c_get_clientdata(client); | ||
132 | |||
133 | /* Increase ref count for pca954x vcc */ | ||
134 | if (data->vcc_reg) { | ||
135 | ret = regulator_enable(data->vcc_reg); | ||
136 | if (ret) { | ||
137 | dev_err(&client->dev, "%s: failed to enable vcc\n", | ||
138 | __func__); | ||
139 | goto vcc_regulator_failed; | ||
140 | } | ||
141 | } | ||
142 | /* Increase ref count for pca954x vcc_i2c */ | ||
143 | if (data->i2c_reg) { | ||
144 | ret = regulator_enable(data->i2c_reg); | ||
145 | if (ret) { | ||
146 | dev_err(&client->dev, "%s: failed to enable vcc_i2c\n", | ||
147 | __func__); | ||
148 | goto i2c_regulator_failed; | ||
149 | } | ||
150 | } | ||
151 | |||
152 | if (adap->algo->master_xfer) { | ||
153 | struct i2c_msg msg; | ||
154 | char buf[1]; | ||
155 | |||
156 | msg.addr = client->addr; | ||
157 | msg.flags = 0; | ||
158 | msg.len = 1; | ||
159 | buf[0] = val; | ||
160 | msg.buf = buf; | ||
161 | ret = adap->algo->master_xfer(adap, &msg, 1); | ||
162 | } else { | ||
163 | union i2c_smbus_data data; | ||
164 | ret = adap->algo->smbus_xfer(adap, client->addr, | ||
165 | client->flags, | ||
166 | I2C_SMBUS_WRITE, | ||
167 | val, I2C_SMBUS_BYTE, &data); | ||
168 | } | ||
169 | |||
170 | /* Decrease ref count for pca954x vcc_i2c */ | ||
171 | if (data->i2c_reg) | ||
172 | regulator_disable(data->i2c_reg); | ||
173 | |||
174 | i2c_regulator_failed: | ||
175 | /* Decrease ref count for pca954x vcc */ | ||
176 | if (data->vcc_reg) | ||
177 | regulator_disable(data->vcc_reg); | ||
178 | vcc_regulator_failed: | ||
179 | return ret; | ||
180 | } | ||
181 | |||
182 | static int pca954x_select_chan(struct i2c_adapter *adap, | ||
183 | void *client, u32 chan) | ||
184 | { | ||
185 | struct pca954x *data = i2c_get_clientdata(client); | ||
186 | const struct chip_desc *chip = &chips[data->type]; | ||
187 | u8 regval; | ||
188 | int ret = 0; | ||
189 | |||
190 | /* we make switches look like muxes, not sure how to be smarter */ | ||
191 | if (chip->muxtype == pca954x_ismux) | ||
192 | regval = chan | chip->enable; | ||
193 | else | ||
194 | regval = 1 << chan; | ||
195 | |||
196 | /* Only select the channel if its different from the last channel */ | ||
197 | if (data->last_chan != regval) { | ||
198 | ret = pca954x_reg_write(adap, client, regval); | ||
199 | data->last_chan = regval; | ||
200 | } | ||
201 | |||
202 | return ret; | ||
203 | } | ||
204 | |||
205 | static int pca954x_deselect_mux(struct i2c_adapter *adap, | ||
206 | void *client, u32 chan) | ||
207 | { | ||
208 | struct pca954x *data = i2c_get_clientdata(client); | ||
209 | |||
210 | /* Deselect active channel */ | ||
211 | data->last_chan = 0; | ||
212 | return pca954x_reg_write(adap, client, data->last_chan); | ||
213 | } | ||
214 | |||
215 | /* | ||
216 | * I2C init/probing/exit functions | ||
217 | */ | ||
218 | static int pca954x_probe(struct i2c_client *client, | ||
219 | const struct i2c_device_id *id) | ||
220 | { | ||
221 | struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); | ||
222 | struct pca954x_platform_data *pdata = client->dev.platform_data; | ||
223 | int num, force; | ||
224 | struct pca954x *data; | ||
225 | int ret = -ENODEV; | ||
226 | |||
227 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) | ||
228 | goto err; | ||
229 | |||
230 | data = kzalloc(sizeof(struct pca954x), GFP_KERNEL); | ||
231 | if (!data) { | ||
232 | ret = -ENOMEM; | ||
233 | goto err; | ||
234 | } | ||
235 | |||
236 | i2c_set_clientdata(client, data); | ||
237 | |||
238 | /* Get regulator pointer for pca954x vcc */ | ||
239 | data->vcc_reg = regulator_get(&client->dev, "vcc"); | ||
240 | if (PTR_ERR(data->vcc_reg) == -ENODEV) | ||
241 | data->vcc_reg = NULL; | ||
242 | else if (IS_ERR(data->vcc_reg)) { | ||
243 | dev_err(&client->dev, "%s: failed to get vcc\n", | ||
244 | __func__); | ||
245 | ret = PTR_ERR(data->vcc_reg); | ||
246 | goto exit_free; | ||
247 | } | ||
248 | /* Get regulator pointer for pca954x vcc_i2c */ | ||
249 | data->i2c_reg = regulator_get(&client->dev, "vcc_i2c"); | ||
250 | if (PTR_ERR(data->i2c_reg) == -ENODEV) | ||
251 | data->i2c_reg = NULL; | ||
252 | else if (IS_ERR(data->i2c_reg)) { | ||
253 | dev_err(&client->dev, "%s: failed to get vcc_i2c\n", | ||
254 | __func__); | ||
255 | ret = PTR_ERR(data->i2c_reg); | ||
256 | regulator_put(data->vcc_reg); | ||
257 | goto exit_free; | ||
258 | } | ||
259 | |||
260 | /* Increase ref count for pca954x vcc */ | ||
261 | if (data->vcc_reg) { | ||
262 | pr_info("%s: enable vcc\n", __func__); | ||
263 | ret = regulator_enable(data->vcc_reg); | ||
264 | if (ret) { | ||
265 | dev_err(&client->dev, "%s: failed to enable vcc\n", | ||
266 | __func__); | ||
267 | goto exit_regulator_put; | ||
268 | } | ||
269 | } | ||
270 | /* Increase ref count for pca954x vcc_i2c */ | ||
271 | if (data->i2c_reg) { | ||
272 | pr_info("%s: enable vcc_i2c\n", __func__); | ||
273 | ret = regulator_enable(data->i2c_reg); | ||
274 | if (ret) { | ||
275 | dev_err(&client->dev, "%s: failed to enable vcc_i2c\n", | ||
276 | __func__); | ||
277 | goto exit_vcc_regulator_disable; | ||
278 | } | ||
279 | } | ||
280 | |||
281 | /* | ||
282 | * Power-On Reset takes time. | ||
283 | * I2C is ready after Power-On Reset. | ||
284 | */ | ||
285 | msleep(1); | ||
286 | |||
287 | /* Write the mux register at addr to verify | ||
288 | * that the mux is in fact present. This also | ||
289 | * initializes the mux to disconnected state. | ||
290 | */ | ||
291 | if (i2c_smbus_write_byte(client, 0) < 0) { | ||
292 | dev_warn(&client->dev, "probe failed\n"); | ||
293 | goto exit_regulator_disable; | ||
294 | } | ||
295 | |||
296 | /* Decrease ref count for pca954x vcc */ | ||
297 | if (data->vcc_reg) | ||
298 | regulator_disable(data->vcc_reg); | ||
299 | /* Decrease ref count for pca954x vcc_i2c */ | ||
300 | if (data->i2c_reg) | ||
301 | regulator_disable(data->i2c_reg); | ||
302 | |||
303 | data->type = id->driver_data; | ||
304 | data->last_chan = 0; /* force the first selection */ | ||
305 | |||
306 | /* Now create an adapter for each channel */ | ||
307 | for (num = 0; num < chips[data->type].nchans; num++) { | ||
308 | force = 0; /* dynamic adap number */ | ||
309 | if (pdata) { | ||
310 | if (num < pdata->num_modes) | ||
311 | /* force static number */ | ||
312 | force = pdata->modes[num].adap_id; | ||
313 | else | ||
314 | /* discard unconfigured channels */ | ||
315 | break; | ||
316 | } | ||
317 | |||
318 | data->virt_adaps[num] = | ||
319 | i2c_add_mux_adapter(adap, client, | ||
320 | force, num, pca954x_select_chan, | ||
321 | (pdata && pdata->modes[num].deselect_on_exit) | ||
322 | ? pca954x_deselect_mux : NULL); | ||
323 | |||
324 | if (data->virt_adaps[num] == NULL) { | ||
325 | ret = -ENODEV; | ||
326 | dev_err(&client->dev, | ||
327 | "failed to register multiplexed adapter" | ||
328 | " %d as bus %d\n", num, force); | ||
329 | goto virt_reg_failed; | ||
330 | } | ||
331 | } | ||
332 | |||
333 | dev_info(&client->dev, | ||
334 | "registered %d multiplexed busses for I2C %s %s\n", | ||
335 | num, chips[data->type].muxtype == pca954x_ismux | ||
336 | ? "mux" : "switch", client->name); | ||
337 | |||
338 | return 0; | ||
339 | |||
340 | virt_reg_failed: | ||
341 | for (num--; num >= 0; num--) | ||
342 | i2c_del_mux_adapter(data->virt_adaps[num]); | ||
343 | exit_regulator_disable: | ||
344 | if (data->i2c_reg) | ||
345 | regulator_disable(data->i2c_reg); | ||
346 | exit_vcc_regulator_disable: | ||
347 | if (data->vcc_reg) | ||
348 | regulator_disable(data->vcc_reg); | ||
349 | exit_regulator_put: | ||
350 | regulator_put(data->i2c_reg); | ||
351 | regulator_put(data->vcc_reg); | ||
352 | exit_free: | ||
353 | kfree(data); | ||
354 | err: | ||
355 | return ret; | ||
356 | } | ||
357 | |||
358 | static int pca954x_remove(struct i2c_client *client) | ||
359 | { | ||
360 | struct pca954x *data = i2c_get_clientdata(client); | ||
361 | const struct chip_desc *chip = &chips[data->type]; | ||
362 | int i, err; | ||
363 | |||
364 | for (i = 0; i < chip->nchans; ++i) | ||
365 | if (data->virt_adaps[i]) { | ||
366 | err = i2c_del_mux_adapter(data->virt_adaps[i]); | ||
367 | if (err) | ||
368 | return err; | ||
369 | data->virt_adaps[i] = NULL; | ||
370 | } | ||
371 | |||
372 | regulator_put(data->i2c_reg); | ||
373 | regulator_put(data->vcc_reg); | ||
374 | |||
375 | kfree(data); | ||
376 | return 0; | ||
377 | } | ||
378 | |||
379 | static struct i2c_driver pca954x_driver = { | ||
380 | .driver = { | ||
381 | .name = "pca954x", | ||
382 | .owner = THIS_MODULE, | ||
383 | }, | ||
384 | .probe = pca954x_probe, | ||
385 | .remove = pca954x_remove, | ||
386 | .id_table = pca954x_id, | ||
387 | }; | ||
388 | |||
389 | static int __init pca954x_init(void) | ||
390 | { | ||
391 | return i2c_add_driver(&pca954x_driver); | ||
392 | } | ||
393 | |||
394 | static void __exit pca954x_exit(void) | ||
395 | { | ||
396 | i2c_del_driver(&pca954x_driver); | ||
397 | } | ||
398 | |||
399 | module_init(pca954x_init); | ||
400 | module_exit(pca954x_exit); | ||
401 | |||
402 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | ||
403 | MODULE_DESCRIPTION("PCA954x I2C mux/switch driver"); | ||
404 | MODULE_LICENSE("GPL v2"); | ||