1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Message Protocol bus layer
5 * Copyright (C) 2018 ARM Ltd.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/device.h>
18 static DEFINE_IDA(scmi_bus_id
);
19 static DEFINE_IDR(scmi_protocols
);
20 static DEFINE_SPINLOCK(protocol_lock
);
22 static const struct scmi_device_id
*
23 scmi_dev_match_id(struct scmi_device
*scmi_dev
, struct scmi_driver
*scmi_drv
)
25 const struct scmi_device_id
*id
= scmi_drv
->id_table
;
30 for (; id
->protocol_id
; id
++)
31 if (id
->protocol_id
== scmi_dev
->protocol_id
)
37 static int scmi_dev_match(struct device
*dev
, struct device_driver
*drv
)
39 struct scmi_driver
*scmi_drv
= to_scmi_driver(drv
);
40 struct scmi_device
*scmi_dev
= to_scmi_dev(dev
);
41 const struct scmi_device_id
*id
;
43 id
= scmi_dev_match_id(scmi_dev
, scmi_drv
);
50 static int scmi_protocol_init(int protocol_id
, struct scmi_handle
*handle
)
52 scmi_prot_init_fn_t fn
= idr_find(&scmi_protocols
, protocol_id
);
59 static int scmi_dev_probe(struct device
*dev
)
61 struct scmi_driver
*scmi_drv
= to_scmi_driver(dev
->driver
);
62 struct scmi_device
*scmi_dev
= to_scmi_dev(dev
);
63 const struct scmi_device_id
*id
;
66 id
= scmi_dev_match_id(scmi_dev
, scmi_drv
);
70 if (!scmi_dev
->handle
)
73 ret
= scmi_protocol_init(scmi_dev
->protocol_id
, scmi_dev
->handle
);
77 return scmi_drv
->probe(scmi_dev
);
80 static int scmi_dev_remove(struct device
*dev
)
82 struct scmi_driver
*scmi_drv
= to_scmi_driver(dev
->driver
);
83 struct scmi_device
*scmi_dev
= to_scmi_dev(dev
);
86 scmi_drv
->remove(scmi_dev
);
91 static struct bus_type scmi_bus_type
= {
92 .name
= "scmi_protocol",
93 .match
= scmi_dev_match
,
94 .probe
= scmi_dev_probe
,
95 .remove
= scmi_dev_remove
,
98 int scmi_driver_register(struct scmi_driver
*driver
, struct module
*owner
,
103 driver
->driver
.bus
= &scmi_bus_type
;
104 driver
->driver
.name
= driver
->name
;
105 driver
->driver
.owner
= owner
;
106 driver
->driver
.mod_name
= mod_name
;
108 retval
= driver_register(&driver
->driver
);
110 pr_debug("registered new scmi driver %s\n", driver
->name
);
114 EXPORT_SYMBOL_GPL(scmi_driver_register
);
116 void scmi_driver_unregister(struct scmi_driver
*driver
)
118 driver_unregister(&driver
->driver
);
120 EXPORT_SYMBOL_GPL(scmi_driver_unregister
);
122 static void scmi_device_release(struct device
*dev
)
124 kfree(to_scmi_dev(dev
));
128 scmi_device_create(struct device_node
*np
, struct device
*parent
, int protocol
)
131 struct scmi_device
*scmi_dev
;
133 scmi_dev
= kzalloc(sizeof(*scmi_dev
), GFP_KERNEL
);
137 id
= ida_simple_get(&scmi_bus_id
, 1, 0, GFP_KERNEL
);
142 scmi_dev
->protocol_id
= protocol
;
143 scmi_dev
->dev
.parent
= parent
;
144 scmi_dev
->dev
.of_node
= np
;
145 scmi_dev
->dev
.bus
= &scmi_bus_type
;
146 scmi_dev
->dev
.release
= scmi_device_release
;
147 dev_set_name(&scmi_dev
->dev
, "scmi_dev.%d", id
);
149 retval
= device_register(&scmi_dev
->dev
);
155 put_device(&scmi_dev
->dev
);
156 ida_simple_remove(&scmi_bus_id
, id
);
162 void scmi_device_destroy(struct scmi_device
*scmi_dev
)
164 scmi_handle_put(scmi_dev
->handle
);
165 ida_simple_remove(&scmi_bus_id
, scmi_dev
->id
);
166 device_unregister(&scmi_dev
->dev
);
169 void scmi_set_handle(struct scmi_device
*scmi_dev
)
171 scmi_dev
->handle
= scmi_handle_get(&scmi_dev
->dev
);
174 int scmi_protocol_register(int protocol_id
, scmi_prot_init_fn_t fn
)
178 spin_lock(&protocol_lock
);
179 ret
= idr_alloc(&scmi_protocols
, fn
, protocol_id
, protocol_id
+ 1,
181 spin_unlock(&protocol_lock
);
182 if (ret
!= protocol_id
)
183 pr_err("unable to allocate SCMI idr slot, err %d\n", ret
);
187 EXPORT_SYMBOL_GPL(scmi_protocol_register
);
189 void scmi_protocol_unregister(int protocol_id
)
191 spin_lock(&protocol_lock
);
192 idr_remove(&scmi_protocols
, protocol_id
);
193 spin_unlock(&protocol_lock
);
195 EXPORT_SYMBOL_GPL(scmi_protocol_unregister
);
197 static int __scmi_devices_unregister(struct device
*dev
, void *data
)
199 struct scmi_device
*scmi_dev
= to_scmi_dev(dev
);
201 scmi_device_destroy(scmi_dev
);
205 static void scmi_devices_unregister(void)
207 bus_for_each_dev(&scmi_bus_type
, NULL
, NULL
, __scmi_devices_unregister
);
210 static int __init
scmi_bus_init(void)
214 retval
= bus_register(&scmi_bus_type
);
216 pr_err("scmi protocol bus register failed (%d)\n", retval
);
220 subsys_initcall(scmi_bus_init
);
222 static void __exit
scmi_bus_exit(void)
224 scmi_devices_unregister();
225 bus_unregister(&scmi_bus_type
);
226 ida_destroy(&scmi_bus_id
);
228 module_exit(scmi_bus_exit
);