sr-thermal.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Broadcom
  4. */
  5. #include <linux/module.h>
  6. #include <linux/of_address.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/thermal.h>
  9. /*
  10. * In stingray thermal IO memory,
  11. * Total Number of available TMONs MASK is at offset 0
  12. * temperature registers BASE is at 4 byte offset.
  13. * Each TMON temperature register size is 4.
  14. */
  15. #define SR_TMON_TEMP_BASE(id) ((id) * 0x4)
  16. #define SR_TMON_MAX_LIST 6
  17. struct sr_tmon {
  18. unsigned int crit_temp;
  19. unsigned int tmon_id;
  20. struct sr_thermal *priv;
  21. };
  22. struct sr_thermal {
  23. void __iomem *regs;
  24. unsigned int max_crit_temp;
  25. struct sr_tmon tmon[SR_TMON_MAX_LIST];
  26. };
  27. static int sr_get_temp(struct thermal_zone_device *tz, int *temp)
  28. {
  29. struct sr_tmon *tmon = tz->devdata;
  30. struct sr_thermal *sr_thermal = tmon->priv;
  31. *temp = readl(sr_thermal->regs + SR_TMON_TEMP_BASE(tmon->tmon_id));
  32. return 0;
  33. }
  34. static const struct thermal_zone_device_ops sr_tz_ops = {
  35. .get_temp = sr_get_temp,
  36. };
  37. static int sr_thermal_probe(struct platform_device *pdev)
  38. {
  39. struct device *dev = &pdev->dev;
  40. struct thermal_zone_device *tz;
  41. struct sr_thermal *sr_thermal;
  42. struct sr_tmon *tmon;
  43. struct resource *res;
  44. u32 sr_tmon_list = 0;
  45. unsigned int i;
  46. int ret;
  47. sr_thermal = devm_kzalloc(dev, sizeof(*sr_thermal), GFP_KERNEL);
  48. if (!sr_thermal)
  49. return -ENOMEM;
  50. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  51. if (!res)
  52. return -ENOENT;
  53. sr_thermal->regs = (void __iomem *)devm_memremap(&pdev->dev, res->start,
  54. resource_size(res),
  55. MEMREMAP_WB);
  56. if (IS_ERR(sr_thermal->regs)) {
  57. dev_err(dev, "failed to get io address\n");
  58. return PTR_ERR(sr_thermal->regs);
  59. }
  60. ret = device_property_read_u32(dev, "brcm,tmon-mask", &sr_tmon_list);
  61. if (ret)
  62. return ret;
  63. tmon = sr_thermal->tmon;
  64. for (i = 0; i < SR_TMON_MAX_LIST; i++, tmon++) {
  65. if (!(sr_tmon_list & BIT(i)))
  66. continue;
  67. /* Flush temperature registers */
  68. writel(0, sr_thermal->regs + SR_TMON_TEMP_BASE(i));
  69. tmon->tmon_id = i;
  70. tmon->priv = sr_thermal;
  71. tz = devm_thermal_of_zone_register(dev, i, tmon,
  72. &sr_tz_ops);
  73. if (IS_ERR(tz))
  74. return PTR_ERR(tz);
  75. dev_dbg(dev, "thermal sensor %d registered\n", i);
  76. }
  77. platform_set_drvdata(pdev, sr_thermal);
  78. return 0;
  79. }
  80. static const struct of_device_id sr_thermal_of_match[] = {
  81. { .compatible = "brcm,sr-thermal", },
  82. {},
  83. };
  84. MODULE_DEVICE_TABLE(of, sr_thermal_of_match);
  85. static struct platform_driver sr_thermal_driver = {
  86. .probe = sr_thermal_probe,
  87. .driver = {
  88. .name = "sr-thermal",
  89. .of_match_table = sr_thermal_of_match,
  90. },
  91. };
  92. module_platform_driver(sr_thermal_driver);
  93. MODULE_AUTHOR("Pramod Kumar <[email protected]>");
  94. MODULE_DESCRIPTION("Stingray thermal driver");
  95. MODULE_LICENSE("GPL v2");