-
Notifications
You must be signed in to change notification settings - Fork 20
/
mlnx-mux-drv.c
414 lines (347 loc) · 12 KB
/
mlnx-mux-drv.c
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
36
37
38
39
40
41
42
43
44
45
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
136
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
*
* Copyright (C) 2010-2015, Mellanox Technologies Ltd. ALL RIGHTS RESERVED.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/i2c-mux.h>
#include <asm/io.h>
#include <linux/platform_device.h>
#include "mlnx-mux-drv.h"
#define MUX_DRV_DESCRIPTION "Mellanox MUX BSP driver. Build:" " "__DATE__" "__TIME__
#define MUX_DRV_VERSION "0.0.1 20/08/2014"
static int dbg_lvl = CPLD_MUX_DFLT_DBG_LVL;
module_param(dbg_lvl, int, 0644);
struct cpld_mux_common_data* cpld_mux_comm_data = NULL;
static const struct mux_desc muxes[] = {
[cpld_mux_tor] = {
.nchans = CPLD_MUX_MAX_NCHANS,
.muxtype = lpc_access,
},
[cpld_mux_mgmt] = {
.nchans = CPLD_MUX_MAX_NCHANS,
.muxtype = lpc_access,
},
[cpld_mux_mgmt_ext] = {
.nchans = CPLD_MUX_EXT_MAX_NCHANS,
.muxtype = lpc_access,
},
[cpld_mux_module] = {
.nchans = CPLD_MUX_MAX_NCHANS,
.muxtype = i2c_access,
},
};
static const struct i2c_device_id cpld_mux_id[] = {
{ "cpld_mux_tor", cpld_mux_tor },
{ "cpld_mux_mgmt", cpld_mux_mgmt },
{ "cpld_mux_mgmt_ext", cpld_mux_mgmt_ext },
{ "cpld_mux_module", cpld_mux_module },
{ }
};
MODULE_DEVICE_TABLE(i2c, cpld_mux_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 cpld_mux_reg_write(struct i2c_adapter *adap,
struct i2c_client *client, u8 val, enum mux_type muxtype)
{
int ret = -ENODEV;
struct cpld_mux_platform_data *pdata = dev_get_platdata(&client->dev);
if (muxtype == lpc_access) {
outb(val, pdata->addr); // Addr = CPLD base + offset
CPLD_MUX_LOG_DBG(4, &client->dev, "LPC Write reg 0x%x val %d\n", pdata->addr, val);
ret = 1;
}
else if (muxtype == i2c_access) {
if (adap->algo->master_xfer) {
struct i2c_msg msg;
u8 msgbuf[] = {pdata->sel_reg_addr, val};
msg.addr = pdata->addr;
msg.flags = 0;
msg.len = 2;
msg.buf = msgbuf;
ret = adap->algo->master_xfer(adap, &msg, 1);
} else {
CPLD_MUX_LOG_ERROR(&client->dev,
"SMBus isn't supported on this adapter\n");
}
}
else
CPLD_MUX_LOG_ERROR(&client->dev, "Incorrect muxtype %d\n", muxtype);
return ret;
}
static int cpld_mux_select_chan(struct i2c_adapter *adap,
void *client, u32 chan)
{
struct cpld_mux *data = i2c_get_clientdata(client);
const struct mux_desc *mux = &muxes[data->type];
u8 regval;
int ret = 0;
struct i2c_client* cl = (struct i2c_client*)client;
struct cpld_mux_platform_data *pdata = dev_get_platdata(&cl->dev);
switch(data->type) {
case cpld_mux_tor:
regval = pdata->first_channel + chan;
break;
case cpld_mux_mgmt:
case cpld_mux_mgmt_ext:
case cpld_mux_module:
regval = chan + 1;
break;
default:
return -ENXIO;
break;
}
CPLD_MUX_LOG_DBG(3, &cl->dev, "%s client %s, type %d, select channel val %d, 1st chan %d, leg %d\n", \
cl->dev.kobj.name, cl->name, data->type, regval, pdata->first_channel, chan);
/* Only select the channel if its different from the last channel */
if (data->last_chan != regval) {
ret = cpld_mux_reg_write(adap, client, regval, mux->muxtype);
data->last_chan = regval;
}
return ret;
}
static int cpld_mux_deselect_mux(struct i2c_adapter *adap,
void *client, u32 chan)
{
struct cpld_mux *data = i2c_get_clientdata(client);
const struct mux_desc *mux = &muxes[data->type];
if (dbg_lvl >= 2){
//struct i2c_client* cl = (struct i2c_client*)client;
//struct cpld_mux_platform_data *pdata = dev_get_platdata(&cl->dev);
CPLD_MUX_LOG_DBG(3, &cl->dev, "%s client %s, type %d, deselect channel, 1st chan %d, leg %d, \n", \
cl->dev.kobj.name, cl->name, data->type, pdata->first_channel, chan);
}
/* Deselect active channel */
data->last_chan = 0;
return cpld_mux_reg_write(adap, client, data->last_chan, mux->muxtype);
}
ssize_t show_channel(struct device *dev, struct device_attribute *attr, char *buf)
{
ssize_t rc;
struct i2c_client *client;
struct cpld_mux_platform_data* pdata;
struct cpld_mux *data;
pdata = dev_get_platdata(dev);
client = container_of(dev, struct i2c_client, dev);
data = i2c_get_clientdata(client);
rc = sprintf(buf, "CPLD_MUX 0x%x last selected channel: %d\n", \
pdata->id, data->last_chan);
return rc+1;
}
ssize_t store_channel(struct device *dev, struct device_attribute *attr, const char *buf, size_t cnt)
{
int rc;
u32 chan, prev_chan;
struct i2c_client *client;
struct i2c_adapter *adap;
struct cpld_mux_platform_data* pdata;
struct cpld_mux *data;
pdata = dev_get_platdata(dev);
client = container_of(dev, struct i2c_client, dev);
if (!buf || cnt == 1) {
CPLD_MUX_LOG_ERROR(&client->dev, "Incorrect empty str input\n");
return cnt;
}
data = i2c_get_clientdata(client);
adap = to_i2c_adapter(&(client->dev));
prev_chan = data->last_chan;
chan = (u32)simple_strtol(buf, NULL, 10);
rc = cpld_mux_select_chan(adap, client, chan);
if (rc < 0) {
CPLD_MUX_LOG_DBG(1, &client->dev, "0x%x change channel %d failed\n", \
pdata->id, chan);
}
else {
CPLD_MUX_LOG_DBG(1, &client->dev, "0x%x last selected channel changed from %d to %d\n", \
pdata->id, prev_chan, chan);
}
return cnt;
}
static DEVICE_ATTR(channel, 0644, show_channel, store_channel);
static struct attribute *cpld_mux_attrs[] = {
&dev_attr_channel.attr,
NULL
};
static struct attribute_group cpld_mux_attr_grp = {
.attrs = cpld_mux_attrs
};
int cpld_mux_sysfs_device_create(struct device* dev)
{
struct cpld_mux_platform_data* pdata = dev_get_platdata(dev);
if (sysfs_create_group(&(dev->kobj), &cpld_mux_attr_grp)) {
CPLD_MUX_LOG_ERROR(dev, "Failed to create sysfs cpld_mux group\n");
pdata->mux_kobj = NULL;
return -EACCES;
}
pdata->mux_kobj = &(dev->kobj);
return 0;
}
void cpld_mux_sysfs_device_delete(struct cpld_mux_platform_data *pdata)
{
if (pdata->mux_kobj)
sysfs_remove_group(pdata->mux_kobj, &cpld_mux_attr_grp);
pdata->mux_kobj = NULL;
}
/*
* I2C init/probing/exit functions
*/
static int cpld_mux_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
struct cpld_mux_platform_data *pdata = dev_get_platdata(&client->dev);
int num, force;
struct cpld_mux *data;
int ret = -ENODEV;
CPLD_MUX_LOG_DBG(2, &client->dev, "client %s probe\n", client->name);
if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
goto err;
data = kzalloc(sizeof(struct cpld_mux), GFP_KERNEL);
if (!data) {
ret = -ENOMEM;
goto err;
}
i2c_set_clientdata(client, data);
data->type = id->driver_data;
data->last_chan = 0; /* force the first selection */
/* Only in cpld_mux_tor first_channel can be different.
* In other cpld_mux types channel numbering begin from 1 */
/*if (data->type != cpld_mux_tor)
pdata->first_channel = 1;*/
/* Now create an adapter for each channel */
for (num = 0; num < muxes[data->type].nchans; num++) {
CPLD_MUX_LOG_DBG(3, &adap->dev,"%s name %s nr=%d num=%d (%d)", __FUNCTION__,
adap->name, adap->nr, num, pdata->num_modes);
force = 0; /* dynamic adap number */
// class = 0; /* no class by default */
if (pdata) {
if (num < pdata->num_modes) {
/* force static number */
/* force = pdata->modes[num].adap_id; */
force = pdata->first_channel + num;
// class = pdata->modes[num].class;
} else
/* discard unconfigured channels */
break;
}
data->virt_adaps[num] =
i2c_add_mux_adapter(adap, &client->dev, client,
force, num, 0, cpld_mux_select_chan,
(pdata && pdata->modes[num].deselect_on_exit)
? cpld_mux_deselect_mux : NULL);
if (data->virt_adaps[num] == NULL) {
ret = -ENODEV;
CPLD_MUX_LOG_ERROR(&client->dev,
"failed to register multiplexed adapter"
" %d as bus %d\n", num, force);
goto virt_reg_failed;
}
else {
CPLD_MUX_LOG_DBG(2, &client->dev, "Added i2c addapter, num %d, deselect %s, force %d, name %s, parent name %s\n", \
num, (pdata && pdata->modes[num].deselect_on_exit) ? "yes" : "no", \
force, data->virt_adaps[num]->name, adap->name);
}
}
if (cpld_mux_sysfs_device_create(&(client->dev))) {
ret = -ENODEV;
CPLD_MUX_LOG_ERROR(&client->dev, "failed create sysfs for cpld_mux_%x\n", pdata->id);
goto virt_reg_failed;
}
else {
//list_add(&(pdata->dev_list), &(cpld_mux_comm_data->head_list));
}
CPLD_MUX_LOG_DBG(1, &client->dev, "Registered %d multiplexed busses for %s, id 0x%x\n", num, client->name, pdata->id);
return 0;
virt_reg_failed:
for (num--; num >= 0; num--)
i2c_del_mux_adapter(data->virt_adaps[num]);
kfree(data);
err:
kfree(pdata);
return ret;
}
static int cpld_mux_remove(struct i2c_client *client)
{
struct cpld_mux *data = i2c_get_clientdata(client);
const struct mux_desc *mux = &muxes[data->type];
struct cpld_mux_platform_data *pdata = dev_get_platdata(&client->dev);
int num;
CPLD_MUX_LOG_DBG(2, &client->dev, "client %s remove\n", client->name);
for (num = 0; num < mux->nchans; ++num) {
if (data->virt_adaps[num]) {
CPLD_MUX_LOG_DBG(2, &client->dev, "%s chan %d, adapter %s\n", __FUNCTION__, num, data->virt_adaps[num]->name);
i2c_del_mux_adapter(data->virt_adaps[num]);
data->virt_adaps[num] = NULL;
}
}
CPLD_MUX_LOG_DBG(1, &client->dev, "Unregistered %d multiplexed busses for %s, id 0x%x\n", num, client->name, pdata->id);
cpld_mux_sysfs_device_delete(pdata);
if (!list_empty(&cpld_mux_comm_data->head_list)) {
//list_del_rcu(&pdata->dev_list);
kfree(pdata);
}
kfree(data);
return 0;
}
static struct i2c_driver cpld_mux_driver = {
.driver = {
.name = "mlnx-mux-drv",
.owner = THIS_MODULE,
},
.probe = cpld_mux_probe,
.remove = cpld_mux_remove,
.id_table = cpld_mux_id,
};
static int __init cpld_mux_init(void)
{
int rc;
cpld_mux_comm_data = kzalloc(sizeof(struct cpld_mux_common_data), GFP_KERNEL);
if (!cpld_mux_comm_data) {
CPLD_MUX_LOG_ERROR(NULL, "Alloc cpld_mux_common_data failed\n");
return -ENOMEM;
}
INIT_LIST_HEAD(&(cpld_mux_comm_data->head_list));
rc = i2c_add_driver(&cpld_mux_driver);
if (rc) {
CPLD_MUX_LOG_ERROR(NULL, "Add %s i2c driver failed (%d)\n", CPLD_MUX_DEVICE_NAME, rc);
kfree(cpld_mux_comm_data);
return rc;
}
printk(KERN_INFO "%s Version %s\n", MUX_DRV_DESCRIPTION, MUX_DRV_VERSION);
return rc;
}
static void __exit cpld_mux_exit(void)
{
struct cpld_mux_platform_data* curr, *next;
list_for_each_entry_safe(curr, next, &(cpld_mux_comm_data->head_list), dev_list) {
cpld_mux_sysfs_device_delete(curr);
}
kfree(cpld_mux_comm_data);
i2c_del_driver(&cpld_mux_driver);
}
module_init(cpld_mux_init);
module_exit(cpld_mux_exit);
MODULE_AUTHOR("Mellanox Technologies (Michael Shych)");
MODULE_DESCRIPTION(MUX_DRV_DESCRIPTION);
MODULE_VERSION(MUX_DRV_VERSION);
MODULE_LICENSE("GPL v2");
MODULE_PARM_DESC(dbg_lvl, " debug level, 0-4, 0 - disabled, 1 - default");