2 #include <linux/delay.h>
3 #include <linux/module.h>
4 #include <linux/thermal.h>
5 #include <linux/platform_device.h>
8 * According to a data sheet draft, "this temperature sensor uses a bandgap
9 * type of circuit to compare a voltage which has a negative temperature
10 * coefficient with a voltage that is proportional to absolute temperature.
11 * A resistor bank allows 41 different temperature thresholds to be selected
12 * and the logic output will then indicate whether the actual die temperature
13 * lies above or below the selected threshold."
27 struct tango_thermal_priv
{
32 static bool temp_above_thresh(void __iomem
*base
, int thresh_idx
)
34 writel(CMD_READ
| thresh_idx
<< 8, base
+ TEMPSI_CMD
);
36 writel(CMD_READ
| thresh_idx
<< 8, base
+ TEMPSI_CMD
);
38 return readl(base
+ TEMPSI_RES
);
41 static int tango_get_temp(void *arg
, int *res
)
43 struct tango_thermal_priv
*priv
= arg
;
44 int idx
= priv
->thresh_idx
;
46 if (temp_above_thresh(priv
->base
, idx
)) {
47 /* Search upward by incrementing thresh_idx */
48 while (idx
< IDX_MAX
&& temp_above_thresh(priv
->base
, ++idx
))
50 idx
= idx
- 1; /* always return lower bound */
52 /* Search downward by decrementing thresh_idx */
53 while (idx
> IDX_MIN
&& !temp_above_thresh(priv
->base
, --idx
))
57 *res
= (idx
* 9 / 2 - 38) * 1000; /* millidegrees Celsius */
58 priv
->thresh_idx
= idx
;
63 static const struct thermal_zone_of_device_ops ops
= {
64 .get_temp
= tango_get_temp
,
67 static void tango_thermal_init(struct tango_thermal_priv
*priv
)
69 writel(0, priv
->base
+ TEMPSI_CFG
);
70 writel(CMD_ON
, priv
->base
+ TEMPSI_CMD
);
73 static int tango_thermal_probe(struct platform_device
*pdev
)
76 struct tango_thermal_priv
*priv
;
77 struct thermal_zone_device
*tzdev
;
79 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
83 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
84 priv
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
85 if (IS_ERR(priv
->base
))
86 return PTR_ERR(priv
->base
);
88 platform_set_drvdata(pdev
, priv
);
89 priv
->thresh_idx
= IDX_MIN
;
90 tango_thermal_init(priv
);
92 tzdev
= devm_thermal_zone_of_sensor_register(&pdev
->dev
, 0, priv
, &ops
);
93 return PTR_ERR_OR_ZERO(tzdev
);
96 static int __maybe_unused
tango_thermal_resume(struct device
*dev
)
98 tango_thermal_init(dev_get_drvdata(dev
));
102 static SIMPLE_DEV_PM_OPS(tango_thermal_pm
, NULL
, tango_thermal_resume
);
104 static const struct of_device_id tango_sensor_ids
[] = {
106 .compatible
= "sigma,smp8758-thermal",
111 static struct platform_driver tango_thermal_driver
= {
112 .probe
= tango_thermal_probe
,
114 .name
= "tango-thermal",
115 .of_match_table
= tango_sensor_ids
,
116 .pm
= &tango_thermal_pm
,
120 module_platform_driver(tango_thermal_driver
);
122 MODULE_LICENSE("GPL");
123 MODULE_AUTHOR("Sigma Designs");
124 MODULE_DESCRIPTION("Tango temperature sensor");