bcm2711_thermal.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Broadcom AVS RO thermal sensor driver
  4. *
  5. * based on brcmstb_thermal
  6. *
  7. * Copyright (C) 2020 Stefan Wahren
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/clk.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/thermal.h>
  21. #include "../thermal_hwmon.h"
  22. #define AVS_RO_TEMP_STATUS 0x200
  23. #define AVS_RO_TEMP_STATUS_VALID_MSK (BIT(16) | BIT(10))
  24. #define AVS_RO_TEMP_STATUS_DATA_MSK GENMASK(9, 0)
  25. struct bcm2711_thermal_priv {
  26. struct regmap *regmap;
  27. struct thermal_zone_device *thermal;
  28. };
  29. static int bcm2711_get_temp(struct thermal_zone_device *tz, int *temp)
  30. {
  31. struct bcm2711_thermal_priv *priv = tz->devdata;
  32. int slope = thermal_zone_get_slope(tz);
  33. int offset = thermal_zone_get_offset(tz);
  34. u32 val;
  35. int ret;
  36. ret = regmap_read(priv->regmap, AVS_RO_TEMP_STATUS, &val);
  37. if (ret)
  38. return ret;
  39. if (!(val & AVS_RO_TEMP_STATUS_VALID_MSK))
  40. return -EIO;
  41. val &= AVS_RO_TEMP_STATUS_DATA_MSK;
  42. /* Convert a HW code to a temperature reading (millidegree celsius) */
  43. *temp = slope * val + offset;
  44. return 0;
  45. }
  46. static const struct thermal_zone_device_ops bcm2711_thermal_of_ops = {
  47. .get_temp = bcm2711_get_temp,
  48. };
  49. static const struct of_device_id bcm2711_thermal_id_table[] = {
  50. { .compatible = "brcm,bcm2711-thermal" },
  51. {},
  52. };
  53. MODULE_DEVICE_TABLE(of, bcm2711_thermal_id_table);
  54. static int bcm2711_thermal_probe(struct platform_device *pdev)
  55. {
  56. struct thermal_zone_device *thermal;
  57. struct bcm2711_thermal_priv *priv;
  58. struct device *dev = &pdev->dev;
  59. struct device_node *parent;
  60. struct regmap *regmap;
  61. int ret;
  62. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  63. if (!priv)
  64. return -ENOMEM;
  65. /* get regmap from syscon node */
  66. parent = of_get_parent(dev->of_node); /* parent should be syscon node */
  67. regmap = syscon_node_to_regmap(parent);
  68. of_node_put(parent);
  69. if (IS_ERR(regmap)) {
  70. ret = PTR_ERR(regmap);
  71. dev_err(dev, "failed to get regmap: %d\n", ret);
  72. return ret;
  73. }
  74. priv->regmap = regmap;
  75. thermal = devm_thermal_of_zone_register(dev, 0, priv,
  76. &bcm2711_thermal_of_ops);
  77. if (IS_ERR(thermal)) {
  78. ret = PTR_ERR(thermal);
  79. dev_err(dev, "could not register sensor: %d\n", ret);
  80. return ret;
  81. }
  82. priv->thermal = thermal;
  83. thermal->tzp->no_hwmon = false;
  84. return thermal_add_hwmon_sysfs(thermal);
  85. }
  86. static struct platform_driver bcm2711_thermal_driver = {
  87. .probe = bcm2711_thermal_probe,
  88. .driver = {
  89. .name = "bcm2711_thermal",
  90. .of_match_table = bcm2711_thermal_id_table,
  91. },
  92. };
  93. module_platform_driver(bcm2711_thermal_driver);
  94. MODULE_LICENSE("GPL");
  95. MODULE_AUTHOR("Stefan Wahren");
  96. MODULE_DESCRIPTION("Broadcom AVS RO thermal sensor driver");