^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * According to a data sheet draft, "this temperature sensor uses a bandgap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * type of circuit to compare a voltage which has a negative temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * coefficient with a voltage that is proportional to absolute temperature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * A resistor bank allows 41 different temperature thresholds to be selected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * and the logic output will then indicate whether the actual die temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * lies above or below the selected threshold."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define TEMPSI_CMD 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define TEMPSI_RES 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define TEMPSI_CFG 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define CMD_OFF 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define CMD_ON 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define CMD_READ 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define IDX_MIN 15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define IDX_MAX 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct tango_thermal_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int thresh_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static bool temp_above_thresh(void __iomem *base, int thresh_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) writel(CMD_READ | thresh_idx << 8, base + TEMPSI_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) usleep_range(10, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) writel(CMD_READ | thresh_idx << 8, base + TEMPSI_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return readl(base + TEMPSI_RES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int tango_get_temp(void *arg, int *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct tango_thermal_priv *priv = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int idx = priv->thresh_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (temp_above_thresh(priv->base, idx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* Search upward by incrementing thresh_idx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) while (idx < IDX_MAX && temp_above_thresh(priv->base, ++idx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) idx = idx - 1; /* always return lower bound */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* Search downward by decrementing thresh_idx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) while (idx > IDX_MIN && !temp_above_thresh(priv->base, --idx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *res = (idx * 9 / 2 - 38) * 1000; /* millidegrees Celsius */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) priv->thresh_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static const struct thermal_zone_of_device_ops ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .get_temp = tango_get_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static void tango_thermal_init(struct tango_thermal_priv *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) writel(0, priv->base + TEMPSI_CFG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) writel(CMD_ON, priv->base + TEMPSI_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static int tango_thermal_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct tango_thermal_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct thermal_zone_device *tzdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) priv->base = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (IS_ERR(priv->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return PTR_ERR(priv->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) platform_set_drvdata(pdev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) priv->thresh_idx = IDX_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) tango_thermal_init(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, priv, &ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return PTR_ERR_OR_ZERO(tzdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int __maybe_unused tango_thermal_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) tango_thermal_init(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static SIMPLE_DEV_PM_OPS(tango_thermal_pm, NULL, tango_thermal_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static const struct of_device_id tango_sensor_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .compatible = "sigma,smp8758-thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) MODULE_DEVICE_TABLE(of, tango_sensor_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static struct platform_driver tango_thermal_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .probe = tango_thermal_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .name = "tango-thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .of_match_table = tango_sensor_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .pm = &tango_thermal_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) module_platform_driver(tango_thermal_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) MODULE_AUTHOR("Sigma Designs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) MODULE_DESCRIPTION("Tango temperature sensor");