1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
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
18 struct thermal_zone_device
*tz
;
22 static int ns_thermal_get_temp(void *data
, int *temp
)
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
);
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
;
34 /* Set temp monitor mode (it's the default actually) */
35 val
|= PVTMON_CONTROL0_SEL_TEMP_MONITOR
;
37 writel(val
, ns_thermal
->pvtmon
+ PVTMON_CONTROL0
);
40 val
= readl(ns_thermal
->pvtmon
+ PVTMON_STATUS
);
41 *temp
= slope
* val
+ offset
;
46 static const struct thermal_zone_of_device_ops ns_thermal_ops
= {
47 .get_temp
= ns_thermal_get_temp
,
50 static int ns_thermal_probe(struct platform_device
*pdev
)
52 struct device
*dev
= &pdev
->dev
;
53 struct ns_thermal
*ns_thermal
;
55 ns_thermal
= devm_kzalloc(dev
, sizeof(*ns_thermal
), GFP_KERNEL
);
59 ns_thermal
->pvtmon
= of_iomap(dev_of_node(dev
), 0);
60 if (WARN_ON(!ns_thermal
->pvtmon
))
63 ns_thermal
->tz
= devm_thermal_zone_of_sensor_register(dev
, 0,
66 if (IS_ERR(ns_thermal
->tz
)) {
67 iounmap(ns_thermal
->pvtmon
);
68 return PTR_ERR(ns_thermal
->tz
);
71 platform_set_drvdata(pdev
, ns_thermal
);
76 static int ns_thermal_remove(struct platform_device
*pdev
)
78 struct ns_thermal
*ns_thermal
= platform_get_drvdata(pdev
);
80 iounmap(ns_thermal
->pvtmon
);
85 static const struct of_device_id ns_thermal_of_match
[] = {
86 { .compatible
= "brcm,ns-thermal", },
89 MODULE_DEVICE_TABLE(of
, ns_thermal_of_match
);
91 static struct platform_driver ns_thermal_driver
= {
92 .probe
= ns_thermal_probe
,
93 .remove
= ns_thermal_remove
,
96 .of_match_table
= ns_thermal_of_match
,
99 module_platform_driver(ns_thermal_driver
);
101 MODULE_AUTHOR("Rafał Miłecki <rafal@milecki.pl>");
102 MODULE_DESCRIPTION("Northstar thermal driver");
103 MODULE_LICENSE("GPL v2");