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 #include <linux/atomic.h>
9 #include <linux/bug.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/of.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/workqueue.h>
19
20 #include "internals.h"
21
22 static DEFINE_IDR(i3c_bus_idr);
23 static DEFINE_MUTEX(i3c_core_lock);
24
25 /**
26 * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
27 * @bus: I3C bus to take the lock on
28 *
29 * This function takes the bus lock so that no other operations can occur on
30 * the bus. This is needed for all kind of bus maintenance operation, like
31 * - enabling/disabling slave events
32 * - re-triggering DAA
33 * - changing the dynamic address of a device
34 * - relinquishing mastership
35 * - ...
36 *
37 * The reason for this kind of locking is that we don't want drivers and core
38 * logic to rely on I3C device information that could be changed behind their
39 * back.
40 */
i3c_bus_maintenance_lock(struct i3c_bus * bus)41 static void i3c_bus_maintenance_lock(struct i3c_bus *bus)
42 {
43 down_write(&bus->lock);
44 }
45
46 /**
47 * i3c_bus_maintenance_unlock - Release the bus lock after a maintenance
48 * operation
49 * @bus: I3C bus to release the lock on
50 *
51 * Should be called when the bus maintenance operation is done. See
52 * i3c_bus_maintenance_lock() for more details on what these maintenance
53 * operations are.
54 */
i3c_bus_maintenance_unlock(struct i3c_bus * bus)55 static void i3c_bus_maintenance_unlock(struct i3c_bus *bus)
56 {
57 up_write(&bus->lock);
58 }
59
60 /**
61 * i3c_bus_normaluse_lock - Lock the bus for a normal operation
62 * @bus: I3C bus to take the lock on
63 *
64 * This function takes the bus lock for any operation that is not a maintenance
65 * operation (see i3c_bus_maintenance_lock() for a non-exhaustive list of
66 * maintenance operations). Basically all communications with I3C devices are
67 * normal operations (HDR, SDR transfers or CCC commands that do not change bus
68 * state or I3C dynamic address).
69 *
70 * Note that this lock is not guaranteeing serialization of normal operations.
71 * In other words, transfer requests passed to the I3C master can be submitted
72 * in parallel and I3C master drivers have to use their own locking to make
73 * sure two different communications are not inter-mixed, or access to the
74 * output/input queue is not done while the engine is busy.
75 */
i3c_bus_normaluse_lock(struct i3c_bus * bus)76 void i3c_bus_normaluse_lock(struct i3c_bus *bus)
77 {
78 down_read(&bus->lock);
79 }
80
81 /**
82 * i3c_bus_normaluse_unlock - Release the bus lock after a normal operation
83 * @bus: I3C bus to release the lock on
84 *
85 * Should be called when a normal operation is done. See
86 * i3c_bus_normaluse_lock() for more details on what these normal operations
87 * are.
88 */
i3c_bus_normaluse_unlock(struct i3c_bus * bus)89 void i3c_bus_normaluse_unlock(struct i3c_bus *bus)
90 {
91 up_read(&bus->lock);
92 }
93
94 static struct i3c_master_controller *
i3c_bus_to_i3c_master(struct i3c_bus * i3cbus)95 i3c_bus_to_i3c_master(struct i3c_bus *i3cbus)
96 {
97 return container_of(i3cbus, struct i3c_master_controller, bus);
98 }
99
dev_to_i3cmaster(struct device * dev)100 static struct i3c_master_controller *dev_to_i3cmaster(struct device *dev)
101 {
102 return container_of(dev, struct i3c_master_controller, dev);
103 }
104
105 static const struct device_type i3c_device_type;
106
dev_to_i3cbus(struct device * dev)107 static struct i3c_bus *dev_to_i3cbus(struct device *dev)
108 {
109 struct i3c_master_controller *master;
110
111 if (dev->type == &i3c_device_type)
112 return dev_to_i3cdev(dev)->bus;
113
114 master = dev_to_i3cmaster(dev);
115
116 return &master->bus;
117 }
118
dev_to_i3cdesc(struct device * dev)119 static struct i3c_dev_desc *dev_to_i3cdesc(struct device *dev)
120 {
121 struct i3c_master_controller *master;
122
123 if (dev->type == &i3c_device_type)
124 return dev_to_i3cdev(dev)->desc;
125
126 master = dev_to_i3cmaster(dev);
127
128 return master->this;
129 }
130
bcr_show(struct device * dev,struct device_attribute * da,char * buf)131 static ssize_t bcr_show(struct device *dev,
132 struct device_attribute *da,
133 char *buf)
134 {
135 struct i3c_bus *bus = dev_to_i3cbus(dev);
136 struct i3c_dev_desc *desc;
137 ssize_t ret;
138
139 i3c_bus_normaluse_lock(bus);
140 desc = dev_to_i3cdesc(dev);
141 ret = sprintf(buf, "%x\n", desc->info.bcr);
142 i3c_bus_normaluse_unlock(bus);
143
144 return ret;
145 }
146 static DEVICE_ATTR_RO(bcr);
147
dcr_show(struct device * dev,struct device_attribute * da,char * buf)148 static ssize_t dcr_show(struct device *dev,
149 struct device_attribute *da,
150 char *buf)
151 {
152 struct i3c_bus *bus = dev_to_i3cbus(dev);
153 struct i3c_dev_desc *desc;
154 ssize_t ret;
155
156 i3c_bus_normaluse_lock(bus);
157 desc = dev_to_i3cdesc(dev);
158 ret = sprintf(buf, "%x\n", desc->info.dcr);
159 i3c_bus_normaluse_unlock(bus);
160
161 return ret;
162 }
163 static DEVICE_ATTR_RO(dcr);
164
pid_show(struct device * dev,struct device_attribute * da,char * buf)165 static ssize_t pid_show(struct device *dev,
166 struct device_attribute *da,
167 char *buf)
168 {
169 struct i3c_bus *bus = dev_to_i3cbus(dev);
170 struct i3c_dev_desc *desc;
171 ssize_t ret;
172
173 i3c_bus_normaluse_lock(bus);
174 desc = dev_to_i3cdesc(dev);
175 ret = sprintf(buf, "%llx\n", desc->info.pid);
176 i3c_bus_normaluse_unlock(bus);
177
178 return ret;
179 }
180 static DEVICE_ATTR_RO(pid);
181
dynamic_address_show(struct device * dev,struct device_attribute * da,char * buf)182 static ssize_t dynamic_address_show(struct device *dev,
183 struct device_attribute *da,
184 char *buf)
185 {
186 struct i3c_bus *bus = dev_to_i3cbus(dev);
187 struct i3c_dev_desc *desc;
188 ssize_t ret;
189
190 i3c_bus_normaluse_lock(bus);
191 desc = dev_to_i3cdesc(dev);
192 ret = sprintf(buf, "%02x\n", desc->info.dyn_addr);
193 i3c_bus_normaluse_unlock(bus);
194
195 return ret;
196 }
197 static DEVICE_ATTR_RO(dynamic_address);
198
199 static const char * const hdrcap_strings[] = {
200 "hdr-ddr", "hdr-tsp", "hdr-tsl",
201 };
202
hdrcap_show(struct device * dev,struct device_attribute * da,char * buf)203 static ssize_t hdrcap_show(struct device *dev,
204 struct device_attribute *da,
205 char *buf)
206 {
207 struct i3c_bus *bus = dev_to_i3cbus(dev);
208 struct i3c_dev_desc *desc;
209 ssize_t offset = 0, ret;
210 unsigned long caps;
211 int mode;
212
213 i3c_bus_normaluse_lock(bus);
214 desc = dev_to_i3cdesc(dev);
215 caps = desc->info.hdr_cap;
216 for_each_set_bit(mode, &caps, 8) {
217 if (mode >= ARRAY_SIZE(hdrcap_strings))
218 break;
219
220 if (!hdrcap_strings[mode])
221 continue;
222
223 ret = sprintf(buf + offset, offset ? " %s" : "%s",
224 hdrcap_strings[mode]);
225 if (ret < 0)
226 goto out;
227
228 offset += ret;
229 }
230
231 ret = sprintf(buf + offset, "\n");
232 if (ret < 0)
233 goto out;
234
235 ret = offset + ret;
236
237 out:
238 i3c_bus_normaluse_unlock(bus);
239
240 return ret;
241 }
242 static DEVICE_ATTR_RO(hdrcap);
243
modalias_show(struct device * dev,struct device_attribute * da,char * buf)244 static ssize_t modalias_show(struct device *dev,
245 struct device_attribute *da, char *buf)
246 {
247 struct i3c_device *i3c = dev_to_i3cdev(dev);
248 struct i3c_device_info devinfo;
249 u16 manuf, part, ext;
250
251 i3c_device_get_info(i3c, &devinfo);
252 manuf = I3C_PID_MANUF_ID(devinfo.pid);
253 part = I3C_PID_PART_ID(devinfo.pid);
254 ext = I3C_PID_EXTRA_INFO(devinfo.pid);
255
256 if (I3C_PID_RND_LOWER_32BITS(devinfo.pid))
257 return sprintf(buf, "i3c:dcr%02Xmanuf%04X", devinfo.dcr,
258 manuf);
259
260 return sprintf(buf, "i3c:dcr%02Xmanuf%04Xpart%04Xext%04X",
261 devinfo.dcr, manuf, part, ext);
262 }
263 static DEVICE_ATTR_RO(modalias);
264
265 static struct attribute *i3c_device_attrs[] = {
266 &dev_attr_bcr.attr,
267 &dev_attr_dcr.attr,
268 &dev_attr_pid.attr,
269 &dev_attr_dynamic_address.attr,
270 &dev_attr_hdrcap.attr,
271 &dev_attr_modalias.attr,
272 NULL,
273 };
274 ATTRIBUTE_GROUPS(i3c_device);
275
i3c_device_uevent(struct device * dev,struct kobj_uevent_env * env)276 static int i3c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
277 {
278 struct i3c_device *i3cdev = dev_to_i3cdev(dev);
279 struct i3c_device_info devinfo;
280 u16 manuf, part, ext;
281
282 i3c_device_get_info(i3cdev, &devinfo);
283 manuf = I3C_PID_MANUF_ID(devinfo.pid);
284 part = I3C_PID_PART_ID(devinfo.pid);
285 ext = I3C_PID_EXTRA_INFO(devinfo.pid);
286
287 if (I3C_PID_RND_LOWER_32BITS(devinfo.pid))
288 return add_uevent_var(env, "MODALIAS=i3c:dcr%02Xmanuf%04X",
289 devinfo.dcr, manuf);
290
291 return add_uevent_var(env,
292 "MODALIAS=i3c:dcr%02Xmanuf%04Xpart%04Xext%04X",
293 devinfo.dcr, manuf, part, ext);
294 }
295
296 static const struct device_type i3c_device_type = {
297 .groups = i3c_device_groups,
298 .uevent = i3c_device_uevent,
299 };
300
i3c_device_match(struct device * dev,struct device_driver * drv)301 static int i3c_device_match(struct device *dev, struct device_driver *drv)
302 {
303 struct i3c_device *i3cdev;
304 struct i3c_driver *i3cdrv;
305
306 if (dev->type != &i3c_device_type)
307 return 0;
308
309 i3cdev = dev_to_i3cdev(dev);
310 i3cdrv = drv_to_i3cdrv(drv);
311 if (i3c_device_match_id(i3cdev, i3cdrv->id_table))
312 return 1;
313
314 return 0;
315 }
316
i3c_device_probe(struct device * dev)317 static int i3c_device_probe(struct device *dev)
318 {
319 struct i3c_device *i3cdev = dev_to_i3cdev(dev);
320 struct i3c_driver *driver = drv_to_i3cdrv(dev->driver);
321
322 return driver->probe(i3cdev);
323 }
324
i3c_device_remove(struct device * dev)325 static void i3c_device_remove(struct device *dev)
326 {
327 struct i3c_device *i3cdev = dev_to_i3cdev(dev);
328 struct i3c_driver *driver = drv_to_i3cdrv(dev->driver);
329
330 if (driver->remove)
331 driver->remove(i3cdev);
332
333 i3c_device_free_ibi(i3cdev);
334 }
335
336 struct bus_type i3c_bus_type = {
337 .name = "i3c",
338 .match = i3c_device_match,
339 .probe = i3c_device_probe,
340 .remove = i3c_device_remove,
341 };
342
343 static enum i3c_addr_slot_status
i3c_bus_get_addr_slot_status(struct i3c_bus * bus,u16 addr)344 i3c_bus_get_addr_slot_status(struct i3c_bus *bus, u16 addr)
345 {
346 unsigned long status;
347 int bitpos = addr * 2;
348
349 if (addr > I2C_MAX_ADDR)
350 return I3C_ADDR_SLOT_RSVD;
351
352 status = bus->addrslots[bitpos / BITS_PER_LONG];
353 status >>= bitpos % BITS_PER_LONG;
354
355 return status & I3C_ADDR_SLOT_STATUS_MASK;
356 }
357
i3c_bus_set_addr_slot_status(struct i3c_bus * bus,u16 addr,enum i3c_addr_slot_status status)358 static void i3c_bus_set_addr_slot_status(struct i3c_bus *bus, u16 addr,
359 enum i3c_addr_slot_status status)
360 {
361 int bitpos = addr * 2;
362 unsigned long *ptr;
363
364 if (addr > I2C_MAX_ADDR)
365 return;
366
367 ptr = bus->addrslots + (bitpos / BITS_PER_LONG);
368 *ptr &= ~((unsigned long)I3C_ADDR_SLOT_STATUS_MASK <<
369 (bitpos % BITS_PER_LONG));
370 *ptr |= (unsigned long)status << (bitpos % BITS_PER_LONG);
371 }
372
i3c_bus_dev_addr_is_avail(struct i3c_bus * bus,u8 addr)373 static bool i3c_bus_dev_addr_is_avail(struct i3c_bus *bus, u8 addr)
374 {
375 enum i3c_addr_slot_status status;
376
377 status = i3c_bus_get_addr_slot_status(bus, addr);
378
379 return status == I3C_ADDR_SLOT_FREE;
380 }
381
i3c_bus_get_free_addr(struct i3c_bus * bus,u8 start_addr)382 static int i3c_bus_get_free_addr(struct i3c_bus *bus, u8 start_addr)
383 {
384 enum i3c_addr_slot_status status;
385 u8 addr;
386
387 for (addr = start_addr; addr < I3C_MAX_ADDR; addr++) {
388 status = i3c_bus_get_addr_slot_status(bus, addr);
389 if (status == I3C_ADDR_SLOT_FREE)
390 return addr;
391 }
392
393 return -ENOMEM;
394 }
395
i3c_bus_init_addrslots(struct i3c_bus * bus)396 static void i3c_bus_init_addrslots(struct i3c_bus *bus)
397 {
398 int i;
399
400 /* Addresses 0 to 7 are reserved. */
401 for (i = 0; i < 8; i++)
402 i3c_bus_set_addr_slot_status(bus, i, I3C_ADDR_SLOT_RSVD);
403
404 /*
405 * Reserve broadcast address and all addresses that might collide
406 * with the broadcast address when facing a single bit error.
407 */
408 i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR,
409 I3C_ADDR_SLOT_RSVD);
410 for (i = 0; i < 7; i++)
411 i3c_bus_set_addr_slot_status(bus, I3C_BROADCAST_ADDR ^ BIT(i),
412 I3C_ADDR_SLOT_RSVD);
413 }
414
i3c_bus_cleanup(struct i3c_bus * i3cbus)415 static void i3c_bus_cleanup(struct i3c_bus *i3cbus)
416 {
417 mutex_lock(&i3c_core_lock);
418 idr_remove(&i3c_bus_idr, i3cbus->id);
419 mutex_unlock(&i3c_core_lock);
420 }
421
i3c_bus_init(struct i3c_bus * i3cbus)422 static int i3c_bus_init(struct i3c_bus *i3cbus)
423 {
424 int ret;
425
426 init_rwsem(&i3cbus->lock);
427 INIT_LIST_HEAD(&i3cbus->devs.i2c);
428 INIT_LIST_HEAD(&i3cbus->devs.i3c);
429 i3c_bus_init_addrslots(i3cbus);
430 i3cbus->mode = I3C_BUS_MODE_PURE;
431
432 mutex_lock(&i3c_core_lock);
433 ret = idr_alloc(&i3c_bus_idr, i3cbus, 0, 0, GFP_KERNEL);
434 mutex_unlock(&i3c_core_lock);
435
436 if (ret < 0)
437 return ret;
438
439 i3cbus->id = ret;
440
441 return 0;
442 }
443
444 static const char * const i3c_bus_mode_strings[] = {
445 [I3C_BUS_MODE_PURE] = "pure",
446 [I3C_BUS_MODE_MIXED_FAST] = "mixed-fast",
447 [I3C_BUS_MODE_MIXED_LIMITED] = "mixed-limited",
448 [I3C_BUS_MODE_MIXED_SLOW] = "mixed-slow",
449 };
450
mode_show(struct device * dev,struct device_attribute * da,char * buf)451 static ssize_t mode_show(struct device *dev,
452 struct device_attribute *da,
453 char *buf)
454 {
455 struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
456 ssize_t ret;
457
458 i3c_bus_normaluse_lock(i3cbus);
459 if (i3cbus->mode < 0 ||
460 i3cbus->mode >= ARRAY_SIZE(i3c_bus_mode_strings) ||
461 !i3c_bus_mode_strings[i3cbus->mode])
462 ret = sprintf(buf, "unknown\n");
463 else
464 ret = sprintf(buf, "%s\n", i3c_bus_mode_strings[i3cbus->mode]);
465 i3c_bus_normaluse_unlock(i3cbus);
466
467 return ret;
468 }
469 static DEVICE_ATTR_RO(mode);
470
current_master_show(struct device * dev,struct device_attribute * da,char * buf)471 static ssize_t current_master_show(struct device *dev,
472 struct device_attribute *da,
473 char *buf)
474 {
475 struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
476 ssize_t ret;
477
478 i3c_bus_normaluse_lock(i3cbus);
479 ret = sprintf(buf, "%d-%llx\n", i3cbus->id,
480 i3cbus->cur_master->info.pid);
481 i3c_bus_normaluse_unlock(i3cbus);
482
483 return ret;
484 }
485 static DEVICE_ATTR_RO(current_master);
486
i3c_scl_frequency_show(struct device * dev,struct device_attribute * da,char * buf)487 static ssize_t i3c_scl_frequency_show(struct device *dev,
488 struct device_attribute *da,
489 char *buf)
490 {
491 struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
492 ssize_t ret;
493
494 i3c_bus_normaluse_lock(i3cbus);
495 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i3c);
496 i3c_bus_normaluse_unlock(i3cbus);
497
498 return ret;
499 }
500 static DEVICE_ATTR_RO(i3c_scl_frequency);
501
i2c_scl_frequency_show(struct device * dev,struct device_attribute * da,char * buf)502 static ssize_t i2c_scl_frequency_show(struct device *dev,
503 struct device_attribute *da,
504 char *buf)
505 {
506 struct i3c_bus *i3cbus = dev_to_i3cbus(dev);
507 ssize_t ret;
508
509 i3c_bus_normaluse_lock(i3cbus);
510 ret = sprintf(buf, "%ld\n", i3cbus->scl_rate.i2c);
511 i3c_bus_normaluse_unlock(i3cbus);
512
513 return ret;
514 }
515 static DEVICE_ATTR_RO(i2c_scl_frequency);
516
517 static struct attribute *i3c_masterdev_attrs[] = {
518 &dev_attr_mode.attr,
519 &dev_attr_current_master.attr,
520 &dev_attr_i3c_scl_frequency.attr,
521 &dev_attr_i2c_scl_frequency.attr,
522 &dev_attr_bcr.attr,
523 &dev_attr_dcr.attr,
524 &dev_attr_pid.attr,
525 &dev_attr_dynamic_address.attr,
526 &dev_attr_hdrcap.attr,
527 NULL,
528 };
529 ATTRIBUTE_GROUPS(i3c_masterdev);
530
i3c_masterdev_release(struct device * dev)531 static void i3c_masterdev_release(struct device *dev)
532 {
533 struct i3c_master_controller *master = dev_to_i3cmaster(dev);
534 struct i3c_bus *bus = dev_to_i3cbus(dev);
535
536 if (master->wq)
537 destroy_workqueue(master->wq);
538
539 WARN_ON(!list_empty(&bus->devs.i2c) || !list_empty(&bus->devs.i3c));
540 i3c_bus_cleanup(bus);
541
542 of_node_put(dev->of_node);
543 }
544
545 static const struct device_type i3c_masterdev_type = {
546 .groups = i3c_masterdev_groups,
547 };
548
i3c_bus_set_mode(struct i3c_bus * i3cbus,enum i3c_bus_mode mode,unsigned long max_i2c_scl_rate)549 static int i3c_bus_set_mode(struct i3c_bus *i3cbus, enum i3c_bus_mode mode,
550 unsigned long max_i2c_scl_rate)
551 {
552 struct i3c_master_controller *master = i3c_bus_to_i3c_master(i3cbus);
553
554 i3cbus->mode = mode;
555
556 switch (i3cbus->mode) {
557 case I3C_BUS_MODE_PURE:
558 if (!i3cbus->scl_rate.i3c)
559 i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE;
560 break;
561 case I3C_BUS_MODE_MIXED_FAST:
562 case I3C_BUS_MODE_MIXED_LIMITED:
563 if (!i3cbus->scl_rate.i3c)
564 i3cbus->scl_rate.i3c = I3C_BUS_TYP_I3C_SCL_RATE;
565 if (!i3cbus->scl_rate.i2c)
566 i3cbus->scl_rate.i2c = max_i2c_scl_rate;
567 break;
568 case I3C_BUS_MODE_MIXED_SLOW:
569 if (!i3cbus->scl_rate.i2c)
570 i3cbus->scl_rate.i2c = max_i2c_scl_rate;
571 if (!i3cbus->scl_rate.i3c ||
572 i3cbus->scl_rate.i3c > i3cbus->scl_rate.i2c)
573 i3cbus->scl_rate.i3c = i3cbus->scl_rate.i2c;
574 break;
575 default:
576 return -EINVAL;
577 }
578
579 dev_dbg(&master->dev, "i2c-scl = %ld Hz i3c-scl = %ld Hz\n",
580 i3cbus->scl_rate.i2c, i3cbus->scl_rate.i3c);
581
582 /*
583 * I3C/I2C frequency may have been overridden, check that user-provided
584 * values are not exceeding max possible frequency.
585 */
586 if (i3cbus->scl_rate.i3c > I3C_BUS_MAX_I3C_SCL_RATE ||
587 i3cbus->scl_rate.i2c > I3C_BUS_I2C_FM_PLUS_SCL_RATE)
588 return -EINVAL;
589
590 return 0;
591 }
592
593 static struct i3c_master_controller *
i2c_adapter_to_i3c_master(struct i2c_adapter * adap)594 i2c_adapter_to_i3c_master(struct i2c_adapter *adap)
595 {
596 return container_of(adap, struct i3c_master_controller, i2c);
597 }
598
599 static struct i2c_adapter *
i3c_master_to_i2c_adapter(struct i3c_master_controller * master)600 i3c_master_to_i2c_adapter(struct i3c_master_controller *master)
601 {
602 return &master->i2c;
603 }
604
i3c_master_free_i2c_dev(struct i2c_dev_desc * dev)605 static void i3c_master_free_i2c_dev(struct i2c_dev_desc *dev)
606 {
607 kfree(dev);
608 }
609
610 static struct i2c_dev_desc *
i3c_master_alloc_i2c_dev(struct i3c_master_controller * master,u16 addr,u8 lvr)611 i3c_master_alloc_i2c_dev(struct i3c_master_controller *master,
612 u16 addr, u8 lvr)
613 {
614 struct i2c_dev_desc *dev;
615
616 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
617 if (!dev)
618 return ERR_PTR(-ENOMEM);
619
620 dev->common.master = master;
621 dev->addr = addr;
622 dev->lvr = lvr;
623
624 return dev;
625 }
626
i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest * dest,u8 addr,u16 payloadlen)627 static void *i3c_ccc_cmd_dest_init(struct i3c_ccc_cmd_dest *dest, u8 addr,
628 u16 payloadlen)
629 {
630 dest->addr = addr;
631 dest->payload.len = payloadlen;
632 if (payloadlen)
633 dest->payload.data = kzalloc(payloadlen, GFP_KERNEL);
634 else
635 dest->payload.data = NULL;
636
637 return dest->payload.data;
638 }
639
i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest * dest)640 static void i3c_ccc_cmd_dest_cleanup(struct i3c_ccc_cmd_dest *dest)
641 {
642 kfree(dest->payload.data);
643 }
644
i3c_ccc_cmd_init(struct i3c_ccc_cmd * cmd,bool rnw,u8 id,struct i3c_ccc_cmd_dest * dests,unsigned int ndests)645 static void i3c_ccc_cmd_init(struct i3c_ccc_cmd *cmd, bool rnw, u8 id,
646 struct i3c_ccc_cmd_dest *dests,
647 unsigned int ndests)
648 {
649 cmd->rnw = rnw ? 1 : 0;
650 cmd->id = id;
651 cmd->dests = dests;
652 cmd->ndests = ndests;
653 cmd->err = I3C_ERROR_UNKNOWN;
654 }
655
i3c_master_send_ccc_cmd_locked(struct i3c_master_controller * master,struct i3c_ccc_cmd * cmd)656 static int i3c_master_send_ccc_cmd_locked(struct i3c_master_controller *master,
657 struct i3c_ccc_cmd *cmd)
658 {
659 int ret;
660
661 if (!cmd || !master)
662 return -EINVAL;
663
664 if (WARN_ON(master->init_done &&
665 !rwsem_is_locked(&master->bus.lock)))
666 return -EINVAL;
667
668 if (!master->ops->send_ccc_cmd)
669 return -ENOTSUPP;
670
671 if ((cmd->id & I3C_CCC_DIRECT) && (!cmd->dests || !cmd->ndests))
672 return -EINVAL;
673
674 if (master->ops->supports_ccc_cmd &&
675 !master->ops->supports_ccc_cmd(master, cmd))
676 return -ENOTSUPP;
677
678 ret = master->ops->send_ccc_cmd(master, cmd);
679 if (ret) {
680 if (cmd->err != I3C_ERROR_UNKNOWN)
681 return cmd->err;
682
683 return ret;
684 }
685
686 return 0;
687 }
688
689 static struct i2c_dev_desc *
i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller * master,u16 addr)690 i3c_master_find_i2c_dev_by_addr(const struct i3c_master_controller *master,
691 u16 addr)
692 {
693 struct i2c_dev_desc *dev;
694
695 i3c_bus_for_each_i2cdev(&master->bus, dev) {
696 if (dev->addr == addr)
697 return dev;
698 }
699
700 return NULL;
701 }
702
703 /**
704 * i3c_master_get_free_addr() - get a free address on the bus
705 * @master: I3C master object
706 * @start_addr: where to start searching
707 *
708 * This function must be called with the bus lock held in write mode.
709 *
710 * Return: the first free address starting at @start_addr (included) or -ENOMEM
711 * if there's no more address available.
712 */
i3c_master_get_free_addr(struct i3c_master_controller * master,u8 start_addr)713 int i3c_master_get_free_addr(struct i3c_master_controller *master,
714 u8 start_addr)
715 {
716 return i3c_bus_get_free_addr(&master->bus, start_addr);
717 }
718 EXPORT_SYMBOL_GPL(i3c_master_get_free_addr);
719
i3c_device_release(struct device * dev)720 static void i3c_device_release(struct device *dev)
721 {
722 struct i3c_device *i3cdev = dev_to_i3cdev(dev);
723
724 WARN_ON(i3cdev->desc);
725
726 of_node_put(i3cdev->dev.of_node);
727 kfree(i3cdev);
728 }
729
i3c_master_free_i3c_dev(struct i3c_dev_desc * dev)730 static void i3c_master_free_i3c_dev(struct i3c_dev_desc *dev)
731 {
732 kfree(dev);
733 }
734
735 static struct i3c_dev_desc *
i3c_master_alloc_i3c_dev(struct i3c_master_controller * master,const struct i3c_device_info * info)736 i3c_master_alloc_i3c_dev(struct i3c_master_controller *master,
737 const struct i3c_device_info *info)
738 {
739 struct i3c_dev_desc *dev;
740
741 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
742 if (!dev)
743 return ERR_PTR(-ENOMEM);
744
745 dev->common.master = master;
746 dev->info = *info;
747 mutex_init(&dev->ibi_lock);
748
749 return dev;
750 }
751
i3c_master_rstdaa_locked(struct i3c_master_controller * master,u8 addr)752 static int i3c_master_rstdaa_locked(struct i3c_master_controller *master,
753 u8 addr)
754 {
755 enum i3c_addr_slot_status addrstat;
756 struct i3c_ccc_cmd_dest dest;
757 struct i3c_ccc_cmd cmd;
758 int ret;
759
760 if (!master)
761 return -EINVAL;
762
763 addrstat = i3c_bus_get_addr_slot_status(&master->bus, addr);
764 if (addr != I3C_BROADCAST_ADDR && addrstat != I3C_ADDR_SLOT_I3C_DEV)
765 return -EINVAL;
766
767 i3c_ccc_cmd_dest_init(&dest, addr, 0);
768 i3c_ccc_cmd_init(&cmd, false,
769 I3C_CCC_RSTDAA(addr == I3C_BROADCAST_ADDR),
770 &dest, 1);
771 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
772 i3c_ccc_cmd_dest_cleanup(&dest);
773
774 return ret;
775 }
776
777 /**
778 * i3c_master_entdaa_locked() - start a DAA (Dynamic Address Assignment)
779 * procedure
780 * @master: master used to send frames on the bus
781 *
782 * Send a ENTDAA CCC command to start a DAA procedure.
783 *
784 * Note that this function only sends the ENTDAA CCC command, all the logic
785 * behind dynamic address assignment has to be handled in the I3C master
786 * driver.
787 *
788 * This function must be called with the bus lock held in write mode.
789 *
790 * Return: 0 in case of success, a positive I3C error code if the error is
791 * one of the official Mx error codes, and a negative error code otherwise.
792 */
i3c_master_entdaa_locked(struct i3c_master_controller * master)793 int i3c_master_entdaa_locked(struct i3c_master_controller *master)
794 {
795 struct i3c_ccc_cmd_dest dest;
796 struct i3c_ccc_cmd cmd;
797 int ret;
798
799 i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR, 0);
800 i3c_ccc_cmd_init(&cmd, false, I3C_CCC_ENTDAA, &dest, 1);
801 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
802 i3c_ccc_cmd_dest_cleanup(&dest);
803
804 return ret;
805 }
806 EXPORT_SYMBOL_GPL(i3c_master_entdaa_locked);
807
i3c_master_enec_disec_locked(struct i3c_master_controller * master,u8 addr,bool enable,u8 evts)808 static int i3c_master_enec_disec_locked(struct i3c_master_controller *master,
809 u8 addr, bool enable, u8 evts)
810 {
811 struct i3c_ccc_events *events;
812 struct i3c_ccc_cmd_dest dest;
813 struct i3c_ccc_cmd cmd;
814 int ret;
815
816 events = i3c_ccc_cmd_dest_init(&dest, addr, sizeof(*events));
817 if (!events)
818 return -ENOMEM;
819
820 events->events = evts;
821 i3c_ccc_cmd_init(&cmd, false,
822 enable ?
823 I3C_CCC_ENEC(addr == I3C_BROADCAST_ADDR) :
824 I3C_CCC_DISEC(addr == I3C_BROADCAST_ADDR),
825 &dest, 1);
826 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
827 i3c_ccc_cmd_dest_cleanup(&dest);
828
829 return ret;
830 }
831
832 /**
833 * i3c_master_disec_locked() - send a DISEC CCC command
834 * @master: master used to send frames on the bus
835 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
836 * @evts: events to disable
837 *
838 * Send a DISEC CCC command to disable some or all events coming from a
839 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
840 *
841 * This function must be called with the bus lock held in write mode.
842 *
843 * Return: 0 in case of success, a positive I3C error code if the error is
844 * one of the official Mx error codes, and a negative error code otherwise.
845 */
i3c_master_disec_locked(struct i3c_master_controller * master,u8 addr,u8 evts)846 int i3c_master_disec_locked(struct i3c_master_controller *master, u8 addr,
847 u8 evts)
848 {
849 return i3c_master_enec_disec_locked(master, addr, false, evts);
850 }
851 EXPORT_SYMBOL_GPL(i3c_master_disec_locked);
852
853 /**
854 * i3c_master_enec_locked() - send an ENEC CCC command
855 * @master: master used to send frames on the bus
856 * @addr: a valid I3C slave address or %I3C_BROADCAST_ADDR
857 * @evts: events to disable
858 *
859 * Sends an ENEC CCC command to enable some or all events coming from a
860 * specific slave, or all devices if @addr is %I3C_BROADCAST_ADDR.
861 *
862 * This function must be called with the bus lock held in write mode.
863 *
864 * Return: 0 in case of success, a positive I3C error code if the error is
865 * one of the official Mx error codes, and a negative error code otherwise.
866 */
i3c_master_enec_locked(struct i3c_master_controller * master,u8 addr,u8 evts)867 int i3c_master_enec_locked(struct i3c_master_controller *master, u8 addr,
868 u8 evts)
869 {
870 return i3c_master_enec_disec_locked(master, addr, true, evts);
871 }
872 EXPORT_SYMBOL_GPL(i3c_master_enec_locked);
873
874 /**
875 * i3c_master_defslvs_locked() - send a DEFSLVS CCC command
876 * @master: master used to send frames on the bus
877 *
878 * Send a DEFSLVS CCC command containing all the devices known to the @master.
879 * This is useful when you have secondary masters on the bus to propagate
880 * device information.
881 *
882 * This should be called after all I3C devices have been discovered (in other
883 * words, after the DAA procedure has finished) and instantiated in
884 * &i3c_master_controller_ops->bus_init().
885 * It should also be called if a master ACKed an Hot-Join request and assigned
886 * a dynamic address to the device joining the bus.
887 *
888 * This function must be called with the bus lock held in write mode.
889 *
890 * Return: 0 in case of success, a positive I3C error code if the error is
891 * one of the official Mx error codes, and a negative error code otherwise.
892 */
i3c_master_defslvs_locked(struct i3c_master_controller * master)893 int i3c_master_defslvs_locked(struct i3c_master_controller *master)
894 {
895 struct i3c_ccc_defslvs *defslvs;
896 struct i3c_ccc_dev_desc *desc;
897 struct i3c_ccc_cmd_dest dest;
898 struct i3c_dev_desc *i3cdev;
899 struct i2c_dev_desc *i2cdev;
900 struct i3c_ccc_cmd cmd;
901 struct i3c_bus *bus;
902 bool send = false;
903 int ndevs = 0, ret;
904
905 if (!master)
906 return -EINVAL;
907
908 bus = i3c_master_get_bus(master);
909 i3c_bus_for_each_i3cdev(bus, i3cdev) {
910 ndevs++;
911
912 if (i3cdev == master->this)
913 continue;
914
915 if (I3C_BCR_DEVICE_ROLE(i3cdev->info.bcr) ==
916 I3C_BCR_I3C_MASTER)
917 send = true;
918 }
919
920 /* No other master on the bus, skip DEFSLVS. */
921 if (!send)
922 return 0;
923
924 i3c_bus_for_each_i2cdev(bus, i2cdev)
925 ndevs++;
926
927 defslvs = i3c_ccc_cmd_dest_init(&dest, I3C_BROADCAST_ADDR,
928 struct_size(defslvs, slaves,
929 ndevs - 1));
930 if (!defslvs)
931 return -ENOMEM;
932
933 defslvs->count = ndevs;
934 defslvs->master.bcr = master->this->info.bcr;
935 defslvs->master.dcr = master->this->info.dcr;
936 defslvs->master.dyn_addr = master->this->info.dyn_addr << 1;
937 defslvs->master.static_addr = I3C_BROADCAST_ADDR << 1;
938
939 desc = defslvs->slaves;
940 i3c_bus_for_each_i2cdev(bus, i2cdev) {
941 desc->lvr = i2cdev->lvr;
942 desc->static_addr = i2cdev->addr << 1;
943 desc++;
944 }
945
946 i3c_bus_for_each_i3cdev(bus, i3cdev) {
947 /* Skip the I3C dev representing this master. */
948 if (i3cdev == master->this)
949 continue;
950
951 desc->bcr = i3cdev->info.bcr;
952 desc->dcr = i3cdev->info.dcr;
953 desc->dyn_addr = i3cdev->info.dyn_addr << 1;
954 desc->static_addr = i3cdev->info.static_addr << 1;
955 desc++;
956 }
957
958 i3c_ccc_cmd_init(&cmd, false, I3C_CCC_DEFSLVS, &dest, 1);
959 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
960 i3c_ccc_cmd_dest_cleanup(&dest);
961
962 return ret;
963 }
964 EXPORT_SYMBOL_GPL(i3c_master_defslvs_locked);
965
i3c_master_setda_locked(struct i3c_master_controller * master,u8 oldaddr,u8 newaddr,bool setdasa)966 static int i3c_master_setda_locked(struct i3c_master_controller *master,
967 u8 oldaddr, u8 newaddr, bool setdasa)
968 {
969 struct i3c_ccc_cmd_dest dest;
970 struct i3c_ccc_setda *setda;
971 struct i3c_ccc_cmd cmd;
972 int ret;
973
974 if (!oldaddr || !newaddr)
975 return -EINVAL;
976
977 setda = i3c_ccc_cmd_dest_init(&dest, oldaddr, sizeof(*setda));
978 if (!setda)
979 return -ENOMEM;
980
981 setda->addr = newaddr << 1;
982 i3c_ccc_cmd_init(&cmd, false,
983 setdasa ? I3C_CCC_SETDASA : I3C_CCC_SETNEWDA,
984 &dest, 1);
985 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
986 i3c_ccc_cmd_dest_cleanup(&dest);
987
988 return ret;
989 }
990
i3c_master_setdasa_locked(struct i3c_master_controller * master,u8 static_addr,u8 dyn_addr)991 static int i3c_master_setdasa_locked(struct i3c_master_controller *master,
992 u8 static_addr, u8 dyn_addr)
993 {
994 return i3c_master_setda_locked(master, static_addr, dyn_addr, true);
995 }
996
i3c_master_setnewda_locked(struct i3c_master_controller * master,u8 oldaddr,u8 newaddr)997 static int i3c_master_setnewda_locked(struct i3c_master_controller *master,
998 u8 oldaddr, u8 newaddr)
999 {
1000 return i3c_master_setda_locked(master, oldaddr, newaddr, false);
1001 }
1002
i3c_master_getmrl_locked(struct i3c_master_controller * master,struct i3c_device_info * info)1003 static int i3c_master_getmrl_locked(struct i3c_master_controller *master,
1004 struct i3c_device_info *info)
1005 {
1006 struct i3c_ccc_cmd_dest dest;
1007 struct i3c_ccc_mrl *mrl;
1008 struct i3c_ccc_cmd cmd;
1009 int ret;
1010
1011 mrl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mrl));
1012 if (!mrl)
1013 return -ENOMEM;
1014
1015 /*
1016 * When the device does not have IBI payload GETMRL only returns 2
1017 * bytes of data.
1018 */
1019 if (!(info->bcr & I3C_BCR_IBI_PAYLOAD))
1020 dest.payload.len -= 1;
1021
1022 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMRL, &dest, 1);
1023 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1024 if (ret)
1025 goto out;
1026
1027 switch (dest.payload.len) {
1028 case 3:
1029 info->max_ibi_len = mrl->ibi_len;
1030 fallthrough;
1031 case 2:
1032 info->max_read_len = be16_to_cpu(mrl->read_len);
1033 break;
1034 default:
1035 ret = -EIO;
1036 goto out;
1037 }
1038
1039 out:
1040 i3c_ccc_cmd_dest_cleanup(&dest);
1041
1042 return ret;
1043 }
1044
i3c_master_getmwl_locked(struct i3c_master_controller * master,struct i3c_device_info * info)1045 static int i3c_master_getmwl_locked(struct i3c_master_controller *master,
1046 struct i3c_device_info *info)
1047 {
1048 struct i3c_ccc_cmd_dest dest;
1049 struct i3c_ccc_mwl *mwl;
1050 struct i3c_ccc_cmd cmd;
1051 int ret;
1052
1053 mwl = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*mwl));
1054 if (!mwl)
1055 return -ENOMEM;
1056
1057 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMWL, &dest, 1);
1058 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1059 if (ret)
1060 goto out;
1061
1062 if (dest.payload.len != sizeof(*mwl)) {
1063 ret = -EIO;
1064 goto out;
1065 }
1066
1067 info->max_write_len = be16_to_cpu(mwl->len);
1068
1069 out:
1070 i3c_ccc_cmd_dest_cleanup(&dest);
1071
1072 return ret;
1073 }
1074
i3c_master_getmxds_locked(struct i3c_master_controller * master,struct i3c_device_info * info)1075 static int i3c_master_getmxds_locked(struct i3c_master_controller *master,
1076 struct i3c_device_info *info)
1077 {
1078 struct i3c_ccc_getmxds *getmaxds;
1079 struct i3c_ccc_cmd_dest dest;
1080 struct i3c_ccc_cmd cmd;
1081 int ret;
1082
1083 getmaxds = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr,
1084 sizeof(*getmaxds));
1085 if (!getmaxds)
1086 return -ENOMEM;
1087
1088 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETMXDS, &dest, 1);
1089 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1090 if (ret)
1091 goto out;
1092
1093 if (dest.payload.len != 2 && dest.payload.len != 5) {
1094 ret = -EIO;
1095 goto out;
1096 }
1097
1098 info->max_read_ds = getmaxds->maxrd;
1099 info->max_write_ds = getmaxds->maxwr;
1100 if (dest.payload.len == 5)
1101 info->max_read_turnaround = getmaxds->maxrdturn[0] |
1102 ((u32)getmaxds->maxrdturn[1] << 8) |
1103 ((u32)getmaxds->maxrdturn[2] << 16);
1104
1105 out:
1106 i3c_ccc_cmd_dest_cleanup(&dest);
1107
1108 return ret;
1109 }
1110
i3c_master_gethdrcap_locked(struct i3c_master_controller * master,struct i3c_device_info * info)1111 static int i3c_master_gethdrcap_locked(struct i3c_master_controller *master,
1112 struct i3c_device_info *info)
1113 {
1114 struct i3c_ccc_gethdrcap *gethdrcap;
1115 struct i3c_ccc_cmd_dest dest;
1116 struct i3c_ccc_cmd cmd;
1117 int ret;
1118
1119 gethdrcap = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr,
1120 sizeof(*gethdrcap));
1121 if (!gethdrcap)
1122 return -ENOMEM;
1123
1124 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETHDRCAP, &dest, 1);
1125 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1126 if (ret)
1127 goto out;
1128
1129 if (dest.payload.len != 1) {
1130 ret = -EIO;
1131 goto out;
1132 }
1133
1134 info->hdr_cap = gethdrcap->modes;
1135
1136 out:
1137 i3c_ccc_cmd_dest_cleanup(&dest);
1138
1139 return ret;
1140 }
1141
i3c_master_getpid_locked(struct i3c_master_controller * master,struct i3c_device_info * info)1142 static int i3c_master_getpid_locked(struct i3c_master_controller *master,
1143 struct i3c_device_info *info)
1144 {
1145 struct i3c_ccc_getpid *getpid;
1146 struct i3c_ccc_cmd_dest dest;
1147 struct i3c_ccc_cmd cmd;
1148 int ret, i;
1149
1150 getpid = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getpid));
1151 if (!getpid)
1152 return -ENOMEM;
1153
1154 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETPID, &dest, 1);
1155 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1156 if (ret)
1157 goto out;
1158
1159 info->pid = 0;
1160 for (i = 0; i < sizeof(getpid->pid); i++) {
1161 int sft = (sizeof(getpid->pid) - i - 1) * 8;
1162
1163 info->pid |= (u64)getpid->pid[i] << sft;
1164 }
1165
1166 out:
1167 i3c_ccc_cmd_dest_cleanup(&dest);
1168
1169 return ret;
1170 }
1171
i3c_master_getbcr_locked(struct i3c_master_controller * master,struct i3c_device_info * info)1172 static int i3c_master_getbcr_locked(struct i3c_master_controller *master,
1173 struct i3c_device_info *info)
1174 {
1175 struct i3c_ccc_getbcr *getbcr;
1176 struct i3c_ccc_cmd_dest dest;
1177 struct i3c_ccc_cmd cmd;
1178 int ret;
1179
1180 getbcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getbcr));
1181 if (!getbcr)
1182 return -ENOMEM;
1183
1184 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETBCR, &dest, 1);
1185 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1186 if (ret)
1187 goto out;
1188
1189 info->bcr = getbcr->bcr;
1190
1191 out:
1192 i3c_ccc_cmd_dest_cleanup(&dest);
1193
1194 return ret;
1195 }
1196
i3c_master_getdcr_locked(struct i3c_master_controller * master,struct i3c_device_info * info)1197 static int i3c_master_getdcr_locked(struct i3c_master_controller *master,
1198 struct i3c_device_info *info)
1199 {
1200 struct i3c_ccc_getdcr *getdcr;
1201 struct i3c_ccc_cmd_dest dest;
1202 struct i3c_ccc_cmd cmd;
1203 int ret;
1204
1205 getdcr = i3c_ccc_cmd_dest_init(&dest, info->dyn_addr, sizeof(*getdcr));
1206 if (!getdcr)
1207 return -ENOMEM;
1208
1209 i3c_ccc_cmd_init(&cmd, true, I3C_CCC_GETDCR, &dest, 1);
1210 ret = i3c_master_send_ccc_cmd_locked(master, &cmd);
1211 if (ret)
1212 goto out;
1213
1214 info->dcr = getdcr->dcr;
1215
1216 out:
1217 i3c_ccc_cmd_dest_cleanup(&dest);
1218
1219 return ret;
1220 }
1221
i3c_master_retrieve_dev_info(struct i3c_dev_desc * dev)1222 static int i3c_master_retrieve_dev_info(struct i3c_dev_desc *dev)
1223 {
1224 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1225 enum i3c_addr_slot_status slot_status;
1226 int ret;
1227
1228 if (!dev->info.dyn_addr)
1229 return -EINVAL;
1230
1231 slot_status = i3c_bus_get_addr_slot_status(&master->bus,
1232 dev->info.dyn_addr);
1233 if (slot_status == I3C_ADDR_SLOT_RSVD ||
1234 slot_status == I3C_ADDR_SLOT_I2C_DEV)
1235 return -EINVAL;
1236
1237 ret = i3c_master_getpid_locked(master, &dev->info);
1238 if (ret)
1239 return ret;
1240
1241 ret = i3c_master_getbcr_locked(master, &dev->info);
1242 if (ret)
1243 return ret;
1244
1245 ret = i3c_master_getdcr_locked(master, &dev->info);
1246 if (ret)
1247 return ret;
1248
1249 if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM) {
1250 ret = i3c_master_getmxds_locked(master, &dev->info);
1251 if (ret)
1252 return ret;
1253 }
1254
1255 if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD)
1256 dev->info.max_ibi_len = 1;
1257
1258 i3c_master_getmrl_locked(master, &dev->info);
1259 i3c_master_getmwl_locked(master, &dev->info);
1260
1261 if (dev->info.bcr & I3C_BCR_HDR_CAP) {
1262 ret = i3c_master_gethdrcap_locked(master, &dev->info);
1263 if (ret)
1264 return ret;
1265 }
1266
1267 return 0;
1268 }
1269
i3c_master_put_i3c_addrs(struct i3c_dev_desc * dev)1270 static void i3c_master_put_i3c_addrs(struct i3c_dev_desc *dev)
1271 {
1272 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1273
1274 if (dev->info.static_addr)
1275 i3c_bus_set_addr_slot_status(&master->bus,
1276 dev->info.static_addr,
1277 I3C_ADDR_SLOT_FREE);
1278
1279 if (dev->info.dyn_addr)
1280 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1281 I3C_ADDR_SLOT_FREE);
1282
1283 if (dev->boardinfo && dev->boardinfo->init_dyn_addr)
1284 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1285 I3C_ADDR_SLOT_FREE);
1286 }
1287
i3c_master_get_i3c_addrs(struct i3c_dev_desc * dev)1288 static int i3c_master_get_i3c_addrs(struct i3c_dev_desc *dev)
1289 {
1290 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1291 enum i3c_addr_slot_status status;
1292
1293 if (!dev->info.static_addr && !dev->info.dyn_addr)
1294 return 0;
1295
1296 if (dev->info.static_addr) {
1297 status = i3c_bus_get_addr_slot_status(&master->bus,
1298 dev->info.static_addr);
1299 if (status != I3C_ADDR_SLOT_FREE)
1300 return -EBUSY;
1301
1302 i3c_bus_set_addr_slot_status(&master->bus,
1303 dev->info.static_addr,
1304 I3C_ADDR_SLOT_I3C_DEV);
1305 }
1306
1307 /*
1308 * ->init_dyn_addr should have been reserved before that, so, if we're
1309 * trying to apply a pre-reserved dynamic address, we should not try
1310 * to reserve the address slot a second time.
1311 */
1312 if (dev->info.dyn_addr &&
1313 (!dev->boardinfo ||
1314 dev->boardinfo->init_dyn_addr != dev->info.dyn_addr)) {
1315 status = i3c_bus_get_addr_slot_status(&master->bus,
1316 dev->info.dyn_addr);
1317 if (status != I3C_ADDR_SLOT_FREE)
1318 goto err_release_static_addr;
1319
1320 i3c_bus_set_addr_slot_status(&master->bus, dev->info.dyn_addr,
1321 I3C_ADDR_SLOT_I3C_DEV);
1322 }
1323
1324 return 0;
1325
1326 err_release_static_addr:
1327 if (dev->info.static_addr)
1328 i3c_bus_set_addr_slot_status(&master->bus,
1329 dev->info.static_addr,
1330 I3C_ADDR_SLOT_FREE);
1331
1332 return -EBUSY;
1333 }
1334
i3c_master_attach_i3c_dev(struct i3c_master_controller * master,struct i3c_dev_desc * dev)1335 static int i3c_master_attach_i3c_dev(struct i3c_master_controller *master,
1336 struct i3c_dev_desc *dev)
1337 {
1338 int ret;
1339
1340 /*
1341 * We don't attach devices to the controller until they are
1342 * addressable on the bus.
1343 */
1344 if (!dev->info.static_addr && !dev->info.dyn_addr)
1345 return 0;
1346
1347 ret = i3c_master_get_i3c_addrs(dev);
1348 if (ret)
1349 return ret;
1350
1351 /* Do not attach the master device itself. */
1352 if (master->this != dev && master->ops->attach_i3c_dev) {
1353 ret = master->ops->attach_i3c_dev(dev);
1354 if (ret) {
1355 i3c_master_put_i3c_addrs(dev);
1356 return ret;
1357 }
1358 }
1359
1360 list_add_tail(&dev->common.node, &master->bus.devs.i3c);
1361
1362 return 0;
1363 }
1364
i3c_master_reattach_i3c_dev(struct i3c_dev_desc * dev,u8 old_dyn_addr)1365 static int i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev,
1366 u8 old_dyn_addr)
1367 {
1368 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1369 enum i3c_addr_slot_status status;
1370 int ret;
1371
1372 if (dev->info.dyn_addr != old_dyn_addr &&
1373 (!dev->boardinfo ||
1374 dev->info.dyn_addr != dev->boardinfo->init_dyn_addr)) {
1375 status = i3c_bus_get_addr_slot_status(&master->bus,
1376 dev->info.dyn_addr);
1377 if (status != I3C_ADDR_SLOT_FREE)
1378 return -EBUSY;
1379 i3c_bus_set_addr_slot_status(&master->bus,
1380 dev->info.dyn_addr,
1381 I3C_ADDR_SLOT_I3C_DEV);
1382 if (old_dyn_addr)
1383 i3c_bus_set_addr_slot_status(&master->bus, old_dyn_addr,
1384 I3C_ADDR_SLOT_FREE);
1385 }
1386
1387 if (master->ops->reattach_i3c_dev) {
1388 ret = master->ops->reattach_i3c_dev(dev, old_dyn_addr);
1389 if (ret) {
1390 i3c_master_put_i3c_addrs(dev);
1391 return ret;
1392 }
1393 }
1394
1395 return 0;
1396 }
1397
i3c_master_detach_i3c_dev(struct i3c_dev_desc * dev)1398 static void i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
1399 {
1400 struct i3c_master_controller *master = i3c_dev_get_master(dev);
1401
1402 /* Do not detach the master device itself. */
1403 if (master->this != dev && master->ops->detach_i3c_dev)
1404 master->ops->detach_i3c_dev(dev);
1405
1406 i3c_master_put_i3c_addrs(dev);
1407 list_del(&dev->common.node);
1408 }
1409
i3c_master_attach_i2c_dev(struct i3c_master_controller * master,struct i2c_dev_desc * dev)1410 static int i3c_master_attach_i2c_dev(struct i3c_master_controller *master,
1411 struct i2c_dev_desc *dev)
1412 {
1413 int ret;
1414
1415 if (master->ops->attach_i2c_dev) {
1416 ret = master->ops->attach_i2c_dev(dev);
1417 if (ret)
1418 return ret;
1419 }
1420
1421 list_add_tail(&dev->common.node, &master->bus.devs.i2c);
1422
1423 return 0;
1424 }
1425
i3c_master_detach_i2c_dev(struct i2c_dev_desc * dev)1426 static void i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev)
1427 {
1428 struct i3c_master_controller *master = i2c_dev_get_master(dev);
1429
1430 list_del(&dev->common.node);
1431
1432 if (master->ops->detach_i2c_dev)
1433 master->ops->detach_i2c_dev(dev);
1434 }
1435
i3c_master_early_i3c_dev_add(struct i3c_master_controller * master,struct i3c_dev_boardinfo * boardinfo)1436 static int i3c_master_early_i3c_dev_add(struct i3c_master_controller *master,
1437 struct i3c_dev_boardinfo *boardinfo)
1438 {
1439 struct i3c_device_info info = {
1440 .static_addr = boardinfo->static_addr,
1441 };
1442 struct i3c_dev_desc *i3cdev;
1443 int ret;
1444
1445 i3cdev = i3c_master_alloc_i3c_dev(master, &info);
1446 if (IS_ERR(i3cdev))
1447 return -ENOMEM;
1448
1449 i3cdev->boardinfo = boardinfo;
1450
1451 ret = i3c_master_attach_i3c_dev(master, i3cdev);
1452 if (ret)
1453 goto err_free_dev;
1454
1455 ret = i3c_master_setdasa_locked(master, i3cdev->info.static_addr,
1456 i3cdev->boardinfo->init_dyn_addr);
1457 if (ret)
1458 goto err_detach_dev;
1459
1460 i3cdev->info.dyn_addr = i3cdev->boardinfo->init_dyn_addr;
1461 ret = i3c_master_reattach_i3c_dev(i3cdev, 0);
1462 if (ret)
1463 goto err_rstdaa;
1464
1465 ret = i3c_master_retrieve_dev_info(i3cdev);
1466 if (ret)
1467 goto err_rstdaa;
1468
1469 return 0;
1470
1471 err_rstdaa:
1472 i3c_master_rstdaa_locked(master, i3cdev->boardinfo->init_dyn_addr);
1473 err_detach_dev:
1474 i3c_master_detach_i3c_dev(i3cdev);
1475 err_free_dev:
1476 i3c_master_free_i3c_dev(i3cdev);
1477
1478 return ret;
1479 }
1480
1481 static void
i3c_master_register_new_i3c_devs(struct i3c_master_controller * master)1482 i3c_master_register_new_i3c_devs(struct i3c_master_controller *master)
1483 {
1484 struct i3c_dev_desc *desc;
1485 int ret;
1486
1487 if (!master->init_done)
1488 return;
1489
1490 i3c_bus_for_each_i3cdev(&master->bus, desc) {
1491 if (desc->dev || !desc->info.dyn_addr || desc == master->this)
1492 continue;
1493
1494 desc->dev = kzalloc(sizeof(*desc->dev), GFP_KERNEL);
1495 if (!desc->dev)
1496 continue;
1497
1498 desc->dev->bus = &master->bus;
1499 desc->dev->desc = desc;
1500 desc->dev->dev.parent = &master->dev;
1501 desc->dev->dev.type = &i3c_device_type;
1502 desc->dev->dev.bus = &i3c_bus_type;
1503 desc->dev->dev.release = i3c_device_release;
1504 dev_set_name(&desc->dev->dev, "%d-%llx", master->bus.id,
1505 desc->info.pid);
1506
1507 if (desc->boardinfo)
1508 desc->dev->dev.of_node = desc->boardinfo->of_node;
1509
1510 ret = device_register(&desc->dev->dev);
1511 if (ret) {
1512 dev_err(&master->dev,
1513 "Failed to add I3C device (err = %d)\n", ret);
1514 put_device(&desc->dev->dev);
1515 }
1516 }
1517 }
1518
1519 /**
1520 * i3c_master_do_daa() - do a DAA (Dynamic Address Assignment)
1521 * @master: master doing the DAA
1522 *
1523 * This function is instantiating an I3C device object and adding it to the
1524 * I3C device list. All device information are automatically retrieved using
1525 * standard CCC commands.
1526 *
1527 * The I3C device object is returned in case the master wants to attach
1528 * private data to it using i3c_dev_set_master_data().
1529 *
1530 * This function must be called with the bus lock held in write mode.
1531 *
1532 * Return: a 0 in case of success, an negative error code otherwise.
1533 */
i3c_master_do_daa(struct i3c_master_controller * master)1534 int i3c_master_do_daa(struct i3c_master_controller *master)
1535 {
1536 int ret;
1537
1538 i3c_bus_maintenance_lock(&master->bus);
1539 ret = master->ops->do_daa(master);
1540 i3c_bus_maintenance_unlock(&master->bus);
1541
1542 if (ret)
1543 return ret;
1544
1545 i3c_bus_normaluse_lock(&master->bus);
1546 i3c_master_register_new_i3c_devs(master);
1547 i3c_bus_normaluse_unlock(&master->bus);
1548
1549 return 0;
1550 }
1551 EXPORT_SYMBOL_GPL(i3c_master_do_daa);
1552
1553 /**
1554 * i3c_master_set_info() - set master device information
1555 * @master: master used to send frames on the bus
1556 * @info: I3C device information
1557 *
1558 * Set master device info. This should be called from
1559 * &i3c_master_controller_ops->bus_init().
1560 *
1561 * Not all &i3c_device_info fields are meaningful for a master device.
1562 * Here is a list of fields that should be properly filled:
1563 *
1564 * - &i3c_device_info->dyn_addr
1565 * - &i3c_device_info->bcr
1566 * - &i3c_device_info->dcr
1567 * - &i3c_device_info->pid
1568 * - &i3c_device_info->hdr_cap if %I3C_BCR_HDR_CAP bit is set in
1569 * &i3c_device_info->bcr
1570 *
1571 * This function must be called with the bus lock held in maintenance mode.
1572 *
1573 * Return: 0 if @info contains valid information (not every piece of
1574 * information can be checked, but we can at least make sure @info->dyn_addr
1575 * and @info->bcr are correct), -EINVAL otherwise.
1576 */
i3c_master_set_info(struct i3c_master_controller * master,const struct i3c_device_info * info)1577 int i3c_master_set_info(struct i3c_master_controller *master,
1578 const struct i3c_device_info *info)
1579 {
1580 struct i3c_dev_desc *i3cdev;
1581 int ret;
1582
1583 if (!i3c_bus_dev_addr_is_avail(&master->bus, info->dyn_addr))
1584 return -EINVAL;
1585
1586 if (I3C_BCR_DEVICE_ROLE(info->bcr) == I3C_BCR_I3C_MASTER &&
1587 master->secondary)
1588 return -EINVAL;
1589
1590 if (master->this)
1591 return -EINVAL;
1592
1593 i3cdev = i3c_master_alloc_i3c_dev(master, info);
1594 if (IS_ERR(i3cdev))
1595 return PTR_ERR(i3cdev);
1596
1597 master->this = i3cdev;
1598 master->bus.cur_master = master->this;
1599
1600 ret = i3c_master_attach_i3c_dev(master, i3cdev);
1601 if (ret)
1602 goto err_free_dev;
1603
1604 return 0;
1605
1606 err_free_dev:
1607 i3c_master_free_i3c_dev(i3cdev);
1608
1609 return ret;
1610 }
1611 EXPORT_SYMBOL_GPL(i3c_master_set_info);
1612
i3c_master_detach_free_devs(struct i3c_master_controller * master)1613 static void i3c_master_detach_free_devs(struct i3c_master_controller *master)
1614 {
1615 struct i3c_dev_desc *i3cdev, *i3ctmp;
1616 struct i2c_dev_desc *i2cdev, *i2ctmp;
1617
1618 list_for_each_entry_safe(i3cdev, i3ctmp, &master->bus.devs.i3c,
1619 common.node) {
1620 i3c_master_detach_i3c_dev(i3cdev);
1621
1622 if (i3cdev->boardinfo && i3cdev->boardinfo->init_dyn_addr)
1623 i3c_bus_set_addr_slot_status(&master->bus,
1624 i3cdev->boardinfo->init_dyn_addr,
1625 I3C_ADDR_SLOT_FREE);
1626
1627 i3c_master_free_i3c_dev(i3cdev);
1628 }
1629
1630 list_for_each_entry_safe(i2cdev, i2ctmp, &master->bus.devs.i2c,
1631 common.node) {
1632 i3c_master_detach_i2c_dev(i2cdev);
1633 i3c_bus_set_addr_slot_status(&master->bus,
1634 i2cdev->addr,
1635 I3C_ADDR_SLOT_FREE);
1636 i3c_master_free_i2c_dev(i2cdev);
1637 }
1638 }
1639
1640 /**
1641 * i3c_master_bus_init() - initialize an I3C bus
1642 * @master: main master initializing the bus
1643 *
1644 * This function is following all initialisation steps described in the I3C
1645 * specification:
1646 *
1647 * 1. Attach I2C devs to the master so that the master can fill its internal
1648 * device table appropriately
1649 *
1650 * 2. Call &i3c_master_controller_ops->bus_init() method to initialize
1651 * the master controller. That's usually where the bus mode is selected
1652 * (pure bus or mixed fast/slow bus)
1653 *
1654 * 3. Instruct all devices on the bus to drop their dynamic address. This is
1655 * particularly important when the bus was previously configured by someone
1656 * else (for example the bootloader)
1657 *
1658 * 4. Disable all slave events.
1659 *
1660 * 5. Reserve address slots for I3C devices with init_dyn_addr. And if devices
1661 * also have static_addr, try to pre-assign dynamic addresses requested by
1662 * the FW with SETDASA and attach corresponding statically defined I3C
1663 * devices to the master.
1664 *
1665 * 6. Do a DAA (Dynamic Address Assignment) to assign dynamic addresses to all
1666 * remaining I3C devices
1667 *
1668 * Once this is done, all I3C and I2C devices should be usable.
1669 *
1670 * Return: a 0 in case of success, an negative error code otherwise.
1671 */
i3c_master_bus_init(struct i3c_master_controller * master)1672 static int i3c_master_bus_init(struct i3c_master_controller *master)
1673 {
1674 enum i3c_addr_slot_status status;
1675 struct i2c_dev_boardinfo *i2cboardinfo;
1676 struct i3c_dev_boardinfo *i3cboardinfo;
1677 struct i2c_dev_desc *i2cdev;
1678 int ret;
1679
1680 /*
1681 * First attach all devices with static definitions provided by the
1682 * FW.
1683 */
1684 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) {
1685 status = i3c_bus_get_addr_slot_status(&master->bus,
1686 i2cboardinfo->base.addr);
1687 if (status != I3C_ADDR_SLOT_FREE) {
1688 ret = -EBUSY;
1689 goto err_detach_devs;
1690 }
1691
1692 i3c_bus_set_addr_slot_status(&master->bus,
1693 i2cboardinfo->base.addr,
1694 I3C_ADDR_SLOT_I2C_DEV);
1695
1696 i2cdev = i3c_master_alloc_i2c_dev(master,
1697 i2cboardinfo->base.addr,
1698 i2cboardinfo->lvr);
1699 if (IS_ERR(i2cdev)) {
1700 ret = PTR_ERR(i2cdev);
1701 goto err_detach_devs;
1702 }
1703
1704 ret = i3c_master_attach_i2c_dev(master, i2cdev);
1705 if (ret) {
1706 i3c_master_free_i2c_dev(i2cdev);
1707 goto err_detach_devs;
1708 }
1709 }
1710
1711 /*
1712 * Now execute the controller specific ->bus_init() routine, which
1713 * might configure its internal logic to match the bus limitations.
1714 */
1715 ret = master->ops->bus_init(master);
1716 if (ret)
1717 goto err_detach_devs;
1718
1719 /*
1720 * The master device should have been instantiated in ->bus_init(),
1721 * complain if this was not the case.
1722 */
1723 if (!master->this) {
1724 dev_err(&master->dev,
1725 "master_set_info() was not called in ->bus_init()\n");
1726 ret = -EINVAL;
1727 goto err_bus_cleanup;
1728 }
1729
1730 /*
1731 * Reset all dynamic address that may have been assigned before
1732 * (assigned by the bootloader for example).
1733 */
1734 ret = i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
1735 if (ret && ret != I3C_ERROR_M2)
1736 goto err_bus_cleanup;
1737
1738 /* Disable all slave events before starting DAA. */
1739 ret = i3c_master_disec_locked(master, I3C_BROADCAST_ADDR,
1740 I3C_CCC_EVENT_SIR | I3C_CCC_EVENT_MR |
1741 I3C_CCC_EVENT_HJ);
1742 if (ret && ret != I3C_ERROR_M2)
1743 goto err_bus_cleanup;
1744
1745 /*
1746 * Reserve init_dyn_addr first, and then try to pre-assign dynamic
1747 * address and retrieve device information if needed.
1748 * In case pre-assign dynamic address fails, setting dynamic address to
1749 * the requested init_dyn_addr is retried after DAA is done in
1750 * i3c_master_add_i3c_dev_locked().
1751 */
1752 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) {
1753
1754 /*
1755 * We don't reserve a dynamic address for devices that
1756 * don't explicitly request one.
1757 */
1758 if (!i3cboardinfo->init_dyn_addr)
1759 continue;
1760
1761 ret = i3c_bus_get_addr_slot_status(&master->bus,
1762 i3cboardinfo->init_dyn_addr);
1763 if (ret != I3C_ADDR_SLOT_FREE) {
1764 ret = -EBUSY;
1765 goto err_rstdaa;
1766 }
1767
1768 i3c_bus_set_addr_slot_status(&master->bus,
1769 i3cboardinfo->init_dyn_addr,
1770 I3C_ADDR_SLOT_I3C_DEV);
1771
1772 /*
1773 * Only try to create/attach devices that have a static
1774 * address. Other devices will be created/attached when
1775 * DAA happens, and the requested dynamic address will
1776 * be set using SETNEWDA once those devices become
1777 * addressable.
1778 */
1779
1780 if (i3cboardinfo->static_addr)
1781 i3c_master_early_i3c_dev_add(master, i3cboardinfo);
1782 }
1783
1784 ret = i3c_master_do_daa(master);
1785 if (ret)
1786 goto err_rstdaa;
1787
1788 return 0;
1789
1790 err_rstdaa:
1791 i3c_master_rstdaa_locked(master, I3C_BROADCAST_ADDR);
1792
1793 err_bus_cleanup:
1794 if (master->ops->bus_cleanup)
1795 master->ops->bus_cleanup(master);
1796
1797 err_detach_devs:
1798 i3c_master_detach_free_devs(master);
1799
1800 return ret;
1801 }
1802
i3c_master_bus_cleanup(struct i3c_master_controller * master)1803 static void i3c_master_bus_cleanup(struct i3c_master_controller *master)
1804 {
1805 if (master->ops->bus_cleanup)
1806 master->ops->bus_cleanup(master);
1807
1808 i3c_master_detach_free_devs(master);
1809 }
1810
i3c_master_attach_boardinfo(struct i3c_dev_desc * i3cdev)1811 static void i3c_master_attach_boardinfo(struct i3c_dev_desc *i3cdev)
1812 {
1813 struct i3c_master_controller *master = i3cdev->common.master;
1814 struct i3c_dev_boardinfo *i3cboardinfo;
1815
1816 list_for_each_entry(i3cboardinfo, &master->boardinfo.i3c, node) {
1817 if (i3cdev->info.pid != i3cboardinfo->pid)
1818 continue;
1819
1820 i3cdev->boardinfo = i3cboardinfo;
1821 i3cdev->info.static_addr = i3cboardinfo->static_addr;
1822 return;
1823 }
1824 }
1825
1826 static struct i3c_dev_desc *
i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc * refdev)1827 i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc *refdev)
1828 {
1829 struct i3c_master_controller *master = i3c_dev_get_master(refdev);
1830 struct i3c_dev_desc *i3cdev;
1831
1832 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) {
1833 if (i3cdev != refdev && i3cdev->info.pid == refdev->info.pid)
1834 return i3cdev;
1835 }
1836
1837 return NULL;
1838 }
1839
1840 /**
1841 * i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
1842 * @master: master used to send frames on the bus
1843 * @addr: I3C slave dynamic address assigned to the device
1844 *
1845 * This function is instantiating an I3C device object and adding it to the
1846 * I3C device list. All device information are automatically retrieved using
1847 * standard CCC commands.
1848 *
1849 * The I3C device object is returned in case the master wants to attach
1850 * private data to it using i3c_dev_set_master_data().
1851 *
1852 * This function must be called with the bus lock held in write mode.
1853 *
1854 * Return: a 0 in case of success, an negative error code otherwise.
1855 */
i3c_master_add_i3c_dev_locked(struct i3c_master_controller * master,u8 addr)1856 int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master,
1857 u8 addr)
1858 {
1859 struct i3c_device_info info = { .dyn_addr = addr };
1860 struct i3c_dev_desc *newdev, *olddev;
1861 u8 old_dyn_addr = addr, expected_dyn_addr;
1862 struct i3c_ibi_setup ibireq = { };
1863 bool enable_ibi = false;
1864 int ret;
1865
1866 if (!master)
1867 return -EINVAL;
1868
1869 newdev = i3c_master_alloc_i3c_dev(master, &info);
1870 if (IS_ERR(newdev))
1871 return PTR_ERR(newdev);
1872
1873 ret = i3c_master_attach_i3c_dev(master, newdev);
1874 if (ret)
1875 goto err_free_dev;
1876
1877 ret = i3c_master_retrieve_dev_info(newdev);
1878 if (ret)
1879 goto err_detach_dev;
1880
1881 i3c_master_attach_boardinfo(newdev);
1882
1883 olddev = i3c_master_search_i3c_dev_duplicate(newdev);
1884 if (olddev) {
1885 newdev->dev = olddev->dev;
1886 if (newdev->dev)
1887 newdev->dev->desc = newdev;
1888
1889 /*
1890 * We need to restore the IBI state too, so let's save the
1891 * IBI information and try to restore them after olddev has
1892 * been detached+released and its IBI has been stopped and
1893 * the associated resources have been freed.
1894 */
1895 mutex_lock(&olddev->ibi_lock);
1896 if (olddev->ibi) {
1897 ibireq.handler = olddev->ibi->handler;
1898 ibireq.max_payload_len = olddev->ibi->max_payload_len;
1899 ibireq.num_slots = olddev->ibi->num_slots;
1900
1901 if (olddev->ibi->enabled) {
1902 enable_ibi = true;
1903 i3c_dev_disable_ibi_locked(olddev);
1904 }
1905
1906 i3c_dev_free_ibi_locked(olddev);
1907 }
1908 mutex_unlock(&olddev->ibi_lock);
1909
1910 old_dyn_addr = olddev->info.dyn_addr;
1911
1912 i3c_master_detach_i3c_dev(olddev);
1913 i3c_master_free_i3c_dev(olddev);
1914 }
1915
1916 /*
1917 * Depending on our previous state, the expected dynamic address might
1918 * differ:
1919 * - if the device already had a dynamic address assigned, let's try to
1920 * re-apply this one
1921 * - if the device did not have a dynamic address and the firmware
1922 * requested a specific address, pick this one
1923 * - in any other case, keep the address automatically assigned by the
1924 * master
1925 */
1926 if (old_dyn_addr && old_dyn_addr != newdev->info.dyn_addr)
1927 expected_dyn_addr = old_dyn_addr;
1928 else if (newdev->boardinfo && newdev->boardinfo->init_dyn_addr)
1929 expected_dyn_addr = newdev->boardinfo->init_dyn_addr;
1930 else
1931 expected_dyn_addr = newdev->info.dyn_addr;
1932
1933 if (newdev->info.dyn_addr != expected_dyn_addr) {
1934 /*
1935 * Try to apply the expected dynamic address. If it fails, keep
1936 * the address assigned by the master.
1937 */
1938 ret = i3c_master_setnewda_locked(master,
1939 newdev->info.dyn_addr,
1940 expected_dyn_addr);
1941 if (!ret) {
1942 old_dyn_addr = newdev->info.dyn_addr;
1943 newdev->info.dyn_addr = expected_dyn_addr;
1944 i3c_master_reattach_i3c_dev(newdev, old_dyn_addr);
1945 } else {
1946 dev_err(&master->dev,
1947 "Failed to assign reserved/old address to device %d%llx",
1948 master->bus.id, newdev->info.pid);
1949 }
1950 }
1951
1952 /*
1953 * Now is time to try to restore the IBI setup. If we're lucky,
1954 * everything works as before, otherwise, all we can do is complain.
1955 * FIXME: maybe we should add callback to inform the driver that it
1956 * should request the IBI again instead of trying to hide that from
1957 * him.
1958 */
1959 if (ibireq.handler) {
1960 mutex_lock(&newdev->ibi_lock);
1961 ret = i3c_dev_request_ibi_locked(newdev, &ibireq);
1962 if (ret) {
1963 dev_err(&master->dev,
1964 "Failed to request IBI on device %d-%llx",
1965 master->bus.id, newdev->info.pid);
1966 } else if (enable_ibi) {
1967 ret = i3c_dev_enable_ibi_locked(newdev);
1968 if (ret)
1969 dev_err(&master->dev,
1970 "Failed to re-enable IBI on device %d-%llx",
1971 master->bus.id, newdev->info.pid);
1972 }
1973 mutex_unlock(&newdev->ibi_lock);
1974 }
1975
1976 return 0;
1977
1978 err_detach_dev:
1979 if (newdev->dev && newdev->dev->desc)
1980 newdev->dev->desc = NULL;
1981
1982 i3c_master_detach_i3c_dev(newdev);
1983
1984 err_free_dev:
1985 i3c_master_free_i3c_dev(newdev);
1986
1987 return ret;
1988 }
1989 EXPORT_SYMBOL_GPL(i3c_master_add_i3c_dev_locked);
1990
1991 #define OF_I3C_REG1_IS_I2C_DEV BIT(31)
1992
1993 static int
of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller * master,struct device_node * node,u32 * reg)1994 of_i3c_master_add_i2c_boardinfo(struct i3c_master_controller *master,
1995 struct device_node *node, u32 *reg)
1996 {
1997 struct i2c_dev_boardinfo *boardinfo;
1998 struct device *dev = &master->dev;
1999 int ret;
2000
2001 boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
2002 if (!boardinfo)
2003 return -ENOMEM;
2004
2005 ret = of_i2c_get_board_info(dev, node, &boardinfo->base);
2006 if (ret)
2007 return ret;
2008
2009 /*
2010 * The I3C Specification does not clearly say I2C devices with 10-bit
2011 * address are supported. These devices can't be passed properly through
2012 * DEFSLVS command.
2013 */
2014 if (boardinfo->base.flags & I2C_CLIENT_TEN) {
2015 dev_err(dev, "I2C device with 10 bit address not supported.");
2016 return -ENOTSUPP;
2017 }
2018
2019 /* LVR is encoded in reg[2]. */
2020 boardinfo->lvr = reg[2];
2021
2022 list_add_tail(&boardinfo->node, &master->boardinfo.i2c);
2023 of_node_get(node);
2024
2025 return 0;
2026 }
2027
2028 static int
of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller * master,struct device_node * node,u32 * reg)2029 of_i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master,
2030 struct device_node *node, u32 *reg)
2031 {
2032 struct i3c_dev_boardinfo *boardinfo;
2033 struct device *dev = &master->dev;
2034 enum i3c_addr_slot_status addrstatus;
2035 u32 init_dyn_addr = 0;
2036
2037 boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
2038 if (!boardinfo)
2039 return -ENOMEM;
2040
2041 if (reg[0]) {
2042 if (reg[0] > I3C_MAX_ADDR)
2043 return -EINVAL;
2044
2045 addrstatus = i3c_bus_get_addr_slot_status(&master->bus,
2046 reg[0]);
2047 if (addrstatus != I3C_ADDR_SLOT_FREE)
2048 return -EINVAL;
2049 }
2050
2051 boardinfo->static_addr = reg[0];
2052
2053 if (!of_property_read_u32(node, "assigned-address", &init_dyn_addr)) {
2054 if (init_dyn_addr > I3C_MAX_ADDR)
2055 return -EINVAL;
2056
2057 addrstatus = i3c_bus_get_addr_slot_status(&master->bus,
2058 init_dyn_addr);
2059 if (addrstatus != I3C_ADDR_SLOT_FREE)
2060 return -EINVAL;
2061 }
2062
2063 boardinfo->pid = ((u64)reg[1] << 32) | reg[2];
2064
2065 if ((boardinfo->pid & GENMASK_ULL(63, 48)) ||
2066 I3C_PID_RND_LOWER_32BITS(boardinfo->pid))
2067 return -EINVAL;
2068
2069 boardinfo->init_dyn_addr = init_dyn_addr;
2070 boardinfo->of_node = of_node_get(node);
2071 list_add_tail(&boardinfo->node, &master->boardinfo.i3c);
2072
2073 return 0;
2074 }
2075
of_i3c_master_add_dev(struct i3c_master_controller * master,struct device_node * node)2076 static int of_i3c_master_add_dev(struct i3c_master_controller *master,
2077 struct device_node *node)
2078 {
2079 u32 reg[3];
2080 int ret;
2081
2082 if (!master || !node)
2083 return -EINVAL;
2084
2085 ret = of_property_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg));
2086 if (ret)
2087 return ret;
2088
2089 /*
2090 * The manufacturer ID can't be 0. If reg[1] == 0 that means we're
2091 * dealing with an I2C device.
2092 */
2093 if (!reg[1])
2094 ret = of_i3c_master_add_i2c_boardinfo(master, node, reg);
2095 else
2096 ret = of_i3c_master_add_i3c_boardinfo(master, node, reg);
2097
2098 return ret;
2099 }
2100
of_populate_i3c_bus(struct i3c_master_controller * master)2101 static int of_populate_i3c_bus(struct i3c_master_controller *master)
2102 {
2103 struct device *dev = &master->dev;
2104 struct device_node *i3cbus_np = dev->of_node;
2105 struct device_node *node;
2106 int ret;
2107 u32 val;
2108
2109 if (!i3cbus_np)
2110 return 0;
2111
2112 for_each_available_child_of_node(i3cbus_np, node) {
2113 ret = of_i3c_master_add_dev(master, node);
2114 if (ret) {
2115 of_node_put(node);
2116 return ret;
2117 }
2118 }
2119
2120 /*
2121 * The user might want to limit I2C and I3C speed in case some devices
2122 * on the bus are not supporting typical rates, or if the bus topology
2123 * prevents it from using max possible rate.
2124 */
2125 if (!of_property_read_u32(i3cbus_np, "i2c-scl-hz", &val))
2126 master->bus.scl_rate.i2c = val;
2127
2128 if (!of_property_read_u32(i3cbus_np, "i3c-scl-hz", &val))
2129 master->bus.scl_rate.i3c = val;
2130
2131 return 0;
2132 }
2133
i3c_master_i2c_adapter_xfer(struct i2c_adapter * adap,struct i2c_msg * xfers,int nxfers)2134 static int i3c_master_i2c_adapter_xfer(struct i2c_adapter *adap,
2135 struct i2c_msg *xfers, int nxfers)
2136 {
2137 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2138 struct i2c_dev_desc *dev;
2139 int i, ret;
2140 u16 addr;
2141
2142 if (!xfers || !master || nxfers <= 0)
2143 return -EINVAL;
2144
2145 if (!master->ops->i2c_xfers)
2146 return -ENOTSUPP;
2147
2148 /* Doing transfers to different devices is not supported. */
2149 addr = xfers[0].addr;
2150 for (i = 1; i < nxfers; i++) {
2151 if (addr != xfers[i].addr)
2152 return -ENOTSUPP;
2153 }
2154
2155 i3c_bus_normaluse_lock(&master->bus);
2156 dev = i3c_master_find_i2c_dev_by_addr(master, addr);
2157 if (!dev)
2158 ret = -ENOENT;
2159 else
2160 ret = master->ops->i2c_xfers(dev, xfers, nxfers);
2161 i3c_bus_normaluse_unlock(&master->bus);
2162
2163 return ret ? ret : nxfers;
2164 }
2165
i3c_master_i2c_funcs(struct i2c_adapter * adapter)2166 static u32 i3c_master_i2c_funcs(struct i2c_adapter *adapter)
2167 {
2168 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
2169 }
2170
i3c_master_i2c_get_lvr(struct i2c_client * client)2171 static u8 i3c_master_i2c_get_lvr(struct i2c_client *client)
2172 {
2173 /* Fall back to no spike filters and FM bus mode. */
2174 u8 lvr = I3C_LVR_I2C_INDEX(2) | I3C_LVR_I2C_FM_MODE;
2175
2176 if (client->dev.of_node) {
2177 u32 reg[3];
2178
2179 if (!of_property_read_u32_array(client->dev.of_node, "reg",
2180 reg, ARRAY_SIZE(reg)))
2181 lvr = reg[2];
2182 }
2183
2184 return lvr;
2185 }
2186
i3c_master_i2c_attach(struct i2c_adapter * adap,struct i2c_client * client)2187 static int i3c_master_i2c_attach(struct i2c_adapter *adap, struct i2c_client *client)
2188 {
2189 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2190 enum i3c_addr_slot_status status;
2191 struct i2c_dev_desc *i2cdev;
2192 int ret;
2193
2194 /* Already added by board info? */
2195 if (i3c_master_find_i2c_dev_by_addr(master, client->addr))
2196 return 0;
2197
2198 status = i3c_bus_get_addr_slot_status(&master->bus, client->addr);
2199 if (status != I3C_ADDR_SLOT_FREE)
2200 return -EBUSY;
2201
2202 i3c_bus_set_addr_slot_status(&master->bus, client->addr,
2203 I3C_ADDR_SLOT_I2C_DEV);
2204
2205 i2cdev = i3c_master_alloc_i2c_dev(master, client->addr,
2206 i3c_master_i2c_get_lvr(client));
2207 if (IS_ERR(i2cdev)) {
2208 ret = PTR_ERR(i2cdev);
2209 goto out_clear_status;
2210 }
2211
2212 ret = i3c_master_attach_i2c_dev(master, i2cdev);
2213 if (ret)
2214 goto out_free_dev;
2215
2216 return 0;
2217
2218 out_free_dev:
2219 i3c_master_free_i2c_dev(i2cdev);
2220 out_clear_status:
2221 i3c_bus_set_addr_slot_status(&master->bus, client->addr,
2222 I3C_ADDR_SLOT_FREE);
2223
2224 return ret;
2225 }
2226
i3c_master_i2c_detach(struct i2c_adapter * adap,struct i2c_client * client)2227 static int i3c_master_i2c_detach(struct i2c_adapter *adap, struct i2c_client *client)
2228 {
2229 struct i3c_master_controller *master = i2c_adapter_to_i3c_master(adap);
2230 struct i2c_dev_desc *dev;
2231
2232 dev = i3c_master_find_i2c_dev_by_addr(master, client->addr);
2233 if (!dev)
2234 return -ENODEV;
2235
2236 i3c_master_detach_i2c_dev(dev);
2237 i3c_bus_set_addr_slot_status(&master->bus, dev->addr,
2238 I3C_ADDR_SLOT_FREE);
2239 i3c_master_free_i2c_dev(dev);
2240
2241 return 0;
2242 }
2243
2244 static const struct i2c_algorithm i3c_master_i2c_algo = {
2245 .master_xfer = i3c_master_i2c_adapter_xfer,
2246 .functionality = i3c_master_i2c_funcs,
2247 };
2248
i3c_i2c_notifier_call(struct notifier_block * nb,unsigned long action,void * data)2249 static int i3c_i2c_notifier_call(struct notifier_block *nb, unsigned long action,
2250 void *data)
2251 {
2252 struct i2c_adapter *adap;
2253 struct i2c_client *client;
2254 struct device *dev = data;
2255 struct i3c_master_controller *master;
2256 int ret;
2257
2258 if (dev->type != &i2c_client_type)
2259 return 0;
2260
2261 client = to_i2c_client(dev);
2262 adap = client->adapter;
2263
2264 if (adap->algo != &i3c_master_i2c_algo)
2265 return 0;
2266
2267 master = i2c_adapter_to_i3c_master(adap);
2268
2269 i3c_bus_maintenance_lock(&master->bus);
2270 switch (action) {
2271 case BUS_NOTIFY_ADD_DEVICE:
2272 ret = i3c_master_i2c_attach(adap, client);
2273 break;
2274 case BUS_NOTIFY_DEL_DEVICE:
2275 ret = i3c_master_i2c_detach(adap, client);
2276 break;
2277 }
2278 i3c_bus_maintenance_unlock(&master->bus);
2279
2280 return ret;
2281 }
2282
2283 static struct notifier_block i2cdev_notifier = {
2284 .notifier_call = i3c_i2c_notifier_call,
2285 };
2286
i3c_master_i2c_adapter_init(struct i3c_master_controller * master)2287 static int i3c_master_i2c_adapter_init(struct i3c_master_controller *master)
2288 {
2289 struct i2c_adapter *adap = i3c_master_to_i2c_adapter(master);
2290 struct i2c_dev_desc *i2cdev;
2291 struct i2c_dev_boardinfo *i2cboardinfo;
2292 int ret;
2293
2294 adap->dev.parent = master->dev.parent;
2295 adap->owner = master->dev.parent->driver->owner;
2296 adap->algo = &i3c_master_i2c_algo;
2297 strncpy(adap->name, dev_name(master->dev.parent), sizeof(adap->name));
2298
2299 /* FIXME: Should we allow i3c masters to override these values? */
2300 adap->timeout = 1000;
2301 adap->retries = 3;
2302
2303 ret = i2c_add_adapter(adap);
2304 if (ret)
2305 return ret;
2306
2307 /*
2308 * We silently ignore failures here. The bus should keep working
2309 * correctly even if one or more i2c devices are not registered.
2310 */
2311 list_for_each_entry(i2cboardinfo, &master->boardinfo.i2c, node) {
2312 i2cdev = i3c_master_find_i2c_dev_by_addr(master,
2313 i2cboardinfo->base.addr);
2314 if (WARN_ON(!i2cdev))
2315 continue;
2316 i2cdev->dev = i2c_new_client_device(adap, &i2cboardinfo->base);
2317 }
2318
2319 return 0;
2320 }
2321
i3c_master_i2c_adapter_cleanup(struct i3c_master_controller * master)2322 static void i3c_master_i2c_adapter_cleanup(struct i3c_master_controller *master)
2323 {
2324 struct i2c_dev_desc *i2cdev;
2325
2326 i2c_del_adapter(&master->i2c);
2327
2328 i3c_bus_for_each_i2cdev(&master->bus, i2cdev)
2329 i2cdev->dev = NULL;
2330 }
2331
i3c_master_unregister_i3c_devs(struct i3c_master_controller * master)2332 static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
2333 {
2334 struct i3c_dev_desc *i3cdev;
2335
2336 i3c_bus_for_each_i3cdev(&master->bus, i3cdev) {
2337 if (!i3cdev->dev)
2338 continue;
2339
2340 i3cdev->dev->desc = NULL;
2341 if (device_is_registered(&i3cdev->dev->dev))
2342 device_unregister(&i3cdev->dev->dev);
2343 else
2344 put_device(&i3cdev->dev->dev);
2345 i3cdev->dev = NULL;
2346 }
2347 }
2348
2349 /**
2350 * i3c_master_queue_ibi() - Queue an IBI
2351 * @dev: the device this IBI is coming from
2352 * @slot: the IBI slot used to store the payload
2353 *
2354 * Queue an IBI to the controller workqueue. The IBI handler attached to
2355 * the dev will be called from a workqueue context.
2356 */
i3c_master_queue_ibi(struct i3c_dev_desc * dev,struct i3c_ibi_slot * slot)2357 void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot)
2358 {
2359 atomic_inc(&dev->ibi->pending_ibis);
2360 queue_work(dev->common.master->wq, &slot->work);
2361 }
2362 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi);
2363
i3c_master_handle_ibi(struct work_struct * work)2364 static void i3c_master_handle_ibi(struct work_struct *work)
2365 {
2366 struct i3c_ibi_slot *slot = container_of(work, struct i3c_ibi_slot,
2367 work);
2368 struct i3c_dev_desc *dev = slot->dev;
2369 struct i3c_master_controller *master = i3c_dev_get_master(dev);
2370 struct i3c_ibi_payload payload;
2371
2372 payload.data = slot->data;
2373 payload.len = slot->len;
2374
2375 if (dev->dev)
2376 dev->ibi->handler(dev->dev, &payload);
2377
2378 master->ops->recycle_ibi_slot(dev, slot);
2379 if (atomic_dec_and_test(&dev->ibi->pending_ibis))
2380 complete(&dev->ibi->all_ibis_handled);
2381 }
2382
i3c_master_init_ibi_slot(struct i3c_dev_desc * dev,struct i3c_ibi_slot * slot)2383 static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
2384 struct i3c_ibi_slot *slot)
2385 {
2386 slot->dev = dev;
2387 INIT_WORK(&slot->work, i3c_master_handle_ibi);
2388 }
2389
2390 struct i3c_generic_ibi_slot {
2391 struct list_head node;
2392 struct i3c_ibi_slot base;
2393 };
2394
2395 struct i3c_generic_ibi_pool {
2396 spinlock_t lock;
2397 unsigned int num_slots;
2398 struct i3c_generic_ibi_slot *slots;
2399 void *payload_buf;
2400 struct list_head free_slots;
2401 struct list_head pending;
2402 };
2403
2404 /**
2405 * i3c_generic_ibi_free_pool() - Free a generic IBI pool
2406 * @pool: the IBI pool to free
2407 *
2408 * Free all IBI slots allated by a generic IBI pool.
2409 */
i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool * pool)2410 void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
2411 {
2412 struct i3c_generic_ibi_slot *slot;
2413 unsigned int nslots = 0;
2414
2415 while (!list_empty(&pool->free_slots)) {
2416 slot = list_first_entry(&pool->free_slots,
2417 struct i3c_generic_ibi_slot, node);
2418 list_del(&slot->node);
2419 nslots++;
2420 }
2421
2422 /*
2423 * If the number of freed slots is not equal to the number of allocated
2424 * slots we have a leak somewhere.
2425 */
2426 WARN_ON(nslots != pool->num_slots);
2427
2428 kfree(pool->payload_buf);
2429 kfree(pool->slots);
2430 kfree(pool);
2431 }
2432 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool);
2433
2434 /**
2435 * i3c_generic_ibi_alloc_pool() - Create a generic IBI pool
2436 * @dev: the device this pool will be used for
2437 * @req: IBI setup request describing what the device driver expects
2438 *
2439 * Create a generic IBI pool based on the information provided in @req.
2440 *
2441 * Return: a valid IBI pool in case of success, an ERR_PTR() otherwise.
2442 */
2443 struct i3c_generic_ibi_pool *
i3c_generic_ibi_alloc_pool(struct i3c_dev_desc * dev,const struct i3c_ibi_setup * req)2444 i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
2445 const struct i3c_ibi_setup *req)
2446 {
2447 struct i3c_generic_ibi_pool *pool;
2448 struct i3c_generic_ibi_slot *slot;
2449 unsigned int i;
2450 int ret;
2451
2452 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
2453 if (!pool)
2454 return ERR_PTR(-ENOMEM);
2455
2456 spin_lock_init(&pool->lock);
2457 INIT_LIST_HEAD(&pool->free_slots);
2458 INIT_LIST_HEAD(&pool->pending);
2459
2460 pool->slots = kcalloc(req->num_slots, sizeof(*slot), GFP_KERNEL);
2461 if (!pool->slots) {
2462 ret = -ENOMEM;
2463 goto err_free_pool;
2464 }
2465
2466 if (req->max_payload_len) {
2467 pool->payload_buf = kcalloc(req->num_slots,
2468 req->max_payload_len, GFP_KERNEL);
2469 if (!pool->payload_buf) {
2470 ret = -ENOMEM;
2471 goto err_free_pool;
2472 }
2473 }
2474
2475 for (i = 0; i < req->num_slots; i++) {
2476 slot = &pool->slots[i];
2477 i3c_master_init_ibi_slot(dev, &slot->base);
2478
2479 if (req->max_payload_len)
2480 slot->base.data = pool->payload_buf +
2481 (i * req->max_payload_len);
2482
2483 list_add_tail(&slot->node, &pool->free_slots);
2484 pool->num_slots++;
2485 }
2486
2487 return pool;
2488
2489 err_free_pool:
2490 i3c_generic_ibi_free_pool(pool);
2491 return ERR_PTR(ret);
2492 }
2493 EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool);
2494
2495 /**
2496 * i3c_generic_ibi_get_free_slot() - Get a free slot from a generic IBI pool
2497 * @pool: the pool to query an IBI slot on
2498 *
2499 * Search for a free slot in a generic IBI pool.
2500 * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot()
2501 * when it's no longer needed.
2502 *
2503 * Return: a pointer to a free slot, or NULL if there's no free slot available.
2504 */
2505 struct i3c_ibi_slot *
i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool * pool)2506 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool)
2507 {
2508 struct i3c_generic_ibi_slot *slot;
2509 unsigned long flags;
2510
2511 spin_lock_irqsave(&pool->lock, flags);
2512 slot = list_first_entry_or_null(&pool->free_slots,
2513 struct i3c_generic_ibi_slot, node);
2514 if (slot)
2515 list_del(&slot->node);
2516 spin_unlock_irqrestore(&pool->lock, flags);
2517
2518 return slot ? &slot->base : NULL;
2519 }
2520 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot);
2521
2522 /**
2523 * i3c_generic_ibi_recycle_slot() - Return a slot to a generic IBI pool
2524 * @pool: the pool to return the IBI slot to
2525 * @s: IBI slot to recycle
2526 *
2527 * Add an IBI slot back to its generic IBI pool. Should be called from the
2528 * master driver struct_master_controller_ops->recycle_ibi() method.
2529 */
i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool * pool,struct i3c_ibi_slot * s)2530 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
2531 struct i3c_ibi_slot *s)
2532 {
2533 struct i3c_generic_ibi_slot *slot;
2534 unsigned long flags;
2535
2536 if (!s)
2537 return;
2538
2539 slot = container_of(s, struct i3c_generic_ibi_slot, base);
2540 spin_lock_irqsave(&pool->lock, flags);
2541 list_add_tail(&slot->node, &pool->free_slots);
2542 spin_unlock_irqrestore(&pool->lock, flags);
2543 }
2544 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot);
2545
i3c_master_check_ops(const struct i3c_master_controller_ops * ops)2546 static int i3c_master_check_ops(const struct i3c_master_controller_ops *ops)
2547 {
2548 if (!ops || !ops->bus_init || !ops->priv_xfers ||
2549 !ops->send_ccc_cmd || !ops->do_daa || !ops->i2c_xfers)
2550 return -EINVAL;
2551
2552 if (ops->request_ibi &&
2553 (!ops->enable_ibi || !ops->disable_ibi || !ops->free_ibi ||
2554 !ops->recycle_ibi_slot))
2555 return -EINVAL;
2556
2557 return 0;
2558 }
2559
2560 /**
2561 * i3c_master_register() - register an I3C master
2562 * @master: master used to send frames on the bus
2563 * @parent: the parent device (the one that provides this I3C master
2564 * controller)
2565 * @ops: the master controller operations
2566 * @secondary: true if you are registering a secondary master. Will return
2567 * -ENOTSUPP if set to true since secondary masters are not yet
2568 * supported
2569 *
2570 * This function takes care of everything for you:
2571 *
2572 * - creates and initializes the I3C bus
2573 * - populates the bus with static I2C devs if @parent->of_node is not
2574 * NULL
2575 * - registers all I3C devices added by the controller during bus
2576 * initialization
2577 * - registers the I2C adapter and all I2C devices
2578 *
2579 * Return: 0 in case of success, a negative error code otherwise.
2580 */
i3c_master_register(struct i3c_master_controller * master,struct device * parent,const struct i3c_master_controller_ops * ops,bool secondary)2581 int i3c_master_register(struct i3c_master_controller *master,
2582 struct device *parent,
2583 const struct i3c_master_controller_ops *ops,
2584 bool secondary)
2585 {
2586 unsigned long i2c_scl_rate = I3C_BUS_I2C_FM_PLUS_SCL_RATE;
2587 struct i3c_bus *i3cbus = i3c_master_get_bus(master);
2588 enum i3c_bus_mode mode = I3C_BUS_MODE_PURE;
2589 struct i2c_dev_boardinfo *i2cbi;
2590 int ret;
2591
2592 /* We do not support secondary masters yet. */
2593 if (secondary)
2594 return -ENOTSUPP;
2595
2596 ret = i3c_master_check_ops(ops);
2597 if (ret)
2598 return ret;
2599
2600 master->dev.parent = parent;
2601 master->dev.of_node = of_node_get(parent->of_node);
2602 master->dev.bus = &i3c_bus_type;
2603 master->dev.type = &i3c_masterdev_type;
2604 master->dev.release = i3c_masterdev_release;
2605 master->ops = ops;
2606 master->secondary = secondary;
2607 INIT_LIST_HEAD(&master->boardinfo.i2c);
2608 INIT_LIST_HEAD(&master->boardinfo.i3c);
2609
2610 ret = i3c_bus_init(i3cbus);
2611 if (ret)
2612 return ret;
2613
2614 device_initialize(&master->dev);
2615 dev_set_name(&master->dev, "i3c-%d", i3cbus->id);
2616
2617 ret = of_populate_i3c_bus(master);
2618 if (ret)
2619 goto err_put_dev;
2620
2621 list_for_each_entry(i2cbi, &master->boardinfo.i2c, node) {
2622 switch (i2cbi->lvr & I3C_LVR_I2C_INDEX_MASK) {
2623 case I3C_LVR_I2C_INDEX(0):
2624 if (mode < I3C_BUS_MODE_MIXED_FAST)
2625 mode = I3C_BUS_MODE_MIXED_FAST;
2626 break;
2627 case I3C_LVR_I2C_INDEX(1):
2628 if (mode < I3C_BUS_MODE_MIXED_LIMITED)
2629 mode = I3C_BUS_MODE_MIXED_LIMITED;
2630 break;
2631 case I3C_LVR_I2C_INDEX(2):
2632 if (mode < I3C_BUS_MODE_MIXED_SLOW)
2633 mode = I3C_BUS_MODE_MIXED_SLOW;
2634 break;
2635 default:
2636 ret = -EINVAL;
2637 goto err_put_dev;
2638 }
2639
2640 if (i2cbi->lvr & I3C_LVR_I2C_FM_MODE)
2641 i2c_scl_rate = I3C_BUS_I2C_FM_SCL_RATE;
2642 }
2643
2644 ret = i3c_bus_set_mode(i3cbus, mode, i2c_scl_rate);
2645 if (ret)
2646 goto err_put_dev;
2647
2648 master->wq = alloc_workqueue("%s", 0, 0, dev_name(parent));
2649 if (!master->wq) {
2650 ret = -ENOMEM;
2651 goto err_put_dev;
2652 }
2653
2654 ret = i3c_master_bus_init(master);
2655 if (ret)
2656 goto err_put_dev;
2657
2658 ret = device_add(&master->dev);
2659 if (ret)
2660 goto err_cleanup_bus;
2661
2662 /*
2663 * Expose our I3C bus as an I2C adapter so that I2C devices are exposed
2664 * through the I2C subsystem.
2665 */
2666 ret = i3c_master_i2c_adapter_init(master);
2667 if (ret)
2668 goto err_del_dev;
2669
2670 /*
2671 * We're done initializing the bus and the controller, we can now
2672 * register I3C devices discovered during the initial DAA.
2673 */
2674 master->init_done = true;
2675 i3c_bus_normaluse_lock(&master->bus);
2676 i3c_master_register_new_i3c_devs(master);
2677 i3c_bus_normaluse_unlock(&master->bus);
2678
2679 return 0;
2680
2681 err_del_dev:
2682 device_del(&master->dev);
2683
2684 err_cleanup_bus:
2685 i3c_master_bus_cleanup(master);
2686
2687 err_put_dev:
2688 put_device(&master->dev);
2689
2690 return ret;
2691 }
2692 EXPORT_SYMBOL_GPL(i3c_master_register);
2693
2694 /**
2695 * i3c_master_unregister() - unregister an I3C master
2696 * @master: master used to send frames on the bus
2697 *
2698 * Basically undo everything done in i3c_master_register().
2699 *
2700 * Return: 0 in case of success, a negative error code otherwise.
2701 */
i3c_master_unregister(struct i3c_master_controller * master)2702 int i3c_master_unregister(struct i3c_master_controller *master)
2703 {
2704 i3c_master_i2c_adapter_cleanup(master);
2705 i3c_master_unregister_i3c_devs(master);
2706 i3c_master_bus_cleanup(master);
2707 device_unregister(&master->dev);
2708
2709 return 0;
2710 }
2711 EXPORT_SYMBOL_GPL(i3c_master_unregister);
2712
i3c_dev_do_priv_xfers_locked(struct i3c_dev_desc * dev,struct i3c_priv_xfer * xfers,int nxfers)2713 int i3c_dev_do_priv_xfers_locked(struct i3c_dev_desc *dev,
2714 struct i3c_priv_xfer *xfers,
2715 int nxfers)
2716 {
2717 struct i3c_master_controller *master;
2718
2719 if (!dev)
2720 return -ENOENT;
2721
2722 master = i3c_dev_get_master(dev);
2723 if (!master || !xfers)
2724 return -EINVAL;
2725
2726 if (!master->ops->priv_xfers)
2727 return -ENOTSUPP;
2728
2729 return master->ops->priv_xfers(dev, xfers, nxfers);
2730 }
2731
i3c_dev_disable_ibi_locked(struct i3c_dev_desc * dev)2732 int i3c_dev_disable_ibi_locked(struct i3c_dev_desc *dev)
2733 {
2734 struct i3c_master_controller *master;
2735 int ret;
2736
2737 if (!dev->ibi)
2738 return -EINVAL;
2739
2740 master = i3c_dev_get_master(dev);
2741 ret = master->ops->disable_ibi(dev);
2742 if (ret)
2743 return ret;
2744
2745 reinit_completion(&dev->ibi->all_ibis_handled);
2746 if (atomic_read(&dev->ibi->pending_ibis))
2747 wait_for_completion(&dev->ibi->all_ibis_handled);
2748
2749 dev->ibi->enabled = false;
2750
2751 return 0;
2752 }
2753
i3c_dev_enable_ibi_locked(struct i3c_dev_desc * dev)2754 int i3c_dev_enable_ibi_locked(struct i3c_dev_desc *dev)
2755 {
2756 struct i3c_master_controller *master = i3c_dev_get_master(dev);
2757 int ret;
2758
2759 if (!dev->ibi)
2760 return -EINVAL;
2761
2762 ret = master->ops->enable_ibi(dev);
2763 if (!ret)
2764 dev->ibi->enabled = true;
2765
2766 return ret;
2767 }
2768
i3c_dev_request_ibi_locked(struct i3c_dev_desc * dev,const struct i3c_ibi_setup * req)2769 int i3c_dev_request_ibi_locked(struct i3c_dev_desc *dev,
2770 const struct i3c_ibi_setup *req)
2771 {
2772 struct i3c_master_controller *master = i3c_dev_get_master(dev);
2773 struct i3c_device_ibi_info *ibi;
2774 int ret;
2775
2776 if (!master->ops->request_ibi)
2777 return -ENOTSUPP;
2778
2779 if (dev->ibi)
2780 return -EBUSY;
2781
2782 ibi = kzalloc(sizeof(*ibi), GFP_KERNEL);
2783 if (!ibi)
2784 return -ENOMEM;
2785
2786 atomic_set(&ibi->pending_ibis, 0);
2787 init_completion(&ibi->all_ibis_handled);
2788 ibi->handler = req->handler;
2789 ibi->max_payload_len = req->max_payload_len;
2790 ibi->num_slots = req->num_slots;
2791
2792 dev->ibi = ibi;
2793 ret = master->ops->request_ibi(dev, req);
2794 if (ret) {
2795 kfree(ibi);
2796 dev->ibi = NULL;
2797 }
2798
2799 return ret;
2800 }
2801
i3c_dev_free_ibi_locked(struct i3c_dev_desc * dev)2802 void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev)
2803 {
2804 struct i3c_master_controller *master = i3c_dev_get_master(dev);
2805
2806 if (!dev->ibi)
2807 return;
2808
2809 if (WARN_ON(dev->ibi->enabled))
2810 WARN_ON(i3c_dev_disable_ibi_locked(dev));
2811
2812 master->ops->free_ibi(dev);
2813 kfree(dev->ibi);
2814 dev->ibi = NULL;
2815 }
2816
i3c_init(void)2817 static int __init i3c_init(void)
2818 {
2819 int res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
2820
2821 if (res)
2822 return res;
2823
2824 res = bus_register(&i3c_bus_type);
2825 if (res)
2826 goto out_unreg_notifier;
2827
2828 return 0;
2829
2830 out_unreg_notifier:
2831 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
2832
2833 return res;
2834 }
2835 subsys_initcall(i3c_init);
2836
i3c_exit(void)2837 static void __exit i3c_exit(void)
2838 {
2839 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
2840 idr_destroy(&i3c_bus_idr);
2841 bus_unregister(&i3c_bus_type);
2842 }
2843 module_exit(i3c_exit);
2844
2845 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
2846 MODULE_DESCRIPTION("I3C core");
2847 MODULE_LICENSE("GPL v2");
2848