Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * atxp1.c - kernel module for setting CPU VID and general purpose
0004  *       I/Os using the Attansic ATXP1 chip.
0005  *
0006  * The ATXP1 can reside on I2C addresses 0x37 or 0x4e. The chip is
0007  * not auto-detected by the driver and must be instantiated explicitly.
0008  * See Documentation/i2c/instantiating-devices.rst for more information.
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/jiffies.h>
0015 #include <linux/i2c.h>
0016 #include <linux/hwmon.h>
0017 #include <linux/hwmon-vid.h>
0018 #include <linux/err.h>
0019 #include <linux/mutex.h>
0020 #include <linux/sysfs.h>
0021 #include <linux/slab.h>
0022 
0023 MODULE_LICENSE("GPL");
0024 MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
0025 MODULE_VERSION("0.6.3");
0026 MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
0027 
0028 #define ATXP1_VID   0x00
0029 #define ATXP1_CVID  0x01
0030 #define ATXP1_GPIO1 0x06
0031 #define ATXP1_GPIO2 0x0a
0032 #define ATXP1_VIDENA    0x20
0033 #define ATXP1_VIDMASK   0x1f
0034 #define ATXP1_GPIO1MASK 0x0f
0035 
0036 struct atxp1_data {
0037     struct i2c_client *client;
0038     struct mutex update_lock;
0039     unsigned long last_updated;
0040     bool valid;
0041     struct {
0042         u8 vid;     /* VID output register */
0043         u8 cpu_vid; /* VID input from CPU */
0044         u8 gpio1;   /* General purpose I/O register 1 */
0045         u8 gpio2;   /* General purpose I/O register 2 */
0046     } reg;
0047     u8 vrm;         /* Detected CPU VRM */
0048 };
0049 
0050 static struct atxp1_data *atxp1_update_device(struct device *dev)
0051 {
0052     struct atxp1_data *data = dev_get_drvdata(dev);
0053     struct i2c_client *client = data->client;
0054 
0055     mutex_lock(&data->update_lock);
0056 
0057     if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
0058 
0059         /* Update local register data */
0060         data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
0061         data->reg.cpu_vid = i2c_smbus_read_byte_data(client,
0062                                  ATXP1_CVID);
0063         data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
0064         data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
0065 
0066         data->valid = true;
0067     }
0068 
0069     mutex_unlock(&data->update_lock);
0070 
0071     return data;
0072 }
0073 
0074 /* sys file functions for cpu0_vid */
0075 static ssize_t cpu0_vid_show(struct device *dev,
0076                  struct device_attribute *attr, char *buf)
0077 {
0078     int size;
0079     struct atxp1_data *data;
0080 
0081     data = atxp1_update_device(dev);
0082 
0083     size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK,
0084                          data->vrm));
0085 
0086     return size;
0087 }
0088 
0089 static ssize_t cpu0_vid_store(struct device *dev,
0090                   struct device_attribute *attr, const char *buf,
0091                   size_t count)
0092 {
0093     struct atxp1_data *data = atxp1_update_device(dev);
0094     struct i2c_client *client = data->client;
0095     int vid, cvid;
0096     unsigned long vcore;
0097     int err;
0098 
0099     err = kstrtoul(buf, 10, &vcore);
0100     if (err)
0101         return err;
0102 
0103     vcore /= 25;
0104     vcore *= 25;
0105 
0106     /* Calculate VID */
0107     vid = vid_to_reg(vcore, data->vrm);
0108     if (vid < 0) {
0109         dev_err(dev, "VID calculation failed.\n");
0110         return vid;
0111     }
0112 
0113     /*
0114      * If output enabled, use control register value.
0115      * Otherwise original CPU VID
0116      */
0117     if (data->reg.vid & ATXP1_VIDENA)
0118         cvid = data->reg.vid & ATXP1_VIDMASK;
0119     else
0120         cvid = data->reg.cpu_vid;
0121 
0122     /* Nothing changed, aborting */
0123     if (vid == cvid)
0124         return count;
0125 
0126     dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", (int)vcore, vid);
0127 
0128     /* Write every 25 mV step to increase stability */
0129     if (cvid > vid) {
0130         for (; cvid >= vid; cvid--)
0131             i2c_smbus_write_byte_data(client,
0132                         ATXP1_VID, cvid | ATXP1_VIDENA);
0133     } else {
0134         for (; cvid <= vid; cvid++)
0135             i2c_smbus_write_byte_data(client,
0136                         ATXP1_VID, cvid | ATXP1_VIDENA);
0137     }
0138 
0139     data->valid = false;
0140 
0141     return count;
0142 }
0143 
0144 /*
0145  * CPU core reference voltage
0146  * unit: millivolt
0147  */
0148 static DEVICE_ATTR_RW(cpu0_vid);
0149 
0150 /* sys file functions for GPIO1 */
0151 static ssize_t gpio1_show(struct device *dev, struct device_attribute *attr,
0152               char *buf)
0153 {
0154     int size;
0155     struct atxp1_data *data;
0156 
0157     data = atxp1_update_device(dev);
0158 
0159     size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
0160 
0161     return size;
0162 }
0163 
0164 static ssize_t gpio1_store(struct device *dev, struct device_attribute *attr,
0165                const char *buf, size_t count)
0166 {
0167     struct atxp1_data *data = atxp1_update_device(dev);
0168     struct i2c_client *client = data->client;
0169     unsigned long value;
0170     int err;
0171 
0172     err = kstrtoul(buf, 16, &value);
0173     if (err)
0174         return err;
0175 
0176     value &= ATXP1_GPIO1MASK;
0177 
0178     if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
0179         dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
0180 
0181         i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
0182 
0183         data->valid = false;
0184     }
0185 
0186     return count;
0187 }
0188 
0189 /*
0190  * GPIO1 data register
0191  * unit: Four bit as hex (e.g. 0x0f)
0192  */
0193 static DEVICE_ATTR_RW(gpio1);
0194 
0195 /* sys file functions for GPIO2 */
0196 static ssize_t gpio2_show(struct device *dev, struct device_attribute *attr,
0197               char *buf)
0198 {
0199     int size;
0200     struct atxp1_data *data;
0201 
0202     data = atxp1_update_device(dev);
0203 
0204     size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
0205 
0206     return size;
0207 }
0208 
0209 static ssize_t gpio2_store(struct device *dev, struct device_attribute *attr,
0210                const char *buf, size_t count)
0211 {
0212     struct atxp1_data *data = atxp1_update_device(dev);
0213     struct i2c_client *client = data->client;
0214     unsigned long value;
0215     int err;
0216 
0217     err = kstrtoul(buf, 16, &value);
0218     if (err)
0219         return err;
0220     value &= 0xff;
0221 
0222     if (value != data->reg.gpio2) {
0223         dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
0224 
0225         i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
0226 
0227         data->valid = false;
0228     }
0229 
0230     return count;
0231 }
0232 
0233 /*
0234  * GPIO2 data register
0235  * unit: Eight bit as hex (e.g. 0xff)
0236  */
0237 static DEVICE_ATTR_RW(gpio2);
0238 
0239 static struct attribute *atxp1_attrs[] = {
0240     &dev_attr_gpio1.attr,
0241     &dev_attr_gpio2.attr,
0242     &dev_attr_cpu0_vid.attr,
0243     NULL
0244 };
0245 ATTRIBUTE_GROUPS(atxp1);
0246 
0247 static int atxp1_probe(struct i2c_client *client)
0248 {
0249     struct device *dev = &client->dev;
0250     struct atxp1_data *data;
0251     struct device *hwmon_dev;
0252 
0253     data = devm_kzalloc(dev, sizeof(struct atxp1_data), GFP_KERNEL);
0254     if (!data)
0255         return -ENOMEM;
0256 
0257     /* Get VRM */
0258     data->vrm = vid_which_vrm();
0259     if (data->vrm != 90 && data->vrm != 91) {
0260         dev_err(dev, "atxp1: Not supporting VRM %d.%d\n",
0261             data->vrm / 10, data->vrm % 10);
0262         return -ENODEV;
0263     }
0264 
0265     data->client = client;
0266     mutex_init(&data->update_lock);
0267 
0268     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0269                                data,
0270                                atxp1_groups);
0271     if (IS_ERR(hwmon_dev))
0272         return PTR_ERR(hwmon_dev);
0273 
0274     dev_info(dev, "Using VRM: %d.%d\n", data->vrm / 10, data->vrm % 10);
0275 
0276     return 0;
0277 };
0278 
0279 static const struct i2c_device_id atxp1_id[] = {
0280     { "atxp1", 0 },
0281     { }
0282 };
0283 MODULE_DEVICE_TABLE(i2c, atxp1_id);
0284 
0285 static struct i2c_driver atxp1_driver = {
0286     .class      = I2C_CLASS_HWMON,
0287     .driver = {
0288         .name   = "atxp1",
0289     },
0290     .probe_new  = atxp1_probe,
0291     .id_table   = atxp1_id,
0292 };
0293 
0294 module_i2c_driver(atxp1_driver);