blob: 1e08a5431f128183b29f4ae5eec3fd8302602468 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Sebastian Witt9cb7d182005-04-13 22:27:53 +02002/*
Guenter Roeckf24d5482012-01-14 13:21:59 -08003 * atxp1.c - kernel module for setting CPU VID and general purpose
4 * I/Os using the Attansic ATXP1 chip.
5 *
Guenter Roecke892b752015-05-27 16:17:19 -07006 * The ATXP1 can reside on I2C addresses 0x37 or 0x4e. The chip is
7 * not auto-detected by the driver and must be instantiated explicitly.
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03008 * See Documentation/i2c/instantiating-devices.rst for more information.
Guenter Roeckf24d5482012-01-14 13:21:59 -08009 */
Sebastian Witt9cb7d182005-04-13 22:27:53 +020010
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
Jean Delvare0cacdf22005-07-29 12:15:12 -070014#include <linux/jiffies.h>
Sebastian Witt9cb7d182005-04-13 22:27:53 +020015#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040016#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020017#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040018#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010019#include <linux/mutex.h>
Jean Delvarea5ebe662006-09-24 21:24:46 +020020#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Sebastian Witt9cb7d182005-04-13 22:27:53 +020022
23MODULE_LICENSE("GPL");
24MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
Jean Delvare13b3c3f2008-09-20 10:25:19 +020025MODULE_VERSION("0.6.3");
Sebastian Witt9cb7d182005-04-13 22:27:53 +020026MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
27
28#define ATXP1_VID 0x00
29#define ATXP1_CVID 0x01
30#define ATXP1_GPIO1 0x06
31#define ATXP1_GPIO2 0x0a
32#define ATXP1_VIDENA 0x20
33#define ATXP1_VIDMASK 0x1f
34#define ATXP1_GPIO1MASK 0x0f
35
Sebastian Witt9cb7d182005-04-13 22:27:53 +020036struct atxp1_data {
Axel Lin11f7e492014-06-07 07:55:10 +080037 struct i2c_client *client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010038 struct mutex update_lock;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020039 unsigned long last_updated;
40 u8 valid;
41 struct {
42 u8 vid; /* VID output register */
43 u8 cpu_vid; /* VID input from CPU */
44 u8 gpio1; /* General purpose I/O register 1 */
45 u8 gpio2; /* General purpose I/O register 2 */
46 } reg;
47 u8 vrm; /* Detected CPU VRM */
48};
49
Guenter Roeckf24d5482012-01-14 13:21:59 -080050static struct atxp1_data *atxp1_update_device(struct device *dev)
Sebastian Witt9cb7d182005-04-13 22:27:53 +020051{
Axel Lin11f7e492014-06-07 07:55:10 +080052 struct atxp1_data *data = dev_get_drvdata(dev);
53 struct i2c_client *client = data->client;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020054
Ingo Molnar9a61bf62006-01-18 23:19:26 +010055 mutex_lock(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020056
Jean Delvare0cacdf22005-07-29 12:15:12 -070057 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Sebastian Witt9cb7d182005-04-13 22:27:53 +020058
59 /* Update local register data */
60 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
Guenter Roeckf24d5482012-01-14 13:21:59 -080061 data->reg.cpu_vid = i2c_smbus_read_byte_data(client,
62 ATXP1_CVID);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020063 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
64 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
65
66 data->valid = 1;
67 }
68
Ingo Molnar9a61bf62006-01-18 23:19:26 +010069 mutex_unlock(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +020070
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +010071 return data;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020072}
73
74/* sys file functions for cpu0_vid */
Julia Lawall0acf2a52016-12-22 13:04:37 +010075static ssize_t cpu0_vid_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +020077{
78 int size;
79 struct atxp1_data *data;
80
81 data = atxp1_update_device(dev);
82
Guenter Roeckf24d5482012-01-14 13:21:59 -080083 size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK,
84 data->vrm));
Sebastian Witt9cb7d182005-04-13 22:27:53 +020085
86 return size;
87}
88
Julia Lawall0acf2a52016-12-22 13:04:37 +010089static ssize_t cpu0_vid_store(struct device *dev,
90 struct device_attribute *attr, const char *buf,
91 size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +020092{
Axel Lin11f7e492014-06-07 07:55:10 +080093 struct atxp1_data *data = atxp1_update_device(dev);
94 struct i2c_client *client = data->client;
Alexey Dobriyanc41bdb52006-08-28 14:18:14 +020095 int vid, cvid;
Guenter Roeckf24d5482012-01-14 13:21:59 -080096 unsigned long vcore;
97 int err;
Sebastian Witt9cb7d182005-04-13 22:27:53 +020098
Guenter Roeckf24d5482012-01-14 13:21:59 -080099 err = kstrtoul(buf, 10, &vcore);
100 if (err)
101 return err;
102
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200103 vcore /= 25;
104 vcore *= 25;
105
106 /* Calculate VID */
107 vid = vid_to_reg(vcore, data->vrm);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200108 if (vid < 0) {
109 dev_err(dev, "VID calculation failed.\n");
Guenter Roeck674d0ed2013-09-13 10:59:27 -0700110 return vid;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200111 }
112
Guenter Roeckf24d5482012-01-14 13:21:59 -0800113 /*
114 * If output enabled, use control register value.
115 * Otherwise original CPU VID
116 */
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200117 if (data->reg.vid & ATXP1_VIDENA)
118 cvid = data->reg.vid & ATXP1_VIDMASK;
119 else
120 cvid = data->reg.cpu_vid;
121
122 /* Nothing changed, aborting */
123 if (vid == cvid)
124 return count;
125
Guenter Roeckf24d5482012-01-14 13:21:59 -0800126 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", (int)vcore, vid);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200127
128 /* Write every 25 mV step to increase stability */
129 if (cvid > vid) {
Guenter Roeckf24d5482012-01-14 13:21:59 -0800130 for (; cvid >= vid; cvid--)
131 i2c_smbus_write_byte_data(client,
132 ATXP1_VID, cvid | ATXP1_VIDENA);
133 } else {
134 for (; cvid <= vid; cvid++)
135 i2c_smbus_write_byte_data(client,
136 ATXP1_VID, cvid | ATXP1_VIDENA);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200137 }
138
139 data->valid = 0;
140
141 return count;
142}
143
Guenter Roeckf24d5482012-01-14 13:21:59 -0800144/*
145 * CPU core reference voltage
146 * unit: millivolt
147 */
Julia Lawall0acf2a52016-12-22 13:04:37 +0100148static DEVICE_ATTR_RW(cpu0_vid);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200149
150/* sys file functions for GPIO1 */
Julia Lawall0acf2a52016-12-22 13:04:37 +0100151static ssize_t gpio1_show(struct device *dev, struct device_attribute *attr,
152 char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200153{
154 int size;
155 struct atxp1_data *data;
156
157 data = atxp1_update_device(dev);
158
159 size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
160
161 return size;
162}
163
Julia Lawall0acf2a52016-12-22 13:04:37 +0100164static ssize_t gpio1_store(struct device *dev, struct device_attribute *attr,
165 const char *buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200166{
Axel Lin11f7e492014-06-07 07:55:10 +0800167 struct atxp1_data *data = atxp1_update_device(dev);
168 struct i2c_client *client = data->client;
Guenter Roeckf24d5482012-01-14 13:21:59 -0800169 unsigned long value;
170 int err;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200171
Guenter Roeckf24d5482012-01-14 13:21:59 -0800172 err = kstrtoul(buf, 16, &value);
173 if (err)
174 return err;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200175
176 value &= ATXP1_GPIO1MASK;
177
178 if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
Guenter Roeckf24d5482012-01-14 13:21:59 -0800179 dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200180
181 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
182
183 data->valid = 0;
184 }
185
186 return count;
187}
188
Guenter Roeckf24d5482012-01-14 13:21:59 -0800189/*
190 * GPIO1 data register
191 * unit: Four bit as hex (e.g. 0x0f)
192 */
Julia Lawall0acf2a52016-12-22 13:04:37 +0100193static DEVICE_ATTR_RW(gpio1);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200194
195/* sys file functions for GPIO2 */
Julia Lawall0acf2a52016-12-22 13:04:37 +0100196static ssize_t gpio2_show(struct device *dev, struct device_attribute *attr,
197 char *buf)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200198{
199 int size;
200 struct atxp1_data *data;
201
202 data = atxp1_update_device(dev);
203
204 size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
205
206 return size;
207}
208
Julia Lawall0acf2a52016-12-22 13:04:37 +0100209static ssize_t gpio2_store(struct device *dev, struct device_attribute *attr,
210 const char *buf, size_t count)
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200211{
Guenter Roeckf24d5482012-01-14 13:21:59 -0800212 struct atxp1_data *data = atxp1_update_device(dev);
Axel Lin11f7e492014-06-07 07:55:10 +0800213 struct i2c_client *client = data->client;
Guenter Roeckf24d5482012-01-14 13:21:59 -0800214 unsigned long value;
215 int err;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200216
Guenter Roeckf24d5482012-01-14 13:21:59 -0800217 err = kstrtoul(buf, 16, &value);
218 if (err)
219 return err;
220 value &= 0xff;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200221
222 if (value != data->reg.gpio2) {
Guenter Roeckf24d5482012-01-14 13:21:59 -0800223 dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200224
225 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
226
227 data->valid = 0;
228 }
229
230 return count;
231}
232
Guenter Roeckf24d5482012-01-14 13:21:59 -0800233/*
234 * GPIO2 data register
235 * unit: Eight bit as hex (e.g. 0xff)
236 */
Julia Lawall0acf2a52016-12-22 13:04:37 +0100237static DEVICE_ATTR_RW(gpio2);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200238
Axel Lin11f7e492014-06-07 07:55:10 +0800239static struct attribute *atxp1_attrs[] = {
Jean Delvarea5ebe662006-09-24 21:24:46 +0200240 &dev_attr_gpio1.attr,
241 &dev_attr_gpio2.attr,
242 &dev_attr_cpu0_vid.attr,
243 NULL
244};
Axel Lin11f7e492014-06-07 07:55:10 +0800245ATTRIBUTE_GROUPS(atxp1);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200246
Stephen Kitt67487032020-08-13 18:02:22 +0200247static int atxp1_probe(struct i2c_client *client)
Jean Delvare71163c72008-07-16 19:30:11 +0200248{
Axel Lin11f7e492014-06-07 07:55:10 +0800249 struct device *dev = &client->dev;
Jean Delvare71163c72008-07-16 19:30:11 +0200250 struct atxp1_data *data;
Axel Lin11f7e492014-06-07 07:55:10 +0800251 struct device *hwmon_dev;
Jean Delvare71163c72008-07-16 19:30:11 +0200252
Axel Lin11f7e492014-06-07 07:55:10 +0800253 data = devm_kzalloc(dev, sizeof(struct atxp1_data), GFP_KERNEL);
Guenter Roeckd466a352012-06-02 09:58:03 -0700254 if (!data)
255 return -ENOMEM;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200256
257 /* Get VRM */
Jean Delvare303760b2005-07-31 21:52:01 +0200258 data->vrm = vid_which_vrm();
Guenter Roecke892b752015-05-27 16:17:19 -0700259 if (data->vrm != 90 && data->vrm != 91) {
260 dev_err(dev, "atxp1: Not supporting VRM %d.%d\n",
261 data->vrm / 10, data->vrm % 10);
262 return -ENODEV;
263 }
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200264
Axel Lin11f7e492014-06-07 07:55:10 +0800265 data->client = client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100266 mutex_init(&data->update_lock);
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200267
Axel Lin11f7e492014-06-07 07:55:10 +0800268 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
269 data,
270 atxp1_groups);
271 if (IS_ERR(hwmon_dev))
272 return PTR_ERR(hwmon_dev);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200273
Axel Lin11f7e492014-06-07 07:55:10 +0800274 dev_info(dev, "Using VRM: %d.%d\n", data->vrm / 10, data->vrm % 10);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400275
Jean Delvare71163c72008-07-16 19:30:11 +0200276 return 0;
Sebastian Witt9cb7d182005-04-13 22:27:53 +0200277};
278
Axel Lin8dea1b42014-06-06 17:50:56 +0800279static const struct i2c_device_id atxp1_id[] = {
280 { "atxp1", 0 },
281 { }
282};
283MODULE_DEVICE_TABLE(i2c, atxp1_id);
284
285static struct i2c_driver atxp1_driver = {
286 .class = I2C_CLASS_HWMON,
287 .driver = {
288 .name = "atxp1",
289 },
Stephen Kitt67487032020-08-13 18:02:22 +0200290 .probe_new = atxp1_probe,
Axel Lin8dea1b42014-06-06 17:50:56 +0800291 .id_table = atxp1_id,
Axel Lin8dea1b42014-06-06 17:50:56 +0800292};
293
Axel Linf0967ee2012-01-20 15:38:18 +0800294module_i2c_driver(atxp1_driver);