i2c: mux: pca954x: make pullup regulator optional
[linux-3.10.git] / drivers / i2c / muxes / i2c-mux-pca954x.c
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 *pullup_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-pullup */
143         if (data->pullup_reg) {
144                 ret = regulator_enable(data->pullup_reg);
145                 if (ret) {
146                         dev_err(&client->dev, "%s: failed to enable vcc-pullup\n",
147                                 __func__);
148                         goto pullup_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-pullup */
171         if (data->pullup_reg)
172                 regulator_disable(data->pullup_reg);
173
174 pullup_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, class;
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) == -EPROBE_DEFER)
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-pullup */
249         data->pullup_reg = regulator_get(&client->dev, "vcc-pullup");
250         if (IS_ERR(data->pullup_reg)) {
251                 dev_info(&client->dev, "vcc-pullup regulator not found\n");
252                 data->pullup_reg = NULL;
253         }
254
255         /* Increase ref count for pca954x vcc */
256         if (data->vcc_reg) {
257                 pr_info("%s: enable vcc\n", __func__);
258                 ret = regulator_enable(data->vcc_reg);
259                 if (ret) {
260                         dev_err(&client->dev, "%s: failed to enable vcc\n",
261                                 __func__);
262                         goto exit_regulator_put;
263                 }
264         }
265         /* Increase ref count for pca954x vcc-pullup */
266         if (data->pullup_reg) {
267                 pr_info("%s: enable vcc-pullup\n", __func__);
268                 ret = regulator_enable(data->pullup_reg);
269                 if (ret) {
270                         dev_err(&client->dev, "%s: failed to enable vcc-pullup\n",
271                                 __func__);
272                         goto exit_vcc_regulator_disable;
273                 }
274         }
275
276         /*
277          * Power-On Reset takes time.
278          * I2C is ready after Power-On Reset.
279          */
280         msleep(1);
281
282         /* Write the mux register at addr to verify
283          * that the mux is in fact present. This also
284          * initializes the mux to disconnected state.
285          */
286         if (i2c_smbus_write_byte(client, 0) < 0) {
287                 dev_warn(&client->dev, "probe failed\n");
288                 goto exit_regulator_disable;
289         }
290
291         /* Decrease ref count for pca954x vcc */
292         if (data->vcc_reg)
293                 regulator_disable(data->vcc_reg);
294         /* Decrease ref count for pca954x vcc-pullup */
295         if (data->pullup_reg)
296                 regulator_disable(data->pullup_reg);
297
298         data->type = id->driver_data;
299         data->last_chan = 0;               /* force the first selection */
300
301         /* Now create an adapter for each channel */
302         for (num = 0; num < chips[data->type].nchans; num++) {
303                 bool deselect_on_exit = false;
304
305                 force = 0;                        /* dynamic adap number */
306                 class = 0;                        /* no class by default */
307                 if (pdata) {
308                         if (num < pdata->num_modes) {
309                                 /* force static number */
310                                 force = pdata->modes[num].adap_id;
311                                 class = pdata->modes[num].class;
312                                 deselect_on_exit =
313                                         pdata->modes[num].deselect_on_exit;
314                         } else
315                                 /* discard unconfigured channels */
316                                 break;
317                 }
318                 if (client->dev.of_node)
319                         deselect_on_exit = true;
320
321                 data->virt_adaps[num] =
322                         i2c_add_mux_adapter(adap, &client->dev, client,
323                                 force, num, class, pca954x_select_chan,
324                                 (deselect_on_exit)
325                                         ? pca954x_deselect_mux : NULL);
326
327                 if (data->virt_adaps[num] == NULL) {
328                         ret = -ENODEV;
329                         dev_err(&client->dev,
330                                 "failed to register multiplexed adapter"
331                                 " %d as bus %d\n", num, force);
332                         goto virt_reg_failed;
333                 }
334         }
335
336         dev_info(&client->dev,
337                  "registered %d multiplexed busses for I2C %s %s\n",
338                  num, chips[data->type].muxtype == pca954x_ismux
339                                 ? "mux" : "switch", client->name);
340
341         return 0;
342
343 virt_reg_failed:
344         for (num--; num >= 0; num--)
345                 i2c_del_mux_adapter(data->virt_adaps[num]);
346 exit_regulator_disable:
347         if (data->pullup_reg)
348                 regulator_disable(data->pullup_reg);
349 exit_vcc_regulator_disable:
350         if (data->vcc_reg)
351                 regulator_disable(data->vcc_reg);
352 exit_regulator_put:
353         if (data->pullup_reg)
354                 regulator_put(data->pullup_reg);
355         regulator_put(data->vcc_reg);
356 exit_free:
357         kfree(data);
358 err:
359         return ret;
360 }
361
362 static int pca954x_remove(struct i2c_client *client)
363 {
364         struct pca954x *data = i2c_get_clientdata(client);
365         const struct chip_desc *chip = &chips[data->type];
366         int i;
367
368         for (i = 0; i < chip->nchans; ++i)
369                 if (data->virt_adaps[i]) {
370                         i2c_del_mux_adapter(data->virt_adaps[i]);
371                         data->virt_adaps[i] = NULL;
372                 }
373
374         if (data->pullup_reg)
375                 regulator_put(data->pullup_reg);
376         regulator_put(data->vcc_reg);
377
378         kfree(data);
379         return 0;
380 }
381
382 static struct i2c_driver pca954x_driver = {
383         .driver         = {
384                 .name   = "pca954x",
385                 .owner  = THIS_MODULE,
386         },
387         .probe          = pca954x_probe,
388         .remove         = pca954x_remove,
389         .id_table       = pca954x_id,
390 };
391
392 module_i2c_driver(pca954x_driver);
393
394 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
395 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
396 MODULE_LICENSE("GPL v2");