| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <[email protected]> |
| 4 |
* |
| 5 |
* Based on drivers/spmi/spmi.c: |
| 6 |
* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/acpi.h> |
| 10 |
#include <linux/errno.h> |
| 11 |
#include <linux/idr.h> |
| 12 |
#include <linux/kernel.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/of.h> |
| 15 |
#include <linux/of_device.h> |
| 16 |
#include <linux/pm_domain.h> |
| 17 |
#include <linux/pm_runtime.h> |
| 18 |
#include <linux/property.h> |
| 19 |
#include <linux/sched.h> |
| 20 |
#include <linux/serdev.h> |
| 21 |
#include <linux/slab.h> |
| 22 |
|
| 23 |
#include <linux/platform_data/x86/apple.h> |
| 24 |
|
| 25 |
static bool is_registered; |
| 26 |
static DEFINE_IDA(ctrl_ida); |
| 27 |
|
| 28 |
static ssize_t modalias_show(struct device *dev, |
| 29 |
struct device_attribute *attr, char *buf) |
| 30 |
{ |
| 31 |
int len; |
| 32 |
|
| 33 |
len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); |
| 34 |
if (len != -ENODEV) |
| 35 |
return len; |
| 36 |
|
| 37 |
return of_device_modalias(dev, buf, PAGE_SIZE); |
| 38 |
} |
| 39 |
static DEVICE_ATTR_RO(modalias); |
| 40 |
|
| 41 |
static struct attribute *serdev_device_attrs[] = { |
| 42 |
&dev_attr_modalias.attr, |
| 43 |
NULL, |
| 44 |
}; |
| 45 |
ATTRIBUTE_GROUPS(serdev_device); |
| 46 |
|
| 47 |
static int serdev_device_uevent(const struct device *dev, struct kobj_uevent_env *env) |
| 48 |
{ |
| 49 |
int rc; |
| 50 |
|
| 51 |
/* TODO: platform modalias */ |
| 52 |
|
| 53 |
rc = acpi_device_uevent_modalias(dev, env); |
| 54 |
if (rc != -ENODEV) |
| 55 |
return rc; |
| 56 |
|
| 57 |
return of_device_uevent_modalias(dev, env); |
| 58 |
} |
| 59 |
|
| 60 |
static void serdev_device_release(struct device *dev) |
| 61 |
{ |
| 62 |
struct serdev_device *serdev = to_serdev_device(dev); |
| 63 |
kfree(serdev); |
| 64 |
} |
| 65 |
|
| 66 |
static const struct device_type serdev_device_type = { |
| 67 |
.groups = serdev_device_groups, |
| 68 |
.uevent = serdev_device_uevent, |
| 69 |
.release = serdev_device_release, |
| 70 |
}; |
| 71 |
|
| 72 |
static bool is_serdev_device(const struct device *dev) |
| 73 |
{ |
| 74 |
return dev->type == &serdev_device_type; |
| 75 |
} |
| 76 |
|
| 77 |
static void serdev_ctrl_release(struct device *dev) |
| 78 |
{ |
| 79 |
struct serdev_controller *ctrl = to_serdev_controller(dev); |
| 80 |
ida_free(&ctrl_ida, ctrl->nr); |
| 81 |
kfree(ctrl); |
| 82 |
} |
| 83 |
|
| 84 |
static const struct device_type serdev_ctrl_type = { |
| 85 |
.release = serdev_ctrl_release, |
| 86 |
}; |
| 87 |
|
| 88 |
static int serdev_device_match(struct device *dev, const struct device_driver *drv) |
| 89 |
{ |
| 90 |
if (!is_serdev_device(dev)) |
| 91 |
return 0; |
| 92 |
|
| 93 |
/* TODO: platform matching */ |
| 94 |
if (acpi_driver_match_device(dev, drv)) |
| 95 |
return 1; |
| 96 |
|
| 97 |
return of_driver_match_device(dev, drv); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* serdev_device_add() - add a device previously constructed via serdev_device_alloc() |
| 102 |
* @serdev: serdev_device to be added |
| 103 |
*/ |
| 104 |
int serdev_device_add(struct serdev_device *serdev) |
| 105 |
{ |
| 106 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 107 |
struct device *parent = serdev->dev.parent; |
| 108 |
int err; |
| 109 |
|
| 110 |
dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr); |
| 111 |
|
| 112 |
/* Only a single slave device is currently supported. */ |
| 113 |
if (ctrl->serdev) { |
| 114 |
dev_err(&serdev->dev, "controller busy\n"); |
| 115 |
return -EBUSY; |
| 116 |
} |
| 117 |
ctrl->serdev = serdev; |
| 118 |
|
| 119 |
err = device_add(&serdev->dev); |
| 120 |
if (err < 0) { |
| 121 |
dev_err(&serdev->dev, "Failed to add serdev: %d\n", err); |
| 122 |
goto err_clear_serdev; |
| 123 |
} |
| 124 |
|
| 125 |
dev_dbg(&serdev->dev, "serdev registered successfully\n"); |
| 126 |
|
| 127 |
return 0; |
| 128 |
|
| 129 |
err_clear_serdev: |
| 130 |
ctrl->serdev = NULL; |
| 131 |
return err; |
| 132 |
} |
| 133 |
EXPORT_SYMBOL_GPL(serdev_device_add); |
| 134 |
|
| 135 |
/** |
| 136 |
* serdev_device_remove(): remove an serdev device |
| 137 |
* @serdev: serdev_device to be removed |
| 138 |
*/ |
| 139 |
void serdev_device_remove(struct serdev_device *serdev) |
| 140 |
{ |
| 141 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 142 |
|
| 143 |
device_unregister(&serdev->dev); |
| 144 |
ctrl->serdev = NULL; |
| 145 |
} |
| 146 |
EXPORT_SYMBOL_GPL(serdev_device_remove); |
| 147 |
|
| 148 |
int serdev_device_open(struct serdev_device *serdev) |
| 149 |
{ |
| 150 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 151 |
int ret; |
| 152 |
|
| 153 |
if (!ctrl || !ctrl->ops->open) |
| 154 |
return -EINVAL; |
| 155 |
|
| 156 |
ret = ctrl->ops->open(ctrl); |
| 157 |
if (ret) |
| 158 |
return ret; |
| 159 |
|
| 160 |
ret = pm_runtime_get_sync(&ctrl->dev); |
| 161 |
if (ret < 0) { |
| 162 |
pm_runtime_put_noidle(&ctrl->dev); |
| 163 |
goto err_close; |
| 164 |
} |
| 165 |
|
| 166 |
return 0; |
| 167 |
|
| 168 |
err_close: |
| 169 |
if (ctrl->ops->close) |
| 170 |
ctrl->ops->close(ctrl); |
| 171 |
|
| 172 |
return ret; |
| 173 |
} |
| 174 |
EXPORT_SYMBOL_GPL(serdev_device_open); |
| 175 |
|
| 176 |
void serdev_device_close(struct serdev_device *serdev) |
| 177 |
{ |
| 178 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 179 |
|
| 180 |
if (!ctrl || !ctrl->ops->close) |
| 181 |
return; |
| 182 |
|
| 183 |
pm_runtime_put(&ctrl->dev); |
| 184 |
|
| 185 |
ctrl->ops->close(ctrl); |
| 186 |
} |
| 187 |
EXPORT_SYMBOL_GPL(serdev_device_close); |
| 188 |
|
| 189 |
static void devm_serdev_device_close(void *serdev) |
| 190 |
{ |
| 191 |
serdev_device_close(serdev); |
| 192 |
} |
| 193 |
|
| 194 |
int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev) |
| 195 |
{ |
| 196 |
int ret; |
| 197 |
|
| 198 |
ret = serdev_device_open(serdev); |
| 199 |
if (ret) |
| 200 |
return ret; |
| 201 |
|
| 202 |
return devm_add_action_or_reset(dev, devm_serdev_device_close, serdev); |
| 203 |
} |
| 204 |
EXPORT_SYMBOL_GPL(devm_serdev_device_open); |
| 205 |
|
| 206 |
void serdev_device_write_wakeup(struct serdev_device *serdev) |
| 207 |
{ |
| 208 |
complete(&serdev->write_comp); |
| 209 |
} |
| 210 |
EXPORT_SYMBOL_GPL(serdev_device_write_wakeup); |
| 211 |
|
| 212 |
/** |
| 213 |
* serdev_device_write_buf() - write data asynchronously |
| 214 |
* @serdev: serdev device |
| 215 |
* @buf: data to be written |
| 216 |
* @count: number of bytes to write |
| 217 |
* |
| 218 |
* Write data to the device asynchronously. |
| 219 |
* |
| 220 |
* Note that any accepted data has only been buffered by the controller; use |
| 221 |
* serdev_device_wait_until_sent() to make sure the controller write buffer |
| 222 |
* has actually been emptied. |
| 223 |
* |
| 224 |
* Return: The number of bytes written (less than count if not enough room in |
| 225 |
* the write buffer), or a negative errno on errors. |
| 226 |
*/ |
| 227 |
int serdev_device_write_buf(struct serdev_device *serdev, const u8 *buf, size_t count) |
| 228 |
{ |
| 229 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 230 |
|
| 231 |
if (!ctrl || !ctrl->ops->write_buf) |
| 232 |
return -EINVAL; |
| 233 |
|
| 234 |
return ctrl->ops->write_buf(ctrl, buf, count); |
| 235 |
} |
| 236 |
EXPORT_SYMBOL_GPL(serdev_device_write_buf); |
| 237 |
|
| 238 |
/** |
| 239 |
* serdev_device_write() - write data synchronously |
| 240 |
* @serdev: serdev device |
| 241 |
* @buf: data to be written |
| 242 |
* @count: number of bytes to write |
| 243 |
* @timeout: timeout in jiffies, or 0 to wait indefinitely |
| 244 |
* |
| 245 |
* Write data to the device synchronously by repeatedly calling |
| 246 |
* serdev_device_write() until the controller has accepted all data (unless |
| 247 |
* interrupted by a timeout or a signal). |
| 248 |
* |
| 249 |
* Note that any accepted data has only been buffered by the controller; use |
| 250 |
* serdev_device_wait_until_sent() to make sure the controller write buffer |
| 251 |
* has actually been emptied. |
| 252 |
* |
| 253 |
* Note that this function depends on serdev_device_write_wakeup() being |
| 254 |
* called in the serdev driver write_wakeup() callback. |
| 255 |
* |
| 256 |
* Return: The number of bytes written (less than count if interrupted), |
| 257 |
* -ETIMEDOUT or -ERESTARTSYS if interrupted before any bytes were written, or |
| 258 |
* a negative errno on errors. |
| 259 |
*/ |
| 260 |
ssize_t serdev_device_write(struct serdev_device *serdev, const u8 *buf, |
| 261 |
size_t count, long timeout) |
| 262 |
{ |
| 263 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 264 |
size_t written = 0; |
| 265 |
ssize_t ret; |
| 266 |
|
| 267 |
if (!ctrl || !ctrl->ops->write_buf || !serdev->ops->write_wakeup) |
| 268 |
return -EINVAL; |
| 269 |
|
| 270 |
if (timeout == 0) |
| 271 |
timeout = MAX_SCHEDULE_TIMEOUT; |
| 272 |
|
| 273 |
mutex_lock(&serdev->write_lock); |
| 274 |
do { |
| 275 |
reinit_completion(&serdev->write_comp); |
| 276 |
|
| 277 |
ret = ctrl->ops->write_buf(ctrl, buf, count); |
| 278 |
if (ret < 0) |
| 279 |
break; |
| 280 |
|
| 281 |
written += ret; |
| 282 |
buf += ret; |
| 283 |
count -= ret; |
| 284 |
|
| 285 |
if (count == 0) |
| 286 |
break; |
| 287 |
|
| 288 |
timeout = wait_for_completion_interruptible_timeout(&serdev->write_comp, |
| 289 |
timeout); |
| 290 |
} while (timeout > 0); |
| 291 |
mutex_unlock(&serdev->write_lock); |
| 292 |
|
| 293 |
if (ret < 0) |
| 294 |
return ret; |
| 295 |
|
| 296 |
if (timeout <= 0 && written == 0) { |
| 297 |
if (timeout == -ERESTARTSYS) |
| 298 |
return -ERESTARTSYS; |
| 299 |
else |
| 300 |
return -ETIMEDOUT; |
| 301 |
} |
| 302 |
|
| 303 |
return written; |
| 304 |
} |
| 305 |
EXPORT_SYMBOL_GPL(serdev_device_write); |
| 306 |
|
| 307 |
void serdev_device_write_flush(struct serdev_device *serdev) |
| 308 |
{ |
| 309 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 310 |
|
| 311 |
if (!ctrl || !ctrl->ops->write_flush) |
| 312 |
return; |
| 313 |
|
| 314 |
ctrl->ops->write_flush(ctrl); |
| 315 |
} |
| 316 |
EXPORT_SYMBOL_GPL(serdev_device_write_flush); |
| 317 |
|
| 318 |
unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed) |
| 319 |
{ |
| 320 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 321 |
|
| 322 |
if (!ctrl || !ctrl->ops->set_baudrate) |
| 323 |
return 0; |
| 324 |
|
| 325 |
return ctrl->ops->set_baudrate(ctrl, speed); |
| 326 |
|
| 327 |
} |
| 328 |
EXPORT_SYMBOL_GPL(serdev_device_set_baudrate); |
| 329 |
|
| 330 |
void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable) |
| 331 |
{ |
| 332 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 333 |
|
| 334 |
if (!ctrl || !ctrl->ops->set_flow_control) |
| 335 |
return; |
| 336 |
|
| 337 |
ctrl->ops->set_flow_control(ctrl, enable); |
| 338 |
} |
| 339 |
EXPORT_SYMBOL_GPL(serdev_device_set_flow_control); |
| 340 |
|
| 341 |
int serdev_device_set_parity(struct serdev_device *serdev, |
| 342 |
enum serdev_parity parity) |
| 343 |
{ |
| 344 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 345 |
|
| 346 |
if (!ctrl || !ctrl->ops->set_parity) |
| 347 |
return -EOPNOTSUPP; |
| 348 |
|
| 349 |
return ctrl->ops->set_parity(ctrl, parity); |
| 350 |
} |
| 351 |
EXPORT_SYMBOL_GPL(serdev_device_set_parity); |
| 352 |
|
| 353 |
void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout) |
| 354 |
{ |
| 355 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 356 |
|
| 357 |
if (!ctrl || !ctrl->ops->wait_until_sent) |
| 358 |
return; |
| 359 |
|
| 360 |
ctrl->ops->wait_until_sent(ctrl, timeout); |
| 361 |
} |
| 362 |
EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent); |
| 363 |
|
| 364 |
int serdev_device_get_tiocm(struct serdev_device *serdev) |
| 365 |
{ |
| 366 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 367 |
|
| 368 |
if (!ctrl || !ctrl->ops->get_tiocm) |
| 369 |
return -EOPNOTSUPP; |
| 370 |
|
| 371 |
return ctrl->ops->get_tiocm(ctrl); |
| 372 |
} |
| 373 |
EXPORT_SYMBOL_GPL(serdev_device_get_tiocm); |
| 374 |
|
| 375 |
int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) |
| 376 |
{ |
| 377 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 378 |
|
| 379 |
if (!ctrl || !ctrl->ops->set_tiocm) |
| 380 |
return -EOPNOTSUPP; |
| 381 |
|
| 382 |
return ctrl->ops->set_tiocm(ctrl, set, clear); |
| 383 |
} |
| 384 |
EXPORT_SYMBOL_GPL(serdev_device_set_tiocm); |
| 385 |
|
| 386 |
int serdev_device_break_ctl(struct serdev_device *serdev, int break_state) |
| 387 |
{ |
| 388 |
struct serdev_controller *ctrl = serdev->ctrl; |
| 389 |
|
| 390 |
if (!ctrl || !ctrl->ops->break_ctl) |
| 391 |
return -EOPNOTSUPP; |
| 392 |
|
| 393 |
return ctrl->ops->break_ctl(ctrl, break_state); |
| 394 |
} |
| 395 |
EXPORT_SYMBOL_GPL(serdev_device_break_ctl); |
| 396 |
|
| 397 |
static int serdev_drv_probe(struct device *dev) |
| 398 |
{ |
| 399 |
const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); |
| 400 |
int ret; |
| 401 |
|
| 402 |
ret = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON | |
| 403 |
PD_FLAG_DETACH_POWER_OFF); |
| 404 |
if (ret) |
| 405 |
return ret; |
| 406 |
|
| 407 |
return sdrv->probe(to_serdev_device(dev)); |
| 408 |
} |
| 409 |
|
| 410 |
static void serdev_drv_remove(struct device *dev) |
| 411 |
{ |
| 412 |
const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); |
| 413 |
if (sdrv->remove) |
| 414 |
sdrv->remove(to_serdev_device(dev)); |
| 415 |
} |
| 416 |
|
| 417 |
static void serdev_drv_shutdown(struct device *dev) |
| 418 |
{ |
| 419 |
const struct serdev_device_driver *sdrv = |
| 420 |
to_serdev_device_driver(dev->driver); |
| 421 |
|
| 422 |
if (dev->driver && sdrv->shutdown) |
| 423 |
sdrv->shutdown(to_serdev_device(dev)); |
| 424 |
} |
| 425 |
|
| 426 |
static const struct bus_type serdev_bus_type = { |
| 427 |
.name = "serial", |
| 428 |
.match = serdev_device_match, |
| 429 |
.probe = serdev_drv_probe, |
| 430 |
.remove = serdev_drv_remove, |
| 431 |
.shutdown = serdev_drv_shutdown, |
| 432 |
}; |
| 433 |
|
| 434 |
/** |
| 435 |
* serdev_device_alloc() - Allocate a new serdev device |
| 436 |
* @ctrl: associated controller |
| 437 |
* |
| 438 |
* Caller is responsible for either calling serdev_device_add() to add the |
| 439 |
* newly allocated controller, or calling serdev_device_put() to discard it. |
| 440 |
*/ |
| 441 |
struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl) |
| 442 |
{ |
| 443 |
struct serdev_device *serdev; |
| 444 |
|
| 445 |
serdev = kzalloc_obj(*serdev); |
| 446 |
if (!serdev) |
| 447 |
return NULL; |
| 448 |
|
| 449 |
serdev->ctrl = ctrl; |
| 450 |
device_initialize(&serdev->dev); |
| 451 |
serdev->dev.parent = &ctrl->dev; |
| 452 |
serdev->dev.bus = &serdev_bus_type; |
| 453 |
serdev->dev.type = &serdev_device_type; |
| 454 |
init_completion(&serdev->write_comp); |
| 455 |
mutex_init(&serdev->write_lock); |
| 456 |
return serdev; |
| 457 |
} |
| 458 |
EXPORT_SYMBOL_GPL(serdev_device_alloc); |
| 459 |
|
| 460 |
/** |
| 461 |
* serdev_controller_alloc() - Allocate a new serdev controller |
| 462 |
* @host: serial port hardware controller device |
| 463 |
* @parent: parent device |
| 464 |
* @size: size of private data |
| 465 |
* |
| 466 |
* Caller is responsible for either calling serdev_controller_add() to add the |
| 467 |
* newly allocated controller, or calling serdev_controller_put() to discard it. |
| 468 |
* The allocated private data region may be accessed via |
| 469 |
* serdev_controller_get_drvdata() |
| 470 |
*/ |
| 471 |
struct serdev_controller *serdev_controller_alloc(struct device *host, |
| 472 |
struct device *parent, |
| 473 |
size_t size) |
| 474 |
{ |
| 475 |
struct serdev_controller *ctrl; |
| 476 |
int id; |
| 477 |
|
| 478 |
if (WARN_ON(!parent)) |
| 479 |
return NULL; |
| 480 |
|
| 481 |
ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL); |
| 482 |
if (!ctrl) |
| 483 |
return NULL; |
| 484 |
|
| 485 |
id = ida_alloc(&ctrl_ida, GFP_KERNEL); |
| 486 |
if (id < 0) { |
| 487 |
dev_err(parent, |
| 488 |
"unable to allocate serdev controller identifier.\n"); |
| 489 |
goto err_free; |
| 490 |
} |
| 491 |
|
| 492 |
ctrl->nr = id; |
| 493 |
|
| 494 |
device_initialize(&ctrl->dev); |
| 495 |
ctrl->dev.type = &serdev_ctrl_type; |
| 496 |
ctrl->dev.bus = &serdev_bus_type; |
| 497 |
ctrl->dev.parent = parent; |
| 498 |
ctrl->host = host; |
| 499 |
device_set_node(&ctrl->dev, dev_fwnode(host)); |
| 500 |
serdev_controller_set_drvdata(ctrl, &ctrl[1]); |
| 501 |
|
| 502 |
dev_set_name(&ctrl->dev, "serial%d", id); |
| 503 |
|
| 504 |
pm_runtime_no_callbacks(&ctrl->dev); |
| 505 |
pm_suspend_ignore_children(&ctrl->dev, true); |
| 506 |
|
| 507 |
dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id); |
| 508 |
return ctrl; |
| 509 |
|
| 510 |
err_free: |
| 511 |
kfree(ctrl); |
| 512 |
|
| 513 |
return NULL; |
| 514 |
} |
| 515 |
EXPORT_SYMBOL_GPL(serdev_controller_alloc); |
| 516 |
|
| 517 |
static int of_serdev_register_devices(struct serdev_controller *ctrl) |
| 518 |
{ |
| 519 |
struct device_node *node; |
| 520 |
struct serdev_device *serdev = NULL; |
| 521 |
int err; |
| 522 |
bool found = false; |
| 523 |
|
| 524 |
for_each_available_child_of_node(ctrl->dev.of_node, node) { |
| 525 |
if (!of_property_present(node, "compatible")) |
| 526 |
continue; |
| 527 |
|
| 528 |
dev_dbg(&ctrl->dev, "adding child %pOF\n", node); |
| 529 |
|
| 530 |
serdev = serdev_device_alloc(ctrl); |
| 531 |
if (!serdev) |
| 532 |
continue; |
| 533 |
|
| 534 |
device_set_node(&serdev->dev, of_fwnode_handle(node)); |
| 535 |
|
| 536 |
err = serdev_device_add(serdev); |
| 537 |
if (err) { |
| 538 |
dev_err(&serdev->dev, |
| 539 |
"failure adding device. status %pe\n", |
| 540 |
ERR_PTR(err)); |
| 541 |
serdev_device_put(serdev); |
| 542 |
} else |
| 543 |
found = true; |
| 544 |
} |
| 545 |
if (!found) |
| 546 |
return -ENODEV; |
| 547 |
|
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
#ifdef CONFIG_ACPI |
| 552 |
|
| 553 |
#define SERDEV_ACPI_MAX_SCAN_DEPTH 32 |
| 554 |
|
| 555 |
struct acpi_serdev_lookup { |
| 556 |
acpi_handle device_handle; |
| 557 |
acpi_handle controller_handle; |
| 558 |
int n; |
| 559 |
int index; |
| 560 |
}; |
| 561 |
|
| 562 |
/** |
| 563 |
* serdev_acpi_get_uart_resource - Gets UARTSerialBus resource if type matches |
| 564 |
* @ares: ACPI resource |
| 565 |
* @uart: Pointer to UARTSerialBus resource will be returned here |
| 566 |
* |
| 567 |
* Checks if the given ACPI resource is of type UARTSerialBus. |
| 568 |
* In this case, returns a pointer to it to the caller. |
| 569 |
* |
| 570 |
* Return: True if resource type is of UARTSerialBus, otherwise false. |
| 571 |
*/ |
| 572 |
bool serdev_acpi_get_uart_resource(struct acpi_resource *ares, |
| 573 |
struct acpi_resource_uart_serialbus **uart) |
| 574 |
{ |
| 575 |
struct acpi_resource_uart_serialbus *sb; |
| 576 |
|
| 577 |
if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) |
| 578 |
return false; |
| 579 |
|
| 580 |
sb = &ares->data.uart_serial_bus; |
| 581 |
if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_UART) |
| 582 |
return false; |
| 583 |
|
| 584 |
*uart = sb; |
| 585 |
return true; |
| 586 |
} |
| 587 |
EXPORT_SYMBOL_GPL(serdev_acpi_get_uart_resource); |
| 588 |
|
| 589 |
static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data) |
| 590 |
{ |
| 591 |
struct acpi_serdev_lookup *lookup = data; |
| 592 |
struct acpi_resource_uart_serialbus *sb; |
| 593 |
acpi_status status; |
| 594 |
|
| 595 |
if (!serdev_acpi_get_uart_resource(ares, &sb)) |
| 596 |
return 1; |
| 597 |
|
| 598 |
if (lookup->index != -1 && lookup->n++ != lookup->index) |
| 599 |
return 1; |
| 600 |
|
| 601 |
status = acpi_get_handle(lookup->device_handle, |
| 602 |
sb->resource_source.string_ptr, |
| 603 |
&lookup->controller_handle); |
| 604 |
if (ACPI_FAILURE(status)) |
| 605 |
return 1; |
| 606 |
|
| 607 |
/* |
| 608 |
* NOTE: Ideally, we would also want to retrieve other properties here, |
| 609 |
* once setting them before opening the device is supported by serdev. |
| 610 |
*/ |
| 611 |
|
| 612 |
return 1; |
| 613 |
} |
| 614 |
|
| 615 |
static int acpi_serdev_do_lookup(struct acpi_device *adev, |
| 616 |
struct acpi_serdev_lookup *lookup) |
| 617 |
{ |
| 618 |
struct list_head resource_list; |
| 619 |
int ret; |
| 620 |
|
| 621 |
lookup->device_handle = acpi_device_handle(adev); |
| 622 |
lookup->controller_handle = NULL; |
| 623 |
lookup->n = 0; |
| 624 |
|
| 625 |
INIT_LIST_HEAD(&resource_list); |
| 626 |
ret = acpi_dev_get_resources(adev, &resource_list, |
| 627 |
acpi_serdev_parse_resource, lookup); |
| 628 |
acpi_dev_free_resource_list(&resource_list); |
| 629 |
|
| 630 |
if (ret < 0) |
| 631 |
return -EINVAL; |
| 632 |
|
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
static int acpi_serdev_check_resources(struct serdev_controller *ctrl, |
| 637 |
struct acpi_device *adev) |
| 638 |
{ |
| 639 |
struct acpi_serdev_lookup lookup; |
| 640 |
int ret; |
| 641 |
|
| 642 |
if (acpi_bus_get_status(adev) || !adev->status.present) |
| 643 |
return -EINVAL; |
| 644 |
|
| 645 |
/* Look for UARTSerialBusV2 resource */ |
| 646 |
lookup.index = -1; // we only care for the last device |
| 647 |
|
| 648 |
ret = acpi_serdev_do_lookup(adev, &lookup); |
| 649 |
if (ret) |
| 650 |
return ret; |
| 651 |
|
| 652 |
/* |
| 653 |
* Apple machines provide an empty resource template, so on those |
| 654 |
* machines just look for immediate children with a "baud" property |
| 655 |
* (from the _DSM method) instead. |
| 656 |
*/ |
| 657 |
if (!lookup.controller_handle && x86_apple_machine && |
| 658 |
!acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, NULL)) |
| 659 |
acpi_get_parent(adev->handle, &lookup.controller_handle); |
| 660 |
|
| 661 |
/* Make sure controller and ResourceSource handle match */ |
| 662 |
if (!device_match_acpi_handle(ctrl->host, lookup.controller_handle)) |
| 663 |
return -ENODEV; |
| 664 |
|
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl, |
| 669 |
struct acpi_device *adev) |
| 670 |
{ |
| 671 |
struct serdev_device *serdev; |
| 672 |
int err; |
| 673 |
|
| 674 |
serdev = serdev_device_alloc(ctrl); |
| 675 |
if (!serdev) { |
| 676 |
dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n", |
| 677 |
dev_name(&adev->dev)); |
| 678 |
return AE_NO_MEMORY; |
| 679 |
} |
| 680 |
|
| 681 |
ACPI_COMPANION_SET(&serdev->dev, adev); |
| 682 |
acpi_device_set_enumerated(adev); |
| 683 |
|
| 684 |
err = serdev_device_add(serdev); |
| 685 |
if (err) { |
| 686 |
dev_err(&serdev->dev, |
| 687 |
"failure adding ACPI serdev device. status %pe\n", |
| 688 |
ERR_PTR(err)); |
| 689 |
serdev_device_put(serdev); |
| 690 |
} |
| 691 |
|
| 692 |
return AE_OK; |
| 693 |
} |
| 694 |
|
| 695 |
static const struct acpi_device_id serdev_acpi_devices_blacklist[] = { |
| 696 |
{ "INT3511", 0 }, |
| 697 |
{ "INT3512", 0 }, |
| 698 |
{ }, |
| 699 |
}; |
| 700 |
|
| 701 |
static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level, |
| 702 |
void *data, void **return_value) |
| 703 |
{ |
| 704 |
struct acpi_device *adev = acpi_fetch_acpi_dev(handle); |
| 705 |
struct serdev_controller *ctrl = data; |
| 706 |
|
| 707 |
if (!adev || acpi_device_enumerated(adev)) |
| 708 |
return AE_OK; |
| 709 |
|
| 710 |
/* Skip if black listed */ |
| 711 |
if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist)) |
| 712 |
return AE_OK; |
| 713 |
|
| 714 |
if (acpi_serdev_check_resources(ctrl, adev)) |
| 715 |
return AE_OK; |
| 716 |
|
| 717 |
return acpi_serdev_register_device(ctrl, adev); |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
static int acpi_serdev_register_devices(struct serdev_controller *ctrl) |
| 722 |
{ |
| 723 |
acpi_status status; |
| 724 |
bool skip; |
| 725 |
int ret; |
| 726 |
|
| 727 |
if (!has_acpi_companion(ctrl->host)) |
| 728 |
return -ENODEV; |
| 729 |
|
| 730 |
/* |
| 731 |
* Skip registration on boards where the ACPI tables are known to |
| 732 |
* contain buggy devices. Note serdev_controller_add() must still |
| 733 |
* succeed in this case, so that the proper serdev devices can be |
| 734 |
* added "manually" later. |
| 735 |
*/ |
| 736 |
ret = acpi_quirk_skip_serdev_enumeration(ctrl->host, &skip); |
| 737 |
if (ret) |
| 738 |
return ret; |
| 739 |
if (skip) |
| 740 |
return 0; |
| 741 |
|
| 742 |
status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
| 743 |
SERDEV_ACPI_MAX_SCAN_DEPTH, |
| 744 |
acpi_serdev_add_device, NULL, ctrl, NULL); |
| 745 |
if (ACPI_FAILURE(status)) |
| 746 |
dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n"); |
| 747 |
|
| 748 |
if (!ctrl->serdev) |
| 749 |
return -ENODEV; |
| 750 |
|
| 751 |
return 0; |
| 752 |
} |
| 753 |
#else |
| 754 |
static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl) |
| 755 |
{ |
| 756 |
return -ENODEV; |
| 757 |
} |
| 758 |
#endif /* CONFIG_ACPI */ |
| 759 |
|
| 760 |
/** |
| 761 |
* serdev_controller_add() - Add an serdev controller |
| 762 |
* @ctrl: controller to be registered. |
| 763 |
* |
| 764 |
* Register a controller previously allocated via serdev_controller_alloc() with |
| 765 |
* the serdev core. |
| 766 |
*/ |
| 767 |
int serdev_controller_add(struct serdev_controller *ctrl) |
| 768 |
{ |
| 769 |
int ret_of, ret_acpi, ret; |
| 770 |
|
| 771 |
/* Can't register until after driver model init */ |
| 772 |
if (WARN_ON(!is_registered)) |
| 773 |
return -EAGAIN; |
| 774 |
|
| 775 |
ret = device_add(&ctrl->dev); |
| 776 |
if (ret) |
| 777 |
return ret; |
| 778 |
|
| 779 |
pm_runtime_enable(&ctrl->dev); |
| 780 |
|
| 781 |
ret_of = of_serdev_register_devices(ctrl); |
| 782 |
ret_acpi = acpi_serdev_register_devices(ctrl); |
| 783 |
if (ret_of && ret_acpi) { |
| 784 |
dev_dbg(&ctrl->dev, "no devices registered: of:%pe acpi:%pe\n", |
| 785 |
ERR_PTR(ret_of), ERR_PTR(ret_acpi)); |
| 786 |
ret = -ENODEV; |
| 787 |
goto err_rpm_disable; |
| 788 |
} |
| 789 |
|
| 790 |
dev_dbg(&ctrl->dev, "serdev controller registered: dev:%p\n", &ctrl->dev); |
| 791 |
return 0; |
| 792 |
|
| 793 |
err_rpm_disable: |
| 794 |
pm_runtime_disable(&ctrl->dev); |
| 795 |
device_del(&ctrl->dev); |
| 796 |
return ret; |
| 797 |
}; |
| 798 |
EXPORT_SYMBOL_GPL(serdev_controller_add); |
| 799 |
|
| 800 |
/* Remove a device associated with a controller */ |
| 801 |
static int serdev_remove_device(struct device *dev, void *data) |
| 802 |
{ |
| 803 |
struct serdev_device *serdev = to_serdev_device(dev); |
| 804 |
if (dev->type == &serdev_device_type) |
| 805 |
serdev_device_remove(serdev); |
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
/** |
| 810 |
* serdev_controller_remove(): remove an serdev controller |
| 811 |
* @ctrl: controller to remove |
| 812 |
* |
| 813 |
* Remove a serdev controller. Caller is responsible for calling |
| 814 |
* serdev_controller_put() to discard the allocated controller. |
| 815 |
*/ |
| 816 |
void serdev_controller_remove(struct serdev_controller *ctrl) |
| 817 |
{ |
| 818 |
if (!ctrl) |
| 819 |
return; |
| 820 |
|
| 821 |
device_for_each_child(&ctrl->dev, NULL, serdev_remove_device); |
| 822 |
pm_runtime_disable(&ctrl->dev); |
| 823 |
device_del(&ctrl->dev); |
| 824 |
} |
| 825 |
EXPORT_SYMBOL_GPL(serdev_controller_remove); |
| 826 |
|
| 827 |
static void serdev_legacy_shutdown(struct serdev_device *serdev) |
| 828 |
{ |
| 829 |
struct device *dev = &serdev->dev; |
| 830 |
struct device_driver *driver = dev->driver; |
| 831 |
|
| 832 |
driver->shutdown(dev); |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* __serdev_device_driver_register() - Register client driver with serdev core |
| 837 |
* @sdrv: client driver to be associated with client-device. |
| 838 |
* @owner: client driver owner to set. |
| 839 |
* |
| 840 |
* This API will register the client driver with the serdev framework. |
| 841 |
* It is typically called from the driver's module-init function. |
| 842 |
*/ |
| 843 |
int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner) |
| 844 |
{ |
| 845 |
sdrv->driver.bus = &serdev_bus_type; |
| 846 |
sdrv->driver.owner = owner; |
| 847 |
|
| 848 |
/* force drivers to async probe so I/O is possible in probe */ |
| 849 |
sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; |
| 850 |
|
| 851 |
if (!sdrv->shutdown && sdrv->driver.shutdown) |
| 852 |
sdrv->shutdown = serdev_legacy_shutdown; |
| 853 |
|
| 854 |
return driver_register(&sdrv->driver); |
| 855 |
} |
| 856 |
EXPORT_SYMBOL_GPL(__serdev_device_driver_register); |
| 857 |
|
| 858 |
static void __exit serdev_exit(void) |
| 859 |
{ |
| 860 |
bus_unregister(&serdev_bus_type); |
| 861 |
ida_destroy(&ctrl_ida); |
| 862 |
} |
| 863 |
module_exit(serdev_exit); |
| 864 |
|
| 865 |
static int __init serdev_init(void) |
| 866 |
{ |
| 867 |
int ret; |
| 868 |
|
| 869 |
ret = bus_register(&serdev_bus_type); |
| 870 |
if (ret) |
| 871 |
return ret; |
| 872 |
|
| 873 |
is_registered = true; |
| 874 |
return 0; |
| 875 |
} |
| 876 |
/* Must be before serial drivers register */ |
| 877 |
postcore_initcall(serdev_init); |
| 878 |
|
| 879 |
MODULE_AUTHOR("Rob Herring <[email protected]>"); |
| 880 |
MODULE_LICENSE("GPL v2"); |
| 881 |
MODULE_DESCRIPTION("Serial attached device bus"); |