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