This source file includes following definitions.
- mlxcpld_mux_reg_write
- mlxcpld_mux_select_chan
- mlxcpld_mux_deselect
- mlxcpld_mux_probe
- mlxcpld_mux_remove
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
51 struct 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
84 static const struct i2c_device_id mlxcpld_mux_id[] = {
85 { "mlxcpld_mux_module", 0 },
86 { }
87 };
88 MODULE_DEVICE_TABLE(i2c, mlxcpld_mux_id);
89
90
91
92
93 static 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 union i2c_smbus_data data = { .byte = val };
98
99 return __i2c_smbus_xfer(adap, client->addr, client->flags,
100 I2C_SMBUS_WRITE, pdata->sel_reg_addr,
101 I2C_SMBUS_BYTE_DATA, &data);
102 }
103
104 static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
105 {
106 struct mlxcpld_mux *data = i2c_mux_priv(muxc);
107 struct i2c_client *client = data->client;
108 u8 regval = chan + 1;
109 int err = 0;
110
111
112 if (data->last_chan != regval) {
113 err = mlxcpld_mux_reg_write(muxc->parent, client, regval);
114 data->last_chan = err < 0 ? 0 : regval;
115 }
116
117 return err;
118 }
119
120 static int mlxcpld_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
121 {
122 struct mlxcpld_mux *data = i2c_mux_priv(muxc);
123 struct i2c_client *client = data->client;
124
125
126 data->last_chan = 0;
127
128 return mlxcpld_mux_reg_write(muxc->parent, client, data->last_chan);
129 }
130
131
132 static int mlxcpld_mux_probe(struct i2c_client *client,
133 const struct i2c_device_id *id)
134 {
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;
138 int num, force;
139 struct mlxcpld_mux *data;
140 int err;
141
142 if (!pdata)
143 return -EINVAL;
144
145 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
146 return -ENODEV;
147
148 muxc = i2c_mux_alloc(adap, &client->dev, CPLD_MUX_MAX_NCHANS,
149 sizeof(*data), 0, mlxcpld_mux_select_chan,
150 mlxcpld_mux_deselect);
151 if (!muxc)
152 return -ENOMEM;
153
154 data = i2c_mux_priv(muxc);
155 i2c_set_clientdata(client, muxc);
156 data->client = client;
157 data->last_chan = 0;
158
159
160 for (num = 0; num < CPLD_MUX_MAX_NCHANS; num++) {
161 if (num >= pdata->num_adaps)
162
163 break;
164
165 force = pdata->adap_ids[num];
166
167 err = i2c_mux_add_adapter(muxc, force, num, 0);
168 if (err)
169 goto virt_reg_failed;
170 }
171
172 return 0;
173
174 virt_reg_failed:
175 i2c_mux_del_adapters(muxc);
176 return err;
177 }
178
179 static int mlxcpld_mux_remove(struct i2c_client *client)
180 {
181 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
182
183 i2c_mux_del_adapters(muxc);
184 return 0;
185 }
186
187 static struct i2c_driver mlxcpld_mux_driver = {
188 .driver = {
189 .name = "mlxcpld-mux",
190 },
191 .probe = mlxcpld_mux_probe,
192 .remove = mlxcpld_mux_remove,
193 .id_table = mlxcpld_mux_id,
194 };
195
196 module_i2c_driver(mlxcpld_mux_driver);
197
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");