xref: /OK3568_Linux_fs/kernel/drivers/i3c/device.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2018 Cadence Design Systems Inc.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/atomic.h>
9*4882a593Smuzhiyun #include <linux/bug.h>
10*4882a593Smuzhiyun #include <linux/completion.h>
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/mutex.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include "internals.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * i3c_device_do_priv_xfers() - do I3C SDR private transfers directed to a
19*4882a593Smuzhiyun  *				specific device
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * @dev: device with which the transfers should be done
22*4882a593Smuzhiyun  * @xfers: array of transfers
23*4882a593Smuzhiyun  * @nxfers: number of transfers
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * Initiate one or several private SDR transfers with @dev.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * This function can sleep and thus cannot be called in atomic context.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * Return: 0 in case of success, a negative error core otherwise.
30*4882a593Smuzhiyun  */
i3c_device_do_priv_xfers(struct i3c_device * dev,struct i3c_priv_xfer * xfers,int nxfers)31*4882a593Smuzhiyun int i3c_device_do_priv_xfers(struct i3c_device *dev,
32*4882a593Smuzhiyun 			     struct i3c_priv_xfer *xfers,
33*4882a593Smuzhiyun 			     int nxfers)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	int ret, i;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	if (nxfers < 1)
38*4882a593Smuzhiyun 		return 0;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	for (i = 0; i < nxfers; i++) {
41*4882a593Smuzhiyun 		if (!xfers[i].len || !xfers[i].data.in)
42*4882a593Smuzhiyun 			return -EINVAL;
43*4882a593Smuzhiyun 	}
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	i3c_bus_normaluse_lock(dev->bus);
46*4882a593Smuzhiyun 	ret = i3c_dev_do_priv_xfers_locked(dev->desc, xfers, nxfers);
47*4882a593Smuzhiyun 	i3c_bus_normaluse_unlock(dev->bus);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return ret;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_device_do_priv_xfers);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun  * i3c_device_get_info() - get I3C device information
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * @dev: device we want information on
57*4882a593Smuzhiyun  * @info: the information object to fill in
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Retrieve I3C dev info.
60*4882a593Smuzhiyun  */
i3c_device_get_info(struct i3c_device * dev,struct i3c_device_info * info)61*4882a593Smuzhiyun void i3c_device_get_info(struct i3c_device *dev,
62*4882a593Smuzhiyun 			 struct i3c_device_info *info)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	if (!info)
65*4882a593Smuzhiyun 		return;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	i3c_bus_normaluse_lock(dev->bus);
68*4882a593Smuzhiyun 	if (dev->desc)
69*4882a593Smuzhiyun 		*info = dev->desc->info;
70*4882a593Smuzhiyun 	i3c_bus_normaluse_unlock(dev->bus);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_device_get_info);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun  * i3c_device_disable_ibi() - Disable IBIs coming from a specific device
76*4882a593Smuzhiyun  * @dev: device on which IBIs should be disabled
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * This function disable IBIs coming from a specific device and wait for
79*4882a593Smuzhiyun  * all pending IBIs to be processed.
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * Return: 0 in case of success, a negative error core otherwise.
82*4882a593Smuzhiyun  */
i3c_device_disable_ibi(struct i3c_device * dev)83*4882a593Smuzhiyun int i3c_device_disable_ibi(struct i3c_device *dev)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	int ret = -ENOENT;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	i3c_bus_normaluse_lock(dev->bus);
88*4882a593Smuzhiyun 	if (dev->desc) {
89*4882a593Smuzhiyun 		mutex_lock(&dev->desc->ibi_lock);
90*4882a593Smuzhiyun 		ret = i3c_dev_disable_ibi_locked(dev->desc);
91*4882a593Smuzhiyun 		mutex_unlock(&dev->desc->ibi_lock);
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 	i3c_bus_normaluse_unlock(dev->bus);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return ret;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_device_disable_ibi);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun  * i3c_device_enable_ibi() - Enable IBIs coming from a specific device
101*4882a593Smuzhiyun  * @dev: device on which IBIs should be enabled
102*4882a593Smuzhiyun  *
103*4882a593Smuzhiyun  * This function enable IBIs coming from a specific device and wait for
104*4882a593Smuzhiyun  * all pending IBIs to be processed. This should be called on a device
105*4882a593Smuzhiyun  * where i3c_device_request_ibi() has succeeded.
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * Note that IBIs from this device might be received before this function
108*4882a593Smuzhiyun  * returns to its caller.
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  * Return: 0 in case of success, a negative error core otherwise.
111*4882a593Smuzhiyun  */
i3c_device_enable_ibi(struct i3c_device * dev)112*4882a593Smuzhiyun int i3c_device_enable_ibi(struct i3c_device *dev)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	int ret = -ENOENT;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	i3c_bus_normaluse_lock(dev->bus);
117*4882a593Smuzhiyun 	if (dev->desc) {
118*4882a593Smuzhiyun 		mutex_lock(&dev->desc->ibi_lock);
119*4882a593Smuzhiyun 		ret = i3c_dev_enable_ibi_locked(dev->desc);
120*4882a593Smuzhiyun 		mutex_unlock(&dev->desc->ibi_lock);
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 	i3c_bus_normaluse_unlock(dev->bus);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	return ret;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_device_enable_ibi);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /**
129*4882a593Smuzhiyun  * i3c_device_request_ibi() - Request an IBI
130*4882a593Smuzhiyun  * @dev: device for which we should enable IBIs
131*4882a593Smuzhiyun  * @req: setup requested for this IBI
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * This function is responsible for pre-allocating all resources needed to
134*4882a593Smuzhiyun  * process IBIs coming from @dev. When this function returns, the IBI is not
135*4882a593Smuzhiyun  * enabled until i3c_device_enable_ibi() is called.
136*4882a593Smuzhiyun  *
137*4882a593Smuzhiyun  * Return: 0 in case of success, a negative error core otherwise.
138*4882a593Smuzhiyun  */
i3c_device_request_ibi(struct i3c_device * dev,const struct i3c_ibi_setup * req)139*4882a593Smuzhiyun int i3c_device_request_ibi(struct i3c_device *dev,
140*4882a593Smuzhiyun 			   const struct i3c_ibi_setup *req)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	int ret = -ENOENT;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	if (!req->handler || !req->num_slots)
145*4882a593Smuzhiyun 		return -EINVAL;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	i3c_bus_normaluse_lock(dev->bus);
148*4882a593Smuzhiyun 	if (dev->desc) {
149*4882a593Smuzhiyun 		mutex_lock(&dev->desc->ibi_lock);
150*4882a593Smuzhiyun 		ret = i3c_dev_request_ibi_locked(dev->desc, req);
151*4882a593Smuzhiyun 		mutex_unlock(&dev->desc->ibi_lock);
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 	i3c_bus_normaluse_unlock(dev->bus);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	return ret;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_device_request_ibi);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun  * i3c_device_free_ibi() - Free all resources needed for IBI handling
161*4882a593Smuzhiyun  * @dev: device on which you want to release IBI resources
162*4882a593Smuzhiyun  *
163*4882a593Smuzhiyun  * This function is responsible for de-allocating resources previously
164*4882a593Smuzhiyun  * allocated by i3c_device_request_ibi(). It should be called after disabling
165*4882a593Smuzhiyun  * IBIs with i3c_device_disable_ibi().
166*4882a593Smuzhiyun  */
i3c_device_free_ibi(struct i3c_device * dev)167*4882a593Smuzhiyun void i3c_device_free_ibi(struct i3c_device *dev)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	i3c_bus_normaluse_lock(dev->bus);
170*4882a593Smuzhiyun 	if (dev->desc) {
171*4882a593Smuzhiyun 		mutex_lock(&dev->desc->ibi_lock);
172*4882a593Smuzhiyun 		i3c_dev_free_ibi_locked(dev->desc);
173*4882a593Smuzhiyun 		mutex_unlock(&dev->desc->ibi_lock);
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 	i3c_bus_normaluse_unlock(dev->bus);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_device_free_ibi);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /**
180*4882a593Smuzhiyun  * i3cdev_to_dev() - Returns the device embedded in @i3cdev
181*4882a593Smuzhiyun  * @i3cdev: I3C device
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * Return: a pointer to a device object.
184*4882a593Smuzhiyun  */
i3cdev_to_dev(struct i3c_device * i3cdev)185*4882a593Smuzhiyun struct device *i3cdev_to_dev(struct i3c_device *i3cdev)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	return &i3cdev->dev;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3cdev_to_dev);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /**
192*4882a593Smuzhiyun  * dev_to_i3cdev() - Returns the I3C device containing @dev
193*4882a593Smuzhiyun  * @dev: device object
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * Return: a pointer to an I3C device object.
196*4882a593Smuzhiyun  */
dev_to_i3cdev(struct device * dev)197*4882a593Smuzhiyun struct i3c_device *dev_to_i3cdev(struct device *dev)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	return container_of(dev, struct i3c_device, dev);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dev_to_i3cdev);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /**
204*4882a593Smuzhiyun  * i3c_device_match_id() - Returns the i3c_device_id entry matching @i3cdev
205*4882a593Smuzhiyun  * @i3cdev: I3C device
206*4882a593Smuzhiyun  * @id_table: I3C device match table
207*4882a593Smuzhiyun  *
208*4882a593Smuzhiyun  * Return: a pointer to an i3c_device_id object or NULL if there's no match.
209*4882a593Smuzhiyun  */
210*4882a593Smuzhiyun const struct i3c_device_id *
i3c_device_match_id(struct i3c_device * i3cdev,const struct i3c_device_id * id_table)211*4882a593Smuzhiyun i3c_device_match_id(struct i3c_device *i3cdev,
212*4882a593Smuzhiyun 		    const struct i3c_device_id *id_table)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	struct i3c_device_info devinfo;
215*4882a593Smuzhiyun 	const struct i3c_device_id *id;
216*4882a593Smuzhiyun 	u16 manuf, part, ext_info;
217*4882a593Smuzhiyun 	bool rndpid;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	i3c_device_get_info(i3cdev, &devinfo);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	manuf = I3C_PID_MANUF_ID(devinfo.pid);
222*4882a593Smuzhiyun 	part = I3C_PID_PART_ID(devinfo.pid);
223*4882a593Smuzhiyun 	ext_info = I3C_PID_EXTRA_INFO(devinfo.pid);
224*4882a593Smuzhiyun 	rndpid = I3C_PID_RND_LOWER_32BITS(devinfo.pid);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	for (id = id_table; id->match_flags != 0; id++) {
227*4882a593Smuzhiyun 		if ((id->match_flags & I3C_MATCH_DCR) &&
228*4882a593Smuzhiyun 		    id->dcr != devinfo.dcr)
229*4882a593Smuzhiyun 			continue;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 		if ((id->match_flags & I3C_MATCH_MANUF) &&
232*4882a593Smuzhiyun 		    id->manuf_id != manuf)
233*4882a593Smuzhiyun 			continue;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 		if ((id->match_flags & I3C_MATCH_PART) &&
236*4882a593Smuzhiyun 		    (rndpid || id->part_id != part))
237*4882a593Smuzhiyun 			continue;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 		if ((id->match_flags & I3C_MATCH_EXTRA_INFO) &&
240*4882a593Smuzhiyun 		    (rndpid || id->extra_info != ext_info))
241*4882a593Smuzhiyun 			continue;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 		return id;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	return NULL;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_device_match_id);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /**
251*4882a593Smuzhiyun  * i3c_driver_register_with_owner() - register an I3C device driver
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * @drv: driver to register
254*4882a593Smuzhiyun  * @owner: module that owns this driver
255*4882a593Smuzhiyun  *
256*4882a593Smuzhiyun  * Register @drv to the core.
257*4882a593Smuzhiyun  *
258*4882a593Smuzhiyun  * Return: 0 in case of success, a negative error core otherwise.
259*4882a593Smuzhiyun  */
i3c_driver_register_with_owner(struct i3c_driver * drv,struct module * owner)260*4882a593Smuzhiyun int i3c_driver_register_with_owner(struct i3c_driver *drv, struct module *owner)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	drv->driver.owner = owner;
263*4882a593Smuzhiyun 	drv->driver.bus = &i3c_bus_type;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	return driver_register(&drv->driver);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_driver_register_with_owner);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /**
270*4882a593Smuzhiyun  * i3c_driver_unregister() - unregister an I3C device driver
271*4882a593Smuzhiyun  *
272*4882a593Smuzhiyun  * @drv: driver to unregister
273*4882a593Smuzhiyun  *
274*4882a593Smuzhiyun  * Unregister @drv.
275*4882a593Smuzhiyun  */
i3c_driver_unregister(struct i3c_driver * drv)276*4882a593Smuzhiyun void i3c_driver_unregister(struct i3c_driver *drv)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	driver_unregister(&drv->driver);
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i3c_driver_unregister);
281