Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / thermal / broadcom / ns-thermal.c
blob8b5b32f749ee89cfc7aacac57e8313e46857fa77
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
4 */
6 #include <linux/module.h>
7 #include <linux/of_address.h>
8 #include <linux/platform_device.h>
9 #include <linux/thermal.h>
11 #define PVTMON_CONTROL0 0x00
12 #define PVTMON_CONTROL0_SEL_MASK 0x0000000e
13 #define PVTMON_CONTROL0_SEL_TEMP_MONITOR 0x00000000
14 #define PVTMON_CONTROL0_SEL_TEST_MODE 0x0000000e
15 #define PVTMON_STATUS 0x08
17 static int ns_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
19 void __iomem *pvtmon = thermal_zone_device_priv(tz);
20 int offset = thermal_zone_get_offset(tz);
21 int slope = thermal_zone_get_slope(tz);
22 u32 val;
24 val = readl(pvtmon + PVTMON_CONTROL0);
25 if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
26 /* Clear current mode selection */
27 val &= ~PVTMON_CONTROL0_SEL_MASK;
29 /* Set temp monitor mode (it's the default actually) */
30 val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
32 writel(val, pvtmon + PVTMON_CONTROL0);
35 val = readl(pvtmon + PVTMON_STATUS);
36 *temp = slope * val + offset;
38 return 0;
41 static const struct thermal_zone_device_ops ns_thermal_ops = {
42 .get_temp = ns_thermal_get_temp,
45 static int ns_thermal_probe(struct platform_device *pdev)
47 struct device *dev = &pdev->dev;
48 struct thermal_zone_device *tz;
49 void __iomem *pvtmon;
51 pvtmon = of_iomap(dev_of_node(dev), 0);
52 if (WARN_ON(!pvtmon))
53 return -ENOENT;
55 tz = devm_thermal_of_zone_register(dev, 0,
56 pvtmon,
57 &ns_thermal_ops);
58 if (IS_ERR(tz)) {
59 iounmap(pvtmon);
60 return PTR_ERR(tz);
63 platform_set_drvdata(pdev, pvtmon);
65 return 0;
68 static void ns_thermal_remove(struct platform_device *pdev)
70 void __iomem *pvtmon = platform_get_drvdata(pdev);
72 iounmap(pvtmon);
75 static const struct of_device_id ns_thermal_of_match[] = {
76 { .compatible = "brcm,ns-thermal", },
77 {},
79 MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
81 static struct platform_driver ns_thermal_driver = {
82 .probe = ns_thermal_probe,
83 .remove = ns_thermal_remove,
84 .driver = {
85 .name = "ns-thermal",
86 .of_match_table = ns_thermal_of_match,
89 module_platform_driver(ns_thermal_driver);
91 MODULE_AUTHOR("Rafał Miłecki <rafal@milecki.pl>");
92 MODULE_DESCRIPTION("Northstar thermal driver");
93 MODULE_LICENSE("GPL v2");