Skip to content

Commit 84a9582

Browse files
tmlindgregkh
authored andcommitted
serial: core: Start managing serial controllers to enable runtime PM
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 <[email protected]> Suggested-by: Greg Kroah-Hartman <[email protected]> Suggested-by: Jiri Slaby <[email protected]> Suggested-by: Johan Hovold <[email protected]> Signed-off-by: Tony Lindgren <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent ae62c49 commit 84a9582

File tree

9 files changed

+598
-23
lines changed

9 files changed

+598
-23
lines changed

drivers/tty/serial/8250/8250_core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ int serial8250_register_8250_port(const struct uart_8250_port *up)
10391039
if (uart->port.dev)
10401040
uart_remove_one_port(&serial8250_reg, &uart->port);
10411041

1042+
uart->port.ctrl_id = up->port.ctrl_id;
10421043
uart->port.iobase = up->port.iobase;
10431044
uart->port.membase = up->port.membase;
10441045
uart->port.irq = up->port.irq;

drivers/tty/serial/8250/8250_port.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,7 @@ void serial8250_init_port(struct uart_8250_port *up)
32823282
struct uart_port *port = &up->port;
32833283

32843284
spin_lock_init(&port->lock);
3285+
port->ctrl_id = 0;
32853286
port->ops = &serial8250_pops;
32863287
port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
32873288

drivers/tty/serial/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Makefile for the kernel serial device drivers.
44
#
55

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

89
obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o
910
obj-$(CONFIG_SERIAL_EARLYCON_SEMIHOST) += earlycon-semihost.o

drivers/tty/serial/serial_base.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
/*
3+
* Serial core related functions, serial port device drivers do not need this.
4+
*
5+
* Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
6+
* Author: Tony Lindgren <[email protected]>
7+
*/
8+
9+
#define to_serial_base_ctrl_device(d) container_of((d), struct serial_ctrl_device, dev)
10+
#define to_serial_base_port_device(d) container_of((d), struct serial_port_device, dev)
11+
12+
struct uart_driver;
13+
struct uart_port;
14+
struct device_driver;
15+
struct device;
16+
17+
struct serial_ctrl_device {
18+
struct device dev;
19+
};
20+
21+
struct serial_port_device {
22+
struct device dev;
23+
struct uart_port *port;
24+
};
25+
26+
int serial_base_ctrl_init(void);
27+
void serial_base_ctrl_exit(void);
28+
29+
int serial_base_port_init(void);
30+
void serial_base_port_exit(void);
31+
32+
int serial_base_driver_register(struct device_driver *driver);
33+
void serial_base_driver_unregister(struct device_driver *driver);
34+
35+
struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
36+
struct device *parent);
37+
struct serial_port_device *serial_base_port_add(struct uart_port *port,
38+
struct serial_ctrl_device *parent);
39+
void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev);
40+
void serial_base_port_device_remove(struct serial_port_device *port_dev);
41+
42+
int serial_ctrl_register_port(struct uart_driver *drv, struct uart_port *port);
43+
void serial_ctrl_unregister_port(struct uart_driver *drv, struct uart_port *port);
44+
45+
int serial_core_register_port(struct uart_driver *drv, struct uart_port *port);
46+
void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port);

drivers/tty/serial/serial_base_bus.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Serial base bus layer for controllers
4+
*
5+
* Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
6+
* Author: Tony Lindgren <[email protected]>
7+
*
8+
* The serial core bus manages the serial core controller instances.
9+
*/
10+
11+
#include <linux/container_of.h>
12+
#include <linux/device.h>
13+
#include <linux/module.h>
14+
#include <linux/serial_core.h>
15+
#include <linux/slab.h>
16+
#include <linux/spinlock.h>
17+
18+
#include "serial_base.h"
19+
20+
static int serial_base_match(struct device *dev, struct device_driver *drv)
21+
{
22+
int len = strlen(drv->name);
23+
24+
return !strncmp(dev_name(dev), drv->name, len);
25+
}
26+
27+
static struct bus_type serial_base_bus_type = {
28+
.name = "serial-base",
29+
.match = serial_base_match,
30+
};
31+
32+
int serial_base_driver_register(struct device_driver *driver)
33+
{
34+
driver->bus = &serial_base_bus_type;
35+
36+
return driver_register(driver);
37+
}
38+
39+
void serial_base_driver_unregister(struct device_driver *driver)
40+
{
41+
driver_unregister(driver);
42+
}
43+
44+
static int serial_base_device_init(struct uart_port *port,
45+
struct device *dev,
46+
struct device *parent_dev,
47+
const struct device_type *type,
48+
void (*release)(struct device *dev),
49+
int id)
50+
{
51+
device_initialize(dev);
52+
dev->type = type;
53+
dev->parent = parent_dev;
54+
dev->bus = &serial_base_bus_type;
55+
dev->release = release;
56+
57+
return dev_set_name(dev, "%s.%s.%d", type->name, dev_name(port->dev), id);
58+
}
59+
60+
static const struct device_type serial_ctrl_type = {
61+
.name = "ctrl",
62+
};
63+
64+
static void serial_base_ctrl_release(struct device *dev)
65+
{
66+
struct serial_ctrl_device *ctrl_dev = to_serial_base_ctrl_device(dev);
67+
68+
kfree(ctrl_dev);
69+
}
70+
71+
void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
72+
{
73+
if (!ctrl_dev)
74+
return;
75+
76+
device_del(&ctrl_dev->dev);
77+
}
78+
79+
struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
80+
struct device *parent)
81+
{
82+
struct serial_ctrl_device *ctrl_dev;
83+
int err;
84+
85+
ctrl_dev = kzalloc(sizeof(*ctrl_dev), GFP_KERNEL);
86+
if (!ctrl_dev)
87+
return ERR_PTR(-ENOMEM);
88+
89+
err = serial_base_device_init(port, &ctrl_dev->dev,
90+
parent, &serial_ctrl_type,
91+
serial_base_ctrl_release,
92+
port->ctrl_id);
93+
if (err)
94+
goto err_free_ctrl_dev;
95+
96+
err = device_add(&ctrl_dev->dev);
97+
if (err)
98+
goto err_put_device;
99+
100+
return ctrl_dev;
101+
102+
err_put_device:
103+
put_device(&ctrl_dev->dev);
104+
err_free_ctrl_dev:
105+
kfree(ctrl_dev);
106+
107+
return ERR_PTR(err);
108+
}
109+
110+
static const struct device_type serial_port_type = {
111+
.name = "port",
112+
};
113+
114+
static void serial_base_port_release(struct device *dev)
115+
{
116+
struct serial_port_device *port_dev = to_serial_base_port_device(dev);
117+
118+
kfree(port_dev);
119+
}
120+
121+
struct serial_port_device *serial_base_port_add(struct uart_port *port,
122+
struct serial_ctrl_device *ctrl_dev)
123+
{
124+
struct serial_port_device *port_dev;
125+
int err;
126+
127+
port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
128+
if (!port_dev)
129+
return ERR_PTR(-ENOMEM);
130+
131+
err = serial_base_device_init(port, &port_dev->dev,
132+
&ctrl_dev->dev, &serial_port_type,
133+
serial_base_port_release,
134+
port->line);
135+
if (err)
136+
goto err_free_port_dev;
137+
138+
port_dev->port = port;
139+
140+
err = device_add(&port_dev->dev);
141+
if (err)
142+
goto err_put_device;
143+
144+
return port_dev;
145+
146+
err_put_device:
147+
put_device(&port_dev->dev);
148+
err_free_port_dev:
149+
kfree(port_dev);
150+
151+
return ERR_PTR(err);
152+
}
153+
154+
void serial_base_port_device_remove(struct serial_port_device *port_dev)
155+
{
156+
if (!port_dev)
157+
return;
158+
159+
device_del(&port_dev->dev);
160+
}
161+
162+
static int serial_base_init(void)
163+
{
164+
int ret;
165+
166+
ret = bus_register(&serial_base_bus_type);
167+
if (ret)
168+
return ret;
169+
170+
ret = serial_base_ctrl_init();
171+
if (ret)
172+
goto err_bus_unregister;
173+
174+
ret = serial_base_port_init();
175+
if (ret)
176+
goto err_ctrl_exit;
177+
178+
return 0;
179+
180+
err_ctrl_exit:
181+
serial_base_ctrl_exit();
182+
183+
err_bus_unregister:
184+
bus_unregister(&serial_base_bus_type);
185+
186+
return ret;
187+
}
188+
module_init(serial_base_init);
189+
190+
static void serial_base_exit(void)
191+
{
192+
serial_base_port_exit();
193+
serial_base_ctrl_exit();
194+
bus_unregister(&serial_base_bus_type);
195+
}
196+
module_exit(serial_base_exit);
197+
198+
MODULE_AUTHOR("Tony Lindgren <[email protected]>");
199+
MODULE_DESCRIPTION("Serial core bus");
200+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)