Merge tag 'bitmap-6.5-rc1' of https://github.com/norov/linux
[linux-block.git] / drivers / thermal / spear_thermal.c
CommitLineData
9c92ab61 1// SPDX-License-Identifier: GPL-2.0-only
6a92c366
VF
2/*
3 * SPEAr thermal driver.
4 *
5 * Copyright (C) 2011-2012 ST Microelectronics
6 * Author: Vincenzo Frascino <vincenzo.frascino@st.com>
6a92c366
VF
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>
b9c7aff4 14#include <linux/of.h>
6a92c366
VF
15#include <linux/module.h>
16#include <linux/platform_device.h>
6a92c366
VF
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,
17e8351a 32 int *temp)
6a92c366 33{
5f68d078 34 struct spear_thermal_dev *stdev = thermal_zone_device_priv(thermal);
6a92c366
VF
35
36 /*
37 * Data are ready to be read after 628 usec from POWERDOWN signal
38 * (PDN) = 1
39 */
de716e32 40 *temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
6a92c366
VF
41 return 0;
42}
43
44static struct thermal_zone_device_ops ops = {
45 .get_temp = thermal_get_temp,
46};
47
d612c64d 48static int __maybe_unused spear_thermal_suspend(struct device *dev)
6a92c366 49{
3fc62efe 50 struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
5f68d078 51 struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
6a92c366
VF
52 unsigned int actual_mask = 0;
53
54 /* Disable SPEAr Thermal Sensor */
de716e32
VK
55 actual_mask = readl_relaxed(stdev->thermal_base);
56 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
6a92c366
VF
57
58 clk_disable(stdev->clk);
59 dev_info(dev, "Suspended.\n");
60
61 return 0;
62}
63
d612c64d 64static int __maybe_unused spear_thermal_resume(struct device *dev)
6a92c366 65{
3fc62efe 66 struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
5f68d078 67 struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
6a92c366
VF
68 unsigned int actual_mask = 0;
69 int ret = 0;
70
71 ret = clk_enable(stdev->clk);
72 if (ret) {
3fc62efe 73 dev_err(dev, "Can't enable clock\n");
6a92c366
VF
74 return ret;
75 }
76
77 /* Enable SPEAr Thermal Sensor */
de716e32
VK
78 actual_mask = readl_relaxed(stdev->thermal_base);
79 writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
6a92c366
VF
80
81 dev_info(dev, "Resumed.\n");
82
83 return 0;
84}
6a92c366
VF
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;
b9c7aff4 93 struct device_node *np = pdev->dev.of_node;
b9c7aff4
VK
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 }
6a92c366 100
6a92c366 101 stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
fa018d3e 102 if (!stdev)
6a92c366 103 return -ENOMEM;
6a92c366
VF
104
105 /* Enable thermal sensor */
f8887fdc 106 stdev->thermal_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
c21bec86 107 if (IS_ERR(stdev->thermal_base))
253e3ae1 108 return PTR_ERR(stdev->thermal_base);
6a92c366 109
03b79bda 110 stdev->clk = devm_clk_get(&pdev->dev, NULL);
6a92c366
VF
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");
03b79bda 119 return ret;
6a92c366
VF
120 }
121
b9c7aff4 122 stdev->flags = val;
de716e32 123 writel_relaxed(stdev->flags, stdev->thermal_base);
6a92c366 124
c56f5c03 125 spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0,
50125a9b 126 stdev, &ops, NULL, 0, 0);
03ee62f0 127 if (IS_ERR(spear_thermal)) {
6a92c366 128 dev_err(&pdev->dev, "thermal zone device is NULL\n");
03ee62f0 129 ret = PTR_ERR(spear_thermal);
6a92c366
VF
130 goto disable_clk;
131 }
bbcf90c0
AP
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 }
6a92c366
VF
137
138 platform_set_drvdata(pdev, spear_thermal);
139
25e43976 140 dev_info(&pdev->dev, "Thermal Sensor Loaded at: 0x%p.\n",
6a92c366
VF
141 stdev->thermal_base);
142
143 return 0;
144
bbcf90c0
AP
145unregister_tzd:
146 thermal_zone_device_unregister(spear_thermal);
6a92c366
VF
147disable_clk:
148 clk_disable(stdev->clk);
6a92c366
VF
149
150 return ret;
151}
152
153static int 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);
5f68d078 157 struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
6a92c366
VF
158
159 thermal_zone_device_unregister(spear_thermal);
6a92c366
VF
160
161 /* Disable SPEAr Thermal Sensor */
de716e32
VK
162 actual_mask = readl_relaxed(stdev->thermal_base);
163 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
6a92c366
VF
164
165 clk_disable(stdev->clk);
6a92c366
VF
166
167 return 0;
168}
169
b9c7aff4
VK
170static const struct of_device_id spear_thermal_id_table[] = {
171 { .compatible = "st,thermal-spear1340" },
172 {}
173};
174MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
175
6a92c366
VF
176static struct platform_driver spear_thermal_driver = {
177 .probe = spear_thermal_probe,
178 .remove = spear_thermal_exit,
179 .driver = {
180 .name = "spear_thermal",
6a92c366 181 .pm = &spear_thermal_pm_ops,
5454f211 182 .of_match_table = spear_thermal_id_table,
6a92c366
VF
183 },
184};
185
186module_platform_driver(spear_thermal_driver);
187
188MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
189MODULE_DESCRIPTION("SPEAr thermal driver");
190MODULE_LICENSE("GPL");