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/pca954x.c | |
parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) |
Diffstat (limited to 'drivers/i2c/muxes/pca954x.c')
-rw-r--r-- | drivers/i2c/muxes/pca954x.c | 404 |
1 files changed, 404 insertions, 0 deletions
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"); | ||