2 +++ b/drivers/hwmon/gsp.c
5 + * A hwmon driver for the Gateworks System Peripheral
6 + * Copyright (C) 2009 Gateworks Corporation
8 + * Author: Chris Lang <clang@gateworks.com>
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License,
12 + * as published by the Free Software Foundation - version 2.
15 +#include <linux/module.h>
16 +#include <linux/i2c.h>
17 +#include <linux/hwmon.h>
18 +#include <linux/hwmon-sysfs.h>
19 +#include <linux/err.h>
22 +#define DRV_VERSION "0.2"
26 +/* AD7418 registers */
27 +#define GSP_REG_TEMP_IN 0x00
28 +#define GSP_REG_VIN 0x02
29 +#define GSP_REG_3P3 0x05
30 +#define GSP_REG_BAT 0x08
31 +#define GSP_REG_5P0 0x0b
32 +#define GSP_REG_CORE 0x0e
33 +#define GSP_REG_CPU1 0x11
34 +#define GSP_REG_CPU2 0x14
35 +#define GSP_REG_DRAM 0x17
36 +#define GSP_REG_EXT_BAT 0x1a
37 +#define GSP_REG_IO1 0x1d
38 +#define GSP_REG_IO2 0x20
39 +#define GSP_REG_PCIE 0x23
40 +#define GSP_REG_CURRENT 0x26
41 +#define GSP_FAN_0 0x2C
42 +#define GSP_FAN_1 0x2E
43 +#define GSP_FAN_2 0x30
44 +#define GSP_FAN_3 0x32
45 +#define GSP_FAN_4 0x34
46 +#define GSP_FAN_5 0x36
48 +struct gsp_sensor_info {
53 +static const struct gsp_sensor_info gsp_sensors[] = {
54 + {"temp", GSP_REG_TEMP_IN},
55 + {"vin", GSP_REG_VIN},
56 + {"3p3", GSP_REG_3P3},
57 + {"bat", GSP_REG_BAT},
58 + {"5p0", GSP_REG_5P0},
59 + {"core", GSP_REG_CORE},
60 + {"cpu1", GSP_REG_CPU1},
61 + {"cpu2", GSP_REG_CPU2},
62 + {"dram", GSP_REG_DRAM},
63 + {"ext_bat", GSP_REG_EXT_BAT},
64 + {"io1", GSP_REG_IO1},
65 + {"io2", GSP_REG_IO2},
66 + {"pci2", GSP_REG_PCIE},
67 + {"current", GSP_REG_CURRENT},
68 + {"fan_point0", GSP_FAN_0},
69 + {"fan_point1", GSP_FAN_1},
70 + {"fan_point2", GSP_FAN_2},
71 + {"fan_point3", GSP_FAN_3},
72 + {"fan_point4", GSP_FAN_4},
73 + {"fan_point5", GSP_FAN_5},
77 + struct device *hwmon_dev;
78 + struct attribute_group attrs;
82 +static int gsp_probe(struct i2c_client *client,
83 + const struct i2c_device_id *id);
84 +static int gsp_remove(struct i2c_client *client);
86 +static const struct i2c_device_id gsp_id[] = {
90 +MODULE_DEVICE_TABLE(i2c, gsp_id);
92 +static struct i2c_driver gsp_driver = {
97 + .remove = gsp_remove,
101 +/* All registers are word-sized, except for the configuration registers.
102 + * AD7418 uses a high-byte first convention. Do NOT use those functions to
103 + * access the configuration registers CONF and CONF2, as they are byte-sized.
105 +static inline int gsp_read(struct i2c_client *client, u8 reg)
107 + unsigned int adc = 0;
108 + if (reg == GSP_REG_TEMP_IN || reg > GSP_REG_CURRENT)
110 + adc |= i2c_smbus_read_byte_data(client, reg);
111 + adc |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
116 + adc |= i2c_smbus_read_byte_data(client, reg);
117 + adc |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
118 + adc |= i2c_smbus_read_byte_data(client, reg + 2) << 16;
123 +static inline int gsp_write(struct i2c_client *client, u8 reg, u16 value)
125 + i2c_smbus_write_byte_data(client, reg, value & 0xff);
126 + i2c_smbus_write_byte_data(client, reg + 1, ((value >> 8) & 0xff));
130 +static ssize_t show_adc(struct device *dev, struct device_attribute *devattr,
133 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
134 + struct i2c_client *client = to_i2c_client(dev);
135 + return sprintf(buf, "%d\n", gsp_read(client, gsp_sensors[attr->index].reg));
138 +static ssize_t show_label(struct device *dev,
139 + struct device_attribute *devattr, char *buf)
141 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
143 + return sprintf(buf, "%s\n", gsp_sensors[attr->index].name);
146 +static ssize_t store_fan(struct device *dev,
147 + struct device_attribute *devattr, const char *buf, size_t count)
150 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
151 + struct i2c_client *client = to_i2c_client(dev);
152 + val = simple_strtoul(buf, NULL, 10);
153 + gsp_write(client, gsp_sensors[attr->index].reg, val);
157 +static SENSOR_DEVICE_ATTR(temp0_input, S_IRUGO, show_adc, NULL, 0);
158 +static SENSOR_DEVICE_ATTR(temp0_label, S_IRUGO, show_label, NULL, 0);
160 +static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_adc, NULL, 1);
161 +static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL, 1);
162 +static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 2);
163 +static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_label, NULL, 2);
164 +static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 3);
165 +static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_label, NULL, 3);
166 +static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 4);
167 +static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 4);
168 +static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 5);
169 +static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_label, NULL, 5);
170 +static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_adc, NULL, 6);
171 +static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_label, NULL, 6);
172 +static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_adc, NULL, 7);
173 +static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_label, NULL, 7);
174 +static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_adc, NULL, 8);
175 +static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 8);
176 +static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_adc, NULL, 9);
177 +static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 9);
178 +static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_adc, NULL, 10);
179 +static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 10);
180 +static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_adc, NULL, 11);
181 +static SENSOR_DEVICE_ATTR(in10_label, S_IRUGO, show_label, NULL, 11);
182 +static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_adc, NULL, 12);
183 +static SENSOR_DEVICE_ATTR(in11_label, S_IRUGO, show_label, NULL, 12);
184 +static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_adc, NULL, 13);
185 +static SENSOR_DEVICE_ATTR(in12_label, S_IRUGO, show_label, NULL, 13);
187 +static SENSOR_DEVICE_ATTR(fan0_point0, S_IRUGO | S_IWUSR, show_adc, store_fan, 14);
188 +static SENSOR_DEVICE_ATTR(fan0_point1, S_IRUGO | S_IWUSR, show_adc, store_fan, 15);
189 +static SENSOR_DEVICE_ATTR(fan0_point2, S_IRUGO | S_IWUSR, show_adc, store_fan, 16);
190 +static SENSOR_DEVICE_ATTR(fan0_point3, S_IRUGO | S_IWUSR, show_adc, store_fan, 17);
191 +static SENSOR_DEVICE_ATTR(fan0_point4, S_IRUGO | S_IWUSR, show_adc, store_fan, 18);
192 +static SENSOR_DEVICE_ATTR(fan0_point5, S_IRUGO | S_IWUSR, show_adc, store_fan, 19);
196 +static struct attribute *gsp_attributes[] = {
197 + &sensor_dev_attr_temp0_input.dev_attr.attr,
198 + &sensor_dev_attr_in0_input.dev_attr.attr,
199 + &sensor_dev_attr_in1_input.dev_attr.attr,
200 + &sensor_dev_attr_in2_input.dev_attr.attr,
201 + &sensor_dev_attr_in3_input.dev_attr.attr,
202 + &sensor_dev_attr_in4_input.dev_attr.attr,
203 + &sensor_dev_attr_in5_input.dev_attr.attr,
204 + &sensor_dev_attr_in6_input.dev_attr.attr,
205 + &sensor_dev_attr_in7_input.dev_attr.attr,
206 + &sensor_dev_attr_in8_input.dev_attr.attr,
207 + &sensor_dev_attr_in9_input.dev_attr.attr,
208 + &sensor_dev_attr_in10_input.dev_attr.attr,
209 + &sensor_dev_attr_in11_input.dev_attr.attr,
210 + &sensor_dev_attr_in12_input.dev_attr.attr,
212 + &sensor_dev_attr_temp0_label.dev_attr.attr,
213 + &sensor_dev_attr_in0_label.dev_attr.attr,
214 + &sensor_dev_attr_in1_label.dev_attr.attr,
215 + &sensor_dev_attr_in2_label.dev_attr.attr,
216 + &sensor_dev_attr_in3_label.dev_attr.attr,
217 + &sensor_dev_attr_in4_label.dev_attr.attr,
218 + &sensor_dev_attr_in5_label.dev_attr.attr,
219 + &sensor_dev_attr_in6_label.dev_attr.attr,
220 + &sensor_dev_attr_in7_label.dev_attr.attr,
221 + &sensor_dev_attr_in8_label.dev_attr.attr,
222 + &sensor_dev_attr_in9_label.dev_attr.attr,
223 + &sensor_dev_attr_in10_label.dev_attr.attr,
224 + &sensor_dev_attr_in11_label.dev_attr.attr,
225 + &sensor_dev_attr_in12_label.dev_attr.attr,
227 + &sensor_dev_attr_fan0_point0.dev_attr.attr,
228 + &sensor_dev_attr_fan0_point1.dev_attr.attr,
229 + &sensor_dev_attr_fan0_point2.dev_attr.attr,
230 + &sensor_dev_attr_fan0_point3.dev_attr.attr,
231 + &sensor_dev_attr_fan0_point4.dev_attr.attr,
232 + &sensor_dev_attr_fan0_point5.dev_attr.attr,
238 +static int gsp_probe(struct i2c_client *client,
239 + const struct i2c_device_id *id)
241 + struct i2c_adapter *adapter = client->adapter;
242 + struct gsp_data *data;
245 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
246 + I2C_FUNC_SMBUS_WORD_DATA)) {
251 + if (!(data = kzalloc(sizeof(struct gsp_data), GFP_KERNEL))) {
256 + i2c_set_clientdata(client, data);
258 + data->type = id->driver_data;
260 + switch (data->type) {
262 + data->attrs.attrs = gsp_attributes;
266 + dev_info(&client->dev, "%s chip found\n", client->name);
268 + /* Register sysfs hooks */
269 + if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
272 + data->hwmon_dev = hwmon_device_register(&client->dev);
273 + if (IS_ERR(data->hwmon_dev)) {
274 + err = PTR_ERR(data->hwmon_dev);
281 + sysfs_remove_group(&client->dev.kobj, &data->attrs);
288 +static int gsp_remove(struct i2c_client *client)
290 + struct gsp_data *data = i2c_get_clientdata(client);
291 + hwmon_device_unregister(data->hwmon_dev);
292 + sysfs_remove_group(&client->dev.kobj, &data->attrs);
297 +static int __init gsp_init(void)
299 + return i2c_add_driver(&gsp_driver);
302 +static void __exit gsp_exit(void)
304 + i2c_del_driver(&gsp_driver);
307 +MODULE_AUTHOR("Chris Lang <clang@gateworks.com>");
308 +MODULE_DESCRIPTION("GSP HWMON driver");
309 +MODULE_LICENSE("GPL");
310 +MODULE_VERSION(DRV_VERSION);
312 +module_init(gsp_init);
313 +module_exit(gsp_exit);
314 --- a/drivers/hwmon/Kconfig
315 +++ b/drivers/hwmon/Kconfig
316 @@ -57,6 +57,15 @@ config SENSORS_ABITUGURU3
317 This driver can also be built as a module. If so, the module
318 will be called abituguru3.
321 + tristate "Gateworks System Peripheral"
322 + depends on I2C && EXPERIMENTAL
324 + If you say yes here you get support for the Gateworks System Peripherals.
326 + This driver can also be built as a module. If so, the module
327 + will be called gsp.
329 config SENSORS_AD7414
330 tristate "Analog Devices AD7414"
331 depends on I2C && EXPERIMENTAL
332 --- a/drivers/hwmon/Makefile
333 +++ b/drivers/hwmon/Makefile
334 @@ -15,6 +15,7 @@ obj-$(CONFIG_SENSORS_W83791D) += w83791d
336 obj-$(CONFIG_SENSORS_ABITUGURU) += abituguru.o
337 obj-$(CONFIG_SENSORS_ABITUGURU3)+= abituguru3.o
338 +obj-$(CONFIG_SENSORS_GSP) += gsp.o
339 obj-$(CONFIG_SENSORS_AD7414) += ad7414.o
340 obj-$(CONFIG_SENSORS_AD7418) += ad7418.o
341 obj-$(CONFIG_SENSORS_ADCXX) += adcxx.o