0001
0002
0003
0004
0005
0006 #include <linux/module.h>
0007 #include <linux/of_address.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/thermal.h>
0010
0011 #define PVTMON_CONTROL0 0x00
0012 #define PVTMON_CONTROL0_SEL_MASK 0x0000000e
0013 #define PVTMON_CONTROL0_SEL_TEMP_MONITOR 0x00000000
0014 #define PVTMON_CONTROL0_SEL_TEST_MODE 0x0000000e
0015 #define PVTMON_STATUS 0x08
0016
0017 struct ns_thermal {
0018 struct thermal_zone_device *tz;
0019 void __iomem *pvtmon;
0020 };
0021
0022 static int ns_thermal_get_temp(void *data, int *temp)
0023 {
0024 struct ns_thermal *ns_thermal = data;
0025 int offset = thermal_zone_get_offset(ns_thermal->tz);
0026 int slope = thermal_zone_get_slope(ns_thermal->tz);
0027 u32 val;
0028
0029 val = readl(ns_thermal->pvtmon + PVTMON_CONTROL0);
0030 if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
0031
0032 val &= ~PVTMON_CONTROL0_SEL_MASK;
0033
0034
0035 val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
0036
0037 writel(val, ns_thermal->pvtmon + PVTMON_CONTROL0);
0038 }
0039
0040 val = readl(ns_thermal->pvtmon + PVTMON_STATUS);
0041 *temp = slope * val + offset;
0042
0043 return 0;
0044 }
0045
0046 static const struct thermal_zone_of_device_ops ns_thermal_ops = {
0047 .get_temp = ns_thermal_get_temp,
0048 };
0049
0050 static int ns_thermal_probe(struct platform_device *pdev)
0051 {
0052 struct device *dev = &pdev->dev;
0053 struct ns_thermal *ns_thermal;
0054
0055 ns_thermal = devm_kzalloc(dev, sizeof(*ns_thermal), GFP_KERNEL);
0056 if (!ns_thermal)
0057 return -ENOMEM;
0058
0059 ns_thermal->pvtmon = of_iomap(dev_of_node(dev), 0);
0060 if (WARN_ON(!ns_thermal->pvtmon))
0061 return -ENOENT;
0062
0063 ns_thermal->tz = devm_thermal_zone_of_sensor_register(dev, 0,
0064 ns_thermal,
0065 &ns_thermal_ops);
0066 if (IS_ERR(ns_thermal->tz)) {
0067 iounmap(ns_thermal->pvtmon);
0068 return PTR_ERR(ns_thermal->tz);
0069 }
0070
0071 platform_set_drvdata(pdev, ns_thermal);
0072
0073 return 0;
0074 }
0075
0076 static int ns_thermal_remove(struct platform_device *pdev)
0077 {
0078 struct ns_thermal *ns_thermal = platform_get_drvdata(pdev);
0079
0080 iounmap(ns_thermal->pvtmon);
0081
0082 return 0;
0083 }
0084
0085 static const struct of_device_id ns_thermal_of_match[] = {
0086 { .compatible = "brcm,ns-thermal", },
0087 {},
0088 };
0089 MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
0090
0091 static struct platform_driver ns_thermal_driver = {
0092 .probe = ns_thermal_probe,
0093 .remove = ns_thermal_remove,
0094 .driver = {
0095 .name = "ns-thermal",
0096 .of_match_table = ns_thermal_of_match,
0097 },
0098 };
0099 module_platform_driver(ns_thermal_driver);
0100
0101 MODULE_AUTHOR("Rafał Miłecki <rafal@milecki.pl>");
0102 MODULE_DESCRIPTION("Northstar thermal driver");
0103 MODULE_LICENSE("GPL v2");