kernel: update 3.10 to 3.10.2
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-3.10 / 013-bcm2835-hwmon-driver.patch
1 --- /dev/null
2 +++ b/drivers/hwmon/bcm2835-hwmon.c
3 @@ -0,0 +1,219 @@
4 +/*****************************************************************************
5 +* Copyright 2011 Broadcom Corporation. All rights reserved.
6 +*
7 +* Unless you and Broadcom execute a separate written software license
8 +* agreement governing use of this software, this software is licensed to you
9 +* under the terms of the GNU General Public License version 2, available at
10 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
11 +*
12 +* Notwithstanding the above, under no circumstances may you combine this
13 +* software in any way with any other Broadcom software provided under a
14 +* license other than the GPL, without Broadcom's express prior written
15 +* consent.
16 +*****************************************************************************/
17 +
18 +#include <linux/kernel.h>
19 +#include <linux/module.h>
20 +#include <linux/init.h>
21 +#include <linux/hwmon.h>
22 +#include <linux/hwmon-sysfs.h>
23 +#include <linux/platform_device.h>
24 +#include <linux/sysfs.h>
25 +#include <mach/vcio.h>
26 +#include <linux/slab.h>
27 +#include <linux/err.h>
28 +
29 +#define MODULE_NAME "bcm2835_hwmon"
30 +
31 +/*#define HWMON_DEBUG_ENABLE*/
32 +
33 +#ifdef HWMON_DEBUG_ENABLE
34 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
35 +#else
36 +#define print_debug(fmt,...)
37 +#endif
38 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
39 +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
40 +
41 +#define VC_TAG_GET_TEMP 0x00030006
42 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
43 +
44 +/* --- STRUCTS --- */
45 +struct bcm2835_hwmon_data {
46 + struct device *hwmon_dev;
47 +};
48 +
49 +/* tag part of the message */
50 +struct vc_msg_tag {
51 + uint32_t tag_id; /* the tag ID for the temperature */
52 + uint32_t buffer_size; /* size of the buffer (should be 8) */
53 + uint32_t request_code; /* identifies message as a request (should be 0) */
54 + uint32_t id; /* extra ID field (should be 0) */
55 + uint32_t val; /* returned value of the temperature */
56 +};
57 +
58 +/* message structure to be sent to videocore */
59 +struct vc_msg {
60 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
61 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
62 + struct vc_msg_tag tag; /* the tag structure above to make */
63 + uint32_t end_tag; /* an end identifier, should be set to NULL */
64 +};
65 +
66 +typedef enum {
67 + TEMP,
68 + MAX_TEMP,
69 +} temp_type;
70 +
71 +/* --- PROTOTYPES --- */
72 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
73 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
74 +
75 +/* --- GLOBALS --- */
76 +
77 +static struct bcm2835_hwmon_data *bcm2835_data;
78 +static struct platform_driver bcm2835_hwmon_driver;
79 +
80 +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
81 +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
82 +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
83 +
84 +static struct attribute* bcm2835_attributes[] = {
85 + &sensor_dev_attr_name.dev_attr.attr,
86 + &sensor_dev_attr_temp1_input.dev_attr.attr,
87 + &sensor_dev_attr_temp1_max.dev_attr.attr,
88 + NULL,
89 +};
90 +
91 +static struct attribute_group bcm2835_attr_group = {
92 + .attrs = bcm2835_attributes,
93 +};
94 +
95 +/* --- FUNCTIONS --- */
96 +
97 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
98 +{
99 + return sprintf(buf,"bcm2835_hwmon\n");
100 +}
101 +
102 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
103 +{
104 + struct vc_msg msg;
105 + int result;
106 + uint temp = 0;
107 + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
108 +
109 + print_debug("IN");
110 +
111 + /* wipe all previous message data */
112 + memset(&msg, 0, sizeof msg);
113 +
114 + /* determine the message type */
115 + if(index == TEMP)
116 + msg.tag.tag_id = VC_TAG_GET_TEMP;
117 + else if (index == MAX_TEMP)
118 + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
119 + else
120 + {
121 + print_debug("Unknown temperature message!");
122 + return -EINVAL;
123 + }
124 +
125 + msg.msg_size = sizeof msg;
126 + msg.tag.buffer_size = 8;
127 +
128 + /* send the message */
129 + result = bcm_mailbox_property(&msg, sizeof msg);
130 +
131 + /* check if it was all ok and return the rate in milli degrees C */
132 + if (result == 0 && (msg.request_code & 0x80000000))
133 + temp = (uint)msg.tag.val;
134 + #ifdef HWMON_DEBUG_ENABLE
135 + else
136 + print_debug("Failed to get temperature!");
137 + #endif
138 + print_debug("Got temperature as %u",temp);
139 + print_debug("OUT");
140 + return sprintf(buf, "%u\n", temp);
141 +}
142 +
143 +
144 +static int bcm2835_hwmon_probe(struct platform_device *pdev)
145 +{
146 + int err;
147 +
148 + print_debug("IN");
149 + print_debug("HWMON Driver has been probed!");
150 +
151 + /* check that the device isn't null!*/
152 + if(pdev == NULL)
153 + {
154 + print_debug("Platform device is empty!");
155 + return -ENODEV;
156 + }
157 +
158 + /* allocate memory for neccessary data */
159 + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
160 + if(!bcm2835_data)
161 + {
162 + print_debug("Unable to allocate memory for hwmon data!");
163 + err = -ENOMEM;
164 + goto kzalloc_error;
165 + }
166 +
167 + /* create the sysfs files */
168 + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
169 + {
170 + print_debug("Unable to create sysfs files!");
171 + err = -EFAULT;
172 + goto sysfs_error;
173 + }
174 +
175 + /* register the hwmon device */
176 + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
177 + if (IS_ERR(bcm2835_data->hwmon_dev))
178 + {
179 + err = PTR_ERR(bcm2835_data->hwmon_dev);
180 + goto hwmon_error;
181 + }
182 + print_debug("OUT");
183 + return 0;
184 +
185 + /* error goto's */
186 + hwmon_error:
187 + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
188 +
189 + sysfs_error:
190 + kfree(bcm2835_data);
191 +
192 + kzalloc_error:
193 +
194 + return err;
195 +
196 +}
197 +
198 +static int bcm2835_hwmon_remove(struct platform_device *pdev)
199 +{
200 + print_debug("IN");
201 + hwmon_device_unregister(bcm2835_data->hwmon_dev);
202 +
203 + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
204 + print_debug("OUT");
205 + return 0;
206 +}
207 +
208 +/* Hwmon Driver */
209 +static struct platform_driver bcm2835_hwmon_driver = {
210 + .probe = bcm2835_hwmon_probe,
211 + .remove = bcm2835_hwmon_remove,
212 + .driver = {
213 + .name = "bcm2835_hwmon",
214 + .owner = THIS_MODULE,
215 + },
216 +};
217 +
218 +MODULE_LICENSE("GPL");
219 +MODULE_AUTHOR("Dorian Peake");
220 +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
221 +
222 +module_platform_driver(bcm2835_hwmon_driver);
223 --- a/drivers/hwmon/Kconfig
224 +++ b/drivers/hwmon/Kconfig
225 @@ -1537,6 +1537,16 @@ config SENSORS_MC13783_ADC
226 help
227 Support for the A/D converter on MC13783 and MC13892 PMIC.
228
229 +config SENSORS_BCM2835
230 + depends on THERMAL_BCM2835=n
231 + tristate "Broadcom BCM2835 HWMON Driver"
232 + help
233 + If you say yes here you get support for the hardware
234 + monitoring features of the BCM2835 Chip
235 +
236 + This driver can also be built as a module. If so, the module
237 + will be called bcm2835-hwmon.
238 +
239 if ACPI
240
241 comment "ACPI drivers"
242 --- a/drivers/hwmon/Makefile
243 +++ b/drivers/hwmon/Makefile
244 @@ -141,6 +141,7 @@ obj-$(CONFIG_SENSORS_W83L786NG) += w83l7
245 obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
246 obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
247 obj-$(CONFIG_SENSORS_GSC) += gsc.o
248 +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
249
250 obj-$(CONFIG_PMBUS) += pmbus/
251