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