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
31
32
33
34
35#include <linux/device.h>
36#include <linux/i2c.h>
37#include <linux/i2c-mux.h>
38#include <linux/io.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>
44
45#define CPLD_MUX_MAX_NCHANS 8
46
47
48
49
50
51struct mlxcpld_mux {
52 u8 last_chan;
53 struct i2c_client *client;
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
84static const struct i2c_device_id mlxcpld_mux_id[] = {
85 { "mlxcpld_mux_module", 0 },
86 { }
87};
88MODULE_DEVICE_TABLE(i2c, mlxcpld_mux_id);
89
90
91
92
93static int mlxcpld_mux_reg_write(struct i2c_adapter *adap,
94 struct i2c_client *client, u8 val)
95{
96 struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&client->dev);
97 int ret = -ENODEV;
98
99 if (adap->algo->master_xfer) {
100 struct i2c_msg msg;
101 u8 msgbuf[] = {pdata->sel_reg_addr, val};
102
103 msg.addr = client->addr;
104 msg.flags = 0;
105 msg.len = 2;
106 msg.buf = msgbuf;
107 ret = __i2c_transfer(adap, &msg, 1);
108
109 if (ret >= 0 && ret != 1)
110 ret = -EREMOTEIO;
111 } else if (adap->algo->smbus_xfer) {
112 union i2c_smbus_data data;
113
114 data.byte = val;
115 ret = adap->algo->smbus_xfer(adap, client->addr,
116 client->flags, I2C_SMBUS_WRITE,
117 pdata->sel_reg_addr,
118 I2C_SMBUS_BYTE_DATA, &data);
119 }
120
121 return ret;
122}
123
124static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
125{
126 struct mlxcpld_mux *data = i2c_mux_priv(muxc);
127 struct i2c_client *client = data->client;
128 u8 regval = chan + 1;
129 int err = 0;
130
131
132 if (data->last_chan != regval) {
133 err = mlxcpld_mux_reg_write(muxc->parent, client, regval);
134 data->last_chan = err < 0 ? 0 : regval;
135 }
136
137 return err;
138}
139
140static int mlxcpld_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
141{
142 struct mlxcpld_mux *data = i2c_mux_priv(muxc);
143 struct i2c_client *client = data->client;
144
145
146 data->last_chan = 0;
147
148 return mlxcpld_mux_reg_write(muxc->parent, client, data->last_chan);
149}
150
151
152static int mlxcpld_mux_probe(struct i2c_client *client,
153 const struct i2c_device_id *id)
154{
155 struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
156 struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&client->dev);
157 struct i2c_mux_core *muxc;
158 int num, force;
159 struct mlxcpld_mux *data;
160 int err;
161
162 if (!pdata)
163 return -EINVAL;
164
165 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
166 return -ENODEV;
167
168 muxc = i2c_mux_alloc(adap, &client->dev, CPLD_MUX_MAX_NCHANS,
169 sizeof(*data), 0, mlxcpld_mux_select_chan,
170 mlxcpld_mux_deselect);
171 if (!muxc)
172 return -ENOMEM;
173
174 data = i2c_mux_priv(muxc);
175 i2c_set_clientdata(client, muxc);
176 data->client = client;
177 data->last_chan = 0;
178
179
180 for (num = 0; num < CPLD_MUX_MAX_NCHANS; num++) {
181 if (num >= pdata->num_adaps)
182
183 break;
184
185 force = pdata->adap_ids[num];
186
187 err = i2c_mux_add_adapter(muxc, force, num, 0);
188 if (err)
189 goto virt_reg_failed;
190 }
191
192 return 0;
193
194virt_reg_failed:
195 i2c_mux_del_adapters(muxc);
196 return err;
197}
198
199static int mlxcpld_mux_remove(struct i2c_client *client)
200{
201 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
202
203 i2c_mux_del_adapters(muxc);
204 return 0;
205}
206
207static struct i2c_driver mlxcpld_mux_driver = {
208 .driver = {
209 .name = "mlxcpld-mux",
210 },
211 .probe = mlxcpld_mux_probe,
212 .remove = mlxcpld_mux_remove,
213 .id_table = mlxcpld_mux_id,
214};
215
216module_i2c_driver(mlxcpld_mux_driver);
217
218MODULE_AUTHOR("Michael Shych (michaels@mellanox.com)");
219MODULE_DESCRIPTION("Mellanox I2C-CPLD-MUX driver");
220MODULE_LICENSE("Dual BSD/GPL");
221MODULE_ALIAS("platform:i2c-mux-mlxcpld");
222