blob: 189b675cf14d9233cbd688c0e46108b1ac06a53e [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +00002/*
3 * Kirkwood thermal sensor driver
4 *
5 * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +00006 */
7#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/io.h>
10#include <linux/kernel.h>
11#include <linux/of.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/thermal.h>
15
16#define KIRKWOOD_THERMAL_VALID_OFFSET 9
17#define KIRKWOOD_THERMAL_VALID_MASK 0x1
18#define KIRKWOOD_THERMAL_TEMP_OFFSET 10
19#define KIRKWOOD_THERMAL_TEMP_MASK 0x1FF
20
21/* Kirkwood Thermal Sensor Dev Structure */
22struct kirkwood_thermal_priv {
23 void __iomem *sensor;
24};
25
26static int kirkwood_get_temp(struct thermal_zone_device *thermal,
Sascha Hauer17e83512015-07-24 08:12:54 +020027 int *temp)
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +000028{
29 unsigned long reg;
30 struct kirkwood_thermal_priv *priv = thermal->devdata;
31
32 reg = readl_relaxed(priv->sensor);
33
34 /* Valid check */
Ezequiel Garcia02519d32013-03-21 17:42:07 -030035 if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) &
36 KIRKWOOD_THERMAL_VALID_MASK)) {
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +000037 dev_err(&thermal->device,
38 "Temperature sensor reading not valid\n");
39 return -EIO;
40 }
41
42 /*
Ezequiel Garcia696b6072013-03-22 09:23:02 -030043 * Calculate temperature. According to Marvell internal
44 * documentation the formula for this is:
45 * Celsius = (322-reg)/1.3625
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +000046 */
47 reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) &
48 KIRKWOOD_THERMAL_TEMP_MASK;
Ezequiel Garcia696b6072013-03-22 09:23:02 -030049 *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +000050
51 return 0;
52}
53
54static struct thermal_zone_device_ops ops = {
55 .get_temp = kirkwood_get_temp,
56};
57
58static const struct of_device_id kirkwood_thermal_id_table[] = {
59 { .compatible = "marvell,kirkwood-thermal" },
60 {}
61};
62
63static int kirkwood_thermal_probe(struct platform_device *pdev)
64{
65 struct thermal_zone_device *thermal = NULL;
66 struct kirkwood_thermal_priv *priv;
67 struct resource *res;
68
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +000069 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
70 if (!priv)
71 return -ENOMEM;
72
Wolfram Sangc28f6922013-05-10 08:17:11 +000073 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sachin Kamataa3b5d22013-03-04 06:45:34 +000074 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
75 if (IS_ERR(priv->sensor))
76 return PTR_ERR(priv->sensor);
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +000077
78 thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0,
79 priv, &ops, NULL, 0, 0);
80 if (IS_ERR(thermal)) {
81 dev_err(&pdev->dev,
82 "Failed to register thermal zone device\n");
83 return PTR_ERR(thermal);
84 }
85
86 platform_set_drvdata(pdev, thermal);
87
88 return 0;
89}
90
91static int kirkwood_thermal_exit(struct platform_device *pdev)
92{
93 struct thermal_zone_device *kirkwood_thermal =
94 platform_get_drvdata(pdev);
95
96 thermal_zone_device_unregister(kirkwood_thermal);
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +000097
98 return 0;
99}
100
101MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table);
102
103static struct platform_driver kirkwood_thermal_driver = {
104 .probe = kirkwood_thermal_probe,
105 .remove = kirkwood_thermal_exit,
106 .driver = {
107 .name = "kirkwood_thermal",
Sachin Kamat90c31942013-05-16 10:28:10 +0000108 .of_match_table = kirkwood_thermal_id_table,
Nobuhiro Iwamatsu7060aa32013-02-06 06:35:24 +0000109 },
110};
111
112module_platform_driver(kirkwood_thermal_driver);
113
114MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
115MODULE_DESCRIPTION("kirkwood thermal driver");
116MODULE_LICENSE("GPL");