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