i2c.c 8.1 KB

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