1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
8 #include <linux/nvmem-consumer.h>
9 #include <linux/of_address.h>
10 #include <linux/of_platform.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
15 char *qfprom_read(struct device *dev, const char *cname)
17 struct nvmem_cell *cell;
21 cell = nvmem_cell_get(dev, cname);
23 return ERR_CAST(cell);
25 ret = nvmem_cell_read(cell, &data);
32 * Use this function on devices where slope and offset calculations
33 * depend on calibration data read from qfprom. On others the slope
34 * and offset values are derived from tz->tzp->slope and tz->tzp->offset
37 void compute_intercept_slope(struct tsens_priv *priv, u32 *p1,
43 for (i = 0; i < priv->num_sensors; i++) {
45 "sensor%d - data_point1:%#x data_point2:%#x\n",
48 priv->sensor[i].slope = SLOPE_DEFAULT;
49 if (mode == TWO_PT_CALIB) {
51 * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
52 * temp_120_degc - temp_30_degc (x2 - x1)
56 den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
57 priv->sensor[i].slope = num / den;
60 priv->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
62 priv->sensor[i].slope);
63 dev_dbg(priv->dev, "offset:%d\n", priv->sensor[i].offset);
67 bool is_sensor_enabled(struct tsens_priv *priv, u32 hw_id)
72 if ((hw_id > (priv->num_sensors - 1)) || (hw_id < 0))
74 ret = regmap_field_read(priv->rf[SENSOR_EN], &val);
78 return val & (1 << hw_id);
81 static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
85 num = (adc_code * SLOPE_FACTOR) - s->offset;
89 degc = num + (den / 2);
91 degc = num - (den / 2);
100 int get_temp_tsens_valid(struct tsens_priv *priv, int i, int *temp)
102 struct tsens_sensor *s = &priv->sensor[i];
103 u32 temp_idx = LAST_TEMP_0 + s->hw_id;
104 u32 valid_idx = VALID_0 + s->hw_id;
105 u32 last_temp = 0, valid, mask;
108 ret = regmap_field_read(priv->rf[valid_idx], &valid);
112 /* Valid bit is 0 for 6 AHB clock cycles.
113 * At 19.2MHz, 1 AHB clock is ~60ns.
114 * We should enter this loop very, very rarely.
117 ret = regmap_field_read(priv->rf[valid_idx], &valid);
122 /* Valid bit is set, OK to read the temperature */
123 ret = regmap_field_read(priv->rf[temp_idx], &last_temp);
127 if (priv->feat->adc) {
128 /* Convert temperature from ADC code to milliCelsius */
129 *temp = code_to_degc(last_temp, s) * 1000;
131 mask = GENMASK(priv->fields[LAST_TEMP_0].msb,
132 priv->fields[LAST_TEMP_0].lsb);
133 /* Convert temperature from deciCelsius to milliCelsius */
134 *temp = sign_extend32(last_temp, fls(mask) - 1) * 100;
140 int get_temp_common(struct tsens_priv *priv, int i, int *temp)
142 struct tsens_sensor *s = &priv->sensor[i];
143 int last_temp = 0, ret;
145 ret = regmap_field_read(priv->rf[LAST_TEMP_0 + s->hw_id], &last_temp);
149 *temp = code_to_degc(last_temp, s) * 1000;
154 static const struct regmap_config tsens_config = {
161 static const struct regmap_config tsens_srot_config = {
168 int __init init_common(struct tsens_priv *priv)
170 void __iomem *tm_base, *srot_base;
171 struct device *dev = priv->dev;
172 struct resource *res;
175 struct platform_device *op = of_find_device_by_node(priv->dev->of_node);
180 if (op->num_resources > 1) {
181 /* DT with separate SROT and TM address space */
183 res = platform_get_resource(op, IORESOURCE_MEM, 1);
184 srot_base = devm_ioremap_resource(&op->dev, res);
185 if (IS_ERR(srot_base)) {
186 ret = PTR_ERR(srot_base);
190 priv->srot_map = devm_regmap_init_mmio(dev, srot_base,
192 if (IS_ERR(priv->srot_map)) {
193 ret = PTR_ERR(priv->srot_map);
197 /* old DTs where SROT and TM were in a contiguous 2K block */
198 priv->tm_offset = 0x1000;
201 res = platform_get_resource(op, IORESOURCE_MEM, 0);
202 tm_base = devm_ioremap_resource(&op->dev, res);
203 if (IS_ERR(tm_base)) {
204 ret = PTR_ERR(tm_base);
208 priv->tm_map = devm_regmap_init_mmio(dev, tm_base, &tsens_config);
209 if (IS_ERR(priv->tm_map)) {
210 ret = PTR_ERR(priv->tm_map);
214 priv->rf[TSENS_EN] = devm_regmap_field_alloc(dev, priv->srot_map,
215 priv->fields[TSENS_EN]);
216 if (IS_ERR(priv->rf[TSENS_EN])) {
217 ret = PTR_ERR(priv->rf[TSENS_EN]);
220 ret = regmap_field_read(priv->rf[TSENS_EN], &enabled);
224 dev_err(dev, "tsens device is not enabled\n");
229 priv->rf[SENSOR_EN] = devm_regmap_field_alloc(dev, priv->srot_map,
230 priv->fields[SENSOR_EN]);
231 if (IS_ERR(priv->rf[SENSOR_EN])) {
232 ret = PTR_ERR(priv->rf[SENSOR_EN]);
235 /* now alloc regmap_fields in tm_map */
236 for (i = 0, j = LAST_TEMP_0; i < priv->feat->max_sensors; i++, j++) {
237 priv->rf[j] = devm_regmap_field_alloc(dev, priv->tm_map,
239 if (IS_ERR(priv->rf[j])) {
240 ret = PTR_ERR(priv->rf[j]);
244 for (i = 0, j = VALID_0; i < priv->feat->max_sensors; i++, j++) {
245 priv->rf[j] = devm_regmap_field_alloc(dev, priv->tm_map,
247 if (IS_ERR(priv->rf[j])) {
248 ret = PTR_ERR(priv->rf[j]);
256 put_device(&op->dev);