1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Lantiq cpu temperature sensor driver
4 * Copyright (C) 2017 Florian Eckert <fe@dev.tdt.de>
7 #include <linux/bitops.h>
8 #include <linux/delay.h>
9 #include <linux/hwmon.h>
10 #include <linux/hwmon-sysfs.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
15 #include <lantiq_soc.h>
17 /* gphy1 configuration register contains cpu temperature */
18 #define CGU_GPHY1_CR 0x0040
19 #define CGU_TEMP_PD BIT(19)
21 static void ltq_cputemp_enable(void)
23 ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR
) | CGU_TEMP_PD
, CGU_GPHY1_CR
);
26 static void ltq_cputemp_disable(void *data
)
28 ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR
) & ~CGU_TEMP_PD
, CGU_GPHY1_CR
);
31 static int ltq_read(struct device
*dev
, enum hwmon_sensor_types type
,
32 u32 attr
, int channel
, long *temp
)
37 case hwmon_temp_input
:
38 /* get the temperature including one decimal place */
39 value
= (ltq_cgu_r32(CGU_GPHY1_CR
) >> 9) & 0x01FF;
41 /* range -38 to +154 °C, register value zero is -38.0 °C */
43 /* scale temp to millidegree */
54 static umode_t
ltq_is_visible(const void *_data
, enum hwmon_sensor_types type
,
55 u32 attr
, int channel
)
57 if (type
!= hwmon_temp
)
61 case hwmon_temp_input
:
68 static const struct hwmon_channel_info
*ltq_info
[] = {
69 HWMON_CHANNEL_INFO(chip
,
71 HWMON_CHANNEL_INFO(temp
,
76 static const struct hwmon_ops ltq_hwmon_ops
= {
77 .is_visible
= ltq_is_visible
,
81 static const struct hwmon_chip_info ltq_chip_info
= {
82 .ops
= <q_hwmon_ops
,
86 static int ltq_cputemp_probe(struct platform_device
*pdev
)
88 struct device
*hwmon_dev
;
91 /* available on vr9 v1.2 SoCs only */
92 if (ltq_soc_type() != SOC_TYPE_VR9_2
)
95 err
= devm_add_action(&pdev
->dev
, ltq_cputemp_disable
, NULL
);
101 hwmon_dev
= devm_hwmon_device_register_with_info(&pdev
->dev
,
107 if (IS_ERR(hwmon_dev
)) {
108 dev_err(&pdev
->dev
, "Failed to register as hwmon device");
109 return PTR_ERR(hwmon_dev
);
115 const struct of_device_id ltq_cputemp_match
[] = {
116 { .compatible
= "lantiq,cputemp" },
119 MODULE_DEVICE_TABLE(of
, ltq_cputemp_match
);
121 static struct platform_driver ltq_cputemp_driver
= {
122 .probe
= ltq_cputemp_probe
,
124 .name
= "ltq-cputemp",
125 .of_match_table
= ltq_cputemp_match
,
129 module_platform_driver(ltq_cputemp_driver
);
131 MODULE_AUTHOR("Florian Eckert <fe@dev.tdt.de>");
132 MODULE_DESCRIPTION("Lantiq cpu temperature sensor driver");
133 MODULE_LICENSE("GPL");