xref: /OK3568_Linux_fs/kernel/drivers/thermal/intel/intel_quark_dts_thermal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * intel_quark_dts_thermal.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
5*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright(c) 2015 Intel Corporation.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
12*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
13*4882a593Smuzhiyun  * published by the Free Software Foundation.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
16*4882a593Smuzhiyun  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18*4882a593Smuzhiyun  * General Public License for more details.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Contact Information:
21*4882a593Smuzhiyun  *  Ong Boon Leong <boon.leong.ong@intel.com>
22*4882a593Smuzhiyun  *  Intel Malaysia, Penang
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * BSD LICENSE
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Copyright(c) 2015 Intel Corporation.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
29*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
30*4882a593Smuzhiyun  * are met:
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  *   * Redistributions of source code must retain the above copyright
33*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer.
34*4882a593Smuzhiyun  *   * Redistributions in binary form must reproduce the above copyright
35*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer in
36*4882a593Smuzhiyun  *     the documentation and/or other materials provided with the
37*4882a593Smuzhiyun  *     distribution.
38*4882a593Smuzhiyun  *   * Neither the name of Intel Corporation nor the names of its
39*4882a593Smuzhiyun  *     contributors may be used to endorse or promote products derived
40*4882a593Smuzhiyun  *     from this software without specific prior written permission.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * Quark DTS thermal driver is implemented by referencing
55*4882a593Smuzhiyun  * intel_soc_dts_thermal.c.
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #include <linux/module.h>
61*4882a593Smuzhiyun #include <linux/slab.h>
62*4882a593Smuzhiyun #include <linux/interrupt.h>
63*4882a593Smuzhiyun #include <linux/thermal.h>
64*4882a593Smuzhiyun #include <asm/cpu_device_id.h>
65*4882a593Smuzhiyun #include <asm/iosf_mbi.h>
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* DTS reset is programmed via QRK_MBI_UNIT_SOC */
68*4882a593Smuzhiyun #define QRK_DTS_REG_OFFSET_RESET	0x34
69*4882a593Smuzhiyun #define QRK_DTS_RESET_BIT		BIT(0)
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /* DTS enable is programmed via QRK_MBI_UNIT_RMU */
72*4882a593Smuzhiyun #define QRK_DTS_REG_OFFSET_ENABLE	0xB0
73*4882a593Smuzhiyun #define QRK_DTS_ENABLE_BIT		BIT(15)
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /* Temperature Register is read via QRK_MBI_UNIT_RMU */
76*4882a593Smuzhiyun #define QRK_DTS_REG_OFFSET_TEMP		0xB1
77*4882a593Smuzhiyun #define QRK_DTS_MASK_TEMP		0xFF
78*4882a593Smuzhiyun #define QRK_DTS_OFFSET_TEMP		0
79*4882a593Smuzhiyun #define QRK_DTS_OFFSET_REL_TEMP		16
80*4882a593Smuzhiyun #define QRK_DTS_TEMP_BASE		50
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* Programmable Trip Point Register is configured via QRK_MBI_UNIT_RMU */
83*4882a593Smuzhiyun #define QRK_DTS_REG_OFFSET_PTPS		0xB2
84*4882a593Smuzhiyun #define QRK_DTS_MASK_TP_THRES		0xFF
85*4882a593Smuzhiyun #define QRK_DTS_SHIFT_TP		8
86*4882a593Smuzhiyun #define QRK_DTS_ID_TP_CRITICAL		0
87*4882a593Smuzhiyun #define QRK_DTS_SAFE_TP_THRES		105
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /* Thermal Sensor Register Lock */
90*4882a593Smuzhiyun #define QRK_DTS_REG_OFFSET_LOCK		0x71
91*4882a593Smuzhiyun #define QRK_DTS_LOCK_BIT		BIT(5)
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /* Quark DTS has 2 trip points: hot & catastrophic */
94*4882a593Smuzhiyun #define QRK_MAX_DTS_TRIPS	2
95*4882a593Smuzhiyun /* If DTS not locked, all trip points are configurable */
96*4882a593Smuzhiyun #define QRK_DTS_WR_MASK_SET	0x3
97*4882a593Smuzhiyun /* If DTS locked, all trip points are not configurable */
98*4882a593Smuzhiyun #define QRK_DTS_WR_MASK_CLR	0
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun #define DEFAULT_POLL_DELAY	2000
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun struct soc_sensor_entry {
103*4882a593Smuzhiyun 	bool locked;
104*4882a593Smuzhiyun 	u32 store_ptps;
105*4882a593Smuzhiyun 	u32 store_dts_enable;
106*4882a593Smuzhiyun 	struct thermal_zone_device *tzone;
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun static struct soc_sensor_entry *soc_dts;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun static int polling_delay = DEFAULT_POLL_DELAY;
112*4882a593Smuzhiyun module_param(polling_delay, int, 0644);
113*4882a593Smuzhiyun MODULE_PARM_DESC(polling_delay,
114*4882a593Smuzhiyun 	"Polling interval for checking trip points (in milliseconds)");
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun static DEFINE_MUTEX(dts_update_mutex);
117*4882a593Smuzhiyun 
soc_dts_enable(struct thermal_zone_device * tzd)118*4882a593Smuzhiyun static int soc_dts_enable(struct thermal_zone_device *tzd)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	u32 out;
121*4882a593Smuzhiyun 	struct soc_sensor_entry *aux_entry = tzd->devdata;
122*4882a593Smuzhiyun 	int ret;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	ret = iosf_mbi_read(QRK_MBI_UNIT_RMU, MBI_REG_READ,
125*4882a593Smuzhiyun 			    QRK_DTS_REG_OFFSET_ENABLE, &out);
126*4882a593Smuzhiyun 	if (ret)
127*4882a593Smuzhiyun 		return ret;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	if (out & QRK_DTS_ENABLE_BIT)
130*4882a593Smuzhiyun 		return 0;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (!aux_entry->locked) {
133*4882a593Smuzhiyun 		out |= QRK_DTS_ENABLE_BIT;
134*4882a593Smuzhiyun 		ret = iosf_mbi_write(QRK_MBI_UNIT_RMU, MBI_REG_WRITE,
135*4882a593Smuzhiyun 				     QRK_DTS_REG_OFFSET_ENABLE, out);
136*4882a593Smuzhiyun 		if (ret)
137*4882a593Smuzhiyun 			return ret;
138*4882a593Smuzhiyun 	} else {
139*4882a593Smuzhiyun 		pr_info("DTS is locked. Cannot enable DTS\n");
140*4882a593Smuzhiyun 		ret = -EPERM;
141*4882a593Smuzhiyun 	}
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	return ret;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
soc_dts_disable(struct thermal_zone_device * tzd)146*4882a593Smuzhiyun static int soc_dts_disable(struct thermal_zone_device *tzd)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	u32 out;
149*4882a593Smuzhiyun 	struct soc_sensor_entry *aux_entry = tzd->devdata;
150*4882a593Smuzhiyun 	int ret;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	ret = iosf_mbi_read(QRK_MBI_UNIT_RMU, MBI_REG_READ,
153*4882a593Smuzhiyun 			    QRK_DTS_REG_OFFSET_ENABLE, &out);
154*4882a593Smuzhiyun 	if (ret)
155*4882a593Smuzhiyun 		return ret;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	if (!(out & QRK_DTS_ENABLE_BIT))
158*4882a593Smuzhiyun 		return 0;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	if (!aux_entry->locked) {
161*4882a593Smuzhiyun 		out &= ~QRK_DTS_ENABLE_BIT;
162*4882a593Smuzhiyun 		ret = iosf_mbi_write(QRK_MBI_UNIT_RMU, MBI_REG_WRITE,
163*4882a593Smuzhiyun 				     QRK_DTS_REG_OFFSET_ENABLE, out);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 		if (ret)
166*4882a593Smuzhiyun 			return ret;
167*4882a593Smuzhiyun 	} else {
168*4882a593Smuzhiyun 		pr_info("DTS is locked. Cannot disable DTS\n");
169*4882a593Smuzhiyun 		ret = -EPERM;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	return ret;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
_get_trip_temp(int trip,int * temp)175*4882a593Smuzhiyun static int _get_trip_temp(int trip, int *temp)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	int status;
178*4882a593Smuzhiyun 	u32 out;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	mutex_lock(&dts_update_mutex);
181*4882a593Smuzhiyun 	status = iosf_mbi_read(QRK_MBI_UNIT_RMU, MBI_REG_READ,
182*4882a593Smuzhiyun 			       QRK_DTS_REG_OFFSET_PTPS, &out);
183*4882a593Smuzhiyun 	mutex_unlock(&dts_update_mutex);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (status)
186*4882a593Smuzhiyun 		return status;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/*
189*4882a593Smuzhiyun 	 * Thermal Sensor Programmable Trip Point Register has 8-bit
190*4882a593Smuzhiyun 	 * fields for critical (catastrophic) and hot set trip point
191*4882a593Smuzhiyun 	 * thresholds. The threshold value is always offset by its
192*4882a593Smuzhiyun 	 * temperature base (50 degree Celsius).
193*4882a593Smuzhiyun 	 */
194*4882a593Smuzhiyun 	*temp = (out >> (trip * QRK_DTS_SHIFT_TP)) & QRK_DTS_MASK_TP_THRES;
195*4882a593Smuzhiyun 	*temp -= QRK_DTS_TEMP_BASE;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
sys_get_trip_temp(struct thermal_zone_device * tzd,int trip,int * temp)200*4882a593Smuzhiyun static inline int sys_get_trip_temp(struct thermal_zone_device *tzd,
201*4882a593Smuzhiyun 				int trip, int *temp)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	return _get_trip_temp(trip, temp);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
sys_get_crit_temp(struct thermal_zone_device * tzd,int * temp)206*4882a593Smuzhiyun static inline int sys_get_crit_temp(struct thermal_zone_device *tzd, int *temp)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	return _get_trip_temp(QRK_DTS_ID_TP_CRITICAL, temp);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
update_trip_temp(struct soc_sensor_entry * aux_entry,int trip,int temp)211*4882a593Smuzhiyun static int update_trip_temp(struct soc_sensor_entry *aux_entry,
212*4882a593Smuzhiyun 				int trip, int temp)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	u32 out;
215*4882a593Smuzhiyun 	u32 temp_out;
216*4882a593Smuzhiyun 	u32 store_ptps;
217*4882a593Smuzhiyun 	int ret;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	mutex_lock(&dts_update_mutex);
220*4882a593Smuzhiyun 	if (aux_entry->locked) {
221*4882a593Smuzhiyun 		ret = -EPERM;
222*4882a593Smuzhiyun 		goto failed;
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	ret = iosf_mbi_read(QRK_MBI_UNIT_RMU, MBI_REG_READ,
226*4882a593Smuzhiyun 			    QRK_DTS_REG_OFFSET_PTPS, &store_ptps);
227*4882a593Smuzhiyun 	if (ret)
228*4882a593Smuzhiyun 		goto failed;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/*
231*4882a593Smuzhiyun 	 * Protection against unsafe trip point thresdhold value.
232*4882a593Smuzhiyun 	 * As Quark X1000 data-sheet does not provide any recommendation
233*4882a593Smuzhiyun 	 * regarding the safe trip point threshold value to use, we choose
234*4882a593Smuzhiyun 	 * the safe value according to the threshold value set by UEFI BIOS.
235*4882a593Smuzhiyun 	 */
236*4882a593Smuzhiyun 	if (temp > QRK_DTS_SAFE_TP_THRES)
237*4882a593Smuzhiyun 		temp = QRK_DTS_SAFE_TP_THRES;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	/*
240*4882a593Smuzhiyun 	 * Thermal Sensor Programmable Trip Point Register has 8-bit
241*4882a593Smuzhiyun 	 * fields for critical (catastrophic) and hot set trip point
242*4882a593Smuzhiyun 	 * thresholds. The threshold value is always offset by its
243*4882a593Smuzhiyun 	 * temperature base (50 degree Celsius).
244*4882a593Smuzhiyun 	 */
245*4882a593Smuzhiyun 	temp_out = temp + QRK_DTS_TEMP_BASE;
246*4882a593Smuzhiyun 	out = (store_ptps & ~(QRK_DTS_MASK_TP_THRES <<
247*4882a593Smuzhiyun 		(trip * QRK_DTS_SHIFT_TP)));
248*4882a593Smuzhiyun 	out |= (temp_out & QRK_DTS_MASK_TP_THRES) <<
249*4882a593Smuzhiyun 		(trip * QRK_DTS_SHIFT_TP);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	ret = iosf_mbi_write(QRK_MBI_UNIT_RMU, MBI_REG_WRITE,
252*4882a593Smuzhiyun 			     QRK_DTS_REG_OFFSET_PTPS, out);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun failed:
255*4882a593Smuzhiyun 	mutex_unlock(&dts_update_mutex);
256*4882a593Smuzhiyun 	return ret;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
sys_set_trip_temp(struct thermal_zone_device * tzd,int trip,int temp)259*4882a593Smuzhiyun static inline int sys_set_trip_temp(struct thermal_zone_device *tzd, int trip,
260*4882a593Smuzhiyun 				int temp)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	return update_trip_temp(tzd->devdata, trip, temp);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
sys_get_trip_type(struct thermal_zone_device * thermal,int trip,enum thermal_trip_type * type)265*4882a593Smuzhiyun static int sys_get_trip_type(struct thermal_zone_device *thermal,
266*4882a593Smuzhiyun 		int trip, enum thermal_trip_type *type)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	if (trip)
269*4882a593Smuzhiyun 		*type = THERMAL_TRIP_HOT;
270*4882a593Smuzhiyun 	else
271*4882a593Smuzhiyun 		*type = THERMAL_TRIP_CRITICAL;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
sys_get_curr_temp(struct thermal_zone_device * tzd,int * temp)276*4882a593Smuzhiyun static int sys_get_curr_temp(struct thermal_zone_device *tzd,
277*4882a593Smuzhiyun 				int *temp)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	u32 out;
280*4882a593Smuzhiyun 	int ret;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	mutex_lock(&dts_update_mutex);
283*4882a593Smuzhiyun 	ret = iosf_mbi_read(QRK_MBI_UNIT_RMU, MBI_REG_READ,
284*4882a593Smuzhiyun 			    QRK_DTS_REG_OFFSET_TEMP, &out);
285*4882a593Smuzhiyun 	mutex_unlock(&dts_update_mutex);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	if (ret)
288*4882a593Smuzhiyun 		return ret;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/*
291*4882a593Smuzhiyun 	 * Thermal Sensor Temperature Register has 8-bit field
292*4882a593Smuzhiyun 	 * for temperature value (offset by temperature base
293*4882a593Smuzhiyun 	 * 50 degree Celsius).
294*4882a593Smuzhiyun 	 */
295*4882a593Smuzhiyun 	out = (out >> QRK_DTS_OFFSET_TEMP) & QRK_DTS_MASK_TEMP;
296*4882a593Smuzhiyun 	*temp = out - QRK_DTS_TEMP_BASE;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	return 0;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
sys_change_mode(struct thermal_zone_device * tzd,enum thermal_device_mode mode)301*4882a593Smuzhiyun static int sys_change_mode(struct thermal_zone_device *tzd,
302*4882a593Smuzhiyun 			   enum thermal_device_mode mode)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	int ret;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	mutex_lock(&dts_update_mutex);
307*4882a593Smuzhiyun 	if (mode == THERMAL_DEVICE_ENABLED)
308*4882a593Smuzhiyun 		ret = soc_dts_enable(tzd);
309*4882a593Smuzhiyun 	else
310*4882a593Smuzhiyun 		ret = soc_dts_disable(tzd);
311*4882a593Smuzhiyun 	mutex_unlock(&dts_update_mutex);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	return ret;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun static struct thermal_zone_device_ops tzone_ops = {
317*4882a593Smuzhiyun 	.get_temp = sys_get_curr_temp,
318*4882a593Smuzhiyun 	.get_trip_temp = sys_get_trip_temp,
319*4882a593Smuzhiyun 	.get_trip_type = sys_get_trip_type,
320*4882a593Smuzhiyun 	.set_trip_temp = sys_set_trip_temp,
321*4882a593Smuzhiyun 	.get_crit_temp = sys_get_crit_temp,
322*4882a593Smuzhiyun 	.change_mode = sys_change_mode,
323*4882a593Smuzhiyun };
324*4882a593Smuzhiyun 
free_soc_dts(struct soc_sensor_entry * aux_entry)325*4882a593Smuzhiyun static void free_soc_dts(struct soc_sensor_entry *aux_entry)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	if (aux_entry) {
328*4882a593Smuzhiyun 		if (!aux_entry->locked) {
329*4882a593Smuzhiyun 			mutex_lock(&dts_update_mutex);
330*4882a593Smuzhiyun 			iosf_mbi_write(QRK_MBI_UNIT_RMU, MBI_REG_WRITE,
331*4882a593Smuzhiyun 				       QRK_DTS_REG_OFFSET_ENABLE,
332*4882a593Smuzhiyun 				       aux_entry->store_dts_enable);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 			iosf_mbi_write(QRK_MBI_UNIT_RMU, MBI_REG_WRITE,
335*4882a593Smuzhiyun 				       QRK_DTS_REG_OFFSET_PTPS,
336*4882a593Smuzhiyun 				       aux_entry->store_ptps);
337*4882a593Smuzhiyun 			mutex_unlock(&dts_update_mutex);
338*4882a593Smuzhiyun 		}
339*4882a593Smuzhiyun 		thermal_zone_device_unregister(aux_entry->tzone);
340*4882a593Smuzhiyun 		kfree(aux_entry);
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
alloc_soc_dts(void)344*4882a593Smuzhiyun static struct soc_sensor_entry *alloc_soc_dts(void)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	struct soc_sensor_entry *aux_entry;
347*4882a593Smuzhiyun 	int err;
348*4882a593Smuzhiyun 	u32 out;
349*4882a593Smuzhiyun 	int wr_mask;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	aux_entry = kzalloc(sizeof(*aux_entry), GFP_KERNEL);
352*4882a593Smuzhiyun 	if (!aux_entry) {
353*4882a593Smuzhiyun 		err = -ENOMEM;
354*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	/* Check if DTS register is locked */
358*4882a593Smuzhiyun 	err = iosf_mbi_read(QRK_MBI_UNIT_RMU, MBI_REG_READ,
359*4882a593Smuzhiyun 			    QRK_DTS_REG_OFFSET_LOCK, &out);
360*4882a593Smuzhiyun 	if (err)
361*4882a593Smuzhiyun 		goto err_ret;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	if (out & QRK_DTS_LOCK_BIT) {
364*4882a593Smuzhiyun 		aux_entry->locked = true;
365*4882a593Smuzhiyun 		wr_mask = QRK_DTS_WR_MASK_CLR;
366*4882a593Smuzhiyun 	} else {
367*4882a593Smuzhiyun 		aux_entry->locked = false;
368*4882a593Smuzhiyun 		wr_mask = QRK_DTS_WR_MASK_SET;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	/* Store DTS default state if DTS registers are not locked */
372*4882a593Smuzhiyun 	if (!aux_entry->locked) {
373*4882a593Smuzhiyun 		/* Store DTS default enable for restore on exit */
374*4882a593Smuzhiyun 		err = iosf_mbi_read(QRK_MBI_UNIT_RMU, MBI_REG_READ,
375*4882a593Smuzhiyun 				    QRK_DTS_REG_OFFSET_ENABLE,
376*4882a593Smuzhiyun 				    &aux_entry->store_dts_enable);
377*4882a593Smuzhiyun 		if (err)
378*4882a593Smuzhiyun 			goto err_ret;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 		/* Store DTS default PTPS register for restore on exit */
381*4882a593Smuzhiyun 		err = iosf_mbi_read(QRK_MBI_UNIT_RMU, MBI_REG_READ,
382*4882a593Smuzhiyun 				    QRK_DTS_REG_OFFSET_PTPS,
383*4882a593Smuzhiyun 				    &aux_entry->store_ptps);
384*4882a593Smuzhiyun 		if (err)
385*4882a593Smuzhiyun 			goto err_ret;
386*4882a593Smuzhiyun 	}
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	aux_entry->tzone = thermal_zone_device_register("quark_dts",
389*4882a593Smuzhiyun 			QRK_MAX_DTS_TRIPS,
390*4882a593Smuzhiyun 			wr_mask,
391*4882a593Smuzhiyun 			aux_entry, &tzone_ops, NULL, 0, polling_delay);
392*4882a593Smuzhiyun 	if (IS_ERR(aux_entry->tzone)) {
393*4882a593Smuzhiyun 		err = PTR_ERR(aux_entry->tzone);
394*4882a593Smuzhiyun 		goto err_ret;
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	err = thermal_zone_device_enable(aux_entry->tzone);
398*4882a593Smuzhiyun 	if (err)
399*4882a593Smuzhiyun 		goto err_aux_status;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	return aux_entry;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun err_aux_status:
404*4882a593Smuzhiyun 	thermal_zone_device_unregister(aux_entry->tzone);
405*4882a593Smuzhiyun err_ret:
406*4882a593Smuzhiyun 	kfree(aux_entry);
407*4882a593Smuzhiyun 	return ERR_PTR(err);
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun static const struct x86_cpu_id qrk_thermal_ids[] __initconst  = {
411*4882a593Smuzhiyun 	X86_MATCH_VENDOR_FAM_MODEL(INTEL, 5, INTEL_FAM5_QUARK_X1000, NULL),
412*4882a593Smuzhiyun 	{}
413*4882a593Smuzhiyun };
414*4882a593Smuzhiyun MODULE_DEVICE_TABLE(x86cpu, qrk_thermal_ids);
415*4882a593Smuzhiyun 
intel_quark_thermal_init(void)416*4882a593Smuzhiyun static int __init intel_quark_thermal_init(void)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun 	int err = 0;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	if (!x86_match_cpu(qrk_thermal_ids) || !iosf_mbi_available())
421*4882a593Smuzhiyun 		return -ENODEV;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	soc_dts = alloc_soc_dts();
424*4882a593Smuzhiyun 	if (IS_ERR(soc_dts)) {
425*4882a593Smuzhiyun 		err = PTR_ERR(soc_dts);
426*4882a593Smuzhiyun 		goto err_free;
427*4882a593Smuzhiyun 	}
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	return 0;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun err_free:
432*4882a593Smuzhiyun 	free_soc_dts(soc_dts);
433*4882a593Smuzhiyun 	return err;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
intel_quark_thermal_exit(void)436*4882a593Smuzhiyun static void __exit intel_quark_thermal_exit(void)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	free_soc_dts(soc_dts);
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun module_init(intel_quark_thermal_init)
442*4882a593Smuzhiyun module_exit(intel_quark_thermal_exit)
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun MODULE_DESCRIPTION("Intel Quark DTS Thermal Driver");
445*4882a593Smuzhiyun MODULE_AUTHOR("Ong Boon Leong <boon.leong.ong@intel.com>");
446*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
447