2 * SPEAr thermal driver.
4 * Copyright (C) 2011-2012 ST Microelectronics
5 * Author: Vincenzo Frascino <vincenzo.frascino@st.com>
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.
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.
18 #include <linux/clk.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/platform_data/spear_thermal.h>
26 #include <linux/thermal.h>
28 #define MD_FACTOR 1000
30 /* SPEAr Thermal Sensor Dev Structure */
31 struct spear_thermal_dev
{
32 /* pointer to base address of the thermal sensor */
33 void __iomem
*thermal_base
;
36 /* pointer to thermal flags */
40 static inline int thermal_get_temp(struct thermal_zone_device
*thermal
,
43 struct spear_thermal_dev
*stdev
= thermal
->devdata
;
46 * Data are ready to be read after 628 usec from POWERDOWN signal
49 *temp
= (readl_relaxed(stdev
->thermal_base
) & 0x7F) * MD_FACTOR
;
53 static struct thermal_zone_device_ops ops
= {
54 .get_temp
= thermal_get_temp
,
58 static int spear_thermal_suspend(struct device
*dev
)
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;
65 /* Disable SPEAr Thermal Sensor */
66 actual_mask
= readl_relaxed(stdev
->thermal_base
);
67 writel_relaxed(actual_mask
& ~stdev
->flags
, stdev
->thermal_base
);
69 clk_disable(stdev
->clk
);
70 dev_info(dev
, "Suspended.\n");
75 static int spear_thermal_resume(struct device
*dev
)
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;
83 ret
= clk_enable(stdev
->clk
);
85 dev_err(&pdev
->dev
, "Can't enable clock\n");
89 /* Enable SPEAr Thermal Sensor */
90 actual_mask
= readl_relaxed(stdev
->thermal_base
);
91 writel_relaxed(actual_mask
| stdev
->flags
, stdev
->thermal_base
);
93 dev_info(dev
, "Resumed.\n");
99 static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops
, spear_thermal_suspend
,
100 spear_thermal_resume
);
102 static int spear_thermal_probe(struct platform_device
*pdev
)
104 struct thermal_zone_device
*spear_thermal
= NULL
;
105 struct spear_thermal_dev
*stdev
;
106 struct spear_thermal_pdata
*pdata
;
108 struct resource
*stres
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
111 dev_err(&pdev
->dev
, "memory resource missing\n");
115 pdata
= dev_get_platdata(&pdev
->dev
);
117 dev_err(&pdev
->dev
, "platform data is NULL\n");
121 stdev
= devm_kzalloc(&pdev
->dev
, sizeof(*stdev
), GFP_KERNEL
);
123 dev_err(&pdev
->dev
, "kzalloc fail\n");
127 /* Enable thermal sensor */
128 stdev
->thermal_base
= devm_ioremap(&pdev
->dev
, stres
->start
,
129 resource_size(stres
));
130 if (!stdev
->thermal_base
) {
131 dev_err(&pdev
->dev
, "ioremap failed\n");
135 stdev
->clk
= clk_get(&pdev
->dev
, NULL
);
136 if (IS_ERR(stdev
->clk
)) {
137 dev_err(&pdev
->dev
, "Can't get clock\n");
138 return PTR_ERR(stdev
->clk
);
141 ret
= clk_enable(stdev
->clk
);
143 dev_err(&pdev
->dev
, "Can't enable clock\n");
147 stdev
->flags
= pdata
->thermal_flags
;
148 writel_relaxed(stdev
->flags
, stdev
->thermal_base
);
150 spear_thermal
= thermal_zone_device_register("spear_thermal", 0,
151 stdev
, &ops
, 0, 0, 0, 0);
152 if (IS_ERR(spear_thermal
)) {
153 dev_err(&pdev
->dev
, "thermal zone device is NULL\n");
154 ret
= PTR_ERR(spear_thermal
);
158 platform_set_drvdata(pdev
, spear_thermal
);
160 dev_info(&spear_thermal
->device
, "Thermal Sensor Loaded at: 0x%p.\n",
161 stdev
->thermal_base
);
166 clk_disable(stdev
->clk
);
173 static int spear_thermal_exit(struct platform_device
*pdev
)
175 unsigned int actual_mask
= 0;
176 struct thermal_zone_device
*spear_thermal
= platform_get_drvdata(pdev
);
177 struct spear_thermal_dev
*stdev
= spear_thermal
->devdata
;
179 thermal_zone_device_unregister(spear_thermal
);
180 platform_set_drvdata(pdev
, NULL
);
182 /* Disable SPEAr Thermal Sensor */
183 actual_mask
= readl_relaxed(stdev
->thermal_base
);
184 writel_relaxed(actual_mask
& ~stdev
->flags
, stdev
->thermal_base
);
186 clk_disable(stdev
->clk
);
192 static struct platform_driver spear_thermal_driver
= {
193 .probe
= spear_thermal_probe
,
194 .remove
= spear_thermal_exit
,
196 .name
= "spear_thermal",
197 .owner
= THIS_MODULE
,
198 .pm
= &spear_thermal_pm_ops
,
202 module_platform_driver(spear_thermal_driver
);
204 MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
205 MODULE_DESCRIPTION("SPEAr thermal driver");
206 MODULE_LICENSE("GPL");