st_thermal_memmap.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. return IRQ_HANDLED;
  40. }
  41. /* Private ops for the Memory Mapped based thermal sensors */
  42. static int st_mmap_power_ctrl(struct st_thermal_sensor *sensor,
  43. enum st_thermal_power_state power_state)
  44. {
  45. const unsigned int mask = (THERMAL_PDN | THERMAL_SRSTN);
  46. const unsigned int val = power_state ? mask : 0;
  47. return regmap_update_bits(sensor->regmap, STIH416_MPE_CONF, mask, val);
  48. }
  49. static int st_mmap_alloc_regfields(struct st_thermal_sensor *sensor)
  50. {
  51. struct device *dev = sensor->dev;
  52. struct regmap *regmap = sensor->regmap;
  53. const struct reg_field *reg_fields = sensor->cdata->reg_fields;
  54. sensor->int_thresh_hi = devm_regmap_field_alloc(dev, regmap,
  55. reg_fields[INT_THRESH_HI]);
  56. sensor->int_enable = devm_regmap_field_alloc(dev, regmap,
  57. reg_fields[INT_ENABLE]);
  58. if (IS_ERR(sensor->int_thresh_hi) || IS_ERR(sensor->int_enable)) {
  59. dev_err(dev, "failed to alloc mmap regfields\n");
  60. return -EINVAL;
  61. }
  62. return 0;
  63. }
  64. static int st_mmap_enable_irq(struct st_thermal_sensor *sensor)
  65. {
  66. int ret;
  67. /* Set upper critical threshold */
  68. ret = regmap_field_write(sensor->int_thresh_hi,
  69. sensor->cdata->crit_temp -
  70. sensor->cdata->temp_adjust_val);
  71. if (ret)
  72. return ret;
  73. return regmap_field_write(sensor->int_enable, 1);
  74. }
  75. static int st_mmap_register_enable_irq(struct st_thermal_sensor *sensor)
  76. {
  77. struct device *dev = sensor->dev;
  78. struct platform_device *pdev = to_platform_device(dev);
  79. int ret;
  80. sensor->irq = platform_get_irq(pdev, 0);
  81. if (sensor->irq < 0) {
  82. dev_err(dev, "failed to register IRQ\n");
  83. return sensor->irq;
  84. }
  85. ret = devm_request_threaded_irq(dev, sensor->irq,
  86. NULL, st_mmap_thermal_trip_handler,
  87. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  88. dev->driver->name, sensor);
  89. if (ret) {
  90. dev_err(dev, "failed to register IRQ %d\n", sensor->irq);
  91. return ret;
  92. }
  93. return st_mmap_enable_irq(sensor);
  94. }
  95. static const struct regmap_config st_416mpe_regmap_config = {
  96. .reg_bits = 32,
  97. .val_bits = 32,
  98. .reg_stride = 4,
  99. };
  100. static int st_mmap_regmap_init(struct st_thermal_sensor *sensor)
  101. {
  102. struct device *dev = sensor->dev;
  103. struct platform_device *pdev = to_platform_device(dev);
  104. struct resource *res;
  105. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  106. if (!res) {
  107. dev_err(dev, "no memory resources defined\n");
  108. return -ENODEV;
  109. }
  110. sensor->mmio_base = devm_ioremap_resource(dev, res);
  111. if (IS_ERR(sensor->mmio_base)) {
  112. dev_err(dev, "failed to remap IO\n");
  113. return PTR_ERR(sensor->mmio_base);
  114. }
  115. sensor->regmap = devm_regmap_init_mmio(dev, sensor->mmio_base,
  116. &st_416mpe_regmap_config);
  117. if (IS_ERR(sensor->regmap)) {
  118. dev_err(dev, "failed to initialise regmap\n");
  119. return PTR_ERR(sensor->regmap);
  120. }
  121. return 0;
  122. }
  123. static const struct st_thermal_sensor_ops st_mmap_sensor_ops = {
  124. .power_ctrl = st_mmap_power_ctrl,
  125. .alloc_regfields = st_mmap_alloc_regfields,
  126. .regmap_init = st_mmap_regmap_init,
  127. .register_enable_irq = st_mmap_register_enable_irq,
  128. .enable_irq = st_mmap_enable_irq,
  129. };
  130. /* Compatible device data stih416 mpe thermal sensor */
  131. static const struct st_thermal_compat_data st_416mpe_cdata = {
  132. .reg_fields = st_mmap_thermal_regfields,
  133. .ops = &st_mmap_sensor_ops,
  134. .calibration_val = 14,
  135. .temp_adjust_val = -95,
  136. .crit_temp = 120,
  137. };
  138. /* Compatible device data stih407 thermal sensor */
  139. static const struct st_thermal_compat_data st_407_cdata = {
  140. .reg_fields = st_mmap_thermal_regfields,
  141. .ops = &st_mmap_sensor_ops,
  142. .calibration_val = 16,
  143. .temp_adjust_val = -95,
  144. .crit_temp = 120,
  145. };
  146. static const struct of_device_id st_mmap_thermal_of_match[] = {
  147. { .compatible = "st,stih416-mpe-thermal", .data = &st_416mpe_cdata },
  148. { .compatible = "st,stih407-thermal", .data = &st_407_cdata },
  149. { /* sentinel */ }
  150. };
  151. MODULE_DEVICE_TABLE(of, st_mmap_thermal_of_match);
  152. static int st_mmap_probe(struct platform_device *pdev)
  153. {
  154. return st_thermal_register(pdev, st_mmap_thermal_of_match);
  155. }
  156. static int st_mmap_remove(struct platform_device *pdev)
  157. {
  158. return st_thermal_unregister(pdev);
  159. }
  160. static struct platform_driver st_mmap_thermal_driver = {
  161. .driver = {
  162. .name = "st_thermal_mmap",
  163. .pm = &st_thermal_pm_ops,
  164. .of_match_table = st_mmap_thermal_of_match,
  165. },
  166. .probe = st_mmap_probe,
  167. .remove = st_mmap_remove,
  168. };
  169. module_platform_driver(st_mmap_thermal_driver);
  170. MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
  171. MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
  172. MODULE_LICENSE("GPL v2");