Lines Matching +full:alert +full:- +full:celsius

1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
6 * Author: J Keerthy <j-keerthy@ti.com>
7 * Author: Moiz Sonasath <m-sonasath@ti.com>
36 #include "ti-bandgap.h"
47 * ti_bandgap_readl() - simple read helper function
56 return readl(bgp->base + reg); in ti_bandgap_readl()
60 * ti_bandgap_writel() - simple write helper function
69 writel(val, bgp->base + reg); in ti_bandgap_writel()
75 * RMW_BITS() - used to read, modify and update bandgap bitfields.
83 t = bgp->conf->sensors[(id)].registers; \
84 r = ti_bandgap_readl(bgp, t->reg); \
85 r &= ~t->mask; \
86 r |= (val) << __ffs(t->mask); \
87 ti_bandgap_writel(bgp, r, t->reg); \
93 * ti_bandgap_power() - controls the power state of a bandgap device
95 * @on: desired power state (1 - on, 0 - off)
100 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
107 return -ENOTSUPP; in ti_bandgap_power()
109 for (i = 0; i < bgp->conf->sensor_count; i++) in ti_bandgap_power()
116 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
122 * corrupted" - Errata ID: i814".
147 * ti_bandgap_read_temp() - helper function to read sensor temperature
163 tsr = bgp->conf->sensors[id].registers; in ti_bandgap_read_temp()
164 reg = tsr->temp_sensor_ctrl; in ti_bandgap_read_temp()
172 reg = tsr->ctrl_dtemp_1; in ti_bandgap_read_temp()
181 temp &= tsr->bgap_dtemp_mask; in ti_bandgap_read_temp()
192 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
211 spin_lock(&bgp->lock); in ti_bandgap_talert_irq_handler()
212 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_talert_irq_handler()
213 tsr = bgp->conf->sensors[i].registers; in ti_bandgap_talert_irq_handler()
214 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status); in ti_bandgap_talert_irq_handler()
217 t_hot = ctrl & tsr->status_hot_mask; in ti_bandgap_talert_irq_handler()
220 t_cold = ctrl & tsr->status_cold_mask; in ti_bandgap_talert_irq_handler()
225 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); in ti_bandgap_talert_irq_handler()
232 ctrl &= ~tsr->mask_hot_mask; in ti_bandgap_talert_irq_handler()
233 ctrl |= tsr->mask_cold_mask; in ti_bandgap_talert_irq_handler()
235 ctrl &= ~tsr->mask_cold_mask; in ti_bandgap_talert_irq_handler()
236 ctrl |= tsr->mask_hot_mask; in ti_bandgap_talert_irq_handler()
239 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl); in ti_bandgap_talert_irq_handler()
241 dev_dbg(bgp->dev, in ti_bandgap_talert_irq_handler()
243 __func__, bgp->conf->sensors[i].domain, in ti_bandgap_talert_irq_handler()
247 if (bgp->conf->report_temperature) in ti_bandgap_talert_irq_handler()
248 bgp->conf->report_temperature(bgp, i); in ti_bandgap_talert_irq_handler()
250 spin_unlock(&bgp->lock); in ti_bandgap_talert_irq_handler()
256 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
276 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
279 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
285 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
288 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
294 const struct ti_bandgap_data *conf = bgp->conf; in ti_bandgap_adc_to_mcelsius()
297 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) in ti_bandgap_adc_to_mcelsius()
298 return -ERANGE; in ti_bandgap_adc_to_mcelsius()
300 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val]; in ti_bandgap_adc_to_mcelsius()
305 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
312 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
319 return -EINVAL; in ti_bandgap_validate()
322 if ((id < 0) || (id >= bgp->conf->sensor_count)) { in ti_bandgap_validate()
323 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n", in ti_bandgap_validate()
325 return -ERANGE; in ti_bandgap_validate()
332 * ti_bandgap_read_counter() - read the sensor counter
343 tsr = bgp->conf->sensors[id].registers; in ti_bandgap_read_counter()
344 time = ti_bandgap_readl(bgp, tsr->bgap_counter); in ti_bandgap_read_counter()
345 time = (time & tsr->counter_mask) >> in ti_bandgap_read_counter()
346 __ffs(tsr->counter_mask); in ti_bandgap_read_counter()
347 time = time * 1000 / bgp->clk_rate; in ti_bandgap_read_counter()
352 * ti_bandgap_read_counter_delay() - read the sensor counter delay
363 tsr = bgp->conf->sensors[id].registers; in ti_bandgap_read_counter_delay()
365 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); in ti_bandgap_read_counter_delay()
366 reg_val = (reg_val & tsr->mask_counter_delay_mask) >> in ti_bandgap_read_counter_delay()
367 __ffs(tsr->mask_counter_delay_mask); in ti_bandgap_read_counter_delay()
388 dev_warn(bgp->dev, "Wrong counter delay value read from register %X", in ti_bandgap_read_counter_delay()
394 * ti_bandgap_read_update_interval() - read the sensor update interval
412 ret = -ENOTSUPP; in ti_bandgap_read_update_interval()
427 * ti_bandgap_write_counter_delay() - set the counter_delay
459 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval); in ti_bandgap_write_counter_delay()
460 return -EINVAL; in ti_bandgap_write_counter_delay()
463 spin_lock(&bgp->lock); in ti_bandgap_write_counter_delay()
465 spin_unlock(&bgp->lock); in ti_bandgap_write_counter_delay()
471 * ti_bandgap_write_counter() - set the bandgap sensor counter
479 interval = interval * bgp->clk_rate / 1000; in ti_bandgap_write_counter()
480 spin_lock(&bgp->lock); in ti_bandgap_write_counter()
482 spin_unlock(&bgp->lock); in ti_bandgap_write_counter()
486 * ti_bandgap_write_update_interval() - set the update interval
502 ret = -ENOTSUPP; in ti_bandgap_write_update_interval()
517 * ti_bandgap_read_temperature() - report current temperature
540 spin_lock(&bgp->lock); in ti_bandgap_read_temperature()
542 spin_unlock(&bgp->lock); in ti_bandgap_read_temperature()
546 return -EIO; in ti_bandgap_read_temperature()
554 * ti_bandgap_set_sensor_data() - helper function to store thermal
568 bgp->regval[id].data = data; in ti_bandgap_set_sensor_data()
574 * ti_bandgap_get_sensor_data() - helper function to get thermal
587 return bgp->regval[id].data; in ti_bandgap_get_sensor_data()
593 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
616 tsr = bgp->conf->sensors[id].registers; in ti_bandgap_force_single_read()
618 while (--counter) { in ti_bandgap_force_single_read()
619 if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) & in ti_bandgap_force_single_read()
620 tsr->bgap_eocz_mask) in ti_bandgap_force_single_read()
629 while (--counter) { in ti_bandgap_force_single_read()
630 if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) & in ti_bandgap_force_single_read()
631 tsr->bgap_eocz_mask)) in ti_bandgap_force_single_read()
639 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
653 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_set_continuous_mode()
663 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
671 * are read from the buffer and for those without the Buffer -ENOTSUPP is
689 ret = -ENOTSUPP; in ti_bandgap_get_trend()
693 spin_lock(&bgp->lock); in ti_bandgap_get_trend()
695 tsr = bgp->conf->sensors[id].registers; in ti_bandgap_get_trend()
699 reg1 = tsr->ctrl_dtemp_1; in ti_bandgap_get_trend()
700 reg2 = tsr->ctrl_dtemp_2; in ti_bandgap_get_trend()
704 temp1 &= tsr->bgap_dtemp_mask; in ti_bandgap_get_trend()
707 temp2 &= tsr->bgap_dtemp_mask; in ti_bandgap_get_trend()
727 *trend = (t1 - t2) / interval; in ti_bandgap_get_trend()
729 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n", in ti_bandgap_get_trend()
734 spin_unlock(&bgp->lock); in ti_bandgap_get_trend()
740 * ti_bandgap_tshut_init() - setup and initialize tshut handling
758 status = request_irq(gpiod_to_irq(bgp->tshut_gpiod), in ti_bandgap_tshut_init()
762 dev_err(bgp->dev, "request irq failed for TSHUT"); in ti_bandgap_tshut_init()
768 * ti_bandgap_alert_init() - setup and initialize talert handling
785 bgp->irq = platform_get_irq(pdev, 0); in ti_bandgap_talert_init()
786 if (bgp->irq < 0) in ti_bandgap_talert_init()
787 return bgp->irq; in ti_bandgap_talert_init()
789 ret = request_threaded_irq(bgp->irq, NULL, in ti_bandgap_talert_init()
794 dev_err(&pdev->dev, "Request threaded irq failed.\n"); in ti_bandgap_talert_init()
803 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
815 struct device_node *node = pdev->dev.of_node; in ti_bandgap_build()
823 dev_err(&pdev->dev, "no platform information available\n"); in ti_bandgap_build()
824 return ERR_PTR(-EINVAL); in ti_bandgap_build()
827 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL); in ti_bandgap_build()
829 return ERR_PTR(-ENOMEM); in ti_bandgap_build()
831 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev); in ti_bandgap_build()
833 bgp->conf = of_id->data; in ti_bandgap_build()
836 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count, in ti_bandgap_build()
837 sizeof(*bgp->regval), GFP_KERNEL); in ti_bandgap_build()
838 if (!bgp->regval) in ti_bandgap_build()
839 return ERR_PTR(-ENOMEM); in ti_bandgap_build()
848 chunk = devm_ioremap_resource(&pdev->dev, res); in ti_bandgap_build()
850 bgp->base = chunk; in ti_bandgap_build()
858 bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN); in ti_bandgap_build()
859 if (IS_ERR(bgp->tshut_gpiod)) { in ti_bandgap_build()
860 dev_err(&pdev->dev, "invalid gpio for tshut\n"); in ti_bandgap_build()
861 return ERR_CAST(bgp->tshut_gpiod); in ti_bandgap_build()
889 dev_err(&pdev->dev, "failed to fetch platform data\n"); in ti_bandgap_probe()
892 bgp->dev = &pdev->dev; in ti_bandgap_probe()
895 dev_warn(&pdev->dev, in ti_bandgap_probe()
901 dev_err(&pdev->dev, in ti_bandgap_probe()
907 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name); in ti_bandgap_probe()
908 if (IS_ERR(bgp->fclock)) { in ti_bandgap_probe()
909 dev_err(&pdev->dev, "failed to request fclock reference\n"); in ti_bandgap_probe()
910 ret = PTR_ERR(bgp->fclock); in ti_bandgap_probe()
914 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name); in ti_bandgap_probe()
915 if (IS_ERR(bgp->div_clk)) { in ti_bandgap_probe()
916 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n"); in ti_bandgap_probe()
917 ret = PTR_ERR(bgp->div_clk); in ti_bandgap_probe()
921 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_probe()
925 tsr = bgp->conf->sensors[i].registers; in ti_bandgap_probe()
927 * check if the efuse has a non-zero value if not in ti_bandgap_probe()
931 val = ti_bandgap_readl(bgp, tsr->bgap_efuse); in ti_bandgap_probe()
933 dev_info(&pdev->dev, in ti_bandgap_probe()
934 "Non-trimmed BGAP, Temp not accurate\n"); in ti_bandgap_probe()
937 clk_rate = clk_round_rate(bgp->div_clk, in ti_bandgap_probe()
938 bgp->conf->sensors[0].ts_data->max_freq); in ti_bandgap_probe()
939 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq || in ti_bandgap_probe()
941 ret = -ENODEV; in ti_bandgap_probe()
942 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate); in ti_bandgap_probe()
946 ret = clk_set_rate(bgp->div_clk, clk_rate); in ti_bandgap_probe()
948 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n"); in ti_bandgap_probe()
950 bgp->clk_rate = clk_rate; in ti_bandgap_probe()
952 clk_prepare_enable(bgp->fclock); in ti_bandgap_probe()
955 spin_lock_init(&bgp->lock); in ti_bandgap_probe()
956 bgp->dev = &pdev->dev; in ti_bandgap_probe()
963 for (i = 0; i < bgp->conf->sensor_count; i++) in ti_bandgap_probe()
966 /* Set default thresholds for alert and shutdown */ in ti_bandgap_probe()
967 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_probe()
970 ts_data = bgp->conf->sensors[i].ts_data; in ti_bandgap_probe()
975 threshold_tcold_mask, ts_data->t_cold); in ti_bandgap_probe()
977 threshold_thot_mask, ts_data->t_hot); in ti_bandgap_probe()
978 /* Enable the alert events */ in ti_bandgap_probe()
986 tshut_hot_mask, ts_data->tshut_hot); in ti_bandgap_probe()
988 tshut_cold_mask, ts_data->tshut_cold); in ti_bandgap_probe()
997 for (i = 0; i < bgp->conf->sensor_count; i++) in ti_bandgap_probe()
999 bgp->clk_rate / 4); in ti_bandgap_probe()
1002 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_probe()
1005 if (bgp->conf->sensors[i].register_cooling) { in ti_bandgap_probe()
1006 ret = bgp->conf->sensors[i].register_cooling(bgp, i); in ti_bandgap_probe()
1011 if (bgp->conf->expose_sensor) { in ti_bandgap_probe()
1012 domain = bgp->conf->sensors[i].domain; in ti_bandgap_probe()
1013 ret = bgp->conf->expose_sensor(bgp, i, domain); in ti_bandgap_probe()
1027 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n"); in ti_bandgap_probe()
1028 i = bgp->conf->sensor_count; in ti_bandgap_probe()
1034 bgp->nb.notifier_call = bandgap_omap_cpu_notifier; in ti_bandgap_probe()
1036 cpu_pm_register_notifier(&bgp->nb); in ti_bandgap_probe()
1042 if (bgp->conf->sensors[i].unregister_cooling) in ti_bandgap_probe()
1043 bgp->conf->sensors[i].unregister_cooling(bgp, i); in ti_bandgap_probe()
1045 for (i--; i >= 0; i--) { in ti_bandgap_probe()
1046 if (bgp->conf->sensors[i].unregister_cooling) in ti_bandgap_probe()
1047 bgp->conf->sensors[i].unregister_cooling(bgp, i); in ti_bandgap_probe()
1048 if (bgp->conf->remove_sensor) in ti_bandgap_probe()
1049 bgp->conf->remove_sensor(bgp, i); in ti_bandgap_probe()
1054 clk_disable_unprepare(bgp->fclock); in ti_bandgap_probe()
1056 clk_put(bgp->div_clk); in ti_bandgap_probe()
1058 clk_put(bgp->fclock); in ti_bandgap_probe()
1061 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL); in ti_bandgap_probe()
1073 cpu_pm_unregister_notifier(&bgp->nb); in ti_bandgap_remove()
1076 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_remove()
1077 if (bgp->conf->sensors[i].unregister_cooling) in ti_bandgap_remove()
1078 bgp->conf->sensors[i].unregister_cooling(bgp, i); in ti_bandgap_remove()
1080 if (bgp->conf->remove_sensor) in ti_bandgap_remove()
1081 bgp->conf->remove_sensor(bgp, i); in ti_bandgap_remove()
1087 clk_disable_unprepare(bgp->fclock); in ti_bandgap_remove()
1088 clk_put(bgp->fclock); in ti_bandgap_remove()
1089 clk_put(bgp->div_clk); in ti_bandgap_remove()
1092 free_irq(bgp->irq, bgp); in ti_bandgap_remove()
1095 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL); in ti_bandgap_remove()
1105 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_save_ctxt()
1109 rval = &bgp->regval[i]; in ti_bandgap_save_ctxt()
1110 tsr = bgp->conf->sensors[i].registers; in ti_bandgap_save_ctxt()
1113 rval->bg_mode_ctrl = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1114 tsr->bgap_mode_ctrl); in ti_bandgap_save_ctxt()
1116 rval->bg_counter = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1117 tsr->bgap_counter); in ti_bandgap_save_ctxt()
1119 rval->bg_threshold = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1120 tsr->bgap_threshold); in ti_bandgap_save_ctxt()
1121 rval->bg_ctrl = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1122 tsr->bgap_mask_ctrl); in ti_bandgap_save_ctxt()
1126 rval->tshut_threshold = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1127 tsr->tshut_threshold); in ti_bandgap_save_ctxt()
1137 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_restore_ctxt()
1142 rval = &bgp->regval[i]; in ti_bandgap_restore_ctxt()
1143 tsr = bgp->conf->sensors[i].registers; in ti_bandgap_restore_ctxt()
1146 val = ti_bandgap_readl(bgp, tsr->bgap_counter); in ti_bandgap_restore_ctxt()
1149 ti_bandgap_writel(bgp, rval->tshut_threshold, in ti_bandgap_restore_ctxt()
1150 tsr->tshut_threshold); in ti_bandgap_restore_ctxt()
1157 ti_bandgap_writel(bgp, rval->bg_counter, in ti_bandgap_restore_ctxt()
1158 tsr->bgap_counter); in ti_bandgap_restore_ctxt()
1160 ti_bandgap_writel(bgp, rval->bg_mode_ctrl, in ti_bandgap_restore_ctxt()
1161 tsr->bgap_mode_ctrl); in ti_bandgap_restore_ctxt()
1163 ti_bandgap_writel(bgp, rval->bg_threshold, in ti_bandgap_restore_ctxt()
1164 tsr->bgap_threshold); in ti_bandgap_restore_ctxt()
1165 ti_bandgap_writel(bgp, rval->bg_ctrl, in ti_bandgap_restore_ctxt()
1166 tsr->bgap_mask_ctrl); in ti_bandgap_restore_ctxt()
1182 clk_disable_unprepare(bgp->fclock); in ti_bandgap_suspend()
1184 bgp->is_suspended = true; in ti_bandgap_suspend()
1196 spin_lock(&bgp->lock); in bandgap_omap_cpu_notifier()
1199 if (bgp->is_suspended) in bandgap_omap_cpu_notifier()
1204 clk_disable(bgp->fclock); in bandgap_omap_cpu_notifier()
1208 if (bgp->is_suspended) in bandgap_omap_cpu_notifier()
1211 clk_enable(bgp->fclock); in bandgap_omap_cpu_notifier()
1216 spin_unlock(&bgp->lock); in bandgap_omap_cpu_notifier()
1226 clk_prepare_enable(bgp->fclock); in ti_bandgap_resume()
1229 bgp->is_suspended = false; in ti_bandgap_resume()
1244 .compatible = "ti,omap34xx-bandgap",
1248 .compatible = "ti,omap36xx-bandgap",
1254 .compatible = "ti,omap4430-bandgap",
1258 .compatible = "ti,omap4460-bandgap",
1262 .compatible = "ti,omap4470-bandgap",
1268 .compatible = "ti,omap5430-bandgap",
1274 .compatible = "ti,dra752-bandgap",
1287 .name = "ti-soc-thermal",
1297 MODULE_ALIAS("platform:ti-soc-thermal");