aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/i3c
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/i3c')
-rw-r--r--include/linux/i3c/ccc.h385
-rw-r--r--include/linux/i3c/device.h331
-rw-r--r--include/linux/i3c/master.h648
3 files changed, 1364 insertions, 0 deletions
diff --git a/include/linux/i3c/ccc.h b/include/linux/i3c/ccc.h
new file mode 100644
index 000000000000..73b0982cc519
--- /dev/null
+++ b/include/linux/i3c/ccc.h
@@ -0,0 +1,385 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2018 Cadence Design Systems Inc.
4 *
5 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6 */
7
8#ifndef I3C_CCC_H
9#define I3C_CCC_H
10
11#include <linux/bitops.h>
12#include <linux/i3c/device.h>
13
14/* I3C CCC (Common Command Codes) related definitions */
15#define I3C_CCC_DIRECT BIT(7)
16
17#define I3C_CCC_ID(id, broadcast) \
18 ((id) | ((broadcast) ? 0 : I3C_CCC_DIRECT))
19
20/* Commands valid in both broadcast and unicast modes */
21#define I3C_CCC_ENEC(broadcast) I3C_CCC_ID(0x0, broadcast)
22#define I3C_CCC_DISEC(broadcast) I3C_CCC_ID(0x1, broadcast)
23#define I3C_CCC_ENTAS(as, broadcast) I3C_CCC_ID(0x2 + (as), broadcast)
24#define I3C_CCC_RSTDAA(broadcast) I3C_CCC_ID(0x6, broadcast)
25#define I3C_CCC_SETMWL(broadcast) I3C_CCC_ID(0x9, broadcast)
26#define I3C_CCC_SETMRL(broadcast) I3C_CCC_ID(0xa, broadcast)
27#define I3C_CCC_SETXTIME(broadcast) ((broadcast) ? 0x28 : 0x98)
28#define I3C_CCC_VENDOR(id, broadcast) ((id) + ((broadcast) ? 0x61 : 0xe0))
29
30/* Broadcast-only commands */
31#define I3C_CCC_ENTDAA I3C_CCC_ID(0x7, true)
32#define I3C_CCC_DEFSLVS I3C_CCC_ID(0x8, true)
33#define I3C_CCC_ENTTM I3C_CCC_ID(0xb, true)
34#define I3C_CCC_ENTHDR(x) I3C_CCC_ID(0x20 + (x), true)
35
36/* Unicast-only commands */
37#define I3C_CCC_SETDASA I3C_CCC_ID(0x7, false)
38#define I3C_CCC_SETNEWDA I3C_CCC_ID(0x8, false)
39#define I3C_CCC_GETMWL I3C_CCC_ID(0xb, false)
40#define I3C_CCC_GETMRL I3C_CCC_ID(0xc, false)
41#define I3C_CCC_GETPID I3C_CCC_ID(0xd, false)
42#define I3C_CCC_GETBCR I3C_CCC_ID(0xe, false)
43#define I3C_CCC_GETDCR I3C_CCC_ID(0xf, false)
44#define I3C_CCC_GETSTATUS I3C_CCC_ID(0x10, false)
45#define I3C_CCC_GETACCMST I3C_CCC_ID(0x11, false)
46#define I3C_CCC_SETBRGTGT I3C_CCC_ID(0x13, false)
47#define I3C_CCC_GETMXDS I3C_CCC_ID(0x14, false)
48#define I3C_CCC_GETHDRCAP I3C_CCC_ID(0x15, false)
49#define I3C_CCC_GETXTIME I3C_CCC_ID(0x19, false)
50
51#define I3C_CCC_EVENT_SIR BIT(0)
52#define I3C_CCC_EVENT_MR BIT(1)
53#define I3C_CCC_EVENT_HJ BIT(3)
54
55/**
56 * struct i3c_ccc_events - payload passed to ENEC/DISEC CCC
57 *
58 * @events: bitmask of I3C_CCC_EVENT_xxx events.
59 *
60 * Depending on the CCC command, the specific events coming from all devices
61 * (broadcast version) or a specific device (unicast version) will be
62 * enabled (ENEC) or disabled (DISEC).
63 */
64struct i3c_ccc_events {
65 u8 events;
66};
67
68/**
69 * struct i3c_ccc_mwl - payload passed to SETMWL/GETMWL CCC
70 *
71 * @len: maximum write length in bytes
72 *
73 * The maximum write length is only applicable to SDR private messages or
74 * extended Write CCCs (like SETXTIME).
75 */
76struct i3c_ccc_mwl {
77 __be16 len;
78};
79
80/**
81 * struct i3c_ccc_mrl - payload passed to SETMRL/GETMRL CCC
82 *
83 * @len: maximum read length in bytes
84 * @ibi_len: maximum IBI payload length
85 *
86 * The maximum read length is only applicable to SDR private messages or
87 * extended Read CCCs (like GETXTIME).
88 * The IBI length is only valid if the I3C slave is IBI capable
89 * (%I3C_BCR_IBI_REQ_CAP is set).
90 */
91struct i3c_ccc_mrl {
92 __be16 read_len;
93 u8 ibi_len;
94} __packed;
95
96/**
97 * struct i3c_ccc_dev_desc - I3C/I2C device descriptor used for DEFSLVS
98 *
99 * @dyn_addr: dynamic address assigned to the I3C slave or 0 if the entry is
100 * describing an I2C slave.
101 * @dcr: DCR value (not applicable to entries describing I2C devices)
102 * @lvr: LVR value (not applicable to entries describing I3C devices)
103 * @bcr: BCR value or 0 if this entry is describing an I2C slave
104 * @static_addr: static address or 0 if the device does not have a static
105 * address
106 *
107 * The DEFSLVS command should be passed an array of i3c_ccc_dev_desc
108 * descriptors (one entry per I3C/I2C dev controlled by the master).
109 */
110struct i3c_ccc_dev_desc {
111 u8 dyn_addr;
112 union {
113 u8 dcr;
114 u8 lvr;
115 };
116 u8 bcr;
117 u8 static_addr;
118};
119
120/**
121 * struct i3c_ccc_defslvs - payload passed to DEFSLVS CCC
122 *
123 * @count: number of dev descriptors
124 * @master: descriptor describing the current master
125 * @slaves: array of descriptors describing slaves controlled by the
126 * current master
127 *
128 * Information passed to the broadcast DEFSLVS to propagate device
129 * information to all masters currently acting as slaves on the bus.
130 * This is only meaningful if you have more than one master.
131 */
132struct i3c_ccc_defslvs {
133 u8 count;
134 struct i3c_ccc_dev_desc master;
135 struct i3c_ccc_dev_desc slaves[0];
136} __packed;
137
138/**
139 * enum i3c_ccc_test_mode - enum listing all available test modes
140 *
141 * @I3C_CCC_EXIT_TEST_MODE: exit test mode
142 * @I3C_CCC_VENDOR_TEST_MODE: enter vendor test mode
143 */
144enum i3c_ccc_test_mode {
145 I3C_CCC_EXIT_TEST_MODE,
146 I3C_CCC_VENDOR_TEST_MODE,
147};
148
149/**
150 * struct i3c_ccc_enttm - payload passed to ENTTM CCC
151 *
152 * @mode: one of the &enum i3c_ccc_test_mode modes
153 *
154 * Information passed to the ENTTM CCC to instruct an I3C device to enter a
155 * specific test mode.
156 */
157struct i3c_ccc_enttm {
158 u8 mode;
159};
160
161/**
162 * struct i3c_ccc_setda - payload passed to SETNEWDA and SETDASA CCCs
163 *
164 * @addr: dynamic address to assign to an I3C device
165 *
166 * Information passed to the SETNEWDA and SETDASA CCCs to assign/change the
167 * dynamic address of an I3C device.
168 */
169struct i3c_ccc_setda {
170 u8 addr;
171};
172
173/**
174 * struct i3c_ccc_getpid - payload passed to GETPID CCC
175 *
176 * @pid: 48 bits PID in big endian
177 */
178struct i3c_ccc_getpid {
179 u8 pid[6];
180};
181
182/**
183 * struct i3c_ccc_getbcr - payload passed to GETBCR CCC
184 *
185 * @bcr: BCR (Bus Characteristic Register) value
186 */
187struct i3c_ccc_getbcr {
188 u8 bcr;
189};
190
191/**
192 * struct i3c_ccc_getdcr - payload passed to GETDCR CCC
193 *
194 * @dcr: DCR (Device Characteristic Register) value
195 */
196struct i3c_ccc_getdcr {
197 u8 dcr;
198};
199
200#define I3C_CCC_STATUS_PENDING_INT(status) ((status) & GENMASK(3, 0))
201#define I3C_CCC_STATUS_PROTOCOL_ERROR BIT(5)
202#define I3C_CCC_STATUS_ACTIVITY_MODE(status) \
203 (((status) & GENMASK(7, 6)) >> 6)
204
205/**
206 * struct i3c_ccc_getstatus - payload passed to GETSTATUS CCC
207 *
208 * @status: status of the I3C slave (see I3C_CCC_STATUS_xxx macros for more
209 * information).
210 */
211struct i3c_ccc_getstatus {
212 __be16 status;
213};
214
215/**
216 * struct i3c_ccc_getaccmst - payload passed to GETACCMST CCC
217 *
218 * @newmaster: address of the master taking bus ownership
219 */
220struct i3c_ccc_getaccmst {
221 u8 newmaster;
222};
223
224/**
225 * struct i3c_ccc_bridged_slave_desc - bridged slave descriptor
226 *
227 * @addr: dynamic address of the bridged device
228 * @id: ID of the slave device behind the bridge
229 */
230struct i3c_ccc_bridged_slave_desc {
231 u8 addr;
232 __be16 id;
233} __packed;
234
235/**
236 * struct i3c_ccc_setbrgtgt - payload passed to SETBRGTGT CCC
237 *
238 * @count: number of bridged slaves
239 * @bslaves: bridged slave descriptors
240 */
241struct i3c_ccc_setbrgtgt {
242 u8 count;
243 struct i3c_ccc_bridged_slave_desc bslaves[0];
244} __packed;
245
246/**
247 * enum i3c_sdr_max_data_rate - max data rate values for private SDR transfers
248 */
249enum i3c_sdr_max_data_rate {
250 I3C_SDR0_FSCL_MAX,
251 I3C_SDR1_FSCL_8MHZ,
252 I3C_SDR2_FSCL_6MHZ,
253 I3C_SDR3_FSCL_4MHZ,
254 I3C_SDR4_FSCL_2MHZ,
255};
256
257/**
258 * enum i3c_tsco - clock to data turn-around
259 */
260enum i3c_tsco {
261 I3C_TSCO_8NS,
262 I3C_TSCO_9NS,
263 I3C_TSCO_10NS,
264 I3C_TSCO_11NS,
265 I3C_TSCO_12NS,
266};
267
268#define I3C_CCC_MAX_SDR_FSCL_MASK GENMASK(2, 0)
269#define I3C_CCC_MAX_SDR_FSCL(x) ((x) & I3C_CCC_MAX_SDR_FSCL_MASK)
270
271/**
272 * struct i3c_ccc_getmxds - payload passed to GETMXDS CCC
273 *
274 * @maxwr: write limitations
275 * @maxrd: read limitations
276 * @maxrdturn: maximum read turn-around expressed micro-seconds and
277 * little-endian formatted
278 */
279struct i3c_ccc_getmxds {
280 u8 maxwr;
281 u8 maxrd;
282 u8 maxrdturn[3];
283} __packed;
284
285#define I3C_CCC_HDR_MODE(mode) BIT(mode)
286
287/**
288 * struct i3c_ccc_gethdrcap - payload passed to GETHDRCAP CCC
289 *
290 * @modes: bitmap of supported HDR modes
291 */
292struct i3c_ccc_gethdrcap {
293 u8 modes;
294} __packed;
295
296/**
297 * enum i3c_ccc_setxtime_subcmd - SETXTIME sub-commands
298 */
299enum i3c_ccc_setxtime_subcmd {
300 I3C_CCC_SETXTIME_ST = 0x7f,
301 I3C_CCC_SETXTIME_DT = 0xbf,
302 I3C_CCC_SETXTIME_ENTER_ASYNC_MODE0 = 0xdf,
303 I3C_CCC_SETXTIME_ENTER_ASYNC_MODE1 = 0xef,
304 I3C_CCC_SETXTIME_ENTER_ASYNC_MODE2 = 0xf7,
305 I3C_CCC_SETXTIME_ENTER_ASYNC_MODE3 = 0xfb,
306 I3C_CCC_SETXTIME_ASYNC_TRIGGER = 0xfd,
307 I3C_CCC_SETXTIME_TPH = 0x3f,
308 I3C_CCC_SETXTIME_TU = 0x9f,
309 I3C_CCC_SETXTIME_ODR = 0x8f,
310};
311
312/**
313 * struct i3c_ccc_setxtime - payload passed to SETXTIME CCC
314 *
315 * @subcmd: one of the sub-commands ddefined in &enum i3c_ccc_setxtime_subcmd
316 * @data: sub-command payload. Amount of data is determined by
317 * &i3c_ccc_setxtime->subcmd
318 */
319struct i3c_ccc_setxtime {
320 u8 subcmd;
321 u8 data[0];
322} __packed;
323
324#define I3C_CCC_GETXTIME_SYNC_MODE BIT(0)
325#define I3C_CCC_GETXTIME_ASYNC_MODE(x) BIT((x) + 1)
326#define I3C_CCC_GETXTIME_OVERFLOW BIT(7)
327
328/**
329 * struct i3c_ccc_getxtime - payload retrieved from GETXTIME CCC
330 *
331 * @supported_modes: bitmap describing supported XTIME modes
332 * @state: current status (enabled mode and overflow status)
333 * @frequency: slave's internal oscillator frequency in 500KHz steps
334 * @inaccuracy: slave's internal oscillator inaccuracy in 0.1% steps
335 */
336struct i3c_ccc_getxtime {
337 u8 supported_modes;
338 u8 state;
339 u8 frequency;
340 u8 inaccuracy;
341} __packed;
342
343/**
344 * struct i3c_ccc_cmd_payload - CCC payload
345 *
346 * @len: payload length
347 * @data: payload data. This buffer must be DMA-able
348 */
349struct i3c_ccc_cmd_payload {
350 u16 len;
351 void *data;
352};
353
354/**
355 * struct i3c_ccc_cmd_dest - CCC command destination
356 *
357 * @addr: can be an I3C device address or the broadcast address if this is a
358 * broadcast CCC
359 * @payload: payload to be sent to this device or broadcasted
360 */
361struct i3c_ccc_cmd_dest {
362 u8 addr;
363 struct i3c_ccc_cmd_payload payload;
364};
365
366/**
367 * struct i3c_ccc_cmd - CCC command
368 *
369 * @rnw: true if the CCC should retrieve data from the device. Only valid for
370 * unicast commands
371 * @id: CCC command id
372 * @ndests: number of destinations. Should always be one for broadcast commands
373 * @dests: array of destinations and associated payload for this CCC. Most of
374 * the time, only one destination is provided
375 * @err: I3C error code
376 */
377struct i3c_ccc_cmd {
378 u8 rnw;
379 u8 id;
380 unsigned int ndests;
381 struct i3c_ccc_cmd_dest *dests;
382 enum i3c_error_code err;
383};
384
385#endif /* I3C_CCC_H */
diff --git a/include/linux/i3c/device.h b/include/linux/i3c/device.h
new file mode 100644
index 000000000000..5ecb055fd375
--- /dev/null
+++ b/include/linux/i3c/device.h
@@ -0,0 +1,331 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2018 Cadence Design Systems Inc.
4 *
5 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6 */
7
8#ifndef I3C_DEV_H
9#define I3C_DEV_H
10
11#include <linux/bitops.h>
12#include <linux/device.h>
13#include <linux/i2c.h>
14#include <linux/kconfig.h>
15#include <linux/mod_devicetable.h>
16#include <linux/module.h>
17
18/**
19 * enum i3c_error_code - I3C error codes
20 *
21 * These are the standard error codes as defined by the I3C specification.
22 * When -EIO is returned by the i3c_device_do_priv_xfers() or
23 * i3c_device_send_hdr_cmds() one can check the error code in
24 * &struct_i3c_priv_xfer.err or &struct i3c_hdr_cmd.err to get a better idea of
25 * what went wrong.
26 *
27 * @I3C_ERROR_UNKNOWN: unknown error, usually means the error is not I3C
28 * related
29 * @I3C_ERROR_M0: M0 error
30 * @I3C_ERROR_M1: M1 error
31 * @I3C_ERROR_M2: M2 error
32 */
33enum i3c_error_code {
34 I3C_ERROR_UNKNOWN = 0,
35 I3C_ERROR_M0 = 1,
36 I3C_ERROR_M1,
37 I3C_ERROR_M2,
38};
39
40/**
41 * enum i3c_hdr_mode - HDR mode ids
42 * @I3C_HDR_DDR: DDR mode
43 * @I3C_HDR_TSP: TSP mode
44 * @I3C_HDR_TSL: TSL mode
45 */
46enum i3c_hdr_mode {
47 I3C_HDR_DDR,
48 I3C_HDR_TSP,
49 I3C_HDR_TSL,
50};
51
52/**
53 * struct i3c_priv_xfer - I3C SDR private transfer
54 * @rnw: encodes the transfer direction. true for a read, false for a write
55 * @len: transfer length in bytes of the transfer
56 * @data: input/output buffer
57 * @data.in: input buffer. Must point to a DMA-able buffer
58 * @data.out: output buffer. Must point to a DMA-able buffer
59 * @err: I3C error code
60 */
61struct i3c_priv_xfer {
62 u8 rnw;
63 u16 len;
64 union {
65 void *in;
66 const void *out;
67 } data;
68 enum i3c_error_code err;
69};
70
71/**
72 * enum i3c_dcr - I3C DCR values
73 * @I3C_DCR_GENERIC_DEVICE: generic I3C device
74 */
75enum i3c_dcr {
76 I3C_DCR_GENERIC_DEVICE = 0,
77};
78
79#define I3C_PID_MANUF_ID(pid) (((pid) & GENMASK_ULL(47, 33)) >> 33)
80#define I3C_PID_RND_LOWER_32BITS(pid) (!!((pid) & BIT_ULL(32)))
81#define I3C_PID_RND_VAL(pid) ((pid) & GENMASK_ULL(31, 0))
82#define I3C_PID_PART_ID(pid) (((pid) & GENMASK_ULL(31, 16)) >> 16)
83#define I3C_PID_INSTANCE_ID(pid) (((pid) & GENMASK_ULL(15, 12)) >> 12)
84#define I3C_PID_EXTRA_INFO(pid) ((pid) & GENMASK_ULL(11, 0))
85
86#define I3C_BCR_DEVICE_ROLE(bcr) ((bcr) & GENMASK(7, 6))
87#define I3C_BCR_I3C_SLAVE (0 << 6)
88#define I3C_BCR_I3C_MASTER (1 << 6)
89#define I3C_BCR_HDR_CAP BIT(5)
90#define I3C_BCR_BRIDGE BIT(4)
91#define I3C_BCR_OFFLINE_CAP BIT(3)
92#define I3C_BCR_IBI_PAYLOAD BIT(2)
93#define I3C_BCR_IBI_REQ_CAP BIT(1)
94#define I3C_BCR_MAX_DATA_SPEED_LIM BIT(0)
95
96/**
97 * struct i3c_device_info - I3C device information
98 * @pid: Provisional ID
99 * @bcr: Bus Characteristic Register
100 * @dcr: Device Characteristic Register
101 * @static_addr: static/I2C address
102 * @dyn_addr: dynamic address
103 * @hdr_cap: supported HDR modes
104 * @max_read_ds: max read speed information
105 * @max_write_ds: max write speed information
106 * @max_ibi_len: max IBI payload length
107 * @max_read_turnaround: max read turn-around time in micro-seconds
108 * @max_read_len: max private SDR read length in bytes
109 * @max_write_len: max private SDR write length in bytes
110 *
111 * These are all basic information that should be advertised by an I3C device.
112 * Some of them are optional depending on the device type and device
113 * capabilities.
114 * For each I3C slave attached to a master with
115 * i3c_master_add_i3c_dev_locked(), the core will send the relevant CCC command
116 * to retrieve these data.
117 */
118struct i3c_device_info {
119 u64 pid;
120 u8 bcr;
121 u8 dcr;
122 u8 static_addr;
123 u8 dyn_addr;
124 u8 hdr_cap;
125 u8 max_read_ds;
126 u8 max_write_ds;
127 u8 max_ibi_len;
128 u32 max_read_turnaround;
129 u16 max_read_len;
130 u16 max_write_len;
131};
132
133/*
134 * I3C device internals are kept hidden from I3C device users. It's just
135 * simpler to refactor things when everything goes through getter/setters, and
136 * I3C device drivers should not have to worry about internal representation
137 * anyway.
138 */
139struct i3c_device;
140
141/* These macros should be used to i3c_device_id entries. */
142#define I3C_MATCH_MANUF_AND_PART (I3C_MATCH_MANUF | I3C_MATCH_PART)
143
144#define I3C_DEVICE(_manufid, _partid, _drvdata) \
145 { \
146 .match_flags = I3C_MATCH_MANUF_AND_PART, \
147 .manuf_id = _manufid, \
148 .part_id = _partid, \
149 .data = _drvdata, \
150 }
151
152#define I3C_DEVICE_EXTRA_INFO(_manufid, _partid, _info, _drvdata) \
153 { \
154 .match_flags = I3C_MATCH_MANUF_AND_PART | \
155 I3C_MATCH_EXTRA_INFO, \
156 .manuf_id = _manufid, \
157 .part_id = _partid, \
158 .extra_info = _info, \
159 .data = _drvdata, \
160 }
161
162#define I3C_CLASS(_dcr, _drvdata) \
163 { \
164 .match_flags = I3C_MATCH_DCR, \
165 .dcr = _dcr, \
166 }
167
168/**
169 * struct i3c_driver - I3C device driver
170 * @driver: inherit from device_driver
171 * @probe: I3C device probe method
172 * @remove: I3C device remove method
173 * @id_table: I3C device match table. Will be used by the framework to decide
174 * which device to bind to this driver
175 */
176struct i3c_driver {
177 struct device_driver driver;
178 int (*probe)(struct i3c_device *dev);
179 int (*remove)(struct i3c_device *dev);
180 const struct i3c_device_id *id_table;
181};
182
183static inline struct i3c_driver *drv_to_i3cdrv(struct device_driver *drv)
184{
185 return container_of(drv, struct i3c_driver, driver);
186}
187
188struct device *i3cdev_to_dev(struct i3c_device *i3cdev);
189struct i3c_device *dev_to_i3cdev(struct device *dev);
190
191static inline void i3cdev_set_drvdata(struct i3c_device *i3cdev,
192 void *data)
193{
194 struct device *dev = i3cdev_to_dev(i3cdev);
195
196 dev_set_drvdata(dev, data);
197}
198
199static inline void *i3cdev_get_drvdata(struct i3c_device *i3cdev)
200{
201 struct device *dev = i3cdev_to_dev(i3cdev);
202
203 return dev_get_drvdata(dev);
204}
205
206int i3c_driver_register_with_owner(struct i3c_driver *drv,
207 struct module *owner);
208void i3c_driver_unregister(struct i3c_driver *drv);
209
210#define i3c_driver_register(__drv) \
211 i3c_driver_register_with_owner(__drv, THIS_MODULE)
212
213/**
214 * module_i3c_driver() - Register a module providing an I3C driver
215 * @__drv: the I3C driver to register
216 *
217 * Provide generic init/exit functions that simply register/unregister an I3C
218 * driver.
219 * Should be used by any driver that does not require extra init/cleanup steps.
220 */
221#define module_i3c_driver(__drv) \
222 module_driver(__drv, i3c_driver_register, i3c_driver_unregister)
223
224/**
225 * i3c_i2c_driver_register() - Register an i2c and an i3c driver
226 * @i3cdrv: the I3C driver to register
227 * @i2cdrv: the I2C driver to register
228 *
229 * This function registers both @i2cdev and @i3cdev, and fails if one of these
230 * registrations fails. This is mainly useful for devices that support both I2C
231 * and I3C modes.
232 * Note that when CONFIG_I3C is not enabled, this function only registers the
233 * I2C driver.
234 *
235 * Return: 0 if both registrations succeeds, a negative error code otherwise.
236 */
237static inline int i3c_i2c_driver_register(struct i3c_driver *i3cdrv,
238 struct i2c_driver *i2cdrv)
239{
240 int ret;
241
242 ret = i2c_add_driver(i2cdrv);
243 if (ret || !IS_ENABLED(CONFIG_I3C))
244 return ret;
245
246 ret = i3c_driver_register(i3cdrv);
247 if (ret)
248 i2c_del_driver(i2cdrv);
249
250 return ret;
251}
252
253/**
254 * i3c_i2c_driver_unregister() - Unregister an i2c and an i3c driver
255 * @i3cdrv: the I3C driver to register
256 * @i2cdrv: the I2C driver to register
257 *
258 * This function unregisters both @i3cdrv and @i2cdrv.
259 * Note that when CONFIG_I3C is not enabled, this function only unregisters the
260 * @i2cdrv.
261 */
262static inline void i3c_i2c_driver_unregister(struct i3c_driver *i3cdrv,
263 struct i2c_driver *i2cdrv)
264{
265 if (IS_ENABLED(CONFIG_I3C))
266 i3c_driver_unregister(i3cdrv);
267
268 i2c_del_driver(i2cdrv);
269}
270
271/**
272 * module_i3c_i2c_driver() - Register a module providing an I3C and an I2C
273 * driver
274 * @__i3cdrv: the I3C driver to register
275 * @__i2cdrv: the I3C driver to register
276 *
277 * Provide generic init/exit functions that simply register/unregister an I3C
278 * and an I2C driver.
279 * This macro can be used even if CONFIG_I3C is disabled, in this case, only
280 * the I2C driver will be registered.
281 * Should be used by any driver that does not require extra init/cleanup steps.
282 */
283#define module_i3c_i2c_driver(__i3cdrv, __i2cdrv) \
284 module_driver(__i3cdrv, \
285 i3c_i2c_driver_register, \
286 i3c_i2c_driver_unregister)
287
288int i3c_device_do_priv_xfers(struct i3c_device *dev,
289 struct i3c_priv_xfer *xfers,
290 int nxfers);
291
292void i3c_device_get_info(struct i3c_device *dev, struct i3c_device_info *info);
293
294struct i3c_ibi_payload {
295 unsigned int len;
296 const void *data;
297};
298
299/**
300 * struct i3c_ibi_setup - IBI setup object
301 * @max_payload_len: maximum length of the payload associated to an IBI. If one
302 * IBI appears to have a payload that is bigger than this
303 * number, the IBI will be rejected.
304 * @num_slots: number of pre-allocated IBI slots. This should be chosen so that
305 * the system never runs out of IBI slots, otherwise you'll lose
306 * IBIs.
307 * @handler: IBI handler, every time an IBI is received. This handler is called
308 * in a workqueue context. It is allowed to sleep and send new
309 * messages on the bus, though it's recommended to keep the
310 * processing done there as fast as possible to avoid delaying
311 * processing of other queued on the same workqueue.
312 *
313 * Temporary structure used to pass information to i3c_device_request_ibi().
314 * This object can be allocated on the stack since i3c_device_request_ibi()
315 * copies every bit of information and do not use it after
316 * i3c_device_request_ibi() has returned.
317 */
318struct i3c_ibi_setup {
319 unsigned int max_payload_len;
320 unsigned int num_slots;
321 void (*handler)(struct i3c_device *dev,
322 const struct i3c_ibi_payload *payload);
323};
324
325int i3c_device_request_ibi(struct i3c_device *dev,
326 const struct i3c_ibi_setup *setup);
327void i3c_device_free_ibi(struct i3c_device *dev);
328int i3c_device_enable_ibi(struct i3c_device *dev);
329int i3c_device_disable_ibi(struct i3c_device *dev);
330
331#endif /* I3C_DEV_H */
diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h
new file mode 100644
index 000000000000..f13fd8b1dd79
--- /dev/null
+++ b/include/linux/i3c/master.h
@@ -0,0 +1,648 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2018 Cadence Design Systems Inc.
4 *
5 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6 */
7
8#ifndef I3C_MASTER_H
9#define I3C_MASTER_H
10
11#include <asm/bitsperlong.h>
12
13#include <linux/bitops.h>
14#include <linux/i2c.h>
15#include <linux/i3c/ccc.h>
16#include <linux/i3c/device.h>
17#include <linux/rwsem.h>
18#include <linux/spinlock.h>
19#include <linux/workqueue.h>
20
21#define I3C_HOT_JOIN_ADDR 0x2
22#define I3C_BROADCAST_ADDR 0x7e
23#define I3C_MAX_ADDR GENMASK(6, 0)
24
25struct i3c_master_controller;
26struct i3c_bus;
27struct i2c_device;
28struct i3c_device;
29
30/**
31 * struct i3c_i2c_dev_desc - Common part of the I3C/I2C device descriptor
32 * @node: node element used to insert the slot into the I2C or I3C device
33 * list
34 * @master: I3C master that instantiated this device. Will be used to do
35 * I2C/I3C transfers
36 * @master_priv: master private data assigned to the device. Can be used to
37 * add master specific information
38 *
39 * This structure is describing common I3C/I2C dev information.
40 */
41struct i3c_i2c_dev_desc {
42 struct list_head node;
43 struct i3c_master_controller *master;
44 void *master_priv;
45};
46
47#define I3C_LVR_I2C_INDEX_MASK GENMASK(7, 5)
48#define I3C_LVR_I2C_INDEX(x) ((x) << 5)
49#define I3C_LVR_I2C_FM_MODE BIT(4)
50
51#define I2C_MAX_ADDR GENMASK(9, 0)
52
53/**
54 * struct i2c_dev_boardinfo - I2C device board information
55 * @node: used to insert the boardinfo object in the I2C boardinfo list
56 * @base: regular I2C board information
57 * @lvr: LVR (Legacy Virtual Register) needed by the I3C core to know about
58 * the I2C device limitations
59 *
60 * This structure is used to attach board-level information to an I2C device.
61 * Each I2C device connected on the I3C bus should have one.
62 */
63struct i2c_dev_boardinfo {
64 struct list_head node;
65 struct i2c_board_info base;
66 u8 lvr;
67};
68
69/**
70 * struct i2c_dev_desc - I2C device descriptor
71 * @common: common part of the I2C device descriptor
72 * @boardinfo: pointer to the boardinfo attached to this I2C device
73 * @dev: I2C device object registered to the I2C framework
74 *
75 * Each I2C device connected on the bus will have an i2c_dev_desc.
76 * This object is created by the core and later attached to the controller
77 * using &struct_i3c_master_controller->ops->attach_i2c_dev().
78 *
79 * &struct_i2c_dev_desc is the internal representation of an I2C device
80 * connected on an I3C bus. This object is also passed to all
81 * &struct_i3c_master_controller_ops hooks.
82 */
83struct i2c_dev_desc {
84 struct i3c_i2c_dev_desc common;
85 const struct i2c_dev_boardinfo *boardinfo;
86 struct i2c_client *dev;
87};
88
89/**
90 * struct i3c_ibi_slot - I3C IBI (In-Band Interrupt) slot
91 * @work: work associated to this slot. The IBI handler will be called from
92 * there
93 * @dev: the I3C device that has generated this IBI
94 * @len: length of the payload associated to this IBI
95 * @data: payload buffer
96 *
97 * An IBI slot is an object pre-allocated by the controller and used when an
98 * IBI comes in.
99 * Every time an IBI comes in, the I3C master driver should find a free IBI
100 * slot in its IBI slot pool, retrieve the IBI payload and queue the IBI using
101 * i3c_master_queue_ibi().
102 *
103 * How IBI slots are allocated is left to the I3C master driver, though, for
104 * simple kmalloc-based allocation, the generic IBI slot pool can be used.
105 */
106struct i3c_ibi_slot {
107 struct work_struct work;
108 struct i3c_dev_desc *dev;
109 unsigned int len;
110 void *data;
111};
112
113/**
114 * struct i3c_device_ibi_info - IBI information attached to a specific device
115 * @all_ibis_handled: used to be informed when no more IBIs are waiting to be
116 * processed. Used by i3c_device_disable_ibi() to wait for
117 * all IBIs to be dequeued
118 * @pending_ibis: count the number of pending IBIs. Each pending IBI has its
119 * work element queued to the controller workqueue
120 * @max_payload_len: maximum payload length for an IBI coming from this device.
121 * this value is specified when calling
122 * i3c_device_request_ibi() and should not change at run
123 * time. All messages IBIs exceeding this limit should be
124 * rejected by the master
125 * @num_slots: number of IBI slots reserved for this device
126 * @enabled: reflect the IBI status
127 * @handler: IBI handler specified at i3c_device_request_ibi() call time. This
128 * handler will be called from the controller workqueue, and as such
129 * is allowed to sleep (though it is recommended to process the IBI
130 * as fast as possible to not stall processing of other IBIs queued
131 * on the same workqueue).
132 * New I3C messages can be sent from the IBI handler
133 *
134 * The &struct_i3c_device_ibi_info object is allocated when
135 * i3c_device_request_ibi() is called and attached to a specific device. This
136 * object is here to manage IBIs coming from a specific I3C device.
137 *
138 * Note that this structure is the generic view of the IBI management
139 * infrastructure. I3C master drivers may have their own internal
140 * representation which they can associate to the device using
141 * controller-private data.
142 */
143struct i3c_device_ibi_info {
144 struct completion all_ibis_handled;
145 atomic_t pending_ibis;
146 unsigned int max_payload_len;
147 unsigned int num_slots;
148 unsigned int enabled;
149 void (*handler)(struct i3c_device *dev,
150 const struct i3c_ibi_payload *payload);
151};
152
153/**
154 * struct i3c_dev_boardinfo - I3C device board information
155 * @node: used to insert the boardinfo object in the I3C boardinfo list
156 * @init_dyn_addr: initial dynamic address requested by the FW. We provide no
157 * guarantee that the device will end up using this address,
158 * but try our best to assign this specific address to the
159 * device
160 * @static_addr: static address the I3C device listen on before it's been
161 * assigned a dynamic address by the master. Will be used during
162 * bus initialization to assign it a specific dynamic address
163 * before starting DAA (Dynamic Address Assignment)
164 * @pid: I3C Provisional ID exposed by the device. This is a unique identifier
165 * that may be used to attach boardinfo to i3c_dev_desc when the device
166 * does not have a static address
167 * @of_node: optional DT node in case the device has been described in the DT
168 *
169 * This structure is used to attach board-level information to an I3C device.
170 * Not all I3C devices connected on the bus will have a boardinfo. It's only
171 * needed if you want to attach extra resources to a device or assign it a
172 * specific dynamic address.
173 */
174struct i3c_dev_boardinfo {
175 struct list_head node;
176 u8 init_dyn_addr;
177 u8 static_addr;
178 u64 pid;
179 struct device_node *of_node;
180};
181
182/**
183 * struct i3c_dev_desc - I3C device descriptor
184 * @common: common part of the I3C device descriptor
185 * @info: I3C device information. Will be automatically filled when you create
186 * your device with i3c_master_add_i3c_dev_locked()
187 * @ibi_lock: lock used to protect the &struct_i3c_device->ibi
188 * @ibi: IBI info attached to a device. Should be NULL until
189 * i3c_device_request_ibi() is called
190 * @dev: pointer to the I3C device object exposed to I3C device drivers. This
191 * should never be accessed from I3C master controller drivers. Only core
192 * code should manipulate it in when updating the dev <-> desc link or
193 * when propagating IBI events to the driver
194 * @boardinfo: pointer to the boardinfo attached to this I3C device
195 *
196 * Internal representation of an I3C device. This object is only used by the
197 * core and passed to I3C master controller drivers when they're requested to
198 * do some operations on the device.
199 * The core maintains the link between the internal I3C dev descriptor and the
200 * object exposed to the I3C device drivers (&struct_i3c_device).
201 */
202struct i3c_dev_desc {
203 struct i3c_i2c_dev_desc common;
204 struct i3c_device_info info;
205 struct mutex ibi_lock;
206 struct i3c_device_ibi_info *ibi;
207 struct i3c_device *dev;
208 const struct i3c_dev_boardinfo *boardinfo;
209};
210
211/**
212 * struct i3c_device - I3C device object
213 * @dev: device object to register the I3C dev to the device model
214 * @desc: pointer to an i3c device descriptor object. This link is updated
215 * every time the I3C device is rediscovered with a different dynamic
216 * address assigned
217 * @bus: I3C bus this device is attached to
218 *
219 * I3C device object exposed to I3C device drivers. The takes care of linking
220 * this object to the relevant &struct_i3c_dev_desc one.
221 * All I3C devs on the I3C bus are represented, including I3C masters. For each
222 * of them, we have an instance of &struct i3c_device.
223 */
224struct i3c_device {
225 struct device dev;
226 struct i3c_dev_desc *desc;
227 struct i3c_bus *bus;
228};
229
230/*
231 * The I3C specification says the maximum number of devices connected on the
232 * bus is 11, but this number depends on external parameters like trace length,
233 * capacitive load per Device, and the types of Devices present on the Bus.
234 * I3C master can also have limitations, so this number is just here as a
235 * reference and should be adjusted on a per-controller/per-board basis.
236 */
237#define I3C_BUS_MAX_DEVS 11
238
239#define I3C_BUS_MAX_I3C_SCL_RATE 12900000
240#define I3C_BUS_TYP_I3C_SCL_RATE 12500000
241#define I3C_BUS_I2C_FM_PLUS_SCL_RATE 1000000
242#define I3C_BUS_I2C_FM_SCL_RATE 400000
243#define I3C_BUS_TLOW_OD_MIN_NS 200
244
245/**
246 * enum i3c_bus_mode - I3C bus mode
247 * @I3C_BUS_MODE_PURE: only I3C devices are connected to the bus. No limitation
248 * expected
249 * @I3C_BUS_MODE_MIXED_FAST: I2C devices with 50ns spike filter are present on
250 * the bus. The only impact in this mode is that the
251 * high SCL pulse has to stay below 50ns to trick I2C
252 * devices when transmitting I3C frames
253 * @I3C_BUS_MODE_MIXED_SLOW: I2C devices without 50ns spike filter are present
254 * on the bus
255 */
256enum i3c_bus_mode {
257 I3C_BUS_MODE_PURE,
258 I3C_BUS_MODE_MIXED_FAST,
259 I3C_BUS_MODE_MIXED_SLOW,
260};
261
262/**
263 * enum i3c_addr_slot_status - I3C address slot status
264 * @I3C_ADDR_SLOT_FREE: address is free
265 * @I3C_ADDR_SLOT_RSVD: address is reserved
266 * @I3C_ADDR_SLOT_I2C_DEV: address is assigned to an I2C device
267 * @I3C_ADDR_SLOT_I3C_DEV: address is assigned to an I3C device
268 * @I3C_ADDR_SLOT_STATUS_MASK: address slot mask
269 *
270 * On an I3C bus, addresses are assigned dynamically, and we need to know which
271 * addresses are free to use and which ones are already assigned.
272 *
273 * Addresses marked as reserved are those reserved by the I3C protocol
274 * (broadcast address, ...).
275 */
276enum i3c_addr_slot_status {
277 I3C_ADDR_SLOT_FREE,
278 I3C_ADDR_SLOT_RSVD,
279 I3C_ADDR_SLOT_I2C_DEV,
280 I3C_ADDR_SLOT_I3C_DEV,
281 I3C_ADDR_SLOT_STATUS_MASK = 3,
282};
283
284/**
285 * struct i3c_bus - I3C bus object
286 * @cur_master: I3C master currently driving the bus. Since I3C is multi-master
287 * this can change over the time. Will be used to let a master
288 * know whether it needs to request bus ownership before sending
289 * a frame or not
290 * @id: bus ID. Assigned by the framework when register the bus
291 * @addrslots: a bitmap with 2-bits per-slot to encode the address status and
292 * ease the DAA (Dynamic Address Assignment) procedure (see
293 * &enum i3c_addr_slot_status)
294 * @mode: bus mode (see &enum i3c_bus_mode)
295 * @scl_rate.i3c: maximum rate for the clock signal when doing I3C SDR/priv
296 * transfers
297 * @scl_rate.i2c: maximum rate for the clock signal when doing I2C transfers
298 * @scl_rate: SCL signal rate for I3C and I2C mode
299 * @devs.i3c: contains a list of I3C device descriptors representing I3C
300 * devices connected on the bus and successfully attached to the
301 * I3C master
302 * @devs.i2c: contains a list of I2C device descriptors representing I2C
303 * devices connected on the bus and successfully attached to the
304 * I3C master
305 * @devs: 2 lists containing all I3C/I2C devices connected to the bus
306 * @lock: read/write lock on the bus. This is needed to protect against
307 * operations that have an impact on the whole bus and the devices
308 * connected to it. For example, when asking slaves to drop their
309 * dynamic address (RSTDAA CCC), we need to make sure no one is trying
310 * to send I3C frames to these devices.
311 * Note that this lock does not protect against concurrency between
312 * devices: several drivers can send different I3C/I2C frames through
313 * the same master in parallel. This is the responsibility of the
314 * master to guarantee that frames are actually sent sequentially and
315 * not interlaced
316 *
317 * The I3C bus is represented with its own object and not implicitly described
318 * by the I3C master to cope with the multi-master functionality, where one bus
319 * can be shared amongst several masters, each of them requesting bus ownership
320 * when they need to.
321 */
322struct i3c_bus {
323 struct i3c_dev_desc *cur_master;
324 int id;
325 unsigned long addrslots[((I2C_MAX_ADDR + 1) * 2) / BITS_PER_LONG];
326 enum i3c_bus_mode mode;
327 struct {
328 unsigned long i3c;
329 unsigned long i2c;
330 } scl_rate;
331 struct {
332 struct list_head i3c;
333 struct list_head i2c;
334 } devs;
335 struct rw_semaphore lock;
336};
337
338/**
339 * struct i3c_master_controller_ops - I3C master methods
340 * @bus_init: hook responsible for the I3C bus initialization. You should at
341 * least call master_set_info() from there and set the bus mode.
342 * You can also put controller specific initialization in there.
343 * This method is mandatory.
344 * @bus_cleanup: cleanup everything done in
345 * &i3c_master_controller_ops->bus_init().
346 * This method is optional.
347 * @attach_i3c_dev: called every time an I3C device is attached to the bus. It
348 * can be after a DAA or when a device is statically declared
349 * by the FW, in which case it will only have a static address
350 * and the dynamic address will be 0.
351 * When this function is called, device information have not
352 * been retrieved yet.
353 * This is a good place to attach master controller specific
354 * data to I3C devices.
355 * This method is optional.
356 * @reattach_i3c_dev: called every time an I3C device has its addressed
357 * changed. It can be because the device has been powered
358 * down and has lost its address, or it can happen when a
359 * device had a static address and has been assigned a
360 * dynamic address with SETDASA.
361 * This method is optional.
362 * @detach_i3c_dev: called when an I3C device is detached from the bus. Usually
363 * happens when the master device is unregistered.
364 * This method is optional.
365 * @do_daa: do a DAA (Dynamic Address Assignment) procedure. This is procedure
366 * should send an ENTDAA CCC command and then add all devices
367 * discovered sure the DAA using i3c_master_add_i3c_dev_locked().
368 * Add devices added with i3c_master_add_i3c_dev_locked() will then be
369 * attached or re-attached to the controller.
370 * This method is mandatory.
371 * @supports_ccc_cmd: should return true if the CCC command is supported, false
372 * otherwise.
373 * This method is optional, if not provided the core assumes
374 * all CCC commands are supported.
375 * @send_ccc_cmd: send a CCC command
376 * This method is mandatory.
377 * @priv_xfers: do one or several private I3C SDR transfers
378 * This method is mandatory.
379 * @attach_i2c_dev: called every time an I2C device is attached to the bus.
380 * This is a good place to attach master controller specific
381 * data to I2C devices.
382 * This method is optional.
383 * @detach_i2c_dev: called when an I2C device is detached from the bus. Usually
384 * happens when the master device is unregistered.
385 * This method is optional.
386 * @i2c_xfers: do one or several I2C transfers. Note that, unlike i3c
387 * transfers, the core does not guarantee that buffers attached to
388 * the transfers are DMA-safe. If drivers want to have DMA-safe
389 * buffers, they should use the i2c_get_dma_safe_msg_buf()
390 * and i2c_put_dma_safe_msg_buf() helpers provided by the I2C
391 * framework.
392 * This method is mandatory.
393 * @i2c_funcs: expose the supported I2C functionalities.
394 * This method is mandatory.
395 * @request_ibi: attach an IBI handler to an I3C device. This implies defining
396 * an IBI handler and the constraints of the IBI (maximum payload
397 * length and number of pre-allocated slots).
398 * Some controllers support less IBI-capable devices than regular
399 * devices, so this method might return -%EBUSY if there's no
400 * more space for an extra IBI registration
401 * This method is optional.
402 * @free_ibi: free an IBI previously requested with ->request_ibi(). The IBI
403 * should have been disabled with ->disable_irq() prior to that
404 * This method is mandatory only if ->request_ibi is not NULL.
405 * @enable_ibi: enable the IBI. Only valid if ->request_ibi() has been called
406 * prior to ->enable_ibi(). The controller should first enable
407 * the IBI on the controller end (for example, unmask the hardware
408 * IRQ) and then send the ENEC CCC command (with the IBI flag set)
409 * to the I3C device.
410 * This method is mandatory only if ->request_ibi is not NULL.
411 * @disable_ibi: disable an IBI. First send the DISEC CCC command with the IBI
412 * flag set and then deactivate the hardware IRQ on the
413 * controller end.
414 * This method is mandatory only if ->request_ibi is not NULL.
415 * @recycle_ibi_slot: recycle an IBI slot. Called every time an IBI has been
416 * processed by its handler. The IBI slot should be put back
417 * in the IBI slot pool so that the controller can re-use it
418 * for a future IBI
419 * This method is mandatory only if ->request_ibi is not
420 * NULL.
421 */
422struct i3c_master_controller_ops {
423 int (*bus_init)(struct i3c_master_controller *master);
424 void (*bus_cleanup)(struct i3c_master_controller *master);
425 int (*attach_i3c_dev)(struct i3c_dev_desc *dev);
426 int (*reattach_i3c_dev)(struct i3c_dev_desc *dev, u8 old_dyn_addr);
427 void (*detach_i3c_dev)(struct i3c_dev_desc *dev);
428 int (*do_daa)(struct i3c_master_controller *master);
429 bool (*supports_ccc_cmd)(struct i3c_master_controller *master,
430 const struct i3c_ccc_cmd *cmd);
431 int (*send_ccc_cmd)(struct i3c_master_controller *master,
432 struct i3c_ccc_cmd *cmd);
433 int (*priv_xfers)(struct i3c_dev_desc *dev,
434 struct i3c_priv_xfer *xfers,
435 int nxfers);
436 int (*attach_i2c_dev)(struct i2c_dev_desc *dev);
437 void (*detach_i2c_dev)(struct i2c_dev_desc *dev);
438 int (*i2c_xfers)(struct i2c_dev_desc *dev,
439 const struct i2c_msg *xfers, int nxfers);
440 u32 (*i2c_funcs)(struct i3c_master_controller *master);
441 int (*request_ibi)(struct i3c_dev_desc *dev,
442 const struct i3c_ibi_setup *req);
443 void (*free_ibi)(struct i3c_dev_desc *dev);
444 int (*enable_ibi)(struct i3c_dev_desc *dev);
445 int (*disable_ibi)(struct i3c_dev_desc *dev);
446 void (*recycle_ibi_slot)(struct i3c_dev_desc *dev,
447 struct i3c_ibi_slot *slot);
448};
449
450/**
451 * struct i3c_master_controller - I3C master controller object
452 * @dev: device to be registered to the device-model
453 * @this: an I3C device object representing this master. This device will be
454 * added to the list of I3C devs available on the bus
455 * @i2c: I2C adapter used for backward compatibility. This adapter is
456 * registered to the I2C subsystem to be as transparent as possible to
457 * existing I2C drivers
458 * @ops: master operations. See &struct i3c_master_controller_ops
459 * @secondary: true if the master is a secondary master
460 * @init_done: true when the bus initialization is done
461 * @boardinfo.i3c: list of I3C boardinfo objects
462 * @boardinfo.i2c: list of I2C boardinfo objects
463 * @boardinfo: board-level information attached to devices connected on the bus
464 * @bus: I3C bus exposed by this master
465 * @wq: workqueue used to execute IBI handlers. Can also be used by master
466 * drivers if they need to postpone operations that need to take place
467 * in a thread context. Typical examples are Hot Join processing which
468 * requires taking the bus lock in maintenance, which in turn, can only
469 * be done from a sleep-able context
470 *
471 * A &struct i3c_master_controller has to be registered to the I3C subsystem
472 * through i3c_master_register(). None of &struct i3c_master_controller fields
473 * should be set manually, just pass appropriate values to
474 * i3c_master_register().
475 */
476struct i3c_master_controller {
477 struct device dev;
478 struct i3c_dev_desc *this;
479 struct i2c_adapter i2c;
480 const struct i3c_master_controller_ops *ops;
481 unsigned int secondary : 1;
482 unsigned int init_done : 1;
483 struct {
484 struct list_head i3c;
485 struct list_head i2c;
486 } boardinfo;
487 struct i3c_bus bus;
488 struct workqueue_struct *wq;
489};
490
491/**
492 * i3c_bus_for_each_i2cdev() - iterate over all I2C devices present on the bus
493 * @bus: the I3C bus
494 * @dev: an I2C device descriptor pointer updated to point to the current slot
495 * at each iteration of the loop
496 *
497 * Iterate over all I2C devs present on the bus.
498 */
499#define i3c_bus_for_each_i2cdev(bus, dev) \
500 list_for_each_entry(dev, &(bus)->devs.i2c, common.node)
501
502/**
503 * i3c_bus_for_each_i3cdev() - iterate over all I3C devices present on the bus
504 * @bus: the I3C bus
505 * @dev: and I3C device descriptor pointer updated to point to the current slot
506 * at each iteration of the loop
507 *
508 * Iterate over all I3C devs present on the bus.
509 */
510#define i3c_bus_for_each_i3cdev(bus, dev) \
511 list_for_each_entry(dev, &(bus)->devs.i3c, common.node)
512
513int i3c_master_do_i2c_xfers(struct i3c_master_controller *master,
514 const struct i2c_msg *xfers,
515 int nxfers);
516
517int i3c_master_disec_locked(struct i3c_master_controller *master, u8 addr,
518 u8 evts);
519int i3c_master_enec_locked(struct i3c_master_controller *master, u8 addr,
520 u8 evts);
521int i3c_master_entdaa_locked(struct i3c_master_controller *master);
522int i3c_master_defslvs_locked(struct i3c_master_controller *master);
523
524int i3c_master_get_free_addr(struct i3c_master_controller *master,
525 u8 start_addr);
526
527int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master,
528 u8 addr);
529int i3c_master_do_daa(struct i3c_master_controller *master);
530
531int i3c_master_set_info(struct i3c_master_controller *master,
532 const struct i3c_device_info *info);
533
534int i3c_master_register(struct i3c_master_controller *master,
535 struct device *parent,
536 const struct i3c_master_controller_ops *ops,
537 bool secondary);
538int i3c_master_unregister(struct i3c_master_controller *master);
539
540/**
541 * i3c_dev_get_master_data() - get master private data attached to an I3C
542 * device descriptor
543 * @dev: the I3C device descriptor to get private data from
544 *
545 * Return: the private data previously attached with i3c_dev_set_master_data()
546 * or NULL if no data has been attached to the device.
547 */
548static inline void *i3c_dev_get_master_data(const struct i3c_dev_desc *dev)
549{
550 return dev->common.master_priv;
551}
552
553/**
554 * i3c_dev_set_master_data() - attach master private data to an I3C device
555 * descriptor
556 * @dev: the I3C device descriptor to attach private data to
557 * @data: private data
558 *
559 * This functions allows a master controller to attach per-device private data
560 * which can then be retrieved with i3c_dev_get_master_data().
561 */
562static inline void i3c_dev_set_master_data(struct i3c_dev_desc *dev,
563 void *data)
564{
565 dev->common.master_priv = data;
566}
567
568/**
569 * i2c_dev_get_master_data() - get master private data attached to an I2C
570 * device descriptor
571 * @dev: the I2C device descriptor to get private data from
572 *
573 * Return: the private data previously attached with i2c_dev_set_master_data()
574 * or NULL if no data has been attached to the device.
575 */
576static inline void *i2c_dev_get_master_data(const struct i2c_dev_desc *dev)
577{
578 return dev->common.master_priv;
579}
580
581/**
582 * i2c_dev_set_master_data() - attach master private data to an I2C device
583 * descriptor
584 * @dev: the I2C device descriptor to attach private data to
585 * @data: private data
586 *
587 * This functions allows a master controller to attach per-device private data
588 * which can then be retrieved with i2c_device_get_master_data().
589 */
590static inline void i2c_dev_set_master_data(struct i2c_dev_desc *dev,
591 void *data)
592{
593 dev->common.master_priv = data;
594}
595
596/**
597 * i3c_dev_get_master() - get master used to communicate with a device
598 * @dev: I3C dev
599 *
600 * Return: the master controller driving @dev
601 */
602static inline struct i3c_master_controller *
603i3c_dev_get_master(struct i3c_dev_desc *dev)
604{
605 return dev->common.master;
606}
607
608/**
609 * i2c_dev_get_master() - get master used to communicate with a device
610 * @dev: I2C dev
611 *
612 * Return: the master controller driving @dev
613 */
614static inline struct i3c_master_controller *
615i2c_dev_get_master(struct i2c_dev_desc *dev)
616{
617 return dev->common.master;
618}
619
620/**
621 * i3c_master_get_bus() - get the bus attached to a master
622 * @master: master object
623 *
624 * Return: the I3C bus @master is connected to
625 */
626static inline struct i3c_bus *
627i3c_master_get_bus(struct i3c_master_controller *master)
628{
629 return &master->bus;
630}
631
632struct i3c_generic_ibi_pool;
633
634struct i3c_generic_ibi_pool *
635i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
636 const struct i3c_ibi_setup *req);
637void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool);
638
639struct i3c_ibi_slot *
640i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool);
641void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
642 struct i3c_ibi_slot *slot);
643
644void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot);
645
646struct i3c_ibi_slot *i3c_master_get_free_ibi_slot(struct i3c_dev_desc *dev);
647
648#endif /* I3C_MASTER_H */