ns-thermal.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/of_address.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/thermal.h>
  12. #define PVTMON_CONTROL0 0x00
  13. #define PVTMON_CONTROL0_SEL_MASK 0x0000000e
  14. #define PVTMON_CONTROL0_SEL_TEMP_MONITOR 0x00000000
  15. #define PVTMON_CONTROL0_SEL_TEST_MODE 0x0000000e
  16. #define PVTMON_STATUS 0x08
  17. struct ns_thermal {
  18. struct thermal_zone_device *tz;
  19. void __iomem *pvtmon;
  20. };
  21. static int ns_thermal_get_temp(void *data, int *temp)
  22. {
  23. struct ns_thermal *ns_thermal = data;
  24. int offset = thermal_zone_get_offset(ns_thermal->tz);
  25. int slope = thermal_zone_get_slope(ns_thermal->tz);
  26. u32 val;
  27. val = readl(ns_thermal->pvtmon + PVTMON_CONTROL0);
  28. if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
  29. /* Clear current mode selection */
  30. val &= ~PVTMON_CONTROL0_SEL_MASK;
  31. /* Set temp monitor mode (it's the default actually) */
  32. val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
  33. writel(val, ns_thermal->pvtmon + PVTMON_CONTROL0);
  34. }
  35. val = readl(ns_thermal->pvtmon + PVTMON_STATUS);
  36. *temp = slope * val + offset;
  37. return 0;
  38. }
  39. static const struct thermal_zone_of_device_ops ns_thermal_ops = {
  40. .get_temp = ns_thermal_get_temp,
  41. };
  42. static int ns_thermal_probe(struct platform_device *pdev)
  43. {
  44. struct device *dev = &pdev->dev;
  45. struct ns_thermal *ns_thermal;
  46. ns_thermal = devm_kzalloc(dev, sizeof(*ns_thermal), GFP_KERNEL);
  47. if (!ns_thermal)
  48. return -ENOMEM;
  49. ns_thermal->pvtmon = of_iomap(dev_of_node(dev), 0);
  50. if (WARN_ON(!ns_thermal->pvtmon))
  51. return -ENOENT;
  52. ns_thermal->tz = devm_thermal_zone_of_sensor_register(dev, 0,
  53. ns_thermal,
  54. &ns_thermal_ops);
  55. if (IS_ERR(ns_thermal->tz)) {
  56. iounmap(ns_thermal->pvtmon);
  57. return PTR_ERR(ns_thermal->tz);
  58. }
  59. platform_set_drvdata(pdev, ns_thermal);
  60. return 0;
  61. }
  62. static int ns_thermal_remove(struct platform_device *pdev)
  63. {
  64. struct ns_thermal *ns_thermal = platform_get_drvdata(pdev);
  65. iounmap(ns_thermal->pvtmon);
  66. return 0;
  67. }
  68. static const struct of_device_id ns_thermal_of_match[] = {
  69. { .compatible = "brcm,ns-thermal", },
  70. {},
  71. };
  72. MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
  73. static struct platform_driver ns_thermal_driver = {
  74. .probe = ns_thermal_probe,
  75. .remove = ns_thermal_remove,
  76. .driver = {
  77. .name = "ns-thermal",
  78. .of_match_table = ns_thermal_of_match,
  79. },
  80. };
  81. module_platform_driver(ns_thermal_driver);
  82. MODULE_AUTHOR("Rafał Miłecki <rafal@milecki.pl>");
  83. MODULE_DESCRIPTION("Northstar thermal driver");
  84. MODULE_LICENSE("GPL v2");