086-0001-thermal-of-thermal-Add-devm-version-of-thermal_zone_.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. Add resource managed version of thermal_zone_of_sensor_register() and
  7. thermal_zone_of_sensor_unregister().
  8. This helps in reducing the code size in error path, remove of
  9. driver remove callbacks and making proper sequence for deallocations.
  10. Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
  11. Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
  12. ---
  13. drivers/thermal/of-thermal.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
  14. include/linux/thermal.h | 18 ++++++++++
  15. 2 files changed, 99 insertions(+)
  16. --- a/drivers/thermal/of-thermal.c
  17. +++ b/drivers/thermal/of-thermal.c
  18. @@ -562,6 +562,87 @@ void thermal_zone_of_sensor_unregister(s
  19. }
  20. EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
  21. +static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
  22. +{
  23. + thermal_zone_of_sensor_unregister(dev,
  24. + *(struct thermal_zone_device **)res);
  25. +}
  26. +
  27. +static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
  28. + void *data)
  29. +{
  30. + struct thermal_zone_device **r = res;
  31. +
  32. + if (WARN_ON(!r || !*r))
  33. + return 0;
  34. +
  35. + return *r == data;
  36. +}
  37. +
  38. +/**
  39. + * devm_thermal_zone_of_sensor_register - Resource managed version of
  40. + * thermal_zone_of_sensor_register()
  41. + * @dev: a valid struct device pointer of a sensor device. Must contain
  42. + * a valid .of_node, for the sensor node.
  43. + * @sensor_id: a sensor identifier, in case the sensor IP has more
  44. + * than one sensors
  45. + * @data: a private pointer (owned by the caller) that will be passed
  46. + * back, when a temperature reading is needed.
  47. + * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
  48. + *
  49. + * Refer thermal_zone_of_sensor_register() for more details.
  50. + *
  51. + * Return: On success returns a valid struct thermal_zone_device,
  52. + * otherwise, it returns a corresponding ERR_PTR(). Caller must
  53. + * check the return value with help of IS_ERR() helper.
  54. + * Registered hermal_zone_device device will automatically be
  55. + * released when device is unbounded.
  56. + */
  57. +struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
  58. + struct device *dev, int sensor_id,
  59. + void *data, const struct thermal_zone_of_device_ops *ops)
  60. +{
  61. + struct thermal_zone_device **ptr, *tzd;
  62. +
  63. + ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
  64. + GFP_KERNEL);
  65. + if (!ptr)
  66. + return ERR_PTR(-ENOMEM);
  67. +
  68. + tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
  69. + if (IS_ERR(tzd)) {
  70. + devres_free(ptr);
  71. + return tzd;
  72. + }
  73. +
  74. + *ptr = tzd;
  75. + devres_add(dev, ptr);
  76. +
  77. + return tzd;
  78. +}
  79. +EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
  80. +
  81. +/**
  82. + * devm_thermal_zone_of_sensor_unregister - Resource managed version of
  83. + * thermal_zone_of_sensor_unregister().
  84. + * @dev: Device for which which resource was allocated.
  85. + * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
  86. + *
  87. + * This function removes the sensor callbacks and private data from the
  88. + * thermal zone device registered with devm_thermal_zone_of_sensor_register()
  89. + * API. It will also silent the zone by remove the .get_temp() and .get_trend()
  90. + * thermal zone device callbacks.
  91. + * Normally this function will not need to be called and the resource
  92. + * management code will ensure that the resource is freed.
  93. + */
  94. +void devm_thermal_zone_of_sensor_unregister(struct device *dev,
  95. + struct thermal_zone_device *tzd)
  96. +{
  97. + WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
  98. + devm_thermal_zone_of_sensor_match, tzd));
  99. +}
  100. +EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
  101. +
  102. /*** functions parsing device tree nodes ***/
  103. /**
  104. --- a/include/linux/thermal.h
  105. +++ b/include/linux/thermal.h
  106. @@ -364,6 +364,11 @@ thermal_zone_of_sensor_register(struct d
  107. const struct thermal_zone_of_device_ops *ops);
  108. void thermal_zone_of_sensor_unregister(struct device *dev,
  109. struct thermal_zone_device *tz);
  110. +struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
  111. + struct device *dev, int id, void *data,
  112. + const struct thermal_zone_of_device_ops *ops);
  113. +void devm_thermal_zone_of_sensor_unregister(struct device *dev,
  114. + struct thermal_zone_device *tz);
  115. #else
  116. static inline struct thermal_zone_device *
  117. thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
  118. @@ -378,6 +383,19 @@ void thermal_zone_of_sensor_unregister(s
  119. {
  120. }
  121. +static inline struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
  122. + struct device *dev, int id, void *data,
  123. + const struct thermal_zone_of_device_ops *ops)
  124. +{
  125. + return ERR_PTR(-ENODEV);
  126. +}
  127. +
  128. +static inline
  129. +void devm_thermal_zone_of_sensor_unregister(struct device *dev,
  130. + struct thermal_zone_device *tz)
  131. +{
  132. +}
  133. +
  134. #endif
  135. #if IS_ENABLED(CONFIG_THERMAL)