xref: /linux/drivers/thermal/broadcom/ns-thermal.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
4  */
5 
6 #include <linux/module.h>
7 #include <linux/of_address.h>
8 #include <linux/platform_device.h>
9 #include <linux/thermal.h>
10 
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
16 
17 static int ns_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
18 {
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;
23 
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;
28 
29 		/* Set temp monitor mode (it's the default actually) */
30 		val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
31 
32 		writel(val, pvtmon + PVTMON_CONTROL0);
33 	}
34 
35 	val = readl(pvtmon + PVTMON_STATUS);
36 	*temp = slope * val + offset;
37 
38 	return 0;
39 }
40 
41 static const struct thermal_zone_device_ops ns_thermal_ops = {
42 	.get_temp = ns_thermal_get_temp,
43 };
44 
45 static int ns_thermal_probe(struct platform_device *pdev)
46 {
47 	struct device *dev = &pdev->dev;
48 	struct thermal_zone_device *tz;
49 	void __iomem *pvtmon;
50 
51 	pvtmon = of_iomap(dev_of_node(dev), 0);
52 	if (WARN_ON(!pvtmon))
53 		return -ENOENT;
54 
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);
61 	}
62 
63 	platform_set_drvdata(pdev, pvtmon);
64 
65 	return 0;
66 }
67 
68 static void ns_thermal_remove(struct platform_device *pdev)
69 {
70 	void __iomem *pvtmon = platform_get_drvdata(pdev);
71 
72 	iounmap(pvtmon);
73 }
74 
75 static const struct of_device_id ns_thermal_of_match[] = {
76 	{ .compatible = "brcm,ns-thermal", },
77 	{},
78 };
79 MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
80 
81 static struct platform_driver ns_thermal_driver = {
82 	.probe		= ns_thermal_probe,
83 	.remove_new	= ns_thermal_remove,
84 	.driver = {
85 		.name = "ns-thermal",
86 		.of_match_table = ns_thermal_of_match,
87 	},
88 };
89 module_platform_driver(ns_thermal_driver);
90 
91 MODULE_AUTHOR("Rafał Miłecki <rafal@milecki.pl>");
92 MODULE_DESCRIPTION("Northstar thermal driver");
93 MODULE_LICENSE("GPL v2");
94