Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <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>

authored by

Tony Lindgren and committed by
Greg Kroah-Hartman
84a9582f ae62c49c

+598 -23
+1
drivers/tty/serial/8250/8250_core.c
··· 1039 1039 if (uart->port.dev) 1040 1040 uart_remove_one_port(&serial8250_reg, &uart->port); 1041 1041 1042 + uart->port.ctrl_id = up->port.ctrl_id; 1042 1043 uart->port.iobase = up->port.iobase; 1043 1044 uart->port.membase = up->port.membase; 1044 1045 uart->port.irq = up->port.irq;
+1
drivers/tty/serial/8250/8250_port.c
··· 3282 3282 struct uart_port *port = &up->port; 3283 3283 3284 3284 spin_lock_init(&port->lock); 3285 + port->ctrl_id = 0; 3285 3286 port->ops = &serial8250_pops; 3286 3287 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE); 3287 3288
+2 -1
drivers/tty/serial/Makefile
··· 3 3 # Makefile for the kernel serial device drivers. 4 4 # 5 5 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 7 8 8 9 obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o 9 10 obj-$(CONFIG_SERIAL_EARLYCON_SEMIHOST) += earlycon-semihost.o
+46
drivers/tty/serial/serial_base.h
··· 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 <tony@atomide.com> 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);
+200
drivers/tty/serial/serial_base_bus.c
··· 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 <tony@atomide.com> 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 <tony@atomide.com>"); 199 + MODULE_DESCRIPTION("Serial core bus"); 200 + MODULE_LICENSE("GPL");
+171 -21
drivers/tty/serial/serial_core.c
··· 17 17 #include <linux/gpio/consumer.h> 18 18 #include <linux/kernel.h> 19 19 #include <linux/of.h> 20 + #include <linux/pm_runtime.h> 20 21 #include <linux/proc_fs.h> 21 22 #include <linux/seq_file.h> 22 23 #include <linux/device.h> ··· 31 30 32 31 #include <linux/irq.h> 33 32 #include <linux/uaccess.h> 33 + 34 + #include "serial_base.h" 34 35 35 36 /* 36 37 * This is used to lock changes in serial line configuration. ··· 137 134 { 138 135 struct uart_state *state = tty->driver_data; 139 136 struct uart_port *port = state->uart_port; 137 + struct serial_port_device *port_dev; 138 + int err; 140 139 141 - if (port && !(port->flags & UPF_DEAD) && !uart_tx_stopped(port)) 140 + if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port)) 141 + return; 142 + 143 + port_dev = port->port_dev; 144 + 145 + /* Increment the runtime PM usage count for the active check below */ 146 + err = pm_runtime_get(&port_dev->dev); 147 + if (err < 0) { 148 + pm_runtime_put_noidle(&port_dev->dev); 149 + return; 150 + } 151 + 152 + /* 153 + * Start TX if enabled, and kick runtime PM. If the device is not 154 + * enabled, serial_port_runtime_resume() calls start_tx() again 155 + * after enabling the device. 156 + */ 157 + if (pm_runtime_active(&port_dev->dev)) 142 158 port->ops->start_tx(port); 159 + pm_runtime_mark_last_busy(&port_dev->dev); 160 + pm_runtime_put_autosuspend(&port_dev->dev); 143 161 } 144 162 145 163 static void uart_start(struct tty_struct *tty) ··· 3072 3048 }; 3073 3049 3074 3050 /** 3075 - * uart_add_one_port - attach a driver-defined port structure 3051 + * serial_core_add_one_port - attach a driver-defined port structure 3076 3052 * @drv: pointer to the uart low level driver structure for this port 3077 3053 * @uport: uart port structure to use for this port. 3078 3054 * ··· 3081 3057 * This allows the driver @drv to register its own uart_port structure with the 3082 3058 * core driver. The main purpose is to allow the low level uart drivers to 3083 3059 * expand uart_port, rather than having yet more levels of structures. 3060 + * Caller must hold port_mutex. 3084 3061 */ 3085 - int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) 3062 + static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *uport) 3086 3063 { 3087 3064 struct uart_state *state; 3088 3065 struct tty_port *port; ··· 3097 3072 state = drv->state + uport->line; 3098 3073 port = &state->port; 3099 3074 3100 - mutex_lock(&port_mutex); 3101 3075 mutex_lock(&port->mutex); 3102 3076 if (state->uart_port) { 3103 3077 ret = -EINVAL; ··· 3161 3137 uport->line); 3162 3138 } 3163 3139 3164 - /* 3165 - * Ensure UPF_DEAD is not set. 3166 - */ 3167 - uport->flags &= ~UPF_DEAD; 3168 - 3169 3140 out: 3170 3141 mutex_unlock(&port->mutex); 3171 - mutex_unlock(&port_mutex); 3172 3142 3173 3143 return ret; 3174 3144 } 3175 - EXPORT_SYMBOL(uart_add_one_port); 3176 3145 3177 3146 /** 3178 - * uart_remove_one_port - detach a driver defined port structure 3147 + * serial_core_remove_one_port - detach a driver defined port structure 3179 3148 * @drv: pointer to the uart low level driver structure for this port 3180 3149 * @uport: uart port structure for this port 3181 3150 * ··· 3176 3159 * 3177 3160 * This unhooks (and hangs up) the specified port structure from the core 3178 3161 * driver. No further calls will be made to the low-level code for this port. 3162 + * Caller must hold port_mutex. 3179 3163 */ 3180 - void uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport) 3164 + static void serial_core_remove_one_port(struct uart_driver *drv, 3165 + struct uart_port *uport) 3181 3166 { 3182 3167 struct uart_state *state = drv->state + uport->line; 3183 3168 struct tty_port *port = &state->port; 3184 3169 struct uart_port *uart_port; 3185 3170 struct tty_struct *tty; 3186 3171 3187 - mutex_lock(&port_mutex); 3188 - 3189 - /* 3190 - * Mark the port "dead" - this prevents any opens from 3191 - * succeeding while we shut down the port. 3192 - */ 3193 3172 mutex_lock(&port->mutex); 3194 3173 uart_port = uart_port_check(state); 3195 3174 if (uart_port != uport) ··· 3196 3183 mutex_unlock(&port->mutex); 3197 3184 goto out; 3198 3185 } 3199 - uport->flags |= UPF_DEAD; 3200 3186 mutex_unlock(&port->mutex); 3201 3187 3202 3188 /* ··· 3227 3215 * Indicate that there isn't a port here anymore. 3228 3216 */ 3229 3217 uport->type = PORT_UNKNOWN; 3218 + uport->port_dev = NULL; 3230 3219 3231 3220 mutex_lock(&port->mutex); 3232 3221 WARN_ON(atomic_dec_return(&state->refcount) < 0); ··· 3237 3224 out: 3238 3225 mutex_unlock(&port_mutex); 3239 3226 } 3240 - EXPORT_SYMBOL(uart_remove_one_port); 3241 3227 3242 3228 /** 3243 3229 * uart_match_port - are the two ports equivalent? ··· 3270 3258 return false; 3271 3259 } 3272 3260 EXPORT_SYMBOL(uart_match_port); 3261 + 3262 + static struct serial_ctrl_device * 3263 + serial_core_get_ctrl_dev(struct serial_port_device *port_dev) 3264 + { 3265 + struct device *dev = &port_dev->dev; 3266 + 3267 + return to_serial_base_ctrl_device(dev->parent); 3268 + } 3269 + 3270 + /* 3271 + * Find a registered serial core controller device if one exists. Returns 3272 + * the first device matching the ctrl_id. Caller must hold port_mutex. 3273 + */ 3274 + static struct serial_ctrl_device *serial_core_ctrl_find(struct uart_driver *drv, 3275 + struct device *phys_dev, 3276 + int ctrl_id) 3277 + { 3278 + struct uart_state *state; 3279 + int i; 3280 + 3281 + lockdep_assert_held(&port_mutex); 3282 + 3283 + for (i = 0; i < drv->nr; i++) { 3284 + state = drv->state + i; 3285 + if (!state->uart_port || !state->uart_port->port_dev) 3286 + continue; 3287 + 3288 + if (state->uart_port->dev == phys_dev && 3289 + state->uart_port->ctrl_id == ctrl_id) 3290 + return serial_core_get_ctrl_dev(state->uart_port->port_dev); 3291 + } 3292 + 3293 + return NULL; 3294 + } 3295 + 3296 + static struct serial_ctrl_device *serial_core_ctrl_device_add(struct uart_port *port) 3297 + { 3298 + return serial_base_ctrl_add(port, port->dev); 3299 + } 3300 + 3301 + static int serial_core_port_device_add(struct serial_ctrl_device *ctrl_dev, 3302 + struct uart_port *port) 3303 + { 3304 + struct serial_port_device *port_dev; 3305 + 3306 + port_dev = serial_base_port_add(port, ctrl_dev); 3307 + if (IS_ERR(port_dev)) 3308 + return PTR_ERR(port_dev); 3309 + 3310 + port->port_dev = port_dev; 3311 + 3312 + return 0; 3313 + } 3314 + 3315 + /* 3316 + * Initialize a serial core port device, and a controller device if needed. 3317 + */ 3318 + int serial_core_register_port(struct uart_driver *drv, struct uart_port *port) 3319 + { 3320 + struct serial_ctrl_device *ctrl_dev, *new_ctrl_dev = NULL; 3321 + int ret; 3322 + 3323 + mutex_lock(&port_mutex); 3324 + 3325 + /* 3326 + * Prevent serial_port_runtime_resume() from trying to use the port 3327 + * until serial_core_add_one_port() has completed 3328 + */ 3329 + port->flags |= UPF_DEAD; 3330 + 3331 + /* Inititalize a serial core controller device if needed */ 3332 + ctrl_dev = serial_core_ctrl_find(drv, port->dev, port->ctrl_id); 3333 + if (!ctrl_dev) { 3334 + new_ctrl_dev = serial_core_ctrl_device_add(port); 3335 + if (!new_ctrl_dev) { 3336 + ret = -ENODEV; 3337 + goto err_unlock; 3338 + } 3339 + ctrl_dev = new_ctrl_dev; 3340 + } 3341 + 3342 + /* 3343 + * Initialize a serial core port device. Tag the port dead to prevent 3344 + * serial_port_runtime_resume() trying to do anything until port has 3345 + * been registered. It gets cleared by serial_core_add_one_port(). 3346 + */ 3347 + ret = serial_core_port_device_add(ctrl_dev, port); 3348 + if (ret) 3349 + goto err_unregister_ctrl_dev; 3350 + 3351 + ret = serial_core_add_one_port(drv, port); 3352 + if (ret) 3353 + goto err_unregister_port_dev; 3354 + 3355 + port->flags &= ~UPF_DEAD; 3356 + 3357 + mutex_unlock(&port_mutex); 3358 + 3359 + return 0; 3360 + 3361 + err_unregister_port_dev: 3362 + serial_base_port_device_remove(port->port_dev); 3363 + 3364 + err_unregister_ctrl_dev: 3365 + serial_base_ctrl_device_remove(new_ctrl_dev); 3366 + 3367 + err_unlock: 3368 + mutex_unlock(&port_mutex); 3369 + 3370 + return ret; 3371 + } 3372 + 3373 + /* 3374 + * Removes a serial core port device, and the related serial core controller 3375 + * device if the last instance. 3376 + */ 3377 + void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port) 3378 + { 3379 + struct device *phys_dev = port->dev; 3380 + struct serial_port_device *port_dev = port->port_dev; 3381 + struct serial_ctrl_device *ctrl_dev = serial_core_get_ctrl_dev(port_dev); 3382 + int ctrl_id = port->ctrl_id; 3383 + 3384 + mutex_lock(&port_mutex); 3385 + 3386 + port->flags |= UPF_DEAD; 3387 + 3388 + serial_core_remove_one_port(drv, port); 3389 + 3390 + /* Note that struct uart_port *port is no longer valid at this point */ 3391 + serial_base_port_device_remove(port_dev); 3392 + 3393 + /* Drop the serial core controller device if no ports are using it */ 3394 + if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id)) 3395 + serial_base_ctrl_device_remove(ctrl_dev); 3396 + 3397 + mutex_unlock(&port_mutex); 3398 + } 3273 3399 3274 3400 /** 3275 3401 * uart_handle_dcd_change - handle a change of carrier detect state
+68
drivers/tty/serial/serial_ctrl.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * Serial core controller driver 4 + * 5 + * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/ 6 + * Author: Tony Lindgren <tony@atomide.com> 7 + * 8 + * This driver manages the serial core controller struct device instances. 9 + * The serial core controller devices are children of the physical serial 10 + * port device. 11 + */ 12 + 13 + #include <linux/device.h> 14 + #include <linux/module.h> 15 + #include <linux/pm_runtime.h> 16 + #include <linux/serial_core.h> 17 + #include <linux/spinlock.h> 18 + 19 + #include "serial_base.h" 20 + 21 + static int serial_ctrl_probe(struct device *dev) 22 + { 23 + pm_runtime_enable(dev); 24 + 25 + return 0; 26 + } 27 + 28 + static int serial_ctrl_remove(struct device *dev) 29 + { 30 + pm_runtime_disable(dev); 31 + 32 + return 0; 33 + } 34 + 35 + /* 36 + * Serial core controller device init functions. Note that the physical 37 + * serial port device driver may not have completed probe at this point. 38 + */ 39 + int serial_ctrl_register_port(struct uart_driver *drv, struct uart_port *port) 40 + { 41 + return serial_core_register_port(drv, port); 42 + } 43 + 44 + void serial_ctrl_unregister_port(struct uart_driver *drv, struct uart_port *port) 45 + { 46 + serial_core_unregister_port(drv, port); 47 + } 48 + 49 + static struct device_driver serial_ctrl_driver = { 50 + .name = "ctrl", 51 + .suppress_bind_attrs = true, 52 + .probe = serial_ctrl_probe, 53 + .remove = serial_ctrl_remove, 54 + }; 55 + 56 + int serial_base_ctrl_init(void) 57 + { 58 + return serial_base_driver_register(&serial_ctrl_driver); 59 + } 60 + 61 + void serial_base_ctrl_exit(void) 62 + { 63 + serial_base_driver_unregister(&serial_ctrl_driver); 64 + } 65 + 66 + MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 67 + MODULE_DESCRIPTION("Serial core controller driver"); 68 + MODULE_LICENSE("GPL");
+105
drivers/tty/serial/serial_port.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * Serial core port device driver 4 + * 5 + * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/ 6 + * Author: Tony Lindgren <tony@atomide.com> 7 + */ 8 + 9 + #include <linux/device.h> 10 + #include <linux/module.h> 11 + #include <linux/pm_runtime.h> 12 + #include <linux/serial_core.h> 13 + #include <linux/spinlock.h> 14 + 15 + #include "serial_base.h" 16 + 17 + #define SERIAL_PORT_AUTOSUSPEND_DELAY_MS 500 18 + 19 + /* Only considers pending TX for now. Caller must take care of locking */ 20 + static int __serial_port_busy(struct uart_port *port) 21 + { 22 + return !uart_tx_stopped(port) && 23 + uart_circ_chars_pending(&port->state->xmit); 24 + } 25 + 26 + static int serial_port_runtime_resume(struct device *dev) 27 + { 28 + struct serial_port_device *port_dev = to_serial_base_port_device(dev); 29 + struct uart_port *port; 30 + unsigned long flags; 31 + 32 + port = port_dev->port; 33 + 34 + if (port->flags & UPF_DEAD) 35 + goto out; 36 + 37 + /* Flush any pending TX for the port */ 38 + spin_lock_irqsave(&port->lock, flags); 39 + if (__serial_port_busy(port)) 40 + port->ops->start_tx(port); 41 + spin_unlock_irqrestore(&port->lock, flags); 42 + 43 + out: 44 + pm_runtime_mark_last_busy(dev); 45 + 46 + return 0; 47 + } 48 + 49 + static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm, 50 + NULL, serial_port_runtime_resume, NULL); 51 + 52 + static int serial_port_probe(struct device *dev) 53 + { 54 + pm_runtime_enable(dev); 55 + pm_runtime_set_autosuspend_delay(dev, SERIAL_PORT_AUTOSUSPEND_DELAY_MS); 56 + pm_runtime_use_autosuspend(dev); 57 + 58 + return 0; 59 + } 60 + 61 + static int serial_port_remove(struct device *dev) 62 + { 63 + pm_runtime_dont_use_autosuspend(dev); 64 + pm_runtime_disable(dev); 65 + 66 + return 0; 67 + } 68 + 69 + /* 70 + * Serial core port device init functions. Note that the physical serial 71 + * port device driver may not have completed probe at this point. 72 + */ 73 + int uart_add_one_port(struct uart_driver *drv, struct uart_port *port) 74 + { 75 + return serial_ctrl_register_port(drv, port); 76 + } 77 + EXPORT_SYMBOL(uart_add_one_port); 78 + 79 + void uart_remove_one_port(struct uart_driver *drv, struct uart_port *port) 80 + { 81 + serial_ctrl_unregister_port(drv, port); 82 + } 83 + EXPORT_SYMBOL(uart_remove_one_port); 84 + 85 + static struct device_driver serial_port_driver = { 86 + .name = "port", 87 + .suppress_bind_attrs = true, 88 + .probe = serial_port_probe, 89 + .remove = serial_port_remove, 90 + .pm = pm_ptr(&serial_port_pm), 91 + }; 92 + 93 + int serial_base_port_init(void) 94 + { 95 + return serial_base_driver_register(&serial_port_driver); 96 + } 97 + 98 + void serial_base_port_exit(void) 99 + { 100 + serial_base_driver_unregister(&serial_port_driver); 101 + } 102 + 103 + MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 104 + MODULE_DESCRIPTION("Serial controller port driver"); 105 + MODULE_LICENSE("GPL");
+4 -1
include/linux/serial_core.h
··· 28 28 29 29 struct uart_port; 30 30 struct serial_struct; 31 + struct serial_port_device; 31 32 struct device; 32 33 struct gpio_desc; 33 34 ··· 459 458 struct serial_rs485 *rs485); 460 459 int (*iso7816_config)(struct uart_port *, 461 460 struct serial_iso7816 *iso7816); 461 + int ctrl_id; /* optional serial core controller id */ 462 462 unsigned int irq; /* irq number */ 463 463 unsigned long irqflags; /* irq flags */ 464 464 unsigned int uartclk; /* base uart clock */ ··· 565 563 unsigned int minor; 566 564 resource_size_t mapbase; /* for ioremap */ 567 565 resource_size_t mapsize; 568 - struct device *dev; /* parent device */ 566 + struct device *dev; /* serial port physical parent device */ 567 + struct serial_port_device *port_dev; /* serial core port device */ 569 568 570 569 unsigned long sysrq; /* sysrq timeout */ 571 570 unsigned int sysrq_ch; /* char for sysrq */