core.c 20.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8
/*
 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
 *
 * Based on drivers/spmi/spmi.c:
 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
 */

F
Frédéric Danis 已提交
9
#include <linux/acpi.h>
10 11 12 13 14 15
#include <linux/errno.h>
#include <linux/idr.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
16
#include <linux/pm_domain.h>
17
#include <linux/pm_runtime.h>
18
#include <linux/sched.h>
19 20
#include <linux/serdev.h>
#include <linux/slab.h>
21
#include <linux/platform_data/x86/apple.h>
22 23 24 25

static bool is_registered;
static DEFINE_IDA(ctrl_ida);

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static ssize_t modalias_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	int len;

	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
	if (len != -ENODEV)
		return len;

	return of_device_modalias(dev, buf, PAGE_SIZE);
}
static DEVICE_ATTR_RO(modalias);

static struct attribute *serdev_device_attrs[] = {
	&dev_attr_modalias.attr,
	NULL,
};
ATTRIBUTE_GROUPS(serdev_device);

static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
{
	int rc;

	/* TODO: platform modalias */

	rc = acpi_device_uevent_modalias(dev, env);
	if (rc != -ENODEV)
		return rc;

	return of_device_uevent_modalias(dev, env);
}

58 59 60 61 62 63 64
static void serdev_device_release(struct device *dev)
{
	struct serdev_device *serdev = to_serdev_device(dev);
	kfree(serdev);
}

static const struct device_type serdev_device_type = {
65 66
	.groups		= serdev_device_groups,
	.uevent		= serdev_device_uevent,
67 68 69
	.release	= serdev_device_release,
};

J
Johan Hovold 已提交
70 71 72 73 74
static bool is_serdev_device(const struct device *dev)
{
	return dev->type == &serdev_device_type;
}

75 76 77 78 79 80 81 82 83 84 85 86 87
static void serdev_ctrl_release(struct device *dev)
{
	struct serdev_controller *ctrl = to_serdev_controller(dev);
	ida_simple_remove(&ctrl_ida, ctrl->nr);
	kfree(ctrl);
}

static const struct device_type serdev_ctrl_type = {
	.release	= serdev_ctrl_release,
};

static int serdev_device_match(struct device *dev, struct device_driver *drv)
{
J
Johan Hovold 已提交
88 89 90
	if (!is_serdev_device(dev))
		return 0;

F
Frédéric Danis 已提交
91 92 93 94
	/* TODO: platform matching */
	if (acpi_driver_match_device(dev, drv))
		return 1;

95 96 97 98 99 100 101 102 103
	return of_driver_match_device(dev, drv);
}

/**
 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
 * @serdev:	serdev_device to be added
 */
int serdev_device_add(struct serdev_device *serdev)
{
104
	struct serdev_controller *ctrl = serdev->ctrl;
105 106 107 108 109
	struct device *parent = serdev->dev.parent;
	int err;

	dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);

110 111 112 113 114 115 116
	/* Only a single slave device is currently supported. */
	if (ctrl->serdev) {
		dev_err(&serdev->dev, "controller busy\n");
		return -EBUSY;
	}
	ctrl->serdev = serdev;

117 118
	err = device_add(&serdev->dev);
	if (err < 0) {
119 120
		dev_err(&serdev->dev, "Can't add %s, status %pe\n",
			dev_name(&serdev->dev), ERR_PTR(err));
121
		goto err_clear_serdev;
122 123 124 125
	}

	dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));

126 127 128 129
	return 0;

err_clear_serdev:
	ctrl->serdev = NULL;
130 131 132 133 134 135 136 137 138 139
	return err;
}
EXPORT_SYMBOL_GPL(serdev_device_add);

/**
 * serdev_device_remove(): remove an serdev device
 * @serdev:	serdev_device to be removed
 */
void serdev_device_remove(struct serdev_device *serdev)
{
140 141
	struct serdev_controller *ctrl = serdev->ctrl;

142
	device_unregister(&serdev->dev);
143
	ctrl->serdev = NULL;
144 145 146 147 148 149
}
EXPORT_SYMBOL_GPL(serdev_device_remove);

int serdev_device_open(struct serdev_device *serdev)
{
	struct serdev_controller *ctrl = serdev->ctrl;
150
	int ret;
151 152 153 154

	if (!ctrl || !ctrl->ops->open)
		return -EINVAL;

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	ret = ctrl->ops->open(ctrl);
	if (ret)
		return ret;

	ret = pm_runtime_get_sync(&ctrl->dev);
	if (ret < 0) {
		pm_runtime_put_noidle(&ctrl->dev);
		goto err_close;
	}

	return 0;

err_close:
	if (ctrl->ops->close)
		ctrl->ops->close(ctrl);

	return ret;
172 173 174 175 176 177 178 179 180 181
}
EXPORT_SYMBOL_GPL(serdev_device_open);

void serdev_device_close(struct serdev_device *serdev)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->close)
		return;

182 183
	pm_runtime_put(&ctrl->dev);

184 185 186 187
	ctrl->ops->close(ctrl);
}
EXPORT_SYMBOL_GPL(serdev_device_close);

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
static void devm_serdev_device_release(struct device *dev, void *dr)
{
	serdev_device_close(*(struct serdev_device **)dr);
}

int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
{
	struct serdev_device **dr;
	int ret;

	dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
	if (!dr)
		return -ENOMEM;

	ret = serdev_device_open(serdev);
	if (ret) {
		devres_free(dr);
		return ret;
	}

	*dr = serdev;
	devres_add(dev, dr);

	return 0;
}
EXPORT_SYMBOL_GPL(devm_serdev_device_open);

215 216 217 218 219 220
void serdev_device_write_wakeup(struct serdev_device *serdev)
{
	complete(&serdev->write_comp);
}
EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/**
 * serdev_device_write_buf() - write data asynchronously
 * @serdev:	serdev device
 * @buf:	data to be written
 * @count:	number of bytes to write
 *
 * Write data to the device asynchronously.
 *
 * Note that any accepted data has only been buffered by the controller; use
 * serdev_device_wait_until_sent() to make sure the controller write buffer
 * has actually been emptied.
 *
 * Return: The number of bytes written (less than count if not enough room in
 * the write buffer), or a negative errno on errors.
 */
236 237 238 239 240 241 242 243 244 245 246 247
int serdev_device_write_buf(struct serdev_device *serdev,
			    const unsigned char *buf, size_t count)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->write_buf)
		return -EINVAL;

	return ctrl->ops->write_buf(ctrl, buf, count);
}
EXPORT_SYMBOL_GPL(serdev_device_write_buf);

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/**
 * serdev_device_write() - write data synchronously
 * @serdev:	serdev device
 * @buf:	data to be written
 * @count:	number of bytes to write
 * @timeout:	timeout in jiffies, or 0 to wait indefinitely
 *
 * Write data to the device synchronously by repeatedly calling
 * serdev_device_write() until the controller has accepted all data (unless
 * interrupted by a timeout or a signal).
 *
 * Note that any accepted data has only been buffered by the controller; use
 * serdev_device_wait_until_sent() to make sure the controller write buffer
 * has actually been emptied.
 *
 * Note that this function depends on serdev_device_write_wakeup() being
 * called in the serdev driver write_wakeup() callback.
 *
 * Return: The number of bytes written (less than count if interrupted),
 * -ETIMEDOUT or -ERESTARTSYS if interrupted before any bytes were written, or
 * a negative errno on errors.
 */
270 271
int serdev_device_write(struct serdev_device *serdev,
			const unsigned char *buf, size_t count,
272
			long timeout)
273 274
{
	struct serdev_controller *ctrl = serdev->ctrl;
275
	int written = 0;
276
	int ret;
277

278
	if (!ctrl || !ctrl->ops->write_buf || !serdev->ops->write_wakeup)
279 280
		return -EINVAL;

281 282 283
	if (timeout == 0)
		timeout = MAX_SCHEDULE_TIMEOUT;

284 285 286 287 288 289 290 291
	mutex_lock(&serdev->write_lock);
	do {
		reinit_completion(&serdev->write_comp);

		ret = ctrl->ops->write_buf(ctrl, buf, count);
		if (ret < 0)
			break;

292
		written += ret;
293 294
		buf += ret;
		count -= ret;
295 296 297 298 299 300 301

		if (count == 0)
			break;

		timeout = wait_for_completion_interruptible_timeout(&serdev->write_comp,
								    timeout);
	} while (timeout > 0);
302
	mutex_unlock(&serdev->write_lock);
303 304 305 306

	if (ret < 0)
		return ret;

307 308 309 310 311 312
	if (timeout <= 0 && written == 0) {
		if (timeout == -ERESTARTSYS)
			return -ERESTARTSYS;
		else
			return -ETIMEDOUT;
	}
313 314

	return written;
315
}
316
EXPORT_SYMBOL_GPL(serdev_device_write);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

void serdev_device_write_flush(struct serdev_device *serdev)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->write_flush)
		return;

	ctrl->ops->write_flush(ctrl);
}
EXPORT_SYMBOL_GPL(serdev_device_write_flush);

int serdev_device_write_room(struct serdev_device *serdev)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->write_room)
		return 0;

	return serdev->ctrl->ops->write_room(ctrl);
}
EXPORT_SYMBOL_GPL(serdev_device_write_room);

unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->set_baudrate)
		return 0;

	return ctrl->ops->set_baudrate(ctrl, speed);

}
EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);

void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->set_flow_control)
		return;

	ctrl->ops->set_flow_control(ctrl, enable);
}
EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);

U
Ulrich Hecht 已提交
363 364 365 366 367 368 369 370 371 372 373 374
int serdev_device_set_parity(struct serdev_device *serdev,
			     enum serdev_parity parity)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->set_parity)
		return -ENOTSUPP;

	return ctrl->ops->set_parity(ctrl, parity);
}
EXPORT_SYMBOL_GPL(serdev_device_set_parity);

375 376 377 378 379 380 381 382 383 384 385
void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->wait_until_sent)
		return;

	ctrl->ops->wait_until_sent(ctrl, timeout);
}
EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
int serdev_device_get_tiocm(struct serdev_device *serdev)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->get_tiocm)
		return -ENOTSUPP;

	return ctrl->ops->get_tiocm(ctrl);
}
EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);

int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
{
	struct serdev_controller *ctrl = serdev->ctrl;

	if (!ctrl || !ctrl->ops->set_tiocm)
		return -ENOTSUPP;

	return ctrl->ops->set_tiocm(ctrl, set, clear);
}
EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);

408 409 410
static int serdev_drv_probe(struct device *dev)
{
	const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
411
	int ret;
412

413 414 415 416 417 418 419 420 421
	ret = dev_pm_domain_attach(dev, true);
	if (ret)
		return ret;

	ret = sdrv->probe(to_serdev_device(dev));
	if (ret)
		dev_pm_domain_detach(dev, true);

	return ret;
422 423
}

424
static void serdev_drv_remove(struct device *dev)
425 426
{
	const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
427 428
	if (sdrv->remove)
		sdrv->remove(to_serdev_device(dev));
429 430

	dev_pm_domain_detach(dev, true);
431 432 433 434 435 436 437 438 439 440
}

static struct bus_type serdev_bus_type = {
	.name		= "serial",
	.match		= serdev_device_match,
	.probe		= serdev_drv_probe,
	.remove		= serdev_drv_remove,
};

/**
441
 * serdev_device_alloc() - Allocate a new serdev device
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
 * @ctrl:	associated controller
 *
 * Caller is responsible for either calling serdev_device_add() to add the
 * newly allocated controller, or calling serdev_device_put() to discard it.
 */
struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
{
	struct serdev_device *serdev;

	serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
	if (!serdev)
		return NULL;

	serdev->ctrl = ctrl;
	device_initialize(&serdev->dev);
	serdev->dev.parent = &ctrl->dev;
	serdev->dev.bus = &serdev_bus_type;
	serdev->dev.type = &serdev_device_type;
460 461
	init_completion(&serdev->write_comp);
	mutex_init(&serdev->write_lock);
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	return serdev;
}
EXPORT_SYMBOL_GPL(serdev_device_alloc);

/**
 * serdev_controller_alloc() - Allocate a new serdev controller
 * @parent:	parent device
 * @size:	size of private data
 *
 * Caller is responsible for either calling serdev_controller_add() to add the
 * newly allocated controller, or calling serdev_controller_put() to discard it.
 * The allocated private data region may be accessed via
 * serdev_controller_get_drvdata()
 */
struct serdev_controller *serdev_controller_alloc(struct device *parent,
					      size_t size)
{
	struct serdev_controller *ctrl;
	int id;

	if (WARN_ON(!parent))
		return NULL;

	ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
	if (!ctrl)
		return NULL;

	id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
	if (id < 0) {
		dev_err(parent,
			"unable to allocate serdev controller identifier.\n");
493
		goto err_free;
494 495 496
	}

	ctrl->nr = id;
497 498 499 500 501 502 503 504

	device_initialize(&ctrl->dev);
	ctrl->dev.type = &serdev_ctrl_type;
	ctrl->dev.bus = &serdev_bus_type;
	ctrl->dev.parent = parent;
	ctrl->dev.of_node = parent->of_node;
	serdev_controller_set_drvdata(ctrl, &ctrl[1]);

505 506
	dev_set_name(&ctrl->dev, "serial%d", id);

507 508 509
	pm_runtime_no_callbacks(&ctrl->dev);
	pm_suspend_ignore_children(&ctrl->dev, true);

510 511
	dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
	return ctrl;
512 513 514 515 516

err_free:
	kfree(ctrl);

	return NULL;
517 518 519 520 521 522 523 524 525 526 527 528 529 530
}
EXPORT_SYMBOL_GPL(serdev_controller_alloc);

static int of_serdev_register_devices(struct serdev_controller *ctrl)
{
	struct device_node *node;
	struct serdev_device *serdev = NULL;
	int err;
	bool found = false;

	for_each_available_child_of_node(ctrl->dev.of_node, node) {
		if (!of_get_property(node, "compatible", NULL))
			continue;

531
		dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
532 533 534 535 536 537 538 539 540 541

		serdev = serdev_device_alloc(ctrl);
		if (!serdev)
			continue;

		serdev->dev.of_node = node;

		err = serdev_device_add(serdev);
		if (err) {
			dev_err(&serdev->dev,
542 543
				"failure adding device. status %pe\n",
				ERR_PTR(err));
544 545 546 547 548 549 550 551 552 553
			serdev_device_put(serdev);
		} else
			found = true;
	}
	if (!found)
		return -ENODEV;

	return 0;
}

F
Frédéric Danis 已提交
554
#ifdef CONFIG_ACPI
555 556 557 558 559 560 561 562 563 564

#define SERDEV_ACPI_MAX_SCAN_DEPTH 32

struct acpi_serdev_lookup {
	acpi_handle device_handle;
	acpi_handle controller_handle;
	int n;
	int index;
};

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
/**
 * serdev_acpi_get_uart_resource - Gets UARTSerialBus resource if type matches
 * @ares:	ACPI resource
 * @uart:	Pointer to UARTSerialBus resource will be returned here
 *
 * Checks if the given ACPI resource is of type UARTSerialBus.
 * In this case, returns a pointer to it to the caller.
 *
 * Return: True if resource type is of UARTSerialBus, otherwise false.
 */
bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
				   struct acpi_resource_uart_serialbus **uart)
{
	struct acpi_resource_uart_serialbus *sb;

	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
		return false;

	sb = &ares->data.uart_serial_bus;
	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_UART)
		return false;

	*uart = sb;
	return true;
}
EXPORT_SYMBOL_GPL(serdev_acpi_get_uart_resource);

592 593 594 595 596 597
static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data)
{
	struct acpi_serdev_lookup *lookup = data;
	struct acpi_resource_uart_serialbus *sb;
	acpi_status status;

598
	if (!serdev_acpi_get_uart_resource(ares, &sb))
599 600 601 602 603 604 605 606 607 608 609 610
		return 1;

	if (lookup->index != -1 && lookup->n++ != lookup->index)
		return 1;

	status = acpi_get_handle(lookup->device_handle,
				 sb->resource_source.string_ptr,
				 &lookup->controller_handle);
	if (ACPI_FAILURE(status))
		return 1;

	/*
611
	 * NOTE: Ideally, we would also want to retrieve other properties here,
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
	 * once setting them before opening the device is supported by serdev.
	 */

	return 1;
}

static int acpi_serdev_do_lookup(struct acpi_device *adev,
                                 struct acpi_serdev_lookup *lookup)
{
	struct list_head resource_list;
	int ret;

	lookup->device_handle = acpi_device_handle(adev);
	lookup->controller_handle = NULL;
	lookup->n = 0;

	INIT_LIST_HEAD(&resource_list);
	ret = acpi_dev_get_resources(adev, &resource_list,
				     acpi_serdev_parse_resource, lookup);
	acpi_dev_free_resource_list(&resource_list);

	if (ret < 0)
		return -EINVAL;

	return 0;
}

static int acpi_serdev_check_resources(struct serdev_controller *ctrl,
				       struct acpi_device *adev)
{
	struct acpi_serdev_lookup lookup;
	int ret;

	if (acpi_bus_get_status(adev) || !adev->status.present)
		return -EINVAL;

	/* Look for UARTSerialBusV2 resource */
	lookup.index = -1;	// we only care for the last device

	ret = acpi_serdev_do_lookup(adev, &lookup);
	if (ret)
		return ret;

655 656 657 658 659 660 661 662 663
	/*
	 * Apple machines provide an empty resource template, so on those
	 * machines just look for immediate children with a "baud" property
	 * (from the _DSM method) instead.
	 */
	if (!lookup.controller_handle && x86_apple_machine &&
	    !acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, NULL))
		acpi_get_parent(adev->handle, &lookup.controller_handle);

664 665 666 667 668 669 670
	/* Make sure controller and ResourceSource handle match */
	if (ACPI_HANDLE(ctrl->dev.parent) != lookup.controller_handle)
		return -ENODEV;

	return 0;
}

F
Frédéric Danis 已提交
671
static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
672
					       struct acpi_device *adev)
F
Frédéric Danis 已提交
673
{
674
	struct serdev_device *serdev;
F
Frédéric Danis 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
	int err;

	serdev = serdev_device_alloc(ctrl);
	if (!serdev) {
		dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
			dev_name(&adev->dev));
		return AE_NO_MEMORY;
	}

	ACPI_COMPANION_SET(&serdev->dev, adev);
	acpi_device_set_enumerated(adev);

	err = serdev_device_add(serdev);
	if (err) {
		dev_err(&serdev->dev,
690 691
			"failure adding ACPI serdev device. status %pe\n",
			ERR_PTR(err));
F
Frédéric Danis 已提交
692 693 694 695 696 697
		serdev_device_put(serdev);
	}

	return AE_OK;
}

698 699 700 701 702 703
static const struct acpi_device_id serdev_acpi_devices_blacklist[] = {
	{ "INT3511", 0 },
	{ "INT3512", 0 },
	{ },
};

F
Frédéric Danis 已提交
704
static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
705
					  void *data, void **return_value)
F
Frédéric Danis 已提交
706 707 708 709 710 711 712
{
	struct serdev_controller *ctrl = data;
	struct acpi_device *adev;

	if (acpi_bus_get_device(handle, &adev))
		return AE_OK;

713 714 715
	if (acpi_device_enumerated(adev))
		return AE_OK;

716 717 718 719
	/* Skip if black listed */
	if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist))
		return AE_OK;

720 721 722
	if (acpi_serdev_check_resources(ctrl, adev))
		return AE_OK;

F
Frédéric Danis 已提交
723 724 725
	return acpi_serdev_register_device(ctrl, adev);
}

726

F
Frédéric Danis 已提交
727 728 729
static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
{
	acpi_status status;
730 731
	bool skip;
	int ret;
F
Frédéric Danis 已提交
732

733
	if (!has_acpi_companion(ctrl->dev.parent))
F
Frédéric Danis 已提交
734 735
		return -ENODEV;

736 737 738 739 740 741 742 743 744 745 746 747
	/*
	 * Skip registration on boards where the ACPI tables are known to
	 * contain buggy devices. Note serdev_controller_add() must still
	 * succeed in this case, so that the proper serdev devices can be
	 * added "manually" later.
	 */
	ret = acpi_quirk_skip_serdev_enumeration(ctrl->dev.parent, &skip);
	if (ret)
		return ret;
	if (skip)
		return 0;

748 749
	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
				     SERDEV_ACPI_MAX_SCAN_DEPTH,
F
Frédéric Danis 已提交
750 751
				     acpi_serdev_add_device, NULL, ctrl, NULL);
	if (ACPI_FAILURE(status))
752
		dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n");
F
Frédéric Danis 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765

	if (!ctrl->serdev)
		return -ENODEV;

	return 0;
}
#else
static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
{
	return -ENODEV;
}
#endif /* CONFIG_ACPI */

766 767 768 769 770 771 772 773 774
/**
 * serdev_controller_add() - Add an serdev controller
 * @ctrl:	controller to be registered.
 *
 * Register a controller previously allocated via serdev_controller_alloc() with
 * the serdev core.
 */
int serdev_controller_add(struct serdev_controller *ctrl)
{
F
Frédéric Danis 已提交
775
	int ret_of, ret_acpi, ret;
776 777 778 779 780 781 782 783 784

	/* Can't register until after driver model init */
	if (WARN_ON(!is_registered))
		return -EAGAIN;

	ret = device_add(&ctrl->dev);
	if (ret)
		return ret;

785 786
	pm_runtime_enable(&ctrl->dev);

F
Frédéric Danis 已提交
787 788 789
	ret_of = of_serdev_register_devices(ctrl);
	ret_acpi = acpi_serdev_register_devices(ctrl);
	if (ret_of && ret_acpi) {
790 791
		dev_dbg(&ctrl->dev, "no devices registered: of:%pe acpi:%pe\n",
			ERR_PTR(ret_of), ERR_PTR(ret_acpi));
F
Frédéric Danis 已提交
792
		ret = -ENODEV;
793
		goto err_rpm_disable;
F
Frédéric Danis 已提交
794
	}
795 796 797 798 799

	dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
		ctrl->nr, &ctrl->dev);
	return 0;

800 801
err_rpm_disable:
	pm_runtime_disable(&ctrl->dev);
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	device_del(&ctrl->dev);
	return ret;
};
EXPORT_SYMBOL_GPL(serdev_controller_add);

/* Remove a device associated with a controller */
static int serdev_remove_device(struct device *dev, void *data)
{
	struct serdev_device *serdev = to_serdev_device(dev);
	if (dev->type == &serdev_device_type)
		serdev_device_remove(serdev);
	return 0;
}

/**
 * serdev_controller_remove(): remove an serdev controller
 * @ctrl:	controller to remove
 *
 * Remove a serdev controller.  Caller is responsible for calling
 * serdev_controller_put() to discard the allocated controller.
 */
void serdev_controller_remove(struct serdev_controller *ctrl)
{
	if (!ctrl)
		return;

828
	device_for_each_child(&ctrl->dev, NULL, serdev_remove_device);
829
	pm_runtime_disable(&ctrl->dev);
830 831 832 833 834
	device_del(&ctrl->dev);
}
EXPORT_SYMBOL_GPL(serdev_controller_remove);

/**
835
 * __serdev_device_driver_register() - Register client driver with serdev core
836
 * @sdrv:	client driver to be associated with client-device.
837
 * @owner:	client driver owner to set.
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
 *
 * This API will register the client driver with the serdev framework.
 * It is typically called from the driver's module-init function.
 */
int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
{
	sdrv->driver.bus = &serdev_bus_type;
	sdrv->driver.owner = owner;

	/* force drivers to async probe so I/O is possible in probe */
        sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;

	return driver_register(&sdrv->driver);
}
EXPORT_SYMBOL_GPL(__serdev_device_driver_register);

static void __exit serdev_exit(void)
{
	bus_unregister(&serdev_bus_type);
857
	ida_destroy(&ctrl_ida);
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
}
module_exit(serdev_exit);

static int __init serdev_init(void)
{
	int ret;

	ret = bus_register(&serdev_bus_type);
	if (ret)
		return ret;

	is_registered = true;
	return 0;
}
/* Must be before serial drivers register */
postcore_initcall(serdev_init);

MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Serial attached device bus");