[PATCH] I2C hwmon: add hwmon sysfs class to drivers
[deliverable/linux.git] / drivers / hwmon / atxp1.c
CommitLineData
9cb7d184
SW
1/*
2 atxp1.c - kernel module for setting CPU VID and general purpose
3 I/Os using the Attansic ATXP1 chip.
4
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.
9
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.
14
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.
18
19*/
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
0cacdf29 24#include <linux/jiffies.h>
9cb7d184
SW
25#include <linux/i2c.h>
26#include <linux/i2c-sensor.h>
27#include <linux/i2c-vid.h>
943b0830
MH
28#include <linux/hwmon.h>
29#include <linux/err.h>
9cb7d184
SW
30
31MODULE_LICENSE("GPL");
32MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
33MODULE_VERSION("0.6.2");
34MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
35
36#define ATXP1_VID 0x00
37#define ATXP1_CVID 0x01
38#define ATXP1_GPIO1 0x06
39#define ATXP1_GPIO2 0x0a
40#define ATXP1_VIDENA 0x20
41#define ATXP1_VIDMASK 0x1f
42#define ATXP1_GPIO1MASK 0x0f
43
44static unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
45static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
46
47SENSORS_INSMOD_1(atxp1);
48
49static int atxp1_attach_adapter(struct i2c_adapter * adapter);
50static int atxp1_detach_client(struct i2c_client * client);
51static struct atxp1_data * atxp1_update_device(struct device *dev);
52static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind);
53
54static struct i2c_driver atxp1_driver = {
55 .owner = THIS_MODULE,
56 .name = "atxp1",
57 .flags = I2C_DF_NOTIFY,
58 .attach_adapter = atxp1_attach_adapter,
59 .detach_client = atxp1_detach_client,
60};
61
62struct atxp1_data {
63 struct i2c_client client;
943b0830 64 struct class_device *class_dev;
9cb7d184
SW
65 struct semaphore update_lock;
66 unsigned long last_updated;
67 u8 valid;
68 struct {
69 u8 vid; /* VID output register */
70 u8 cpu_vid; /* VID input from CPU */
71 u8 gpio1; /* General purpose I/O register 1 */
72 u8 gpio2; /* General purpose I/O register 2 */
73 } reg;
74 u8 vrm; /* Detected CPU VRM */
75};
76
77static struct atxp1_data * atxp1_update_device(struct device *dev)
78{
79 struct i2c_client *client;
80 struct atxp1_data *data;
81
82 client = to_i2c_client(dev);
83 data = i2c_get_clientdata(client);
84
85 down(&data->update_lock);
86
0cacdf29 87 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
9cb7d184
SW
88
89 /* Update local register data */
90 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
91 data->reg.cpu_vid = i2c_smbus_read_byte_data(client, ATXP1_CVID);
92 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
93 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
94
95 data->valid = 1;
96 }
97
98 up(&data->update_lock);
99
100 return(data);
101}
102
103/* sys file functions for cpu0_vid */
6f637a64 104static ssize_t atxp1_showvcore(struct device *dev, struct device_attribute *attr, char *buf)
9cb7d184
SW
105{
106 int size;
107 struct atxp1_data *data;
108
109 data = atxp1_update_device(dev);
110
111 size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK, data->vrm));
112
113 return size;
114}
115
6f637a64 116static ssize_t atxp1_storevcore(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
9cb7d184
SW
117{
118 struct atxp1_data *data;
119 struct i2c_client *client;
120 char vid;
121 char cvid;
122 unsigned int vcore;
123
124 client = to_i2c_client(dev);
125 data = atxp1_update_device(dev);
126
127 vcore = simple_strtoul(buf, NULL, 10);
128 vcore /= 25;
129 vcore *= 25;
130
131 /* Calculate VID */
132 vid = vid_to_reg(vcore, data->vrm);
133
134 if (vid < 0) {
135 dev_err(dev, "VID calculation failed.\n");
136 return -1;
137 }
138
139 /* If output enabled, use control register value. Otherwise original CPU VID */
140 if (data->reg.vid & ATXP1_VIDENA)
141 cvid = data->reg.vid & ATXP1_VIDMASK;
142 else
143 cvid = data->reg.cpu_vid;
144
145 /* Nothing changed, aborting */
146 if (vid == cvid)
147 return count;
148
164cad9b 149 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", vcore, vid);
9cb7d184
SW
150
151 /* Write every 25 mV step to increase stability */
152 if (cvid > vid) {
153 for (; cvid >= vid; cvid--) {
154 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
155 }
156 }
157 else {
158 for (; cvid <= vid; cvid++) {
159 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
160 }
161 }
162
163 data->valid = 0;
164
165 return count;
166}
167
168/* CPU core reference voltage
169 unit: millivolt
170*/
171static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore, atxp1_storevcore);
172
173/* sys file functions for GPIO1 */
6f637a64 174static ssize_t atxp1_showgpio1(struct device *dev, struct device_attribute *attr, char *buf)
9cb7d184
SW
175{
176 int size;
177 struct atxp1_data *data;
178
179 data = atxp1_update_device(dev);
180
181 size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
182
183 return size;
184}
185
6f637a64 186static ssize_t atxp1_storegpio1(struct device *dev, struct device_attribute *attr, const char*buf, size_t count)
9cb7d184
SW
187{
188 struct atxp1_data *data;
189 struct i2c_client *client;
190 unsigned int value;
191
192 client = to_i2c_client(dev);
193 data = atxp1_update_device(dev);
194
195 value = simple_strtoul(buf, NULL, 16);
196
197 value &= ATXP1_GPIO1MASK;
198
199 if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
200 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
201
202 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
203
204 data->valid = 0;
205 }
206
207 return count;
208}
209
210/* GPIO1 data register
211 unit: Four bit as hex (e.g. 0x0f)
212*/
213static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
214
215/* sys file functions for GPIO2 */
6f637a64 216static ssize_t atxp1_showgpio2(struct device *dev, struct device_attribute *attr, char *buf)
9cb7d184
SW
217{
218 int size;
219 struct atxp1_data *data;
220
221 data = atxp1_update_device(dev);
222
223 size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
224
225 return size;
226}
227
6f637a64 228static ssize_t atxp1_storegpio2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
9cb7d184
SW
229{
230 struct atxp1_data *data;
231 struct i2c_client *client;
232 unsigned int value;
233
234 client = to_i2c_client(dev);
235 data = atxp1_update_device(dev);
236
237 value = simple_strtoul(buf, NULL, 16) & 0xff;
238
239 if (value != data->reg.gpio2) {
240 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
241
242 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
243
244 data->valid = 0;
245 }
246
247 return count;
248}
249
250/* GPIO2 data register
251 unit: Eight bit as hex (e.g. 0xff)
252*/
253static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
254
255
256static int atxp1_attach_adapter(struct i2c_adapter *adapter)
257{
258 return i2c_detect(adapter, &addr_data, &atxp1_detect);
259};
260
261static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind)
262{
263 struct i2c_client * new_client;
264 struct atxp1_data * data;
265 int err = 0;
266 u8 temp;
267
268 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
269 goto exit;
270
271 if (!(data = kmalloc(sizeof(struct atxp1_data), GFP_KERNEL))) {
272 err = -ENOMEM;
273 goto exit;
274 }
275
276 memset(data, 0, sizeof(struct atxp1_data));
277 new_client = &data->client;
278 i2c_set_clientdata(new_client, data);
279
280 new_client->addr = address;
281 new_client->adapter = adapter;
282 new_client->driver = &atxp1_driver;
283 new_client->flags = 0;
284
285 /* Detect ATXP1, checking if vendor ID registers are all zero */
286 if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
287 (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
288 (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
289 (i2c_smbus_read_byte_data(new_client, 0xff) == 0) )) {
290
291 /* No vendor ID, now checking if registers 0x10,0x11 (non-existent)
292 * showing the same as register 0x00 */
293 temp = i2c_smbus_read_byte_data(new_client, 0x00);
294
295 if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
296 (i2c_smbus_read_byte_data(new_client, 0x11) == temp) ))
297 goto exit_free;
298 }
299
300 /* Get VRM */
301 data->vrm = i2c_which_vrm();
302
303 if ((data->vrm != 90) && (data->vrm != 91)) {
304 dev_err(&new_client->dev, "Not supporting VRM %d.%d\n",
305 data->vrm / 10, data->vrm % 10);
306 goto exit_free;
307 }
308
309 strncpy(new_client->name, "atxp1", I2C_NAME_SIZE);
310
311 data->valid = 0;
312
313 init_MUTEX(&data->update_lock);
314
315 err = i2c_attach_client(new_client);
316
317 if (err)
318 {
319 dev_err(&new_client->dev, "Attach client error.\n");
320 goto exit_free;
321 }
322
943b0830
MH
323 data->class_dev = hwmon_device_register(&new_client->dev);
324 if (IS_ERR(data->class_dev)) {
325 err = PTR_ERR(data->class_dev);
326 goto exit_detach;
327 }
328
9cb7d184
SW
329 device_create_file(&new_client->dev, &dev_attr_gpio1);
330 device_create_file(&new_client->dev, &dev_attr_gpio2);
331 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
332
333 dev_info(&new_client->dev, "Using VRM: %d.%d\n",
334 data->vrm / 10, data->vrm % 10);
335
336 return 0;
337
943b0830
MH
338exit_detach:
339 i2c_detach_client(new_client);
9cb7d184
SW
340exit_free:
341 kfree(data);
342exit:
343 return err;
344};
345
346static int atxp1_detach_client(struct i2c_client * client)
347{
943b0830 348 struct atxp1_data * data = i2c_get_clientdata(client);
9cb7d184
SW
349 int err;
350
943b0830
MH
351 hwmon_device_unregister(data->class_dev);
352
9cb7d184
SW
353 err = i2c_detach_client(client);
354
355 if (err)
356 dev_err(&client->dev, "Failed to detach client.\n");
357 else
943b0830 358 kfree(data);
9cb7d184
SW
359
360 return err;
361};
362
363static int __init atxp1_init(void)
364{
365 return i2c_add_driver(&atxp1_driver);
366};
367
368static void __exit atxp1_exit(void)
369{
370 i2c_del_driver(&atxp1_driver);
371};
372
373module_init(atxp1_init);
374module_exit(atxp1_exit);
This page took 0.078868 seconds and 5 git commands to generate.