1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Turris Mox module configuration bus driver
4  *
5  * Copyright (C) 2019 Marek Behún <kabel@kernel.org>
6  */
7 
8 #ifndef __LINUX_MOXTET_H
9 #define __LINUX_MOXTET_H
10 
11 #include <linux/device.h>
12 #include <linux/irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/mutex.h>
15 
16 #define TURRIS_MOX_MAX_MODULES	10
17 
18 enum turris_mox_cpu_module_id {
19 	TURRIS_MOX_CPU_ID_EMMC	= 0x00,
20 	TURRIS_MOX_CPU_ID_SD	= 0x10,
21 };
22 
23 enum turris_mox_module_id {
24 	TURRIS_MOX_MODULE_FIRST		= 0x01,
25 
26 	TURRIS_MOX_MODULE_SFP		= 0x01,
27 	TURRIS_MOX_MODULE_PCI		= 0x02,
28 	TURRIS_MOX_MODULE_TOPAZ		= 0x03,
29 	TURRIS_MOX_MODULE_PERIDOT	= 0x04,
30 	TURRIS_MOX_MODULE_USB3		= 0x05,
31 	TURRIS_MOX_MODULE_PCI_BRIDGE	= 0x06,
32 
33 	TURRIS_MOX_MODULE_LAST		= 0x06,
34 };
35 
36 #define MOXTET_NIRQS	16
37 
38 extern struct bus_type moxtet_type;
39 
40 struct moxtet {
41 	struct device			*dev;
42 	struct mutex			lock;
43 	u8				modules[TURRIS_MOX_MAX_MODULES];
44 	int				count;
45 	u8				tx[TURRIS_MOX_MAX_MODULES];
46 	int				dev_irq;
47 	struct {
48 		struct irq_domain	*domain;
49 		struct irq_chip		chip;
50 		unsigned long		masked, exists;
51 		struct moxtet_irqpos {
52 					u8 idx;
53 					u8 bit;
54 		} position[MOXTET_NIRQS];
55 	} irq;
56 #ifdef CONFIG_DEBUG_FS
57 	struct dentry			*debugfs_root;
58 #endif
59 };
60 
61 struct moxtet_driver {
62 	const enum turris_mox_module_id	*id_table;
63 	struct device_driver		driver;
64 };
65 
66 static inline struct moxtet_driver *
to_moxtet_driver(struct device_driver * drv)67 to_moxtet_driver(struct device_driver *drv)
68 {
69 	if (!drv)
70 		return NULL;
71 	return container_of(drv, struct moxtet_driver, driver);
72 }
73 
74 extern int __moxtet_register_driver(struct module *owner,
75 				    struct moxtet_driver *mdrv);
76 
moxtet_unregister_driver(struct moxtet_driver * mdrv)77 static inline void moxtet_unregister_driver(struct moxtet_driver *mdrv)
78 {
79 	if (mdrv)
80 		driver_unregister(&mdrv->driver);
81 }
82 
83 #define moxtet_register_driver(driver) \
84 	__moxtet_register_driver(THIS_MODULE, driver)
85 
86 #define module_moxtet_driver(__moxtet_driver) \
87 	module_driver(__moxtet_driver, moxtet_register_driver, \
88 			moxtet_unregister_driver)
89 
90 struct moxtet_device {
91 	struct device			dev;
92 	struct moxtet			*moxtet;
93 	enum turris_mox_module_id	id;
94 	unsigned int			idx;
95 };
96 
97 extern int moxtet_device_read(struct device *dev);
98 extern int moxtet_device_write(struct device *dev, u8 val);
99 extern int moxtet_device_written(struct device *dev);
100 
101 static inline struct moxtet_device *
to_moxtet_device(struct device * dev)102 to_moxtet_device(struct device *dev)
103 {
104 	if (!dev)
105 		return NULL;
106 	return container_of(dev, struct moxtet_device, dev);
107 }
108 
109 #endif /* __LINUX_MOXTET_H */
110