Commit | Line | Data |
---|---|---|
9c92ab61 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
7060aa36 NI |
2 | /* |
3 | * Kirkwood thermal sensor driver | |
4 | * | |
5 | * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org> | |
7060aa36 NI |
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 */ | |
22 | struct kirkwood_thermal_priv { | |
23 | void __iomem *sensor; | |
24 | }; | |
25 | ||
26 | static int kirkwood_get_temp(struct thermal_zone_device *thermal, | |
17e8351a | 27 | int *temp) |
7060aa36 NI |
28 | { |
29 | unsigned long reg; | |
5f68d078 | 30 | struct kirkwood_thermal_priv *priv = thermal_zone_device_priv(thermal); |
7060aa36 NI |
31 | |
32 | reg = readl_relaxed(priv->sensor); | |
33 | ||
34 | /* Valid check */ | |
02519d33 | 35 | if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) & |
abda7383 | 36 | KIRKWOOD_THERMAL_VALID_MASK)) |
7060aa36 | 37 | return -EIO; |
7060aa36 NI |
38 | |
39 | /* | |
696b6075 EG |
40 | * Calculate temperature. According to Marvell internal |
41 | * documentation the formula for this is: | |
42 | * Celsius = (322-reg)/1.3625 | |
7060aa36 NI |
43 | */ |
44 | reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) & | |
45 | KIRKWOOD_THERMAL_TEMP_MASK; | |
696b6075 | 46 | *temp = ((3220000000UL - (10000000UL * reg)) / 13625); |
7060aa36 NI |
47 | |
48 | return 0; | |
49 | } | |
50 | ||
51 | static struct thermal_zone_device_ops ops = { | |
52 | .get_temp = kirkwood_get_temp, | |
53 | }; | |
54 | ||
55 | static const struct of_device_id kirkwood_thermal_id_table[] = { | |
56 | { .compatible = "marvell,kirkwood-thermal" }, | |
57 | {} | |
58 | }; | |
59 | ||
60 | static int kirkwood_thermal_probe(struct platform_device *pdev) | |
61 | { | |
62 | struct thermal_zone_device *thermal = NULL; | |
63 | struct kirkwood_thermal_priv *priv; | |
bbcf90c0 | 64 | int ret; |
7060aa36 | 65 | |
7060aa36 NI |
66 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
67 | if (!priv) | |
68 | return -ENOMEM; | |
69 | ||
821e4309 | 70 | priv->sensor = devm_platform_get_and_ioremap_resource(pdev, 0, NULL); |
aa3b5d22 SK |
71 | if (IS_ERR(priv->sensor)) |
72 | return PTR_ERR(priv->sensor); | |
7060aa36 NI |
73 | |
74 | thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0, | |
75 | priv, &ops, NULL, 0, 0); | |
76 | if (IS_ERR(thermal)) { | |
77 | dev_err(&pdev->dev, | |
78 | "Failed to register thermal zone device\n"); | |
79 | return PTR_ERR(thermal); | |
80 | } | |
bbcf90c0 AP |
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 | } | |
7060aa36 NI |
87 | |
88 | platform_set_drvdata(pdev, thermal); | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | static int 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); | |
7060aa36 NI |
99 | |
100 | return 0; | |
101 | } | |
102 | ||
103 | MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table); | |
104 | ||
105 | static struct platform_driver kirkwood_thermal_driver = { | |
106 | .probe = kirkwood_thermal_probe, | |
107 | .remove = kirkwood_thermal_exit, | |
108 | .driver = { | |
109 | .name = "kirkwood_thermal", | |
90c3194e | 110 | .of_match_table = kirkwood_thermal_id_table, |
7060aa36 NI |
111 | }, |
112 | }; | |
113 | ||
114 | module_platform_driver(kirkwood_thermal_driver); | |
115 | ||
116 | MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>"); | |
117 | MODULE_DESCRIPTION("kirkwood thermal driver"); | |
118 | MODULE_LICENSE("GPL"); |