2012-07-21 02:53:48 +02:00
|
|
|
/*
|
|
|
|
* R-Car THS/TSC thermal sensor driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Renesas Solutions Corp.
|
|
|
|
* Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
|
|
|
*/
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
2012-12-03 03:48:41 +01:00
|
|
|
#include <linux/reboot.h>
|
2012-07-21 02:53:48 +02:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/thermal.h>
|
|
|
|
|
2012-12-03 03:48:41 +01:00
|
|
|
#define IDLE_INTERVAL 5000
|
|
|
|
|
2012-07-21 02:53:48 +02:00
|
|
|
#define THSCR 0x2c
|
|
|
|
#define THSSR 0x30
|
|
|
|
|
|
|
|
/* THSCR */
|
2013-01-31 10:03:11 +01:00
|
|
|
#define CPCTL (1 << 12)
|
2012-07-21 02:53:48 +02:00
|
|
|
|
|
|
|
/* THSSR */
|
|
|
|
#define CTEMP 0x3f
|
|
|
|
|
|
|
|
|
|
|
|
struct rcar_thermal_priv {
|
|
|
|
void __iomem *base;
|
|
|
|
struct device *dev;
|
|
|
|
spinlock_t lock;
|
|
|
|
};
|
|
|
|
|
2012-11-26 03:32:06 +01:00
|
|
|
#define MCELSIUS(temp) ((temp) * 1000)
|
2013-01-31 10:02:51 +01:00
|
|
|
#define rcar_zone_to_priv(zone) ((zone)->devdata)
|
2013-01-31 10:03:11 +01:00
|
|
|
#define rcar_priv_to_dev(priv) ((priv)->dev)
|
2012-11-26 03:32:06 +01:00
|
|
|
|
2012-07-21 02:53:48 +02:00
|
|
|
/*
|
|
|
|
* basic functions
|
|
|
|
*/
|
|
|
|
static u32 rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
u32 ret;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&priv->lock, flags);
|
|
|
|
|
|
|
|
ret = ioread32(priv->base + reg);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&priv->lock, flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 /* no user at this point */
|
|
|
|
static void rcar_thermal_write(struct rcar_thermal_priv *priv,
|
|
|
|
u32 reg, u32 data)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&priv->lock, flags);
|
|
|
|
|
|
|
|
iowrite32(data, priv->base + reg);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&priv->lock, flags);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
|
|
|
|
u32 mask, u32 data)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&priv->lock, flags);
|
|
|
|
|
|
|
|
val = ioread32(priv->base + reg);
|
|
|
|
val &= ~mask;
|
|
|
|
val |= (data & mask);
|
|
|
|
iowrite32(val, priv->base + reg);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&priv->lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* zone device functions
|
|
|
|
*/
|
|
|
|
static int rcar_thermal_get_temp(struct thermal_zone_device *zone,
|
|
|
|
unsigned long *temp)
|
|
|
|
{
|
2012-11-26 03:32:20 +01:00
|
|
|
struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
|
2013-01-31 10:03:11 +01:00
|
|
|
struct device *dev = rcar_priv_to_dev(priv);
|
|
|
|
int i;
|
|
|
|
int ctemp, old, new;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TSC decides a value of CPTAP automatically,
|
|
|
|
* and this is the conditions which validate interrupt.
|
|
|
|
*/
|
|
|
|
rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL);
|
|
|
|
|
|
|
|
ctemp = 0;
|
|
|
|
old = ~0;
|
|
|
|
for (i = 0; i < 128; i++) {
|
2012-07-21 02:53:48 +02:00
|
|
|
/*
|
|
|
|
* we need to wait 300us after changing comparator offset
|
|
|
|
* to get stable temperature.
|
|
|
|
* see "Usage Notes" on datasheet
|
|
|
|
*/
|
|
|
|
udelay(300);
|
|
|
|
|
2013-01-31 10:03:11 +01:00
|
|
|
new = rcar_thermal_read(priv, THSSR) & CTEMP;
|
|
|
|
if (new == old) {
|
|
|
|
ctemp = new;
|
2012-07-21 02:53:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-01-31 10:03:11 +01:00
|
|
|
old = new;
|
2012-07-21 02:53:48 +02:00
|
|
|
}
|
|
|
|
|
2013-01-31 10:03:11 +01:00
|
|
|
if (!ctemp) {
|
|
|
|
dev_err(dev, "thermal sensor was broken\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*temp = MCELSIUS((ctemp * 5) - 65);
|
|
|
|
|
2012-07-21 02:53:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-03 03:48:41 +01:00
|
|
|
static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
|
|
|
|
int trip, enum thermal_trip_type *type)
|
|
|
|
{
|
|
|
|
struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
|
|
|
|
|
|
|
|
/* see rcar_thermal_get_temp() */
|
|
|
|
switch (trip) {
|
|
|
|
case 0: /* +90 <= temp */
|
|
|
|
*type = THERMAL_TRIP_CRITICAL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(priv->dev, "rcar driver trip error\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
|
|
|
|
int trip, unsigned long *temp)
|
|
|
|
{
|
|
|
|
struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
|
|
|
|
|
|
|
|
/* see rcar_thermal_get_temp() */
|
|
|
|
switch (trip) {
|
|
|
|
case 0: /* +90 <= temp */
|
|
|
|
*temp = MCELSIUS(90);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(priv->dev, "rcar driver trip error\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_thermal_notify(struct thermal_zone_device *zone,
|
|
|
|
int trip, enum thermal_trip_type type)
|
|
|
|
{
|
|
|
|
struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case THERMAL_TRIP_CRITICAL:
|
|
|
|
/* FIXME */
|
|
|
|
dev_warn(priv->dev,
|
|
|
|
"Thermal reached to critical temperature\n");
|
|
|
|
machine_power_off();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-21 02:53:48 +02:00
|
|
|
static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
|
2012-12-03 03:48:41 +01:00
|
|
|
.get_temp = rcar_thermal_get_temp,
|
|
|
|
.get_trip_type = rcar_thermal_get_trip_type,
|
|
|
|
.get_trip_temp = rcar_thermal_get_trip_temp,
|
|
|
|
.notify = rcar_thermal_notify,
|
2012-07-21 02:53:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* platform functions
|
|
|
|
*/
|
|
|
|
static int rcar_thermal_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct thermal_zone_device *zone;
|
|
|
|
struct rcar_thermal_priv *priv;
|
|
|
|
struct resource *res;
|
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
if (!res) {
|
|
|
|
dev_err(&pdev->dev, "Could not get platform resource\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
|
|
|
|
if (!priv) {
|
|
|
|
dev_err(&pdev->dev, "Could not allocate priv\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->dev = &pdev->dev;
|
|
|
|
spin_lock_init(&priv->lock);
|
|
|
|
priv->base = devm_ioremap_nocache(&pdev->dev,
|
|
|
|
res->start, resource_size(res));
|
|
|
|
if (!priv->base) {
|
|
|
|
dev_err(&pdev->dev, "Unable to ioremap thermal register\n");
|
2012-10-03 08:51:09 +02:00
|
|
|
return -ENOMEM;
|
2012-07-21 02:53:48 +02:00
|
|
|
}
|
|
|
|
|
2012-12-03 03:48:41 +01:00
|
|
|
zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv,
|
|
|
|
&rcar_thermal_zone_ops, NULL, 0,
|
|
|
|
IDLE_INTERVAL);
|
2012-07-21 02:53:48 +02:00
|
|
|
if (IS_ERR(zone)) {
|
|
|
|
dev_err(&pdev->dev, "thermal zone device is NULL\n");
|
2012-10-03 08:51:09 +02:00
|
|
|
return PTR_ERR(zone);
|
2012-07-21 02:53:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, zone);
|
|
|
|
|
|
|
|
dev_info(&pdev->dev, "proved\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_thermal_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct thermal_zone_device *zone = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
thermal_zone_device_unregister(zone);
|
|
|
|
platform_set_drvdata(pdev, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver rcar_thermal_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "rcar_thermal",
|
|
|
|
},
|
|
|
|
.probe = rcar_thermal_probe,
|
|
|
|
.remove = rcar_thermal_remove,
|
|
|
|
};
|
|
|
|
module_platform_driver(rcar_thermal_driver);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
|
|
|
|
MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
|