Loading...
1/*
2 * SPEAr thermal driver.
3 *
4 * Copyright (C) 2011-2012 ST Microelectronics
5 * Author: Vincenzo Frascino <vincenzo.frascino@st.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/clk.h>
19#include <linux/device.h>
20#include <linux/err.h>
21#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/of.h>
24#include <linux/module.h>
25#include <linux/platform_device.h>
26#include <linux/thermal.h>
27
28#define MD_FACTOR 1000
29
30/* SPEAr Thermal Sensor Dev Structure */
31struct spear_thermal_dev {
32 /* pointer to base address of the thermal sensor */
33 void __iomem *thermal_base;
34 /* clk structure */
35 struct clk *clk;
36 /* pointer to thermal flags */
37 unsigned int flags;
38};
39
40static inline int thermal_get_temp(struct thermal_zone_device *thermal,
41 unsigned long *temp)
42{
43 struct spear_thermal_dev *stdev = thermal->devdata;
44
45 /*
46 * Data are ready to be read after 628 usec from POWERDOWN signal
47 * (PDN) = 1
48 */
49 *temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
50 return 0;
51}
52
53static struct thermal_zone_device_ops ops = {
54 .get_temp = thermal_get_temp,
55};
56
57#ifdef CONFIG_PM
58static int spear_thermal_suspend(struct device *dev)
59{
60 struct platform_device *pdev = to_platform_device(dev);
61 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
62 struct spear_thermal_dev *stdev = spear_thermal->devdata;
63 unsigned int actual_mask = 0;
64
65 /* Disable SPEAr Thermal Sensor */
66 actual_mask = readl_relaxed(stdev->thermal_base);
67 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
68
69 clk_disable(stdev->clk);
70 dev_info(dev, "Suspended.\n");
71
72 return 0;
73}
74
75static int spear_thermal_resume(struct device *dev)
76{
77 struct platform_device *pdev = to_platform_device(dev);
78 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
79 struct spear_thermal_dev *stdev = spear_thermal->devdata;
80 unsigned int actual_mask = 0;
81 int ret = 0;
82
83 ret = clk_enable(stdev->clk);
84 if (ret) {
85 dev_err(&pdev->dev, "Can't enable clock\n");
86 return ret;
87 }
88
89 /* Enable SPEAr Thermal Sensor */
90 actual_mask = readl_relaxed(stdev->thermal_base);
91 writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
92
93 dev_info(dev, "Resumed.\n");
94
95 return 0;
96}
97#endif
98
99static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend,
100 spear_thermal_resume);
101
102static int spear_thermal_probe(struct platform_device *pdev)
103{
104 struct thermal_zone_device *spear_thermal = NULL;
105 struct spear_thermal_dev *stdev;
106 struct device_node *np = pdev->dev.of_node;
107 struct resource *res;
108 int ret = 0, val;
109
110 if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) {
111 dev_err(&pdev->dev, "Failed: DT Pdata not passed\n");
112 return -EINVAL;
113 }
114
115 stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
116 if (!stdev) {
117 dev_err(&pdev->dev, "kzalloc fail\n");
118 return -ENOMEM;
119 }
120
121 /* Enable thermal sensor */
122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123 stdev->thermal_base = devm_ioremap_resource(&pdev->dev, res);
124 if (IS_ERR(stdev->thermal_base))
125 return PTR_ERR(stdev->thermal_base);
126
127 stdev->clk = devm_clk_get(&pdev->dev, NULL);
128 if (IS_ERR(stdev->clk)) {
129 dev_err(&pdev->dev, "Can't get clock\n");
130 return PTR_ERR(stdev->clk);
131 }
132
133 ret = clk_enable(stdev->clk);
134 if (ret) {
135 dev_err(&pdev->dev, "Can't enable clock\n");
136 return ret;
137 }
138
139 stdev->flags = val;
140 writel_relaxed(stdev->flags, stdev->thermal_base);
141
142 spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0,
143 stdev, &ops, NULL, 0, 0);
144 if (IS_ERR(spear_thermal)) {
145 dev_err(&pdev->dev, "thermal zone device is NULL\n");
146 ret = PTR_ERR(spear_thermal);
147 goto disable_clk;
148 }
149
150 platform_set_drvdata(pdev, spear_thermal);
151
152 dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n",
153 stdev->thermal_base);
154
155 return 0;
156
157disable_clk:
158 clk_disable(stdev->clk);
159
160 return ret;
161}
162
163static int spear_thermal_exit(struct platform_device *pdev)
164{
165 unsigned int actual_mask = 0;
166 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
167 struct spear_thermal_dev *stdev = spear_thermal->devdata;
168
169 thermal_zone_device_unregister(spear_thermal);
170
171 /* Disable SPEAr Thermal Sensor */
172 actual_mask = readl_relaxed(stdev->thermal_base);
173 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
174
175 clk_disable(stdev->clk);
176
177 return 0;
178}
179
180static const struct of_device_id spear_thermal_id_table[] = {
181 { .compatible = "st,thermal-spear1340" },
182 {}
183};
184MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
185
186static struct platform_driver spear_thermal_driver = {
187 .probe = spear_thermal_probe,
188 .remove = spear_thermal_exit,
189 .driver = {
190 .name = "spear_thermal",
191 .owner = THIS_MODULE,
192 .pm = &spear_thermal_pm_ops,
193 .of_match_table = spear_thermal_id_table,
194 },
195};
196
197module_platform_driver(spear_thermal_driver);
198
199MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
200MODULE_DESCRIPTION("SPEAr thermal driver");
201MODULE_LICENSE("GPL");
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 */
22struct 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
31static inline int thermal_get_temp(struct thermal_zone_device *thermal,
32 int *temp)
33{
34 struct spear_thermal_dev *stdev = thermal_zone_device_priv(thermal);
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
44static struct thermal_zone_device_ops ops = {
45 .get_temp = thermal_get_temp,
46};
47
48static 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 = thermal_zone_device_priv(spear_thermal);
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
64static 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 = thermal_zone_device_priv(spear_thermal);
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
86static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend,
87 spear_thermal_resume);
88
89static 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 int ret = 0, val;
95
96 if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) {
97 dev_err(&pdev->dev, "Failed: DT Pdata not passed\n");
98 return -EINVAL;
99 }
100
101 stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
102 if (!stdev)
103 return -ENOMEM;
104
105 /* Enable thermal sensor */
106 stdev->thermal_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
107 if (IS_ERR(stdev->thermal_base))
108 return PTR_ERR(stdev->thermal_base);
109
110 stdev->clk = devm_clk_get(&pdev->dev, NULL);
111 if (IS_ERR(stdev->clk)) {
112 dev_err(&pdev->dev, "Can't get clock\n");
113 return PTR_ERR(stdev->clk);
114 }
115
116 ret = clk_enable(stdev->clk);
117 if (ret) {
118 dev_err(&pdev->dev, "Can't enable clock\n");
119 return ret;
120 }
121
122 stdev->flags = val;
123 writel_relaxed(stdev->flags, stdev->thermal_base);
124
125 spear_thermal = thermal_tripless_zone_device_register("spear_thermal",
126 stdev, &ops, NULL);
127 if (IS_ERR(spear_thermal)) {
128 dev_err(&pdev->dev, "thermal zone device is NULL\n");
129 ret = PTR_ERR(spear_thermal);
130 goto disable_clk;
131 }
132 ret = thermal_zone_device_enable(spear_thermal);
133 if (ret) {
134 dev_err(&pdev->dev, "Cannot enable thermal zone\n");
135 goto unregister_tzd;
136 }
137
138 platform_set_drvdata(pdev, spear_thermal);
139
140 dev_info(&pdev->dev, "Thermal Sensor Loaded at: 0x%p.\n",
141 stdev->thermal_base);
142
143 return 0;
144
145unregister_tzd:
146 thermal_zone_device_unregister(spear_thermal);
147disable_clk:
148 clk_disable(stdev->clk);
149
150 return ret;
151}
152
153static void spear_thermal_exit(struct platform_device *pdev)
154{
155 unsigned int actual_mask = 0;
156 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
157 struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
158
159 thermal_zone_device_unregister(spear_thermal);
160
161 /* Disable SPEAr Thermal Sensor */
162 actual_mask = readl_relaxed(stdev->thermal_base);
163 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
164
165 clk_disable(stdev->clk);
166}
167
168static const struct of_device_id spear_thermal_id_table[] = {
169 { .compatible = "st,thermal-spear1340" },
170 {}
171};
172MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
173
174static struct platform_driver spear_thermal_driver = {
175 .probe = spear_thermal_probe,
176 .remove = spear_thermal_exit,
177 .driver = {
178 .name = "spear_thermal",
179 .pm = &spear_thermal_pm_ops,
180 .of_match_table = spear_thermal_id_table,
181 },
182};
183
184module_platform_driver(spear_thermal_driver);
185
186MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
187MODULE_DESCRIPTION("SPEAr thermal driver");
188MODULE_LICENSE("GPL");