1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * I2C bridge driver for the Greybus "generic" I2C module.
4 *
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13
14 #include "greybus.h"
15 #include "gbphy.h"
16
17 struct gb_i2c_device {
18 struct gb_connection *connection;
19 struct gbphy_device *gbphy_dev;
20
21 u32 functionality;
22
23 struct i2c_adapter adapter;
24 };
25
26 /*
27 * Map Greybus i2c functionality bits into Linux ones
28 */
gb_i2c_functionality_map(u32 gb_i2c_functionality)29 static u32 gb_i2c_functionality_map(u32 gb_i2c_functionality)
30 {
31 return gb_i2c_functionality; /* All bits the same for now */
32 }
33
gb_i2c_functionality_operation(struct gb_i2c_device * gb_i2c_dev)34 static int gb_i2c_functionality_operation(struct gb_i2c_device *gb_i2c_dev)
35 {
36 struct gb_i2c_functionality_response response;
37 u32 functionality;
38 int ret;
39
40 ret = gb_operation_sync(gb_i2c_dev->connection,
41 GB_I2C_TYPE_FUNCTIONALITY,
42 NULL, 0, &response, sizeof(response));
43 if (ret)
44 return ret;
45
46 functionality = le32_to_cpu(response.functionality);
47 gb_i2c_dev->functionality = gb_i2c_functionality_map(functionality);
48
49 return 0;
50 }
51
52 /*
53 * Map Linux i2c_msg flags into Greybus i2c transfer op flags.
54 */
gb_i2c_transfer_op_flags_map(u16 flags)55 static u16 gb_i2c_transfer_op_flags_map(u16 flags)
56 {
57 return flags; /* All flags the same for now */
58 }
59
60 static void
gb_i2c_fill_transfer_op(struct gb_i2c_transfer_op * op,struct i2c_msg * msg)61 gb_i2c_fill_transfer_op(struct gb_i2c_transfer_op *op, struct i2c_msg *msg)
62 {
63 u16 flags = gb_i2c_transfer_op_flags_map(msg->flags);
64
65 op->addr = cpu_to_le16(msg->addr);
66 op->flags = cpu_to_le16(flags);
67 op->size = cpu_to_le16(msg->len);
68 }
69
70 static struct gb_operation *
gb_i2c_operation_create(struct gb_connection * connection,struct i2c_msg * msgs,u32 msg_count)71 gb_i2c_operation_create(struct gb_connection *connection,
72 struct i2c_msg *msgs, u32 msg_count)
73 {
74 struct gb_i2c_device *gb_i2c_dev = gb_connection_get_data(connection);
75 struct gb_i2c_transfer_request *request;
76 struct gb_operation *operation;
77 struct gb_i2c_transfer_op *op;
78 struct i2c_msg *msg;
79 u32 data_out_size = 0;
80 u32 data_in_size = 0;
81 size_t request_size;
82 void *data;
83 u16 op_count;
84 u32 i;
85
86 if (msg_count > (u32)U16_MAX) {
87 dev_err(&gb_i2c_dev->gbphy_dev->dev, "msg_count (%u) too big\n",
88 msg_count);
89 return NULL;
90 }
91 op_count = (u16)msg_count;
92
93 /*
94 * In addition to space for all message descriptors we need
95 * to have enough to hold all outbound message data.
96 */
97 msg = msgs;
98 for (i = 0; i < msg_count; i++, msg++)
99 if (msg->flags & I2C_M_RD)
100 data_in_size += (u32)msg->len;
101 else
102 data_out_size += (u32)msg->len;
103
104 request_size = sizeof(*request);
105 request_size += msg_count * sizeof(*op);
106 request_size += data_out_size;
107
108 /* Response consists only of incoming data */
109 operation = gb_operation_create(connection, GB_I2C_TYPE_TRANSFER,
110 request_size, data_in_size, GFP_KERNEL);
111 if (!operation)
112 return NULL;
113
114 request = operation->request->payload;
115 request->op_count = cpu_to_le16(op_count);
116 /* Fill in the ops array */
117 op = &request->ops[0];
118 msg = msgs;
119 for (i = 0; i < msg_count; i++)
120 gb_i2c_fill_transfer_op(op++, msg++);
121
122 if (!data_out_size)
123 return operation;
124
125 /* Copy over the outgoing data; it starts after the last op */
126 data = op;
127 msg = msgs;
128 for (i = 0; i < msg_count; i++) {
129 if (!(msg->flags & I2C_M_RD)) {
130 memcpy(data, msg->buf, msg->len);
131 data += msg->len;
132 }
133 msg++;
134 }
135
136 return operation;
137 }
138
gb_i2c_decode_response(struct i2c_msg * msgs,u32 msg_count,struct gb_i2c_transfer_response * response)139 static void gb_i2c_decode_response(struct i2c_msg *msgs, u32 msg_count,
140 struct gb_i2c_transfer_response *response)
141 {
142 struct i2c_msg *msg = msgs;
143 u8 *data;
144 u32 i;
145
146 if (!response)
147 return;
148 data = response->data;
149 for (i = 0; i < msg_count; i++) {
150 if (msg->flags & I2C_M_RD) {
151 memcpy(msg->buf, data, msg->len);
152 data += msg->len;
153 }
154 msg++;
155 }
156 }
157
158 /*
159 * Some i2c transfer operations return results that are expected.
160 */
gb_i2c_expected_transfer_error(int errno)161 static bool gb_i2c_expected_transfer_error(int errno)
162 {
163 return errno == -EAGAIN || errno == -ENODEV;
164 }
165
gb_i2c_transfer_operation(struct gb_i2c_device * gb_i2c_dev,struct i2c_msg * msgs,u32 msg_count)166 static int gb_i2c_transfer_operation(struct gb_i2c_device *gb_i2c_dev,
167 struct i2c_msg *msgs, u32 msg_count)
168 {
169 struct gb_connection *connection = gb_i2c_dev->connection;
170 struct device *dev = &gb_i2c_dev->gbphy_dev->dev;
171 struct gb_operation *operation;
172 int ret;
173
174 operation = gb_i2c_operation_create(connection, msgs, msg_count);
175 if (!operation)
176 return -ENOMEM;
177
178 ret = gbphy_runtime_get_sync(gb_i2c_dev->gbphy_dev);
179 if (ret)
180 goto exit_operation_put;
181
182 ret = gb_operation_request_send_sync(operation);
183 if (!ret) {
184 struct gb_i2c_transfer_response *response;
185
186 response = operation->response->payload;
187 gb_i2c_decode_response(msgs, msg_count, response);
188 ret = msg_count;
189 } else if (!gb_i2c_expected_transfer_error(ret)) {
190 dev_err(dev, "transfer operation failed (%d)\n", ret);
191 }
192
193 gbphy_runtime_put_autosuspend(gb_i2c_dev->gbphy_dev);
194
195 exit_operation_put:
196 gb_operation_put(operation);
197
198 return ret;
199 }
200
gb_i2c_master_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int msg_count)201 static int gb_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
202 int msg_count)
203 {
204 struct gb_i2c_device *gb_i2c_dev;
205
206 gb_i2c_dev = i2c_get_adapdata(adap);
207
208 return gb_i2c_transfer_operation(gb_i2c_dev, msgs, msg_count);
209 }
210
211 #if 0
212 /* Later */
213 static int gb_i2c_smbus_xfer(struct i2c_adapter *adap,
214 u16 addr, unsigned short flags, char read_write,
215 u8 command, int size, union i2c_smbus_data *data)
216 {
217 struct gb_i2c_device *gb_i2c_dev;
218
219 gb_i2c_dev = i2c_get_adapdata(adap);
220
221 return 0;
222 }
223 #endif
224
gb_i2c_functionality(struct i2c_adapter * adap)225 static u32 gb_i2c_functionality(struct i2c_adapter *adap)
226 {
227 struct gb_i2c_device *gb_i2c_dev = i2c_get_adapdata(adap);
228
229 return gb_i2c_dev->functionality;
230 }
231
232 static const struct i2c_algorithm gb_i2c_algorithm = {
233 .master_xfer = gb_i2c_master_xfer,
234 /* .smbus_xfer = gb_i2c_smbus_xfer, */
235 .functionality = gb_i2c_functionality,
236 };
237
238 /*
239 * Do initial setup of the i2c device. This includes verifying we
240 * can support it (based on the protocol version it advertises).
241 * If that's OK, we get and cached its functionality bits.
242 *
243 * Note: gb_i2c_dev->connection is assumed to have been valid.
244 */
gb_i2c_device_setup(struct gb_i2c_device * gb_i2c_dev)245 static int gb_i2c_device_setup(struct gb_i2c_device *gb_i2c_dev)
246 {
247 /* Assume the functionality never changes, just get it once */
248 return gb_i2c_functionality_operation(gb_i2c_dev);
249 }
250
gb_i2c_probe(struct gbphy_device * gbphy_dev,const struct gbphy_device_id * id)251 static int gb_i2c_probe(struct gbphy_device *gbphy_dev,
252 const struct gbphy_device_id *id)
253 {
254 struct gb_connection *connection;
255 struct gb_i2c_device *gb_i2c_dev;
256 struct i2c_adapter *adapter;
257 int ret;
258
259 gb_i2c_dev = kzalloc(sizeof(*gb_i2c_dev), GFP_KERNEL);
260 if (!gb_i2c_dev)
261 return -ENOMEM;
262
263 connection = gb_connection_create(gbphy_dev->bundle,
264 le16_to_cpu(gbphy_dev->cport_desc->id),
265 NULL);
266 if (IS_ERR(connection)) {
267 ret = PTR_ERR(connection);
268 goto exit_i2cdev_free;
269 }
270
271 gb_i2c_dev->connection = connection;
272 gb_connection_set_data(connection, gb_i2c_dev);
273 gb_i2c_dev->gbphy_dev = gbphy_dev;
274 gb_gbphy_set_data(gbphy_dev, gb_i2c_dev);
275
276 ret = gb_connection_enable(connection);
277 if (ret)
278 goto exit_connection_destroy;
279
280 ret = gb_i2c_device_setup(gb_i2c_dev);
281 if (ret)
282 goto exit_connection_disable;
283
284 /* Looks good; up our i2c adapter */
285 adapter = &gb_i2c_dev->adapter;
286 adapter->owner = THIS_MODULE;
287 adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
288 adapter->algo = &gb_i2c_algorithm;
289 /* adapter->algo_data = what? */
290
291 adapter->dev.parent = &gbphy_dev->dev;
292 snprintf(adapter->name, sizeof(adapter->name), "Greybus i2c adapter");
293 i2c_set_adapdata(adapter, gb_i2c_dev);
294
295 ret = i2c_add_adapter(adapter);
296 if (ret)
297 goto exit_connection_disable;
298
299 gbphy_runtime_put_autosuspend(gbphy_dev);
300 return 0;
301
302 exit_connection_disable:
303 gb_connection_disable(connection);
304 exit_connection_destroy:
305 gb_connection_destroy(connection);
306 exit_i2cdev_free:
307 kfree(gb_i2c_dev);
308
309 return ret;
310 }
311
gb_i2c_remove(struct gbphy_device * gbphy_dev)312 static void gb_i2c_remove(struct gbphy_device *gbphy_dev)
313 {
314 struct gb_i2c_device *gb_i2c_dev = gb_gbphy_get_data(gbphy_dev);
315 struct gb_connection *connection = gb_i2c_dev->connection;
316 int ret;
317
318 ret = gbphy_runtime_get_sync(gbphy_dev);
319 if (ret)
320 gbphy_runtime_get_noresume(gbphy_dev);
321
322 i2c_del_adapter(&gb_i2c_dev->adapter);
323 gb_connection_disable(connection);
324 gb_connection_destroy(connection);
325 kfree(gb_i2c_dev);
326 }
327
328 static const struct gbphy_device_id gb_i2c_id_table[] = {
329 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_I2C) },
330 { },
331 };
332 MODULE_DEVICE_TABLE(gbphy, gb_i2c_id_table);
333
334 static struct gbphy_driver i2c_driver = {
335 .name = "i2c",
336 .probe = gb_i2c_probe,
337 .remove = gb_i2c_remove,
338 .id_table = gb_i2c_id_table,
339 };
340
341 module_gbphy_driver(i2c_driver);
342 MODULE_LICENSE("GPL v2");
343