blob: cce3350e539ef68509410b246a1dd3b387698c76 [file] [log] [blame]
Sebastian Witt9cb7d182005-04-13 22:27:53 +02001/*
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>
Jean Delvare0cacdf22005-07-29 12:15:12 -070024#include <linux/jiffies.h>
Sebastian Witt9cb7d182005-04-13 22:27:53 +020025#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040026#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020027#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040028#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010029#include <linux/mutex.h>
Jean Delvarea5ebe662006-09-24 21:24:46 +020030#include <linux/sysfs.h>
Sebastian Witt9cb7d182005-04-13 22:27:53 +020031
32MODULE_LICENSE("GPL");
33MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
34MODULE_VERSION("0.6.2");
35MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
36
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
44
45static unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
Sebastian Witt9cb7d182005-04-13 22:27:53 +020046
Jean Delvaref4b50262005-07-31 21:49:03 +020047I2C_CLIENT_INSMOD_1(atxp1);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020048
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 = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010055 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010056 .name = "atxp1",
57 },
Sebastian Witt9cb7d182005-04-13 22:27:53 +020058 .attach_adapter = atxp1_attach_adapter,
59 .detach_client = atxp1_detach_client,
60};
61
62struct atxp1_data {
63 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -070064 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010065 struct mutex update_lock;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020066 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
Ingo Molnar9a61bf62006-01-18 23:19:26 +010085 mutex_lock(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020086
Jean Delvare0cacdf22005-07-29 12:15:12 -070087 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Sebastian Witt9cb7d182005-04-13 22:27:53 +020088
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
Ingo Molnar9a61bf62006-01-18 23:19:26 +010098 mutex_unlock(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020099
100 return(data);
101}
102
103/* sys file functions for cpu0_vid */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700104static ssize_t atxp1_showvcore(struct device *dev, struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200105{
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
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700116static ssize_t atxp1_storevcore(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200117{
118 struct atxp1_data *data;
119 struct i2c_client *client;
Alexey Dobriyanc41bdb52006-08-28 14:18:14 +0200120 int vid, cvid;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200121 unsigned int vcore;
122
123 client = to_i2c_client(dev);
124 data = atxp1_update_device(dev);
125
126 vcore = simple_strtoul(buf, NULL, 10);
127 vcore /= 25;
128 vcore *= 25;
129
130 /* Calculate VID */
131 vid = vid_to_reg(vcore, data->vrm);
132
133 if (vid < 0) {
134 dev_err(dev, "VID calculation failed.\n");
135 return -1;
136 }
137
138 /* If output enabled, use control register value. Otherwise original CPU VID */
139 if (data->reg.vid & ATXP1_VIDENA)
140 cvid = data->reg.vid & ATXP1_VIDMASK;
141 else
142 cvid = data->reg.cpu_vid;
143
144 /* Nothing changed, aborting */
145 if (vid == cvid)
146 return count;
147
Prakash Punnoor164cad92005-06-29 14:13:54 +0200148 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", vcore, vid);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200149
150 /* Write every 25 mV step to increase stability */
151 if (cvid > vid) {
152 for (; cvid >= vid; cvid--) {
153 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
154 }
155 }
156 else {
157 for (; cvid <= vid; cvid++) {
158 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
159 }
160 }
161
162 data->valid = 0;
163
164 return count;
165}
166
167/* CPU core reference voltage
168 unit: millivolt
169*/
170static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore, atxp1_storevcore);
171
172/* sys file functions for GPIO1 */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700173static ssize_t atxp1_showgpio1(struct device *dev, struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200174{
175 int size;
176 struct atxp1_data *data;
177
178 data = atxp1_update_device(dev);
179
180 size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
181
182 return size;
183}
184
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700185static ssize_t atxp1_storegpio1(struct device *dev, struct device_attribute *attr, const char*buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200186{
187 struct atxp1_data *data;
188 struct i2c_client *client;
189 unsigned int value;
190
191 client = to_i2c_client(dev);
192 data = atxp1_update_device(dev);
193
194 value = simple_strtoul(buf, NULL, 16);
195
196 value &= ATXP1_GPIO1MASK;
197
198 if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
199 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
200
201 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
202
203 data->valid = 0;
204 }
205
206 return count;
207}
208
209/* GPIO1 data register
210 unit: Four bit as hex (e.g. 0x0f)
211*/
212static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
213
214/* sys file functions for GPIO2 */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700215static ssize_t atxp1_showgpio2(struct device *dev, struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200216{
217 int size;
218 struct atxp1_data *data;
219
220 data = atxp1_update_device(dev);
221
222 size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
223
224 return size;
225}
226
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700227static ssize_t atxp1_storegpio2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200228{
229 struct atxp1_data *data;
230 struct i2c_client *client;
231 unsigned int value;
232
233 client = to_i2c_client(dev);
234 data = atxp1_update_device(dev);
235
236 value = simple_strtoul(buf, NULL, 16) & 0xff;
237
238 if (value != data->reg.gpio2) {
239 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
240
241 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
242
243 data->valid = 0;
244 }
245
246 return count;
247}
248
249/* GPIO2 data register
250 unit: Eight bit as hex (e.g. 0xff)
251*/
252static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
253
Jean Delvarea5ebe662006-09-24 21:24:46 +0200254static struct attribute *atxp1_attributes[] = {
255 &dev_attr_gpio1.attr,
256 &dev_attr_gpio2.attr,
257 &dev_attr_cpu0_vid.attr,
258 NULL
259};
260
261static const struct attribute_group atxp1_group = {
262 .attrs = atxp1_attributes,
263};
264
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200265
266static int atxp1_attach_adapter(struct i2c_adapter *adapter)
267{
Jean Delvareddec7482005-10-17 23:02:42 +0200268 if (!(adapter->class & I2C_CLASS_HWMON))
269 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200270 return i2c_probe(adapter, &addr_data, &atxp1_detect);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200271};
272
273static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind)
274{
275 struct i2c_client * new_client;
276 struct atxp1_data * data;
277 int err = 0;
278 u8 temp;
279
280 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
281 goto exit;
282
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200283 if (!(data = kzalloc(sizeof(struct atxp1_data), GFP_KERNEL))) {
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200284 err = -ENOMEM;
285 goto exit;
286 }
287
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200288 new_client = &data->client;
289 i2c_set_clientdata(new_client, data);
290
291 new_client->addr = address;
292 new_client->adapter = adapter;
293 new_client->driver = &atxp1_driver;
294 new_client->flags = 0;
295
296 /* Detect ATXP1, checking if vendor ID registers are all zero */
297 if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
298 (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
299 (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
300 (i2c_smbus_read_byte_data(new_client, 0xff) == 0) )) {
301
302 /* No vendor ID, now checking if registers 0x10,0x11 (non-existent)
303 * showing the same as register 0x00 */
304 temp = i2c_smbus_read_byte_data(new_client, 0x00);
305
306 if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
307 (i2c_smbus_read_byte_data(new_client, 0x11) == temp) ))
308 goto exit_free;
309 }
310
311 /* Get VRM */
Jean Delvare303760b2005-07-31 21:52:01 +0200312 data->vrm = vid_which_vrm();
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200313
314 if ((data->vrm != 90) && (data->vrm != 91)) {
315 dev_err(&new_client->dev, "Not supporting VRM %d.%d\n",
316 data->vrm / 10, data->vrm % 10);
317 goto exit_free;
318 }
319
320 strncpy(new_client->name, "atxp1", I2C_NAME_SIZE);
321
322 data->valid = 0;
323
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100324 mutex_init(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200325
326 err = i2c_attach_client(new_client);
327
328 if (err)
329 {
330 dev_err(&new_client->dev, "Attach client error.\n");
331 goto exit_free;
332 }
333
Jean Delvarea5ebe662006-09-24 21:24:46 +0200334 /* Register sysfs hooks */
335 if ((err = sysfs_create_group(&new_client->dev.kobj, &atxp1_group)))
336 goto exit_detach;
337
Tony Jones1beeffe2007-08-20 13:46:20 -0700338 data->hwmon_dev = hwmon_device_register(&new_client->dev);
339 if (IS_ERR(data->hwmon_dev)) {
340 err = PTR_ERR(data->hwmon_dev);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200341 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400342 }
343
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200344 dev_info(&new_client->dev, "Using VRM: %d.%d\n",
345 data->vrm / 10, data->vrm % 10);
346
347 return 0;
348
Jean Delvarea5ebe662006-09-24 21:24:46 +0200349exit_remove_files:
350 sysfs_remove_group(&new_client->dev.kobj, &atxp1_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400351exit_detach:
352 i2c_detach_client(new_client);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200353exit_free:
354 kfree(data);
355exit:
356 return err;
357};
358
359static int atxp1_detach_client(struct i2c_client * client)
360{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400361 struct atxp1_data * data = i2c_get_clientdata(client);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200362 int err;
363
Tony Jones1beeffe2007-08-20 13:46:20 -0700364 hwmon_device_unregister(data->hwmon_dev);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200365 sysfs_remove_group(&client->dev.kobj, &atxp1_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400366
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200367 err = i2c_detach_client(client);
368
369 if (err)
370 dev_err(&client->dev, "Failed to detach client.\n");
371 else
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400372 kfree(data);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200373
374 return err;
375};
376
377static int __init atxp1_init(void)
378{
379 return i2c_add_driver(&atxp1_driver);
380};
381
382static void __exit atxp1_exit(void)
383{
384 i2c_del_driver(&atxp1_driver);
385};
386
387module_init(atxp1_init);
388module_exit(atxp1_exit);