atxp1.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * atxp1.c - kernel module for setting CPU VID and general purpose
  4. * I/Os using the Attansic ATXP1 chip.
  5. *
  6. * 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.
  8. * See Documentation/i2c/instantiating-devices.rst for more information.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/i2c.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/hwmon-vid.h>
  17. #include <linux/err.h>
  18. #include <linux/mutex.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/slab.h>
  21. MODULE_LICENSE("GPL");
  22. MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
  23. MODULE_VERSION("0.6.3");
  24. MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
  25. #define ATXP1_VID 0x00
  26. #define ATXP1_CVID 0x01
  27. #define ATXP1_GPIO1 0x06
  28. #define ATXP1_GPIO2 0x0a
  29. #define ATXP1_VIDENA 0x20
  30. #define ATXP1_VIDMASK 0x1f
  31. #define ATXP1_GPIO1MASK 0x0f
  32. struct atxp1_data {
  33. struct i2c_client *client;
  34. struct mutex update_lock;
  35. unsigned long last_updated;
  36. u8 valid;
  37. struct {
  38. u8 vid; /* VID output register */
  39. u8 cpu_vid; /* VID input from CPU */
  40. u8 gpio1; /* General purpose I/O register 1 */
  41. u8 gpio2; /* General purpose I/O register 2 */
  42. } reg;
  43. u8 vrm; /* Detected CPU VRM */
  44. };
  45. static struct atxp1_data *atxp1_update_device(struct device *dev)
  46. {
  47. struct atxp1_data *data = dev_get_drvdata(dev);
  48. struct i2c_client *client = data->client;
  49. mutex_lock(&data->update_lock);
  50. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  51. /* Update local register data */
  52. data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
  53. data->reg.cpu_vid = i2c_smbus_read_byte_data(client,
  54. ATXP1_CVID);
  55. data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
  56. data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
  57. data->valid = 1;
  58. }
  59. mutex_unlock(&data->update_lock);
  60. return data;
  61. }
  62. /* sys file functions for cpu0_vid */
  63. static ssize_t cpu0_vid_show(struct device *dev,
  64. struct device_attribute *attr, char *buf)
  65. {
  66. int size;
  67. struct atxp1_data *data;
  68. data = atxp1_update_device(dev);
  69. size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK,
  70. data->vrm));
  71. return size;
  72. }
  73. static ssize_t cpu0_vid_store(struct device *dev,
  74. struct device_attribute *attr, const char *buf,
  75. size_t count)
  76. {
  77. struct atxp1_data *data = atxp1_update_device(dev);
  78. struct i2c_client *client = data->client;
  79. int vid, cvid;
  80. unsigned long vcore;
  81. int err;
  82. err = kstrtoul(buf, 10, &vcore);
  83. if (err)
  84. return err;
  85. vcore /= 25;
  86. vcore *= 25;
  87. /* Calculate VID */
  88. vid = vid_to_reg(vcore, data->vrm);
  89. if (vid < 0) {
  90. dev_err(dev, "VID calculation failed.\n");
  91. return vid;
  92. }
  93. /*
  94. * If output enabled, use control register value.
  95. * Otherwise original CPU VID
  96. */
  97. if (data->reg.vid & ATXP1_VIDENA)
  98. cvid = data->reg.vid & ATXP1_VIDMASK;
  99. else
  100. cvid = data->reg.cpu_vid;
  101. /* Nothing changed, aborting */
  102. if (vid == cvid)
  103. return count;
  104. dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", (int)vcore, vid);
  105. /* Write every 25 mV step to increase stability */
  106. if (cvid > vid) {
  107. for (; cvid >= vid; cvid--)
  108. i2c_smbus_write_byte_data(client,
  109. ATXP1_VID, cvid | ATXP1_VIDENA);
  110. } else {
  111. for (; cvid <= vid; cvid++)
  112. i2c_smbus_write_byte_data(client,
  113. ATXP1_VID, cvid | ATXP1_VIDENA);
  114. }
  115. data->valid = 0;
  116. return count;
  117. }
  118. /*
  119. * CPU core reference voltage
  120. * unit: millivolt
  121. */
  122. static DEVICE_ATTR_RW(cpu0_vid);
  123. /* sys file functions for GPIO1 */
  124. static ssize_t gpio1_show(struct device *dev, struct device_attribute *attr,
  125. char *buf)
  126. {
  127. int size;
  128. struct atxp1_data *data;
  129. data = atxp1_update_device(dev);
  130. size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
  131. return size;
  132. }
  133. static ssize_t gpio1_store(struct device *dev, struct device_attribute *attr,
  134. const char *buf, size_t count)
  135. {
  136. struct atxp1_data *data = atxp1_update_device(dev);
  137. struct i2c_client *client = data->client;
  138. unsigned long value;
  139. int err;
  140. err = kstrtoul(buf, 16, &value);
  141. if (err)
  142. return err;
  143. value &= ATXP1_GPIO1MASK;
  144. if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
  145. dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
  146. i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
  147. data->valid = 0;
  148. }
  149. return count;
  150. }
  151. /*
  152. * GPIO1 data register
  153. * unit: Four bit as hex (e.g. 0x0f)
  154. */
  155. static DEVICE_ATTR_RW(gpio1);
  156. /* sys file functions for GPIO2 */
  157. static ssize_t gpio2_show(struct device *dev, struct device_attribute *attr,
  158. char *buf)
  159. {
  160. int size;
  161. struct atxp1_data *data;
  162. data = atxp1_update_device(dev);
  163. size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
  164. return size;
  165. }
  166. static ssize_t gpio2_store(struct device *dev, struct device_attribute *attr,
  167. const char *buf, size_t count)
  168. {
  169. struct atxp1_data *data = atxp1_update_device(dev);
  170. struct i2c_client *client = data->client;
  171. unsigned long value;
  172. int err;
  173. err = kstrtoul(buf, 16, &value);
  174. if (err)
  175. return err;
  176. value &= 0xff;
  177. if (value != data->reg.gpio2) {
  178. dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
  179. i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
  180. data->valid = 0;
  181. }
  182. return count;
  183. }
  184. /*
  185. * GPIO2 data register
  186. * unit: Eight bit as hex (e.g. 0xff)
  187. */
  188. static DEVICE_ATTR_RW(gpio2);
  189. static struct attribute *atxp1_attrs[] = {
  190. &dev_attr_gpio1.attr,
  191. &dev_attr_gpio2.attr,
  192. &dev_attr_cpu0_vid.attr,
  193. NULL
  194. };
  195. ATTRIBUTE_GROUPS(atxp1);
  196. static int atxp1_probe(struct i2c_client *client)
  197. {
  198. struct device *dev = &client->dev;
  199. struct atxp1_data *data;
  200. struct device *hwmon_dev;
  201. data = devm_kzalloc(dev, sizeof(struct atxp1_data), GFP_KERNEL);
  202. if (!data)
  203. return -ENOMEM;
  204. /* Get VRM */
  205. data->vrm = vid_which_vrm();
  206. if (data->vrm != 90 && data->vrm != 91) {
  207. dev_err(dev, "atxp1: Not supporting VRM %d.%d\n",
  208. data->vrm / 10, data->vrm % 10);
  209. return -ENODEV;
  210. }
  211. data->client = client;
  212. mutex_init(&data->update_lock);
  213. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  214. data,
  215. atxp1_groups);
  216. if (IS_ERR(hwmon_dev))
  217. return PTR_ERR(hwmon_dev);
  218. dev_info(dev, "Using VRM: %d.%d\n", data->vrm / 10, data->vrm % 10);
  219. return 0;
  220. };
  221. static const struct i2c_device_id atxp1_id[] = {
  222. { "atxp1", 0 },
  223. { }
  224. };
  225. MODULE_DEVICE_TABLE(i2c, atxp1_id);
  226. static struct i2c_driver atxp1_driver = {
  227. .class = I2C_CLASS_HWMON,
  228. .driver = {
  229. .name = "atxp1",
  230. },
  231. .probe_new = atxp1_probe,
  232. .id_table = atxp1_id,
  233. };
  234. module_i2c_driver(atxp1_driver);