Lines Matching +full:reset +full:- +full:active +full:- +full:low

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>
17 #include <linux/reset.h>
24 * the resulting TSHUT gave CRU module,let it reset the entire chip,
35 * 0: low active, 1: high active
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
141 * struct rockchip_thermal_data - hold the private data of thermal driver
144 * @reset: the reset controller of tsadc
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)
157 struct reset_control *reset; member
230 * struct tsadc_table - code to temperature conversion table
245 {0, -40000},
246 {374, -40000},
247 {382, -35000},
248 {389, -30000},
249 {397, -25000},
250 {405, -20000},
251 {413, -15000},
252 {421, -10000},
253 {429, -5000},
284 {0, -40000},
285 {588, -40000},
286 {593, -35000},
287 {598, -30000},
288 {603, -25000},
289 {608, -20000},
290 {613, -15000},
291 {618, -10000},
292 {623, -5000},
323 {TSADCV2_DATA_MASK, -40000},
324 {3800, -40000},
325 {3792, -35000},
326 {3783, -30000},
327 {3774, -25000},
328 {3765, -20000},
329 {3756, -15000},
330 {3747, -10000},
331 {3737, -5000},
362 {0, -40000},
363 {296, -40000},
364 {304, -35000},
365 {313, -30000},
366 {331, -20000},
367 {340, -15000},
368 {349, -10000},
369 {359, -5000},
400 {0, -40000},
401 {106, -40000},
402 {108, -35000},
403 {110, -30000},
404 {112, -25000},
405 {114, -20000},
406 {116, -15000},
407 {118, -10000},
408 {120, -5000},
439 {0, -40000},
440 {402, -40000},
441 {410, -35000},
442 {419, -30000},
443 {427, -25000},
444 {436, -20000},
445 {444, -15000},
446 {453, -10000},
447 {461, -5000},
480 int high, low, mid; in rk_tsadcv2_temp_to_code() local
483 u32 error = table->data_mask; in rk_tsadcv2_temp_to_code()
485 low = 0; in rk_tsadcv2_temp_to_code()
486 high = (table->length - 1) - 1; /* ignore the last check for table */ in rk_tsadcv2_temp_to_code()
487 mid = (high + low) / 2; in rk_tsadcv2_temp_to_code()
490 if (temp < table->id[low].temp || temp > table->id[high].temp) in rk_tsadcv2_temp_to_code()
493 while (low <= high) { in rk_tsadcv2_temp_to_code()
494 if (temp == table->id[mid].temp) in rk_tsadcv2_temp_to_code()
495 return table->id[mid].code; in rk_tsadcv2_temp_to_code()
496 else if (temp < table->id[mid].temp) in rk_tsadcv2_temp_to_code()
497 high = mid - 1; in rk_tsadcv2_temp_to_code()
499 low = mid + 1; in rk_tsadcv2_temp_to_code()
500 mid = (low + high) / 2; in rk_tsadcv2_temp_to_code()
509 num = abs(table->id[mid + 1].code - table->id[mid].code); in rk_tsadcv2_temp_to_code()
510 num *= temp - table->id[mid].temp; in rk_tsadcv2_temp_to_code()
511 denom = table->id[mid + 1].temp - table->id[mid].temp; in rk_tsadcv2_temp_to_code()
513 switch (table->mode) { in rk_tsadcv2_temp_to_code()
515 return table->id[mid].code - (num / denom); in rk_tsadcv2_temp_to_code()
517 return table->id[mid].code + (num / denom); in rk_tsadcv2_temp_to_code()
519 pr_err("%s: unknown table mode: %d\n", __func__, table->mode); in rk_tsadcv2_temp_to_code()
532 unsigned int low = 1; in rk_tsadcv2_code_to_temp() local
533 unsigned int high = table->length - 1; in rk_tsadcv2_code_to_temp()
534 unsigned int mid = (low + high) / 2; in rk_tsadcv2_code_to_temp()
538 WARN_ON(table->length < 2); in rk_tsadcv2_code_to_temp()
540 switch (table->mode) { in rk_tsadcv2_code_to_temp()
542 code &= table->data_mask; in rk_tsadcv2_code_to_temp()
543 if (code <= table->id[high].code) in rk_tsadcv2_code_to_temp()
544 return -EAGAIN; /* Incorrect reading */ in rk_tsadcv2_code_to_temp()
546 while (low <= high) { in rk_tsadcv2_code_to_temp()
547 if (code >= table->id[mid].code && in rk_tsadcv2_code_to_temp()
548 code < table->id[mid - 1].code) in rk_tsadcv2_code_to_temp()
550 else if (code < table->id[mid].code) in rk_tsadcv2_code_to_temp()
551 low = mid + 1; in rk_tsadcv2_code_to_temp()
553 high = mid - 1; in rk_tsadcv2_code_to_temp()
555 mid = (low + high) / 2; in rk_tsadcv2_code_to_temp()
559 code &= table->data_mask; in rk_tsadcv2_code_to_temp()
560 if (code < table->id[low].code) in rk_tsadcv2_code_to_temp()
561 return -EAGAIN; /* Incorrect reading */ in rk_tsadcv2_code_to_temp()
563 while (low <= high) { in rk_tsadcv2_code_to_temp()
564 if (code <= table->id[mid].code && in rk_tsadcv2_code_to_temp()
565 code > table->id[mid - 1].code) in rk_tsadcv2_code_to_temp()
567 else if (code > table->id[mid].code) in rk_tsadcv2_code_to_temp()
568 low = mid + 1; in rk_tsadcv2_code_to_temp()
570 high = mid - 1; in rk_tsadcv2_code_to_temp()
572 mid = (low + high) / 2; in rk_tsadcv2_code_to_temp()
576 pr_err("%s: unknown table mode: %d\n", __func__, table->mode); in rk_tsadcv2_code_to_temp()
577 return -EINVAL; in rk_tsadcv2_code_to_temp()
586 num = table->id[mid].temp - table->id[mid - 1].temp; in rk_tsadcv2_code_to_temp()
587 num *= abs(table->id[mid - 1].code - code); in rk_tsadcv2_code_to_temp()
588 denom = abs(table->id[mid - 1].code - table->id[mid].code); in rk_tsadcv2_code_to_temp()
589 *temp = table->id[mid - 1].temp + (num / denom); in rk_tsadcv2_code_to_temp()
595 * rk_tsadcv2_initialize - initialize TASDC Controller.
598 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
632 * rk_tsadcv3_initialize - initialize TASDC Controller.
635 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
734 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
739 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
773 * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm in rk_tsadcv2_alarm_temp()
786 if (alarm_value == table->data_mask) in rk_tsadcv2_alarm_temp()
787 return -ERANGE; in rk_tsadcv2_alarm_temp()
789 writel_relaxed(alarm_value & table->data_mask, in rk_tsadcv2_alarm_temp()
806 if (tshut_value == table->data_mask) in rk_tsadcv2_tshut_temp()
807 return -ERANGE; in rk_tsadcv2_tshut_temp()
864 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
888 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
913 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
961 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
986 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1011 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1031 { .compatible = "rockchip,px30-tsadc",
1035 .compatible = "rockchip,rv1108-tsadc",
1039 .compatible = "rockchip,rk3228-tsadc",
1043 .compatible = "rockchip,rk3288-tsadc",
1047 .compatible = "rockchip,rk3328-tsadc",
1051 .compatible = "rockchip,rk3366-tsadc",
1055 .compatible = "rockchip,rk3368-tsadc",
1059 .compatible = "rockchip,rk3399-tsadc",
1069 struct thermal_zone_device *tzd = sensor->tzd; in rockchip_thermal_toggle_sensor()
1082 dev_dbg(&thermal->pdev->dev, "thermal alarm\n"); in rockchip_thermal_alarm_irq_thread()
1084 thermal->chip->irq_ack(thermal->regs); in rockchip_thermal_alarm_irq_thread()
1086 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_alarm_irq_thread()
1087 thermal_zone_device_update(thermal->sensors[i].tzd, in rockchip_thermal_alarm_irq_thread()
1093 static int rockchip_thermal_set_trips(void *_sensor, int low, int high) in rockchip_thermal_set_trips() argument
1096 struct rockchip_thermal_data *thermal = sensor->thermal; in rockchip_thermal_set_trips()
1097 const struct rockchip_tsadc_chip *tsadc = thermal->chip; in rockchip_thermal_set_trips()
1099 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n", in rockchip_thermal_set_trips()
1100 __func__, sensor->id, low, high); in rockchip_thermal_set_trips()
1102 return tsadc->set_alarm_temp(&tsadc->table, in rockchip_thermal_set_trips()
1103 sensor->id, thermal->regs, high); in rockchip_thermal_set_trips()
1109 struct rockchip_thermal_data *thermal = sensor->thermal; in rockchip_thermal_get_temp()
1110 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip; in rockchip_thermal_get_temp()
1113 retval = tsadc->get_temp(&tsadc->table, in rockchip_thermal_get_temp()
1114 sensor->id, thermal->regs, out_temp); in rockchip_thermal_get_temp()
1115 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n", in rockchip_thermal_get_temp()
1116 sensor->id, *out_temp, retval); in rockchip_thermal_get_temp()
1132 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) { in rockchip_configure_from_dt()
1135 thermal->chip->tshut_temp); in rockchip_configure_from_dt()
1136 thermal->tshut_temp = thermal->chip->tshut_temp; in rockchip_configure_from_dt()
1141 return -ERANGE; in rockchip_configure_from_dt()
1143 thermal->tshut_temp = shut_temp; in rockchip_configure_from_dt()
1146 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) { in rockchip_configure_from_dt()
1149 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ? in rockchip_configure_from_dt()
1151 thermal->tshut_mode = thermal->chip->tshut_mode; in rockchip_configure_from_dt()
1153 thermal->tshut_mode = tshut_mode; in rockchip_configure_from_dt()
1156 if (thermal->tshut_mode > 1) { in rockchip_configure_from_dt()
1158 thermal->tshut_mode); in rockchip_configure_from_dt()
1159 return -EINVAL; in rockchip_configure_from_dt()
1162 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity", in rockchip_configure_from_dt()
1165 "Missing tshut-polarity property, using default (%s)\n", in rockchip_configure_from_dt()
1166 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ? in rockchip_configure_from_dt()
1167 "low" : "high"); in rockchip_configure_from_dt()
1168 thermal->tshut_polarity = thermal->chip->tshut_polarity; in rockchip_configure_from_dt()
1170 thermal->tshut_polarity = tshut_polarity; in rockchip_configure_from_dt()
1173 if (thermal->tshut_polarity > 1) { in rockchip_configure_from_dt()
1174 dev_err(dev, "Invalid tshut-polarity specified: %d\n", in rockchip_configure_from_dt()
1175 thermal->tshut_polarity); in rockchip_configure_from_dt()
1176 return -EINVAL; in rockchip_configure_from_dt()
1182 thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf"); in rockchip_configure_from_dt()
1183 if (IS_ERR(thermal->grf)) in rockchip_configure_from_dt()
1195 const struct rockchip_tsadc_chip *tsadc = thermal->chip; in rockchip_thermal_register_sensor()
1198 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode); in rockchip_thermal_register_sensor()
1200 error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs, in rockchip_thermal_register_sensor()
1201 thermal->tshut_temp); in rockchip_thermal_register_sensor()
1203 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n", in rockchip_thermal_register_sensor()
1204 __func__, thermal->tshut_temp, error); in rockchip_thermal_register_sensor()
1206 sensor->thermal = thermal; in rockchip_thermal_register_sensor()
1207 sensor->id = id; in rockchip_thermal_register_sensor()
1208 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id, in rockchip_thermal_register_sensor()
1210 if (IS_ERR(sensor->tzd)) { in rockchip_thermal_register_sensor()
1211 error = PTR_ERR(sensor->tzd); in rockchip_thermal_register_sensor()
1212 dev_err(&pdev->dev, "failed to register sensor %d: %d\n", in rockchip_thermal_register_sensor()
1221 * Reset TSADC Controller, reset all tsadc registers.
1222 * @reset: the reset controller of tsadc
1224 static void rockchip_thermal_reset_controller(struct reset_control *reset) in rockchip_thermal_reset_controller() argument
1226 reset_control_assert(reset); in rockchip_thermal_reset_controller()
1228 reset_control_deassert(reset); in rockchip_thermal_reset_controller()
1233 struct device_node *np = pdev->dev.of_node; in rockchip_thermal_probe()
1243 return -ENXIO; in rockchip_thermal_probe()
1247 return -EINVAL; in rockchip_thermal_probe()
1249 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data), in rockchip_thermal_probe()
1252 return -ENOMEM; in rockchip_thermal_probe()
1254 thermal->pdev = pdev; in rockchip_thermal_probe()
1256 thermal->chip = (const struct rockchip_tsadc_chip *)match->data; in rockchip_thermal_probe()
1257 if (!thermal->chip) in rockchip_thermal_probe()
1258 return -EINVAL; in rockchip_thermal_probe()
1261 thermal->regs = devm_ioremap_resource(&pdev->dev, res); in rockchip_thermal_probe()
1262 if (IS_ERR(thermal->regs)) in rockchip_thermal_probe()
1263 return PTR_ERR(thermal->regs); in rockchip_thermal_probe()
1265 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb"); in rockchip_thermal_probe()
1266 if (IS_ERR(thermal->reset)) { in rockchip_thermal_probe()
1267 error = PTR_ERR(thermal->reset); in rockchip_thermal_probe()
1268 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error); in rockchip_thermal_probe()
1272 thermal->clk = devm_clk_get(&pdev->dev, "tsadc"); in rockchip_thermal_probe()
1273 if (IS_ERR(thermal->clk)) { in rockchip_thermal_probe()
1274 error = PTR_ERR(thermal->clk); in rockchip_thermal_probe()
1275 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error); in rockchip_thermal_probe()
1279 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk"); in rockchip_thermal_probe()
1280 if (IS_ERR(thermal->pclk)) { in rockchip_thermal_probe()
1281 error = PTR_ERR(thermal->pclk); in rockchip_thermal_probe()
1282 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n", in rockchip_thermal_probe()
1287 error = clk_prepare_enable(thermal->clk); in rockchip_thermal_probe()
1289 dev_err(&pdev->dev, "failed to enable converter clock: %d\n", in rockchip_thermal_probe()
1294 error = clk_prepare_enable(thermal->pclk); in rockchip_thermal_probe()
1296 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error); in rockchip_thermal_probe()
1300 rockchip_thermal_reset_controller(thermal->reset); in rockchip_thermal_probe()
1302 error = rockchip_configure_from_dt(&pdev->dev, np, thermal); in rockchip_thermal_probe()
1304 dev_err(&pdev->dev, "failed to parse device tree data: %d\n", in rockchip_thermal_probe()
1309 thermal->chip->initialize(thermal->grf, thermal->regs, in rockchip_thermal_probe()
1310 thermal->tshut_polarity); in rockchip_thermal_probe()
1312 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_probe()
1314 &thermal->sensors[i], in rockchip_thermal_probe()
1315 thermal->chip->chn_id[i]); in rockchip_thermal_probe()
1317 dev_err(&pdev->dev, in rockchip_thermal_probe()
1324 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, in rockchip_thermal_probe()
1329 dev_err(&pdev->dev, in rockchip_thermal_probe()
1334 thermal->chip->control(thermal->regs, true); in rockchip_thermal_probe()
1336 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_probe()
1337 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); in rockchip_thermal_probe()
1338 thermal->sensors[i].tzd->tzp->no_hwmon = false; in rockchip_thermal_probe()
1339 error = thermal_add_hwmon_sysfs(thermal->sensors[i].tzd); in rockchip_thermal_probe()
1341 dev_warn(&pdev->dev, in rockchip_thermal_probe()
1351 clk_disable_unprepare(thermal->pclk); in rockchip_thermal_probe()
1353 clk_disable_unprepare(thermal->clk); in rockchip_thermal_probe()
1363 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_remove()
1364 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i]; in rockchip_thermal_remove()
1366 thermal_remove_hwmon_sysfs(sensor->tzd); in rockchip_thermal_remove()
1370 thermal->chip->control(thermal->regs, false); in rockchip_thermal_remove()
1372 clk_disable_unprepare(thermal->pclk); in rockchip_thermal_remove()
1373 clk_disable_unprepare(thermal->clk); in rockchip_thermal_remove()
1383 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_suspend()
1384 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false); in rockchip_thermal_suspend()
1386 thermal->chip->control(thermal->regs, false); in rockchip_thermal_suspend()
1388 clk_disable(thermal->pclk); in rockchip_thermal_suspend()
1389 clk_disable(thermal->clk); in rockchip_thermal_suspend()
1402 error = clk_enable(thermal->clk); in rockchip_thermal_resume()
1406 error = clk_enable(thermal->pclk); in rockchip_thermal_resume()
1408 clk_disable(thermal->clk); in rockchip_thermal_resume()
1412 rockchip_thermal_reset_controller(thermal->reset); in rockchip_thermal_resume()
1414 thermal->chip->initialize(thermal->grf, thermal->regs, in rockchip_thermal_resume()
1415 thermal->tshut_polarity); in rockchip_thermal_resume()
1417 for (i = 0; i < thermal->chip->chn_num; i++) { in rockchip_thermal_resume()
1418 int id = thermal->sensors[i].id; in rockchip_thermal_resume()
1420 thermal->chip->set_tshut_mode(id, thermal->regs, in rockchip_thermal_resume()
1421 thermal->tshut_mode); in rockchip_thermal_resume()
1423 error = thermal->chip->set_tshut_temp(&thermal->chip->table, in rockchip_thermal_resume()
1424 id, thermal->regs, in rockchip_thermal_resume()
1425 thermal->tshut_temp); in rockchip_thermal_resume()
1428 __func__, thermal->tshut_temp, error); in rockchip_thermal_resume()
1431 thermal->chip->control(thermal->regs, true); in rockchip_thermal_resume()
1433 for (i = 0; i < thermal->chip->chn_num; i++) in rockchip_thermal_resume()
1434 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); in rockchip_thermal_resume()
1446 .name = "rockchip-thermal",
1459 MODULE_ALIAS("platform:rockchip-thermal");