blob: 4c829bb2f9db45ac28a1b38e1e8466ca88491721 [file] [log] [blame]
Sebastian Witt9cb7d182005-04-13 22:27:53 +02001/*
Guenter Roeckf24d5482012-01-14 13:21:59 -08002 * 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 */
Sebastian Witt9cb7d182005-04-13 22:27:53 +020020
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>
Jean Delvarea5ebe662006-09-24 21:24:46 +020030#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Sebastian Witt9cb7d182005-04-13 22:27:53 +020032
33MODULE_LICENSE("GPL");
34MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
Jean Delvare13b3c3f2008-09-20 10:25:19 +020035MODULE_VERSION("0.6.3");
Sebastian Witt9cb7d182005-04-13 22:27:53 +020036MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
37
38#define ATXP1_VID 0x00
39#define ATXP1_CVID 0x01
40#define ATXP1_GPIO1 0x06
41#define ATXP1_GPIO2 0x0a
42#define ATXP1_VIDENA 0x20
43#define ATXP1_VIDMASK 0x1f
44#define ATXP1_GPIO1MASK 0x0f
45
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050046static const unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
Sebastian Witt9cb7d182005-04-13 22:27:53 +020047
Sebastian Witt9cb7d182005-04-13 22:27:53 +020048struct atxp1_data {
Axel Lin11f7e492014-06-07 07:55:10 +080049 struct i2c_client *client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010050 struct mutex update_lock;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020051 unsigned long last_updated;
52 u8 valid;
53 struct {
54 u8 vid; /* VID output register */
55 u8 cpu_vid; /* VID input from CPU */
56 u8 gpio1; /* General purpose I/O register 1 */
57 u8 gpio2; /* General purpose I/O register 2 */
58 } reg;
59 u8 vrm; /* Detected CPU VRM */
60};
61
Guenter Roeckf24d5482012-01-14 13:21:59 -080062static struct atxp1_data *atxp1_update_device(struct device *dev)
Sebastian Witt9cb7d182005-04-13 22:27:53 +020063{
Axel Lin11f7e492014-06-07 07:55:10 +080064 struct atxp1_data *data = dev_get_drvdata(dev);
65 struct i2c_client *client = data->client;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020066
Ingo Molnar9a61bf62006-01-18 23:19:26 +010067 mutex_lock(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020068
Jean Delvare0cacdf22005-07-29 12:15:12 -070069 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Sebastian Witt9cb7d182005-04-13 22:27:53 +020070
71 /* Update local register data */
72 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
Guenter Roeckf24d5482012-01-14 13:21:59 -080073 data->reg.cpu_vid = i2c_smbus_read_byte_data(client,
74 ATXP1_CVID);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020075 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
76 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
77
78 data->valid = 1;
79 }
80
Ingo Molnar9a61bf62006-01-18 23:19:26 +010081 mutex_unlock(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020082
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +010083 return data;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020084}
85
86/* sys file functions for cpu0_vid */
Guenter Roeckf24d5482012-01-14 13:21:59 -080087static ssize_t atxp1_showvcore(struct device *dev,
88 struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +020089{
90 int size;
91 struct atxp1_data *data;
92
93 data = atxp1_update_device(dev);
94
Guenter Roeckf24d5482012-01-14 13:21:59 -080095 size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK,
96 data->vrm));
Sebastian Witt9cb7d182005-04-13 22:27:53 +020097
98 return size;
99}
100
Guenter Roeckf24d5482012-01-14 13:21:59 -0800101static ssize_t atxp1_storevcore(struct device *dev,
102 struct device_attribute *attr,
103 const char *buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200104{
Axel Lin11f7e492014-06-07 07:55:10 +0800105 struct atxp1_data *data = atxp1_update_device(dev);
106 struct i2c_client *client = data->client;
Alexey Dobriyanc41bdb52006-08-28 14:18:14 +0200107 int vid, cvid;
Guenter Roeckf24d5482012-01-14 13:21:59 -0800108 unsigned long vcore;
109 int err;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200110
Guenter Roeckf24d5482012-01-14 13:21:59 -0800111 err = kstrtoul(buf, 10, &vcore);
112 if (err)
113 return err;
114
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200115 vcore /= 25;
116 vcore *= 25;
117
118 /* Calculate VID */
119 vid = vid_to_reg(vcore, data->vrm);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200120 if (vid < 0) {
121 dev_err(dev, "VID calculation failed.\n");
Guenter Roeck674d0ed2013-09-13 10:59:27 -0700122 return vid;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200123 }
124
Guenter Roeckf24d5482012-01-14 13:21:59 -0800125 /*
126 * If output enabled, use control register value.
127 * Otherwise original CPU VID
128 */
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200129 if (data->reg.vid & ATXP1_VIDENA)
130 cvid = data->reg.vid & ATXP1_VIDMASK;
131 else
132 cvid = data->reg.cpu_vid;
133
134 /* Nothing changed, aborting */
135 if (vid == cvid)
136 return count;
137
Guenter Roeckf24d5482012-01-14 13:21:59 -0800138 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", (int)vcore, vid);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200139
140 /* Write every 25 mV step to increase stability */
141 if (cvid > vid) {
Guenter Roeckf24d5482012-01-14 13:21:59 -0800142 for (; cvid >= vid; cvid--)
143 i2c_smbus_write_byte_data(client,
144 ATXP1_VID, cvid | ATXP1_VIDENA);
145 } else {
146 for (; cvid <= vid; cvid++)
147 i2c_smbus_write_byte_data(client,
148 ATXP1_VID, cvid | ATXP1_VIDENA);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200149 }
150
151 data->valid = 0;
152
153 return count;
154}
155
Guenter Roeckf24d5482012-01-14 13:21:59 -0800156/*
157 * CPU core reference voltage
158 * unit: millivolt
159 */
160static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore,
161 atxp1_storevcore);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200162
163/* sys file functions for GPIO1 */
Guenter Roeckf24d5482012-01-14 13:21:59 -0800164static ssize_t atxp1_showgpio1(struct device *dev,
165 struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200166{
167 int size;
168 struct atxp1_data *data;
169
170 data = atxp1_update_device(dev);
171
172 size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
173
174 return size;
175}
176
Guenter Roeckf24d5482012-01-14 13:21:59 -0800177static ssize_t atxp1_storegpio1(struct device *dev,
178 struct device_attribute *attr, const char *buf,
179 size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200180{
Axel Lin11f7e492014-06-07 07:55:10 +0800181 struct atxp1_data *data = atxp1_update_device(dev);
182 struct i2c_client *client = data->client;
Guenter Roeckf24d5482012-01-14 13:21:59 -0800183 unsigned long value;
184 int err;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200185
Guenter Roeckf24d5482012-01-14 13:21:59 -0800186 err = kstrtoul(buf, 16, &value);
187 if (err)
188 return err;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200189
190 value &= ATXP1_GPIO1MASK;
191
192 if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
Guenter Roeckf24d5482012-01-14 13:21:59 -0800193 dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200194
195 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
196
197 data->valid = 0;
198 }
199
200 return count;
201}
202
Guenter Roeckf24d5482012-01-14 13:21:59 -0800203/*
204 * GPIO1 data register
205 * unit: Four bit as hex (e.g. 0x0f)
206 */
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200207static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
208
209/* sys file functions for GPIO2 */
Guenter Roeckf24d5482012-01-14 13:21:59 -0800210static ssize_t atxp1_showgpio2(struct device *dev,
211 struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200212{
213 int size;
214 struct atxp1_data *data;
215
216 data = atxp1_update_device(dev);
217
218 size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
219
220 return size;
221}
222
Guenter Roeckf24d5482012-01-14 13:21:59 -0800223static ssize_t atxp1_storegpio2(struct device *dev,
224 struct device_attribute *attr,
225 const char *buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200226{
Guenter Roeckf24d5482012-01-14 13:21:59 -0800227 struct atxp1_data *data = atxp1_update_device(dev);
Axel Lin11f7e492014-06-07 07:55:10 +0800228 struct i2c_client *client = data->client;
Guenter Roeckf24d5482012-01-14 13:21:59 -0800229 unsigned long value;
230 int err;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200231
Guenter Roeckf24d5482012-01-14 13:21:59 -0800232 err = kstrtoul(buf, 16, &value);
233 if (err)
234 return err;
235 value &= 0xff;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200236
237 if (value != data->reg.gpio2) {
Guenter Roeckf24d5482012-01-14 13:21:59 -0800238 dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200239
240 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
241
242 data->valid = 0;
243 }
244
245 return count;
246}
247
Guenter Roeckf24d5482012-01-14 13:21:59 -0800248/*
249 * GPIO2 data register
250 * unit: Eight bit as hex (e.g. 0xff)
251 */
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200252static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
253
Axel Lin11f7e492014-06-07 07:55:10 +0800254static struct attribute *atxp1_attrs[] = {
Jean Delvarea5ebe662006-09-24 21:24:46 +0200255 &dev_attr_gpio1.attr,
256 &dev_attr_gpio2.attr,
257 &dev_attr_cpu0_vid.attr,
258 NULL
259};
Axel Lin11f7e492014-06-07 07:55:10 +0800260ATTRIBUTE_GROUPS(atxp1);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200261
Jean Delvare71163c72008-07-16 19:30:11 +0200262/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100263static int atxp1_detect(struct i2c_client *new_client,
Jean Delvare71163c72008-07-16 19:30:11 +0200264 struct i2c_board_info *info)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200265{
Jean Delvare71163c72008-07-16 19:30:11 +0200266 struct i2c_adapter *adapter = new_client->adapter;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200267
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200268 u8 temp;
269
270 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare71163c72008-07-16 19:30:11 +0200271 return -ENODEV;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200272
273 /* Detect ATXP1, checking if vendor ID registers are all zero */
274 if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
275 (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
276 (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
Jean Delvare13b3c3f2008-09-20 10:25:19 +0200277 (i2c_smbus_read_byte_data(new_client, 0xff) == 0)))
278 return -ENODEV;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200279
Guenter Roeckf24d5482012-01-14 13:21:59 -0800280 /*
281 * No vendor ID, now checking if registers 0x10,0x11 (non-existent)
282 * showing the same as register 0x00
283 */
Jean Delvare13b3c3f2008-09-20 10:25:19 +0200284 temp = i2c_smbus_read_byte_data(new_client, 0x00);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200285
Jean Delvare13b3c3f2008-09-20 10:25:19 +0200286 if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
287 (i2c_smbus_read_byte_data(new_client, 0x11) == temp)))
288 return -ENODEV;
Jean Delvare71163c72008-07-16 19:30:11 +0200289
290 /* Get VRM */
291 temp = vid_which_vrm();
292
293 if ((temp != 90) && (temp != 91)) {
294 dev_err(&adapter->dev, "atxp1: Not supporting VRM %d.%d\n",
295 temp / 10, temp % 10);
296 return -ENODEV;
297 }
298
299 strlcpy(info->type, "atxp1", I2C_NAME_SIZE);
300
301 return 0;
302}
303
Axel Lin11f7e492014-06-07 07:55:10 +0800304static int atxp1_probe(struct i2c_client *client,
Jean Delvare71163c72008-07-16 19:30:11 +0200305 const struct i2c_device_id *id)
306{
Axel Lin11f7e492014-06-07 07:55:10 +0800307 struct device *dev = &client->dev;
Jean Delvare71163c72008-07-16 19:30:11 +0200308 struct atxp1_data *data;
Axel Lin11f7e492014-06-07 07:55:10 +0800309 struct device *hwmon_dev;
Jean Delvare71163c72008-07-16 19:30:11 +0200310
Axel Lin11f7e492014-06-07 07:55:10 +0800311 data = devm_kzalloc(dev, sizeof(struct atxp1_data), GFP_KERNEL);
Guenter Roeckd466a352012-06-02 09:58:03 -0700312 if (!data)
313 return -ENOMEM;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200314
315 /* Get VRM */
Jean Delvare303760b2005-07-31 21:52:01 +0200316 data->vrm = vid_which_vrm();
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200317
Axel Lin11f7e492014-06-07 07:55:10 +0800318 data->client = client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100319 mutex_init(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200320
Axel Lin11f7e492014-06-07 07:55:10 +0800321 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
322 data,
323 atxp1_groups);
324 if (IS_ERR(hwmon_dev))
325 return PTR_ERR(hwmon_dev);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200326
Axel Lin11f7e492014-06-07 07:55:10 +0800327 dev_info(dev, "Using VRM: %d.%d\n", data->vrm / 10, data->vrm % 10);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400328
Jean Delvare71163c72008-07-16 19:30:11 +0200329 return 0;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200330};
331
Axel Lin8dea1b42014-06-06 17:50:56 +0800332static const struct i2c_device_id atxp1_id[] = {
333 { "atxp1", 0 },
334 { }
335};
336MODULE_DEVICE_TABLE(i2c, atxp1_id);
337
338static struct i2c_driver atxp1_driver = {
339 .class = I2C_CLASS_HWMON,
340 .driver = {
341 .name = "atxp1",
342 },
343 .probe = atxp1_probe,
Axel Lin8dea1b42014-06-06 17:50:56 +0800344 .id_table = atxp1_id,
345 .detect = atxp1_detect,
346 .address_list = normal_i2c,
347};
348
Axel Linf0967ee2012-01-20 15:38:18 +0800349module_i2c_driver(atxp1_driver);