Lines Matching +full:- +full:thermal
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd
4 * Caesar Wang <wxt@rock-chips.com>
18 #include <linux/thermal.h>
70 * struct chip_tsadc_table - hold information about chip-specific differences
84 * struct rockchip_tsadc_chip - hold the private data of tsadc chip
87 * @tshut_temp: the hardware-controlled shutdown temperature value
88 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
89 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
95 * @set_tshut_temp: set the hardware-controlled shutdown temperature
96 * @set_tshut_mode: set the hardware-controlled shutdown mode
97 * @table: the chip-specific conversion table
104 /* The hardware-controlled tshut property */
109 /* Chip-wide methods */
115 /* Per-sensor methods */
124 /* Per-table methods */
129 * struct rockchip_thermal_sensor - hold the information of thermal sensor
130 * @thermal: pointer to the platform/configuration data
131 * @tzd: pointer to a thermal zone
132 * @id: identifier of the thermal sensor
135 struct rockchip_thermal_data *thermal; member
141 * struct rockchip_thermal_data - hold the private data of thermal driver
143 * @pdev: platform device of thermal
145 * @sensors: array of thermal sensors
150 * @tshut_temp: the hardware-controlled shutdown temperature value
151 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
152 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
240 * struct tsadc_table - code to temperature conversion table
255 {0, -40000},
256 {374, -40000},
257 {382, -35000},
258 {389, -30000},
259 {397, -25000},
260 {405, -20000},
261 {413, -15000},
262 {421, -10000},
263 {429, -5000},
294 {0, -40000},
295 {588, -40000},
296 {593, -35000},
297 {598, -30000},
298 {603, -25000},
299 {608, -20000},
300 {613, -15000},
301 {618, -10000},
302 {623, -5000},
333 {TSADCV2_DATA_MASK, -40000},
334 {3800, -40000},
335 {3792, -35000},
336 {3783, -30000},
337 {3774, -25000},
338 {3765, -20000},
339 {3756, -15000},
340 {3747, -10000},
341 {3737, -5000},
372 {0, -40000},
373 {296, -40000},
374 {304, -35000},
375 {313, -30000},
376 {331, -20000},
377 {340, -15000},
378 {349, -10000},
379 {359, -5000},
410 {0, -40000},
411 {106, -40000},
412 {108, -35000},
413 {110, -30000},
414 {112, -25000},
415 {114, -20000},
416 {116, -15000},
417 {118, -10000},
418 {120, -5000},
449 {0, -40000},
450 {402, -40000},
451 {410, -35000},
452 {419, -30000},
453 {427, -25000},
454 {436, -20000},
455 {444, -15000},
456 {453, -10000},
457 {461, -5000},
488 {0, -40000},
489 {1584, -40000},
490 {1620, -35000},
491 {1652, -30000},
492 {1688, -25000},
493 {1720, -20000},
494 {1756, -15000},
495 {1788, -10000},
496 {1824, -5000},
532 u32 error = table->data_mask; in rk_tsadcv2_temp_to_code()
535 high = (table->length - 1) - 1; /* ignore the last check for table */ in rk_tsadcv2_temp_to_code()
539 if (temp < table->id[low].temp || temp > table->id[high].temp) in rk_tsadcv2_temp_to_code()
543 if (temp == table->id[mid].temp) in rk_tsadcv2_temp_to_code()
544 return table->id[mid].code; in rk_tsadcv2_temp_to_code()
545 else if (temp < table->id[mid].temp) in rk_tsadcv2_temp_to_code()
546 high = mid - 1; in rk_tsadcv2_temp_to_code()
558 num = abs(table->id[mid + 1].code - table->id[mid].code); in rk_tsadcv2_temp_to_code()
559 num *= temp - table->id[mid].temp; in rk_tsadcv2_temp_to_code()
560 denom = table->id[mid + 1].temp - table->id[mid].temp; in rk_tsadcv2_temp_to_code()
562 switch (table->mode) { in rk_tsadcv2_temp_to_code()
564 return table->id[mid].code - (num / denom); in rk_tsadcv2_temp_to_code()
566 return table->id[mid].code + (num / denom); in rk_tsadcv2_temp_to_code()
568 pr_err("%s: unknown table mode: %d\n", __func__, table->mode); in rk_tsadcv2_temp_to_code()
582 unsigned int high = table->length - 1; in rk_tsadcv2_code_to_temp()
587 WARN_ON(table->length < 2); in rk_tsadcv2_code_to_temp()
589 switch (table->mode) { in rk_tsadcv2_code_to_temp()
591 code &= table->data_mask; in rk_tsadcv2_code_to_temp()
592 if (code <= table->id[high].code) in rk_tsadcv2_code_to_temp()
593 return -EAGAIN; /* Incorrect reading */ in rk_tsadcv2_code_to_temp()
596 if (code >= table->id[mid].code && in rk_tsadcv2_code_to_temp()
597 code < table->id[mid - 1].code) in rk_tsadcv2_code_to_temp()
599 else if (code < table->id[mid].code) in rk_tsadcv2_code_to_temp()
602 high = mid - 1; in rk_tsadcv2_code_to_temp()
608 code &= table->data_mask; in rk_tsadcv2_code_to_temp()
609 if (code < table->id[low].code) in rk_tsadcv2_code_to_temp()
610 return -EAGAIN; /* Incorrect reading */ in rk_tsadcv2_code_to_temp()
613 if (code <= table->id[mid].code && in rk_tsadcv2_code_to_temp()
614 code > table->id[mid - 1].code) in rk_tsadcv2_code_to_temp()
616 else if (code > table->id[mid].code) in rk_tsadcv2_code_to_temp()
619 high = mid - 1; in rk_tsadcv2_code_to_temp()
625 pr_err("%s: unknown table mode: %d\n", __func__, table->mode); in rk_tsadcv2_code_to_temp()
626 return -EINVAL; in rk_tsadcv2_code_to_temp()
635 num = table->id[mid].temp - table->id[mid - 1].temp; in rk_tsadcv2_code_to_temp()
636 num *= abs(table->id[mid - 1].code - code); in rk_tsadcv2_code_to_temp()
637 denom = abs(table->id[mid - 1].code - table->id[mid].code); in rk_tsadcv2_code_to_temp()
638 *temp = table->id[mid - 1].temp + (num / denom); in rk_tsadcv2_code_to_temp()
644 * rk_tsadcv2_initialize - initialize TASDC Controller.
647 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
681 * rk_tsadcv3_initialize - initialize TASDC Controller.
684 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
826 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
831 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
865 * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm in rk_tsadcv2_alarm_temp()
878 if (alarm_value == table->data_mask) in rk_tsadcv2_alarm_temp()
879 return -ERANGE; in rk_tsadcv2_alarm_temp()
881 writel_relaxed(alarm_value & table->data_mask, in rk_tsadcv2_alarm_temp()
898 if (tshut_value == table->data_mask) in rk_tsadcv2_tshut_temp()
899 return -ERANGE; in rk_tsadcv2_tshut_temp()
1148 { .compatible = "rockchip,px30-tsadc",
1152 .compatible = "rockchip,rv1108-tsadc",
1156 .compatible = "rockchip,rk3228-tsadc",
1160 .compatible = "rockchip,rk3288-tsadc",
1164 .compatible = "rockchip,rk3328-tsadc",
1168 .compatible = "rockchip,rk3366-tsadc",
1172 .compatible = "rockchip,rk3368-tsadc",
1176 .compatible = "rockchip,rk3399-tsadc",
1180 .compatible = "rockchip,rk3568-tsadc",
1190 struct thermal_zone_device *tzd = sensor->tzd; in rockchip_thermal_toggle_sensor()
1200 struct rockchip_thermal_data *thermal = dev; in rockchip_thermal_alarm_irq_thread() local
1203 dev_dbg(&thermal->pdev->dev, "thermal alarm\n"); in rockchip_thermal_alarm_irq_thread()
1205 thermal->chip->irq_ack(thermal->regs); in rockchip_thermal_alarm_irq_thread()
1207 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_alarm_irq_thread()
1208 thermal_zone_device_update(thermal->sensors[i].tzd, in rockchip_thermal_alarm_irq_thread()
1216 struct rockchip_thermal_sensor *sensor = tz->devdata; in rockchip_thermal_set_trips()
1217 struct rockchip_thermal_data *thermal = sensor->thermal; in rockchip_thermal_set_trips() local
1218 const struct rockchip_tsadc_chip *tsadc = thermal->chip; in rockchip_thermal_set_trips()
1220 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n", in rockchip_thermal_set_trips()
1221 __func__, sensor->id, low, high); in rockchip_thermal_set_trips()
1223 return tsadc->set_alarm_temp(&tsadc->table, in rockchip_thermal_set_trips()
1224 sensor->id, thermal->regs, high); in rockchip_thermal_set_trips()
1229 struct rockchip_thermal_sensor *sensor = tz->devdata; in rockchip_thermal_get_temp()
1230 struct rockchip_thermal_data *thermal = sensor->thermal; in rockchip_thermal_get_temp() local
1231 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip; in rockchip_thermal_get_temp()
1234 retval = tsadc->get_temp(&tsadc->table, in rockchip_thermal_get_temp()
1235 sensor->id, thermal->regs, out_temp); in rockchip_thermal_get_temp()
1236 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n", in rockchip_thermal_get_temp()
1237 sensor->id, *out_temp, retval); in rockchip_thermal_get_temp()
1249 struct rockchip_thermal_data *thermal) in rockchip_configure_from_dt() argument
1253 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) { in rockchip_configure_from_dt()
1256 thermal->chip->tshut_temp); in rockchip_configure_from_dt()
1257 thermal->tshut_temp = thermal->chip->tshut_temp; in rockchip_configure_from_dt()
1262 return -ERANGE; in rockchip_configure_from_dt()
1264 thermal->tshut_temp = shut_temp; in rockchip_configure_from_dt()
1267 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) { in rockchip_configure_from_dt()
1270 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ? in rockchip_configure_from_dt()
1272 thermal->tshut_mode = thermal->chip->tshut_mode; in rockchip_configure_from_dt()
1274 thermal->tshut_mode = tshut_mode; in rockchip_configure_from_dt()
1277 if (thermal->tshut_mode > 1) { in rockchip_configure_from_dt()
1279 thermal->tshut_mode); in rockchip_configure_from_dt()
1280 return -EINVAL; in rockchip_configure_from_dt()
1283 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity", in rockchip_configure_from_dt()
1286 "Missing tshut-polarity property, using default (%s)\n", in rockchip_configure_from_dt()
1287 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ? in rockchip_configure_from_dt()
1289 thermal->tshut_polarity = thermal->chip->tshut_polarity; in rockchip_configure_from_dt()
1291 thermal->tshut_polarity = tshut_polarity; in rockchip_configure_from_dt()
1294 if (thermal->tshut_polarity > 1) { in rockchip_configure_from_dt()
1295 dev_err(dev, "Invalid tshut-polarity specified: %d\n", in rockchip_configure_from_dt()
1296 thermal->tshut_polarity); in rockchip_configure_from_dt()
1297 return -EINVAL; in rockchip_configure_from_dt()
1303 thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf"); in rockchip_configure_from_dt()
1304 if (IS_ERR(thermal->grf)) in rockchip_configure_from_dt()
1312 struct rockchip_thermal_data *thermal, in rockchip_thermal_register_sensor() argument
1316 const struct rockchip_tsadc_chip *tsadc = thermal->chip; in rockchip_thermal_register_sensor()
1319 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode); in rockchip_thermal_register_sensor()
1321 error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs, in rockchip_thermal_register_sensor()
1322 thermal->tshut_temp); in rockchip_thermal_register_sensor()
1324 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n", in rockchip_thermal_register_sensor()
1325 __func__, thermal->tshut_temp, error); in rockchip_thermal_register_sensor()
1327 sensor->thermal = thermal; in rockchip_thermal_register_sensor()
1328 sensor->id = id; in rockchip_thermal_register_sensor()
1329 sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, id, sensor, in rockchip_thermal_register_sensor()
1331 if (IS_ERR(sensor->tzd)) { in rockchip_thermal_register_sensor()
1332 error = PTR_ERR(sensor->tzd); in rockchip_thermal_register_sensor()
1333 dev_err(&pdev->dev, "failed to register sensor %d: %d\n", in rockchip_thermal_register_sensor()
1354 struct device_node *np = pdev->dev.of_node; in rockchip_thermal_probe()
1355 struct rockchip_thermal_data *thermal; in rockchip_thermal_probe() local
1364 return -ENXIO; in rockchip_thermal_probe()
1368 return -EINVAL; in rockchip_thermal_probe()
1370 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data), in rockchip_thermal_probe()
1372 if (!thermal) in rockchip_thermal_probe()
1373 return -ENOMEM; in rockchip_thermal_probe()
1375 thermal->pdev = pdev; in rockchip_thermal_probe()
1377 thermal->chip = (const struct rockchip_tsadc_chip *)match->data; in rockchip_thermal_probe()
1378 if (!thermal->chip) in rockchip_thermal_probe()
1379 return -EINVAL; in rockchip_thermal_probe()
1382 thermal->regs = devm_ioremap_resource(&pdev->dev, res); in rockchip_thermal_probe()
1383 if (IS_ERR(thermal->regs)) in rockchip_thermal_probe()
1384 return PTR_ERR(thermal->regs); in rockchip_thermal_probe()
1386 thermal->reset = devm_reset_control_array_get(&pdev->dev, false, false); in rockchip_thermal_probe()
1387 if (IS_ERR(thermal->reset)) { in rockchip_thermal_probe()
1388 error = PTR_ERR(thermal->reset); in rockchip_thermal_probe()
1389 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error); in rockchip_thermal_probe()
1393 thermal->clk = devm_clk_get(&pdev->dev, "tsadc"); in rockchip_thermal_probe()
1394 if (IS_ERR(thermal->clk)) { in rockchip_thermal_probe()
1395 error = PTR_ERR(thermal->clk); in rockchip_thermal_probe()
1396 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error); in rockchip_thermal_probe()
1400 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk"); in rockchip_thermal_probe()
1401 if (IS_ERR(thermal->pclk)) { in rockchip_thermal_probe()
1402 error = PTR_ERR(thermal->pclk); in rockchip_thermal_probe()
1403 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n", in rockchip_thermal_probe()
1408 error = clk_prepare_enable(thermal->clk); in rockchip_thermal_probe()
1410 dev_err(&pdev->dev, "failed to enable converter clock: %d\n", in rockchip_thermal_probe()
1415 error = clk_prepare_enable(thermal->pclk); in rockchip_thermal_probe()
1417 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error); in rockchip_thermal_probe()
1421 rockchip_thermal_reset_controller(thermal->reset); in rockchip_thermal_probe()
1423 error = rockchip_configure_from_dt(&pdev->dev, np, thermal); in rockchip_thermal_probe()
1425 dev_err(&pdev->dev, "failed to parse device tree data: %d\n", in rockchip_thermal_probe()
1430 thermal->chip->initialize(thermal->grf, thermal->regs, in rockchip_thermal_probe()
1431 thermal->tshut_polarity); in rockchip_thermal_probe()
1433 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_probe()
1434 error = rockchip_thermal_register_sensor(pdev, thermal, in rockchip_thermal_probe()
1435 &thermal->sensors[i], in rockchip_thermal_probe()
1436 thermal->chip->chn_id[i]); in rockchip_thermal_probe()
1438 dev_err(&pdev->dev, in rockchip_thermal_probe()
1445 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, in rockchip_thermal_probe()
1448 "rockchip_thermal", thermal); in rockchip_thermal_probe()
1450 dev_err(&pdev->dev, in rockchip_thermal_probe()
1455 thermal->chip->control(thermal->regs, true); in rockchip_thermal_probe()
1457 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_probe()
1458 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); in rockchip_thermal_probe()
1459 thermal->sensors[i].tzd->tzp->no_hwmon = false; in rockchip_thermal_probe()
1460 error = thermal_add_hwmon_sysfs(thermal->sensors[i].tzd); in rockchip_thermal_probe()
1462 dev_warn(&pdev->dev, in rockchip_thermal_probe()
1467 platform_set_drvdata(pdev, thermal); in rockchip_thermal_probe()
1472 clk_disable_unprepare(thermal->pclk); in rockchip_thermal_probe()
1474 clk_disable_unprepare(thermal->clk); in rockchip_thermal_probe()
1481 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev); in rockchip_thermal_remove() local
1484 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_remove()
1485 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i]; in rockchip_thermal_remove()
1487 thermal_remove_hwmon_sysfs(sensor->tzd); in rockchip_thermal_remove()
1491 thermal->chip->control(thermal->regs, false); in rockchip_thermal_remove()
1493 clk_disable_unprepare(thermal->pclk); in rockchip_thermal_remove()
1494 clk_disable_unprepare(thermal->clk); in rockchip_thermal_remove()
1501 struct rockchip_thermal_data *thermal = dev_get_drvdata(dev); in rockchip_thermal_suspend() local
1504 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_suspend()
1505 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false); in rockchip_thermal_suspend()
1507 thermal->chip->control(thermal->regs, false); in rockchip_thermal_suspend()
1509 clk_disable(thermal->pclk); in rockchip_thermal_suspend()
1510 clk_disable(thermal->clk); in rockchip_thermal_suspend()
1519 struct rockchip_thermal_data *thermal = dev_get_drvdata(dev); in rockchip_thermal_resume() local
1523 error = clk_enable(thermal->clk); in rockchip_thermal_resume()
1527 error = clk_enable(thermal->pclk); in rockchip_thermal_resume()
1529 clk_disable(thermal->clk); in rockchip_thermal_resume()
1533 rockchip_thermal_reset_controller(thermal->reset); in rockchip_thermal_resume()
1535 thermal->chip->initialize(thermal->grf, thermal->regs, in rockchip_thermal_resume()
1536 thermal->tshut_polarity); in rockchip_thermal_resume()
1538 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_resume()
1539 int id = thermal->sensors[i].id; in rockchip_thermal_resume()
1541 thermal->chip->set_tshut_mode(id, thermal->regs, in rockchip_thermal_resume()
1542 thermal->tshut_mode); in rockchip_thermal_resume()
1544 error = thermal->chip->set_tshut_temp(&thermal->chip->table, in rockchip_thermal_resume()
1545 id, thermal->regs, in rockchip_thermal_resume()
1546 thermal->tshut_temp); in rockchip_thermal_resume()
1549 __func__, thermal->tshut_temp, error); in rockchip_thermal_resume()
1552 thermal->chip->control(thermal->regs, true); in rockchip_thermal_resume()
1554 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_resume()
1555 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); in rockchip_thermal_resume()
1567 .name = "rockchip-thermal",
1577 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1580 MODULE_ALIAS("platform:rockchip-thermal");