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