2 * Kirkwood thermal sensor driver
4 * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
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.
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.
16 #include <linux/device.h>
17 #include <linux/err.h>
19 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/thermal.h>
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
30 /* Kirkwood Thermal Sensor Dev Structure */
31 struct kirkwood_thermal_priv
{
35 static int kirkwood_get_temp(struct thermal_zone_device
*thermal
,
39 struct kirkwood_thermal_priv
*priv
= thermal
->devdata
;
41 reg
= readl_relaxed(priv
->sensor
);
44 if (!(reg
>> KIRKWOOD_THERMAL_VALID_OFFSET
) &
45 KIRKWOOD_THERMAL_VALID_MASK
) {
46 dev_err(&thermal
->device
,
47 "Temperature sensor reading not valid\n");
52 * Calculate temperature. See Section 8.10.1 of the 88AP510,
53 * datasheet, which has the same sensor.
54 * Documentation/arm/Marvell/README
56 reg
= (reg
>> KIRKWOOD_THERMAL_TEMP_OFFSET
) &
57 KIRKWOOD_THERMAL_TEMP_MASK
;
58 *temp
= ((2281638UL - (7298*reg
)) / 10);
63 static struct thermal_zone_device_ops ops
= {
64 .get_temp
= kirkwood_get_temp
,
67 static const struct of_device_id kirkwood_thermal_id_table
[] = {
68 { .compatible
= "marvell,kirkwood-thermal" },
72 static int kirkwood_thermal_probe(struct platform_device
*pdev
)
74 struct thermal_zone_device
*thermal
= NULL
;
75 struct kirkwood_thermal_priv
*priv
;
78 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
80 dev_err(&pdev
->dev
, "Failed to get platform resource\n");
84 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
88 priv
->sensor
= devm_request_and_ioremap(&pdev
->dev
, res
);
90 dev_err(&pdev
->dev
, "Failed to request_ioremap memory\n");
91 return -EADDRNOTAVAIL
;
94 thermal
= thermal_zone_device_register("kirkwood_thermal", 0, 0,
95 priv
, &ops
, NULL
, 0, 0);
96 if (IS_ERR(thermal
)) {
98 "Failed to register thermal zone device\n");
99 return PTR_ERR(thermal
);
102 platform_set_drvdata(pdev
, thermal
);
107 static int kirkwood_thermal_exit(struct platform_device
*pdev
)
109 struct thermal_zone_device
*kirkwood_thermal
=
110 platform_get_drvdata(pdev
);
112 thermal_zone_device_unregister(kirkwood_thermal
);
113 platform_set_drvdata(pdev
, NULL
);
118 MODULE_DEVICE_TABLE(of
, kirkwood_thermal_id_table
);
120 static struct platform_driver kirkwood_thermal_driver
= {
121 .probe
= kirkwood_thermal_probe
,
122 .remove
= kirkwood_thermal_exit
,
124 .name
= "kirkwood_thermal",
125 .owner
= THIS_MODULE
,
126 .of_match_table
= of_match_ptr(kirkwood_thermal_id_table
),
130 module_platform_driver(kirkwood_thermal_driver
);
132 MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
133 MODULE_DESCRIPTION("kirkwood thermal driver");
134 MODULE_LICENSE("GPL");