^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * R-Car THS/TSC thermal sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Renesas Solutions Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "thermal_hwmon.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define IDLE_INTERVAL 5000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define COMMON_STR 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define COMMON_ENR 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define COMMON_INTMSK 0x0c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define REG_POSNEG 0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define REG_FILONOFF 0x28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define REG_THSCR 0x2c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define REG_THSSR 0x30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define REG_INTCTRL 0x34
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* THSCR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define CPCTL (1 << 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* THSSR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define CTEMP 0x3f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct rcar_thermal_common {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct list_head head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct rcar_thermal_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned int use_of_thermal : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int has_filonoff : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int irq_per_ch : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int needs_suspend_resume : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int nirqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned int ctemp_bands;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static const struct rcar_thermal_chip rcar_thermal = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .use_of_thermal = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .has_filonoff = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .irq_per_ch = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .needs_suspend_resume = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .nirqs = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .ctemp_bands = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static const struct rcar_thermal_chip rcar_gen2_thermal = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .use_of_thermal = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .has_filonoff = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .irq_per_ch = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .needs_suspend_resume = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .nirqs = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .ctemp_bands = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static const struct rcar_thermal_chip rcar_gen3_thermal = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .use_of_thermal = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .has_filonoff = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .irq_per_ch = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .needs_suspend_resume = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * The Gen3 chip has 3 interrupts, but this driver uses only 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * interrupts to detect a temperature change, rise or fall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .nirqs = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .ctemp_bands = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct rcar_thermal_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct rcar_thermal_common *common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct thermal_zone_device *zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) const struct rcar_thermal_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct delayed_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define rcar_thermal_for_each_priv(pos, common) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) list_for_each_entry(pos, &common->head, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define MCELSIUS(temp) ((temp) * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define rcar_zone_to_priv(zone) ((zone)->devdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define rcar_priv_to_dev(priv) ((priv)->common->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #define rcar_has_irq_support(priv) ((priv)->common->base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define rcar_id_to_shift(priv) ((priv)->id * 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static const struct of_device_id rcar_thermal_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .compatible = "renesas,rcar-thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .data = &rcar_thermal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .compatible = "renesas,rcar-gen2-thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .data = &rcar_gen2_thermal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .compatible = "renesas,thermal-r8a774c0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .data = &rcar_gen3_thermal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .compatible = "renesas,thermal-r8a77970",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .data = &rcar_gen3_thermal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .compatible = "renesas,thermal-r8a77990",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .data = &rcar_gen3_thermal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .compatible = "renesas,thermal-r8a77995",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .data = &rcar_gen3_thermal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) MODULE_DEVICE_TABLE(of, rcar_thermal_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * basic functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #define rcar_thermal_common_read(c, r) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) _rcar_thermal_common_read(c, COMMON_ ##r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static u32 _rcar_thermal_common_read(struct rcar_thermal_common *common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) u32 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return ioread32(common->base + reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define rcar_thermal_common_write(c, r, d) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) _rcar_thermal_common_write(c, COMMON_ ##r, d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void _rcar_thermal_common_write(struct rcar_thermal_common *common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) u32 reg, u32 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) iowrite32(data, common->base + reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #define rcar_thermal_common_bset(c, r, m, d) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) _rcar_thermal_common_bset(c, COMMON_ ##r, m, d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static void _rcar_thermal_common_bset(struct rcar_thermal_common *common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) u32 reg, u32 mask, u32 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) val = ioread32(common->base + reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) val &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) val |= (data & mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) iowrite32(val, common->base + reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #define rcar_thermal_read(p, r) _rcar_thermal_read(p, REG_ ##r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static u32 _rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return ioread32(priv->base + reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #define rcar_thermal_write(p, r, d) _rcar_thermal_write(p, REG_ ##r, d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static void _rcar_thermal_write(struct rcar_thermal_priv *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) u32 reg, u32 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) iowrite32(data, priv->base + reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #define rcar_thermal_bset(p, r, m, d) _rcar_thermal_bset(p, REG_ ##r, m, d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static void _rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) u32 mask, u32 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) val = ioread32(priv->base + reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) val &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) val |= (data & mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) iowrite32(val, priv->base + reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * zone device functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int rcar_thermal_update_temp(struct rcar_thermal_priv *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct device *dev = rcar_priv_to_dev(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int old, new, ctemp = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) mutex_lock(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * TSC decides a value of CPTAP automatically,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * and this is the conditions which validate interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) old = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) for (i = 0; i < 128; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * we need to wait 300us after changing comparator offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * to get stable temperature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * see "Usage Notes" on datasheet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) usleep_range(300, 400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) new = rcar_thermal_read(priv, THSSR) & CTEMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (new == old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ctemp = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) old = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (ctemp < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) dev_err(dev, "thermal sensor was broken\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) goto err_out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * enable IRQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (rcar_has_irq_support(priv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (priv->chip->has_filonoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) rcar_thermal_write(priv, FILONOFF, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* enable Rising/Falling edge interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) rcar_thermal_write(priv, POSNEG, 0x1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) rcar_thermal_write(priv, INTCTRL, (((ctemp - 0) << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ((ctemp - 1) << 0)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) err_out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) mutex_unlock(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return ctemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int rcar_thermal_get_current_temp(struct rcar_thermal_priv *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int ctemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) ctemp = rcar_thermal_update_temp(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (ctemp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return ctemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* Guaranteed operating range is -45C to 125C. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (priv->chip->ctemp_bands == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) *temp = MCELSIUS((ctemp * 5) - 65);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) else if (ctemp < 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *temp = MCELSIUS(((ctemp * 55) - 720) / 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) *temp = MCELSIUS((ctemp * 5) - 60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static int rcar_thermal_of_get_temp(void *data, int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct rcar_thermal_priv *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return rcar_thermal_get_current_temp(priv, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int rcar_thermal_get_temp(struct thermal_zone_device *zone, int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return rcar_thermal_get_current_temp(priv, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) int trip, enum thermal_trip_type *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct device *dev = rcar_priv_to_dev(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* see rcar_thermal_get_temp() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) switch (trip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) case 0: /* +90 <= temp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) *type = THERMAL_TRIP_CRITICAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) dev_err(dev, "rcar driver trip error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) int trip, int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct device *dev = rcar_priv_to_dev(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* see rcar_thermal_get_temp() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) switch (trip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) case 0: /* +90 <= temp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) *temp = MCELSIUS(90);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) dev_err(dev, "rcar driver trip error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int rcar_thermal_notify(struct thermal_zone_device *zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int trip, enum thermal_trip_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct device *dev = rcar_priv_to_dev(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) case THERMAL_TRIP_CRITICAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* FIXME */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) dev_warn(dev, "Thermal reached to critical temperature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static const struct thermal_zone_of_device_ops rcar_thermal_zone_of_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .get_temp = rcar_thermal_of_get_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) .get_temp = rcar_thermal_get_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) .get_trip_type = rcar_thermal_get_trip_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) .get_trip_temp = rcar_thermal_get_trip_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .notify = rcar_thermal_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) #define rcar_thermal_irq_enable(p) _rcar_thermal_irq_ctrl(p, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) #define rcar_thermal_irq_disable(p) _rcar_thermal_irq_ctrl(p, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static void _rcar_thermal_irq_ctrl(struct rcar_thermal_priv *priv, int enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct rcar_thermal_common *common = priv->common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) u32 mask = 0x3 << rcar_id_to_shift(priv); /* enable Rising/Falling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (!rcar_has_irq_support(priv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) spin_lock_irqsave(&common->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) rcar_thermal_common_bset(common, INTMSK, mask, enable ? 0 : mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) spin_unlock_irqrestore(&common->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static void rcar_thermal_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct rcar_thermal_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) priv = container_of(work, struct rcar_thermal_priv, work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) ret = rcar_thermal_update_temp(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) rcar_thermal_irq_enable(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) thermal_zone_device_update(priv->zone, THERMAL_EVENT_UNSPECIFIED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static u32 rcar_thermal_had_changed(struct rcar_thermal_priv *priv, u32 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct device *dev = rcar_priv_to_dev(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) status = (status >> rcar_id_to_shift(priv)) & 0x3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) dev_dbg(dev, "thermal%d %s%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) priv->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) (status & 0x2) ? "Rising " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) (status & 0x1) ? "Falling" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static irqreturn_t rcar_thermal_irq(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct rcar_thermal_common *common = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct rcar_thermal_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) u32 status, mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) spin_lock_irqsave(&common->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) mask = rcar_thermal_common_read(common, INTMSK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) status = rcar_thermal_common_read(common, STR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) rcar_thermal_common_write(common, STR, 0x000F0F0F & mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) spin_unlock_irqrestore(&common->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) status = status & ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * check the status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) rcar_thermal_for_each_priv(priv, common) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (rcar_thermal_had_changed(priv, status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) rcar_thermal_irq_disable(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) queue_delayed_work(system_freezable_wq, &priv->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) msecs_to_jiffies(300));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * platform functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static int rcar_thermal_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) struct rcar_thermal_common *common = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) struct rcar_thermal_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) rcar_thermal_for_each_priv(priv, common) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) rcar_thermal_irq_disable(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) cancel_delayed_work_sync(&priv->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (priv->chip->use_of_thermal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) thermal_remove_hwmon_sysfs(priv->zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) thermal_zone_device_unregister(priv->zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) pm_runtime_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) pm_runtime_disable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static int rcar_thermal_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct rcar_thermal_common *common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) struct rcar_thermal_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) struct resource *res, *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) const struct rcar_thermal_chip *chip = of_device_get_match_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) int mres = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) int ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) int idle = IDLE_INTERVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) u32 enr_bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (!common)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) platform_set_drvdata(pdev, common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) INIT_LIST_HEAD(&common->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) spin_lock_init(&common->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) common->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) pm_runtime_enable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) pm_runtime_get_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) for (i = 0; i < chip->nirqs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) irq = platform_get_resource(pdev, IORESOURCE_IRQ, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (!irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (!common->base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * platform has IRQ support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * Then, driver uses common registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * rcar_has_irq_support() will be enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) res = platform_get_resource(pdev, IORESOURCE_MEM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) mres++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) common->base = devm_ioremap_resource(dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (IS_ERR(common->base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) ret = PTR_ERR(common->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) goto error_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) idle = 0; /* polling delay is not needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) ret = devm_request_irq(dev, irq->start, rcar_thermal_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) IRQF_SHARED, dev_name(dev), common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) dev_err(dev, "irq request failed\n ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) goto error_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /* update ENR bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (chip->irq_per_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) enr_bits |= 1 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) for (i = 0;; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) res = platform_get_resource(pdev, IORESOURCE_MEM, mres++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (!priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) goto error_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) priv->base = devm_ioremap_resource(dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (IS_ERR(priv->base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) ret = PTR_ERR(priv->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) goto error_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) priv->common = common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) priv->id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) priv->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) mutex_init(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) INIT_LIST_HEAD(&priv->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) INIT_DELAYED_WORK(&priv->work, rcar_thermal_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) ret = rcar_thermal_update_temp(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) goto error_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (chip->use_of_thermal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) priv->zone = devm_thermal_zone_of_sensor_register(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) dev, i, priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) &rcar_thermal_zone_of_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) priv->zone = thermal_zone_device_register(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) "rcar_thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 1, 0, priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) &rcar_thermal_zone_ops, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ret = thermal_zone_device_enable(priv->zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) thermal_zone_device_unregister(priv->zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) priv->zone = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (IS_ERR(priv->zone)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) dev_err(dev, "can't register thermal zone\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) ret = PTR_ERR(priv->zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) priv->zone = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) goto error_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (chip->use_of_thermal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * thermal_zone doesn't enable hwmon as default,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * but, enable it here to keep compatible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) priv->zone->tzp->no_hwmon = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) ret = thermal_add_hwmon_sysfs(priv->zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) goto error_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) rcar_thermal_irq_enable(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) list_move_tail(&priv->list, &common->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) /* update ENR bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (!chip->irq_per_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) enr_bits |= 3 << (i * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if (common->base && enr_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) rcar_thermal_common_write(common, ENR, enr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) dev_info(dev, "%d sensor probed\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) error_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) rcar_thermal_remove(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) static int rcar_thermal_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) struct rcar_thermal_common *common = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) struct rcar_thermal_priv *priv = list_first_entry(&common->head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) typeof(*priv), list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (priv->chip->needs_suspend_resume) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) rcar_thermal_common_write(common, ENR, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) rcar_thermal_irq_disable(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) rcar_thermal_bset(priv, THSCR, CPCTL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static int rcar_thermal_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) struct rcar_thermal_common *common = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) struct rcar_thermal_priv *priv = list_first_entry(&common->head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) typeof(*priv), list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (priv->chip->needs_suspend_resume) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) ret = rcar_thermal_update_temp(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) rcar_thermal_irq_enable(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) rcar_thermal_common_write(common, ENR, 0x03);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static SIMPLE_DEV_PM_OPS(rcar_thermal_pm_ops, rcar_thermal_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) rcar_thermal_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) static struct platform_driver rcar_thermal_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) .name = "rcar_thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) .pm = &rcar_thermal_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) .of_match_table = rcar_thermal_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) .probe = rcar_thermal_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) .remove = rcar_thermal_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) module_platform_driver(rcar_thermal_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");