Lines Matching +full:adc +full:- +full:use +full:- +full:res

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
156 * Return: temperature in ADC values.
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
196 * This is the Talert handler. Use it only if bandgap device features
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
260 * This is the Tshut handler. Use it only if bandgap device features
276 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
279 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
281 * @adc_val: value in ADC representation
284 * Simple conversion from ADC representation to mCelsius. In case the ADC value
285 * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
286 * The conversion table is indexed by the ADC values.
288 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
289 * argument is out of the ADC conv table range.
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
605 struct temp_sensor_registers *tsr = bgp->conf->sensors[id].registers; in ti_bandgap_force_single_read()
606 void __iomem *temp_sensor_ctrl = bgp->base + tsr->temp_sensor_ctrl; in ti_bandgap_force_single_read()
619 if (tsr->bgap_soc_mask) { in ti_bandgap_force_single_read()
624 val & tsr->bgap_eocz_mask, in ti_bandgap_force_single_read()
627 dev_warn(bgp->dev, "eocz timed out waiting high\n"); in ti_bandgap_force_single_read()
635 !(val & tsr->bgap_eocz_mask), in ti_bandgap_force_single_read()
638 dev_warn(bgp->dev, "eocz timed out waiting low\n"); in ti_bandgap_force_single_read()
644 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
658 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_set_continuous_mode()
668 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
676 * are read from the buffer and for those without the Buffer -ENOTSUPP is
694 ret = -ENOTSUPP; in ti_bandgap_get_trend()
698 spin_lock(&bgp->lock); in ti_bandgap_get_trend()
700 tsr = bgp->conf->sensors[id].registers; in ti_bandgap_get_trend()
704 reg1 = tsr->ctrl_dtemp_1; in ti_bandgap_get_trend()
705 reg2 = tsr->ctrl_dtemp_2; in ti_bandgap_get_trend()
709 temp1 &= tsr->bgap_dtemp_mask; in ti_bandgap_get_trend()
712 temp2 &= tsr->bgap_dtemp_mask; in ti_bandgap_get_trend()
714 /* Convert from adc values to mCelsius temperature */ in ti_bandgap_get_trend()
732 *trend = (t1 - t2) / interval; in ti_bandgap_get_trend()
734 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n", in ti_bandgap_get_trend()
739 spin_unlock(&bgp->lock); in ti_bandgap_get_trend()
745 * ti_bandgap_tshut_init() - setup and initialize tshut handling
763 status = request_irq(gpiod_to_irq(bgp->tshut_gpiod), in ti_bandgap_tshut_init()
767 dev_err(bgp->dev, "request irq failed for TSHUT"); in ti_bandgap_tshut_init()
773 * ti_bandgap_talert_init() - setup and initialize talert handling
790 bgp->irq = platform_get_irq(pdev, 0); in ti_bandgap_talert_init()
791 if (bgp->irq < 0) in ti_bandgap_talert_init()
792 return bgp->irq; in ti_bandgap_talert_init()
794 ret = request_threaded_irq(bgp->irq, NULL, in ti_bandgap_talert_init()
799 dev_err(&pdev->dev, "Request threaded irq failed.\n"); in ti_bandgap_talert_init()
808 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
820 struct device_node *node = pdev->dev.of_node; in ti_bandgap_build()
823 struct resource *res; in ti_bandgap_build() local
828 dev_err(&pdev->dev, "no platform information available\n"); in ti_bandgap_build()
829 return ERR_PTR(-EINVAL); in ti_bandgap_build()
832 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL); in ti_bandgap_build()
834 return ERR_PTR(-ENOMEM); in ti_bandgap_build()
836 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev); in ti_bandgap_build()
838 bgp->conf = of_id->data; in ti_bandgap_build()
841 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count, in ti_bandgap_build()
842 sizeof(*bgp->regval), GFP_KERNEL); in ti_bandgap_build()
843 if (!bgp->regval) in ti_bandgap_build()
844 return ERR_PTR(-ENOMEM); in ti_bandgap_build()
850 res = platform_get_resource(pdev, IORESOURCE_MEM, i); in ti_bandgap_build()
851 if (!res) in ti_bandgap_build()
853 chunk = devm_ioremap_resource(&pdev->dev, res); in ti_bandgap_build()
855 bgp->base = chunk; in ti_bandgap_build()
860 } while (res); in ti_bandgap_build()
863 bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN); in ti_bandgap_build()
864 if (IS_ERR(bgp->tshut_gpiod)) { in ti_bandgap_build()
865 dev_err(&pdev->dev, "invalid gpio for tshut\n"); in ti_bandgap_build()
866 return ERR_CAST(bgp->tshut_gpiod); in ti_bandgap_build()
894 dev_err(&pdev->dev, "failed to fetch platform data\n"); in ti_bandgap_probe()
897 bgp->dev = &pdev->dev; in ti_bandgap_probe()
900 dev_warn(&pdev->dev, in ti_bandgap_probe()
906 dev_err(&pdev->dev, in ti_bandgap_probe()
912 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name); in ti_bandgap_probe()
913 if (IS_ERR(bgp->fclock)) { in ti_bandgap_probe()
914 dev_err(&pdev->dev, "failed to request fclock reference\n"); in ti_bandgap_probe()
915 ret = PTR_ERR(bgp->fclock); in ti_bandgap_probe()
919 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name); in ti_bandgap_probe()
920 if (IS_ERR(bgp->div_clk)) { in ti_bandgap_probe()
921 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n"); in ti_bandgap_probe()
922 ret = PTR_ERR(bgp->div_clk); in ti_bandgap_probe()
926 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_probe()
930 tsr = bgp->conf->sensors[i].registers; in ti_bandgap_probe()
932 * check if the efuse has a non-zero value if not in ti_bandgap_probe()
936 val = ti_bandgap_readl(bgp, tsr->bgap_efuse); in ti_bandgap_probe()
938 dev_info(&pdev->dev, in ti_bandgap_probe()
939 "Non-trimmed BGAP, Temp not accurate\n"); in ti_bandgap_probe()
942 clk_rate = clk_round_rate(bgp->div_clk, in ti_bandgap_probe()
943 bgp->conf->sensors[0].ts_data->max_freq); in ti_bandgap_probe()
944 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq || in ti_bandgap_probe()
946 ret = -ENODEV; in ti_bandgap_probe()
947 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate); in ti_bandgap_probe()
951 ret = clk_set_rate(bgp->div_clk, clk_rate); in ti_bandgap_probe()
953 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n"); in ti_bandgap_probe()
955 bgp->clk_rate = clk_rate; in ti_bandgap_probe()
957 clk_prepare_enable(bgp->fclock); in ti_bandgap_probe()
960 spin_lock_init(&bgp->lock); in ti_bandgap_probe()
961 bgp->dev = &pdev->dev; in ti_bandgap_probe()
968 for (i = 0; i < bgp->conf->sensor_count; i++) in ti_bandgap_probe()
972 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_probe()
975 ts_data = bgp->conf->sensors[i].ts_data; in ti_bandgap_probe()
980 threshold_tcold_mask, ts_data->t_cold); in ti_bandgap_probe()
982 threshold_thot_mask, ts_data->t_hot); in ti_bandgap_probe()
991 tshut_hot_mask, ts_data->tshut_hot); in ti_bandgap_probe()
993 tshut_cold_mask, ts_data->tshut_cold); in ti_bandgap_probe()
1002 for (i = 0; i < bgp->conf->sensor_count; i++) in ti_bandgap_probe()
1004 bgp->clk_rate / 4); in ti_bandgap_probe()
1007 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_probe()
1010 if (bgp->conf->sensors[i].register_cooling) { in ti_bandgap_probe()
1011 ret = bgp->conf->sensors[i].register_cooling(bgp, i); in ti_bandgap_probe()
1016 if (bgp->conf->expose_sensor) { in ti_bandgap_probe()
1017 domain = bgp->conf->sensors[i].domain; in ti_bandgap_probe()
1018 ret = bgp->conf->expose_sensor(bgp, i, domain); in ti_bandgap_probe()
1032 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n"); in ti_bandgap_probe()
1033 i = bgp->conf->sensor_count; in ti_bandgap_probe()
1039 bgp->nb.notifier_call = bandgap_omap_cpu_notifier; in ti_bandgap_probe()
1041 cpu_pm_register_notifier(&bgp->nb); in ti_bandgap_probe()
1047 if (bgp->conf->sensors[i].unregister_cooling) in ti_bandgap_probe()
1048 bgp->conf->sensors[i].unregister_cooling(bgp, i); in ti_bandgap_probe()
1050 for (i--; i >= 0; i--) { in ti_bandgap_probe()
1051 if (bgp->conf->sensors[i].unregister_cooling) in ti_bandgap_probe()
1052 bgp->conf->sensors[i].unregister_cooling(bgp, i); in ti_bandgap_probe()
1053 if (bgp->conf->remove_sensor) in ti_bandgap_probe()
1054 bgp->conf->remove_sensor(bgp, i); in ti_bandgap_probe()
1059 clk_disable_unprepare(bgp->fclock); in ti_bandgap_probe()
1061 clk_put(bgp->div_clk); in ti_bandgap_probe()
1063 clk_put(bgp->fclock); in ti_bandgap_probe()
1066 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL); in ti_bandgap_probe()
1078 cpu_pm_unregister_notifier(&bgp->nb); in ti_bandgap_remove()
1081 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_remove()
1082 if (bgp->conf->sensors[i].unregister_cooling) in ti_bandgap_remove()
1083 bgp->conf->sensors[i].unregister_cooling(bgp, i); in ti_bandgap_remove()
1085 if (bgp->conf->remove_sensor) in ti_bandgap_remove()
1086 bgp->conf->remove_sensor(bgp, i); in ti_bandgap_remove()
1092 clk_disable_unprepare(bgp->fclock); in ti_bandgap_remove()
1093 clk_put(bgp->fclock); in ti_bandgap_remove()
1094 clk_put(bgp->div_clk); in ti_bandgap_remove()
1097 free_irq(bgp->irq, bgp); in ti_bandgap_remove()
1100 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL); in ti_bandgap_remove()
1108 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_save_ctxt()
1112 rval = &bgp->regval[i]; in ti_bandgap_save_ctxt()
1113 tsr = bgp->conf->sensors[i].registers; in ti_bandgap_save_ctxt()
1116 rval->bg_mode_ctrl = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1117 tsr->bgap_mode_ctrl); in ti_bandgap_save_ctxt()
1119 rval->bg_counter = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1120 tsr->bgap_counter); in ti_bandgap_save_ctxt()
1122 rval->bg_threshold = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1123 tsr->bgap_threshold); in ti_bandgap_save_ctxt()
1124 rval->bg_ctrl = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1125 tsr->bgap_mask_ctrl); in ti_bandgap_save_ctxt()
1129 rval->tshut_threshold = ti_bandgap_readl(bgp, in ti_bandgap_save_ctxt()
1130 tsr->tshut_threshold); in ti_bandgap_save_ctxt()
1140 for (i = 0; i < bgp->conf->sensor_count; i++) { in ti_bandgap_restore_ctxt()
1144 rval = &bgp->regval[i]; in ti_bandgap_restore_ctxt()
1145 tsr = bgp->conf->sensors[i].registers; in ti_bandgap_restore_ctxt()
1148 ti_bandgap_writel(bgp, rval->tshut_threshold, in ti_bandgap_restore_ctxt()
1149 tsr->tshut_threshold); in ti_bandgap_restore_ctxt()
1156 ti_bandgap_writel(bgp, rval->bg_counter, in ti_bandgap_restore_ctxt()
1157 tsr->bgap_counter); in ti_bandgap_restore_ctxt()
1159 ti_bandgap_writel(bgp, rval->bg_mode_ctrl, in ti_bandgap_restore_ctxt()
1160 tsr->bgap_mode_ctrl); in ti_bandgap_restore_ctxt()
1162 ti_bandgap_writel(bgp, rval->bg_threshold, in ti_bandgap_restore_ctxt()
1163 tsr->bgap_threshold); in ti_bandgap_restore_ctxt()
1164 ti_bandgap_writel(bgp, rval->bg_ctrl, in ti_bandgap_restore_ctxt()
1165 tsr->bgap_mask_ctrl); in ti_bandgap_restore_ctxt()
1181 clk_disable_unprepare(bgp->fclock); in ti_bandgap_suspend()
1183 bgp->is_suspended = true; in ti_bandgap_suspend()
1195 spin_lock(&bgp->lock); in bandgap_omap_cpu_notifier()
1198 if (bgp->is_suspended) in bandgap_omap_cpu_notifier()
1203 clk_disable(bgp->fclock); in bandgap_omap_cpu_notifier()
1207 if (bgp->is_suspended) in bandgap_omap_cpu_notifier()
1210 clk_enable(bgp->fclock); in bandgap_omap_cpu_notifier()
1215 spin_unlock(&bgp->lock); in bandgap_omap_cpu_notifier()
1225 clk_prepare_enable(bgp->fclock); in ti_bandgap_resume()
1228 bgp->is_suspended = false; in ti_bandgap_resume()
1243 .compatible = "ti,omap34xx-bandgap",
1247 .compatible = "ti,omap36xx-bandgap",
1253 .compatible = "ti,omap4430-bandgap",
1257 .compatible = "ti,omap4460-bandgap",
1261 .compatible = "ti,omap4470-bandgap",
1267 .compatible = "ti,omap5430-bandgap",
1273 .compatible = "ti,dra752-bandgap",
1286 .name = "ti-soc-thermal",
1296 MODULE_ALIAS("platform:ti-soc-thermal");