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/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
16 #include <lantiq_soc.h>
18 /* gphy1 configuration register contains cpu temperature */
19 #define CGU_GPHY1_CR 0x0040
20 #define CGU_TEMP_PD BIT(19)
22 static void ltq_cputemp_enable(void)
24 ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR
) | CGU_TEMP_PD
, CGU_GPHY1_CR
);
27 static void ltq_cputemp_disable(void *data
)
29 ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR
) & ~CGU_TEMP_PD
, CGU_GPHY1_CR
);
32 static int ltq_read(struct device
*dev
, enum hwmon_sensor_types type
,
33 u32 attr
, int channel
, long *temp
)
38 case hwmon_temp_input
:
39 /* get the temperature including one decimal place */
40 value
= (ltq_cgu_r32(CGU_GPHY1_CR
) >> 9) & 0x01FF;
42 /* range -38 to +154 °C, register value zero is -38.0 °C */
44 /* scale temp to millidegree */
55 static umode_t
ltq_is_visible(const void *_data
, enum hwmon_sensor_types type
,
56 u32 attr
, int channel
)
58 if (type
!= hwmon_temp
)
62 case hwmon_temp_input
:
69 static const struct hwmon_channel_info
* const ltq_info
[] = {
70 HWMON_CHANNEL_INFO(chip
,
72 HWMON_CHANNEL_INFO(temp
,
77 static const struct hwmon_ops ltq_hwmon_ops
= {
78 .is_visible
= ltq_is_visible
,
82 static const struct hwmon_chip_info ltq_chip_info
= {
83 .ops
= <q_hwmon_ops
,
87 static int ltq_cputemp_probe(struct platform_device
*pdev
)
89 struct device
*hwmon_dev
;
92 /* available on vr9 v1.2 SoCs only */
93 if (ltq_soc_type() != SOC_TYPE_VR9_2
)
96 err
= devm_add_action(&pdev
->dev
, ltq_cputemp_disable
, NULL
);
100 ltq_cputemp_enable();
102 hwmon_dev
= devm_hwmon_device_register_with_info(&pdev
->dev
,
108 if (IS_ERR(hwmon_dev
)) {
109 dev_err(&pdev
->dev
, "Failed to register as hwmon device");
110 return PTR_ERR(hwmon_dev
);
116 const struct of_device_id ltq_cputemp_match
[] = {
117 { .compatible
= "lantiq,cputemp" },
120 MODULE_DEVICE_TABLE(of
, ltq_cputemp_match
);
122 static struct platform_driver ltq_cputemp_driver
= {
123 .probe
= ltq_cputemp_probe
,
125 .name
= "ltq-cputemp",
126 .of_match_table
= ltq_cputemp_match
,
130 module_platform_driver(ltq_cputemp_driver
);
132 MODULE_AUTHOR("Florian Eckert <fe@dev.tdt.de>");
133 MODULE_DESCRIPTION("Lantiq cpu temperature sensor driver");
134 MODULE_LICENSE("GPL");