1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Sparx5 SoC temperature sensor driver
4 * Copyright (C) 2020 Lars Povlsen <lars.povlsen@microchip.com>
7 #include <linux/bitfield.h>
9 #include <linux/hwmon.h>
10 #include <linux/init.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
18 #define TEMP_CFG_CYCLES GENMASK(24, 15)
19 #define TEMP_CFG_ENA BIT(0)
21 #define TEMP_STAT_VALID BIT(12)
22 #define TEMP_STAT_TEMP GENMASK(11, 0)
29 static void s5_temp_clk_disable(void *data)
31 struct clk *clk = data;
33 clk_disable_unprepare(clk);
36 static void s5_temp_enable(struct s5_hwmon *hwmon)
38 u32 val = readl(hwmon->base + TEMP_CFG);
39 u32 clk = clk_get_rate(hwmon->clk) / USEC_PER_SEC;
41 val &= ~TEMP_CFG_CYCLES;
42 val |= FIELD_PREP(TEMP_CFG_CYCLES, clk);
45 writel(val, hwmon->base + TEMP_CFG);
48 static int s5_read(struct device *dev, enum hwmon_sensor_types type,
49 u32 attr, int channel, long *temp)
51 struct s5_hwmon *hwmon = dev_get_drvdata(dev);
56 case hwmon_temp_input:
57 stat = readl_relaxed(hwmon->base + TEMP_STAT);
58 if (!(stat & TEMP_STAT_VALID))
60 value = stat & TEMP_STAT_TEMP;
62 * From register documentation:
63 * Temp(C) = TEMP_SENSOR_STAT.TEMP / 4096 * 352.2 - 109.4
65 value = DIV_ROUND_CLOSEST(value * 3522, 4096) - 1094;
67 * Scale down by 10 from above and multiply by 1000 to
68 * have millidegrees as specified by the hwmon sysfs
82 static umode_t s5_is_visible(const void *_data, enum hwmon_sensor_types type,
83 u32 attr, int channel)
85 if (type != hwmon_temp)
89 case hwmon_temp_input:
96 static const struct hwmon_channel_info *s5_info[] = {
97 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
98 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
102 static const struct hwmon_ops s5_hwmon_ops = {
103 .is_visible = s5_is_visible,
107 static const struct hwmon_chip_info s5_chip_info = {
108 .ops = &s5_hwmon_ops,
112 static int s5_temp_probe(struct platform_device *pdev)
114 struct device *hwmon_dev;
115 struct s5_hwmon *hwmon;
118 hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
122 hwmon->base = devm_platform_ioremap_resource(pdev, 0);
123 if (IS_ERR(hwmon->base))
124 return PTR_ERR(hwmon->base);
126 hwmon->clk = devm_clk_get(&pdev->dev, NULL);
127 if (IS_ERR(hwmon->clk))
128 return PTR_ERR(hwmon->clk);
130 ret = clk_prepare_enable(hwmon->clk);
134 ret = devm_add_action_or_reset(&pdev->dev, s5_temp_clk_disable,
139 s5_temp_enable(hwmon);
141 hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
147 return PTR_ERR_OR_ZERO(hwmon_dev);
150 static const struct of_device_id s5_temp_match[] = {
151 { .compatible = "microchip,sparx5-temp" },
154 MODULE_DEVICE_TABLE(of, s5_temp_match);
156 static struct platform_driver s5_temp_driver = {
157 .probe = s5_temp_probe,
159 .name = "sparx5-temp",
160 .of_match_table = s5_temp_match,
164 module_platform_driver(s5_temp_driver);
166 MODULE_AUTHOR("Lars Povlsen <lars.povlsen@microchip.com>");
167 MODULE_DESCRIPTION("Sparx5 SoC temperature sensor driver");
168 MODULE_LICENSE("GPL");