Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Turris Mox module configuration bus driver
0004  *
0005  * Copyright (C) 2019 Marek Behún <kabel@kernel.org>
0006  */
0007 
0008 #include <dt-bindings/bus/moxtet.h>
0009 #include <linux/bitops.h>
0010 #include <linux/debugfs.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/module.h>
0013 #include <linux/moxtet.h>
0014 #include <linux/mutex.h>
0015 #include <linux/of_device.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/spi/spi.h>
0018 
0019 /*
0020  * @name:   module name for sysfs
0021  * @hwirq_base: base index for IRQ for this module (-1 if no IRQs)
0022  * @nirqs:  how many interrupts does the shift register provide
0023  * @desc:   module description for kernel log
0024  */
0025 static const struct {
0026     const char *name;
0027     int hwirq_base;
0028     int nirqs;
0029     const char *desc;
0030 } mox_module_table[] = {
0031     /* do not change order of this array! */
0032     { NULL,      0,         0, NULL },
0033     { "sfp",    -1,         0, "MOX D (SFP cage)" },
0034     { "pci",    MOXTET_IRQ_PCI,     1, "MOX B (Mini-PCIe)" },
0035     { "topaz",  MOXTET_IRQ_TOPAZ,   1, "MOX C (4 port switch)" },
0036     { "peridot",    MOXTET_IRQ_PERIDOT(0),  1, "MOX E (8 port switch)" },
0037     { "usb3",   MOXTET_IRQ_USB3,    2, "MOX F (USB 3.0)" },
0038     { "pci-bridge", -1,         0, "MOX G (Mini-PCIe bridge)" },
0039 };
0040 
0041 static inline bool mox_module_known(unsigned int id)
0042 {
0043     return id >= TURRIS_MOX_MODULE_FIRST && id <= TURRIS_MOX_MODULE_LAST;
0044 }
0045 
0046 static inline const char *mox_module_name(unsigned int id)
0047 {
0048     if (mox_module_known(id))
0049         return mox_module_table[id].name;
0050     else
0051         return "unknown";
0052 }
0053 
0054 #define DEF_MODULE_ATTR(name, fmt, ...)                 \
0055 static ssize_t                              \
0056 module_##name##_show(struct device *dev, struct device_attribute *a,    \
0057              char *buf)                     \
0058 {                                   \
0059     struct moxtet_device *mdev = to_moxtet_device(dev);     \
0060     return sprintf(buf, (fmt), __VA_ARGS__);            \
0061 }                                   \
0062 static DEVICE_ATTR_RO(module_##name)
0063 
0064 DEF_MODULE_ATTR(id, "0x%x\n", mdev->id);
0065 DEF_MODULE_ATTR(name, "%s\n", mox_module_name(mdev->id));
0066 DEF_MODULE_ATTR(description, "%s\n",
0067         mox_module_known(mdev->id) ? mox_module_table[mdev->id].desc
0068                        : "");
0069 
0070 static struct attribute *moxtet_dev_attrs[] = {
0071     &dev_attr_module_id.attr,
0072     &dev_attr_module_name.attr,
0073     &dev_attr_module_description.attr,
0074     NULL,
0075 };
0076 
0077 static const struct attribute_group moxtet_dev_group = {
0078     .attrs = moxtet_dev_attrs,
0079 };
0080 
0081 static const struct attribute_group *moxtet_dev_groups[] = {
0082     &moxtet_dev_group,
0083     NULL,
0084 };
0085 
0086 static int moxtet_match(struct device *dev, struct device_driver *drv)
0087 {
0088     struct moxtet_device *mdev = to_moxtet_device(dev);
0089     struct moxtet_driver *tdrv = to_moxtet_driver(drv);
0090     const enum turris_mox_module_id *t;
0091 
0092     if (of_driver_match_device(dev, drv))
0093         return 1;
0094 
0095     if (!tdrv->id_table)
0096         return 0;
0097 
0098     for (t = tdrv->id_table; *t; ++t)
0099         if (*t == mdev->id)
0100             return 1;
0101 
0102     return 0;
0103 }
0104 
0105 static struct bus_type moxtet_bus_type = {
0106     .name       = "moxtet",
0107     .dev_groups = moxtet_dev_groups,
0108     .match      = moxtet_match,
0109 };
0110 
0111 int __moxtet_register_driver(struct module *owner,
0112                  struct moxtet_driver *mdrv)
0113 {
0114     mdrv->driver.owner = owner;
0115     mdrv->driver.bus = &moxtet_bus_type;
0116     return driver_register(&mdrv->driver);
0117 }
0118 EXPORT_SYMBOL_GPL(__moxtet_register_driver);
0119 
0120 static int moxtet_dev_check(struct device *dev, void *data)
0121 {
0122     struct moxtet_device *mdev = to_moxtet_device(dev);
0123     struct moxtet_device *new_dev = data;
0124 
0125     if (mdev->moxtet == new_dev->moxtet && mdev->id == new_dev->id &&
0126         mdev->idx == new_dev->idx)
0127         return -EBUSY;
0128     return 0;
0129 }
0130 
0131 static void moxtet_dev_release(struct device *dev)
0132 {
0133     struct moxtet_device *mdev = to_moxtet_device(dev);
0134 
0135     put_device(mdev->moxtet->dev);
0136     kfree(mdev);
0137 }
0138 
0139 static struct moxtet_device *
0140 moxtet_alloc_device(struct moxtet *moxtet)
0141 {
0142     struct moxtet_device *dev;
0143 
0144     if (!get_device(moxtet->dev))
0145         return NULL;
0146 
0147     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0148     if (!dev) {
0149         put_device(moxtet->dev);
0150         return NULL;
0151     }
0152 
0153     dev->moxtet = moxtet;
0154     dev->dev.parent = moxtet->dev;
0155     dev->dev.bus = &moxtet_bus_type;
0156     dev->dev.release = moxtet_dev_release;
0157 
0158     device_initialize(&dev->dev);
0159 
0160     return dev;
0161 }
0162 
0163 static int moxtet_add_device(struct moxtet_device *dev)
0164 {
0165     static DEFINE_MUTEX(add_mutex);
0166     int ret;
0167 
0168     if (dev->idx >= TURRIS_MOX_MAX_MODULES || dev->id > 0xf)
0169         return -EINVAL;
0170 
0171     dev_set_name(&dev->dev, "moxtet-%s.%u", mox_module_name(dev->id),
0172              dev->idx);
0173 
0174     mutex_lock(&add_mutex);
0175 
0176     ret = bus_for_each_dev(&moxtet_bus_type, NULL, dev,
0177                    moxtet_dev_check);
0178     if (ret)
0179         goto done;
0180 
0181     ret = device_add(&dev->dev);
0182     if (ret < 0)
0183         dev_err(dev->moxtet->dev, "can't add %s, status %d\n",
0184             dev_name(dev->moxtet->dev), ret);
0185 
0186 done:
0187     mutex_unlock(&add_mutex);
0188     return ret;
0189 }
0190 
0191 static int __unregister(struct device *dev, void *null)
0192 {
0193     if (dev->of_node) {
0194         of_node_clear_flag(dev->of_node, OF_POPULATED);
0195         of_node_put(dev->of_node);
0196     }
0197 
0198     device_unregister(dev);
0199 
0200     return 0;
0201 }
0202 
0203 static struct moxtet_device *
0204 of_register_moxtet_device(struct moxtet *moxtet, struct device_node *nc)
0205 {
0206     struct moxtet_device *dev;
0207     u32 val;
0208     int ret;
0209 
0210     dev = moxtet_alloc_device(moxtet);
0211     if (!dev) {
0212         dev_err(moxtet->dev,
0213             "Moxtet device alloc error for %pOF\n", nc);
0214         return ERR_PTR(-ENOMEM);
0215     }
0216 
0217     ret = of_property_read_u32(nc, "reg", &val);
0218     if (ret) {
0219         dev_err(moxtet->dev, "%pOF has no valid 'reg' property (%d)\n",
0220             nc, ret);
0221         goto err_put;
0222     }
0223 
0224     dev->idx = val;
0225 
0226     if (dev->idx >= TURRIS_MOX_MAX_MODULES) {
0227         dev_err(moxtet->dev, "%pOF Moxtet address 0x%x out of range\n",
0228             nc, dev->idx);
0229         ret = -EINVAL;
0230         goto err_put;
0231     }
0232 
0233     dev->id = moxtet->modules[dev->idx];
0234 
0235     if (!dev->id) {
0236         dev_err(moxtet->dev, "%pOF Moxtet address 0x%x is empty\n", nc,
0237             dev->idx);
0238         ret = -ENODEV;
0239         goto err_put;
0240     }
0241 
0242     of_node_get(nc);
0243     dev->dev.of_node = nc;
0244 
0245     ret = moxtet_add_device(dev);
0246     if (ret) {
0247         dev_err(moxtet->dev,
0248             "Moxtet device register error for %pOF\n", nc);
0249         of_node_put(nc);
0250         goto err_put;
0251     }
0252 
0253     return dev;
0254 
0255 err_put:
0256     put_device(&dev->dev);
0257     return ERR_PTR(ret);
0258 }
0259 
0260 static void of_register_moxtet_devices(struct moxtet *moxtet)
0261 {
0262     struct moxtet_device *dev;
0263     struct device_node *nc;
0264 
0265     if (!moxtet->dev->of_node)
0266         return;
0267 
0268     for_each_available_child_of_node(moxtet->dev->of_node, nc) {
0269         if (of_node_test_and_set_flag(nc, OF_POPULATED))
0270             continue;
0271         dev = of_register_moxtet_device(moxtet, nc);
0272         if (IS_ERR(dev)) {
0273             dev_warn(moxtet->dev,
0274                  "Failed to create Moxtet device for %pOF\n",
0275                  nc);
0276             of_node_clear_flag(nc, OF_POPULATED);
0277         }
0278     }
0279 }
0280 
0281 static void
0282 moxtet_register_devices_from_topology(struct moxtet *moxtet)
0283 {
0284     struct moxtet_device *dev;
0285     int i, ret;
0286 
0287     for (i = 0; i < moxtet->count; ++i) {
0288         dev = moxtet_alloc_device(moxtet);
0289         if (!dev) {
0290             dev_err(moxtet->dev, "Moxtet device %u alloc error\n",
0291                 i);
0292             continue;
0293         }
0294 
0295         dev->idx = i;
0296         dev->id = moxtet->modules[i];
0297 
0298         ret = moxtet_add_device(dev);
0299         if (ret && ret != -EBUSY) {
0300             put_device(&dev->dev);
0301             dev_err(moxtet->dev,
0302                 "Moxtet device %u register error: %i\n", i,
0303                 ret);
0304         }
0305     }
0306 }
0307 
0308 /*
0309  * @nsame:  how many modules with same id are already in moxtet->modules
0310  */
0311 static int moxtet_set_irq(struct moxtet *moxtet, int idx, int id, int nsame)
0312 {
0313     int i, first;
0314     struct moxtet_irqpos *pos;
0315 
0316     first = mox_module_table[id].hwirq_base +
0317         nsame * mox_module_table[id].nirqs;
0318 
0319     if (first + mox_module_table[id].nirqs > MOXTET_NIRQS)
0320         return -EINVAL;
0321 
0322     for (i = 0; i < mox_module_table[id].nirqs; ++i) {
0323         pos = &moxtet->irq.position[first + i];
0324         pos->idx = idx;
0325         pos->bit = i;
0326         moxtet->irq.exists |= BIT(first + i);
0327     }
0328 
0329     return 0;
0330 }
0331 
0332 static int moxtet_find_topology(struct moxtet *moxtet)
0333 {
0334     u8 buf[TURRIS_MOX_MAX_MODULES];
0335     int cnts[TURRIS_MOX_MODULE_LAST];
0336     int i, ret;
0337 
0338     memset(cnts, 0, sizeof(cnts));
0339 
0340     ret = spi_read(to_spi_device(moxtet->dev), buf, TURRIS_MOX_MAX_MODULES);
0341     if (ret < 0)
0342         return ret;
0343 
0344     if (buf[0] == TURRIS_MOX_CPU_ID_EMMC) {
0345         dev_info(moxtet->dev, "Found MOX A (eMMC CPU) module\n");
0346     } else if (buf[0] == TURRIS_MOX_CPU_ID_SD) {
0347         dev_info(moxtet->dev, "Found MOX A (CPU) module\n");
0348     } else {
0349         dev_err(moxtet->dev, "Invalid Turris MOX A CPU module 0x%02x\n",
0350             buf[0]);
0351         return -ENODEV;
0352     }
0353 
0354     moxtet->count = 0;
0355 
0356     for (i = 1; i < TURRIS_MOX_MAX_MODULES; ++i) {
0357         int id;
0358 
0359         if (buf[i] == 0xff)
0360             break;
0361 
0362         id = buf[i] & 0xf;
0363 
0364         moxtet->modules[i-1] = id;
0365         ++moxtet->count;
0366 
0367         if (mox_module_known(id)) {
0368             dev_info(moxtet->dev, "Found %s module\n",
0369                  mox_module_table[id].desc);
0370 
0371             if (moxtet_set_irq(moxtet, i-1, id, cnts[id]++) < 0)
0372                 dev_err(moxtet->dev,
0373                     "  Cannot set IRQ for module %s\n",
0374                     mox_module_table[id].desc);
0375         } else {
0376             dev_warn(moxtet->dev,
0377                  "Unknown Moxtet module found (ID 0x%02x)\n",
0378                  id);
0379         }
0380     }
0381 
0382     return 0;
0383 }
0384 
0385 static int moxtet_spi_read(struct moxtet *moxtet, u8 *buf)
0386 {
0387     struct spi_transfer xfer = {
0388         .rx_buf = buf,
0389         .tx_buf = moxtet->tx,
0390         .len = moxtet->count + 1
0391     };
0392     int ret;
0393 
0394     mutex_lock(&moxtet->lock);
0395 
0396     ret = spi_sync_transfer(to_spi_device(moxtet->dev), &xfer, 1);
0397 
0398     mutex_unlock(&moxtet->lock);
0399 
0400     return ret;
0401 }
0402 
0403 int moxtet_device_read(struct device *dev)
0404 {
0405     struct moxtet_device *mdev = to_moxtet_device(dev);
0406     struct moxtet *moxtet = mdev->moxtet;
0407     u8 buf[TURRIS_MOX_MAX_MODULES];
0408     int ret;
0409 
0410     if (mdev->idx >= moxtet->count)
0411         return -EINVAL;
0412 
0413     ret = moxtet_spi_read(moxtet, buf);
0414     if (ret < 0)
0415         return ret;
0416 
0417     return buf[mdev->idx + 1] >> 4;
0418 }
0419 EXPORT_SYMBOL_GPL(moxtet_device_read);
0420 
0421 int moxtet_device_write(struct device *dev, u8 val)
0422 {
0423     struct moxtet_device *mdev = to_moxtet_device(dev);
0424     struct moxtet *moxtet = mdev->moxtet;
0425     int ret;
0426 
0427     if (mdev->idx >= moxtet->count)
0428         return -EINVAL;
0429 
0430     mutex_lock(&moxtet->lock);
0431 
0432     moxtet->tx[moxtet->count - mdev->idx] = val;
0433 
0434     ret = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
0435             moxtet->count + 1);
0436 
0437     mutex_unlock(&moxtet->lock);
0438 
0439     return ret;
0440 }
0441 EXPORT_SYMBOL_GPL(moxtet_device_write);
0442 
0443 int moxtet_device_written(struct device *dev)
0444 {
0445     struct moxtet_device *mdev = to_moxtet_device(dev);
0446     struct moxtet *moxtet = mdev->moxtet;
0447 
0448     if (mdev->idx >= moxtet->count)
0449         return -EINVAL;
0450 
0451     return moxtet->tx[moxtet->count - mdev->idx];
0452 }
0453 EXPORT_SYMBOL_GPL(moxtet_device_written);
0454 
0455 #ifdef CONFIG_DEBUG_FS
0456 static int moxtet_debug_open(struct inode *inode, struct file *file)
0457 {
0458     file->private_data = inode->i_private;
0459 
0460     return nonseekable_open(inode, file);
0461 }
0462 
0463 static ssize_t input_read(struct file *file, char __user *buf, size_t len,
0464               loff_t *ppos)
0465 {
0466     struct moxtet *moxtet = file->private_data;
0467     u8 bin[TURRIS_MOX_MAX_MODULES];
0468     u8 hex[sizeof(bin) * 2 + 1];
0469     int ret, n;
0470 
0471     ret = moxtet_spi_read(moxtet, bin);
0472     if (ret < 0)
0473         return ret;
0474 
0475     n = moxtet->count + 1;
0476     bin2hex(hex, bin, n);
0477 
0478     hex[2*n] = '\n';
0479 
0480     return simple_read_from_buffer(buf, len, ppos, hex, 2*n + 1);
0481 }
0482 
0483 static const struct file_operations input_fops = {
0484     .owner  = THIS_MODULE,
0485     .open   = moxtet_debug_open,
0486     .read   = input_read,
0487     .llseek = no_llseek,
0488 };
0489 
0490 static ssize_t output_read(struct file *file, char __user *buf, size_t len,
0491                loff_t *ppos)
0492 {
0493     struct moxtet *moxtet = file->private_data;
0494     u8 hex[TURRIS_MOX_MAX_MODULES * 2 + 1];
0495     u8 *p = hex;
0496     int i;
0497 
0498     mutex_lock(&moxtet->lock);
0499 
0500     for (i = 0; i < moxtet->count; ++i)
0501         p = hex_byte_pack(p, moxtet->tx[moxtet->count - i]);
0502 
0503     mutex_unlock(&moxtet->lock);
0504 
0505     *p++ = '\n';
0506 
0507     return simple_read_from_buffer(buf, len, ppos, hex, p - hex);
0508 }
0509 
0510 static ssize_t output_write(struct file *file, const char __user *buf,
0511                 size_t len, loff_t *ppos)
0512 {
0513     struct moxtet *moxtet = file->private_data;
0514     u8 bin[TURRIS_MOX_MAX_MODULES];
0515     u8 hex[sizeof(bin) * 2 + 1];
0516     ssize_t res;
0517     loff_t dummy = 0;
0518     int err, i;
0519 
0520     if (len > 2 * moxtet->count + 1 || len < 2 * moxtet->count)
0521         return -EINVAL;
0522 
0523     res = simple_write_to_buffer(hex, sizeof(hex), &dummy, buf, len);
0524     if (res < 0)
0525         return res;
0526 
0527     if (len % 2 == 1 && hex[len - 1] != '\n')
0528         return -EINVAL;
0529 
0530     err = hex2bin(bin, hex, moxtet->count);
0531     if (err < 0)
0532         return -EINVAL;
0533 
0534     mutex_lock(&moxtet->lock);
0535 
0536     for (i = 0; i < moxtet->count; ++i)
0537         moxtet->tx[moxtet->count - i] = bin[i];
0538 
0539     err = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
0540             moxtet->count + 1);
0541 
0542     mutex_unlock(&moxtet->lock);
0543 
0544     return err < 0 ? err : len;
0545 }
0546 
0547 static const struct file_operations output_fops = {
0548     .owner  = THIS_MODULE,
0549     .open   = moxtet_debug_open,
0550     .read   = output_read,
0551     .write  = output_write,
0552     .llseek = no_llseek,
0553 };
0554 
0555 static int moxtet_register_debugfs(struct moxtet *moxtet)
0556 {
0557     struct dentry *root, *entry;
0558 
0559     root = debugfs_create_dir("moxtet", NULL);
0560 
0561     if (IS_ERR(root))
0562         return PTR_ERR(root);
0563 
0564     entry = debugfs_create_file_unsafe("input", 0444, root, moxtet,
0565                        &input_fops);
0566     if (IS_ERR(entry))
0567         goto err_remove;
0568 
0569     entry = debugfs_create_file_unsafe("output", 0644, root, moxtet,
0570                        &output_fops);
0571     if (IS_ERR(entry))
0572         goto err_remove;
0573 
0574     moxtet->debugfs_root = root;
0575 
0576     return 0;
0577 err_remove:
0578     debugfs_remove_recursive(root);
0579     return PTR_ERR(entry);
0580 }
0581 
0582 static void moxtet_unregister_debugfs(struct moxtet *moxtet)
0583 {
0584     debugfs_remove_recursive(moxtet->debugfs_root);
0585 }
0586 #else
0587 static inline int moxtet_register_debugfs(struct moxtet *moxtet)
0588 {
0589     return 0;
0590 }
0591 
0592 static inline void moxtet_unregister_debugfs(struct moxtet *moxtet)
0593 {
0594 }
0595 #endif
0596 
0597 static int moxtet_irq_domain_map(struct irq_domain *d, unsigned int irq,
0598                  irq_hw_number_t hw)
0599 {
0600     struct moxtet *moxtet = d->host_data;
0601 
0602     if (hw >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(hw))) {
0603         dev_err(moxtet->dev, "Invalid hw irq number\n");
0604         return -EINVAL;
0605     }
0606 
0607     irq_set_chip_data(irq, d->host_data);
0608     irq_set_chip_and_handler(irq, &moxtet->irq.chip, handle_level_irq);
0609 
0610     return 0;
0611 }
0612 
0613 static int moxtet_irq_domain_xlate(struct irq_domain *d,
0614                    struct device_node *ctrlr,
0615                    const u32 *intspec, unsigned int intsize,
0616                    unsigned long *out_hwirq,
0617                    unsigned int *out_type)
0618 {
0619     struct moxtet *moxtet = d->host_data;
0620     int irq;
0621 
0622     if (WARN_ON(intsize < 1))
0623         return -EINVAL;
0624 
0625     irq = intspec[0];
0626 
0627     if (irq >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(irq)))
0628         return -EINVAL;
0629 
0630     *out_hwirq = irq;
0631     *out_type = IRQ_TYPE_NONE;
0632     return 0;
0633 }
0634 
0635 static const struct irq_domain_ops moxtet_irq_domain = {
0636     .map = moxtet_irq_domain_map,
0637     .xlate = moxtet_irq_domain_xlate,
0638 };
0639 
0640 static void moxtet_irq_mask(struct irq_data *d)
0641 {
0642     struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
0643 
0644     moxtet->irq.masked |= BIT(d->hwirq);
0645 }
0646 
0647 static void moxtet_irq_unmask(struct irq_data *d)
0648 {
0649     struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
0650 
0651     moxtet->irq.masked &= ~BIT(d->hwirq);
0652 }
0653 
0654 static void moxtet_irq_print_chip(struct irq_data *d, struct seq_file *p)
0655 {
0656     struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
0657     struct moxtet_irqpos *pos = &moxtet->irq.position[d->hwirq];
0658     int id;
0659 
0660     id = moxtet->modules[pos->idx];
0661 
0662     seq_printf(p, " moxtet-%s.%i#%i", mox_module_name(id), pos->idx,
0663            pos->bit);
0664 }
0665 
0666 static const struct irq_chip moxtet_irq_chip = {
0667     .name           = "moxtet",
0668     .irq_mask       = moxtet_irq_mask,
0669     .irq_unmask     = moxtet_irq_unmask,
0670     .irq_print_chip     = moxtet_irq_print_chip,
0671 };
0672 
0673 static int moxtet_irq_read(struct moxtet *moxtet, unsigned long *map)
0674 {
0675     struct moxtet_irqpos *pos = moxtet->irq.position;
0676     u8 buf[TURRIS_MOX_MAX_MODULES];
0677     int i, ret;
0678 
0679     ret = moxtet_spi_read(moxtet, buf);
0680     if (ret < 0)
0681         return ret;
0682 
0683     *map = 0;
0684 
0685     for_each_set_bit(i, &moxtet->irq.exists, MOXTET_NIRQS) {
0686         if (!(buf[pos[i].idx + 1] & BIT(4 + pos[i].bit)))
0687             set_bit(i, map);
0688     }
0689 
0690     return 0;
0691 }
0692 
0693 static irqreturn_t moxtet_irq_thread_fn(int irq, void *data)
0694 {
0695     struct moxtet *moxtet = data;
0696     unsigned long set;
0697     int nhandled = 0, i, sub_irq, ret;
0698 
0699     ret = moxtet_irq_read(moxtet, &set);
0700     if (ret < 0)
0701         goto out;
0702 
0703     set &= ~moxtet->irq.masked;
0704 
0705     do {
0706         for_each_set_bit(i, &set, MOXTET_NIRQS) {
0707             sub_irq = irq_find_mapping(moxtet->irq.domain, i);
0708             handle_nested_irq(sub_irq);
0709             dev_dbg(moxtet->dev, "%i irq\n", i);
0710             ++nhandled;
0711         }
0712 
0713         ret = moxtet_irq_read(moxtet, &set);
0714         if (ret < 0)
0715             goto out;
0716 
0717         set &= ~moxtet->irq.masked;
0718     } while (set);
0719 
0720 out:
0721     return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
0722 }
0723 
0724 static void moxtet_irq_free(struct moxtet *moxtet)
0725 {
0726     int i, irq;
0727 
0728     for (i = 0; i < MOXTET_NIRQS; ++i) {
0729         if (moxtet->irq.exists & BIT(i)) {
0730             irq = irq_find_mapping(moxtet->irq.domain, i);
0731             irq_dispose_mapping(irq);
0732         }
0733     }
0734 
0735     irq_domain_remove(moxtet->irq.domain);
0736 }
0737 
0738 static int moxtet_irq_setup(struct moxtet *moxtet)
0739 {
0740     int i, ret;
0741 
0742     moxtet->irq.domain = irq_domain_add_simple(moxtet->dev->of_node,
0743                            MOXTET_NIRQS, 0,
0744                            &moxtet_irq_domain, moxtet);
0745     if (moxtet->irq.domain == NULL) {
0746         dev_err(moxtet->dev, "Could not add IRQ domain\n");
0747         return -ENOMEM;
0748     }
0749 
0750     for (i = 0; i < MOXTET_NIRQS; ++i)
0751         if (moxtet->irq.exists & BIT(i))
0752             irq_create_mapping(moxtet->irq.domain, i);
0753 
0754     moxtet->irq.chip = moxtet_irq_chip;
0755     moxtet->irq.masked = ~0;
0756 
0757     ret = request_threaded_irq(moxtet->dev_irq, NULL, moxtet_irq_thread_fn,
0758                    IRQF_ONESHOT, "moxtet", moxtet);
0759     if (ret < 0)
0760         goto err_free;
0761 
0762     return 0;
0763 
0764 err_free:
0765     moxtet_irq_free(moxtet);
0766     return ret;
0767 }
0768 
0769 static int moxtet_probe(struct spi_device *spi)
0770 {
0771     struct moxtet *moxtet;
0772     int ret;
0773 
0774     ret = spi_setup(spi);
0775     if (ret < 0)
0776         return ret;
0777 
0778     moxtet = devm_kzalloc(&spi->dev, sizeof(struct moxtet),
0779                   GFP_KERNEL);
0780     if (!moxtet)
0781         return -ENOMEM;
0782 
0783     moxtet->dev = &spi->dev;
0784     spi_set_drvdata(spi, moxtet);
0785 
0786     mutex_init(&moxtet->lock);
0787 
0788     moxtet->dev_irq = of_irq_get(moxtet->dev->of_node, 0);
0789     if (moxtet->dev_irq == -EPROBE_DEFER)
0790         return -EPROBE_DEFER;
0791 
0792     if (moxtet->dev_irq <= 0) {
0793         dev_err(moxtet->dev, "No IRQ resource found\n");
0794         return -ENXIO;
0795     }
0796 
0797     ret = moxtet_find_topology(moxtet);
0798     if (ret < 0)
0799         return ret;
0800 
0801     if (moxtet->irq.exists) {
0802         ret = moxtet_irq_setup(moxtet);
0803         if (ret < 0)
0804             return ret;
0805     }
0806 
0807     of_register_moxtet_devices(moxtet);
0808     moxtet_register_devices_from_topology(moxtet);
0809 
0810     ret = moxtet_register_debugfs(moxtet);
0811     if (ret < 0)
0812         dev_warn(moxtet->dev, "Failed creating debugfs entries: %i\n",
0813              ret);
0814 
0815     return 0;
0816 }
0817 
0818 static void moxtet_remove(struct spi_device *spi)
0819 {
0820     struct moxtet *moxtet = spi_get_drvdata(spi);
0821 
0822     free_irq(moxtet->dev_irq, moxtet);
0823 
0824     moxtet_irq_free(moxtet);
0825 
0826     moxtet_unregister_debugfs(moxtet);
0827 
0828     device_for_each_child(moxtet->dev, NULL, __unregister);
0829 
0830     mutex_destroy(&moxtet->lock);
0831 }
0832 
0833 static const struct of_device_id moxtet_dt_ids[] = {
0834     { .compatible = "cznic,moxtet" },
0835     {},
0836 };
0837 MODULE_DEVICE_TABLE(of, moxtet_dt_ids);
0838 
0839 static struct spi_driver moxtet_spi_driver = {
0840     .driver = {
0841         .name       = "moxtet",
0842         .of_match_table = moxtet_dt_ids,
0843     },
0844     .probe      = moxtet_probe,
0845     .remove     = moxtet_remove,
0846 };
0847 
0848 static int __init moxtet_init(void)
0849 {
0850     int ret;
0851 
0852     ret = bus_register(&moxtet_bus_type);
0853     if (ret < 0) {
0854         pr_err("moxtet bus registration failed: %d\n", ret);
0855         goto error;
0856     }
0857 
0858     ret = spi_register_driver(&moxtet_spi_driver);
0859     if (ret < 0) {
0860         pr_err("moxtet spi driver registration failed: %d\n", ret);
0861         goto error_bus;
0862     }
0863 
0864     return 0;
0865 
0866 error_bus:
0867     bus_unregister(&moxtet_bus_type);
0868 error:
0869     return ret;
0870 }
0871 postcore_initcall_sync(moxtet_init);
0872 
0873 static void __exit moxtet_exit(void)
0874 {
0875     spi_unregister_driver(&moxtet_spi_driver);
0876     bus_unregister(&moxtet_bus_type);
0877 }
0878 module_exit(moxtet_exit);
0879 
0880 MODULE_AUTHOR("Marek Behun <kabel@kernel.org>");
0881 MODULE_DESCRIPTION("CZ.NIC's Turris Mox module configuration bus");
0882 MODULE_LICENSE("GPL v2");