Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * I2C bridge driver for the Greybus "generic" I2C module.
0004  *
0005  * Copyright 2014 Google Inc.
0006  * Copyright 2014 Linaro Ltd.
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/slab.h>
0012 #include <linux/i2c.h>
0013 #include <linux/greybus.h>
0014 
0015 #include "gbphy.h"
0016 
0017 struct gb_i2c_device {
0018     struct gb_connection    *connection;
0019     struct gbphy_device *gbphy_dev;
0020 
0021     u32         functionality;
0022 
0023     struct i2c_adapter  adapter;
0024 };
0025 
0026 /*
0027  * Map Greybus i2c functionality bits into Linux ones
0028  */
0029 static u32 gb_i2c_functionality_map(u32 gb_i2c_functionality)
0030 {
0031     return gb_i2c_functionality;    /* All bits the same for now */
0032 }
0033 
0034 /*
0035  * Do initial setup of the i2c device.  This includes verifying we
0036  * can support it (based on the protocol version it advertises).
0037  * If that's OK, we get and cached its functionality bits.
0038  *
0039  * Note: gb_i2c_dev->connection is assumed to have been valid.
0040  */
0041 static int gb_i2c_device_setup(struct gb_i2c_device *gb_i2c_dev)
0042 {
0043     struct gb_i2c_functionality_response response;
0044     u32 functionality;
0045     int ret;
0046 
0047     ret = gb_operation_sync(gb_i2c_dev->connection,
0048                 GB_I2C_TYPE_FUNCTIONALITY,
0049                 NULL, 0, &response, sizeof(response));
0050     if (ret)
0051         return ret;
0052 
0053     functionality = le32_to_cpu(response.functionality);
0054     gb_i2c_dev->functionality = gb_i2c_functionality_map(functionality);
0055 
0056     return 0;
0057 }
0058 
0059 /*
0060  * Map Linux i2c_msg flags into Greybus i2c transfer op flags.
0061  */
0062 static u16 gb_i2c_transfer_op_flags_map(u16 flags)
0063 {
0064     return flags;   /* All flags the same for now */
0065 }
0066 
0067 static void
0068 gb_i2c_fill_transfer_op(struct gb_i2c_transfer_op *op, struct i2c_msg *msg)
0069 {
0070     u16 flags = gb_i2c_transfer_op_flags_map(msg->flags);
0071 
0072     op->addr = cpu_to_le16(msg->addr);
0073     op->flags = cpu_to_le16(flags);
0074     op->size = cpu_to_le16(msg->len);
0075 }
0076 
0077 static struct gb_operation *
0078 gb_i2c_operation_create(struct gb_connection *connection,
0079             struct i2c_msg *msgs, u32 msg_count)
0080 {
0081     struct gb_i2c_device *gb_i2c_dev = gb_connection_get_data(connection);
0082     struct gb_i2c_transfer_request *request;
0083     struct gb_operation *operation;
0084     struct gb_i2c_transfer_op *op;
0085     struct i2c_msg *msg;
0086     u32 data_out_size = 0;
0087     u32 data_in_size = 0;
0088     size_t request_size;
0089     void *data;
0090     u16 op_count;
0091     u32 i;
0092 
0093     if (msg_count > (u32)U16_MAX) {
0094         dev_err(&gb_i2c_dev->gbphy_dev->dev, "msg_count (%u) too big\n",
0095             msg_count);
0096         return NULL;
0097     }
0098     op_count = (u16)msg_count;
0099 
0100     /*
0101      * In addition to space for all message descriptors we need
0102      * to have enough to hold all outbound message data.
0103      */
0104     msg = msgs;
0105     for (i = 0; i < msg_count; i++, msg++)
0106         if (msg->flags & I2C_M_RD)
0107             data_in_size += (u32)msg->len;
0108         else
0109             data_out_size += (u32)msg->len;
0110 
0111     request_size = sizeof(*request);
0112     request_size += msg_count * sizeof(*op);
0113     request_size += data_out_size;
0114 
0115     /* Response consists only of incoming data */
0116     operation = gb_operation_create(connection, GB_I2C_TYPE_TRANSFER,
0117                     request_size, data_in_size, GFP_KERNEL);
0118     if (!operation)
0119         return NULL;
0120 
0121     request = operation->request->payload;
0122     request->op_count = cpu_to_le16(op_count);
0123     /* Fill in the ops array */
0124     op = &request->ops[0];
0125     msg = msgs;
0126     for (i = 0; i < msg_count; i++)
0127         gb_i2c_fill_transfer_op(op++, msg++);
0128 
0129     if (!data_out_size)
0130         return operation;
0131 
0132     /* Copy over the outgoing data; it starts after the last op */
0133     data = op;
0134     msg = msgs;
0135     for (i = 0; i < msg_count; i++) {
0136         if (!(msg->flags & I2C_M_RD)) {
0137             memcpy(data, msg->buf, msg->len);
0138             data += msg->len;
0139         }
0140         msg++;
0141     }
0142 
0143     return operation;
0144 }
0145 
0146 static void gb_i2c_decode_response(struct i2c_msg *msgs, u32 msg_count,
0147                    struct gb_i2c_transfer_response *response)
0148 {
0149     struct i2c_msg *msg = msgs;
0150     u8 *data;
0151     u32 i;
0152 
0153     if (!response)
0154         return;
0155     data = response->data;
0156     for (i = 0; i < msg_count; i++) {
0157         if (msg->flags & I2C_M_RD) {
0158             memcpy(msg->buf, data, msg->len);
0159             data += msg->len;
0160         }
0161         msg++;
0162     }
0163 }
0164 
0165 /*
0166  * Some i2c transfer operations return results that are expected.
0167  */
0168 static bool gb_i2c_expected_transfer_error(int errno)
0169 {
0170     return errno == -EAGAIN || errno == -ENODEV;
0171 }
0172 
0173 static int gb_i2c_transfer_operation(struct gb_i2c_device *gb_i2c_dev,
0174                      struct i2c_msg *msgs, u32 msg_count)
0175 {
0176     struct gb_connection *connection = gb_i2c_dev->connection;
0177     struct device *dev = &gb_i2c_dev->gbphy_dev->dev;
0178     struct gb_operation *operation;
0179     int ret;
0180 
0181     operation = gb_i2c_operation_create(connection, msgs, msg_count);
0182     if (!operation)
0183         return -ENOMEM;
0184 
0185     ret = gbphy_runtime_get_sync(gb_i2c_dev->gbphy_dev);
0186     if (ret)
0187         goto exit_operation_put;
0188 
0189     ret = gb_operation_request_send_sync(operation);
0190     if (!ret) {
0191         struct gb_i2c_transfer_response *response;
0192 
0193         response = operation->response->payload;
0194         gb_i2c_decode_response(msgs, msg_count, response);
0195         ret = msg_count;
0196     } else if (!gb_i2c_expected_transfer_error(ret)) {
0197         dev_err(dev, "transfer operation failed (%d)\n", ret);
0198     }
0199 
0200     gbphy_runtime_put_autosuspend(gb_i2c_dev->gbphy_dev);
0201 
0202 exit_operation_put:
0203     gb_operation_put(operation);
0204 
0205     return ret;
0206 }
0207 
0208 static int gb_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
0209                   int msg_count)
0210 {
0211     struct gb_i2c_device *gb_i2c_dev;
0212 
0213     gb_i2c_dev = i2c_get_adapdata(adap);
0214 
0215     return gb_i2c_transfer_operation(gb_i2c_dev, msgs, msg_count);
0216 }
0217 
0218 static u32 gb_i2c_functionality(struct i2c_adapter *adap)
0219 {
0220     struct gb_i2c_device *gb_i2c_dev = i2c_get_adapdata(adap);
0221 
0222     return gb_i2c_dev->functionality;
0223 }
0224 
0225 static const struct i2c_algorithm gb_i2c_algorithm = {
0226     .master_xfer    = gb_i2c_master_xfer,
0227     .functionality  = gb_i2c_functionality,
0228 };
0229 
0230 static int gb_i2c_probe(struct gbphy_device *gbphy_dev,
0231             const struct gbphy_device_id *id)
0232 {
0233     struct gb_connection *connection;
0234     struct gb_i2c_device *gb_i2c_dev;
0235     struct i2c_adapter *adapter;
0236     int ret;
0237 
0238     gb_i2c_dev = kzalloc(sizeof(*gb_i2c_dev), GFP_KERNEL);
0239     if (!gb_i2c_dev)
0240         return -ENOMEM;
0241 
0242     connection =
0243         gb_connection_create(gbphy_dev->bundle,
0244                      le16_to_cpu(gbphy_dev->cport_desc->id),
0245                      NULL);
0246     if (IS_ERR(connection)) {
0247         ret = PTR_ERR(connection);
0248         goto exit_i2cdev_free;
0249     }
0250 
0251     gb_i2c_dev->connection = connection;
0252     gb_connection_set_data(connection, gb_i2c_dev);
0253     gb_i2c_dev->gbphy_dev = gbphy_dev;
0254     gb_gbphy_set_data(gbphy_dev, gb_i2c_dev);
0255 
0256     ret = gb_connection_enable(connection);
0257     if (ret)
0258         goto exit_connection_destroy;
0259 
0260     ret = gb_i2c_device_setup(gb_i2c_dev);
0261     if (ret)
0262         goto exit_connection_disable;
0263 
0264     /* Looks good; up our i2c adapter */
0265     adapter = &gb_i2c_dev->adapter;
0266     adapter->owner = THIS_MODULE;
0267     adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
0268     adapter->algo = &gb_i2c_algorithm;
0269 
0270     adapter->dev.parent = &gbphy_dev->dev;
0271     snprintf(adapter->name, sizeof(adapter->name), "Greybus i2c adapter");
0272     i2c_set_adapdata(adapter, gb_i2c_dev);
0273 
0274     ret = i2c_add_adapter(adapter);
0275     if (ret)
0276         goto exit_connection_disable;
0277 
0278     gbphy_runtime_put_autosuspend(gbphy_dev);
0279     return 0;
0280 
0281 exit_connection_disable:
0282     gb_connection_disable(connection);
0283 exit_connection_destroy:
0284     gb_connection_destroy(connection);
0285 exit_i2cdev_free:
0286     kfree(gb_i2c_dev);
0287 
0288     return ret;
0289 }
0290 
0291 static void gb_i2c_remove(struct gbphy_device *gbphy_dev)
0292 {
0293     struct gb_i2c_device *gb_i2c_dev = gb_gbphy_get_data(gbphy_dev);
0294     struct gb_connection *connection = gb_i2c_dev->connection;
0295     int ret;
0296 
0297     ret = gbphy_runtime_get_sync(gbphy_dev);
0298     if (ret)
0299         gbphy_runtime_get_noresume(gbphy_dev);
0300 
0301     i2c_del_adapter(&gb_i2c_dev->adapter);
0302     gb_connection_disable(connection);
0303     gb_connection_destroy(connection);
0304     kfree(gb_i2c_dev);
0305 }
0306 
0307 static const struct gbphy_device_id gb_i2c_id_table[] = {
0308     { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_I2C) },
0309     { },
0310 };
0311 MODULE_DEVICE_TABLE(gbphy, gb_i2c_id_table);
0312 
0313 static struct gbphy_driver i2c_driver = {
0314     .name       = "i2c",
0315     .probe      = gb_i2c_probe,
0316     .remove     = gb_i2c_remove,
0317     .id_table   = gb_i2c_id_table,
0318 };
0319 
0320 module_gbphy_driver(i2c_driver);
0321 MODULE_LICENSE("GPL v2");