lantiq: add support for kernel 4.9
[openwrt/openwrt.git] / target / linux / lantiq / patches-4.9 / 0302-xrx200-add-sensors-driver.patch
1 --- a/drivers/hwmon/Makefile
2 +++ b/drivers/hwmon/Makefile
3 @@ -109,6 +109,7 @@ obj-$(CONFIG_SENSORS_LTC4222) += ltc4222
4 obj-$(CONFIG_SENSORS_LTC4245) += ltc4245.o
5 obj-$(CONFIG_SENSORS_LTC4260) += ltc4260.o
6 obj-$(CONFIG_SENSORS_LTC4261) += ltc4261.o
7 +obj-$(CONFIG_SENSORS_LTQ_CPUTEMP) += ltq-cputemp.o
8 obj-$(CONFIG_SENSORS_MAX1111) += max1111.o
9 obj-$(CONFIG_SENSORS_MAX16065) += max16065.o
10 obj-$(CONFIG_SENSORS_MAX1619) += max1619.o
11 --- a/drivers/hwmon/Kconfig
12 +++ b/drivers/hwmon/Kconfig
13 @@ -780,6 +780,14 @@ config SENSORS_LTC4261
14 This driver can also be built as a module. If so, the module will
15 be called ltc4261.
16
17 +config SENSORS_LTQ_CPUTEMP
18 + bool "Lantiq CPU temperature sensor"
19 + depends on LANTIQ
20 + default n
21 + help
22 + If you say yes here you get support for the temperature
23 + sensor inside your CPU.
24 +
25 config SENSORS_MAX1111
26 tristate "Maxim MAX1111 Serial 8-bit ADC chip and compatibles"
27 depends on SPI_MASTER
28 --- /dev/null
29 +++ b/drivers/hwmon/ltq-cputemp.c
30 @@ -0,0 +1,154 @@
31 +/* Lantiq CPU Temperatur sensor driver for xrx200
32 + *
33 + * Copyright (C) 2016 Florian Eckert <feckert@tdt.de>
34 + *
35 + * This program is free software; you can redistribute it and/or modify
36 + * it under the terms of the GNU General Public License as published by
37 + * the Free Software Foundation; either version 2 of the License, or
38 + * (at your option) any later version
39 + *
40 + * This program is distributed in the hope that it will be useful
41 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 + * GNU General Public License for more details
44 + *
45 + * You should have received a copy of the GNU General Public License
46 + * along with this program; if not, see <http://www.gnu.org/licenses/>
47 + */
48 +
49 +#include <linux/module.h>
50 +#include <linux/init.h>
51 +#include <linux/delay.h>
52 +#include <linux/of_device.h>
53 +#include <linux/hwmon.h>
54 +#include <linux/hwmon-sysfs.h>
55 +
56 +#include <lantiq_soc.h>
57 +
58 +/* gphy1 configuration register contains cpu temperature */
59 +#define CGU_GPHY1_CR 0x0040
60 +#define CGU_TEMP_PD BIT(19)
61 +
62 +static void ltq_cputemp_enable(void)
63 +{
64 + ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) | CGU_TEMP_PD, CGU_GPHY1_CR);
65 +
66 + /* wait a short moment to let the SoC get the first temperatur value */
67 + mdelay(100);
68 +}
69 +
70 +static void ltq_cputemp_disable(void)
71 +{
72 + ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) & ~CGU_TEMP_PD, CGU_GPHY1_CR);
73 +}
74 +
75 +static int ltq_cputemp_read(void)
76 +{
77 + int value;
78 +
79 + /* get the temperature including one decimal place */
80 + value = (ltq_cgu_r32(CGU_GPHY1_CR) >> 9) & 0x01FF;
81 + value = (value << 2 ) + value;
82 +
83 + /* range -38 to +154 °C, register value zero is -38.0 °C */
84 + value -= 380;
85 +
86 + return value;
87 +}
88 +
89 +static ssize_t show_cputemp(struct device *dev,
90 + struct device_attribute *attr, char *buf)
91 +{
92 + int value;
93 +
94 + value = ltq_cputemp_read();
95 + /* scale temp to millidegree */
96 + value = value * 100;
97 +
98 + return sprintf(buf, "%d\n", value);
99 +}
100 +
101 +static DEVICE_ATTR(temp1_input, S_IRUGO, show_cputemp, NULL);
102 +
103 +static struct attribute *ltq_cputemp_attrs[] = {
104 + &dev_attr_temp1_input.attr,
105 + NULL
106 +};
107 +
108 +ATTRIBUTE_GROUPS(ltq_cputemp);
109 +
110 +static int ltq_cputemp_probe(struct platform_device *pdev)
111 +{
112 + int value = 0;
113 + int ret;
114 + struct device *hwmon_dev;
115 +
116 + /* available on vr9 v1.2 SoCs only */
117 + if (ltq_soc_type() != SOC_TYPE_VR9_2)
118 + return -ENODEV;
119 +
120 + hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
121 + "CPU0",
122 + NULL,
123 + ltq_cputemp_groups);
124 +
125 + if (IS_ERR(hwmon_dev)) {
126 + dev_err(&pdev->dev, "Failed to register as hwmon device");
127 + ret = PTR_ERR(hwmon_dev);
128 + goto error_hwmon;
129 + }
130 +
131 + ltq_cputemp_enable();
132 + value = ltq_cputemp_read();
133 + dev_info(&pdev->dev, "Current CPU die temperature: %d.%d °C", value / 10, value % 10);
134 +
135 + return 0;
136 +
137 +error_hwmon:
138 + return ret;
139 +}
140 +
141 +static int ltq_cputemp_release(struct platform_device *pdev)
142 +{
143 + hwmon_device_unregister(&pdev->dev);
144 + ltq_cputemp_disable();
145 + return 0;
146 +}
147 +
148 +const struct of_device_id ltq_cputemp_match[] = {
149 + { .compatible = "lantiq,cputemp" },
150 + {},
151 +};
152 +MODULE_DEVICE_TABLE(of, ltq_cputemp_match);
153 +
154 +static struct platform_driver ltq_cputemp_driver = {
155 + .probe = ltq_cputemp_probe,
156 + .remove = ltq_cputemp_release,
157 + .driver = {
158 + .name = "ltq-cputemp",
159 + .owner = THIS_MODULE,
160 + .of_match_table = ltq_cputemp_match,
161 + },
162 +};
163 +
164 +int __init init_ltq_cputemp(void)
165 +{
166 + int ret;
167 +
168 + ret = platform_driver_register(&ltq_cputemp_driver);
169 + return ret;
170 +}
171 +
172 +void clean_ltq_cputemp(void)
173 +{
174 + platform_driver_unregister(&ltq_cputemp_driver);
175 + return;
176 +}
177 +
178 +module_init(init_ltq_cputemp);
179 +module_exit(clean_ltq_cputemp);
180 +
181 +MODULE_AUTHOR("Florian Eckert <feckert@tdt.de>");
182 +
183 +MODULE_DESCRIPTION("Lantiq Temperature Sensor");
184 +MODULE_LICENSE("GPL");