1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
5 * Based on drivers/spmi/spmi.c:
6 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
9 #include <linux/acpi.h>
10 #include <linux/errno.h>
11 #include <linux/idr.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_domain.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/property.h>
19 #include <linux/sched.h>
20 #include <linux/serdev.h>
21 #include <linux/slab.h>
23 #include <linux/platform_data/x86/apple.h>
25 static bool is_registered
;
26 static DEFINE_IDA(ctrl_ida
);
28 static ssize_t
modalias_show(struct device
*dev
,
29 struct device_attribute
*attr
, char *buf
)
33 len
= acpi_device_modalias(dev
, buf
, PAGE_SIZE
- 1);
37 return of_device_modalias(dev
, buf
, PAGE_SIZE
);
39 static DEVICE_ATTR_RO(modalias
);
41 static struct attribute
*serdev_device_attrs
[] = {
42 &dev_attr_modalias
.attr
,
45 ATTRIBUTE_GROUPS(serdev_device
);
47 static int serdev_device_uevent(const struct device
*dev
, struct kobj_uevent_env
*env
)
51 /* TODO: platform modalias */
53 rc
= acpi_device_uevent_modalias(dev
, env
);
57 return of_device_uevent_modalias(dev
, env
);
60 static void serdev_device_release(struct device
*dev
)
62 struct serdev_device
*serdev
= to_serdev_device(dev
);
66 static const struct device_type serdev_device_type
= {
67 .groups
= serdev_device_groups
,
68 .uevent
= serdev_device_uevent
,
69 .release
= serdev_device_release
,
72 static bool is_serdev_device(const struct device
*dev
)
74 return dev
->type
== &serdev_device_type
;
77 static void serdev_ctrl_release(struct device
*dev
)
79 struct serdev_controller
*ctrl
= to_serdev_controller(dev
);
80 ida_free(&ctrl_ida
, ctrl
->nr
);
84 static const struct device_type serdev_ctrl_type
= {
85 .release
= serdev_ctrl_release
,
88 static int serdev_device_match(struct device
*dev
, const struct device_driver
*drv
)
90 if (!is_serdev_device(dev
))
93 /* TODO: platform matching */
94 if (acpi_driver_match_device(dev
, drv
))
97 return of_driver_match_device(dev
, drv
);
101 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
102 * @serdev: serdev_device to be added
104 int serdev_device_add(struct serdev_device
*serdev
)
106 struct serdev_controller
*ctrl
= serdev
->ctrl
;
107 struct device
*parent
= serdev
->dev
.parent
;
110 dev_set_name(&serdev
->dev
, "%s-%d", dev_name(parent
), serdev
->nr
);
112 /* Only a single slave device is currently supported. */
114 dev_err(&serdev
->dev
, "controller busy\n");
117 ctrl
->serdev
= serdev
;
119 err
= device_add(&serdev
->dev
);
121 dev_err(&serdev
->dev
, "Can't add %s, status %pe\n",
122 dev_name(&serdev
->dev
), ERR_PTR(err
));
123 goto err_clear_serdev
;
126 dev_dbg(&serdev
->dev
, "device %s registered\n", dev_name(&serdev
->dev
));
134 EXPORT_SYMBOL_GPL(serdev_device_add
);
137 * serdev_device_remove(): remove an serdev device
138 * @serdev: serdev_device to be removed
140 void serdev_device_remove(struct serdev_device
*serdev
)
142 struct serdev_controller
*ctrl
= serdev
->ctrl
;
144 device_unregister(&serdev
->dev
);
147 EXPORT_SYMBOL_GPL(serdev_device_remove
);
149 int serdev_device_open(struct serdev_device
*serdev
)
151 struct serdev_controller
*ctrl
= serdev
->ctrl
;
154 if (!ctrl
|| !ctrl
->ops
->open
)
157 ret
= ctrl
->ops
->open(ctrl
);
161 ret
= pm_runtime_get_sync(&ctrl
->dev
);
163 pm_runtime_put_noidle(&ctrl
->dev
);
170 if (ctrl
->ops
->close
)
171 ctrl
->ops
->close(ctrl
);
175 EXPORT_SYMBOL_GPL(serdev_device_open
);
177 void serdev_device_close(struct serdev_device
*serdev
)
179 struct serdev_controller
*ctrl
= serdev
->ctrl
;
181 if (!ctrl
|| !ctrl
->ops
->close
)
184 pm_runtime_put(&ctrl
->dev
);
186 ctrl
->ops
->close(ctrl
);
188 EXPORT_SYMBOL_GPL(serdev_device_close
);
190 static void devm_serdev_device_close(void *serdev
)
192 serdev_device_close(serdev
);
195 int devm_serdev_device_open(struct device
*dev
, struct serdev_device
*serdev
)
199 ret
= serdev_device_open(serdev
);
203 return devm_add_action_or_reset(dev
, devm_serdev_device_close
, serdev
);
205 EXPORT_SYMBOL_GPL(devm_serdev_device_open
);
207 void serdev_device_write_wakeup(struct serdev_device
*serdev
)
209 complete(&serdev
->write_comp
);
211 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup
);
214 * serdev_device_write_buf() - write data asynchronously
215 * @serdev: serdev device
216 * @buf: data to be written
217 * @count: number of bytes to write
219 * Write data to the device asynchronously.
221 * Note that any accepted data has only been buffered by the controller; use
222 * serdev_device_wait_until_sent() to make sure the controller write buffer
223 * has actually been emptied.
225 * Return: The number of bytes written (less than count if not enough room in
226 * the write buffer), or a negative errno on errors.
228 int serdev_device_write_buf(struct serdev_device
*serdev
, const u8
*buf
, size_t count
)
230 struct serdev_controller
*ctrl
= serdev
->ctrl
;
232 if (!ctrl
|| !ctrl
->ops
->write_buf
)
235 return ctrl
->ops
->write_buf(ctrl
, buf
, count
);
237 EXPORT_SYMBOL_GPL(serdev_device_write_buf
);
240 * serdev_device_write() - write data synchronously
241 * @serdev: serdev device
242 * @buf: data to be written
243 * @count: number of bytes to write
244 * @timeout: timeout in jiffies, or 0 to wait indefinitely
246 * Write data to the device synchronously by repeatedly calling
247 * serdev_device_write() until the controller has accepted all data (unless
248 * interrupted by a timeout or a signal).
250 * Note that any accepted data has only been buffered by the controller; use
251 * serdev_device_wait_until_sent() to make sure the controller write buffer
252 * has actually been emptied.
254 * Note that this function depends on serdev_device_write_wakeup() being
255 * called in the serdev driver write_wakeup() callback.
257 * Return: The number of bytes written (less than count if interrupted),
258 * -ETIMEDOUT or -ERESTARTSYS if interrupted before any bytes were written, or
259 * a negative errno on errors.
261 ssize_t
serdev_device_write(struct serdev_device
*serdev
, const u8
*buf
,
262 size_t count
, long timeout
)
264 struct serdev_controller
*ctrl
= serdev
->ctrl
;
268 if (!ctrl
|| !ctrl
->ops
->write_buf
|| !serdev
->ops
->write_wakeup
)
272 timeout
= MAX_SCHEDULE_TIMEOUT
;
274 mutex_lock(&serdev
->write_lock
);
276 reinit_completion(&serdev
->write_comp
);
278 ret
= ctrl
->ops
->write_buf(ctrl
, buf
, count
);
289 timeout
= wait_for_completion_interruptible_timeout(&serdev
->write_comp
,
291 } while (timeout
> 0);
292 mutex_unlock(&serdev
->write_lock
);
297 if (timeout
<= 0 && written
== 0) {
298 if (timeout
== -ERESTARTSYS
)
306 EXPORT_SYMBOL_GPL(serdev_device_write
);
308 void serdev_device_write_flush(struct serdev_device
*serdev
)
310 struct serdev_controller
*ctrl
= serdev
->ctrl
;
312 if (!ctrl
|| !ctrl
->ops
->write_flush
)
315 ctrl
->ops
->write_flush(ctrl
);
317 EXPORT_SYMBOL_GPL(serdev_device_write_flush
);
319 int serdev_device_write_room(struct serdev_device
*serdev
)
321 struct serdev_controller
*ctrl
= serdev
->ctrl
;
323 if (!ctrl
|| !ctrl
->ops
->write_room
)
326 return serdev
->ctrl
->ops
->write_room(ctrl
);
328 EXPORT_SYMBOL_GPL(serdev_device_write_room
);
330 unsigned int serdev_device_set_baudrate(struct serdev_device
*serdev
, unsigned int speed
)
332 struct serdev_controller
*ctrl
= serdev
->ctrl
;
334 if (!ctrl
|| !ctrl
->ops
->set_baudrate
)
337 return ctrl
->ops
->set_baudrate(ctrl
, speed
);
340 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate
);
342 void serdev_device_set_flow_control(struct serdev_device
*serdev
, bool enable
)
344 struct serdev_controller
*ctrl
= serdev
->ctrl
;
346 if (!ctrl
|| !ctrl
->ops
->set_flow_control
)
349 ctrl
->ops
->set_flow_control(ctrl
, enable
);
351 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control
);
353 int serdev_device_set_parity(struct serdev_device
*serdev
,
354 enum serdev_parity parity
)
356 struct serdev_controller
*ctrl
= serdev
->ctrl
;
358 if (!ctrl
|| !ctrl
->ops
->set_parity
)
361 return ctrl
->ops
->set_parity(ctrl
, parity
);
363 EXPORT_SYMBOL_GPL(serdev_device_set_parity
);
365 void serdev_device_wait_until_sent(struct serdev_device
*serdev
, long timeout
)
367 struct serdev_controller
*ctrl
= serdev
->ctrl
;
369 if (!ctrl
|| !ctrl
->ops
->wait_until_sent
)
372 ctrl
->ops
->wait_until_sent(ctrl
, timeout
);
374 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent
);
376 int serdev_device_get_tiocm(struct serdev_device
*serdev
)
378 struct serdev_controller
*ctrl
= serdev
->ctrl
;
380 if (!ctrl
|| !ctrl
->ops
->get_tiocm
)
383 return ctrl
->ops
->get_tiocm(ctrl
);
385 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm
);
387 int serdev_device_set_tiocm(struct serdev_device
*serdev
, int set
, int clear
)
389 struct serdev_controller
*ctrl
= serdev
->ctrl
;
391 if (!ctrl
|| !ctrl
->ops
->set_tiocm
)
394 return ctrl
->ops
->set_tiocm(ctrl
, set
, clear
);
396 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm
);
398 int serdev_device_break_ctl(struct serdev_device
*serdev
, int break_state
)
400 struct serdev_controller
*ctrl
= serdev
->ctrl
;
402 if (!ctrl
|| !ctrl
->ops
->break_ctl
)
405 return ctrl
->ops
->break_ctl(ctrl
, break_state
);
407 EXPORT_SYMBOL_GPL(serdev_device_break_ctl
);
409 static int serdev_drv_probe(struct device
*dev
)
411 const struct serdev_device_driver
*sdrv
= to_serdev_device_driver(dev
->driver
);
414 ret
= dev_pm_domain_attach(dev
, true);
418 ret
= sdrv
->probe(to_serdev_device(dev
));
420 dev_pm_domain_detach(dev
, true);
425 static void serdev_drv_remove(struct device
*dev
)
427 const struct serdev_device_driver
*sdrv
= to_serdev_device_driver(dev
->driver
);
429 sdrv
->remove(to_serdev_device(dev
));
431 dev_pm_domain_detach(dev
, true);
434 static const struct bus_type serdev_bus_type
= {
436 .match
= serdev_device_match
,
437 .probe
= serdev_drv_probe
,
438 .remove
= serdev_drv_remove
,
442 * serdev_device_alloc() - Allocate a new serdev device
443 * @ctrl: associated controller
445 * Caller is responsible for either calling serdev_device_add() to add the
446 * newly allocated controller, or calling serdev_device_put() to discard it.
448 struct serdev_device
*serdev_device_alloc(struct serdev_controller
*ctrl
)
450 struct serdev_device
*serdev
;
452 serdev
= kzalloc(sizeof(*serdev
), GFP_KERNEL
);
457 device_initialize(&serdev
->dev
);
458 serdev
->dev
.parent
= &ctrl
->dev
;
459 serdev
->dev
.bus
= &serdev_bus_type
;
460 serdev
->dev
.type
= &serdev_device_type
;
461 init_completion(&serdev
->write_comp
);
462 mutex_init(&serdev
->write_lock
);
465 EXPORT_SYMBOL_GPL(serdev_device_alloc
);
468 * serdev_controller_alloc() - Allocate a new serdev controller
469 * @host: serial port hardware controller device
470 * @parent: parent device
471 * @size: size of private data
473 * Caller is responsible for either calling serdev_controller_add() to add the
474 * newly allocated controller, or calling serdev_controller_put() to discard it.
475 * The allocated private data region may be accessed via
476 * serdev_controller_get_drvdata()
478 struct serdev_controller
*serdev_controller_alloc(struct device
*host
,
479 struct device
*parent
,
482 struct serdev_controller
*ctrl
;
485 if (WARN_ON(!parent
))
488 ctrl
= kzalloc(sizeof(*ctrl
) + size
, GFP_KERNEL
);
492 id
= ida_alloc(&ctrl_ida
, GFP_KERNEL
);
495 "unable to allocate serdev controller identifier.\n");
501 device_initialize(&ctrl
->dev
);
502 ctrl
->dev
.type
= &serdev_ctrl_type
;
503 ctrl
->dev
.bus
= &serdev_bus_type
;
504 ctrl
->dev
.parent
= parent
;
506 device_set_node(&ctrl
->dev
, dev_fwnode(host
));
507 serdev_controller_set_drvdata(ctrl
, &ctrl
[1]);
509 dev_set_name(&ctrl
->dev
, "serial%d", id
);
511 pm_runtime_no_callbacks(&ctrl
->dev
);
512 pm_suspend_ignore_children(&ctrl
->dev
, true);
514 dev_dbg(&ctrl
->dev
, "allocated controller 0x%p id %d\n", ctrl
, id
);
522 EXPORT_SYMBOL_GPL(serdev_controller_alloc
);
524 static int of_serdev_register_devices(struct serdev_controller
*ctrl
)
526 struct device_node
*node
;
527 struct serdev_device
*serdev
= NULL
;
531 for_each_available_child_of_node(ctrl
->dev
.of_node
, node
) {
532 if (!of_property_present(node
, "compatible"))
535 dev_dbg(&ctrl
->dev
, "adding child %pOF\n", node
);
537 serdev
= serdev_device_alloc(ctrl
);
541 device_set_node(&serdev
->dev
, of_fwnode_handle(node
));
543 err
= serdev_device_add(serdev
);
545 dev_err(&serdev
->dev
,
546 "failure adding device. status %pe\n",
548 serdev_device_put(serdev
);
560 #define SERDEV_ACPI_MAX_SCAN_DEPTH 32
562 struct acpi_serdev_lookup
{
563 acpi_handle device_handle
;
564 acpi_handle controller_handle
;
570 * serdev_acpi_get_uart_resource - Gets UARTSerialBus resource if type matches
571 * @ares: ACPI resource
572 * @uart: Pointer to UARTSerialBus resource will be returned here
574 * Checks if the given ACPI resource is of type UARTSerialBus.
575 * In this case, returns a pointer to it to the caller.
577 * Return: True if resource type is of UARTSerialBus, otherwise false.
579 bool serdev_acpi_get_uart_resource(struct acpi_resource
*ares
,
580 struct acpi_resource_uart_serialbus
**uart
)
582 struct acpi_resource_uart_serialbus
*sb
;
584 if (ares
->type
!= ACPI_RESOURCE_TYPE_SERIAL_BUS
)
587 sb
= &ares
->data
.uart_serial_bus
;
588 if (sb
->type
!= ACPI_RESOURCE_SERIAL_TYPE_UART
)
594 EXPORT_SYMBOL_GPL(serdev_acpi_get_uart_resource
);
596 static int acpi_serdev_parse_resource(struct acpi_resource
*ares
, void *data
)
598 struct acpi_serdev_lookup
*lookup
= data
;
599 struct acpi_resource_uart_serialbus
*sb
;
602 if (!serdev_acpi_get_uart_resource(ares
, &sb
))
605 if (lookup
->index
!= -1 && lookup
->n
++ != lookup
->index
)
608 status
= acpi_get_handle(lookup
->device_handle
,
609 sb
->resource_source
.string_ptr
,
610 &lookup
->controller_handle
);
611 if (ACPI_FAILURE(status
))
615 * NOTE: Ideally, we would also want to retrieve other properties here,
616 * once setting them before opening the device is supported by serdev.
622 static int acpi_serdev_do_lookup(struct acpi_device
*adev
,
623 struct acpi_serdev_lookup
*lookup
)
625 struct list_head resource_list
;
628 lookup
->device_handle
= acpi_device_handle(adev
);
629 lookup
->controller_handle
= NULL
;
632 INIT_LIST_HEAD(&resource_list
);
633 ret
= acpi_dev_get_resources(adev
, &resource_list
,
634 acpi_serdev_parse_resource
, lookup
);
635 acpi_dev_free_resource_list(&resource_list
);
643 static int acpi_serdev_check_resources(struct serdev_controller
*ctrl
,
644 struct acpi_device
*adev
)
646 struct acpi_serdev_lookup lookup
;
649 if (acpi_bus_get_status(adev
) || !adev
->status
.present
)
652 /* Look for UARTSerialBusV2 resource */
653 lookup
.index
= -1; // we only care for the last device
655 ret
= acpi_serdev_do_lookup(adev
, &lookup
);
660 * Apple machines provide an empty resource template, so on those
661 * machines just look for immediate children with a "baud" property
662 * (from the _DSM method) instead.
664 if (!lookup
.controller_handle
&& x86_apple_machine
&&
665 !acpi_dev_get_property(adev
, "baud", ACPI_TYPE_BUFFER
, NULL
))
666 acpi_get_parent(adev
->handle
, &lookup
.controller_handle
);
668 /* Make sure controller and ResourceSource handle match */
669 if (!device_match_acpi_handle(ctrl
->host
, lookup
.controller_handle
))
675 static acpi_status
acpi_serdev_register_device(struct serdev_controller
*ctrl
,
676 struct acpi_device
*adev
)
678 struct serdev_device
*serdev
;
681 serdev
= serdev_device_alloc(ctrl
);
683 dev_err(&ctrl
->dev
, "failed to allocate serdev device for %s\n",
684 dev_name(&adev
->dev
));
688 ACPI_COMPANION_SET(&serdev
->dev
, adev
);
689 acpi_device_set_enumerated(adev
);
691 err
= serdev_device_add(serdev
);
693 dev_err(&serdev
->dev
,
694 "failure adding ACPI serdev device. status %pe\n",
696 serdev_device_put(serdev
);
702 static const struct acpi_device_id serdev_acpi_devices_blacklist
[] = {
708 static acpi_status
acpi_serdev_add_device(acpi_handle handle
, u32 level
,
709 void *data
, void **return_value
)
711 struct acpi_device
*adev
= acpi_fetch_acpi_dev(handle
);
712 struct serdev_controller
*ctrl
= data
;
714 if (!adev
|| acpi_device_enumerated(adev
))
717 /* Skip if black listed */
718 if (!acpi_match_device_ids(adev
, serdev_acpi_devices_blacklist
))
721 if (acpi_serdev_check_resources(ctrl
, adev
))
724 return acpi_serdev_register_device(ctrl
, adev
);
728 static int acpi_serdev_register_devices(struct serdev_controller
*ctrl
)
734 if (!has_acpi_companion(ctrl
->host
))
738 * Skip registration on boards where the ACPI tables are known to
739 * contain buggy devices. Note serdev_controller_add() must still
740 * succeed in this case, so that the proper serdev devices can be
741 * added "manually" later.
743 ret
= acpi_quirk_skip_serdev_enumeration(ctrl
->host
, &skip
);
749 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
750 SERDEV_ACPI_MAX_SCAN_DEPTH
,
751 acpi_serdev_add_device
, NULL
, ctrl
, NULL
);
752 if (ACPI_FAILURE(status
))
753 dev_warn(&ctrl
->dev
, "failed to enumerate serdev slaves\n");
761 static inline int acpi_serdev_register_devices(struct serdev_controller
*ctrl
)
765 #endif /* CONFIG_ACPI */
768 * serdev_controller_add() - Add an serdev controller
769 * @ctrl: controller to be registered.
771 * Register a controller previously allocated via serdev_controller_alloc() with
774 int serdev_controller_add(struct serdev_controller
*ctrl
)
776 int ret_of
, ret_acpi
, ret
;
778 /* Can't register until after driver model init */
779 if (WARN_ON(!is_registered
))
782 ret
= device_add(&ctrl
->dev
);
786 pm_runtime_enable(&ctrl
->dev
);
788 ret_of
= of_serdev_register_devices(ctrl
);
789 ret_acpi
= acpi_serdev_register_devices(ctrl
);
790 if (ret_of
&& ret_acpi
) {
791 dev_dbg(&ctrl
->dev
, "no devices registered: of:%pe acpi:%pe\n",
792 ERR_PTR(ret_of
), ERR_PTR(ret_acpi
));
794 goto err_rpm_disable
;
797 dev_dbg(&ctrl
->dev
, "serdev%d registered: dev:%p\n",
798 ctrl
->nr
, &ctrl
->dev
);
802 pm_runtime_disable(&ctrl
->dev
);
803 device_del(&ctrl
->dev
);
806 EXPORT_SYMBOL_GPL(serdev_controller_add
);
808 /* Remove a device associated with a controller */
809 static int serdev_remove_device(struct device
*dev
, void *data
)
811 struct serdev_device
*serdev
= to_serdev_device(dev
);
812 if (dev
->type
== &serdev_device_type
)
813 serdev_device_remove(serdev
);
818 * serdev_controller_remove(): remove an serdev controller
819 * @ctrl: controller to remove
821 * Remove a serdev controller. Caller is responsible for calling
822 * serdev_controller_put() to discard the allocated controller.
824 void serdev_controller_remove(struct serdev_controller
*ctrl
)
829 device_for_each_child(&ctrl
->dev
, NULL
, serdev_remove_device
);
830 pm_runtime_disable(&ctrl
->dev
);
831 device_del(&ctrl
->dev
);
833 EXPORT_SYMBOL_GPL(serdev_controller_remove
);
836 * __serdev_device_driver_register() - Register client driver with serdev core
837 * @sdrv: client driver to be associated with client-device.
838 * @owner: client driver owner to set.
840 * This API will register the client driver with the serdev framework.
841 * It is typically called from the driver's module-init function.
843 int __serdev_device_driver_register(struct serdev_device_driver
*sdrv
, struct module
*owner
)
845 sdrv
->driver
.bus
= &serdev_bus_type
;
846 sdrv
->driver
.owner
= owner
;
848 /* force drivers to async probe so I/O is possible in probe */
849 sdrv
->driver
.probe_type
= PROBE_PREFER_ASYNCHRONOUS
;
851 return driver_register(&sdrv
->driver
);
853 EXPORT_SYMBOL_GPL(__serdev_device_driver_register
);
855 static void __exit
serdev_exit(void)
857 bus_unregister(&serdev_bus_type
);
858 ida_destroy(&ctrl_ida
);
860 module_exit(serdev_exit
);
862 static int __init
serdev_init(void)
866 ret
= bus_register(&serdev_bus_type
);
870 is_registered
= true;
873 /* Must be before serial drivers register */
874 postcore_initcall(serdev_init
);
876 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
877 MODULE_LICENSE("GPL v2");
878 MODULE_DESCRIPTION("Serial attached device bus");