1 From e498b4984db82b4ba3ceea7dba813222a31e9c2e Mon Sep 17 00:00:00 2001
2 From: Laxman Dewangan <ldewangan@nvidia.com>
3 Date: Wed, 9 Mar 2016 18:40:06 +0530
4 Subject: [PATCH] thermal: of-thermal: Add devm version of
5 thermal_zone_of_sensor_register
7 Add resource managed version of thermal_zone_of_sensor_register() and
8 thermal_zone_of_sensor_unregister().
10 This helps in reducing the code size in error path, remove of
11 driver remove callbacks and making proper sequence for deallocations.
13 Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
14 Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
16 drivers/thermal/of-thermal.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
17 include/linux/thermal.h | 18 ++++++++++
18 2 files changed, 99 insertions(+)
20 --- a/drivers/thermal/of-thermal.c
21 +++ b/drivers/thermal/of-thermal.c
22 @@ -559,6 +559,87 @@ void thermal_zone_of_sensor_unregister(s
24 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
26 +static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
28 + thermal_zone_of_sensor_unregister(dev,
29 + *(struct thermal_zone_device **)res);
32 +static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
35 + struct thermal_zone_device **r = res;
37 + if (WARN_ON(!r || !*r))
44 + * devm_thermal_zone_of_sensor_register - Resource managed version of
45 + * thermal_zone_of_sensor_register()
46 + * @dev: a valid struct device pointer of a sensor device. Must contain
47 + * a valid .of_node, for the sensor node.
48 + * @sensor_id: a sensor identifier, in case the sensor IP has more
50 + * @data: a private pointer (owned by the caller) that will be passed
51 + * back, when a temperature reading is needed.
52 + * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
54 + * Refer thermal_zone_of_sensor_register() for more details.
56 + * Return: On success returns a valid struct thermal_zone_device,
57 + * otherwise, it returns a corresponding ERR_PTR(). Caller must
58 + * check the return value with help of IS_ERR() helper.
59 + * Registered hermal_zone_device device will automatically be
60 + * released when device is unbounded.
62 +struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
63 + struct device *dev, int sensor_id,
64 + void *data, const struct thermal_zone_of_device_ops *ops)
66 + struct thermal_zone_device **ptr, *tzd;
68 + ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
71 + return ERR_PTR(-ENOMEM);
73 + tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
80 + devres_add(dev, ptr);
84 +EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
87 + * devm_thermal_zone_of_sensor_unregister - Resource managed version of
88 + * thermal_zone_of_sensor_unregister().
89 + * @dev: Device for which which resource was allocated.
90 + * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
92 + * This function removes the sensor callbacks and private data from the
93 + * thermal zone device registered with devm_thermal_zone_of_sensor_register()
94 + * API. It will also silent the zone by remove the .get_temp() and .get_trend()
95 + * thermal zone device callbacks.
96 + * Normally this function will not need to be called and the resource
97 + * management code will ensure that the resource is freed.
99 +void devm_thermal_zone_of_sensor_unregister(struct device *dev,
100 + struct thermal_zone_device *tzd)
102 + WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
103 + devm_thermal_zone_of_sensor_match, tzd));
105 +EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
107 /*** functions parsing device tree nodes ***/
110 --- a/include/linux/thermal.h
111 +++ b/include/linux/thermal.h
112 @@ -364,6 +364,11 @@ thermal_zone_of_sensor_register(struct d
113 const struct thermal_zone_of_device_ops *ops);
114 void thermal_zone_of_sensor_unregister(struct device *dev,
115 struct thermal_zone_device *tz);
116 +struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
117 + struct device *dev, int id, void *data,
118 + const struct thermal_zone_of_device_ops *ops);
119 +void devm_thermal_zone_of_sensor_unregister(struct device *dev,
120 + struct thermal_zone_device *tz);
122 static inline struct thermal_zone_device *
123 thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
124 @@ -378,6 +383,19 @@ void thermal_zone_of_sensor_unregister(s
128 +static inline struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
129 + struct device *dev, int id, void *data,
130 + const struct thermal_zone_of_device_ops *ops)
132 + return ERR_PTR(-ENODEV);
136 +void devm_thermal_zone_of_sensor_unregister(struct device *dev,
137 + struct thermal_zone_device *tz)
143 #if IS_ENABLED(CONFIG_THERMAL)