1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Cadence Design Systems Inc.
5 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
8 #include <linux/atomic.h>
10 #include <linux/completion.h>
11 #include <linux/device.h>
12 #include <linux/mutex.h>
13 #include <linux/slab.h>
15 #include "internals.h"
18 * i3c_device_do_priv_xfers() - do I3C SDR private transfers directed to a
21 * @dev: device with which the transfers should be done
22 * @xfers: array of transfers
23 * @nxfers: number of transfers
25 * Initiate one or several private SDR transfers with @dev.
27 * This function can sleep and thus cannot be called in atomic context.
29 * Return: 0 in case of success, a negative error core otherwise.
31 int i3c_device_do_priv_xfers(struct i3c_device *dev,
32 struct i3c_priv_xfer *xfers,
40 for (i = 0; i < nxfers; i++) {
41 if (!xfers[i].len || !xfers[i].data.in)
45 i3c_bus_normaluse_lock(dev->bus);
46 ret = i3c_dev_do_priv_xfers_locked(dev->desc, xfers, nxfers);
47 i3c_bus_normaluse_unlock(dev->bus);
51 EXPORT_SYMBOL_GPL(i3c_device_do_priv_xfers);
54 * i3c_device_do_setdasa() - do I3C dynamic address assignement with
57 * @dev: device with which the DAA should be done
59 * Return: 0 in case of success, a negative error core otherwise.
61 int i3c_device_do_setdasa(struct i3c_device *dev)
65 i3c_bus_normaluse_lock(dev->bus);
66 ret = i3c_dev_setdasa_locked(dev->desc);
67 i3c_bus_normaluse_unlock(dev->bus);
71 EXPORT_SYMBOL_GPL(i3c_device_do_setdasa);
74 * i3c_device_get_info() - get I3C device information
76 * @dev: device we want information on
77 * @info: the information object to fill in
79 * Retrieve I3C dev info.
81 void i3c_device_get_info(const struct i3c_device *dev,
82 struct i3c_device_info *info)
87 i3c_bus_normaluse_lock(dev->bus);
89 *info = dev->desc->info;
90 i3c_bus_normaluse_unlock(dev->bus);
92 EXPORT_SYMBOL_GPL(i3c_device_get_info);
95 * i3c_device_disable_ibi() - Disable IBIs coming from a specific device
96 * @dev: device on which IBIs should be disabled
98 * This function disable IBIs coming from a specific device and wait for
99 * all pending IBIs to be processed.
101 * Return: 0 in case of success, a negative error core otherwise.
103 int i3c_device_disable_ibi(struct i3c_device *dev)
107 i3c_bus_normaluse_lock(dev->bus);
109 mutex_lock(&dev->desc->ibi_lock);
110 ret = i3c_dev_disable_ibi_locked(dev->desc);
111 mutex_unlock(&dev->desc->ibi_lock);
113 i3c_bus_normaluse_unlock(dev->bus);
117 EXPORT_SYMBOL_GPL(i3c_device_disable_ibi);
120 * i3c_device_enable_ibi() - Enable IBIs coming from a specific device
121 * @dev: device on which IBIs should be enabled
123 * This function enable IBIs coming from a specific device and wait for
124 * all pending IBIs to be processed. This should be called on a device
125 * where i3c_device_request_ibi() has succeeded.
127 * Note that IBIs from this device might be received before this function
128 * returns to its caller.
130 * Return: 0 in case of success, a negative error core otherwise.
132 int i3c_device_enable_ibi(struct i3c_device *dev)
136 i3c_bus_normaluse_lock(dev->bus);
138 mutex_lock(&dev->desc->ibi_lock);
139 ret = i3c_dev_enable_ibi_locked(dev->desc);
140 mutex_unlock(&dev->desc->ibi_lock);
142 i3c_bus_normaluse_unlock(dev->bus);
146 EXPORT_SYMBOL_GPL(i3c_device_enable_ibi);
149 * i3c_device_request_ibi() - Request an IBI
150 * @dev: device for which we should enable IBIs
151 * @req: setup requested for this IBI
153 * This function is responsible for pre-allocating all resources needed to
154 * process IBIs coming from @dev. When this function returns, the IBI is not
155 * enabled until i3c_device_enable_ibi() is called.
157 * Return: 0 in case of success, a negative error core otherwise.
159 int i3c_device_request_ibi(struct i3c_device *dev,
160 const struct i3c_ibi_setup *req)
164 if (!req->handler || !req->num_slots)
167 i3c_bus_normaluse_lock(dev->bus);
169 mutex_lock(&dev->desc->ibi_lock);
170 ret = i3c_dev_request_ibi_locked(dev->desc, req);
171 mutex_unlock(&dev->desc->ibi_lock);
173 i3c_bus_normaluse_unlock(dev->bus);
177 EXPORT_SYMBOL_GPL(i3c_device_request_ibi);
180 * i3c_device_free_ibi() - Free all resources needed for IBI handling
181 * @dev: device on which you want to release IBI resources
183 * This function is responsible for de-allocating resources previously
184 * allocated by i3c_device_request_ibi(). It should be called after disabling
185 * IBIs with i3c_device_disable_ibi().
187 void i3c_device_free_ibi(struct i3c_device *dev)
189 i3c_bus_normaluse_lock(dev->bus);
191 mutex_lock(&dev->desc->ibi_lock);
192 i3c_dev_free_ibi_locked(dev->desc);
193 mutex_unlock(&dev->desc->ibi_lock);
195 i3c_bus_normaluse_unlock(dev->bus);
197 EXPORT_SYMBOL_GPL(i3c_device_free_ibi);
200 * i3cdev_to_dev() - Returns the device embedded in @i3cdev
201 * @i3cdev: I3C device
203 * Return: a pointer to a device object.
205 struct device *i3cdev_to_dev(struct i3c_device *i3cdev)
209 EXPORT_SYMBOL_GPL(i3cdev_to_dev);
212 * i3c_device_match_id() - Returns the i3c_device_id entry matching @i3cdev
213 * @i3cdev: I3C device
214 * @id_table: I3C device match table
216 * Return: a pointer to an i3c_device_id object or NULL if there's no match.
218 const struct i3c_device_id *
219 i3c_device_match_id(struct i3c_device *i3cdev,
220 const struct i3c_device_id *id_table)
222 struct i3c_device_info devinfo;
223 const struct i3c_device_id *id;
224 u16 manuf, part, ext_info;
227 i3c_device_get_info(i3cdev, &devinfo);
229 manuf = I3C_PID_MANUF_ID(devinfo.pid);
230 part = I3C_PID_PART_ID(devinfo.pid);
231 ext_info = I3C_PID_EXTRA_INFO(devinfo.pid);
232 rndpid = I3C_PID_RND_LOWER_32BITS(devinfo.pid);
234 for (id = id_table; id->match_flags != 0; id++) {
235 if ((id->match_flags & I3C_MATCH_DCR) &&
236 id->dcr != devinfo.dcr)
239 if ((id->match_flags & I3C_MATCH_MANUF) &&
240 id->manuf_id != manuf)
243 if ((id->match_flags & I3C_MATCH_PART) &&
244 (rndpid || id->part_id != part))
247 if ((id->match_flags & I3C_MATCH_EXTRA_INFO) &&
248 (rndpid || id->extra_info != ext_info))
256 EXPORT_SYMBOL_GPL(i3c_device_match_id);
259 * i3c_driver_register_with_owner() - register an I3C device driver
261 * @drv: driver to register
262 * @owner: module that owns this driver
264 * Register @drv to the core.
266 * Return: 0 in case of success, a negative error core otherwise.
268 int i3c_driver_register_with_owner(struct i3c_driver *drv, struct module *owner)
270 drv->driver.owner = owner;
271 drv->driver.bus = &i3c_bus_type;
274 pr_err("Trying to register an i3c driver without probe callback\n");
278 return driver_register(&drv->driver);
280 EXPORT_SYMBOL_GPL(i3c_driver_register_with_owner);
283 * i3c_driver_unregister() - unregister an I3C device driver
285 * @drv: driver to unregister
289 void i3c_driver_unregister(struct i3c_driver *drv)
291 driver_unregister(&drv->driver);
293 EXPORT_SYMBOL_GPL(i3c_driver_unregister);