ipq806x: add thermal sensor driver
[openwrt/openwrt.git] / target / linux / ipq806x / patches-4.4 / 015-2-thermal-qcom-tsens-8916-Add-support-for-8916-family-of-SoCs.patch
1 From 840a5bd3ed3fdd62456d4d26c3128ec10496555b Mon Sep 17 00:00:00 2001
2 From: Rajendra Nayak <rnayak@codeaurora.org>
3 Date: Thu, 5 May 2016 14:21:40 +0530
4 Subject: thermal: qcom: tsens-8916: Add support for 8916 family of SoCs
5
6 Add support to calibrate sensors on 8916 family and also add common
7 functions to read temperature from sensors (This can be reused on
8 other SoCs having similar TSENS device)
9 The calibration data is read from eeprom using the generic nvmem
10 framework apis.
11
12 Based on the original code by Siddartha Mohanadoss and Stephen Boyd.
13
14 Signed-off-by: Rajendra Nayak <rnayak@codeaurora.org>
15 Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
16 Signed-off-by: Zhang Rui <rui.zhang@intel.com>
17 ---
18 drivers/thermal/qcom/Makefile | 2 +-
19 drivers/thermal/qcom/tsens-8916.c | 113 ++++++++++++++++++++++++++++++++++++++
20 drivers/thermal/qcom/tsens.c | 1 +
21 drivers/thermal/qcom/tsens.h | 2 +
22 4 files changed, 117 insertions(+), 1 deletion(-)
23 create mode 100644 drivers/thermal/qcom/tsens-8916.c
24
25 --- a/drivers/thermal/qcom/Makefile
26 +++ b/drivers/thermal/qcom/Makefile
27 @@ -1,2 +1,2 @@
28 obj-$(CONFIG_QCOM_TSENS) += qcom_tsens.o
29 -qcom_tsens-y += tsens.o tsens-common.o
30 +qcom_tsens-y += tsens.o tsens-common.o tsens-8916.o
31 --- /dev/null
32 +++ b/drivers/thermal/qcom/tsens-8916.c
33 @@ -0,0 +1,113 @@
34 +/*
35 + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
36 + *
37 + * This program is free software; you can redistribute it and/or modify
38 + * it under the terms of the GNU General Public License version 2 and
39 + * only version 2 as published by the Free Software Foundation.
40 + *
41 + * This program is distributed in the hope that it will be useful,
42 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 + * GNU General Public License for more details.
45 + *
46 + */
47 +
48 +#include <linux/platform_device.h>
49 +#include "tsens.h"
50 +
51 +/* eeprom layout data for 8916 */
52 +#define BASE0_MASK 0x0000007f
53 +#define BASE1_MASK 0xfe000000
54 +#define BASE0_SHIFT 0
55 +#define BASE1_SHIFT 25
56 +
57 +#define S0_P1_MASK 0x00000f80
58 +#define S1_P1_MASK 0x003e0000
59 +#define S2_P1_MASK 0xf8000000
60 +#define S3_P1_MASK 0x000003e0
61 +#define S4_P1_MASK 0x000f8000
62 +
63 +#define S0_P2_MASK 0x0001f000
64 +#define S1_P2_MASK 0x07c00000
65 +#define S2_P2_MASK 0x0000001f
66 +#define S3_P2_MASK 0x00007c00
67 +#define S4_P2_MASK 0x01f00000
68 +
69 +#define S0_P1_SHIFT 7
70 +#define S1_P1_SHIFT 17
71 +#define S2_P1_SHIFT 27
72 +#define S3_P1_SHIFT 5
73 +#define S4_P1_SHIFT 15
74 +
75 +#define S0_P2_SHIFT 12
76 +#define S1_P2_SHIFT 22
77 +#define S2_P2_SHIFT 0
78 +#define S3_P2_SHIFT 10
79 +#define S4_P2_SHIFT 20
80 +
81 +#define CAL_SEL_MASK 0xe0000000
82 +#define CAL_SEL_SHIFT 29
83 +
84 +static int calibrate_8916(struct tsens_device *tmdev)
85 +{
86 + int base0 = 0, base1 = 0, i;
87 + u32 p1[5], p2[5];
88 + int mode = 0;
89 + u32 *qfprom_cdata, *qfprom_csel;
90 +
91 + qfprom_cdata = (u32 *)qfprom_read(tmdev->dev, "calib");
92 + if (IS_ERR(qfprom_cdata))
93 + return PTR_ERR(qfprom_cdata);
94 +
95 + qfprom_csel = (u32 *)qfprom_read(tmdev->dev, "calib_sel");
96 + if (IS_ERR(qfprom_csel))
97 + return PTR_ERR(qfprom_csel);
98 +
99 + mode = (qfprom_csel[0] & CAL_SEL_MASK) >> CAL_SEL_SHIFT;
100 + dev_dbg(tmdev->dev, "calibration mode is %d\n", mode);
101 +
102 + switch (mode) {
103 + case TWO_PT_CALIB:
104 + base1 = (qfprom_cdata[1] & BASE1_MASK) >> BASE1_SHIFT;
105 + p2[0] = (qfprom_cdata[0] & S0_P2_MASK) >> S0_P2_SHIFT;
106 + p2[1] = (qfprom_cdata[0] & S1_P2_MASK) >> S1_P2_SHIFT;
107 + p2[2] = (qfprom_cdata[1] & S2_P2_MASK) >> S2_P2_SHIFT;
108 + p2[3] = (qfprom_cdata[1] & S3_P2_MASK) >> S3_P2_SHIFT;
109 + p2[4] = (qfprom_cdata[1] & S4_P2_MASK) >> S4_P2_SHIFT;
110 + for (i = 0; i < tmdev->num_sensors; i++)
111 + p2[i] = ((base1 + p2[i]) << 3);
112 + /* Fall through */
113 + case ONE_PT_CALIB2:
114 + base0 = (qfprom_cdata[0] & BASE0_MASK);
115 + p1[0] = (qfprom_cdata[0] & S0_P1_MASK) >> S0_P1_SHIFT;
116 + p1[1] = (qfprom_cdata[0] & S1_P1_MASK) >> S1_P1_SHIFT;
117 + p1[2] = (qfprom_cdata[0] & S2_P1_MASK) >> S2_P1_SHIFT;
118 + p1[3] = (qfprom_cdata[1] & S3_P1_MASK) >> S3_P1_SHIFT;
119 + p1[4] = (qfprom_cdata[1] & S4_P1_MASK) >> S4_P1_SHIFT;
120 + for (i = 0; i < tmdev->num_sensors; i++)
121 + p1[i] = (((base0) + p1[i]) << 3);
122 + break;
123 + default:
124 + for (i = 0; i < tmdev->num_sensors; i++) {
125 + p1[i] = 500;
126 + p2[i] = 780;
127 + }
128 + break;
129 + }
130 +
131 + compute_intercept_slope(tmdev, p1, p2, mode);
132 +
133 + return 0;
134 +}
135 +
136 +const struct tsens_ops ops_8916 = {
137 + .init = init_common,
138 + .calibrate = calibrate_8916,
139 + .get_temp = get_temp_common,
140 +};
141 +
142 +const struct tsens_data data_8916 = {
143 + .num_sensors = 5,
144 + .ops = &ops_8916,
145 + .hw_ids = (unsigned int []){0, 1, 2, 4, 5 },
146 +};
147 --- a/drivers/thermal/qcom/tsens.c
148 +++ b/drivers/thermal/qcom/tsens.c
149 @@ -65,6 +65,7 @@ static SIMPLE_DEV_PM_OPS(tsens_pm_ops, t
150 static const struct of_device_id tsens_table[] = {
151 {
152 .compatible = "qcom,msm8916-tsens",
153 + .data = &data_8916,
154 }, {
155 .compatible = "qcom,msm8974-tsens",
156 },
157 --- a/drivers/thermal/qcom/tsens.h
158 +++ b/drivers/thermal/qcom/tsens.h
159 @@ -87,4 +87,6 @@ void compute_intercept_slope(struct tsen
160 int init_common(struct tsens_device *);
161 int get_temp_common(struct tsens_device *, int, int *);
162
163 +extern const struct tsens_data data_8916;
164 +
165 #endif /* __QCOM_TSENS_H__ */