virtio_ring: Make interrupt suppression spec compliant
[cascardo/linux.git] / drivers / thermal / qcom / tsens-common.c
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
15 #include <linux/err.h>
16 #include <linux/io.h>
17 #include <linux/nvmem-consumer.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include "tsens.h"
22
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
30
31 char *qfprom_read(struct device *dev, const char *cname)
32 {
33         struct nvmem_cell *cell;
34         ssize_t data;
35         char *ret;
36
37         cell = nvmem_cell_get(dev, cname);
38         if (IS_ERR(cell))
39                 return ERR_CAST(cell);
40
41         ret = nvmem_cell_read(cell, &data);
42         nvmem_cell_put(cell);
43
44         return ret;
45 }
46
47 /*
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
51  * resp.
52  */
53 void compute_intercept_slope(struct tsens_device *tmdev, u32 *p1,
54                              u32 *p2, u32 mode)
55 {
56         int i;
57         int num, den;
58
59         for (i = 0; i < tmdev->num_sensors; i++) {
60                 dev_dbg(tmdev->dev,
61                         "sensor%d - data_point1:%#x data_point2:%#x\n",
62                         i, p1[i], p2[i]);
63
64                 tmdev->sensor[i].slope = SLOPE_DEFAULT;
65                 if (mode == TWO_PT_CALIB) {
66                         /*
67                          * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
68                          *      temp_120_degc - temp_30_degc (x2 - x1)
69                          */
70                         num = p2[i] - p1[i];
71                         num *= SLOPE_FACTOR;
72                         den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
73                         tmdev->sensor[i].slope = num / den;
74                 }
75
76                 tmdev->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
77                                 (CAL_DEGC_PT1 *
78                                 tmdev->sensor[i].slope);
79                 dev_dbg(tmdev->dev, "offset:%d\n", tmdev->sensor[i].offset);
80         }
81 }
82
83 static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
84 {
85         int degc, num, den;
86
87         num = (adc_code * SLOPE_FACTOR) - s->offset;
88         den = s->slope;
89
90         if (num > 0)
91                 degc = num + (den / 2);
92         else if (num < 0)
93                 degc = num - (den / 2);
94         else
95                 degc = num;
96
97         degc /= den;
98
99         return degc;
100 }
101
102 int get_temp_common(struct tsens_device *tmdev, int id, int *temp)
103 {
104         struct tsens_sensor *s = &tmdev->sensor[id];
105         u32 code;
106         unsigned int sensor_addr;
107         int last_temp = 0, ret;
108
109         sensor_addr = S0_ST_ADDR + s->hw_id * SN_ADDR_OFFSET;
110         ret = regmap_read(tmdev->map, sensor_addr, &code);
111         if (ret)
112                 return ret;
113         last_temp = code & SN_ST_TEMP_MASK;
114
115         *temp = code_to_degc(last_temp, s) * 1000;
116
117         return 0;
118 }
119
120 static const struct regmap_config tsens_config = {
121         .reg_bits       = 32,
122         .val_bits       = 32,
123         .reg_stride     = 4,
124 };
125
126 int __init init_common(struct tsens_device *tmdev)
127 {
128         void __iomem *base;
129
130         base = of_iomap(tmdev->dev->of_node, 0);
131         if (!base)
132                 return -EINVAL;
133
134         tmdev->map = devm_regmap_init_mmio(tmdev->dev, base, &tsens_config);
135         if (IS_ERR(tmdev->map)) {
136                 iounmap(base);
137                 return PTR_ERR(tmdev->map);
138         }
139
140         return 0;
141 }