1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4 */
5 #include <linux/bitfield.h>
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 { -60000, 4337, 1967 },
69 { -40000, 4731, 1964 },
70 { -20000, 5124, 1957 },
71 { 0, 5515, 1949 },
72 { 20000, 5905, 1940 },
73 { 40000, 6293, 1930 },
74 { 60000, 6679, 1921 },
75 { 80000, 7064, 1910 },
76 { 100000, 7446, 1896 },
77 { 120000, 7825, 1878 },
78 { 140000, 8201, 1859 },
79 };
80
mbg_tm_get_temp(struct thermal_zone_device * tz,int * temp)81 static int mbg_tm_get_temp(struct thermal_zone_device *tz, int *temp)
82 {
83 struct mbg_tm_chip *chip = thermal_zone_device_priv(tz);
84 int ret, milli_celsius;
85
86 scoped_guard(mutex, &chip->lock) {
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
95 ret = iio_read_channel_processed(chip->adc, &milli_celsius);
96 if (ret < 0) {
97 dev_err(chip->dev, "Failed to read iio channel with %d\n", ret);
98 return ret;
99 }
100
101 *temp = milli_celsius;
102
103 return 0;
104 }
105
temp_to_vtemp_mv(int temp)106 static int temp_to_vtemp_mv(int temp)
107 {
108 int idx, vtemp, tc = 0, t0 = 0, vtemp0 = 0;
109
110 for (idx = 0; idx < ARRAY_SIZE(map_table); idx++)
111 if (temp >= map_table[idx].min_temp &&
112 temp < (map_table[idx].min_temp + 20000)) {
113 tc = map_table[idx].tc;
114 t0 = map_table[idx].min_temp;
115 vtemp0 = map_table[idx].vtemp0;
116 break;
117 }
118
119 /*
120 * Formula to calculate vtemp(mV) from a given temp
121 * vtemp = (temp - minT) * tc + vtemp0
122 * tc, t0 and vtemp0 values are mentioned in the map_table array.
123 */
124 vtemp = ((temp - t0) * tc + vtemp0 * 100000) / 1000000;
125
126 /* step size is 8mV */
127 return abs(vtemp - MBG_TEMP_DEFAULT_TEMP_MV) / MBG_TEMP_STEP_MV;
128 }
129
mbg_tm_set_trip_temp(struct thermal_zone_device * tz,int low_temp,int temp)130 static int mbg_tm_set_trip_temp(struct thermal_zone_device *tz, int low_temp,
131 int temp)
132 {
133 struct mbg_tm_chip *chip = thermal_zone_device_priv(tz);
134 int ret = 0;
135
136 guard(mutex)(&chip->lock);
137
138 /* The HW has a limitation that the trip set must be above 25C */
139 if (temp > MBG_MIN_TRIP_TEMP && temp < MBG_MAX_SUPPORTED_TEMP) {
140 ret = regmap_write(chip->map, chip->base + MON2_LVL1_UP_THRESH,
141 temp_to_vtemp_mv(temp));
142 if (ret < 0)
143 return ret;
144
145 ret = regmap_set_bits(chip->map, chip->base + MBG_TEMP_MON2_MISC_CFG,
146 MON2_UP_THRESH_EN);
147 if (ret < 0)
148 return ret;
149 } else {
150 dev_err(chip->dev, "Set trip b/w 25C and 160C\n");
151 ret = regmap_clear_bits(chip->map, chip->base + MBG_TEMP_MON2_MISC_CFG,
152 MON2_UP_THRESH_EN);
153 return -ERANGE;
154 }
155
156 /*
157 * Configure the last_temp one degree higher, to ensure the
158 * violated temp is returned to thermal framework when it reads
159 * temperature for the first time after the violation happens.
160 * This is needed to account for the inaccuracy in the conversion
161 * formula used which leads to the thermal framework setting back
162 * the same thresholds in case the temperature it reads does not
163 * show violation.
164 */
165 chip->last_temp = temp + MBG_TEMP_CONSTANT;
166
167 return ret;
168 }
169
170 static const struct thermal_zone_device_ops mbg_tm_ops = {
171 .get_temp = mbg_tm_get_temp,
172 .set_trips = mbg_tm_set_trip_temp,
173 };
174
mbg_tm_isr(int irq,void * data)175 static irqreturn_t mbg_tm_isr(int irq, void *data)
176 {
177 struct mbg_tm_chip *chip = data;
178 int ret, val;
179
180 scoped_guard(mutex, &chip->lock) {
181 ret = regmap_read(chip->map, chip->base + MBG_TEMP_MON2_FAULT_STATUS, &val);
182 if (ret < 0)
183 return IRQ_HANDLED;
184 if (FIELD_GET(MON_FAULT_STATUS_MASK, val) == MON_FAULT_LVL1_UPR)
185 chip->last_thres_crossed = true;
186 }
187
188 if (FIELD_GET(MON_FAULT_STATUS_MASK, val) == MON_FAULT_LVL1_UPR) {
189 dev_dbg(chip->dev, "Notifying Thermal, fault status=%d\n", val);
190 thermal_zone_device_update(chip->tz_dev, THERMAL_TRIP_VIOLATED);
191 } else {
192 dev_dbg(chip->dev, "Lvl1 upper threshold not violated, ignoring interrupt\n");
193 }
194
195 return IRQ_HANDLED;
196 }
197
mbg_tm_probe(struct platform_device * pdev)198 static int mbg_tm_probe(struct platform_device *pdev)
199 {
200 struct mbg_tm_chip *chip;
201 struct device_node *node = pdev->dev.of_node;
202 u32 res;
203 int ret;
204
205 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
206 if (!chip)
207 return -ENOMEM;
208
209 chip->dev = &pdev->dev;
210
211 mutex_init(&chip->lock);
212
213 chip->map = dev_get_regmap(pdev->dev.parent, NULL);
214 if (!chip->map)
215 return -ENXIO;
216
217 ret = device_property_read_u32(chip->dev, "reg", &res);
218 if (ret < 0)
219 return dev_err_probe(chip->dev, ret, "Couldn't read reg property\n");
220
221 chip->base = res;
222
223 chip->irq = platform_get_irq(pdev, 0);
224 if (chip->irq < 0)
225 return dev_err_probe(chip->dev, chip->irq, "Failed to get irq\n");
226
227 chip->adc = devm_iio_channel_get(&pdev->dev, "thermal");
228 if (IS_ERR(chip->adc))
229 return dev_err_probe(chip->dev, PTR_ERR(chip->adc), "Failed to get adc channel\n");
230
231 chip->tz_dev = devm_thermal_of_zone_register(chip->dev, 0, chip, &mbg_tm_ops);
232 if (IS_ERR(chip->tz_dev))
233 return dev_err_probe(chip->dev, PTR_ERR(chip->tz_dev),
234 "Failed to register sensor\n");
235
236 return devm_request_threaded_irq(&pdev->dev, chip->irq, NULL, mbg_tm_isr, IRQF_ONESHOT,
237 node->name, chip);
238 }
239
240 static const struct of_device_id mbg_tm_match_table[] = {
241 { .compatible = "qcom,pm8775-mbg-tm" },
242 { }
243 };
244 MODULE_DEVICE_TABLE(of, mbg_tm_match_table);
245
246 static struct platform_driver mbg_tm_driver = {
247 .driver = {
248 .name = "qcom-spmi-mbg-tm",
249 .of_match_table = mbg_tm_match_table,
250 },
251 .probe = mbg_tm_probe,
252 };
253 module_platform_driver(mbg_tm_driver);
254
255 MODULE_DESCRIPTION("PMIC MBG Temperature monitor driver");
256 MODULE_LICENSE("GPL");
257 MODULE_IMPORT_NS("IIO_CONSUMER");
258