at v4.12 489 lines 12 kB view raw
1/* 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 119void serdev_device_write_wakeup(struct serdev_device *serdev) 120{ 121 complete(&serdev->write_comp); 122} 123EXPORT_SYMBOL_GPL(serdev_device_write_wakeup); 124 125int serdev_device_write_buf(struct serdev_device *serdev, 126 const unsigned char *buf, size_t count) 127{ 128 struct serdev_controller *ctrl = serdev->ctrl; 129 130 if (!ctrl || !ctrl->ops->write_buf) 131 return -EINVAL; 132 133 return ctrl->ops->write_buf(ctrl, buf, count); 134} 135EXPORT_SYMBOL_GPL(serdev_device_write_buf); 136 137int serdev_device_write(struct serdev_device *serdev, 138 const unsigned char *buf, size_t count, 139 unsigned long timeout) 140{ 141 struct serdev_controller *ctrl = serdev->ctrl; 142 int ret; 143 144 if (!ctrl || !ctrl->ops->write_buf || 145 (timeout && !serdev->ops->write_wakeup)) 146 return -EINVAL; 147 148 mutex_lock(&serdev->write_lock); 149 do { 150 reinit_completion(&serdev->write_comp); 151 152 ret = ctrl->ops->write_buf(ctrl, buf, count); 153 if (ret < 0) 154 break; 155 156 buf += ret; 157 count -= ret; 158 159 } while (count && 160 (timeout = wait_for_completion_timeout(&serdev->write_comp, 161 timeout))); 162 mutex_unlock(&serdev->write_lock); 163 return ret < 0 ? ret : (count ? -ETIMEDOUT : 0); 164} 165EXPORT_SYMBOL_GPL(serdev_device_write); 166 167void serdev_device_write_flush(struct serdev_device *serdev) 168{ 169 struct serdev_controller *ctrl = serdev->ctrl; 170 171 if (!ctrl || !ctrl->ops->write_flush) 172 return; 173 174 ctrl->ops->write_flush(ctrl); 175} 176EXPORT_SYMBOL_GPL(serdev_device_write_flush); 177 178int serdev_device_write_room(struct serdev_device *serdev) 179{ 180 struct serdev_controller *ctrl = serdev->ctrl; 181 182 if (!ctrl || !ctrl->ops->write_room) 183 return 0; 184 185 return serdev->ctrl->ops->write_room(ctrl); 186} 187EXPORT_SYMBOL_GPL(serdev_device_write_room); 188 189unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed) 190{ 191 struct serdev_controller *ctrl = serdev->ctrl; 192 193 if (!ctrl || !ctrl->ops->set_baudrate) 194 return 0; 195 196 return ctrl->ops->set_baudrate(ctrl, speed); 197 198} 199EXPORT_SYMBOL_GPL(serdev_device_set_baudrate); 200 201void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable) 202{ 203 struct serdev_controller *ctrl = serdev->ctrl; 204 205 if (!ctrl || !ctrl->ops->set_flow_control) 206 return; 207 208 ctrl->ops->set_flow_control(ctrl, enable); 209} 210EXPORT_SYMBOL_GPL(serdev_device_set_flow_control); 211 212void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout) 213{ 214 struct serdev_controller *ctrl = serdev->ctrl; 215 216 if (!ctrl || !ctrl->ops->wait_until_sent) 217 return; 218 219 ctrl->ops->wait_until_sent(ctrl, timeout); 220} 221EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent); 222 223int serdev_device_get_tiocm(struct serdev_device *serdev) 224{ 225 struct serdev_controller *ctrl = serdev->ctrl; 226 227 if (!ctrl || !ctrl->ops->get_tiocm) 228 return -ENOTSUPP; 229 230 return ctrl->ops->get_tiocm(ctrl); 231} 232EXPORT_SYMBOL_GPL(serdev_device_get_tiocm); 233 234int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 235{ 236 struct serdev_controller *ctrl = serdev->ctrl; 237 238 if (!ctrl || !ctrl->ops->set_tiocm) 239 return -ENOTSUPP; 240 241 return ctrl->ops->set_tiocm(ctrl, set, clear); 242} 243EXPORT_SYMBOL_GPL(serdev_device_set_tiocm); 244 245static int serdev_drv_probe(struct device *dev) 246{ 247 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); 248 249 return sdrv->probe(to_serdev_device(dev)); 250} 251 252static int serdev_drv_remove(struct device *dev) 253{ 254 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); 255 256 sdrv->remove(to_serdev_device(dev)); 257 return 0; 258} 259 260static ssize_t modalias_show(struct device *dev, 261 struct device_attribute *attr, char *buf) 262{ 263 return of_device_modalias(dev, buf, PAGE_SIZE); 264} 265 266static struct device_attribute serdev_device_attrs[] = { 267 __ATTR_RO(modalias), 268 __ATTR_NULL 269}; 270 271static struct bus_type serdev_bus_type = { 272 .name = "serial", 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, 278}; 279 280/** 281 * serdev_controller_alloc() - Allocate a new serdev device 282 * @ctrl: associated controller 283 * 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. 286 */ 287struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl) 288{ 289 struct serdev_device *serdev; 290 291 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL); 292 if (!serdev) 293 return NULL; 294 295 serdev->ctrl = ctrl; 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); 303 return serdev; 304} 305EXPORT_SYMBOL_GPL(serdev_device_alloc); 306 307/** 308 * serdev_controller_alloc() - Allocate a new serdev controller 309 * @parent: parent device 310 * @size: size of private data 311 * 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() 316 */ 317struct serdev_controller *serdev_controller_alloc(struct device *parent, 318 size_t size) 319{ 320 struct serdev_controller *ctrl; 321 int id; 322 323 if (WARN_ON(!parent)) 324 return NULL; 325 326 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL); 327 if (!ctrl) 328 return NULL; 329 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]); 336 337 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL); 338 if (id < 0) { 339 dev_err(parent, 340 "unable to allocate serdev controller identifier.\n"); 341 serdev_controller_put(ctrl); 342 return NULL; 343 } 344 345 ctrl->nr = id; 346 dev_set_name(&ctrl->dev, "serial%d", id); 347 348 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id); 349 return ctrl; 350} 351EXPORT_SYMBOL_GPL(serdev_controller_alloc); 352 353static int of_serdev_register_devices(struct serdev_controller *ctrl) 354{ 355 struct device_node *node; 356 struct serdev_device *serdev = NULL; 357 int err; 358 bool found = false; 359 360 for_each_available_child_of_node(ctrl->dev.of_node, node) { 361 if (!of_get_property(node, "compatible", NULL)) 362 continue; 363 364 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name); 365 366 serdev = serdev_device_alloc(ctrl); 367 if (!serdev) 368 continue; 369 370 serdev->dev.of_node = node; 371 372 err = serdev_device_add(serdev); 373 if (err) { 374 dev_err(&serdev->dev, 375 "failure adding device. status %d\n", err); 376 serdev_device_put(serdev); 377 } else 378 found = true; 379 } 380 if (!found) 381 return -ENODEV; 382 383 return 0; 384} 385 386/** 387 * serdev_controller_add() - Add an serdev controller 388 * @ctrl: controller to be registered. 389 * 390 * Register a controller previously allocated via serdev_controller_alloc() with 391 * the serdev core. 392 */ 393int serdev_controller_add(struct serdev_controller *ctrl) 394{ 395 int ret; 396 397 /* Can't register until after driver model init */ 398 if (WARN_ON(!is_registered)) 399 return -EAGAIN; 400 401 ret = device_add(&ctrl->dev); 402 if (ret) 403 return ret; 404 405 ret = of_serdev_register_devices(ctrl); 406 if (ret) 407 goto out_dev_del; 408 409 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n", 410 ctrl->nr, &ctrl->dev); 411 return 0; 412 413out_dev_del: 414 device_del(&ctrl->dev); 415 return ret; 416}; 417EXPORT_SYMBOL_GPL(serdev_controller_add); 418 419/* Remove a device associated with a controller */ 420static int serdev_remove_device(struct device *dev, void *data) 421{ 422 struct serdev_device *serdev = to_serdev_device(dev); 423 if (dev->type == &serdev_device_type) 424 serdev_device_remove(serdev); 425 return 0; 426} 427 428/** 429 * serdev_controller_remove(): remove an serdev controller 430 * @ctrl: controller to remove 431 * 432 * Remove a serdev controller. Caller is responsible for calling 433 * serdev_controller_put() to discard the allocated controller. 434 */ 435void serdev_controller_remove(struct serdev_controller *ctrl) 436{ 437 int dummy; 438 439 if (!ctrl) 440 return; 441 442 dummy = device_for_each_child(&ctrl->dev, NULL, 443 serdev_remove_device); 444 device_del(&ctrl->dev); 445} 446EXPORT_SYMBOL_GPL(serdev_controller_remove); 447 448/** 449 * serdev_driver_register() - Register client driver with serdev core 450 * @sdrv: client driver to be associated with client-device. 451 * 452 * This API will register the client driver with the serdev framework. 453 * It is typically called from the driver's module-init function. 454 */ 455int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner) 456{ 457 sdrv->driver.bus = &serdev_bus_type; 458 sdrv->driver.owner = owner; 459 460 /* force drivers to async probe so I/O is possible in probe */ 461 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; 462 463 return driver_register(&sdrv->driver); 464} 465EXPORT_SYMBOL_GPL(__serdev_device_driver_register); 466 467static void __exit serdev_exit(void) 468{ 469 bus_unregister(&serdev_bus_type); 470} 471module_exit(serdev_exit); 472 473static int __init serdev_init(void) 474{ 475 int ret; 476 477 ret = bus_register(&serdev_bus_type); 478 if (ret) 479 return ret; 480 481 is_registered = true; 482 return 0; 483} 484/* Must be before serial drivers register */ 485postcore_initcall(serdev_init); 486 487MODULE_AUTHOR("Rob Herring <robh@kernel.org>"); 488MODULE_LICENSE("GPL v2"); 489MODULE_DESCRIPTION("Serial attached device bus");