Lines Matching refs:moxtet

8 #include <dt-bindings/bus/moxtet.h>
13 #include <linux/moxtet.h>
106 .name = "moxtet",
125 if (mdev->moxtet == new_dev->moxtet && mdev->id == new_dev->id &&
135 put_device(mdev->moxtet->dev);
140 moxtet_alloc_device(struct moxtet *moxtet)
144 if (!get_device(moxtet->dev))
149 put_device(moxtet->dev);
153 dev->moxtet = moxtet;
154 dev->dev.parent = moxtet->dev;
171 dev_set_name(&dev->dev, "moxtet-%s.%u", mox_module_name(dev->id),
183 dev_err(dev->moxtet->dev, "can't add %s, status %d\n",
184 dev_name(dev->moxtet->dev), ret);
204 of_register_moxtet_device(struct moxtet *moxtet, struct device_node *nc)
210 dev = moxtet_alloc_device(moxtet);
212 dev_err(moxtet->dev,
219 dev_err(moxtet->dev, "%pOF has no valid 'reg' property (%d)\n",
227 dev_err(moxtet->dev, "%pOF Moxtet address 0x%x out of range\n",
233 dev->id = moxtet->modules[dev->idx];
236 dev_err(moxtet->dev, "%pOF Moxtet address 0x%x is empty\n", nc,
247 dev_err(moxtet->dev,
260 static void of_register_moxtet_devices(struct moxtet *moxtet)
265 if (!moxtet->dev->of_node)
268 for_each_available_child_of_node(moxtet->dev->of_node, nc) {
271 dev = of_register_moxtet_device(moxtet, nc);
273 dev_warn(moxtet->dev,
282 moxtet_register_devices_from_topology(struct moxtet *moxtet)
287 for (i = 0; i < moxtet->count; ++i) {
288 dev = moxtet_alloc_device(moxtet);
290 dev_err(moxtet->dev, "Moxtet device %u alloc error\n",
296 dev->id = moxtet->modules[i];
301 dev_err(moxtet->dev,
309 * @nsame: how many modules with same id are already in moxtet->modules
311 static int moxtet_set_irq(struct moxtet *moxtet, int idx, int id, int nsame)
323 pos = &moxtet->irq.position[first + i];
326 moxtet->irq.exists |= BIT(first + i);
332 static int moxtet_find_topology(struct moxtet *moxtet)
340 ret = spi_read(to_spi_device(moxtet->dev), buf, TURRIS_MOX_MAX_MODULES);
345 dev_info(moxtet->dev, "Found MOX A (eMMC CPU) module\n");
347 dev_info(moxtet->dev, "Found MOX A (CPU) module\n");
349 dev_err(moxtet->dev, "Invalid Turris MOX A CPU module 0x%02x\n",
354 moxtet->count = 0;
364 moxtet->modules[i-1] = id;
365 ++moxtet->count;
368 dev_info(moxtet->dev, "Found %s module\n",
371 if (moxtet_set_irq(moxtet, i-1, id, cnts[id]++) < 0)
372 dev_err(moxtet->dev,
376 dev_warn(moxtet->dev,
385 static int moxtet_spi_read(struct moxtet *moxtet, u8 *buf)
389 .tx_buf = moxtet->tx,
390 .len = moxtet->count + 1
394 mutex_lock(&moxtet->lock);
396 ret = spi_sync_transfer(to_spi_device(moxtet->dev), &xfer, 1);
398 mutex_unlock(&moxtet->lock);
406 struct moxtet *moxtet = mdev->moxtet;
410 if (mdev->idx >= moxtet->count)
413 ret = moxtet_spi_read(moxtet, buf);
424 struct moxtet *moxtet = mdev->moxtet;
427 if (mdev->idx >= moxtet->count)
430 mutex_lock(&moxtet->lock);
432 moxtet->tx[moxtet->count - mdev->idx] = val;
434 ret = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
435 moxtet->count + 1);
437 mutex_unlock(&moxtet->lock);
446 struct moxtet *moxtet = mdev->moxtet;
448 if (mdev->idx >= moxtet->count)
451 return moxtet->tx[moxtet->count - mdev->idx];
466 struct moxtet *moxtet = file->private_data;
471 ret = moxtet_spi_read(moxtet, bin);
475 n = moxtet->count + 1;
493 struct moxtet *moxtet = file->private_data;
498 mutex_lock(&moxtet->lock);
500 for (i = 0; i < moxtet->count; ++i)
501 p = hex_byte_pack(p, moxtet->tx[moxtet->count - i]);
503 mutex_unlock(&moxtet->lock);
513 struct moxtet *moxtet = file->private_data;
520 if (len > 2 * moxtet->count + 1 || len < 2 * moxtet->count)
530 err = hex2bin(bin, hex, moxtet->count);
534 mutex_lock(&moxtet->lock);
536 for (i = 0; i < moxtet->count; ++i)
537 moxtet->tx[moxtet->count - i] = bin[i];
539 err = spi_write(to_spi_device(moxtet->dev), moxtet->tx,
540 moxtet->count + 1);
542 mutex_unlock(&moxtet->lock);
555 static int moxtet_register_debugfs(struct moxtet *moxtet)
559 root = debugfs_create_dir("moxtet", NULL);
564 entry = debugfs_create_file_unsafe("input", 0444, root, moxtet,
569 entry = debugfs_create_file_unsafe("output", 0644, root, moxtet,
574 moxtet->debugfs_root = root;
582 static void moxtet_unregister_debugfs(struct moxtet *moxtet)
584 debugfs_remove_recursive(moxtet->debugfs_root);
587 static inline int moxtet_register_debugfs(struct moxtet *moxtet)
592 static inline void moxtet_unregister_debugfs(struct moxtet *moxtet)
600 struct moxtet *moxtet = d->host_data;
602 if (hw >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(hw))) {
603 dev_err(moxtet->dev, "Invalid hw irq number\n");
608 irq_set_chip_and_handler(irq, &moxtet->irq.chip, handle_level_irq);
619 struct moxtet *moxtet = d->host_data;
627 if (irq >= MOXTET_NIRQS || !(moxtet->irq.exists & BIT(irq)))
642 struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
644 moxtet->irq.masked |= BIT(d->hwirq);
649 struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
651 moxtet->irq.masked &= ~BIT(d->hwirq);
656 struct moxtet *moxtet = irq_data_get_irq_chip_data(d);
657 struct moxtet_irqpos *pos = &moxtet->irq.position[d->hwirq];
660 id = moxtet->modules[pos->idx];
662 seq_printf(p, " moxtet-%s.%i#%i", mox_module_name(id), pos->idx,
667 .name = "moxtet",
673 static int moxtet_irq_read(struct moxtet *moxtet, unsigned long *map)
675 struct moxtet_irqpos *pos = moxtet->irq.position;
679 ret = moxtet_spi_read(moxtet, buf);
685 for_each_set_bit(i, &moxtet->irq.exists, MOXTET_NIRQS) {
695 struct moxtet *moxtet = data;
699 ret = moxtet_irq_read(moxtet, &set);
703 set &= ~moxtet->irq.masked;
707 sub_irq = irq_find_mapping(moxtet->irq.domain, i);
709 dev_dbg(moxtet->dev, "%i irq\n", i);
713 ret = moxtet_irq_read(moxtet, &set);
717 set &= ~moxtet->irq.masked;
724 static void moxtet_irq_free(struct moxtet *moxtet)
729 if (moxtet->irq.exists & BIT(i)) {
730 irq = irq_find_mapping(moxtet->irq.domain, i);
735 irq_domain_remove(moxtet->irq.domain);
738 static int moxtet_irq_setup(struct moxtet *moxtet)
742 moxtet->irq.domain = irq_domain_add_simple(moxtet->dev->of_node,
744 &moxtet_irq_domain, moxtet);
745 if (moxtet->irq.domain == NULL) {
746 dev_err(moxtet->dev, "Could not add IRQ domain\n");
751 if (moxtet->irq.exists & BIT(i))
752 irq_create_mapping(moxtet->irq.domain, i);
754 moxtet->irq.chip = moxtet_irq_chip;
755 moxtet->irq.masked = ~0;
757 ret = request_threaded_irq(moxtet->dev_irq, NULL, moxtet_irq_thread_fn,
758 IRQF_SHARED | IRQF_ONESHOT, "moxtet", moxtet);
765 moxtet_irq_free(moxtet);
771 struct moxtet *moxtet;
778 moxtet = devm_kzalloc(&spi->dev, sizeof(struct moxtet),
780 if (!moxtet)
783 moxtet->dev = &spi->dev;
784 spi_set_drvdata(spi, moxtet);
786 mutex_init(&moxtet->lock);
788 moxtet->dev_irq = of_irq_get(moxtet->dev->of_node, 0);
789 if (moxtet->dev_irq == -EPROBE_DEFER)
792 if (moxtet->dev_irq <= 0) {
793 dev_err(moxtet->dev, "No IRQ resource found\n");
797 ret = moxtet_find_topology(moxtet);
801 if (moxtet->irq.exists) {
802 ret = moxtet_irq_setup(moxtet);
807 of_register_moxtet_devices(moxtet);
808 moxtet_register_devices_from_topology(moxtet);
810 ret = moxtet_register_debugfs(moxtet);
812 dev_warn(moxtet->dev, "Failed creating debugfs entries: %i\n",
820 struct moxtet *moxtet = spi_get_drvdata(spi);
822 free_irq(moxtet->dev_irq, moxtet);
824 moxtet_irq_free(moxtet);
826 moxtet_unregister_debugfs(moxtet);
828 device_for_each_child(moxtet->dev, NULL, __unregister);
830 mutex_destroy(&moxtet->lock);
834 { "moxtet" },
840 { .compatible = "cznic,moxtet" },
847 .name = "moxtet",
861 pr_err("moxtet bus registration failed: %d\n", ret);
867 pr_err("moxtet spi driver registration failed: %d\n", ret);