xref: /linux/drivers/hwmon/ltq-cputemp.c (revision d27bb0246e5356dbef4d923e72c680bf893885a8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Lantiq cpu temperature sensor driver
3  *
4  * Copyright (C) 2017 Florian Eckert <fe@dev.tdt.de>
5  */
6 
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>
15 
16 #include <lantiq_soc.h>
17 
18 /* gphy1 configuration register contains cpu temperature */
19 #define CGU_GPHY1_CR   0x0040
20 #define CGU_TEMP_PD    BIT(19)
21 
22 static void ltq_cputemp_enable(void)
23 {
24 	ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) | CGU_TEMP_PD, CGU_GPHY1_CR);
25 }
26 
27 static void ltq_cputemp_disable(void *data)
28 {
29 	ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) & ~CGU_TEMP_PD, CGU_GPHY1_CR);
30 }
31 
32 static int ltq_read(struct device *dev, enum hwmon_sensor_types type,
33 		    u32 attr, int channel, long *temp)
34 {
35 	int value;
36 
37 	switch (attr) {
38 	case hwmon_temp_input:
39 		/* get the temperature including one decimal place */
40 		value = (ltq_cgu_r32(CGU_GPHY1_CR) >> 9) & 0x01FF;
41 		value = value * 5;
42 		/* range -38 to +154 °C, register value zero is -38.0 °C */
43 		value -= 380;
44 		/* scale temp to millidegree */
45 		value = value * 100;
46 		break;
47 	default:
48 		return -EOPNOTSUPP;
49 	}
50 
51 	*temp = value;
52 	return 0;
53 }
54 
55 static umode_t ltq_is_visible(const void *_data, enum hwmon_sensor_types type,
56 			      u32 attr, int channel)
57 {
58 	if (type != hwmon_temp)
59 		return 0;
60 
61 	switch (attr) {
62 	case hwmon_temp_input:
63 		return 0444;
64 	default:
65 		return 0;
66 	}
67 }
68 
69 static const struct hwmon_channel_info * const ltq_info[] = {
70 	HWMON_CHANNEL_INFO(chip,
71 			   HWMON_C_REGISTER_TZ),
72 	HWMON_CHANNEL_INFO(temp,
73 			   HWMON_T_INPUT),
74 	NULL
75 };
76 
77 static const struct hwmon_ops ltq_hwmon_ops = {
78 	.is_visible = ltq_is_visible,
79 	.read = ltq_read,
80 };
81 
82 static const struct hwmon_chip_info ltq_chip_info = {
83 	.ops = &ltq_hwmon_ops,
84 	.info = ltq_info,
85 };
86 
87 static int ltq_cputemp_probe(struct platform_device *pdev)
88 {
89 	struct device *hwmon_dev;
90 	int err = 0;
91 
92 	/* available on vr9 v1.2 SoCs only */
93 	if (ltq_soc_type() != SOC_TYPE_VR9_2)
94 		return -ENODEV;
95 
96 	err = devm_add_action(&pdev->dev, ltq_cputemp_disable, NULL);
97 	if (err)
98 		return err;
99 
100 	ltq_cputemp_enable();
101 
102 	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
103 							 "ltq_cputemp",
104 							 NULL,
105 							 &ltq_chip_info,
106 							 NULL);
107 
108 	if (IS_ERR(hwmon_dev)) {
109 		dev_err(&pdev->dev, "Failed to register as hwmon device");
110 		return PTR_ERR(hwmon_dev);
111 	}
112 
113 	return 0;
114 }
115 
116 const struct of_device_id ltq_cputemp_match[] = {
117 	{ .compatible = "lantiq,cputemp" },
118 	{},
119 };
120 MODULE_DEVICE_TABLE(of, ltq_cputemp_match);
121 
122 static struct platform_driver ltq_cputemp_driver = {
123 	.probe = ltq_cputemp_probe,
124 	.driver = {
125 		.name = "ltq-cputemp",
126 		.of_match_table = ltq_cputemp_match,
127 	},
128 };
129 
130 module_platform_driver(ltq_cputemp_driver);
131 
132 MODULE_AUTHOR("Florian Eckert <fe@dev.tdt.de>");
133 MODULE_DESCRIPTION("Lantiq cpu temperature sensor driver");
134 MODULE_LICENSE("GPL");
135