bcl_soc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "%s:%s " fmt, KBUILD_MODNAME, __func__
  7. #include <linux/module.h>
  8. #include <linux/workqueue.h>
  9. #include <linux/kernel.h>
  10. #include <linux/io.h>
  11. #include <linux/err.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/mutex.h>
  15. #include <linux/power_supply.h>
  16. #include <linux/thermal.h>
  17. #include "thermal_zone_internal.h"
  18. #define BCL_DRIVER_NAME "bcl_soc_peripheral"
  19. struct bcl_device {
  20. struct device *dev;
  21. struct notifier_block psy_nb;
  22. struct work_struct soc_eval_work;
  23. long trip_temp;
  24. int trip_val;
  25. struct mutex state_trans_lock;
  26. bool irq_enabled;
  27. struct thermal_zone_device *tz_dev;
  28. struct thermal_zone_device_ops ops;
  29. };
  30. static struct bcl_device *bcl_perph;
  31. static int bcl_soc_get_trend(struct thermal_zone_device *tz, int trip,
  32. enum thermal_trend *trend)
  33. {
  34. int trip_temp = 0, trip_hyst = 0, temp, ret;
  35. if (!tz)
  36. return -EINVAL;
  37. ret = tz->ops->get_trip_temp(tz, trip, &trip_temp);
  38. if (ret)
  39. return ret;
  40. if (tz->ops->get_trip_hyst) {
  41. ret = tz->ops->get_trip_hyst(tz, trip, &trip_hyst);
  42. if (ret)
  43. return ret;
  44. }
  45. temp = READ_ONCE(tz->temperature);
  46. if (temp >= trip_temp)
  47. *trend = THERMAL_TREND_RAISING;
  48. else if (!trip_hyst && temp < trip_temp)
  49. *trend = THERMAL_TREND_DROPPING;
  50. else if (temp <= (trip_temp - trip_hyst))
  51. *trend = THERMAL_TREND_DROPPING;
  52. else
  53. *trend = THERMAL_TREND_STABLE;
  54. return 0;
  55. }
  56. static int bcl_set_soc(struct thermal_zone_device *tz, int low, int high)
  57. {
  58. if (high == bcl_perph->trip_temp)
  59. return 0;
  60. mutex_lock(&bcl_perph->state_trans_lock);
  61. pr_debug("socd threshold:%d\n", high);
  62. bcl_perph->trip_temp = high;
  63. if (high == INT_MAX) {
  64. bcl_perph->irq_enabled = false;
  65. goto unlock_and_exit;
  66. }
  67. bcl_perph->irq_enabled = true;
  68. schedule_work(&bcl_perph->soc_eval_work);
  69. unlock_and_exit:
  70. mutex_unlock(&bcl_perph->state_trans_lock);
  71. return 0;
  72. }
  73. static int bcl_read_soc(struct thermal_zone_device *tz, int *val)
  74. {
  75. static struct power_supply *batt_psy;
  76. union power_supply_propval ret = {0,};
  77. int err = 0;
  78. *val = 0;
  79. if (!batt_psy)
  80. batt_psy = power_supply_get_by_name("battery");
  81. if (batt_psy) {
  82. err = power_supply_get_property(batt_psy,
  83. POWER_SUPPLY_PROP_CAPACITY, &ret);
  84. if (err) {
  85. pr_err("battery percentage read error:%d\n",
  86. err);
  87. return err;
  88. }
  89. *val = 100 - ret.intval;
  90. }
  91. pr_debug("soc:%d\n", *val);
  92. return err;
  93. }
  94. static void bcl_evaluate_soc(struct work_struct *work)
  95. {
  96. int battery_depletion;
  97. if (!bcl_perph->tz_dev)
  98. return;
  99. if (bcl_read_soc(NULL, &battery_depletion))
  100. return;
  101. mutex_lock(&bcl_perph->state_trans_lock);
  102. if (!bcl_perph->irq_enabled)
  103. goto eval_exit;
  104. if (battery_depletion < bcl_perph->trip_temp)
  105. goto eval_exit;
  106. bcl_perph->trip_val = battery_depletion;
  107. mutex_unlock(&bcl_perph->state_trans_lock);
  108. thermal_zone_device_update(bcl_perph->tz_dev,
  109. THERMAL_TRIP_VIOLATED);
  110. return;
  111. eval_exit:
  112. mutex_unlock(&bcl_perph->state_trans_lock);
  113. }
  114. static int battery_supply_callback(struct notifier_block *nb,
  115. unsigned long event, void *data)
  116. {
  117. struct power_supply *psy = data;
  118. if (strcmp(psy->desc->name, "battery"))
  119. return NOTIFY_OK;
  120. schedule_work(&bcl_perph->soc_eval_work);
  121. return NOTIFY_OK;
  122. }
  123. static int bcl_soc_remove(struct platform_device *pdev)
  124. {
  125. power_supply_unreg_notifier(&bcl_perph->psy_nb);
  126. flush_work(&bcl_perph->soc_eval_work);
  127. if (bcl_perph->tz_dev) {
  128. qti_update_tz_ops(bcl_perph->tz_dev, false);
  129. }
  130. return 0;
  131. }
  132. static int bcl_soc_probe(struct platform_device *pdev)
  133. {
  134. int ret = 0;
  135. bcl_perph = devm_kzalloc(&pdev->dev, sizeof(*bcl_perph), GFP_KERNEL);
  136. if (!bcl_perph)
  137. return -ENOMEM;
  138. mutex_init(&bcl_perph->state_trans_lock);
  139. bcl_perph->dev = &pdev->dev;
  140. bcl_perph->ops.get_temp = bcl_read_soc;
  141. bcl_perph->ops.set_trips = bcl_set_soc;
  142. bcl_perph->ops.get_trend = bcl_soc_get_trend;
  143. INIT_WORK(&bcl_perph->soc_eval_work, bcl_evaluate_soc);
  144. bcl_perph->psy_nb.notifier_call = battery_supply_callback;
  145. ret = power_supply_reg_notifier(&bcl_perph->psy_nb);
  146. if (ret < 0) {
  147. pr_err("soc notifier registration error. defer. err:%d\n",
  148. ret);
  149. ret = -EPROBE_DEFER;
  150. goto bcl_soc_probe_exit;
  151. }
  152. if (bcl_read_soc(bcl_perph->tz_dev, &ret) == -ENODATA) {
  153. ret = -EPROBE_DEFER;
  154. goto bcl_soc_probe_exit;
  155. }
  156. bcl_perph->tz_dev = devm_thermal_of_zone_register(&pdev->dev,
  157. 0, bcl_perph, &bcl_perph->ops);
  158. if (IS_ERR(bcl_perph->tz_dev)) {
  159. pr_err("soc TZ register failed. err:%ld\n",
  160. PTR_ERR(bcl_perph->tz_dev));
  161. ret = PTR_ERR(bcl_perph->tz_dev);
  162. bcl_perph->tz_dev = NULL;
  163. goto bcl_soc_probe_exit;
  164. }
  165. qti_update_tz_ops(bcl_perph->tz_dev, true);
  166. thermal_zone_device_update(bcl_perph->tz_dev, THERMAL_DEVICE_UP);
  167. schedule_work(&bcl_perph->soc_eval_work);
  168. dev_set_drvdata(&pdev->dev, bcl_perph);
  169. return 0;
  170. bcl_soc_probe_exit:
  171. bcl_soc_remove(pdev);
  172. return ret;
  173. }
  174. static const struct of_device_id bcl_match[] = {
  175. {
  176. .compatible = "qcom,msm-bcl-soc",
  177. },
  178. {},
  179. };
  180. static struct platform_driver bcl_driver = {
  181. .probe = bcl_soc_probe,
  182. .remove = bcl_soc_remove,
  183. .driver = {
  184. .name = BCL_DRIVER_NAME,
  185. .of_match_table = bcl_match,
  186. },
  187. };
  188. module_platform_driver(bcl_driver);
  189. MODULE_LICENSE("GPL");