2019-05-29 22:17:56 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-03-22 03:55:03 +08:00
|
|
|
/*
|
|
|
|
* SPEAr thermal driver.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011-2012 ST Microelectronics
|
|
|
|
* Author: Vincenzo Frascino <vincenzo.frascino@st.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
2012-05-30 02:18:51 +08:00
|
|
|
#include <linux/of.h>
|
2012-03-22 03:55:03 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/thermal.h>
|
|
|
|
|
|
|
|
#define MD_FACTOR 1000
|
|
|
|
|
|
|
|
/* SPEAr Thermal Sensor Dev Structure */
|
|
|
|
struct spear_thermal_dev {
|
|
|
|
/* pointer to base address of the thermal sensor */
|
|
|
|
void __iomem *thermal_base;
|
|
|
|
/* clk structure */
|
|
|
|
struct clk *clk;
|
|
|
|
/* pointer to thermal flags */
|
|
|
|
unsigned int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int thermal_get_temp(struct thermal_zone_device *thermal,
|
2015-07-24 14:12:54 +08:00
|
|
|
int *temp)
|
2012-03-22 03:55:03 +08:00
|
|
|
{
|
2023-03-02 04:14:30 +08:00
|
|
|
struct spear_thermal_dev *stdev = thermal_zone_device_priv(thermal);
|
2012-03-22 03:55:03 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Data are ready to be read after 628 usec from POWERDOWN signal
|
|
|
|
* (PDN) = 1
|
|
|
|
*/
|
2012-03-22 03:55:03 +08:00
|
|
|
*temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
|
2012-03-22 03:55:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct thermal_zone_device_ops ops = {
|
|
|
|
.get_temp = thermal_get_temp,
|
|
|
|
};
|
|
|
|
|
2016-01-26 00:44:10 +08:00
|
|
|
static int __maybe_unused spear_thermal_suspend(struct device *dev)
|
2012-03-22 03:55:03 +08:00
|
|
|
{
|
2018-10-22 04:00:50 +08:00
|
|
|
struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
|
2023-03-02 04:14:30 +08:00
|
|
|
struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
|
2012-03-22 03:55:03 +08:00
|
|
|
unsigned int actual_mask = 0;
|
|
|
|
|
|
|
|
/* Disable SPEAr Thermal Sensor */
|
2012-03-22 03:55:03 +08:00
|
|
|
actual_mask = readl_relaxed(stdev->thermal_base);
|
|
|
|
writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
|
2012-03-22 03:55:03 +08:00
|
|
|
|
|
|
|
clk_disable(stdev->clk);
|
|
|
|
dev_info(dev, "Suspended.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-26 00:44:10 +08:00
|
|
|
static int __maybe_unused spear_thermal_resume(struct device *dev)
|
2012-03-22 03:55:03 +08:00
|
|
|
{
|
2018-10-22 04:00:50 +08:00
|
|
|
struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
|
2023-03-02 04:14:30 +08:00
|
|
|
struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
|
2012-03-22 03:55:03 +08:00
|
|
|
unsigned int actual_mask = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = clk_enable(stdev->clk);
|
|
|
|
if (ret) {
|
2018-10-22 04:00:50 +08:00
|
|
|
dev_err(dev, "Can't enable clock\n");
|
2012-03-22 03:55:03 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable SPEAr Thermal Sensor */
|
2012-03-22 03:55:03 +08:00
|
|
|
actual_mask = readl_relaxed(stdev->thermal_base);
|
|
|
|
writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
|
2012-03-22 03:55:03 +08:00
|
|
|
|
|
|
|
dev_info(dev, "Resumed.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend,
|
|
|
|
spear_thermal_resume);
|
|
|
|
|
|
|
|
static int spear_thermal_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct thermal_zone_device *spear_thermal = NULL;
|
|
|
|
struct spear_thermal_dev *stdev;
|
2012-05-30 02:18:51 +08:00
|
|
|
struct device_node *np = pdev->dev.of_node;
|
|
|
|
int ret = 0, val;
|
|
|
|
|
|
|
|
if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) {
|
|
|
|
dev_err(&pdev->dev, "Failed: DT Pdata not passed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2012-03-22 03:55:03 +08:00
|
|
|
|
|
|
|
stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
|
2014-05-07 14:05:32 +08:00
|
|
|
if (!stdev)
|
2012-03-22 03:55:03 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Enable thermal sensor */
|
2023-01-18 16:44:43 +08:00
|
|
|
stdev->thermal_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
|
2013-05-16 10:16:21 +08:00
|
|
|
if (IS_ERR(stdev->thermal_base))
|
2013-05-16 10:16:20 +08:00
|
|
|
return PTR_ERR(stdev->thermal_base);
|
2012-03-22 03:55:03 +08:00
|
|
|
|
2012-12-07 18:29:32 +08:00
|
|
|
stdev->clk = devm_clk_get(&pdev->dev, NULL);
|
2012-03-22 03:55:03 +08:00
|
|
|
if (IS_ERR(stdev->clk)) {
|
|
|
|
dev_err(&pdev->dev, "Can't get clock\n");
|
|
|
|
return PTR_ERR(stdev->clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_enable(stdev->clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "Can't enable clock\n");
|
2012-12-07 18:29:32 +08:00
|
|
|
return ret;
|
2012-03-22 03:55:03 +08:00
|
|
|
}
|
|
|
|
|
2012-05-30 02:18:51 +08:00
|
|
|
stdev->flags = val;
|
2012-03-22 03:55:03 +08:00
|
|
|
writel_relaxed(stdev->flags, stdev->thermal_base);
|
2012-03-22 03:55:03 +08:00
|
|
|
|
2023-08-31 00:14:57 +08:00
|
|
|
spear_thermal = thermal_tripless_zone_device_register("spear_thermal",
|
|
|
|
stdev, &ops, NULL);
|
2012-03-22 03:55:04 +08:00
|
|
|
if (IS_ERR(spear_thermal)) {
|
2012-03-22 03:55:03 +08:00
|
|
|
dev_err(&pdev->dev, "thermal zone device is NULL\n");
|
2012-03-22 03:55:04 +08:00
|
|
|
ret = PTR_ERR(spear_thermal);
|
2012-03-22 03:55:03 +08:00
|
|
|
goto disable_clk;
|
|
|
|
}
|
2020-06-29 20:29:22 +08:00
|
|
|
ret = thermal_zone_device_enable(spear_thermal);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "Cannot enable thermal zone\n");
|
|
|
|
goto unregister_tzd;
|
|
|
|
}
|
2012-03-22 03:55:03 +08:00
|
|
|
|
|
|
|
platform_set_drvdata(pdev, spear_thermal);
|
|
|
|
|
2023-03-02 04:14:39 +08:00
|
|
|
dev_info(&pdev->dev, "Thermal Sensor Loaded at: 0x%p.\n",
|
2012-03-22 03:55:03 +08:00
|
|
|
stdev->thermal_base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2020-06-29 20:29:22 +08:00
|
|
|
unregister_tzd:
|
|
|
|
thermal_zone_device_unregister(spear_thermal);
|
2012-03-22 03:55:03 +08:00
|
|
|
disable_clk:
|
|
|
|
clk_disable(stdev->clk);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-28 03:37:30 +08:00
|
|
|
static void spear_thermal_exit(struct platform_device *pdev)
|
2012-03-22 03:55:03 +08:00
|
|
|
{
|
|
|
|
unsigned int actual_mask = 0;
|
|
|
|
struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
|
2023-03-02 04:14:30 +08:00
|
|
|
struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
|
2012-03-22 03:55:03 +08:00
|
|
|
|
|
|
|
thermal_zone_device_unregister(spear_thermal);
|
|
|
|
|
|
|
|
/* Disable SPEAr Thermal Sensor */
|
2012-03-22 03:55:03 +08:00
|
|
|
actual_mask = readl_relaxed(stdev->thermal_base);
|
|
|
|
writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
|
2012-03-22 03:55:03 +08:00
|
|
|
|
|
|
|
clk_disable(stdev->clk);
|
|
|
|
}
|
|
|
|
|
2012-05-30 02:18:51 +08:00
|
|
|
static const struct of_device_id spear_thermal_id_table[] = {
|
|
|
|
{ .compatible = "st,thermal-spear1340" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
|
|
|
|
|
2012-03-22 03:55:03 +08:00
|
|
|
static struct platform_driver spear_thermal_driver = {
|
|
|
|
.probe = spear_thermal_probe,
|
2023-09-28 03:37:30 +08:00
|
|
|
.remove_new = spear_thermal_exit,
|
2012-03-22 03:55:03 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "spear_thermal",
|
|
|
|
.pm = &spear_thermal_pm_ops,
|
2013-05-16 18:28:11 +08:00
|
|
|
.of_match_table = spear_thermal_id_table,
|
2012-03-22 03:55:03 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(spear_thermal_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
|
|
|
|
MODULE_DESCRIPTION("SPEAr thermal driver");
|
|
|
|
MODULE_LICENSE("GPL");
|