aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaesar Wang <wxt@rock-chips.com>2016-12-12 06:05:33 -0500
committerEduardo Valentin <edubezval@gmail.com>2016-12-13 23:32:03 -0500
commitd3530497f5c33530c50acb435b7d54e0a82d8032 (patch)
treef3729d4381986fcfd3fd14528f780e105fb7d4cc
parentcdd8b3f7b779e39bda1a8057f287da065216720b (diff)
thermal: rockchip: fixes invalid temperature case
The temp_to_code function will return 0 when we set the temperature to a invalid value (e.g. 61C, 62C, 63C....), that's unpractical. This patch will prevent this case happening. That will return the max analog value to indicate the temperature is invalid or over table temperature range. Signed-off-by: Caesar Wang <wxt@rock-chips.com> Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
-rw-r--r--drivers/thermal/rockchip_thermal.c48
1 files changed, 28 insertions, 20 deletions
diff --git a/drivers/thermal/rockchip_thermal.c b/drivers/thermal/rockchip_thermal.c
index 415e0ced3dce..f027b86b993d 100644
--- a/drivers/thermal/rockchip_thermal.c
+++ b/drivers/thermal/rockchip_thermal.c
@@ -120,10 +120,10 @@ struct rockchip_tsadc_chip {
120 /* Per-sensor methods */ 120 /* Per-sensor methods */
121 int (*get_temp)(const struct chip_tsadc_table *table, 121 int (*get_temp)(const struct chip_tsadc_table *table,
122 int chn, void __iomem *reg, int *temp); 122 int chn, void __iomem *reg, int *temp);
123 void (*set_alarm_temp)(const struct chip_tsadc_table *table, 123 int (*set_alarm_temp)(const struct chip_tsadc_table *table,
124 int chn, void __iomem *reg, int temp); 124 int chn, void __iomem *reg, int temp);
125 void (*set_tshut_temp)(const struct chip_tsadc_table *table, 125 int (*set_tshut_temp)(const struct chip_tsadc_table *table,
126 int chn, void __iomem *reg, int temp); 126 int chn, void __iomem *reg, int temp);
127 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m); 127 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
128 128
129 /* Per-table methods */ 129 /* Per-table methods */
@@ -401,17 +401,15 @@ static u32 rk_tsadcv2_temp_to_code(const struct chip_tsadc_table *table,
401 int temp) 401 int temp)
402{ 402{
403 int high, low, mid; 403 int high, low, mid;
404 u32 error = 0; 404 u32 error = table->data_mask;
405 405
406 low = 0; 406 low = 0;
407 high = table->length - 1; 407 high = table->length - 1;
408 mid = (high + low) / 2; 408 mid = (high + low) / 2;
409 409
410 /* Return mask code data when the temp is over table range */ 410 /* Return mask code data when the temp is over table range */
411 if (temp < table->id[low].temp || temp > table->id[high].temp) { 411 if (temp < table->id[low].temp || temp > table->id[high].temp)
412 error = table->data_mask;
413 goto exit; 412 goto exit;
414 }
415 413
416 while (low <= high) { 414 while (low <= high) {
417 if (temp == table->id[mid].temp) 415 if (temp == table->id[mid].temp)
@@ -650,15 +648,15 @@ static int rk_tsadcv2_get_temp(const struct chip_tsadc_table *table,
650 return rk_tsadcv2_code_to_temp(table, val, temp); 648 return rk_tsadcv2_code_to_temp(table, val, temp);
651} 649}
652 650
653static void rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table, 651static int rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table,
654 int chn, void __iomem *regs, int temp) 652 int chn, void __iomem *regs, int temp)
655{ 653{
656 u32 alarm_value, int_en; 654 u32 alarm_value, int_en;
657 655
658 /* Make sure the value is valid */ 656 /* Make sure the value is valid */
659 alarm_value = rk_tsadcv2_temp_to_code(table, temp); 657 alarm_value = rk_tsadcv2_temp_to_code(table, temp);
660 if (alarm_value == table->data_mask) 658 if (alarm_value == table->data_mask)
661 return; 659 return -ERANGE;
662 660
663 writel_relaxed(alarm_value & table->data_mask, 661 writel_relaxed(alarm_value & table->data_mask,
664 regs + TSADCV2_COMP_INT(chn)); 662 regs + TSADCV2_COMP_INT(chn));
@@ -666,23 +664,27 @@ static void rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table,
666 int_en = readl_relaxed(regs + TSADCV2_INT_EN); 664 int_en = readl_relaxed(regs + TSADCV2_INT_EN);
667 int_en |= TSADCV2_INT_SRC_EN(chn); 665 int_en |= TSADCV2_INT_SRC_EN(chn);
668 writel_relaxed(int_en, regs + TSADCV2_INT_EN); 666 writel_relaxed(int_en, regs + TSADCV2_INT_EN);
667
668 return 0;
669} 669}
670 670
671static void rk_tsadcv2_tshut_temp(const struct chip_tsadc_table *table, 671static int rk_tsadcv2_tshut_temp(const struct chip_tsadc_table *table,
672 int chn, void __iomem *regs, int temp) 672 int chn, void __iomem *regs, int temp)
673{ 673{
674 u32 tshut_value, val; 674 u32 tshut_value, val;
675 675
676 /* Make sure the value is valid */ 676 /* Make sure the value is valid */
677 tshut_value = rk_tsadcv2_temp_to_code(table, temp); 677 tshut_value = rk_tsadcv2_temp_to_code(table, temp);
678 if (tshut_value == table->data_mask) 678 if (tshut_value == table->data_mask)
679 return; 679 return -ERANGE;
680 680
681 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn)); 681 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
682 682
683 /* TSHUT will be valid */ 683 /* TSHUT will be valid */
684 val = readl_relaxed(regs + TSADCV2_AUTO_CON); 684 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
685 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON); 685 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
686
687 return 0;
686} 688}
687 689
688static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs, 690static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
@@ -885,10 +887,8 @@ static int rockchip_thermal_set_trips(void *_sensor, int low, int high)
885 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n", 887 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
886 __func__, sensor->id, low, high); 888 __func__, sensor->id, low, high);
887 889
888 tsadc->set_alarm_temp(&tsadc->table, 890 return tsadc->set_alarm_temp(&tsadc->table,
889 sensor->id, thermal->regs, high); 891 sensor->id, thermal->regs, high);
890
891 return 0;
892} 892}
893 893
894static int rockchip_thermal_get_temp(void *_sensor, int *out_temp) 894static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
@@ -984,8 +984,12 @@ rockchip_thermal_register_sensor(struct platform_device *pdev,
984 int error; 984 int error;
985 985
986 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode); 986 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
987 tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs, 987
988 error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs,
988 thermal->tshut_temp); 989 thermal->tshut_temp);
990 if (error)
991 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
992 __func__, thermal->tshut_temp, error);
989 993
990 sensor->thermal = thermal; 994 sensor->thermal = thermal;
991 sensor->id = id; 995 sensor->id = id;
@@ -1198,9 +1202,13 @@ static int __maybe_unused rockchip_thermal_resume(struct device *dev)
1198 1202
1199 thermal->chip->set_tshut_mode(id, thermal->regs, 1203 thermal->chip->set_tshut_mode(id, thermal->regs,
1200 thermal->tshut_mode); 1204 thermal->tshut_mode);
1201 thermal->chip->set_tshut_temp(&thermal->chip->table, 1205
1206 error = thermal->chip->set_tshut_temp(&thermal->chip->table,
1202 id, thermal->regs, 1207 id, thermal->regs,
1203 thermal->tshut_temp); 1208 thermal->tshut_temp);
1209 if (error)
1210 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
1211 __func__, thermal->tshut_temp, error);
1204 } 1212 }
1205 1213
1206 thermal->chip->control(thermal->regs, true); 1214 thermal->chip->control(thermal->regs, true);