]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/thermal/spear_thermal.c
f68f581fd669202f600696221b0e2f79d140f580
[mirror_ubuntu-hirsute-kernel.git] / drivers / thermal / spear_thermal.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SPEAr thermal driver.
4 *
5 * Copyright (C) 2011-2012 ST Microelectronics
6 * Author: Vincenzo Frascino <vincenzo.frascino@st.com>
7 */
8
9 #include <linux/clk.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/of.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/thermal.h>
18
19 #define MD_FACTOR 1000
20
21 /* SPEAr Thermal Sensor Dev Structure */
22 struct spear_thermal_dev {
23 /* pointer to base address of the thermal sensor */
24 void __iomem *thermal_base;
25 /* clk structure */
26 struct clk *clk;
27 /* pointer to thermal flags */
28 unsigned int flags;
29 };
30
31 static inline int thermal_get_temp(struct thermal_zone_device *thermal,
32 int *temp)
33 {
34 struct spear_thermal_dev *stdev = thermal->devdata;
35
36 /*
37 * Data are ready to be read after 628 usec from POWERDOWN signal
38 * (PDN) = 1
39 */
40 *temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
41 return 0;
42 }
43
44 static struct thermal_zone_device_ops ops = {
45 .get_temp = thermal_get_temp,
46 };
47
48 static int __maybe_unused spear_thermal_suspend(struct device *dev)
49 {
50 struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
51 struct spear_thermal_dev *stdev = spear_thermal->devdata;
52 unsigned int actual_mask = 0;
53
54 /* Disable SPEAr Thermal Sensor */
55 actual_mask = readl_relaxed(stdev->thermal_base);
56 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
57
58 clk_disable(stdev->clk);
59 dev_info(dev, "Suspended.\n");
60
61 return 0;
62 }
63
64 static int __maybe_unused spear_thermal_resume(struct device *dev)
65 {
66 struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
67 struct spear_thermal_dev *stdev = spear_thermal->devdata;
68 unsigned int actual_mask = 0;
69 int ret = 0;
70
71 ret = clk_enable(stdev->clk);
72 if (ret) {
73 dev_err(dev, "Can't enable clock\n");
74 return ret;
75 }
76
77 /* Enable SPEAr Thermal Sensor */
78 actual_mask = readl_relaxed(stdev->thermal_base);
79 writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
80
81 dev_info(dev, "Resumed.\n");
82
83 return 0;
84 }
85
86 static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend,
87 spear_thermal_resume);
88
89 static int spear_thermal_probe(struct platform_device *pdev)
90 {
91 struct thermal_zone_device *spear_thermal = NULL;
92 struct spear_thermal_dev *stdev;
93 struct device_node *np = pdev->dev.of_node;
94 struct resource *res;
95 int ret = 0, val;
96
97 if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) {
98 dev_err(&pdev->dev, "Failed: DT Pdata not passed\n");
99 return -EINVAL;
100 }
101
102 stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
103 if (!stdev)
104 return -ENOMEM;
105
106 /* Enable thermal sensor */
107 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
108 stdev->thermal_base = devm_ioremap_resource(&pdev->dev, res);
109 if (IS_ERR(stdev->thermal_base))
110 return PTR_ERR(stdev->thermal_base);
111
112 stdev->clk = devm_clk_get(&pdev->dev, NULL);
113 if (IS_ERR(stdev->clk)) {
114 dev_err(&pdev->dev, "Can't get clock\n");
115 return PTR_ERR(stdev->clk);
116 }
117
118 ret = clk_enable(stdev->clk);
119 if (ret) {
120 dev_err(&pdev->dev, "Can't enable clock\n");
121 return ret;
122 }
123
124 stdev->flags = val;
125 writel_relaxed(stdev->flags, stdev->thermal_base);
126
127 spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0,
128 stdev, &ops, NULL, 0, 0);
129 if (IS_ERR(spear_thermal)) {
130 dev_err(&pdev->dev, "thermal zone device is NULL\n");
131 ret = PTR_ERR(spear_thermal);
132 goto disable_clk;
133 }
134
135 platform_set_drvdata(pdev, spear_thermal);
136
137 dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n",
138 stdev->thermal_base);
139
140 return 0;
141
142 disable_clk:
143 clk_disable(stdev->clk);
144
145 return ret;
146 }
147
148 static int spear_thermal_exit(struct platform_device *pdev)
149 {
150 unsigned int actual_mask = 0;
151 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
152 struct spear_thermal_dev *stdev = spear_thermal->devdata;
153
154 thermal_zone_device_unregister(spear_thermal);
155
156 /* Disable SPEAr Thermal Sensor */
157 actual_mask = readl_relaxed(stdev->thermal_base);
158 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
159
160 clk_disable(stdev->clk);
161
162 return 0;
163 }
164
165 static const struct of_device_id spear_thermal_id_table[] = {
166 { .compatible = "st,thermal-spear1340" },
167 {}
168 };
169 MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
170
171 static struct platform_driver spear_thermal_driver = {
172 .probe = spear_thermal_probe,
173 .remove = spear_thermal_exit,
174 .driver = {
175 .name = "spear_thermal",
176 .pm = &spear_thermal_pm_ops,
177 .of_match_table = spear_thermal_id_table,
178 },
179 };
180
181 module_platform_driver(spear_thermal_driver);
182
183 MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
184 MODULE_DESCRIPTION("SPEAr thermal driver");
185 MODULE_LICENSE("GPL");