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