^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Dove 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) 2013 Andrew Lunn <andrew@lunn.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define DOVE_THERMAL_TEMP_OFFSET 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DOVE_THERMAL_TEMP_MASK 0x1FF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* Dove Thermal Manager Control and Status Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define PMU_TM_DISABLE_OFFS 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Dove Theraml Diode Control 0 Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define PMU_TDC0_SEL_VCAL_OFFS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define PMU_TDC0_SEL_VCAL_MASK (0x3 << PMU_TDC0_SEL_VCAL_OFFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define PMU_TDC0_REF_CAL_CNT_OFFS 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define PMU_TDC0_REF_CAL_CNT_MASK (0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define PMU_TDC0_AVG_NUM_OFFS 25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define PMU_TDC0_AVG_NUM_MASK (0x7 << PMU_TDC0_AVG_NUM_OFFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* Dove Thermal Diode Control 1 Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define PMU_TEMP_DIOD_CTRL1_REG 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define PMU_TDC1_TEMP_VALID_MASK (0x1 << 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* Dove Thermal Sensor Dev Structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct dove_thermal_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) void __iomem *sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void __iomem *control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int dove_init_sensor(const struct dove_thermal_priv *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Configure the Diode Control Register #0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) reg = readl_relaxed(priv->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Use average of 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) reg &= ~PMU_TDC0_AVG_NUM_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Reference calibration value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Set the high level reference for calibration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) reg &= ~PMU_TDC0_SEL_VCAL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) writel(reg, priv->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* Reset the sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) reg = readl_relaxed(priv->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) writel(reg, priv->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* Enable the sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) reg = readl_relaxed(priv->sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) reg &= ~PMU_TM_DISABLE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) writel(reg, priv->sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* Poll the sensor for the first reading */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) for (i = 0; i < 1000000; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) reg = readl_relaxed(priv->sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (reg & DOVE_THERMAL_TEMP_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (i == 1000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int dove_get_temp(struct thermal_zone_device *thermal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned long reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct dove_thermal_priv *priv = thermal->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* Valid check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) dev_err(&thermal->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) "Temperature sensor reading not valid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -EIO;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Calculate temperature. According to Marvell internal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * documentation the formula for this is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * Celsius = (322-reg)/1.3625
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) reg = readl_relaxed(priv->sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static struct thermal_zone_device_ops ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .get_temp = dove_get_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static const struct of_device_id dove_thermal_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) { .compatible = "marvell,dove-thermal" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int dove_thermal_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct thermal_zone_device *thermal = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct dove_thermal_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) priv->sensor = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (IS_ERR(priv->sensor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return PTR_ERR(priv->sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) priv->control = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (IS_ERR(priv->control))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return PTR_ERR(priv->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret = dove_init_sensor(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dev_err(&pdev->dev, "Failed to initialize sensor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) thermal = thermal_zone_device_register("dove_thermal", 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) priv, &ops, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (IS_ERR(thermal)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) "Failed to register thermal zone device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return PTR_ERR(thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ret = thermal_zone_device_enable(thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) thermal_zone_device_unregister(thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) platform_set_drvdata(pdev, thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int dove_thermal_exit(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct thermal_zone_device *dove_thermal =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) thermal_zone_device_unregister(dove_thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static struct platform_driver dove_thermal_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .probe = dove_thermal_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .remove = dove_thermal_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .name = "dove_thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .of_match_table = dove_thermal_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) module_platform_driver(dove_thermal_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) MODULE_DESCRIPTION("Dove thermal driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) MODULE_LICENSE("GPL");