blob: ba843f8c4cef42355697245f916bf3346a6355aa [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>
Sebastian Witt9cb7d182005-04-13 22:27:53 +020030
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 };
Sebastian Witt9cb7d182005-04-13 22:27:53 +020045
Jean Delvaref4b50262005-07-31 21:49:03 +020046I2C_CLIENT_INSMOD_1(atxp1);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020047
48static int atxp1_attach_adapter(struct i2c_adapter * adapter);
49static int atxp1_detach_client(struct i2c_client * client);
50static struct atxp1_data * atxp1_update_device(struct device *dev);
51static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind);
52
53static struct i2c_driver atxp1_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010054 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010055 .name = "atxp1",
56 },
Sebastian Witt9cb7d182005-04-13 22:27:53 +020057 .attach_adapter = atxp1_attach_adapter,
58 .detach_client = atxp1_detach_client,
59};
60
61struct atxp1_data {
62 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -040063 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010064 struct mutex update_lock;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020065 unsigned long last_updated;
66 u8 valid;
67 struct {
68 u8 vid; /* VID output register */
69 u8 cpu_vid; /* VID input from CPU */
70 u8 gpio1; /* General purpose I/O register 1 */
71 u8 gpio2; /* General purpose I/O register 2 */
72 } reg;
73 u8 vrm; /* Detected CPU VRM */
74};
75
76static struct atxp1_data * atxp1_update_device(struct device *dev)
77{
78 struct i2c_client *client;
79 struct atxp1_data *data;
80
81 client = to_i2c_client(dev);
82 data = i2c_get_clientdata(client);
83
Ingo Molnar9a61bf62006-01-18 23:19:26 +010084 mutex_lock(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020085
Jean Delvare0cacdf22005-07-29 12:15:12 -070086 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Sebastian Witt9cb7d182005-04-13 22:27:53 +020087
88 /* Update local register data */
89 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
90 data->reg.cpu_vid = i2c_smbus_read_byte_data(client, ATXP1_CVID);
91 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
92 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
93
94 data->valid = 1;
95 }
96
Ingo Molnar9a61bf62006-01-18 23:19:26 +010097 mutex_unlock(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020098
99 return(data);
100}
101
102/* sys file functions for cpu0_vid */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700103static ssize_t atxp1_showvcore(struct device *dev, struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200104{
105 int size;
106 struct atxp1_data *data;
107
108 data = atxp1_update_device(dev);
109
110 size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK, data->vrm));
111
112 return size;
113}
114
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700115static ssize_t atxp1_storevcore(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200116{
117 struct atxp1_data *data;
118 struct i2c_client *client;
Alexey Dobriyanc41bdb52006-08-28 14:18:14 +0200119 int vid, cvid;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200120 unsigned int vcore;
121
122 client = to_i2c_client(dev);
123 data = atxp1_update_device(dev);
124
125 vcore = simple_strtoul(buf, NULL, 10);
126 vcore /= 25;
127 vcore *= 25;
128
129 /* Calculate VID */
130 vid = vid_to_reg(vcore, data->vrm);
131
132 if (vid < 0) {
133 dev_err(dev, "VID calculation failed.\n");
134 return -1;
135 }
136
137 /* If output enabled, use control register value. Otherwise original CPU VID */
138 if (data->reg.vid & ATXP1_VIDENA)
139 cvid = data->reg.vid & ATXP1_VIDMASK;
140 else
141 cvid = data->reg.cpu_vid;
142
143 /* Nothing changed, aborting */
144 if (vid == cvid)
145 return count;
146
Prakash Punnoor164cad92005-06-29 14:13:54 +0200147 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", vcore, vid);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200148
149 /* Write every 25 mV step to increase stability */
150 if (cvid > vid) {
151 for (; cvid >= vid; cvid--) {
152 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
153 }
154 }
155 else {
156 for (; cvid <= vid; cvid++) {
157 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
158 }
159 }
160
161 data->valid = 0;
162
163 return count;
164}
165
166/* CPU core reference voltage
167 unit: millivolt
168*/
169static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore, atxp1_storevcore);
170
171/* sys file functions for GPIO1 */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700172static ssize_t atxp1_showgpio1(struct device *dev, struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200173{
174 int size;
175 struct atxp1_data *data;
176
177 data = atxp1_update_device(dev);
178
179 size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
180
181 return size;
182}
183
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700184static ssize_t atxp1_storegpio1(struct device *dev, struct device_attribute *attr, const char*buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200185{
186 struct atxp1_data *data;
187 struct i2c_client *client;
188 unsigned int value;
189
190 client = to_i2c_client(dev);
191 data = atxp1_update_device(dev);
192
193 value = simple_strtoul(buf, NULL, 16);
194
195 value &= ATXP1_GPIO1MASK;
196
197 if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
198 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
199
200 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
201
202 data->valid = 0;
203 }
204
205 return count;
206}
207
208/* GPIO1 data register
209 unit: Four bit as hex (e.g. 0x0f)
210*/
211static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
212
213/* sys file functions for GPIO2 */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700214static ssize_t atxp1_showgpio2(struct device *dev, struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200215{
216 int size;
217 struct atxp1_data *data;
218
219 data = atxp1_update_device(dev);
220
221 size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
222
223 return size;
224}
225
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700226static ssize_t atxp1_storegpio2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200227{
228 struct atxp1_data *data;
229 struct i2c_client *client;
230 unsigned int value;
231
232 client = to_i2c_client(dev);
233 data = atxp1_update_device(dev);
234
235 value = simple_strtoul(buf, NULL, 16) & 0xff;
236
237 if (value != data->reg.gpio2) {
238 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
239
240 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
241
242 data->valid = 0;
243 }
244
245 return count;
246}
247
248/* GPIO2 data register
249 unit: Eight bit as hex (e.g. 0xff)
250*/
251static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
252
253
254static int atxp1_attach_adapter(struct i2c_adapter *adapter)
255{
Jean Delvareddec7482005-10-17 23:02:42 +0200256 if (!(adapter->class & I2C_CLASS_HWMON))
257 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200258 return i2c_probe(adapter, &addr_data, &atxp1_detect);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200259};
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
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200271 if (!(data = kzalloc(sizeof(struct atxp1_data), GFP_KERNEL))) {
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200272 err = -ENOMEM;
273 goto exit;
274 }
275
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200276 new_client = &data->client;
277 i2c_set_clientdata(new_client, data);
278
279 new_client->addr = address;
280 new_client->adapter = adapter;
281 new_client->driver = &atxp1_driver;
282 new_client->flags = 0;
283
284 /* Detect ATXP1, checking if vendor ID registers are all zero */
285 if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
286 (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
287 (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
288 (i2c_smbus_read_byte_data(new_client, 0xff) == 0) )) {
289
290 /* No vendor ID, now checking if registers 0x10,0x11 (non-existent)
291 * showing the same as register 0x00 */
292 temp = i2c_smbus_read_byte_data(new_client, 0x00);
293
294 if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
295 (i2c_smbus_read_byte_data(new_client, 0x11) == temp) ))
296 goto exit_free;
297 }
298
299 /* Get VRM */
Jean Delvare303760b2005-07-31 21:52:01 +0200300 data->vrm = vid_which_vrm();
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200301
302 if ((data->vrm != 90) && (data->vrm != 91)) {
303 dev_err(&new_client->dev, "Not supporting VRM %d.%d\n",
304 data->vrm / 10, data->vrm % 10);
305 goto exit_free;
306 }
307
308 strncpy(new_client->name, "atxp1", I2C_NAME_SIZE);
309
310 data->valid = 0;
311
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100312 mutex_init(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200313
314 err = i2c_attach_client(new_client);
315
316 if (err)
317 {
318 dev_err(&new_client->dev, "Attach client error.\n");
319 goto exit_free;
320 }
321
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400322 data->class_dev = hwmon_device_register(&new_client->dev);
323 if (IS_ERR(data->class_dev)) {
324 err = PTR_ERR(data->class_dev);
325 goto exit_detach;
326 }
327
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200328 device_create_file(&new_client->dev, &dev_attr_gpio1);
329 device_create_file(&new_client->dev, &dev_attr_gpio2);
330 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
331
332 dev_info(&new_client->dev, "Using VRM: %d.%d\n",
333 data->vrm / 10, data->vrm % 10);
334
335 return 0;
336
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400337exit_detach:
338 i2c_detach_client(new_client);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200339exit_free:
340 kfree(data);
341exit:
342 return err;
343};
344
345static int atxp1_detach_client(struct i2c_client * client)
346{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400347 struct atxp1_data * data = i2c_get_clientdata(client);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200348 int err;
349
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400350 hwmon_device_unregister(data->class_dev);
351
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200352 err = i2c_detach_client(client);
353
354 if (err)
355 dev_err(&client->dev, "Failed to detach client.\n");
356 else
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400357 kfree(data);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200358
359 return err;
360};
361
362static int __init atxp1_init(void)
363{
364 return i2c_add_driver(&atxp1_driver);
365};
366
367static void __exit atxp1_exit(void)
368{
369 i2c_del_driver(&atxp1_driver);
370};
371
372module_init(atxp1_init);
373module_exit(atxp1_exit);