0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/delay.h>
0009 #include <linux/err.h>
0010 #include <linux/irq.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/io.h>
0013 #include <linux/module.h>
0014 #include <linux/of_device.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/pm_runtime.h>
0017 #include <linux/reboot.h>
0018 #include <linux/slab.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/thermal.h>
0021
0022 #include "thermal_hwmon.h"
0023
0024 #define IDLE_INTERVAL 5000
0025
0026 #define COMMON_STR 0x00
0027 #define COMMON_ENR 0x04
0028 #define COMMON_INTMSK 0x0c
0029
0030 #define REG_POSNEG 0x20
0031 #define REG_FILONOFF 0x28
0032 #define REG_THSCR 0x2c
0033 #define REG_THSSR 0x30
0034 #define REG_INTCTRL 0x34
0035
0036
0037 #define CPCTL (1 << 12)
0038
0039
0040 #define CTEMP 0x3f
0041
0042 struct rcar_thermal_common {
0043 void __iomem *base;
0044 struct device *dev;
0045 struct list_head head;
0046 spinlock_t lock;
0047 };
0048
0049 struct rcar_thermal_chip {
0050 unsigned int use_of_thermal : 1;
0051 unsigned int has_filonoff : 1;
0052 unsigned int irq_per_ch : 1;
0053 unsigned int needs_suspend_resume : 1;
0054 unsigned int nirqs;
0055 unsigned int ctemp_bands;
0056 };
0057
0058 static const struct rcar_thermal_chip rcar_thermal = {
0059 .use_of_thermal = 0,
0060 .has_filonoff = 1,
0061 .irq_per_ch = 0,
0062 .needs_suspend_resume = 0,
0063 .nirqs = 1,
0064 .ctemp_bands = 1,
0065 };
0066
0067 static const struct rcar_thermal_chip rcar_gen2_thermal = {
0068 .use_of_thermal = 1,
0069 .has_filonoff = 1,
0070 .irq_per_ch = 0,
0071 .needs_suspend_resume = 0,
0072 .nirqs = 1,
0073 .ctemp_bands = 1,
0074 };
0075
0076 static const struct rcar_thermal_chip rcar_gen3_thermal = {
0077 .use_of_thermal = 1,
0078 .has_filonoff = 0,
0079 .irq_per_ch = 1,
0080 .needs_suspend_resume = 1,
0081
0082
0083
0084
0085 .nirqs = 2,
0086 .ctemp_bands = 2,
0087 };
0088
0089 struct rcar_thermal_priv {
0090 void __iomem *base;
0091 struct rcar_thermal_common *common;
0092 struct thermal_zone_device *zone;
0093 const struct rcar_thermal_chip *chip;
0094 struct delayed_work work;
0095 struct mutex lock;
0096 struct list_head list;
0097 int id;
0098 };
0099
0100 #define rcar_thermal_for_each_priv(pos, common) \
0101 list_for_each_entry(pos, &common->head, list)
0102
0103 #define MCELSIUS(temp) ((temp) * 1000)
0104 #define rcar_zone_to_priv(zone) ((zone)->devdata)
0105 #define rcar_priv_to_dev(priv) ((priv)->common->dev)
0106 #define rcar_has_irq_support(priv) ((priv)->common->base)
0107 #define rcar_id_to_shift(priv) ((priv)->id * 8)
0108
0109 static const struct of_device_id rcar_thermal_dt_ids[] = {
0110 {
0111 .compatible = "renesas,rcar-thermal",
0112 .data = &rcar_thermal,
0113 },
0114 {
0115 .compatible = "renesas,rcar-gen2-thermal",
0116 .data = &rcar_gen2_thermal,
0117 },
0118 {
0119 .compatible = "renesas,thermal-r8a774c0",
0120 .data = &rcar_gen3_thermal,
0121 },
0122 {
0123 .compatible = "renesas,thermal-r8a77970",
0124 .data = &rcar_gen3_thermal,
0125 },
0126 {
0127 .compatible = "renesas,thermal-r8a77990",
0128 .data = &rcar_gen3_thermal,
0129 },
0130 {
0131 .compatible = "renesas,thermal-r8a77995",
0132 .data = &rcar_gen3_thermal,
0133 },
0134 {},
0135 };
0136 MODULE_DEVICE_TABLE(of, rcar_thermal_dt_ids);
0137
0138
0139
0140
0141 #define rcar_thermal_common_read(c, r) \
0142 _rcar_thermal_common_read(c, COMMON_ ##r)
0143 static u32 _rcar_thermal_common_read(struct rcar_thermal_common *common,
0144 u32 reg)
0145 {
0146 return ioread32(common->base + reg);
0147 }
0148
0149 #define rcar_thermal_common_write(c, r, d) \
0150 _rcar_thermal_common_write(c, COMMON_ ##r, d)
0151 static void _rcar_thermal_common_write(struct rcar_thermal_common *common,
0152 u32 reg, u32 data)
0153 {
0154 iowrite32(data, common->base + reg);
0155 }
0156
0157 #define rcar_thermal_common_bset(c, r, m, d) \
0158 _rcar_thermal_common_bset(c, COMMON_ ##r, m, d)
0159 static void _rcar_thermal_common_bset(struct rcar_thermal_common *common,
0160 u32 reg, u32 mask, u32 data)
0161 {
0162 u32 val;
0163
0164 val = ioread32(common->base + reg);
0165 val &= ~mask;
0166 val |= (data & mask);
0167 iowrite32(val, common->base + reg);
0168 }
0169
0170 #define rcar_thermal_read(p, r) _rcar_thermal_read(p, REG_ ##r)
0171 static u32 _rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
0172 {
0173 return ioread32(priv->base + reg);
0174 }
0175
0176 #define rcar_thermal_write(p, r, d) _rcar_thermal_write(p, REG_ ##r, d)
0177 static void _rcar_thermal_write(struct rcar_thermal_priv *priv,
0178 u32 reg, u32 data)
0179 {
0180 iowrite32(data, priv->base + reg);
0181 }
0182
0183 #define rcar_thermal_bset(p, r, m, d) _rcar_thermal_bset(p, REG_ ##r, m, d)
0184 static void _rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
0185 u32 mask, u32 data)
0186 {
0187 u32 val;
0188
0189 val = ioread32(priv->base + reg);
0190 val &= ~mask;
0191 val |= (data & mask);
0192 iowrite32(val, priv->base + reg);
0193 }
0194
0195
0196
0197
0198 static int rcar_thermal_update_temp(struct rcar_thermal_priv *priv)
0199 {
0200 struct device *dev = rcar_priv_to_dev(priv);
0201 int old, new, ctemp = -EINVAL;
0202 unsigned int i;
0203
0204 mutex_lock(&priv->lock);
0205
0206
0207
0208
0209
0210 rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL);
0211
0212 old = ~0;
0213 for (i = 0; i < 128; i++) {
0214
0215
0216
0217
0218
0219 usleep_range(300, 400);
0220
0221 new = rcar_thermal_read(priv, THSSR) & CTEMP;
0222 if (new == old) {
0223 ctemp = new;
0224 break;
0225 }
0226 old = new;
0227 }
0228
0229 if (ctemp < 0) {
0230 dev_err(dev, "thermal sensor was broken\n");
0231 goto err_out_unlock;
0232 }
0233
0234
0235
0236
0237 if (rcar_has_irq_support(priv)) {
0238 if (priv->chip->has_filonoff)
0239 rcar_thermal_write(priv, FILONOFF, 0);
0240
0241
0242 rcar_thermal_write(priv, POSNEG, 0x1);
0243 rcar_thermal_write(priv, INTCTRL, (((ctemp - 0) << 8) |
0244 ((ctemp - 1) << 0)));
0245 }
0246
0247 err_out_unlock:
0248 mutex_unlock(&priv->lock);
0249
0250 return ctemp;
0251 }
0252
0253 static int rcar_thermal_get_current_temp(struct rcar_thermal_priv *priv,
0254 int *temp)
0255 {
0256 int ctemp;
0257
0258 ctemp = rcar_thermal_update_temp(priv);
0259 if (ctemp < 0)
0260 return ctemp;
0261
0262
0263
0264 if (priv->chip->ctemp_bands == 1)
0265 *temp = MCELSIUS((ctemp * 5) - 65);
0266 else if (ctemp < 24)
0267 *temp = MCELSIUS(((ctemp * 55) - 720) / 10);
0268 else
0269 *temp = MCELSIUS((ctemp * 5) - 60);
0270
0271 return 0;
0272 }
0273
0274 static int rcar_thermal_of_get_temp(void *data, int *temp)
0275 {
0276 struct rcar_thermal_priv *priv = data;
0277
0278 return rcar_thermal_get_current_temp(priv, temp);
0279 }
0280
0281 static int rcar_thermal_get_temp(struct thermal_zone_device *zone, int *temp)
0282 {
0283 struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
0284
0285 return rcar_thermal_get_current_temp(priv, temp);
0286 }
0287
0288 static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
0289 int trip, enum thermal_trip_type *type)
0290 {
0291 struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
0292 struct device *dev = rcar_priv_to_dev(priv);
0293
0294
0295 switch (trip) {
0296 case 0:
0297 *type = THERMAL_TRIP_CRITICAL;
0298 break;
0299 default:
0300 dev_err(dev, "rcar driver trip error\n");
0301 return -EINVAL;
0302 }
0303
0304 return 0;
0305 }
0306
0307 static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
0308 int trip, int *temp)
0309 {
0310 struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
0311 struct device *dev = rcar_priv_to_dev(priv);
0312
0313
0314 switch (trip) {
0315 case 0:
0316 *temp = MCELSIUS(90);
0317 break;
0318 default:
0319 dev_err(dev, "rcar driver trip error\n");
0320 return -EINVAL;
0321 }
0322
0323 return 0;
0324 }
0325
0326 static const struct thermal_zone_of_device_ops rcar_thermal_zone_of_ops = {
0327 .get_temp = rcar_thermal_of_get_temp,
0328 };
0329
0330 static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
0331 .get_temp = rcar_thermal_get_temp,
0332 .get_trip_type = rcar_thermal_get_trip_type,
0333 .get_trip_temp = rcar_thermal_get_trip_temp,
0334 };
0335
0336
0337
0338
0339 #define rcar_thermal_irq_enable(p) _rcar_thermal_irq_ctrl(p, 1)
0340 #define rcar_thermal_irq_disable(p) _rcar_thermal_irq_ctrl(p, 0)
0341 static void _rcar_thermal_irq_ctrl(struct rcar_thermal_priv *priv, int enable)
0342 {
0343 struct rcar_thermal_common *common = priv->common;
0344 unsigned long flags;
0345 u32 mask = 0x3 << rcar_id_to_shift(priv);
0346
0347 if (!rcar_has_irq_support(priv))
0348 return;
0349
0350 spin_lock_irqsave(&common->lock, flags);
0351
0352 rcar_thermal_common_bset(common, INTMSK, mask, enable ? 0 : mask);
0353
0354 spin_unlock_irqrestore(&common->lock, flags);
0355 }
0356
0357 static void rcar_thermal_work(struct work_struct *work)
0358 {
0359 struct rcar_thermal_priv *priv;
0360 int ret;
0361
0362 priv = container_of(work, struct rcar_thermal_priv, work.work);
0363
0364 ret = rcar_thermal_update_temp(priv);
0365 if (ret < 0)
0366 return;
0367
0368 rcar_thermal_irq_enable(priv);
0369
0370 thermal_zone_device_update(priv->zone, THERMAL_EVENT_UNSPECIFIED);
0371 }
0372
0373 static u32 rcar_thermal_had_changed(struct rcar_thermal_priv *priv, u32 status)
0374 {
0375 struct device *dev = rcar_priv_to_dev(priv);
0376
0377 status = (status >> rcar_id_to_shift(priv)) & 0x3;
0378
0379 if (status) {
0380 dev_dbg(dev, "thermal%d %s%s\n",
0381 priv->id,
0382 (status & 0x2) ? "Rising " : "",
0383 (status & 0x1) ? "Falling" : "");
0384 }
0385
0386 return status;
0387 }
0388
0389 static irqreturn_t rcar_thermal_irq(int irq, void *data)
0390 {
0391 struct rcar_thermal_common *common = data;
0392 struct rcar_thermal_priv *priv;
0393 u32 status, mask;
0394
0395 spin_lock(&common->lock);
0396
0397 mask = rcar_thermal_common_read(common, INTMSK);
0398 status = rcar_thermal_common_read(common, STR);
0399 rcar_thermal_common_write(common, STR, 0x000F0F0F & mask);
0400
0401 spin_unlock(&common->lock);
0402
0403 status = status & ~mask;
0404
0405
0406
0407
0408 rcar_thermal_for_each_priv(priv, common) {
0409 if (rcar_thermal_had_changed(priv, status)) {
0410 rcar_thermal_irq_disable(priv);
0411 queue_delayed_work(system_freezable_wq, &priv->work,
0412 msecs_to_jiffies(300));
0413 }
0414 }
0415
0416 return IRQ_HANDLED;
0417 }
0418
0419
0420
0421
0422 static int rcar_thermal_remove(struct platform_device *pdev)
0423 {
0424 struct rcar_thermal_common *common = platform_get_drvdata(pdev);
0425 struct device *dev = &pdev->dev;
0426 struct rcar_thermal_priv *priv;
0427
0428 rcar_thermal_for_each_priv(priv, common) {
0429 rcar_thermal_irq_disable(priv);
0430 cancel_delayed_work_sync(&priv->work);
0431 if (priv->chip->use_of_thermal)
0432 thermal_remove_hwmon_sysfs(priv->zone);
0433 else
0434 thermal_zone_device_unregister(priv->zone);
0435 }
0436
0437 pm_runtime_put(dev);
0438 pm_runtime_disable(dev);
0439
0440 return 0;
0441 }
0442
0443 static int rcar_thermal_probe(struct platform_device *pdev)
0444 {
0445 struct rcar_thermal_common *common;
0446 struct rcar_thermal_priv *priv;
0447 struct device *dev = &pdev->dev;
0448 struct resource *res;
0449 const struct rcar_thermal_chip *chip = of_device_get_match_data(dev);
0450 int mres = 0;
0451 int i;
0452 int ret = -ENODEV;
0453 int idle = IDLE_INTERVAL;
0454 u32 enr_bits = 0;
0455
0456 common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
0457 if (!common)
0458 return -ENOMEM;
0459
0460 platform_set_drvdata(pdev, common);
0461
0462 INIT_LIST_HEAD(&common->head);
0463 spin_lock_init(&common->lock);
0464 common->dev = dev;
0465
0466 pm_runtime_enable(dev);
0467 pm_runtime_get_sync(dev);
0468
0469 for (i = 0; i < chip->nirqs; i++) {
0470 int irq;
0471
0472 ret = platform_get_irq_optional(pdev, i);
0473 if (ret < 0 && ret != -ENXIO)
0474 goto error_unregister;
0475 if (ret > 0)
0476 irq = ret;
0477 else
0478 break;
0479
0480 if (!common->base) {
0481
0482
0483
0484
0485
0486 res = platform_get_resource(pdev, IORESOURCE_MEM,
0487 mres++);
0488 common->base = devm_ioremap_resource(dev, res);
0489 if (IS_ERR(common->base)) {
0490 ret = PTR_ERR(common->base);
0491 goto error_unregister;
0492 }
0493
0494 idle = 0;
0495 }
0496
0497 ret = devm_request_irq(dev, irq, rcar_thermal_irq,
0498 IRQF_SHARED, dev_name(dev), common);
0499 if (ret) {
0500 dev_err(dev, "irq request failed\n ");
0501 goto error_unregister;
0502 }
0503
0504
0505 if (chip->irq_per_ch)
0506 enr_bits |= 1 << i;
0507 }
0508
0509 for (i = 0;; i++) {
0510 res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
0511 if (!res)
0512 break;
0513
0514 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0515 if (!priv) {
0516 ret = -ENOMEM;
0517 goto error_unregister;
0518 }
0519
0520 priv->base = devm_ioremap_resource(dev, res);
0521 if (IS_ERR(priv->base)) {
0522 ret = PTR_ERR(priv->base);
0523 goto error_unregister;
0524 }
0525
0526 priv->common = common;
0527 priv->id = i;
0528 priv->chip = chip;
0529 mutex_init(&priv->lock);
0530 INIT_LIST_HEAD(&priv->list);
0531 INIT_DELAYED_WORK(&priv->work, rcar_thermal_work);
0532 ret = rcar_thermal_update_temp(priv);
0533 if (ret < 0)
0534 goto error_unregister;
0535
0536 if (chip->use_of_thermal) {
0537 priv->zone = devm_thermal_zone_of_sensor_register(
0538 dev, i, priv,
0539 &rcar_thermal_zone_of_ops);
0540 } else {
0541 priv->zone = thermal_zone_device_register(
0542 "rcar_thermal",
0543 1, 0, priv,
0544 &rcar_thermal_zone_ops, NULL, 0,
0545 idle);
0546
0547 ret = thermal_zone_device_enable(priv->zone);
0548 if (ret) {
0549 thermal_zone_device_unregister(priv->zone);
0550 priv->zone = ERR_PTR(ret);
0551 }
0552 }
0553 if (IS_ERR(priv->zone)) {
0554 dev_err(dev, "can't register thermal zone\n");
0555 ret = PTR_ERR(priv->zone);
0556 priv->zone = NULL;
0557 goto error_unregister;
0558 }
0559
0560 if (chip->use_of_thermal) {
0561
0562
0563
0564
0565 priv->zone->tzp->no_hwmon = false;
0566 ret = thermal_add_hwmon_sysfs(priv->zone);
0567 if (ret)
0568 goto error_unregister;
0569 }
0570
0571 rcar_thermal_irq_enable(priv);
0572
0573 list_move_tail(&priv->list, &common->head);
0574
0575
0576 if (!chip->irq_per_ch)
0577 enr_bits |= 3 << (i * 8);
0578 }
0579
0580 if (common->base && enr_bits)
0581 rcar_thermal_common_write(common, ENR, enr_bits);
0582
0583 dev_info(dev, "%d sensor probed\n", i);
0584
0585 return 0;
0586
0587 error_unregister:
0588 rcar_thermal_remove(pdev);
0589
0590 return ret;
0591 }
0592
0593 #ifdef CONFIG_PM_SLEEP
0594 static int rcar_thermal_suspend(struct device *dev)
0595 {
0596 struct rcar_thermal_common *common = dev_get_drvdata(dev);
0597 struct rcar_thermal_priv *priv = list_first_entry(&common->head,
0598 typeof(*priv), list);
0599
0600 if (priv->chip->needs_suspend_resume) {
0601 rcar_thermal_common_write(common, ENR, 0);
0602 rcar_thermal_irq_disable(priv);
0603 rcar_thermal_bset(priv, THSCR, CPCTL, 0);
0604 }
0605
0606 return 0;
0607 }
0608
0609 static int rcar_thermal_resume(struct device *dev)
0610 {
0611 struct rcar_thermal_common *common = dev_get_drvdata(dev);
0612 struct rcar_thermal_priv *priv = list_first_entry(&common->head,
0613 typeof(*priv), list);
0614 int ret;
0615
0616 if (priv->chip->needs_suspend_resume) {
0617 ret = rcar_thermal_update_temp(priv);
0618 if (ret < 0)
0619 return ret;
0620 rcar_thermal_irq_enable(priv);
0621 rcar_thermal_common_write(common, ENR, 0x03);
0622 }
0623
0624 return 0;
0625 }
0626 #endif
0627
0628 static SIMPLE_DEV_PM_OPS(rcar_thermal_pm_ops, rcar_thermal_suspend,
0629 rcar_thermal_resume);
0630
0631 static struct platform_driver rcar_thermal_driver = {
0632 .driver = {
0633 .name = "rcar_thermal",
0634 .pm = &rcar_thermal_pm_ops,
0635 .of_match_table = rcar_thermal_dt_ids,
0636 },
0637 .probe = rcar_thermal_probe,
0638 .remove = rcar_thermal_remove,
0639 };
0640 module_platform_driver(rcar_thermal_driver);
0641
0642 MODULE_LICENSE("GPL v2");
0643 MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
0644 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");