Lines Matching +full:thermal +full:- +full:sensor
2 * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd
3 * Caesar Wang <wxt@rock-chips.com>
26 #include <linux/thermal.h>
71 * Two sensors: CPU and GPU sensor.
76 * struct chip_tsadc_table - hold information about chip-specific differences
90 * struct rockchip_tsadc_chip - hold the private data of tsadc chip
91 * @chn_id[SOC_MAX_SENSORS]: the sensor id of chip correspond to the channel
93 * @tshut_temp: the hardware-controlled shutdown temperature value
94 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
95 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
100 * @set_tshut_temp: set the hardware-controlled shutdown temperature
101 * @set_tshut_mode: set the hardware-controlled shutdown mode
102 * @table: the chip-specific conversion table
105 /* The sensor id of chip correspond to the ADC channel */
109 /* The hardware-controlled tshut property */
114 /* Chip-wide methods */
120 /* Per-sensor methods */
129 /* Per-table methods */
134 * struct rockchip_thermal_sensor - hold the information of thermal sensor
135 * @thermal: pointer to the platform/configuration data
136 * @tzd: pointer to a thermal zone
137 * @id: identifier of the thermal sensor
140 struct rockchip_thermal_data *thermal; member
146 * struct rockchip_thermal_data - hold the private data of thermal driver
148 * @pdev: platform device of thermal
150 * @sensors[SOC_MAX_SENSORS]: the thermal sensor
155 * @tshut_temp: the hardware-controlled shutdown temperature value
156 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
157 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
178 * TSADC Sensor Register description:
231 * struct tsadc_table - code to temperature conversion table
235 * code to temperature mapping of the temperature sensor is a piece wise linear
246 {0, -40000},
247 {374, -40000},
248 {382, -35000},
249 {389, -30000},
250 {397, -25000},
251 {405, -20000},
252 {413, -15000},
253 {421, -10000},
254 {429, -5000},
285 {0, -40000},
286 {588, -40000},
287 {593, -35000},
288 {598, -30000},
289 {603, -25000},
290 {608, -20000},
291 {613, -15000},
292 {618, -10000},
293 {623, -5000},
324 {TSADCV2_DATA_MASK, -40000},
325 {3800, -40000},
326 {3792, -35000},
327 {3783, -30000},
328 {3774, -25000},
329 {3765, -20000},
330 {3756, -15000},
331 {3747, -10000},
332 {3737, -5000},
363 {0, -40000},
364 {296, -40000},
365 {304, -35000},
366 {313, -30000},
367 {331, -20000},
368 {340, -15000},
369 {349, -10000},
370 {359, -5000},
401 {0, -40000},
402 {106, -40000},
403 {108, -35000},
404 {110, -30000},
405 {112, -25000},
406 {114, -20000},
407 {116, -15000},
408 {118, -10000},
409 {120, -5000},
440 {0, -40000},
441 {402, -40000},
442 {410, -35000},
443 {419, -30000},
444 {427, -25000},
445 {436, -20000},
446 {444, -15000},
447 {453, -10000},
448 {461, -5000},
484 u32 error = table->data_mask; in rk_tsadcv2_temp_to_code()
487 high = (table->length - 1) - 1; /* ignore the last check for table */ in rk_tsadcv2_temp_to_code()
491 if (temp < table->id[low].temp || temp > table->id[high].temp) in rk_tsadcv2_temp_to_code()
495 if (temp == table->id[mid].temp) in rk_tsadcv2_temp_to_code()
496 return table->id[mid].code; in rk_tsadcv2_temp_to_code()
497 else if (temp < table->id[mid].temp) in rk_tsadcv2_temp_to_code()
498 high = mid - 1; in rk_tsadcv2_temp_to_code()
510 num = abs(table->id[mid + 1].code - table->id[mid].code); in rk_tsadcv2_temp_to_code()
511 num *= temp - table->id[mid].temp; in rk_tsadcv2_temp_to_code()
512 denom = table->id[mid + 1].temp - table->id[mid].temp; in rk_tsadcv2_temp_to_code()
514 switch (table->mode) { in rk_tsadcv2_temp_to_code()
516 return table->id[mid].code - (num / denom); in rk_tsadcv2_temp_to_code()
518 return table->id[mid].code + (num / denom); in rk_tsadcv2_temp_to_code()
520 pr_err("%s: unknown table mode: %d\n", __func__, table->mode); in rk_tsadcv2_temp_to_code()
534 unsigned int high = table->length - 1; in rk_tsadcv2_code_to_temp()
539 WARN_ON(table->length < 2); in rk_tsadcv2_code_to_temp()
541 switch (table->mode) { in rk_tsadcv2_code_to_temp()
543 code &= table->data_mask; in rk_tsadcv2_code_to_temp()
544 if (code <= table->id[high].code) in rk_tsadcv2_code_to_temp()
545 return -EAGAIN; /* Incorrect reading */ in rk_tsadcv2_code_to_temp()
548 if (code >= table->id[mid].code && in rk_tsadcv2_code_to_temp()
549 code < table->id[mid - 1].code) in rk_tsadcv2_code_to_temp()
551 else if (code < table->id[mid].code) in rk_tsadcv2_code_to_temp()
554 high = mid - 1; in rk_tsadcv2_code_to_temp()
560 code &= table->data_mask; in rk_tsadcv2_code_to_temp()
561 if (code < table->id[low].code) in rk_tsadcv2_code_to_temp()
562 return -EAGAIN; /* Incorrect reading */ in rk_tsadcv2_code_to_temp()
565 if (code <= table->id[mid].code && in rk_tsadcv2_code_to_temp()
566 code > table->id[mid - 1].code) in rk_tsadcv2_code_to_temp()
568 else if (code > table->id[mid].code) in rk_tsadcv2_code_to_temp()
571 high = mid - 1; in rk_tsadcv2_code_to_temp()
577 pr_err("%s: unknown table mode: %d\n", __func__, table->mode); in rk_tsadcv2_code_to_temp()
578 return -EINVAL; in rk_tsadcv2_code_to_temp()
583 * assume that the relationship between sensor readings and in rk_tsadcv2_code_to_temp()
587 num = table->id[mid].temp - table->id[mid - 1].temp; in rk_tsadcv2_code_to_temp()
588 num *= abs(table->id[mid - 1].code - code); in rk_tsadcv2_code_to_temp()
589 denom = abs(table->id[mid - 1].code - table->id[mid].code); in rk_tsadcv2_code_to_temp()
590 *temp = table->id[mid - 1].temp + (num / denom); in rk_tsadcv2_code_to_temp()
596 * rk_tsadcv2_initialize - initialize TASDC Controller.
630 * rk_tsadcv3_initialize - initialize TASDC Controller.
722 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
725 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
759 * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm in rk_tsadcv2_alarm_temp()
772 if (alarm_value == table->data_mask) in rk_tsadcv2_alarm_temp()
773 return -ERANGE; in rk_tsadcv2_alarm_temp()
775 writel_relaxed(alarm_value & table->data_mask, in rk_tsadcv2_alarm_temp()
792 if (tshut_value == table->data_mask) in rk_tsadcv2_tshut_temp()
793 return -ERANGE; in rk_tsadcv2_tshut_temp()
822 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
846 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
870 .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
871 .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
895 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
918 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
919 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
943 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
944 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
968 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
969 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
994 .compatible = "rockchip,rv1108-tsadc",
998 .compatible = "rockchip,rk3228-tsadc",
1002 .compatible = "rockchip,rk3288-tsadc",
1006 .compatible = "rockchip,rk3328-tsadc",
1010 .compatible = "rockchip,rk3366-tsadc",
1014 .compatible = "rockchip,rk3368-tsadc",
1018 .compatible = "rockchip,rk3399-tsadc",
1026 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on) in rockchip_thermal_toggle_sensor() argument
1028 struct thermal_zone_device *tzd = sensor->tzd; in rockchip_thermal_toggle_sensor()
1030 tzd->ops->set_mode(tzd, in rockchip_thermal_toggle_sensor()
1036 struct rockchip_thermal_data *thermal = dev; in rockchip_thermal_alarm_irq_thread() local
1039 dev_dbg(&thermal->pdev->dev, "thermal alarm\n"); in rockchip_thermal_alarm_irq_thread()
1041 thermal->chip->irq_ack(thermal->regs); in rockchip_thermal_alarm_irq_thread()
1043 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_alarm_irq_thread()
1044 thermal_zone_device_update(thermal->sensors[i].tzd, in rockchip_thermal_alarm_irq_thread()
1052 struct rockchip_thermal_sensor *sensor = _sensor; in rockchip_thermal_set_trips() local
1053 struct rockchip_thermal_data *thermal = sensor->thermal; in rockchip_thermal_set_trips() local
1054 const struct rockchip_tsadc_chip *tsadc = thermal->chip; in rockchip_thermal_set_trips()
1056 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n", in rockchip_thermal_set_trips()
1057 __func__, sensor->id, low, high); in rockchip_thermal_set_trips()
1059 return tsadc->set_alarm_temp(&tsadc->table, in rockchip_thermal_set_trips()
1060 sensor->id, thermal->regs, high); in rockchip_thermal_set_trips()
1065 struct rockchip_thermal_sensor *sensor = _sensor; in rockchip_thermal_get_temp() local
1066 struct rockchip_thermal_data *thermal = sensor->thermal; in rockchip_thermal_get_temp() local
1067 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip; in rockchip_thermal_get_temp()
1070 retval = tsadc->get_temp(&tsadc->table, in rockchip_thermal_get_temp()
1071 sensor->id, thermal->regs, out_temp); in rockchip_thermal_get_temp()
1072 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n", in rockchip_thermal_get_temp()
1073 sensor->id, *out_temp, retval); in rockchip_thermal_get_temp()
1085 struct rockchip_thermal_data *thermal) in rockchip_configure_from_dt() argument
1089 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) { in rockchip_configure_from_dt()
1092 thermal->chip->tshut_temp); in rockchip_configure_from_dt()
1093 thermal->tshut_temp = thermal->chip->tshut_temp; in rockchip_configure_from_dt()
1098 return -ERANGE; in rockchip_configure_from_dt()
1100 thermal->tshut_temp = shut_temp; in rockchip_configure_from_dt()
1103 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) { in rockchip_configure_from_dt()
1106 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ? in rockchip_configure_from_dt()
1108 thermal->tshut_mode = thermal->chip->tshut_mode; in rockchip_configure_from_dt()
1110 thermal->tshut_mode = tshut_mode; in rockchip_configure_from_dt()
1113 if (thermal->tshut_mode > 1) { in rockchip_configure_from_dt()
1115 thermal->tshut_mode); in rockchip_configure_from_dt()
1116 return -EINVAL; in rockchip_configure_from_dt()
1119 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity", in rockchip_configure_from_dt()
1122 "Missing tshut-polarity property, using default (%s)\n", in rockchip_configure_from_dt()
1123 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ? in rockchip_configure_from_dt()
1125 thermal->tshut_polarity = thermal->chip->tshut_polarity; in rockchip_configure_from_dt()
1127 thermal->tshut_polarity = tshut_polarity; in rockchip_configure_from_dt()
1130 if (thermal->tshut_polarity > 1) { in rockchip_configure_from_dt()
1131 dev_err(dev, "Invalid tshut-polarity specified: %d\n", in rockchip_configure_from_dt()
1132 thermal->tshut_polarity); in rockchip_configure_from_dt()
1133 return -EINVAL; in rockchip_configure_from_dt()
1139 thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf"); in rockchip_configure_from_dt()
1140 if (IS_ERR(thermal->grf)) in rockchip_configure_from_dt()
1148 struct rockchip_thermal_data *thermal, in rockchip_thermal_register_sensor() argument
1149 struct rockchip_thermal_sensor *sensor, in rockchip_thermal_register_sensor() argument
1152 const struct rockchip_tsadc_chip *tsadc = thermal->chip; in rockchip_thermal_register_sensor()
1155 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode); in rockchip_thermal_register_sensor()
1157 error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs, in rockchip_thermal_register_sensor()
1158 thermal->tshut_temp); in rockchip_thermal_register_sensor()
1160 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n", in rockchip_thermal_register_sensor()
1161 __func__, thermal->tshut_temp, error); in rockchip_thermal_register_sensor()
1163 sensor->thermal = thermal; in rockchip_thermal_register_sensor()
1164 sensor->id = id; in rockchip_thermal_register_sensor()
1165 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id, in rockchip_thermal_register_sensor()
1166 sensor, &rockchip_of_thermal_ops); in rockchip_thermal_register_sensor()
1167 if (IS_ERR(sensor->tzd)) { in rockchip_thermal_register_sensor()
1168 error = PTR_ERR(sensor->tzd); in rockchip_thermal_register_sensor()
1169 dev_err(&pdev->dev, "failed to register sensor %d: %d\n", in rockchip_thermal_register_sensor()
1189 struct device_node *np = pdev->dev.of_node; in rockchip_thermal_probe()
1190 struct rockchip_thermal_data *thermal; in rockchip_thermal_probe() local
1199 return -ENXIO; in rockchip_thermal_probe()
1203 dev_err(&pdev->dev, "no irq resource?\n"); in rockchip_thermal_probe()
1204 return -EINVAL; in rockchip_thermal_probe()
1207 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data), in rockchip_thermal_probe()
1209 if (!thermal) in rockchip_thermal_probe()
1210 return -ENOMEM; in rockchip_thermal_probe()
1212 thermal->pdev = pdev; in rockchip_thermal_probe()
1214 thermal->chip = (const struct rockchip_tsadc_chip *)match->data; in rockchip_thermal_probe()
1215 if (!thermal->chip) in rockchip_thermal_probe()
1216 return -EINVAL; in rockchip_thermal_probe()
1219 thermal->regs = devm_ioremap_resource(&pdev->dev, res); in rockchip_thermal_probe()
1220 if (IS_ERR(thermal->regs)) in rockchip_thermal_probe()
1221 return PTR_ERR(thermal->regs); in rockchip_thermal_probe()
1223 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb"); in rockchip_thermal_probe()
1224 if (IS_ERR(thermal->reset)) { in rockchip_thermal_probe()
1225 error = PTR_ERR(thermal->reset); in rockchip_thermal_probe()
1226 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error); in rockchip_thermal_probe()
1230 thermal->clk = devm_clk_get(&pdev->dev, "tsadc"); in rockchip_thermal_probe()
1231 if (IS_ERR(thermal->clk)) { in rockchip_thermal_probe()
1232 error = PTR_ERR(thermal->clk); in rockchip_thermal_probe()
1233 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error); in rockchip_thermal_probe()
1237 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk"); in rockchip_thermal_probe()
1238 if (IS_ERR(thermal->pclk)) { in rockchip_thermal_probe()
1239 error = PTR_ERR(thermal->pclk); in rockchip_thermal_probe()
1240 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n", in rockchip_thermal_probe()
1245 error = clk_prepare_enable(thermal->clk); in rockchip_thermal_probe()
1247 dev_err(&pdev->dev, "failed to enable converter clock: %d\n", in rockchip_thermal_probe()
1252 error = clk_prepare_enable(thermal->pclk); in rockchip_thermal_probe()
1254 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error); in rockchip_thermal_probe()
1258 rockchip_thermal_reset_controller(thermal->reset); in rockchip_thermal_probe()
1260 error = rockchip_configure_from_dt(&pdev->dev, np, thermal); in rockchip_thermal_probe()
1262 dev_err(&pdev->dev, "failed to parse device tree data: %d\n", in rockchip_thermal_probe()
1267 thermal->chip->initialize(thermal->grf, thermal->regs, in rockchip_thermal_probe()
1268 thermal->tshut_polarity); in rockchip_thermal_probe()
1270 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_probe()
1271 error = rockchip_thermal_register_sensor(pdev, thermal, in rockchip_thermal_probe()
1272 &thermal->sensors[i], in rockchip_thermal_probe()
1273 thermal->chip->chn_id[i]); in rockchip_thermal_probe()
1275 dev_err(&pdev->dev, in rockchip_thermal_probe()
1276 "failed to register sensor[%d] : error = %d\n", in rockchip_thermal_probe()
1282 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, in rockchip_thermal_probe()
1285 "rockchip_thermal", thermal); in rockchip_thermal_probe()
1287 dev_err(&pdev->dev, in rockchip_thermal_probe()
1292 thermal->chip->control(thermal->regs, true); in rockchip_thermal_probe()
1294 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_probe()
1295 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); in rockchip_thermal_probe()
1297 platform_set_drvdata(pdev, thermal); in rockchip_thermal_probe()
1302 clk_disable_unprepare(thermal->pclk); in rockchip_thermal_probe()
1304 clk_disable_unprepare(thermal->clk); in rockchip_thermal_probe()
1311 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev); in rockchip_thermal_remove() local
1314 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_remove()
1315 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i]; in rockchip_thermal_remove() local
1317 rockchip_thermal_toggle_sensor(sensor, false); in rockchip_thermal_remove()
1320 thermal->chip->control(thermal->regs, false); in rockchip_thermal_remove()
1322 clk_disable_unprepare(thermal->pclk); in rockchip_thermal_remove()
1323 clk_disable_unprepare(thermal->clk); in rockchip_thermal_remove()
1331 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev); in rockchip_thermal_suspend() local
1334 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_suspend()
1335 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false); in rockchip_thermal_suspend()
1337 thermal->chip->control(thermal->regs, false); in rockchip_thermal_suspend()
1339 clk_disable(thermal->pclk); in rockchip_thermal_suspend()
1340 clk_disable(thermal->clk); in rockchip_thermal_suspend()
1350 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev); in rockchip_thermal_resume() local
1354 error = clk_enable(thermal->clk); in rockchip_thermal_resume()
1358 error = clk_enable(thermal->pclk); in rockchip_thermal_resume()
1360 clk_disable(thermal->clk); in rockchip_thermal_resume()
1364 rockchip_thermal_reset_controller(thermal->reset); in rockchip_thermal_resume()
1366 thermal->chip->initialize(thermal->grf, thermal->regs, in rockchip_thermal_resume()
1367 thermal->tshut_polarity); in rockchip_thermal_resume()
1369 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_resume()
1370 int id = thermal->sensors[i].id; in rockchip_thermal_resume()
1372 thermal->chip->set_tshut_mode(id, thermal->regs, in rockchip_thermal_resume()
1373 thermal->tshut_mode); in rockchip_thermal_resume()
1375 error = thermal->chip->set_tshut_temp(&thermal->chip->table, in rockchip_thermal_resume()
1376 id, thermal->regs, in rockchip_thermal_resume()
1377 thermal->tshut_temp); in rockchip_thermal_resume()
1379 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n", in rockchip_thermal_resume()
1380 __func__, thermal->tshut_temp, error); in rockchip_thermal_resume()
1383 thermal->chip->control(thermal->regs, true); in rockchip_thermal_resume()
1385 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_resume()
1386 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); in rockchip_thermal_resume()
1398 .name = "rockchip-thermal",
1408 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1411 MODULE_ALIAS("platform:rockchip-thermal");