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