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_get_info() - get I3C device information
56 * @dev: device we want information on
57 * @info: the information object to fill in
59 * Retrieve I3C dev info.
61 void i3c_device_get_info(struct i3c_device
*dev
,
62 struct i3c_device_info
*info
)
67 i3c_bus_normaluse_lock(dev
->bus
);
69 *info
= dev
->desc
->info
;
70 i3c_bus_normaluse_unlock(dev
->bus
);
72 EXPORT_SYMBOL_GPL(i3c_device_get_info
);
75 * i3c_device_disable_ibi() - Disable IBIs coming from a specific device
76 * @dev: device on which IBIs should be disabled
78 * This function disable IBIs coming from a specific device and wait for
79 * all pending IBIs to be processed.
81 * Return: 0 in case of success, a negative error core otherwise.
83 int i3c_device_disable_ibi(struct i3c_device
*dev
)
87 i3c_bus_normaluse_lock(dev
->bus
);
89 mutex_lock(&dev
->desc
->ibi_lock
);
90 ret
= i3c_dev_disable_ibi_locked(dev
->desc
);
91 mutex_unlock(&dev
->desc
->ibi_lock
);
93 i3c_bus_normaluse_unlock(dev
->bus
);
97 EXPORT_SYMBOL_GPL(i3c_device_disable_ibi
);
100 * i3c_device_enable_ibi() - Enable IBIs coming from a specific device
101 * @dev: device on which IBIs should be enabled
103 * This function enable IBIs coming from a specific device and wait for
104 * all pending IBIs to be processed. This should be called on a device
105 * where i3c_device_request_ibi() has succeeded.
107 * Note that IBIs from this device might be received before this function
108 * returns to its caller.
110 * Return: 0 in case of success, a negative error core otherwise.
112 int i3c_device_enable_ibi(struct i3c_device
*dev
)
116 i3c_bus_normaluse_lock(dev
->bus
);
118 mutex_lock(&dev
->desc
->ibi_lock
);
119 ret
= i3c_dev_enable_ibi_locked(dev
->desc
);
120 mutex_unlock(&dev
->desc
->ibi_lock
);
122 i3c_bus_normaluse_unlock(dev
->bus
);
126 EXPORT_SYMBOL_GPL(i3c_device_enable_ibi
);
129 * i3c_device_request_ibi() - Request an IBI
130 * @dev: device for which we should enable IBIs
131 * @req: setup requested for this IBI
133 * This function is responsible for pre-allocating all resources needed to
134 * process IBIs coming from @dev. When this function returns, the IBI is not
135 * enabled until i3c_device_enable_ibi() is called.
137 * Return: 0 in case of success, a negative error core otherwise.
139 int i3c_device_request_ibi(struct i3c_device
*dev
,
140 const struct i3c_ibi_setup
*req
)
144 if (!req
->handler
|| !req
->num_slots
)
147 i3c_bus_normaluse_lock(dev
->bus
);
149 mutex_lock(&dev
->desc
->ibi_lock
);
150 ret
= i3c_dev_request_ibi_locked(dev
->desc
, req
);
151 mutex_unlock(&dev
->desc
->ibi_lock
);
153 i3c_bus_normaluse_unlock(dev
->bus
);
157 EXPORT_SYMBOL_GPL(i3c_device_request_ibi
);
160 * i3c_device_free_ibi() - Free all resources needed for IBI handling
161 * @dev: device on which you want to release IBI resources
163 * This function is responsible for de-allocating resources previously
164 * allocated by i3c_device_request_ibi(). It should be called after disabling
165 * IBIs with i3c_device_disable_ibi().
167 void i3c_device_free_ibi(struct i3c_device
*dev
)
169 i3c_bus_normaluse_lock(dev
->bus
);
171 mutex_lock(&dev
->desc
->ibi_lock
);
172 i3c_dev_free_ibi_locked(dev
->desc
);
173 mutex_unlock(&dev
->desc
->ibi_lock
);
175 i3c_bus_normaluse_unlock(dev
->bus
);
177 EXPORT_SYMBOL_GPL(i3c_device_free_ibi
);
180 * i3cdev_to_dev() - Returns the device embedded in @i3cdev
181 * @i3cdev: I3C device
183 * Return: a pointer to a device object.
185 struct device
*i3cdev_to_dev(struct i3c_device
*i3cdev
)
189 EXPORT_SYMBOL_GPL(i3cdev_to_dev
);
192 * dev_to_i3cdev() - Returns the I3C device containing @dev
193 * @dev: device object
195 * Return: a pointer to an I3C device object.
197 struct i3c_device
*dev_to_i3cdev(struct device
*dev
)
199 return container_of(dev
, struct i3c_device
, dev
);
201 EXPORT_SYMBOL_GPL(dev_to_i3cdev
);
204 * i3c_device_match_id() - Returns the i3c_device_id entry matching @i3cdev
205 * @i3cdev: I3C device
206 * @id_table: I3C device match table
208 * Return: a pointer to an i3c_device_id object or NULL if there's no match.
210 const struct i3c_device_id
*
211 i3c_device_match_id(struct i3c_device
*i3cdev
,
212 const struct i3c_device_id
*id_table
)
214 struct i3c_device_info devinfo
;
215 const struct i3c_device_id
*id
;
217 i3c_device_get_info(i3cdev
, &devinfo
);
220 * The lower 32bits of the provisional ID is just filled with a random
221 * value, try to match using DCR info.
223 if (!I3C_PID_RND_LOWER_32BITS(devinfo
.pid
)) {
224 u16 manuf
= I3C_PID_MANUF_ID(devinfo
.pid
);
225 u16 part
= I3C_PID_PART_ID(devinfo
.pid
);
226 u16 ext_info
= I3C_PID_EXTRA_INFO(devinfo
.pid
);
228 /* First try to match by manufacturer/part ID. */
229 for (id
= id_table
; id
->match_flags
!= 0; id
++) {
230 if ((id
->match_flags
& I3C_MATCH_MANUF_AND_PART
) !=
231 I3C_MATCH_MANUF_AND_PART
)
234 if (manuf
!= id
->manuf_id
|| part
!= id
->part_id
)
237 if ((id
->match_flags
& I3C_MATCH_EXTRA_INFO
) &&
238 ext_info
!= id
->extra_info
)
245 /* Fallback to DCR match. */
246 for (id
= id_table
; id
->match_flags
!= 0; id
++) {
247 if ((id
->match_flags
& I3C_MATCH_DCR
) &&
248 id
->dcr
== devinfo
.dcr
)
254 EXPORT_SYMBOL_GPL(i3c_device_match_id
);
257 * i3c_driver_register_with_owner() - register an I3C device driver
259 * @drv: driver to register
260 * @owner: module that owns this driver
262 * Register @drv to the core.
264 * Return: 0 in case of success, a negative error core otherwise.
266 int i3c_driver_register_with_owner(struct i3c_driver
*drv
, struct module
*owner
)
268 drv
->driver
.owner
= owner
;
269 drv
->driver
.bus
= &i3c_bus_type
;
271 return driver_register(&drv
->driver
);
273 EXPORT_SYMBOL_GPL(i3c_driver_register_with_owner
);
276 * i3c_driver_unregister() - unregister an I3C device driver
278 * @drv: driver to unregister
282 void i3c_driver_unregister(struct i3c_driver
*drv
)
284 driver_unregister(&drv
->driver
);
286 EXPORT_SYMBOL_GPL(i3c_driver_unregister
);