Linux 4.19.133
[linux/fpc-iii.git] / drivers / thermal / broadcom / ns-thermal.c
blob322e741a2463c7c70ab0026931262abb77771ecb
1 /*
2 * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/module.h>
10 #include <linux/of_address.h>
11 #include <linux/platform_device.h>
12 #include <linux/thermal.h>
14 #define PVTMON_CONTROL0 0x00
15 #define PVTMON_CONTROL0_SEL_MASK 0x0000000e
16 #define PVTMON_CONTROL0_SEL_TEMP_MONITOR 0x00000000
17 #define PVTMON_CONTROL0_SEL_TEST_MODE 0x0000000e
18 #define PVTMON_STATUS 0x08
20 struct ns_thermal {
21 struct thermal_zone_device *tz;
22 void __iomem *pvtmon;
25 static int ns_thermal_get_temp(void *data, int *temp)
27 struct ns_thermal *ns_thermal = data;
28 int offset = thermal_zone_get_offset(ns_thermal->tz);
29 int slope = thermal_zone_get_slope(ns_thermal->tz);
30 u32 val;
32 val = readl(ns_thermal->pvtmon + PVTMON_CONTROL0);
33 if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
34 /* Clear current mode selection */
35 val &= ~PVTMON_CONTROL0_SEL_MASK;
37 /* Set temp monitor mode (it's the default actually) */
38 val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
40 writel(val, ns_thermal->pvtmon + PVTMON_CONTROL0);
43 val = readl(ns_thermal->pvtmon + PVTMON_STATUS);
44 *temp = slope * val + offset;
46 return 0;
49 static const struct thermal_zone_of_device_ops ns_thermal_ops = {
50 .get_temp = ns_thermal_get_temp,
53 static int ns_thermal_probe(struct platform_device *pdev)
55 struct device *dev = &pdev->dev;
56 struct ns_thermal *ns_thermal;
58 ns_thermal = devm_kzalloc(dev, sizeof(*ns_thermal), GFP_KERNEL);
59 if (!ns_thermal)
60 return -ENOMEM;
62 ns_thermal->pvtmon = of_iomap(dev_of_node(dev), 0);
63 if (WARN_ON(!ns_thermal->pvtmon))
64 return -ENOENT;
66 ns_thermal->tz = devm_thermal_zone_of_sensor_register(dev, 0,
67 ns_thermal,
68 &ns_thermal_ops);
69 if (IS_ERR(ns_thermal->tz)) {
70 iounmap(ns_thermal->pvtmon);
71 return PTR_ERR(ns_thermal->tz);
74 platform_set_drvdata(pdev, ns_thermal);
76 return 0;
79 static int ns_thermal_remove(struct platform_device *pdev)
81 struct ns_thermal *ns_thermal = platform_get_drvdata(pdev);
83 iounmap(ns_thermal->pvtmon);
85 return 0;
88 static const struct of_device_id ns_thermal_of_match[] = {
89 { .compatible = "brcm,ns-thermal", },
90 {},
92 MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
94 static struct platform_driver ns_thermal_driver = {
95 .probe = ns_thermal_probe,
96 .remove = ns_thermal_remove,
97 .driver = {
98 .name = "ns-thermal",
99 .of_match_table = ns_thermal_of_match,
102 module_platform_driver(ns_thermal_driver);
104 MODULE_AUTHOR("Rafał Miłecki <rafal@milecki.pl>");
105 MODULE_DESCRIPTION("Northstar thermal driver");
106 MODULE_LICENSE("GPL v2");