xref: /linux/drivers/thermal/broadcom/ns-thermal.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
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 struct ns_thermal {
18 	struct thermal_zone_device *tz;
19 	void __iomem *pvtmon;
20 };
21 
22 static int ns_thermal_get_temp(void *data, int *temp)
23 {
24 	struct ns_thermal *ns_thermal = data;
25 	int offset = thermal_zone_get_offset(ns_thermal->tz);
26 	int slope = thermal_zone_get_slope(ns_thermal->tz);
27 	u32 val;
28 
29 	val = readl(ns_thermal->pvtmon + PVTMON_CONTROL0);
30 	if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
31 		/* Clear current mode selection */
32 		val &= ~PVTMON_CONTROL0_SEL_MASK;
33 
34 		/* Set temp monitor mode (it's the default actually) */
35 		val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
36 
37 		writel(val, ns_thermal->pvtmon + PVTMON_CONTROL0);
38 	}
39 
40 	val = readl(ns_thermal->pvtmon + PVTMON_STATUS);
41 	*temp = slope * val + offset;
42 
43 	return 0;
44 }
45 
46 static const struct thermal_zone_of_device_ops ns_thermal_ops = {
47 	.get_temp = ns_thermal_get_temp,
48 };
49 
50 static int ns_thermal_probe(struct platform_device *pdev)
51 {
52 	struct device *dev = &pdev->dev;
53 	struct ns_thermal *ns_thermal;
54 
55 	ns_thermal = devm_kzalloc(dev, sizeof(*ns_thermal), GFP_KERNEL);
56 	if (!ns_thermal)
57 		return -ENOMEM;
58 
59 	ns_thermal->pvtmon = of_iomap(dev_of_node(dev), 0);
60 	if (WARN_ON(!ns_thermal->pvtmon))
61 		return -ENOENT;
62 
63 	ns_thermal->tz = devm_thermal_zone_of_sensor_register(dev, 0,
64 							      ns_thermal,
65 							      &ns_thermal_ops);
66 	if (IS_ERR(ns_thermal->tz)) {
67 		iounmap(ns_thermal->pvtmon);
68 		return PTR_ERR(ns_thermal->tz);
69 	}
70 
71 	platform_set_drvdata(pdev, ns_thermal);
72 
73 	return 0;
74 }
75 
76 static int ns_thermal_remove(struct platform_device *pdev)
77 {
78 	struct ns_thermal *ns_thermal = platform_get_drvdata(pdev);
79 
80 	iounmap(ns_thermal->pvtmon);
81 
82 	return 0;
83 }
84 
85 static const struct of_device_id ns_thermal_of_match[] = {
86 	{ .compatible = "brcm,ns-thermal", },
87 	{},
88 };
89 MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
90 
91 static struct platform_driver ns_thermal_driver = {
92 	.probe		= ns_thermal_probe,
93 	.remove		= ns_thermal_remove,
94 	.driver = {
95 		.name = "ns-thermal",
96 		.of_match_table = ns_thermal_of_match,
97 	},
98 };
99 module_platform_driver(ns_thermal_driver);
100 
101 MODULE_AUTHOR("Rafał Miłecki <rafal@milecki.pl>");
102 MODULE_DESCRIPTION("Northstar thermal driver");
103 MODULE_LICENSE("GPL v2");
104