2 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
4 * Based on drivers/spmi/spmi.c:
5 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/errno.h>
18 #include <linux/idr.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/serdev.h>
24 #include <linux/slab.h>
26 static bool is_registered;
27 static DEFINE_IDA(ctrl_ida);
29 static void serdev_device_release(struct device *dev)
31 struct serdev_device *serdev = to_serdev_device(dev);
35 static const struct device_type serdev_device_type = {
36 .release = serdev_device_release,
39 static void serdev_ctrl_release(struct device *dev)
41 struct serdev_controller *ctrl = to_serdev_controller(dev);
42 ida_simple_remove(&ctrl_ida, ctrl->nr);
46 static const struct device_type serdev_ctrl_type = {
47 .release = serdev_ctrl_release,
50 static int serdev_device_match(struct device *dev, struct device_driver *drv)
52 /* TODO: ACPI and platform matching */
53 return of_driver_match_device(dev, drv);
56 static int serdev_uevent(struct device *dev, struct kobj_uevent_env *env)
58 /* TODO: ACPI and platform modalias */
59 return of_device_uevent_modalias(dev, env);
63 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
64 * @serdev: serdev_device to be added
66 int serdev_device_add(struct serdev_device *serdev)
68 struct device *parent = serdev->dev.parent;
71 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
73 err = device_add(&serdev->dev);
75 dev_err(&serdev->dev, "Can't add %s, status %d\n",
76 dev_name(&serdev->dev), err);
80 dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
85 EXPORT_SYMBOL_GPL(serdev_device_add);
88 * serdev_device_remove(): remove an serdev device
89 * @serdev: serdev_device to be removed
91 void serdev_device_remove(struct serdev_device *serdev)
93 device_unregister(&serdev->dev);
95 EXPORT_SYMBOL_GPL(serdev_device_remove);
97 int serdev_device_open(struct serdev_device *serdev)
99 struct serdev_controller *ctrl = serdev->ctrl;
101 if (!ctrl || !ctrl->ops->open)
104 return ctrl->ops->open(ctrl);
106 EXPORT_SYMBOL_GPL(serdev_device_open);
108 void serdev_device_close(struct serdev_device *serdev)
110 struct serdev_controller *ctrl = serdev->ctrl;
112 if (!ctrl || !ctrl->ops->close)
115 ctrl->ops->close(ctrl);
117 EXPORT_SYMBOL_GPL(serdev_device_close);
119 void serdev_device_write_wakeup(struct serdev_device *serdev)
121 complete(&serdev->write_comp);
123 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
125 int serdev_device_write_buf(struct serdev_device *serdev,
126 const unsigned char *buf, size_t count)
128 struct serdev_controller *ctrl = serdev->ctrl;
130 if (!ctrl || !ctrl->ops->write_buf)
133 return ctrl->ops->write_buf(ctrl, buf, count);
135 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
137 int serdev_device_write(struct serdev_device *serdev,
138 const unsigned char *buf, size_t count,
139 unsigned long timeout)
141 struct serdev_controller *ctrl = serdev->ctrl;
144 if (!ctrl || !ctrl->ops->write_buf ||
145 (timeout && !serdev->ops->write_wakeup))
148 mutex_lock(&serdev->write_lock);
150 reinit_completion(&serdev->write_comp);
152 ret = ctrl->ops->write_buf(ctrl, buf, count);
160 (timeout = wait_for_completion_timeout(&serdev->write_comp,
162 mutex_unlock(&serdev->write_lock);
163 return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
165 EXPORT_SYMBOL_GPL(serdev_device_write);
167 void serdev_device_write_flush(struct serdev_device *serdev)
169 struct serdev_controller *ctrl = serdev->ctrl;
171 if (!ctrl || !ctrl->ops->write_flush)
174 ctrl->ops->write_flush(ctrl);
176 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
178 int serdev_device_write_room(struct serdev_device *serdev)
180 struct serdev_controller *ctrl = serdev->ctrl;
182 if (!ctrl || !ctrl->ops->write_room)
185 return serdev->ctrl->ops->write_room(ctrl);
187 EXPORT_SYMBOL_GPL(serdev_device_write_room);
189 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
191 struct serdev_controller *ctrl = serdev->ctrl;
193 if (!ctrl || !ctrl->ops->set_baudrate)
196 return ctrl->ops->set_baudrate(ctrl, speed);
199 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
201 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
203 struct serdev_controller *ctrl = serdev->ctrl;
205 if (!ctrl || !ctrl->ops->set_flow_control)
208 ctrl->ops->set_flow_control(ctrl, enable);
210 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
212 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
214 struct serdev_controller *ctrl = serdev->ctrl;
216 if (!ctrl || !ctrl->ops->wait_until_sent)
219 ctrl->ops->wait_until_sent(ctrl, timeout);
221 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
223 int serdev_device_get_tiocm(struct serdev_device *serdev)
225 struct serdev_controller *ctrl = serdev->ctrl;
227 if (!ctrl || !ctrl->ops->get_tiocm)
230 return ctrl->ops->get_tiocm(ctrl);
232 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
234 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
236 struct serdev_controller *ctrl = serdev->ctrl;
238 if (!ctrl || !ctrl->ops->set_tiocm)
241 return ctrl->ops->set_tiocm(ctrl, set, clear);
243 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
245 static int serdev_drv_probe(struct device *dev)
247 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
249 return sdrv->probe(to_serdev_device(dev));
252 static int serdev_drv_remove(struct device *dev)
254 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
256 sdrv->remove(to_serdev_device(dev));
260 static ssize_t modalias_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
263 return of_device_modalias(dev, buf, PAGE_SIZE);
266 static struct device_attribute serdev_device_attrs[] = {
271 static struct bus_type serdev_bus_type = {
273 .match = serdev_device_match,
274 .probe = serdev_drv_probe,
275 .remove = serdev_drv_remove,
276 .uevent = serdev_uevent,
277 .dev_attrs = serdev_device_attrs,
281 * serdev_controller_alloc() - Allocate a new serdev device
282 * @ctrl: associated controller
284 * Caller is responsible for either calling serdev_device_add() to add the
285 * newly allocated controller, or calling serdev_device_put() to discard it.
287 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
289 struct serdev_device *serdev;
291 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
296 ctrl->serdev = serdev;
297 device_initialize(&serdev->dev);
298 serdev->dev.parent = &ctrl->dev;
299 serdev->dev.bus = &serdev_bus_type;
300 serdev->dev.type = &serdev_device_type;
301 init_completion(&serdev->write_comp);
302 mutex_init(&serdev->write_lock);
305 EXPORT_SYMBOL_GPL(serdev_device_alloc);
308 * serdev_controller_alloc() - Allocate a new serdev controller
309 * @parent: parent device
310 * @size: size of private data
312 * Caller is responsible for either calling serdev_controller_add() to add the
313 * newly allocated controller, or calling serdev_controller_put() to discard it.
314 * The allocated private data region may be accessed via
315 * serdev_controller_get_drvdata()
317 struct serdev_controller *serdev_controller_alloc(struct device *parent,
320 struct serdev_controller *ctrl;
323 if (WARN_ON(!parent))
326 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
330 device_initialize(&ctrl->dev);
331 ctrl->dev.type = &serdev_ctrl_type;
332 ctrl->dev.bus = &serdev_bus_type;
333 ctrl->dev.parent = parent;
334 ctrl->dev.of_node = parent->of_node;
335 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
337 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
340 "unable to allocate serdev controller identifier.\n");
341 serdev_controller_put(ctrl);
346 dev_set_name(&ctrl->dev, "serial%d", id);
348 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
351 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
353 static int of_serdev_register_devices(struct serdev_controller *ctrl)
355 struct device_node *node;
356 struct serdev_device *serdev = NULL;
360 for_each_available_child_of_node(ctrl->dev.of_node, node) {
361 if (!of_get_property(node, "compatible", NULL))
364 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
366 serdev = serdev_device_alloc(ctrl);
370 serdev->dev.of_node = node;
372 err = serdev_device_add(serdev);
374 dev_err(&serdev->dev,
375 "failure adding device. status %d\n", err);
376 serdev_device_put(serdev);
387 * serdev_controller_add() - Add an serdev controller
388 * @ctrl: controller to be registered.
390 * Register a controller previously allocated via serdev_controller_alloc() with
393 int serdev_controller_add(struct serdev_controller *ctrl)
397 /* Can't register until after driver model init */
398 if (WARN_ON(!is_registered))
401 ret = device_add(&ctrl->dev);
405 ret = of_serdev_register_devices(ctrl);
409 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
410 ctrl->nr, &ctrl->dev);
414 device_del(&ctrl->dev);
417 EXPORT_SYMBOL_GPL(serdev_controller_add);
419 /* Remove a device associated with a controller */
420 static int serdev_remove_device(struct device *dev, void *data)
422 struct serdev_device *serdev = to_serdev_device(dev);
423 if (dev->type == &serdev_device_type)
424 serdev_device_remove(serdev);
429 * serdev_controller_remove(): remove an serdev controller
430 * @ctrl: controller to remove
432 * Remove a serdev controller. Caller is responsible for calling
433 * serdev_controller_put() to discard the allocated controller.
435 void serdev_controller_remove(struct serdev_controller *ctrl)
442 dummy = device_for_each_child(&ctrl->dev, NULL,
443 serdev_remove_device);
444 device_del(&ctrl->dev);
446 EXPORT_SYMBOL_GPL(serdev_controller_remove);
449 * serdev_driver_register() - Register client driver with serdev core
450 * @sdrv: client driver to be associated with client-device.
452 * This API will register the client driver with the serdev framework.
453 * It is typically called from the driver's module-init function.
455 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
457 sdrv->driver.bus = &serdev_bus_type;
458 sdrv->driver.owner = owner;
460 /* force drivers to async probe so I/O is possible in probe */
461 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
463 return driver_register(&sdrv->driver);
465 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
467 static void __exit serdev_exit(void)
469 bus_unregister(&serdev_bus_type);
471 module_exit(serdev_exit);
473 static int __init serdev_init(void)
477 ret = bus_register(&serdev_bus_type);
481 is_registered = true;
484 /* Must be before serial drivers register */
485 postcore_initcall(serdev_init);
487 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
488 MODULE_LICENSE("GPL v2");
489 MODULE_DESCRIPTION("Serial attached device bus");