| 1 | |
| 2 | |
| 3 | * Turris Mox module configuration bus driver
|
| 4 | *
|
| 5 | * Copyright (C) 2019 Marek Behún <[email protected]>
|
| 6 |
|
| 7 |
|
| 8 | #include <dt-bindings/bus/moxtet.h>
|
| 9 | #include <linux/bitops.h>
|
| 10 | #include <linux/debugfs.h>
|
| 11 | #include <linux/hex.h>
|
| 12 | #include <linux/interrupt.h>
|
| 13 | #include <linux/module.h>
|
| 14 | #include <linux/moxtet.h>
|
| 15 | #include <linux/mutex.h>
|
| 16 | #include <linux/of_device.h>
|
| 17 | #include <linux/of_irq.h>
|
| 18 | #include <linux/spi/spi.h>
|
| 19 |
|
| 20 | |
| 21 | * @name: module name for sysfs
|
| 22 | * @hwirq_base: base index for IRQ for this module (-1 if no IRQs)
|
| 23 | * @nirqs: how many interrupts does the shift register provide
|
| 24 | * @desc: module description for kernel log
|
| 25 |
|
| 26 | static const struct {
|
| 27 | const char *name;
|
| 28 | int hwirq_base;
|
| 29 | int nirqs;
|
| 30 | const char *desc;
|
| 31 | } mox_module_table[] = {
|
| 32 |
|
| 33 | { NULL, 0, 0, NULL },
|
| 34 | { "sfp", -1, 0, "MOX D (SFP cage)" },
|
| 35 | { "pci", MOXTET_IRQ_PCI, 1, "MOX B (Mini-PCIe)" },
|
| 36 | { "topaz", MOXTET_IRQ_TOPAZ, 1, "MOX C (4 port switch)" },
|
| 37 | { "peridot", MOXTET_IRQ_PERIDOT(0), 1, "MOX E (8 port switch)" },
|
| 38 | { "usb3", MOXTET_IRQ_USB3, 2, "MOX F (USB 3.0)" },
|
| 39 | { "pci-bridge", -1, 0, "MOX G (Mini-PCIe bridge)" },
|
| 40 | };
|
| 41 |
|
| 42 | static inline bool mox_module_known(unsigned int id)
|
| 43 | {
|
| 44 | return id >= TURRIS_MOX_MODULE_FIRST && id <= TURRIS_MOX_MODULE_LAST;
|
| 45 | }
|
| 46 |
|
| 47 | static inline const char *mox_module_name(unsigned int id)
|
| 48 | {
|
| 49 | if (mox_module_known(id))
|
| 50 | return mox_module_table[id].name;
|
| 51 | else
|
| 52 | return "unknown";
|
| 53 | }
|
| 54 |
|
| 55 | #define DEF_MODULE_ATTR(name, fmt, ...) \
|
| 56 | static ssize_t \
|
| 57 | module_##name##_show(struct device *dev, struct device_attribute *a, \
|
| 58 | char *buf) \
|
| 59 | { \
|
| 60 | struct moxtet_device *mdev = to_moxtet_device(dev); \
|
| 61 | return sprintf(buf, (fmt), __VA_ARGS__); \
|
| 62 | } \
|
| 63 | static DEVICE_ATTR_RO(module_##name)
|
| 64 |
|
| 65 | DEF_MODULE_ATTR(id, "0x%x\n", mdev->id);
|
| 66 | DEF_MODULE_ATTR(name, "%s\n", mox_module_name(mdev->id));
|
| 67 | DEF_MODULE_ATTR(description, "%s\n",
|
| 68 | mox_module_known(mdev->id) ? mox_module_table[mdev->id].desc
|
| 69 | : "");
|
| 70 |
|
| 71 | static struct attribute *moxtet_dev_attrs[] = {
|
| 72 | &dev_attr_module_id.attr,
|
| 73 | &dev_attr_module_name.attr,
|
| 74 | &dev_attr_module_description.attr,
|
| 75 | NULL,
|
| 76 | };
|
| 77 |
|
| 78 | static const struct attribute_group moxtet_dev_group = {
|
| 79 | .attrs = moxtet_dev_attrs,
|
| 80 | };
|
| 81 |
|
| 82 | static const struct attribute_group *moxtet_dev_groups[] = {
|
| 83 | &moxtet_dev_group,
|
| 84 | NULL,
|
| 85 | };
|
| 86 |
|
| 87 | static int moxtet_match(struct device *dev, const struct device_driver *drv)
|
| 88 | {
|
| 89 | struct moxtet_device *mdev = to_moxtet_device(dev);
|
| 90 | const struct moxtet_driver *tdrv = to_moxtet_driver(drv);
|
| 91 | const enum turris_mox_module_id *t;
|
| 92 |
|
| 93 | if (of_driver_match_device(dev, drv))
|
| 94 | return 1;
|
| 95 |
|
| 96 | if (!tdrv->id_table)
|
| 97 | return 0;
|
| 98 |
|
| 99 | for (t = tdrv->id_table; *t; ++t)
|
| 100 | if (*t == mdev->id)
|
| 101 | return 1;
|
| 102 |
|
| 103 | return 0;
|
| 104 | }
|
| 105 |
|
| 106 | static const struct bus_type moxtet_bus_type = {
|
| 107 | .name = "moxtet",
|
| 108 | .dev_groups = moxtet_dev_groups,
|
| 109 | .match = moxtet_match,
|
| 110 | };
|
| 111 |
|
| 112 | int __moxtet_register_driver(struct module *owner,
|
| 113 | struct moxtet_driver *mdrv)
|
| 114 | {
|
| 115 | mdrv->driver.owner = owner;
|
| 116 | mdrv->driver.bus = &moxtet_bus_type;
|
| 117 | return driver_register(&mdrv->driver);
|
| 118 | }
|
| 119 | EXPORT_SYMBOL_GPL(__moxtet_register_driver);
|
| 120 |
|
| 121 | static int moxtet_dev_check(struct device *dev, void *data)
|
| 122 | {
|
| 123 | struct moxtet_device *mdev = to_moxtet_device(dev);
|
| 124 | struct moxtet_device *new_dev = data;
|
| 125 |
|
| 126 | if (mdev->moxtet == new_dev->moxtet && mdev->id == new_dev->id &&
|
| 127 | mdev->idx == new_dev->idx)
|
| 128 | return -EBUSY;
|
| 129 | return 0;
|
| 130 | }
|
| 131 |
|
| 132 | static void moxtet_dev_release(struct device *dev)
|
| 133 | {
|
| 134 | struct moxtet_device *mdev = to_moxtet_device(dev);
|
| 135 |
|
| 136 | put_device(mdev->moxtet->dev);
|
| 137 | kfree(mdev);
|
| 138 | }
|
| 139 |
|
| 140 | static struct moxtet_device *
|
| 141 | moxtet_alloc_device(struct moxtet *moxtet)
|
| 142 | {
|
| 143 | struct moxtet_device *dev;
|
| 144 |
|
| 145 | if (!get_device(moxtet->dev))
|
| 146 | return NULL;
|
| 147 |
|
| 148 | dev = kzalloc_obj(*dev);
|
| 149 | if (!dev) {
|
| 150 | put_device(moxtet->dev);
|
| 151 | return NULL;
|
| 152 | }
|
| 153 |
|
| 154 | dev->moxtet = moxtet;
|
| 155 | dev->dev.parent = moxtet->dev;
|
| 156 | dev->dev.bus = &moxtet_bus_type;
|
| 157 | dev->dev.release = moxtet_dev_release;
|
| 158 |
|
| 159 | device_initialize(&dev->dev);
|
| 160 |
|
| 161 | return dev;
|
| 162 | }
|
| 163 |
|
| 164 | static int moxtet_add_device(struct moxtet_device *dev)
|
| 165 | {
|
| 166 | static DEFINE_MUTEX(add_mutex);
|
| 167 | int ret;
|
| 168 |
|
| 169 | if (dev->idx >= TURRIS_MOX_MAX_MODULES || dev->id > 0xf)
|
| 170 | return -EINVAL;
|
| 171 |
|
| 172 | dev_set_name(&dev->dev, "moxtet-%s.%u", mox_module_name(dev->id),
|
| 173 | dev->idx);
|
| 174 |
|
| 175 | mutex_lock(&add_mutex);
|
| 176 |
|
| 177 | ret = bus_for_each_dev(&moxtet_bus_type, NULL, dev,
|
| 178 | moxtet_dev_check);
|
| 179 | if (ret)
|
| 180 | goto done;
|
| 181 |
|
| 182 | ret = device_add(&dev->dev);
|
| 183 | if (ret < 0)
|
| 184 | dev_err(dev->moxtet->dev, "can't add %s, status %d\n",
|
| 185 | dev_name(dev->moxtet->dev), ret);
|
| 186 |
|
| 187 | done:
|
| 188 | mutex_unlock(&add_mutex);
|
| 189 | return ret;
|
| 190 | }
|
| 191 |
|
| 192 | static int __unregister(struct device *dev, void *null)
|
| 193 | {
|
| 194 | if (dev->of_node) {
|
| 195 | of_node_clear_flag(dev->of_node, OF_POPULATED);
|
| 196 | of_node_put(dev->of_node);
|
| 197 | }
|
| 198 |
|
| 199 | device_unregister(dev);
|
| 200 |
|
| 201 | return 0;
|
| 202 | }
|
| 203 |
|
| 204 | static struct moxtet_device *
|
| 205 | of_register_moxtet_device(struct moxtet *moxtet, struct device_node *nc)
|
| 206 | {
|
| 207 | struct moxtet_device *dev;
|
| 208 | u32 val;
|
| 209 | int ret;
|
| 210 |
|
| 211 | dev = moxtet_alloc_device(moxtet);
|
| 212 | if (!dev) {
|
| 213 | dev_err(moxtet->dev,
|
| 214 | "Moxtet device alloc error for %pOF\n", nc);
|
| 215 | return ERR_PTR(-ENOMEM);
|
| 216 | }
|
| 217 |
|
| 218 | ret = of_property_read_u32(nc, "reg", &val);
|
| 219 | if (ret) {
|
| 220 | dev_err(moxtet->dev, "%pOF has no valid 'reg' property (%d)\n",
|
| 221 | nc, ret);
|
| 222 | goto err_put;
|
| 223 | }
|
| 224 |
|
| 225 | dev->idx = val;
|
| 226 |
|
| 227 | if (dev->idx >= TURRIS_MOX_MAX_MODULES) {
|
| 228 | dev_err(moxtet->dev, "%pOF Moxtet address 0x%x out of range\n",
|
| 229 | nc, dev->idx);
|
| 230 | ret = -EINVAL;
|
| 231 | goto err_put;
|
| 232 | }
|
| 233 |
|
| 234 | dev->id = moxtet->modules[dev->idx];
|
| 235 |
|
| 236 | if (!dev->id) {
|
| 237 | dev_err(moxtet->dev, "%pOF Moxtet address 0x%x is empty\n", nc,
|
| 238 | dev->idx);
|
| 239 | ret = -ENODEV;
|
| 240 | goto err_put;
|
| 241 | }
|
| 242 |
|
| 243 | of_node_get(nc);
|
| 244 | dev->dev.of_node = nc;
|
| 245 |
|
| 246 | ret = moxtet_add_device(dev);
|
| 247 | if (ret) {
|
| 248 | dev_err(moxtet->dev,
|
| 249 | "Moxtet device register error for %pOF\n", nc);
|
| 250 | of_node_put(nc);
|
| 251 | goto err_put;
|
| 252 | }
|
| 253 |
|
| 254 | return dev;
|
| 255 |
|
| 256 | err_put:
|
| 257 | put_device(&dev->dev);
|
| 258 | return ERR_PTR(ret);
|
| 259 | }
|
| 260 |
|
| 261 | static void of_register_moxtet_devices(struct moxtet *moxtet)
|
| 262 | {
|
| 263 | struct moxtet_device *dev;
|
| 264 | struct device_node *nc;
|
| 265 |
|
| 266 | if (!moxtet->dev->of_node)
|
| 267 | return;
|
| 268 |
|
| 269 | for_each_available_child_of_node(moxtet->dev->of_node, nc) {
|
| 270 | if (of_node_test_and_set_flag(nc, OF_POPULATED))
|
| 271 | continue;
|
| 272 | dev = of_register_moxtet_device(moxtet, nc);
|
| 273 | if (IS_ERR(dev)) {
|
| 274 | dev_warn(moxtet->dev,
|
| 275 | "Failed to create Moxtet device for %pOF\n",
|
| 276 | nc);
|
| 277 | of_node_clear_flag(nc, OF_POPULATED);
|
| 278 | }
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | static void
|
| 283 | moxtet_register_devices_from_topology(struct moxtet *moxtet)
|
| 284 | {
|
| 285 | struct moxtet_device *dev;
|
| 286 | int i, ret;
|
| 287 |
|
| 288 | for (i = 0; i < moxtet->count; ++i) {
|
| 289 | dev = moxtet_alloc_device(moxtet);
|
| 290 | if (!dev) {
|
| 291 | dev_err(moxtet->dev, "Moxtet device %u alloc error\n",
|
| 292 | i);
|
| 293 | continue;
|
| 294 | }
|
| 295 |
|
| 296 | dev->idx = i;
|
| 297 | dev->id = moxtet->modules[i];
|
| 298 |
|
| 299 | ret = moxtet_add_device(dev);
|
| 300 | if (ret && ret != -EBUSY) {
|
| 301 | put_device(&dev->dev);
|
| 302 | dev_err(moxtet->dev,
|
| 303 | "Moxtet device %u register error: %i\n", i,
|
| 304 | ret);
|
| 305 | }
|
| 306 | }
|
| 307 | }
|
| 308 |
|
| 309 | |
| 310 | * @nsame: how many modules with same id are already in moxtet->modules
|
| 311 |
|
| 312 | static int moxtet_set_irq(struct moxtet *moxtet, int idx, int id, int nsame)
|
| 313 | {
|
| 314 | int i, first;
|
| 315 | struct moxtet_irqpos *pos;
|
| 316 |
|
| 317 | first = mox_module_table[id].hwirq_base +
|
| 318 | nsame * mox_module_table[id].nirqs;
|
| 319 |
|
| 320 | if (first + mox_module_table[id].nirqs > MOXTET_NIRQS)
|
| 321 | return -EINVAL;
|
| 322 |
|
| 323 | for (i = 0; i < mox_module_table[id].nirqs; ++i) {
|
| 324 | pos = &moxtet->irq.position[first + i];
|
| 325 | pos->idx = idx;
|
| 326 | pos->bit = i;
|
| 327 | moxtet->irq.exists |= BIT(first + i);
|
| 328 | }
|
| 329 |
|
| 330 | return 0;
|
| 331 | }
|
| 332 |
|
| 333 | static int moxtet_find_topology(struct moxtet *moxtet)
|
| 334 | {
|
| 335 | u8 buf[TURRIS_MOX_MAX_MODULES];
|
| 336 | int cnts[TURRIS_MOX_MODULE_LAST];
|
| 337 | int i, ret;
|
| 338 |
|
| 339 | memset(cnts, 0, sizeof(cnts));
|
| 340 |
|
| 341 | ret = spi_read(to_spi_device(moxtet->dev), buf, TURRIS_MOX_MAX_MODULES);
|
| 342 | if (ret < 0)
|
| 343 | return ret;
|
| 344 |
|
| 345 | if (buf[0] == TURRIS_MOX_CPU_ID_EMMC) {
|
| 346 | dev_info(moxtet->dev, "Found MOX A (eMMC CPU) module\n");
|
| 347 | } else if (buf[0] == TURRIS_MOX_CPU_ID_SD) {
|
| 348 | dev_info(moxtet->dev, "Found MOX A (CPU) module\n");
|
| 349 | } else {
|
| 350 | dev_err(moxtet->dev, "Invalid Turris MOX A CPU module 0x%02x\n",
|
| 351 | buf[0]);
|
| 352 | return -ENODEV;
|
| 353 | }
|
| 354 |
|
| 355 | moxtet->count = 0;
|
| 356 |
|
| 357 | for (i = 1; i < TURRIS_MOX_MAX_MODULES; ++i) {
|
| 358 | int id;
|
| 359 |
|
| 360 | if (buf[i] == 0xff)
|
| 361 | break;
|
| 362 |
|
| 363 | id = buf[i] & 0xf;
|
| 364 |
|
| 365 | moxtet->modules[i-1] = id;
|
| 366 | ++moxtet->count;
|
| 367 |
|
| 368 | if (mox_module_known(id)) {
|
| 369 | dev_info(moxtet->dev, "Found %s module\n",
|
| 370 | mox_module_table[id].desc);
|
| 371 |
|
| 372 | if (moxtet_set_irq(moxtet, i-1, id, cnts[id]++) < 0)
|
| 373 | dev_err(moxtet->dev,
|
| 374 | " Cannot set IRQ for module %s\n",
|
| 375 | mox_module_table[id].desc);
|
| 376 | } else {
|
| 377 | dev_warn(moxtet->dev,
|
| 378 | "Unknown Moxtet module found (ID 0x%02x)\n",
|
| 379 | id);
|
| 380 | }
|
| 381 | }
|
| 382 |
|
| 383 | return 0;
|
| 384 | }
|
| 385 |
|
| 386 | static int moxtet_spi_read(struct moxtet *moxtet, u8 *buf)
|
| 387 | {
|
| 388 | struct spi_transfer xfer = {
|
| 389 | .rx_buf = buf,
|
| 390 | .tx_buf = moxtet->tx,
|
| 391 | .len = moxtet->count + 1
|
| 392 | };
|
| 393 | int ret;
|
| 394 |
|
| 395 | mutex_lock(&moxtet->lock);
|
| 396 |
|
| 397 | ret = spi_sync_transfer(to_spi_device(moxtet->dev), &xfer, 1);
|
| 398 |
|
| 399 | mutex_unlock(&moxtet->lock);
|
| 400 |
|
| 401 | return ret;
|
| 402 | }
|
| 403 |
|
| 404 | int moxtet_device_read(struct device *dev)
|
| 405 | {
|
| 406 | struct moxtet_device *mdev = to_moxtet_device(dev);
|
| 407 | struct moxtet *moxtet = mdev->moxtet;
|
| 408 | u8 buf[TURRIS_MOX_MAX_MODULES];
|
| 409 | int ret;
|
| 410 |
|
| 411 | if (mdev->idx >= moxtet->count)
|
| 412 | return -EINVAL;
|
| 413 |
|
| 414 | ret = moxtet_spi_read(moxtet, buf);
|
| 415 | if (ret < 0)
|
| 416 | return ret;
|
| 417 |
|
| 418 | return buf[mdev->idx + 1] >> 4;
|
| 419 | }
|
| 420 | EXPORT_SYMBOL_GPL(moxtet_device_read);
|
| 421 |
|
| 422 | int moxtet_device_write(struct device *dev, u8 val)
|
| 423 | {
|
| 424 | struct moxtet_device *mdev = to_moxtet_device(dev);
|
| 425 | struct moxtet *moxtet = mdev->moxtet;
|
| 426 | int ret;
|
| 427 |
|
| 428 | if (mdev->idx >= moxtet->count)
|
| 429 | return -EINVAL;
|
| 430 |
|
| 431 | mutex_lock(&moxtet->lock);
|
| 432 |
|
| 433 | moxtet->tx[moxtet->count - mdev->idx] = val;
|
| 434 |
|
| 435 | ret = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
|
| 436 | moxtet->count + 1);
|
| 437 |
|
| 438 | mutex_unlock(&moxtet->lock);
|
| 439 |
|
| 440 | return ret;
|
| 441 | }
|
| 442 | EXPORT_SYMBOL_GPL(moxtet_device_write);
|
| 443 |
|
| 444 | int moxtet_device_written(struct device *dev)
|
| 445 | {
|
| 446 | struct moxtet_device *mdev = to_moxtet_device(dev);
|
| 447 | struct moxtet *moxtet = mdev->moxtet;
|
| 448 |
|
| 449 | if (mdev->idx >= moxtet->count)
|
| 450 | return -EINVAL;
|
| 451 |
|
| 452 | return moxtet->tx[moxtet->count - mdev->idx];
|
| 453 | }
|
| 454 | EXPORT_SYMBOL_GPL(moxtet_device_written);
|
| 455 |
|
| 456 | #ifdef CONFIG_DEBUG_FS
|
| 457 | static int moxtet_debug_open(struct inode *inode, struct file *file)
|
| 458 | {
|
| 459 | file->private_data = inode->i_private;
|
| 460 |
|
| 461 | return nonseekable_open(inode, file);
|
| 462 | }
|
| 463 |
|
| 464 | static ssize_t input_read(struct file *file, char __user *buf, size_t len,
|
| 465 | loff_t *ppos)
|
| 466 | {
|
| 467 | struct moxtet *moxtet = file->private_data;
|
| 468 | u8 bin[TURRIS_MOX_MAX_MODULES];
|
| 469 | u8 hex[sizeof(bin) * 2 + 1];
|
| 470 | int ret, n;
|
| 471 |
|
| 472 | ret = moxtet_spi_read(moxtet, bin);
|
| 473 | if (ret < 0)
|
| 474 | return ret;
|
| 475 |
|
| 476 | n = moxtet->count + 1;
|
| 477 | bin2hex(hex, bin, n);
|
| 478 |
|
| 479 | hex[2*n] = '\n';
|
| 480 |
|
| 481 | return simple_read_from_buffer(buf, len, ppos, hex, 2*n + 1);
|
| 482 | }
|
| 483 |
|
| 484 | static const struct file_operations input_fops = {
|
| 485 | .owner = THIS_MODULE,
|
| 486 | .open = moxtet_debug_open,
|
| 487 | .read = input_read,
|
| 488 | };
|
| 489 |
|
| 490 | static ssize_t output_read(struct file *file, char __user *buf, size_t len,
|
| 491 | loff_t *ppos)
|
| 492 | {
|
| 493 | struct moxtet *moxtet = file->private_data;
|
| 494 | u8 hex[TURRIS_MOX_MAX_MODULES * 2 + 1];
|
| 495 | u8 *p = hex;
|
| 496 | int i;
|
| 497 |
|
| 498 | mutex_lock(&moxtet->lock);
|
| 499 |
|
| 500 | for (i = 0; i < moxtet->count; ++i)
|
| 501 | p = hex_byte_pack(p, moxtet->tx[moxtet->count - i]);
|
| 502 |
|
| 503 | mutex_unlock(&moxtet->lock);
|
| 504 |
|
| 505 | *p++ = '\n';
|
| 506 |
|
| 507 | return simple_read_from_buffer(buf, len, ppos, hex, p - hex);
|
| 508 | }
|
| 509 |
|
| 510 | static ssize_t output_write(struct file *file, const char __user *buf,
|
| 511 | size_t len, loff_t *ppos)
|
| 512 | {
|
| 513 | struct moxtet *moxtet = file->private_data;
|
| 514 | u8 bin[TURRIS_MOX_MAX_MODULES];
|
| 515 | u8 hex[sizeof(bin) * 2 + 1];
|
| 516 | ssize_t res;
|
| 517 | loff_t dummy = 0;
|
| 518 | int err, i;
|
| 519 |
|
| 520 | if (len > 2 * moxtet->count + 1 || len < 2 * moxtet->count)
|
| 521 | return -EINVAL;
|
| 522 |
|
| 523 | res = simple_write_to_buffer(hex, sizeof(hex), &dummy, buf, len);
|
| 524 | if (res < 0)
|
| 525 | return res;
|
| 526 |
|
| 527 | if (len % 2 == 1 && hex[len - 1] != '\n')
|
| 528 | return -EINVAL;
|
| 529 |
|
| 530 | err = hex2bin(bin, hex, moxtet->count);
|
| 531 | if (err < 0)
|
| 532 | return -EINVAL;
|
| 533 |
|
| 534 | mutex_lock(&moxtet->lock);
|
| 535 |
|
| 536 | for (i = 0; i < moxtet->count; ++i)
|
| 537 | moxtet->tx[moxtet->count - i] = bin[i];
|
| 538 |
|
| 539 | err = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
|
| 540 | moxtet->count + 1);
|
| 541 |
|
| 542 | mutex_unlock(&moxtet->lock);
|
| 543 |
|
| 544 | return err < 0 ? err : len;
|
| 545 | }
|
| 546 |
|
| 547 | static const struct file_operations output_fops = {
|
| 548 | .owner = THIS_MODULE,
|
| 549 | .open = moxtet_debug_open,
|
| 550 | .read = output_read,
|
| 551 | .write = output_write,
|
| 552 | };
|
| 553 |
|
| 554 | static int moxtet_register_debugfs(struct moxtet *moxtet)
|
| 555 | {
|
| 556 | struct dentry *root, *entry;
|
| 557 |
|
| 558 | root = debugfs_create_dir("moxtet", NULL);
|
| 559 |
|
| 560 | if (IS_ERR(root))
|
| 561 | return PTR_ERR(root);
|
| 562 |
|
| 563 | entry = debugfs_create_file_unsafe("input", 0444, root, moxtet,
|
| 564 | &input_fops);
|
| 565 | if (IS_ERR(entry))
|
| 566 | goto err_remove;
|
| 567 |
|
| 568 | entry = debugfs_create_file_unsafe("output", 0644, root, moxtet,
|
| 569 | &output_fops);
|
| 570 | if (IS_ERR(entry))
|
| 571 | goto err_remove;
|
| 572 |
|
| 573 | moxtet->debugfs_root = root;
|
| 574 |
|
| 575 | return 0;
|
| 576 | err_remove:
|
| 577 | debugfs_remove_recursive(root);
|
| 578 | return PTR_ERR(entry);
|
| 579 | }
|
| 580 |
|
| 581 | static void moxtet_unregister_debugfs(struct moxtet *moxtet)
|
| 582 | {
|
| 583 | debugfs_remove_recursive(moxtet->debugfs_root);
|
| 584 | }
|
| 585 | #else
|
| 586 | static inline int moxtet_register_debugfs(struct moxtet *moxtet)
|
| 587 | {
|
| 588 | return 0;
|
| 589 | }
|
| 590 |
|
| 591 | static inline void moxtet_unregister_debugfs(struct moxtet *moxtet)
|
| 592 | {
|
| 593 | }
|
| 594 | #endif
|
| 595 |
|
| 596 | static int moxtet_irq_domain_map(struct irq_domain *d, unsigned int irq,
|
| 597 | irq_hw_number_t hw)
|
| 598 | {
|
| 599 | struct moxtet *moxtet = d->host_data;
|
| 600 |
|
| 601 | if (hw >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(hw))) {
|
| 602 | dev_err(moxtet->dev, "Invalid hw irq number\n");
|
| 603 | return -EINVAL;
|
| 604 | }
|
| 605 |
|
| 606 | irq_set_chip_data(irq, d->host_data);
|
| 607 | irq_set_chip_and_handler(irq, &moxtet->irq.chip, handle_level_irq);
|
| 608 |
|
| 609 | return 0;
|
| 610 | }
|
| 611 |
|
| 612 | static int moxtet_irq_domain_xlate(struct irq_domain *d,
|
| 613 | struct device_node *ctrlr,
|
| 614 | const u32 *intspec, unsigned int intsize,
|
| 615 | unsigned long *out_hwirq,
|
| 616 | unsigned int *out_type)
|
| 617 | {
|
| 618 | struct moxtet *moxtet = d->host_data;
|
| 619 | int irq;
|
| 620 |
|
| 621 | if (WARN_ON(intsize < 1))
|
| 622 | return -EINVAL;
|
| 623 |
|
| 624 | irq = intspec[0];
|
| 625 |
|
| 626 | if (irq >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(irq)))
|
| 627 | return -EINVAL;
|
| 628 |
|
| 629 | *out_hwirq = irq;
|
| 630 | *out_type = IRQ_TYPE_NONE;
|
| 631 | return 0;
|
| 632 | }
|
| 633 |
|
| 634 | static const struct irq_domain_ops moxtet_irq_domain = {
|
| 635 | .map = moxtet_irq_domain_map,
|
| 636 | .xlate = moxtet_irq_domain_xlate,
|
| 637 | };
|
| 638 |
|
| 639 | static void moxtet_irq_mask(struct irq_data *d)
|
| 640 | {
|
| 641 | struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
|
| 642 |
|
| 643 | moxtet->irq.masked |= BIT(d->hwirq);
|
| 644 | }
|
| 645 |
|
| 646 | static void moxtet_irq_unmask(struct irq_data *d)
|
| 647 | {
|
| 648 | struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
|
| 649 |
|
| 650 | moxtet->irq.masked &= ~BIT(d->hwirq);
|
| 651 | }
|
| 652 |
|
| 653 | static void moxtet_irq_print_chip(struct irq_data *d, struct seq_file *p)
|
| 654 | {
|
| 655 | struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
|
| 656 | struct moxtet_irqpos *pos = &moxtet->irq.position[d->hwirq];
|
| 657 | int id;
|
| 658 |
|
| 659 | id = moxtet->modules[pos->idx];
|
| 660 |
|
| 661 | seq_printf(p, "moxtet-%s.%i#%i", mox_module_name(id), pos->idx,
|
| 662 | pos->bit);
|
| 663 | }
|
| 664 |
|
| 665 | static const struct irq_chip moxtet_irq_chip = {
|
| 666 | .name = "moxtet",
|
| 667 | .irq_mask = moxtet_irq_mask,
|
| 668 | .irq_unmask = moxtet_irq_unmask,
|
| 669 | .irq_print_chip = moxtet_irq_print_chip,
|
| 670 | };
|
| 671 |
|
| 672 | static int moxtet_irq_read(struct moxtet *moxtet, unsigned long *map)
|
| 673 | {
|
| 674 | struct moxtet_irqpos *pos = moxtet->irq.position;
|
| 675 | u8 buf[TURRIS_MOX_MAX_MODULES];
|
| 676 | int i, ret;
|
| 677 |
|
| 678 | ret = moxtet_spi_read(moxtet, buf);
|
| 679 | if (ret < 0)
|
| 680 | return ret;
|
| 681 |
|
| 682 | *map = 0;
|
| 683 |
|
| 684 | for_each_set_bit(i, &moxtet->irq.exists, MOXTET_NIRQS) {
|
| 685 | if (!(buf[pos[i].idx + 1] & BIT(4 + pos[i].bit)))
|
| 686 | set_bit(i, map);
|
| 687 | }
|
| 688 |
|
| 689 | return 0;
|
| 690 | }
|
| 691 |
|
| 692 | static irqreturn_t moxtet_irq_thread_fn(int irq, void *data)
|
| 693 | {
|
| 694 | struct moxtet *moxtet = data;
|
| 695 | unsigned long set;
|
| 696 | int nhandled = 0, i, sub_irq, ret;
|
| 697 |
|
| 698 | ret = moxtet_irq_read(moxtet, &set);
|
| 699 | if (ret < 0)
|
| 700 | goto out;
|
| 701 |
|
| 702 | set &= ~moxtet->irq.masked;
|
| 703 |
|
| 704 | do {
|
| 705 | for_each_set_bit(i, &set, MOXTET_NIRQS) {
|
| 706 | sub_irq = irq_find_mapping(moxtet->irq.domain, i);
|
| 707 | handle_nested_irq(sub_irq);
|
| 708 | dev_dbg(moxtet->dev, "%i irq\n", i);
|
| 709 | ++nhandled;
|
| 710 | }
|
| 711 |
|
| 712 | ret = moxtet_irq_read(moxtet, &set);
|
| 713 | if (ret < 0)
|
| 714 | goto out;
|
| 715 |
|
| 716 | set &= ~moxtet->irq.masked;
|
| 717 | } while (set);
|
| 718 |
|
| 719 | out:
|
| 720 | return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
|
| 721 | }
|
| 722 |
|
| 723 | static void moxtet_irq_free(struct moxtet *moxtet)
|
| 724 | {
|
| 725 | int i, irq;
|
| 726 |
|
| 727 | for (i = 0; i < MOXTET_NIRQS; ++i) {
|
| 728 | if (moxtet->irq.exists & BIT(i)) {
|
| 729 | irq = irq_find_mapping(moxtet->irq.domain, i);
|
| 730 | irq_dispose_mapping(irq);
|
| 731 | }
|
| 732 | }
|
| 733 |
|
| 734 | irq_domain_remove(moxtet->irq.domain);
|
| 735 | }
|
| 736 |
|
| 737 | static int moxtet_irq_setup(struct moxtet *moxtet)
|
| 738 | {
|
| 739 | int i, ret;
|
| 740 |
|
| 741 | moxtet->irq.domain = irq_domain_create_simple(dev_fwnode(moxtet->dev), MOXTET_NIRQS, 0,
|
| 742 | &moxtet_irq_domain, moxtet);
|
| 743 | if (moxtet->irq.domain == NULL) {
|
| 744 | dev_err(moxtet->dev, "Could not add IRQ domain\n");
|
| 745 | return -ENOMEM;
|
| 746 | }
|
| 747 |
|
| 748 | for (i = 0; i < MOXTET_NIRQS; ++i)
|
| 749 | if (moxtet->irq.exists & BIT(i))
|
| 750 | irq_create_mapping(moxtet->irq.domain, i);
|
| 751 |
|
| 752 | moxtet->irq.chip = moxtet_irq_chip;
|
| 753 | moxtet->irq.masked = ~0;
|
| 754 |
|
| 755 | ret = request_threaded_irq(moxtet->dev_irq, NULL, moxtet_irq_thread_fn,
|
| 756 | IRQF_SHARED | IRQF_ONESHOT, "moxtet", moxtet);
|
| 757 | if (ret < 0)
|
| 758 | goto err_free;
|
| 759 |
|
| 760 | return 0;
|
| 761 |
|
| 762 | err_free:
|
| 763 | moxtet_irq_free(moxtet);
|
| 764 | return ret;
|
| 765 | }
|
| 766 |
|
| 767 | static int moxtet_probe(struct spi_device *spi)
|
| 768 | {
|
| 769 | struct moxtet *moxtet;
|
| 770 | int ret;
|
| 771 |
|
| 772 | ret = spi_setup(spi);
|
| 773 | if (ret < 0)
|
| 774 | return ret;
|
| 775 |
|
| 776 | moxtet = devm_kzalloc(&spi->dev, sizeof(struct moxtet),
|
| 777 | GFP_KERNEL);
|
| 778 | if (!moxtet)
|
| 779 | return -ENOMEM;
|
| 780 |
|
| 781 | moxtet->dev = &spi->dev;
|
| 782 | spi_set_drvdata(spi, moxtet);
|
| 783 |
|
| 784 | mutex_init(&moxtet->lock);
|
| 785 |
|
| 786 | moxtet->dev_irq = of_irq_get(moxtet->dev->of_node, 0);
|
| 787 | if (moxtet->dev_irq == -EPROBE_DEFER)
|
| 788 | return -EPROBE_DEFER;
|
| 789 |
|
| 790 | if (moxtet->dev_irq <= 0) {
|
| 791 | dev_err(moxtet->dev, "No IRQ resource found\n");
|
| 792 | return -ENXIO;
|
| 793 | }
|
| 794 |
|
| 795 | ret = moxtet_find_topology(moxtet);
|
| 796 | if (ret < 0)
|
| 797 | return ret;
|
| 798 |
|
| 799 | if (moxtet->irq.exists) {
|
| 800 | ret = moxtet_irq_setup(moxtet);
|
| 801 | if (ret < 0)
|
| 802 | return ret;
|
| 803 | }
|
| 804 |
|
| 805 | of_register_moxtet_devices(moxtet);
|
| 806 | moxtet_register_devices_from_topology(moxtet);
|
| 807 |
|
| 808 | ret = moxtet_register_debugfs(moxtet);
|
| 809 | if (ret < 0)
|
| 810 | dev_warn(moxtet->dev, "Failed creating debugfs entries: %i\n",
|
| 811 | ret);
|
| 812 |
|
| 813 | return 0;
|
| 814 | }
|
| 815 |
|
| 816 | static void moxtet_remove(struct spi_device *spi)
|
| 817 | {
|
| 818 | struct moxtet *moxtet = spi_get_drvdata(spi);
|
| 819 |
|
| 820 | free_irq(moxtet->dev_irq, moxtet);
|
| 821 |
|
| 822 | moxtet_irq_free(moxtet);
|
| 823 |
|
| 824 | moxtet_unregister_debugfs(moxtet);
|
| 825 |
|
| 826 | device_for_each_child(moxtet->dev, NULL, __unregister);
|
| 827 |
|
| 828 | mutex_destroy(&moxtet->lock);
|
| 829 | }
|
| 830 |
|
| 831 | static const struct spi_device_id moxtet_spi_ids[] = {
|
| 832 | { "moxtet" },
|
| 833 | { },
|
| 834 | };
|
| 835 | MODULE_DEVICE_TABLE(spi, moxtet_spi_ids);
|
| 836 |
|
| 837 | static const struct of_device_id moxtet_dt_ids[] = {
|
| 838 | { .compatible = "cznic,moxtet" },
|
| 839 | {},
|
| 840 | };
|
| 841 | MODULE_DEVICE_TABLE(of, moxtet_dt_ids);
|
| 842 |
|
| 843 | static struct spi_driver moxtet_spi_driver = {
|
| 844 | .driver = {
|
| 845 | .name = "moxtet",
|
| 846 | .of_match_table = moxtet_dt_ids,
|
| 847 | },
|
| 848 | .id_table = moxtet_spi_ids,
|
| 849 | .probe = moxtet_probe,
|
| 850 | .remove = moxtet_remove,
|
| 851 | };
|
| 852 |
|
| 853 | static int __init moxtet_init(void)
|
| 854 | {
|
| 855 | int ret;
|
| 856 |
|
| 857 | ret = bus_register(&moxtet_bus_type);
|
| 858 | if (ret < 0) {
|
| 859 | pr_err("moxtet bus registration failed: %d\n", ret);
|
| 860 | goto error;
|
| 861 | }
|
| 862 |
|
| 863 | ret = spi_register_driver(&moxtet_spi_driver);
|
| 864 | if (ret < 0) {
|
| 865 | pr_err("moxtet spi driver registration failed: %d\n", ret);
|
| 866 | goto error_bus;
|
| 867 | }
|
| 868 |
|
| 869 | return 0;
|
| 870 |
|
| 871 | error_bus:
|
| 872 | bus_unregister(&moxtet_bus_type);
|
| 873 | error:
|
| 874 | return ret;
|
| 875 | }
|
| 876 | postcore_initcall_sync(moxtet_init);
|
| 877 |
|
| 878 | static void __exit moxtet_exit(void)
|
| 879 | {
|
| 880 | spi_unregister_driver(&moxtet_spi_driver);
|
| 881 | bus_unregister(&moxtet_bus_type);
|
| 882 | }
|
| 883 | module_exit(moxtet_exit);
|
| 884 |
|
| 885 | MODULE_AUTHOR("Marek Behun <[email protected]>");
|
| 886 | MODULE_DESCRIPTION("CZ.NIC's Turris Mox module configuration bus");
|
| 887 | MODULE_LICENSE("GPL v2");
|