hwmon: (atxp1) Drop FSF mailing address
[deliverable/linux.git] / drivers / hwmon / atxp1.c
CommitLineData
9cb7d184 1/*
f24d548b
GR
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.
f24d548b 14 */
9cb7d184
SW
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/module.h>
0cacdf29 19#include <linux/jiffies.h>
9cb7d184 20#include <linux/i2c.h>
943b0830 21#include <linux/hwmon.h>
303760b4 22#include <linux/hwmon-vid.h>
943b0830 23#include <linux/err.h>
9a61bf63 24#include <linux/mutex.h>
a5ebe668 25#include <linux/sysfs.h>
5a0e3ad6 26#include <linux/slab.h>
9cb7d184
SW
27
28MODULE_LICENSE("GPL");
29MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
13b3c3fa 30MODULE_VERSION("0.6.3");
9cb7d184
SW
31MODULE_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
25e9c86d 41static const unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
9cb7d184 42
9cb7d184 43struct atxp1_data {
11f7e494 44 struct i2c_client *client;
9a61bf63 45 struct mutex update_lock;
9cb7d184
SW
46 unsigned long last_updated;
47 u8 valid;
48 struct {
49 u8 vid; /* VID output register */
50 u8 cpu_vid; /* VID input from CPU */
51 u8 gpio1; /* General purpose I/O register 1 */
52 u8 gpio2; /* General purpose I/O register 2 */
53 } reg;
54 u8 vrm; /* Detected CPU VRM */
55};
56
f24d548b 57static struct atxp1_data *atxp1_update_device(struct device *dev)
9cb7d184 58{
11f7e494
AL
59 struct atxp1_data *data = dev_get_drvdata(dev);
60 struct i2c_client *client = data->client;
9cb7d184 61
9a61bf63 62 mutex_lock(&data->update_lock);
9cb7d184 63
0cacdf29 64 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
9cb7d184
SW
65
66 /* Update local register data */
67 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
f24d548b
GR
68 data->reg.cpu_vid = i2c_smbus_read_byte_data(client,
69 ATXP1_CVID);
9cb7d184
SW
70 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
71 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
72
73 data->valid = 1;
74 }
75
9a61bf63 76 mutex_unlock(&data->update_lock);
9cb7d184 77
7fe83ad8 78 return data;
9cb7d184
SW
79}
80
81/* sys file functions for cpu0_vid */
f24d548b
GR
82static ssize_t atxp1_showvcore(struct device *dev,
83 struct device_attribute *attr, char *buf)
9cb7d184
SW
84{
85 int size;
86 struct atxp1_data *data;
87
88 data = atxp1_update_device(dev);
89
f24d548b
GR
90 size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK,
91 data->vrm));
9cb7d184
SW
92
93 return size;
94}
95
f24d548b
GR
96static ssize_t atxp1_storevcore(struct device *dev,
97 struct device_attribute *attr,
98 const char *buf, size_t count)
9cb7d184 99{
11f7e494
AL
100 struct atxp1_data *data = atxp1_update_device(dev);
101 struct i2c_client *client = data->client;
c41bdb52 102 int vid, cvid;
f24d548b
GR
103 unsigned long vcore;
104 int err;
9cb7d184 105
f24d548b
GR
106 err = kstrtoul(buf, 10, &vcore);
107 if (err)
108 return err;
109
9cb7d184
SW
110 vcore /= 25;
111 vcore *= 25;
112
113 /* Calculate VID */
114 vid = vid_to_reg(vcore, data->vrm);
9cb7d184
SW
115 if (vid < 0) {
116 dev_err(dev, "VID calculation failed.\n");
674d0ed8 117 return vid;
9cb7d184
SW
118 }
119
f24d548b
GR
120 /*
121 * If output enabled, use control register value.
122 * Otherwise original CPU VID
123 */
9cb7d184
SW
124 if (data->reg.vid & ATXP1_VIDENA)
125 cvid = data->reg.vid & ATXP1_VIDMASK;
126 else
127 cvid = data->reg.cpu_vid;
128
129 /* Nothing changed, aborting */
130 if (vid == cvid)
131 return count;
132
f24d548b 133 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", (int)vcore, vid);
9cb7d184
SW
134
135 /* Write every 25 mV step to increase stability */
136 if (cvid > vid) {
f24d548b
GR
137 for (; cvid >= vid; cvid--)
138 i2c_smbus_write_byte_data(client,
139 ATXP1_VID, cvid | ATXP1_VIDENA);
140 } else {
141 for (; cvid <= vid; cvid++)
142 i2c_smbus_write_byte_data(client,
143 ATXP1_VID, cvid | ATXP1_VIDENA);
9cb7d184
SW
144 }
145
146 data->valid = 0;
147
148 return count;
149}
150
f24d548b
GR
151/*
152 * CPU core reference voltage
153 * unit: millivolt
154 */
155static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore,
156 atxp1_storevcore);
9cb7d184
SW
157
158/* sys file functions for GPIO1 */
f24d548b
GR
159static ssize_t atxp1_showgpio1(struct device *dev,
160 struct device_attribute *attr, char *buf)
9cb7d184
SW
161{
162 int size;
163 struct atxp1_data *data;
164
165 data = atxp1_update_device(dev);
166
167 size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
168
169 return size;
170}
171
f24d548b
GR
172static ssize_t atxp1_storegpio1(struct device *dev,
173 struct device_attribute *attr, const char *buf,
174 size_t count)
9cb7d184 175{
11f7e494
AL
176 struct atxp1_data *data = atxp1_update_device(dev);
177 struct i2c_client *client = data->client;
f24d548b
GR
178 unsigned long value;
179 int err;
9cb7d184 180
f24d548b
GR
181 err = kstrtoul(buf, 16, &value);
182 if (err)
183 return err;
9cb7d184
SW
184
185 value &= ATXP1_GPIO1MASK;
186
187 if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
f24d548b 188 dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
9cb7d184
SW
189
190 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
191
192 data->valid = 0;
193 }
194
195 return count;
196}
197
f24d548b
GR
198/*
199 * GPIO1 data register
200 * unit: Four bit as hex (e.g. 0x0f)
201 */
9cb7d184
SW
202static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
203
204/* sys file functions for GPIO2 */
f24d548b
GR
205static ssize_t atxp1_showgpio2(struct device *dev,
206 struct device_attribute *attr, char *buf)
9cb7d184
SW
207{
208 int size;
209 struct atxp1_data *data;
210
211 data = atxp1_update_device(dev);
212
213 size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
214
215 return size;
216}
217
f24d548b
GR
218static ssize_t atxp1_storegpio2(struct device *dev,
219 struct device_attribute *attr,
220 const char *buf, size_t count)
9cb7d184 221{
f24d548b 222 struct atxp1_data *data = atxp1_update_device(dev);
11f7e494 223 struct i2c_client *client = data->client;
f24d548b
GR
224 unsigned long value;
225 int err;
9cb7d184 226
f24d548b
GR
227 err = kstrtoul(buf, 16, &value);
228 if (err)
229 return err;
230 value &= 0xff;
9cb7d184
SW
231
232 if (value != data->reg.gpio2) {
f24d548b 233 dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
9cb7d184
SW
234
235 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
236
237 data->valid = 0;
238 }
239
240 return count;
241}
242
f24d548b
GR
243/*
244 * GPIO2 data register
245 * unit: Eight bit as hex (e.g. 0xff)
246 */
9cb7d184
SW
247static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
248
11f7e494 249static struct attribute *atxp1_attrs[] = {
a5ebe668
JD
250 &dev_attr_gpio1.attr,
251 &dev_attr_gpio2.attr,
252 &dev_attr_cpu0_vid.attr,
253 NULL
254};
11f7e494 255ATTRIBUTE_GROUPS(atxp1);
9cb7d184 256
71163c7c 257/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 258static int atxp1_detect(struct i2c_client *new_client,
71163c7c 259 struct i2c_board_info *info)
9cb7d184 260{
71163c7c 261 struct i2c_adapter *adapter = new_client->adapter;
9cb7d184 262
9cb7d184
SW
263 u8 temp;
264
265 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
71163c7c 266 return -ENODEV;
9cb7d184
SW
267
268 /* Detect ATXP1, checking if vendor ID registers are all zero */
269 if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
270 (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
271 (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
13b3c3fa
JD
272 (i2c_smbus_read_byte_data(new_client, 0xff) == 0)))
273 return -ENODEV;
9cb7d184 274
f24d548b
GR
275 /*
276 * No vendor ID, now checking if registers 0x10,0x11 (non-existent)
277 * showing the same as register 0x00
278 */
13b3c3fa 279 temp = i2c_smbus_read_byte_data(new_client, 0x00);
9cb7d184 280
13b3c3fa
JD
281 if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
282 (i2c_smbus_read_byte_data(new_client, 0x11) == temp)))
283 return -ENODEV;
9cb7d184
SW
284
285 /* Get VRM */
71163c7c 286 temp = vid_which_vrm();
9cb7d184 287
71163c7c
JD
288 if ((temp != 90) && (temp != 91)) {
289 dev_err(&adapter->dev, "atxp1: Not supporting VRM %d.%d\n",
290 temp / 10, temp % 10);
291 return -ENODEV;
9cb7d184
SW
292 }
293
71163c7c 294 strlcpy(info->type, "atxp1", I2C_NAME_SIZE);
9cb7d184 295
71163c7c
JD
296 return 0;
297}
9cb7d184 298
11f7e494 299static int atxp1_probe(struct i2c_client *client,
71163c7c
JD
300 const struct i2c_device_id *id)
301{
11f7e494 302 struct device *dev = &client->dev;
71163c7c 303 struct atxp1_data *data;
11f7e494 304 struct device *hwmon_dev;
9cb7d184 305
11f7e494 306 data = devm_kzalloc(dev, sizeof(struct atxp1_data), GFP_KERNEL);
d466a353
GR
307 if (!data)
308 return -ENOMEM;
9cb7d184 309
71163c7c
JD
310 /* Get VRM */
311 data->vrm = vid_which_vrm();
312
11f7e494 313 data->client = client;
71163c7c
JD
314 mutex_init(&data->update_lock);
315
11f7e494
AL
316 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
317 data,
318 atxp1_groups);
319 if (IS_ERR(hwmon_dev))
320 return PTR_ERR(hwmon_dev);
9cb7d184 321
11f7e494 322 dev_info(dev, "Using VRM: %d.%d\n", data->vrm / 10, data->vrm % 10);
943b0830 323
71163c7c 324 return 0;
9cb7d184
SW
325};
326
8dea1b4e
AL
327static const struct i2c_device_id atxp1_id[] = {
328 { "atxp1", 0 },
329 { }
330};
331MODULE_DEVICE_TABLE(i2c, atxp1_id);
332
333static struct i2c_driver atxp1_driver = {
334 .class = I2C_CLASS_HWMON,
335 .driver = {
336 .name = "atxp1",
337 },
338 .probe = atxp1_probe,
8dea1b4e
AL
339 .id_table = atxp1_id,
340 .detect = atxp1_detect,
341 .address_list = normal_i2c,
342};
343
f0967eea 344module_i2c_driver(atxp1_driver);
This page took 1.02172 seconds and 5 git commands to generate.