Merge tag 'stable/for-linus-3.16-rc5-tag' of git://git.kernel.org/pub/scm/linux/kerne...
[cascardo/linux.git] / drivers / hwmon / atxp1.c
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 #include <linux/sysfs.h>
31 #include <linux/slab.h>
32
33 MODULE_LICENSE("GPL");
34 MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
35 MODULE_VERSION("0.6.3");
36 MODULE_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
46 static const unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
47
48 struct atxp1_data {
49         struct device *hwmon_dev;
50         struct mutex update_lock;
51         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
62 static struct atxp1_data *atxp1_update_device(struct device *dev)
63 {
64         struct i2c_client *client;
65         struct atxp1_data *data;
66
67         client = to_i2c_client(dev);
68         data = i2c_get_clientdata(client);
69
70         mutex_lock(&data->update_lock);
71
72         if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
73
74                 /* Update local register data */
75                 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
76                 data->reg.cpu_vid = i2c_smbus_read_byte_data(client,
77                                                              ATXP1_CVID);
78                 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
79                 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
80
81                 data->valid = 1;
82         }
83
84         mutex_unlock(&data->update_lock);
85
86         return data;
87 }
88
89 /* sys file functions for cpu0_vid */
90 static ssize_t atxp1_showvcore(struct device *dev,
91                                struct device_attribute *attr, char *buf)
92 {
93         int size;
94         struct atxp1_data *data;
95
96         data = atxp1_update_device(dev);
97
98         size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK,
99                                                  data->vrm));
100
101         return size;
102 }
103
104 static ssize_t atxp1_storevcore(struct device *dev,
105                                 struct device_attribute *attr,
106                                 const char *buf, size_t count)
107 {
108         struct atxp1_data *data;
109         struct i2c_client *client;
110         int vid, cvid;
111         unsigned long vcore;
112         int err;
113
114         client = to_i2c_client(dev);
115         data = atxp1_update_device(dev);
116
117         err = kstrtoul(buf, 10, &vcore);
118         if (err)
119                 return err;
120
121         vcore /= 25;
122         vcore *= 25;
123
124         /* Calculate VID */
125         vid = vid_to_reg(vcore, data->vrm);
126         if (vid < 0) {
127                 dev_err(dev, "VID calculation failed.\n");
128                 return vid;
129         }
130
131         /*
132          * If output enabled, use control register value.
133          * Otherwise original CPU VID
134          */
135         if (data->reg.vid & ATXP1_VIDENA)
136                 cvid = data->reg.vid & ATXP1_VIDMASK;
137         else
138                 cvid = data->reg.cpu_vid;
139
140         /* Nothing changed, aborting */
141         if (vid == cvid)
142                 return count;
143
144         dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", (int)vcore, vid);
145
146         /* Write every 25 mV step to increase stability */
147         if (cvid > vid) {
148                 for (; cvid >= vid; cvid--)
149                         i2c_smbus_write_byte_data(client,
150                                                 ATXP1_VID, cvid | ATXP1_VIDENA);
151         } else {
152                 for (; cvid <= vid; cvid++)
153                         i2c_smbus_write_byte_data(client,
154                                                 ATXP1_VID, cvid | ATXP1_VIDENA);
155         }
156
157         data->valid = 0;
158
159         return count;
160 }
161
162 /*
163  * CPU core reference voltage
164  * unit: millivolt
165  */
166 static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore,
167                    atxp1_storevcore);
168
169 /* sys file functions for GPIO1 */
170 static ssize_t atxp1_showgpio1(struct device *dev,
171                                struct device_attribute *attr, char *buf)
172 {
173         int size;
174         struct atxp1_data *data;
175
176         data = atxp1_update_device(dev);
177
178         size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
179
180         return size;
181 }
182
183 static ssize_t atxp1_storegpio1(struct device *dev,
184                                 struct device_attribute *attr, const char *buf,
185                                 size_t count)
186 {
187         struct atxp1_data *data;
188         struct i2c_client *client;
189         unsigned long value;
190         int err;
191
192         client = to_i2c_client(dev);
193         data = atxp1_update_device(dev);
194
195         err = kstrtoul(buf, 16, &value);
196         if (err)
197                 return err;
198
199         value &= ATXP1_GPIO1MASK;
200
201         if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
202                 dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
203
204                 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
205
206                 data->valid = 0;
207         }
208
209         return count;
210 }
211
212 /*
213  * GPIO1 data register
214  * unit: Four bit as hex (e.g. 0x0f)
215  */
216 static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
217
218 /* sys file functions for GPIO2 */
219 static ssize_t atxp1_showgpio2(struct device *dev,
220                                struct device_attribute *attr, char *buf)
221 {
222         int size;
223         struct atxp1_data *data;
224
225         data = atxp1_update_device(dev);
226
227         size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
228
229         return size;
230 }
231
232 static ssize_t atxp1_storegpio2(struct device *dev,
233                                 struct device_attribute *attr,
234                                 const char *buf, size_t count)
235 {
236         struct atxp1_data *data = atxp1_update_device(dev);
237         struct i2c_client *client = to_i2c_client(dev);
238         unsigned long value;
239         int err;
240
241         err = kstrtoul(buf, 16, &value);
242         if (err)
243                 return err;
244         value &= 0xff;
245
246         if (value != data->reg.gpio2) {
247                 dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
248
249                 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
250
251                 data->valid = 0;
252         }
253
254         return count;
255 }
256
257 /*
258  * GPIO2 data register
259  * unit: Eight bit as hex (e.g. 0xff)
260  */
261 static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
262
263 static struct attribute *atxp1_attributes[] = {
264         &dev_attr_gpio1.attr,
265         &dev_attr_gpio2.attr,
266         &dev_attr_cpu0_vid.attr,
267         NULL
268 };
269
270 static const struct attribute_group atxp1_group = {
271         .attrs = atxp1_attributes,
272 };
273
274
275 /* Return 0 if detection is successful, -ENODEV otherwise */
276 static int atxp1_detect(struct i2c_client *new_client,
277                         struct i2c_board_info *info)
278 {
279         struct i2c_adapter *adapter = new_client->adapter;
280
281         u8 temp;
282
283         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
284                 return -ENODEV;
285
286         /* Detect ATXP1, checking if vendor ID registers are all zero */
287         if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
288              (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
289              (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
290              (i2c_smbus_read_byte_data(new_client, 0xff) == 0)))
291                 return -ENODEV;
292
293         /*
294          * No vendor ID, now checking if registers 0x10,0x11 (non-existent)
295          * showing the same as register 0x00
296          */
297         temp = i2c_smbus_read_byte_data(new_client, 0x00);
298
299         if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
300               (i2c_smbus_read_byte_data(new_client, 0x11) == temp)))
301                 return -ENODEV;
302
303         /* Get VRM */
304         temp = vid_which_vrm();
305
306         if ((temp != 90) && (temp != 91)) {
307                 dev_err(&adapter->dev, "atxp1: Not supporting VRM %d.%d\n",
308                                 temp / 10, temp % 10);
309                 return -ENODEV;
310         }
311
312         strlcpy(info->type, "atxp1", I2C_NAME_SIZE);
313
314         return 0;
315 }
316
317 static int atxp1_probe(struct i2c_client *new_client,
318                        const struct i2c_device_id *id)
319 {
320         struct atxp1_data *data;
321         int err;
322
323         data = devm_kzalloc(&new_client->dev, sizeof(struct atxp1_data),
324                             GFP_KERNEL);
325         if (!data)
326                 return -ENOMEM;
327
328         /* Get VRM */
329         data->vrm = vid_which_vrm();
330
331         i2c_set_clientdata(new_client, data);
332         mutex_init(&data->update_lock);
333
334         /* Register sysfs hooks */
335         err = sysfs_create_group(&new_client->dev.kobj, &atxp1_group);
336         if (err)
337                 return err;
338
339         data->hwmon_dev = hwmon_device_register(&new_client->dev);
340         if (IS_ERR(data->hwmon_dev)) {
341                 err = PTR_ERR(data->hwmon_dev);
342                 goto exit_remove_files;
343         }
344
345         dev_info(&new_client->dev, "Using VRM: %d.%d\n",
346                          data->vrm / 10, data->vrm % 10);
347
348         return 0;
349
350 exit_remove_files:
351         sysfs_remove_group(&new_client->dev.kobj, &atxp1_group);
352         return err;
353 };
354
355 static int atxp1_remove(struct i2c_client *client)
356 {
357         struct atxp1_data *data = i2c_get_clientdata(client);
358
359         hwmon_device_unregister(data->hwmon_dev);
360         sysfs_remove_group(&client->dev.kobj, &atxp1_group);
361
362         return 0;
363 };
364
365 static const struct i2c_device_id atxp1_id[] = {
366         { "atxp1", 0 },
367         { }
368 };
369 MODULE_DEVICE_TABLE(i2c, atxp1_id);
370
371 static struct i2c_driver atxp1_driver = {
372         .class          = I2C_CLASS_HWMON,
373         .driver = {
374                 .name   = "atxp1",
375         },
376         .probe          = atxp1_probe,
377         .remove         = atxp1_remove,
378         .id_table       = atxp1_id,
379         .detect         = atxp1_detect,
380         .address_list   = normal_i2c,
381 };
382
383 module_i2c_driver(atxp1_driver);