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