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 manage serial controllers, 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.

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>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20230525113034.46880-1-tony@atomide.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Tony Lindgren authored and Greg Kroah-Hartman committed May 31, 2023
1 parent ae62c49 commit 84a9582
Show file tree
Hide file tree
Showing 9 changed files with 598 additions and 23 deletions.
1 change: 1 addition & 0 deletions drivers/tty/serial/8250/8250_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,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
1 change: 1 addition & 0 deletions drivers/tty/serial/8250/8250_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -3282,6 +3282,7 @@ void serial8250_init_port(struct uart_8250_port *up)
struct uart_port *port = &up->port;

spin_lock_init(&port->lock);
port->ctrl_id = 0;
port->ops = &serial8250_pops;
port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);

Expand Down
3 changes: 2 additions & 1 deletion drivers/tty/serial/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# Makefile for the kernel serial device drivers.
#

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

obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o
obj-$(CONFIG_SERIAL_EARLYCON_SEMIHOST) += earlycon-semihost.o
Expand Down
46 changes: 46 additions & 0 deletions drivers/tty/serial/serial_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Serial core related functions, serial port device drivers do not need this.
*
* Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
* Author: Tony Lindgren <tony@atomide.com>
*/

#define to_serial_base_ctrl_device(d) container_of((d), struct serial_ctrl_device, dev)
#define to_serial_base_port_device(d) container_of((d), struct serial_port_device, dev)

struct uart_driver;
struct uart_port;
struct device_driver;
struct device;

struct serial_ctrl_device {
struct device dev;
};

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

int serial_base_ctrl_init(void);
void serial_base_ctrl_exit(void);

int serial_base_port_init(void);
void serial_base_port_exit(void);

int serial_base_driver_register(struct device_driver *driver);
void serial_base_driver_unregister(struct device_driver *driver);

struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
struct device *parent);
struct serial_port_device *serial_base_port_add(struct uart_port *port,
struct serial_ctrl_device *parent);
void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev);
void serial_base_port_device_remove(struct serial_port_device *port_dev);

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

int serial_core_register_port(struct uart_driver *drv, struct uart_port *port);
void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port);
200 changes: 200 additions & 0 deletions drivers/tty/serial/serial_base_bus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Serial base bus layer for controllers
*
* Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
* Author: Tony Lindgren <tony@atomide.com>
*
* The serial core bus manages the serial core controller instances.
*/

#include <linux/container_of.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/serial_core.h>
#include <linux/slab.h>
#include <linux/spinlock.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);
}

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);
}

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

static int serial_base_device_init(struct uart_port *port,
struct device *dev,
struct device *parent_dev,
const struct device_type *type,
void (*release)(struct device *dev),
int id)
{
device_initialize(dev);
dev->type = type;
dev->parent = parent_dev;
dev->bus = &serial_base_bus_type;
dev->release = release;

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

static const struct device_type serial_ctrl_type = {
.name = "ctrl",
};

static void serial_base_ctrl_release(struct device *dev)
{
struct serial_ctrl_device *ctrl_dev = to_serial_base_ctrl_device(dev);

kfree(ctrl_dev);
}

void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
{
if (!ctrl_dev)
return;

device_del(&ctrl_dev->dev);
}

struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
struct device *parent)
{
struct serial_ctrl_device *ctrl_dev;
int err;

ctrl_dev = kzalloc(sizeof(*ctrl_dev), GFP_KERNEL);
if (!ctrl_dev)
return ERR_PTR(-ENOMEM);

err = serial_base_device_init(port, &ctrl_dev->dev,
parent, &serial_ctrl_type,
serial_base_ctrl_release,
port->ctrl_id);
if (err)
goto err_free_ctrl_dev;

err = device_add(&ctrl_dev->dev);
if (err)
goto err_put_device;

return ctrl_dev;

err_put_device:
put_device(&ctrl_dev->dev);
err_free_ctrl_dev:
kfree(ctrl_dev);

return ERR_PTR(err);
}

static const struct device_type serial_port_type = {
.name = "port",
};

static void serial_base_port_release(struct device *dev)
{
struct serial_port_device *port_dev = to_serial_base_port_device(dev);

kfree(port_dev);
}

struct serial_port_device *serial_base_port_add(struct uart_port *port,
struct serial_ctrl_device *ctrl_dev)
{
struct serial_port_device *port_dev;
int err;

port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
if (!port_dev)
return ERR_PTR(-ENOMEM);

err = serial_base_device_init(port, &port_dev->dev,
&ctrl_dev->dev, &serial_port_type,
serial_base_port_release,
port->line);
if (err)
goto err_free_port_dev;

port_dev->port = port;

err = device_add(&port_dev->dev);
if (err)
goto err_put_device;

return port_dev;

err_put_device:
put_device(&port_dev->dev);
err_free_port_dev:
kfree(port_dev);

return ERR_PTR(err);
}

void serial_base_port_device_remove(struct serial_port_device *port_dev)
{
if (!port_dev)
return;

device_del(&port_dev->dev);
}

static int serial_base_init(void)
{
int ret;

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

ret = serial_base_ctrl_init();
if (ret)
goto err_bus_unregister;

ret = serial_base_port_init();
if (ret)
goto err_ctrl_exit;

return 0;

err_ctrl_exit:
serial_base_ctrl_exit();

err_bus_unregister:
bus_unregister(&serial_base_bus_type);

return ret;
}
module_init(serial_base_init);

static void serial_base_exit(void)
{
serial_base_port_exit();
serial_base_ctrl_exit();
bus_unregister(&serial_base_bus_type);
}
module_exit(serial_base_exit);

MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
MODULE_DESCRIPTION("Serial core bus");
MODULE_LICENSE("GPL");
Loading

0 comments on commit 84a9582

Please sign in to comment.