drivers: thermal: tsens: Get rid of id field in tsens_sensor
[linux-2.6-microblaze.git] / drivers / thermal / qcom / tsens-common.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4  */
5
6 #include <linux/err.h>
7 #include <linux/io.h>
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>
13 #include "tsens.h"
14
15 char *qfprom_read(struct device *dev, const char *cname)
16 {
17         struct nvmem_cell *cell;
18         ssize_t data;
19         char *ret;
20
21         cell = nvmem_cell_get(dev, cname);
22         if (IS_ERR(cell))
23                 return ERR_CAST(cell);
24
25         ret = nvmem_cell_read(cell, &data);
26         nvmem_cell_put(cell);
27
28         return ret;
29 }
30
31 /*
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
35  * resp.
36  */
37 void compute_intercept_slope(struct tsens_priv *priv, u32 *p1,
38                              u32 *p2, u32 mode)
39 {
40         int i;
41         int num, den;
42
43         for (i = 0; i < priv->num_sensors; i++) {
44                 dev_dbg(priv->dev,
45                         "sensor%d - data_point1:%#x data_point2:%#x\n",
46                         i, p1[i], p2[i]);
47
48                 priv->sensor[i].slope = SLOPE_DEFAULT;
49                 if (mode == TWO_PT_CALIB) {
50                         /*
51                          * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
52                          *      temp_120_degc - temp_30_degc (x2 - x1)
53                          */
54                         num = p2[i] - p1[i];
55                         num *= SLOPE_FACTOR;
56                         den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
57                         priv->sensor[i].slope = num / den;
58                 }
59
60                 priv->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
61                                 (CAL_DEGC_PT1 *
62                                 priv->sensor[i].slope);
63                 dev_dbg(priv->dev, "offset:%d\n", priv->sensor[i].offset);
64         }
65 }
66
67 static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
68 {
69         int degc, num, den;
70
71         num = (adc_code * SLOPE_FACTOR) - s->offset;
72         den = s->slope;
73
74         if (num > 0)
75                 degc = num + (den / 2);
76         else if (num < 0)
77                 degc = num - (den / 2);
78         else
79                 degc = num;
80
81         degc /= den;
82
83         return degc;
84 }
85
86 int get_temp_tsens_valid(struct tsens_sensor *s, int *temp)
87 {
88         struct tsens_priv *priv = s->priv;
89         int hw_id = s->hw_id;
90         u32 temp_idx = LAST_TEMP_0 + hw_id;
91         u32 valid_idx = VALID_0 + hw_id;
92         u32 last_temp = 0, valid, mask;
93         int ret;
94
95         ret = regmap_field_read(priv->rf[valid_idx], &valid);
96         if (ret)
97                 return ret;
98         while (!valid) {
99                 /* Valid bit is 0 for 6 AHB clock cycles.
100                  * At 19.2MHz, 1 AHB clock is ~60ns.
101                  * We should enter this loop very, very rarely.
102                  */
103                 ndelay(400);
104                 ret = regmap_field_read(priv->rf[valid_idx], &valid);
105                 if (ret)
106                         return ret;
107         }
108
109         /* Valid bit is set, OK to read the temperature */
110         ret = regmap_field_read(priv->rf[temp_idx], &last_temp);
111         if (ret)
112                 return ret;
113
114         if (priv->feat->adc) {
115                 /* Convert temperature from ADC code to milliCelsius */
116                 *temp = code_to_degc(last_temp, s) * 1000;
117         } else {
118                 mask = GENMASK(priv->fields[LAST_TEMP_0].msb,
119                                priv->fields[LAST_TEMP_0].lsb);
120                 /* Convert temperature from deciCelsius to milliCelsius */
121                 *temp = sign_extend32(last_temp, fls(mask) - 1) * 100;
122         }
123
124         return 0;
125 }
126
127 int get_temp_common(struct tsens_sensor *s, int *temp)
128 {
129         struct tsens_priv *priv = s->priv;
130         int hw_id = s->hw_id;
131         int last_temp = 0, ret;
132
133         ret = regmap_field_read(priv->rf[LAST_TEMP_0 + hw_id], &last_temp);
134         if (ret)
135                 return ret;
136
137         *temp = code_to_degc(last_temp, s) * 1000;
138
139         return 0;
140 }
141
142 static const struct regmap_config tsens_config = {
143         .name           = "tm",
144         .reg_bits       = 32,
145         .val_bits       = 32,
146         .reg_stride     = 4,
147 };
148
149 static const struct regmap_config tsens_srot_config = {
150         .name           = "srot",
151         .reg_bits       = 32,
152         .val_bits       = 32,
153         .reg_stride     = 4,
154 };
155
156 int __init init_common(struct tsens_priv *priv)
157 {
158         void __iomem *tm_base, *srot_base;
159         struct device *dev = priv->dev;
160         struct resource *res;
161         u32 enabled;
162         int ret, i, j;
163         struct platform_device *op = of_find_device_by_node(priv->dev->of_node);
164
165         if (!op)
166                 return -EINVAL;
167
168         if (op->num_resources > 1) {
169                 /* DT with separate SROT and TM address space */
170                 priv->tm_offset = 0;
171                 res = platform_get_resource(op, IORESOURCE_MEM, 1);
172                 srot_base = devm_ioremap_resource(&op->dev, res);
173                 if (IS_ERR(srot_base)) {
174                         ret = PTR_ERR(srot_base);
175                         goto err_put_device;
176                 }
177
178                 priv->srot_map = devm_regmap_init_mmio(dev, srot_base,
179                                                         &tsens_srot_config);
180                 if (IS_ERR(priv->srot_map)) {
181                         ret = PTR_ERR(priv->srot_map);
182                         goto err_put_device;
183                 }
184         } else {
185                 /* old DTs where SROT and TM were in a contiguous 2K block */
186                 priv->tm_offset = 0x1000;
187         }
188
189         res = platform_get_resource(op, IORESOURCE_MEM, 0);
190         tm_base = devm_ioremap_resource(&op->dev, res);
191         if (IS_ERR(tm_base)) {
192                 ret = PTR_ERR(tm_base);
193                 goto err_put_device;
194         }
195
196         priv->tm_map = devm_regmap_init_mmio(dev, tm_base, &tsens_config);
197         if (IS_ERR(priv->tm_map)) {
198                 ret = PTR_ERR(priv->tm_map);
199                 goto err_put_device;
200         }
201
202         priv->rf[TSENS_EN] = devm_regmap_field_alloc(dev, priv->srot_map,
203                                                      priv->fields[TSENS_EN]);
204         if (IS_ERR(priv->rf[TSENS_EN])) {
205                 ret = PTR_ERR(priv->rf[TSENS_EN]);
206                 goto err_put_device;
207         }
208         ret = regmap_field_read(priv->rf[TSENS_EN], &enabled);
209         if (ret)
210                 goto err_put_device;
211         if (!enabled) {
212                 dev_err(dev, "tsens device is not enabled\n");
213                 ret = -ENODEV;
214                 goto err_put_device;
215         }
216
217         priv->rf[SENSOR_EN] = devm_regmap_field_alloc(dev, priv->srot_map,
218                                                       priv->fields[SENSOR_EN]);
219         if (IS_ERR(priv->rf[SENSOR_EN])) {
220                 ret = PTR_ERR(priv->rf[SENSOR_EN]);
221                 goto err_put_device;
222         }
223         /* now alloc regmap_fields in tm_map */
224         for (i = 0, j = LAST_TEMP_0; i < priv->feat->max_sensors; i++, j++) {
225                 priv->rf[j] = devm_regmap_field_alloc(dev, priv->tm_map,
226                                                       priv->fields[j]);
227                 if (IS_ERR(priv->rf[j])) {
228                         ret = PTR_ERR(priv->rf[j]);
229                         goto err_put_device;
230                 }
231         }
232         for (i = 0, j = VALID_0; i < priv->feat->max_sensors; i++, j++) {
233                 priv->rf[j] = devm_regmap_field_alloc(dev, priv->tm_map,
234                                                       priv->fields[j]);
235                 if (IS_ERR(priv->rf[j])) {
236                         ret = PTR_ERR(priv->rf[j]);
237                         goto err_put_device;
238                 }
239         }
240
241         return 0;
242
243 err_put_device:
244         put_device(&op->dev);
245         return ret;
246 }