atxp1.c 8.3 KB

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