xref: /linux/drivers/thermal/qcom/tsens.c (revision 439f2409a242549b614decfccbbacecad85d2c79)
12d71d8deSAmit Kucheria // SPDX-License-Identifier: GPL-2.0
29066073cSRajendra Nayak /*
39066073cSRajendra Nayak  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4a7ff8297SAmit Kucheria  * Copyright (c) 2019, 2020, Linaro Ltd.
59066073cSRajendra Nayak  */
69066073cSRajendra Nayak 
77c938f48SAmit Kucheria #include <linux/debugfs.h>
89066073cSRajendra Nayak #include <linux/err.h>
9a7ff8297SAmit Kucheria #include <linux/io.h>
109066073cSRajendra Nayak #include <linux/module.h>
11a7ff8297SAmit Kucheria #include <linux/nvmem-consumer.h>
129066073cSRajendra Nayak #include <linux/of.h>
13a7ff8297SAmit Kucheria #include <linux/of_address.h>
14634e11d5SAmit Kucheria #include <linux/of_platform.h>
1553e2a20eSAnsuel Smith #include <linux/mfd/syscon.h>
169066073cSRajendra Nayak #include <linux/platform_device.h>
179066073cSRajendra Nayak #include <linux/pm.h>
18a7ff8297SAmit Kucheria #include <linux/regmap.h>
199066073cSRajendra Nayak #include <linux/slab.h>
209066073cSRajendra Nayak #include <linux/thermal.h>
218556e19dSDmitry Baryshkov #include "../thermal_hwmon.h"
229066073cSRajendra Nayak #include "tsens.h"
239066073cSRajendra Nayak 
24a7ff8297SAmit Kucheria /**
25a7ff8297SAmit Kucheria  * struct tsens_irq_data - IRQ status and temperature violations
26a7ff8297SAmit Kucheria  * @up_viol:        upper threshold violated
27a7ff8297SAmit Kucheria  * @up_thresh:      upper threshold temperature value
28a7ff8297SAmit Kucheria  * @up_irq_mask:    mask register for upper threshold irqs
29a7ff8297SAmit Kucheria  * @up_irq_clear:   clear register for uppper threshold irqs
30a7ff8297SAmit Kucheria  * @low_viol:       lower threshold violated
31a7ff8297SAmit Kucheria  * @low_thresh:     lower threshold temperature value
32a7ff8297SAmit Kucheria  * @low_irq_mask:   mask register for lower threshold irqs
33a7ff8297SAmit Kucheria  * @low_irq_clear:  clear register for lower threshold irqs
34a7ff8297SAmit Kucheria  * @crit_viol:      critical threshold violated
35a7ff8297SAmit Kucheria  * @crit_thresh:    critical threshold temperature value
36a7ff8297SAmit Kucheria  * @crit_irq_mask:  mask register for critical threshold irqs
37a7ff8297SAmit Kucheria  * @crit_irq_clear: clear register for critical threshold irqs
38a7ff8297SAmit Kucheria  *
39a7ff8297SAmit Kucheria  * Structure containing data about temperature threshold settings and
40a7ff8297SAmit Kucheria  * irq status if they were violated.
41a7ff8297SAmit Kucheria  */
42a7ff8297SAmit Kucheria struct tsens_irq_data {
43a7ff8297SAmit Kucheria 	u32 up_viol;
44a7ff8297SAmit Kucheria 	int up_thresh;
45a7ff8297SAmit Kucheria 	u32 up_irq_mask;
46a7ff8297SAmit Kucheria 	u32 up_irq_clear;
47a7ff8297SAmit Kucheria 	u32 low_viol;
48a7ff8297SAmit Kucheria 	int low_thresh;
49a7ff8297SAmit Kucheria 	u32 low_irq_mask;
50a7ff8297SAmit Kucheria 	u32 low_irq_clear;
51a7ff8297SAmit Kucheria 	u32 crit_viol;
52a7ff8297SAmit Kucheria 	u32 crit_thresh;
53a7ff8297SAmit Kucheria 	u32 crit_irq_mask;
54a7ff8297SAmit Kucheria 	u32 crit_irq_clear;
55a7ff8297SAmit Kucheria };
56a7ff8297SAmit Kucheria 
57a7ff8297SAmit Kucheria char *qfprom_read(struct device *dev, const char *cname)
58a7ff8297SAmit Kucheria {
59a7ff8297SAmit Kucheria 	struct nvmem_cell *cell;
60a7ff8297SAmit Kucheria 	ssize_t data;
61a7ff8297SAmit Kucheria 	char *ret;
62a7ff8297SAmit Kucheria 
63a7ff8297SAmit Kucheria 	cell = nvmem_cell_get(dev, cname);
64a7ff8297SAmit Kucheria 	if (IS_ERR(cell))
65a7ff8297SAmit Kucheria 		return ERR_CAST(cell);
66a7ff8297SAmit Kucheria 
67a7ff8297SAmit Kucheria 	ret = nvmem_cell_read(cell, &data);
68a7ff8297SAmit Kucheria 	nvmem_cell_put(cell);
69a7ff8297SAmit Kucheria 
70a7ff8297SAmit Kucheria 	return ret;
71a7ff8297SAmit Kucheria }
72a7ff8297SAmit Kucheria 
73*439f2409SDmitry Baryshkov int tsens_read_calibration(struct tsens_priv *priv, int shift, u32 *p1, u32 *p2, bool backup)
74498d2457SDmitry Baryshkov {
75498d2457SDmitry Baryshkov 	u32 mode;
76498d2457SDmitry Baryshkov 	u32 base1, base2;
77*439f2409SDmitry Baryshkov 	char name[] = "sXX_pY_backup"; /* s10_p1_backup */
78498d2457SDmitry Baryshkov 	int i, ret;
79498d2457SDmitry Baryshkov 
80498d2457SDmitry Baryshkov 	if (priv->num_sensors > MAX_SENSORS)
81498d2457SDmitry Baryshkov 		return -EINVAL;
82498d2457SDmitry Baryshkov 
83*439f2409SDmitry Baryshkov 	ret = snprintf(name, sizeof(name), "mode%s", backup ? "_backup" : "");
84*439f2409SDmitry Baryshkov 	if (ret < 0)
85*439f2409SDmitry Baryshkov 		return ret;
86*439f2409SDmitry Baryshkov 
87*439f2409SDmitry Baryshkov 	ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &mode);
88498d2457SDmitry Baryshkov 	if (ret == -ENOENT)
89498d2457SDmitry Baryshkov 		dev_warn(priv->dev, "Please migrate to separate nvmem cells for calibration data\n");
90498d2457SDmitry Baryshkov 	if (ret < 0)
91498d2457SDmitry Baryshkov 		return ret;
92498d2457SDmitry Baryshkov 
93498d2457SDmitry Baryshkov 	dev_dbg(priv->dev, "calibration mode is %d\n", mode);
94498d2457SDmitry Baryshkov 
95*439f2409SDmitry Baryshkov 	ret = snprintf(name, sizeof(name), "base1%s", backup ? "_backup" : "");
96498d2457SDmitry Baryshkov 	if (ret < 0)
97498d2457SDmitry Baryshkov 		return ret;
98498d2457SDmitry Baryshkov 
99*439f2409SDmitry Baryshkov 	ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &base1);
100*439f2409SDmitry Baryshkov 	if (ret < 0)
101*439f2409SDmitry Baryshkov 		return ret;
102*439f2409SDmitry Baryshkov 
103*439f2409SDmitry Baryshkov 	ret = snprintf(name, sizeof(name), "base2%s", backup ? "_backup" : "");
104*439f2409SDmitry Baryshkov 	if (ret < 0)
105*439f2409SDmitry Baryshkov 		return ret;
106*439f2409SDmitry Baryshkov 
107*439f2409SDmitry Baryshkov 	ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &base2);
108498d2457SDmitry Baryshkov 	if (ret < 0)
109498d2457SDmitry Baryshkov 		return ret;
110498d2457SDmitry Baryshkov 
111498d2457SDmitry Baryshkov 	for (i = 0; i < priv->num_sensors; i++) {
112*439f2409SDmitry Baryshkov 		ret = snprintf(name, sizeof(name), "s%d_p1%s", priv->sensor[i].hw_id,
113*439f2409SDmitry Baryshkov 			       backup ? "_backup" : "");
114498d2457SDmitry Baryshkov 		if (ret < 0)
115498d2457SDmitry Baryshkov 			return ret;
116498d2457SDmitry Baryshkov 
117498d2457SDmitry Baryshkov 		ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &p1[i]);
118498d2457SDmitry Baryshkov 		if (ret)
119498d2457SDmitry Baryshkov 			return ret;
120498d2457SDmitry Baryshkov 
121*439f2409SDmitry Baryshkov 		ret = snprintf(name, sizeof(name), "s%d_p2%s", priv->sensor[i].hw_id,
122*439f2409SDmitry Baryshkov 			       backup ? "_backup" : "");
123498d2457SDmitry Baryshkov 		if (ret < 0)
124498d2457SDmitry Baryshkov 			return ret;
125498d2457SDmitry Baryshkov 
126498d2457SDmitry Baryshkov 		ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &p2[i]);
127498d2457SDmitry Baryshkov 		if (ret)
128498d2457SDmitry Baryshkov 			return ret;
129498d2457SDmitry Baryshkov 	}
130498d2457SDmitry Baryshkov 
131498d2457SDmitry Baryshkov 	switch (mode) {
132498d2457SDmitry Baryshkov 	case ONE_PT_CALIB:
133498d2457SDmitry Baryshkov 		for (i = 0; i < priv->num_sensors; i++)
134498d2457SDmitry Baryshkov 			p1[i] = p1[i] + (base1 << shift);
135498d2457SDmitry Baryshkov 		break;
136498d2457SDmitry Baryshkov 	case TWO_PT_CALIB:
137498d2457SDmitry Baryshkov 		for (i = 0; i < priv->num_sensors; i++)
138498d2457SDmitry Baryshkov 			p2[i] = (p2[i] + base2) << shift;
139498d2457SDmitry Baryshkov 		fallthrough;
140498d2457SDmitry Baryshkov 	case ONE_PT_CALIB2:
141498d2457SDmitry Baryshkov 		for (i = 0; i < priv->num_sensors; i++)
142498d2457SDmitry Baryshkov 			p1[i] = (p1[i] + base1) << shift;
143498d2457SDmitry Baryshkov 		break;
144498d2457SDmitry Baryshkov 	default:
145498d2457SDmitry Baryshkov 		dev_dbg(priv->dev, "calibrationless mode\n");
146498d2457SDmitry Baryshkov 		for (i = 0; i < priv->num_sensors; i++) {
147498d2457SDmitry Baryshkov 			p1[i] = 500;
148498d2457SDmitry Baryshkov 			p2[i] = 780;
149498d2457SDmitry Baryshkov 		}
150498d2457SDmitry Baryshkov 	}
151498d2457SDmitry Baryshkov 
152*439f2409SDmitry Baryshkov 	return mode;
153*439f2409SDmitry Baryshkov }
154*439f2409SDmitry Baryshkov 
155*439f2409SDmitry Baryshkov int tsens_calibrate_nvmem(struct tsens_priv *priv, int shift)
156*439f2409SDmitry Baryshkov {
157*439f2409SDmitry Baryshkov 	u32 p1[MAX_SENSORS], p2[MAX_SENSORS];
158*439f2409SDmitry Baryshkov 	int mode;
159*439f2409SDmitry Baryshkov 
160*439f2409SDmitry Baryshkov 	mode = tsens_read_calibration(priv, shift, p1, p2, false);
161*439f2409SDmitry Baryshkov 	if (mode < 0)
162*439f2409SDmitry Baryshkov 		return mode;
163*439f2409SDmitry Baryshkov 
164498d2457SDmitry Baryshkov 	compute_intercept_slope(priv, p1, p2, mode);
165498d2457SDmitry Baryshkov 
166498d2457SDmitry Baryshkov 	return 0;
167498d2457SDmitry Baryshkov }
168498d2457SDmitry Baryshkov 
169498d2457SDmitry Baryshkov int tsens_calibrate_common(struct tsens_priv *priv)
170498d2457SDmitry Baryshkov {
171498d2457SDmitry Baryshkov 	return tsens_calibrate_nvmem(priv, 2);
172498d2457SDmitry Baryshkov }
173498d2457SDmitry Baryshkov 
174a7ff8297SAmit Kucheria /*
175a7ff8297SAmit Kucheria  * Use this function on devices where slope and offset calculations
176a7ff8297SAmit Kucheria  * depend on calibration data read from qfprom. On others the slope
177a7ff8297SAmit Kucheria  * and offset values are derived from tz->tzp->slope and tz->tzp->offset
178a7ff8297SAmit Kucheria  * resp.
179a7ff8297SAmit Kucheria  */
180a7ff8297SAmit Kucheria void compute_intercept_slope(struct tsens_priv *priv, u32 *p1,
181a7ff8297SAmit Kucheria 			     u32 *p2, u32 mode)
182a7ff8297SAmit Kucheria {
183a7ff8297SAmit Kucheria 	int i;
184a7ff8297SAmit Kucheria 	int num, den;
185a7ff8297SAmit Kucheria 
186a7ff8297SAmit Kucheria 	for (i = 0; i < priv->num_sensors; i++) {
187a7ff8297SAmit Kucheria 		dev_dbg(priv->dev,
188a7ff8297SAmit Kucheria 			"%s: sensor%d - data_point1:%#x data_point2:%#x\n",
189a7ff8297SAmit Kucheria 			__func__, i, p1[i], p2[i]);
190a7ff8297SAmit Kucheria 
1919d51769bSAnsuel Smith 		if (!priv->sensor[i].slope)
192a7ff8297SAmit Kucheria 			priv->sensor[i].slope = SLOPE_DEFAULT;
193a7ff8297SAmit Kucheria 		if (mode == TWO_PT_CALIB) {
194a7ff8297SAmit Kucheria 			/*
195a7ff8297SAmit Kucheria 			 * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
196a7ff8297SAmit Kucheria 			 *	temp_120_degc - temp_30_degc (x2 - x1)
197a7ff8297SAmit Kucheria 			 */
198a7ff8297SAmit Kucheria 			num = p2[i] - p1[i];
199a7ff8297SAmit Kucheria 			num *= SLOPE_FACTOR;
200a7ff8297SAmit Kucheria 			den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
201a7ff8297SAmit Kucheria 			priv->sensor[i].slope = num / den;
202a7ff8297SAmit Kucheria 		}
203a7ff8297SAmit Kucheria 
204a7ff8297SAmit Kucheria 		priv->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
205a7ff8297SAmit Kucheria 				(CAL_DEGC_PT1 *
206a7ff8297SAmit Kucheria 				priv->sensor[i].slope);
207a7ff8297SAmit Kucheria 		dev_dbg(priv->dev, "%s: offset:%d\n", __func__,
208a7ff8297SAmit Kucheria 			priv->sensor[i].offset);
209a7ff8297SAmit Kucheria 	}
210a7ff8297SAmit Kucheria }
211a7ff8297SAmit Kucheria 
212a7ff8297SAmit Kucheria static inline u32 degc_to_code(int degc, const struct tsens_sensor *s)
213a7ff8297SAmit Kucheria {
214a7ff8297SAmit Kucheria 	u64 code = div_u64(((u64)degc * s->slope + s->offset), SLOPE_FACTOR);
215a7ff8297SAmit Kucheria 
216a7ff8297SAmit Kucheria 	pr_debug("%s: raw_code: 0x%llx, degc:%d\n", __func__, code, degc);
217a7ff8297SAmit Kucheria 	return clamp_val(code, THRESHOLD_MIN_ADC_CODE, THRESHOLD_MAX_ADC_CODE);
218a7ff8297SAmit Kucheria }
219a7ff8297SAmit Kucheria 
220a7ff8297SAmit Kucheria static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
221a7ff8297SAmit Kucheria {
222a7ff8297SAmit Kucheria 	int degc, num, den;
223a7ff8297SAmit Kucheria 
224a7ff8297SAmit Kucheria 	num = (adc_code * SLOPE_FACTOR) - s->offset;
225a7ff8297SAmit Kucheria 	den = s->slope;
226a7ff8297SAmit Kucheria 
227a7ff8297SAmit Kucheria 	if (num > 0)
228a7ff8297SAmit Kucheria 		degc = num + (den / 2);
229a7ff8297SAmit Kucheria 	else if (num < 0)
230a7ff8297SAmit Kucheria 		degc = num - (den / 2);
231a7ff8297SAmit Kucheria 	else
232a7ff8297SAmit Kucheria 		degc = num;
233a7ff8297SAmit Kucheria 
234a7ff8297SAmit Kucheria 	degc /= den;
235a7ff8297SAmit Kucheria 
236a7ff8297SAmit Kucheria 	return degc;
237a7ff8297SAmit Kucheria }
238a7ff8297SAmit Kucheria 
239a7ff8297SAmit Kucheria /**
240a7ff8297SAmit Kucheria  * tsens_hw_to_mC - Return sign-extended temperature in mCelsius.
241a7ff8297SAmit Kucheria  * @s:     Pointer to sensor struct
242a7ff8297SAmit Kucheria  * @field: Index into regmap_field array pointing to temperature data
243a7ff8297SAmit Kucheria  *
244a7ff8297SAmit Kucheria  * This function handles temperature returned in ADC code or deciCelsius
245a7ff8297SAmit Kucheria  * depending on IP version.
246a7ff8297SAmit Kucheria  *
247a7ff8297SAmit Kucheria  * Return: Temperature in milliCelsius on success, a negative errno will
248a7ff8297SAmit Kucheria  * be returned in error cases
249a7ff8297SAmit Kucheria  */
250a7ff8297SAmit Kucheria static int tsens_hw_to_mC(const struct tsens_sensor *s, int field)
251a7ff8297SAmit Kucheria {
252a7ff8297SAmit Kucheria 	struct tsens_priv *priv = s->priv;
253a7ff8297SAmit Kucheria 	u32 resolution;
254a7ff8297SAmit Kucheria 	u32 temp = 0;
255a7ff8297SAmit Kucheria 	int ret;
256a7ff8297SAmit Kucheria 
257a7ff8297SAmit Kucheria 	resolution = priv->fields[LAST_TEMP_0].msb -
258a7ff8297SAmit Kucheria 		priv->fields[LAST_TEMP_0].lsb;
259a7ff8297SAmit Kucheria 
260a7ff8297SAmit Kucheria 	ret = regmap_field_read(priv->rf[field], &temp);
261a7ff8297SAmit Kucheria 	if (ret)
262a7ff8297SAmit Kucheria 		return ret;
263a7ff8297SAmit Kucheria 
264a7ff8297SAmit Kucheria 	/* Convert temperature from ADC code to milliCelsius */
265a7ff8297SAmit Kucheria 	if (priv->feat->adc)
266a7ff8297SAmit Kucheria 		return code_to_degc(temp, s) * 1000;
267a7ff8297SAmit Kucheria 
268a7ff8297SAmit Kucheria 	/* deciCelsius -> milliCelsius along with sign extension */
269a7ff8297SAmit Kucheria 	return sign_extend32(temp, resolution) * 100;
270a7ff8297SAmit Kucheria }
271a7ff8297SAmit Kucheria 
272a7ff8297SAmit Kucheria /**
273a7ff8297SAmit Kucheria  * tsens_mC_to_hw - Convert temperature to hardware register value
274a7ff8297SAmit Kucheria  * @s: Pointer to sensor struct
275a7ff8297SAmit Kucheria  * @temp: temperature in milliCelsius to be programmed to hardware
276a7ff8297SAmit Kucheria  *
277a7ff8297SAmit Kucheria  * This function outputs the value to be written to hardware in ADC code
278a7ff8297SAmit Kucheria  * or deciCelsius depending on IP version.
279a7ff8297SAmit Kucheria  *
280a7ff8297SAmit Kucheria  * Return: ADC code or temperature in deciCelsius.
281a7ff8297SAmit Kucheria  */
282a7ff8297SAmit Kucheria static int tsens_mC_to_hw(const struct tsens_sensor *s, int temp)
283a7ff8297SAmit Kucheria {
284a7ff8297SAmit Kucheria 	struct tsens_priv *priv = s->priv;
285a7ff8297SAmit Kucheria 
286a7ff8297SAmit Kucheria 	/* milliC to adc code */
287a7ff8297SAmit Kucheria 	if (priv->feat->adc)
288a7ff8297SAmit Kucheria 		return degc_to_code(temp / 1000, s);
289a7ff8297SAmit Kucheria 
290a7ff8297SAmit Kucheria 	/* milliC to deciC */
291a7ff8297SAmit Kucheria 	return temp / 100;
292a7ff8297SAmit Kucheria }
293a7ff8297SAmit Kucheria 
294a7ff8297SAmit Kucheria static inline enum tsens_ver tsens_version(struct tsens_priv *priv)
295a7ff8297SAmit Kucheria {
296a7ff8297SAmit Kucheria 	return priv->feat->ver_major;
297a7ff8297SAmit Kucheria }
298a7ff8297SAmit Kucheria 
299a7ff8297SAmit Kucheria static void tsens_set_interrupt_v1(struct tsens_priv *priv, u32 hw_id,
300a7ff8297SAmit Kucheria 				   enum tsens_irq_type irq_type, bool enable)
301a7ff8297SAmit Kucheria {
302a7ff8297SAmit Kucheria 	u32 index = 0;
303a7ff8297SAmit Kucheria 
304a7ff8297SAmit Kucheria 	switch (irq_type) {
305a7ff8297SAmit Kucheria 	case UPPER:
306a7ff8297SAmit Kucheria 		index = UP_INT_CLEAR_0 + hw_id;
307a7ff8297SAmit Kucheria 		break;
308a7ff8297SAmit Kucheria 	case LOWER:
309a7ff8297SAmit Kucheria 		index = LOW_INT_CLEAR_0 + hw_id;
310a7ff8297SAmit Kucheria 		break;
311a7ff8297SAmit Kucheria 	case CRITICAL:
312a7ff8297SAmit Kucheria 		/* No critical interrupts before v2 */
313a7ff8297SAmit Kucheria 		return;
314a7ff8297SAmit Kucheria 	}
315a7ff8297SAmit Kucheria 	regmap_field_write(priv->rf[index], enable ? 0 : 1);
316a7ff8297SAmit Kucheria }
317a7ff8297SAmit Kucheria 
318a7ff8297SAmit Kucheria static void tsens_set_interrupt_v2(struct tsens_priv *priv, u32 hw_id,
319a7ff8297SAmit Kucheria 				   enum tsens_irq_type irq_type, bool enable)
320a7ff8297SAmit Kucheria {
321a7ff8297SAmit Kucheria 	u32 index_mask = 0, index_clear = 0;
322a7ff8297SAmit Kucheria 
323a7ff8297SAmit Kucheria 	/*
324a7ff8297SAmit Kucheria 	 * To enable the interrupt flag for a sensor:
325a7ff8297SAmit Kucheria 	 *    - clear the mask bit
326a7ff8297SAmit Kucheria 	 * To disable the interrupt flag for a sensor:
327a7ff8297SAmit Kucheria 	 *    - Mask further interrupts for this sensor
328a7ff8297SAmit Kucheria 	 *    - Write 1 followed by 0 to clear the interrupt
329a7ff8297SAmit Kucheria 	 */
330a7ff8297SAmit Kucheria 	switch (irq_type) {
331a7ff8297SAmit Kucheria 	case UPPER:
332a7ff8297SAmit Kucheria 		index_mask  = UP_INT_MASK_0 + hw_id;
333a7ff8297SAmit Kucheria 		index_clear = UP_INT_CLEAR_0 + hw_id;
334a7ff8297SAmit Kucheria 		break;
335a7ff8297SAmit Kucheria 	case LOWER:
336a7ff8297SAmit Kucheria 		index_mask  = LOW_INT_MASK_0 + hw_id;
337a7ff8297SAmit Kucheria 		index_clear = LOW_INT_CLEAR_0 + hw_id;
338a7ff8297SAmit Kucheria 		break;
339a7ff8297SAmit Kucheria 	case CRITICAL:
340a7ff8297SAmit Kucheria 		index_mask  = CRIT_INT_MASK_0 + hw_id;
341a7ff8297SAmit Kucheria 		index_clear = CRIT_INT_CLEAR_0 + hw_id;
342a7ff8297SAmit Kucheria 		break;
343a7ff8297SAmit Kucheria 	}
344a7ff8297SAmit Kucheria 
345a7ff8297SAmit Kucheria 	if (enable) {
346a7ff8297SAmit Kucheria 		regmap_field_write(priv->rf[index_mask], 0);
347a7ff8297SAmit Kucheria 	} else {
348a7ff8297SAmit Kucheria 		regmap_field_write(priv->rf[index_mask],  1);
349a7ff8297SAmit Kucheria 		regmap_field_write(priv->rf[index_clear], 1);
350a7ff8297SAmit Kucheria 		regmap_field_write(priv->rf[index_clear], 0);
351a7ff8297SAmit Kucheria 	}
352a7ff8297SAmit Kucheria }
353a7ff8297SAmit Kucheria 
354a7ff8297SAmit Kucheria /**
355a7ff8297SAmit Kucheria  * tsens_set_interrupt - Set state of an interrupt
356a7ff8297SAmit Kucheria  * @priv: Pointer to tsens controller private data
357a7ff8297SAmit Kucheria  * @hw_id: Hardware ID aka. sensor number
358a7ff8297SAmit Kucheria  * @irq_type: irq_type from enum tsens_irq_type
359a7ff8297SAmit Kucheria  * @enable: false = disable, true = enable
360a7ff8297SAmit Kucheria  *
361a7ff8297SAmit Kucheria  * Call IP-specific function to set state of an interrupt
362a7ff8297SAmit Kucheria  *
363a7ff8297SAmit Kucheria  * Return: void
364a7ff8297SAmit Kucheria  */
365a7ff8297SAmit Kucheria static void tsens_set_interrupt(struct tsens_priv *priv, u32 hw_id,
366a7ff8297SAmit Kucheria 				enum tsens_irq_type irq_type, bool enable)
367a7ff8297SAmit Kucheria {
368a7ff8297SAmit Kucheria 	dev_dbg(priv->dev, "[%u] %s: %s -> %s\n", hw_id, __func__,
369a7ff8297SAmit Kucheria 		irq_type ? ((irq_type == 1) ? "UP" : "CRITICAL") : "LOW",
370a7ff8297SAmit Kucheria 		enable ? "en" : "dis");
371a7ff8297SAmit Kucheria 	if (tsens_version(priv) > VER_1_X)
372a7ff8297SAmit Kucheria 		tsens_set_interrupt_v2(priv, hw_id, irq_type, enable);
373a7ff8297SAmit Kucheria 	else
374a7ff8297SAmit Kucheria 		tsens_set_interrupt_v1(priv, hw_id, irq_type, enable);
375a7ff8297SAmit Kucheria }
376a7ff8297SAmit Kucheria 
377a7ff8297SAmit Kucheria /**
378a7ff8297SAmit Kucheria  * tsens_threshold_violated - Check if a sensor temperature violated a preset threshold
379a7ff8297SAmit Kucheria  * @priv: Pointer to tsens controller private data
380a7ff8297SAmit Kucheria  * @hw_id: Hardware ID aka. sensor number
381a7ff8297SAmit Kucheria  * @d: Pointer to irq state data
382a7ff8297SAmit Kucheria  *
383a7ff8297SAmit Kucheria  * Return: 0 if threshold was not violated, 1 if it was violated and negative
384a7ff8297SAmit Kucheria  * errno in case of errors
385a7ff8297SAmit Kucheria  */
386a7ff8297SAmit Kucheria static int tsens_threshold_violated(struct tsens_priv *priv, u32 hw_id,
387a7ff8297SAmit Kucheria 				    struct tsens_irq_data *d)
388a7ff8297SAmit Kucheria {
389a7ff8297SAmit Kucheria 	int ret;
390a7ff8297SAmit Kucheria 
391a7ff8297SAmit Kucheria 	ret = regmap_field_read(priv->rf[UPPER_STATUS_0 + hw_id], &d->up_viol);
392a7ff8297SAmit Kucheria 	if (ret)
393a7ff8297SAmit Kucheria 		return ret;
394a7ff8297SAmit Kucheria 	ret = regmap_field_read(priv->rf[LOWER_STATUS_0 + hw_id], &d->low_viol);
395a7ff8297SAmit Kucheria 	if (ret)
396a7ff8297SAmit Kucheria 		return ret;
397a7ff8297SAmit Kucheria 
398a7ff8297SAmit Kucheria 	if (priv->feat->crit_int) {
399a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[CRITICAL_STATUS_0 + hw_id],
400a7ff8297SAmit Kucheria 					&d->crit_viol);
401a7ff8297SAmit Kucheria 		if (ret)
402a7ff8297SAmit Kucheria 			return ret;
403a7ff8297SAmit Kucheria 	}
404a7ff8297SAmit Kucheria 
405a7ff8297SAmit Kucheria 	if (d->up_viol || d->low_viol || d->crit_viol)
406a7ff8297SAmit Kucheria 		return 1;
407a7ff8297SAmit Kucheria 
408a7ff8297SAmit Kucheria 	return 0;
409a7ff8297SAmit Kucheria }
410a7ff8297SAmit Kucheria 
411a7ff8297SAmit Kucheria static int tsens_read_irq_state(struct tsens_priv *priv, u32 hw_id,
412a7ff8297SAmit Kucheria 				const struct tsens_sensor *s,
413a7ff8297SAmit Kucheria 				struct tsens_irq_data *d)
414a7ff8297SAmit Kucheria {
415a7ff8297SAmit Kucheria 	int ret;
416a7ff8297SAmit Kucheria 
417a7ff8297SAmit Kucheria 	ret = regmap_field_read(priv->rf[UP_INT_CLEAR_0 + hw_id], &d->up_irq_clear);
418a7ff8297SAmit Kucheria 	if (ret)
419a7ff8297SAmit Kucheria 		return ret;
420a7ff8297SAmit Kucheria 	ret = regmap_field_read(priv->rf[LOW_INT_CLEAR_0 + hw_id], &d->low_irq_clear);
421a7ff8297SAmit Kucheria 	if (ret)
422a7ff8297SAmit Kucheria 		return ret;
423a7ff8297SAmit Kucheria 	if (tsens_version(priv) > VER_1_X) {
424a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[UP_INT_MASK_0 + hw_id], &d->up_irq_mask);
425a7ff8297SAmit Kucheria 		if (ret)
426a7ff8297SAmit Kucheria 			return ret;
427a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[LOW_INT_MASK_0 + hw_id], &d->low_irq_mask);
428a7ff8297SAmit Kucheria 		if (ret)
429a7ff8297SAmit Kucheria 			return ret;
430a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[CRIT_INT_CLEAR_0 + hw_id],
431a7ff8297SAmit Kucheria 					&d->crit_irq_clear);
432a7ff8297SAmit Kucheria 		if (ret)
433a7ff8297SAmit Kucheria 			return ret;
434a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[CRIT_INT_MASK_0 + hw_id],
435a7ff8297SAmit Kucheria 					&d->crit_irq_mask);
436a7ff8297SAmit Kucheria 		if (ret)
437a7ff8297SAmit Kucheria 			return ret;
438a7ff8297SAmit Kucheria 
439a7ff8297SAmit Kucheria 		d->crit_thresh = tsens_hw_to_mC(s, CRIT_THRESH_0 + hw_id);
440a7ff8297SAmit Kucheria 	} else {
441a7ff8297SAmit Kucheria 		/* No mask register on older TSENS */
442a7ff8297SAmit Kucheria 		d->up_irq_mask = 0;
443a7ff8297SAmit Kucheria 		d->low_irq_mask = 0;
444a7ff8297SAmit Kucheria 		d->crit_irq_clear = 0;
445a7ff8297SAmit Kucheria 		d->crit_irq_mask = 0;
446a7ff8297SAmit Kucheria 		d->crit_thresh = 0;
447a7ff8297SAmit Kucheria 	}
448a7ff8297SAmit Kucheria 
449a7ff8297SAmit Kucheria 	d->up_thresh  = tsens_hw_to_mC(s, UP_THRESH_0 + hw_id);
450a7ff8297SAmit Kucheria 	d->low_thresh = tsens_hw_to_mC(s, LOW_THRESH_0 + hw_id);
451a7ff8297SAmit Kucheria 
452a7ff8297SAmit Kucheria 	dev_dbg(priv->dev, "[%u] %s%s: status(%u|%u|%u) | clr(%u|%u|%u) | mask(%u|%u|%u)\n",
453a7ff8297SAmit Kucheria 		hw_id, __func__,
454a7ff8297SAmit Kucheria 		(d->up_viol || d->low_viol || d->crit_viol) ? "(V)" : "",
455a7ff8297SAmit Kucheria 		d->low_viol, d->up_viol, d->crit_viol,
456a7ff8297SAmit Kucheria 		d->low_irq_clear, d->up_irq_clear, d->crit_irq_clear,
457a7ff8297SAmit Kucheria 		d->low_irq_mask, d->up_irq_mask, d->crit_irq_mask);
458a7ff8297SAmit Kucheria 	dev_dbg(priv->dev, "[%u] %s%s: thresh: (%d:%d:%d)\n", hw_id, __func__,
459a7ff8297SAmit Kucheria 		(d->up_viol || d->low_viol || d->crit_viol) ? "(V)" : "",
460a7ff8297SAmit Kucheria 		d->low_thresh, d->up_thresh, d->crit_thresh);
461a7ff8297SAmit Kucheria 
462a7ff8297SAmit Kucheria 	return 0;
463a7ff8297SAmit Kucheria }
464a7ff8297SAmit Kucheria 
465a7ff8297SAmit Kucheria static inline u32 masked_irq(u32 hw_id, u32 mask, enum tsens_ver ver)
466a7ff8297SAmit Kucheria {
467a7ff8297SAmit Kucheria 	if (ver > VER_1_X)
468a7ff8297SAmit Kucheria 		return mask & (1 << hw_id);
469a7ff8297SAmit Kucheria 
470a7ff8297SAmit Kucheria 	/* v1, v0.1 don't have a irq mask register */
471a7ff8297SAmit Kucheria 	return 0;
472a7ff8297SAmit Kucheria }
473a7ff8297SAmit Kucheria 
474a7ff8297SAmit Kucheria /**
475a7ff8297SAmit Kucheria  * tsens_critical_irq_thread() - Threaded handler for critical interrupts
476a7ff8297SAmit Kucheria  * @irq: irq number
477a7ff8297SAmit Kucheria  * @data: tsens controller private data
478a7ff8297SAmit Kucheria  *
479a7ff8297SAmit Kucheria  * Check FSM watchdog bark status and clear if needed.
480a7ff8297SAmit Kucheria  * Check all sensors to find ones that violated their critical threshold limits.
481a7ff8297SAmit Kucheria  * Clear and then re-enable the interrupt.
482a7ff8297SAmit Kucheria  *
483a7ff8297SAmit Kucheria  * The level-triggered interrupt might deassert if the temperature returned to
484a7ff8297SAmit Kucheria  * within the threshold limits by the time the handler got scheduled. We
485a7ff8297SAmit Kucheria  * consider the irq to have been handled in that case.
486a7ff8297SAmit Kucheria  *
487a7ff8297SAmit Kucheria  * Return: IRQ_HANDLED
488a7ff8297SAmit Kucheria  */
4893ecc8292SAmit Kucheria static irqreturn_t tsens_critical_irq_thread(int irq, void *data)
490a7ff8297SAmit Kucheria {
491a7ff8297SAmit Kucheria 	struct tsens_priv *priv = data;
492a7ff8297SAmit Kucheria 	struct tsens_irq_data d;
493a7ff8297SAmit Kucheria 	int temp, ret, i;
494a7ff8297SAmit Kucheria 	u32 wdog_status, wdog_count;
495a7ff8297SAmit Kucheria 
496a7ff8297SAmit Kucheria 	if (priv->feat->has_watchdog) {
497a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[WDOG_BARK_STATUS],
498a7ff8297SAmit Kucheria 					&wdog_status);
499a7ff8297SAmit Kucheria 		if (ret)
500a7ff8297SAmit Kucheria 			return ret;
501a7ff8297SAmit Kucheria 
502a7ff8297SAmit Kucheria 		if (wdog_status) {
503a7ff8297SAmit Kucheria 			/* Clear WDOG interrupt */
504a7ff8297SAmit Kucheria 			regmap_field_write(priv->rf[WDOG_BARK_CLEAR], 1);
505a7ff8297SAmit Kucheria 			regmap_field_write(priv->rf[WDOG_BARK_CLEAR], 0);
506a7ff8297SAmit Kucheria 			ret = regmap_field_read(priv->rf[WDOG_BARK_COUNT],
507a7ff8297SAmit Kucheria 						&wdog_count);
508a7ff8297SAmit Kucheria 			if (ret)
509a7ff8297SAmit Kucheria 				return ret;
510a7ff8297SAmit Kucheria 			if (wdog_count)
511a7ff8297SAmit Kucheria 				dev_dbg(priv->dev, "%s: watchdog count: %d\n",
512a7ff8297SAmit Kucheria 					__func__, wdog_count);
513a7ff8297SAmit Kucheria 
514a7ff8297SAmit Kucheria 			/* Fall through to handle critical interrupts if any */
515a7ff8297SAmit Kucheria 		}
516a7ff8297SAmit Kucheria 	}
517a7ff8297SAmit Kucheria 
518a7ff8297SAmit Kucheria 	for (i = 0; i < priv->num_sensors; i++) {
519a7ff8297SAmit Kucheria 		const struct tsens_sensor *s = &priv->sensor[i];
520a7ff8297SAmit Kucheria 		u32 hw_id = s->hw_id;
521a7ff8297SAmit Kucheria 
522cf969218SAnsuel Smith 		if (!s->tzd)
523a7ff8297SAmit Kucheria 			continue;
524a7ff8297SAmit Kucheria 		if (!tsens_threshold_violated(priv, hw_id, &d))
525a7ff8297SAmit Kucheria 			continue;
526a7ff8297SAmit Kucheria 		ret = get_temp_tsens_valid(s, &temp);
527a7ff8297SAmit Kucheria 		if (ret) {
528a7ff8297SAmit Kucheria 			dev_err(priv->dev, "[%u] %s: error reading sensor\n",
529a7ff8297SAmit Kucheria 				hw_id, __func__);
530a7ff8297SAmit Kucheria 			continue;
531a7ff8297SAmit Kucheria 		}
532a7ff8297SAmit Kucheria 
533a7ff8297SAmit Kucheria 		tsens_read_irq_state(priv, hw_id, s, &d);
534a7ff8297SAmit Kucheria 		if (d.crit_viol &&
535a7ff8297SAmit Kucheria 		    !masked_irq(hw_id, d.crit_irq_mask, tsens_version(priv))) {
536a7ff8297SAmit Kucheria 			/* Mask critical interrupts, unused on Linux */
537a7ff8297SAmit Kucheria 			tsens_set_interrupt(priv, hw_id, CRITICAL, false);
538a7ff8297SAmit Kucheria 		}
539a7ff8297SAmit Kucheria 	}
540a7ff8297SAmit Kucheria 
541a7ff8297SAmit Kucheria 	return IRQ_HANDLED;
542a7ff8297SAmit Kucheria }
543a7ff8297SAmit Kucheria 
544a7ff8297SAmit Kucheria /**
545a7ff8297SAmit Kucheria  * tsens_irq_thread - Threaded interrupt handler for uplow interrupts
546a7ff8297SAmit Kucheria  * @irq: irq number
547a7ff8297SAmit Kucheria  * @data: tsens controller private data
548a7ff8297SAmit Kucheria  *
549a7ff8297SAmit Kucheria  * Check all sensors to find ones that violated their threshold limits. If the
550a7ff8297SAmit Kucheria  * temperature is still outside the limits, call thermal_zone_device_update() to
551a7ff8297SAmit Kucheria  * update the thresholds, else re-enable the interrupts.
552a7ff8297SAmit Kucheria  *
553a7ff8297SAmit Kucheria  * The level-triggered interrupt might deassert if the temperature returned to
554a7ff8297SAmit Kucheria  * within the threshold limits by the time the handler got scheduled. We
555a7ff8297SAmit Kucheria  * consider the irq to have been handled in that case.
556a7ff8297SAmit Kucheria  *
557a7ff8297SAmit Kucheria  * Return: IRQ_HANDLED
558a7ff8297SAmit Kucheria  */
5593ecc8292SAmit Kucheria static irqreturn_t tsens_irq_thread(int irq, void *data)
560a7ff8297SAmit Kucheria {
561a7ff8297SAmit Kucheria 	struct tsens_priv *priv = data;
562a7ff8297SAmit Kucheria 	struct tsens_irq_data d;
563a7ff8297SAmit Kucheria 	bool enable = true, disable = false;
564a7ff8297SAmit Kucheria 	unsigned long flags;
565a7ff8297SAmit Kucheria 	int temp, ret, i;
566a7ff8297SAmit Kucheria 
567a7ff8297SAmit Kucheria 	for (i = 0; i < priv->num_sensors; i++) {
568a7ff8297SAmit Kucheria 		bool trigger = false;
569a7ff8297SAmit Kucheria 		const struct tsens_sensor *s = &priv->sensor[i];
570a7ff8297SAmit Kucheria 		u32 hw_id = s->hw_id;
571a7ff8297SAmit Kucheria 
572cf969218SAnsuel Smith 		if (!s->tzd)
573a7ff8297SAmit Kucheria 			continue;
574a7ff8297SAmit Kucheria 		if (!tsens_threshold_violated(priv, hw_id, &d))
575a7ff8297SAmit Kucheria 			continue;
576a7ff8297SAmit Kucheria 		ret = get_temp_tsens_valid(s, &temp);
577a7ff8297SAmit Kucheria 		if (ret) {
578a7ff8297SAmit Kucheria 			dev_err(priv->dev, "[%u] %s: error reading sensor\n",
579a7ff8297SAmit Kucheria 				hw_id, __func__);
580a7ff8297SAmit Kucheria 			continue;
581a7ff8297SAmit Kucheria 		}
582a7ff8297SAmit Kucheria 
583a7ff8297SAmit Kucheria 		spin_lock_irqsave(&priv->ul_lock, flags);
584a7ff8297SAmit Kucheria 
585a7ff8297SAmit Kucheria 		tsens_read_irq_state(priv, hw_id, s, &d);
586a7ff8297SAmit Kucheria 
587a7ff8297SAmit Kucheria 		if (d.up_viol &&
588a7ff8297SAmit Kucheria 		    !masked_irq(hw_id, d.up_irq_mask, tsens_version(priv))) {
589a7ff8297SAmit Kucheria 			tsens_set_interrupt(priv, hw_id, UPPER, disable);
590a7ff8297SAmit Kucheria 			if (d.up_thresh > temp) {
591a7ff8297SAmit Kucheria 				dev_dbg(priv->dev, "[%u] %s: re-arm upper\n",
592a7ff8297SAmit Kucheria 					hw_id, __func__);
593a7ff8297SAmit Kucheria 				tsens_set_interrupt(priv, hw_id, UPPER, enable);
594a7ff8297SAmit Kucheria 			} else {
595a7ff8297SAmit Kucheria 				trigger = true;
596a7ff8297SAmit Kucheria 				/* Keep irq masked */
597a7ff8297SAmit Kucheria 			}
598a7ff8297SAmit Kucheria 		} else if (d.low_viol &&
599a7ff8297SAmit Kucheria 			   !masked_irq(hw_id, d.low_irq_mask, tsens_version(priv))) {
600a7ff8297SAmit Kucheria 			tsens_set_interrupt(priv, hw_id, LOWER, disable);
601a7ff8297SAmit Kucheria 			if (d.low_thresh < temp) {
602a7ff8297SAmit Kucheria 				dev_dbg(priv->dev, "[%u] %s: re-arm low\n",
603a7ff8297SAmit Kucheria 					hw_id, __func__);
604a7ff8297SAmit Kucheria 				tsens_set_interrupt(priv, hw_id, LOWER, enable);
605a7ff8297SAmit Kucheria 			} else {
606a7ff8297SAmit Kucheria 				trigger = true;
607a7ff8297SAmit Kucheria 				/* Keep irq masked */
608a7ff8297SAmit Kucheria 			}
609a7ff8297SAmit Kucheria 		}
610a7ff8297SAmit Kucheria 
611a7ff8297SAmit Kucheria 		spin_unlock_irqrestore(&priv->ul_lock, flags);
612a7ff8297SAmit Kucheria 
613a7ff8297SAmit Kucheria 		if (trigger) {
614a7ff8297SAmit Kucheria 			dev_dbg(priv->dev, "[%u] %s: TZ update trigger (%d mC)\n",
615a7ff8297SAmit Kucheria 				hw_id, __func__, temp);
616a7ff8297SAmit Kucheria 			thermal_zone_device_update(s->tzd,
617a7ff8297SAmit Kucheria 						   THERMAL_EVENT_UNSPECIFIED);
618a7ff8297SAmit Kucheria 		} else {
619a7ff8297SAmit Kucheria 			dev_dbg(priv->dev, "[%u] %s: no violation:  %d\n",
620a7ff8297SAmit Kucheria 				hw_id, __func__, temp);
621a7ff8297SAmit Kucheria 		}
62253e2a20eSAnsuel Smith 
62353e2a20eSAnsuel Smith 		if (tsens_version(priv) < VER_0_1) {
62453e2a20eSAnsuel Smith 			/* Constraint: There is only 1 interrupt control register for all
62553e2a20eSAnsuel Smith 			 * 11 temperature sensor. So monitoring more than 1 sensor based
62653e2a20eSAnsuel Smith 			 * on interrupts will yield inconsistent result. To overcome this
62753e2a20eSAnsuel Smith 			 * issue we will monitor only sensor 0 which is the master sensor.
62853e2a20eSAnsuel Smith 			 */
62953e2a20eSAnsuel Smith 			break;
63053e2a20eSAnsuel Smith 		}
631a7ff8297SAmit Kucheria 	}
632a7ff8297SAmit Kucheria 
633a7ff8297SAmit Kucheria 	return IRQ_HANDLED;
634a7ff8297SAmit Kucheria }
635a7ff8297SAmit Kucheria 
6364360af35SRobert Marko /**
6374360af35SRobert Marko  * tsens_combined_irq_thread() - Threaded interrupt handler for combined interrupts
6384360af35SRobert Marko  * @irq: irq number
6394360af35SRobert Marko  * @data: tsens controller private data
6404360af35SRobert Marko  *
6414360af35SRobert Marko  * Handle the combined interrupt as if it were 2 separate interrupts, so call the
6424360af35SRobert Marko  * critical handler first and then the up/low one.
6434360af35SRobert Marko  *
6444360af35SRobert Marko  * Return: IRQ_HANDLED
6454360af35SRobert Marko  */
6464360af35SRobert Marko static irqreturn_t tsens_combined_irq_thread(int irq, void *data)
6474360af35SRobert Marko {
6484360af35SRobert Marko 	irqreturn_t ret;
6494360af35SRobert Marko 
6504360af35SRobert Marko 	ret = tsens_critical_irq_thread(irq, data);
6514360af35SRobert Marko 	if (ret != IRQ_HANDLED)
6524360af35SRobert Marko 		return ret;
6534360af35SRobert Marko 
6544360af35SRobert Marko 	return tsens_irq_thread(irq, data);
6554360af35SRobert Marko }
6564360af35SRobert Marko 
657ca1b9a9eSDaniel Lezcano static int tsens_set_trips(struct thermal_zone_device *tz, int low, int high)
658a7ff8297SAmit Kucheria {
659ca1b9a9eSDaniel Lezcano 	struct tsens_sensor *s = tz->devdata;
660a7ff8297SAmit Kucheria 	struct tsens_priv *priv = s->priv;
661a7ff8297SAmit Kucheria 	struct device *dev = priv->dev;
662a7ff8297SAmit Kucheria 	struct tsens_irq_data d;
663a7ff8297SAmit Kucheria 	unsigned long flags;
664a7ff8297SAmit Kucheria 	int high_val, low_val, cl_high, cl_low;
665a7ff8297SAmit Kucheria 	u32 hw_id = s->hw_id;
666a7ff8297SAmit Kucheria 
66753e2a20eSAnsuel Smith 	if (tsens_version(priv) < VER_0_1) {
66853e2a20eSAnsuel Smith 		/* Pre v0.1 IP had a single register for each type of interrupt
66953e2a20eSAnsuel Smith 		 * and thresholds
67053e2a20eSAnsuel Smith 		 */
67153e2a20eSAnsuel Smith 		hw_id = 0;
67253e2a20eSAnsuel Smith 	}
67353e2a20eSAnsuel Smith 
674a7ff8297SAmit Kucheria 	dev_dbg(dev, "[%u] %s: proposed thresholds: (%d:%d)\n",
675a7ff8297SAmit Kucheria 		hw_id, __func__, low, high);
676a7ff8297SAmit Kucheria 
677f63bacedSRobert Marko 	cl_high = clamp_val(high, priv->feat->trip_min_temp, priv->feat->trip_max_temp);
678f63bacedSRobert Marko 	cl_low  = clamp_val(low, priv->feat->trip_min_temp, priv->feat->trip_max_temp);
679a7ff8297SAmit Kucheria 
680a7ff8297SAmit Kucheria 	high_val = tsens_mC_to_hw(s, cl_high);
681a7ff8297SAmit Kucheria 	low_val  = tsens_mC_to_hw(s, cl_low);
682a7ff8297SAmit Kucheria 
683a7ff8297SAmit Kucheria 	spin_lock_irqsave(&priv->ul_lock, flags);
684a7ff8297SAmit Kucheria 
685a7ff8297SAmit Kucheria 	tsens_read_irq_state(priv, hw_id, s, &d);
686a7ff8297SAmit Kucheria 
687a7ff8297SAmit Kucheria 	/* Write the new thresholds and clear the status */
688a7ff8297SAmit Kucheria 	regmap_field_write(priv->rf[LOW_THRESH_0 + hw_id], low_val);
689a7ff8297SAmit Kucheria 	regmap_field_write(priv->rf[UP_THRESH_0 + hw_id], high_val);
690a7ff8297SAmit Kucheria 	tsens_set_interrupt(priv, hw_id, LOWER, true);
691a7ff8297SAmit Kucheria 	tsens_set_interrupt(priv, hw_id, UPPER, true);
692a7ff8297SAmit Kucheria 
693a7ff8297SAmit Kucheria 	spin_unlock_irqrestore(&priv->ul_lock, flags);
694a7ff8297SAmit Kucheria 
695a7ff8297SAmit Kucheria 	dev_dbg(dev, "[%u] %s: (%d:%d)->(%d:%d)\n",
696a7ff8297SAmit Kucheria 		hw_id, __func__, d.low_thresh, d.up_thresh, cl_low, cl_high);
697a7ff8297SAmit Kucheria 
698a7ff8297SAmit Kucheria 	return 0;
699a7ff8297SAmit Kucheria }
700a7ff8297SAmit Kucheria 
7013ecc8292SAmit Kucheria static int tsens_enable_irq(struct tsens_priv *priv)
702a7ff8297SAmit Kucheria {
703a7ff8297SAmit Kucheria 	int ret;
704a7ff8297SAmit Kucheria 	int val = tsens_version(priv) > VER_1_X ? 7 : 1;
705a7ff8297SAmit Kucheria 
706a7ff8297SAmit Kucheria 	ret = regmap_field_write(priv->rf[INT_EN], val);
707a7ff8297SAmit Kucheria 	if (ret < 0)
708a7ff8297SAmit Kucheria 		dev_err(priv->dev, "%s: failed to enable interrupts\n",
709a7ff8297SAmit Kucheria 			__func__);
710a7ff8297SAmit Kucheria 
711a7ff8297SAmit Kucheria 	return ret;
712a7ff8297SAmit Kucheria }
713a7ff8297SAmit Kucheria 
7143ecc8292SAmit Kucheria static void tsens_disable_irq(struct tsens_priv *priv)
715a7ff8297SAmit Kucheria {
716a7ff8297SAmit Kucheria 	regmap_field_write(priv->rf[INT_EN], 0);
717a7ff8297SAmit Kucheria }
718a7ff8297SAmit Kucheria 
719a7ff8297SAmit Kucheria int get_temp_tsens_valid(const struct tsens_sensor *s, int *temp)
720a7ff8297SAmit Kucheria {
721a7ff8297SAmit Kucheria 	struct tsens_priv *priv = s->priv;
722a7ff8297SAmit Kucheria 	int hw_id = s->hw_id;
723a7ff8297SAmit Kucheria 	u32 temp_idx = LAST_TEMP_0 + hw_id;
724a7ff8297SAmit Kucheria 	u32 valid_idx = VALID_0 + hw_id;
725a7ff8297SAmit Kucheria 	u32 valid;
726a7ff8297SAmit Kucheria 	int ret;
727a7ff8297SAmit Kucheria 
72853e2a20eSAnsuel Smith 	/* VER_0 doesn't have VALID bit */
729d012f918SAnsuel Smith 	if (tsens_version(priv) == VER_0)
730d012f918SAnsuel Smith 		goto get_temp;
731d012f918SAnsuel Smith 
732a7ff8297SAmit Kucheria 	/* Valid bit is 0 for 6 AHB clock cycles.
733a7ff8297SAmit Kucheria 	 * At 19.2MHz, 1 AHB clock is ~60ns.
734a7ff8297SAmit Kucheria 	 * We should enter this loop very, very rarely.
735d012f918SAnsuel Smith 	 * Wait 1 us since it's the min of poll_timeout macro.
736d012f918SAnsuel Smith 	 * Old value was 400 ns.
737a7ff8297SAmit Kucheria 	 */
738d012f918SAnsuel Smith 	ret = regmap_field_read_poll_timeout(priv->rf[valid_idx], valid,
739d012f918SAnsuel Smith 					     valid, 1, 20 * USEC_PER_MSEC);
740a7ff8297SAmit Kucheria 	if (ret)
741a7ff8297SAmit Kucheria 		return ret;
742a7ff8297SAmit Kucheria 
743d012f918SAnsuel Smith get_temp:
744a7ff8297SAmit Kucheria 	/* Valid bit is set, OK to read the temperature */
745a7ff8297SAmit Kucheria 	*temp = tsens_hw_to_mC(s, temp_idx);
746a7ff8297SAmit Kucheria 
747a7ff8297SAmit Kucheria 	return 0;
748a7ff8297SAmit Kucheria }
749a7ff8297SAmit Kucheria 
750a7ff8297SAmit Kucheria int get_temp_common(const struct tsens_sensor *s, int *temp)
751a7ff8297SAmit Kucheria {
752a7ff8297SAmit Kucheria 	struct tsens_priv *priv = s->priv;
753a7ff8297SAmit Kucheria 	int hw_id = s->hw_id;
75453e2a20eSAnsuel Smith 	int last_temp = 0, ret, trdy;
75553e2a20eSAnsuel Smith 	unsigned long timeout;
75653e2a20eSAnsuel Smith 
75753e2a20eSAnsuel Smith 	timeout = jiffies + usecs_to_jiffies(TIMEOUT_US);
75853e2a20eSAnsuel Smith 	do {
75953e2a20eSAnsuel Smith 		if (tsens_version(priv) == VER_0) {
76053e2a20eSAnsuel Smith 			ret = regmap_field_read(priv->rf[TRDY], &trdy);
76153e2a20eSAnsuel Smith 			if (ret)
76253e2a20eSAnsuel Smith 				return ret;
76353e2a20eSAnsuel Smith 			if (!trdy)
76453e2a20eSAnsuel Smith 				continue;
76553e2a20eSAnsuel Smith 		}
766a7ff8297SAmit Kucheria 
767a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[LAST_TEMP_0 + hw_id], &last_temp);
768a7ff8297SAmit Kucheria 		if (ret)
769a7ff8297SAmit Kucheria 			return ret;
770a7ff8297SAmit Kucheria 
771a7ff8297SAmit Kucheria 		*temp = code_to_degc(last_temp, s) * 1000;
772a7ff8297SAmit Kucheria 
773a7ff8297SAmit Kucheria 		return 0;
77453e2a20eSAnsuel Smith 	} while (time_before(jiffies, timeout));
77553e2a20eSAnsuel Smith 
77653e2a20eSAnsuel Smith 	return -ETIMEDOUT;
777a7ff8297SAmit Kucheria }
778a7ff8297SAmit Kucheria 
779a7ff8297SAmit Kucheria #ifdef CONFIG_DEBUG_FS
780a7ff8297SAmit Kucheria static int dbg_sensors_show(struct seq_file *s, void *data)
781a7ff8297SAmit Kucheria {
782a7ff8297SAmit Kucheria 	struct platform_device *pdev = s->private;
783a7ff8297SAmit Kucheria 	struct tsens_priv *priv = platform_get_drvdata(pdev);
784a7ff8297SAmit Kucheria 	int i;
785a7ff8297SAmit Kucheria 
786a7ff8297SAmit Kucheria 	seq_printf(s, "max: %2d\nnum: %2d\n\n",
787a7ff8297SAmit Kucheria 		   priv->feat->max_sensors, priv->num_sensors);
788a7ff8297SAmit Kucheria 
789a7ff8297SAmit Kucheria 	seq_puts(s, "      id    slope   offset\n--------------------------\n");
790a7ff8297SAmit Kucheria 	for (i = 0;  i < priv->num_sensors; i++) {
791a7ff8297SAmit Kucheria 		seq_printf(s, "%8d %8d %8d\n", priv->sensor[i].hw_id,
792a7ff8297SAmit Kucheria 			   priv->sensor[i].slope, priv->sensor[i].offset);
793a7ff8297SAmit Kucheria 	}
794a7ff8297SAmit Kucheria 
795a7ff8297SAmit Kucheria 	return 0;
796a7ff8297SAmit Kucheria }
797a7ff8297SAmit Kucheria 
798a7ff8297SAmit Kucheria static int dbg_version_show(struct seq_file *s, void *data)
799a7ff8297SAmit Kucheria {
800a7ff8297SAmit Kucheria 	struct platform_device *pdev = s->private;
801a7ff8297SAmit Kucheria 	struct tsens_priv *priv = platform_get_drvdata(pdev);
802a7ff8297SAmit Kucheria 	u32 maj_ver, min_ver, step_ver;
803a7ff8297SAmit Kucheria 	int ret;
804a7ff8297SAmit Kucheria 
805a7ff8297SAmit Kucheria 	if (tsens_version(priv) > VER_0_1) {
806a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[VER_MAJOR], &maj_ver);
807a7ff8297SAmit Kucheria 		if (ret)
808a7ff8297SAmit Kucheria 			return ret;
809a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[VER_MINOR], &min_ver);
810a7ff8297SAmit Kucheria 		if (ret)
811a7ff8297SAmit Kucheria 			return ret;
812a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[VER_STEP], &step_ver);
813a7ff8297SAmit Kucheria 		if (ret)
814a7ff8297SAmit Kucheria 			return ret;
815a7ff8297SAmit Kucheria 		seq_printf(s, "%d.%d.%d\n", maj_ver, min_ver, step_ver);
816a7ff8297SAmit Kucheria 	} else {
817c7e077e9SChristian Marangi 		seq_printf(s, "0.%d.0\n", priv->feat->ver_major);
818a7ff8297SAmit Kucheria 	}
819a7ff8297SAmit Kucheria 
820a7ff8297SAmit Kucheria 	return 0;
821a7ff8297SAmit Kucheria }
822a7ff8297SAmit Kucheria 
823a7ff8297SAmit Kucheria DEFINE_SHOW_ATTRIBUTE(dbg_version);
824a7ff8297SAmit Kucheria DEFINE_SHOW_ATTRIBUTE(dbg_sensors);
825a7ff8297SAmit Kucheria 
826a7ff8297SAmit Kucheria static void tsens_debug_init(struct platform_device *pdev)
827a7ff8297SAmit Kucheria {
828a7ff8297SAmit Kucheria 	struct tsens_priv *priv = platform_get_drvdata(pdev);
829a7ff8297SAmit Kucheria 
83089992d95SChristian Marangi 	priv->debug_root = debugfs_lookup("tsens", NULL);
83189992d95SChristian Marangi 	if (!priv->debug_root)
832a7ff8297SAmit Kucheria 		priv->debug_root = debugfs_create_dir("tsens", NULL);
833a7ff8297SAmit Kucheria 
834a7ff8297SAmit Kucheria 	/* A directory for each instance of the TSENS IP */
835a7ff8297SAmit Kucheria 	priv->debug = debugfs_create_dir(dev_name(&pdev->dev), priv->debug_root);
83689992d95SChristian Marangi 	debugfs_create_file("version", 0444, priv->debug, pdev, &dbg_version_fops);
837a7ff8297SAmit Kucheria 	debugfs_create_file("sensors", 0444, priv->debug, pdev, &dbg_sensors_fops);
838a7ff8297SAmit Kucheria }
839a7ff8297SAmit Kucheria #else
840a7ff8297SAmit Kucheria static inline void tsens_debug_init(struct platform_device *pdev) {}
841a7ff8297SAmit Kucheria #endif
842a7ff8297SAmit Kucheria 
843a7ff8297SAmit Kucheria static const struct regmap_config tsens_config = {
844a7ff8297SAmit Kucheria 	.name		= "tm",
845a7ff8297SAmit Kucheria 	.reg_bits	= 32,
846a7ff8297SAmit Kucheria 	.val_bits	= 32,
847a7ff8297SAmit Kucheria 	.reg_stride	= 4,
848a7ff8297SAmit Kucheria };
849a7ff8297SAmit Kucheria 
850a7ff8297SAmit Kucheria static const struct regmap_config tsens_srot_config = {
851a7ff8297SAmit Kucheria 	.name		= "srot",
852a7ff8297SAmit Kucheria 	.reg_bits	= 32,
853a7ff8297SAmit Kucheria 	.val_bits	= 32,
854a7ff8297SAmit Kucheria 	.reg_stride	= 4,
855a7ff8297SAmit Kucheria };
856a7ff8297SAmit Kucheria 
857a7ff8297SAmit Kucheria int __init init_common(struct tsens_priv *priv)
858a7ff8297SAmit Kucheria {
859a7ff8297SAmit Kucheria 	void __iomem *tm_base, *srot_base;
860a7ff8297SAmit Kucheria 	struct device *dev = priv->dev;
861a7ff8297SAmit Kucheria 	u32 ver_minor;
862a7ff8297SAmit Kucheria 	struct resource *res;
863a7ff8297SAmit Kucheria 	u32 enabled;
864a7ff8297SAmit Kucheria 	int ret, i, j;
865a7ff8297SAmit Kucheria 	struct platform_device *op = of_find_device_by_node(priv->dev->of_node);
866a7ff8297SAmit Kucheria 
867a7ff8297SAmit Kucheria 	if (!op)
868a7ff8297SAmit Kucheria 		return -EINVAL;
869a7ff8297SAmit Kucheria 
870a7ff8297SAmit Kucheria 	if (op->num_resources > 1) {
871a7ff8297SAmit Kucheria 		/* DT with separate SROT and TM address space */
872a7ff8297SAmit Kucheria 		priv->tm_offset = 0;
873a7ff8297SAmit Kucheria 		res = platform_get_resource(op, IORESOURCE_MEM, 1);
874a7ff8297SAmit Kucheria 		srot_base = devm_ioremap_resource(dev, res);
875a7ff8297SAmit Kucheria 		if (IS_ERR(srot_base)) {
876a7ff8297SAmit Kucheria 			ret = PTR_ERR(srot_base);
877a7ff8297SAmit Kucheria 			goto err_put_device;
878a7ff8297SAmit Kucheria 		}
879a7ff8297SAmit Kucheria 
880a7ff8297SAmit Kucheria 		priv->srot_map = devm_regmap_init_mmio(dev, srot_base,
881a7ff8297SAmit Kucheria 						       &tsens_srot_config);
882a7ff8297SAmit Kucheria 		if (IS_ERR(priv->srot_map)) {
883a7ff8297SAmit Kucheria 			ret = PTR_ERR(priv->srot_map);
884a7ff8297SAmit Kucheria 			goto err_put_device;
885a7ff8297SAmit Kucheria 		}
886a7ff8297SAmit Kucheria 	} else {
887a7ff8297SAmit Kucheria 		/* old DTs where SROT and TM were in a contiguous 2K block */
888a7ff8297SAmit Kucheria 		priv->tm_offset = 0x1000;
889a7ff8297SAmit Kucheria 	}
890a7ff8297SAmit Kucheria 
89153e2a20eSAnsuel Smith 	if (tsens_version(priv) >= VER_0_1) {
892a7ff8297SAmit Kucheria 		res = platform_get_resource(op, IORESOURCE_MEM, 0);
893a7ff8297SAmit Kucheria 		tm_base = devm_ioremap_resource(dev, res);
894a7ff8297SAmit Kucheria 		if (IS_ERR(tm_base)) {
895a7ff8297SAmit Kucheria 			ret = PTR_ERR(tm_base);
896a7ff8297SAmit Kucheria 			goto err_put_device;
897a7ff8297SAmit Kucheria 		}
898a7ff8297SAmit Kucheria 
899a7ff8297SAmit Kucheria 		priv->tm_map = devm_regmap_init_mmio(dev, tm_base, &tsens_config);
90053e2a20eSAnsuel Smith 	} else { /* VER_0 share the same gcc regs using a syscon */
90153e2a20eSAnsuel Smith 		struct device *parent = priv->dev->parent;
90253e2a20eSAnsuel Smith 
90353e2a20eSAnsuel Smith 		if (parent)
90453e2a20eSAnsuel Smith 			priv->tm_map = syscon_node_to_regmap(parent->of_node);
90553e2a20eSAnsuel Smith 	}
90653e2a20eSAnsuel Smith 
90753e2a20eSAnsuel Smith 	if (IS_ERR_OR_NULL(priv->tm_map)) {
90853e2a20eSAnsuel Smith 		if (!priv->tm_map)
90953e2a20eSAnsuel Smith 			ret = -ENODEV;
91053e2a20eSAnsuel Smith 		else
911a7ff8297SAmit Kucheria 			ret = PTR_ERR(priv->tm_map);
912a7ff8297SAmit Kucheria 		goto err_put_device;
913a7ff8297SAmit Kucheria 	}
914a7ff8297SAmit Kucheria 
91553e2a20eSAnsuel Smith 	/* VER_0 have only tm_map */
91653e2a20eSAnsuel Smith 	if (!priv->srot_map)
91753e2a20eSAnsuel Smith 		priv->srot_map = priv->tm_map;
91853e2a20eSAnsuel Smith 
919a7ff8297SAmit Kucheria 	if (tsens_version(priv) > VER_0_1) {
920a7ff8297SAmit Kucheria 		for (i = VER_MAJOR; i <= VER_STEP; i++) {
921a7ff8297SAmit Kucheria 			priv->rf[i] = devm_regmap_field_alloc(dev, priv->srot_map,
922a7ff8297SAmit Kucheria 							      priv->fields[i]);
923f4136863SGuangqing Zhu 			if (IS_ERR(priv->rf[i])) {
924f4136863SGuangqing Zhu 				ret = PTR_ERR(priv->rf[i]);
925f4136863SGuangqing Zhu 				goto err_put_device;
926f4136863SGuangqing Zhu 			}
927a7ff8297SAmit Kucheria 		}
928a7ff8297SAmit Kucheria 		ret = regmap_field_read(priv->rf[VER_MINOR], &ver_minor);
929a7ff8297SAmit Kucheria 		if (ret)
930a7ff8297SAmit Kucheria 			goto err_put_device;
931a7ff8297SAmit Kucheria 	}
932a7ff8297SAmit Kucheria 
933a7ff8297SAmit Kucheria 	priv->rf[TSENS_EN] = devm_regmap_field_alloc(dev, priv->srot_map,
934a7ff8297SAmit Kucheria 						     priv->fields[TSENS_EN]);
935a7ff8297SAmit Kucheria 	if (IS_ERR(priv->rf[TSENS_EN])) {
936a7ff8297SAmit Kucheria 		ret = PTR_ERR(priv->rf[TSENS_EN]);
937a7ff8297SAmit Kucheria 		goto err_put_device;
938a7ff8297SAmit Kucheria 	}
93953e2a20eSAnsuel Smith 	/* in VER_0 TSENS need to be explicitly enabled */
94053e2a20eSAnsuel Smith 	if (tsens_version(priv) == VER_0)
94153e2a20eSAnsuel Smith 		regmap_field_write(priv->rf[TSENS_EN], 1);
94253e2a20eSAnsuel Smith 
943a7ff8297SAmit Kucheria 	ret = regmap_field_read(priv->rf[TSENS_EN], &enabled);
944a7ff8297SAmit Kucheria 	if (ret)
945a7ff8297SAmit Kucheria 		goto err_put_device;
946a7ff8297SAmit Kucheria 	if (!enabled) {
947a7ff8297SAmit Kucheria 		dev_err(dev, "%s: device not enabled\n", __func__);
948a7ff8297SAmit Kucheria 		ret = -ENODEV;
949a7ff8297SAmit Kucheria 		goto err_put_device;
950a7ff8297SAmit Kucheria 	}
951a7ff8297SAmit Kucheria 
952a7ff8297SAmit Kucheria 	priv->rf[SENSOR_EN] = devm_regmap_field_alloc(dev, priv->srot_map,
953a7ff8297SAmit Kucheria 						      priv->fields[SENSOR_EN]);
954a7ff8297SAmit Kucheria 	if (IS_ERR(priv->rf[SENSOR_EN])) {
955a7ff8297SAmit Kucheria 		ret = PTR_ERR(priv->rf[SENSOR_EN]);
956a7ff8297SAmit Kucheria 		goto err_put_device;
957a7ff8297SAmit Kucheria 	}
958a7ff8297SAmit Kucheria 	priv->rf[INT_EN] = devm_regmap_field_alloc(dev, priv->tm_map,
959a7ff8297SAmit Kucheria 						   priv->fields[INT_EN]);
960a7ff8297SAmit Kucheria 	if (IS_ERR(priv->rf[INT_EN])) {
961a7ff8297SAmit Kucheria 		ret = PTR_ERR(priv->rf[INT_EN]);
962a7ff8297SAmit Kucheria 		goto err_put_device;
963a7ff8297SAmit Kucheria 	}
964a7ff8297SAmit Kucheria 
96553e2a20eSAnsuel Smith 	priv->rf[TSENS_SW_RST] =
96653e2a20eSAnsuel Smith 		devm_regmap_field_alloc(dev, priv->srot_map, priv->fields[TSENS_SW_RST]);
96753e2a20eSAnsuel Smith 	if (IS_ERR(priv->rf[TSENS_SW_RST])) {
96853e2a20eSAnsuel Smith 		ret = PTR_ERR(priv->rf[TSENS_SW_RST]);
96953e2a20eSAnsuel Smith 		goto err_put_device;
97053e2a20eSAnsuel Smith 	}
97153e2a20eSAnsuel Smith 
97253e2a20eSAnsuel Smith 	priv->rf[TRDY] = devm_regmap_field_alloc(dev, priv->tm_map, priv->fields[TRDY]);
97353e2a20eSAnsuel Smith 	if (IS_ERR(priv->rf[TRDY])) {
97453e2a20eSAnsuel Smith 		ret = PTR_ERR(priv->rf[TRDY]);
97553e2a20eSAnsuel Smith 		goto err_put_device;
97653e2a20eSAnsuel Smith 	}
97753e2a20eSAnsuel Smith 
978a7ff8297SAmit Kucheria 	/* This loop might need changes if enum regfield_ids is reordered */
979a7ff8297SAmit Kucheria 	for (j = LAST_TEMP_0; j <= UP_THRESH_15; j += 16) {
980a7ff8297SAmit Kucheria 		for (i = 0; i < priv->feat->max_sensors; i++) {
981a7ff8297SAmit Kucheria 			int idx = j + i;
982a7ff8297SAmit Kucheria 
983a7ff8297SAmit Kucheria 			priv->rf[idx] = devm_regmap_field_alloc(dev,
984a7ff8297SAmit Kucheria 								priv->tm_map,
985a7ff8297SAmit Kucheria 								priv->fields[idx]);
986a7ff8297SAmit Kucheria 			if (IS_ERR(priv->rf[idx])) {
987a7ff8297SAmit Kucheria 				ret = PTR_ERR(priv->rf[idx]);
988a7ff8297SAmit Kucheria 				goto err_put_device;
989a7ff8297SAmit Kucheria 			}
990a7ff8297SAmit Kucheria 		}
991a7ff8297SAmit Kucheria 	}
992a7ff8297SAmit Kucheria 
99353e2a20eSAnsuel Smith 	if (priv->feat->crit_int || tsens_version(priv) < VER_0_1) {
994a7ff8297SAmit Kucheria 		/* Loop might need changes if enum regfield_ids is reordered */
995a7ff8297SAmit Kucheria 		for (j = CRITICAL_STATUS_0; j <= CRIT_THRESH_15; j += 16) {
996a7ff8297SAmit Kucheria 			for (i = 0; i < priv->feat->max_sensors; i++) {
997a7ff8297SAmit Kucheria 				int idx = j + i;
998a7ff8297SAmit Kucheria 
999a7ff8297SAmit Kucheria 				priv->rf[idx] =
1000a7ff8297SAmit Kucheria 					devm_regmap_field_alloc(dev,
1001a7ff8297SAmit Kucheria 								priv->tm_map,
1002a7ff8297SAmit Kucheria 								priv->fields[idx]);
1003a7ff8297SAmit Kucheria 				if (IS_ERR(priv->rf[idx])) {
1004a7ff8297SAmit Kucheria 					ret = PTR_ERR(priv->rf[idx]);
1005a7ff8297SAmit Kucheria 					goto err_put_device;
1006a7ff8297SAmit Kucheria 				}
1007a7ff8297SAmit Kucheria 			}
1008a7ff8297SAmit Kucheria 		}
1009a7ff8297SAmit Kucheria 	}
1010a7ff8297SAmit Kucheria 
1011a7ff8297SAmit Kucheria 	if (tsens_version(priv) > VER_1_X &&  ver_minor > 2) {
1012a7ff8297SAmit Kucheria 		/* Watchdog is present only on v2.3+ */
1013a7ff8297SAmit Kucheria 		priv->feat->has_watchdog = 1;
1014a7ff8297SAmit Kucheria 		for (i = WDOG_BARK_STATUS; i <= CC_MON_MASK; i++) {
1015a7ff8297SAmit Kucheria 			priv->rf[i] = devm_regmap_field_alloc(dev, priv->tm_map,
1016a7ff8297SAmit Kucheria 							      priv->fields[i]);
1017a7ff8297SAmit Kucheria 			if (IS_ERR(priv->rf[i])) {
1018a7ff8297SAmit Kucheria 				ret = PTR_ERR(priv->rf[i]);
1019a7ff8297SAmit Kucheria 				goto err_put_device;
1020a7ff8297SAmit Kucheria 			}
1021a7ff8297SAmit Kucheria 		}
1022a7ff8297SAmit Kucheria 		/*
1023a7ff8297SAmit Kucheria 		 * Watchdog is already enabled, unmask the bark.
1024a7ff8297SAmit Kucheria 		 * Disable cycle completion monitoring
1025a7ff8297SAmit Kucheria 		 */
1026a7ff8297SAmit Kucheria 		regmap_field_write(priv->rf[WDOG_BARK_MASK], 0);
1027a7ff8297SAmit Kucheria 		regmap_field_write(priv->rf[CC_MON_MASK], 1);
1028a7ff8297SAmit Kucheria 	}
1029a7ff8297SAmit Kucheria 
1030a7ff8297SAmit Kucheria 	spin_lock_init(&priv->ul_lock);
103153e2a20eSAnsuel Smith 
103253e2a20eSAnsuel Smith 	/* VER_0 interrupt doesn't need to be enabled */
103353e2a20eSAnsuel Smith 	if (tsens_version(priv) >= VER_0_1)
1034a7ff8297SAmit Kucheria 		tsens_enable_irq(priv);
103553e2a20eSAnsuel Smith 
1036a7ff8297SAmit Kucheria err_put_device:
1037a7ff8297SAmit Kucheria 	put_device(&op->dev);
1038a7ff8297SAmit Kucheria 	return ret;
1039a7ff8297SAmit Kucheria }
1040a7ff8297SAmit Kucheria 
1041ca1b9a9eSDaniel Lezcano static int tsens_get_temp(struct thermal_zone_device *tz, int *temp)
10429066073cSRajendra Nayak {
1043ca1b9a9eSDaniel Lezcano 	struct tsens_sensor *s = tz->devdata;
104469b628acSAmit Kucheria 	struct tsens_priv *priv = s->priv;
10459066073cSRajendra Nayak 
10468b71bce4SAmit Kucheria 	return priv->ops->get_temp(s, temp);
10479066073cSRajendra Nayak }
10489066073cSRajendra Nayak 
10495b97469aSArnd Bergmann static int  __maybe_unused tsens_suspend(struct device *dev)
10509066073cSRajendra Nayak {
105169b628acSAmit Kucheria 	struct tsens_priv *priv = dev_get_drvdata(dev);
10529066073cSRajendra Nayak 
105369b628acSAmit Kucheria 	if (priv->ops && priv->ops->suspend)
105469b628acSAmit Kucheria 		return priv->ops->suspend(priv);
10559066073cSRajendra Nayak 
10569066073cSRajendra Nayak 	return 0;
10579066073cSRajendra Nayak }
10589066073cSRajendra Nayak 
10595b97469aSArnd Bergmann static int __maybe_unused tsens_resume(struct device *dev)
10609066073cSRajendra Nayak {
106169b628acSAmit Kucheria 	struct tsens_priv *priv = dev_get_drvdata(dev);
10629066073cSRajendra Nayak 
106369b628acSAmit Kucheria 	if (priv->ops && priv->ops->resume)
106469b628acSAmit Kucheria 		return priv->ops->resume(priv);
10659066073cSRajendra Nayak 
10669066073cSRajendra Nayak 	return 0;
10679066073cSRajendra Nayak }
10689066073cSRajendra Nayak 
10699066073cSRajendra Nayak static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
10709066073cSRajendra Nayak 
10719066073cSRajendra Nayak static const struct of_device_id tsens_table[] = {
10729066073cSRajendra Nayak 	{
10736b3aeafbSAnsuel Smith 		.compatible = "qcom,ipq8064-tsens",
10746b3aeafbSAnsuel Smith 		.data = &data_8960,
10756b3aeafbSAnsuel Smith 	}, {
10766840455dSRobert Marko 		.compatible = "qcom,ipq8074-tsens",
10776840455dSRobert Marko 		.data = &data_ipq8074,
10786840455dSRobert Marko 	}, {
1079a2149ab8SKonrad Dybcio 		.compatible = "qcom,mdm9607-tsens",
1080a2149ab8SKonrad Dybcio 		.data = &data_9607,
1081a2149ab8SKonrad Dybcio 	}, {
10829066073cSRajendra Nayak 		.compatible = "qcom,msm8916-tsens",
1083840a5bd3SRajendra Nayak 		.data = &data_8916,
10849066073cSRajendra Nayak 	}, {
1085332bc8ebSShawn Guo 		.compatible = "qcom,msm8939-tsens",
1086332bc8ebSShawn Guo 		.data = &data_8939,
1087332bc8ebSShawn Guo 	}, {
1088a7d3006bSDmitry Baryshkov 		.compatible = "qcom,msm8956-tsens",
1089a7d3006bSDmitry Baryshkov 		.data = &data_8956,
1090a7d3006bSDmitry Baryshkov 	}, {
10912caf7396SDmitry Baryshkov 		.compatible = "qcom,msm8960-tsens",
10922caf7396SDmitry Baryshkov 		.data = &data_8960,
10932caf7396SDmitry Baryshkov 	}, {
10949066073cSRajendra Nayak 		.compatible = "qcom,msm8974-tsens",
10955e6703bdSRajendra Nayak 		.data = &data_8974,
1096d059c739SRajendra Nayak 	}, {
10970e580290SAngeloGioacchino Del Regno 		.compatible = "qcom,msm8976-tsens",
10980e580290SAngeloGioacchino Del Regno 		.data = &data_8976,
10990e580290SAngeloGioacchino Del Regno 	}, {
1100d059c739SRajendra Nayak 		.compatible = "qcom,msm8996-tsens",
1101d059c739SRajendra Nayak 		.data = &data_8996,
1102191dc74bSAmit Kucheria 	}, {
1103e8c24c6fSAmit Kucheria 		.compatible = "qcom,tsens-v1",
1104e8c24c6fSAmit Kucheria 		.data = &data_tsens_v1,
1105e8c24c6fSAmit Kucheria 	}, {
1106191dc74bSAmit Kucheria 		.compatible = "qcom,tsens-v2",
1107191dc74bSAmit Kucheria 		.data = &data_tsens_v2,
11089066073cSRajendra Nayak 	},
11099066073cSRajendra Nayak 	{}
11109066073cSRajendra Nayak };
11119066073cSRajendra Nayak MODULE_DEVICE_TABLE(of, tsens_table);
11129066073cSRajendra Nayak 
1113ca1b9a9eSDaniel Lezcano static const struct thermal_zone_device_ops tsens_of_ops = {
11149066073cSRajendra Nayak 	.get_temp = tsens_get_temp,
1115634e11d5SAmit Kucheria 	.set_trips = tsens_set_trips,
11169066073cSRajendra Nayak };
11179066073cSRajendra Nayak 
111879125e03SAmit Kucheria static int tsens_register_irq(struct tsens_priv *priv, char *irqname,
111979125e03SAmit Kucheria 			      irq_handler_t thread_fn)
112079125e03SAmit Kucheria {
112179125e03SAmit Kucheria 	struct platform_device *pdev;
112279125e03SAmit Kucheria 	int ret, irq;
112379125e03SAmit Kucheria 
112479125e03SAmit Kucheria 	pdev = of_find_device_by_node(priv->dev->of_node);
112579125e03SAmit Kucheria 	if (!pdev)
112679125e03SAmit Kucheria 		return -ENODEV;
112779125e03SAmit Kucheria 
112879125e03SAmit Kucheria 	irq = platform_get_irq_byname(pdev, irqname);
112979125e03SAmit Kucheria 	if (irq < 0) {
113079125e03SAmit Kucheria 		ret = irq;
113179125e03SAmit Kucheria 		/* For old DTs with no IRQ defined */
113279125e03SAmit Kucheria 		if (irq == -ENXIO)
113379125e03SAmit Kucheria 			ret = 0;
113479125e03SAmit Kucheria 	} else {
113553e2a20eSAnsuel Smith 		/* VER_0 interrupt is TRIGGER_RISING, VER_0_1 and up is ONESHOT */
113653e2a20eSAnsuel Smith 		if (tsens_version(priv) == VER_0)
113779125e03SAmit Kucheria 			ret = devm_request_threaded_irq(&pdev->dev, irq,
113853e2a20eSAnsuel Smith 							thread_fn, NULL,
113953e2a20eSAnsuel Smith 							IRQF_TRIGGER_RISING,
114053e2a20eSAnsuel Smith 							dev_name(&pdev->dev),
114153e2a20eSAnsuel Smith 							priv);
114253e2a20eSAnsuel Smith 		else
114353e2a20eSAnsuel Smith 			ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
114453e2a20eSAnsuel Smith 							thread_fn, IRQF_ONESHOT,
114553e2a20eSAnsuel Smith 							dev_name(&pdev->dev),
114653e2a20eSAnsuel Smith 							priv);
114753e2a20eSAnsuel Smith 
114879125e03SAmit Kucheria 		if (ret)
114979125e03SAmit Kucheria 			dev_err(&pdev->dev, "%s: failed to get irq\n",
115079125e03SAmit Kucheria 				__func__);
115179125e03SAmit Kucheria 		else
115279125e03SAmit Kucheria 			enable_irq_wake(irq);
115379125e03SAmit Kucheria 	}
115479125e03SAmit Kucheria 
115579125e03SAmit Kucheria 	put_device(&pdev->dev);
115679125e03SAmit Kucheria 	return ret;
115779125e03SAmit Kucheria }
115879125e03SAmit Kucheria 
115969b628acSAmit Kucheria static int tsens_register(struct tsens_priv *priv)
11609066073cSRajendra Nayak {
116179125e03SAmit Kucheria 	int i, ret;
11629066073cSRajendra Nayak 	struct thermal_zone_device *tzd;
11639066073cSRajendra Nayak 
116469b628acSAmit Kucheria 	for (i = 0;  i < priv->num_sensors; i++) {
116569b628acSAmit Kucheria 		priv->sensor[i].priv = priv;
1166ca1b9a9eSDaniel Lezcano 		tzd = devm_thermal_of_zone_register(priv->dev, priv->sensor[i].hw_id,
116769b628acSAmit Kucheria 						    &priv->sensor[i],
11689066073cSRajendra Nayak 						    &tsens_of_ops);
11699066073cSRajendra Nayak 		if (IS_ERR(tzd))
11709066073cSRajendra Nayak 			continue;
117169b628acSAmit Kucheria 		priv->sensor[i].tzd = tzd;
117269b628acSAmit Kucheria 		if (priv->ops->enable)
117369b628acSAmit Kucheria 			priv->ops->enable(priv, i);
11748556e19dSDmitry Baryshkov 
11758556e19dSDmitry Baryshkov 		if (devm_thermal_add_hwmon_sysfs(tzd))
11768556e19dSDmitry Baryshkov 			dev_warn(priv->dev,
11778556e19dSDmitry Baryshkov 				 "Failed to add hwmon sysfs attributes\n");
11789066073cSRajendra Nayak 	}
1179634e11d5SAmit Kucheria 
118053e2a20eSAnsuel Smith 	/* VER_0 require to set MIN and MAX THRESH
118153e2a20eSAnsuel Smith 	 * These 2 regs are set using the:
118253e2a20eSAnsuel Smith 	 * - CRIT_THRESH_0 for MAX THRESH hardcoded to 120°C
118353e2a20eSAnsuel Smith 	 * - CRIT_THRESH_1 for MIN THRESH hardcoded to   0°C
118453e2a20eSAnsuel Smith 	 */
118553e2a20eSAnsuel Smith 	if (tsens_version(priv) < VER_0_1) {
118653e2a20eSAnsuel Smith 		regmap_field_write(priv->rf[CRIT_THRESH_0],
118753e2a20eSAnsuel Smith 				   tsens_mC_to_hw(priv->sensor, 120000));
118853e2a20eSAnsuel Smith 
118953e2a20eSAnsuel Smith 		regmap_field_write(priv->rf[CRIT_THRESH_1],
119053e2a20eSAnsuel Smith 				   tsens_mC_to_hw(priv->sensor, 0));
119153e2a20eSAnsuel Smith 	}
119253e2a20eSAnsuel Smith 
11934360af35SRobert Marko 	if (priv->feat->combo_int) {
11944360af35SRobert Marko 		ret = tsens_register_irq(priv, "combined",
11954360af35SRobert Marko 					 tsens_combined_irq_thread);
11964360af35SRobert Marko 	} else {
119779125e03SAmit Kucheria 		ret = tsens_register_irq(priv, "uplow", tsens_irq_thread);
119879125e03SAmit Kucheria 		if (ret < 0)
119979125e03SAmit Kucheria 			return ret;
1200634e11d5SAmit Kucheria 
120179125e03SAmit Kucheria 		if (priv->feat->crit_int)
120279125e03SAmit Kucheria 			ret = tsens_register_irq(priv, "critical",
120379125e03SAmit Kucheria 						 tsens_critical_irq_thread);
12044360af35SRobert Marko 	}
1205634e11d5SAmit Kucheria 
1206634e11d5SAmit Kucheria 	return ret;
12079066073cSRajendra Nayak }
12089066073cSRajendra Nayak 
12099066073cSRajendra Nayak static int tsens_probe(struct platform_device *pdev)
12109066073cSRajendra Nayak {
12119066073cSRajendra Nayak 	int ret, i;
12129066073cSRajendra Nayak 	struct device *dev;
12139066073cSRajendra Nayak 	struct device_node *np;
121469b628acSAmit Kucheria 	struct tsens_priv *priv;
12153c040ce0SAmit Kucheria 	const struct tsens_plat_data *data;
12169066073cSRajendra Nayak 	const struct of_device_id *id;
12176d7c70d1SBjorn Andersson 	u32 num_sensors;
12189066073cSRajendra Nayak 
12199066073cSRajendra Nayak 	if (pdev->dev.of_node)
12209066073cSRajendra Nayak 		dev = &pdev->dev;
12219066073cSRajendra Nayak 	else
12229066073cSRajendra Nayak 		dev = pdev->dev.parent;
12239066073cSRajendra Nayak 
12249066073cSRajendra Nayak 	np = dev->of_node;
12259066073cSRajendra Nayak 
12269066073cSRajendra Nayak 	id = of_match_node(tsens_table, np);
122720d4fd84SRajendra Nayak 	if (id)
12289066073cSRajendra Nayak 		data = id->data;
122920d4fd84SRajendra Nayak 	else
123020d4fd84SRajendra Nayak 		data = &data_8960;
12319066073cSRajendra Nayak 
12326d7c70d1SBjorn Andersson 	num_sensors = data->num_sensors;
12336d7c70d1SBjorn Andersson 
12346d7c70d1SBjorn Andersson 	if (np)
12356d7c70d1SBjorn Andersson 		of_property_read_u32(np, "#qcom,sensors", &num_sensors);
12366d7c70d1SBjorn Andersson 
12376d7c70d1SBjorn Andersson 	if (num_sensors <= 0) {
12383795ad5eSAmit Kucheria 		dev_err(dev, "%s: invalid number of sensors\n", __func__);
12399066073cSRajendra Nayak 		return -EINVAL;
12409066073cSRajendra Nayak 	}
12419066073cSRajendra Nayak 
124269b628acSAmit Kucheria 	priv = devm_kzalloc(dev,
124369b628acSAmit Kucheria 			     struct_size(priv, sensor, num_sensors),
12440ed2dd03SKees Cook 			     GFP_KERNEL);
124569b628acSAmit Kucheria 	if (!priv)
12469066073cSRajendra Nayak 		return -ENOMEM;
12479066073cSRajendra Nayak 
124869b628acSAmit Kucheria 	priv->dev = dev;
124969b628acSAmit Kucheria 	priv->num_sensors = num_sensors;
125069b628acSAmit Kucheria 	priv->ops = data->ops;
125169b628acSAmit Kucheria 	for (i = 0;  i < priv->num_sensors; i++) {
12529066073cSRajendra Nayak 		if (data->hw_ids)
125369b628acSAmit Kucheria 			priv->sensor[i].hw_id = data->hw_ids[i];
12549066073cSRajendra Nayak 		else
125569b628acSAmit Kucheria 			priv->sensor[i].hw_id = i;
12569066073cSRajendra Nayak 	}
1257c1997054SAmit Kucheria 	priv->feat = data->feat;
1258c1997054SAmit Kucheria 	priv->fields = data->fields;
12599066073cSRajendra Nayak 
12600e9c0bc7SAmit Kucheria 	platform_set_drvdata(pdev, priv);
12610e9c0bc7SAmit Kucheria 
126269b628acSAmit Kucheria 	if (!priv->ops || !priv->ops->init || !priv->ops->get_temp)
12639066073cSRajendra Nayak 		return -EINVAL;
12649066073cSRajendra Nayak 
126569b628acSAmit Kucheria 	ret = priv->ops->init(priv);
12669066073cSRajendra Nayak 	if (ret < 0) {
12673795ad5eSAmit Kucheria 		dev_err(dev, "%s: init failed\n", __func__);
12689066073cSRajendra Nayak 		return ret;
12699066073cSRajendra Nayak 	}
12709066073cSRajendra Nayak 
127169b628acSAmit Kucheria 	if (priv->ops->calibrate) {
127269b628acSAmit Kucheria 		ret = priv->ops->calibrate(priv);
12739066073cSRajendra Nayak 		if (ret < 0) {
1274fc7d18cfSAmit Kucheria 			if (ret != -EPROBE_DEFER)
12753795ad5eSAmit Kucheria 				dev_err(dev, "%s: calibration failed\n", __func__);
12769066073cSRajendra Nayak 			return ret;
12779066073cSRajendra Nayak 		}
12789066073cSRajendra Nayak 	}
12799066073cSRajendra Nayak 
1280de48d876SChristian Marangi 	ret = tsens_register(priv);
1281de48d876SChristian Marangi 	if (!ret)
1282de48d876SChristian Marangi 		tsens_debug_init(pdev);
1283de48d876SChristian Marangi 
1284de48d876SChristian Marangi 	return ret;
12859066073cSRajendra Nayak }
12869066073cSRajendra Nayak 
12879066073cSRajendra Nayak static int tsens_remove(struct platform_device *pdev)
12889066073cSRajendra Nayak {
128969b628acSAmit Kucheria 	struct tsens_priv *priv = platform_get_drvdata(pdev);
12909066073cSRajendra Nayak 
12917c938f48SAmit Kucheria 	debugfs_remove_recursive(priv->debug_root);
1292634e11d5SAmit Kucheria 	tsens_disable_irq(priv);
129369b628acSAmit Kucheria 	if (priv->ops->disable)
129469b628acSAmit Kucheria 		priv->ops->disable(priv);
12959066073cSRajendra Nayak 
12969066073cSRajendra Nayak 	return 0;
12979066073cSRajendra Nayak }
12989066073cSRajendra Nayak 
12999066073cSRajendra Nayak static struct platform_driver tsens_driver = {
13009066073cSRajendra Nayak 	.probe = tsens_probe,
13019066073cSRajendra Nayak 	.remove = tsens_remove,
13029066073cSRajendra Nayak 	.driver = {
13039066073cSRajendra Nayak 		.name = "qcom-tsens",
13049066073cSRajendra Nayak 		.pm	= &tsens_pm_ops,
13059066073cSRajendra Nayak 		.of_match_table = tsens_table,
13069066073cSRajendra Nayak 	},
13079066073cSRajendra Nayak };
13089066073cSRajendra Nayak module_platform_driver(tsens_driver);
13099066073cSRajendra Nayak 
13109066073cSRajendra Nayak MODULE_LICENSE("GPL v2");
13119066073cSRajendra Nayak MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
13129066073cSRajendra Nayak MODULE_ALIAS("platform:qcom-tsens");
1313