^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) * Kirkwood 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 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
^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 KIRKWOOD_THERMAL_VALID_OFFSET 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define KIRKWOOD_THERMAL_VALID_MASK 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define KIRKWOOD_THERMAL_TEMP_OFFSET 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define KIRKWOOD_THERMAL_TEMP_MASK 0x1FF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Kirkwood Thermal Sensor Dev Structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct kirkwood_thermal_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) void __iomem *sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int kirkwood_get_temp(struct thermal_zone_device *thermal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct kirkwood_thermal_priv *priv = thermal->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) reg = readl_relaxed(priv->sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Valid check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) KIRKWOOD_THERMAL_VALID_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) dev_err(&thermal->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) "Temperature sensor reading not valid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return -EIO;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Calculate temperature. According to Marvell internal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * documentation the formula for this is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Celsius = (322-reg)/1.3625
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) KIRKWOOD_THERMAL_TEMP_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static struct thermal_zone_device_ops ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .get_temp = kirkwood_get_temp,
^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 of_device_id kirkwood_thermal_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) { .compatible = "marvell,kirkwood-thermal" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int kirkwood_thermal_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct thermal_zone_device *thermal = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct kirkwood_thermal_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) priv->sensor = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (IS_ERR(priv->sensor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return PTR_ERR(priv->sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) priv, &ops, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (IS_ERR(thermal)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) "Failed to register thermal zone device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return PTR_ERR(thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret = thermal_zone_device_enable(thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) thermal_zone_device_unregister(thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dev_err(&pdev->dev, "Failed to enable thermal zone device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) platform_set_drvdata(pdev, thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int kirkwood_thermal_exit(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct thermal_zone_device *kirkwood_thermal =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) thermal_zone_device_unregister(kirkwood_thermal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static struct platform_driver kirkwood_thermal_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .probe = kirkwood_thermal_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .remove = kirkwood_thermal_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .name = "kirkwood_thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .of_match_table = kirkwood_thermal_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) },
^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) module_platform_driver(kirkwood_thermal_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) MODULE_DESCRIPTION("kirkwood thermal driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) MODULE_LICENSE("GPL");