lantiq: add cpu temperatur sensor driver for xrx200
[openwrt/.git] / target / linux / lantiq / patches-4.4 / 0302-xrx200-add-sensors-driver.patch
1 --- a/drivers/hwmon/Makefile
2 +++ b/drivers/hwmon/Makefile
3 @@ -107,6 +107,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 @@ -762,6 +762,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,138 @@
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/of_device.h>
52 +#include <linux/hwmon.h>
53 +#include <linux/hwmon-sysfs.h>
54 +
55 +#include <lantiq_soc.h>
56 +
57 +/* gphy1 configuration register contains cpu temperature */
58 +#define CGU_GPHY1_CR   0x0040
59 +#define CGU_TEMP_PD    BIT(19)
60 +
61 +static void ltq_cputemp_enable(void)
62 +{
63 +       ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) | CGU_TEMP_PD, CGU_GPHY1_CR);
64 +}
65 +
66 +static void ltq_cputemp_disable(void)
67 +{
68 +       ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) & ~CGU_TEMP_PD, CGU_GPHY1_CR);
69 +}
70 +
71 +static int ltq_cputemp_read(void)
72 +{
73 +       /* Shift 9 for register alignment and 1 to divide value by 2 */
74 +       return (ltq_cgu_r32(CGU_GPHY1_CR) >> 10) & 0xFF;
75 +}
76 +
77 +static ssize_t show_cputemp(struct device *dev,
78 +                       struct device_attribute *attr, char *buf)
79 +{
80 +       int value;
81 +
82 +       value = ltq_cputemp_read();
83 +       /* scale temp to millidegree */
84 +       value = value * 1000;
85 +
86 +       return sprintf(buf, "%d\n", value);
87 +}
88 +
89 +static DEVICE_ATTR(temp1_input, S_IRUGO, show_cputemp, NULL);
90 +
91 +static struct attribute *ltq_cputemp_attrs[] = {
92 +       &dev_attr_temp1_input.attr,
93 +       NULL
94 +};
95 +
96 +ATTRIBUTE_GROUPS(ltq_cputemp);
97 +
98 +static int ltq_cputemp_probe(struct platform_device *pdev)
99 +{
100 +       int value = 0;
101 +       int ret;
102 +       struct device *hwmon_dev;
103 +
104 +       hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
105 +                                                       "CPU0",
106 +                                                       NULL,
107 +                                                       ltq_cputemp_groups);
108 +
109 +       if (IS_ERR(hwmon_dev)) {
110 +               dev_err(&pdev->dev, "Failed to register as hwmon device");
111 +               ret = PTR_ERR(hwmon_dev);
112 +               goto error_hwmon;
113 +       }
114 +
115 +       ltq_cputemp_enable();
116 +       value = ltq_cputemp_read();
117 +       dev_info(&pdev->dev, "Current CPU die temperature: %d °C", value);
118 +
119 +       return 0;
120 +
121 +error_hwmon:
122 +       return ret;
123 +}
124 +
125 +static int ltq_cputemp_release(struct platform_device *pdev)
126 +{
127 +       hwmon_device_unregister(&pdev->dev);
128 +       ltq_cputemp_disable();
129 +       return 0;
130 +}
131 +
132 +const struct of_device_id ltq_cputemp_match[] = {
133 +       { .compatible = "lantiq,cputemp" },
134 +       {},
135 +};
136 +MODULE_DEVICE_TABLE(of, ltq_cputemp_match);
137 +
138 +static struct platform_driver ltq_cputemp_driver = {
139 +       .probe = ltq_cputemp_probe,
140 +       .remove = ltq_cputemp_release,
141 +       .driver = {
142 +               .name = "ltq-cputemp",
143 +               .owner = THIS_MODULE,
144 +               .of_match_table = ltq_cputemp_match,
145 +       },
146 +};
147 +
148 +int __init init_ltq_cputemp(void)
149 +{
150 +       int ret;
151 +
152 +       ret = platform_driver_register(&ltq_cputemp_driver);
153 +       return ret;
154 +}
155 +
156 +void clean_ltq_cputemp(void)
157 +{
158 +       platform_driver_unregister(&ltq_cputemp_driver);
159 +       return;
160 +}
161 +
162 +module_init(init_ltq_cputemp);
163 +module_exit(clean_ltq_cputemp);
164 +
165 +MODULE_AUTHOR("Florian Eckert <feckert@tdt.de>");
166 +
167 +MODULE_DESCRIPTION("Lantiq Temperature Sensor");
168 +MODULE_LICENSE("GPL");