2 * intel_quark_dts_thermal.c
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * Copyright(c) 2015 Intel Corporation.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * Contact Information:
21 * Ong Boon Leong <boon.leong.ong@intel.com>
22 * Intel Malaysia, Penang
26 * Copyright(c) 2015 Intel Corporation.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
32 * * Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * * Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in
36 * the documentation and/or other materials provided with the
38 * * Neither the name of Intel Corporation nor the names of its
39 * contributors may be used to endorse or promote products derived
40 * from this software without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 * Quark DTS thermal driver is implemented by referencing
55 * intel_soc_dts_thermal.c.
58 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
60 #include <linux/module.h>
61 #include <linux/slab.h>
62 #include <linux/interrupt.h>
63 #include <linux/thermal.h>
64 #include <asm/cpu_device_id.h>
65 #include <asm/iosf_mbi.h>
67 /* DTS reset is programmed via QRK_MBI_UNIT_SOC */
68 #define QRK_DTS_REG_OFFSET_RESET 0x34
69 #define QRK_DTS_RESET_BIT BIT(0)
71 /* DTS enable is programmed via QRK_MBI_UNIT_RMU */
72 #define QRK_DTS_REG_OFFSET_ENABLE 0xB0
73 #define QRK_DTS_ENABLE_BIT BIT(15)
75 /* Temperature Register is read via QRK_MBI_UNIT_RMU */
76 #define QRK_DTS_REG_OFFSET_TEMP 0xB1
77 #define QRK_DTS_MASK_TEMP 0xFF
78 #define QRK_DTS_OFFSET_TEMP 0
79 #define QRK_DTS_OFFSET_REL_TEMP 16
80 #define QRK_DTS_TEMP_BASE 50
82 /* Programmable Trip Point Register is configured via QRK_MBI_UNIT_RMU */
83 #define QRK_DTS_REG_OFFSET_PTPS 0xB2
84 #define QRK_DTS_MASK_TP_THRES 0xFF
85 #define QRK_DTS_SHIFT_TP 8
86 #define QRK_DTS_ID_TP_CRITICAL 0
87 #define QRK_DTS_ID_TP_HOT 1
88 #define QRK_DTS_SAFE_TP_THRES 105
90 /* Thermal Sensor Register Lock */
91 #define QRK_DTS_REG_OFFSET_LOCK 0x71
92 #define QRK_DTS_LOCK_BIT BIT(5)
94 /* Quark DTS has 2 trip points: hot & catastrophic */
95 #define QRK_MAX_DTS_TRIPS 2
97 #define DEFAULT_POLL_DELAY 2000
99 struct soc_sensor_entry
{
102 u32 store_dts_enable
;
103 struct thermal_zone_device
*tzone
;
106 static struct soc_sensor_entry
*soc_dts
;
108 static int polling_delay
= DEFAULT_POLL_DELAY
;
109 module_param(polling_delay
, int, 0644);
110 MODULE_PARM_DESC(polling_delay
,
111 "Polling interval for checking trip points (in milliseconds)");
113 static DEFINE_MUTEX(dts_update_mutex
);
115 static int soc_dts_enable(struct thermal_zone_device
*tzd
)
118 struct soc_sensor_entry
*aux_entry
= thermal_zone_device_priv(tzd
);
121 ret
= iosf_mbi_read(QRK_MBI_UNIT_RMU
, MBI_REG_READ
,
122 QRK_DTS_REG_OFFSET_ENABLE
, &out
);
126 if (out
& QRK_DTS_ENABLE_BIT
)
129 if (!aux_entry
->locked
) {
130 out
|= QRK_DTS_ENABLE_BIT
;
131 ret
= iosf_mbi_write(QRK_MBI_UNIT_RMU
, MBI_REG_WRITE
,
132 QRK_DTS_REG_OFFSET_ENABLE
, out
);
136 pr_info("DTS is locked. Cannot enable DTS\n");
143 static int soc_dts_disable(struct thermal_zone_device
*tzd
)
146 struct soc_sensor_entry
*aux_entry
= thermal_zone_device_priv(tzd
);
149 ret
= iosf_mbi_read(QRK_MBI_UNIT_RMU
, MBI_REG_READ
,
150 QRK_DTS_REG_OFFSET_ENABLE
, &out
);
154 if (!(out
& QRK_DTS_ENABLE_BIT
))
157 if (!aux_entry
->locked
) {
158 out
&= ~QRK_DTS_ENABLE_BIT
;
159 ret
= iosf_mbi_write(QRK_MBI_UNIT_RMU
, MBI_REG_WRITE
,
160 QRK_DTS_REG_OFFSET_ENABLE
, out
);
165 pr_info("DTS is locked. Cannot disable DTS\n");
172 static int get_trip_temp(int trip
)
177 mutex_lock(&dts_update_mutex
);
178 status
= iosf_mbi_read(QRK_MBI_UNIT_RMU
, MBI_REG_READ
,
179 QRK_DTS_REG_OFFSET_PTPS
, &out
);
180 mutex_unlock(&dts_update_mutex
);
183 return THERMAL_TEMP_INVALID
;
186 * Thermal Sensor Programmable Trip Point Register has 8-bit
187 * fields for critical (catastrophic) and hot set trip point
188 * thresholds. The threshold value is always offset by its
189 * temperature base (50 degree Celsius).
191 temp
= (out
>> (trip
* QRK_DTS_SHIFT_TP
)) & QRK_DTS_MASK_TP_THRES
;
192 temp
-= QRK_DTS_TEMP_BASE
;
197 static int update_trip_temp(struct soc_sensor_entry
*aux_entry
,
198 int trip_index
, int temp
)
205 mutex_lock(&dts_update_mutex
);
206 if (aux_entry
->locked
) {
211 ret
= iosf_mbi_read(QRK_MBI_UNIT_RMU
, MBI_REG_READ
,
212 QRK_DTS_REG_OFFSET_PTPS
, &store_ptps
);
217 * Protection against unsafe trip point thresdhold value.
218 * As Quark X1000 data-sheet does not provide any recommendation
219 * regarding the safe trip point threshold value to use, we choose
220 * the safe value according to the threshold value set by UEFI BIOS.
222 if (temp
> QRK_DTS_SAFE_TP_THRES
)
223 temp
= QRK_DTS_SAFE_TP_THRES
;
226 * Thermal Sensor Programmable Trip Point Register has 8-bit
227 * fields for critical (catastrophic) and hot set trip point
228 * thresholds. The threshold value is always offset by its
229 * temperature base (50 degree Celsius).
231 temp_out
= temp
+ QRK_DTS_TEMP_BASE
;
232 out
= (store_ptps
& ~(QRK_DTS_MASK_TP_THRES
<<
233 (trip_index
* QRK_DTS_SHIFT_TP
)));
234 out
|= (temp_out
& QRK_DTS_MASK_TP_THRES
) <<
235 (trip_index
* QRK_DTS_SHIFT_TP
);
237 ret
= iosf_mbi_write(QRK_MBI_UNIT_RMU
, MBI_REG_WRITE
,
238 QRK_DTS_REG_OFFSET_PTPS
, out
);
241 mutex_unlock(&dts_update_mutex
);
245 static inline int sys_set_trip_temp(struct thermal_zone_device
*tzd
,
246 const struct thermal_trip
*trip
,
249 unsigned int trip_index
;
251 switch (trip
->type
) {
252 case THERMAL_TRIP_HOT
:
253 trip_index
= QRK_DTS_ID_TP_HOT
;
256 case THERMAL_TRIP_CRITICAL
:
257 trip_index
= QRK_DTS_ID_TP_CRITICAL
;
264 return update_trip_temp(thermal_zone_device_priv(tzd
), trip_index
, temp
);
267 static int sys_get_curr_temp(struct thermal_zone_device
*tzd
,
273 mutex_lock(&dts_update_mutex
);
274 ret
= iosf_mbi_read(QRK_MBI_UNIT_RMU
, MBI_REG_READ
,
275 QRK_DTS_REG_OFFSET_TEMP
, &out
);
276 mutex_unlock(&dts_update_mutex
);
282 * Thermal Sensor Temperature Register has 8-bit field
283 * for temperature value (offset by temperature base
284 * 50 degree Celsius).
286 out
= (out
>> QRK_DTS_OFFSET_TEMP
) & QRK_DTS_MASK_TEMP
;
287 *temp
= out
- QRK_DTS_TEMP_BASE
;
292 static int sys_change_mode(struct thermal_zone_device
*tzd
,
293 enum thermal_device_mode mode
)
297 mutex_lock(&dts_update_mutex
);
298 if (mode
== THERMAL_DEVICE_ENABLED
)
299 ret
= soc_dts_enable(tzd
);
301 ret
= soc_dts_disable(tzd
);
302 mutex_unlock(&dts_update_mutex
);
307 static const struct thermal_zone_device_ops tzone_ops
= {
308 .get_temp
= sys_get_curr_temp
,
309 .set_trip_temp
= sys_set_trip_temp
,
310 .change_mode
= sys_change_mode
,
313 static void free_soc_dts(struct soc_sensor_entry
*aux_entry
)
316 if (!aux_entry
->locked
) {
317 mutex_lock(&dts_update_mutex
);
318 iosf_mbi_write(QRK_MBI_UNIT_RMU
, MBI_REG_WRITE
,
319 QRK_DTS_REG_OFFSET_ENABLE
,
320 aux_entry
->store_dts_enable
);
322 iosf_mbi_write(QRK_MBI_UNIT_RMU
, MBI_REG_WRITE
,
323 QRK_DTS_REG_OFFSET_PTPS
,
324 aux_entry
->store_ptps
);
325 mutex_unlock(&dts_update_mutex
);
327 thermal_zone_device_unregister(aux_entry
->tzone
);
332 static struct soc_sensor_entry
*alloc_soc_dts(void)
334 struct thermal_trip trips
[QRK_MAX_DTS_TRIPS
] = { 0 };
335 struct soc_sensor_entry
*aux_entry
;
339 aux_entry
= kzalloc(sizeof(*aux_entry
), GFP_KERNEL
);
342 return ERR_PTR(-ENOMEM
);
345 /* Check if DTS register is locked */
346 err
= iosf_mbi_read(QRK_MBI_UNIT_RMU
, MBI_REG_READ
,
347 QRK_DTS_REG_OFFSET_LOCK
, &out
);
351 aux_entry
->locked
= !!(out
& QRK_DTS_LOCK_BIT
);
353 /* Store DTS default state if DTS registers are not locked */
354 if (!aux_entry
->locked
) {
355 /* Store DTS default enable for restore on exit */
356 err
= iosf_mbi_read(QRK_MBI_UNIT_RMU
, MBI_REG_READ
,
357 QRK_DTS_REG_OFFSET_ENABLE
,
358 &aux_entry
->store_dts_enable
);
362 /* Store DTS default PTPS register for restore on exit */
363 err
= iosf_mbi_read(QRK_MBI_UNIT_RMU
, MBI_REG_READ
,
364 QRK_DTS_REG_OFFSET_PTPS
,
365 &aux_entry
->store_ptps
);
369 trips
[QRK_DTS_ID_TP_CRITICAL
].flags
|= THERMAL_TRIP_FLAG_RW_TEMP
;
370 trips
[QRK_DTS_ID_TP_HOT
].flags
|= THERMAL_TRIP_FLAG_RW_TEMP
;
373 trips
[QRK_DTS_ID_TP_CRITICAL
].temperature
= get_trip_temp(QRK_DTS_ID_TP_CRITICAL
);
374 trips
[QRK_DTS_ID_TP_CRITICAL
].type
= THERMAL_TRIP_CRITICAL
;
376 trips
[QRK_DTS_ID_TP_HOT
].temperature
= get_trip_temp(QRK_DTS_ID_TP_HOT
);
377 trips
[QRK_DTS_ID_TP_HOT
].type
= THERMAL_TRIP_HOT
;
379 aux_entry
->tzone
= thermal_zone_device_register_with_trips("quark_dts",
384 NULL
, 0, polling_delay
);
385 if (IS_ERR(aux_entry
->tzone
)) {
386 err
= PTR_ERR(aux_entry
->tzone
);
390 err
= thermal_zone_device_enable(aux_entry
->tzone
);
397 thermal_zone_device_unregister(aux_entry
->tzone
);
403 static const struct x86_cpu_id qrk_thermal_ids
[] __initconst
= {
404 X86_MATCH_VFM(INTEL_QUARK_X1000
, NULL
),
407 MODULE_DEVICE_TABLE(x86cpu
, qrk_thermal_ids
);
409 static int __init
intel_quark_thermal_init(void)
411 if (!x86_match_cpu(qrk_thermal_ids
) || !iosf_mbi_available())
414 soc_dts
= alloc_soc_dts();
416 return PTR_ERR(soc_dts
);
421 static void __exit
intel_quark_thermal_exit(void)
423 free_soc_dts(soc_dts
);
426 module_init(intel_quark_thermal_init
)
427 module_exit(intel_quark_thermal_exit
)
429 MODULE_DESCRIPTION("Intel Quark DTS Thermal Driver");
430 MODULE_AUTHOR("Ong Boon Leong <boon.leong.ong@intel.com>");
431 MODULE_LICENSE("Dual BSD/GPL");