blob: ee33ed692e4f7ee13983e7fffee97d346a7a5e55 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Vincenzo Frascino6a92c362012-03-21 12:55:03 -07002/*
3 * SPEAr thermal driver.
4 *
5 * Copyright (C) 2011-2012 ST Microelectronics
6 * Author: Vincenzo Frascino <vincenzo.frascino@st.com>
Vincenzo Frascino6a92c362012-03-21 12:55:03 -07007 */
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>
Viresh Kumarb9c7aff2012-05-29 11:18:51 -070014#include <linux/of.h>
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070015#include <linux/module.h>
16#include <linux/platform_device.h>
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070017#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,
Sascha Hauer17e83512015-07-24 08:12:54 +020032 int *temp)
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070033{
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 */
Viresh Kumarde716e32012-03-21 12:55:03 -070040 *temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070041 return 0;
42}
43
44static struct thermal_zone_device_ops ops = {
45 .get_temp = thermal_get_temp,
46};
47
Arnd Bergmannd612c642016-01-25 17:44:10 +010048static int __maybe_unused spear_thermal_suspend(struct device *dev)
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070049{
Wolfram Sang3fc62ef2018-10-21 22:00:50 +020050 struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070051 struct spear_thermal_dev *stdev = spear_thermal->devdata;
52 unsigned int actual_mask = 0;
53
54 /* Disable SPEAr Thermal Sensor */
Viresh Kumarde716e32012-03-21 12:55:03 -070055 actual_mask = readl_relaxed(stdev->thermal_base);
56 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070057
58 clk_disable(stdev->clk);
59 dev_info(dev, "Suspended.\n");
60
61 return 0;
62}
63
Arnd Bergmannd612c642016-01-25 17:44:10 +010064static int __maybe_unused spear_thermal_resume(struct device *dev)
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070065{
Wolfram Sang3fc62ef2018-10-21 22:00:50 +020066 struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070067 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) {
Wolfram Sang3fc62ef2018-10-21 22:00:50 +020073 dev_err(dev, "Can't enable clock\n");
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070074 return ret;
75 }
76
77 /* Enable SPEAr Thermal Sensor */
Viresh Kumarde716e32012-03-21 12:55:03 -070078 actual_mask = readl_relaxed(stdev->thermal_base);
79 writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070080
81 dev_info(dev, "Resumed.\n");
82
83 return 0;
84}
Vincenzo Frascino6a92c362012-03-21 12:55:03 -070085
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;
Viresh Kumarb9c7aff2012-05-29 11:18:51 -070093 struct device_node *np = pdev->dev.of_node;
Zhang Rui253e3ae2013-05-16 02:16:20 +000094 struct resource *res;
Viresh Kumarb9c7aff2012-05-29 11:18:51 -070095 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 }
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700101
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700102 stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
Jingoo Hanfa018d32014-05-07 15:05:32 +0900103 if (!stdev)
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700104 return -ENOMEM;
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700105
106 /* Enable thermal sensor */
Zhang Ruic21bec82013-05-16 02:16:21 +0000107 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))
Zhang Rui253e3ae2013-05-16 02:16:20 +0000110 return PTR_ERR(stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700111
Julia Lawall03b79bd2012-12-07 11:29:32 +0100112 stdev->clk = devm_clk_get(&pdev->dev, NULL);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700113 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");
Julia Lawall03b79bd2012-12-07 11:29:32 +0100121 return ret;
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700122 }
123
Viresh Kumarb9c7aff2012-05-29 11:18:51 -0700124 stdev->flags = val;
Viresh Kumarde716e32012-03-21 12:55:03 -0700125 writel_relaxed(stdev->flags, stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700126
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800127 spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0,
Durgadoss R50125a92012-09-18 11:04:56 +0530128 stdev, &ops, NULL, 0, 0);
Dan Carpenter03ee62f2012-03-21 12:55:04 -0700129 if (IS_ERR(spear_thermal)) {
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700130 dev_err(&pdev->dev, "thermal zone device is NULL\n");
Dan Carpenter03ee62f2012-03-21 12:55:04 -0700131 ret = PTR_ERR(spear_thermal);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700132 goto disable_clk;
133 }
Andrzej Pietrasiewiczbbcf90c2020-06-29 14:29:22 +0200134 ret = thermal_zone_device_enable(spear_thermal);
135 if (ret) {
136 dev_err(&pdev->dev, "Cannot enable thermal zone\n");
137 goto unregister_tzd;
138 }
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700139
140 platform_set_drvdata(pdev, spear_thermal);
141
142 dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n",
143 stdev->thermal_base);
144
145 return 0;
146
Andrzej Pietrasiewiczbbcf90c2020-06-29 14:29:22 +0200147unregister_tzd:
148 thermal_zone_device_unregister(spear_thermal);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700149disable_clk:
150 clk_disable(stdev->clk);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700151
152 return ret;
153}
154
155static int spear_thermal_exit(struct platform_device *pdev)
156{
157 unsigned int actual_mask = 0;
158 struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
159 struct spear_thermal_dev *stdev = spear_thermal->devdata;
160
161 thermal_zone_device_unregister(spear_thermal);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700162
163 /* Disable SPEAr Thermal Sensor */
Viresh Kumarde716e32012-03-21 12:55:03 -0700164 actual_mask = readl_relaxed(stdev->thermal_base);
165 writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700166
167 clk_disable(stdev->clk);
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700168
169 return 0;
170}
171
Viresh Kumarb9c7aff2012-05-29 11:18:51 -0700172static const struct of_device_id spear_thermal_id_table[] = {
173 { .compatible = "st,thermal-spear1340" },
174 {}
175};
176MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
177
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700178static struct platform_driver spear_thermal_driver = {
179 .probe = spear_thermal_probe,
180 .remove = spear_thermal_exit,
181 .driver = {
182 .name = "spear_thermal",
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700183 .pm = &spear_thermal_pm_ops,
Sachin Kamat5454f212013-05-16 10:28:11 +0000184 .of_match_table = spear_thermal_id_table,
Vincenzo Frascino6a92c362012-03-21 12:55:03 -0700185 },
186};
187
188module_platform_driver(spear_thermal_driver);
189
190MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
191MODULE_DESCRIPTION("SPEAr thermal driver");
192MODULE_LICENSE("GPL");