st_thermal_memmap.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ST Thermal Sensor Driver for memory mapped sensors.
  4. * Author: Ajit Pal Singh <ajitpal.singh@st.com>
  5. *
  6. * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
  7. */
  8. #include <linux/of.h>
  9. #include <linux/module.h>
  10. #include "st_thermal.h"
  11. #define STIH416_MPE_CONF 0x0
  12. #define STIH416_MPE_STATUS 0x4
  13. #define STIH416_MPE_INT_THRESH 0x8
  14. #define STIH416_MPE_INT_EN 0xC
  15. /* Power control bits for the memory mapped thermal sensor */
  16. #define THERMAL_PDN BIT(4)
  17. #define THERMAL_SRSTN BIT(10)
  18. static const struct reg_field st_mmap_thermal_regfields[MAX_REGFIELDS] = {
  19. /*
  20. * According to the STIH416 MPE temp sensor data sheet -
  21. * the PDN (Power Down Bit) and SRSTN (Soft Reset Bit) need to be
  22. * written simultaneously for powering on and off the temperature
  23. * sensor. regmap_update_bits() will be used to update the register.
  24. */
  25. [INT_THRESH_HI] = REG_FIELD(STIH416_MPE_INT_THRESH, 0, 7),
  26. [DCORRECT] = REG_FIELD(STIH416_MPE_CONF, 5, 9),
  27. [OVERFLOW] = REG_FIELD(STIH416_MPE_STATUS, 9, 9),
  28. [DATA] = REG_FIELD(STIH416_MPE_STATUS, 11, 18),
  29. [INT_ENABLE] = REG_FIELD(STIH416_MPE_INT_EN, 0, 0),
  30. };
  31. static irqreturn_t st_mmap_thermal_trip_handler(int irq, void *sdata)
  32. {
  33. struct st_thermal_sensor *sensor = sdata;
  34. thermal_zone_device_update(sensor->thermal_dev,
  35. THERMAL_EVENT_UNSPECIFIED);
  36. return IRQ_HANDLED;
  37. }
  38. /* Private ops for the Memory Mapped based thermal sensors */
  39. static int st_mmap_power_ctrl(struct st_thermal_sensor *sensor,
  40. enum st_thermal_power_state power_state)
  41. {
  42. const unsigned int mask = (THERMAL_PDN | THERMAL_SRSTN);
  43. const unsigned int val = power_state ? mask : 0;
  44. return regmap_update_bits(sensor->regmap, STIH416_MPE_CONF, mask, val);
  45. }
  46. static int st_mmap_alloc_regfields(struct st_thermal_sensor *sensor)
  47. {
  48. struct device *dev = sensor->dev;
  49. struct regmap *regmap = sensor->regmap;
  50. const struct reg_field *reg_fields = sensor->cdata->reg_fields;
  51. sensor->int_thresh_hi = devm_regmap_field_alloc(dev, regmap,
  52. reg_fields[INT_THRESH_HI]);
  53. sensor->int_enable = devm_regmap_field_alloc(dev, regmap,
  54. reg_fields[INT_ENABLE]);
  55. if (IS_ERR(sensor->int_thresh_hi) || IS_ERR(sensor->int_enable)) {
  56. dev_err(dev, "failed to alloc mmap regfields\n");
  57. return -EINVAL;
  58. }
  59. return 0;
  60. }
  61. static int st_mmap_enable_irq(struct st_thermal_sensor *sensor)
  62. {
  63. int ret;
  64. /* Set upper critical threshold */
  65. ret = regmap_field_write(sensor->int_thresh_hi,
  66. sensor->cdata->crit_temp -
  67. sensor->cdata->temp_adjust_val);
  68. if (ret)
  69. return ret;
  70. return regmap_field_write(sensor->int_enable, 1);
  71. }
  72. static int st_mmap_register_enable_irq(struct st_thermal_sensor *sensor)
  73. {
  74. struct device *dev = sensor->dev;
  75. struct platform_device *pdev = to_platform_device(dev);
  76. int ret;
  77. sensor->irq = platform_get_irq(pdev, 0);
  78. if (sensor->irq < 0)
  79. return sensor->irq;
  80. ret = devm_request_threaded_irq(dev, sensor->irq,
  81. NULL, st_mmap_thermal_trip_handler,
  82. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  83. dev->driver->name, sensor);
  84. if (ret) {
  85. dev_err(dev, "failed to register IRQ %d\n", sensor->irq);
  86. return ret;
  87. }
  88. return st_mmap_enable_irq(sensor);
  89. }
  90. static const struct regmap_config st_416mpe_regmap_config = {
  91. .reg_bits = 32,
  92. .val_bits = 32,
  93. .reg_stride = 4,
  94. };
  95. static int st_mmap_regmap_init(struct st_thermal_sensor *sensor)
  96. {
  97. struct device *dev = sensor->dev;
  98. struct platform_device *pdev = to_platform_device(dev);
  99. sensor->mmio_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  100. if (IS_ERR(sensor->mmio_base))
  101. return PTR_ERR(sensor->mmio_base);
  102. sensor->regmap = devm_regmap_init_mmio(dev, sensor->mmio_base,
  103. &st_416mpe_regmap_config);
  104. if (IS_ERR(sensor->regmap)) {
  105. dev_err(dev, "failed to initialise regmap\n");
  106. return PTR_ERR(sensor->regmap);
  107. }
  108. return 0;
  109. }
  110. static const struct st_thermal_sensor_ops st_mmap_sensor_ops = {
  111. .power_ctrl = st_mmap_power_ctrl,
  112. .alloc_regfields = st_mmap_alloc_regfields,
  113. .regmap_init = st_mmap_regmap_init,
  114. .register_enable_irq = st_mmap_register_enable_irq,
  115. .enable_irq = st_mmap_enable_irq,
  116. };
  117. /* Compatible device data stih407 thermal sensor */
  118. static const struct st_thermal_compat_data st_407_cdata = {
  119. .reg_fields = st_mmap_thermal_regfields,
  120. .ops = &st_mmap_sensor_ops,
  121. .calibration_val = 16,
  122. .temp_adjust_val = -95,
  123. .crit_temp = 120,
  124. };
  125. static const struct of_device_id st_mmap_thermal_of_match[] = {
  126. { .compatible = "st,stih407-thermal", .data = &st_407_cdata },
  127. { /* sentinel */ }
  128. };
  129. MODULE_DEVICE_TABLE(of, st_mmap_thermal_of_match);
  130. static int st_mmap_probe(struct platform_device *pdev)
  131. {
  132. return st_thermal_register(pdev, st_mmap_thermal_of_match);
  133. }
  134. static void st_mmap_remove(struct platform_device *pdev)
  135. {
  136. st_thermal_unregister(pdev);
  137. }
  138. static struct platform_driver st_mmap_thermal_driver = {
  139. .driver = {
  140. .name = "st_thermal_mmap",
  141. .pm = pm_sleep_ptr(&st_thermal_pm_ops),
  142. .of_match_table = st_mmap_thermal_of_match,
  143. },
  144. .probe = st_mmap_probe,
  145. .remove_new = st_mmap_remove,
  146. };
  147. module_platform_driver(st_mmap_thermal_driver);
  148. MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
  149. MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
  150. MODULE_LICENSE("GPL v2");