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/i2c.h> 25 #include <linux/i2c-sensor.h> 26 #include <linux/i2c-vid.h> 27 28 MODULE_LICENSE("GPL"); 29 MODULE_DESCRIPTION("System voltages control via Attansic ATXP1"); 30 MODULE_VERSION("0.6.2"); 31 MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>"); 32 33 #define ATXP1_VID 0x00 34 #define ATXP1_CVID 0x01 35 #define ATXP1_GPIO1 0x06 36 #define ATXP1_GPIO2 0x0a 37 #define ATXP1_VIDENA 0x20 38 #define ATXP1_VIDMASK 0x1f 39 #define ATXP1_GPIO1MASK 0x0f 40 41 static unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END }; 42 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; 43 44 SENSORS_INSMOD_1(atxp1); 45 46 static int atxp1_attach_adapter(struct i2c_adapter * adapter); 47 static int atxp1_detach_client(struct i2c_client * client); 48 static struct atxp1_data * atxp1_update_device(struct device *dev); 49 static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind); 50 51 static struct i2c_driver atxp1_driver = { 52 .owner = THIS_MODULE, 53 .name = "atxp1", 54 .flags = I2C_DF_NOTIFY, 55 .attach_adapter = atxp1_attach_adapter, 56 .detach_client = atxp1_detach_client, 57 }; 58 59 struct atxp1_data { 60 struct i2c_client client; 61 struct semaphore update_lock; 62 unsigned long last_updated; 63 u8 valid; 64 struct { 65 u8 vid; /* VID output register */ 66 u8 cpu_vid; /* VID input from CPU */ 67 u8 gpio1; /* General purpose I/O register 1 */ 68 u8 gpio2; /* General purpose I/O register 2 */ 69 } reg; 70 u8 vrm; /* Detected CPU VRM */ 71 }; 72 73 static struct atxp1_data * atxp1_update_device(struct device *dev) 74 { 75 struct i2c_client *client; 76 struct atxp1_data *data; 77 78 client = to_i2c_client(dev); 79 data = i2c_get_clientdata(client); 80 81 down(&data->update_lock); 82 83 if ((jiffies - data->last_updated > HZ) || 84 (jiffies < data->last_updated) || 85 !data->valid) { 86 87 /* Update local register data */ 88 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID); 89 data->reg.cpu_vid = i2c_smbus_read_byte_data(client, ATXP1_CVID); 90 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1); 91 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2); 92 93 data->valid = 1; 94 } 95 96 up(&data->update_lock); 97 98 return(data); 99 } 100 101 /* sys file functions for cpu0_vid */ 102 static ssize_t atxp1_showvcore(struct device *dev, struct device_attribute *attr, char *buf) 103 { 104 int size; 105 struct atxp1_data *data; 106 107 data = atxp1_update_device(dev); 108 109 size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK, data->vrm)); 110 111 return size; 112 } 113 114 static ssize_t atxp1_storevcore(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 115 { 116 struct atxp1_data *data; 117 struct i2c_client *client; 118 char vid; 119 char cvid; 120 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 147 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", vcore, vid); 148 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 */ 169 static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore, atxp1_storevcore); 170 171 /* sys file functions for GPIO1 */ 172 static ssize_t atxp1_showgpio1(struct device *dev, struct device_attribute *attr, char *buf) 173 { 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 184 static ssize_t atxp1_storegpio1(struct device *dev, struct device_attribute *attr, const char*buf, size_t count) 185 { 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 */ 211 static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1); 212 213 /* sys file functions for GPIO2 */ 214 static ssize_t atxp1_showgpio2(struct device *dev, struct device_attribute *attr, char *buf) 215 { 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 226 static ssize_t atxp1_storegpio2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 227 { 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 */ 251 static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2); 252 253 254 static int atxp1_attach_adapter(struct i2c_adapter *adapter) 255 { 256 return i2c_detect(adapter, &addr_data, &atxp1_detect); 257 }; 258 259 static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind) 260 { 261 struct i2c_client * new_client; 262 struct atxp1_data * data; 263 int err = 0; 264 u8 temp; 265 266 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 267 goto exit; 268 269 if (!(data = kmalloc(sizeof(struct atxp1_data), GFP_KERNEL))) { 270 err = -ENOMEM; 271 goto exit; 272 } 273 274 memset(data, 0, sizeof(struct atxp1_data)); 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 */ 299 data->vrm = i2c_which_vrm(); 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 321 device_create_file(&new_client->dev, &dev_attr_gpio1); 322 device_create_file(&new_client->dev, &dev_attr_gpio2); 323 device_create_file(&new_client->dev, &dev_attr_cpu0_vid); 324 325 dev_info(&new_client->dev, "Using VRM: %d.%d\n", 326 data->vrm / 10, data->vrm % 10); 327 328 return 0; 329 330 exit_free: 331 kfree(data); 332 exit: 333 return err; 334 }; 335 336 static int atxp1_detach_client(struct i2c_client * client) 337 { 338 int err; 339 340 err = i2c_detach_client(client); 341 342 if (err) 343 dev_err(&client->dev, "Failed to detach client.\n"); 344 else 345 kfree(i2c_get_clientdata(client)); 346 347 return err; 348 }; 349 350 static int __init atxp1_init(void) 351 { 352 return i2c_add_driver(&atxp1_driver); 353 }; 354 355 static void __exit atxp1_exit(void) 356 { 357 i2c_del_driver(&atxp1_driver); 358 }; 359 360 module_init(atxp1_init); 361 module_exit(atxp1_exit); 362