Skip to content

Commit

Permalink
serial: core: Start managing serial controllers to enable runtime PM
Browse files Browse the repository at this point in the history
We want to enable runtime PM for serial port device drivers in a generic
way. To do this, we want to have the serial core layer manage the
registered physical serial controller devices.

To do this, let's set up a struct bus and struct device for the serial
core controller as suggested by Greg and Jiri. The serial core controller
devices are children of the physical serial port device. The serial core
controller device is needed to support multiple different kind of ports
connected to single physical serial port device.

Let's also set up a struct device for the serial core port. The serial
core port instances are children of the serial core controller device.

We need to also update the documentation a bit as suggested by Andy.

With the serial core port device we can now flush pending TX on the
runtime PM resume as suggested by Johan.

Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Suggested-by: Jiri Slaby <jirislaby@kernel.org>
Suggested-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Tony Lindgren <tony@atomide.com>
  • Loading branch information
tmlind authored and intel-lab-lkp committed Mar 9, 2023
1 parent fe15c26 commit 08f76f2
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 12 deletions.
5 changes: 2 additions & 3 deletions Documentation/firmware-guide/acpi/enumeration.rst
Expand Up @@ -19,9 +19,8 @@ possible we decided to do following:
platform devices.

- Devices behind real busses where there is a connector resource
are represented as struct spi_device or struct i2c_device. Note
that standard UARTs are not busses so there is no struct uart_device,
although some of them may be represented by struct serdev_device.
are represented as struct spi_device, struct i2c_device or
struct serdev_device.

As both ACPI and Device Tree represent a tree of devices (and their
resources) this implementation follows the Device Tree way as much as
Expand Down
1 change: 1 addition & 0 deletions drivers/tty/serial/8250/8250_core.c
Expand Up @@ -996,6 +996,7 @@ int serial8250_register_8250_port(const struct uart_8250_port *up)
if (uart->port.dev)
uart_remove_one_port(&serial8250_reg, &uart->port);

uart->port.ctrl_id = up->port.ctrl_id;
uart->port.iobase = up->port.iobase;
uart->port.membase = up->port.membase;
uart->port.irq = up->port.irq;
Expand Down
2 changes: 1 addition & 1 deletion drivers/tty/serial/Makefile
Expand Up @@ -3,7 +3,7 @@
# Makefile for the kernel serial device drivers.
#

obj-$(CONFIG_SERIAL_CORE) += serial_core.o
obj-$(CONFIG_SERIAL_CORE) += serial_base.o serial_core.o serial_ctrl.o serial_port.o

obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o
obj-$(CONFIG_SERIAL_EARLYCON_SEMIHOST) += earlycon-semihost.o
Expand Down
98 changes: 98 additions & 0 deletions drivers/tty/serial/serial_base.c
@@ -0,0 +1,98 @@
// SPDX-License-Identifier: GPL-2.0-or-later

/*
* Serial core base layer for controllers
*
* The serial core bus manages the serial core controller instances.
*/

#include <linux/device.h>
#include <linux/module.h>
#include <linux/serial_core.h>
#include <linux/slab.h>

#include "serial_base.h"

static int serial_base_match(struct device *dev, struct device_driver *drv)
{
int len = strlen(drv->name);

return (strncmp(dev_name(dev), drv->name, len) == 0);
}

static struct bus_type serial_base_bus_type = {
.name = "serial-base",
.match = serial_base_match,
};

int serial_base_driver_register(struct device_driver *driver)
{
driver->bus = &serial_base_bus_type;

return driver_register(driver);
}
EXPORT_SYMBOL_GPL(serial_base_driver_register);

void serial_base_driver_unregister(struct device_driver *driver)
{
driver_unregister(driver);
}
EXPORT_SYMBOL_GPL(serial_base_driver_unregister);

struct serial_base_device *serial_base_device_add(struct uart_port *port,
const char *name,
struct device *parent_dev)
{
struct serial_base_device *dev;
int ret, id;

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

device_initialize(&dev->dev);
dev->dev.parent = parent_dev;
dev->dev.bus = &serial_base_bus_type;

if (!strncmp(name, "ctrl", 4)) {
id = port->ctrl_id;
} else {
id = port->line;
dev->port = port;
}

dev_set_name(&dev->dev, "%s.%s.%d", name, dev_name(port->dev), id);

ret = device_add(&dev->dev);
if (ret) {
kfree(dev);
return NULL;
}

return dev;
}
EXPORT_SYMBOL_GPL(serial_base_device_add);

void serial_base_device_remove(struct serial_base_device *dev)
{
device_del(&dev->dev);
kfree(dev);
}
EXPORT_SYMBOL_GPL(serial_base_device_remove);

static int serial_base_init(void)
{
return bus_register(&serial_base_bus_type);
}

static void serial_base_exit(void)
{
bus_unregister(&serial_base_bus_type);
}

module_init(serial_base_init);
module_exit(serial_base_exit);

MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
MODULE_DESCRIPTION("Serial core bus");
MODULE_LICENSE("GPL");
27 changes: 27 additions & 0 deletions drivers/tty/serial/serial_base.h
@@ -0,0 +1,27 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */

/* Serial core related functions, serial port device drivers do not need this. */

struct device;
struct uart_driver;
struct uart_port;

struct serial_base_device {
struct device dev;
struct uart_port *port;
};

#define to_serial_base_device(x) container_of((x), struct serial_base_device, dev)

extern int serial_base_driver_register(struct device_driver *driver);
extern void serial_base_driver_unregister(struct device_driver *driver);
extern struct serial_base_device *serial_base_device_add(struct uart_port *port,
const char *name,
struct device *parent_dev);
extern void serial_base_device_remove(struct serial_base_device *dev);

extern int serial_ctrl_register_port(struct uart_driver *drv, struct uart_port *port);
extern void serial_ctrl_unregister_port(struct uart_driver *drv, struct uart_port *port);

extern int serial_core_register_port(struct uart_driver *drv, struct uart_port *port);
extern void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port);

0 comments on commit 08f76f2

Please sign in to comment.