blob: f80c73f117406b587e9b2a5c38bd7e337eca2b9b [file] [log] [blame]
Amit Kucheria2d71d8d2018-09-12 15:22:47 +05301// SPDX-License-Identifier: GPL-2.0
Rajendra Nayak90660732016-05-05 14:21:39 +05302/*
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
Rajendra Nayak90660732016-05-05 14:21:39 +05304 */
5
6#include <linux/err.h>
7#include <linux/io.h>
8#include <linux/nvmem-consumer.h>
9#include <linux/of_address.h>
Amit Kucheria5b128392018-07-18 12:13:09 +053010#include <linux/of_platform.h>
Rajendra Nayak90660732016-05-05 14:21:39 +053011#include <linux/platform_device.h>
12#include <linux/regmap.h>
13#include "tsens.h"
14
Amit Kucheriac8c3b092018-09-12 15:22:53 +053015/* SROT */
16#define TSENS_EN BIT(0)
17
18/* TM */
Amit Kucheriabd7557f2018-09-12 15:22:46 +053019#define STATUS_OFFSET 0x30
Rajendra Nayak90660732016-05-05 14:21:39 +053020#define SN_ADDR_OFFSET 0x4
21#define SN_ST_TEMP_MASK 0x3ff
22#define CAL_DEGC_PT1 30
23#define CAL_DEGC_PT2 120
24#define SLOPE_FACTOR 1000
25#define SLOPE_DEFAULT 3200
26
27char *qfprom_read(struct device *dev, const char *cname)
28{
29 struct nvmem_cell *cell;
30 ssize_t data;
31 char *ret;
32
33 cell = nvmem_cell_get(dev, cname);
34 if (IS_ERR(cell))
35 return ERR_CAST(cell);
36
37 ret = nvmem_cell_read(cell, &data);
38 nvmem_cell_put(cell);
39
40 return ret;
41}
42
43/*
44 * Use this function on devices where slope and offset calculations
45 * depend on calibration data read from qfprom. On others the slope
46 * and offset values are derived from tz->tzp->slope and tz->tzp->offset
47 * resp.
48 */
49void compute_intercept_slope(struct tsens_device *tmdev, u32 *p1,
50 u32 *p2, u32 mode)
51{
52 int i;
53 int num, den;
54
55 for (i = 0; i < tmdev->num_sensors; i++) {
56 dev_dbg(tmdev->dev,
57 "sensor%d - data_point1:%#x data_point2:%#x\n",
58 i, p1[i], p2[i]);
59
60 tmdev->sensor[i].slope = SLOPE_DEFAULT;
61 if (mode == TWO_PT_CALIB) {
62 /*
63 * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
64 * temp_120_degc - temp_30_degc (x2 - x1)
65 */
66 num = p2[i] - p1[i];
67 num *= SLOPE_FACTOR;
68 den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
69 tmdev->sensor[i].slope = num / den;
70 }
71
72 tmdev->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
73 (CAL_DEGC_PT1 *
74 tmdev->sensor[i].slope);
75 dev_dbg(tmdev->dev, "offset:%d\n", tmdev->sensor[i].offset);
76 }
77}
78
79static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
80{
81 int degc, num, den;
82
83 num = (adc_code * SLOPE_FACTOR) - s->offset;
84 den = s->slope;
85
86 if (num > 0)
87 degc = num + (den / 2);
88 else if (num < 0)
89 degc = num - (den / 2);
90 else
91 degc = num;
92
93 degc /= den;
94
95 return degc;
96}
97
98int get_temp_common(struct tsens_device *tmdev, int id, int *temp)
99{
100 struct tsens_sensor *s = &tmdev->sensor[id];
101 u32 code;
Amit Kucheriae0fe0142018-07-26 16:03:08 +0530102 unsigned int status_reg;
Rajendra Nayak90660732016-05-05 14:21:39 +0530103 int last_temp = 0, ret;
104
Amit Kucheriabd7557f2018-09-12 15:22:46 +0530105 status_reg = tmdev->tm_offset + STATUS_OFFSET + s->hw_id * SN_ADDR_OFFSET;
Amit Kucheria67b0f5e2018-09-12 15:22:49 +0530106 ret = regmap_read(tmdev->tm_map, status_reg, &code);
Rajendra Nayak90660732016-05-05 14:21:39 +0530107 if (ret)
108 return ret;
109 last_temp = code & SN_ST_TEMP_MASK;
110
111 *temp = code_to_degc(last_temp, s) * 1000;
112
113 return 0;
114}
115
116static const struct regmap_config tsens_config = {
Srinivas Kandagatla4ab248b2018-11-15 17:43:30 +0000117 .name = "tm",
118 .reg_bits = 32,
119 .val_bits = 32,
120 .reg_stride = 4,
121};
122
123static const struct regmap_config tsens_srot_config = {
124 .name = "srot",
Rajendra Nayak90660732016-05-05 14:21:39 +0530125 .reg_bits = 32,
126 .val_bits = 32,
127 .reg_stride = 4,
128};
129
130int __init init_common(struct tsens_device *tmdev)
131{
Amit Kucheriaa15525b2018-09-12 15:22:50 +0530132 void __iomem *tm_base, *srot_base;
Amit Kucheriafaa590b2018-07-26 16:03:09 +0530133 struct resource *res;
Amit Kucheriac8c3b092018-09-12 15:22:53 +0530134 u32 code;
135 int ret;
Amit Kucheria5b128392018-07-18 12:13:09 +0530136 struct platform_device *op = of_find_device_by_node(tmdev->dev->of_node);
Amit Kucheriac8c3b092018-09-12 15:22:53 +0530137 u16 ctrl_offset = tmdev->reg_offsets[SROT_CTRL_OFFSET];
Rajendra Nayak90660732016-05-05 14:21:39 +0530138
Amit Kucheria5b128392018-07-18 12:13:09 +0530139 if (!op)
140 return -EINVAL;
Rajendra Nayak90660732016-05-05 14:21:39 +0530141
Amit Kucheria5b128392018-07-18 12:13:09 +0530142 if (op->num_resources > 1) {
Amit Kucheriaa15525b2018-09-12 15:22:50 +0530143 /* DT with separate SROT and TM address space */
Amit Kucheria5b128392018-07-18 12:13:09 +0530144 tmdev->tm_offset = 0;
Amit Kucheriaa15525b2018-09-12 15:22:50 +0530145 res = platform_get_resource(op, IORESOURCE_MEM, 1);
146 srot_base = devm_ioremap_resource(&op->dev, res);
Peng Haoa245b622019-02-13 22:53:29 +0800147 if (IS_ERR(srot_base)) {
148 ret = PTR_ERR(srot_base);
149 goto err_put_device;
150 }
Amit Kucheriaa15525b2018-09-12 15:22:50 +0530151
Srinivas Kandagatla4ab248b2018-11-15 17:43:30 +0000152 tmdev->srot_map = devm_regmap_init_mmio(tmdev->dev, srot_base,
153 &tsens_srot_config);
Peng Haoa245b622019-02-13 22:53:29 +0800154 if (IS_ERR(tmdev->srot_map)) {
155 ret = PTR_ERR(tmdev->srot_map);
156 goto err_put_device;
157 }
Amit Kucheriaa15525b2018-09-12 15:22:50 +0530158
Amit Kucheria5b128392018-07-18 12:13:09 +0530159 } else {
160 /* old DTs where SROT and TM were in a contiguous 2K block */
161 tmdev->tm_offset = 0x1000;
162 }
163
Amit Kucheriafaa590b2018-07-26 16:03:09 +0530164 res = platform_get_resource(op, IORESOURCE_MEM, 0);
Amit Kucheria67b0f5e2018-09-12 15:22:49 +0530165 tm_base = devm_ioremap_resource(&op->dev, res);
Peng Haoa245b622019-02-13 22:53:29 +0800166 if (IS_ERR(tm_base)) {
167 ret = PTR_ERR(tm_base);
168 goto err_put_device;
169 }
Amit Kucheriafaa590b2018-07-26 16:03:09 +0530170
Amit Kucheria67b0f5e2018-09-12 15:22:49 +0530171 tmdev->tm_map = devm_regmap_init_mmio(tmdev->dev, tm_base, &tsens_config);
Peng Haoa245b622019-02-13 22:53:29 +0800172 if (IS_ERR(tmdev->tm_map)) {
173 ret = PTR_ERR(tmdev->tm_map);
174 goto err_put_device;
175 }
Rajendra Nayak90660732016-05-05 14:21:39 +0530176
Amit Kucheriac8c3b092018-09-12 15:22:53 +0530177 if (tmdev->srot_map) {
178 ret = regmap_read(tmdev->srot_map, ctrl_offset, &code);
179 if (ret)
Peng Haoa245b622019-02-13 22:53:29 +0800180 goto err_put_device;
Amit Kucheriac8c3b092018-09-12 15:22:53 +0530181 if (!(code & TSENS_EN)) {
182 dev_err(tmdev->dev, "tsens device is not enabled\n");
Peng Haoa245b622019-02-13 22:53:29 +0800183 ret = -ENODEV;
184 goto err_put_device;
Amit Kucheriac8c3b092018-09-12 15:22:53 +0530185 }
186 }
187
Rajendra Nayak90660732016-05-05 14:21:39 +0530188 return 0;
Peng Haoa245b622019-02-13 22:53:29 +0800189
190err_put_device:
191 put_device(&op->dev);
192 return ret;
Rajendra Nayak90660732016-05-05 14:21:39 +0530193}