st_thermal_memmap.c 5.9 KB

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