mei: me: add cannon point device ids for 4th device
[linux/fpc-iii.git] / drivers / tty / serdev / core.c
blob1bef39828ca760ab69d08107cf980ab61ef7cc71
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
7 */
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>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/serdev.h>
17 #include <linux/slab.h>
19 static bool is_registered;
20 static DEFINE_IDA(ctrl_ida);
22 static void serdev_device_release(struct device *dev)
24 struct serdev_device *serdev = to_serdev_device(dev);
25 kfree(serdev);
28 static const struct device_type serdev_device_type = {
29 .release = serdev_device_release,
32 static void serdev_ctrl_release(struct device *dev)
34 struct serdev_controller *ctrl = to_serdev_controller(dev);
35 ida_simple_remove(&ctrl_ida, ctrl->nr);
36 kfree(ctrl);
39 static const struct device_type serdev_ctrl_type = {
40 .release = serdev_ctrl_release,
43 static int serdev_device_match(struct device *dev, struct device_driver *drv)
45 /* TODO: platform matching */
46 if (acpi_driver_match_device(dev, drv))
47 return 1;
49 return of_driver_match_device(dev, drv);
52 static int serdev_uevent(struct device *dev, struct kobj_uevent_env *env)
54 int rc;
56 /* TODO: platform modalias */
57 rc = acpi_device_uevent_modalias(dev, env);
58 if (rc != -ENODEV)
59 return rc;
61 return of_device_uevent_modalias(dev, env);
64 /**
65 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
66 * @serdev: serdev_device to be added
68 int serdev_device_add(struct serdev_device *serdev)
70 struct serdev_controller *ctrl = serdev->ctrl;
71 struct device *parent = serdev->dev.parent;
72 int err;
74 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
76 /* Only a single slave device is currently supported. */
77 if (ctrl->serdev) {
78 dev_err(&serdev->dev, "controller busy\n");
79 return -EBUSY;
81 ctrl->serdev = serdev;
83 err = device_add(&serdev->dev);
84 if (err < 0) {
85 dev_err(&serdev->dev, "Can't add %s, status %d\n",
86 dev_name(&serdev->dev), err);
87 goto err_clear_serdev;
90 dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
92 return 0;
94 err_clear_serdev:
95 ctrl->serdev = NULL;
96 return err;
98 EXPORT_SYMBOL_GPL(serdev_device_add);
101 * serdev_device_remove(): remove an serdev device
102 * @serdev: serdev_device to be removed
104 void serdev_device_remove(struct serdev_device *serdev)
106 struct serdev_controller *ctrl = serdev->ctrl;
108 device_unregister(&serdev->dev);
109 ctrl->serdev = NULL;
111 EXPORT_SYMBOL_GPL(serdev_device_remove);
113 int serdev_device_open(struct serdev_device *serdev)
115 struct serdev_controller *ctrl = serdev->ctrl;
117 if (!ctrl || !ctrl->ops->open)
118 return -EINVAL;
120 return ctrl->ops->open(ctrl);
122 EXPORT_SYMBOL_GPL(serdev_device_open);
124 void serdev_device_close(struct serdev_device *serdev)
126 struct serdev_controller *ctrl = serdev->ctrl;
128 if (!ctrl || !ctrl->ops->close)
129 return;
131 ctrl->ops->close(ctrl);
133 EXPORT_SYMBOL_GPL(serdev_device_close);
135 void serdev_device_write_wakeup(struct serdev_device *serdev)
137 complete(&serdev->write_comp);
139 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
141 int serdev_device_write_buf(struct serdev_device *serdev,
142 const unsigned char *buf, size_t count)
144 struct serdev_controller *ctrl = serdev->ctrl;
146 if (!ctrl || !ctrl->ops->write_buf)
147 return -EINVAL;
149 return ctrl->ops->write_buf(ctrl, buf, count);
151 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
153 int serdev_device_write(struct serdev_device *serdev,
154 const unsigned char *buf, size_t count,
155 unsigned long timeout)
157 struct serdev_controller *ctrl = serdev->ctrl;
158 int ret;
160 if (!ctrl || !ctrl->ops->write_buf ||
161 (timeout && !serdev->ops->write_wakeup))
162 return -EINVAL;
164 mutex_lock(&serdev->write_lock);
165 do {
166 reinit_completion(&serdev->write_comp);
168 ret = ctrl->ops->write_buf(ctrl, buf, count);
169 if (ret < 0)
170 break;
172 buf += ret;
173 count -= ret;
175 } while (count &&
176 (timeout = wait_for_completion_timeout(&serdev->write_comp,
177 timeout)));
178 mutex_unlock(&serdev->write_lock);
179 return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
181 EXPORT_SYMBOL_GPL(serdev_device_write);
183 void serdev_device_write_flush(struct serdev_device *serdev)
185 struct serdev_controller *ctrl = serdev->ctrl;
187 if (!ctrl || !ctrl->ops->write_flush)
188 return;
190 ctrl->ops->write_flush(ctrl);
192 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
194 int serdev_device_write_room(struct serdev_device *serdev)
196 struct serdev_controller *ctrl = serdev->ctrl;
198 if (!ctrl || !ctrl->ops->write_room)
199 return 0;
201 return serdev->ctrl->ops->write_room(ctrl);
203 EXPORT_SYMBOL_GPL(serdev_device_write_room);
205 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
207 struct serdev_controller *ctrl = serdev->ctrl;
209 if (!ctrl || !ctrl->ops->set_baudrate)
210 return 0;
212 return ctrl->ops->set_baudrate(ctrl, speed);
215 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
217 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
219 struct serdev_controller *ctrl = serdev->ctrl;
221 if (!ctrl || !ctrl->ops->set_flow_control)
222 return;
224 ctrl->ops->set_flow_control(ctrl, enable);
226 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
228 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
230 struct serdev_controller *ctrl = serdev->ctrl;
232 if (!ctrl || !ctrl->ops->wait_until_sent)
233 return;
235 ctrl->ops->wait_until_sent(ctrl, timeout);
237 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
239 int serdev_device_get_tiocm(struct serdev_device *serdev)
241 struct serdev_controller *ctrl = serdev->ctrl;
243 if (!ctrl || !ctrl->ops->get_tiocm)
244 return -ENOTSUPP;
246 return ctrl->ops->get_tiocm(ctrl);
248 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
250 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
252 struct serdev_controller *ctrl = serdev->ctrl;
254 if (!ctrl || !ctrl->ops->set_tiocm)
255 return -ENOTSUPP;
257 return ctrl->ops->set_tiocm(ctrl, set, clear);
259 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
261 static int serdev_drv_probe(struct device *dev)
263 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
265 return sdrv->probe(to_serdev_device(dev));
268 static int serdev_drv_remove(struct device *dev)
270 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
272 sdrv->remove(to_serdev_device(dev));
273 return 0;
276 static ssize_t modalias_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
279 int len;
281 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
282 if (len != -ENODEV)
283 return len;
285 return of_device_modalias(dev, buf, PAGE_SIZE);
287 DEVICE_ATTR_RO(modalias);
289 static struct attribute *serdev_device_attrs[] = {
290 &dev_attr_modalias.attr,
291 NULL,
293 ATTRIBUTE_GROUPS(serdev_device);
295 static struct bus_type serdev_bus_type = {
296 .name = "serial",
297 .match = serdev_device_match,
298 .probe = serdev_drv_probe,
299 .remove = serdev_drv_remove,
300 .uevent = serdev_uevent,
301 .dev_groups = serdev_device_groups,
305 * serdev_controller_alloc() - Allocate a new serdev device
306 * @ctrl: associated controller
308 * Caller is responsible for either calling serdev_device_add() to add the
309 * newly allocated controller, or calling serdev_device_put() to discard it.
311 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
313 struct serdev_device *serdev;
315 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
316 if (!serdev)
317 return NULL;
319 serdev->ctrl = ctrl;
320 device_initialize(&serdev->dev);
321 serdev->dev.parent = &ctrl->dev;
322 serdev->dev.bus = &serdev_bus_type;
323 serdev->dev.type = &serdev_device_type;
324 init_completion(&serdev->write_comp);
325 mutex_init(&serdev->write_lock);
326 return serdev;
328 EXPORT_SYMBOL_GPL(serdev_device_alloc);
331 * serdev_controller_alloc() - Allocate a new serdev controller
332 * @parent: parent device
333 * @size: size of private data
335 * Caller is responsible for either calling serdev_controller_add() to add the
336 * newly allocated controller, or calling serdev_controller_put() to discard it.
337 * The allocated private data region may be accessed via
338 * serdev_controller_get_drvdata()
340 struct serdev_controller *serdev_controller_alloc(struct device *parent,
341 size_t size)
343 struct serdev_controller *ctrl;
344 int id;
346 if (WARN_ON(!parent))
347 return NULL;
349 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
350 if (!ctrl)
351 return NULL;
353 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
354 if (id < 0) {
355 dev_err(parent,
356 "unable to allocate serdev controller identifier.\n");
357 goto err_free;
360 ctrl->nr = id;
362 device_initialize(&ctrl->dev);
363 ctrl->dev.type = &serdev_ctrl_type;
364 ctrl->dev.bus = &serdev_bus_type;
365 ctrl->dev.parent = parent;
366 ctrl->dev.of_node = parent->of_node;
367 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
369 dev_set_name(&ctrl->dev, "serial%d", id);
371 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
372 return ctrl;
374 err_free:
375 kfree(ctrl);
377 return NULL;
379 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
381 static int of_serdev_register_devices(struct serdev_controller *ctrl)
383 struct device_node *node;
384 struct serdev_device *serdev = NULL;
385 int err;
386 bool found = false;
388 for_each_available_child_of_node(ctrl->dev.of_node, node) {
389 if (!of_get_property(node, "compatible", NULL))
390 continue;
392 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
394 serdev = serdev_device_alloc(ctrl);
395 if (!serdev)
396 continue;
398 serdev->dev.of_node = node;
400 err = serdev_device_add(serdev);
401 if (err) {
402 dev_err(&serdev->dev,
403 "failure adding device. status %d\n", err);
404 serdev_device_put(serdev);
405 } else
406 found = true;
408 if (!found)
409 return -ENODEV;
411 return 0;
414 #ifdef CONFIG_ACPI
415 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
416 struct acpi_device *adev)
418 struct serdev_device *serdev = NULL;
419 int err;
421 if (acpi_bus_get_status(adev) || !adev->status.present ||
422 acpi_device_enumerated(adev))
423 return AE_OK;
425 serdev = serdev_device_alloc(ctrl);
426 if (!serdev) {
427 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
428 dev_name(&adev->dev));
429 return AE_NO_MEMORY;
432 ACPI_COMPANION_SET(&serdev->dev, adev);
433 acpi_device_set_enumerated(adev);
435 err = serdev_device_add(serdev);
436 if (err) {
437 dev_err(&serdev->dev,
438 "failure adding ACPI serdev device. status %d\n", err);
439 serdev_device_put(serdev);
442 return AE_OK;
445 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
446 void *data, void **return_value)
448 struct serdev_controller *ctrl = data;
449 struct acpi_device *adev;
451 if (acpi_bus_get_device(handle, &adev))
452 return AE_OK;
454 return acpi_serdev_register_device(ctrl, adev);
457 static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
459 acpi_status status;
460 acpi_handle handle;
462 handle = ACPI_HANDLE(ctrl->dev.parent);
463 if (!handle)
464 return -ENODEV;
466 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
467 acpi_serdev_add_device, NULL, ctrl, NULL);
468 if (ACPI_FAILURE(status))
469 dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
471 if (!ctrl->serdev)
472 return -ENODEV;
474 return 0;
476 #else
477 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
479 return -ENODEV;
481 #endif /* CONFIG_ACPI */
484 * serdev_controller_add() - Add an serdev controller
485 * @ctrl: controller to be registered.
487 * Register a controller previously allocated via serdev_controller_alloc() with
488 * the serdev core.
490 int serdev_controller_add(struct serdev_controller *ctrl)
492 int ret_of, ret_acpi, ret;
494 /* Can't register until after driver model init */
495 if (WARN_ON(!is_registered))
496 return -EAGAIN;
498 ret = device_add(&ctrl->dev);
499 if (ret)
500 return ret;
502 ret_of = of_serdev_register_devices(ctrl);
503 ret_acpi = acpi_serdev_register_devices(ctrl);
504 if (ret_of && ret_acpi) {
505 dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n",
506 ret_of, ret_acpi);
507 ret = -ENODEV;
508 goto out_dev_del;
511 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
512 ctrl->nr, &ctrl->dev);
513 return 0;
515 out_dev_del:
516 device_del(&ctrl->dev);
517 return ret;
519 EXPORT_SYMBOL_GPL(serdev_controller_add);
521 /* Remove a device associated with a controller */
522 static int serdev_remove_device(struct device *dev, void *data)
524 struct serdev_device *serdev = to_serdev_device(dev);
525 if (dev->type == &serdev_device_type)
526 serdev_device_remove(serdev);
527 return 0;
531 * serdev_controller_remove(): remove an serdev controller
532 * @ctrl: controller to remove
534 * Remove a serdev controller. Caller is responsible for calling
535 * serdev_controller_put() to discard the allocated controller.
537 void serdev_controller_remove(struct serdev_controller *ctrl)
539 int dummy;
541 if (!ctrl)
542 return;
544 dummy = device_for_each_child(&ctrl->dev, NULL,
545 serdev_remove_device);
546 device_del(&ctrl->dev);
548 EXPORT_SYMBOL_GPL(serdev_controller_remove);
551 * serdev_driver_register() - Register client driver with serdev core
552 * @sdrv: client driver to be associated with client-device.
554 * This API will register the client driver with the serdev framework.
555 * It is typically called from the driver's module-init function.
557 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
559 sdrv->driver.bus = &serdev_bus_type;
560 sdrv->driver.owner = owner;
562 /* force drivers to async probe so I/O is possible in probe */
563 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
565 return driver_register(&sdrv->driver);
567 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
569 static void __exit serdev_exit(void)
571 bus_unregister(&serdev_bus_type);
573 module_exit(serdev_exit);
575 static int __init serdev_init(void)
577 int ret;
579 ret = bus_register(&serdev_bus_type);
580 if (ret)
581 return ret;
583 is_registered = true;
584 return 0;
586 /* Must be before serial drivers register */
587 postcore_initcall(serdev_init);
589 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
590 MODULE_LICENSE("GPL v2");
591 MODULE_DESCRIPTION("Serial attached device bus");