2 * drivers/i2c/muxes/i2c-mux-mlxcpld.c
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2016 Michael Shych <michaels@mellanox.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the names of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/device.h>
36 #include <linux/i2c.h>
37 #include <linux/i2c-mux.h>
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/platform_data/x86/mlxcpld.h>
42 #include <linux/platform_device.h>
43 #include <linux/slab.h>
45 #define CPLD_MUX_MAX_NCHANS 8
47 /* mlxcpld_mux - mux control structure:
48 * @last_chan - last register value
49 * @client - I2C device client
53 struct i2c_client
*client
;
56 /* MUX logic description.
57 * Driver can support different mux control logic, according to CPLD
60 * Connectivity schema.
62 * i2c-mlxcpld Digital Analog
64 * *--------* * -> mux1 (virt bus2) -> mux -> |
65 * | I2CLPC | i2c physical * -> mux2 (virt bus3) -> mux -> |
66 * | bridge | bus 1 *---------* |
67 * | logic |---------------------> * mux reg * |
68 * | in CPLD| *---------* |
69 * *--------* i2c-mux-mlxpcld ^ * -> muxn (virt busn) -> mux -> |
71 * | *---------------* | Devices
72 * | * CPLD (i2c bus)* select |
73 * | * registers for *--------*
74 * | * mux selection * deselect
77 * <--------> <----------->
78 * i2c cntrl Board cntrl reg
79 * reg space space (mux select,
84 static const struct i2c_device_id mlxcpld_mux_id
[] = {
85 { "mlxcpld_mux_module", 0 },
88 MODULE_DEVICE_TABLE(i2c
, mlxcpld_mux_id
);
90 /* Write to mux register. Don't use i2c_transfer() and i2c_smbus_xfer()
91 * for this as they will try to lock adapter a second time.
93 static int mlxcpld_mux_reg_write(struct i2c_adapter
*adap
,
94 struct i2c_client
*client
, u8 val
)
96 struct mlxcpld_mux_plat_data
*pdata
= dev_get_platdata(&client
->dev
);
97 union i2c_smbus_data data
= { .byte
= val
};
99 return __i2c_smbus_xfer(adap
, client
->addr
, client
->flags
,
100 I2C_SMBUS_WRITE
, pdata
->sel_reg_addr
,
101 I2C_SMBUS_BYTE_DATA
, &data
);
104 static int mlxcpld_mux_select_chan(struct i2c_mux_core
*muxc
, u32 chan
)
106 struct mlxcpld_mux
*data
= i2c_mux_priv(muxc
);
107 struct i2c_client
*client
= data
->client
;
108 u8 regval
= chan
+ 1;
111 /* Only select the channel if its different from the last channel */
112 if (data
->last_chan
!= regval
) {
113 err
= mlxcpld_mux_reg_write(muxc
->parent
, client
, regval
);
114 data
->last_chan
= err
< 0 ? 0 : regval
;
120 static int mlxcpld_mux_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
122 struct mlxcpld_mux
*data
= i2c_mux_priv(muxc
);
123 struct i2c_client
*client
= data
->client
;
125 /* Deselect active channel */
128 return mlxcpld_mux_reg_write(muxc
->parent
, client
, data
->last_chan
);
131 /* Probe/reomove functions */
132 static int mlxcpld_mux_probe(struct i2c_client
*client
,
133 const struct i2c_device_id
*id
)
135 struct i2c_adapter
*adap
= client
->adapter
;
136 struct mlxcpld_mux_plat_data
*pdata
= dev_get_platdata(&client
->dev
);
137 struct i2c_mux_core
*muxc
;
139 struct mlxcpld_mux
*data
;
145 if (!i2c_check_functionality(adap
, I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
148 muxc
= i2c_mux_alloc(adap
, &client
->dev
, CPLD_MUX_MAX_NCHANS
,
149 sizeof(*data
), 0, mlxcpld_mux_select_chan
,
150 mlxcpld_mux_deselect
);
154 data
= i2c_mux_priv(muxc
);
155 i2c_set_clientdata(client
, muxc
);
156 data
->client
= client
;
157 data
->last_chan
= 0; /* force the first selection */
159 /* Create an adapter for each channel. */
160 for (num
= 0; num
< CPLD_MUX_MAX_NCHANS
; num
++) {
161 if (num
>= pdata
->num_adaps
)
162 /* discard unconfigured channels */
165 force
= pdata
->adap_ids
[num
];
167 err
= i2c_mux_add_adapter(muxc
, force
, num
, 0);
169 goto virt_reg_failed
;
175 i2c_mux_del_adapters(muxc
);
179 static int mlxcpld_mux_remove(struct i2c_client
*client
)
181 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
183 i2c_mux_del_adapters(muxc
);
187 static struct i2c_driver mlxcpld_mux_driver
= {
189 .name
= "mlxcpld-mux",
191 .probe
= mlxcpld_mux_probe
,
192 .remove
= mlxcpld_mux_remove
,
193 .id_table
= mlxcpld_mux_id
,
196 module_i2c_driver(mlxcpld_mux_driver
);
198 MODULE_AUTHOR("Michael Shych (michaels@mellanox.com)");
199 MODULE_DESCRIPTION("Mellanox I2C-CPLD-MUX driver");
200 MODULE_LICENSE("Dual BSD/GPL");
201 MODULE_ALIAS("platform:i2c-mux-mlxcpld");