xref: /linux-6.15/include/linux/mfd/core.h (revision 42e59982)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * drivers/mfd/mfd-core.h
4  *
5  * core MFD support
6  * Copyright (c) 2006 Ian Molton
7  * Copyright (c) 2007 Dmitry Baryshkov
8  */
9 
10 #ifndef MFD_CORE_H
11 #define MFD_CORE_H
12 
13 #include <linux/platform_device.h>
14 
15 #define MFD_RES_SIZE(arr) (sizeof(arr) / sizeof(struct resource))
16 
17 #define MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, _compat, _of_reg, _use_of_reg, _match) \
18 	{								\
19 		.name = (_name),					\
20 		.resources = (_res),					\
21 		.num_resources = MFD_RES_SIZE((_res)),			\
22 		.platform_data = (_pdata),				\
23 		.pdata_size = (_pdsize),				\
24 		.of_compatible = (_compat),				\
25 		.of_reg = (_of_reg),					\
26 		.use_of_reg = (_use_of_reg),				\
27 		.acpi_match = (_match),					\
28 		.id = (_id),						\
29 	}
30 
31 #define MFD_CELL_OF_REG(_name, _res, _pdata, _pdsize, _id, _compat, _of_reg) \
32 	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, _compat, _of_reg, true, NULL)
33 
34 #define MFD_CELL_OF(_name, _res, _pdata, _pdsize, _id, _compat) \
35 	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, _compat, 0, false, NULL)
36 
37 #define MFD_CELL_ACPI(_name, _res, _pdata, _pdsize, _id, _match) \
38 	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, NULL, 0, false, _match)
39 
40 #define MFD_CELL_BASIC(_name, _res, _pdata, _pdsize, _id) \
41 	MFD_CELL_ALL(_name, _res, _pdata, _pdsize, _id, NULL, 0, false, NULL)
42 
43 #define MFD_CELL_RES(_name, _res) \
44 	MFD_CELL_ALL(_name, _res, NULL, 0, 0, NULL, 0, false, NULL)
45 
46 #define MFD_CELL_NAME(_name) \
47 	MFD_CELL_ALL(_name, NULL, NULL, 0, 0, NULL, 0, false, NULL)
48 
49 #define MFD_DEP_LEVEL_NORMAL 0
50 #define MFD_DEP_LEVEL_HIGH 1
51 
52 struct irq_domain;
53 struct software_node;
54 struct property_entry;
55 
56 /* Matches ACPI PNP id, either _HID or _CID, or ACPI _ADR */
57 struct mfd_cell_acpi_match {
58 	const char			*pnpid;
59 	const unsigned long long	adr;
60 };
61 
62 /*
63  * This struct describes the MFD part ("cell").
64  * After registration the copy of this structure will become the platform data
65  * of the resulting platform_device
66  */
67 struct mfd_cell {
68 	const char		*name;
69 	int			id;
70 	int			level;
71 
72 	int			(*enable)(struct platform_device *dev);
73 	int			(*disable)(struct platform_device *dev);
74 
75 	int			(*suspend)(struct platform_device *dev);
76 	int			(*resume)(struct platform_device *dev);
77 
78 	/* platform data passed to the sub devices drivers */
79 	void			*platform_data;
80 	size_t			pdata_size;
81 
82 	/* Software node for the device. */
83 	const struct software_node *swnode;
84 
85 	/* device properties passed to the sub devices drivers */
86 	const struct property_entry *properties;
87 
88 	/*
89 	 * Device Tree compatible string
90 	 * See: Documentation/devicetree/usage-model.rst Chapter 2.2 for details
91 	 */
92 	const char		*of_compatible;
93 
94 	/*
95 	 * Address as defined in Device Tree.  Used to compement 'of_compatible'
96 	 * (above) when matching OF nodes with devices that have identical
97 	 * compatible strings
98 	 */
99 	const u64 of_reg;
100 
101 	/* Set to 'true' to use 'of_reg' (above) - allows for of_reg=0 */
102 	bool use_of_reg;
103 
104 	/* Matches ACPI */
105 	const struct mfd_cell_acpi_match	*acpi_match;
106 
107 	/*
108 	 * These resources can be specified relative to the parent device.
109 	 * For accessing hardware you should use resources from the platform dev
110 	 */
111 	int			num_resources;
112 	const struct resource	*resources;
113 
114 	/* don't check for resource conflicts */
115 	bool			ignore_resource_conflicts;
116 
117 	/*
118 	 * Disable runtime PM callbacks for this subdevice - see
119 	 * pm_runtime_no_callbacks().
120 	 */
121 	bool			pm_runtime_no_callbacks;
122 
123 	/* A list of regulator supplies that should be mapped to the MFD
124 	 * device rather than the child device when requested
125 	 */
126 	const char * const	*parent_supplies;
127 	int			num_parent_supplies;
128 };
129 
130 /*
131  * Convenience functions for clients using shared cells.  Refcounting
132  * happens automatically, with the cell's enable/disable callbacks
133  * being called only when a device is first being enabled or no other
134  * clients are making use of it.
135  */
136 extern int mfd_cell_enable(struct platform_device *pdev);
137 extern int mfd_cell_disable(struct platform_device *pdev);
138 
139 /*
140  * Given a platform device that's been created by mfd_add_devices(), fetch
141  * the mfd_cell that created it.
142  */
143 static inline const struct mfd_cell *mfd_get_cell(struct platform_device *pdev)
144 {
145 	return pdev->mfd_cell;
146 }
147 
148 extern int mfd_add_devices(struct device *parent, int id,
149 			   const struct mfd_cell *cells, int n_devs,
150 			   struct resource *mem_base,
151 			   int irq_base, struct irq_domain *irq_domain);
152 
153 static inline int mfd_add_hotplug_devices(struct device *parent,
154 		const struct mfd_cell *cells, int n_devs)
155 {
156 	return mfd_add_devices(parent, PLATFORM_DEVID_AUTO, cells, n_devs,
157 			NULL, 0, NULL);
158 }
159 
160 extern void mfd_remove_devices(struct device *parent);
161 extern void mfd_remove_devices_late(struct device *parent);
162 
163 extern int devm_mfd_add_devices(struct device *dev, int id,
164 				const struct mfd_cell *cells, int n_devs,
165 				struct resource *mem_base,
166 				int irq_base, struct irq_domain *irq_domain);
167 #endif
168