st_thermal_syscfg.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * ST Thermal Sensor Driver for syscfg based sensors.
  3. * Author: Ajit Pal Singh <ajitpal.singh@st.com>
  4. *
  5. * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/of.h>
  13. #include <linux/module.h>
  14. #include <linux/mfd/syscon.h>
  15. #include "st_thermal.h"
  16. /* STiH415 */
  17. #define STIH415_SYSCFG_FRONT(num) ((num - 100) * 4)
  18. #define STIH415_SAS_THSENS_CONF STIH415_SYSCFG_FRONT(178)
  19. #define STIH415_SAS_THSENS_STATUS STIH415_SYSCFG_FRONT(198)
  20. #define STIH415_SYSCFG_MPE(num) ((num - 600) * 4)
  21. #define STIH415_MPE_THSENS_CONF STIH415_SYSCFG_MPE(607)
  22. #define STIH415_MPE_THSENS_STATUS STIH415_SYSCFG_MPE(667)
  23. /* STiH416 */
  24. #define STIH416_SYSCFG_FRONT(num) ((num - 1000) * 4)
  25. #define STIH416_SAS_THSENS_CONF STIH416_SYSCFG_FRONT(1552)
  26. #define STIH416_SAS_THSENS_STATUS1 STIH416_SYSCFG_FRONT(1554)
  27. #define STIH416_SAS_THSENS_STATUS2 STIH416_SYSCFG_FRONT(1594)
  28. /* STiD127 */
  29. #define STID127_SYSCFG_CPU(num) ((num - 700) * 4)
  30. #define STID127_THSENS_CONF STID127_SYSCFG_CPU(743)
  31. #define STID127_THSENS_STATUS STID127_SYSCFG_CPU(767)
  32. static const struct reg_field st_415sas_regfields[MAX_REGFIELDS] = {
  33. [TEMP_PWR] = REG_FIELD(STIH415_SAS_THSENS_CONF, 9, 9),
  34. [DCORRECT] = REG_FIELD(STIH415_SAS_THSENS_CONF, 4, 8),
  35. [OVERFLOW] = REG_FIELD(STIH415_SAS_THSENS_STATUS, 8, 8),
  36. [DATA] = REG_FIELD(STIH415_SAS_THSENS_STATUS, 10, 16),
  37. };
  38. static const struct reg_field st_415mpe_regfields[MAX_REGFIELDS] = {
  39. [TEMP_PWR] = REG_FIELD(STIH415_MPE_THSENS_CONF, 8, 8),
  40. [DCORRECT] = REG_FIELD(STIH415_MPE_THSENS_CONF, 3, 7),
  41. [OVERFLOW] = REG_FIELD(STIH415_MPE_THSENS_STATUS, 9, 9),
  42. [DATA] = REG_FIELD(STIH415_MPE_THSENS_STATUS, 11, 18),
  43. };
  44. static const struct reg_field st_416sas_regfields[MAX_REGFIELDS] = {
  45. [TEMP_PWR] = REG_FIELD(STIH416_SAS_THSENS_CONF, 9, 9),
  46. [DCORRECT] = REG_FIELD(STIH416_SAS_THSENS_CONF, 4, 8),
  47. [OVERFLOW] = REG_FIELD(STIH416_SAS_THSENS_STATUS1, 8, 8),
  48. [DATA] = REG_FIELD(STIH416_SAS_THSENS_STATUS2, 10, 16),
  49. };
  50. static const struct reg_field st_127_regfields[MAX_REGFIELDS] = {
  51. [TEMP_PWR] = REG_FIELD(STID127_THSENS_CONF, 7, 7),
  52. [DCORRECT] = REG_FIELD(STID127_THSENS_CONF, 2, 6),
  53. [OVERFLOW] = REG_FIELD(STID127_THSENS_STATUS, 9, 9),
  54. [DATA] = REG_FIELD(STID127_THSENS_STATUS, 11, 18),
  55. };
  56. /* Private OPs for System Configuration Register based thermal sensors */
  57. static int st_syscfg_power_ctrl(struct st_thermal_sensor *sensor,
  58. enum st_thermal_power_state power_state)
  59. {
  60. return regmap_field_write(sensor->pwr, power_state);
  61. }
  62. static int st_syscfg_alloc_regfields(struct st_thermal_sensor *sensor)
  63. {
  64. struct device *dev = sensor->dev;
  65. sensor->pwr = devm_regmap_field_alloc(dev, sensor->regmap,
  66. sensor->cdata->reg_fields[TEMP_PWR]);
  67. if (IS_ERR(sensor->pwr)) {
  68. dev_err(dev, "failed to alloc syscfg regfields\n");
  69. return PTR_ERR(sensor->pwr);
  70. }
  71. return 0;
  72. }
  73. static int st_syscfg_regmap_init(struct st_thermal_sensor *sensor)
  74. {
  75. sensor->regmap =
  76. syscon_regmap_lookup_by_compatible(sensor->cdata->sys_compat);
  77. if (IS_ERR(sensor->regmap)) {
  78. dev_err(sensor->dev, "failed to find syscfg regmap\n");
  79. return PTR_ERR(sensor->regmap);
  80. }
  81. return 0;
  82. }
  83. static const struct st_thermal_sensor_ops st_syscfg_sensor_ops = {
  84. .power_ctrl = st_syscfg_power_ctrl,
  85. .alloc_regfields = st_syscfg_alloc_regfields,
  86. .regmap_init = st_syscfg_regmap_init,
  87. };
  88. /* Compatible device data for stih415 sas thermal sensor */
  89. static const struct st_thermal_compat_data st_415sas_cdata = {
  90. .sys_compat = "st,stih415-front-syscfg",
  91. .reg_fields = st_415sas_regfields,
  92. .ops = &st_syscfg_sensor_ops,
  93. .calibration_val = 16,
  94. .temp_adjust_val = 20,
  95. .crit_temp = 120,
  96. };
  97. /* Compatible device data for stih415 mpe thermal sensor */
  98. static const struct st_thermal_compat_data st_415mpe_cdata = {
  99. .sys_compat = "st,stih415-system-syscfg",
  100. .reg_fields = st_415mpe_regfields,
  101. .ops = &st_syscfg_sensor_ops,
  102. .calibration_val = 16,
  103. .temp_adjust_val = -103,
  104. .crit_temp = 120,
  105. };
  106. /* Compatible device data for stih416 sas thermal sensor */
  107. static const struct st_thermal_compat_data st_416sas_cdata = {
  108. .sys_compat = "st,stih416-front-syscfg",
  109. .reg_fields = st_416sas_regfields,
  110. .ops = &st_syscfg_sensor_ops,
  111. .calibration_val = 16,
  112. .temp_adjust_val = 20,
  113. .crit_temp = 120,
  114. };
  115. /* Compatible device data for stid127 thermal sensor */
  116. static const struct st_thermal_compat_data st_127_cdata = {
  117. .sys_compat = "st,stid127-cpu-syscfg",
  118. .reg_fields = st_127_regfields,
  119. .ops = &st_syscfg_sensor_ops,
  120. .calibration_val = 8,
  121. .temp_adjust_val = -103,
  122. .crit_temp = 120,
  123. };
  124. static const struct of_device_id st_syscfg_thermal_of_match[] = {
  125. { .compatible = "st,stih415-sas-thermal", .data = &st_415sas_cdata },
  126. { .compatible = "st,stih415-mpe-thermal", .data = &st_415mpe_cdata },
  127. { .compatible = "st,stih416-sas-thermal", .data = &st_416sas_cdata },
  128. { .compatible = "st,stid127-thermal", .data = &st_127_cdata },
  129. { /* sentinel */ }
  130. };
  131. MODULE_DEVICE_TABLE(of, st_syscfg_thermal_of_match);
  132. static int st_syscfg_probe(struct platform_device *pdev)
  133. {
  134. return st_thermal_register(pdev, st_syscfg_thermal_of_match);
  135. }
  136. static int st_syscfg_remove(struct platform_device *pdev)
  137. {
  138. return st_thermal_unregister(pdev);
  139. }
  140. static struct platform_driver st_syscfg_thermal_driver = {
  141. .driver = {
  142. .name = "st_syscfg_thermal",
  143. .pm = &st_thermal_pm_ops,
  144. .of_match_table = st_syscfg_thermal_of_match,
  145. },
  146. .probe = st_syscfg_probe,
  147. .remove = st_syscfg_remove,
  148. };
  149. module_platform_driver(st_syscfg_thermal_driver);
  150. MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
  151. MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
  152. MODULE_LICENSE("GPL v2");