Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2018 Cadence Design Systems Inc.
0004  *
0005  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
0006  */
0007 
0008 #include <linux/atomic.h>
0009 #include <linux/bug.h>
0010 #include <linux/completion.h>
0011 #include <linux/device.h>
0012 #include <linux/mutex.h>
0013 #include <linux/slab.h>
0014 
0015 #include "internals.h"
0016 
0017 /**
0018  * i3c_device_do_priv_xfers() - do I3C SDR private transfers directed to a
0019  *              specific device
0020  *
0021  * @dev: device with which the transfers should be done
0022  * @xfers: array of transfers
0023  * @nxfers: number of transfers
0024  *
0025  * Initiate one or several private SDR transfers with @dev.
0026  *
0027  * This function can sleep and thus cannot be called in atomic context.
0028  *
0029  * Return: 0 in case of success, a negative error core otherwise.
0030  */
0031 int i3c_device_do_priv_xfers(struct i3c_device *dev,
0032                  struct i3c_priv_xfer *xfers,
0033                  int nxfers)
0034 {
0035     int ret, i;
0036 
0037     if (nxfers < 1)
0038         return 0;
0039 
0040     for (i = 0; i < nxfers; i++) {
0041         if (!xfers[i].len || !xfers[i].data.in)
0042             return -EINVAL;
0043     }
0044 
0045     i3c_bus_normaluse_lock(dev->bus);
0046     ret = i3c_dev_do_priv_xfers_locked(dev->desc, xfers, nxfers);
0047     i3c_bus_normaluse_unlock(dev->bus);
0048 
0049     return ret;
0050 }
0051 EXPORT_SYMBOL_GPL(i3c_device_do_priv_xfers);
0052 
0053 /**
0054  * i3c_device_get_info() - get I3C device information
0055  *
0056  * @dev: device we want information on
0057  * @info: the information object to fill in
0058  *
0059  * Retrieve I3C dev info.
0060  */
0061 void i3c_device_get_info(struct i3c_device *dev,
0062              struct i3c_device_info *info)
0063 {
0064     if (!info)
0065         return;
0066 
0067     i3c_bus_normaluse_lock(dev->bus);
0068     if (dev->desc)
0069         *info = dev->desc->info;
0070     i3c_bus_normaluse_unlock(dev->bus);
0071 }
0072 EXPORT_SYMBOL_GPL(i3c_device_get_info);
0073 
0074 /**
0075  * i3c_device_disable_ibi() - Disable IBIs coming from a specific device
0076  * @dev: device on which IBIs should be disabled
0077  *
0078  * This function disable IBIs coming from a specific device and wait for
0079  * all pending IBIs to be processed.
0080  *
0081  * Return: 0 in case of success, a negative error core otherwise.
0082  */
0083 int i3c_device_disable_ibi(struct i3c_device *dev)
0084 {
0085     int ret = -ENOENT;
0086 
0087     i3c_bus_normaluse_lock(dev->bus);
0088     if (dev->desc) {
0089         mutex_lock(&dev->desc->ibi_lock);
0090         ret = i3c_dev_disable_ibi_locked(dev->desc);
0091         mutex_unlock(&dev->desc->ibi_lock);
0092     }
0093     i3c_bus_normaluse_unlock(dev->bus);
0094 
0095     return ret;
0096 }
0097 EXPORT_SYMBOL_GPL(i3c_device_disable_ibi);
0098 
0099 /**
0100  * i3c_device_enable_ibi() - Enable IBIs coming from a specific device
0101  * @dev: device on which IBIs should be enabled
0102  *
0103  * This function enable IBIs coming from a specific device and wait for
0104  * all pending IBIs to be processed. This should be called on a device
0105  * where i3c_device_request_ibi() has succeeded.
0106  *
0107  * Note that IBIs from this device might be received before this function
0108  * returns to its caller.
0109  *
0110  * Return: 0 in case of success, a negative error core otherwise.
0111  */
0112 int i3c_device_enable_ibi(struct i3c_device *dev)
0113 {
0114     int ret = -ENOENT;
0115 
0116     i3c_bus_normaluse_lock(dev->bus);
0117     if (dev->desc) {
0118         mutex_lock(&dev->desc->ibi_lock);
0119         ret = i3c_dev_enable_ibi_locked(dev->desc);
0120         mutex_unlock(&dev->desc->ibi_lock);
0121     }
0122     i3c_bus_normaluse_unlock(dev->bus);
0123 
0124     return ret;
0125 }
0126 EXPORT_SYMBOL_GPL(i3c_device_enable_ibi);
0127 
0128 /**
0129  * i3c_device_request_ibi() - Request an IBI
0130  * @dev: device for which we should enable IBIs
0131  * @req: setup requested for this IBI
0132  *
0133  * This function is responsible for pre-allocating all resources needed to
0134  * process IBIs coming from @dev. When this function returns, the IBI is not
0135  * enabled until i3c_device_enable_ibi() is called.
0136  *
0137  * Return: 0 in case of success, a negative error core otherwise.
0138  */
0139 int i3c_device_request_ibi(struct i3c_device *dev,
0140                const struct i3c_ibi_setup *req)
0141 {
0142     int ret = -ENOENT;
0143 
0144     if (!req->handler || !req->num_slots)
0145         return -EINVAL;
0146 
0147     i3c_bus_normaluse_lock(dev->bus);
0148     if (dev->desc) {
0149         mutex_lock(&dev->desc->ibi_lock);
0150         ret = i3c_dev_request_ibi_locked(dev->desc, req);
0151         mutex_unlock(&dev->desc->ibi_lock);
0152     }
0153     i3c_bus_normaluse_unlock(dev->bus);
0154 
0155     return ret;
0156 }
0157 EXPORT_SYMBOL_GPL(i3c_device_request_ibi);
0158 
0159 /**
0160  * i3c_device_free_ibi() - Free all resources needed for IBI handling
0161  * @dev: device on which you want to release IBI resources
0162  *
0163  * This function is responsible for de-allocating resources previously
0164  * allocated by i3c_device_request_ibi(). It should be called after disabling
0165  * IBIs with i3c_device_disable_ibi().
0166  */
0167 void i3c_device_free_ibi(struct i3c_device *dev)
0168 {
0169     i3c_bus_normaluse_lock(dev->bus);
0170     if (dev->desc) {
0171         mutex_lock(&dev->desc->ibi_lock);
0172         i3c_dev_free_ibi_locked(dev->desc);
0173         mutex_unlock(&dev->desc->ibi_lock);
0174     }
0175     i3c_bus_normaluse_unlock(dev->bus);
0176 }
0177 EXPORT_SYMBOL_GPL(i3c_device_free_ibi);
0178 
0179 /**
0180  * i3cdev_to_dev() - Returns the device embedded in @i3cdev
0181  * @i3cdev: I3C device
0182  *
0183  * Return: a pointer to a device object.
0184  */
0185 struct device *i3cdev_to_dev(struct i3c_device *i3cdev)
0186 {
0187     return &i3cdev->dev;
0188 }
0189 EXPORT_SYMBOL_GPL(i3cdev_to_dev);
0190 
0191 /**
0192  * dev_to_i3cdev() - Returns the I3C device containing @dev
0193  * @dev: device object
0194  *
0195  * Return: a pointer to an I3C device object.
0196  */
0197 struct i3c_device *dev_to_i3cdev(struct device *dev)
0198 {
0199     return container_of(dev, struct i3c_device, dev);
0200 }
0201 EXPORT_SYMBOL_GPL(dev_to_i3cdev);
0202 
0203 /**
0204  * i3c_device_match_id() - Returns the i3c_device_id entry matching @i3cdev
0205  * @i3cdev: I3C device
0206  * @id_table: I3C device match table
0207  *
0208  * Return: a pointer to an i3c_device_id object or NULL if there's no match.
0209  */
0210 const struct i3c_device_id *
0211 i3c_device_match_id(struct i3c_device *i3cdev,
0212             const struct i3c_device_id *id_table)
0213 {
0214     struct i3c_device_info devinfo;
0215     const struct i3c_device_id *id;
0216     u16 manuf, part, ext_info;
0217     bool rndpid;
0218 
0219     i3c_device_get_info(i3cdev, &devinfo);
0220 
0221     manuf = I3C_PID_MANUF_ID(devinfo.pid);
0222     part = I3C_PID_PART_ID(devinfo.pid);
0223     ext_info = I3C_PID_EXTRA_INFO(devinfo.pid);
0224     rndpid = I3C_PID_RND_LOWER_32BITS(devinfo.pid);
0225 
0226     for (id = id_table; id->match_flags != 0; id++) {
0227         if ((id->match_flags & I3C_MATCH_DCR) &&
0228             id->dcr != devinfo.dcr)
0229             continue;
0230 
0231         if ((id->match_flags & I3C_MATCH_MANUF) &&
0232             id->manuf_id != manuf)
0233             continue;
0234 
0235         if ((id->match_flags & I3C_MATCH_PART) &&
0236             (rndpid || id->part_id != part))
0237             continue;
0238 
0239         if ((id->match_flags & I3C_MATCH_EXTRA_INFO) &&
0240             (rndpid || id->extra_info != ext_info))
0241             continue;
0242 
0243         return id;
0244     }
0245 
0246     return NULL;
0247 }
0248 EXPORT_SYMBOL_GPL(i3c_device_match_id);
0249 
0250 /**
0251  * i3c_driver_register_with_owner() - register an I3C device driver
0252  *
0253  * @drv: driver to register
0254  * @owner: module that owns this driver
0255  *
0256  * Register @drv to the core.
0257  *
0258  * Return: 0 in case of success, a negative error core otherwise.
0259  */
0260 int i3c_driver_register_with_owner(struct i3c_driver *drv, struct module *owner)
0261 {
0262     drv->driver.owner = owner;
0263     drv->driver.bus = &i3c_bus_type;
0264 
0265     if (!drv->probe) {
0266         pr_err("Trying to register an i3c driver without probe callback\n");
0267         return -EINVAL;
0268     }
0269 
0270     return driver_register(&drv->driver);
0271 }
0272 EXPORT_SYMBOL_GPL(i3c_driver_register_with_owner);
0273 
0274 /**
0275  * i3c_driver_unregister() - unregister an I3C device driver
0276  *
0277  * @drv: driver to unregister
0278  *
0279  * Unregister @drv.
0280  */
0281 void i3c_driver_unregister(struct i3c_driver *drv)
0282 {
0283     driver_unregister(&drv->driver);
0284 }
0285 EXPORT_SYMBOL_GPL(i3c_driver_unregister);