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