xref: /linux/drivers/thermal/thermal_fw.c (revision ccda015d76736dc5ccefc70a040b9304a6c2000f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  thermal-fw.c - Thermal components creation from firmware description
4  *
5  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
6  */
7 #include <linux/fwnode.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/thermal.h>
11 
12 /**
13  * thermal_fwnode_cooling_device_register() - register an thermal cooling device
14  * @np:		a pointer to a device tree node.
15  * @of_index:	a cooling device index in the cooling controller
16  * @type:	the thermal cooling device type.
17  * @devdata:	device private data.
18  * @ops:		standard thermal cooling devices callbacks.
19  *
20  * This function will register a cooling device with device tree node reference.
21  * This interface function adds a new thermal cooling device (fan/processor/...)
22  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
23  * to all the thermal zone devices registered at the same time.
24  *
25  * Return: a pointer to the created struct thermal_cooling_device or an
26  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
27  */
28 struct thermal_cooling_device *
29 thermal_fwnode_cooling_device_register(struct fwnode_handle *fwnode, int fwn_index,
30 				       const char *type, void *devdata,
31 				       const struct thermal_cooling_device_ops *ops)
32 {
33 	struct thermal_cooling_device *cdev;
34 
35 	cdev = __thermal_cooling_device_register(type, devdata, ops);
36 	if (IS_ERR(cdev))
37 		return cdev;
38 
39 	cdev->np = (struct device_node *)fwnode;
40 	cdev->of_index = fwn_index;
41 	thermal_cooling_device_init_complete(cdev);
42 
43 	return cdev;
44 }
45 EXPORT_SYMBOL_GPL(thermal_fwnode_cooling_device_register);
46 
47 static struct thermal_cooling_device *
48 __devm_thermal_fwnode_cooling_device_register(struct device *dev, struct fwnode_handle *fwnode,
49 					      int fwn_index, const char *type, void *devdata,
50 					      const struct thermal_cooling_device_ops *ops)
51 {
52 	struct thermal_cooling_device **ptr, *tcd;
53 
54 	ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
55 			   GFP_KERNEL);
56 	if (!ptr)
57 		return ERR_PTR(-ENOMEM);
58 
59 	tcd = thermal_fwnode_cooling_device_register(fwnode, fwn_index, type, devdata, ops);
60 	if (IS_ERR(tcd)) {
61 		devres_free(ptr);
62 		return tcd;
63 	}
64 
65 	*ptr = tcd;
66 	devres_add(dev, ptr);
67 
68 	return tcd;
69 }
70 
71 /**
72  * devm_thermal_fwnode_cooling_device_register() - register a thermal cooling device
73  * @dev:	a valid struct device pointer of a sensor device.
74  * @fw_index:	a cooling device index in the cooling controller
75  * @type:	the thermal cooling device type.
76  * @devdata:	device private data.
77  * @ops:	standard thermal cooling devices callbacks.
78  *
79  * This function will register a cooling device with a firmware node reference.
80  * This interface function adds a new thermal cooling device (fan/processor/...)
81  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
82  * to all the thermal zone devices registered at the same time.
83  *
84  * Return: a pointer to the created struct thermal_cooling_device or an
85  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
86  */
87 struct thermal_cooling_device *
88 devm_thermal_fwnode_cooling_device_register(struct device *dev, int fwn_index,
89 					    const char *type, void *devdata,
90 					    const struct thermal_cooling_device_ops *ops)
91 {
92 	return __devm_thermal_fwnode_cooling_device_register(dev, dev_fwnode(dev), fwn_index,
93 							     type, devdata, ops);
94 }
95 EXPORT_SYMBOL_GPL(devm_thermal_fwnode_cooling_device_register);
96 
97 
98