ns-thermal.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Rafał Miłecki <[email protected]>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/of_address.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/thermal.h>
  9. #define PVTMON_CONTROL0 0x00
  10. #define PVTMON_CONTROL0_SEL_MASK 0x0000000e
  11. #define PVTMON_CONTROL0_SEL_TEMP_MONITOR 0x00000000
  12. #define PVTMON_CONTROL0_SEL_TEST_MODE 0x0000000e
  13. #define PVTMON_STATUS 0x08
  14. static int ns_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
  15. {
  16. void __iomem *pvtmon = tz->devdata;
  17. int offset = thermal_zone_get_offset(tz);
  18. int slope = thermal_zone_get_slope(tz);
  19. u32 val;
  20. val = readl(pvtmon + PVTMON_CONTROL0);
  21. if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
  22. /* Clear current mode selection */
  23. val &= ~PVTMON_CONTROL0_SEL_MASK;
  24. /* Set temp monitor mode (it's the default actually) */
  25. val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
  26. writel(val, pvtmon + PVTMON_CONTROL0);
  27. }
  28. val = readl(pvtmon + PVTMON_STATUS);
  29. *temp = slope * val + offset;
  30. return 0;
  31. }
  32. static const struct thermal_zone_device_ops ns_thermal_ops = {
  33. .get_temp = ns_thermal_get_temp,
  34. };
  35. static int ns_thermal_probe(struct platform_device *pdev)
  36. {
  37. struct device *dev = &pdev->dev;
  38. struct thermal_zone_device *tz;
  39. void __iomem *pvtmon;
  40. pvtmon = of_iomap(dev_of_node(dev), 0);
  41. if (WARN_ON(!pvtmon))
  42. return -ENOENT;
  43. tz = devm_thermal_of_zone_register(dev, 0,
  44. pvtmon,
  45. &ns_thermal_ops);
  46. if (IS_ERR(tz)) {
  47. iounmap(pvtmon);
  48. return PTR_ERR(tz);
  49. }
  50. platform_set_drvdata(pdev, pvtmon);
  51. return 0;
  52. }
  53. static int ns_thermal_remove(struct platform_device *pdev)
  54. {
  55. void __iomem *pvtmon = platform_get_drvdata(pdev);
  56. iounmap(pvtmon);
  57. return 0;
  58. }
  59. static const struct of_device_id ns_thermal_of_match[] = {
  60. { .compatible = "brcm,ns-thermal", },
  61. {},
  62. };
  63. MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
  64. static struct platform_driver ns_thermal_driver = {
  65. .probe = ns_thermal_probe,
  66. .remove = ns_thermal_remove,
  67. .driver = {
  68. .name = "ns-thermal",
  69. .of_match_table = ns_thermal_of_match,
  70. },
  71. };
  72. module_platform_driver(ns_thermal_driver);
  73. MODULE_AUTHOR("Rafał Miłecki <[email protected]>");
  74. MODULE_DESCRIPTION("Northstar thermal driver");
  75. MODULE_LICENSE("GPL v2");