diff options
author | Marc Gonzalez <marc_gonzalez@sigmadesigns.com> | 2016-04-19 10:21:16 -0400 |
---|---|---|
committer | Eduardo Valentin <edubezval@gmail.com> | 2016-05-17 10:28:30 -0400 |
commit | 0b58c08b593d082858e74503bdf553ea01f9f15a (patch) | |
tree | f94f22fa151b5a0e7069b384fdc82b99d62d73b0 /drivers/thermal/tango_thermal.c | |
parent | 469ace07c29310b1401316e5e910c157c7544af3 (diff) |
thermal: add temperature sensor support for tango SoC
The Tango thermal driver provides support for the primitive temperature
sensor embedded in Tango chips since the SMP8758.
This sensor only generates a 1-bit signal to indicate whether the die
temperature exceeds a programmable threshold.
Signed-off-by: Marc Gonzalez <marc_gonzalez@sigmadesigns.com>
Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
Diffstat (limited to 'drivers/thermal/tango_thermal.c')
-rw-r--r-- | drivers/thermal/tango_thermal.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/drivers/thermal/tango_thermal.c b/drivers/thermal/tango_thermal.c new file mode 100644 index 000000000000..d6592bfbd057 --- /dev/null +++ b/drivers/thermal/tango_thermal.c | |||
@@ -0,0 +1,111 @@ | |||
1 | #include <linux/io.h> | ||
2 | #include <linux/delay.h> | ||
3 | #include <linux/module.h> | ||
4 | #include <linux/thermal.h> | ||
5 | #include <linux/platform_device.h> | ||
6 | |||
7 | /* | ||
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." | ||
14 | */ | ||
15 | |||
16 | #define TEMPSI_CMD 0 | ||
17 | #define TEMPSI_RES 4 | ||
18 | #define TEMPSI_CFG 8 | ||
19 | |||
20 | #define CMD_OFF 0 | ||
21 | #define CMD_ON 1 | ||
22 | #define CMD_READ 2 | ||
23 | |||
24 | #define IDX_MIN 15 | ||
25 | #define IDX_MAX 40 | ||
26 | |||
27 | struct tango_thermal_priv { | ||
28 | void __iomem *base; | ||
29 | int thresh_idx; | ||
30 | }; | ||
31 | |||
32 | static bool temp_above_thresh(void __iomem *base, int thresh_idx) | ||
33 | { | ||
34 | writel(CMD_READ | thresh_idx << 8, base + TEMPSI_CMD); | ||
35 | usleep_range(10, 20); | ||
36 | writel(CMD_READ | thresh_idx << 8, base + TEMPSI_CMD); | ||
37 | |||
38 | return readl(base + TEMPSI_RES); | ||
39 | } | ||
40 | |||
41 | static int tango_get_temp(void *arg, int *res) | ||
42 | { | ||
43 | struct tango_thermal_priv *priv = arg; | ||
44 | int idx = priv->thresh_idx; | ||
45 | |||
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)) | ||
49 | cpu_relax(); | ||
50 | idx = idx - 1; /* always return lower bound */ | ||
51 | } else { | ||
52 | /* Search downward by decrementing thresh_idx */ | ||
53 | while (idx > IDX_MIN && !temp_above_thresh(priv->base, --idx)) | ||
54 | cpu_relax(); | ||
55 | } | ||
56 | |||
57 | *res = (idx * 9 / 2 - 38) * 1000; /* millidegrees Celsius */ | ||
58 | priv->thresh_idx = idx; | ||
59 | |||
60 | return 0; | ||
61 | } | ||
62 | |||
63 | static const struct thermal_zone_of_device_ops ops = { | ||
64 | .get_temp = tango_get_temp, | ||
65 | }; | ||
66 | |||
67 | static int tango_thermal_probe(struct platform_device *pdev) | ||
68 | { | ||
69 | struct resource *res; | ||
70 | struct tango_thermal_priv *priv; | ||
71 | struct thermal_zone_device *tzdev; | ||
72 | |||
73 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); | ||
74 | if (!priv) | ||
75 | return -ENOMEM; | ||
76 | |||
77 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | ||
78 | priv->base = devm_ioremap_resource(&pdev->dev, res); | ||
79 | if (IS_ERR(priv->base)) | ||
80 | return PTR_ERR(priv->base); | ||
81 | |||
82 | priv->thresh_idx = IDX_MIN; | ||
83 | writel(CMD_ON, priv->base + TEMPSI_CMD); | ||
84 | |||
85 | tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, priv, &ops); | ||
86 | if (IS_ERR(tzdev)) | ||
87 | return PTR_ERR(tzdev); | ||
88 | |||
89 | return 0; | ||
90 | } | ||
91 | |||
92 | static const struct of_device_id tango_sensor_ids[] = { | ||
93 | { | ||
94 | .compatible = "sigma,smp8758-thermal", | ||
95 | }, | ||
96 | { /* sentinel */ } | ||
97 | }; | ||
98 | |||
99 | static struct platform_driver tango_thermal_driver = { | ||
100 | .probe = tango_thermal_probe, | ||
101 | .driver = { | ||
102 | .name = "tango-thermal", | ||
103 | .of_match_table = tango_sensor_ids, | ||
104 | }, | ||
105 | }; | ||
106 | |||
107 | module_platform_driver(tango_thermal_driver); | ||
108 | |||
109 | MODULE_LICENSE("GPL"); | ||
110 | MODULE_AUTHOR("Sigma Designs"); | ||
111 | MODULE_DESCRIPTION("Tango temperature sensor"); | ||