2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
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.
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.
15 #include <linux/err.h>
17 #include <linux/nvmem-consumer.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
23 #define S0_ST_ADDR 0x1030
24 #define SN_ADDR_OFFSET 0x4
25 #define SN_ST_TEMP_MASK 0x3ff
26 #define CAL_DEGC_PT1 30
27 #define CAL_DEGC_PT2 120
28 #define SLOPE_FACTOR 1000
29 #define SLOPE_DEFAULT 3200
31 char *qfprom_read(struct device *dev, const char *cname)
33 struct nvmem_cell *cell;
37 cell = nvmem_cell_get(dev, cname);
39 return ERR_CAST(cell);
41 ret = nvmem_cell_read(cell, &data);
48 * Use this function on devices where slope and offset calculations
49 * depend on calibration data read from qfprom. On others the slope
50 * and offset values are derived from tz->tzp->slope and tz->tzp->offset
53 void compute_intercept_slope(struct tsens_device *tmdev, u32 *p1,
59 for (i = 0; i < tmdev->num_sensors; i++) {
61 "sensor%d - data_point1:%#x data_point2:%#x\n",
64 tmdev->sensor[i].slope = SLOPE_DEFAULT;
65 if (mode == TWO_PT_CALIB) {
67 * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
68 * temp_120_degc - temp_30_degc (x2 - x1)
72 den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
73 tmdev->sensor[i].slope = num / den;
76 tmdev->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
78 tmdev->sensor[i].slope);
79 dev_dbg(tmdev->dev, "offset:%d\n", tmdev->sensor[i].offset);
83 static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
87 num = (adc_code * SLOPE_FACTOR) - s->offset;
91 degc = num + (den / 2);
93 degc = num - (den / 2);
102 int get_temp_common(struct tsens_device *tmdev, int id, int *temp)
104 struct tsens_sensor *s = &tmdev->sensor[id];
106 unsigned int sensor_addr;
107 int last_temp = 0, ret;
109 sensor_addr = S0_ST_ADDR + s->hw_id * SN_ADDR_OFFSET;
110 ret = regmap_read(tmdev->map, sensor_addr, &code);
113 last_temp = code & SN_ST_TEMP_MASK;
115 *temp = code_to_degc(last_temp, s) * 1000;
120 static const struct regmap_config tsens_config = {
126 int __init init_common(struct tsens_device *tmdev)
130 base = of_iomap(tmdev->dev->of_node, 0);
134 tmdev->map = devm_regmap_init_mmio(tmdev->dev, base, &tsens_config);
135 if (IS_ERR(tmdev->map)) {
137 return PTR_ERR(tmdev->map);