Loading...
1/*
2 * Kirkwood thermal sensor driver
3 *
4 * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16#include <linux/device.h>
17#include <linux/err.h>
18#include <linux/io.h>
19#include <linux/kernel.h>
20#include <linux/of.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/thermal.h>
24
25#define KIRKWOOD_THERMAL_VALID_OFFSET 9
26#define KIRKWOOD_THERMAL_VALID_MASK 0x1
27#define KIRKWOOD_THERMAL_TEMP_OFFSET 10
28#define KIRKWOOD_THERMAL_TEMP_MASK 0x1FF
29
30/* Kirkwood Thermal Sensor Dev Structure */
31struct kirkwood_thermal_priv {
32 void __iomem *sensor;
33};
34
35static int kirkwood_get_temp(struct thermal_zone_device *thermal,
36 unsigned long *temp)
37{
38 unsigned long reg;
39 struct kirkwood_thermal_priv *priv = thermal->devdata;
40
41 reg = readl_relaxed(priv->sensor);
42
43 /* Valid check */
44 if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) &
45 KIRKWOOD_THERMAL_VALID_MASK)) {
46 dev_err(&thermal->device,
47 "Temperature sensor reading not valid\n");
48 return -EIO;
49 }
50
51 /*
52 * Calculate temperature. According to Marvell internal
53 * documentation the formula for this is:
54 * Celsius = (322-reg)/1.3625
55 */
56 reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) &
57 KIRKWOOD_THERMAL_TEMP_MASK;
58 *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
59
60 return 0;
61}
62
63static struct thermal_zone_device_ops ops = {
64 .get_temp = kirkwood_get_temp,
65};
66
67static const struct of_device_id kirkwood_thermal_id_table[] = {
68 { .compatible = "marvell,kirkwood-thermal" },
69 {}
70};
71
72static int kirkwood_thermal_probe(struct platform_device *pdev)
73{
74 struct thermal_zone_device *thermal = NULL;
75 struct kirkwood_thermal_priv *priv;
76 struct resource *res;
77
78 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
79 if (!priv)
80 return -ENOMEM;
81
82 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
83 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
84 if (IS_ERR(priv->sensor))
85 return PTR_ERR(priv->sensor);
86
87 thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0,
88 priv, &ops, NULL, 0, 0);
89 if (IS_ERR(thermal)) {
90 dev_err(&pdev->dev,
91 "Failed to register thermal zone device\n");
92 return PTR_ERR(thermal);
93 }
94
95 platform_set_drvdata(pdev, thermal);
96
97 return 0;
98}
99
100static int kirkwood_thermal_exit(struct platform_device *pdev)
101{
102 struct thermal_zone_device *kirkwood_thermal =
103 platform_get_drvdata(pdev);
104
105 thermal_zone_device_unregister(kirkwood_thermal);
106
107 return 0;
108}
109
110MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table);
111
112static struct platform_driver kirkwood_thermal_driver = {
113 .probe = kirkwood_thermal_probe,
114 .remove = kirkwood_thermal_exit,
115 .driver = {
116 .name = "kirkwood_thermal",
117 .owner = THIS_MODULE,
118 .of_match_table = kirkwood_thermal_id_table,
119 },
120};
121
122module_platform_driver(kirkwood_thermal_driver);
123
124MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
125MODULE_DESCRIPTION("kirkwood thermal driver");
126MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Kirkwood thermal sensor driver
4 *
5 * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
6 */
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,
27 int *temp)
28{
29 unsigned long reg;
30 struct kirkwood_thermal_priv *priv = thermal_zone_device_priv(thermal);
31
32 reg = readl_relaxed(priv->sensor);
33
34 /* Valid check */
35 if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) &
36 KIRKWOOD_THERMAL_VALID_MASK))
37 return -EIO;
38
39 /*
40 * Calculate temperature. According to Marvell internal
41 * documentation the formula for this is:
42 * Celsius = (322-reg)/1.3625
43 */
44 reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) &
45 KIRKWOOD_THERMAL_TEMP_MASK;
46 *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
47
48 return 0;
49}
50
51static struct thermal_zone_device_ops ops = {
52 .get_temp = kirkwood_get_temp,
53};
54
55static const struct of_device_id kirkwood_thermal_id_table[] = {
56 { .compatible = "marvell,kirkwood-thermal" },
57 {}
58};
59
60static int kirkwood_thermal_probe(struct platform_device *pdev)
61{
62 struct thermal_zone_device *thermal = NULL;
63 struct kirkwood_thermal_priv *priv;
64 int ret;
65
66 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
67 if (!priv)
68 return -ENOMEM;
69
70 priv->sensor = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
71 if (IS_ERR(priv->sensor))
72 return PTR_ERR(priv->sensor);
73
74 thermal = thermal_tripless_zone_device_register("kirkwood_thermal",
75 priv, &ops, NULL);
76 if (IS_ERR(thermal)) {
77 dev_err(&pdev->dev,
78 "Failed to register thermal zone device\n");
79 return PTR_ERR(thermal);
80 }
81 ret = thermal_zone_device_enable(thermal);
82 if (ret) {
83 thermal_zone_device_unregister(thermal);
84 dev_err(&pdev->dev, "Failed to enable thermal zone device\n");
85 return ret;
86 }
87
88 platform_set_drvdata(pdev, thermal);
89
90 return 0;
91}
92
93static void kirkwood_thermal_exit(struct platform_device *pdev)
94{
95 struct thermal_zone_device *kirkwood_thermal =
96 platform_get_drvdata(pdev);
97
98 thermal_zone_device_unregister(kirkwood_thermal);
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",
108 .of_match_table = kirkwood_thermal_id_table,
109 },
110};
111
112module_platform_driver(kirkwood_thermal_driver);
113
114MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
115MODULE_DESCRIPTION("kirkwood thermal driver");
116MODULE_LICENSE("GPL");