b551bac69d6f6196661a0f6e55a658a3532cb76f
[librecmc/librecmc.git] /
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
6
7 Add resource managed version of thermal_zone_of_sensor_register() and
8 thermal_zone_of_sensor_unregister().
9
10 This helps in reducing the code size in error path, remove of
11 driver remove callbacks and making proper sequence for deallocations.
12
13 Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
14 Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
15 ---
16  drivers/thermal/of-thermal.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
17  include/linux/thermal.h      | 18 ++++++++++
18  2 files changed, 99 insertions(+)
19
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
23  }
24  EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
25  
26 +static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
27 +{
28 +       thermal_zone_of_sensor_unregister(dev,
29 +                                         *(struct thermal_zone_device **)res);
30 +}
31 +
32 +static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
33 +                                            void *data)
34 +{
35 +       struct thermal_zone_device **r = res;
36 +
37 +       if (WARN_ON(!r || !*r))
38 +               return 0;
39 +
40 +       return *r == data;
41 +}
42 +
43 +/**
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
49 + *            than one sensors
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.
53 + *
54 + * Refer thermal_zone_of_sensor_register() for more details.
55 + *
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.
61 + */
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)
65 +{
66 +       struct thermal_zone_device **ptr, *tzd;
67 +
68 +       ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
69 +                          GFP_KERNEL);
70 +       if (!ptr)
71 +               return ERR_PTR(-ENOMEM);
72 +
73 +       tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
74 +       if (IS_ERR(tzd)) {
75 +               devres_free(ptr);
76 +               return tzd;
77 +       }
78 +
79 +       *ptr = tzd;
80 +       devres_add(dev, ptr);
81 +
82 +       return tzd;
83 +}
84 +EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
85 +
86 +/**
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.
91 + *
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.
98 + */
99 +void devm_thermal_zone_of_sensor_unregister(struct device *dev,
100 +                                           struct thermal_zone_device *tzd)
101 +{
102 +       WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
103 +                              devm_thermal_zone_of_sensor_match, tzd));
104 +}
105 +EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
106 +
107  /***   functions parsing device tree nodes   ***/
108  
109  /**
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);
121  #else
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
125  {
126  }
127  
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)
131 +{
132 +       return ERR_PTR(-ENODEV);
133 +}
134 +
135 +static inline
136 +void devm_thermal_zone_of_sensor_unregister(struct device *dev,
137 +                                           struct thermal_zone_device *tz)
138 +{
139 +}
140 +
141  #endif
142  
143  #if IS_ENABLED(CONFIG_THERMAL)