tsens-common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include "tsens.h"
  21. #define S0_ST_ADDR 0x1030
  22. #define SN_ADDR_OFFSET 0x4
  23. #define SN_ST_TEMP_MASK 0x3ff
  24. #define CAL_DEGC_PT1 30
  25. #define CAL_DEGC_PT2 120
  26. #define SLOPE_FACTOR 1000
  27. #define SLOPE_DEFAULT 3200
  28. char *qfprom_read(struct device *dev, const char *cname)
  29. {
  30. struct nvmem_cell *cell;
  31. ssize_t data;
  32. char *ret;
  33. cell = nvmem_cell_get(dev, cname);
  34. if (IS_ERR(cell))
  35. return ERR_CAST(cell);
  36. ret = nvmem_cell_read(cell, &data);
  37. nvmem_cell_put(cell);
  38. return ret;
  39. }
  40. /*
  41. * Use this function on devices where slope and offset calculations
  42. * depend on calibration data read from qfprom. On others the slope
  43. * and offset values are derived from tz->tzp->slope and tz->tzp->offset
  44. * resp.
  45. */
  46. void compute_intercept_slope(struct tsens_device *tmdev, u32 *p1,
  47. u32 *p2, u32 mode)
  48. {
  49. int i;
  50. int num, den;
  51. for (i = 0; i < tmdev->num_sensors; i++) {
  52. dev_dbg(tmdev->dev,
  53. "sensor%d - data_point1:%#x data_point2:%#x\n",
  54. i, p1[i], p2[i]);
  55. tmdev->sensor[i].slope = SLOPE_DEFAULT;
  56. if (mode == TWO_PT_CALIB) {
  57. /*
  58. * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
  59. * temp_120_degc - temp_30_degc (x2 - x1)
  60. */
  61. num = p2[i] - p1[i];
  62. num *= SLOPE_FACTOR;
  63. den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
  64. tmdev->sensor[i].slope = num / den;
  65. }
  66. tmdev->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
  67. (CAL_DEGC_PT1 *
  68. tmdev->sensor[i].slope);
  69. dev_dbg(tmdev->dev, "offset:%d\n", tmdev->sensor[i].offset);
  70. }
  71. }
  72. static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
  73. {
  74. int degc, num, den;
  75. num = (adc_code * SLOPE_FACTOR) - s->offset;
  76. den = s->slope;
  77. if (num > 0)
  78. degc = num + (den / 2);
  79. else if (num < 0)
  80. degc = num - (den / 2);
  81. else
  82. degc = num;
  83. degc /= den;
  84. return degc;
  85. }
  86. int get_temp_common(struct tsens_device *tmdev, int id, int *temp)
  87. {
  88. struct tsens_sensor *s = &tmdev->sensor[id];
  89. u32 code;
  90. unsigned int sensor_addr;
  91. int last_temp = 0, ret;
  92. sensor_addr = S0_ST_ADDR + s->hw_id * SN_ADDR_OFFSET;
  93. ret = regmap_read(tmdev->map, sensor_addr, &code);
  94. if (ret)
  95. return ret;
  96. last_temp = code & SN_ST_TEMP_MASK;
  97. *temp = code_to_degc(last_temp, s) * 1000;
  98. return 0;
  99. }
  100. static const struct regmap_config tsens_config = {
  101. .reg_bits = 32,
  102. .val_bits = 32,
  103. .reg_stride = 4,
  104. };
  105. int __init init_common(struct tsens_device *tmdev)
  106. {
  107. void __iomem *base;
  108. base = of_iomap(tmdev->dev->of_node, 0);
  109. if (!base)
  110. return -EINVAL;
  111. tmdev->map = devm_regmap_init_mmio(tmdev->dev, base, &tsens_config);
  112. if (IS_ERR(tmdev->map)) {
  113. iounmap(base);
  114. return PTR_ERR(tmdev->map);
  115. }
  116. return 0;
  117. }