tsens-common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/nvmem-consumer.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include "tsens.h"
  22. #define S0_ST_ADDR 0x1030
  23. #define SN_ADDR_OFFSET 0x4
  24. #define SN_ST_TEMP_MASK 0x3ff
  25. #define CAL_DEGC_PT1 30
  26. #define CAL_DEGC_PT2 120
  27. #define SLOPE_FACTOR 1000
  28. #define SLOPE_DEFAULT 3200
  29. char *qfprom_read(struct device *dev, const char *cname)
  30. {
  31. struct nvmem_cell *cell;
  32. ssize_t data;
  33. char *ret;
  34. cell = nvmem_cell_get(dev, cname);
  35. if (IS_ERR(cell))
  36. return ERR_CAST(cell);
  37. ret = nvmem_cell_read(cell, &data);
  38. nvmem_cell_put(cell);
  39. return ret;
  40. }
  41. /*
  42. * Use this function on devices where slope and offset calculations
  43. * depend on calibration data read from qfprom. On others the slope
  44. * and offset values are derived from tz->tzp->slope and tz->tzp->offset
  45. * resp.
  46. */
  47. void compute_intercept_slope(struct tsens_device *tmdev, u32 *p1,
  48. u32 *p2, u32 mode)
  49. {
  50. int i;
  51. int num, den;
  52. for (i = 0; i < tmdev->num_sensors; i++) {
  53. dev_dbg(tmdev->dev,
  54. "sensor%d - data_point1:%#x data_point2:%#x\n",
  55. i, p1[i], p2[i]);
  56. tmdev->sensor[i].slope = SLOPE_DEFAULT;
  57. if (mode == TWO_PT_CALIB) {
  58. /*
  59. * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
  60. * temp_120_degc - temp_30_degc (x2 - x1)
  61. */
  62. num = p2[i] - p1[i];
  63. num *= SLOPE_FACTOR;
  64. den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
  65. tmdev->sensor[i].slope = num / den;
  66. }
  67. tmdev->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
  68. (CAL_DEGC_PT1 *
  69. tmdev->sensor[i].slope);
  70. dev_dbg(tmdev->dev, "offset:%d\n", tmdev->sensor[i].offset);
  71. }
  72. }
  73. static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
  74. {
  75. int degc, num, den;
  76. num = (adc_code * SLOPE_FACTOR) - s->offset;
  77. den = s->slope;
  78. if (num > 0)
  79. degc = num + (den / 2);
  80. else if (num < 0)
  81. degc = num - (den / 2);
  82. else
  83. degc = num;
  84. degc /= den;
  85. return degc;
  86. }
  87. int get_temp_common(struct tsens_device *tmdev, int id, int *temp)
  88. {
  89. struct tsens_sensor *s = &tmdev->sensor[id];
  90. u32 code;
  91. unsigned int status_reg;
  92. int last_temp = 0, ret;
  93. status_reg = S0_ST_ADDR + s->hw_id * SN_ADDR_OFFSET;
  94. ret = regmap_read(tmdev->map, status_reg, &code);
  95. if (ret)
  96. return ret;
  97. last_temp = code & SN_ST_TEMP_MASK;
  98. *temp = code_to_degc(last_temp, s) * 1000;
  99. return 0;
  100. }
  101. static const struct regmap_config tsens_config = {
  102. .reg_bits = 32,
  103. .val_bits = 32,
  104. .reg_stride = 4,
  105. };
  106. int __init init_common(struct tsens_device *tmdev)
  107. {
  108. void __iomem *base;
  109. struct resource *res;
  110. struct platform_device *op = of_find_device_by_node(tmdev->dev->of_node);
  111. if (!op)
  112. return -EINVAL;
  113. /* The driver only uses the TM register address space for now */
  114. if (op->num_resources > 1) {
  115. tmdev->tm_offset = 0;
  116. } else {
  117. /* old DTs where SROT and TM were in a contiguous 2K block */
  118. tmdev->tm_offset = 0x1000;
  119. }
  120. res = platform_get_resource(op, IORESOURCE_MEM, 0);
  121. base = devm_ioremap_resource(&op->dev, res);
  122. if (IS_ERR(base))
  123. return PTR_ERR(base);
  124. tmdev->map = devm_regmap_init_mmio(tmdev->dev, base, &tsens_config);
  125. if (IS_ERR(tmdev->map))
  126. return PTR_ERR(tmdev->map);
  127. return 0;
  128. }