Blame view
drivers/i2c/muxes/i2c-mux-pca954x.c
7.53 KB
7f528135d i2c: I2C bus mult... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* * I2C multiplexer * * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it> * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it> * * This module supports the PCA954x series of I2C multiplexer/switch chips * made by Philips Semiconductors. * This includes the: * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547 * and PCA9548. * * These chips are all controlled via the I2C bus itself, and all have a * single 8-bit register. The upstream "parent" bus fans out to two, * four, or eight downstream busses or channels; which of these * are selected is determined by the chip type and register contents. A * mux can select only one sub-bus at a time; a switch can select any * combination simultaneously. * * Based on: * pca954x.c from Kumar Gala <galak@kernel.crashing.org> * Copyright (C) 2006 * * Based on: * pca954x.c from Ken Harrenstien * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) * * Based on: * i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com> * and |
7c81c60f3 Update Jean Delva... |
31 |
* pca9540.c from Jean Delvare <jdelvare@suse.de>. |
7f528135d i2c: I2C bus mult... |
32 33 34 35 36 |
* * This file is licensed under the terms of the GNU General Public * License version 2. This program is licensed "as is" without any * warranty of any kind, whether express or implied. */ |
7f528135d i2c: I2C bus mult... |
37 |
#include <linux/device.h> |
642653d16 i2c: pca954x: Fix... |
38 |
#include <linux/gpio/consumer.h> |
7f528135d i2c: I2C bus mult... |
39 40 |
#include <linux/i2c.h> #include <linux/i2c-mux.h> |
7f528135d i2c: I2C bus mult... |
41 |
#include <linux/i2c/pca954x.h> |
4b9b00734 i2c: pca954x: Sor... |
42 |
#include <linux/module.h> |
72f027157 of: i2c: Add i2c-... |
43 |
#include <linux/of.h> |
f5e596cd9 i2c: pca954x: put... |
44 |
#include <linux/pm.h> |
4b9b00734 i2c: pca954x: Sor... |
45 |
#include <linux/slab.h> |
7f528135d i2c: I2C bus mult... |
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
#define PCA954X_MAX_NCHANS 8 enum pca_type { pca_9540, pca_9542, pca_9543, pca_9544, pca_9545, pca_9546, pca_9547, pca_9548, }; struct pca954x { enum pca_type type; struct i2c_adapter *virt_adaps[PCA954X_MAX_NCHANS]; u8 last_chan; /* last register value */ }; struct chip_desc { u8 nchans; u8 enable; /* used for muxes only */ enum muxtype { pca954x_ismux = 0, pca954x_isswi } muxtype; }; /* Provide specs for the PCA954x types we know about */ static const struct chip_desc chips[] = { [pca_9540] = { .nchans = 2, .enable = 0x4, .muxtype = pca954x_ismux, }, [pca_9543] = { .nchans = 2, .muxtype = pca954x_isswi, }, [pca_9544] = { .nchans = 4, .enable = 0x4, .muxtype = pca954x_ismux, }, [pca_9545] = { .nchans = 4, .muxtype = pca954x_isswi, }, [pca_9547] = { .nchans = 8, .enable = 0x8, .muxtype = pca954x_ismux, }, [pca_9548] = { .nchans = 8, .muxtype = pca954x_isswi, }, }; static const struct i2c_device_id pca954x_id[] = { { "pca9540", pca_9540 }, { "pca9542", pca_9540 }, { "pca9543", pca_9543 }, { "pca9544", pca_9544 }, { "pca9545", pca_9545 }, { "pca9546", pca_9545 }, { "pca9547", pca_9547 }, { "pca9548", pca_9548 }, { } }; MODULE_DEVICE_TABLE(i2c, pca954x_id); /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer() for this as they will try to lock adapter a second time */ static int pca954x_reg_write(struct i2c_adapter *adap, struct i2c_client *client, u8 val) { int ret = -ENODEV; if (adap->algo->master_xfer) { struct i2c_msg msg; char buf[1]; msg.addr = client->addr; msg.flags = 0; msg.len = 1; buf[0] = val; msg.buf = buf; |
0a8237ae3 i2c: mux: pca954x... |
136 |
ret = __i2c_transfer(adap, &msg, 1); |
7f528135d i2c: I2C bus mult... |
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
} else { union i2c_smbus_data data; ret = adap->algo->smbus_xfer(adap, client->addr, client->flags, I2C_SMBUS_WRITE, val, I2C_SMBUS_BYTE, &data); } return ret; } static int pca954x_select_chan(struct i2c_adapter *adap, void *client, u32 chan) { struct pca954x *data = i2c_get_clientdata(client); const struct chip_desc *chip = &chips[data->type]; u8 regval; int ret = 0; /* we make switches look like muxes, not sure how to be smarter */ if (chip->muxtype == pca954x_ismux) regval = chan | chip->enable; else regval = 1 << chan; /* Only select the channel if its different from the last channel */ if (data->last_chan != regval) { ret = pca954x_reg_write(adap, client, regval); data->last_chan = regval; } return ret; } static int pca954x_deselect_mux(struct i2c_adapter *adap, void *client, u32 chan) { struct pca954x *data = i2c_get_clientdata(client); /* Deselect active channel */ data->last_chan = 0; return pca954x_reg_write(adap, client, data->last_chan); } /* * I2C init/probing/exit functions */ |
db79f2a1d i2c/pca954x: Remo... |
184 185 |
static int pca954x_probe(struct i2c_client *client, const struct i2c_device_id *id) |
7f528135d i2c: I2C bus mult... |
186 187 |
{ struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); |
6d4028c64 i2c: use dev_get_... |
188 |
struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev); |
72f027157 of: i2c: Add i2c-... |
189 190 |
struct device_node *of_node = client->dev.of_node; bool idle_disconnect_dt; |
4807e8459 i2c: mux: pca954x... |
191 |
struct gpio_desc *gpio; |
eee543e82 i2c-mux: Add supp... |
192 |
int num, force, class; |
7f528135d i2c: I2C bus mult... |
193 |
struct pca954x *data; |
bc12cfc87 i2c: pca954x: Use... |
194 |
int ret; |
7f528135d i2c: I2C bus mult... |
195 196 |
if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) |
bc12cfc87 i2c: pca954x: Use... |
197 |
return -ENODEV; |
7f528135d i2c: I2C bus mult... |
198 |
|
bc12cfc87 i2c: pca954x: Use... |
199 200 201 |
data = devm_kzalloc(&client->dev, sizeof(struct pca954x), GFP_KERNEL); if (!data) return -ENOMEM; |
7f528135d i2c: I2C bus mult... |
202 203 |
i2c_set_clientdata(client, data); |
4807e8459 i2c: mux: pca954x... |
204 |
/* Get the mux out of reset if a reset GPIO is specified. */ |
58b59e0f2 i2c: pca954x: imp... |
205 206 207 |
gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_LOW); if (IS_ERR(gpio)) return PTR_ERR(gpio); |
12097957a i2c: pca954x: Add... |
208 |
|
cd823db8b i2c/pca954x: Init... |
209 210 211 |
/* Write the mux register at addr to verify * that the mux is in fact present. This also * initializes the mux to disconnected state. |
7f528135d i2c: I2C bus mult... |
212 |
*/ |
cd823db8b i2c/pca954x: Init... |
213 |
if (i2c_smbus_write_byte(client, 0) < 0) { |
7f528135d i2c: I2C bus mult... |
214 215 |
dev_warn(&client->dev, "probe failed "); |
bc12cfc87 i2c: pca954x: Use... |
216 |
return -ENODEV; |
7f528135d i2c: I2C bus mult... |
217 218 219 220 |
} data->type = id->driver_data; data->last_chan = 0; /* force the first selection */ |
72f027157 of: i2c: Add i2c-... |
221 222 |
idle_disconnect_dt = of_node && of_property_read_bool(of_node, "i2c-mux-idle-disconnect"); |
7f528135d i2c: I2C bus mult... |
223 224 |
/* Now create an adapter for each channel */ for (num = 0; num < chips[data->type].nchans; num++) { |
72f027157 of: i2c: Add i2c-... |
225 |
bool idle_disconnect_pd = false; |
7f528135d i2c: I2C bus mult... |
226 |
force = 0; /* dynamic adap number */ |
eee543e82 i2c-mux: Add supp... |
227 |
class = 0; /* no class by default */ |
7f528135d i2c: I2C bus mult... |
228 |
if (pdata) { |
eee543e82 i2c-mux: Add supp... |
229 |
if (num < pdata->num_modes) { |
7f528135d i2c: I2C bus mult... |
230 231 |
/* force static number */ force = pdata->modes[num].adap_id; |
eee543e82 i2c-mux: Add supp... |
232 233 |
class = pdata->modes[num].class; } else |
7f528135d i2c: I2C bus mult... |
234 235 |
/* discard unconfigured channels */ break; |
72f027157 of: i2c: Add i2c-... |
236 |
idle_disconnect_pd = pdata->modes[num].deselect_on_exit; |
7f528135d i2c: I2C bus mult... |
237 238 239 |
} data->virt_adaps[num] = |
5a3ecd5f9 i2c: Add a struct... |
240 |
i2c_add_mux_adapter(adap, &client->dev, client, |
eee543e82 i2c-mux: Add supp... |
241 |
force, num, class, pca954x_select_chan, |
72f027157 of: i2c: Add i2c-... |
242 |
(idle_disconnect_pd || idle_disconnect_dt) |
7f528135d i2c: I2C bus mult... |
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
? pca954x_deselect_mux : NULL); if (data->virt_adaps[num] == NULL) { ret = -ENODEV; dev_err(&client->dev, "failed to register multiplexed adapter" " %d as bus %d ", num, force); goto virt_reg_failed; } } dev_info(&client->dev, "registered %d multiplexed busses for I2C %s %s ", num, chips[data->type].muxtype == pca954x_ismux ? "mux" : "switch", client->name); return 0; virt_reg_failed: for (num--; num >= 0; num--) i2c_del_mux_adapter(data->virt_adaps[num]); |
7f528135d i2c: I2C bus mult... |
266 267 |
return ret; } |
db79f2a1d i2c/pca954x: Remo... |
268 |
static int pca954x_remove(struct i2c_client *client) |
7f528135d i2c: I2C bus mult... |
269 270 271 |
{ struct pca954x *data = i2c_get_clientdata(client); const struct chip_desc *chip = &chips[data->type]; |
a205e63d1 i2c: Ignore the r... |
272 |
int i; |
7f528135d i2c: I2C bus mult... |
273 274 275 |
for (i = 0; i < chip->nchans; ++i) if (data->virt_adaps[i]) { |
a205e63d1 i2c: Ignore the r... |
276 |
i2c_del_mux_adapter(data->virt_adaps[i]); |
7f528135d i2c: I2C bus mult... |
277 278 |
data->virt_adaps[i] = NULL; } |
7f528135d i2c: I2C bus mult... |
279 280 |
return 0; } |
f5e596cd9 i2c: pca954x: put... |
281 282 283 284 285 286 287 288 289 290 291 292 |
#ifdef CONFIG_PM_SLEEP static int pca954x_resume(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); struct pca954x *data = i2c_get_clientdata(client); data->last_chan = 0; return i2c_smbus_write_byte(client, 0); } #endif static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume); |
7f528135d i2c: I2C bus mult... |
293 294 295 |
static struct i2c_driver pca954x_driver = { .driver = { .name = "pca954x", |
f5e596cd9 i2c: pca954x: put... |
296 |
.pm = &pca954x_pm, |
7f528135d i2c: I2C bus mult... |
297 298 |
}, .probe = pca954x_probe, |
db79f2a1d i2c/pca954x: Remo... |
299 |
.remove = pca954x_remove, |
7f528135d i2c: I2C bus mult... |
300 301 |
.id_table = pca954x_id, }; |
de05497aa i2c: Convert driv... |
302 |
module_i2c_driver(pca954x_driver); |
7f528135d i2c: I2C bus mult... |
303 304 305 306 |
MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); MODULE_DESCRIPTION("PCA954x I2C mux/switch driver"); MODULE_LICENSE("GPL v2"); |