moxtet
if (mdev->moxtet == new_dev->moxtet && mdev->id == new_dev->id &&
put_device(mdev->moxtet->dev);
moxtet_alloc_device(struct moxtet *moxtet)
if (!get_device(moxtet->dev))
put_device(moxtet->dev);
dev->moxtet = moxtet;
dev->dev.parent = moxtet->dev;
dev_err(dev->moxtet->dev, "can't add %s, status %d\n",
dev_name(dev->moxtet->dev), ret);
of_register_moxtet_device(struct moxtet *moxtet, struct device_node *nc)
dev = moxtet_alloc_device(moxtet);
dev_err(moxtet->dev,
dev_err(moxtet->dev, "%pOF has no valid 'reg' property (%d)\n",
dev_err(moxtet->dev, "%pOF Moxtet address 0x%x out of range\n",
dev->id = moxtet->modules[dev->idx];
dev_err(moxtet->dev, "%pOF Moxtet address 0x%x is empty\n", nc,
dev_err(moxtet->dev,
static void of_register_moxtet_devices(struct moxtet *moxtet)
if (!moxtet->dev->of_node)
for_each_available_child_of_node(moxtet->dev->of_node, nc) {
dev = of_register_moxtet_device(moxtet, nc);
dev_warn(moxtet->dev,
moxtet_register_devices_from_topology(struct moxtet *moxtet)
for (i = 0; i < moxtet->count; ++i) {
dev = moxtet_alloc_device(moxtet);
dev_err(moxtet->dev, "Moxtet device %u alloc error\n",
dev->id = moxtet->modules[i];
dev_err(moxtet->dev,
static int moxtet_set_irq(struct moxtet *moxtet, int idx, int id, int nsame)
pos = &moxtet->irq.position[first + i];
moxtet->irq.exists |= BIT(first + i);
static int moxtet_find_topology(struct moxtet *moxtet)
ret = spi_read(to_spi_device(moxtet->dev), buf, TURRIS_MOX_MAX_MODULES);
dev_info(moxtet->dev, "Found MOX A (eMMC CPU) module\n");
dev_info(moxtet->dev, "Found MOX A (CPU) module\n");
dev_err(moxtet->dev, "Invalid Turris MOX A CPU module 0x%02x\n",
moxtet->count = 0;
moxtet->modules[i-1] = id;
++moxtet->count;
dev_info(moxtet->dev, "Found %s module\n",
if (moxtet_set_irq(moxtet, i-1, id, cnts[id]++) < 0)
dev_err(moxtet->dev,
dev_warn(moxtet->dev,
static int moxtet_spi_read(struct moxtet *moxtet, u8 *buf)
.tx_buf = moxtet->tx,
.len = moxtet->count + 1
mutex_lock(&moxtet->lock);
ret = spi_sync_transfer(to_spi_device(moxtet->dev), &xfer, 1);
mutex_unlock(&moxtet->lock);
struct moxtet *moxtet = mdev->moxtet;
if (mdev->idx >= moxtet->count)
ret = moxtet_spi_read(moxtet, buf);
struct moxtet *moxtet = mdev->moxtet;
if (mdev->idx >= moxtet->count)
mutex_lock(&moxtet->lock);
moxtet->tx[moxtet->count - mdev->idx] = val;
ret = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
moxtet->count + 1);
mutex_unlock(&moxtet->lock);
struct moxtet *moxtet = mdev->moxtet;
if (mdev->idx >= moxtet->count)
return moxtet->tx[moxtet->count - mdev->idx];
struct moxtet *moxtet = file->private_data;
ret = moxtet_spi_read(moxtet, bin);
n = moxtet->count + 1;
struct moxtet *moxtet = file->private_data;
mutex_lock(&moxtet->lock);
for (i = 0; i < moxtet->count; ++i)
p = hex_byte_pack(p, moxtet->tx[moxtet->count - i]);
mutex_unlock(&moxtet->lock);
struct moxtet *moxtet = file->private_data;
if (len > 2 * moxtet->count + 1 || len < 2 * moxtet->count)
err = hex2bin(bin, hex, moxtet->count);
mutex_lock(&moxtet->lock);
for (i = 0; i < moxtet->count; ++i)
moxtet->tx[moxtet->count - i] = bin[i];
err = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
moxtet->count + 1);
mutex_unlock(&moxtet->lock);
static int moxtet_register_debugfs(struct moxtet *moxtet)
entry = debugfs_create_file_unsafe("input", 0444, root, moxtet,
entry = debugfs_create_file_unsafe("output", 0644, root, moxtet,
moxtet->debugfs_root = root;
static void moxtet_unregister_debugfs(struct moxtet *moxtet)
debugfs_remove_recursive(moxtet->debugfs_root);
static inline int moxtet_register_debugfs(struct moxtet *moxtet)
static inline void moxtet_unregister_debugfs(struct moxtet *moxtet)
struct moxtet *moxtet = d->host_data;
if (hw >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(hw))) {
dev_err(moxtet->dev, "Invalid hw irq number\n");
irq_set_chip_and_handler(irq, &moxtet->irq.chip, handle_level_irq);
struct moxtet *moxtet = d->host_data;
if (irq >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(irq)))
struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
moxtet->irq.masked |= BIT(d->hwirq);
struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
moxtet->irq.masked &= ~BIT(d->hwirq);
struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
struct moxtet_irqpos *pos = &moxtet->irq.position[d->hwirq];
id = moxtet->modules[pos->idx];
static int moxtet_irq_read(struct moxtet *moxtet, unsigned long *map)
struct moxtet_irqpos *pos = moxtet->irq.position;
ret = moxtet_spi_read(moxtet, buf);
for_each_set_bit(i, &moxtet->irq.exists, MOXTET_NIRQS) {
struct moxtet *moxtet = data;
ret = moxtet_irq_read(moxtet, &set);
set &= ~moxtet->irq.masked;
sub_irq = irq_find_mapping(moxtet->irq.domain, i);
dev_dbg(moxtet->dev, "%i irq\n", i);
ret = moxtet_irq_read(moxtet, &set);
set &= ~moxtet->irq.masked;
static void moxtet_irq_free(struct moxtet *moxtet)
if (moxtet->irq.exists & BIT(i)) {
irq = irq_find_mapping(moxtet->irq.domain, i);
irq_domain_remove(moxtet->irq.domain);
static int moxtet_irq_setup(struct moxtet *moxtet)
moxtet->irq.domain = irq_domain_create_simple(dev_fwnode(moxtet->dev), MOXTET_NIRQS, 0,
&moxtet_irq_domain, moxtet);
if (moxtet->irq.domain == NULL) {
dev_err(moxtet->dev, "Could not add IRQ domain\n");
if (moxtet->irq.exists & BIT(i))
irq_create_mapping(moxtet->irq.domain, i);
moxtet->irq.chip = moxtet_irq_chip;
moxtet->irq.masked = ~0;
ret = request_threaded_irq(moxtet->dev_irq, NULL, moxtet_irq_thread_fn,
IRQF_SHARED | IRQF_ONESHOT, "moxtet", moxtet);
moxtet_irq_free(moxtet);
struct moxtet *moxtet;
moxtet = devm_kzalloc(&spi->dev, sizeof(struct moxtet),
if (!moxtet)
moxtet->dev = &spi->dev;
spi_set_drvdata(spi, moxtet);
mutex_init(&moxtet->lock);
moxtet->dev_irq = of_irq_get(moxtet->dev->of_node, 0);
if (moxtet->dev_irq == -EPROBE_DEFER)
if (moxtet->dev_irq <= 0) {
dev_err(moxtet->dev, "No IRQ resource found\n");
ret = moxtet_find_topology(moxtet);
if (moxtet->irq.exists) {
ret = moxtet_irq_setup(moxtet);
of_register_moxtet_devices(moxtet);
moxtet_register_devices_from_topology(moxtet);
ret = moxtet_register_debugfs(moxtet);
dev_warn(moxtet->dev, "Failed creating debugfs entries: %i\n",
struct moxtet *moxtet = spi_get_drvdata(spi);
free_irq(moxtet->dev_irq, moxtet);
moxtet_irq_free(moxtet);
moxtet_unregister_debugfs(moxtet);
device_for_each_child(moxtet->dev, NULL, __unregister);
mutex_destroy(&moxtet->lock);
struct moxtet *moxtet;