1414#include <linux/property.h>
1515#include <linux/thermal.h>
1616#include <linux/units.h>
17+
1718#include "thermal_hwmon.h"
1819
19- #define LOONGSON2_MAX_SENSOR_SEL_NUM 3
20+ #define LOONGSON2_MAX_SENSOR_SEL_NUM 3
2021
21- #define LOONGSON2_THSENS_CTRL_HI_REG 0x0
22- #define LOONGSON2_THSENS_CTRL_LOW_REG 0x8
23- #define LOONGSON2_THSENS_STATUS_REG 0x10
24- #define LOONGSON2_THSENS_OUT_REG 0x14
22+ #define LOONGSON2_THSENS_CTRL_HI_REG 0x0
23+ #define LOONGSON2_THSENS_CTRL_LOW_REG 0x8
24+ #define LOONGSON2_THSENS_STATUS_REG 0x10
25+ #define LOONGSON2_THSENS_OUT_REG 0x14
2526
26- #define LOONGSON2_THSENS_INT_LO BIT(0)
27- #define LOONGSON2_THSENS_INT_HIGH BIT(1)
28- #define LOONGSON2_THSENS_OUT_MASK 0xFF
27+ #define LOONGSON2_THSENS_INT_LO BIT(0)
28+ #define LOONGSON2_THSENS_INT_HIGH BIT(1)
29+ #define LOONGSON2_THSENS_INT_EN (LOONGSON2_THSENS_INT_LO | \
30+ LOONGSON2_THSENS_INT_HIGH)
31+ #define LOONGSON2_THSENS_OUT_MASK 0xFF
2932
3033struct loongson2_thermal_chip_data {
31- unsigned int thermal_sensor_sel ;
34+ unsigned int thermal_sensor_sel ;
3235};
3336
3437struct loongson2_thermal_data {
35- void __iomem * regs ;
38+ void __iomem * regs ;
3639 const struct loongson2_thermal_chip_data * chip_data ;
3740};
3841
39- static int loongson2_thermal_set (struct loongson2_thermal_data * data ,
40- int low , int high , bool enable )
42+ static void loongson2_set_ctrl_regs (struct loongson2_thermal_data * data ,
43+ int ctrl_data , bool low , bool enable )
4144{
42- u64 reg_ctrl = 0 ;
43- int reg_off = data -> chip_data -> thermal_sensor_sel * 2 ;
44-
45- low = clamp (-40 , low , high );
46- high = clamp (125 , low , high );
45+ int reg_ctrl = 0 ;
46+ int reg_off = data -> chip_data -> thermal_sensor_sel * 2 ;
47+ int ctrl_reg = low ? LOONGSON2_THSENS_CTRL_LOW_REG : LOONGSON2_THSENS_CTRL_HI_REG ;
4748
48- low += HECTO ;
49- high += HECTO ;
50-
51- reg_ctrl = low ;
49+ reg_ctrl = ctrl_data + HECTO ;
5250 reg_ctrl |= enable ? 0x100 : 0 ;
53- writew (reg_ctrl , data -> regs + LOONGSON2_THSENS_CTRL_LOW_REG + reg_off );
51+ writew (reg_ctrl , data -> regs + ctrl_reg + reg_off );
52+ }
5453
55- reg_ctrl = high ;
56- reg_ctrl |= enable ? 0x100 : 0 ;
57- writew (reg_ctrl , data -> regs + LOONGSON2_THSENS_CTRL_HI_REG + reg_off );
54+ static int loongson2_thermal_set (struct loongson2_thermal_data * data ,
55+ int low , int high , bool enable )
56+ {
57+ /* Set low temperature threshold */
58+ loongson2_set_ctrl_regs (data , clamp (-40 , low , high ), true, enable );
59+
60+ /* Set high temperature threshold */
61+ loongson2_set_ctrl_regs (data , clamp (125 , low , high ), false, enable );
5862
5963 return 0 ;
6064}
@@ -75,8 +79,7 @@ static irqreturn_t loongson2_thermal_irq_thread(int irq, void *dev)
7579 struct thermal_zone_device * tzd = dev ;
7680 struct loongson2_thermal_data * data = thermal_zone_device_priv (tzd );
7781
78- writeb (LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH , data -> regs +
79- LOONGSON2_THSENS_STATUS_REG );
82+ writeb (LOONGSON2_THSENS_INT_EN , data -> regs + LOONGSON2_THSENS_STATUS_REG );
8083
8184 thermal_zone_device_update (tzd , THERMAL_EVENT_UNSPECIFIED );
8285
@@ -116,14 +119,13 @@ static int loongson2_thermal_probe(struct platform_device *pdev)
116119 if (irq < 0 )
117120 return irq ;
118121
119- writeb (LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH , data -> regs +
120- LOONGSON2_THSENS_STATUS_REG );
122+ writeb (LOONGSON2_THSENS_INT_EN , data -> regs + LOONGSON2_THSENS_STATUS_REG );
121123
122124 loongson2_thermal_set (data , 0 , 0 , false);
123125
124126 for (i = 0 ; i <= LOONGSON2_MAX_SENSOR_SEL_NUM ; i ++ ) {
125127 tzd = devm_thermal_of_zone_register (dev , i , data ,
126- & loongson2_of_thermal_ops );
128+ & loongson2_of_thermal_ops );
127129
128130 if (!IS_ERR (tzd ))
129131 break ;
@@ -135,7 +137,7 @@ static int loongson2_thermal_probe(struct platform_device *pdev)
135137 }
136138
137139 ret = devm_request_threaded_irq (dev , irq , NULL , loongson2_thermal_irq_thread ,
138- IRQF_ONESHOT , "loongson2_thermal" , tzd );
140+ IRQF_ONESHOT , "loongson2_thermal" , tzd );
139141 if (ret < 0 )
140142 return dev_err_probe (dev , ret , "failed to request alarm irq\n" );
141143
@@ -167,4 +169,5 @@ static struct platform_driver loongson2_thermal_driver = {
167169module_platform_driver (loongson2_thermal_driver );
168170
169171MODULE_DESCRIPTION ("Loongson2 thermal driver" );
172+ MODULE_AUTHOR ("Loongson Technology Corporation Limited" );
170173MODULE_LICENSE ("GPL" );
0 commit comments