atxp1.c 9.1 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * atxp1.c - kernel module for setting CPU VID and general purpose
 *	     I/Os using the Attansic ATXP1 chip.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
20 21 22 23

#include <linux/kernel.h>
#include <linux/init.h>
#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
#include <linux/sysfs.h>
31
#include <linux/slab.h>
32 33 34

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
35
MODULE_VERSION("0.6.3");
36 37 38 39 40 41 42 43 44 45
MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");

#define ATXP1_VID	0x00
#define ATXP1_CVID	0x01
#define ATXP1_GPIO1	0x06
#define ATXP1_GPIO2	0x0a
#define ATXP1_VIDENA	0x20
#define ATXP1_VIDMASK	0x1f
#define ATXP1_GPIO1MASK	0x0f

46
static const unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
47

48 49 50
static int atxp1_probe(struct i2c_client *client,
		       const struct i2c_device_id *id);
static int atxp1_remove(struct i2c_client *client);
51
static struct atxp1_data *atxp1_update_device(struct device *dev);
52
static int atxp1_detect(struct i2c_client *client, struct i2c_board_info *info);
53 54

static const struct i2c_device_id atxp1_id[] = {
J
Jean Delvare 已提交
55
	{ "atxp1", 0 },
56 57 58
	{ }
};
MODULE_DEVICE_TABLE(i2c, atxp1_id);
59 60

static struct i2c_driver atxp1_driver = {
61
	.class		= I2C_CLASS_HWMON,
62 63 64
	.driver = {
		.name	= "atxp1",
	},
65 66 67 68
	.probe		= atxp1_probe,
	.remove		= atxp1_remove,
	.id_table	= atxp1_id,
	.detect		= atxp1_detect,
69
	.address_list	= normal_i2c,
70 71 72
};

struct atxp1_data {
73
	struct device *hwmon_dev;
74
	struct mutex update_lock;
75 76 77 78 79 80 81 82 83 84 85
	unsigned long last_updated;
	u8 valid;
	struct {
		u8 vid;		/* VID output register */
		u8 cpu_vid; /* VID input from CPU */
		u8 gpio1;   /* General purpose I/O register 1 */
		u8 gpio2;   /* General purpose I/O register 2 */
	} reg;
	u8 vrm;			/* Detected CPU VRM */
};

86
static struct atxp1_data *atxp1_update_device(struct device *dev)
87 88 89 90 91 92 93
{
	struct i2c_client *client;
	struct atxp1_data *data;

	client = to_i2c_client(dev);
	data = i2c_get_clientdata(client);

94
	mutex_lock(&data->update_lock);
95

96
	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
97 98 99

		/* Update local register data */
		data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
100 101
		data->reg.cpu_vid = i2c_smbus_read_byte_data(client,
							     ATXP1_CVID);
102 103 104 105 106 107
		data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
		data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);

		data->valid = 1;
	}

108
	mutex_unlock(&data->update_lock);
109

110
	return data;
111 112 113
}

/* sys file functions for cpu0_vid */
114 115
static ssize_t atxp1_showvcore(struct device *dev,
			       struct device_attribute *attr, char *buf)
116 117 118 119 120 121
{
	int size;
	struct atxp1_data *data;

	data = atxp1_update_device(dev);

122 123
	size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK,
						 data->vrm));
124 125 126 127

	return size;
}

128 129 130
static ssize_t atxp1_storevcore(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
131 132 133
{
	struct atxp1_data *data;
	struct i2c_client *client;
134
	int vid, cvid;
135 136
	unsigned long vcore;
	int err;
137 138 139 140

	client = to_i2c_client(dev);
	data = atxp1_update_device(dev);

141 142 143 144
	err = kstrtoul(buf, 10, &vcore);
	if (err)
		return err;

145 146 147 148 149 150 151
	vcore /= 25;
	vcore *= 25;

	/* Calculate VID */
	vid = vid_to_reg(vcore, data->vrm);
	if (vid < 0) {
		dev_err(dev, "VID calculation failed.\n");
152
		return vid;
153 154
	}

155 156 157 158
	/*
	 * If output enabled, use control register value.
	 * Otherwise original CPU VID
	 */
159 160 161 162 163 164 165 166 167
	if (data->reg.vid & ATXP1_VIDENA)
		cvid = data->reg.vid & ATXP1_VIDMASK;
	else
		cvid = data->reg.cpu_vid;

	/* Nothing changed, aborting */
	if (vid == cvid)
		return count;

168
	dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", (int)vcore, vid);
169 170 171

	/* Write every 25 mV step to increase stability */
	if (cvid > vid) {
172 173 174 175 176 177 178
		for (; cvid >= vid; cvid--)
			i2c_smbus_write_byte_data(client,
						ATXP1_VID, cvid | ATXP1_VIDENA);
	} else {
		for (; cvid <= vid; cvid++)
			i2c_smbus_write_byte_data(client,
						ATXP1_VID, cvid | ATXP1_VIDENA);
179 180 181 182 183 184 185
	}

	data->valid = 0;

	return count;
}

186 187 188 189 190 191
/*
 * CPU core reference voltage
 * unit: millivolt
 */
static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore,
		   atxp1_storevcore);
192 193

/* sys file functions for GPIO1 */
194 195
static ssize_t atxp1_showgpio1(struct device *dev,
			       struct device_attribute *attr, char *buf)
196 197 198 199 200 201 202 203 204 205 206
{
	int size;
	struct atxp1_data *data;

	data = atxp1_update_device(dev);

	size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);

	return size;
}

207 208 209
static ssize_t atxp1_storegpio1(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
210 211 212
{
	struct atxp1_data *data;
	struct i2c_client *client;
213 214
	unsigned long value;
	int err;
215 216 217 218

	client = to_i2c_client(dev);
	data = atxp1_update_device(dev);

219 220 221
	err = kstrtoul(buf, 16, &value);
	if (err)
		return err;
222 223 224 225

	value &= ATXP1_GPIO1MASK;

	if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
226
		dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
227 228 229 230 231 232 233 234 235

		i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);

		data->valid = 0;
	}

	return count;
}

236 237 238 239
/*
 * GPIO1 data register
 * unit: Four bit as hex (e.g. 0x0f)
 */
240 241 242
static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);

/* sys file functions for GPIO2 */
243 244
static ssize_t atxp1_showgpio2(struct device *dev,
			       struct device_attribute *attr, char *buf)
245 246 247 248 249 250 251 252 253 254 255
{
	int size;
	struct atxp1_data *data;

	data = atxp1_update_device(dev);

	size = sprintf(buf, "0x%02x\n", data->reg.gpio2);

	return size;
}

256 257 258
static ssize_t atxp1_storegpio2(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
259
{
260 261 262 263
	struct atxp1_data *data = atxp1_update_device(dev);
	struct i2c_client *client = to_i2c_client(dev);
	unsigned long value;
	int err;
264

265 266 267 268
	err = kstrtoul(buf, 16, &value);
	if (err)
		return err;
	value &= 0xff;
269 270

	if (value != data->reg.gpio2) {
271
		dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
272 273 274 275 276 277 278 279 280

		i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);

		data->valid = 0;
	}

	return count;
}

281 282 283 284
/*
 * GPIO2 data register
 * unit: Eight bit as hex (e.g. 0xff)
 */
285 286
static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);

287 288 289 290 291 292 293 294 295 296 297
static struct attribute *atxp1_attributes[] = {
	&dev_attr_gpio1.attr,
	&dev_attr_gpio2.attr,
	&dev_attr_cpu0_vid.attr,
	NULL
};

static const struct attribute_group atxp1_group = {
	.attrs = atxp1_attributes,
};

298

299
/* Return 0 if detection is successful, -ENODEV otherwise */
300
static int atxp1_detect(struct i2c_client *new_client,
301
			struct i2c_board_info *info)
302
{
303
	struct i2c_adapter *adapter = new_client->adapter;
304 305 306 307

	u8 temp;

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
308
		return -ENODEV;
309 310 311 312 313

	/* Detect ATXP1, checking if vendor ID registers are all zero */
	if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
	     (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
	     (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
314 315
	     (i2c_smbus_read_byte_data(new_client, 0xff) == 0)))
		return -ENODEV;
316

317 318 319 320
	/*
	 * No vendor ID, now checking if registers 0x10,0x11 (non-existent)
	 * showing the same as register 0x00
	 */
321
	temp = i2c_smbus_read_byte_data(new_client, 0x00);
322

323 324 325
	if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
	      (i2c_smbus_read_byte_data(new_client, 0x11) == temp)))
		return -ENODEV;
326 327

	/* Get VRM */
328
	temp = vid_which_vrm();
329

330 331 332 333
	if ((temp != 90) && (temp != 91)) {
		dev_err(&adapter->dev, "atxp1: Not supporting VRM %d.%d\n",
				temp / 10, temp % 10);
		return -ENODEV;
334 335
	}

336
	strlcpy(info->type, "atxp1", I2C_NAME_SIZE);
337

338 339
	return 0;
}
340

341 342 343 344 345
static int atxp1_probe(struct i2c_client *new_client,
		       const struct i2c_device_id *id)
{
	struct atxp1_data *data;
	int err;
346

347 348 349 350
	data = devm_kzalloc(&new_client->dev, sizeof(struct atxp1_data),
			    GFP_KERNEL);
	if (!data)
		return -ENOMEM;
351

352 353 354 355 356 357
	/* Get VRM */
	data->vrm = vid_which_vrm();

	i2c_set_clientdata(new_client, data);
	mutex_init(&data->update_lock);

358
	/* Register sysfs hooks */
359 360
	err = sysfs_create_group(&new_client->dev.kobj, &atxp1_group);
	if (err)
361
		return err;
362

363 364 365
	data->hwmon_dev = hwmon_device_register(&new_client->dev);
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
366
		goto exit_remove_files;
367 368
	}

369 370 371 372 373
	dev_info(&new_client->dev, "Using VRM: %d.%d\n",
			 data->vrm / 10, data->vrm % 10);

	return 0;

374 375
exit_remove_files:
	sysfs_remove_group(&new_client->dev.kobj, &atxp1_group);
376 377 378
	return err;
};

379
static int atxp1_remove(struct i2c_client *client)
380
{
381
	struct atxp1_data *data = i2c_get_clientdata(client);
382

383
	hwmon_device_unregister(data->hwmon_dev);
384
	sysfs_remove_group(&client->dev.kobj, &atxp1_group);
385

386
	return 0;
387 388
};

389
module_i2c_driver(atxp1_driver);