blob: 531aa89ff243d2abd5e66b3b28cc4c505c40bdb6 [file] [log] [blame]
Rob Herringcd6484e2017-02-02 13:48:07 -06001/*
2 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
3 *
4 * Based on drivers/spmi/spmi.c:
5 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
6 *
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.
10 *
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.
15 */
16
17#include <linux/errno.h>
18#include <linux/idr.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/serdev.h>
24#include <linux/slab.h>
25
26static bool is_registered;
27static DEFINE_IDA(ctrl_ida);
28
29static void serdev_device_release(struct device *dev)
30{
31 struct serdev_device *serdev = to_serdev_device(dev);
32 kfree(serdev);
33}
34
35static const struct device_type serdev_device_type = {
36 .release = serdev_device_release,
37};
38
39static void serdev_ctrl_release(struct device *dev)
40{
41 struct serdev_controller *ctrl = to_serdev_controller(dev);
42 ida_simple_remove(&ctrl_ida, ctrl->nr);
43 kfree(ctrl);
44}
45
46static const struct device_type serdev_ctrl_type = {
47 .release = serdev_ctrl_release,
48};
49
50static int serdev_device_match(struct device *dev, struct device_driver *drv)
51{
52 /* TODO: ACPI and platform matching */
53 return of_driver_match_device(dev, drv);
54}
55
56static int serdev_uevent(struct device *dev, struct kobj_uevent_env *env)
57{
58 /* TODO: ACPI and platform modalias */
59 return of_device_uevent_modalias(dev, env);
60}
61
62/**
63 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
64 * @serdev: serdev_device to be added
65 */
66int serdev_device_add(struct serdev_device *serdev)
67{
68 struct device *parent = serdev->dev.parent;
69 int err;
70
71 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
72
73 err = device_add(&serdev->dev);
74 if (err < 0) {
75 dev_err(&serdev->dev, "Can't add %s, status %d\n",
76 dev_name(&serdev->dev), err);
77 goto err_device_add;
78 }
79
80 dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
81
82err_device_add:
83 return err;
84}
85EXPORT_SYMBOL_GPL(serdev_device_add);
86
87/**
88 * serdev_device_remove(): remove an serdev device
89 * @serdev: serdev_device to be removed
90 */
91void serdev_device_remove(struct serdev_device *serdev)
92{
93 device_unregister(&serdev->dev);
94}
95EXPORT_SYMBOL_GPL(serdev_device_remove);
96
97int serdev_device_open(struct serdev_device *serdev)
98{
99 struct serdev_controller *ctrl = serdev->ctrl;
100
101 if (!ctrl || !ctrl->ops->open)
102 return -EINVAL;
103
104 return ctrl->ops->open(ctrl);
105}
106EXPORT_SYMBOL_GPL(serdev_device_open);
107
108void serdev_device_close(struct serdev_device *serdev)
109{
110 struct serdev_controller *ctrl = serdev->ctrl;
111
112 if (!ctrl || !ctrl->ops->close)
113 return;
114
115 ctrl->ops->close(ctrl);
116}
117EXPORT_SYMBOL_GPL(serdev_device_close);
118
119int serdev_device_write_buf(struct serdev_device *serdev,
120 const unsigned char *buf, size_t count)
121{
122 struct serdev_controller *ctrl = serdev->ctrl;
123
124 if (!ctrl || !ctrl->ops->write_buf)
125 return -EINVAL;
126
127 return ctrl->ops->write_buf(ctrl, buf, count);
128}
129EXPORT_SYMBOL_GPL(serdev_device_write_buf);
130
131void serdev_device_write_flush(struct serdev_device *serdev)
132{
133 struct serdev_controller *ctrl = serdev->ctrl;
134
135 if (!ctrl || !ctrl->ops->write_flush)
136 return;
137
138 ctrl->ops->write_flush(ctrl);
139}
140EXPORT_SYMBOL_GPL(serdev_device_write_flush);
141
142int serdev_device_write_room(struct serdev_device *serdev)
143{
144 struct serdev_controller *ctrl = serdev->ctrl;
145
146 if (!ctrl || !ctrl->ops->write_room)
147 return 0;
148
149 return serdev->ctrl->ops->write_room(ctrl);
150}
151EXPORT_SYMBOL_GPL(serdev_device_write_room);
152
153unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
154{
155 struct serdev_controller *ctrl = serdev->ctrl;
156
157 if (!ctrl || !ctrl->ops->set_baudrate)
158 return 0;
159
160 return ctrl->ops->set_baudrate(ctrl, speed);
161
162}
163EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
164
165void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
166{
167 struct serdev_controller *ctrl = serdev->ctrl;
168
169 if (!ctrl || !ctrl->ops->set_flow_control)
170 return;
171
172 ctrl->ops->set_flow_control(ctrl, enable);
173}
174EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
175
176static int serdev_drv_probe(struct device *dev)
177{
178 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
179
180 return sdrv->probe(to_serdev_device(dev));
181}
182
183static int serdev_drv_remove(struct device *dev)
184{
185 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
186
187 sdrv->remove(to_serdev_device(dev));
188 return 0;
189}
190
191static ssize_t modalias_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
193{
Rob Herring0634c292017-03-22 09:16:27 -0500194 return of_device_modalias(dev, buf, PAGE_SIZE);
Rob Herringcd6484e2017-02-02 13:48:07 -0600195}
196
197static struct device_attribute serdev_device_attrs[] = {
198 __ATTR_RO(modalias),
199 __ATTR_NULL
200};
201
202static struct bus_type serdev_bus_type = {
203 .name = "serial",
204 .match = serdev_device_match,
205 .probe = serdev_drv_probe,
206 .remove = serdev_drv_remove,
207 .uevent = serdev_uevent,
208 .dev_attrs = serdev_device_attrs,
209};
210
211/**
212 * serdev_controller_alloc() - Allocate a new serdev device
213 * @ctrl: associated controller
214 *
215 * Caller is responsible for either calling serdev_device_add() to add the
216 * newly allocated controller, or calling serdev_device_put() to discard it.
217 */
218struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
219{
220 struct serdev_device *serdev;
221
222 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
223 if (!serdev)
224 return NULL;
225
226 serdev->ctrl = ctrl;
227 ctrl->serdev = serdev;
228 device_initialize(&serdev->dev);
229 serdev->dev.parent = &ctrl->dev;
230 serdev->dev.bus = &serdev_bus_type;
231 serdev->dev.type = &serdev_device_type;
232 return serdev;
233}
234EXPORT_SYMBOL_GPL(serdev_device_alloc);
235
236/**
237 * serdev_controller_alloc() - Allocate a new serdev controller
238 * @parent: parent device
239 * @size: size of private data
240 *
241 * Caller is responsible for either calling serdev_controller_add() to add the
242 * newly allocated controller, or calling serdev_controller_put() to discard it.
243 * The allocated private data region may be accessed via
244 * serdev_controller_get_drvdata()
245 */
246struct serdev_controller *serdev_controller_alloc(struct device *parent,
247 size_t size)
248{
249 struct serdev_controller *ctrl;
250 int id;
251
252 if (WARN_ON(!parent))
253 return NULL;
254
255 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
256 if (!ctrl)
257 return NULL;
258
259 device_initialize(&ctrl->dev);
260 ctrl->dev.type = &serdev_ctrl_type;
261 ctrl->dev.bus = &serdev_bus_type;
262 ctrl->dev.parent = parent;
263 ctrl->dev.of_node = parent->of_node;
264 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
265
266 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
267 if (id < 0) {
268 dev_err(parent,
269 "unable to allocate serdev controller identifier.\n");
270 serdev_controller_put(ctrl);
271 return NULL;
272 }
273
274 ctrl->nr = id;
275 dev_set_name(&ctrl->dev, "serial%d", id);
276
277 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
278 return ctrl;
279}
280EXPORT_SYMBOL_GPL(serdev_controller_alloc);
281
282static int of_serdev_register_devices(struct serdev_controller *ctrl)
283{
284 struct device_node *node;
285 struct serdev_device *serdev = NULL;
286 int err;
287 bool found = false;
288
289 for_each_available_child_of_node(ctrl->dev.of_node, node) {
290 if (!of_get_property(node, "compatible", NULL))
291 continue;
292
293 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
294
295 serdev = serdev_device_alloc(ctrl);
296 if (!serdev)
297 continue;
298
299 serdev->dev.of_node = node;
300
301 err = serdev_device_add(serdev);
302 if (err) {
303 dev_err(&serdev->dev,
304 "failure adding device. status %d\n", err);
305 serdev_device_put(serdev);
306 } else
307 found = true;
308 }
309 if (!found)
310 return -ENODEV;
311
312 return 0;
313}
314
315/**
316 * serdev_controller_add() - Add an serdev controller
317 * @ctrl: controller to be registered.
318 *
319 * Register a controller previously allocated via serdev_controller_alloc() with
320 * the serdev core.
321 */
322int serdev_controller_add(struct serdev_controller *ctrl)
323{
324 int ret;
325
326 /* Can't register until after driver model init */
327 if (WARN_ON(!is_registered))
328 return -EAGAIN;
329
330 ret = device_add(&ctrl->dev);
331 if (ret)
332 return ret;
333
334 ret = of_serdev_register_devices(ctrl);
335 if (ret)
336 goto out_dev_del;
337
338 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
339 ctrl->nr, &ctrl->dev);
340 return 0;
341
342out_dev_del:
343 device_del(&ctrl->dev);
344 return ret;
345};
346EXPORT_SYMBOL_GPL(serdev_controller_add);
347
348/* Remove a device associated with a controller */
349static int serdev_remove_device(struct device *dev, void *data)
350{
351 struct serdev_device *serdev = to_serdev_device(dev);
352 if (dev->type == &serdev_device_type)
353 serdev_device_remove(serdev);
354 return 0;
355}
356
357/**
358 * serdev_controller_remove(): remove an serdev controller
359 * @ctrl: controller to remove
360 *
361 * Remove a serdev controller. Caller is responsible for calling
362 * serdev_controller_put() to discard the allocated controller.
363 */
364void serdev_controller_remove(struct serdev_controller *ctrl)
365{
366 int dummy;
367
368 if (!ctrl)
369 return;
370
371 dummy = device_for_each_child(&ctrl->dev, NULL,
372 serdev_remove_device);
373 device_del(&ctrl->dev);
374}
375EXPORT_SYMBOL_GPL(serdev_controller_remove);
376
377/**
378 * serdev_driver_register() - Register client driver with serdev core
379 * @sdrv: client driver to be associated with client-device.
380 *
381 * This API will register the client driver with the serdev framework.
382 * It is typically called from the driver's module-init function.
383 */
384int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
385{
386 sdrv->driver.bus = &serdev_bus_type;
387 sdrv->driver.owner = owner;
388
389 /* force drivers to async probe so I/O is possible in probe */
390 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
391
392 return driver_register(&sdrv->driver);
393}
394EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
395
396static void __exit serdev_exit(void)
397{
398 bus_unregister(&serdev_bus_type);
399}
400module_exit(serdev_exit);
401
402static int __init serdev_init(void)
403{
404 int ret;
405
406 ret = bus_register(&serdev_bus_type);
407 if (ret)
408 return ret;
409
410 is_registered = true;
411 return 0;
412}
413/* Must be before serial drivers register */
414postcore_initcall(serdev_init);
415
416MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
417MODULE_LICENSE("GPL v2");
418MODULE_DESCRIPTION("Serial attached device bus");