Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / atxp1.c
blob6105621fa00f032339fe7da502b3a88500eb1ba0
1 /*
2 atxp1.c - kernel module for setting CPU VID and general purpose
3 I/Os using the Attansic ATXP1 chip.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-vid.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <linux/sysfs.h>
32 MODULE_LICENSE("GPL");
33 MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
34 MODULE_VERSION("0.6.2");
35 MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
37 #define ATXP1_VID 0x00
38 #define ATXP1_CVID 0x01
39 #define ATXP1_GPIO1 0x06
40 #define ATXP1_GPIO2 0x0a
41 #define ATXP1_VIDENA 0x20
42 #define ATXP1_VIDMASK 0x1f
43 #define ATXP1_GPIO1MASK 0x0f
45 <<<<<<< HEAD:drivers/hwmon/atxp1.c
46 static unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
47 =======
48 static const unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
49 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/atxp1.c
51 I2C_CLIENT_INSMOD_1(atxp1);
53 static int atxp1_attach_adapter(struct i2c_adapter * adapter);
54 static int atxp1_detach_client(struct i2c_client * client);
55 static struct atxp1_data * atxp1_update_device(struct device *dev);
56 static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind);
58 static struct i2c_driver atxp1_driver = {
59 .driver = {
60 .name = "atxp1",
62 .attach_adapter = atxp1_attach_adapter,
63 .detach_client = atxp1_detach_client,
66 struct atxp1_data {
67 struct i2c_client client;
68 struct device *hwmon_dev;
69 struct mutex update_lock;
70 unsigned long last_updated;
71 u8 valid;
72 struct {
73 u8 vid; /* VID output register */
74 u8 cpu_vid; /* VID input from CPU */
75 u8 gpio1; /* General purpose I/O register 1 */
76 u8 gpio2; /* General purpose I/O register 2 */
77 } reg;
78 u8 vrm; /* Detected CPU VRM */
81 static struct atxp1_data * atxp1_update_device(struct device *dev)
83 struct i2c_client *client;
84 struct atxp1_data *data;
86 client = to_i2c_client(dev);
87 data = i2c_get_clientdata(client);
89 mutex_lock(&data->update_lock);
91 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
93 /* Update local register data */
94 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
95 data->reg.cpu_vid = i2c_smbus_read_byte_data(client, ATXP1_CVID);
96 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
97 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
99 data->valid = 1;
102 mutex_unlock(&data->update_lock);
104 return(data);
107 /* sys file functions for cpu0_vid */
108 static ssize_t atxp1_showvcore(struct device *dev, struct device_attribute *attr, char *buf)
110 int size;
111 struct atxp1_data *data;
113 data = atxp1_update_device(dev);
115 size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK, data->vrm));
117 return size;
120 static ssize_t atxp1_storevcore(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
122 struct atxp1_data *data;
123 struct i2c_client *client;
124 int vid, cvid;
125 unsigned int vcore;
127 client = to_i2c_client(dev);
128 data = atxp1_update_device(dev);
130 vcore = simple_strtoul(buf, NULL, 10);
131 vcore /= 25;
132 vcore *= 25;
134 /* Calculate VID */
135 vid = vid_to_reg(vcore, data->vrm);
137 if (vid < 0) {
138 dev_err(dev, "VID calculation failed.\n");
139 return -1;
142 /* If output enabled, use control register value. Otherwise original CPU VID */
143 if (data->reg.vid & ATXP1_VIDENA)
144 cvid = data->reg.vid & ATXP1_VIDMASK;
145 else
146 cvid = data->reg.cpu_vid;
148 /* Nothing changed, aborting */
149 if (vid == cvid)
150 return count;
152 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", vcore, vid);
154 /* Write every 25 mV step to increase stability */
155 if (cvid > vid) {
156 for (; cvid >= vid; cvid--) {
157 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
160 else {
161 for (; cvid <= vid; cvid++) {
162 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
166 data->valid = 0;
168 return count;
171 /* CPU core reference voltage
172 unit: millivolt
174 static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore, atxp1_storevcore);
176 /* sys file functions for GPIO1 */
177 static ssize_t atxp1_showgpio1(struct device *dev, struct device_attribute *attr, char *buf)
179 int size;
180 struct atxp1_data *data;
182 data = atxp1_update_device(dev);
184 size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
186 return size;
189 static ssize_t atxp1_storegpio1(struct device *dev, struct device_attribute *attr, const char*buf, size_t count)
191 struct atxp1_data *data;
192 struct i2c_client *client;
193 unsigned int value;
195 client = to_i2c_client(dev);
196 data = atxp1_update_device(dev);
198 value = simple_strtoul(buf, NULL, 16);
200 value &= ATXP1_GPIO1MASK;
202 if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
203 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
205 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
207 data->valid = 0;
210 return count;
213 /* GPIO1 data register
214 unit: Four bit as hex (e.g. 0x0f)
216 static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
218 /* sys file functions for GPIO2 */
219 static ssize_t atxp1_showgpio2(struct device *dev, struct device_attribute *attr, char *buf)
221 int size;
222 struct atxp1_data *data;
224 data = atxp1_update_device(dev);
226 size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
228 return size;
231 static ssize_t atxp1_storegpio2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
233 struct atxp1_data *data;
234 struct i2c_client *client;
235 unsigned int value;
237 client = to_i2c_client(dev);
238 data = atxp1_update_device(dev);
240 value = simple_strtoul(buf, NULL, 16) & 0xff;
242 if (value != data->reg.gpio2) {
243 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
245 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
247 data->valid = 0;
250 return count;
253 /* GPIO2 data register
254 unit: Eight bit as hex (e.g. 0xff)
256 static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
258 static struct attribute *atxp1_attributes[] = {
259 &dev_attr_gpio1.attr,
260 &dev_attr_gpio2.attr,
261 &dev_attr_cpu0_vid.attr,
262 NULL
265 static const struct attribute_group atxp1_group = {
266 .attrs = atxp1_attributes,
270 static int atxp1_attach_adapter(struct i2c_adapter *adapter)
272 if (!(adapter->class & I2C_CLASS_HWMON))
273 return 0;
274 return i2c_probe(adapter, &addr_data, &atxp1_detect);
277 static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind)
279 struct i2c_client * new_client;
280 struct atxp1_data * data;
281 int err = 0;
282 u8 temp;
284 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
285 goto exit;
287 if (!(data = kzalloc(sizeof(struct atxp1_data), GFP_KERNEL))) {
288 err = -ENOMEM;
289 goto exit;
292 new_client = &data->client;
293 i2c_set_clientdata(new_client, data);
295 new_client->addr = address;
296 new_client->adapter = adapter;
297 new_client->driver = &atxp1_driver;
298 new_client->flags = 0;
300 /* Detect ATXP1, checking if vendor ID registers are all zero */
301 if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
302 (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
303 (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
304 (i2c_smbus_read_byte_data(new_client, 0xff) == 0) )) {
306 /* No vendor ID, now checking if registers 0x10,0x11 (non-existent)
307 * showing the same as register 0x00 */
308 temp = i2c_smbus_read_byte_data(new_client, 0x00);
310 if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
311 (i2c_smbus_read_byte_data(new_client, 0x11) == temp) ))
312 goto exit_free;
315 /* Get VRM */
316 data->vrm = vid_which_vrm();
318 if ((data->vrm != 90) && (data->vrm != 91)) {
319 dev_err(&new_client->dev, "Not supporting VRM %d.%d\n",
320 data->vrm / 10, data->vrm % 10);
321 goto exit_free;
324 strncpy(new_client->name, "atxp1", I2C_NAME_SIZE);
326 data->valid = 0;
328 mutex_init(&data->update_lock);
330 err = i2c_attach_client(new_client);
332 if (err)
334 dev_err(&new_client->dev, "Attach client error.\n");
335 goto exit_free;
338 /* Register sysfs hooks */
339 if ((err = sysfs_create_group(&new_client->dev.kobj, &atxp1_group)))
340 goto exit_detach;
342 data->hwmon_dev = hwmon_device_register(&new_client->dev);
343 if (IS_ERR(data->hwmon_dev)) {
344 err = PTR_ERR(data->hwmon_dev);
345 goto exit_remove_files;
348 dev_info(&new_client->dev, "Using VRM: %d.%d\n",
349 data->vrm / 10, data->vrm % 10);
351 return 0;
353 exit_remove_files:
354 sysfs_remove_group(&new_client->dev.kobj, &atxp1_group);
355 exit_detach:
356 i2c_detach_client(new_client);
357 exit_free:
358 kfree(data);
359 exit:
360 return err;
363 static int atxp1_detach_client(struct i2c_client * client)
365 struct atxp1_data * data = i2c_get_clientdata(client);
366 int err;
368 hwmon_device_unregister(data->hwmon_dev);
369 sysfs_remove_group(&client->dev.kobj, &atxp1_group);
371 err = i2c_detach_client(client);
373 if (err)
374 dev_err(&client->dev, "Failed to detach client.\n");
375 else
376 kfree(data);
378 return err;
381 static int __init atxp1_init(void)
383 return i2c_add_driver(&atxp1_driver);
386 static void __exit atxp1_exit(void)
388 i2c_del_driver(&atxp1_driver);
391 module_init(atxp1_init);
392 module_exit(atxp1_exit);