26 #include <linux/module.h>
27 #include <linux/export.h>
29 #include <linux/kernel.h>
35 #include <linux/types.h>
37 #include <linux/reboot.h>
54 static int omap_bandgap_power(
struct omap_bandgap *bg_ptr,
bool on)
63 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
64 tsr = bg_ptr->
conf->sensors[
i].registers;
87 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
88 tsr = bg_ptr->
conf->sensors[
i].registers;
89 t_hot = omap_bandgap_readl(bg_ptr, tsr->
bgap_status);
93 t_cold = omap_bandgap_readl(bg_ptr, tsr->
bgap_status);
96 if (!t_cold && !t_hot)
120 if (bg_ptr->
conf->report_temperature)
121 bg_ptr->
conf->report_temperature(bg_ptr, i);
128 static irqreturn_t omap_bandgap_tshut_irq_handler(
int irq,
void *data)
136 int adc_to_temp_conversion(
struct omap_bandgap *bg_ptr,
int id,
int adc_val,
150 static int temp_to_adc_conversion(
long temp,
struct omap_bandgap *bg_ptr,
int i,
158 mid = (high +
low) / 2;
160 if (temp < bg_ptr->conv_table[low] || temp > bg_ptr->
conv_table[high])
164 if (temp < bg_ptr->conv_table[mid])
168 mid = (low +
high) / 2;
177 static int temp_sensor_unmask_interrupts(
struct omap_bandgap *bg_ptr,
int id,
184 tsr = bg_ptr->
conf->sensors[
id].registers;
204 int add_hyst(
int adc_val,
int hyst_val,
struct omap_bandgap *bg_ptr,
int i,
209 ret = adc_to_temp_conversion(bg_ptr, i, adc_val, &temp);
215 return temp_to_adc_conversion(temp, bg_ptr, i, sum);
220 int temp_sensor_configure_thot(
struct omap_bandgap *bg_ptr,
int id,
int t_hot)
227 tsr = bg_ptr->
conf->sensors[
id].registers;
235 err |= add_hyst(t_hot, -ts_data->
hyst_val, bg_ptr,
id, &cold);
248 dev_err(bg_ptr->
dev,
"failed to reprogram thot threshold\n");
252 return temp_sensor_unmask_interrupts(bg_ptr,
id, t_hot, cold);
257 int temp_sensor_init_talert_thresholds(
struct omap_bandgap *bg_ptr,
int id,
258 int t_hot,
int t_cold)
263 tsr = bg_ptr->
conf->sensors[
id].registers;
288 int temp_sensor_configure_tcold(
struct omap_bandgap *bg_ptr,
int id,
296 tsr = bg_ptr->
conf->sensors[
id].registers;
304 err |= add_hyst(t_cold, ts_data->
hyst_val, bg_ptr,
id, &hot);
317 dev_err(bg_ptr->
dev,
"failed to reprogram tcold threshold\n");
321 return temp_sensor_unmask_interrupts(bg_ptr,
id, hot, t_cold);
325 static int temp_sensor_configure_tshut_hot(
struct omap_bandgap *bg_ptr,
326 int id,
int tshut_hot)
331 tsr = bg_ptr->
conf->sensors[
id].registers;
341 static int temp_sensor_configure_tshut_cold(
struct omap_bandgap *bg_ptr,
342 int id,
int tshut_cold)
347 tsr = bg_ptr->
conf->sensors[
id].registers;
357 static int configure_temp_sensor_counter(
struct omap_bandgap *bg_ptr,
int id,
363 tsr = bg_ptr->
conf->sensors[
id].registers;
372 #define bandgap_is_valid(b) \
374 #define bandgap_is_valid_sensor_id(b, i) \
375 ((i) >= 0 && (i) < (b)->conf->sensor_count)
376 static inline int omap_bandgap_validate(
struct omap_bandgap *bg_ptr,
int id)
379 pr_err(
"%s: invalid bandgap pointer\n", __func__);
384 dev_err(bg_ptr->
dev,
"%s: sensor id out of range (%d)\n",
408 ret = omap_bandgap_validate(bg_ptr,
id);
415 tsr = bg_ptr->
conf->sensors[
id].registers;
419 ret |= adc_to_temp_conversion(bg_ptr,
id, temp, &temp);
421 dev_err(bg_ptr->
dev,
"failed to read thot\n");
445 ret = omap_bandgap_validate(bg_ptr,
id);
452 ts_data = bg_ptr->
conf->sensors[
id].ts_data;
453 tsr = bg_ptr->
conf->sensors[
id].registers;
455 if (val < ts_data->min_temp + ts_data->
hyst_val)
457 ret = temp_to_adc_conversion(val, bg_ptr,
id, &t_hot);
462 temp_sensor_configure_thot(bg_ptr,
id, t_hot);
483 ret = omap_bandgap_validate(bg_ptr,
id);
490 tsr = bg_ptr->
conf->sensors[
id].registers;
494 ret |= adc_to_temp_conversion(bg_ptr,
id, temp, &temp);
518 ret = omap_bandgap_validate(bg_ptr,
id);
525 ts_data = bg_ptr->
conf->sensors[
id].ts_data;
526 tsr = bg_ptr->
conf->sensors[
id].registers;
530 ret = temp_to_adc_conversion(val, bg_ptr,
id, &t_cold);
535 temp_sensor_configure_tcold(bg_ptr,
id, t_cold);
556 ret = omap_bandgap_validate(bg_ptr,
id);
563 tsr = bg_ptr->
conf->sensors[
id].registers;
568 time = time * 1000 / bg_ptr->
clk_rate;
586 int ret = omap_bandgap_validate(bg_ptr,
id);
593 interval = interval * bg_ptr->
clk_rate / 1000;
595 configure_temp_sensor_counter(bg_ptr,
id, interval);
616 ret = omap_bandgap_validate(bg_ptr,
id);
620 tsr = bg_ptr->
conf->sensors[
id].registers;
624 ret |= adc_to_temp_conversion(bg_ptr,
id, temp, &temp);
645 int ret = omap_bandgap_validate(bg_ptr,
id);
664 int ret = omap_bandgap_validate(bg_ptr,
id);
668 return bg_ptr->
conf->sensors[
id].data;
672 omap_bandgap_force_single_read(
struct omap_bandgap *bg_ptr,
int id)
677 tsr = bg_ptr->
conf->sensors[
id].registers;
692 while ((temp == 0) && --
counter) {
710 static int enable_continuous_mode(
struct omap_bandgap *bg_ptr)
716 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
718 omap_bandgap_force_single_read(bg_ptr, i);
719 tsr = bg_ptr->
conf->sensors[
i].registers;
728 static int omap_bandgap_tshut_init(
struct omap_bandgap *bg_ptr,
738 "Could not request for TSHUT GPIO:%i\n", 86);
744 "Cannot set input TSHUT GPIO %d\n", gpio_nr);
749 omap_bandgap_tshut_irq_handler,
754 dev_err(bg_ptr->
dev,
"request irq failed for TSHUT");
761 static int omap_bandgap_talert_init(
struct omap_bandgap *bg_ptr,
767 if (bg_ptr->
irq < 0) {
776 dev_err(&pdev->
dev,
"Request threaded irq failed.\n");
783 static const struct of_device_id of_omap_bandgap_match[];
795 dev_err(&pdev->
dev,
"no platform information available\n");
802 dev_err(&pdev->
dev,
"Unable to allocate mem for driver ref\n");
822 "failed to request the IO (%d:%pR).\n",
830 if (of_property_read_u32(node,
"ti,tshut-gpio", &prop) < 0) {
831 dev_err(&pdev->
dev,
"missing tshut gpio in device tree\n");
836 dev_err(&pdev->
dev,
"invalid gpio for tshut (%d)\n",
851 bg_ptr = omap_bandgap_build(pdev);
852 if (IS_ERR_OR_NULL(bg_ptr)) {
853 dev_err(&pdev->
dev,
"failed to fetch platform data\n");
854 return PTR_ERR(bg_ptr);
859 ret = omap_bandgap_tshut_init(bg_ptr, pdev);
862 "failed to initialize system tshut IRQ\n");
868 ret = IS_ERR_OR_NULL(bg_ptr->
fclock);
870 dev_err(&pdev->
dev,
"failed to request fclock reference\n");
875 ret = IS_ERR_OR_NULL(bg_ptr->
div_clk);
878 "failed to request div_ts_ck clock ref\n");
883 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
887 tsr = bg_ptr->
conf->sensors[
i].registers;
893 val = omap_bandgap_readl(bg_ptr, tsr->
bgap_efuse);
896 "Non-trimmed BGAP, Temp not accurate\n");
900 bg_ptr->
conf->sensors[0].ts_data->max_freq);
901 if (clk_rate < bg_ptr->conf->sensors[0].ts_data->min_freq ||
902 clk_rate == 0xffffffff) {
904 dev_err(&pdev->
dev,
"wrong clock rate (%d)\n", clk_rate);
910 dev_err(&pdev->
dev,
"Cannot re-set clock rate. Continuing\n");
917 platform_set_drvdata(pdev, bg_ptr);
919 omap_bandgap_power(bg_ptr,
true);
923 for (i = 0; i < bg_ptr->
conf->sensor_count; i++)
924 configure_temp_sensor_counter(bg_ptr, i, 1);
926 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
929 ts_data = bg_ptr->
conf->sensors[
i].ts_data;
932 temp_sensor_init_talert_thresholds(bg_ptr, i,
936 temp_sensor_configure_tshut_hot(bg_ptr, i,
938 temp_sensor_configure_tshut_cold(bg_ptr, i,
944 enable_continuous_mode(bg_ptr);
948 for (i = 0; i < bg_ptr->
conf->sensor_count; i++)
949 configure_temp_sensor_counter(bg_ptr, i,
953 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
956 if (bg_ptr->
conf->sensors[i].register_cooling)
957 bg_ptr->
conf->sensors[
i].register_cooling(bg_ptr, i);
959 domain = bg_ptr->
conf->sensors[
i].domain;
960 if (bg_ptr->
conf->expose_sensor)
961 bg_ptr->
conf->expose_sensor(bg_ptr, i, domain);
970 ret = omap_bandgap_talert_init(bg_ptr, pdev);
972 dev_err(&pdev->
dev,
"failed to initialize Talert IRQ\n");
973 i = bg_ptr->
conf->sensor_count;
997 struct omap_bandgap *bg_ptr = platform_get_drvdata(pdev);
1001 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
1002 if (bg_ptr->
conf->sensors[i].register_cooling)
1003 bg_ptr->
conf->sensors[
i].unregister_cooling(bg_ptr, i);
1005 if (bg_ptr->
conf->remove_sensor)
1006 bg_ptr->
conf->remove_sensor(bg_ptr, i);
1009 omap_bandgap_power(bg_ptr,
false);
1027 static int omap_bandgap_save_ctxt(
struct omap_bandgap *bg_ptr)
1031 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
1035 rval = &bg_ptr->
conf->sensors[
i].regval;
1036 tsr = bg_ptr->
conf->sensors[
i].registers;
1042 rval->
bg_counter = omap_bandgap_readl(bg_ptr,
1047 rval->
bg_ctrl = omap_bandgap_readl(bg_ptr,
1059 static int omap_bandgap_restore_ctxt(
struct omap_bandgap *bg_ptr)
1064 for (i = 0; i < bg_ptr->
conf->sensor_count; i++) {
1069 rval = &bg_ptr->
conf->sensors[
i].regval;
1070 tsr = bg_ptr->
conf->sensors[
i].registers;
1077 omap_bandgap_writel(bg_ptr,
1083 omap_bandgap_force_single_read(bg_ptr, i);
1086 omap_bandgap_writel(bg_ptr, rval->
bg_counter,
1092 omap_bandgap_writel(bg_ptr,
1095 omap_bandgap_writel(bg_ptr, rval->
bg_ctrl,
1099 temp = omap_bandgap_readl(bg_ptr,
1102 omap_bandgap_force_single_read(bg_ptr, i);
1104 temp = omap_bandgap_readl(bg_ptr,
1107 omap_bandgap_writel(bg_ptr, temp,
1116 static int omap_bandgap_suspend(
struct device *
dev)
1121 err = omap_bandgap_save_ctxt(bg_ptr);
1122 omap_bandgap_power(bg_ptr,
false);
1128 static int omap_bandgap_resume(
struct device *
dev)
1133 omap_bandgap_power(bg_ptr,
true);
1135 return omap_bandgap_restore_ctxt(bg_ptr);
1137 static const struct dev_pm_ops omap_bandgap_dev_pm_ops = {
1139 omap_bandgap_resume)
1142 #define DEV_PM_OPS (&omap_bandgap_dev_pm_ops)
1144 #define DEV_PM_OPS NULL
1147 static const struct of_device_id of_omap_bandgap_match[] = {
1148 #ifdef CONFIG_OMAP4_THERMAL
1150 .compatible =
"ti,omap4430-bandgap",
1154 .compatible =
"ti,omap4460-bandgap",
1158 .compatible =
"ti,omap4470-bandgap",
1162 #ifdef CONFIG_OMAP5_THERMAL
1164 .compatible =
"ti,omap5430-bandgap",
1174 .
probe = omap_bandgap_probe,
1175 .remove = omap_bandgap_remove,
1177 .name =
"omap-bandgap",
1179 .of_match_table = of_omap_bandgap_match,