blob: 81b35aace9de0439c4dd76c4c4c0d91597d3b3f3 [file] [log] [blame]
Vincenzo Frascino6a92c362012-03-21 12:55:03 -07001/*
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>
Viresh Kumarb9c7aff2012-05-29 11:18:51 -070023#include <linux/of.h>
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070024#include <linux/module.h>
25#include <linux/platform_device.h>
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070026#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,
Sascha Hauer17e83512015-07-24 08:12:54 +020041 int *temp)
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070042{
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 */
Viresh Kumarde716e32012-03-21 12:55:03 -070049 *temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070050 return 0;
51}
52
53static struct thermal_zone_device_ops ops = {
54 .get_temp = thermal_get_temp,
55};
56
Arnd Bergmannd612c642016-01-25 17:44:10 +010057static int __maybe_unused spear_thermal_suspend(struct device *dev)
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070058{
59 struct platform_device *pdev = to_platform_device(dev);
60 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
61 struct spear_thermal_dev *stdev = spear_thermal->devdata;
62 unsigned int actual_mask = 0;
63
64 /* Disable SPEAr Thermal Sensor */
Viresh Kumarde716e32012-03-21 12:55:03 -070065 actual_mask = readl_relaxed(stdev->thermal_base);
66 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070067
68 clk_disable(stdev->clk);
69 dev_info(dev, "Suspended.\n");
70
71 return 0;
72}
73
Arnd Bergmannd612c642016-01-25 17:44:10 +010074static int __maybe_unused spear_thermal_resume(struct device *dev)
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070075{
76 struct platform_device *pdev = to_platform_device(dev);
77 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
78 struct spear_thermal_dev *stdev = spear_thermal->devdata;
79 unsigned int actual_mask = 0;
80 int ret = 0;
81
82 ret = clk_enable(stdev->clk);
83 if (ret) {
84 dev_err(&pdev->dev, "Can't enable clock\n");
85 return ret;
86 }
87
88 /* Enable SPEAr Thermal Sensor */
Viresh Kumarde716e32012-03-21 12:55:03 -070089 actual_mask = readl_relaxed(stdev->thermal_base);
90 writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070091
92 dev_info(dev, "Resumed.\n");
93
94 return 0;
95}
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070096
97static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend,
98 spear_thermal_resume);
99
100static int spear_thermal_probe(struct platform_device *pdev)
101{
102 struct thermal_zone_device *spear_thermal = NULL;
103 struct spear_thermal_dev *stdev;
Viresh Kumarb9c7aff2012-05-29 11:18:51 -0700104 struct device_node *np = pdev->dev.of_node;
Zhang Rui253e3ae2013-05-16 02:16:20 +0000105 struct resource *res;
Viresh Kumarb9c7aff2012-05-29 11:18:51 -0700106 int ret = 0, val;
107
108 if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) {
109 dev_err(&pdev->dev, "Failed: DT Pdata not passed\n");
110 return -EINVAL;
111 }
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700112
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700113 stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
Jingoo Hanfa018d32014-05-07 15:05:32 +0900114 if (!stdev)
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700115 return -ENOMEM;
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700116
117 /* Enable thermal sensor */
Zhang Ruic21bec82013-05-16 02:16:21 +0000118 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
119 stdev->thermal_base = devm_ioremap_resource(&pdev->dev, res);
120 if (IS_ERR(stdev->thermal_base))
Zhang Rui253e3ae2013-05-16 02:16:20 +0000121 return PTR_ERR(stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700122
Julia Lawall03b79bd2012-12-07 11:29:32 +0100123 stdev->clk = devm_clk_get(&pdev->dev, NULL);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700124 if (IS_ERR(stdev->clk)) {
125 dev_err(&pdev->dev, "Can't get clock\n");
126 return PTR_ERR(stdev->clk);
127 }
128
129 ret = clk_enable(stdev->clk);
130 if (ret) {
131 dev_err(&pdev->dev, "Can't enable clock\n");
Julia Lawall03b79bd2012-12-07 11:29:32 +0100132 return ret;
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700133 }
134
Viresh Kumarb9c7aff2012-05-29 11:18:51 -0700135 stdev->flags = val;
Viresh Kumarde716e32012-03-21 12:55:03 -0700136 writel_relaxed(stdev->flags, stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700137
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800138 spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0,
Durgadoss R50125a92012-09-18 11:04:56 +0530139 stdev, &ops, NULL, 0, 0);
Dan Carpenter03ee62f2012-03-21 12:55:04 -0700140 if (IS_ERR(spear_thermal)) {
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700141 dev_err(&pdev->dev, "thermal zone device is NULL\n");
Dan Carpenter03ee62f2012-03-21 12:55:04 -0700142 ret = PTR_ERR(spear_thermal);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700143 goto disable_clk;
144 }
145
146 platform_set_drvdata(pdev, spear_thermal);
147
148 dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n",
149 stdev->thermal_base);
150
151 return 0;
152
153disable_clk:
154 clk_disable(stdev->clk);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700155
156 return ret;
157}
158
159static int spear_thermal_exit(struct platform_device *pdev)
160{
161 unsigned int actual_mask = 0;
162 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
163 struct spear_thermal_dev *stdev = spear_thermal->devdata;
164
165 thermal_zone_device_unregister(spear_thermal);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700166
167 /* Disable SPEAr Thermal Sensor */
Viresh Kumarde716e32012-03-21 12:55:03 -0700168 actual_mask = readl_relaxed(stdev->thermal_base);
169 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700170
171 clk_disable(stdev->clk);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700172
173 return 0;
174}
175
Viresh Kumarb9c7aff2012-05-29 11:18:51 -0700176static const struct of_device_id spear_thermal_id_table[] = {
177 { .compatible = "st,thermal-spear1340" },
178 {}
179};
180MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
181
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700182static struct platform_driver spear_thermal_driver = {
183 .probe = spear_thermal_probe,
184 .remove = spear_thermal_exit,
185 .driver = {
186 .name = "spear_thermal",
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700187 .pm = &spear_thermal_pm_ops,
Sachin Kamat5454f212013-05-16 10:28:11 +0000188 .of_match_table = spear_thermal_id_table,
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700189 },
190};
191
192module_platform_driver(spear_thermal_driver);
193
194MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
195MODULE_DESCRIPTION("SPEAr thermal driver");
196MODULE_LICENSE("GPL");