1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2011-2017, The Linux Foundation 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/errno.h> 8 #include <linux/slab.h> 9 #include <linux/init.h> 10 #include <linux/idr.h> 11 #include <linux/of.h> 12 #include <linux/of_device.h> 13 #include <linux/pm_runtime.h> 14 #include <linux/slimbus.h> 15 #include "slimbus.h" 16 17 static DEFINE_IDA(ctrl_ida); 18 19 static const struct slim_device_id *slim_match(const struct slim_device_id *id, 20 const struct slim_device *sbdev) 21 { 22 while (id->manf_id != 0 || id->prod_code != 0) { 23 if (id->manf_id == sbdev->e_addr.manf_id && 24 id->prod_code == sbdev->e_addr.prod_code && 25 id->dev_index == sbdev->e_addr.dev_index && 26 id->instance == sbdev->e_addr.instance) 27 return id; 28 id++; 29 } 30 return NULL; 31 } 32 33 static int slim_device_match(struct device *dev, struct device_driver *drv) 34 { 35 struct slim_device *sbdev = to_slim_device(dev); 36 struct slim_driver *sbdrv = to_slim_driver(drv); 37 38 /* Attempt an OF style match first */ 39 if (of_driver_match_device(dev, drv)) 40 return 1; 41 42 return !!slim_match(sbdrv->id_table, sbdev); 43 } 44 45 static void slim_device_update_status(struct slim_device *sbdev, 46 enum slim_device_status status) 47 { 48 struct slim_driver *sbdrv; 49 50 if (sbdev->status == status) 51 return; 52 53 sbdev->status = status; 54 if (!sbdev->dev.driver) 55 return; 56 57 sbdrv = to_slim_driver(sbdev->dev.driver); 58 if (sbdrv->device_status) 59 sbdrv->device_status(sbdev, sbdev->status); 60 } 61 62 static int slim_device_probe(struct device *dev) 63 { 64 struct slim_device *sbdev = to_slim_device(dev); 65 struct slim_driver *sbdrv = to_slim_driver(dev->driver); 66 int ret; 67 68 ret = sbdrv->probe(sbdev); 69 if (ret) 70 return ret; 71 72 /* try getting the logical address after probe */ 73 ret = slim_get_logical_addr(sbdev); 74 if (!ret) { 75 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP); 76 } else { 77 dev_err(&sbdev->dev, "Failed to get logical address\n"); 78 ret = -EPROBE_DEFER; 79 } 80 81 return ret; 82 } 83 84 static int slim_device_remove(struct device *dev) 85 { 86 struct slim_device *sbdev = to_slim_device(dev); 87 struct slim_driver *sbdrv; 88 89 if (dev->driver) { 90 sbdrv = to_slim_driver(dev->driver); 91 if (sbdrv->remove) 92 sbdrv->remove(sbdev); 93 } 94 95 return 0; 96 } 97 98 static int slim_device_uevent(struct device *dev, struct kobj_uevent_env *env) 99 { 100 struct slim_device *sbdev = to_slim_device(dev); 101 int ret; 102 103 ret = of_device_uevent_modalias(dev, env); 104 if (ret != -ENODEV) 105 return ret; 106 107 return add_uevent_var(env, "MODALIAS=slim:%s", dev_name(&sbdev->dev)); 108 } 109 110 struct bus_type slimbus_bus = { 111 .name = "slimbus", 112 .match = slim_device_match, 113 .probe = slim_device_probe, 114 .remove = slim_device_remove, 115 .uevent = slim_device_uevent, 116 }; 117 EXPORT_SYMBOL_GPL(slimbus_bus); 118 119 /* 120 * __slim_driver_register() - Client driver registration with SLIMbus 121 * 122 * @drv:Client driver to be associated with client-device. 123 * @owner: owning module/driver 124 * 125 * This API will register the client driver with the SLIMbus 126 * It is called from the driver's module-init function. 127 */ 128 int __slim_driver_register(struct slim_driver *drv, struct module *owner) 129 { 130 /* ID table and probe are mandatory */ 131 if (!(drv->driver.of_match_table || drv->id_table) || !drv->probe) 132 return -EINVAL; 133 134 drv->driver.bus = &slimbus_bus; 135 drv->driver.owner = owner; 136 137 return driver_register(&drv->driver); 138 } 139 EXPORT_SYMBOL_GPL(__slim_driver_register); 140 141 /* 142 * slim_driver_unregister() - Undo effect of slim_driver_register 143 * 144 * @drv: Client driver to be unregistered 145 */ 146 void slim_driver_unregister(struct slim_driver *drv) 147 { 148 driver_unregister(&drv->driver); 149 } 150 EXPORT_SYMBOL_GPL(slim_driver_unregister); 151 152 static void slim_dev_release(struct device *dev) 153 { 154 struct slim_device *sbdev = to_slim_device(dev); 155 156 kfree(sbdev); 157 } 158 159 static int slim_add_device(struct slim_controller *ctrl, 160 struct slim_device *sbdev, 161 struct device_node *node) 162 { 163 sbdev->dev.bus = &slimbus_bus; 164 sbdev->dev.parent = ctrl->dev; 165 sbdev->dev.release = slim_dev_release; 166 sbdev->dev.driver = NULL; 167 sbdev->ctrl = ctrl; 168 INIT_LIST_HEAD(&sbdev->stream_list); 169 spin_lock_init(&sbdev->stream_list_lock); 170 171 if (node) 172 sbdev->dev.of_node = of_node_get(node); 173 174 dev_set_name(&sbdev->dev, "%x:%x:%x:%x", 175 sbdev->e_addr.manf_id, 176 sbdev->e_addr.prod_code, 177 sbdev->e_addr.dev_index, 178 sbdev->e_addr.instance); 179 180 return device_register(&sbdev->dev); 181 } 182 183 static struct slim_device *slim_alloc_device(struct slim_controller *ctrl, 184 struct slim_eaddr *eaddr, 185 struct device_node *node) 186 { 187 struct slim_device *sbdev; 188 int ret; 189 190 sbdev = kzalloc(sizeof(*sbdev), GFP_KERNEL); 191 if (!sbdev) 192 return NULL; 193 194 sbdev->e_addr = *eaddr; 195 ret = slim_add_device(ctrl, sbdev, node); 196 if (ret) { 197 put_device(&sbdev->dev); 198 return NULL; 199 } 200 201 return sbdev; 202 } 203 204 static void of_register_slim_devices(struct slim_controller *ctrl) 205 { 206 struct device *dev = ctrl->dev; 207 struct device_node *node; 208 209 if (!ctrl->dev->of_node) 210 return; 211 212 for_each_child_of_node(ctrl->dev->of_node, node) { 213 struct slim_device *sbdev; 214 struct slim_eaddr e_addr; 215 const char *compat = NULL; 216 int reg[2], ret; 217 int manf_id, prod_code; 218 219 compat = of_get_property(node, "compatible", NULL); 220 if (!compat) 221 continue; 222 223 ret = sscanf(compat, "slim%x,%x", &manf_id, &prod_code); 224 if (ret != 2) { 225 dev_err(dev, "Manf ID & Product code not found %s\n", 226 compat); 227 continue; 228 } 229 230 ret = of_property_read_u32_array(node, "reg", reg, 2); 231 if (ret) { 232 dev_err(dev, "Device and Instance id not found:%d\n", 233 ret); 234 continue; 235 } 236 237 e_addr.dev_index = reg[0]; 238 e_addr.instance = reg[1]; 239 e_addr.manf_id = manf_id; 240 e_addr.prod_code = prod_code; 241 242 sbdev = slim_alloc_device(ctrl, &e_addr, node); 243 if (!sbdev) 244 continue; 245 } 246 } 247 248 /* 249 * slim_register_controller() - Controller bring-up and registration. 250 * 251 * @ctrl: Controller to be registered. 252 * 253 * A controller is registered with the framework using this API. 254 * If devices on a controller were registered before controller, 255 * this will make sure that they get probed when controller is up 256 */ 257 int slim_register_controller(struct slim_controller *ctrl) 258 { 259 int id; 260 261 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL); 262 if (id < 0) 263 return id; 264 265 ctrl->id = id; 266 267 if (!ctrl->min_cg) 268 ctrl->min_cg = SLIM_MIN_CLK_GEAR; 269 if (!ctrl->max_cg) 270 ctrl->max_cg = SLIM_MAX_CLK_GEAR; 271 272 ida_init(&ctrl->laddr_ida); 273 idr_init(&ctrl->tid_idr); 274 mutex_init(&ctrl->lock); 275 mutex_init(&ctrl->sched.m_reconf); 276 init_completion(&ctrl->sched.pause_comp); 277 278 dev_dbg(ctrl->dev, "Bus [%s] registered:dev:%p\n", 279 ctrl->name, ctrl->dev); 280 281 of_register_slim_devices(ctrl); 282 283 return 0; 284 } 285 EXPORT_SYMBOL_GPL(slim_register_controller); 286 287 /* slim_remove_device: Remove the effect of slim_add_device() */ 288 static void slim_remove_device(struct slim_device *sbdev) 289 { 290 device_unregister(&sbdev->dev); 291 } 292 293 static int slim_ctrl_remove_device(struct device *dev, void *null) 294 { 295 slim_remove_device(to_slim_device(dev)); 296 return 0; 297 } 298 299 /** 300 * slim_unregister_controller() - Controller tear-down. 301 * 302 * @ctrl: Controller to tear-down. 303 */ 304 int slim_unregister_controller(struct slim_controller *ctrl) 305 { 306 /* Remove all clients */ 307 device_for_each_child(ctrl->dev, NULL, slim_ctrl_remove_device); 308 /* Enter Clock Pause */ 309 slim_ctrl_clk_pause(ctrl, false, 0); 310 ida_simple_remove(&ctrl_ida, ctrl->id); 311 312 return 0; 313 } 314 EXPORT_SYMBOL_GPL(slim_unregister_controller); 315 316 /** 317 * slim_report_absent() - Controller calls this function when a device 318 * reports absent, OR when the device cannot be communicated with 319 * 320 * @sbdev: Device that cannot be reached, or sent report absent 321 */ 322 void slim_report_absent(struct slim_device *sbdev) 323 { 324 struct slim_controller *ctrl = sbdev->ctrl; 325 326 if (!ctrl) 327 return; 328 329 /* invalidate logical addresses */ 330 mutex_lock(&ctrl->lock); 331 sbdev->is_laddr_valid = false; 332 mutex_unlock(&ctrl->lock); 333 334 ida_simple_remove(&ctrl->laddr_ida, sbdev->laddr); 335 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_DOWN); 336 } 337 EXPORT_SYMBOL_GPL(slim_report_absent); 338 339 static bool slim_eaddr_equal(struct slim_eaddr *a, struct slim_eaddr *b) 340 { 341 return (a->manf_id == b->manf_id && 342 a->prod_code == b->prod_code && 343 a->dev_index == b->dev_index && 344 a->instance == b->instance); 345 } 346 347 static int slim_match_dev(struct device *dev, void *data) 348 { 349 struct slim_eaddr *e_addr = data; 350 struct slim_device *sbdev = to_slim_device(dev); 351 352 return slim_eaddr_equal(&sbdev->e_addr, e_addr); 353 } 354 355 static struct slim_device *find_slim_device(struct slim_controller *ctrl, 356 struct slim_eaddr *eaddr) 357 { 358 struct slim_device *sbdev; 359 struct device *dev; 360 361 dev = device_find_child(ctrl->dev, eaddr, slim_match_dev); 362 if (dev) { 363 sbdev = to_slim_device(dev); 364 return sbdev; 365 } 366 367 return NULL; 368 } 369 370 /** 371 * slim_get_device() - get handle to a device. 372 * 373 * @ctrl: Controller on which this device will be added/queried 374 * @e_addr: Enumeration address of the device to be queried 375 * 376 * Return: pointer to a device if it has already reported. Creates a new 377 * device and returns pointer to it if the device has not yet enumerated. 378 */ 379 struct slim_device *slim_get_device(struct slim_controller *ctrl, 380 struct slim_eaddr *e_addr) 381 { 382 struct slim_device *sbdev; 383 384 sbdev = find_slim_device(ctrl, e_addr); 385 if (!sbdev) { 386 sbdev = slim_alloc_device(ctrl, e_addr, NULL); 387 if (!sbdev) 388 return ERR_PTR(-ENOMEM); 389 } 390 391 return sbdev; 392 } 393 EXPORT_SYMBOL_GPL(slim_get_device); 394 395 static int of_slim_match_dev(struct device *dev, void *data) 396 { 397 struct device_node *np = data; 398 struct slim_device *sbdev = to_slim_device(dev); 399 400 return (sbdev->dev.of_node == np); 401 } 402 403 static struct slim_device *of_find_slim_device(struct slim_controller *ctrl, 404 struct device_node *np) 405 { 406 struct slim_device *sbdev; 407 struct device *dev; 408 409 dev = device_find_child(ctrl->dev, np, of_slim_match_dev); 410 if (dev) { 411 sbdev = to_slim_device(dev); 412 return sbdev; 413 } 414 415 return NULL; 416 } 417 418 /** 419 * of_slim_get_device() - get handle to a device using dt node. 420 * 421 * @ctrl: Controller on which this device will be added/queried 422 * @np: node pointer to device 423 * 424 * Return: pointer to a device if it has already reported. Creates a new 425 * device and returns pointer to it if the device has not yet enumerated. 426 */ 427 struct slim_device *of_slim_get_device(struct slim_controller *ctrl, 428 struct device_node *np) 429 { 430 return of_find_slim_device(ctrl, np); 431 } 432 EXPORT_SYMBOL_GPL(of_slim_get_device); 433 434 static int slim_device_alloc_laddr(struct slim_device *sbdev, 435 bool report_present) 436 { 437 struct slim_controller *ctrl = sbdev->ctrl; 438 u8 laddr; 439 int ret; 440 441 mutex_lock(&ctrl->lock); 442 if (ctrl->get_laddr) { 443 ret = ctrl->get_laddr(ctrl, &sbdev->e_addr, &laddr); 444 if (ret < 0) 445 goto err; 446 } else if (report_present) { 447 ret = ida_simple_get(&ctrl->laddr_ida, 448 0, SLIM_LA_MANAGER - 1, GFP_KERNEL); 449 if (ret < 0) 450 goto err; 451 452 laddr = ret; 453 } else { 454 ret = -EINVAL; 455 goto err; 456 } 457 458 if (ctrl->set_laddr) { 459 ret = ctrl->set_laddr(ctrl, &sbdev->e_addr, laddr); 460 if (ret) { 461 ret = -EINVAL; 462 goto err; 463 } 464 } 465 466 sbdev->laddr = laddr; 467 sbdev->is_laddr_valid = true; 468 mutex_unlock(&ctrl->lock); 469 470 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP); 471 472 dev_dbg(ctrl->dev, "setting slimbus l-addr:%x, ea:%x,%x,%x,%x\n", 473 laddr, sbdev->e_addr.manf_id, sbdev->e_addr.prod_code, 474 sbdev->e_addr.dev_index, sbdev->e_addr.instance); 475 476 return 0; 477 478 err: 479 mutex_unlock(&ctrl->lock); 480 return ret; 481 482 } 483 484 /** 485 * slim_device_report_present() - Report enumerated device. 486 * 487 * @ctrl: Controller with which device is enumerated. 488 * @e_addr: Enumeration address of the device. 489 * @laddr: Return logical address (if valid flag is false) 490 * 491 * Called by controller in response to REPORT_PRESENT. Framework will assign 492 * a logical address to this enumeration address. 493 * Function returns -EXFULL to indicate that all logical addresses are already 494 * taken. 495 */ 496 int slim_device_report_present(struct slim_controller *ctrl, 497 struct slim_eaddr *e_addr, u8 *laddr) 498 { 499 struct slim_device *sbdev; 500 int ret; 501 502 ret = pm_runtime_get_sync(ctrl->dev); 503 504 if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) { 505 dev_err(ctrl->dev, "slim ctrl not active,state:%d, ret:%d\n", 506 ctrl->sched.clk_state, ret); 507 goto slimbus_not_active; 508 } 509 510 sbdev = slim_get_device(ctrl, e_addr); 511 if (IS_ERR(sbdev)) 512 return -ENODEV; 513 514 if (sbdev->is_laddr_valid) { 515 *laddr = sbdev->laddr; 516 return 0; 517 } 518 519 ret = slim_device_alloc_laddr(sbdev, true); 520 521 slimbus_not_active: 522 pm_runtime_mark_last_busy(ctrl->dev); 523 pm_runtime_put_autosuspend(ctrl->dev); 524 return ret; 525 } 526 EXPORT_SYMBOL_GPL(slim_device_report_present); 527 528 /** 529 * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device. 530 * 531 * @sbdev: client handle requesting the address. 532 * 533 * Return: zero if a logical address is valid or a new logical address 534 * has been assigned. error code in case of error. 535 */ 536 int slim_get_logical_addr(struct slim_device *sbdev) 537 { 538 if (!sbdev->is_laddr_valid) 539 return slim_device_alloc_laddr(sbdev, false); 540 541 return 0; 542 } 543 EXPORT_SYMBOL_GPL(slim_get_logical_addr); 544 545 static void __exit slimbus_exit(void) 546 { 547 bus_unregister(&slimbus_bus); 548 } 549 module_exit(slimbus_exit); 550 551 static int __init slimbus_init(void) 552 { 553 return bus_register(&slimbus_bus); 554 } 555 postcore_initcall(slimbus_init); 556 557 MODULE_LICENSE("GPL v2"); 558 MODULE_DESCRIPTION("SLIMbus core"); 559