Skip to content

Commit a2dd2bb

Browse files
Satya Priya Kakitapallijprakash-qc
authored andcommitted
FROMLIST: thermal: qcom: Add support for Qualcomm MBG thermal monitoring
Add driver for the Qualcomm MBG thermal monitoring device. It monitors the die temperature, and when there is a level 1 upper threshold violation, it receives an interrupt over spmi. The driver reads the fault status register and notifies thermal accordingly. Link: https://lore.kernel.org/all/20260601-spmi-mbg-driver-v1-2-b4892b55a17f@oss.qualcomm.com/ Signed-off-by: Satya Priya Kakitapalli <quic_skakitap@quicinc.com> Co-developed-by: Sachin Gupta <sachin.gupta@oss.qualcomm.com> Signed-off-by: Sachin Gupta <sachin.gupta@oss.qualcomm.com>
1 parent cbe0e1d commit a2dd2bb

3 files changed

Lines changed: 266 additions & 0 deletions

File tree

drivers/thermal/qcom/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ config QCOM_SPMI_ADC_TM5_GEN3
3030
Thermal client sets threshold temperature for both warm and cool and
3131
gets updated when a threshold is reached.
3232

33+
config QCOM_SPMI_MBG_TM
34+
tristate "Qualcomm SPMI PMIC MBG Temperature monitor"
35+
depends on QCOM_SPMI_ADC5_GEN3
36+
select REGMAP_SPMI
37+
help
38+
This enables a thermal driver for the MBG thermal monitoring device.
39+
It shows up in sysfs as a thermal sensor with single trip point.
40+
It notifies the thermal framework when this trip is violated. The
41+
temperature reported by the thermal sensor reflects the real
42+
time die temperature through ADC channel.
43+
3344
config QCOM_SPMI_TEMP_ALARM
3445
tristate "Qualcomm SPMI PMIC Temperature Alarm"
3546
depends on OF && SPMI && IIO

drivers/thermal/qcom/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ qcom_tsens-y += tsens.o tsens-v2.o tsens-v1.o tsens-v0_1.o \
55
tsens-8960.o
66
obj-$(CONFIG_QCOM_SPMI_ADC_TM5) += qcom-spmi-adc-tm5.o
77
obj-$(CONFIG_QCOM_SPMI_ADC_TM5_GEN3) += qcom-spmi-adc-tm5-gen3.o
8+
obj-$(CONFIG_QCOM_SPMI_MBG_TM) += qcom-spmi-mbg-tm.o
89
obj-$(CONFIG_QCOM_SPMI_TEMP_ALARM) += qcom-spmi-temp-alarm.o
910
obj-$(CONFIG_QCOM_LMH) += lmh.o
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
*/
5+
6+
#include <linux/interrupt.h>
7+
#include <linux/irq.h>
8+
#include <linux/module.h>
9+
#include <linux/of.h>
10+
#include <linux/platform_device.h>
11+
#include <linux/regmap.h>
12+
#include <linux/thermal.h>
13+
#include <linux/iio/consumer.h>
14+
15+
#define MBG_TEMP_MON2_FAULT_STATUS 0x50
16+
17+
#define MON_FAULT_STATUS_MASK GENMASK(7, 4)
18+
#define MON_FAULT_LVL1_UPR 0x5
19+
20+
#define MON2_LVL1_UP_THRESH 0x59
21+
22+
#define MBG_TEMP_MON2_MISC_CFG 0x5f
23+
#define MON2_UP_THRESH_EN BIT(1)
24+
25+
#define MBG_TEMP_STEP_MV 8
26+
#define MBG_TEMP_DEFAULT_TEMP_MV 600
27+
#define MBG_TEMP_CONSTANT 1000
28+
#define MBG_MIN_TRIP_TEMP 25000
29+
#define MBG_MAX_SUPPORTED_TEMP 160000
30+
31+
/**
32+
* struct mbg_tm_chip - MBG thermal monitor device data.
33+
* @map: regmap for accessing MBG thermal registers.
34+
* @dev: mbg_tm_chip device.
35+
* @tz_dev: thermal zone device registered with the thermal framework.
36+
* @lock: mbg_tm_chip lock for set trip temperature.
37+
* @base: base register offset for this MBG instance
38+
* @irq: interrupt line used to signal threshold events
39+
* @last_temp: last measured temperature.
40+
* @last_thres_crossed: indicates whether the last interrupt crossed a threshold
41+
* @adc: IIO ADC channel used for temperature sensing
42+
*/
43+
struct mbg_tm_chip {
44+
struct regmap *map;
45+
struct device *dev;
46+
struct thermal_zone_device *tz_dev;
47+
struct mutex lock;
48+
unsigned int base;
49+
int irq;
50+
int last_temp;
51+
bool last_thres_crossed;
52+
struct iio_channel *adc;
53+
};
54+
55+
/**
56+
* struct mbg_map_table - temperature to voltage mapping entry
57+
* @min_temp: minimum temperature supported by this mapping entry
58+
* @vtemp0: reference voltage or ADC code corresponding to the temperature
59+
* @tc: temperature coefficient used for conversion calculations
60+
*/
61+
struct mbg_map_table {
62+
int min_temp;
63+
int vtemp0;
64+
int tc;
65+
};
66+
67+
static const struct mbg_map_table map_table[] = {
68+
/* minT vtemp0 tc */
69+
{ -60000, 4337, 1967 },
70+
{ -40000, 4731, 1964 },
71+
{ -20000, 5124, 1957 },
72+
{ 0, 5515, 1949 },
73+
{ 20000, 5905, 1940 },
74+
{ 40000, 6293, 1930 },
75+
{ 60000, 6679, 1921 },
76+
{ 80000, 7064, 1910 },
77+
{ 100000, 7446, 1896 },
78+
{ 120000, 7825, 1878 },
79+
{ 140000, 8201, 1859 },
80+
};
81+
82+
static int mbg_tm_get_temp(struct thermal_zone_device *tz, int *temp)
83+
{
84+
struct mbg_tm_chip *chip = thermal_zone_device_priv(tz);
85+
int ret, milli_celsius;
86+
87+
if (chip->last_thres_crossed) {
88+
dev_dbg(chip->dev, "last_temp: %d\n", chip->last_temp);
89+
chip->last_thres_crossed = false;
90+
*temp = chip->last_temp;
91+
return 0;
92+
}
93+
94+
ret = iio_read_channel_processed(chip->adc, &milli_celsius);
95+
if (ret < 0) {
96+
dev_err(chip->dev, "Failed to read iio channel with %d\n", ret);
97+
return ret;
98+
}
99+
100+
*temp = milli_celsius;
101+
102+
return 0;
103+
}
104+
105+
static int temp_to_vtemp_mv(int temp)
106+
{
107+
int idx, vtemp, tc = 0, t0 = 0, vtemp0 = 0;
108+
109+
for (idx = 0; idx < ARRAY_SIZE(map_table); idx++)
110+
if (temp >= map_table[idx].min_temp &&
111+
temp < (map_table[idx].min_temp + 20000)) {
112+
tc = map_table[idx].tc;
113+
t0 = map_table[idx].min_temp;
114+
vtemp0 = map_table[idx].vtemp0;
115+
break;
116+
}
117+
118+
/*
119+
* Formula to calculate vtemp(mV) from a given temp
120+
* vtemp = (temp - minT) * tc + vtemp0
121+
* tc, t0 and vtemp0 values are mentioned in the map_table array.
122+
*/
123+
vtemp = ((temp - t0) * tc + vtemp0 * 100000) / 1000000;
124+
125+
/* step size is 8mV */
126+
return abs(vtemp - MBG_TEMP_DEFAULT_TEMP_MV) / MBG_TEMP_STEP_MV;
127+
}
128+
129+
static int mbg_tm_set_trip_temp(struct thermal_zone_device *tz, int low_temp,
130+
int temp)
131+
{
132+
struct mbg_tm_chip *chip = thermal_zone_device_priv(tz);
133+
int ret = 0;
134+
135+
guard(mutex)(&chip->lock);
136+
137+
/* The HW has a limitation that the trip set must be above 25C */
138+
if (temp > MBG_MIN_TRIP_TEMP && temp < MBG_MAX_SUPPORTED_TEMP) {
139+
ret = regmap_set_bits(chip->map, chip->base + MBG_TEMP_MON2_MISC_CFG,
140+
MON2_UP_THRESH_EN);
141+
if (ret < 0)
142+
return ret;
143+
144+
ret = regmap_write(chip->map, chip->base + MON2_LVL1_UP_THRESH,
145+
temp_to_vtemp_mv(temp));
146+
if (ret < 0)
147+
return ret;
148+
} else {
149+
dev_dbg(chip->dev, "Set trip b/w 25C and 160C\n");
150+
ret = regmap_clear_bits(chip->map, chip->base + MBG_TEMP_MON2_MISC_CFG,
151+
MON2_UP_THRESH_EN);
152+
return ret;
153+
}
154+
155+
/*
156+
* Configure the last_temp one degree higher, to ensure the
157+
* violated temp is returned to thermal framework when it reads
158+
* temperature for the first time after the violation happens.
159+
* This is needed to account for the inaccuracy in the conversion
160+
* formula used which leads to the thermal framework setting back
161+
* the same thresholds in case the temperature it reads does not
162+
* show violation.
163+
*/
164+
chip->last_temp = temp + MBG_TEMP_CONSTANT;
165+
166+
return ret;
167+
}
168+
169+
static const struct thermal_zone_device_ops mbg_tm_ops = {
170+
.get_temp = mbg_tm_get_temp,
171+
.set_trips = mbg_tm_set_trip_temp,
172+
};
173+
174+
static irqreturn_t mbg_tm_isr(int irq, void *data)
175+
{
176+
struct mbg_tm_chip *chip = data;
177+
int ret, val;
178+
179+
scoped_guard(mutex, &chip->lock) {
180+
ret = regmap_read(chip->map, chip->base + MBG_TEMP_MON2_FAULT_STATUS, &val);
181+
if (ret < 0)
182+
return IRQ_HANDLED;
183+
}
184+
185+
if (FIELD_GET(MON_FAULT_STATUS_MASK, val) & MON_FAULT_LVL1_UPR) {
186+
chip->last_thres_crossed = true;
187+
dev_dbg(chip->dev, "Notifying Thermal, fault status=%d\n", val);
188+
thermal_zone_device_update(chip->tz_dev, THERMAL_TRIP_VIOLATED);
189+
} else {
190+
dev_dbg(chip->dev, "Lvl1 upper threshold not violated, ignoring interrupt\n");
191+
}
192+
193+
return IRQ_HANDLED;
194+
}
195+
196+
static int mbg_tm_probe(struct platform_device *pdev)
197+
{
198+
struct mbg_tm_chip *chip;
199+
struct device_node *node = pdev->dev.of_node;
200+
u32 res;
201+
int ret;
202+
203+
chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
204+
if (!chip)
205+
return -ENOMEM;
206+
207+
chip->dev = &pdev->dev;
208+
209+
mutex_init(&chip->lock);
210+
211+
chip->map = dev_get_regmap(pdev->dev.parent, NULL);
212+
if (!chip->map)
213+
return -ENXIO;
214+
215+
ret = device_property_read_u32(chip->dev, "reg", &res);
216+
if (ret < 0)
217+
return dev_err_probe(chip->dev, ret, "Couldn't read reg property\n");
218+
219+
chip->base = res;
220+
221+
chip->irq = platform_get_irq(pdev, 0);
222+
if (chip->irq < 0)
223+
return dev_err_probe(chip->dev, chip->irq, "Failed to get irq\n");
224+
225+
chip->adc = devm_iio_channel_get(&pdev->dev, "thermal");
226+
if (IS_ERR(chip->adc))
227+
return dev_err_probe(chip->dev, PTR_ERR(chip->adc), "Failed to get adc channel\n");
228+
229+
chip->tz_dev = devm_thermal_of_zone_register(chip->dev, 0, chip, &mbg_tm_ops);
230+
if (IS_ERR(chip->tz_dev))
231+
return dev_err_probe(chip->dev, PTR_ERR(chip->tz_dev),
232+
"Failed to register sensor\n");
233+
234+
return devm_request_threaded_irq(&pdev->dev, chip->irq, NULL, mbg_tm_isr, IRQF_ONESHOT,
235+
node->name, chip);
236+
}
237+
238+
static const struct of_device_id mbg_tm_match_table[] = {
239+
{ .compatible = "qcom,pm8775-mbg-tm" },
240+
{ }
241+
};
242+
MODULE_DEVICE_TABLE(of, mbg_tm_match_table);
243+
244+
static struct platform_driver mbg_tm_driver = {
245+
.driver = {
246+
.name = "qcom-spmi-mbg-tm",
247+
.of_match_table = mbg_tm_match_table,
248+
},
249+
.probe = mbg_tm_probe,
250+
};
251+
module_platform_driver(mbg_tm_driver);
252+
253+
MODULE_DESCRIPTION("PMIC MBG Temperature monitor driver");
254+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)