rcar_thermal.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *  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>
25
#include <linux/reboot.h>
26 27 28 29
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/thermal.h>

30 31
#define IDLE_INTERVAL	5000

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#define THSCR	0x2c
#define THSSR	0x30

/* THSCR */
#define CPTAP	0xf

/* THSSR */
#define CTEMP	0x3f


struct rcar_thermal_priv {
	void __iomem *base;
	struct device *dev;
	spinlock_t lock;
	u32 comp;
};

49
#define MCELSIUS(temp)			((temp) * 1000)
50
#define rcar_zone_to_priv(zone)		(zone->devdata)
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/*
 *		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)
{
105
	struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	int val, min, max, tmp;

	tmp = -200; /* default */
	while (1) {
		if (priv->comp < 1 || priv->comp > 12) {
			dev_err(priv->dev,
				"THSSR invalid data (%d)\n", priv->comp);
			priv->comp = 4; /* for next thermal */
			return -EINVAL;
		}

		/*
		 * THS comparator offset and the reference temperature
		 *
		 * Comparator	| reference	| Temperature field
		 * offset	| temperature	| measurement
		 *		| (degrees C)	| (degrees C)
		 * -------------+---------------+-------------------
		 *  1		|  -45		|  -45 to  -30
		 *  2		|  -30		|  -30 to  -15
		 *  3		|  -15		|  -15 to    0
		 *  4		|    0		|    0 to  +15
		 *  5		|  +15		|  +15 to  +30
		 *  6		|  +30		|  +30 to  +45
		 *  7		|  +45		|  +45 to  +60
		 *  8		|  +60		|  +60 to  +75
		 *  9		|  +75		|  +75 to  +90
		 * 10		|  +90		|  +90 to +105
		 * 11		| +105		| +105 to +120
		 * 12		| +120		| +120 to +135
		 */

		/* calculate thermal limitation */
		min = (priv->comp * 15) - 60;
		max = min + 15;

		/*
		 * we need to wait 300us after changing comparator offset
		 * to get stable temperature.
		 * see "Usage Notes" on datasheet
		 */
		rcar_thermal_bset(priv, THSCR, CPTAP, priv->comp);
		udelay(300);

		/* calculate current temperature */
		val = rcar_thermal_read(priv, THSSR) & CTEMP;
		val = (val * 5) - 65;

		dev_dbg(priv->dev, "comp/min/max/val = %d/%d/%d/%d\n",
			priv->comp, min, max, val);

		/*
		 * If val is same as min/max, then,
		 * it should try again on next comparator.
		 * But the val might be correct temperature.
		 * Keep it on "tmp" and compare with next val.
		 */
		if (tmp == val)
			break;

		if (val <= min) {
			tmp = min;
			priv->comp--; /* try again */
		} else if (val >= max) {
			tmp = max;
			priv->comp++; /* try again */
		} else {
			tmp = val;
			break;
		}
	}

178
	*temp = MCELSIUS(tmp);
179 180 181
	return 0;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
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;
}

237
static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
238 239 240 241
	.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,
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
};

/*
 *		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->comp = 4; /* basic setup */
	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");
272
		return -ENOMEM;
273 274
	}

275 276 277
	zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv,
					    &rcar_thermal_zone_ops, NULL, 0,
					    IDLE_INTERVAL);
278 279
	if (IS_ERR(zone)) {
		dev_err(&pdev->dev, "thermal zone device is NULL\n");
280
		return PTR_ERR(zone);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	}

	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>");