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> 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 31 MODULE_LICENSE("GPL"); 32 MODULE_DESCRIPTION("System voltages control via Attansic ATXP1"); 33 MODULE_VERSION("0.6.2"); 34 MODULE_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 44 static unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END }; 45 46 I2C_CLIENT_INSMOD_1(atxp1); 47 48 static int atxp1_attach_adapter(struct i2c_adapter * adapter); 49 static int atxp1_detach_client(struct i2c_client * client); 50 static struct atxp1_data * atxp1_update_device(struct device *dev); 51 static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind); 52 53 static struct i2c_driver atxp1_driver = { 54 .driver = { 55 .name = "atxp1", 56 }, 57 .attach_adapter = atxp1_attach_adapter, 58 .detach_client = atxp1_detach_client, 59 }; 60 61 struct atxp1_data { 62 struct i2c_client client; 63 struct class_device *class_dev; 64 struct mutex update_lock; 65 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 76 static 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 84 mutex_lock(&data->update_lock); 85 86 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 87 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 97 mutex_unlock(&data->update_lock); 98 99 return(data); 100 } 101 102 /* sys file functions for cpu0_vid */ 103 static ssize_t atxp1_showvcore(struct device *dev, struct device_attribute *attr, char *buf) 104 { 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 115 static ssize_t atxp1_storevcore(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 116 { 117 struct atxp1_data *data; 118 struct i2c_client *client; 119 char vid; 120 char cvid; 121 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 148 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", vcore, vid); 149 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 */ 170 static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore, atxp1_storevcore); 171 172 /* sys file functions for GPIO1 */ 173 static ssize_t atxp1_showgpio1(struct device *dev, struct device_attribute *attr, char *buf) 174 { 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 185 static ssize_t atxp1_storegpio1(struct device *dev, struct device_attribute *attr, const char*buf, size_t count) 186 { 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 */ 212 static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1); 213 214 /* sys file functions for GPIO2 */ 215 static ssize_t atxp1_showgpio2(struct device *dev, struct device_attribute *attr, char *buf) 216 { 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 227 static ssize_t atxp1_storegpio2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 228 { 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 */ 252 static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2); 253 254 255 static int atxp1_attach_adapter(struct i2c_adapter *adapter) 256 { 257 if (!(adapter->class & I2C_CLASS_HWMON)) 258 return 0; 259 return i2c_probe(adapter, &addr_data, &atxp1_detect); 260 }; 261 262 static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind) 263 { 264 struct i2c_client * new_client; 265 struct atxp1_data * data; 266 int err = 0; 267 u8 temp; 268 269 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 270 goto exit; 271 272 if (!(data = kzalloc(sizeof(struct atxp1_data), GFP_KERNEL))) { 273 err = -ENOMEM; 274 goto exit; 275 } 276 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 = vid_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 mutex_init(&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 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 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 338 exit_detach: 339 i2c_detach_client(new_client); 340 exit_free: 341 kfree(data); 342 exit: 343 return err; 344 }; 345 346 static int atxp1_detach_client(struct i2c_client * client) 347 { 348 struct atxp1_data * data = i2c_get_clientdata(client); 349 int err; 350 351 hwmon_device_unregister(data->class_dev); 352 353 err = i2c_detach_client(client); 354 355 if (err) 356 dev_err(&client->dev, "Failed to detach client.\n"); 357 else 358 kfree(data); 359 360 return err; 361 }; 362 363 static int __init atxp1_init(void) 364 { 365 return i2c_add_driver(&atxp1_driver); 366 }; 367 368 static void __exit atxp1_exit(void) 369 { 370 i2c_del_driver(&atxp1_driver); 371 }; 372 373 module_init(atxp1_init); 374 module_exit(atxp1_exit); 375