tsens-common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/io.h>
  7. #include <linux/nvmem-consumer.h>
  8. #include <linux/of_address.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regmap.h>
  12. #include "tsens.h"
  13. /* SROT */
  14. #define TSENS_EN BIT(0)
  15. /* TM */
  16. #define STATUS_OFFSET 0x30
  17. #define SN_ADDR_OFFSET 0x4
  18. #define SN_ST_TEMP_MASK 0x3ff
  19. #define CAL_DEGC_PT1 30
  20. #define CAL_DEGC_PT2 120
  21. #define SLOPE_FACTOR 1000
  22. #define SLOPE_DEFAULT 3200
  23. char *qfprom_read(struct device *dev, const char *cname)
  24. {
  25. struct nvmem_cell *cell;
  26. ssize_t data;
  27. char *ret;
  28. cell = nvmem_cell_get(dev, cname);
  29. if (IS_ERR(cell))
  30. return ERR_CAST(cell);
  31. ret = nvmem_cell_read(cell, &data);
  32. nvmem_cell_put(cell);
  33. return ret;
  34. }
  35. /*
  36. * Use this function on devices where slope and offset calculations
  37. * depend on calibration data read from qfprom. On others the slope
  38. * and offset values are derived from tz->tzp->slope and tz->tzp->offset
  39. * resp.
  40. */
  41. void compute_intercept_slope(struct tsens_device *tmdev, u32 *p1,
  42. u32 *p2, u32 mode)
  43. {
  44. int i;
  45. int num, den;
  46. for (i = 0; i < tmdev->num_sensors; i++) {
  47. dev_dbg(tmdev->dev,
  48. "sensor%d - data_point1:%#x data_point2:%#x\n",
  49. i, p1[i], p2[i]);
  50. tmdev->sensor[i].slope = SLOPE_DEFAULT;
  51. if (mode == TWO_PT_CALIB) {
  52. /*
  53. * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
  54. * temp_120_degc - temp_30_degc (x2 - x1)
  55. */
  56. num = p2[i] - p1[i];
  57. num *= SLOPE_FACTOR;
  58. den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
  59. tmdev->sensor[i].slope = num / den;
  60. }
  61. tmdev->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
  62. (CAL_DEGC_PT1 *
  63. tmdev->sensor[i].slope);
  64. dev_dbg(tmdev->dev, "offset:%d\n", tmdev->sensor[i].offset);
  65. }
  66. }
  67. static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
  68. {
  69. int degc, num, den;
  70. num = (adc_code * SLOPE_FACTOR) - s->offset;
  71. den = s->slope;
  72. if (num > 0)
  73. degc = num + (den / 2);
  74. else if (num < 0)
  75. degc = num - (den / 2);
  76. else
  77. degc = num;
  78. degc /= den;
  79. return degc;
  80. }
  81. int get_temp_common(struct tsens_device *tmdev, int id, int *temp)
  82. {
  83. struct tsens_sensor *s = &tmdev->sensor[id];
  84. u32 code;
  85. unsigned int status_reg;
  86. int last_temp = 0, ret;
  87. status_reg = tmdev->tm_offset + STATUS_OFFSET + s->hw_id * SN_ADDR_OFFSET;
  88. ret = regmap_read(tmdev->tm_map, status_reg, &code);
  89. if (ret)
  90. return ret;
  91. last_temp = code & SN_ST_TEMP_MASK;
  92. *temp = code_to_degc(last_temp, s) * 1000;
  93. return 0;
  94. }
  95. static const struct regmap_config tsens_config = {
  96. .reg_bits = 32,
  97. .val_bits = 32,
  98. .reg_stride = 4,
  99. };
  100. int __init init_common(struct tsens_device *tmdev)
  101. {
  102. void __iomem *tm_base, *srot_base;
  103. struct resource *res;
  104. u32 code;
  105. int ret;
  106. struct platform_device *op = of_find_device_by_node(tmdev->dev->of_node);
  107. u16 ctrl_offset = tmdev->reg_offsets[SROT_CTRL_OFFSET];
  108. if (!op)
  109. return -EINVAL;
  110. if (op->num_resources > 1) {
  111. /* DT with separate SROT and TM address space */
  112. tmdev->tm_offset = 0;
  113. res = platform_get_resource(op, IORESOURCE_MEM, 1);
  114. srot_base = devm_ioremap_resource(&op->dev, res);
  115. if (IS_ERR(srot_base))
  116. return PTR_ERR(srot_base);
  117. tmdev->srot_map = devm_regmap_init_mmio(tmdev->dev,
  118. srot_base, &tsens_config);
  119. if (IS_ERR(tmdev->srot_map))
  120. return PTR_ERR(tmdev->srot_map);
  121. } else {
  122. /* old DTs where SROT and TM were in a contiguous 2K block */
  123. tmdev->tm_offset = 0x1000;
  124. }
  125. res = platform_get_resource(op, IORESOURCE_MEM, 0);
  126. tm_base = devm_ioremap_resource(&op->dev, res);
  127. if (IS_ERR(tm_base))
  128. return PTR_ERR(tm_base);
  129. tmdev->tm_map = devm_regmap_init_mmio(tmdev->dev, tm_base, &tsens_config);
  130. if (IS_ERR(tmdev->tm_map))
  131. return PTR_ERR(tmdev->tm_map);
  132. if (tmdev->srot_map) {
  133. ret = regmap_read(tmdev->srot_map, ctrl_offset, &code);
  134. if (ret)
  135. return ret;
  136. if (!(code & TSENS_EN)) {
  137. dev_err(tmdev->dev, "tsens device is not enabled\n");
  138. return -ENODEV;
  139. }
  140. }
  141. return 0;
  142. }