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

33 34
#define IDLE_INTERVAL	5000

35 36 37 38 39 40
#define COMMON_STR	0x00
#define COMMON_ENR	0x04
#define COMMON_INTMSK	0x0c

#define REG_POSNEG	0x20
#define REG_FILONOFF	0x28
41 42
#define REG_THSCR	0x2c
#define REG_THSSR	0x30
43
#define REG_INTCTRL	0x34
44 45

/* THSCR */
46
#define CPCTL	(1 << 12)
47 48 49 50

/* THSSR */
#define CTEMP	0x3f

51 52 53 54
struct rcar_thermal_common {
	void __iomem *base;
	struct device *dev;
	struct list_head head;
55
	spinlock_t lock;
56
};
57 58 59

struct rcar_thermal_priv {
	void __iomem *base;
60 61
	struct rcar_thermal_common *common;
	struct thermal_zone_device *zone;
62
	struct delayed_work work;
63
	struct mutex lock;
64
	struct list_head list;
65
	int id;
66
	u32 ctemp;
67 68
};

69 70 71
#define rcar_thermal_for_each_priv(pos, common)	\
	list_for_each_entry(pos, &common->head, list)

72
#define MCELSIUS(temp)			((temp) * 1000)
73
#define rcar_zone_to_priv(zone)		((zone)->devdata)
74 75
#define rcar_priv_to_dev(priv)		((priv)->common->dev)
#define rcar_has_irq_support(priv)	((priv)->common->base)
76 77 78 79 80 81 82
#define rcar_id_to_shift(priv)		((priv)->id * 8)

#ifdef DEBUG
# define rcar_force_update_temp(priv)	1
#else
# define rcar_force_update_temp(priv)	0
#endif
83

84 85 86
/*
 *		basic functions
 */
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#define rcar_thermal_common_read(c, r) \
	_rcar_thermal_common_read(c, COMMON_ ##r)
static u32 _rcar_thermal_common_read(struct rcar_thermal_common *common,
				     u32 reg)
{
	return ioread32(common->base + reg);
}

#define rcar_thermal_common_write(c, r, d) \
	_rcar_thermal_common_write(c, COMMON_ ##r, d)
static void _rcar_thermal_common_write(struct rcar_thermal_common *common,
				       u32 reg, u32 data)
{
	iowrite32(data, common->base + reg);
}

#define rcar_thermal_common_bset(c, r, m, d) \
	_rcar_thermal_common_bset(c, COMMON_ ##r, m, d)
static void _rcar_thermal_common_bset(struct rcar_thermal_common *common,
				      u32 reg, u32 mask, u32 data)
{
	u32 val;

	val = ioread32(common->base + reg);
	val &= ~mask;
	val |= (data & mask);
	iowrite32(val, common->base + reg);
}

#define rcar_thermal_read(p, r) _rcar_thermal_read(p, REG_ ##r)
static u32 _rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
118
{
119
	return ioread32(priv->base + reg);
120 121
}

122 123 124
#define rcar_thermal_write(p, r, d) _rcar_thermal_write(p, REG_ ##r, d)
static void _rcar_thermal_write(struct rcar_thermal_priv *priv,
				u32 reg, u32 data)
125 126 127 128
{
	iowrite32(data, priv->base + reg);
}

129 130 131
#define rcar_thermal_bset(p, r, m, d) _rcar_thermal_bset(p, REG_ ##r, m, d)
static void _rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
			       u32 mask, u32 data)
132 133 134 135 136 137 138 139 140 141 142 143
{
	u32 val;

	val = ioread32(priv->base + reg);
	val &= ~mask;
	val |= (data & mask);
	iowrite32(val, priv->base + reg);
}

/*
 *		zone device functions
 */
144
static int rcar_thermal_update_temp(struct rcar_thermal_priv *priv)
145
{
146 147
	struct device *dev = rcar_priv_to_dev(priv);
	int i;
148
	u32 ctemp, old, new;
149
	int ret = -EINVAL;
150

151 152
	mutex_lock(&priv->lock);

153 154 155 156 157 158 159 160 161
	/*
	 * 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++) {
162 163 164 165 166 167 168
		/*
		 * we need to wait 300us after changing comparator offset
		 * to get stable temperature.
		 * see "Usage Notes" on datasheet
		 */
		udelay(300);

169 170 171
		new = rcar_thermal_read(priv, THSSR) & CTEMP;
		if (new == old) {
			ctemp = new;
172 173
			break;
		}
174
		old = new;
175 176
	}

177 178
	if (!ctemp) {
		dev_err(dev, "thermal sensor was broken\n");
179
		goto err_out_unlock;
180 181
	}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	/*
	 * enable IRQ
	 */
	if (rcar_has_irq_support(priv)) {
		rcar_thermal_write(priv, FILONOFF, 0);

		/* enable Rising/Falling edge interrupt */
		rcar_thermal_write(priv, POSNEG,  0x1);
		rcar_thermal_write(priv, INTCTRL, (((ctemp - 0) << 8) |
						   ((ctemp - 1) << 0)));
	}

	dev_dbg(dev, "thermal%d  %d -> %d\n", priv->id, priv->ctemp, ctemp);

	priv->ctemp = ctemp;
197 198
	ret = 0;
err_out_unlock:
199
	mutex_unlock(&priv->lock);
200
	return ret;
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static int rcar_thermal_get_temp(struct thermal_zone_device *zone,
				 unsigned long *temp)
{
	struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);

	if (!rcar_has_irq_support(priv) || rcar_force_update_temp(priv))
		rcar_thermal_update_temp(priv);

	mutex_lock(&priv->lock);
	*temp =  MCELSIUS((priv->ctemp * 5) - 65);
	mutex_unlock(&priv->lock);

	return 0;
}

218 219 220 221
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);
222
	struct device *dev = rcar_priv_to_dev(priv);
223 224 225 226 227 228 229

	/* see rcar_thermal_get_temp() */
	switch (trip) {
	case 0: /* +90 <= temp */
		*type = THERMAL_TRIP_CRITICAL;
		break;
	default:
230
		dev_err(dev, "rcar driver trip error\n");
231 232 233 234 235 236 237 238 239 240
		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);
241
	struct device *dev = rcar_priv_to_dev(priv);
242 243 244 245 246 247 248

	/* see rcar_thermal_get_temp() */
	switch (trip) {
	case 0: /* +90 <= temp */
		*temp = MCELSIUS(90);
		break;
	default:
249
		dev_err(dev, "rcar driver trip error\n");
250 251 252 253 254 255 256 257 258 259
		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);
260
	struct device *dev = rcar_priv_to_dev(priv);
261 262 263 264

	switch (type) {
	case THERMAL_TRIP_CRITICAL:
		/* FIXME */
265
		dev_warn(dev, "Thermal reached to critical temperature\n");
266 267 268 269 270 271 272 273
		break;
	default:
		break;
	}

	return 0;
}

274
static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
275 276 277 278
	.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,
279 280
};

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/*
 *		interrupt
 */
#define rcar_thermal_irq_enable(p)	_rcar_thermal_irq_ctrl(p, 1)
#define rcar_thermal_irq_disable(p)	_rcar_thermal_irq_ctrl(p, 0)
static void _rcar_thermal_irq_ctrl(struct rcar_thermal_priv *priv, int enable)
{
	struct rcar_thermal_common *common = priv->common;
	unsigned long flags;
	u32 mask = 0x3 << rcar_id_to_shift(priv); /* enable Rising/Falling */

	spin_lock_irqsave(&common->lock, flags);

	rcar_thermal_common_bset(common, INTMSK, mask, enable ? 0 : mask);

	spin_unlock_irqrestore(&common->lock, flags);
}

static void rcar_thermal_work(struct work_struct *work)
{
	struct rcar_thermal_priv *priv;
302
	unsigned long cctemp, nctemp;
303 304 305

	priv = container_of(work, struct rcar_thermal_priv, work.work);

306
	rcar_thermal_get_temp(priv->zone, &cctemp);
307 308
	rcar_thermal_update_temp(priv);
	rcar_thermal_irq_enable(priv);
309 310 311 312

	rcar_thermal_get_temp(priv->zone, &nctemp);
	if (nctemp != cctemp)
		thermal_zone_device_update(priv->zone);
313 314 315 316 317 318 319 320
}

static u32 rcar_thermal_had_changed(struct rcar_thermal_priv *priv, u32 status)
{
	struct device *dev = rcar_priv_to_dev(priv);

	status = (status >> rcar_id_to_shift(priv)) & 0x3;

321
	if (status) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
		dev_dbg(dev, "thermal%d %s%s\n",
			priv->id,
			(status & 0x2) ? "Rising " : "",
			(status & 0x1) ? "Falling" : "");
	}

	return status;
}

static irqreturn_t rcar_thermal_irq(int irq, void *data)
{
	struct rcar_thermal_common *common = data;
	struct rcar_thermal_priv *priv;
	unsigned long flags;
	u32 status, mask;

	spin_lock_irqsave(&common->lock, flags);

	mask	= rcar_thermal_common_read(common, INTMSK);
	status	= rcar_thermal_common_read(common, STR);
	rcar_thermal_common_write(common, STR, 0x000F0F0F & mask);

	spin_unlock_irqrestore(&common->lock, flags);

	status = status & ~mask;

	/*
	 * check the status
	 */
	rcar_thermal_for_each_priv(priv, common) {
		if (rcar_thermal_had_changed(priv, status)) {
			rcar_thermal_irq_disable(priv);
			schedule_delayed_work(&priv->work,
					      msecs_to_jiffies(300));
		}
	}

	return IRQ_HANDLED;
}

362 363 364 365 366
/*
 *		platform functions
 */
static int rcar_thermal_probe(struct platform_device *pdev)
{
367
	struct rcar_thermal_common *common;
368
	struct rcar_thermal_priv *priv;
369 370 371 372
	struct device *dev = &pdev->dev;
	struct resource *res, *irq;
	int mres = 0;
	int i;
373
	int ret = -ENODEV;
374
	int idle = IDLE_INTERVAL;
375
	u32 enr_bits = 0;
376

377
	common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
378
	if (!common)
379 380
		return -ENOMEM;

381
	INIT_LIST_HEAD(&common->head);
382
	spin_lock_init(&common->lock);
383 384
	common->dev = dev;

385 386 387
	pm_runtime_enable(dev);
	pm_runtime_get_sync(dev);

388 389 390 391
	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (irq) {
		/*
		 * platform has IRQ support.
392
		 * Then, driver uses common registers
393 394
		 * rcar_has_irq_support() will be enabled
		 */
395
		res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
396 397 398
		common->base = devm_ioremap_resource(dev, res);
		if (IS_ERR(common->base))
			return PTR_ERR(common->base);
399

400
		idle = 0; /* polling delay is not needed */
401 402
	}

403 404 405 406 407 408 409
	for (i = 0;; i++) {
		res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
		if (!res)
			break;

		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
		if (!priv) {
410 411
			ret = -ENOMEM;
			goto error_unregister;
412 413
		}

414
		priv->base = devm_ioremap_resource(dev, res);
415 416 417 418
		if (IS_ERR(priv->base)) {
			ret = PTR_ERR(priv->base);
			goto error_unregister;
		}
419 420

		priv->common = common;
421
		priv->id = i;
422 423
		mutex_init(&priv->lock);
		INIT_LIST_HEAD(&priv->list);
424 425
		INIT_DELAYED_WORK(&priv->work, rcar_thermal_work);
		rcar_thermal_update_temp(priv);
426 427 428 429

		priv->zone = thermal_zone_device_register("rcar_thermal",
						1, 0, priv,
						&rcar_thermal_zone_ops, NULL, 0,
430
						idle);
431 432
		if (IS_ERR(priv->zone)) {
			dev_err(dev, "can't register thermal zone\n");
433
			ret = PTR_ERR(priv->zone);
434 435 436
			goto error_unregister;
		}

437 438
		if (rcar_has_irq_support(priv))
			rcar_thermal_irq_enable(priv);
439 440

		list_move_tail(&priv->list, &common->head);
441 442 443

		/* update ENR bits */
		enr_bits |= 3 << (i * 8);
444 445
	}

446
	/* enable temperature comparation */
447 448 449 450 451 452 453 454
	if (irq) {
		ret = devm_request_irq(dev, irq->start, rcar_thermal_irq, 0,
				       dev_name(dev), common);
		if (ret) {
			dev_err(dev, "irq request failed\n ");
			goto error_unregister;
		}

455
		rcar_thermal_common_write(common, ENR, enr_bits);
456
	}
457

458
	platform_set_drvdata(pdev, common);
459

460
	dev_info(dev, "%d sensor probed\n", i);
461 462

	return 0;
463 464

error_unregister:
465
	rcar_thermal_for_each_priv(priv, common) {
466
		thermal_zone_device_unregister(priv->zone);
467 468 469
		if (rcar_has_irq_support(priv))
			rcar_thermal_irq_disable(priv);
	}
470

471
	pm_runtime_put(dev);
472 473
	pm_runtime_disable(dev);

474
	return ret;
475 476 477 478
}

static int rcar_thermal_remove(struct platform_device *pdev)
{
479
	struct rcar_thermal_common *common = platform_get_drvdata(pdev);
480
	struct device *dev = &pdev->dev;
481 482
	struct rcar_thermal_priv *priv;

483
	rcar_thermal_for_each_priv(priv, common) {
484
		thermal_zone_device_unregister(priv->zone);
485 486 487
		if (rcar_has_irq_support(priv))
			rcar_thermal_irq_disable(priv);
	}
488

489
	pm_runtime_put(dev);
490 491
	pm_runtime_disable(dev);

492 493 494
	return 0;
}

495
static const struct of_device_id rcar_thermal_dt_ids[] = {
496 497 498 499 500
	{ .compatible = "renesas,rcar-thermal", },
	{},
};
MODULE_DEVICE_TABLE(of, rcar_thermal_dt_ids);

501 502 503
static struct platform_driver rcar_thermal_driver = {
	.driver	= {
		.name	= "rcar_thermal",
504
		.of_match_table = rcar_thermal_dt_ids,
505 506 507 508 509 510 511 512 513
	},
	.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>");