Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Virtio I2C Bus Driver
0004  *
0005  * The Virtio I2C Specification:
0006  * https://raw.githubusercontent.com/oasis-tcs/virtio-spec/master/virtio-i2c.tex
0007  *
0008  * Copyright (c) 2021 Intel Corporation. All rights reserved.
0009  */
0010 
0011 #include <linux/acpi.h>
0012 #include <linux/completion.h>
0013 #include <linux/err.h>
0014 #include <linux/i2c.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/virtio.h>
0018 #include <linux/virtio_ids.h>
0019 #include <linux/virtio_config.h>
0020 #include <linux/virtio_i2c.h>
0021 
0022 /**
0023  * struct virtio_i2c - virtio I2C data
0024  * @vdev: virtio device for this controller
0025  * @adap: I2C adapter for this controller
0026  * @vq: the virtio virtqueue for communication
0027  */
0028 struct virtio_i2c {
0029     struct virtio_device *vdev;
0030     struct i2c_adapter adap;
0031     struct virtqueue *vq;
0032 };
0033 
0034 /**
0035  * struct virtio_i2c_req - the virtio I2C request structure
0036  * @completion: completion of virtio I2C message
0037  * @out_hdr: the OUT header of the virtio I2C message
0038  * @buf: the buffer into which data is read, or from which it's written
0039  * @in_hdr: the IN header of the virtio I2C message
0040  */
0041 struct virtio_i2c_req {
0042     struct completion completion;
0043     struct virtio_i2c_out_hdr out_hdr   ____cacheline_aligned;
0044     uint8_t *buf                ____cacheline_aligned;
0045     struct virtio_i2c_in_hdr in_hdr     ____cacheline_aligned;
0046 };
0047 
0048 static void virtio_i2c_msg_done(struct virtqueue *vq)
0049 {
0050     struct virtio_i2c_req *req;
0051     unsigned int len;
0052 
0053     while ((req = virtqueue_get_buf(vq, &len)))
0054         complete(&req->completion);
0055 }
0056 
0057 static int virtio_i2c_prepare_reqs(struct virtqueue *vq,
0058                    struct virtio_i2c_req *reqs,
0059                    struct i2c_msg *msgs, int num)
0060 {
0061     struct scatterlist *sgs[3], out_hdr, msg_buf, in_hdr;
0062     int i;
0063 
0064     for (i = 0; i < num; i++) {
0065         int outcnt = 0, incnt = 0;
0066 
0067         init_completion(&reqs[i].completion);
0068 
0069         /*
0070          * Only 7-bit mode supported for this moment. For the address
0071          * format, Please check the Virtio I2C Specification.
0072          */
0073         reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1);
0074 
0075         if (msgs[i].flags & I2C_M_RD)
0076             reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_M_RD);
0077 
0078         if (i != num - 1)
0079             reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT);
0080 
0081         sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr));
0082         sgs[outcnt++] = &out_hdr;
0083 
0084         if (msgs[i].len) {
0085             reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1);
0086             if (!reqs[i].buf)
0087                 break;
0088 
0089             sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len);
0090 
0091             if (msgs[i].flags & I2C_M_RD)
0092                 sgs[outcnt + incnt++] = &msg_buf;
0093             else
0094                 sgs[outcnt++] = &msg_buf;
0095         }
0096 
0097         sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr));
0098         sgs[outcnt + incnt++] = &in_hdr;
0099 
0100         if (virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
0101             i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false);
0102             break;
0103         }
0104     }
0105 
0106     return i;
0107 }
0108 
0109 static int virtio_i2c_complete_reqs(struct virtqueue *vq,
0110                     struct virtio_i2c_req *reqs,
0111                     struct i2c_msg *msgs, int num)
0112 {
0113     bool failed = false;
0114     int i, j = 0;
0115 
0116     for (i = 0; i < num; i++) {
0117         struct virtio_i2c_req *req = &reqs[i];
0118 
0119         wait_for_completion(&req->completion);
0120 
0121         if (!failed && req->in_hdr.status != VIRTIO_I2C_MSG_OK)
0122             failed = true;
0123 
0124         i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], !failed);
0125 
0126         if (!failed)
0127             j++;
0128     }
0129 
0130     return j;
0131 }
0132 
0133 static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
0134                int num)
0135 {
0136     struct virtio_i2c *vi = i2c_get_adapdata(adap);
0137     struct virtqueue *vq = vi->vq;
0138     struct virtio_i2c_req *reqs;
0139     int count;
0140 
0141     reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
0142     if (!reqs)
0143         return -ENOMEM;
0144 
0145     count = virtio_i2c_prepare_reqs(vq, reqs, msgs, num);
0146     if (!count)
0147         goto err_free;
0148 
0149     /*
0150      * For the case where count < num, i.e. we weren't able to queue all the
0151      * msgs, ideally we should abort right away and return early, but some
0152      * of the messages are already sent to the remote I2C controller and the
0153      * virtqueue will be left in undefined state in that case. We kick the
0154      * remote here to clear the virtqueue, so we can try another set of
0155      * messages later on.
0156      */
0157     virtqueue_kick(vq);
0158 
0159     count = virtio_i2c_complete_reqs(vq, reqs, msgs, count);
0160 
0161 err_free:
0162     kfree(reqs);
0163     return count;
0164 }
0165 
0166 static void virtio_i2c_del_vqs(struct virtio_device *vdev)
0167 {
0168     virtio_reset_device(vdev);
0169     vdev->config->del_vqs(vdev);
0170 }
0171 
0172 static int virtio_i2c_setup_vqs(struct virtio_i2c *vi)
0173 {
0174     struct virtio_device *vdev = vi->vdev;
0175 
0176     vi->vq = virtio_find_single_vq(vdev, virtio_i2c_msg_done, "msg");
0177     return PTR_ERR_OR_ZERO(vi->vq);
0178 }
0179 
0180 static u32 virtio_i2c_func(struct i2c_adapter *adap)
0181 {
0182     return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0183 }
0184 
0185 static struct i2c_algorithm virtio_algorithm = {
0186     .master_xfer = virtio_i2c_xfer,
0187     .functionality = virtio_i2c_func,
0188 };
0189 
0190 static int virtio_i2c_probe(struct virtio_device *vdev)
0191 {
0192     struct virtio_i2c *vi;
0193     int ret;
0194 
0195     if (!virtio_has_feature(vdev, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST)) {
0196         dev_err(&vdev->dev, "Zero-length request feature is mandatory\n");
0197         return -EINVAL;
0198     }
0199 
0200     vi = devm_kzalloc(&vdev->dev, sizeof(*vi), GFP_KERNEL);
0201     if (!vi)
0202         return -ENOMEM;
0203 
0204     vdev->priv = vi;
0205     vi->vdev = vdev;
0206 
0207     ret = virtio_i2c_setup_vqs(vi);
0208     if (ret)
0209         return ret;
0210 
0211     vi->adap.owner = THIS_MODULE;
0212     snprintf(vi->adap.name, sizeof(vi->adap.name),
0213          "i2c_virtio at virtio bus %d", vdev->index);
0214     vi->adap.algo = &virtio_algorithm;
0215     vi->adap.dev.parent = &vdev->dev;
0216     vi->adap.dev.of_node = vdev->dev.of_node;
0217     i2c_set_adapdata(&vi->adap, vi);
0218 
0219     /*
0220      * Setup ACPI node for controlled devices which will be probed through
0221      * ACPI.
0222      */
0223     ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(vdev->dev.parent));
0224 
0225     ret = i2c_add_adapter(&vi->adap);
0226     if (ret)
0227         virtio_i2c_del_vqs(vdev);
0228 
0229     return ret;
0230 }
0231 
0232 static void virtio_i2c_remove(struct virtio_device *vdev)
0233 {
0234     struct virtio_i2c *vi = vdev->priv;
0235 
0236     i2c_del_adapter(&vi->adap);
0237     virtio_i2c_del_vqs(vdev);
0238 }
0239 
0240 static struct virtio_device_id id_table[] = {
0241     { VIRTIO_ID_I2C_ADAPTER, VIRTIO_DEV_ANY_ID },
0242     {}
0243 };
0244 MODULE_DEVICE_TABLE(virtio, id_table);
0245 
0246 #ifdef CONFIG_PM_SLEEP
0247 static int virtio_i2c_freeze(struct virtio_device *vdev)
0248 {
0249     virtio_i2c_del_vqs(vdev);
0250     return 0;
0251 }
0252 
0253 static int virtio_i2c_restore(struct virtio_device *vdev)
0254 {
0255     return virtio_i2c_setup_vqs(vdev->priv);
0256 }
0257 #endif
0258 
0259 static const unsigned int features[] = {
0260     VIRTIO_I2C_F_ZERO_LENGTH_REQUEST,
0261 };
0262 
0263 static struct virtio_driver virtio_i2c_driver = {
0264     .feature_table      = features,
0265     .feature_table_size = ARRAY_SIZE(features),
0266     .id_table       = id_table,
0267     .probe          = virtio_i2c_probe,
0268     .remove         = virtio_i2c_remove,
0269     .driver         = {
0270         .name   = "i2c_virtio",
0271     },
0272 #ifdef CONFIG_PM_SLEEP
0273     .freeze = virtio_i2c_freeze,
0274     .restore = virtio_i2c_restore,
0275 #endif
0276 };
0277 module_virtio_driver(virtio_i2c_driver);
0278 
0279 MODULE_AUTHOR("Jie Deng <jie.deng@intel.com>");
0280 MODULE_AUTHOR("Conghui Chen <conghui.chen@intel.com>");
0281 MODULE_DESCRIPTION("Virtio i2c bus driver");
0282 MODULE_LICENSE("GPL");