atxp1.c 7.1 KB

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