Amit Kucheria | 2d71d8d | 2018-09-12 15:22:47 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/err.h> |
| 7 | #include <linux/io.h> |
| 8 | #include <linux/nvmem-consumer.h> |
| 9 | #include <linux/of_address.h> |
Amit Kucheria | 5b12839 | 2018-07-18 12:13:09 +0530 | [diff] [blame] | 10 | #include <linux/of_platform.h> |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/regmap.h> |
| 13 | #include "tsens.h" |
| 14 | |
Amit Kucheria | c8c3b09 | 2018-09-12 15:22:53 +0530 | [diff] [blame] | 15 | /* SROT */ |
| 16 | #define TSENS_EN BIT(0) |
| 17 | |
| 18 | /* TM */ |
Amit Kucheria | bd7557f | 2018-09-12 15:22:46 +0530 | [diff] [blame] | 19 | #define STATUS_OFFSET 0x30 |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 20 | #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 | |
| 27 | char *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 | */ |
| 49 | void 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 | |
| 79 | static 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 | |
| 98 | int get_temp_common(struct tsens_device *tmdev, int id, int *temp) |
| 99 | { |
| 100 | struct tsens_sensor *s = &tmdev->sensor[id]; |
| 101 | u32 code; |
Amit Kucheria | e0fe014 | 2018-07-26 16:03:08 +0530 | [diff] [blame] | 102 | unsigned int status_reg; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 103 | int last_temp = 0, ret; |
| 104 | |
Amit Kucheria | bd7557f | 2018-09-12 15:22:46 +0530 | [diff] [blame] | 105 | status_reg = tmdev->tm_offset + STATUS_OFFSET + s->hw_id * SN_ADDR_OFFSET; |
Amit Kucheria | 67b0f5e | 2018-09-12 15:22:49 +0530 | [diff] [blame] | 106 | ret = regmap_read(tmdev->tm_map, status_reg, &code); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 107 | 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 | |
| 116 | static const struct regmap_config tsens_config = { |
Srinivas Kandagatla | 4ab248b | 2018-11-15 17:43:30 +0000 | [diff] [blame] | 117 | .name = "tm", |
| 118 | .reg_bits = 32, |
| 119 | .val_bits = 32, |
| 120 | .reg_stride = 4, |
| 121 | }; |
| 122 | |
| 123 | static const struct regmap_config tsens_srot_config = { |
| 124 | .name = "srot", |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 125 | .reg_bits = 32, |
| 126 | .val_bits = 32, |
| 127 | .reg_stride = 4, |
| 128 | }; |
| 129 | |
| 130 | int __init init_common(struct tsens_device *tmdev) |
| 131 | { |
Amit Kucheria | a15525b | 2018-09-12 15:22:50 +0530 | [diff] [blame] | 132 | void __iomem *tm_base, *srot_base; |
Amit Kucheria | faa590b | 2018-07-26 16:03:09 +0530 | [diff] [blame] | 133 | struct resource *res; |
Amit Kucheria | c8c3b09 | 2018-09-12 15:22:53 +0530 | [diff] [blame] | 134 | u32 code; |
| 135 | int ret; |
Amit Kucheria | 5b12839 | 2018-07-18 12:13:09 +0530 | [diff] [blame] | 136 | struct platform_device *op = of_find_device_by_node(tmdev->dev->of_node); |
Amit Kucheria | c8c3b09 | 2018-09-12 15:22:53 +0530 | [diff] [blame] | 137 | u16 ctrl_offset = tmdev->reg_offsets[SROT_CTRL_OFFSET]; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 138 | |
Amit Kucheria | 5b12839 | 2018-07-18 12:13:09 +0530 | [diff] [blame] | 139 | if (!op) |
| 140 | return -EINVAL; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 141 | |
Amit Kucheria | 5b12839 | 2018-07-18 12:13:09 +0530 | [diff] [blame] | 142 | if (op->num_resources > 1) { |
Amit Kucheria | a15525b | 2018-09-12 15:22:50 +0530 | [diff] [blame] | 143 | /* DT with separate SROT and TM address space */ |
Amit Kucheria | 5b12839 | 2018-07-18 12:13:09 +0530 | [diff] [blame] | 144 | tmdev->tm_offset = 0; |
Amit Kucheria | a15525b | 2018-09-12 15:22:50 +0530 | [diff] [blame] | 145 | res = platform_get_resource(op, IORESOURCE_MEM, 1); |
| 146 | srot_base = devm_ioremap_resource(&op->dev, res); |
Peng Hao | a245b62 | 2019-02-13 22:53:29 +0800 | [diff] [blame] | 147 | if (IS_ERR(srot_base)) { |
| 148 | ret = PTR_ERR(srot_base); |
| 149 | goto err_put_device; |
| 150 | } |
Amit Kucheria | a15525b | 2018-09-12 15:22:50 +0530 | [diff] [blame] | 151 | |
Srinivas Kandagatla | 4ab248b | 2018-11-15 17:43:30 +0000 | [diff] [blame] | 152 | tmdev->srot_map = devm_regmap_init_mmio(tmdev->dev, srot_base, |
| 153 | &tsens_srot_config); |
Peng Hao | a245b62 | 2019-02-13 22:53:29 +0800 | [diff] [blame] | 154 | if (IS_ERR(tmdev->srot_map)) { |
| 155 | ret = PTR_ERR(tmdev->srot_map); |
| 156 | goto err_put_device; |
| 157 | } |
Amit Kucheria | a15525b | 2018-09-12 15:22:50 +0530 | [diff] [blame] | 158 | |
Amit Kucheria | 5b12839 | 2018-07-18 12:13:09 +0530 | [diff] [blame] | 159 | } else { |
| 160 | /* old DTs where SROT and TM were in a contiguous 2K block */ |
| 161 | tmdev->tm_offset = 0x1000; |
| 162 | } |
| 163 | |
Amit Kucheria | faa590b | 2018-07-26 16:03:09 +0530 | [diff] [blame] | 164 | res = platform_get_resource(op, IORESOURCE_MEM, 0); |
Amit Kucheria | 67b0f5e | 2018-09-12 15:22:49 +0530 | [diff] [blame] | 165 | tm_base = devm_ioremap_resource(&op->dev, res); |
Peng Hao | a245b62 | 2019-02-13 22:53:29 +0800 | [diff] [blame] | 166 | if (IS_ERR(tm_base)) { |
| 167 | ret = PTR_ERR(tm_base); |
| 168 | goto err_put_device; |
| 169 | } |
Amit Kucheria | faa590b | 2018-07-26 16:03:09 +0530 | [diff] [blame] | 170 | |
Amit Kucheria | 67b0f5e | 2018-09-12 15:22:49 +0530 | [diff] [blame] | 171 | tmdev->tm_map = devm_regmap_init_mmio(tmdev->dev, tm_base, &tsens_config); |
Peng Hao | a245b62 | 2019-02-13 22:53:29 +0800 | [diff] [blame] | 172 | if (IS_ERR(tmdev->tm_map)) { |
| 173 | ret = PTR_ERR(tmdev->tm_map); |
| 174 | goto err_put_device; |
| 175 | } |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 176 | |
Amit Kucheria | c8c3b09 | 2018-09-12 15:22:53 +0530 | [diff] [blame] | 177 | if (tmdev->srot_map) { |
| 178 | ret = regmap_read(tmdev->srot_map, ctrl_offset, &code); |
| 179 | if (ret) |
Peng Hao | a245b62 | 2019-02-13 22:53:29 +0800 | [diff] [blame] | 180 | goto err_put_device; |
Amit Kucheria | c8c3b09 | 2018-09-12 15:22:53 +0530 | [diff] [blame] | 181 | if (!(code & TSENS_EN)) { |
| 182 | dev_err(tmdev->dev, "tsens device is not enabled\n"); |
Peng Hao | a245b62 | 2019-02-13 22:53:29 +0800 | [diff] [blame] | 183 | ret = -ENODEV; |
| 184 | goto err_put_device; |
Amit Kucheria | c8c3b09 | 2018-09-12 15:22:53 +0530 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 188 | return 0; |
Peng Hao | a245b62 | 2019-02-13 22:53:29 +0800 | [diff] [blame] | 189 | |
| 190 | err_put_device: |
| 191 | put_device(&op->dev); |
| 192 | return ret; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 193 | } |